Annotation of src/sys/sys/socketvar.h, Revision 1.100.2.1
1.100.2.1! ad 1: /* $NetBSD: socketvar.h,v 1.101 2007/12/16 21:25:59 elad Exp $ */
1.11 cgd 2:
1.1 cgd 3: /*-
1.8 mycroft 4: * Copyright (c) 1982, 1986, 1990, 1993
5: * The Regents of the University of California. All rights reserved.
1.1 cgd 6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
1.65 agc 15: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: *
1.28 fvdl 31: * @(#)socketvar.h 8.3 (Berkeley) 2/19/95
1.1 cgd 32: */
33:
1.24 mikel 34: #ifndef _SYS_SOCKETVAR_H_
1.42 lukem 35: #define _SYS_SOCKETVAR_H_
1.24 mikel 36:
1.81 kleink 37: #include <sys/select.h>
38: #include <sys/selinfo.h> /* for struct selinfo */
1.27 thorpej 39: #include <sys/queue.h>
40:
1.38 simonb 41: #if !defined(_KERNEL) || defined(LKM)
1.29 matt 42: struct uio;
1.85 christos 43: struct lwp;
1.82 christos 44: struct uidinfo;
1.29 matt 45: #endif
46:
1.27 thorpej 47: TAILQ_HEAD(soqhead, socket);
1.3 mycroft 48:
1.1 cgd 49: /*
1.58 matt 50: * Variables for socket buffering.
51: */
52: struct sockbuf {
1.66 wrstuden 53: struct selinfo sb_sel; /* process selecting read/write */
54: struct mowner *sb_mowner; /* who owns data for this sockbuf */
55: /* When re-zeroing this struct, we zero from sb_startzero to the end */
56: #define sb_startzero sb_cc
1.58 matt 57: u_long sb_cc; /* actual chars in buffer */
58: u_long sb_hiwat; /* max actual char count */
59: u_long sb_mbcnt; /* chars of mbufs used */
60: u_long sb_mbmax; /* max chars of mbufs to use */
61: long sb_lowat; /* low water mark */
62: struct mbuf *sb_mb; /* the mbuf chain */
63: struct mbuf *sb_mbtail; /* the last mbuf in the chain */
64: struct mbuf *sb_lastrecord; /* first mbuf of last record in
65: socket buffer */
1.79 yamt 66: int sb_flags; /* flags, see below */
67: int sb_timeo; /* timeout for read/write */
1.80 darrenr 68: u_long sb_overflowed; /* # of drops due to full buffer */
1.58 matt 69: };
70:
71: #ifndef SB_MAX
72: #define SB_MAX (256*1024) /* default for max chars in sockbuf */
73: #endif
74:
75: #define SB_LOCK 0x01 /* lock on data queue */
76: #define SB_WANT 0x02 /* someone is waiting to lock */
77: #define SB_WAIT 0x04 /* someone is waiting for data/space */
78: #define SB_SEL 0x08 /* someone is selecting */
79: #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
80: #define SB_UPCALL 0x20 /* someone wants an upcall */
81: #define SB_NOINTR 0x40 /* operations not interruptible */
82: /* XXXLUKEM: 0x80 left for FreeBSD's SB_AIO */
83: #define SB_KNOTE 0x100 /* kernel note attached */
1.98 rmind 84: #define SB_AUTOSIZE 0x800 /* automatically size socket buffer */
1.58 matt 85:
86: /*
1.1 cgd 87: * Kernel structure per socket.
88: * Contains send and receive buffer queues,
89: * handle on protocol and pointer to protocol
90: * private data and error information.
91: */
92: struct socket {
1.42 lukem 93: short so_type; /* generic type, see socket.h */
94: short so_options; /* from socket call, see socket.h */
1.92 christos 95: u_short so_linger; /* time to linger while closing */
1.42 lukem 96: short so_state; /* internal state flags SS_*, below */
97: void *so_pcb; /* protocol control block */
1.74 matt 98: const struct protosw *so_proto; /* protocol handle */
1.1 cgd 99: /*
100: * Variables for connection queueing.
101: * Socket where accepts occur is so_head in all subsidiary sockets.
102: * If so_head is 0, socket is not related to an accept.
103: * For head socket so_q0 queues partially completed connections,
104: * while so_q is a queue of connections ready to be accepted.
105: * If a connection is aborted and it has so_head set, then
106: * it has to be pulled out of either so_q0 or so_q.
107: * We allow connections to queue up based on current queue lengths
108: * and limit on number of queued connections for this socket.
109: */
1.42 lukem 110: struct socket *so_head; /* back pointer to accept socket */
111: struct soqhead *so_onq; /* queue (q or q0) that we're on */
112: struct soqhead so_q0; /* queue of partial connections */
113: struct soqhead so_q; /* queue of incoming connections */
1.27 thorpej 114: TAILQ_ENTRY(socket) so_qe; /* our queue entry (q or q0) */
1.42 lukem 115: short so_q0len; /* partials on so_q0 */
116: short so_qlen; /* number of connections on so_q */
117: short so_qlimit; /* max number queued connections */
118: short so_timeo; /* connection timeout */
119: u_short so_error; /* error affecting connection */
120: pid_t so_pgid; /* pgid for signals */
121: u_long so_oobmark; /* chars to oob mark */
1.58 matt 122: struct sockbuf so_snd; /* send buffer */
123: struct sockbuf so_rcv; /* receive buffer */
1.1 cgd 124:
1.42 lukem 125: void *so_internal; /* Space for svr4 stream data */
1.93 christos 126: void (*so_upcall) (struct socket *, void *, int);
127: void * so_upcallarg; /* Arg for above */
1.71 matt 128: int (*so_send) (struct socket *, struct mbuf *,
1.64 itojun 129: struct uio *, struct mbuf *,
1.83 christos 130: struct mbuf *, int, struct lwp *);
1.71 matt 131: int (*so_receive) (struct socket *,
1.64 itojun 132: struct mbuf **,
133: struct uio *, struct mbuf **,
1.71 matt 134: struct mbuf **, int *);
1.58 matt 135: struct mowner *so_mowner; /* who owns mbufs for this socket */
1.82 christos 136: struct uidinfo *so_uidinfo; /* who opened the socket */
1.1 cgd 137: };
138:
1.54 thorpej 139: #define SB_EMPTY_FIXUP(sb) \
1.52 thorpej 140: do { \
141: if ((sb)->sb_mb == NULL) { \
142: (sb)->sb_mbtail = NULL; \
143: (sb)->sb_lastrecord = NULL; \
144: } \
145: } while (/*CONSTCOND*/0)
146:
1.1 cgd 147: /*
148: * Socket state bits.
149: */
150: #define SS_NOFDREF 0x001 /* no file table ref any more */
151: #define SS_ISCONNECTED 0x002 /* socket connected to a peer */
152: #define SS_ISCONNECTING 0x004 /* in process of connecting to peer */
153: #define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */
154: #define SS_CANTSENDMORE 0x010 /* can't send more data to peer */
155: #define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */
156: #define SS_RCVATMARK 0x040 /* at mark on input */
1.33 mycroft 157: #define SS_ISDISCONNECTED 0x800 /* socket disconnected from peer */
1.1 cgd 158:
1.19 mycroft 159: #define SS_NBIO 0x080 /* non-blocking ops */
160: #define SS_ASYNC 0x100 /* async i/o notify */
161: #define SS_ISCONFIRMING 0x200 /* deciding to accept connection req */
1.42 lukem 162: #define SS_MORETOCOME 0x400 /*
163: * hint from sosend to lower layer;
164: * more data coming
165: */
1.94 yamt 166: #define SS_ISAPIPE 0x1000 /* socket is implementing a pipe */
1.1 cgd 167:
168:
169: /*
170: * Macros for sockets and socket buffering.
171: */
1.15 mycroft 172:
173: /*
174: * Do we need to notify the other side when I/O is possible?
175: */
1.42 lukem 176: #define sb_notify(sb) (((sb)->sb_flags & \
1.55 jdolecek 177: (SB_WAIT | SB_SEL | SB_ASYNC | SB_UPCALL | SB_KNOTE)) != 0)
1.1 cgd 178:
179: /*
180: * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
181: * This is problematical if the fields are unsigned, as the space might
182: * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
1.50 jdolecek 183: * overflow and return 0.
1.1 cgd 184: */
185: #define sbspace(sb) \
1.50 jdolecek 186: (lmin((sb)->sb_hiwat - (sb)->sb_cc, (sb)->sb_mbmax - (sb)->sb_mbcnt))
1.1 cgd 187:
188: /* do we have to send all at once on a socket? */
189: #define sosendallatonce(so) \
1.42 lukem 190: ((so)->so_proto->pr_flags & PR_ATOMIC)
1.1 cgd 191:
192: /* can we read something from so? */
193: #define soreadable(so) \
1.42 lukem 194: ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
195: ((so)->so_state & SS_CANTRCVMORE) || \
196: (so)->so_qlen || (so)->so_error)
1.1 cgd 197:
198: /* can we write something to so? */
1.56 wiz 199: #define sowritable(so) \
1.42 lukem 200: ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
201: (((so)->so_state&SS_ISCONNECTED) || \
202: ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
203: ((so)->so_state & SS_CANTSENDMORE) || \
204: (so)->so_error)
1.1 cgd 205:
206: /* adjust counters in sb reflecting allocation of m */
1.42 lukem 207: #define sballoc(sb, m) \
208: do { \
209: (sb)->sb_cc += (m)->m_len; \
210: (sb)->sb_mbcnt += MSIZE; \
211: if ((m)->m_flags & M_EXT) \
212: (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
1.45 lukem 213: } while (/* CONSTCOND */ 0)
1.1 cgd 214:
215: /* adjust counters in sb reflecting freeing of m */
1.42 lukem 216: #define sbfree(sb, m) \
217: do { \
218: (sb)->sb_cc -= (m)->m_len; \
219: (sb)->sb_mbcnt -= MSIZE; \
220: if ((m)->m_flags & M_EXT) \
221: (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
1.45 lukem 222: } while (/* CONSTCOND */ 0)
1.1 cgd 223:
224: /*
225: * Set lock on sockbuf sb; sleep if lock is already held.
226: * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
227: * Returns error without lock if sleep is interrupted.
228: */
1.42 lukem 229: #define sblock(sb, wf) \
230: ((sb)->sb_flags & SB_LOCK ? \
231: (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
1.97 christos 232: ((sb)->sb_flags |= SB_LOCK, 0))
1.1 cgd 233:
234: /* release lock on sockbuf sb */
1.42 lukem 235: #define sbunlock(sb) \
236: do { \
237: (sb)->sb_flags &= ~SB_LOCK; \
238: if ((sb)->sb_flags & SB_WANT) { \
239: (sb)->sb_flags &= ~SB_WANT; \
1.93 christos 240: wakeup((void *)&(sb)->sb_flags); \
1.42 lukem 241: } \
1.45 lukem 242: } while (/* CONSTCOND */ 0)
1.42 lukem 243:
244: #define sorwakeup(so) \
245: do { \
246: if (sb_notify(&(so)->so_rcv)) \
1.67 christos 247: sowakeup((so), &(so)->so_rcv, POLL_IN); \
1.45 lukem 248: } while (/* CONSTCOND */ 0)
1.42 lukem 249:
250: #define sowwakeup(so) \
251: do { \
252: if (sb_notify(&(so)->so_snd)) \
1.67 christos 253: sowakeup((so), &(so)->so_snd, POLL_OUT); \
1.45 lukem 254: } while (/* CONSTCOND */ 0)
1.1 cgd 255:
1.14 jtc 256: #ifdef _KERNEL
1.42 lukem 257: extern u_long sb_max;
1.63 ragge 258: extern int somaxkva;
1.88 yamt 259: extern int sock_loan_thresh;
1.1 cgd 260:
261: /* strings for sleep message: */
1.42 lukem 262: extern const char netio[], netcon[], netcls[];
1.1 cgd 263:
1.42 lukem 264: extern struct pool socket_pool;
1.31 thorpej 265:
1.17 christos 266: struct mbuf;
267: struct sockaddr;
1.84 christos 268: struct lwp;
1.17 christos 269: struct msghdr;
270: struct stat;
1.55 jdolecek 271: struct knote;
1.17 christos 272:
1.99 dyoung 273: struct mbuf *m_intopt(struct socket *, int);
1.100 dyoung 274: struct mbuf *getsombuf(struct socket *, int);
1.99 dyoung 275:
1.1 cgd 276: /*
277: * File operations on sockets.
278: */
1.87 elad 279: int soo_read(struct file *, off_t *, struct uio *, kauth_cred_t, int);
280: int soo_write(struct file *, off_t *, struct uio *, kauth_cred_t, int);
1.83 christos 281: int soo_fcntl(struct file *, u_int cmd, void *, struct lwp *);
282: int soo_ioctl(struct file *, u_long cmd, void *, struct lwp *);
283: int soo_poll(struct file *, int, struct lwp *);
1.64 itojun 284: int soo_kqfilter(struct file *, struct knote *);
1.83 christos 285: int soo_close(struct file *, struct lwp *);
286: int soo_stat(struct file *, struct stat *, struct lwp *);
1.64 itojun 287: void sbappend(struct sockbuf *, struct mbuf *);
288: void sbappendstream(struct sockbuf *, struct mbuf *);
1.73 matt 289: int sbappendaddr(struct sockbuf *, const struct sockaddr *, struct mbuf *,
1.64 itojun 290: struct mbuf *);
1.76 jonathan 291: int sbappendaddrchain(struct sockbuf *, const struct sockaddr *,
292: struct mbuf *, int);
1.64 itojun 293: int sbappendcontrol(struct sockbuf *, struct mbuf *, struct mbuf *);
294: void sbappendrecord(struct sockbuf *, struct mbuf *);
295: void sbcheck(struct sockbuf *);
296: void sbcompress(struct sockbuf *, struct mbuf *, struct mbuf *);
1.23 thorpej 297: struct mbuf *
1.93 christos 298: sbcreatecontrol(void *, int, int, int);
1.64 itojun 299: void sbdrop(struct sockbuf *, int);
300: void sbdroprecord(struct sockbuf *);
301: void sbflush(struct sockbuf *);
302: void sbinsertoob(struct sockbuf *, struct mbuf *);
1.70 christos 303: void sbrelease(struct sockbuf *, struct socket *);
304: int sbreserve(struct sockbuf *, u_long, struct socket *);
1.64 itojun 305: int sbwait(struct sockbuf *);
306: int sb_lock(struct sockbuf *);
1.68 thorpej 307: int sb_max_set(u_long);
1.42 lukem 308: void soinit(void);
1.64 itojun 309: int soabort(struct socket *);
310: int soaccept(struct socket *, struct mbuf *);
1.83 christos 311: int sobind(struct socket *, struct mbuf *, struct lwp *);
1.64 itojun 312: void socantrcvmore(struct socket *);
313: void socantsendmore(struct socket *);
314: int soclose(struct socket *);
1.83 christos 315: int soconnect(struct socket *, struct mbuf *, struct lwp *);
1.64 itojun 316: int soconnect2(struct socket *, struct socket *);
1.83 christos 317: int socreate(int, struct socket **, int, int, struct lwp *);
1.99 dyoung 318: int fsocreate(int, struct socket **, int, int, struct lwp *, int *);
1.64 itojun 319: int sodisconnect(struct socket *);
320: void sofree(struct socket *);
321: int sogetopt(struct socket *, int, int, struct mbuf **);
322: void sohasoutofband(struct socket *);
323: void soisconnected(struct socket *);
324: void soisconnecting(struct socket *);
325: void soisdisconnected(struct socket *);
326: void soisdisconnecting(struct socket *);
1.100.2.1! ad 327: int solisten(struct socket *, int, struct lwp *);
1.7 mycroft 328: struct socket *
1.89 plunky 329: sonewconn(struct socket *, int);
1.64 itojun 330: void soqinsque(struct socket *, struct socket *, int);
331: int soqremque(struct socket *, int);
332: int soreceive(struct socket *, struct mbuf **, struct uio *,
333: struct mbuf **, struct mbuf **, int *);
334: int soreserve(struct socket *, u_long, u_long);
335: void sorflush(struct socket *);
336: int sosend(struct socket *, struct mbuf *, struct uio *,
1.83 christos 337: struct mbuf *, struct mbuf *, int, struct lwp *);
1.64 itojun 338: int sosetopt(struct socket *, int, int, struct mbuf *);
339: int soshutdown(struct socket *, int);
1.67 christos 340: void sowakeup(struct socket *, struct sockbuf *, int);
1.49 matt 341: int sockargs(struct mbuf **, const void *, size_t, int);
1.42 lukem 342:
1.96 dsl 343: int copyout_sockname(struct sockaddr *, unsigned int *, int, struct mbuf *);
344: int copyout_msg_control(struct lwp *, struct msghdr *, struct mbuf *);
345: void free_control_mbuf(struct lwp *, struct mbuf *, struct mbuf *);
346:
347:
348: int do_sys_getsockname(struct lwp *, int, int, struct mbuf **);
1.95 dsl 349: int do_sys_sendmsg(struct lwp *, int, struct msghdr *, int, register_t *);
1.96 dsl 350: int do_sys_recvmsg(struct lwp *, int, struct msghdr *, struct mbuf **,
351: struct mbuf **, register_t *);
1.52 thorpej 352:
1.96 dsl 353: int do_sys_bind(struct lwp *, int, struct mbuf *);
354: int do_sys_connect(struct lwp *, int, struct mbuf *);
355: int do_sys_accept(struct lwp *, int, struct mbuf **, register_t *);
1.95 dsl 356:
357:
1.52 thorpej 358: #ifdef SOCKBUF_DEBUG
1.78 yamt 359: /*
360: * SBLASTRECORDCHK: check sb->sb_lastrecord is maintained correctly.
361: * SBLASTMBUFCHK: check sb->sb_mbtail is maintained correctly.
362: *
363: * => panic if the socket buffer is inconsistent.
364: * => 'where' is used for a panic message.
365: */
1.52 thorpej 366: void sblastrecordchk(struct sockbuf *, const char *);
367: #define SBLASTRECORDCHK(sb, where) sblastrecordchk((sb), (where))
368:
369: void sblastmbufchk(struct sockbuf *, const char *);
370: #define SBLASTMBUFCHK(sb, where) sblastmbufchk((sb), (where))
371: #else
372: #define SBLASTRECORDCHK(sb, where) /* nothing */
373: #define SBLASTMBUFCHK(sb, where) /* nothing */
374: #endif /* SOCKBUF_DEBUG */
1.60 yamt 375:
376: /* sosend loan */
377: vaddr_t sokvaalloc(vsize_t, struct socket *);
378: void sokvafree(vaddr_t, vsize_t);
1.93 christos 379: void soloanfree(struct mbuf *, void *, size_t, void *);
1.17 christos 380:
1.76 jonathan 381: /*
382: * Values for socket-buffer-append priority argument to sbappendaddrchain().
383: * The following flags are reserved for future implementation:
384: *
385: * SB_PRIO_NONE: honour normal socket-buffer limits.
386: *
387: * SB_PRIO_ONESHOT_OVERFLOW: if the socket has any space,
388: * deliver the entire chain. Intended for large requests
389: * that should be delivered in their entirety, or not at all.
390: *
391: * SB_PRIO_OVERDRAFT: allow a small (2*MLEN) overflow, over and
392: * aboce normal socket limits. Intended messages indicating
393: * buffer overflow in earlier normal/lower-priority messages .
394: *
395: * SB_PRIO_BESTEFFORT: Ignore limits entirely. Intended only for
396: * kernel-generated messages to specially-marked scokets which
397: * require "reliable" delivery, nd where the source socket/protocol
398: * message generator enforce some hard limit (but possibly well
399: * above kern.sbmax). It is entirely up to the in-kernel source to
400: * avoid complete mbuf exhaustion or DoS scenarios.
401: */
402: #define SB_PRIO_NONE 0
403: #define SB_PRIO_ONESHOT_OVERFLOW 1
404: #define SB_PRIO_OVERDRAFT 2
405: #define SB_PRIO_BESTEFFORT 3
406:
1.14 jtc 407: #endif /* _KERNEL */
1.24 mikel 408:
409: #endif /* !_SYS_SOCKETVAR_H_ */
CVSweb <webmaster@jp.NetBSD.org>