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