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