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