]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/socketvar.h
xnu-792.10.96.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 */
c0fea474
A
207/* for debug pruposes */
208#define SO_LCKDBG_MAX 4 /* number of debug locking Link Registers recorded */
209 u_int32_t lock_lr[SO_LCKDBG_MAX]; /* socket locking calling history */
210 int next_lock_lr;
211 u_int32_t unlock_lr[SO_LCKDBG_MAX]; /* socket unlocking caller history */
212 int next_unlock_lr;
213
214
91447636
A
215 void *reserved3; /* Temporarily in use/debug: last socket lock LR */
216 void *reserved4; /* Temporarily in use/debug: last socket unlock LR */
3a60a9f5 217 thread_t so_send_filt_thread;
9bccf70c 218#endif
1c79356b 219};
91447636 220#endif /* KERNEL_PRIVATE */
1c79356b
A
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 */
9bccf70c 239#define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */
91447636 240#define SS_DRAINING 0x4000 /* close waiting for blocked system calls to drain */
1c79356b
A
241
242/*
243 * Externalized form of struct socket used by the sysctl(3) interface.
244 */
245struct 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
91447636 274#ifdef KERNEL_PRIVATE
1c79356b
A
275/*
276 * Macros for sockets and socket buffering.
277 */
91447636 278
1c79356b
A
279#define sbtoso(sb) (sb->sb_so)
280
9bccf70c
A
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 */
91447636
A
286
287__BEGIN_DECLS
288int sb_notify(struct sockbuf *sb);
289long sbspace(struct sockbuf *sb);
290int sosendallatonce(struct socket *so);
291int soreadable(struct socket *so);
292int sowriteable(struct socket *so);
293void sballoc(struct sockbuf *sb, struct mbuf *m);
294void sbfree(struct sockbuf *sb, struct mbuf *m);
295int sblock(struct sockbuf *sb, int wf);
296void sbunlock(struct sockbuf *sb, int locked);
297void sorwakeup(struct socket * so);
298void sowwakeup(struct socket * so);
299__END_DECLS
1c79356b
A
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 */
308struct 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
9bccf70c 320
91447636
A
321#define SO_FILT_HINT_LOCKED 0x1
322
1c79356b
A
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 */
327enum sopt_dir { SOPT_GET, SOPT_SET };
328struct 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 */
91447636 332 user_addr_t sopt_val; /* fourth arg of [gs]etsockopt */
1c79356b
A
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
1c79356b
A
338struct 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};
1c79356b
A
344#endif
345
346#ifdef MALLOC_DECLARE
347MALLOC_DECLARE(M_PCB);
348MALLOC_DECLARE(M_SONAME);
349#endif
350
351extern int maxsockets;
352extern u_long sb_max;
353extern int socket_zone;
354extern so_gen_t so_gencnt;
355
356struct file;
357struct filedesc;
358struct mbuf;
359struct sockaddr;
360struct stat;
361struct ucred;
362struct uio;
9bccf70c 363struct knote;
1c79356b 364
1c79356b
A
365/*
366 * From uipc_socket and friends
367 */
91447636
A
368__BEGIN_DECLS
369struct sockaddr *dup_sockaddr(struct sockaddr *sa, int canwait);
370int getsock(struct filedesc *fdp, int fd, struct file **fpp);
371int sockargs(struct mbuf **mp, user_addr_t data, int buflen, int type);
372int getsockaddr(struct sockaddr **namp, user_addr_t uaddr, size_t len);
373int sbappend(struct sockbuf *sb, struct mbuf *m);
374int sbappendaddr(struct sockbuf *sb, struct sockaddr *asa,
375 struct mbuf *m0, struct mbuf *control, int *error_out);
376int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
377 struct mbuf *control, int *error_out);
378int sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
379void sbcheck(struct sockbuf *sb);
1c79356b 380struct mbuf *
91447636
A
381 sbcreatecontrol(caddr_t p, int size, int type, int level);
382void sbdrop(struct sockbuf *sb, int len);
383void sbdroprecord(struct sockbuf *sb);
384void sbflush(struct sockbuf *sb);
385int sbinsertoob(struct sockbuf *sb, struct mbuf *m0);
386void sbrelease(struct sockbuf *sb);
387int sbreserve(struct sockbuf *sb, u_long cc);
388void sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
389int sbwait(struct sockbuf *sb);
390int sb_lock(struct sockbuf *sb);
391int soabort(struct socket *so);
392int soaccept(struct socket *so, struct sockaddr **nam);
393int soacceptlock (struct socket *so, struct sockaddr **nam, int dolock);
394struct socket *soalloc(int waitok, int dom, int type);
395int sobind(struct socket *so, struct sockaddr *nam);
396void socantrcvmore(struct socket *so);
397void socantsendmore(struct socket *so);
398int soclose(struct socket *so);
399int soconnect(struct socket *so, struct sockaddr *nam);
400int soconnectlock (struct socket *so, struct sockaddr *nam, int dolock);
401int soconnect2(struct socket *so1, struct socket *so2);
402int socreate(int dom, struct socket **aso, int type, int proto);
403void sodealloc(struct socket *so);
404int sodisconnect(struct socket *so);
405void sofree(struct socket *so);
406int sogetopt(struct socket *so, struct sockopt *sopt);
407void sohasoutofband(struct socket *so);
408void soisconnected(struct socket *so);
409void soisconnecting(struct socket *so);
410void soisdisconnected(struct socket *so);
411void soisdisconnecting(struct socket *so);
412int solisten(struct socket *so, int backlog);
1c79356b 413struct socket *
91447636 414 sodropablereq(struct socket *head);
1c79356b 415struct socket *
91447636
A
416 sonewconn(struct socket *head, int connstatus, const struct sockaddr* from);
417int sooptcopyin(struct sockopt *sopt, void *data, size_t len, size_t minlen);
418int sooptcopyout(struct sockopt *sopt, void *data, size_t len);
419int socket_lock(struct socket *so, int refcount);
420int socket_unlock(struct socket *so, int refcount);
9bccf70c
A
421
422/*
423 * XXX; prepare mbuf for (__FreeBSD__ < 3) routines.
424 * Used primarily in IPSec and IPv6 code.
425 */
91447636
A
426int soopt_getm(struct sockopt *sopt, struct mbuf **mp);
427int soopt_mcopyin(struct sockopt *sopt, struct mbuf *m);
428int soopt_mcopyout(struct sockopt *sopt, struct mbuf *m);
9bccf70c 429
91447636
A
430int sopoll(struct socket *so, int events, struct ucred *cred, void *wql);
431int soreceive(struct socket *so, struct sockaddr **paddr,
1c79356b 432 struct uio *uio, struct mbuf **mp0,
91447636
A
433 struct mbuf **controlp, int *flagsp);
434int soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
435void sorflush(struct socket *so);
436int sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
437 struct mbuf *top, struct mbuf *control, int flags);
1c79356b 438
91447636
A
439int sosetopt(struct socket *so, struct sockopt *sopt);
440int soshutdown(struct socket *so, int how);
441void sotoxsocket(struct socket *so, struct xsocket *xso);
442void sowakeup(struct socket *so, struct sockbuf *sb);
443int soioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p);
1c79356b 444
9bccf70c
A
445#ifndef __APPLE__
446/* accept filter functions */
91447636
A
447int accept_filt_add(struct accept_filter *filt);
448int accept_filt_del(char *name);
449struct accept_filter * accept_filt_get(char *name);
9bccf70c 450#ifdef ACCEPT_FILTER_MOD
91447636 451int accept_filt_generic_mod_event(module_t mod, int event, void *data);
9bccf70c
A
452SYSCTL_DECL(_net_inet_accf);
453#endif /* ACCEPT_FILTER_MOD */
454#endif /* !defined(__APPLE__) */
1c79356b 455
91447636
A
456__END_DECLS
457
1c79356b 458#endif /* KERNEL */
91447636 459#endif /* KERNEL_PRIVATE */
9bccf70c 460
1c79356b 461#endif /* !_SYS_SOCKETVAR_H_ */