2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
31 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
33 * Copyright (c) 1982, 1986, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
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 $
68 #ifndef _SYS_SOCKETVAR_H_
69 #define _SYS_SOCKETVAR_H_
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>
76 #include <sys/cdefs.h>
79 * Hacks to get around compiler complaints
82 struct socket_filter_entry
;
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
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.
103 #endif /* KERNEL_PRIVATE */
105 typedef u_quad_t so_gen_t
;
107 #ifdef KERNEL_PRIVATE
109 /* We don't support BSD style socket filters */
110 struct accept_filter
;
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 */
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.
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
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 */
145 /* We don't support AIO ops */
146 TAILQ_HEAD(, aiocblist
) so_aiojobq
; /* AIO ops waiting on socket */
149 * Variables for socket buffering.
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 */
159 struct socket
*sb_so
; /* socket back ptr for kexts */
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 */
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 */
177 #define SB_AIO 0x80 /* AIO operations queued */
179 #define SB_NOTIFY (SB_WAIT|SB_SEL|SB_ASYNC)
180 #define SB_RECV 0x8000 /* this is rcv sb */
182 caddr_t so_tpcb
; /* Wisc. protocol control block - XXX unused? */
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 */
191 void *so_emuldata
; /* private data for emulators */
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 */
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 */;
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
;
220 #endif /* KERNEL_PRIVATE */
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 */
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 */
243 * Externalized form of struct socket used by the sysctl(3) interface.
246 size_t xso_len
; /* length of this structure */
247 struct socket
*xso_so
; /* makes a convenient handle sometimes */
252 caddr_t so_pcb
; /* another convenient handle */
271 uid_t so_uid
; /* XXX */
274 #ifdef KERNEL_PRIVATE
276 * Macros for sockets and socket buffering.
279 #define sbtoso(sb) (sb->sb_so)
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
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
);
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.
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 */
316 #define EXT_NULL 0x0 /* STATE: Not in use */
317 #define sotokextcb(so) (so ? so->so_ext : 0)
321 #define SO_FILT_HINT_LOCKED 0x1
324 * Argument structure for sosetopt et seq. This is in the KERNEL
325 * section because it will never be visible to user code.
327 enum sopt_dir
{ SOPT_GET
, SOPT_SET
};
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 */
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 */
346 #ifdef MALLOC_DECLARE
347 MALLOC_DECLARE(M_PCB
);
348 MALLOC_DECLARE(M_SONAME
);
351 extern int maxsockets
;
352 extern u_long sb_max
;
353 extern int socket_zone
;
354 extern so_gen_t so_gencnt
;
366 * From uipc_socket and friends
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
);
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
);
415 sodropablereq(struct socket
*head
);
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
);
424 * XXX; prepare mbuf for (__FreeBSD__ < 3) routines.
425 * Used primarily in IPSec and IPv6 code.
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
);
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
);
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
);
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__) */
460 #endif /* KERNEL_PRIVATE */
462 #endif /* !_SYS_SOCKETVAR_H_ */