Annotation of src/sys/kern/uipc_socket.c, Revision 1.111.2.3
1.111.2.3! yamt 1: /* $NetBSD: uipc_socket.c,v 1.111.2.2 2005/07/07 12:42:25 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.3! yamt 71: __KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.111.2.2 2005/07/07 12:42:25 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);
323: KASSERT(!MCLISREFERENCED(m));
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:
474: if (proto)
475: prp = pffindproto(dom, proto, type);
476: else
477: prp = pffindtype(dom, type);
1.111.2.3! yamt 478: if (prp == 0) {
! 479: /* no support for domain */
! 480: if (pffinddomain(dom) == 0)
! 481: return (EAFNOSUPPORT);
! 482: /* no support for socket type */
! 483: if (proto == 0 && type != 0)
! 484: return (EPROTOTYPE);
! 485: return (EPROTONOSUPPORT);
! 486: }
! 487: if (prp->pr_usrreq == 0)
1.1 cgd 488: return (EPROTONOSUPPORT);
489: if (prp->pr_type != type)
490: return (EPROTOTYPE);
1.39 matt 491: s = splsoftnet();
1.37 thorpej 492: so = pool_get(&socket_pool, PR_WAITOK);
1.38 perry 493: memset((caddr_t)so, 0, sizeof(*so));
1.31 thorpej 494: TAILQ_INIT(&so->so_q0);
495: TAILQ_INIT(&so->so_q);
1.1 cgd 496: so->so_type = type;
497: so->so_proto = prp;
1.33 matt 498: so->so_send = sosend;
499: so->so_receive = soreceive;
1.78 matt 500: #ifdef MBUFTRACE
501: so->so_rcv.sb_mowner = &prp->pr_domain->dom_mowner;
502: so->so_snd.sb_mowner = &prp->pr_domain->dom_mowner;
503: so->so_mowner = &prp->pr_domain->dom_mowner;
504: #endif
1.111.2.3! yamt 505: if (l != NULL) {
! 506: uid = kauth_cred_geteuid(l->l_proc->p_cred);
! 507: } else {
! 508: uid = 0;
! 509: }
! 510: so->so_uidinfo = uid_find(uid);
1.22 mycroft 511: error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0,
1.111.2.3! yamt 512: (struct mbuf *)(long)proto, (struct mbuf *)0, l);
1.1 cgd 513: if (error) {
514: so->so_state |= SS_NOFDREF;
515: sofree(so);
1.39 matt 516: splx(s);
1.1 cgd 517: return (error);
518: }
1.39 matt 519: splx(s);
1.1 cgd 520: *aso = so;
521: return (0);
522: }
523:
1.3 andrew 524: int
1.111.2.3! yamt 525: sobind(struct socket *so, struct mbuf *nam, struct lwp *l)
1.1 cgd 526: {
1.54 lukem 527: int s, error;
1.1 cgd 528:
1.54 lukem 529: s = splsoftnet();
1.22 mycroft 530: error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, (struct mbuf *)0,
1.111.2.3! yamt 531: nam, (struct mbuf *)0, l);
1.1 cgd 532: splx(s);
533: return (error);
534: }
535:
1.3 andrew 536: int
1.54 lukem 537: solisten(struct socket *so, int backlog)
1.1 cgd 538: {
1.54 lukem 539: int s, error;
1.1 cgd 540:
1.54 lukem 541: s = splsoftnet();
1.22 mycroft 542: error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, (struct mbuf *)0,
1.111.2.3! yamt 543: (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1.1 cgd 544: if (error) {
545: splx(s);
546: return (error);
547: }
1.63 matt 548: if (TAILQ_EMPTY(&so->so_q))
1.1 cgd 549: so->so_options |= SO_ACCEPTCONN;
550: if (backlog < 0)
551: backlog = 0;
1.49 jonathan 552: so->so_qlimit = min(backlog, somaxconn);
1.1 cgd 553: splx(s);
554: return (0);
555: }
556:
1.21 christos 557: void
1.54 lukem 558: sofree(struct socket *so)
1.1 cgd 559: {
560:
1.43 mycroft 561: if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
1.1 cgd 562: return;
1.43 mycroft 563: if (so->so_head) {
564: /*
565: * We must not decommission a socket that's on the accept(2)
566: * queue. If we do, then accept(2) may hang after select(2)
567: * indicated that the listening socket was ready.
568: */
569: if (!soqremque(so, 0))
570: return;
571: }
1.98 christos 572: if (so->so_rcv.sb_hiwat)
1.110 christos 573: (void)chgsbsize(so->so_uidinfo, &so->so_rcv.sb_hiwat, 0,
1.98 christos 574: RLIM_INFINITY);
575: if (so->so_snd.sb_hiwat)
1.110 christos 576: (void)chgsbsize(so->so_uidinfo, &so->so_snd.sb_hiwat, 0,
1.98 christos 577: RLIM_INFINITY);
578: sbrelease(&so->so_snd, so);
1.1 cgd 579: sorflush(so);
1.37 thorpej 580: pool_put(&socket_pool, so);
1.1 cgd 581: }
582:
583: /*
584: * Close a socket on last file table reference removal.
585: * Initiate disconnect if connected.
586: * Free socket when disconnect complete.
587: */
1.3 andrew 588: int
1.54 lukem 589: soclose(struct socket *so)
1.1 cgd 590: {
1.54 lukem 591: struct socket *so2;
592: int s, error;
1.1 cgd 593:
1.54 lukem 594: error = 0;
595: s = splsoftnet(); /* conservative */
1.1 cgd 596: if (so->so_options & SO_ACCEPTCONN) {
1.63 matt 597: while ((so2 = TAILQ_FIRST(&so->so_q0)) != 0) {
1.42 mycroft 598: (void) soqremque(so2, 0);
1.41 mycroft 599: (void) soabort(so2);
600: }
1.63 matt 601: while ((so2 = TAILQ_FIRST(&so->so_q)) != 0) {
1.42 mycroft 602: (void) soqremque(so2, 1);
1.41 mycroft 603: (void) soabort(so2);
604: }
1.1 cgd 605: }
606: if (so->so_pcb == 0)
607: goto discard;
608: if (so->so_state & SS_ISCONNECTED) {
609: if ((so->so_state & SS_ISDISCONNECTING) == 0) {
610: error = sodisconnect(so);
611: if (error)
612: goto drop;
613: }
614: if (so->so_options & SO_LINGER) {
615: if ((so->so_state & SS_ISDISCONNECTING) &&
616: (so->so_state & SS_NBIO))
617: goto drop;
1.21 christos 618: while (so->so_state & SS_ISCONNECTED) {
619: error = tsleep((caddr_t)&so->so_timeo,
620: PSOCK | PCATCH, netcls,
1.30 thorpej 621: so->so_linger * hz);
1.21 christos 622: if (error)
1.1 cgd 623: break;
1.21 christos 624: }
1.1 cgd 625: }
626: }
1.54 lukem 627: drop:
1.1 cgd 628: if (so->so_pcb) {
1.22 mycroft 629: int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
630: (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
1.111.2.3! yamt 631: (struct lwp *)0);
1.1 cgd 632: if (error == 0)
633: error = error2;
634: }
1.54 lukem 635: discard:
1.1 cgd 636: if (so->so_state & SS_NOFDREF)
637: panic("soclose: NOFDREF");
638: so->so_state |= SS_NOFDREF;
639: sofree(so);
640: splx(s);
641: return (error);
642: }
643:
644: /*
1.20 mycroft 645: * Must be called at splsoftnet...
1.1 cgd 646: */
1.3 andrew 647: int
1.54 lukem 648: soabort(struct socket *so)
1.1 cgd 649: {
650:
1.22 mycroft 651: return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, (struct mbuf *)0,
1.111.2.3! yamt 652: (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1.1 cgd 653: }
654:
1.3 andrew 655: int
1.54 lukem 656: soaccept(struct socket *so, struct mbuf *nam)
1.1 cgd 657: {
1.54 lukem 658: int s, error;
1.1 cgd 659:
1.54 lukem 660: error = 0;
661: s = splsoftnet();
1.1 cgd 662: if ((so->so_state & SS_NOFDREF) == 0)
663: panic("soaccept: !NOFDREF");
664: so->so_state &= ~SS_NOFDREF;
1.55 thorpej 665: if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
666: (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
1.41 mycroft 667: error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
1.111.2.3! yamt 668: (struct mbuf *)0, nam, (struct mbuf *)0, (struct lwp *)0);
1.41 mycroft 669: else
1.53 itojun 670: error = ECONNABORTED;
1.52 itojun 671:
1.1 cgd 672: splx(s);
673: return (error);
674: }
675:
1.3 andrew 676: int
1.111.2.3! yamt 677: soconnect(struct socket *so, struct mbuf *nam, struct lwp *l)
1.1 cgd 678: {
1.54 lukem 679: int s, error;
1.1 cgd 680:
681: if (so->so_options & SO_ACCEPTCONN)
682: return (EOPNOTSUPP);
1.20 mycroft 683: s = splsoftnet();
1.1 cgd 684: /*
685: * If protocol is connection-based, can only connect once.
686: * Otherwise, if connected, try to disconnect first.
687: * This allows user to disconnect by connecting to, e.g.,
688: * a null address.
689: */
690: if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
691: ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
692: (error = sodisconnect(so))))
693: error = EISCONN;
694: else
695: error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
1.111.2.3! yamt 696: (struct mbuf *)0, nam, (struct mbuf *)0, l);
1.1 cgd 697: splx(s);
698: return (error);
699: }
700:
1.3 andrew 701: int
1.54 lukem 702: soconnect2(struct socket *so1, struct socket *so2)
1.1 cgd 703: {
1.54 lukem 704: int s, error;
1.1 cgd 705:
1.54 lukem 706: s = splsoftnet();
1.22 mycroft 707: error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
708: (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0,
1.111.2.3! yamt 709: (struct lwp *)0);
1.1 cgd 710: splx(s);
711: return (error);
712: }
713:
1.3 andrew 714: int
1.54 lukem 715: sodisconnect(struct socket *so)
1.1 cgd 716: {
1.54 lukem 717: int s, error;
1.1 cgd 718:
1.54 lukem 719: s = splsoftnet();
1.1 cgd 720: if ((so->so_state & SS_ISCONNECTED) == 0) {
721: error = ENOTCONN;
722: goto bad;
723: }
724: if (so->so_state & SS_ISDISCONNECTING) {
725: error = EALREADY;
726: goto bad;
727: }
1.22 mycroft 728: error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
729: (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
1.111.2.3! yamt 730: (struct lwp *)0);
1.54 lukem 731: bad:
1.1 cgd 732: splx(s);
1.111.2.3! yamt 733: sodopendfree();
1.1 cgd 734: return (error);
735: }
736:
1.15 mycroft 737: #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
1.1 cgd 738: /*
739: * Send on a socket.
740: * If send must go all at once and message is larger than
741: * send buffering, then hard error.
742: * Lock against other senders.
743: * If must go all at once and not enough room now, then
744: * inform user that this would block and do nothing.
745: * Otherwise, if nonblocking, send as much as possible.
746: * The data to be sent is described by "uio" if nonzero,
747: * otherwise by the mbuf chain "top" (which must be null
748: * if uio is not). Data provided in mbuf chain must be small
749: * enough to send all at once.
750: *
751: * Returns nonzero on error, timeout or signal; callers
752: * must check for short counts if EINTR/ERESTART are returned.
753: * Data and control buffers are freed on return.
754: */
1.3 andrew 755: int
1.54 lukem 756: sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
1.111.2.3! yamt 757: struct mbuf *control, int flags, struct lwp *l)
1.1 cgd 758: {
1.54 lukem 759: struct mbuf **mp, *m;
1.111.2.3! yamt 760: struct proc *p;
1.58 jdolecek 761: long space, len, resid, clen, mlen;
762: int error, s, dontroute, atomic;
1.54 lukem 763:
1.111.2.3! yamt 764: p = l->l_proc;
! 765: sodopendfree();
1.64 thorpej 766:
1.54 lukem 767: clen = 0;
768: atomic = sosendallatonce(so) || top;
1.1 cgd 769: if (uio)
770: resid = uio->uio_resid;
771: else
772: resid = top->m_pkthdr.len;
1.7 cgd 773: /*
774: * In theory resid should be unsigned.
775: * However, space must be signed, as it might be less than 0
776: * if we over-committed, and we must use a signed comparison
777: * of space and resid. On the other hand, a negative resid
778: * causes us to loop sending 0-length segments to the protocol.
779: */
1.29 mycroft 780: if (resid < 0) {
781: error = EINVAL;
782: goto out;
783: }
1.1 cgd 784: dontroute =
785: (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
786: (so->so_proto->pr_flags & PR_ATOMIC);
1.102 jonathan 787: if (p)
788: p->p_stats->p_ru.ru_msgsnd++;
1.1 cgd 789: if (control)
790: clen = control->m_len;
791: #define snderr(errno) { error = errno; splx(s); goto release; }
792:
1.54 lukem 793: restart:
1.21 christos 794: if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
1.1 cgd 795: goto out;
796: do {
1.20 mycroft 797: s = splsoftnet();
1.1 cgd 798: if (so->so_state & SS_CANTSENDMORE)
799: snderr(EPIPE);
1.48 thorpej 800: if (so->so_error) {
801: error = so->so_error;
802: so->so_error = 0;
803: splx(s);
804: goto release;
805: }
1.1 cgd 806: if ((so->so_state & SS_ISCONNECTED) == 0) {
807: if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
808: if ((so->so_state & SS_ISCONFIRMING) == 0 &&
809: !(resid == 0 && clen != 0))
810: snderr(ENOTCONN);
811: } else if (addr == 0)
812: snderr(EDESTADDRREQ);
813: }
814: space = sbspace(&so->so_snd);
815: if (flags & MSG_OOB)
816: space += 1024;
1.21 christos 817: if ((atomic && resid > so->so_snd.sb_hiwat) ||
1.11 mycroft 818: clen > so->so_snd.sb_hiwat)
819: snderr(EMSGSIZE);
1.96 mycroft 820: if (space < resid + clen &&
1.1 cgd 821: (atomic || space < so->so_snd.sb_lowat || space < clen)) {
822: if (so->so_state & SS_NBIO)
823: snderr(EWOULDBLOCK);
824: sbunlock(&so->so_snd);
825: error = sbwait(&so->so_snd);
826: splx(s);
827: if (error)
828: goto out;
829: goto restart;
830: }
831: splx(s);
832: mp = ⊤
833: space -= clen;
834: do {
1.45 tv 835: if (uio == NULL) {
836: /*
837: * Data is prepackaged in "top".
838: */
839: resid = 0;
840: if (flags & MSG_EOR)
841: top->m_flags |= M_EOR;
842: } else do {
843: if (top == 0) {
1.78 matt 844: m = m_gethdr(M_WAIT, MT_DATA);
1.45 tv 845: mlen = MHLEN;
846: m->m_pkthdr.len = 0;
847: m->m_pkthdr.rcvif = (struct ifnet *)0;
848: } else {
1.78 matt 849: m = m_get(M_WAIT, MT_DATA);
1.45 tv 850: mlen = MLEN;
851: }
1.78 matt 852: MCLAIM(m, so->so_snd.sb_mowner);
1.111.2.3! yamt 853: if (sock_loan_thresh >= 0 &&
! 854: uio->uio_iov->iov_len >= sock_loan_thresh &&
! 855: space >= sock_loan_thresh &&
1.64 thorpej 856: (len = sosend_loan(so, uio, m,
857: space)) != 0) {
858: SOSEND_COUNTER_INCR(&sosend_loan_big);
859: space -= len;
860: goto have_data;
861: }
1.45 tv 862: if (resid >= MINCLSIZE && space >= MCLBYTES) {
1.64 thorpej 863: SOSEND_COUNTER_INCR(&sosend_copy_big);
1.78 matt 864: m_clget(m, M_WAIT);
1.45 tv 865: if ((m->m_flags & M_EXT) == 0)
866: goto nopages;
867: mlen = MCLBYTES;
868: if (atomic && top == 0) {
1.58 jdolecek 869: len = lmin(MCLBYTES - max_hdr,
1.54 lukem 870: resid);
1.45 tv 871: m->m_data += max_hdr;
872: } else
1.58 jdolecek 873: len = lmin(MCLBYTES, resid);
1.45 tv 874: space -= len;
875: } else {
1.64 thorpej 876: nopages:
877: SOSEND_COUNTER_INCR(&sosend_copy_small);
1.58 jdolecek 878: len = lmin(lmin(mlen, resid), space);
1.45 tv 879: space -= len;
880: /*
881: * For datagram protocols, leave room
882: * for protocol headers in first mbuf.
883: */
884: if (atomic && top == 0 && len < mlen)
885: MH_ALIGN(m, len);
886: }
1.54 lukem 887: error = uiomove(mtod(m, caddr_t), (int)len,
888: uio);
1.64 thorpej 889: have_data:
1.45 tv 890: resid = uio->uio_resid;
891: m->m_len = len;
892: *mp = m;
893: top->m_pkthdr.len += len;
894: if (error)
895: goto release;
896: mp = &m->m_next;
897: if (resid <= 0) {
898: if (flags & MSG_EOR)
899: top->m_flags |= M_EOR;
900: break;
901: }
902: } while (space > 0 && atomic);
1.108 perry 903:
1.46 sommerfe 904: s = splsoftnet();
905:
906: if (so->so_state & SS_CANTSENDMORE)
907: snderr(EPIPE);
1.45 tv 908:
909: if (dontroute)
910: so->so_options |= SO_DONTROUTE;
911: if (resid > 0)
912: so->so_state |= SS_MORETOCOME;
1.46 sommerfe 913: error = (*so->so_proto->pr_usrreq)(so,
914: (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
1.111.2.3! yamt 915: top, addr, control, curlwp); /* XXX */
1.45 tv 916: if (dontroute)
917: so->so_options &= ~SO_DONTROUTE;
918: if (resid > 0)
919: so->so_state &= ~SS_MORETOCOME;
1.46 sommerfe 920: splx(s);
921:
1.45 tv 922: clen = 0;
923: control = 0;
924: top = 0;
925: mp = ⊤
1.1 cgd 926: if (error)
927: goto release;
928: } while (resid && space > 0);
929: } while (resid);
930:
1.54 lukem 931: release:
1.1 cgd 932: sbunlock(&so->so_snd);
1.54 lukem 933: out:
1.1 cgd 934: if (top)
935: m_freem(top);
936: if (control)
937: m_freem(control);
938: return (error);
939: }
940:
941: /*
942: * Implement receive operations on a socket.
943: * We depend on the way that records are added to the sockbuf
944: * by sbappend*. In particular, each record (mbufs linked through m_next)
945: * must begin with an address if the protocol so specifies,
946: * followed by an optional mbuf or mbufs containing ancillary data,
947: * and then zero or more mbufs of data.
948: * In order to avoid blocking network interrupts for the entire time here,
949: * we splx() while doing the actual copy to user space.
950: * Although the sockbuf is locked, new data may still be appended,
951: * and thus we must maintain consistency of the sockbuf during that time.
952: *
953: * The caller may receive the data as a single mbuf chain by supplying
954: * an mbuf **mp0 for use in returning the chain. The uio is then used
955: * only for the count in uio_resid.
956: */
1.3 andrew 957: int
1.54 lukem 958: soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
959: struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1.1 cgd 960: {
1.111.2.3! yamt 961: struct lwp *l = curlwp;
1.54 lukem 962: struct mbuf *m, **mp;
963: int flags, len, error, s, offset, moff, type, orig_resid;
1.99 matt 964: const struct protosw *pr;
1.54 lukem 965: struct mbuf *nextrecord;
1.67 he 966: int mbuf_removed = 0;
1.64 thorpej 967:
1.54 lukem 968: pr = so->so_proto;
1.1 cgd 969: mp = mp0;
1.54 lukem 970: type = 0;
971: orig_resid = uio->uio_resid;
1.102 jonathan 972:
1.1 cgd 973: if (paddr)
974: *paddr = 0;
975: if (controlp)
976: *controlp = 0;
977: if (flagsp)
978: flags = *flagsp &~ MSG_EOR;
979: else
980: flags = 0;
1.66 enami 981:
982: if ((flags & MSG_DONTWAIT) == 0)
1.111.2.3! yamt 983: sodopendfree();
1.66 enami 984:
1.1 cgd 985: if (flags & MSG_OOB) {
986: m = m_get(M_WAIT, MT_DATA);
1.17 cgd 987: error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
1.102 jonathan 988: (struct mbuf *)(long)(flags & MSG_PEEK),
1.111.2.3! yamt 989: (struct mbuf *)0, l);
1.1 cgd 990: if (error)
991: goto bad;
992: do {
993: error = uiomove(mtod(m, caddr_t),
994: (int) min(uio->uio_resid, m->m_len), uio);
995: m = m_free(m);
996: } while (uio->uio_resid && error == 0 && m);
1.54 lukem 997: bad:
1.1 cgd 998: if (m)
999: m_freem(m);
1000: return (error);
1001: }
1002: if (mp)
1003: *mp = (struct mbuf *)0;
1004: if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
1.22 mycroft 1005: (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
1.111.2.3! yamt 1006: (struct mbuf *)0, (struct mbuf *)0, l);
1.1 cgd 1007:
1.54 lukem 1008: restart:
1.21 christos 1009: if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
1.1 cgd 1010: return (error);
1.20 mycroft 1011: s = splsoftnet();
1.1 cgd 1012:
1013: m = so->so_rcv.sb_mb;
1014: /*
1015: * If we have less data than requested, block awaiting more
1016: * (subject to any timeout) if:
1.15 mycroft 1017: * 1. the current count is less than the low water mark,
1.1 cgd 1018: * 2. MSG_WAITALL is set, and it is possible to do the entire
1.15 mycroft 1019: * receive operation at once if we block (resid <= hiwat), or
1020: * 3. MSG_DONTWAIT is not set.
1.1 cgd 1021: * If MSG_WAITALL is set but resid is larger than the receive buffer,
1022: * we have to do the receive in sections, and thus risk returning
1023: * a short count if a timeout or signal occurs after we start.
1024: */
1.21 christos 1025: if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
1.15 mycroft 1026: so->so_rcv.sb_cc < uio->uio_resid) &&
1.1 cgd 1027: (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
1028: ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1.21 christos 1029: m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
1.1 cgd 1030: #ifdef DIAGNOSTIC
1031: if (m == 0 && so->so_rcv.sb_cc)
1032: panic("receive 1");
1033: #endif
1034: if (so->so_error) {
1035: if (m)
1.15 mycroft 1036: goto dontblock;
1.1 cgd 1037: error = so->so_error;
1038: if ((flags & MSG_PEEK) == 0)
1039: so->so_error = 0;
1040: goto release;
1041: }
1042: if (so->so_state & SS_CANTRCVMORE) {
1043: if (m)
1.15 mycroft 1044: goto dontblock;
1.1 cgd 1045: else
1046: goto release;
1047: }
1048: for (; m; m = m->m_next)
1049: if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1050: m = so->so_rcv.sb_mb;
1051: goto dontblock;
1052: }
1053: if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1054: (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1055: error = ENOTCONN;
1056: goto release;
1057: }
1058: if (uio->uio_resid == 0)
1059: goto release;
1.15 mycroft 1060: if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
1.1 cgd 1061: error = EWOULDBLOCK;
1062: goto release;
1063: }
1.69 thorpej 1064: SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1");
1065: SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1");
1.1 cgd 1066: sbunlock(&so->so_rcv);
1067: error = sbwait(&so->so_rcv);
1068: splx(s);
1069: if (error)
1070: return (error);
1071: goto restart;
1072: }
1.54 lukem 1073: dontblock:
1.69 thorpej 1074: /*
1075: * On entry here, m points to the first record of the socket buffer.
1076: * While we process the initial mbufs containing address and control
1077: * info, we save a copy of m->m_nextpkt into nextrecord.
1078: */
1.111.2.3! yamt 1079: if (l)
! 1080: l->l_proc->p_stats->p_ru.ru_msgrcv++;
1.69 thorpej 1081: KASSERT(m == so->so_rcv.sb_mb);
1082: SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
1083: SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
1.1 cgd 1084: nextrecord = m->m_nextpkt;
1085: if (pr->pr_flags & PR_ADDR) {
1086: #ifdef DIAGNOSTIC
1087: if (m->m_type != MT_SONAME)
1088: panic("receive 1a");
1089: #endif
1.3 andrew 1090: orig_resid = 0;
1.1 cgd 1091: if (flags & MSG_PEEK) {
1092: if (paddr)
1093: *paddr = m_copy(m, 0, m->m_len);
1094: m = m->m_next;
1095: } else {
1096: sbfree(&so->so_rcv, m);
1.67 he 1097: mbuf_removed = 1;
1.1 cgd 1098: if (paddr) {
1099: *paddr = m;
1100: so->so_rcv.sb_mb = m->m_next;
1101: m->m_next = 0;
1102: m = so->so_rcv.sb_mb;
1103: } else {
1104: MFREE(m, so->so_rcv.sb_mb);
1105: m = so->so_rcv.sb_mb;
1106: }
1107: }
1108: }
1109: while (m && m->m_type == MT_CONTROL && error == 0) {
1110: if (flags & MSG_PEEK) {
1111: if (controlp)
1112: *controlp = m_copy(m, 0, m->m_len);
1113: m = m->m_next;
1114: } else {
1115: sbfree(&so->so_rcv, m);
1.67 he 1116: mbuf_removed = 1;
1.1 cgd 1117: if (controlp) {
1.102 jonathan 1118: struct domain *dom = pr->pr_domain;
1.111.2.3! yamt 1119: if (dom->dom_externalize && l &&
1.1 cgd 1120: mtod(m, struct cmsghdr *)->cmsg_type ==
1121: SCM_RIGHTS)
1.111.2.3! yamt 1122: error = (*dom->dom_externalize)(m, l);
1.1 cgd 1123: *controlp = m;
1124: so->so_rcv.sb_mb = m->m_next;
1125: m->m_next = 0;
1126: m = so->so_rcv.sb_mb;
1127: } else {
1.106 itojun 1128: /*
1129: * Dispose of any SCM_RIGHTS message that went
1130: * through the read path rather than recv.
1131: */
1132: if (pr->pr_domain->dom_dispose &&
1133: mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
1134: (*pr->pr_domain->dom_dispose)(m);
1.1 cgd 1135: MFREE(m, so->so_rcv.sb_mb);
1136: m = so->so_rcv.sb_mb;
1137: }
1138: }
1.3 andrew 1139: if (controlp) {
1140: orig_resid = 0;
1.1 cgd 1141: controlp = &(*controlp)->m_next;
1.3 andrew 1142: }
1.1 cgd 1143: }
1.69 thorpej 1144:
1145: /*
1146: * If m is non-NULL, we have some data to read. From now on,
1147: * make sure to keep sb_lastrecord consistent when working on
1148: * the last packet on the chain (nextrecord == NULL) and we
1149: * change m->m_nextpkt.
1150: */
1.1 cgd 1151: if (m) {
1.69 thorpej 1152: if ((flags & MSG_PEEK) == 0) {
1.1 cgd 1153: m->m_nextpkt = nextrecord;
1.69 thorpej 1154: /*
1155: * If nextrecord == NULL (this is a single chain),
1156: * then sb_lastrecord may not be valid here if m
1157: * was changed earlier.
1158: */
1159: if (nextrecord == NULL) {
1160: KASSERT(so->so_rcv.sb_mb == m);
1161: so->so_rcv.sb_lastrecord = m;
1162: }
1163: }
1.1 cgd 1164: type = m->m_type;
1165: if (type == MT_OOBDATA)
1166: flags |= MSG_OOB;
1.69 thorpej 1167: } else {
1168: if ((flags & MSG_PEEK) == 0) {
1169: KASSERT(so->so_rcv.sb_mb == m);
1170: so->so_rcv.sb_mb = nextrecord;
1.70 thorpej 1171: SB_EMPTY_FIXUP(&so->so_rcv);
1.69 thorpej 1172: }
1.1 cgd 1173: }
1.69 thorpej 1174: SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
1175: SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
1176:
1.1 cgd 1177: moff = 0;
1178: offset = 0;
1179: while (m && uio->uio_resid > 0 && error == 0) {
1180: if (m->m_type == MT_OOBDATA) {
1181: if (type != MT_OOBDATA)
1182: break;
1183: } else if (type == MT_OOBDATA)
1184: break;
1185: #ifdef DIAGNOSTIC
1186: else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
1187: panic("receive 3");
1188: #endif
1189: so->so_state &= ~SS_RCVATMARK;
1190: len = uio->uio_resid;
1191: if (so->so_oobmark && len > so->so_oobmark - offset)
1192: len = so->so_oobmark - offset;
1193: if (len > m->m_len - moff)
1194: len = m->m_len - moff;
1195: /*
1196: * If mp is set, just pass back the mbufs.
1197: * Otherwise copy them out via the uio, then free.
1198: * Sockbuf must be consistent here (points to current mbuf,
1199: * it points to next record) when we drop priority;
1200: * we must note any additions to the sockbuf when we
1201: * block interrupts again.
1202: */
1203: if (mp == 0) {
1.69 thorpej 1204: SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove");
1205: SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove");
1.1 cgd 1206: splx(s);
1207: error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
1.20 mycroft 1208: s = splsoftnet();
1.67 he 1209: if (error) {
1210: /*
1211: * If any part of the record has been removed
1212: * (such as the MT_SONAME mbuf, which will
1213: * happen when PR_ADDR, and thus also
1214: * PR_ATOMIC, is set), then drop the entire
1215: * record to maintain the atomicity of the
1216: * receive operation.
1217: *
1218: * This avoids a later panic("receive 1a")
1219: * when compiled with DIAGNOSTIC.
1220: */
1221: if (m && mbuf_removed
1222: && (pr->pr_flags & PR_ATOMIC))
1223: (void) sbdroprecord(&so->so_rcv);
1224:
1.57 jdolecek 1225: goto release;
1.67 he 1226: }
1.1 cgd 1227: } else
1228: uio->uio_resid -= len;
1229: if (len == m->m_len - moff) {
1230: if (m->m_flags & M_EOR)
1231: flags |= MSG_EOR;
1232: if (flags & MSG_PEEK) {
1233: m = m->m_next;
1234: moff = 0;
1235: } else {
1236: nextrecord = m->m_nextpkt;
1237: sbfree(&so->so_rcv, m);
1238: if (mp) {
1239: *mp = m;
1240: mp = &m->m_next;
1241: so->so_rcv.sb_mb = m = m->m_next;
1242: *mp = (struct mbuf *)0;
1243: } else {
1244: MFREE(m, so->so_rcv.sb_mb);
1245: m = so->so_rcv.sb_mb;
1246: }
1.69 thorpej 1247: /*
1248: * If m != NULL, we also know that
1249: * so->so_rcv.sb_mb != NULL.
1250: */
1251: KASSERT(so->so_rcv.sb_mb == m);
1252: if (m) {
1.1 cgd 1253: m->m_nextpkt = nextrecord;
1.69 thorpej 1254: if (nextrecord == NULL)
1255: so->so_rcv.sb_lastrecord = m;
1256: } else {
1257: so->so_rcv.sb_mb = nextrecord;
1.70 thorpej 1258: SB_EMPTY_FIXUP(&so->so_rcv);
1.69 thorpej 1259: }
1260: SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
1261: SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
1.1 cgd 1262: }
1263: } else {
1264: if (flags & MSG_PEEK)
1265: moff += len;
1266: else {
1267: if (mp)
1268: *mp = m_copym(m, 0, len, M_WAIT);
1269: m->m_data += len;
1270: m->m_len -= len;
1271: so->so_rcv.sb_cc -= len;
1272: }
1273: }
1274: if (so->so_oobmark) {
1275: if ((flags & MSG_PEEK) == 0) {
1276: so->so_oobmark -= len;
1277: if (so->so_oobmark == 0) {
1278: so->so_state |= SS_RCVATMARK;
1279: break;
1280: }
1.7 cgd 1281: } else {
1.1 cgd 1282: offset += len;
1.7 cgd 1283: if (offset == so->so_oobmark)
1284: break;
1285: }
1.1 cgd 1286: }
1287: if (flags & MSG_EOR)
1288: break;
1289: /*
1290: * If the MSG_WAITALL flag is set (for non-atomic socket),
1291: * we must not quit until "uio->uio_resid == 0" or an error
1292: * termination. If a signal/timeout occurs, return
1293: * with a short count but without error.
1294: * Keep sockbuf locked against other readers.
1295: */
1296: while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
1.3 andrew 1297: !sosendallatonce(so) && !nextrecord) {
1.1 cgd 1298: if (so->so_error || so->so_state & SS_CANTRCVMORE)
1299: break;
1.68 matt 1300: /*
1301: * If we are peeking and the socket receive buffer is
1302: * full, stop since we can't get more data to peek at.
1303: */
1304: if ((flags & MSG_PEEK) && sbspace(&so->so_rcv) <= 0)
1305: break;
1306: /*
1307: * If we've drained the socket buffer, tell the
1308: * protocol in case it needs to do something to
1309: * get it filled again.
1310: */
1311: if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
1312: (*pr->pr_usrreq)(so, PRU_RCVD,
1313: (struct mbuf *)0,
1314: (struct mbuf *)(long)flags,
1.111.2.3! yamt 1315: (struct mbuf *)0, l);
1.69 thorpej 1316: SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2");
1317: SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2");
1.1 cgd 1318: error = sbwait(&so->so_rcv);
1319: if (error) {
1320: sbunlock(&so->so_rcv);
1321: splx(s);
1322: return (0);
1323: }
1.21 christos 1324: if ((m = so->so_rcv.sb_mb) != NULL)
1.1 cgd 1325: nextrecord = m->m_nextpkt;
1326: }
1327: }
1.3 andrew 1328:
1329: if (m && pr->pr_flags & PR_ATOMIC) {
1330: flags |= MSG_TRUNC;
1331: if ((flags & MSG_PEEK) == 0)
1332: (void) sbdroprecord(&so->so_rcv);
1333: }
1.1 cgd 1334: if ((flags & MSG_PEEK) == 0) {
1.69 thorpej 1335: if (m == 0) {
1336: /*
1.70 thorpej 1337: * First part is an inline SB_EMPTY_FIXUP(). Second
1.69 thorpej 1338: * part makes sure sb_lastrecord is up-to-date if
1339: * there is still data in the socket buffer.
1340: */
1.1 cgd 1341: so->so_rcv.sb_mb = nextrecord;
1.69 thorpej 1342: if (so->so_rcv.sb_mb == NULL) {
1343: so->so_rcv.sb_mbtail = NULL;
1344: so->so_rcv.sb_lastrecord = NULL;
1345: } else if (nextrecord->m_nextpkt == NULL)
1346: so->so_rcv.sb_lastrecord = nextrecord;
1347: }
1348: SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
1349: SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
1.1 cgd 1350: if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1.22 mycroft 1351: (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
1.111.2.3! yamt 1352: (struct mbuf *)(long)flags, (struct mbuf *)0, l);
1.1 cgd 1353: }
1.3 andrew 1354: if (orig_resid == uio->uio_resid && orig_resid &&
1355: (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
1356: sbunlock(&so->so_rcv);
1357: splx(s);
1358: goto restart;
1359: }
1.108 perry 1360:
1.1 cgd 1361: if (flagsp)
1362: *flagsp |= flags;
1.54 lukem 1363: release:
1.1 cgd 1364: sbunlock(&so->so_rcv);
1365: splx(s);
1366: return (error);
1367: }
1368:
1.14 mycroft 1369: int
1.54 lukem 1370: soshutdown(struct socket *so, int how)
1.1 cgd 1371: {
1.99 matt 1372: const struct protosw *pr;
1.34 kleink 1373:
1.54 lukem 1374: pr = so->so_proto;
1.34 kleink 1375: if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1376: return (EINVAL);
1.1 cgd 1377:
1.34 kleink 1378: if (how == SHUT_RD || how == SHUT_RDWR)
1.1 cgd 1379: sorflush(so);
1.34 kleink 1380: if (how == SHUT_WR || how == SHUT_RDWR)
1.22 mycroft 1381: return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0,
1.111.2.3! yamt 1382: (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1.1 cgd 1383: return (0);
1384: }
1385:
1.14 mycroft 1386: void
1.54 lukem 1387: sorflush(struct socket *so)
1.1 cgd 1388: {
1.54 lukem 1389: struct sockbuf *sb, asb;
1.99 matt 1390: const struct protosw *pr;
1.54 lukem 1391: int s;
1.1 cgd 1392:
1.54 lukem 1393: sb = &so->so_rcv;
1394: pr = so->so_proto;
1.1 cgd 1395: sb->sb_flags |= SB_NOINTR;
1.15 mycroft 1396: (void) sblock(sb, M_WAITOK);
1.56 thorpej 1397: s = splnet();
1.1 cgd 1398: socantrcvmore(so);
1399: sbunlock(sb);
1400: asb = *sb;
1.86 wrstuden 1401: /*
1402: * Clear most of the sockbuf structure, but leave some of the
1403: * fields valid.
1404: */
1405: memset(&sb->sb_startzero, 0,
1406: sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
1.1 cgd 1407: splx(s);
1408: if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
1409: (*pr->pr_domain->dom_dispose)(asb.sb_mb);
1.98 christos 1410: sbrelease(&asb, so);
1.1 cgd 1411: }
1412:
1.14 mycroft 1413: int
1.54 lukem 1414: sosetopt(struct socket *so, int level, int optname, struct mbuf *m0)
1.1 cgd 1415: {
1.54 lukem 1416: int error;
1417: struct mbuf *m;
1.1 cgd 1418:
1.54 lukem 1419: error = 0;
1420: m = m0;
1.1 cgd 1421: if (level != SOL_SOCKET) {
1422: if (so->so_proto && so->so_proto->pr_ctloutput)
1423: return ((*so->so_proto->pr_ctloutput)
1424: (PRCO_SETOPT, so, level, optname, &m0));
1425: error = ENOPROTOOPT;
1426: } else {
1427: switch (optname) {
1428:
1429: case SO_LINGER:
1.36 perry 1430: if (m == NULL || m->m_len != sizeof(struct linger)) {
1.1 cgd 1431: error = EINVAL;
1432: goto bad;
1433: }
1.111.2.3! yamt 1434: if (mtod(m, struct linger *)->l_linger < 0 ||
! 1435: mtod(m, struct linger *)->l_linger > (INT_MAX / hz)) {
! 1436: error = EDOM;
! 1437: goto bad;
! 1438: }
1.1 cgd 1439: so->so_linger = mtod(m, struct linger *)->l_linger;
1440: /* fall thru... */
1441:
1442: case SO_DEBUG:
1443: case SO_KEEPALIVE:
1444: case SO_DONTROUTE:
1445: case SO_USELOOPBACK:
1446: case SO_BROADCAST:
1447: case SO_REUSEADDR:
1.15 mycroft 1448: case SO_REUSEPORT:
1.1 cgd 1449: case SO_OOBINLINE:
1.26 thorpej 1450: case SO_TIMESTAMP:
1.36 perry 1451: if (m == NULL || m->m_len < sizeof(int)) {
1.1 cgd 1452: error = EINVAL;
1453: goto bad;
1454: }
1455: if (*mtod(m, int *))
1456: so->so_options |= optname;
1457: else
1458: so->so_options &= ~optname;
1459: break;
1460:
1461: case SO_SNDBUF:
1462: case SO_RCVBUF:
1463: case SO_SNDLOWAT:
1464: case SO_RCVLOWAT:
1.28 thorpej 1465: {
1466: int optval;
1467:
1.36 perry 1468: if (m == NULL || m->m_len < sizeof(int)) {
1.1 cgd 1469: error = EINVAL;
1470: goto bad;
1471: }
1.28 thorpej 1472:
1473: /*
1474: * Values < 1 make no sense for any of these
1475: * options, so disallow them.
1476: */
1477: optval = *mtod(m, int *);
1478: if (optval < 1) {
1479: error = EINVAL;
1480: goto bad;
1481: }
1482:
1.1 cgd 1483: switch (optname) {
1484:
1485: case SO_SNDBUF:
1486: case SO_RCVBUF:
1487: if (sbreserve(optname == SO_SNDBUF ?
1488: &so->so_snd : &so->so_rcv,
1.98 christos 1489: (u_long) optval, so) == 0) {
1.1 cgd 1490: error = ENOBUFS;
1491: goto bad;
1492: }
1493: break;
1494:
1.28 thorpej 1495: /*
1496: * Make sure the low-water is never greater than
1497: * the high-water.
1498: */
1.1 cgd 1499: case SO_SNDLOWAT:
1.28 thorpej 1500: so->so_snd.sb_lowat =
1501: (optval > so->so_snd.sb_hiwat) ?
1502: so->so_snd.sb_hiwat : optval;
1.1 cgd 1503: break;
1504: case SO_RCVLOWAT:
1.28 thorpej 1505: so->so_rcv.sb_lowat =
1506: (optval > so->so_rcv.sb_hiwat) ?
1507: so->so_rcv.sb_hiwat : optval;
1.1 cgd 1508: break;
1509: }
1510: break;
1.28 thorpej 1511: }
1.1 cgd 1512:
1513: case SO_SNDTIMEO:
1514: case SO_RCVTIMEO:
1515: {
1516: struct timeval *tv;
1.104 yamt 1517: int val;
1.1 cgd 1518:
1.36 perry 1519: if (m == NULL || m->m_len < sizeof(*tv)) {
1.1 cgd 1520: error = EINVAL;
1521: goto bad;
1522: }
1523: tv = mtod(m, struct timeval *);
1.104 yamt 1524: if (tv->tv_sec > (INT_MAX - tv->tv_usec / tick) / hz) {
1.1 cgd 1525: error = EDOM;
1526: goto bad;
1527: }
1528: val = tv->tv_sec * hz + tv->tv_usec / tick;
1.74 itojun 1529: if (val == 0 && tv->tv_usec != 0)
1530: val = 1;
1.1 cgd 1531:
1532: switch (optname) {
1533:
1534: case SO_SNDTIMEO:
1535: so->so_snd.sb_timeo = val;
1536: break;
1537: case SO_RCVTIMEO:
1538: so->so_rcv.sb_timeo = val;
1539: break;
1540: }
1541: break;
1542: }
1543:
1544: default:
1545: error = ENOPROTOOPT;
1546: break;
1547: }
1.15 mycroft 1548: if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1549: (void) ((*so->so_proto->pr_ctloutput)
1550: (PRCO_SETOPT, so, level, optname, &m0));
1551: m = NULL; /* freed by protocol */
1552: }
1.1 cgd 1553: }
1.54 lukem 1554: bad:
1.1 cgd 1555: if (m)
1556: (void) m_free(m);
1557: return (error);
1558: }
1559:
1.14 mycroft 1560: int
1.54 lukem 1561: sogetopt(struct socket *so, int level, int optname, struct mbuf **mp)
1.1 cgd 1562: {
1.54 lukem 1563: struct mbuf *m;
1.1 cgd 1564:
1565: if (level != SOL_SOCKET) {
1566: if (so->so_proto && so->so_proto->pr_ctloutput) {
1567: return ((*so->so_proto->pr_ctloutput)
1568: (PRCO_GETOPT, so, level, optname, mp));
1569: } else
1570: return (ENOPROTOOPT);
1571: } else {
1572: m = m_get(M_WAIT, MT_SOOPTS);
1.36 perry 1573: m->m_len = sizeof(int);
1.1 cgd 1574:
1575: switch (optname) {
1576:
1577: case SO_LINGER:
1.36 perry 1578: m->m_len = sizeof(struct linger);
1.1 cgd 1579: mtod(m, struct linger *)->l_onoff =
1580: so->so_options & SO_LINGER;
1581: mtod(m, struct linger *)->l_linger = so->so_linger;
1582: break;
1583:
1584: case SO_USELOOPBACK:
1585: case SO_DONTROUTE:
1586: case SO_DEBUG:
1587: case SO_KEEPALIVE:
1588: case SO_REUSEADDR:
1.15 mycroft 1589: case SO_REUSEPORT:
1.1 cgd 1590: case SO_BROADCAST:
1591: case SO_OOBINLINE:
1.26 thorpej 1592: case SO_TIMESTAMP:
1.1 cgd 1593: *mtod(m, int *) = so->so_options & optname;
1594: break;
1595:
1596: case SO_TYPE:
1597: *mtod(m, int *) = so->so_type;
1598: break;
1599:
1600: case SO_ERROR:
1601: *mtod(m, int *) = so->so_error;
1602: so->so_error = 0;
1603: break;
1604:
1605: case SO_SNDBUF:
1606: *mtod(m, int *) = so->so_snd.sb_hiwat;
1607: break;
1608:
1609: case SO_RCVBUF:
1610: *mtod(m, int *) = so->so_rcv.sb_hiwat;
1611: break;
1612:
1613: case SO_SNDLOWAT:
1614: *mtod(m, int *) = so->so_snd.sb_lowat;
1615: break;
1616:
1617: case SO_RCVLOWAT:
1618: *mtod(m, int *) = so->so_rcv.sb_lowat;
1619: break;
1620:
1621: case SO_SNDTIMEO:
1622: case SO_RCVTIMEO:
1623: {
1624: int val = (optname == SO_SNDTIMEO ?
1625: so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1626:
1627: m->m_len = sizeof(struct timeval);
1628: mtod(m, struct timeval *)->tv_sec = val / hz;
1629: mtod(m, struct timeval *)->tv_usec =
1.27 kleink 1630: (val % hz) * tick;
1.1 cgd 1631: break;
1632: }
1633:
1.107 darrenr 1634: case SO_OVERFLOWED:
1635: *mtod(m, int *) = so->so_rcv.sb_overflowed;
1636: break;
1637:
1.1 cgd 1638: default:
1639: (void)m_free(m);
1640: return (ENOPROTOOPT);
1641: }
1642: *mp = m;
1643: return (0);
1644: }
1645: }
1646:
1.14 mycroft 1647: void
1.54 lukem 1648: sohasoutofband(struct socket *so)
1.1 cgd 1649: {
1.90 christos 1650: fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so);
1.2 cgd 1651: selwakeup(&so->so_rcv.sb_sel);
1.1 cgd 1652: }
1.72 jdolecek 1653:
1654: static void
1655: filt_sordetach(struct knote *kn)
1656: {
1657: struct socket *so;
1658:
1659: so = (struct socket *)kn->kn_fp->f_data;
1.73 christos 1660: SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
1661: if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
1.72 jdolecek 1662: so->so_rcv.sb_flags &= ~SB_KNOTE;
1663: }
1664:
1665: /*ARGSUSED*/
1666: static int
1667: filt_soread(struct knote *kn, long hint)
1668: {
1669: struct socket *so;
1670:
1671: so = (struct socket *)kn->kn_fp->f_data;
1672: kn->kn_data = so->so_rcv.sb_cc;
1673: if (so->so_state & SS_CANTRCVMORE) {
1.108 perry 1674: kn->kn_flags |= EV_EOF;
1.72 jdolecek 1675: kn->kn_fflags = so->so_error;
1676: return (1);
1677: }
1678: if (so->so_error) /* temporary udp error */
1679: return (1);
1680: if (kn->kn_sfflags & NOTE_LOWAT)
1681: return (kn->kn_data >= kn->kn_sdata);
1682: return (kn->kn_data >= so->so_rcv.sb_lowat);
1683: }
1684:
1685: static void
1686: filt_sowdetach(struct knote *kn)
1687: {
1688: struct socket *so;
1689:
1690: so = (struct socket *)kn->kn_fp->f_data;
1.73 christos 1691: SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
1692: if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
1.72 jdolecek 1693: so->so_snd.sb_flags &= ~SB_KNOTE;
1694: }
1695:
1696: /*ARGSUSED*/
1697: static int
1698: filt_sowrite(struct knote *kn, long hint)
1699: {
1700: struct socket *so;
1701:
1702: so = (struct socket *)kn->kn_fp->f_data;
1703: kn->kn_data = sbspace(&so->so_snd);
1704: if (so->so_state & SS_CANTSENDMORE) {
1.108 perry 1705: kn->kn_flags |= EV_EOF;
1.72 jdolecek 1706: kn->kn_fflags = so->so_error;
1707: return (1);
1708: }
1709: if (so->so_error) /* temporary udp error */
1710: return (1);
1711: if (((so->so_state & SS_ISCONNECTED) == 0) &&
1712: (so->so_proto->pr_flags & PR_CONNREQUIRED))
1713: return (0);
1714: if (kn->kn_sfflags & NOTE_LOWAT)
1715: return (kn->kn_data >= kn->kn_sdata);
1716: return (kn->kn_data >= so->so_snd.sb_lowat);
1717: }
1718:
1719: /*ARGSUSED*/
1720: static int
1721: filt_solisten(struct knote *kn, long hint)
1722: {
1723: struct socket *so;
1724:
1725: so = (struct socket *)kn->kn_fp->f_data;
1726:
1727: /*
1728: * Set kn_data to number of incoming connections, not
1729: * counting partial (incomplete) connections.
1.108 perry 1730: */
1.72 jdolecek 1731: kn->kn_data = so->so_qlen;
1732: return (kn->kn_data > 0);
1733: }
1734:
1735: static const struct filterops solisten_filtops =
1736: { 1, NULL, filt_sordetach, filt_solisten };
1737: static const struct filterops soread_filtops =
1738: { 1, NULL, filt_sordetach, filt_soread };
1739: static const struct filterops sowrite_filtops =
1740: { 1, NULL, filt_sowdetach, filt_sowrite };
1741:
1742: int
1743: soo_kqfilter(struct file *fp, struct knote *kn)
1744: {
1745: struct socket *so;
1746: struct sockbuf *sb;
1747:
1748: so = (struct socket *)kn->kn_fp->f_data;
1749: switch (kn->kn_filter) {
1750: case EVFILT_READ:
1751: if (so->so_options & SO_ACCEPTCONN)
1752: kn->kn_fop = &solisten_filtops;
1753: else
1754: kn->kn_fop = &soread_filtops;
1755: sb = &so->so_rcv;
1756: break;
1757: case EVFILT_WRITE:
1758: kn->kn_fop = &sowrite_filtops;
1759: sb = &so->so_snd;
1760: break;
1761: default:
1762: return (1);
1763: }
1.73 christos 1764: SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, kn, kn_selnext);
1.72 jdolecek 1765: sb->sb_flags |= SB_KNOTE;
1766: return (0);
1767: }
1768:
1.94 yamt 1769: #include <sys/sysctl.h>
1770:
1771: static int sysctl_kern_somaxkva(SYSCTLFN_PROTO);
1772:
1773: /*
1774: * sysctl helper routine for kern.somaxkva. ensures that the given
1775: * value is not too small.
1776: * (XXX should we maybe make sure it's not too large as well?)
1777: */
1778: static int
1779: sysctl_kern_somaxkva(SYSCTLFN_ARGS)
1780: {
1781: int error, new_somaxkva;
1782: struct sysctlnode node;
1783: int s;
1784:
1785: new_somaxkva = somaxkva;
1786: node = *rnode;
1787: node.sysctl_data = &new_somaxkva;
1788: error = sysctl_lookup(SYSCTLFN_CALL(&node));
1789: if (error || newp == NULL)
1790: return (error);
1791:
1792: if (new_somaxkva < (16 * 1024 * 1024)) /* sanity */
1793: return (EINVAL);
1794:
1795: s = splvm();
1796: simple_lock(&so_pendfree_slock);
1797: somaxkva = new_somaxkva;
1798: wakeup(&socurkva);
1799: simple_unlock(&so_pendfree_slock);
1800: splx(s);
1801:
1802: return (error);
1803: }
1804:
1805: SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup")
1806: {
1807:
1.97 atatat 1808: sysctl_createv(clog, 0, NULL, NULL,
1809: CTLFLAG_PERMANENT,
1810: CTLTYPE_NODE, "kern", NULL,
1811: NULL, 0, NULL, 0,
1812: CTL_KERN, CTL_EOL);
1813:
1814: sysctl_createv(clog, 0, NULL, NULL,
1815: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.103 atatat 1816: CTLTYPE_INT, "somaxkva",
1817: SYSCTL_DESCR("Maximum amount of kernel memory to be "
1818: "used for socket buffers"),
1.94 yamt 1819: sysctl_kern_somaxkva, 0, NULL, 0,
1820: CTL_KERN, KERN_SOMAXKVA, CTL_EOL);
1821: }
CVSweb <webmaster@jp.NetBSD.org>