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