]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/socketvar.h
xnu-2050.18.24.tar.gz
[apple/xnu.git] / bsd / sys / socketvar.h
CommitLineData
1c79356b 1/*
316670eb 2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 *
9bccf70c
A
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 $
1c79356b 64 */
2d21ac55
A
65/*
66 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
67 * support for mandatory and extensible security protections. This notice
68 * is included in support of clause 2.2 (b) of the Apple Public License,
69 * Version 2.0.
70 */
1c79356b 71
9bccf70c 72#ifndef _SYS_SOCKETVAR_H_
2d21ac55 73#define _SYS_SOCKETVAR_H_
1c79356b 74
9bccf70c
A
75#include <sys/appleapiopts.h>
76#include <sys/queue.h> /* for TAILQ macros */
1c79356b 77#include <sys/select.h> /* for struct selinfo */
1c79356b
A
78#include <net/kext_net.h>
79#include <sys/ev.h>
91447636
A
80#include <sys/cdefs.h>
81
1c79356b
A
82/*
83 * Hacks to get around compiler complaints
84 */
85struct mbuf;
91447636 86struct socket_filter_entry;
1c79356b
A
87struct protosw;
88struct sockif;
89struct sockutil;
90
91447636 91#ifdef KERNEL_PRIVATE
1c79356b
A
92/* strings for sleep message: */
93extern char netio[], netcon[], netcls[];
2d21ac55
A
94#define SOCKET_CACHE_ON
95#define SO_CACHE_FLUSH_INTERVAL 1 /* Seconds */
96#define SO_CACHE_TIME_LIMIT (120/SO_CACHE_FLUSH_INTERVAL) /* Seconds */
97#define SO_CACHE_MAX_FREE_BATCH 50
98#define MAX_CACHED_SOCKETS 60000
99#define TEMPDEBUG 0
1c79356b
A
100
101/*
102 * Kernel structure per socket.
103 * Contains send and receive buffer queues,
104 * handle on protocol and pointer to protocol
105 * private data and error information.
106 */
91447636
A
107#endif /* KERNEL_PRIVATE */
108
1c79356b
A
109typedef u_quad_t so_gen_t;
110
6d2010ae
A
111#ifdef PRIVATE
112#define SO_TC_STATS_MAX 4
113
114struct data_stats {
115 u_int64_t rxpackets;
116 u_int64_t rxbytes;
117 u_int64_t txpackets;
118 u_int64_t txbytes;
119};
120#endif /* PRIVATE */
121
91447636 122#ifdef KERNEL_PRIVATE
9bccf70c
A
123#ifndef __APPLE__
124/* We don't support BSD style socket filters */
125struct accept_filter;
126#endif
127
1c79356b 128struct socket {
2d21ac55
A
129 int so_zone; /* zone we were allocated from */
130 short so_type; /* generic type, see socket.h */
316670eb 131 u_int32_t so_options; /* from socket call, see socket.h */
1c79356b 132 short so_linger; /* time to linger while closing */
2d21ac55 133 short so_state; /* internal state flags SS_*, below */
316670eb 134 void *so_pcb; /* protocol control block */
1c79356b 135 struct protosw *so_proto; /* protocol handle */
2d21ac55
A
136 /*
137 * Variables for connection queueing.
138 * Socket where accepts occur is so_head in all subsidiary sockets.
139 * If so_head is 0, socket is not related to an accept.
140 * For head socket so_incomp queues partially completed connections,
141 * while so_comp is a queue of connections ready to be accepted.
142 * If a connection is aborted and it has so_head set, then
143 * it has to be pulled out of either so_incomp or so_comp.
144 * We allow connections to queue up based on current queue lengths
145 * and limit on number of queued connections for this socket.
146 */
1c79356b 147 struct socket *so_head; /* back pointer to accept socket */
2d21ac55
A
148 TAILQ_HEAD(, socket) so_incomp; /* q of partially unaccepted conns */
149 TAILQ_HEAD(, socket) so_comp; /* q of complete unaccepted conns */
1c79356b
A
150 TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */
151 short so_qlen; /* number of unaccepted connections */
152 short so_incqlen; /* number of unaccepted incomplete
153 connections */
154 short so_qlimit; /* max number queued connections */
155 short so_timeo; /* connection timeout */
156 u_short so_error; /* error affecting connection */
157 pid_t so_pgid; /* pgid for signals */
b0d623f7 158 u_int32_t so_oobmark; /* chars to oob mark */
9bccf70c
A
159#ifndef __APPLE__
160 /* We don't support AIO ops */
161 TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
162#endif
2d21ac55
A
163 /*
164 * Variables for socket buffering.
165 */
1c79356b 166 struct sockbuf {
b0d623f7
A
167 u_int32_t sb_cc; /* actual chars in buffer */
168 u_int32_t sb_hiwat; /* max actual char count */
169 u_int32_t sb_mbcnt; /* chars of mbufs used */
170 u_int32_t sb_mbmax; /* max chars of mbufs to use */
171 u_int32_t sb_ctl; /* non-data chars in buffer */
172 u_int32_t sb_lowat; /* low water mark */
1c79356b 173 struct mbuf *sb_mb; /* the mbuf chain */
2d21ac55
A
174 struct mbuf *sb_mbtail; /* the last mbuf in the chain */
175 struct mbuf *sb_lastrecord; /* first mbuf of last record */
9bccf70c 176#if __APPLE__
2d21ac55 177 struct socket *sb_so; /* socket back ptr for kexts */
9bccf70c 178#endif
1c79356b
A
179 struct selinfo sb_sel; /* process selecting read/write */
180 short sb_flags; /* flags, see below */
2d21ac55 181 struct timeval sb_timeo; /* timeout for read/write */
316670eb 182 u_int32_t sb_idealsize; /* Ideal size for the sb based on bandwidth and delay */
2d21ac55 183 void *reserved1[4]; /* for future use */
1c79356b 184 } so_rcv, so_snd;
2d21ac55 185#define SB_MAX (8192*1024) /* default for max chars in sockbuf */
b0d623f7 186#define LOW_SB_MAX (2*9*1024) /* lower limit on max socket buffer size, 2 max datagrams */
1c79356b
A
187#define SB_LOCK 0x01 /* lock on data queue */
188#define SB_WANT 0x02 /* someone is waiting to lock */
189#define SB_WAIT 0x04 /* someone is waiting for data/space */
0b4e3aa0 190#define SB_SEL 0x08 /* someone is selecting */
2d21ac55
A
191#define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
192#define SB_UPCALL 0x20 /* someone wants an upcall */
193#define SB_NOINTR 0x40 /* operations not interruptible */
194#define SB_KNOTE 0x100 /* kernel note attached */
195#define SB_USRSIZE 0x200 /* user specified sbreserve */
9bccf70c 196#ifndef __APPLE__
2d21ac55 197#define SB_AIO 0x80 /* AIO operations queued */
9bccf70c 198#else
0b4e3aa0 199#define SB_NOTIFY (SB_WAIT|SB_SEL|SB_ASYNC)
2d21ac55
A
200#define SB_DROP 0x400 /* does not accept any more data */
201#define SB_UNIX 0x800 /* UNIX domain socket buffer */
316670eb
A
202#define SB_AUTOSIZE 0x1000 /* automatically size socket buffer */
203#define SB_TRIM 0x2000 /* Trim the socket buffer */
2d21ac55 204#define SB_RECV 0x8000 /* this is rcv sb */
1c79356b 205
316670eb 206 caddr_t so_tpcb; /* Wisc. protocol control block, used by some kexts */
9bccf70c
A
207#endif
208
91447636 209 void (*so_upcall)(struct socket *so, caddr_t arg, int waitf);
1c79356b 210 caddr_t so_upcallarg; /* Arg for above */
316670eb 211 kauth_cred_t so_cred; /* cred of who opened the socket */
1c79356b
A
212 /* NB: generation count must not be first; easiest to make it last. */
213 so_gen_t so_gencnt; /* generation count */
9bccf70c
A
214#ifndef __APPLE__
215 void *so_emuldata; /* private data for emulators */
2d21ac55 216 struct so_accf {
9bccf70c
A
217 struct accept_filter *so_accept_filter;
218 void *so_accept_filter_arg; /* saved filter args */
219 char *so_accept_filter_str; /* saved user args */
220 } *so_accf;
221#else
2d21ac55
A
222 TAILQ_HEAD(, eventqelt) so_evlist;
223 int cached_in_sock_layer; /* bundled with pcb/pcb.inp_ppcb? */
1c79356b
A
224 struct socket *cache_next;
225 struct socket *cache_prev;
b0d623f7 226 u_int32_t cache_timestamp;
1c79356b
A
227 caddr_t so_saved_pcb; /* Saved pcb when cacheing */
228 struct mbuf *so_temp; /* Holding area for outbound frags */
229 /* Plug-in support - make the socket interface overridable */
230 struct mbuf *so_tail;
2d21ac55 231 struct socket_filter_entry *so_filt; /* NKE hook */
b0d623f7 232 u_int32_t so_flags; /* Flags */
2d21ac55
A
233#define SOF_NOSIGPIPE 0x1
234#define SOF_NOADDRAVAIL 0x2 /* EADDRNOTAVAIL if src addr is gone */
235#define SOF_PCBCLEARING 0x4 /* pru_disconnect done; don't call pru_detach */
6d2010ae 236#define SOF_DEFUNCT 0x8 /* socket marked as inactive */
2d21ac55 237#define SOF_CLOSEWAIT 0x10 /* blocked in close awaiting some events */
2d21ac55
A
238#define SOF_REUSESHAREUID 0x40 /* Allows SO_REUSEADDR/SO_REUSEPORT for multiple so_uid */
239#define SOF_MULTIPAGES 0x80 /* jumbo clusters may be used for sosend */
240#define SOF_ABORTED 0x100 /* soabort was already called once on the socket */
241#define SOF_OVERFLOW 0x200 /* socket was dropped as overflow of listen queue */
242#ifdef __APPLE_API_PRIVATE
243#define SOF_NOTIFYCONFLICT 0x400 /* notify that a bind was done on a port already in use */
244#endif
4a3eedf9 245#define SOF_UPCALLCLOSEWAIT 0x800 /* block on close until an upcall returns */
b0d623f7
A
246#define SOF_BINDRANDOMPORT 0x1000 /* Request a randomized port number for the bind */
247#define SOF_NPX_SETOPTSHUT 0x2000 /* Non POSIX extension to allow setsockopt(2) after shut down */
6d2010ae
A
248#define SOF_RECV_TRAFFIC_CLASS 0x4000 /* Receive traffic class as ancillary data */
249#define SOF_NODEFUNCT 0x8000 /* socket cannot be defunct'd */
316670eb
A
250#define SOF_PRIVILEGED_TRAFFIC_CLASS 0x10000 /* traffic class is privileged */
251#define SOF_SUSPENDED 0x20000 /* interface output queue is suspended */
252#define SOF_INCOMP_INPROGRESS 0x40000 /* incomp socket still being processed */
253#define SOF_NOTSENT_LOWAT 0x80000 /* A different lowat on not sent data has been set */
254#define SOF_KNOTE 0x100000 /* socket is on the EV_SOCK klist */
255#define SOF_USELRO 0x200000 /* TCP must use LRO on these sockets */
256 uint32_t so_upcallusecount; /* number of upcalls in progress */
2d21ac55 257 int so_usecount; /* refcounting of socket use */;
91447636 258 int so_retaincnt;
2d21ac55
A
259 u_int32_t so_filteruse; /* usecount for the socket filters */
260 u_int32_t so_traffic_mgt_flags; /* traffic_mgt socket config */
261 thread_t so_send_filt_thread;
262 u_int32_t so_restrictions;
263
0c530ab8 264/* for debug pruposes */
2d21ac55 265#define SO_LCKDBG_MAX 4 /* number of debug locking Link Registers recorded */
b0d623f7 266 void *lock_lr[SO_LCKDBG_MAX]; /* locking calling history */
0c530ab8 267 int next_lock_lr;
b0d623f7 268 void *unlock_lr[SO_LCKDBG_MAX]; /* unlocking caller history */
0c530ab8 269 int next_unlock_lr;
2d21ac55
A
270 void *reserved; /* reserved for future use */
271#endif /* __APPLE__ */
0c530ab8 272
2d21ac55
A
273 struct label *so_label; /* MAC label for socket */
274 struct label *so_peerlabel; /* cached MAC label for socket peer */
275 thread_t so_background_thread; /* thread that marked this socket background */
d41d1dae 276 int so_traffic_class;
316670eb 277
6d2010ae
A
278 // last process to interact with this socket
279 u_int64_t last_upid;
280 pid_t last_pid;
6d2010ae 281 struct data_stats so_tc_stats[SO_TC_STATS_MAX];
316670eb 282 struct klist so_klist; /* klist for EV_SOCK events */
1c79356b 283};
6d2010ae
A
284
285/* Control message accessor in mbufs */
286
287#define _MIN_NXT_CMSGHDR_PTR(cmsg) \
288 ((char *)(cmsg) + \
289 __DARWIN_ALIGN32((__uint32_t)(cmsg)->cmsg_len) + \
290 __DARWIN_ALIGN32(sizeof(struct cmsghdr)))
291
292#define M_FIRST_CMSGHDR(m) \
293 ((char *)(m) != (char *)0L && (size_t)(m)->m_len >= sizeof(struct cmsghdr) && \
316670eb
A
294 (socklen_t)(m)->m_len >= __DARWIN_ALIGN32(((struct cmsghdr *)(void *)(m)->m_data)->cmsg_len) ?\
295 (struct cmsghdr *)(void *)(m)->m_data : \
6d2010ae
A
296 (struct cmsghdr *)0L)
297
298#define M_NXT_CMSGHDR(m, cmsg) \
299 ((char *)(cmsg) == (char *)0L ? M_FIRST_CMSGHDR(m) : \
300 _MIN_NXT_CMSGHDR_PTR(cmsg) > ((char *)(m)->m_data) + (m)->m_len || \
301 _MIN_NXT_CMSGHDR_PTR(cmsg) < (char *)(m)->m_data ? \
302 (struct cmsghdr *)0L /* NULL */ : \
316670eb 303 (struct cmsghdr *)(void *)((unsigned char *)(cmsg) + \
6d2010ae
A
304 __DARWIN_ALIGN32((__uint32_t)(cmsg)->cmsg_len)))
305
91447636 306#endif /* KERNEL_PRIVATE */
1c79356b
A
307
308/*
309 * Socket state bits.
310 */
2d21ac55
A
311#define SS_NOFDREF 0x0001 /* no file table ref any more */
312#define SS_ISCONNECTED 0x0002 /* socket connected to a peer */
313#define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */
314#define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */
315#define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */
316#define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */
317#define SS_RCVATMARK 0x0040 /* at mark on input */
318
319#define SS_PRIV 0x0080 /* privileged for broadcast, raw... */
320#define SS_NBIO 0x0100 /* non-blocking ops */
321#define SS_ASYNC 0x0200 /* async i/o notify */
322#define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */
323#define SS_INCOMP 0x0800 /* Unaccepted, incomplete connection */
1c79356b 324#define SS_COMP 0x1000 /* unaccepted, complete connection */
9bccf70c 325#define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */
2d21ac55
A
326#define SS_DRAINING 0x4000 /* close waiting for blocked system
327 calls to drain */
6d2010ae 328#define SS_DEFUNCT 0x8000 /* has been fully defunct'd */
2d21ac55
A
329
330#if defined(__LP64__)
331#define _XSOCKET_PTR(x) u_int32_t
332#else
333#define _XSOCKET_PTR(x) x
334#endif
335
316670eb
A
336#ifdef PRIVATE
337/* Flags returned in data field for EVFILT_SOCK events. */
338#define SOCKEV_CONNECTED 0x00000001 /* connected */
339#define SOCKEV_DISCONNECTED 0x00000002 /* disconnected */
340#endif /* PRIVATE */
341
2d21ac55 342#pragma pack(4)
1c79356b 343
b0d623f7 344struct xsockbuf {
6d2010ae
A
345 u_int32_t sb_cc;
346 u_int32_t sb_hiwat;
347 u_int32_t sb_mbcnt;
348 u_int32_t sb_mbmax;
349 int32_t sb_lowat;
350 short sb_flags;
351 short sb_timeo;
1c79356b
A
352};
353
b0d623f7
A
354/*
355 * Externalized form of struct socket used by the sysctl(3) interface.
356 */
357struct xsocket {
358 u_int32_t xso_len; /* length of this structure */
359 _XSOCKET_PTR(struct socket *) xso_so; /* makes a convenient handle */
360 short so_type;
361 short so_options;
362 short so_linger;
363 short so_state;
364 _XSOCKET_PTR(caddr_t) so_pcb; /* another convenient handle */
365 int xso_protocol;
366 int xso_family;
367 short so_qlen;
368 short so_incqlen;
369 short so_qlimit;
370 short so_timeo;
371 u_short so_error;
372 pid_t so_pgid;
373 u_int32_t so_oobmark;
374 struct xsockbuf so_rcv;
375 struct xsockbuf so_snd;
376 uid_t so_uid; /* XXX */
377};
378
379#if !CONFIG_EMBEDDED
380
381struct xsocket64 {
382 u_int32_t xso_len; /* length of this structure */
383 u_int64_t xso_so; /* makes a convenient handle */
384 short so_type;
385 short so_options;
386 short so_linger;
387 short so_state;
388 u_int64_t so_pcb; /* another convenient handle */
389 int xso_protocol;
390 int xso_family;
391 short so_qlen;
392 short so_incqlen;
393 short so_qlimit;
394 short so_timeo;
395 u_short so_error;
396 pid_t so_pgid;
397 u_int32_t so_oobmark;
398 struct xsockbuf so_rcv;
399 struct xsockbuf so_snd;
400 uid_t so_uid; /* XXX */
401};
402
403#endif /* !CONFIG_EMBEDDED */
404
6d2010ae
A
405#ifdef PRIVATE
406
407#define XSO_SOCKET 0x001
408#define XSO_RCVBUF 0x002
409#define XSO_SNDBUF 0x004
410#define XSO_STATS 0x008
411#define XSO_INPCB 0x010
412#define XSO_TCPCB 0x020
413
414struct xsocket_n {
415 u_int32_t xso_len; /* length of this structure */
416 u_int32_t xso_kind; /* XSO_SOCKET */
417 u_int64_t xso_so; /* makes a convenient handle */
418 short so_type;
316670eb 419 u_int32_t so_options;
6d2010ae
A
420 short so_linger;
421 short so_state;
422 u_int64_t so_pcb; /* another convenient handle */
423 int xso_protocol;
424 int xso_family;
425 short so_qlen;
426 short so_incqlen;
427 short so_qlimit;
428 short so_timeo;
429 u_short so_error;
430 pid_t so_pgid;
431 u_int32_t so_oobmark;
432 uid_t so_uid; /* XXX */
433};
434
435struct xsockbuf_n {
436 u_int32_t xsb_len; /* length of this structure */
437 u_int32_t xsb_kind; /* XSO_RCVBUF or XSO_SNDBUF */
438 u_int32_t sb_cc;
439 u_int32_t sb_hiwat;
440 u_int32_t sb_mbcnt;
441 u_int32_t sb_mbmax;
442 int32_t sb_lowat;
443 short sb_flags;
444 short sb_timeo;
445};
446
447struct xsockstat_n {
448 u_int32_t xst_len; /* length of this structure */
449 u_int32_t xst_kind; /* XSO_STATS */
450 struct data_stats xst_tc_stats[SO_TC_STATS_MAX];
451};
452
453#endif /* PRIVATE */
454
2d21ac55
A
455#pragma pack()
456
91447636 457#ifdef KERNEL_PRIVATE
1c79356b
A
458/*
459 * Macros for sockets and socket buffering.
460 */
91447636 461
2d21ac55 462#define sbtoso(sb) (sb->sb_so)
1c79356b 463
9bccf70c
A
464/*
465 * Functions for sockets and socket buffering.
466 * These are macros on FreeBSD. On Darwin the
467 * implementation is in bsd/kern/uipc_socket2.c
468 */
91447636
A
469
470__BEGIN_DECLS
2d21ac55 471int sb_notify(struct sockbuf *sb);
b0d623f7 472int sbspace(struct sockbuf *sb);
2d21ac55
A
473int sosendallatonce(struct socket *so);
474int soreadable(struct socket *so);
475int sowriteable(struct socket *so);
476void sballoc(struct sockbuf *sb, struct mbuf *m);
477void sbfree(struct sockbuf *sb, struct mbuf *m);
478int sblock(struct sockbuf *sb, int wf);
479void sbunlock(struct sockbuf *sb, int locked);
480void sorwakeup(struct socket *so);
481void sowwakeup(struct socket *so);
91447636 482__END_DECLS
1c79356b
A
483
484/*
485 * Socket extension mechanism: control block hooks:
486 * This is the "head" of any control block for an extenstion
487 * Note: we separate intercept function dispatch vectors from
488 * the NFDescriptor to permit selective replacement during
489 * operation, e.g., to disable some functions.
490 */
2d21ac55
A
491struct kextcb {
492 struct kextcb *e_next; /* Next kext control block */
1c79356b
A
493 void *e_fcb; /* Real filter control block */
494 struct NFDescriptor *e_nfd; /* NKE Descriptor */
495 /* Plug-in support - intercept functions */
496 struct sockif *e_soif; /* Socket functions */
497 struct sockutil *e_sout; /* Sockbuf utility functions */
498};
2d21ac55
A
499#define EXT_NULL 0x0 /* STATE: Not in use */
500#define sotokextcb(so) (so ? so->so_ext : 0)
1c79356b
A
501
502#ifdef KERNEL
316670eb
A
503#include <sys/kpi_mbuf.h>
504
505/* Hints for socket event processing */
506#define SO_FILT_HINT_LOCKED 0x00000001 /* socket is already locked */
507#define SO_FILT_HINT_CONNRESET 0x00000002 /* Reset is received */
508#define SO_FILT_HINT_CANTRCVMORE 0x00000004 /* No more data to read */
509#define SO_FILT_HINT_CANTSENDMORE 0x00000008 /* Can't write more data */
510#define SO_FILT_HINT_TIMEOUT 0x00000010 /* timeout */
511#define SO_FILT_HINT_NOSRCADDR 0x00000020 /* No src address available */
512#define SO_FILT_HINT_IFDENIED 0x00000040 /* interface denied connection */
513#define SO_FILT_HINT_SUSPEND 0x00000080 /* output queue suspended */
514#define SO_FILT_HINT_RESUME 0x00000100 /* output queue resumed */
515#define SO_FILT_HINT_KEEPALIVE 0x00000200 /* TCP Keepalive received */
516
517#define SO_FILT_HINT_EV (SO_FILT_HINT_CONNRESET | \
518 SO_FILT_HINT_CANTRCVMORE | SO_FILT_HINT_CANTSENDMORE | \
519 SO_FILT_HINT_TIMEOUT | SO_FILT_HINT_NOSRCADDR | \
520 SO_FILT_HINT_IFDENIED | SO_FILT_HINT_SUSPEND | \
521 SO_FILT_HINT_RESUME | SO_FILT_HINT_KEEPALIVE)
91447636 522
1c79356b
A
523/*
524 * Argument structure for sosetopt et seq. This is in the KERNEL
525 * section because it will never be visible to user code.
526 */
527enum sopt_dir { SOPT_GET, SOPT_SET };
528struct sockopt {
529 enum sopt_dir sopt_dir; /* is this a get or a set? */
530 int sopt_level; /* second arg of [gs]etsockopt */
531 int sopt_name; /* third arg of [gs]etsockopt */
91447636 532 user_addr_t sopt_val; /* fourth arg of [gs]etsockopt */
1c79356b
A
533 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */
534 struct proc *sopt_p; /* calling process or null if kernel */
535};
536
537#if SENDFILE
1c79356b
A
538struct sf_buf {
539 SLIST_ENTRY(sf_buf) free_list; /* list of free buffer slots */
540 int refcnt; /* reference count */
541 struct vm_page *m; /* currently mapped page */
542 vm_offset_t kva; /* va of mapping */
543};
1c79356b
A
544#endif
545
546#ifdef MALLOC_DECLARE
547MALLOC_DECLARE(M_PCB);
548MALLOC_DECLARE(M_SONAME);
549#endif
550
551extern int maxsockets;
b0d623f7 552extern u_int32_t sb_max;
1c79356b
A
553extern int socket_zone;
554extern so_gen_t so_gencnt;
2d21ac55
A
555extern int socket_debug;
556extern int sosendjcl;
557extern int sosendjcl_ignore_capab;
6d2010ae 558extern int sodefunctlog;
316670eb 559extern int sothrottlelog;
2d21ac55 560extern int somaxconn;
1c79356b
A
561
562struct file;
563struct filedesc;
564struct mbuf;
565struct sockaddr;
566struct stat;
567struct ucred;
568struct uio;
9bccf70c 569struct knote;
6d2010ae 570struct so_tcdbg;
1c79356b 571
2d21ac55
A
572#define SBLASTRECORDCHK(sb, s) \
573 if (socket_debug) sblastrecordchk(sb, s);
574
575#define SBLASTMBUFCHK(sb, s) \
576 if (socket_debug) sblastmbufchk(sb, s);
577
578#define SB_EMPTY_FIXUP(sb) { \
579 if ((sb)->sb_mb == NULL) { \
580 (sb)->sb_mbtail = NULL; \
581 (sb)->sb_lastrecord = NULL; \
582 } \
583}
584
316670eb
A
585#define SB_MB_CHECK(sb) do { \
586 if (((sb)->sb_mb != NULL && \
587 (sb)->sb_cc == 0) || \
588 ((sb)->sb_mb == NULL && \
589 (sb)->sb_cc > 0)) \
590 panic("corrupt so_rcv: sb_mb %p sb_cc %d\n", \
591 (sb)->sb_mb, (sb)->sb_cc); \
592} while(0)
593
594
6d2010ae
A
595#define SODEFUNCTLOG(x) do { if (sodefunctlog) printf x; } while (0)
596
316670eb
A
597#define SOTHROTTLELOG(x) do { if (sothrottlelog) printf x; } while (0)
598
6d2010ae
A
599/*
600 * For debugging traffic class behaviors
601 */
602#define SOTCDB_NO_DSCP 0x01 /* Do not set DSCP code in IP header */
603#define SOTCDB_NO_MTC 0x02 /* Do not set the mbuf traffic class */
604#define SOTCDB_NO_SENDTCPBG 0x04 /* Do not use background TCP CC algorithm for sender */
605#define SOTCDB_NO_LCLTST 0x08 /* Do not test for local destination for setting DSCP */
606#define SOTCDB_NO_DSCPTST 0x10 /* Overwritte any existing DSCP code */
316670eb
A
607#define SOTCDB_NO_RECVTCPBG 0x20 /* Do not use throttling on receiver-side of TCP */
608#define SOTCDB_NO_PRIVILEGED 0x40 /* Do not set privileged traffic flag */
6d2010ae
A
609
610extern u_int32_t sotcdb;
611
1c79356b
A
612/*
613 * From uipc_socket and friends
614 */
91447636 615__BEGIN_DECLS
2d21ac55
A
616/* Not exported */
617extern void socketinit(void) __attribute__((section("__TEXT, initcode")));
618
619/* Exported */
620extern struct sockaddr *dup_sockaddr(struct sockaddr *sa, int canwait);
621extern int getsock(struct filedesc *fdp, int fd, struct file **fpp);
622extern int sockargs(struct mbuf **mp, user_addr_t data, int buflen, int type);
623
624extern int sbappend(struct sockbuf *sb, struct mbuf *m);
625extern int sbappendstream(struct sockbuf *sb, struct mbuf *m);
626extern int sbappendaddr(struct sockbuf *sb, struct sockaddr *asa,
627 struct mbuf *m0, struct mbuf *control, int *error_out);
628extern int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
629 struct mbuf *control, int *error_out);
630extern int sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
631extern void sbcheck(struct sockbuf *sb);
632extern void sblastmbufchk(struct sockbuf *, const char *);
633extern void sblastrecordchk(struct sockbuf *, const char *);
634extern struct mbuf *sbcreatecontrol(caddr_t p, int size, int type, int level);
6d2010ae 635extern struct mbuf **sbcreatecontrol_mbuf(caddr_t p, int size, int type, int level, struct mbuf** m);
2d21ac55
A
636extern void sbdrop(struct sockbuf *sb, int len);
637extern void sbdroprecord(struct sockbuf *sb);
638extern void sbflush(struct sockbuf *sb);
639extern int sbinsertoob(struct sockbuf *sb, struct mbuf *m0);
640extern void sbrelease(struct sockbuf *sb);
b0d623f7 641extern int sbreserve(struct sockbuf *sb, u_int32_t cc);
2d21ac55
A
642extern void sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
643extern int sbwait(struct sockbuf *sb);
644extern int sb_lock(struct sockbuf *sb);
645extern void sb_empty_assert(struct sockbuf *, const char *);
646
647extern int soabort(struct socket *so);
648extern int soaccept(struct socket *so, struct sockaddr **nam);
649extern int soacceptlock(struct socket *so, struct sockaddr **nam, int dolock);
650extern int soacceptfilter(struct socket *so);
651extern struct socket *soalloc(int waitok, int dom, int type);
652extern int sobind(struct socket *so, struct sockaddr *nam);
653extern void socantrcvmore(struct socket *so);
654extern void socantsendmore(struct socket *so);
655extern int soclose(struct socket *so);
656extern int soconnect(struct socket *so, struct sockaddr *nam);
657extern int soconnectlock(struct socket *so, struct sockaddr *nam, int dolock);
658extern int soconnect2(struct socket *so1, struct socket *so2);
659extern int socreate(int dom, struct socket **aso, int type, int proto);
660extern void sodealloc(struct socket *so);
661extern int sodisconnect(struct socket *so);
662extern int sodisconnectlocked(struct socket *so);
663extern void sofree(struct socket *so);
664extern void soreference(struct socket *so);
665extern void sodereference(struct socket *so);
666extern void somultipages(struct socket *, boolean_t);
6d2010ae
A
667extern int sosetdefunct(struct proc *, struct socket *, int level, boolean_t);
668extern int sodefunct(struct proc *, struct socket *, int level);
2d21ac55
A
669extern int sogetopt(struct socket *so, struct sockopt *sopt);
670extern void sohasoutofband(struct socket *so);
671extern void soisconnected(struct socket *so);
672extern void soisconnecting(struct socket *so);
673extern void soisdisconnected(struct socket *so);
6d2010ae 674extern void sodisconnectwakeup(struct socket *so);
2d21ac55 675extern void soisdisconnecting(struct socket *so);
316670eb
A
676extern int soisthrottled(struct socket *so);
677extern int soisprivilegedtraffic(struct socket *so);
2d21ac55
A
678extern int solisten(struct socket *so, int backlog);
679extern struct socket *sodropablereq(struct socket *head);
680extern struct socket *sonewconn(struct socket *head, int connstatus,
681 const struct sockaddr *from);
682extern int sooptcopyin(struct sockopt *sopt, void *data, size_t len,
683 size_t minlen);
684extern int sooptcopyout(struct sockopt *sopt, void *data, size_t len);
685extern int socket_lock(struct socket *so, int refcount);
686extern int socket_unlock(struct socket *so, int refcount);
687extern void sofreelastref(struct socket *, int);
b0d623f7
A
688extern int sogetaddr_locked(struct socket *, struct sockaddr **, int);
689extern const char *solockhistory_nr(struct socket *);
316670eb
A
690extern void soevent(struct socket *so, long hint);
691extern void get_sockev_state(struct socket *, u_int32_t *);
692
693#ifdef BSD_KERNEL_PRIVATE
694/* Service class flags used for setting service class on a packet */
695#define PKT_SCF_IPV6 0x00000001 /* IPv6 packet */
696#define PKT_SCF_TCP_ACK 0x00000002 /* Pure TCP ACK */
697
698extern void set_packet_service_class(struct mbuf *, struct socket *,
699 mbuf_svc_class_t, u_int32_t);
700extern void so_tc_update_stats(struct mbuf *, struct socket *, mbuf_svc_class_t );
701extern mbuf_svc_class_t mbuf_service_class_from_control(struct mbuf *);
702extern mbuf_svc_class_t so_tc2msc(int);
703extern int so_svc2tc(mbuf_svc_class_t);
704
6d2010ae
A
705extern void set_tcp_stream_priority(struct socket *so);
706extern int so_set_traffic_class(struct socket *, int);
707extern void so_set_default_traffic_class(struct socket *);
316670eb
A
708extern int so_set_opportunistic(struct socket *, int);
709extern int so_get_opportunistic(struct socket *);
710extern int so_set_recv_anyif(struct socket *, int);
711extern int so_get_recv_anyif(struct socket *);
6d2010ae
A
712extern void socket_tclass_init(void);
713extern int so_set_tcdbg(struct socket *, struct so_tcdbg *);
714extern int sogetopt_tcdbg(struct socket *, struct sockopt *);
715extern void so_recv_data_stat(struct socket *, struct mbuf *, size_t);
316670eb
A
716extern int so_wait_for_if_feedback(struct socket *);
717#endif /* BSD_KERNEL_PRIVATE */
9bccf70c
A
718
719/*
720 * XXX; prepare mbuf for (__FreeBSD__ < 3) routines.
721 * Used primarily in IPSec and IPv6 code.
722 */
2d21ac55
A
723extern int soopt_getm(struct sockopt *sopt, struct mbuf **mp);
724extern int soopt_mcopyin(struct sockopt *sopt, struct mbuf *m);
725extern int soopt_mcopyout(struct sockopt *sopt, struct mbuf *m);
726
727extern int sopoll(struct socket *so, int events, struct ucred *cred, void *wql);
728extern int soreceive(struct socket *so, struct sockaddr **paddr,
729 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
b0d623f7 730extern int soreserve(struct socket *so, u_int32_t sndcc, u_int32_t rcvcc);
2d21ac55
A
731extern void sorflush(struct socket *so);
732extern int sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
733 struct mbuf *top, struct mbuf *control, int flags);
734
735extern int sosetopt(struct socket *so, struct sockopt *sopt);
736extern int soshutdown(struct socket *so, int how);
737extern int soshutdownlock(struct socket *so, int how);
738extern void sotoxsocket(struct socket *so, struct xsocket *xso);
b0d623f7
A
739#if !CONFIG_EMBEDDED
740extern void sotoxsocket64(struct socket *so, struct xsocket64 *xso);
741#endif
6d2010ae 742extern void sbwakeup(struct sockbuf *sb);
2d21ac55
A
743extern void sowakeup(struct socket *so, struct sockbuf *sb);
744extern int soioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p);
1c79356b 745
9bccf70c
A
746#ifndef __APPLE__
747/* accept filter functions */
2d21ac55
A
748extern int accept_filt_add(struct accept_filter *filt);
749extern int accept_filt_del(char *name);
750extern struct accept_filter *accept_filt_get(char *name);
9bccf70c 751#ifdef ACCEPT_FILTER_MOD
2d21ac55 752extern int accept_filt_generic_mod_event(module_t mod, int event, void *data);
9bccf70c
A
753SYSCTL_DECL(_net_inet_accf);
754#endif /* ACCEPT_FILTER_MOD */
755#endif /* !defined(__APPLE__) */
1c79356b 756
91447636
A
757__END_DECLS
758
1c79356b 759#endif /* KERNEL */
91447636 760#endif /* KERNEL_PRIVATE */
9bccf70c 761
1c79356b 762#endif /* !_SYS_SOCKETVAR_H_ */