]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/socketvar.h
xnu-792.6.76.tar.gz
[apple/xnu.git] / bsd / sys / socketvar.h
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
37839358
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
23/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24/*-
25 * Copyright (c) 1982, 1986, 1990, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 *
9bccf70c
A
56 * @(#)socketvar.h 8.3 (Berkeley) 2/19/95
57 * $FreeBSD: src/sys/sys/socketvar.h,v 1.46.2.6 2001/08/31 13:45:49 jlemon Exp $
1c79356b
A
58 */
59
9bccf70c 60#ifndef _SYS_SOCKETVAR_H_
1c79356b
A
61#define _SYS_SOCKETVAR_H_
62
9bccf70c
A
63#include <sys/appleapiopts.h>
64#include <sys/queue.h> /* for TAILQ macros */
1c79356b 65#include <sys/select.h> /* for struct selinfo */
1c79356b
A
66#include <net/kext_net.h>
67#include <sys/ev.h>
91447636
A
68#include <sys/cdefs.h>
69
1c79356b
A
70/*
71 * Hacks to get around compiler complaints
72 */
73struct mbuf;
91447636 74struct socket_filter_entry;
1c79356b
A
75struct protosw;
76struct sockif;
77struct sockutil;
78
91447636 79#ifdef KERNEL_PRIVATE
1c79356b
A
80/* strings for sleep message: */
81extern char netio[], netcon[], netcls[];
82#define SOCKET_CACHE_ON
83#define SO_CACHE_FLUSH_INTERVAL 1 /* Seconds */
84#define SO_CACHE_TIME_LIMIT (120/SO_CACHE_FLUSH_INTERVAL) /* Seconds */
85#define SO_CACHE_MAX_FREE_BATCH 50
86#define MAX_CACHED_SOCKETS 60000
87#define TEMPDEBUG 0
88
89/*
90 * Kernel structure per socket.
91 * Contains send and receive buffer queues,
92 * handle on protocol and pointer to protocol
93 * private data and error information.
94 */
91447636
A
95#endif /* KERNEL_PRIVATE */
96
1c79356b
A
97typedef u_quad_t so_gen_t;
98
91447636 99#ifdef KERNEL_PRIVATE
9bccf70c
A
100#ifndef __APPLE__
101/* We don't support BSD style socket filters */
102struct accept_filter;
103#endif
104
1c79356b 105struct socket {
9bccf70c
A
106 int so_zone; /* zone we were allocated from */
107 short so_type; /* generic type, see socket.h */
1c79356b
A
108 short so_options; /* from socket call, see socket.h */
109 short so_linger; /* time to linger while closing */
9bccf70c 110 short so_state; /* internal state flags SS_*, below */
1c79356b
A
111 caddr_t so_pcb; /* protocol control block */
112 struct protosw *so_proto; /* protocol handle */
113/*
9bccf70c 114 * Variables for connection queuing.
1c79356b
A
115 * Socket where accepts occur is so_head in all subsidiary sockets.
116 * If so_head is 0, socket is not related to an accept.
9bccf70c
A
117 * For head socket so_incomp queues partially completed connections,
118 * while so_comp is a queue of connections ready to be accepted.
1c79356b 119 * If a connection is aborted and it has so_head set, then
9bccf70c 120 * it has to be pulled out of either so_incomp or so_comp.
1c79356b
A
121 * We allow connections to queue up based on current queue lengths
122 * and limit on number of queued connections for this socket.
123 */
124 struct socket *so_head; /* back pointer to accept socket */
125 TAILQ_HEAD(, socket) so_incomp; /* queue of partial unaccepted connections */
126 TAILQ_HEAD(, socket) so_comp; /* queue of complete unaccepted connections */
127 TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */
128 short so_qlen; /* number of unaccepted connections */
129 short so_incqlen; /* number of unaccepted incomplete
130 connections */
131 short so_qlimit; /* max number queued connections */
132 short so_timeo; /* connection timeout */
133 u_short so_error; /* error affecting connection */
134 pid_t so_pgid; /* pgid for signals */
135 u_long so_oobmark; /* chars to oob mark */
9bccf70c
A
136#ifndef __APPLE__
137 /* We don't support AIO ops */
138 TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
139#endif
1c79356b
A
140/*
141 * Variables for socket buffering.
142 */
143 struct sockbuf {
144 u_long sb_cc; /* actual chars in buffer */
145 u_long sb_hiwat; /* max actual char count */
146 u_long sb_mbcnt; /* chars of mbufs used */
147 u_long sb_mbmax; /* max chars of mbufs to use */
148 long sb_lowat; /* low water mark */
149 struct mbuf *sb_mb; /* the mbuf chain */
9bccf70c
A
150#if __APPLE__
151 struct socket *sb_so; /* socket back ptr for kexts */
152#endif
1c79356b
A
153 struct selinfo sb_sel; /* process selecting read/write */
154 short sb_flags; /* flags, see below */
91447636
A
155 struct timeval sb_timeo; /* timeout for read/write */
156 void *reserved1; /* for future use if needed */
157 void *reserved2;
1c79356b
A
158 } so_rcv, so_snd;
159#define SB_MAX (256*1024) /* default for max chars in sockbuf */
160#define SB_LOCK 0x01 /* lock on data queue */
161#define SB_WANT 0x02 /* someone is waiting to lock */
162#define SB_WAIT 0x04 /* someone is waiting for data/space */
0b4e3aa0 163#define SB_SEL 0x08 /* someone is selecting */
9bccf70c
A
164#define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
165#define SB_UPCALL 0x20 /* someone wants an upcall */
166#define SB_NOINTR 0x40 /* operations not interruptible */
55e303ae 167#define SB_KNOTE 0x100 /* kernel note attached */
9bccf70c
A
168#ifndef __APPLE__
169#define SB_AIO 0x80 /* AIO operations queued */
9bccf70c 170#else
0b4e3aa0 171#define SB_NOTIFY (SB_WAIT|SB_SEL|SB_ASYNC)
1c79356b
A
172#define SB_RECV 0x8000 /* this is rcv sb */
173
9bccf70c
A
174 caddr_t so_tpcb; /* Wisc. protocol control block - XXX unused? */
175#endif
176
91447636 177 void (*so_upcall)(struct socket *so, caddr_t arg, int waitf);
1c79356b
A
178 caddr_t so_upcallarg; /* Arg for above */
179 uid_t so_uid; /* who opened the socket */
180 /* NB: generation count must not be first; easiest to make it last. */
181 so_gen_t so_gencnt; /* generation count */
9bccf70c
A
182#ifndef __APPLE__
183 void *so_emuldata; /* private data for emulators */
184 struct so_accf {
185 struct accept_filter *so_accept_filter;
186 void *so_accept_filter_arg; /* saved filter args */
187 char *so_accept_filter_str; /* saved user args */
188 } *so_accf;
189#else
1c79356b
A
190 TAILQ_HEAD(,eventqelt) so_evlist;
191 int cached_in_sock_layer; /* Is socket bundled with pcb/pcb.inp_ppcb? */
192 struct socket *cache_next;
193 struct socket *cache_prev;
194 u_long cache_timestamp;
195 caddr_t so_saved_pcb; /* Saved pcb when cacheing */
196 struct mbuf *so_temp; /* Holding area for outbound frags */
197 /* Plug-in support - make the socket interface overridable */
198 struct mbuf *so_tail;
91447636 199 struct socket_filter_entry *so_filt; /* NKE hook */
9bccf70c 200 u_long so_flags; /* Flags */
91447636
A
201#define SOF_NOSIGPIPE 0x00000001
202#define SOF_NOADDRAVAIL 0x00000002 /* returns EADDRNOTAVAIL if src address is gone */
203#define SOF_PCBCLEARING 0x00000004 /* pru_disconnect done, no need to call pru_detach */
204 int so_usecount; /* refcounting of socket use */;
205 int so_retaincnt;
206 u_int32_t so_filteruse; /* usecount for the socket filters */
207 void *reserved3; /* Temporarily in use/debug: last socket lock LR */
208 void *reserved4; /* Temporarily in use/debug: last socket unlock LR */
3a60a9f5 209 thread_t so_send_filt_thread;
9bccf70c 210#endif
1c79356b 211};
91447636 212#endif /* KERNEL_PRIVATE */
1c79356b
A
213
214/*
215 * Socket state bits.
216 */
217#define SS_NOFDREF 0x001 /* no file table ref any more */
218#define SS_ISCONNECTED 0x002 /* socket connected to a peer */
219#define SS_ISCONNECTING 0x004 /* in process of connecting to peer */
220#define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */
221#define SS_CANTSENDMORE 0x010 /* can't send more data to peer */
222#define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */
223#define SS_RCVATMARK 0x040 /* at mark on input */
224
225#define SS_PRIV 0x080 /* privileged for broadcast, raw... */
226#define SS_NBIO 0x100 /* non-blocking ops */
227#define SS_ASYNC 0x200 /* async i/o notify */
228#define SS_ISCONFIRMING 0x400 /* deciding to accept connection req */
229#define SS_INCOMP 0x800 /* Unaccepted, incomplete connection */
230#define SS_COMP 0x1000 /* unaccepted, complete connection */
9bccf70c 231#define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */
91447636 232#define SS_DRAINING 0x4000 /* close waiting for blocked system calls to drain */
1c79356b
A
233
234/*
235 * Externalized form of struct socket used by the sysctl(3) interface.
236 */
237struct xsocket {
238 size_t xso_len; /* length of this structure */
239 struct socket *xso_so; /* makes a convenient handle sometimes */
240 short so_type;
241 short so_options;
242 short so_linger;
243 short so_state;
244 caddr_t so_pcb; /* another convenient handle */
245 int xso_protocol;
246 int xso_family;
247 short so_qlen;
248 short so_incqlen;
249 short so_qlimit;
250 short so_timeo;
251 u_short so_error;
252 pid_t so_pgid;
253 u_long so_oobmark;
254 struct xsockbuf {
255 u_long sb_cc;
256 u_long sb_hiwat;
257 u_long sb_mbcnt;
258 u_long sb_mbmax;
259 long sb_lowat;
260 short sb_flags;
261 short sb_timeo;
262 } so_rcv, so_snd;
263 uid_t so_uid; /* XXX */
264};
265
91447636 266#ifdef KERNEL_PRIVATE
1c79356b
A
267/*
268 * Macros for sockets and socket buffering.
269 */
91447636 270
1c79356b
A
271#define sbtoso(sb) (sb->sb_so)
272
9bccf70c
A
273/*
274 * Functions for sockets and socket buffering.
275 * These are macros on FreeBSD. On Darwin the
276 * implementation is in bsd/kern/uipc_socket2.c
277 */
91447636
A
278
279__BEGIN_DECLS
280int sb_notify(struct sockbuf *sb);
281long sbspace(struct sockbuf *sb);
282int sosendallatonce(struct socket *so);
283int soreadable(struct socket *so);
284int sowriteable(struct socket *so);
285void sballoc(struct sockbuf *sb, struct mbuf *m);
286void sbfree(struct sockbuf *sb, struct mbuf *m);
287int sblock(struct sockbuf *sb, int wf);
288void sbunlock(struct sockbuf *sb, int locked);
289void sorwakeup(struct socket * so);
290void sowwakeup(struct socket * so);
291__END_DECLS
1c79356b
A
292
293/*
294 * Socket extension mechanism: control block hooks:
295 * This is the "head" of any control block for an extenstion
296 * Note: we separate intercept function dispatch vectors from
297 * the NFDescriptor to permit selective replacement during
298 * operation, e.g., to disable some functions.
299 */
300struct kextcb
301{ struct kextcb *e_next; /* Next kext control block */
302 void *e_fcb; /* Real filter control block */
303 struct NFDescriptor *e_nfd; /* NKE Descriptor */
304 /* Plug-in support - intercept functions */
305 struct sockif *e_soif; /* Socket functions */
306 struct sockutil *e_sout; /* Sockbuf utility functions */
307};
308#define EXT_NULL 0x0 /* STATE: Not in use */
309#define sotokextcb(so) (so ? so->so_ext : 0)
310
311#ifdef KERNEL
9bccf70c 312
91447636
A
313#define SO_FILT_HINT_LOCKED 0x1
314
1c79356b
A
315/*
316 * Argument structure for sosetopt et seq. This is in the KERNEL
317 * section because it will never be visible to user code.
318 */
319enum sopt_dir { SOPT_GET, SOPT_SET };
320struct sockopt {
321 enum sopt_dir sopt_dir; /* is this a get or a set? */
322 int sopt_level; /* second arg of [gs]etsockopt */
323 int sopt_name; /* third arg of [gs]etsockopt */
91447636 324 user_addr_t sopt_val; /* fourth arg of [gs]etsockopt */
1c79356b
A
325 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */
326 struct proc *sopt_p; /* calling process or null if kernel */
327};
328
329#if SENDFILE
1c79356b
A
330struct sf_buf {
331 SLIST_ENTRY(sf_buf) free_list; /* list of free buffer slots */
332 int refcnt; /* reference count */
333 struct vm_page *m; /* currently mapped page */
334 vm_offset_t kva; /* va of mapping */
335};
1c79356b
A
336#endif
337
338#ifdef MALLOC_DECLARE
339MALLOC_DECLARE(M_PCB);
340MALLOC_DECLARE(M_SONAME);
341#endif
342
343extern int maxsockets;
344extern u_long sb_max;
345extern int socket_zone;
346extern so_gen_t so_gencnt;
347
348struct file;
349struct filedesc;
350struct mbuf;
351struct sockaddr;
352struct stat;
353struct ucred;
354struct uio;
9bccf70c 355struct knote;
1c79356b 356
1c79356b
A
357/*
358 * From uipc_socket and friends
359 */
91447636
A
360__BEGIN_DECLS
361struct sockaddr *dup_sockaddr(struct sockaddr *sa, int canwait);
362int getsock(struct filedesc *fdp, int fd, struct file **fpp);
363int sockargs(struct mbuf **mp, user_addr_t data, int buflen, int type);
364int getsockaddr(struct sockaddr **namp, user_addr_t uaddr, size_t len);
365int sbappend(struct sockbuf *sb, struct mbuf *m);
366int sbappendaddr(struct sockbuf *sb, struct sockaddr *asa,
367 struct mbuf *m0, struct mbuf *control, int *error_out);
368int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
369 struct mbuf *control, int *error_out);
370int sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
371void sbcheck(struct sockbuf *sb);
372int sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
1c79356b 373struct mbuf *
91447636
A
374 sbcreatecontrol(caddr_t p, int size, int type, int level);
375void sbdrop(struct sockbuf *sb, int len);
376void sbdroprecord(struct sockbuf *sb);
377void sbflush(struct sockbuf *sb);
378int sbinsertoob(struct sockbuf *sb, struct mbuf *m0);
379void sbrelease(struct sockbuf *sb);
380int sbreserve(struct sockbuf *sb, u_long cc);
381void sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
382int sbwait(struct sockbuf *sb);
383int sb_lock(struct sockbuf *sb);
384int soabort(struct socket *so);
385int soaccept(struct socket *so, struct sockaddr **nam);
386int soacceptlock (struct socket *so, struct sockaddr **nam, int dolock);
387struct socket *soalloc(int waitok, int dom, int type);
388int sobind(struct socket *so, struct sockaddr *nam);
389void socantrcvmore(struct socket *so);
390void socantsendmore(struct socket *so);
391int soclose(struct socket *so);
392int soconnect(struct socket *so, struct sockaddr *nam);
393int soconnectlock (struct socket *so, struct sockaddr *nam, int dolock);
394int soconnect2(struct socket *so1, struct socket *so2);
395int socreate(int dom, struct socket **aso, int type, int proto);
396void sodealloc(struct socket *so);
397int sodisconnect(struct socket *so);
398void sofree(struct socket *so);
399int sogetopt(struct socket *so, struct sockopt *sopt);
400void sohasoutofband(struct socket *so);
401void soisconnected(struct socket *so);
402void soisconnecting(struct socket *so);
403void soisdisconnected(struct socket *so);
404void soisdisconnecting(struct socket *so);
405int solisten(struct socket *so, int backlog);
1c79356b 406struct socket *
91447636 407 sodropablereq(struct socket *head);
1c79356b 408struct socket *
91447636
A
409 sonewconn(struct socket *head, int connstatus, const struct sockaddr* from);
410int sooptcopyin(struct sockopt *sopt, void *data, size_t len, size_t minlen);
411int sooptcopyout(struct sockopt *sopt, void *data, size_t len);
412int socket_lock(struct socket *so, int refcount);
413int socket_unlock(struct socket *so, int refcount);
9bccf70c
A
414
415/*
416 * XXX; prepare mbuf for (__FreeBSD__ < 3) routines.
417 * Used primarily in IPSec and IPv6 code.
418 */
91447636
A
419int soopt_getm(struct sockopt *sopt, struct mbuf **mp);
420int soopt_mcopyin(struct sockopt *sopt, struct mbuf *m);
421int soopt_mcopyout(struct sockopt *sopt, struct mbuf *m);
9bccf70c 422
91447636
A
423int sopoll(struct socket *so, int events, struct ucred *cred, void *wql);
424int soreceive(struct socket *so, struct sockaddr **paddr,
1c79356b 425 struct uio *uio, struct mbuf **mp0,
91447636
A
426 struct mbuf **controlp, int *flagsp);
427int soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
428void sorflush(struct socket *so);
429int sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
430 struct mbuf *top, struct mbuf *control, int flags);
1c79356b 431
91447636
A
432int sosetopt(struct socket *so, struct sockopt *sopt);
433int soshutdown(struct socket *so, int how);
434void sotoxsocket(struct socket *so, struct xsocket *xso);
435void sowakeup(struct socket *so, struct sockbuf *sb);
436int soioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p);
1c79356b 437
9bccf70c
A
438#ifndef __APPLE__
439/* accept filter functions */
91447636
A
440int accept_filt_add(struct accept_filter *filt);
441int accept_filt_del(char *name);
442struct accept_filter * accept_filt_get(char *name);
9bccf70c 443#ifdef ACCEPT_FILTER_MOD
91447636 444int accept_filt_generic_mod_event(module_t mod, int event, void *data);
9bccf70c
A
445SYSCTL_DECL(_net_inet_accf);
446#endif /* ACCEPT_FILTER_MOD */
447#endif /* !defined(__APPLE__) */
1c79356b 448
91447636
A
449__END_DECLS
450
1c79356b 451#endif /* KERNEL */
91447636 452#endif /* KERNEL_PRIVATE */
9bccf70c 453
1c79356b 454#endif /* !_SYS_SOCKETVAR_H_ */