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