FreeBSD/Linux Kernel Cross Reference
sys/sys/socketvar.h
1 /* $NetBSD: socketvar.h,v 1.116 2008/10/14 13:45:26 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*-
30 * Copyright (c) 1982, 1986, 1990, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * @(#)socketvar.h 8.3 (Berkeley) 2/19/95
58 */
59
60 #ifndef _SYS_SOCKETVAR_H_
61 #define _SYS_SOCKETVAR_H_
62
63 #include <sys/select.h>
64 #include <sys/selinfo.h> /* for struct selinfo */
65 #include <sys/queue.h>
66 #include <sys/mutex.h>
67 #include <sys/condvar.h>
68 #ifdef ACCEPT_FILTER_MOD
69 #include <sys/lkm.h>
70 #endif
71
72 #if !defined(_KERNEL)
73 struct uio;
74 struct lwp;
75 struct uidinfo;
76 #else
77 #include <sys/uidinfo.h>
78 #endif
79
80 TAILQ_HEAD(soqhead, socket);
81
82 /*
83 * Variables for socket buffering.
84 */
85 struct sockbuf {
86 struct selinfo sb_sel; /* process selecting read/write */
87 struct mowner *sb_mowner; /* who owns data for this sockbuf */
88 struct socket *sb_so; /* back pointer to socket */
89 kcondvar_t sb_cv; /* notifier */
90 /* When re-zeroing this struct, we zero from sb_startzero to the end */
91 #define sb_startzero sb_cc
92 u_long sb_cc; /* actual chars in buffer */
93 u_long sb_hiwat; /* max actual char count */
94 u_long sb_mbcnt; /* chars of mbufs used */
95 u_long sb_mbmax; /* max chars of mbufs to use */
96 long sb_lowat; /* low water mark */
97 struct mbuf *sb_mb; /* the mbuf chain */
98 struct mbuf *sb_mbtail; /* the last mbuf in the chain */
99 struct mbuf *sb_lastrecord; /* first mbuf of last record in
100 socket buffer */
101 int sb_flags; /* flags, see below */
102 int sb_timeo; /* timeout for read/write */
103 u_long sb_overflowed; /* # of drops due to full buffer */
104 };
105
106 #ifndef SB_MAX
107 #define SB_MAX (256*1024) /* default for max chars in sockbuf */
108 #endif
109
110 #define SB_LOCK 0x01 /* lock on data queue */
111 #define SB_NOTIFY 0x04 /* someone is waiting for data/space */
112 #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
113 #define SB_UPCALL 0x20 /* someone wants an upcall */
114 #define SB_NOINTR 0x40 /* operations not interruptible */
115 #define SB_KNOTE 0x100 /* kernel note attached */
116 #define SB_AUTOSIZE 0x800 /* automatically size socket buffer */
117
118 /*
119 * Kernel structure per socket.
120 * Contains send and receive buffer queues,
121 * handle on protocol and pointer to protocol
122 * private data and error information.
123 */
124 struct socket {
125 kmutex_t * volatile so_lock; /* pointer to lock on structure */
126 kcondvar_t so_cv; /* notifier */
127 short so_type; /* generic type, see socket.h */
128 short so_options; /* from socket call, see socket.h */
129 u_short so_linger; /* time to linger while closing */
130 short so_state; /* internal state flags SS_*, below */
131 int so_nbio; /* non-blocking I/O enabled */
132 void *so_pcb; /* protocol control block */
133 const struct protosw *so_proto; /* protocol handle */
134 /*
135 * Variables for connection queueing.
136 * Socket where accepts occur is so_head in all subsidiary sockets.
137 * If so_head is 0, socket is not related to an accept.
138 * For head socket so_q0 queues partially completed connections,
139 * while so_q is a queue of connections ready to be accepted.
140 * If a connection is aborted and it has so_head set, then
141 * it has to be pulled out of either so_q0 or so_q.
142 * We allow connections to queue up based on current queue lengths
143 * and limit on number of queued connections for this socket.
144 */
145 struct socket *so_head; /* back pointer to accept socket */
146 struct soqhead *so_onq; /* queue (q or q0) that we're on */
147 struct soqhead so_q0; /* queue of partial connections */
148 struct soqhead so_q; /* queue of incoming connections */
149 TAILQ_ENTRY(socket) so_qe; /* our queue entry (q or q0) */
150 short so_q0len; /* partials on so_q0 */
151 short so_qlen; /* number of connections on so_q */
152 short so_qlimit; /* max number queued connections */
153 short so_timeo; /* connection timeout */
154 u_short so_error; /* error affecting connection */
155 u_short so_aborting; /* references from soabort() */
156 pid_t so_pgid; /* pgid for signals */
157 u_long so_oobmark; /* chars to oob mark */
158 struct sockbuf so_snd; /* send buffer */
159 struct sockbuf so_rcv; /* receive buffer */
160
161 void *so_internal; /* Space for svr4 stream data */
162 void (*so_upcall) (struct socket *, void *, int);
163 void * so_upcallarg; /* Arg for above */
164 int (*so_send) (struct socket *, struct mbuf *,
165 struct uio *, struct mbuf *,
166 struct mbuf *, int, struct lwp *);
167 int (*so_receive) (struct socket *,
168 struct mbuf **,
169 struct uio *, struct mbuf **,
170 struct mbuf **, int *);
171 struct mowner *so_mowner; /* who owns mbufs for this socket */
172 struct uidinfo *so_uidinfo; /* who opened the socket */
173 gid_t so_egid; /* creator effective gid */
174 pid_t so_cpid; /* creator pid */
175 struct so_accf {
176 struct accept_filter *so_accept_filter;
177 void *so_accept_filter_arg; /* saved filter args */
178 char *so_accept_filter_str; /* saved user args */
179 } *so_accf;
180 };
181
182 #define SB_EMPTY_FIXUP(sb) \
183 do { \
184 KASSERT(solocked((sb)->sb_so)); \
185 if ((sb)->sb_mb == NULL) { \
186 (sb)->sb_mbtail = NULL; \
187 (sb)->sb_lastrecord = NULL; \
188 } \
189 } while (/*CONSTCOND*/0)
190
191 /*
192 * Socket state bits.
193 */
194 #define SS_NOFDREF 0x001 /* no file table ref any more */
195 #define SS_ISCONNECTED 0x002 /* socket connected to a peer */
196 #define SS_ISCONNECTING 0x004 /* in process of connecting to peer */
197 #define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */
198 #define SS_CANTSENDMORE 0x010 /* can't send more data to peer */
199 #define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */
200 #define SS_RCVATMARK 0x040 /* at mark on input */
201 #define SS_ISDISCONNECTED 0x800 /* socket disconnected from peer */
202
203 #define SS_ASYNC 0x100 /* async i/o notify */
204 #define SS_ISCONFIRMING 0x200 /* deciding to accept connection req */
205 #define SS_MORETOCOME 0x400 /*
206 * hint from sosend to lower layer;
207 * more data coming
208 */
209 #define SS_ISAPIPE 0x1000 /* socket is implementing a pipe */
210
211 #ifdef _KERNEL
212
213 struct accept_filter {
214 char accf_name[16];
215 void (*accf_callback)
216 (struct socket *so, void *arg, int waitflag);
217 void * (*accf_create)
218 (struct socket *so, char *arg);
219 void (*accf_destroy)
220 (struct socket *so);
221 LIST_ENTRY(accept_filter) accf_next;
222 u_int accf_refcnt;
223 };
224
225 struct sockopt {
226 int sopt_level; /* option level */
227 int sopt_name; /* option name */
228 size_t sopt_size; /* data length */
229 void * sopt_data; /* data pointer */
230 uint8_t sopt_buf[sizeof(int)]; /* internal storage */
231 };
232
233 extern u_long sb_max;
234 extern int somaxkva;
235 extern int sock_loan_thresh;
236 extern kmutex_t *softnet_lock;
237
238 struct mbuf;
239 struct sockaddr;
240 struct lwp;
241 struct msghdr;
242 struct stat;
243 struct knote;
244
245 struct mbuf *getsombuf(struct socket *, int);
246
247 /*
248 * File operations on sockets.
249 */
250 int soo_read(file_t *, off_t *, struct uio *, kauth_cred_t, int);
251 int soo_write(file_t *, off_t *, struct uio *, kauth_cred_t, int);
252 int soo_fcntl(file_t *, u_int cmd, void *);
253 int soo_ioctl(file_t *, u_long cmd, void *);
254 int soo_poll(file_t *, int);
255 int soo_kqfilter(file_t *, struct knote *);
256 int soo_close(file_t *);
257 int soo_stat(file_t *, struct stat *);
258 void sbappend(struct sockbuf *, struct mbuf *);
259 void sbappendstream(struct sockbuf *, struct mbuf *);
260 int sbappendaddr(struct sockbuf *, const struct sockaddr *, struct mbuf *,
261 struct mbuf *);
262 int sbappendaddrchain(struct sockbuf *, const struct sockaddr *,
263 struct mbuf *, int);
264 int sbappendcontrol(struct sockbuf *, struct mbuf *, struct mbuf *);
265 void sbappendrecord(struct sockbuf *, struct mbuf *);
266 void sbcheck(struct sockbuf *);
267 void sbcompress(struct sockbuf *, struct mbuf *, struct mbuf *);
268 struct mbuf *
269 sbcreatecontrol(void *, int, int, int);
270 void sbdrop(struct sockbuf *, int);
271 void sbdroprecord(struct sockbuf *);
272 void sbflush(struct sockbuf *);
273 void sbinsertoob(struct sockbuf *, struct mbuf *);
274 void sbrelease(struct sockbuf *, struct socket *);
275 int sbreserve(struct sockbuf *, u_long, struct socket *);
276 int sbwait(struct sockbuf *);
277 int sb_max_set(u_long);
278 void soinit(void);
279 void soinit2(void);
280 int soabort(struct socket *);
281 int soaccept(struct socket *, struct mbuf *);
282 int sobind(struct socket *, struct mbuf *, struct lwp *);
283 void socantrcvmore(struct socket *);
284 void socantsendmore(struct socket *);
285 int soclose(struct socket *);
286 int soconnect(struct socket *, struct mbuf *, struct lwp *);
287 int soconnect2(struct socket *, struct socket *);
288 int socreate(int, struct socket **, int, int, struct lwp *,
289 struct socket *);
290 int fsocreate(int, struct socket **, int, int, struct lwp *, int *);
291 int sodisconnect(struct socket *);
292 void sofree(struct socket *);
293 int sogetopt(struct socket *, struct sockopt *);
294 void sohasoutofband(struct socket *);
295 void soisconnected(struct socket *);
296 void soisconnecting(struct socket *);
297 void soisdisconnected(struct socket *);
298 void soisdisconnecting(struct socket *);
299 int solisten(struct socket *, int, struct lwp *);
300 struct socket *
301 sonewconn(struct socket *, int);
302 void soqinsque(struct socket *, struct socket *, int);
303 int soqremque(struct socket *, int);
304 int soreceive(struct socket *, struct mbuf **, struct uio *,
305 struct mbuf **, struct mbuf **, int *);
306 int soreserve(struct socket *, u_long, u_long);
307 void sorflush(struct socket *);
308 int sosend(struct socket *, struct mbuf *, struct uio *,
309 struct mbuf *, struct mbuf *, int, struct lwp *);
310 int sosetopt(struct socket *, struct sockopt *);
311 int so_setsockopt(struct lwp *, struct socket *, int, int, const void *, size_t);
312 int soshutdown(struct socket *, int);
313 void sowakeup(struct socket *, struct sockbuf *, int);
314 int sockargs(struct mbuf **, const void *, size_t, int);
315 int sopoll(struct socket *, int);
316 struct socket *soget(bool);
317 void soput(struct socket *);
318 bool solocked(struct socket *);
319 bool solocked2(struct socket *, struct socket *);
320 int sblock(struct sockbuf *, int);
321 void sbunlock(struct sockbuf *);
322 int sowait(struct socket *, int);
323 void solockretry(struct socket *, kmutex_t *);
324 void sosetlock(struct socket *);
325 void solockreset(struct socket *, kmutex_t *);
326
327 void sockopt_init(struct sockopt *, int, int, size_t);
328 void sockopt_destroy(struct sockopt *);
329 int sockopt_set(struct sockopt *, const void *, size_t);
330 int sockopt_setint(struct sockopt *, int);
331 int sockopt_get(const struct sockopt *, void *, size_t);
332 int sockopt_getint(const struct sockopt *, int *);
333 int sockopt_setmbuf(struct sockopt *, struct mbuf *);
334 struct mbuf *sockopt_getmbuf(const struct sockopt *);
335
336 int copyout_sockname(struct sockaddr *, unsigned int *, int, struct mbuf *);
337 int copyout_msg_control(struct lwp *, struct msghdr *, struct mbuf *);
338 void free_control_mbuf(struct lwp *, struct mbuf *, struct mbuf *);
339
340 int do_sys_getsockname(struct lwp *, int, int, struct mbuf **);
341 int do_sys_sendmsg(struct lwp *, int, struct msghdr *, int, register_t *);
342 int do_sys_recvmsg(struct lwp *, int, struct msghdr *, struct mbuf **,
343 struct mbuf **, register_t *);
344
345 int do_sys_bind(struct lwp *, int, struct mbuf *);
346 int do_sys_connect(struct lwp *, int, struct mbuf *);
347 int do_sys_accept(struct lwp *, int, struct mbuf **, register_t *);
348
349 /*
350 * Inline functions for sockets and socket buffering.
351 */
352
353 #include <sys/protosw.h>
354 #include <sys/mbuf.h>
355
356 /*
357 * Do we need to notify the other side when I/O is possible?
358 */
359 static inline int
360 sb_notify(struct sockbuf *sb)
361 {
362
363 KASSERT(solocked(sb->sb_so));
364
365 return sb->sb_flags & (SB_NOTIFY | SB_ASYNC | SB_UPCALL | SB_KNOTE);
366 }
367
368 /*
369 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
370 * This is problematical if the fields are unsigned, as the space might
371 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
372 * overflow and return 0.
373 */
374 static inline long
375 sbspace(struct sockbuf *sb)
376 {
377
378 KASSERT(solocked(sb->sb_so));
379
380 return lmin(sb->sb_hiwat - sb->sb_cc, sb->sb_mbmax - sb->sb_mbcnt);
381 }
382
383 /* do we have to send all at once on a socket? */
384 static inline int
385 sosendallatonce(struct socket *so)
386 {
387
388 return so->so_proto->pr_flags & PR_ATOMIC;
389 }
390
391 /* can we read something from so? */
392 static inline int
393 soreadable(struct socket *so)
394 {
395
396 KASSERT(solocked(so));
397
398 return so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
399 (so->so_state & SS_CANTRCVMORE) != 0 ||
400 so->so_qlen != 0 || so->so_error != 0;
401 }
402
403 /* can we write something to so? */
404 static inline int
405 sowritable(struct socket *so)
406 {
407
408 KASSERT(solocked(so));
409
410 return (sbspace(&so->so_snd) >= so->so_snd.sb_lowat &&
411 ((so->so_state & SS_ISCONNECTED) != 0 ||
412 (so->so_proto->pr_flags & PR_CONNREQUIRED) == 0)) ||
413 (so->so_state & SS_CANTSENDMORE) != 0 ||
414 so->so_error != 0;
415 }
416
417 /* adjust counters in sb reflecting allocation of m */
418 static inline void
419 sballoc(struct sockbuf *sb, struct mbuf *m)
420 {
421
422 KASSERT(solocked(sb->sb_so));
423
424 sb->sb_cc += m->m_len;
425 sb->sb_mbcnt += MSIZE;
426 if (m->m_flags & M_EXT)
427 sb->sb_mbcnt += m->m_ext.ext_size;
428 }
429
430 /* adjust counters in sb reflecting freeing of m */
431 static inline void
432 sbfree(struct sockbuf *sb, struct mbuf *m)
433 {
434
435 KASSERT(solocked(sb->sb_so));
436
437 sb->sb_cc -= m->m_len;
438 sb->sb_mbcnt -= MSIZE;
439 if (m->m_flags & M_EXT)
440 sb->sb_mbcnt -= m->m_ext.ext_size;
441 }
442
443 static inline void
444 sorwakeup(struct socket *so)
445 {
446
447 KASSERT(solocked(so));
448
449 if (sb_notify(&so->so_rcv))
450 sowakeup(so, &so->so_rcv, POLL_IN);
451 }
452
453 static inline void
454 sowwakeup(struct socket *so)
455 {
456
457 KASSERT(solocked(so));
458
459 if (sb_notify(&so->so_snd))
460 sowakeup(so, &so->so_snd, POLL_OUT);
461 }
462
463 static inline void
464 solock(struct socket *so)
465 {
466 kmutex_t *lock;
467
468 lock = so->so_lock;
469 mutex_enter(lock);
470 if (__predict_false(lock != so->so_lock))
471 solockretry(so, lock);
472 }
473
474 static inline void
475 sounlock(struct socket *so)
476 {
477
478 mutex_exit(so->so_lock);
479 }
480
481 #ifdef SOCKBUF_DEBUG
482 /*
483 * SBLASTRECORDCHK: check sb->sb_lastrecord is maintained correctly.
484 * SBLASTMBUFCHK: check sb->sb_mbtail is maintained correctly.
485 *
486 * => panic if the socket buffer is inconsistent.
487 * => 'where' is used for a panic message.
488 */
489 void sblastrecordchk(struct sockbuf *, const char *);
490 #define SBLASTRECORDCHK(sb, where) sblastrecordchk((sb), (where))
491
492 void sblastmbufchk(struct sockbuf *, const char *);
493 #define SBLASTMBUFCHK(sb, where) sblastmbufchk((sb), (where))
494 #define SBCHECK(sb) sbcheck(sb)
495 #else
496 #define SBLASTRECORDCHK(sb, where) /* nothing */
497 #define SBLASTMBUFCHK(sb, where) /* nothing */
498 #define SBCHECK(sb) /* nothing */
499 #endif /* SOCKBUF_DEBUG */
500
501 /* sosend loan */
502 vaddr_t sokvaalloc(vsize_t, struct socket *);
503 void sokvafree(vaddr_t, vsize_t);
504 void soloanfree(struct mbuf *, void *, size_t, void *);
505
506 /*
507 * Values for socket-buffer-append priority argument to sbappendaddrchain().
508 * The following flags are reserved for future implementation:
509 *
510 * SB_PRIO_NONE: honour normal socket-buffer limits.
511 *
512 * SB_PRIO_ONESHOT_OVERFLOW: if the socket has any space,
513 * deliver the entire chain. Intended for large requests
514 * that should be delivered in their entirety, or not at all.
515 *
516 * SB_PRIO_OVERDRAFT: allow a small (2*MLEN) overflow, over and
517 * aboce normal socket limits. Intended messages indicating
518 * buffer overflow in earlier normal/lower-priority messages .
519 *
520 * SB_PRIO_BESTEFFORT: Ignore limits entirely. Intended only for
521 * kernel-generated messages to specially-marked scokets which
522 * require "reliable" delivery, nd where the source socket/protocol
523 * message generator enforce some hard limit (but possibly well
524 * above kern.sbmax). It is entirely up to the in-kernel source to
525 * avoid complete mbuf exhaustion or DoS scenarios.
526 */
527 #define SB_PRIO_NONE 0
528 #define SB_PRIO_ONESHOT_OVERFLOW 1
529 #define SB_PRIO_OVERDRAFT 2
530 #define SB_PRIO_BESTEFFORT 3
531
532 /*
533 * Accept filter functions (duh).
534 */
535 int accept_filt_getopt(struct socket *, struct sockopt *);
536 int accept_filt_setopt(struct socket *, const struct sockopt *);
537 int accept_filt_clear(struct socket *);
538 int accept_filt_add(struct accept_filter *);
539 int accept_filt_del(struct accept_filter *);
540 struct accept_filter *accept_filt_get(char *);
541 #ifdef ACCEPT_FILTER_MOD
542 #ifdef SYSCTL_DECL
543 SYSCTL_DECL(_net_inet_accf);
544 #endif
545 void accept_filter_init(void);
546 int accept_filt_generic_mod_event(struct lkm_table *lkmtp, int event, void *data);
547 #endif
548
549 #endif /* _KERNEL */
550
551 #endif /* !_SYS_SOCKETVAR_H_ */
Cache object: f0bf2fb621220cf34987999949203bcf
|