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