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