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