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