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