]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/socketvar.h
xnu-124.13.tar.gz
[apple/xnu.git] / bsd / sys / socketvar.h
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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 *
56 * @(#)socketvar.h 8.1 (Berkeley) 6/2/93
57 */
58
59#ifndef _SYS_SOCKETVAR_H_
60#define _SYS_SOCKETVAR_H_
61
62#include <sys/select.h> /* for struct selinfo */
63#include <sys/queue.h>
64#include <net/kext_net.h>
65#include <sys/ev.h>
66/*
67 * Hacks to get around compiler complaints
68 */
69struct mbuf;
70struct socket;
71struct uio;
72struct sockbuf;
73struct sockaddr;
74struct kextcb;
75struct protosw;
76struct sockif;
77struct sockutil;
78
79/* strings for sleep message: */
80extern char netio[], netcon[], netcls[];
81#define SOCKET_CACHE_ON
82#define SO_CACHE_FLUSH_INTERVAL 1 /* Seconds */
83#define SO_CACHE_TIME_LIMIT (120/SO_CACHE_FLUSH_INTERVAL) /* Seconds */
84#define SO_CACHE_MAX_FREE_BATCH 50
85#define MAX_CACHED_SOCKETS 60000
86#define TEMPDEBUG 0
87
88/*
89 * Kernel structure per socket.
90 * Contains send and receive buffer queues,
91 * handle on protocol and pointer to protocol
92 * private data and error information.
93 */
94typedef u_quad_t so_gen_t;
95
96struct socket {
97 int so_zone; /* zone we were allocated from */
98 short so_type; /* generic type, see socket.h */
99 short so_options; /* from socket call, see socket.h */
100 short so_linger; /* time to linger while closing */
101 short so_state; /* internal state flags SS_*, below */
102 caddr_t so_pcb; /* protocol control block */
103 struct protosw *so_proto; /* protocol handle */
104/*
105 * Variables for connection queueing.
106 * Socket where accepts occur is so_head in all subsidiary sockets.
107 * If so_head is 0, socket is not related to an accept.
108 * For head socket so_q0 queues partially completed connections,
109 * while so_q is a queue of connections ready to be accepted.
110 * If a connection is aborted and it has so_head set, then
111 * it has to be pulled out of either so_q0 or so_q.
112 * We allow connections to queue up based on current queue lengths
113 * and limit on number of queued connections for this socket.
114 */
115 struct socket *so_head; /* back pointer to accept socket */
116 TAILQ_HEAD(, socket) so_incomp; /* queue of partial unaccepted connections */
117 TAILQ_HEAD(, socket) so_comp; /* queue of complete unaccepted connections */
118 TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */
119 short so_qlen; /* number of unaccepted connections */
120 short so_incqlen; /* number of unaccepted incomplete
121 connections */
122 short so_qlimit; /* max number queued connections */
123 short so_timeo; /* connection timeout */
124 u_short so_error; /* error affecting connection */
125 pid_t so_pgid; /* pgid for signals */
126 u_long so_oobmark; /* chars to oob mark */
127/*
128 * Variables for socket buffering.
129 */
130 struct sockbuf {
131 u_long sb_cc; /* actual chars in buffer */
132 u_long sb_hiwat; /* max actual char count */
133 u_long sb_mbcnt; /* chars of mbufs used */
134 u_long sb_mbmax; /* max chars of mbufs to use */
135 long sb_lowat; /* low water mark */
136 struct mbuf *sb_mb; /* the mbuf chain */
137 struct socket *sb_so; /* socket back ptr */
138 struct selinfo sb_sel; /* process selecting read/write */
139 short sb_flags; /* flags, see below */
140 short sb_timeo; /* timeout for read/write */
141 } so_rcv, so_snd;
142#define SB_MAX (256*1024) /* default for max chars in sockbuf */
143#define SB_LOCK 0x01 /* lock on data queue */
144#define SB_WANT 0x02 /* someone is waiting to lock */
145#define SB_WAIT 0x04 /* someone is waiting for data/space */
e3027f41 146#define SB_SEL_XXX 0x08 /* Don't use. replaced by SI_SBSEL in selinfo */
1c79356b 147#define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
e3027f41 148#define SB_NOTIFY (SB_WAIT|SB_ASYNC)
1c79356b
A
149#define SB_UPCALL 0x20 /* someone wants an upcall */
150#define SB_NOINTR 0x40 /* operations not interruptible */
151#define SB_RECV 0x8000 /* this is rcv sb */
152
153 caddr_t so_tpcb; /* Wisc. protocol control block XXX */
154 void (*so_upcall) __P((struct socket *so, caddr_t arg, int waitf));
155 caddr_t so_upcallarg; /* Arg for above */
156 uid_t so_uid; /* who opened the socket */
157 /* NB: generation count must not be first; easiest to make it last. */
158 so_gen_t so_gencnt; /* generation count */
159 TAILQ_HEAD(,eventqelt) so_evlist;
160 int cached_in_sock_layer; /* Is socket bundled with pcb/pcb.inp_ppcb? */
161 struct socket *cache_next;
162 struct socket *cache_prev;
163 u_long cache_timestamp;
164 caddr_t so_saved_pcb; /* Saved pcb when cacheing */
165 struct mbuf *so_temp; /* Holding area for outbound frags */
166 /* Plug-in support - make the socket interface overridable */
167 struct mbuf *so_tail;
168 struct kextcb *so_ext; /* NKE hook */
169};
170
171/*
172 * Socket state bits.
173 */
174#define SS_NOFDREF 0x001 /* no file table ref any more */
175#define SS_ISCONNECTED 0x002 /* socket connected to a peer */
176#define SS_ISCONNECTING 0x004 /* in process of connecting to peer */
177#define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */
178#define SS_CANTSENDMORE 0x010 /* can't send more data to peer */
179#define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */
180#define SS_RCVATMARK 0x040 /* at mark on input */
181
182#define SS_PRIV 0x080 /* privileged for broadcast, raw... */
183#define SS_NBIO 0x100 /* non-blocking ops */
184#define SS_ASYNC 0x200 /* async i/o notify */
185#define SS_ISCONFIRMING 0x400 /* deciding to accept connection req */
186#define SS_INCOMP 0x800 /* Unaccepted, incomplete connection */
187#define SS_COMP 0x1000 /* unaccepted, complete connection */
188
189/*
190 * Externalized form of struct socket used by the sysctl(3) interface.
191 */
192struct xsocket {
193 size_t xso_len; /* length of this structure */
194 struct socket *xso_so; /* makes a convenient handle sometimes */
195 short so_type;
196 short so_options;
197 short so_linger;
198 short so_state;
199 caddr_t so_pcb; /* another convenient handle */
200 int xso_protocol;
201 int xso_family;
202 short so_qlen;
203 short so_incqlen;
204 short so_qlimit;
205 short so_timeo;
206 u_short so_error;
207 pid_t so_pgid;
208 u_long so_oobmark;
209 struct xsockbuf {
210 u_long sb_cc;
211 u_long sb_hiwat;
212 u_long sb_mbcnt;
213 u_long sb_mbmax;
214 long sb_lowat;
215 short sb_flags;
216 short sb_timeo;
217 } so_rcv, so_snd;
218 uid_t so_uid; /* XXX */
219};
220
221/*
222 * Macros for sockets and socket buffering.
223 */
224#define sbtoso(sb) (sb->sb_so)
225
226/*
227 * Do we need to notify the other side when I/O is possible?
228 */
e3027f41 229#define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT|SB_ASYNC|SB_UPCALL)) != 0 || ((sb)->sb_sel.si_flags & SI_SBSEL) != 0)
1c79356b
A
230
231/*
232 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
233 * This is problematical if the fields are unsigned, as the space might
234 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
235 * overflow and return 0. Should use "lmin" but it doesn't exist now.
236 */
237#define sbspace(sb) \
238 ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
239 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
240
241/* do we have to send all at once on a socket? */
242#define sosendallatonce(so) \
243 ((so)->so_proto->pr_flags & PR_ATOMIC)
244
245/* can we read something from so? */
246#define soreadable(so) \
247 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
248 ((so)->so_state & SS_CANTRCVMORE) || \
249 (so)->so_comp.tqh_first || (so)->so_error)
250
251/* can we write something to so? */
252#define sowriteable(so) \
253 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
254 (((so)->so_state&SS_ISCONNECTED) || \
255 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
256 ((so)->so_state & SS_CANTSENDMORE) || \
257 (so)->so_error)
258
259/* adjust counters in sb reflecting allocation of m */
260#define sballoc(sb, m) { \
261 (sb)->sb_cc += (m)->m_len; \
262 (sb)->sb_mbcnt += MSIZE; \
263 if ((m)->m_flags & M_EXT) \
264 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
265}
266
267/* adjust counters in sb reflecting freeing of m */
268#define sbfree(sb, m) { \
269 (sb)->sb_cc -= (m)->m_len; \
270 (sb)->sb_mbcnt -= MSIZE; \
271 if ((m)->m_flags & M_EXT) \
272 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
273}
274
275/*
276 * Set lock on sockbuf sb; sleep if lock is already held.
277 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
278 * Returns error without lock if sleep is interrupted.
279 */
280#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
281 (((wf) == M_WAIT) ? sb_lock(sb) : EWOULDBLOCK) : \
282 ((sb)->sb_flags |= SB_LOCK), 0)
283
284/* release lock on sockbuf sb */
285#define sbunlock(sb) { \
286 (sb)->sb_flags &= ~SB_LOCK; \
287 if ((sb)->sb_flags & SB_WANT) { \
288 (sb)->sb_flags &= ~SB_WANT; \
289 wakeup((caddr_t)&(sb)->sb_flags); \
290 } \
291}
292
293#define sorwakeup(so) do { \
294 if (sb_notify(&(so)->so_rcv)) \
295 sowakeup((so), &(so)->so_rcv); \
296 } while (0)
297
298#define sowwakeup(so) do { \
299 if (sb_notify(&(so)->so_snd)) \
300 sowakeup((so), &(so)->so_snd); \
301 } while (0)
302
303
304/*
305 * Socket extension mechanism: control block hooks:
306 * This is the "head" of any control block for an extenstion
307 * Note: we separate intercept function dispatch vectors from
308 * the NFDescriptor to permit selective replacement during
309 * operation, e.g., to disable some functions.
310 */
311struct kextcb
312{ struct kextcb *e_next; /* Next kext control block */
313 void *e_fcb; /* Real filter control block */
314 struct NFDescriptor *e_nfd; /* NKE Descriptor */
315 /* Plug-in support - intercept functions */
316 struct sockif *e_soif; /* Socket functions */
317 struct sockutil *e_sout; /* Sockbuf utility functions */
318};
319#define EXT_NULL 0x0 /* STATE: Not in use */
320#define sotokextcb(so) (so ? so->so_ext : 0)
321
322#ifdef KERNEL
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 */
332 void *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
339struct sf_buf {
340 SLIST_ENTRY(sf_buf) free_list; /* list of free buffer slots */
341 int refcnt; /* reference count */
342 struct vm_page *m; /* currently mapped page */
343 vm_offset_t kva; /* va of mapping */
344};
345
346#endif
347
348#ifdef MALLOC_DECLARE
349MALLOC_DECLARE(M_PCB);
350MALLOC_DECLARE(M_SONAME);
351#endif
352
353extern int maxsockets;
354extern u_long sb_max;
355extern int socket_zone;
356extern so_gen_t so_gencnt;
357
358struct file;
359struct filedesc;
360struct mbuf;
361struct sockaddr;
362struct stat;
363struct ucred;
364struct uio;
365
366/*
367 * File operations on sockets.
368 */
369int soo_read __P((struct file *fp, struct uio *uio, struct ucred *cred));
370int soo_write __P((struct file *fp, struct uio *uio, struct ucred *cred));
371int soo_ioctl __P((struct file *fp, u_long cmd, caddr_t data,
372 struct proc *p));
373int soo_select __P((struct file *fp, int which, struct proc *p));
374int soo_stat __P((struct socket *so, struct stat *ub));
375
376int soo_close __P((struct file *fp, struct proc *p));
377
378
379/*
380 * From uipc_socket and friends
381 */
382struct sockaddr *dup_sockaddr __P((struct sockaddr *sa, int canwait));
383int getsock __P((struct filedesc *fdp, int fd, struct file **fpp));
384int sockargs __P((struct mbuf **mp, caddr_t buf, int buflen, int type));
385int getsockaddr __P((struct sockaddr **namp, caddr_t uaddr, size_t len));
386void sbappend __P((struct sockbuf *sb, struct mbuf *m));
387int sbappendaddr __P((struct sockbuf *sb, struct sockaddr *asa,
388 struct mbuf *m0, struct mbuf *control));
389int sbappendcontrol __P((struct sockbuf *sb, struct mbuf *m0,
390 struct mbuf *control));
391void sbappendrecord __P((struct sockbuf *sb, struct mbuf *m0));
392void sbcheck __P((struct sockbuf *sb));
393void sbcompress __P((struct sockbuf *sb, struct mbuf *m, struct mbuf *n));
394struct mbuf *
395 sbcreatecontrol __P((caddr_t p, int size, int type, int level));
396void sbdrop __P((struct sockbuf *sb, int len));
397void sbdroprecord __P((struct sockbuf *sb));
398void sbflush __P((struct sockbuf *sb));
399void sbinsertoob __P((struct sockbuf *sb, struct mbuf *m0));
400void sbrelease __P((struct sockbuf *sb));
401int sbreserve __P((struct sockbuf *sb, u_long cc));
402void sbtoxsockbuf __P((struct sockbuf *sb, struct xsockbuf *xsb));
403int sbwait __P((struct sockbuf *sb));
404int sb_lock __P((struct sockbuf *sb));
405int soabort __P((struct socket *so));
406int soaccept __P((struct socket *so, struct sockaddr **nam));
407struct socket *soalloc __P((int waitok, int dom, int type));
408int sobind __P((struct socket *so, struct sockaddr *nam));
409void socantrcvmore __P((struct socket *so));
410void socantsendmore __P((struct socket *so));
411int soclose __P((struct socket *so));
412int soconnect __P((struct socket *so, struct sockaddr *nam));
413int soconnect2 __P((struct socket *so1, struct socket *so2));
414int socreate __P((int dom, struct socket **aso, int type, int proto));
415void sodealloc __P((struct socket *so));
416int sodisconnect __P((struct socket *so));
417void sofree __P((struct socket *so));
418int sogetopt __P((struct socket *so, struct sockopt *sopt));
419void sohasoutofband __P((struct socket *so));
420void soisconnected __P((struct socket *so));
421void soisconnecting __P((struct socket *so));
422void soisdisconnected __P((struct socket *so));
423void soisdisconnecting __P((struct socket *so));
424int solisten __P((struct socket *so, int backlog));
425struct socket *
426 sodropablereq __P((struct socket *head));
427struct socket *
428 sonewconn __P((struct socket *head, int connstatus));
429int sooptcopyin __P((struct sockopt *sopt, void *buf, size_t len,
430 size_t minlen));
431int sooptcopyout __P((struct sockopt *sopt, void *buf, size_t len));
432int sopoll __P((struct socket *so, int events, struct ucred *cred));
433int soreceive __P((struct socket *so, struct sockaddr **paddr,
434 struct uio *uio, struct mbuf **mp0,
435 struct mbuf **controlp, int *flagsp));
436int soreserve __P((struct socket *so, u_long sndcc, u_long rcvcc));
437void sorflush __P((struct socket *so));
438int sosend __P((struct socket *so, struct sockaddr *addr, struct uio *uio,
439 struct mbuf *top, struct mbuf *control, int flags));
440
441int sosetopt __P((struct socket *so, struct sockopt *sopt));
442
443
444int soshutdown __P((struct socket *so, int how));
445void sotoxsocket __P((struct socket *so, struct xsocket *xso));
446void sowakeup __P((struct socket *so, struct sockbuf *sb));
447
448
449#endif /* KERNEL */
450#endif /* !_SYS_SOCKETVAR_H_ */