Annotation of src/sys/kern/uipc_socket.c, Revision 1.111.2.5
1.111.2.5! yamt 1: /* $NetBSD: uipc_socket.c,v 1.111.2.4 2006/07/07 12:30:51 yamt Exp $ */
1.64 thorpej 2:
3: /*-
4: * Copyright (c) 2002 The NetBSD Foundation, Inc.
5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
8: * by Jason R. Thorpe of Wasabi Systems, Inc.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the NetBSD
21: * Foundation, Inc. and its contributors.
22: * 4. Neither the name of The NetBSD Foundation nor the names of its
23: * contributors may be used to endorse or promote products derived
24: * from this software without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36: * POSSIBILITY OF SUCH DAMAGE.
37: */
1.16 cgd 38:
1.1 cgd 39: /*
1.15 mycroft 40: * Copyright (c) 1982, 1986, 1988, 1990, 1993
41: * The Regents of the University of California. All rights reserved.
1.1 cgd 42: *
43: * Redistribution and use in source and binary forms, with or without
44: * modification, are permitted provided that the following conditions
45: * are met:
46: * 1. Redistributions of source code must retain the above copyright
47: * notice, this list of conditions and the following disclaimer.
48: * 2. Redistributions in binary form must reproduce the above copyright
49: * notice, this list of conditions and the following disclaimer in the
50: * documentation and/or other materials provided with the distribution.
1.85 agc 51: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 52: * may be used to endorse or promote products derived from this software
53: * without specific prior written permission.
54: *
55: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65: * SUCH DAMAGE.
66: *
1.32 fvdl 67: * @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95
1.1 cgd 68: */
1.59 lukem 69:
70: #include <sys/cdefs.h>
1.111.2.5! yamt 71: __KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.111.2.4 2006/07/07 12:30:51 yamt Exp $");
1.64 thorpej 72:
73: #include "opt_sock_counters.h"
74: #include "opt_sosend_loan.h"
1.81 martin 75: #include "opt_mbuftrace.h"
1.84 ragge 76: #include "opt_somaxkva.h"
1.1 cgd 77:
1.9 mycroft 78: #include <sys/param.h>
79: #include <sys/systm.h>
80: #include <sys/proc.h>
81: #include <sys/file.h>
82: #include <sys/malloc.h>
83: #include <sys/mbuf.h>
84: #include <sys/domain.h>
85: #include <sys/kernel.h>
86: #include <sys/protosw.h>
87: #include <sys/socket.h>
88: #include <sys/socketvar.h>
1.21 christos 89: #include <sys/signalvar.h>
1.9 mycroft 90: #include <sys/resourcevar.h>
1.37 thorpej 91: #include <sys/pool.h>
1.72 jdolecek 92: #include <sys/event.h>
1.89 christos 93: #include <sys/poll.h>
1.111.2.3 yamt 94: #include <sys/kauth.h>
1.37 thorpej 95:
1.64 thorpej 96: #include <uvm/uvm.h>
97:
1.100 simonb 98: POOL_INIT(socket_pool, sizeof(struct socket), 0, 0, 0, "sockpl", NULL);
1.77 thorpej 99:
100: MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options");
101: MALLOC_DEFINE(M_SONAME, "soname", "socket name");
1.37 thorpej 102:
1.54 lukem 103: extern int somaxconn; /* patchable (XXX sysctl) */
104: int somaxconn = SOMAXCONN;
1.49 jonathan 105:
1.64 thorpej 106: #ifdef SOSEND_COUNTERS
107: #include <sys/device.h>
108:
1.111.2.3 yamt 109: static struct evcnt sosend_loan_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
1.64 thorpej 110: NULL, "sosend", "loan big");
1.111.2.3 yamt 111: static struct evcnt sosend_copy_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
1.64 thorpej 112: NULL, "sosend", "copy big");
1.111.2.3 yamt 113: static struct evcnt sosend_copy_small = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
1.64 thorpej 114: NULL, "sosend", "copy small");
1.111.2.3 yamt 115: static struct evcnt sosend_kvalimit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
1.64 thorpej 116: NULL, "sosend", "kva limit");
117:
118: #define SOSEND_COUNTER_INCR(ev) (ev)->ev_count++
119:
1.101 matt 120: EVCNT_ATTACH_STATIC(sosend_loan_big);
121: EVCNT_ATTACH_STATIC(sosend_copy_big);
122: EVCNT_ATTACH_STATIC(sosend_copy_small);
123: EVCNT_ATTACH_STATIC(sosend_kvalimit);
1.64 thorpej 124: #else
125:
126: #define SOSEND_COUNTER_INCR(ev) /* nothing */
127:
128: #endif /* SOSEND_COUNTERS */
129:
1.111.2.3 yamt 130: static struct callback_entry sokva_reclaimerentry;
1.1 cgd 131:
1.71 thorpej 132: #ifdef SOSEND_NO_LOAN
1.111.2.3 yamt 133: int sock_loan_thresh = -1;
1.71 thorpej 134: #else
1.111.2.3 yamt 135: int sock_loan_thresh = 4096;
1.65 thorpej 136: #endif
1.64 thorpej 137:
1.111.2.3 yamt 138: static struct simplelock so_pendfree_slock = SIMPLELOCK_INITIALIZER;
139: static struct mbuf *so_pendfree;
1.64 thorpej 140:
1.84 ragge 141: #ifndef SOMAXKVA
142: #define SOMAXKVA (16 * 1024 * 1024)
143: #endif
144: int somaxkva = SOMAXKVA;
1.111.2.3 yamt 145: static int socurkva;
146: static int sokvawaiters;
1.64 thorpej 147:
148: #define SOCK_LOAN_CHUNK 65536
149:
1.111.2.3 yamt 150: static size_t sodopendfree(void);
151: static size_t sodopendfreel(void);
1.93 yamt 152:
1.111.2.3 yamt 153: static vsize_t
1.95 yamt 154: sokvareserve(struct socket *so, vsize_t len)
1.80 yamt 155: {
156: int s;
1.98 christos 157: int error;
1.80 yamt 158:
1.93 yamt 159: s = splvm();
160: simple_lock(&so_pendfree_slock);
1.80 yamt 161: while (socurkva + len > somaxkva) {
1.93 yamt 162: size_t freed;
163:
164: /*
165: * try to do pendfree.
166: */
167:
1.111.2.3 yamt 168: freed = sodopendfreel();
1.93 yamt 169:
170: /*
171: * if some kva was freed, try again.
172: */
173:
174: if (freed)
1.80 yamt 175: continue;
1.93 yamt 176:
1.80 yamt 177: SOSEND_COUNTER_INCR(&sosend_kvalimit);
178: sokvawaiters++;
1.98 christos 179: error = ltsleep(&socurkva, PVM | PCATCH, "sokva", 0,
180: &so_pendfree_slock);
1.80 yamt 181: sokvawaiters--;
1.98 christos 182: if (error) {
183: len = 0;
184: break;
185: }
1.80 yamt 186: }
1.93 yamt 187: socurkva += len;
188: simple_unlock(&so_pendfree_slock);
189: splx(s);
1.98 christos 190: return len;
1.95 yamt 191: }
192:
1.111.2.3 yamt 193: static void
1.95 yamt 194: sokvaunreserve(vsize_t len)
195: {
196: int s;
197:
198: s = splvm();
199: simple_lock(&so_pendfree_slock);
200: socurkva -= len;
201: if (sokvawaiters)
202: wakeup(&socurkva);
203: simple_unlock(&so_pendfree_slock);
204: splx(s);
205: }
206:
207: /*
208: * sokvaalloc: allocate kva for loan.
209: */
210:
211: vaddr_t
212: sokvaalloc(vsize_t len, struct socket *so)
213: {
214: vaddr_t lva;
215:
216: /*
217: * reserve kva.
218: */
219:
1.98 christos 220: if (sokvareserve(so, len) == 0)
221: return 0;
1.93 yamt 222:
223: /*
224: * allocate kva.
225: */
1.80 yamt 226:
1.109 yamt 227: lva = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
1.95 yamt 228: if (lva == 0) {
229: sokvaunreserve(len);
1.80 yamt 230: return (0);
1.95 yamt 231: }
1.80 yamt 232:
233: return lva;
234: }
235:
1.93 yamt 236: /*
237: * sokvafree: free kva for loan.
238: */
239:
1.80 yamt 240: void
241: sokvafree(vaddr_t sva, vsize_t len)
242: {
1.93 yamt 243:
244: /*
245: * free kva.
246: */
1.80 yamt 247:
1.109 yamt 248: uvm_km_free(kernel_map, sva, len, UVM_KMF_VAONLY);
1.93 yamt 249:
250: /*
251: * unreserve kva.
252: */
253:
1.95 yamt 254: sokvaunreserve(len);
1.80 yamt 255: }
256:
1.64 thorpej 257: static void
1.111.2.1 yamt 258: sodoloanfree(struct vm_page **pgs, caddr_t buf, size_t size, boolean_t mapped)
1.64 thorpej 259: {
1.111.2.1 yamt 260: vaddr_t sva, eva;
1.64 thorpej 261: vsize_t len;
1.111.2.1 yamt 262: int npgs;
263:
264: KASSERT(pgs != NULL);
1.64 thorpej 265:
266: eva = round_page((vaddr_t) buf + size);
267: sva = trunc_page((vaddr_t) buf);
268: len = eva - sva;
269: npgs = len >> PAGE_SHIFT;
270:
1.111.2.1 yamt 271: if (mapped) {
272: pmap_kremove(sva, len);
273: pmap_update(pmap_kernel());
1.64 thorpej 274: }
275: uvm_unloan(pgs, npgs, UVM_LOAN_TOPAGE);
1.80 yamt 276: sokvafree(sva, len);
1.64 thorpej 277: }
278:
279: static size_t
1.111.2.3 yamt 280: sodopendfree()
1.64 thorpej 281: {
282: int s;
1.93 yamt 283: size_t rv;
1.64 thorpej 284:
285: s = splvm();
1.93 yamt 286: simple_lock(&so_pendfree_slock);
1.111.2.3 yamt 287: rv = sodopendfreel();
1.93 yamt 288: simple_unlock(&so_pendfree_slock);
289: splx(s);
290:
291: return rv;
292: }
293:
294: /*
295: * sodopendfreel: free mbufs on "pendfree" list.
296: * unlock and relock so_pendfree_slock when freeing mbufs.
297: *
298: * => called with so_pendfree_slock held.
299: * => called at splvm.
300: */
301:
302: static size_t
1.111.2.3 yamt 303: sodopendfreel()
1.93 yamt 304: {
305: size_t rv = 0;
306:
307: LOCK_ASSERT(simple_lock_held(&so_pendfree_slock));
1.64 thorpej 308:
309: for (;;) {
1.93 yamt 310: struct mbuf *m;
311: struct mbuf *next;
312:
1.64 thorpej 313: m = so_pendfree;
314: if (m == NULL)
315: break;
1.93 yamt 316: so_pendfree = NULL;
317: simple_unlock(&so_pendfree_slock);
318: /* XXX splx */
319:
320: for (; m != NULL; m = next) {
321: next = m->m_next;
1.111.2.1 yamt 322: KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0);
1.111.2.4 yamt 323: KASSERT(m->m_ext.ext_refcnt == 0);
1.93 yamt 324:
325: rv += m->m_ext.ext_size;
1.111.2.1 yamt 326: sodoloanfree(m->m_ext.ext_pgs, m->m_ext.ext_buf,
327: m->m_ext.ext_size,
328: (m->m_ext.ext_flags & M_EXT_LAZY) == 0);
1.93 yamt 329: pool_cache_put(&mbpool_cache, m);
330: }
1.64 thorpej 331:
1.93 yamt 332: /* XXX splvm */
333: simple_lock(&so_pendfree_slock);
1.64 thorpej 334: }
335:
336: return (rv);
337: }
338:
1.80 yamt 339: void
1.76 thorpej 340: soloanfree(struct mbuf *m, caddr_t buf, size_t size, void *arg)
1.64 thorpej 341: {
342: int s;
343:
1.111.2.1 yamt 344: KASSERT(m != NULL);
1.64 thorpej 345:
1.93 yamt 346: /*
347: * postpone freeing mbuf.
348: *
349: * we can't do it in interrupt context
350: * because we need to put kva back to kernel_map.
351: */
352:
1.64 thorpej 353: s = splvm();
1.93 yamt 354: simple_lock(&so_pendfree_slock);
1.92 yamt 355: m->m_next = so_pendfree;
356: so_pendfree = m;
1.64 thorpej 357: if (sokvawaiters)
358: wakeup(&socurkva);
1.93 yamt 359: simple_unlock(&so_pendfree_slock);
360: splx(s);
1.64 thorpej 361: }
362:
363: static long
364: sosend_loan(struct socket *so, struct uio *uio, struct mbuf *m, long space)
365: {
366: struct iovec *iov = uio->uio_iov;
367: vaddr_t sva, eva;
368: vsize_t len;
1.111.2.1 yamt 369: vaddr_t lva;
370: int npgs, error;
1.111.2.2 yamt 371: #if !defined(__HAVE_LAZY_MBUF)
1.111.2.1 yamt 372: vaddr_t va;
373: int i;
1.111.2.2 yamt 374: #endif /* !defined(__HAVE_LAZY_MBUF) */
1.64 thorpej 375:
1.111.2.3 yamt 376: if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace))
1.64 thorpej 377: return (0);
378:
379: if (iov->iov_len < (size_t) space)
380: space = iov->iov_len;
381: if (space > SOCK_LOAN_CHUNK)
382: space = SOCK_LOAN_CHUNK;
383:
384: eva = round_page((vaddr_t) iov->iov_base + space);
385: sva = trunc_page((vaddr_t) iov->iov_base);
386: len = eva - sva;
387: npgs = len >> PAGE_SHIFT;
388:
1.79 thorpej 389: /* XXX KDASSERT */
390: KASSERT(npgs <= M_EXT_MAXPAGES);
391:
1.80 yamt 392: lva = sokvaalloc(len, so);
1.64 thorpej 393: if (lva == 0)
1.80 yamt 394: return 0;
1.64 thorpej 395:
1.111.2.3 yamt 396: error = uvm_loan(&uio->uio_vmspace->vm_map, sva, len,
1.79 thorpej 397: m->m_ext.ext_pgs, UVM_LOAN_TOPAGE);
1.64 thorpej 398: if (error) {
1.80 yamt 399: sokvafree(lva, len);
1.64 thorpej 400: return (0);
401: }
402:
1.111.2.2 yamt 403: #if !defined(__HAVE_LAZY_MBUF)
1.64 thorpej 404: for (i = 0, va = lva; i < npgs; i++, va += PAGE_SIZE)
1.79 thorpej 405: pmap_kenter_pa(va, VM_PAGE_TO_PHYS(m->m_ext.ext_pgs[i]),
406: VM_PROT_READ);
1.64 thorpej 407: pmap_update(pmap_kernel());
1.111.2.2 yamt 408: #endif /* !defined(__HAVE_LAZY_MBUF) */
1.64 thorpej 409:
410: lva += (vaddr_t) iov->iov_base & PAGE_MASK;
411:
412: MEXTADD(m, (caddr_t) lva, space, M_MBUF, soloanfree, so);
1.79 thorpej 413: m->m_flags |= M_EXT_PAGES | M_EXT_ROMAP;
1.64 thorpej 414:
1.111.2.2 yamt 415: #if defined(__HAVE_LAZY_MBUF)
1.111.2.1 yamt 416: m->m_flags |= M_EXT_LAZY;
417: m->m_ext.ext_flags |= M_EXT_LAZY;
1.111.2.2 yamt 418: #endif /* defined(__HAVE_LAZY_MBUF) */
1.111.2.1 yamt 419:
1.64 thorpej 420: uio->uio_resid -= space;
421: /* uio_offset not updated, not set/used for write(2) */
422: uio->uio_iov->iov_base = (caddr_t) uio->uio_iov->iov_base + space;
423: uio->uio_iov->iov_len -= space;
424: if (uio->uio_iov->iov_len == 0) {
425: uio->uio_iov++;
426: uio->uio_iovcnt--;
427: }
428:
429: return (space);
430: }
431:
1.111.2.3 yamt 432: static int
433: sokva_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)
434: {
435:
436: KASSERT(ce == &sokva_reclaimerentry);
437: KASSERT(obj == NULL);
438:
439: sodopendfree();
440: if (!vm_map_starved_p(kernel_map)) {
441: return CALLBACK_CHAIN_ABORT;
442: }
443: return CALLBACK_CHAIN_CONTINUE;
444: }
445:
446: void
447: soinit(void)
448: {
449:
450: /* Set the initial adjusted socket buffer size. */
451: if (sb_max_set(sb_max))
452: panic("bad initial sb_max value: %lu", sb_max);
453:
454: callback_register(&vm_map_to_kernel(kernel_map)->vmk_reclaim_callback,
455: &sokva_reclaimerentry, NULL, sokva_reclaim_callback);
456: }
457:
1.1 cgd 458: /*
459: * Socket operation routines.
460: * These routines are called by the routines in
461: * sys_socket.c or from a system process, and
462: * implement the semantics of socket operations by
463: * switching out to the protocol specific routines.
464: */
465: /*ARGSUSED*/
1.3 andrew 466: int
1.111.2.3 yamt 467: socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l)
1.1 cgd 468: {
1.99 matt 469: const struct protosw *prp;
1.54 lukem 470: struct socket *so;
1.111.2.3 yamt 471: uid_t uid;
1.54 lukem 472: int error, s;
1.1 cgd 473:
1.111.2.5! yamt 474: if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
! 475: KAUTH_REQ_NETWORK_SOCKET_OPEN, (void *)(u_long)dom,
! 476: (void *)(u_long)type, (void *)(u_long)proto) != 0)
! 477: return (EPERM);
! 478:
1.1 cgd 479: if (proto)
480: prp = pffindproto(dom, proto, type);
481: else
482: prp = pffindtype(dom, type);
1.111.2.3 yamt 483: if (prp == 0) {
484: /* no support for domain */
485: if (pffinddomain(dom) == 0)
486: return (EAFNOSUPPORT);
487: /* no support for socket type */
488: if (proto == 0 && type != 0)
489: return (EPROTOTYPE);
490: return (EPROTONOSUPPORT);
491: }
492: if (prp->pr_usrreq == 0)
1.1 cgd 493: return (EPROTONOSUPPORT);
494: if (prp->pr_type != type)
495: return (EPROTOTYPE);
1.39 matt 496: s = splsoftnet();
1.37 thorpej 497: so = pool_get(&socket_pool, PR_WAITOK);
1.38 perry 498: memset((caddr_t)so, 0, sizeof(*so));
1.31 thorpej 499: TAILQ_INIT(&so->so_q0);
500: TAILQ_INIT(&so->so_q);
1.1 cgd 501: so->so_type = type;
502: so->so_proto = prp;
1.33 matt 503: so->so_send = sosend;
504: so->so_receive = soreceive;
1.78 matt 505: #ifdef MBUFTRACE
506: so->so_rcv.sb_mowner = &prp->pr_domain->dom_mowner;
507: so->so_snd.sb_mowner = &prp->pr_domain->dom_mowner;
508: so->so_mowner = &prp->pr_domain->dom_mowner;
509: #endif
1.111.2.3 yamt 510: if (l != NULL) {
1.111.2.5! yamt 511: uid = kauth_cred_geteuid(l->l_cred);
1.111.2.3 yamt 512: } else {
513: uid = 0;
514: }
515: so->so_uidinfo = uid_find(uid);
1.22 mycroft 516: error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0,
1.111.2.3 yamt 517: (struct mbuf *)(long)proto, (struct mbuf *)0, l);
1.1 cgd 518: if (error) {
519: so->so_state |= SS_NOFDREF;
520: sofree(so);
1.39 matt 521: splx(s);
1.1 cgd 522: return (error);
523: }
1.39 matt 524: splx(s);
1.1 cgd 525: *aso = so;
526: return (0);
527: }
528:
1.3 andrew 529: int
1.111.2.3 yamt 530: sobind(struct socket *so, struct mbuf *nam, struct lwp *l)
1.1 cgd 531: {
1.54 lukem 532: int s, error;
1.1 cgd 533:
1.54 lukem 534: s = splsoftnet();
1.22 mycroft 535: error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, (struct mbuf *)0,
1.111.2.3 yamt 536: nam, (struct mbuf *)0, l);
1.1 cgd 537: splx(s);
538: return (error);
539: }
540:
1.3 andrew 541: int
1.54 lukem 542: solisten(struct socket *so, int backlog)
1.1 cgd 543: {
1.54 lukem 544: int s, error;
1.1 cgd 545:
1.54 lukem 546: s = splsoftnet();
1.22 mycroft 547: error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, (struct mbuf *)0,
1.111.2.3 yamt 548: (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1.1 cgd 549: if (error) {
550: splx(s);
551: return (error);
552: }
1.63 matt 553: if (TAILQ_EMPTY(&so->so_q))
1.1 cgd 554: so->so_options |= SO_ACCEPTCONN;
555: if (backlog < 0)
556: backlog = 0;
1.49 jonathan 557: so->so_qlimit = min(backlog, somaxconn);
1.1 cgd 558: splx(s);
559: return (0);
560: }
561:
1.21 christos 562: void
1.54 lukem 563: sofree(struct socket *so)
1.1 cgd 564: {
565:
1.43 mycroft 566: if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
1.1 cgd 567: return;
1.43 mycroft 568: if (so->so_head) {
569: /*
570: * We must not decommission a socket that's on the accept(2)
571: * queue. If we do, then accept(2) may hang after select(2)
572: * indicated that the listening socket was ready.
573: */
574: if (!soqremque(so, 0))
575: return;
576: }
1.98 christos 577: if (so->so_rcv.sb_hiwat)
1.110 christos 578: (void)chgsbsize(so->so_uidinfo, &so->so_rcv.sb_hiwat, 0,
1.98 christos 579: RLIM_INFINITY);
580: if (so->so_snd.sb_hiwat)
1.110 christos 581: (void)chgsbsize(so->so_uidinfo, &so->so_snd.sb_hiwat, 0,
1.98 christos 582: RLIM_INFINITY);
583: sbrelease(&so->so_snd, so);
1.1 cgd 584: sorflush(so);
1.37 thorpej 585: pool_put(&socket_pool, so);
1.1 cgd 586: }
587:
588: /*
589: * Close a socket on last file table reference removal.
590: * Initiate disconnect if connected.
591: * Free socket when disconnect complete.
592: */
1.3 andrew 593: int
1.54 lukem 594: soclose(struct socket *so)
1.1 cgd 595: {
1.54 lukem 596: struct socket *so2;
597: int s, error;
1.1 cgd 598:
1.54 lukem 599: error = 0;
600: s = splsoftnet(); /* conservative */
1.1 cgd 601: if (so->so_options & SO_ACCEPTCONN) {
1.63 matt 602: while ((so2 = TAILQ_FIRST(&so->so_q0)) != 0) {
1.42 mycroft 603: (void) soqremque(so2, 0);
1.41 mycroft 604: (void) soabort(so2);
605: }
1.63 matt 606: while ((so2 = TAILQ_FIRST(&so->so_q)) != 0) {
1.42 mycroft 607: (void) soqremque(so2, 1);
1.41 mycroft 608: (void) soabort(so2);
609: }
1.1 cgd 610: }
611: if (so->so_pcb == 0)
612: goto discard;
613: if (so->so_state & SS_ISCONNECTED) {
614: if ((so->so_state & SS_ISDISCONNECTING) == 0) {
615: error = sodisconnect(so);
616: if (error)
617: goto drop;
618: }
619: if (so->so_options & SO_LINGER) {
620: if ((so->so_state & SS_ISDISCONNECTING) &&
621: (so->so_state & SS_NBIO))
622: goto drop;
1.21 christos 623: while (so->so_state & SS_ISCONNECTED) {
624: error = tsleep((caddr_t)&so->so_timeo,
625: PSOCK | PCATCH, netcls,
1.30 thorpej 626: so->so_linger * hz);
1.21 christos 627: if (error)
1.1 cgd 628: break;
1.21 christos 629: }
1.1 cgd 630: }
631: }
1.54 lukem 632: drop:
1.1 cgd 633: if (so->so_pcb) {
1.22 mycroft 634: int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
635: (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
1.111.2.3 yamt 636: (struct lwp *)0);
1.1 cgd 637: if (error == 0)
638: error = error2;
639: }
1.54 lukem 640: discard:
1.1 cgd 641: if (so->so_state & SS_NOFDREF)
642: panic("soclose: NOFDREF");
643: so->so_state |= SS_NOFDREF;
644: sofree(so);
645: splx(s);
646: return (error);
647: }
648:
649: /*
1.20 mycroft 650: * Must be called at splsoftnet...
1.1 cgd 651: */
1.3 andrew 652: int
1.54 lukem 653: soabort(struct socket *so)
1.1 cgd 654: {
655:
1.22 mycroft 656: return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, (struct mbuf *)0,
1.111.2.3 yamt 657: (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1.1 cgd 658: }
659:
1.3 andrew 660: int
1.54 lukem 661: soaccept(struct socket *so, struct mbuf *nam)
1.1 cgd 662: {
1.54 lukem 663: int s, error;
1.1 cgd 664:
1.54 lukem 665: error = 0;
666: s = splsoftnet();
1.1 cgd 667: if ((so->so_state & SS_NOFDREF) == 0)
668: panic("soaccept: !NOFDREF");
669: so->so_state &= ~SS_NOFDREF;
1.55 thorpej 670: if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
671: (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
1.41 mycroft 672: error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
1.111.2.3 yamt 673: (struct mbuf *)0, nam, (struct mbuf *)0, (struct lwp *)0);
1.41 mycroft 674: else
1.53 itojun 675: error = ECONNABORTED;
1.52 itojun 676:
1.1 cgd 677: splx(s);
678: return (error);
679: }
680:
1.3 andrew 681: int
1.111.2.3 yamt 682: soconnect(struct socket *so, struct mbuf *nam, struct lwp *l)
1.1 cgd 683: {
1.54 lukem 684: int s, error;
1.1 cgd 685:
686: if (so->so_options & SO_ACCEPTCONN)
687: return (EOPNOTSUPP);
1.20 mycroft 688: s = splsoftnet();
1.1 cgd 689: /*
690: * If protocol is connection-based, can only connect once.
691: * Otherwise, if connected, try to disconnect first.
692: * This allows user to disconnect by connecting to, e.g.,
693: * a null address.
694: */
695: if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
696: ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
697: (error = sodisconnect(so))))
698: error = EISCONN;
699: else
700: error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
1.111.2.3 yamt 701: (struct mbuf *)0, nam, (struct mbuf *)0, l);
1.1 cgd 702: splx(s);
703: return (error);
704: }
705:
1.3 andrew 706: int
1.54 lukem 707: soconnect2(struct socket *so1, struct socket *so2)
1.1 cgd 708: {
1.54 lukem 709: int s, error;
1.1 cgd 710:
1.54 lukem 711: s = splsoftnet();
1.22 mycroft 712: error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
713: (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0,
1.111.2.3 yamt 714: (struct lwp *)0);
1.1 cgd 715: splx(s);
716: return (error);
717: }
718:
1.3 andrew 719: int
1.54 lukem 720: sodisconnect(struct socket *so)
1.1 cgd 721: {
1.54 lukem 722: int s, error;
1.1 cgd 723:
1.54 lukem 724: s = splsoftnet();
1.1 cgd 725: if ((so->so_state & SS_ISCONNECTED) == 0) {
726: error = ENOTCONN;
727: goto bad;
728: }
729: if (so->so_state & SS_ISDISCONNECTING) {
730: error = EALREADY;
731: goto bad;
732: }
1.22 mycroft 733: error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
734: (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
1.111.2.3 yamt 735: (struct lwp *)0);
1.54 lukem 736: bad:
1.1 cgd 737: splx(s);
1.111.2.3 yamt 738: sodopendfree();
1.1 cgd 739: return (error);
740: }
741:
1.15 mycroft 742: #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
1.1 cgd 743: /*
744: * Send on a socket.
745: * If send must go all at once and message is larger than
746: * send buffering, then hard error.
747: * Lock against other senders.
748: * If must go all at once and not enough room now, then
749: * inform user that this would block and do nothing.
750: * Otherwise, if nonblocking, send as much as possible.
751: * The data to be sent is described by "uio" if nonzero,
752: * otherwise by the mbuf chain "top" (which must be null
753: * if uio is not). Data provided in mbuf chain must be small
754: * enough to send all at once.
755: *
756: * Returns nonzero on error, timeout or signal; callers
757: * must check for short counts if EINTR/ERESTART are returned.
758: * Data and control buffers are freed on return.
759: */
1.3 andrew 760: int
1.54 lukem 761: sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
1.111.2.3 yamt 762: struct mbuf *control, int flags, struct lwp *l)
1.1 cgd 763: {
1.54 lukem 764: struct mbuf **mp, *m;
1.111.2.3 yamt 765: struct proc *p;
1.58 jdolecek 766: long space, len, resid, clen, mlen;
767: int error, s, dontroute, atomic;
1.54 lukem 768:
1.111.2.3 yamt 769: p = l->l_proc;
770: sodopendfree();
1.64 thorpej 771:
1.54 lukem 772: clen = 0;
773: atomic = sosendallatonce(so) || top;
1.1 cgd 774: if (uio)
775: resid = uio->uio_resid;
776: else
777: resid = top->m_pkthdr.len;
1.7 cgd 778: /*
779: * In theory resid should be unsigned.
780: * However, space must be signed, as it might be less than 0
781: * if we over-committed, and we must use a signed comparison
782: * of space and resid. On the other hand, a negative resid
783: * causes us to loop sending 0-length segments to the protocol.
784: */
1.29 mycroft 785: if (resid < 0) {
786: error = EINVAL;
787: goto out;
788: }
1.1 cgd 789: dontroute =
790: (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
791: (so->so_proto->pr_flags & PR_ATOMIC);
1.102 jonathan 792: if (p)
793: p->p_stats->p_ru.ru_msgsnd++;
1.1 cgd 794: if (control)
795: clen = control->m_len;
796: #define snderr(errno) { error = errno; splx(s); goto release; }
797:
1.54 lukem 798: restart:
1.21 christos 799: if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
1.1 cgd 800: goto out;
801: do {
1.20 mycroft 802: s = splsoftnet();
1.1 cgd 803: if (so->so_state & SS_CANTSENDMORE)
804: snderr(EPIPE);
1.48 thorpej 805: if (so->so_error) {
806: error = so->so_error;
807: so->so_error = 0;
808: splx(s);
809: goto release;
810: }
1.1 cgd 811: if ((so->so_state & SS_ISCONNECTED) == 0) {
812: if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
813: if ((so->so_state & SS_ISCONFIRMING) == 0 &&
814: !(resid == 0 && clen != 0))
815: snderr(ENOTCONN);
816: } else if (addr == 0)
817: snderr(EDESTADDRREQ);
818: }
819: space = sbspace(&so->so_snd);
820: if (flags & MSG_OOB)
821: space += 1024;
1.21 christos 822: if ((atomic && resid > so->so_snd.sb_hiwat) ||
1.11 mycroft 823: clen > so->so_snd.sb_hiwat)
824: snderr(EMSGSIZE);
1.96 mycroft 825: if (space < resid + clen &&
1.1 cgd 826: (atomic || space < so->so_snd.sb_lowat || space < clen)) {
827: if (so->so_state & SS_NBIO)
828: snderr(EWOULDBLOCK);
829: sbunlock(&so->so_snd);
830: error = sbwait(&so->so_snd);
831: splx(s);
832: if (error)
833: goto out;
834: goto restart;
835: }
836: splx(s);
837: mp = ⊤
838: space -= clen;
839: do {
1.45 tv 840: if (uio == NULL) {
841: /*
842: * Data is prepackaged in "top".
843: */
844: resid = 0;
845: if (flags & MSG_EOR)
846: top->m_flags |= M_EOR;
847: } else do {
848: if (top == 0) {
1.78 matt 849: m = m_gethdr(M_WAIT, MT_DATA);
1.45 tv 850: mlen = MHLEN;
851: m->m_pkthdr.len = 0;
852: m->m_pkthdr.rcvif = (struct ifnet *)0;
853: } else {
1.78 matt 854: m = m_get(M_WAIT, MT_DATA);
1.45 tv 855: mlen = MLEN;
856: }
1.78 matt 857: MCLAIM(m, so->so_snd.sb_mowner);
1.111.2.3 yamt 858: if (sock_loan_thresh >= 0 &&
859: uio->uio_iov->iov_len >= sock_loan_thresh &&
860: space >= sock_loan_thresh &&
1.64 thorpej 861: (len = sosend_loan(so, uio, m,
862: space)) != 0) {
863: SOSEND_COUNTER_INCR(&sosend_loan_big);
864: space -= len;
865: goto have_data;
866: }
1.45 tv 867: if (resid >= MINCLSIZE && space >= MCLBYTES) {
1.64 thorpej 868: SOSEND_COUNTER_INCR(&sosend_copy_big);
1.78 matt 869: m_clget(m, M_WAIT);
1.45 tv 870: if ((m->m_flags & M_EXT) == 0)
871: goto nopages;
872: mlen = MCLBYTES;
873: if (atomic && top == 0) {
1.58 jdolecek 874: len = lmin(MCLBYTES - max_hdr,
1.54 lukem 875: resid);
1.45 tv 876: m->m_data += max_hdr;
877: } else
1.58 jdolecek 878: len = lmin(MCLBYTES, resid);
1.45 tv 879: space -= len;
880: } else {
1.64 thorpej 881: nopages:
882: SOSEND_COUNTER_INCR(&sosend_copy_small);
1.58 jdolecek 883: len = lmin(lmin(mlen, resid), space);
1.45 tv 884: space -= len;
885: /*
886: * For datagram protocols, leave room
887: * for protocol headers in first mbuf.
888: */
889: if (atomic && top == 0 && len < mlen)
890: MH_ALIGN(m, len);
891: }
1.54 lukem 892: error = uiomove(mtod(m, caddr_t), (int)len,
893: uio);
1.64 thorpej 894: have_data:
1.45 tv 895: resid = uio->uio_resid;
896: m->m_len = len;
897: *mp = m;
898: top->m_pkthdr.len += len;
899: if (error)
900: goto release;
901: mp = &m->m_next;
902: if (resid <= 0) {
903: if (flags & MSG_EOR)
904: top->m_flags |= M_EOR;
905: break;
906: }
907: } while (space > 0 && atomic);
1.108 perry 908:
1.46 sommerfe 909: s = splsoftnet();
910:
911: if (so->so_state & SS_CANTSENDMORE)
912: snderr(EPIPE);
1.45 tv 913:
914: if (dontroute)
915: so->so_options |= SO_DONTROUTE;
916: if (resid > 0)
917: so->so_state |= SS_MORETOCOME;
1.46 sommerfe 918: error = (*so->so_proto->pr_usrreq)(so,
919: (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
1.111.2.3 yamt 920: top, addr, control, curlwp); /* XXX */
1.45 tv 921: if (dontroute)
922: so->so_options &= ~SO_DONTROUTE;
923: if (resid > 0)
924: so->so_state &= ~SS_MORETOCOME;
1.46 sommerfe 925: splx(s);
926:
1.45 tv 927: clen = 0;
928: control = 0;
929: top = 0;
930: mp = ⊤
1.1 cgd 931: if (error)
932: goto release;
933: } while (resid && space > 0);
934: } while (resid);
935:
1.54 lukem 936: release:
1.1 cgd 937: sbunlock(&so->so_snd);
1.54 lukem 938: out:
1.1 cgd 939: if (top)
940: m_freem(top);
941: if (control)
942: m_freem(control);
943: return (error);
944: }
945:
946: /*
947: * Implement receive operations on a socket.
948: * We depend on the way that records are added to the sockbuf
949: * by sbappend*. In particular, each record (mbufs linked through m_next)
950: * must begin with an address if the protocol so specifies,
951: * followed by an optional mbuf or mbufs containing ancillary data,
952: * and then zero or more mbufs of data.
953: * In order to avoid blocking network interrupts for the entire time here,
954: * we splx() while doing the actual copy to user space.
955: * Although the sockbuf is locked, new data may still be appended,
956: * and thus we must maintain consistency of the sockbuf during that time.
957: *
958: * The caller may receive the data as a single mbuf chain by supplying
959: * an mbuf **mp0 for use in returning the chain. The uio is then used
960: * only for the count in uio_resid.
961: */
1.3 andrew 962: int
1.54 lukem 963: soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
964: struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1.1 cgd 965: {
1.111.2.3 yamt 966: struct lwp *l = curlwp;
1.54 lukem 967: struct mbuf *m, **mp;
968: int flags, len, error, s, offset, moff, type, orig_resid;
1.99 matt 969: const struct protosw *pr;
1.54 lukem 970: struct mbuf *nextrecord;
1.67 he 971: int mbuf_removed = 0;
1.64 thorpej 972:
1.54 lukem 973: pr = so->so_proto;
1.1 cgd 974: mp = mp0;
1.54 lukem 975: type = 0;
976: orig_resid = uio->uio_resid;
1.102 jonathan 977:
1.1 cgd 978: if (paddr)
979: *paddr = 0;
980: if (controlp)
981: *controlp = 0;
982: if (flagsp)
983: flags = *flagsp &~ MSG_EOR;
984: else
985: flags = 0;
1.66 enami 986:
987: if ((flags & MSG_DONTWAIT) == 0)
1.111.2.3 yamt 988: sodopendfree();
1.66 enami 989:
1.1 cgd 990: if (flags & MSG_OOB) {
991: m = m_get(M_WAIT, MT_DATA);
1.17 cgd 992: error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
1.102 jonathan 993: (struct mbuf *)(long)(flags & MSG_PEEK),
1.111.2.3 yamt 994: (struct mbuf *)0, l);
1.1 cgd 995: if (error)
996: goto bad;
997: do {
998: error = uiomove(mtod(m, caddr_t),
999: (int) min(uio->uio_resid, m->m_len), uio);
1000: m = m_free(m);
1001: } while (uio->uio_resid && error == 0 && m);
1.54 lukem 1002: bad:
1.1 cgd 1003: if (m)
1004: m_freem(m);
1005: return (error);
1006: }
1007: if (mp)
1008: *mp = (struct mbuf *)0;
1009: if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
1.22 mycroft 1010: (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
1.111.2.3 yamt 1011: (struct mbuf *)0, (struct mbuf *)0, l);
1.1 cgd 1012:
1.54 lukem 1013: restart:
1.21 christos 1014: if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
1.1 cgd 1015: return (error);
1.20 mycroft 1016: s = splsoftnet();
1.1 cgd 1017:
1018: m = so->so_rcv.sb_mb;
1019: /*
1020: * If we have less data than requested, block awaiting more
1021: * (subject to any timeout) if:
1.15 mycroft 1022: * 1. the current count is less than the low water mark,
1.1 cgd 1023: * 2. MSG_WAITALL is set, and it is possible to do the entire
1.15 mycroft 1024: * receive operation at once if we block (resid <= hiwat), or
1025: * 3. MSG_DONTWAIT is not set.
1.1 cgd 1026: * If MSG_WAITALL is set but resid is larger than the receive buffer,
1027: * we have to do the receive in sections, and thus risk returning
1028: * a short count if a timeout or signal occurs after we start.
1029: */
1.21 christos 1030: if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
1.15 mycroft 1031: so->so_rcv.sb_cc < uio->uio_resid) &&
1.1 cgd 1032: (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
1033: ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1.21 christos 1034: m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
1.1 cgd 1035: #ifdef DIAGNOSTIC
1036: if (m == 0 && so->so_rcv.sb_cc)
1037: panic("receive 1");
1038: #endif
1039: if (so->so_error) {
1040: if (m)
1.15 mycroft 1041: goto dontblock;
1.1 cgd 1042: error = so->so_error;
1043: if ((flags & MSG_PEEK) == 0)
1044: so->so_error = 0;
1045: goto release;
1046: }
1047: if (so->so_state & SS_CANTRCVMORE) {
1048: if (m)
1.15 mycroft 1049: goto dontblock;
1.1 cgd 1050: else
1051: goto release;
1052: }
1053: for (; m; m = m->m_next)
1054: if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1055: m = so->so_rcv.sb_mb;
1056: goto dontblock;
1057: }
1058: if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1059: (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1060: error = ENOTCONN;
1061: goto release;
1062: }
1063: if (uio->uio_resid == 0)
1064: goto release;
1.15 mycroft 1065: if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
1.1 cgd 1066: error = EWOULDBLOCK;
1067: goto release;
1068: }
1.69 thorpej 1069: SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1");
1070: SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1");
1.1 cgd 1071: sbunlock(&so->so_rcv);
1072: error = sbwait(&so->so_rcv);
1073: splx(s);
1074: if (error)
1075: return (error);
1076: goto restart;
1077: }
1.54 lukem 1078: dontblock:
1.69 thorpej 1079: /*
1080: * On entry here, m points to the first record of the socket buffer.
1081: * While we process the initial mbufs containing address and control
1082: * info, we save a copy of m->m_nextpkt into nextrecord.
1083: */
1.111.2.3 yamt 1084: if (l)
1085: l->l_proc->p_stats->p_ru.ru_msgrcv++;
1.69 thorpej 1086: KASSERT(m == so->so_rcv.sb_mb);
1087: SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
1088: SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
1.1 cgd 1089: nextrecord = m->m_nextpkt;
1090: if (pr->pr_flags & PR_ADDR) {
1091: #ifdef DIAGNOSTIC
1092: if (m->m_type != MT_SONAME)
1093: panic("receive 1a");
1094: #endif
1.3 andrew 1095: orig_resid = 0;
1.1 cgd 1096: if (flags & MSG_PEEK) {
1097: if (paddr)
1098: *paddr = m_copy(m, 0, m->m_len);
1099: m = m->m_next;
1100: } else {
1101: sbfree(&so->so_rcv, m);
1.67 he 1102: mbuf_removed = 1;
1.1 cgd 1103: if (paddr) {
1104: *paddr = m;
1105: so->so_rcv.sb_mb = m->m_next;
1106: m->m_next = 0;
1107: m = so->so_rcv.sb_mb;
1108: } else {
1109: MFREE(m, so->so_rcv.sb_mb);
1110: m = so->so_rcv.sb_mb;
1111: }
1112: }
1113: }
1114: while (m && m->m_type == MT_CONTROL && error == 0) {
1115: if (flags & MSG_PEEK) {
1116: if (controlp)
1117: *controlp = m_copy(m, 0, m->m_len);
1118: m = m->m_next;
1119: } else {
1120: sbfree(&so->so_rcv, m);
1.67 he 1121: mbuf_removed = 1;
1.1 cgd 1122: if (controlp) {
1.102 jonathan 1123: struct domain *dom = pr->pr_domain;
1.111.2.3 yamt 1124: if (dom->dom_externalize && l &&
1.1 cgd 1125: mtod(m, struct cmsghdr *)->cmsg_type ==
1126: SCM_RIGHTS)
1.111.2.3 yamt 1127: error = (*dom->dom_externalize)(m, l);
1.1 cgd 1128: *controlp = m;
1129: so->so_rcv.sb_mb = m->m_next;
1130: m->m_next = 0;
1131: m = so->so_rcv.sb_mb;
1132: } else {
1.106 itojun 1133: /*
1134: * Dispose of any SCM_RIGHTS message that went
1135: * through the read path rather than recv.
1136: */
1137: if (pr->pr_domain->dom_dispose &&
1138: mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
1139: (*pr->pr_domain->dom_dispose)(m);
1.1 cgd 1140: MFREE(m, so->so_rcv.sb_mb);
1141: m = so->so_rcv.sb_mb;
1142: }
1143: }
1.3 andrew 1144: if (controlp) {
1145: orig_resid = 0;
1.1 cgd 1146: controlp = &(*controlp)->m_next;
1.3 andrew 1147: }
1.1 cgd 1148: }
1.69 thorpej 1149:
1150: /*
1151: * If m is non-NULL, we have some data to read. From now on,
1152: * make sure to keep sb_lastrecord consistent when working on
1153: * the last packet on the chain (nextrecord == NULL) and we
1154: * change m->m_nextpkt.
1155: */
1.1 cgd 1156: if (m) {
1.69 thorpej 1157: if ((flags & MSG_PEEK) == 0) {
1.1 cgd 1158: m->m_nextpkt = nextrecord;
1.69 thorpej 1159: /*
1160: * If nextrecord == NULL (this is a single chain),
1161: * then sb_lastrecord may not be valid here if m
1162: * was changed earlier.
1163: */
1164: if (nextrecord == NULL) {
1165: KASSERT(so->so_rcv.sb_mb == m);
1166: so->so_rcv.sb_lastrecord = m;
1167: }
1168: }
1.1 cgd 1169: type = m->m_type;
1170: if (type == MT_OOBDATA)
1171: flags |= MSG_OOB;
1.69 thorpej 1172: } else {
1173: if ((flags & MSG_PEEK) == 0) {
1174: KASSERT(so->so_rcv.sb_mb == m);
1175: so->so_rcv.sb_mb = nextrecord;
1.70 thorpej 1176: SB_EMPTY_FIXUP(&so->so_rcv);
1.69 thorpej 1177: }
1.1 cgd 1178: }
1.69 thorpej 1179: SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
1180: SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
1181:
1.1 cgd 1182: moff = 0;
1183: offset = 0;
1184: while (m && uio->uio_resid > 0 && error == 0) {
1185: if (m->m_type == MT_OOBDATA) {
1186: if (type != MT_OOBDATA)
1187: break;
1188: } else if (type == MT_OOBDATA)
1189: break;
1190: #ifdef DIAGNOSTIC
1191: else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
1192: panic("receive 3");
1193: #endif
1194: so->so_state &= ~SS_RCVATMARK;
1195: len = uio->uio_resid;
1196: if (so->so_oobmark && len > so->so_oobmark - offset)
1197: len = so->so_oobmark - offset;
1198: if (len > m->m_len - moff)
1199: len = m->m_len - moff;
1200: /*
1201: * If mp is set, just pass back the mbufs.
1202: * Otherwise copy them out via the uio, then free.
1203: * Sockbuf must be consistent here (points to current mbuf,
1204: * it points to next record) when we drop priority;
1205: * we must note any additions to the sockbuf when we
1206: * block interrupts again.
1207: */
1208: if (mp == 0) {
1.69 thorpej 1209: SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove");
1210: SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove");
1.1 cgd 1211: splx(s);
1212: error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
1.20 mycroft 1213: s = splsoftnet();
1.67 he 1214: if (error) {
1215: /*
1216: * If any part of the record has been removed
1217: * (such as the MT_SONAME mbuf, which will
1218: * happen when PR_ADDR, and thus also
1219: * PR_ATOMIC, is set), then drop the entire
1220: * record to maintain the atomicity of the
1221: * receive operation.
1222: *
1223: * This avoids a later panic("receive 1a")
1224: * when compiled with DIAGNOSTIC.
1225: */
1226: if (m && mbuf_removed
1227: && (pr->pr_flags & PR_ATOMIC))
1228: (void) sbdroprecord(&so->so_rcv);
1229:
1.57 jdolecek 1230: goto release;
1.67 he 1231: }
1.1 cgd 1232: } else
1233: uio->uio_resid -= len;
1234: if (len == m->m_len - moff) {
1235: if (m->m_flags & M_EOR)
1236: flags |= MSG_EOR;
1237: if (flags & MSG_PEEK) {
1238: m = m->m_next;
1239: moff = 0;
1240: } else {
1241: nextrecord = m->m_nextpkt;
1242: sbfree(&so->so_rcv, m);
1243: if (mp) {
1244: *mp = m;
1245: mp = &m->m_next;
1246: so->so_rcv.sb_mb = m = m->m_next;
1247: *mp = (struct mbuf *)0;
1248: } else {
1249: MFREE(m, so->so_rcv.sb_mb);
1250: m = so->so_rcv.sb_mb;
1251: }
1.69 thorpej 1252: /*
1253: * If m != NULL, we also know that
1254: * so->so_rcv.sb_mb != NULL.
1255: */
1256: KASSERT(so->so_rcv.sb_mb == m);
1257: if (m) {
1.1 cgd 1258: m->m_nextpkt = nextrecord;
1.69 thorpej 1259: if (nextrecord == NULL)
1260: so->so_rcv.sb_lastrecord = m;
1261: } else {
1262: so->so_rcv.sb_mb = nextrecord;
1.70 thorpej 1263: SB_EMPTY_FIXUP(&so->so_rcv);
1.69 thorpej 1264: }
1265: SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
1266: SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
1.1 cgd 1267: }
1268: } else {
1269: if (flags & MSG_PEEK)
1270: moff += len;
1271: else {
1272: if (mp)
1273: *mp = m_copym(m, 0, len, M_WAIT);
1274: m->m_data += len;
1275: m->m_len -= len;
1276: so->so_rcv.sb_cc -= len;
1277: }
1278: }
1279: if (so->so_oobmark) {
1280: if ((flags & MSG_PEEK) == 0) {
1281: so->so_oobmark -= len;
1282: if (so->so_oobmark == 0) {
1283: so->so_state |= SS_RCVATMARK;
1284: break;
1285: }
1.7 cgd 1286: } else {
1.1 cgd 1287: offset += len;
1.7 cgd 1288: if (offset == so->so_oobmark)
1289: break;
1290: }
1.1 cgd 1291: }
1292: if (flags & MSG_EOR)
1293: break;
1294: /*
1295: * If the MSG_WAITALL flag is set (for non-atomic socket),
1296: * we must not quit until "uio->uio_resid == 0" or an error
1297: * termination. If a signal/timeout occurs, return
1298: * with a short count but without error.
1299: * Keep sockbuf locked against other readers.
1300: */
1301: while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
1.3 andrew 1302: !sosendallatonce(so) && !nextrecord) {
1.1 cgd 1303: if (so->so_error || so->so_state & SS_CANTRCVMORE)
1304: break;
1.68 matt 1305: /*
1306: * If we are peeking and the socket receive buffer is
1307: * full, stop since we can't get more data to peek at.
1308: */
1309: if ((flags & MSG_PEEK) && sbspace(&so->so_rcv) <= 0)
1310: break;
1311: /*
1312: * If we've drained the socket buffer, tell the
1313: * protocol in case it needs to do something to
1314: * get it filled again.
1315: */
1316: if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
1317: (*pr->pr_usrreq)(so, PRU_RCVD,
1318: (struct mbuf *)0,
1319: (struct mbuf *)(long)flags,
1.111.2.3 yamt 1320: (struct mbuf *)0, l);
1.69 thorpej 1321: SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2");
1322: SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2");
1.1 cgd 1323: error = sbwait(&so->so_rcv);
1324: if (error) {
1325: sbunlock(&so->so_rcv);
1326: splx(s);
1327: return (0);
1328: }
1.21 christos 1329: if ((m = so->so_rcv.sb_mb) != NULL)
1.1 cgd 1330: nextrecord = m->m_nextpkt;
1331: }
1332: }
1.3 andrew 1333:
1334: if (m && pr->pr_flags & PR_ATOMIC) {
1335: flags |= MSG_TRUNC;
1336: if ((flags & MSG_PEEK) == 0)
1337: (void) sbdroprecord(&so->so_rcv);
1338: }
1.1 cgd 1339: if ((flags & MSG_PEEK) == 0) {
1.69 thorpej 1340: if (m == 0) {
1341: /*
1.70 thorpej 1342: * First part is an inline SB_EMPTY_FIXUP(). Second
1.69 thorpej 1343: * part makes sure sb_lastrecord is up-to-date if
1344: * there is still data in the socket buffer.
1345: */
1.1 cgd 1346: so->so_rcv.sb_mb = nextrecord;
1.69 thorpej 1347: if (so->so_rcv.sb_mb == NULL) {
1348: so->so_rcv.sb_mbtail = NULL;
1349: so->so_rcv.sb_lastrecord = NULL;
1350: } else if (nextrecord->m_nextpkt == NULL)
1351: so->so_rcv.sb_lastrecord = nextrecord;
1352: }
1353: SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
1354: SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
1.1 cgd 1355: if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1.22 mycroft 1356: (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
1.111.2.3 yamt 1357: (struct mbuf *)(long)flags, (struct mbuf *)0, l);
1.1 cgd 1358: }
1.3 andrew 1359: if (orig_resid == uio->uio_resid && orig_resid &&
1360: (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
1361: sbunlock(&so->so_rcv);
1362: splx(s);
1363: goto restart;
1364: }
1.108 perry 1365:
1.1 cgd 1366: if (flagsp)
1367: *flagsp |= flags;
1.54 lukem 1368: release:
1.1 cgd 1369: sbunlock(&so->so_rcv);
1370: splx(s);
1371: return (error);
1372: }
1373:
1.14 mycroft 1374: int
1.54 lukem 1375: soshutdown(struct socket *so, int how)
1.1 cgd 1376: {
1.99 matt 1377: const struct protosw *pr;
1.34 kleink 1378:
1.54 lukem 1379: pr = so->so_proto;
1.34 kleink 1380: if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1381: return (EINVAL);
1.1 cgd 1382:
1.34 kleink 1383: if (how == SHUT_RD || how == SHUT_RDWR)
1.1 cgd 1384: sorflush(so);
1.34 kleink 1385: if (how == SHUT_WR || how == SHUT_RDWR)
1.22 mycroft 1386: return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0,
1.111.2.3 yamt 1387: (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1.1 cgd 1388: return (0);
1389: }
1390:
1.14 mycroft 1391: void
1.54 lukem 1392: sorflush(struct socket *so)
1.1 cgd 1393: {
1.54 lukem 1394: struct sockbuf *sb, asb;
1.99 matt 1395: const struct protosw *pr;
1.54 lukem 1396: int s;
1.1 cgd 1397:
1.54 lukem 1398: sb = &so->so_rcv;
1399: pr = so->so_proto;
1.1 cgd 1400: sb->sb_flags |= SB_NOINTR;
1.15 mycroft 1401: (void) sblock(sb, M_WAITOK);
1.56 thorpej 1402: s = splnet();
1.1 cgd 1403: socantrcvmore(so);
1404: sbunlock(sb);
1405: asb = *sb;
1.86 wrstuden 1406: /*
1407: * Clear most of the sockbuf structure, but leave some of the
1408: * fields valid.
1409: */
1410: memset(&sb->sb_startzero, 0,
1411: sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
1.1 cgd 1412: splx(s);
1413: if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
1414: (*pr->pr_domain->dom_dispose)(asb.sb_mb);
1.98 christos 1415: sbrelease(&asb, so);
1.1 cgd 1416: }
1417:
1.14 mycroft 1418: int
1.54 lukem 1419: sosetopt(struct socket *so, int level, int optname, struct mbuf *m0)
1.1 cgd 1420: {
1.54 lukem 1421: int error;
1422: struct mbuf *m;
1.111.2.5! yamt 1423: struct linger *l;
1.1 cgd 1424:
1.54 lukem 1425: error = 0;
1426: m = m0;
1.1 cgd 1427: if (level != SOL_SOCKET) {
1428: if (so->so_proto && so->so_proto->pr_ctloutput)
1429: return ((*so->so_proto->pr_ctloutput)
1430: (PRCO_SETOPT, so, level, optname, &m0));
1431: error = ENOPROTOOPT;
1432: } else {
1433: switch (optname) {
1434:
1435: case SO_LINGER:
1.36 perry 1436: if (m == NULL || m->m_len != sizeof(struct linger)) {
1.1 cgd 1437: error = EINVAL;
1438: goto bad;
1439: }
1.111.2.5! yamt 1440: l = mtod(m, struct linger *);
! 1441: if (l->l_linger < 0 || l->l_linger > USHRT_MAX ||
! 1442: l->l_linger > (INT_MAX / hz)) {
1.111.2.3 yamt 1443: error = EDOM;
1444: goto bad;
1445: }
1.111.2.5! yamt 1446: so->so_linger = l->l_linger;
! 1447: if (l->l_onoff)
! 1448: so->so_options |= SO_LINGER;
! 1449: else
! 1450: so->so_options &= ~SO_LINGER;
! 1451: break;
1.1 cgd 1452:
1453: case SO_DEBUG:
1454: case SO_KEEPALIVE:
1455: case SO_DONTROUTE:
1456: case SO_USELOOPBACK:
1457: case SO_BROADCAST:
1458: case SO_REUSEADDR:
1.15 mycroft 1459: case SO_REUSEPORT:
1.1 cgd 1460: case SO_OOBINLINE:
1.26 thorpej 1461: case SO_TIMESTAMP:
1.36 perry 1462: if (m == NULL || m->m_len < sizeof(int)) {
1.1 cgd 1463: error = EINVAL;
1464: goto bad;
1465: }
1466: if (*mtod(m, int *))
1467: so->so_options |= optname;
1468: else
1469: so->so_options &= ~optname;
1470: break;
1471:
1472: case SO_SNDBUF:
1473: case SO_RCVBUF:
1474: case SO_SNDLOWAT:
1475: case SO_RCVLOWAT:
1.28 thorpej 1476: {
1477: int optval;
1478:
1.36 perry 1479: if (m == NULL || m->m_len < sizeof(int)) {
1.1 cgd 1480: error = EINVAL;
1481: goto bad;
1482: }
1.28 thorpej 1483:
1484: /*
1485: * Values < 1 make no sense for any of these
1486: * options, so disallow them.
1487: */
1488: optval = *mtod(m, int *);
1489: if (optval < 1) {
1490: error = EINVAL;
1491: goto bad;
1492: }
1493:
1.1 cgd 1494: switch (optname) {
1495:
1496: case SO_SNDBUF:
1497: case SO_RCVBUF:
1498: if (sbreserve(optname == SO_SNDBUF ?
1499: &so->so_snd : &so->so_rcv,
1.98 christos 1500: (u_long) optval, so) == 0) {
1.1 cgd 1501: error = ENOBUFS;
1502: goto bad;
1503: }
1504: break;
1505:
1.28 thorpej 1506: /*
1507: * Make sure the low-water is never greater than
1508: * the high-water.
1509: */
1.1 cgd 1510: case SO_SNDLOWAT:
1.28 thorpej 1511: so->so_snd.sb_lowat =
1512: (optval > so->so_snd.sb_hiwat) ?
1513: so->so_snd.sb_hiwat : optval;
1.1 cgd 1514: break;
1515: case SO_RCVLOWAT:
1.28 thorpej 1516: so->so_rcv.sb_lowat =
1517: (optval > so->so_rcv.sb_hiwat) ?
1518: so->so_rcv.sb_hiwat : optval;
1.1 cgd 1519: break;
1520: }
1521: break;
1.28 thorpej 1522: }
1.1 cgd 1523:
1524: case SO_SNDTIMEO:
1525: case SO_RCVTIMEO:
1526: {
1527: struct timeval *tv;
1.104 yamt 1528: int val;
1.1 cgd 1529:
1.36 perry 1530: if (m == NULL || m->m_len < sizeof(*tv)) {
1.1 cgd 1531: error = EINVAL;
1532: goto bad;
1533: }
1534: tv = mtod(m, struct timeval *);
1.104 yamt 1535: if (tv->tv_sec > (INT_MAX - tv->tv_usec / tick) / hz) {
1.1 cgd 1536: error = EDOM;
1537: goto bad;
1538: }
1539: val = tv->tv_sec * hz + tv->tv_usec / tick;
1.74 itojun 1540: if (val == 0 && tv->tv_usec != 0)
1541: val = 1;
1.1 cgd 1542:
1543: switch (optname) {
1544:
1545: case SO_SNDTIMEO:
1546: so->so_snd.sb_timeo = val;
1547: break;
1548: case SO_RCVTIMEO:
1549: so->so_rcv.sb_timeo = val;
1550: break;
1551: }
1552: break;
1553: }
1554:
1555: default:
1556: error = ENOPROTOOPT;
1557: break;
1558: }
1.15 mycroft 1559: if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1560: (void) ((*so->so_proto->pr_ctloutput)
1561: (PRCO_SETOPT, so, level, optname, &m0));
1562: m = NULL; /* freed by protocol */
1563: }
1.1 cgd 1564: }
1.54 lukem 1565: bad:
1.1 cgd 1566: if (m)
1567: (void) m_free(m);
1568: return (error);
1569: }
1570:
1.14 mycroft 1571: int
1.54 lukem 1572: sogetopt(struct socket *so, int level, int optname, struct mbuf **mp)
1.1 cgd 1573: {
1.54 lukem 1574: struct mbuf *m;
1.1 cgd 1575:
1576: if (level != SOL_SOCKET) {
1577: if (so->so_proto && so->so_proto->pr_ctloutput) {
1578: return ((*so->so_proto->pr_ctloutput)
1579: (PRCO_GETOPT, so, level, optname, mp));
1580: } else
1581: return (ENOPROTOOPT);
1582: } else {
1583: m = m_get(M_WAIT, MT_SOOPTS);
1.36 perry 1584: m->m_len = sizeof(int);
1.1 cgd 1585:
1586: switch (optname) {
1587:
1588: case SO_LINGER:
1.36 perry 1589: m->m_len = sizeof(struct linger);
1.1 cgd 1590: mtod(m, struct linger *)->l_onoff =
1.111.2.5! yamt 1591: (so->so_options & SO_LINGER) ? 1 : 0;
1.1 cgd 1592: mtod(m, struct linger *)->l_linger = so->so_linger;
1593: break;
1594:
1595: case SO_USELOOPBACK:
1596: case SO_DONTROUTE:
1597: case SO_DEBUG:
1598: case SO_KEEPALIVE:
1599: case SO_REUSEADDR:
1.15 mycroft 1600: case SO_REUSEPORT:
1.1 cgd 1601: case SO_BROADCAST:
1602: case SO_OOBINLINE:
1.26 thorpej 1603: case SO_TIMESTAMP:
1.111.2.5! yamt 1604: *mtod(m, int *) = (so->so_options & optname) ? 1 : 0;
1.1 cgd 1605: break;
1606:
1607: case SO_TYPE:
1608: *mtod(m, int *) = so->so_type;
1609: break;
1610:
1611: case SO_ERROR:
1612: *mtod(m, int *) = so->so_error;
1613: so->so_error = 0;
1614: break;
1615:
1616: case SO_SNDBUF:
1617: *mtod(m, int *) = so->so_snd.sb_hiwat;
1618: break;
1619:
1620: case SO_RCVBUF:
1621: *mtod(m, int *) = so->so_rcv.sb_hiwat;
1622: break;
1623:
1624: case SO_SNDLOWAT:
1625: *mtod(m, int *) = so->so_snd.sb_lowat;
1626: break;
1627:
1628: case SO_RCVLOWAT:
1629: *mtod(m, int *) = so->so_rcv.sb_lowat;
1630: break;
1631:
1632: case SO_SNDTIMEO:
1633: case SO_RCVTIMEO:
1634: {
1635: int val = (optname == SO_SNDTIMEO ?
1636: so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1637:
1638: m->m_len = sizeof(struct timeval);
1639: mtod(m, struct timeval *)->tv_sec = val / hz;
1640: mtod(m, struct timeval *)->tv_usec =
1.27 kleink 1641: (val % hz) * tick;
1.1 cgd 1642: break;
1643: }
1644:
1.107 darrenr 1645: case SO_OVERFLOWED:
1646: *mtod(m, int *) = so->so_rcv.sb_overflowed;
1647: break;
1648:
1.1 cgd 1649: default:
1650: (void)m_free(m);
1651: return (ENOPROTOOPT);
1652: }
1653: *mp = m;
1654: return (0);
1655: }
1656: }
1657:
1.14 mycroft 1658: void
1.54 lukem 1659: sohasoutofband(struct socket *so)
1.1 cgd 1660: {
1.90 christos 1661: fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so);
1.2 cgd 1662: selwakeup(&so->so_rcv.sb_sel);
1.1 cgd 1663: }
1.72 jdolecek 1664:
1665: static void
1666: filt_sordetach(struct knote *kn)
1667: {
1668: struct socket *so;
1669:
1670: so = (struct socket *)kn->kn_fp->f_data;
1.73 christos 1671: SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
1672: if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
1.72 jdolecek 1673: so->so_rcv.sb_flags &= ~SB_KNOTE;
1674: }
1675:
1676: /*ARGSUSED*/
1677: static int
1678: filt_soread(struct knote *kn, long hint)
1679: {
1680: struct socket *so;
1681:
1682: so = (struct socket *)kn->kn_fp->f_data;
1683: kn->kn_data = so->so_rcv.sb_cc;
1684: if (so->so_state & SS_CANTRCVMORE) {
1.108 perry 1685: kn->kn_flags |= EV_EOF;
1.72 jdolecek 1686: kn->kn_fflags = so->so_error;
1687: return (1);
1688: }
1689: if (so->so_error) /* temporary udp error */
1690: return (1);
1691: if (kn->kn_sfflags & NOTE_LOWAT)
1692: return (kn->kn_data >= kn->kn_sdata);
1693: return (kn->kn_data >= so->so_rcv.sb_lowat);
1694: }
1695:
1696: static void
1697: filt_sowdetach(struct knote *kn)
1698: {
1699: struct socket *so;
1700:
1701: so = (struct socket *)kn->kn_fp->f_data;
1.73 christos 1702: SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
1703: if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
1.72 jdolecek 1704: so->so_snd.sb_flags &= ~SB_KNOTE;
1705: }
1706:
1707: /*ARGSUSED*/
1708: static int
1709: filt_sowrite(struct knote *kn, long hint)
1710: {
1711: struct socket *so;
1712:
1713: so = (struct socket *)kn->kn_fp->f_data;
1714: kn->kn_data = sbspace(&so->so_snd);
1715: if (so->so_state & SS_CANTSENDMORE) {
1.108 perry 1716: kn->kn_flags |= EV_EOF;
1.72 jdolecek 1717: kn->kn_fflags = so->so_error;
1718: return (1);
1719: }
1720: if (so->so_error) /* temporary udp error */
1721: return (1);
1722: if (((so->so_state & SS_ISCONNECTED) == 0) &&
1723: (so->so_proto->pr_flags & PR_CONNREQUIRED))
1724: return (0);
1725: if (kn->kn_sfflags & NOTE_LOWAT)
1726: return (kn->kn_data >= kn->kn_sdata);
1727: return (kn->kn_data >= so->so_snd.sb_lowat);
1728: }
1729:
1730: /*ARGSUSED*/
1731: static int
1732: filt_solisten(struct knote *kn, long hint)
1733: {
1734: struct socket *so;
1735:
1736: so = (struct socket *)kn->kn_fp->f_data;
1737:
1738: /*
1739: * Set kn_data to number of incoming connections, not
1740: * counting partial (incomplete) connections.
1.108 perry 1741: */
1.72 jdolecek 1742: kn->kn_data = so->so_qlen;
1743: return (kn->kn_data > 0);
1744: }
1745:
1746: static const struct filterops solisten_filtops =
1747: { 1, NULL, filt_sordetach, filt_solisten };
1748: static const struct filterops soread_filtops =
1749: { 1, NULL, filt_sordetach, filt_soread };
1750: static const struct filterops sowrite_filtops =
1751: { 1, NULL, filt_sowdetach, filt_sowrite };
1752:
1753: int
1754: soo_kqfilter(struct file *fp, struct knote *kn)
1755: {
1756: struct socket *so;
1757: struct sockbuf *sb;
1758:
1759: so = (struct socket *)kn->kn_fp->f_data;
1760: switch (kn->kn_filter) {
1761: case EVFILT_READ:
1762: if (so->so_options & SO_ACCEPTCONN)
1763: kn->kn_fop = &solisten_filtops;
1764: else
1765: kn->kn_fop = &soread_filtops;
1766: sb = &so->so_rcv;
1767: break;
1768: case EVFILT_WRITE:
1769: kn->kn_fop = &sowrite_filtops;
1770: sb = &so->so_snd;
1771: break;
1772: default:
1773: return (1);
1774: }
1.73 christos 1775: SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, kn, kn_selnext);
1.72 jdolecek 1776: sb->sb_flags |= SB_KNOTE;
1777: return (0);
1778: }
1779:
1.94 yamt 1780: #include <sys/sysctl.h>
1781:
1782: static int sysctl_kern_somaxkva(SYSCTLFN_PROTO);
1783:
1784: /*
1785: * sysctl helper routine for kern.somaxkva. ensures that the given
1786: * value is not too small.
1787: * (XXX should we maybe make sure it's not too large as well?)
1788: */
1789: static int
1790: sysctl_kern_somaxkva(SYSCTLFN_ARGS)
1791: {
1792: int error, new_somaxkva;
1793: struct sysctlnode node;
1794: int s;
1795:
1796: new_somaxkva = somaxkva;
1797: node = *rnode;
1798: node.sysctl_data = &new_somaxkva;
1799: error = sysctl_lookup(SYSCTLFN_CALL(&node));
1800: if (error || newp == NULL)
1801: return (error);
1802:
1803: if (new_somaxkva < (16 * 1024 * 1024)) /* sanity */
1804: return (EINVAL);
1805:
1806: s = splvm();
1807: simple_lock(&so_pendfree_slock);
1808: somaxkva = new_somaxkva;
1809: wakeup(&socurkva);
1810: simple_unlock(&so_pendfree_slock);
1811: splx(s);
1812:
1813: return (error);
1814: }
1815:
1816: SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup")
1817: {
1818:
1.97 atatat 1819: sysctl_createv(clog, 0, NULL, NULL,
1820: CTLFLAG_PERMANENT,
1821: CTLTYPE_NODE, "kern", NULL,
1822: NULL, 0, NULL, 0,
1823: CTL_KERN, CTL_EOL);
1824:
1825: sysctl_createv(clog, 0, NULL, NULL,
1826: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.103 atatat 1827: CTLTYPE_INT, "somaxkva",
1828: SYSCTL_DESCR("Maximum amount of kernel memory to be "
1829: "used for socket buffers"),
1.94 yamt 1830: sysctl_kern_somaxkva, 0, NULL, 0,
1831: CTL_KERN, KERN_SOMAXKVA, CTL_EOL);
1832: }
CVSweb <webmaster@jp.NetBSD.org>