Annotation of src/sys/kern/uipc_socket.c, Revision 1.176
1.176 ! plunky 1: /* $NetBSD: uipc_socket.c,v 1.175 2008/10/11 16:39:07 tls Exp $ */
1.64 thorpej 2:
3: /*-
1.154 ad 4: * Copyright (c) 2002, 2007, 2008 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: *
19: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29: * POSSIBILITY OF SUCH DAMAGE.
30: */
1.16 cgd 31:
1.1 cgd 32: /*
1.159 ad 33: * Copyright (c) 2004 The FreeBSD Foundation
34: * Copyright (c) 2004 Robert Watson
1.15 mycroft 35: * Copyright (c) 1982, 1986, 1988, 1990, 1993
36: * The Regents of the University of California. All rights reserved.
1.1 cgd 37: *
38: * Redistribution and use in source and binary forms, with or without
39: * modification, are permitted provided that the following conditions
40: * are met:
41: * 1. Redistributions of source code must retain the above copyright
42: * notice, this list of conditions and the following disclaimer.
43: * 2. Redistributions in binary form must reproduce the above copyright
44: * notice, this list of conditions and the following disclaimer in the
45: * documentation and/or other materials provided with the distribution.
1.85 agc 46: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 47: * may be used to endorse or promote products derived from this software
48: * without specific prior written permission.
49: *
50: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60: * SUCH DAMAGE.
61: *
1.32 fvdl 62: * @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95
1.1 cgd 63: */
1.59 lukem 64:
65: #include <sys/cdefs.h>
1.176 ! plunky 66: __KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.175 2008/10/11 16:39:07 tls Exp $");
1.64 thorpej 67:
1.170 tls 68: #include "opt_inet.h"
1.64 thorpej 69: #include "opt_sock_counters.h"
70: #include "opt_sosend_loan.h"
1.81 martin 71: #include "opt_mbuftrace.h"
1.84 ragge 72: #include "opt_somaxkva.h"
1.167 ad 73: #include "opt_multiprocessor.h" /* XXX */
1.1 cgd 74:
1.9 mycroft 75: #include <sys/param.h>
76: #include <sys/systm.h>
77: #include <sys/proc.h>
78: #include <sys/file.h>
1.142 dyoung 79: #include <sys/filedesc.h>
1.173 plunky 80: #include <sys/kmem.h>
1.9 mycroft 81: #include <sys/mbuf.h>
82: #include <sys/domain.h>
83: #include <sys/kernel.h>
84: #include <sys/protosw.h>
85: #include <sys/socket.h>
86: #include <sys/socketvar.h>
1.21 christos 87: #include <sys/signalvar.h>
1.9 mycroft 88: #include <sys/resourcevar.h>
1.174 pooka 89: #include <sys/uidinfo.h>
1.72 jdolecek 90: #include <sys/event.h>
1.89 christos 91: #include <sys/poll.h>
1.118 elad 92: #include <sys/kauth.h>
1.136 ad 93: #include <sys/mutex.h>
94: #include <sys/condvar.h>
1.37 thorpej 95:
1.64 thorpej 96: #include <uvm/uvm.h>
97:
1.77 thorpej 98: MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options");
99: MALLOC_DEFINE(M_SONAME, "soname", "socket name");
1.37 thorpej 100:
1.142 dyoung 101: extern const struct fileops socketops;
102:
1.54 lukem 103: extern int somaxconn; /* patchable (XXX sysctl) */
104: int somaxconn = SOMAXCONN;
1.160 ad 105: kmutex_t *softnet_lock;
1.49 jonathan 106:
1.64 thorpej 107: #ifdef SOSEND_COUNTERS
108: #include <sys/device.h>
109:
1.113 thorpej 110: static struct evcnt sosend_loan_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
1.64 thorpej 111: NULL, "sosend", "loan big");
1.113 thorpej 112: static struct evcnt sosend_copy_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
1.64 thorpej 113: NULL, "sosend", "copy big");
1.113 thorpej 114: static struct evcnt sosend_copy_small = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
1.64 thorpej 115: NULL, "sosend", "copy small");
1.113 thorpej 116: static struct evcnt sosend_kvalimit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
1.64 thorpej 117: NULL, "sosend", "kva limit");
118:
119: #define SOSEND_COUNTER_INCR(ev) (ev)->ev_count++
120:
1.101 matt 121: EVCNT_ATTACH_STATIC(sosend_loan_big);
122: EVCNT_ATTACH_STATIC(sosend_copy_big);
123: EVCNT_ATTACH_STATIC(sosend_copy_small);
124: EVCNT_ATTACH_STATIC(sosend_kvalimit);
1.64 thorpej 125: #else
126:
127: #define SOSEND_COUNTER_INCR(ev) /* nothing */
128:
129: #endif /* SOSEND_COUNTERS */
130:
1.119 yamt 131: static struct callback_entry sokva_reclaimerentry;
1.1 cgd 132:
1.167 ad 133: #if defined(SOSEND_NO_LOAN) || defined(MULTIPROCESSOR)
1.121 yamt 134: int sock_loan_thresh = -1;
1.71 thorpej 135: #else
1.121 yamt 136: int sock_loan_thresh = 4096;
1.65 thorpej 137: #endif
1.64 thorpej 138:
1.136 ad 139: static kmutex_t so_pendfree_lock;
1.113 thorpej 140: static struct mbuf *so_pendfree;
1.64 thorpej 141:
1.84 ragge 142: #ifndef SOMAXKVA
143: #define SOMAXKVA (16 * 1024 * 1024)
144: #endif
145: int somaxkva = SOMAXKVA;
1.113 thorpej 146: static int socurkva;
1.136 ad 147: static kcondvar_t socurkva_cv;
1.64 thorpej 148:
149: #define SOCK_LOAN_CHUNK 65536
150:
1.117 yamt 151: static size_t sodopendfree(void);
152: static size_t sodopendfreel(void);
1.93 yamt 153:
1.113 thorpej 154: static vsize_t
1.129 yamt 155: sokvareserve(struct socket *so, vsize_t len)
1.80 yamt 156: {
1.98 christos 157: int error;
1.80 yamt 158:
1.136 ad 159: mutex_enter(&so_pendfree_lock);
1.80 yamt 160: while (socurkva + len > somaxkva) {
1.93 yamt 161: size_t freed;
162:
163: /*
164: * try to do pendfree.
165: */
166:
1.117 yamt 167: freed = sodopendfreel();
1.93 yamt 168:
169: /*
170: * if some kva was freed, try again.
171: */
172:
173: if (freed)
1.80 yamt 174: continue;
1.93 yamt 175:
1.80 yamt 176: SOSEND_COUNTER_INCR(&sosend_kvalimit);
1.136 ad 177: error = cv_wait_sig(&socurkva_cv, &so_pendfree_lock);
1.98 christos 178: if (error) {
179: len = 0;
180: break;
181: }
1.80 yamt 182: }
1.93 yamt 183: socurkva += len;
1.136 ad 184: mutex_exit(&so_pendfree_lock);
1.98 christos 185: return len;
1.95 yamt 186: }
187:
1.113 thorpej 188: static void
1.95 yamt 189: sokvaunreserve(vsize_t len)
190: {
191:
1.136 ad 192: mutex_enter(&so_pendfree_lock);
1.95 yamt 193: socurkva -= len;
1.136 ad 194: cv_broadcast(&socurkva_cv);
195: mutex_exit(&so_pendfree_lock);
1.95 yamt 196: }
197:
198: /*
199: * sokvaalloc: allocate kva for loan.
200: */
201:
202: vaddr_t
203: sokvaalloc(vsize_t len, struct socket *so)
204: {
205: vaddr_t lva;
206:
207: /*
208: * reserve kva.
209: */
210:
1.98 christos 211: if (sokvareserve(so, len) == 0)
212: return 0;
1.93 yamt 213:
214: /*
215: * allocate kva.
216: */
1.80 yamt 217:
1.109 yamt 218: lva = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
1.95 yamt 219: if (lva == 0) {
220: sokvaunreserve(len);
1.80 yamt 221: return (0);
1.95 yamt 222: }
1.80 yamt 223:
224: return lva;
225: }
226:
1.93 yamt 227: /*
228: * sokvafree: free kva for loan.
229: */
230:
1.80 yamt 231: void
232: sokvafree(vaddr_t sva, vsize_t len)
233: {
1.93 yamt 234:
235: /*
236: * free kva.
237: */
1.80 yamt 238:
1.109 yamt 239: uvm_km_free(kernel_map, sva, len, UVM_KMF_VAONLY);
1.93 yamt 240:
241: /*
242: * unreserve kva.
243: */
244:
1.95 yamt 245: sokvaunreserve(len);
1.80 yamt 246: }
247:
1.64 thorpej 248: static void
1.134 christos 249: sodoloanfree(struct vm_page **pgs, void *buf, size_t size)
1.64 thorpej 250: {
1.156 yamt 251: vaddr_t sva, eva;
1.64 thorpej 252: vsize_t len;
1.156 yamt 253: int npgs;
254:
255: KASSERT(pgs != NULL);
1.64 thorpej 256:
257: eva = round_page((vaddr_t) buf + size);
258: sva = trunc_page((vaddr_t) buf);
259: len = eva - sva;
260: npgs = len >> PAGE_SHIFT;
261:
262: pmap_kremove(sva, len);
263: pmap_update(pmap_kernel());
264: uvm_unloan(pgs, npgs, UVM_LOAN_TOPAGE);
1.80 yamt 265: sokvafree(sva, len);
1.64 thorpej 266: }
267:
268: static size_t
1.152 matt 269: sodopendfree(void)
1.64 thorpej 270: {
1.93 yamt 271: size_t rv;
1.64 thorpej 272:
1.160 ad 273: if (__predict_true(so_pendfree == NULL))
274: return 0;
275:
1.136 ad 276: mutex_enter(&so_pendfree_lock);
1.117 yamt 277: rv = sodopendfreel();
1.136 ad 278: mutex_exit(&so_pendfree_lock);
1.93 yamt 279:
280: return rv;
281: }
282:
283: /*
284: * sodopendfreel: free mbufs on "pendfree" list.
1.136 ad 285: * unlock and relock so_pendfree_lock when freeing mbufs.
1.93 yamt 286: *
1.136 ad 287: * => called with so_pendfree_lock held.
1.93 yamt 288: */
289:
290: static size_t
1.152 matt 291: sodopendfreel(void)
1.93 yamt 292: {
1.137 ad 293: struct mbuf *m, *next;
1.93 yamt 294: size_t rv = 0;
295:
1.136 ad 296: KASSERT(mutex_owned(&so_pendfree_lock));
1.64 thorpej 297:
1.137 ad 298: while (so_pendfree != NULL) {
1.64 thorpej 299: m = so_pendfree;
1.93 yamt 300: so_pendfree = NULL;
1.136 ad 301: mutex_exit(&so_pendfree_lock);
1.93 yamt 302:
303: for (; m != NULL; m = next) {
304: next = m->m_next;
1.156 yamt 305: KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0);
306: KASSERT(m->m_ext.ext_refcnt == 0);
1.93 yamt 307:
308: rv += m->m_ext.ext_size;
1.156 yamt 309: sodoloanfree(m->m_ext.ext_pgs, m->m_ext.ext_buf,
1.93 yamt 310: m->m_ext.ext_size);
1.145 ad 311: pool_cache_put(mb_cache, m);
1.93 yamt 312: }
1.64 thorpej 313:
1.136 ad 314: mutex_enter(&so_pendfree_lock);
1.64 thorpej 315: }
316:
317: return (rv);
318: }
319:
1.80 yamt 320: void
1.134 christos 321: soloanfree(struct mbuf *m, void *buf, size_t size, void *arg)
1.64 thorpej 322: {
323:
1.156 yamt 324: KASSERT(m != NULL);
1.64 thorpej 325:
1.93 yamt 326: /*
327: * postpone freeing mbuf.
328: *
329: * we can't do it in interrupt context
330: * because we need to put kva back to kernel_map.
331: */
332:
1.136 ad 333: mutex_enter(&so_pendfree_lock);
1.92 yamt 334: m->m_next = so_pendfree;
335: so_pendfree = m;
1.136 ad 336: cv_broadcast(&socurkva_cv);
337: mutex_exit(&so_pendfree_lock);
1.64 thorpej 338: }
339:
340: static long
341: sosend_loan(struct socket *so, struct uio *uio, struct mbuf *m, long space)
342: {
343: struct iovec *iov = uio->uio_iov;
344: vaddr_t sva, eva;
345: vsize_t len;
1.156 yamt 346: vaddr_t lva;
347: int npgs, error;
348: vaddr_t va;
349: int i;
1.64 thorpej 350:
1.116 yamt 351: if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace))
1.64 thorpej 352: return (0);
353:
354: if (iov->iov_len < (size_t) space)
355: space = iov->iov_len;
356: if (space > SOCK_LOAN_CHUNK)
357: space = SOCK_LOAN_CHUNK;
358:
359: eva = round_page((vaddr_t) iov->iov_base + space);
360: sva = trunc_page((vaddr_t) iov->iov_base);
361: len = eva - sva;
362: npgs = len >> PAGE_SHIFT;
363:
1.79 thorpej 364: KASSERT(npgs <= M_EXT_MAXPAGES);
365:
1.80 yamt 366: lva = sokvaalloc(len, so);
1.64 thorpej 367: if (lva == 0)
1.80 yamt 368: return 0;
1.64 thorpej 369:
1.116 yamt 370: error = uvm_loan(&uio->uio_vmspace->vm_map, sva, len,
1.79 thorpej 371: m->m_ext.ext_pgs, UVM_LOAN_TOPAGE);
1.64 thorpej 372: if (error) {
1.80 yamt 373: sokvafree(lva, len);
1.64 thorpej 374: return (0);
375: }
376:
377: for (i = 0, va = lva; i < npgs; i++, va += PAGE_SIZE)
1.79 thorpej 378: pmap_kenter_pa(va, VM_PAGE_TO_PHYS(m->m_ext.ext_pgs[i]),
379: VM_PROT_READ);
1.64 thorpej 380: pmap_update(pmap_kernel());
381:
382: lva += (vaddr_t) iov->iov_base & PAGE_MASK;
383:
1.134 christos 384: MEXTADD(m, (void *) lva, space, M_MBUF, soloanfree, so);
1.79 thorpej 385: m->m_flags |= M_EXT_PAGES | M_EXT_ROMAP;
1.64 thorpej 386:
387: uio->uio_resid -= space;
388: /* uio_offset not updated, not set/used for write(2) */
1.134 christos 389: uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + space;
1.64 thorpej 390: uio->uio_iov->iov_len -= space;
391: if (uio->uio_iov->iov_len == 0) {
392: uio->uio_iov++;
393: uio->uio_iovcnt--;
394: }
395:
396: return (space);
397: }
398:
1.119 yamt 399: static int
1.129 yamt 400: sokva_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)
1.119 yamt 401: {
402:
403: KASSERT(ce == &sokva_reclaimerentry);
404: KASSERT(obj == NULL);
405:
406: sodopendfree();
407: if (!vm_map_starved_p(kernel_map)) {
408: return CALLBACK_CHAIN_ABORT;
409: }
410: return CALLBACK_CHAIN_CONTINUE;
411: }
412:
1.142 dyoung 413: struct mbuf *
1.147 dyoung 414: getsombuf(struct socket *so, int type)
1.142 dyoung 415: {
416: struct mbuf *m;
417:
1.147 dyoung 418: m = m_get(M_WAIT, type);
1.142 dyoung 419: MCLAIM(m, so->so_mowner);
420: return m;
421: }
422:
1.119 yamt 423: void
424: soinit(void)
425: {
426:
1.148 ad 427: mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM);
1.160 ad 428: softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
1.136 ad 429: cv_init(&socurkva_cv, "sokva");
1.166 ad 430: soinit2();
1.136 ad 431:
1.119 yamt 432: /* Set the initial adjusted socket buffer size. */
433: if (sb_max_set(sb_max))
434: panic("bad initial sb_max value: %lu", sb_max);
435:
436: callback_register(&vm_map_to_kernel(kernel_map)->vmk_reclaim_callback,
437: &sokva_reclaimerentry, NULL, sokva_reclaim_callback);
438: }
439:
1.1 cgd 440: /*
441: * Socket operation routines.
442: * These routines are called by the routines in
443: * sys_socket.c or from a system process, and
444: * implement the semantics of socket operations by
445: * switching out to the protocol specific routines.
446: */
447: /*ARGSUSED*/
1.3 andrew 448: int
1.160 ad 449: socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l,
450: struct socket *lockso)
1.1 cgd 451: {
1.99 matt 452: const struct protosw *prp;
1.54 lukem 453: struct socket *so;
1.115 yamt 454: uid_t uid;
1.160 ad 455: int error;
456: kmutex_t *lock;
1.1 cgd 457:
1.132 elad 458: error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
459: KAUTH_REQ_NETWORK_SOCKET_OPEN, KAUTH_ARG(dom), KAUTH_ARG(type),
460: KAUTH_ARG(proto));
1.140 dyoung 461: if (error != 0)
462: return error;
1.127 elad 463:
1.1 cgd 464: if (proto)
465: prp = pffindproto(dom, proto, type);
466: else
467: prp = pffindtype(dom, type);
1.140 dyoung 468: if (prp == NULL) {
1.120 ginsbach 469: /* no support for domain */
470: if (pffinddomain(dom) == 0)
1.140 dyoung 471: return EAFNOSUPPORT;
1.120 ginsbach 472: /* no support for socket type */
473: if (proto == 0 && type != 0)
1.140 dyoung 474: return EPROTOTYPE;
475: return EPROTONOSUPPORT;
1.120 ginsbach 476: }
1.140 dyoung 477: if (prp->pr_usrreq == NULL)
478: return EPROTONOSUPPORT;
1.1 cgd 479: if (prp->pr_type != type)
1.140 dyoung 480: return EPROTOTYPE;
1.160 ad 481:
482: so = soget(true);
1.1 cgd 483: so->so_type = type;
484: so->so_proto = prp;
1.33 matt 485: so->so_send = sosend;
486: so->so_receive = soreceive;
1.78 matt 487: #ifdef MBUFTRACE
488: so->so_rcv.sb_mowner = &prp->pr_domain->dom_mowner;
489: so->so_snd.sb_mowner = &prp->pr_domain->dom_mowner;
490: so->so_mowner = &prp->pr_domain->dom_mowner;
491: #endif
1.138 rmind 492: uid = kauth_cred_geteuid(l->l_cred);
1.115 yamt 493: so->so_uidinfo = uid_find(uid);
1.168 yamt 494: so->so_egid = kauth_cred_getegid(l->l_cred);
495: so->so_cpid = l->l_proc->p_pid;
1.160 ad 496: if (lockso != NULL) {
497: /* Caller wants us to share a lock. */
498: lock = lockso->so_lock;
499: so->so_lock = lock;
500: mutex_obj_hold(lock);
501: mutex_enter(lock);
502: } else {
503: /* Lock assigned and taken during PRU_ATTACH. */
504: }
1.140 dyoung 505: error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL,
506: (struct mbuf *)(long)proto, NULL, l);
1.160 ad 507: KASSERT(solocked(so));
1.140 dyoung 508: if (error != 0) {
1.1 cgd 509: so->so_state |= SS_NOFDREF;
510: sofree(so);
1.140 dyoung 511: return error;
1.1 cgd 512: }
1.160 ad 513: sounlock(so);
1.1 cgd 514: *aso = so;
1.140 dyoung 515: return 0;
1.1 cgd 516: }
517:
1.142 dyoung 518: /* On success, write file descriptor to fdout and return zero. On
519: * failure, return non-zero; *fdout will be undefined.
520: */
521: int
522: fsocreate(int domain, struct socket **sop, int type, int protocol,
523: struct lwp *l, int *fdout)
524: {
525: struct socket *so;
526: struct file *fp;
527: int fd, error;
528:
1.155 ad 529: if ((error = fd_allocfile(&fp, &fd)) != 0)
1.142 dyoung 530: return (error);
531: fp->f_flag = FREAD|FWRITE;
532: fp->f_type = DTYPE_SOCKET;
533: fp->f_ops = &socketops;
1.160 ad 534: error = socreate(domain, &so, type, protocol, l, NULL);
1.142 dyoung 535: if (error != 0) {
1.155 ad 536: fd_abort(curproc, fp, fd);
1.142 dyoung 537: } else {
538: if (sop != NULL)
539: *sop = so;
540: fp->f_data = so;
1.155 ad 541: fd_affix(curproc, fp, fd);
1.142 dyoung 542: *fdout = fd;
543: }
544: return error;
545: }
546:
1.3 andrew 547: int
1.114 christos 548: sobind(struct socket *so, struct mbuf *nam, struct lwp *l)
1.1 cgd 549: {
1.160 ad 550: int error;
1.1 cgd 551:
1.160 ad 552: solock(so);
1.140 dyoung 553: error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, l);
1.160 ad 554: sounlock(so);
1.140 dyoung 555: return error;
1.1 cgd 556: }
557:
1.3 andrew 558: int
1.150 elad 559: solisten(struct socket *so, int backlog, struct lwp *l)
1.1 cgd 560: {
1.160 ad 561: int error;
1.1 cgd 562:
1.160 ad 563: solock(so);
1.158 ad 564: if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
1.163 ad 565: SS_ISDISCONNECTING)) != 0) {
566: sounlock(so);
1.158 ad 567: return (EOPNOTSUPP);
1.163 ad 568: }
1.140 dyoung 569: error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL,
1.150 elad 570: NULL, NULL, l);
1.140 dyoung 571: if (error != 0) {
1.160 ad 572: sounlock(so);
1.140 dyoung 573: return error;
1.1 cgd 574: }
1.63 matt 575: if (TAILQ_EMPTY(&so->so_q))
1.1 cgd 576: so->so_options |= SO_ACCEPTCONN;
577: if (backlog < 0)
578: backlog = 0;
1.49 jonathan 579: so->so_qlimit = min(backlog, somaxconn);
1.160 ad 580: sounlock(so);
1.140 dyoung 581: return 0;
1.1 cgd 582: }
583:
1.21 christos 584: void
1.54 lukem 585: sofree(struct socket *so)
1.1 cgd 586: {
1.161 ad 587: u_int refs;
1.1 cgd 588:
1.160 ad 589: KASSERT(solocked(so));
590:
591: if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) {
592: sounlock(so);
1.1 cgd 593: return;
1.160 ad 594: }
1.43 mycroft 595: if (so->so_head) {
596: /*
597: * We must not decommission a socket that's on the accept(2)
598: * queue. If we do, then accept(2) may hang after select(2)
599: * indicated that the listening socket was ready.
600: */
1.160 ad 601: if (!soqremque(so, 0)) {
602: sounlock(so);
1.43 mycroft 603: return;
1.160 ad 604: }
1.43 mycroft 605: }
1.98 christos 606: if (so->so_rcv.sb_hiwat)
1.110 christos 607: (void)chgsbsize(so->so_uidinfo, &so->so_rcv.sb_hiwat, 0,
1.98 christos 608: RLIM_INFINITY);
609: if (so->so_snd.sb_hiwat)
1.110 christos 610: (void)chgsbsize(so->so_uidinfo, &so->so_snd.sb_hiwat, 0,
1.98 christos 611: RLIM_INFINITY);
612: sbrelease(&so->so_snd, so);
1.160 ad 613: KASSERT(!cv_has_waiters(&so->so_cv));
614: KASSERT(!cv_has_waiters(&so->so_rcv.sb_cv));
615: KASSERT(!cv_has_waiters(&so->so_snd.sb_cv));
1.1 cgd 616: sorflush(so);
1.161 ad 617: refs = so->so_aborting; /* XXX */
1.170 tls 618: /* remove acccept filter if one is present. */
619: if (so->so_accf != NULL)
620: do_setopt_accept_filter(so, NULL);
1.160 ad 621: sounlock(so);
1.161 ad 622: if (refs == 0) /* XXX */
623: soput(so);
1.1 cgd 624: }
625:
626: /*
627: * Close a socket on last file table reference removal.
628: * Initiate disconnect if connected.
629: * Free socket when disconnect complete.
630: */
1.3 andrew 631: int
1.54 lukem 632: soclose(struct socket *so)
1.1 cgd 633: {
1.54 lukem 634: struct socket *so2;
1.160 ad 635: int error;
636: int error2;
1.1 cgd 637:
1.54 lukem 638: error = 0;
1.160 ad 639: solock(so);
1.1 cgd 640: if (so->so_options & SO_ACCEPTCONN) {
1.172 ad 641: for (;;) {
642: if ((so2 = TAILQ_FIRST(&so->so_q0)) != 0) {
1.160 ad 643: KASSERT(solocked2(so, so2));
644: (void) soqremque(so2, 0);
645: /* soabort drops the lock. */
646: (void) soabort(so2);
647: solock(so);
1.172 ad 648: continue;
1.160 ad 649: }
1.172 ad 650: if ((so2 = TAILQ_FIRST(&so->so_q)) != 0) {
1.160 ad 651: KASSERT(solocked2(so, so2));
652: (void) soqremque(so2, 1);
653: /* soabort drops the lock. */
654: (void) soabort(so2);
655: solock(so);
1.172 ad 656: continue;
1.160 ad 657: }
1.172 ad 658: break;
659: }
1.1 cgd 660: }
661: if (so->so_pcb == 0)
662: goto discard;
663: if (so->so_state & SS_ISCONNECTED) {
664: if ((so->so_state & SS_ISDISCONNECTING) == 0) {
665: error = sodisconnect(so);
666: if (error)
667: goto drop;
668: }
669: if (so->so_options & SO_LINGER) {
1.151 ad 670: if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio)
1.1 cgd 671: goto drop;
1.21 christos 672: while (so->so_state & SS_ISCONNECTED) {
1.160 ad 673: error = sowait(so, so->so_linger * hz);
1.21 christos 674: if (error)
1.1 cgd 675: break;
1.21 christos 676: }
1.1 cgd 677: }
678: }
1.54 lukem 679: drop:
1.1 cgd 680: if (so->so_pcb) {
1.160 ad 681: error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
1.140 dyoung 682: NULL, NULL, NULL, NULL);
1.1 cgd 683: if (error == 0)
684: error = error2;
685: }
1.54 lukem 686: discard:
1.1 cgd 687: if (so->so_state & SS_NOFDREF)
688: panic("soclose: NOFDREF");
689: so->so_state |= SS_NOFDREF;
690: sofree(so);
691: return (error);
692: }
693:
694: /*
1.160 ad 695: * Must be called with the socket locked.. Will return with it unlocked.
1.1 cgd 696: */
1.3 andrew 697: int
1.54 lukem 698: soabort(struct socket *so)
1.1 cgd 699: {
1.161 ad 700: u_int refs;
1.139 yamt 701: int error;
1.160 ad 702:
703: KASSERT(solocked(so));
704: KASSERT(so->so_head == NULL);
1.1 cgd 705:
1.161 ad 706: so->so_aborting++; /* XXX */
1.140 dyoung 707: error = (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL,
708: NULL, NULL, NULL);
1.161 ad 709: refs = --so->so_aborting; /* XXX */
1.164 drochner 710: if (error || (refs == 0)) {
1.139 yamt 711: sofree(so);
1.160 ad 712: } else {
713: sounlock(so);
1.139 yamt 714: }
715: return error;
1.1 cgd 716: }
717:
1.3 andrew 718: int
1.54 lukem 719: soaccept(struct socket *so, struct mbuf *nam)
1.1 cgd 720: {
1.160 ad 721: int error;
722:
723: KASSERT(solocked(so));
1.1 cgd 724:
1.54 lukem 725: error = 0;
1.1 cgd 726: if ((so->so_state & SS_NOFDREF) == 0)
727: panic("soaccept: !NOFDREF");
728: so->so_state &= ~SS_NOFDREF;
1.55 thorpej 729: if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
730: (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
1.41 mycroft 731: error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
1.140 dyoung 732: NULL, nam, NULL, NULL);
1.41 mycroft 733: else
1.53 itojun 734: error = ECONNABORTED;
1.52 itojun 735:
1.1 cgd 736: return (error);
737: }
738:
1.3 andrew 739: int
1.114 christos 740: soconnect(struct socket *so, struct mbuf *nam, struct lwp *l)
1.1 cgd 741: {
1.160 ad 742: int error;
743:
744: KASSERT(solocked(so));
1.1 cgd 745:
746: if (so->so_options & SO_ACCEPTCONN)
747: return (EOPNOTSUPP);
748: /*
749: * If protocol is connection-based, can only connect once.
750: * Otherwise, if connected, try to disconnect first.
751: * This allows user to disconnect by connecting to, e.g.,
752: * a null address.
753: */
754: if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
755: ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
756: (error = sodisconnect(so))))
757: error = EISCONN;
758: else
759: error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
1.140 dyoung 760: NULL, nam, NULL, l);
1.1 cgd 761: return (error);
762: }
763:
1.3 andrew 764: int
1.54 lukem 765: soconnect2(struct socket *so1, struct socket *so2)
1.1 cgd 766: {
1.160 ad 767: int error;
768:
769: KASSERT(solocked2(so1, so2));
1.1 cgd 770:
1.22 mycroft 771: error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
1.140 dyoung 772: NULL, (struct mbuf *)so2, NULL, NULL);
1.1 cgd 773: return (error);
774: }
775:
1.3 andrew 776: int
1.54 lukem 777: sodisconnect(struct socket *so)
1.1 cgd 778: {
1.160 ad 779: int error;
780:
781: KASSERT(solocked(so));
1.1 cgd 782:
783: if ((so->so_state & SS_ISCONNECTED) == 0) {
784: error = ENOTCONN;
1.160 ad 785: } else if (so->so_state & SS_ISDISCONNECTING) {
1.1 cgd 786: error = EALREADY;
1.160 ad 787: } else {
788: error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
789: NULL, NULL, NULL, NULL);
1.1 cgd 790: }
1.117 yamt 791: sodopendfree();
1.1 cgd 792: return (error);
793: }
794:
1.15 mycroft 795: #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
1.1 cgd 796: /*
797: * Send on a socket.
798: * If send must go all at once and message is larger than
799: * send buffering, then hard error.
800: * Lock against other senders.
801: * If must go all at once and not enough room now, then
802: * inform user that this would block and do nothing.
803: * Otherwise, if nonblocking, send as much as possible.
804: * The data to be sent is described by "uio" if nonzero,
805: * otherwise by the mbuf chain "top" (which must be null
806: * if uio is not). Data provided in mbuf chain must be small
807: * enough to send all at once.
808: *
809: * Returns nonzero on error, timeout or signal; callers
810: * must check for short counts if EINTR/ERESTART are returned.
811: * Data and control buffers are freed on return.
812: */
1.3 andrew 813: int
1.54 lukem 814: sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
1.114 christos 815: struct mbuf *control, int flags, struct lwp *l)
1.1 cgd 816: {
1.54 lukem 817: struct mbuf **mp, *m;
1.114 christos 818: struct proc *p;
1.58 jdolecek 819: long space, len, resid, clen, mlen;
820: int error, s, dontroute, atomic;
1.54 lukem 821:
1.114 christos 822: p = l->l_proc;
1.117 yamt 823: sodopendfree();
1.160 ad 824: clen = 0;
1.64 thorpej 825:
1.160 ad 826: /*
827: * solock() provides atomicity of access. splsoftnet() prevents
828: * protocol processing soft interrupts from interrupting us and
829: * blocking (expensive).
830: */
831: s = splsoftnet();
832: solock(so);
1.54 lukem 833: atomic = sosendallatonce(so) || top;
1.1 cgd 834: if (uio)
835: resid = uio->uio_resid;
836: else
837: resid = top->m_pkthdr.len;
1.7 cgd 838: /*
839: * In theory resid should be unsigned.
840: * However, space must be signed, as it might be less than 0
841: * if we over-committed, and we must use a signed comparison
842: * of space and resid. On the other hand, a negative resid
843: * causes us to loop sending 0-length segments to the protocol.
844: */
1.29 mycroft 845: if (resid < 0) {
846: error = EINVAL;
847: goto out;
848: }
1.1 cgd 849: dontroute =
850: (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
851: (so->so_proto->pr_flags & PR_ATOMIC);
1.165 christos 852: l->l_ru.ru_msgsnd++;
1.1 cgd 853: if (control)
854: clen = control->m_len;
1.54 lukem 855: restart:
1.21 christos 856: if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
1.1 cgd 857: goto out;
858: do {
1.160 ad 859: if (so->so_state & SS_CANTSENDMORE) {
860: error = EPIPE;
861: goto release;
862: }
1.48 thorpej 863: if (so->so_error) {
864: error = so->so_error;
865: so->so_error = 0;
866: goto release;
867: }
1.1 cgd 868: if ((so->so_state & SS_ISCONNECTED) == 0) {
869: if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
870: if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1.160 ad 871: !(resid == 0 && clen != 0)) {
872: error = ENOTCONN;
873: goto release;
874: }
875: } else if (addr == 0) {
876: error = EDESTADDRREQ;
877: goto release;
878: }
1.1 cgd 879: }
880: space = sbspace(&so->so_snd);
881: if (flags & MSG_OOB)
882: space += 1024;
1.21 christos 883: if ((atomic && resid > so->so_snd.sb_hiwat) ||
1.160 ad 884: clen > so->so_snd.sb_hiwat) {
885: error = EMSGSIZE;
886: goto release;
887: }
1.96 mycroft 888: if (space < resid + clen &&
1.1 cgd 889: (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1.160 ad 890: if (so->so_nbio) {
891: error = EWOULDBLOCK;
892: goto release;
893: }
1.1 cgd 894: sbunlock(&so->so_snd);
895: error = sbwait(&so->so_snd);
896: if (error)
897: goto out;
898: goto restart;
899: }
900: mp = ⊤
901: space -= clen;
902: do {
1.45 tv 903: if (uio == NULL) {
904: /*
905: * Data is prepackaged in "top".
906: */
907: resid = 0;
908: if (flags & MSG_EOR)
909: top->m_flags |= M_EOR;
910: } else do {
1.160 ad 911: sounlock(so);
912: splx(s);
1.144 dyoung 913: if (top == NULL) {
1.78 matt 914: m = m_gethdr(M_WAIT, MT_DATA);
1.45 tv 915: mlen = MHLEN;
916: m->m_pkthdr.len = 0;
1.140 dyoung 917: m->m_pkthdr.rcvif = NULL;
1.45 tv 918: } else {
1.78 matt 919: m = m_get(M_WAIT, MT_DATA);
1.45 tv 920: mlen = MLEN;
921: }
1.78 matt 922: MCLAIM(m, so->so_snd.sb_mowner);
1.121 yamt 923: if (sock_loan_thresh >= 0 &&
924: uio->uio_iov->iov_len >= sock_loan_thresh &&
925: space >= sock_loan_thresh &&
1.64 thorpej 926: (len = sosend_loan(so, uio, m,
927: space)) != 0) {
928: SOSEND_COUNTER_INCR(&sosend_loan_big);
929: space -= len;
930: goto have_data;
931: }
1.45 tv 932: if (resid >= MINCLSIZE && space >= MCLBYTES) {
1.64 thorpej 933: SOSEND_COUNTER_INCR(&sosend_copy_big);
1.78 matt 934: m_clget(m, M_WAIT);
1.45 tv 935: if ((m->m_flags & M_EXT) == 0)
936: goto nopages;
937: mlen = MCLBYTES;
938: if (atomic && top == 0) {
1.58 jdolecek 939: len = lmin(MCLBYTES - max_hdr,
1.54 lukem 940: resid);
1.45 tv 941: m->m_data += max_hdr;
942: } else
1.58 jdolecek 943: len = lmin(MCLBYTES, resid);
1.45 tv 944: space -= len;
945: } else {
1.64 thorpej 946: nopages:
947: SOSEND_COUNTER_INCR(&sosend_copy_small);
1.58 jdolecek 948: len = lmin(lmin(mlen, resid), space);
1.45 tv 949: space -= len;
950: /*
951: * For datagram protocols, leave room
952: * for protocol headers in first mbuf.
953: */
954: if (atomic && top == 0 && len < mlen)
955: MH_ALIGN(m, len);
956: }
1.144 dyoung 957: error = uiomove(mtod(m, void *), (int)len, uio);
1.64 thorpej 958: have_data:
1.45 tv 959: resid = uio->uio_resid;
960: m->m_len = len;
961: *mp = m;
962: top->m_pkthdr.len += len;
1.160 ad 963: s = splsoftnet();
964: solock(so);
1.144 dyoung 965: if (error != 0)
1.45 tv 966: goto release;
967: mp = &m->m_next;
968: if (resid <= 0) {
969: if (flags & MSG_EOR)
970: top->m_flags |= M_EOR;
971: break;
972: }
973: } while (space > 0 && atomic);
1.108 perry 974:
1.160 ad 975: if (so->so_state & SS_CANTSENDMORE) {
976: error = EPIPE;
977: goto release;
978: }
1.45 tv 979: if (dontroute)
980: so->so_options |= SO_DONTROUTE;
981: if (resid > 0)
982: so->so_state |= SS_MORETOCOME;
1.46 sommerfe 983: error = (*so->so_proto->pr_usrreq)(so,
984: (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
1.160 ad 985: top, addr, control, curlwp);
1.45 tv 986: if (dontroute)
987: so->so_options &= ~SO_DONTROUTE;
988: if (resid > 0)
989: so->so_state &= ~SS_MORETOCOME;
990: clen = 0;
1.144 dyoung 991: control = NULL;
992: top = NULL;
1.45 tv 993: mp = ⊤
1.144 dyoung 994: if (error != 0)
1.1 cgd 995: goto release;
996: } while (resid && space > 0);
997: } while (resid);
998:
1.54 lukem 999: release:
1.1 cgd 1000: sbunlock(&so->so_snd);
1.54 lukem 1001: out:
1.160 ad 1002: sounlock(so);
1003: splx(s);
1.1 cgd 1004: if (top)
1005: m_freem(top);
1006: if (control)
1007: m_freem(control);
1008: return (error);
1009: }
1010:
1011: /*
1.159 ad 1012: * Following replacement or removal of the first mbuf on the first
1013: * mbuf chain of a socket buffer, push necessary state changes back
1014: * into the socket buffer so that other consumers see the values
1015: * consistently. 'nextrecord' is the callers locally stored value of
1016: * the original value of sb->sb_mb->m_nextpkt which must be restored
1017: * when the lead mbuf changes. NOTE: 'nextrecord' may be NULL.
1018: */
1019: static void
1020: sbsync(struct sockbuf *sb, struct mbuf *nextrecord)
1021: {
1022:
1.160 ad 1023: KASSERT(solocked(sb->sb_so));
1024:
1.159 ad 1025: /*
1026: * First, update for the new value of nextrecord. If necessary,
1027: * make it the first record.
1028: */
1029: if (sb->sb_mb != NULL)
1030: sb->sb_mb->m_nextpkt = nextrecord;
1031: else
1032: sb->sb_mb = nextrecord;
1033:
1034: /*
1035: * Now update any dependent socket buffer fields to reflect
1036: * the new state. This is an inline of SB_EMPTY_FIXUP, with
1037: * the addition of a second clause that takes care of the
1038: * case where sb_mb has been updated, but remains the last
1039: * record.
1040: */
1041: if (sb->sb_mb == NULL) {
1042: sb->sb_mbtail = NULL;
1043: sb->sb_lastrecord = NULL;
1044: } else if (sb->sb_mb->m_nextpkt == NULL)
1045: sb->sb_lastrecord = sb->sb_mb;
1046: }
1047:
1048: /*
1.1 cgd 1049: * Implement receive operations on a socket.
1050: * We depend on the way that records are added to the sockbuf
1051: * by sbappend*. In particular, each record (mbufs linked through m_next)
1052: * must begin with an address if the protocol so specifies,
1053: * followed by an optional mbuf or mbufs containing ancillary data,
1054: * and then zero or more mbufs of data.
1055: * In order to avoid blocking network interrupts for the entire time here,
1056: * we splx() while doing the actual copy to user space.
1057: * Although the sockbuf is locked, new data may still be appended,
1058: * and thus we must maintain consistency of the sockbuf during that time.
1059: *
1060: * The caller may receive the data as a single mbuf chain by supplying
1061: * an mbuf **mp0 for use in returning the chain. The uio is then used
1062: * only for the count in uio_resid.
1063: */
1.3 andrew 1064: int
1.54 lukem 1065: soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
1066: struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1.1 cgd 1067: {
1.116 yamt 1068: struct lwp *l = curlwp;
1.160 ad 1069: struct mbuf *m, **mp, *mt;
1.146 dyoung 1070: int atomic, flags, len, error, s, offset, moff, type, orig_resid;
1.99 matt 1071: const struct protosw *pr;
1.54 lukem 1072: struct mbuf *nextrecord;
1.67 he 1073: int mbuf_removed = 0;
1.146 dyoung 1074: const struct domain *dom;
1.64 thorpej 1075:
1.54 lukem 1076: pr = so->so_proto;
1.146 dyoung 1077: atomic = pr->pr_flags & PR_ATOMIC;
1078: dom = pr->pr_domain;
1.1 cgd 1079: mp = mp0;
1.54 lukem 1080: type = 0;
1081: orig_resid = uio->uio_resid;
1.102 jonathan 1082:
1.144 dyoung 1083: if (paddr != NULL)
1084: *paddr = NULL;
1085: if (controlp != NULL)
1086: *controlp = NULL;
1087: if (flagsp != NULL)
1.1 cgd 1088: flags = *flagsp &~ MSG_EOR;
1089: else
1090: flags = 0;
1.66 enami 1091:
1092: if ((flags & MSG_DONTWAIT) == 0)
1.117 yamt 1093: sodopendfree();
1.66 enami 1094:
1.1 cgd 1095: if (flags & MSG_OOB) {
1096: m = m_get(M_WAIT, MT_DATA);
1.160 ad 1097: solock(so);
1.17 cgd 1098: error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
1.140 dyoung 1099: (struct mbuf *)(long)(flags & MSG_PEEK), NULL, l);
1.160 ad 1100: sounlock(so);
1.1 cgd 1101: if (error)
1102: goto bad;
1103: do {
1.134 christos 1104: error = uiomove(mtod(m, void *),
1.1 cgd 1105: (int) min(uio->uio_resid, m->m_len), uio);
1106: m = m_free(m);
1.144 dyoung 1107: } while (uio->uio_resid > 0 && error == 0 && m);
1.54 lukem 1108: bad:
1.144 dyoung 1109: if (m != NULL)
1.1 cgd 1110: m_freem(m);
1.144 dyoung 1111: return error;
1.1 cgd 1112: }
1.144 dyoung 1113: if (mp != NULL)
1.140 dyoung 1114: *mp = NULL;
1.160 ad 1115:
1116: /*
1117: * solock() provides atomicity of access. splsoftnet() prevents
1118: * protocol processing soft interrupts from interrupting us and
1119: * blocking (expensive).
1120: */
1121: s = splsoftnet();
1122: solock(so);
1.1 cgd 1123: if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
1.140 dyoung 1124: (*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l);
1.1 cgd 1125:
1.54 lukem 1126: restart:
1.160 ad 1127: if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) {
1128: sounlock(so);
1129: splx(s);
1.144 dyoung 1130: return error;
1.160 ad 1131: }
1.1 cgd 1132:
1133: m = so->so_rcv.sb_mb;
1134: /*
1135: * If we have less data than requested, block awaiting more
1136: * (subject to any timeout) if:
1.15 mycroft 1137: * 1. the current count is less than the low water mark,
1.1 cgd 1138: * 2. MSG_WAITALL is set, and it is possible to do the entire
1.15 mycroft 1139: * receive operation at once if we block (resid <= hiwat), or
1140: * 3. MSG_DONTWAIT is not set.
1.1 cgd 1141: * If MSG_WAITALL is set but resid is larger than the receive buffer,
1142: * we have to do the receive in sections, and thus risk returning
1143: * a short count if a timeout or signal occurs after we start.
1144: */
1.144 dyoung 1145: if (m == NULL ||
1146: ((flags & MSG_DONTWAIT) == 0 &&
1147: so->so_rcv.sb_cc < uio->uio_resid &&
1148: (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
1149: ((flags & MSG_WAITALL) &&
1150: uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1.146 dyoung 1151: m->m_nextpkt == NULL && !atomic)) {
1.1 cgd 1152: #ifdef DIAGNOSTIC
1.144 dyoung 1153: if (m == NULL && so->so_rcv.sb_cc)
1.1 cgd 1154: panic("receive 1");
1155: #endif
1156: if (so->so_error) {
1.144 dyoung 1157: if (m != NULL)
1.15 mycroft 1158: goto dontblock;
1.1 cgd 1159: error = so->so_error;
1160: if ((flags & MSG_PEEK) == 0)
1161: so->so_error = 0;
1162: goto release;
1163: }
1164: if (so->so_state & SS_CANTRCVMORE) {
1.144 dyoung 1165: if (m != NULL)
1.15 mycroft 1166: goto dontblock;
1.1 cgd 1167: else
1168: goto release;
1169: }
1.144 dyoung 1170: for (; m != NULL; m = m->m_next)
1.1 cgd 1171: if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1172: m = so->so_rcv.sb_mb;
1173: goto dontblock;
1174: }
1175: if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1176: (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1177: error = ENOTCONN;
1178: goto release;
1179: }
1180: if (uio->uio_resid == 0)
1181: goto release;
1.151 ad 1182: if (so->so_nbio || (flags & MSG_DONTWAIT)) {
1.1 cgd 1183: error = EWOULDBLOCK;
1184: goto release;
1185: }
1.69 thorpej 1186: SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1");
1187: SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1");
1.1 cgd 1188: sbunlock(&so->so_rcv);
1189: error = sbwait(&so->so_rcv);
1.160 ad 1190: if (error != 0) {
1191: sounlock(so);
1192: splx(s);
1.144 dyoung 1193: return error;
1.160 ad 1194: }
1.1 cgd 1195: goto restart;
1196: }
1.54 lukem 1197: dontblock:
1.69 thorpej 1198: /*
1199: * On entry here, m points to the first record of the socket buffer.
1.159 ad 1200: * From this point onward, we maintain 'nextrecord' as a cache of the
1201: * pointer to the next record in the socket buffer. We must keep the
1202: * various socket buffer pointers and local stack versions of the
1203: * pointers in sync, pushing out modifications before dropping the
1.160 ad 1204: * socket lock, and re-reading them when picking it up.
1.159 ad 1205: *
1206: * Otherwise, we will race with the network stack appending new data
1207: * or records onto the socket buffer by using inconsistent/stale
1208: * versions of the field, possibly resulting in socket buffer
1209: * corruption.
1210: *
1211: * By holding the high-level sblock(), we prevent simultaneous
1212: * readers from pulling off the front of the socket buffer.
1.69 thorpej 1213: */
1.144 dyoung 1214: if (l != NULL)
1.157 ad 1215: l->l_ru.ru_msgrcv++;
1.69 thorpej 1216: KASSERT(m == so->so_rcv.sb_mb);
1217: SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
1218: SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
1.1 cgd 1219: nextrecord = m->m_nextpkt;
1220: if (pr->pr_flags & PR_ADDR) {
1221: #ifdef DIAGNOSTIC
1222: if (m->m_type != MT_SONAME)
1223: panic("receive 1a");
1224: #endif
1.3 andrew 1225: orig_resid = 0;
1.1 cgd 1226: if (flags & MSG_PEEK) {
1227: if (paddr)
1228: *paddr = m_copy(m, 0, m->m_len);
1229: m = m->m_next;
1230: } else {
1231: sbfree(&so->so_rcv, m);
1.67 he 1232: mbuf_removed = 1;
1.144 dyoung 1233: if (paddr != NULL) {
1.1 cgd 1234: *paddr = m;
1235: so->so_rcv.sb_mb = m->m_next;
1.144 dyoung 1236: m->m_next = NULL;
1.1 cgd 1237: m = so->so_rcv.sb_mb;
1238: } else {
1239: MFREE(m, so->so_rcv.sb_mb);
1240: m = so->so_rcv.sb_mb;
1241: }
1.159 ad 1242: sbsync(&so->so_rcv, nextrecord);
1.1 cgd 1243: }
1244: }
1.159 ad 1245:
1246: /*
1247: * Process one or more MT_CONTROL mbufs present before any data mbufs
1248: * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1249: * just copy the data; if !MSG_PEEK, we call into the protocol to
1250: * perform externalization (or freeing if controlp == NULL).
1251: */
1252: if (__predict_false(m != NULL && m->m_type == MT_CONTROL)) {
1253: struct mbuf *cm = NULL, *cmn;
1254: struct mbuf **cme = &cm;
1255:
1256: do {
1257: if (flags & MSG_PEEK) {
1258: if (controlp != NULL) {
1259: *controlp = m_copy(m, 0, m->m_len);
1260: controlp = &(*controlp)->m_next;
1261: }
1262: m = m->m_next;
1263: } else {
1264: sbfree(&so->so_rcv, m);
1.1 cgd 1265: so->so_rcv.sb_mb = m->m_next;
1.144 dyoung 1266: m->m_next = NULL;
1.159 ad 1267: *cme = m;
1268: cme = &(*cme)->m_next;
1.1 cgd 1269: m = so->so_rcv.sb_mb;
1.159 ad 1270: }
1271: } while (m != NULL && m->m_type == MT_CONTROL);
1272: if ((flags & MSG_PEEK) == 0)
1273: sbsync(&so->so_rcv, nextrecord);
1274: for (; cm != NULL; cm = cmn) {
1275: cmn = cm->m_next;
1276: cm->m_next = NULL;
1277: type = mtod(cm, struct cmsghdr *)->cmsg_type;
1278: if (controlp != NULL) {
1279: if (dom->dom_externalize != NULL &&
1280: type == SCM_RIGHTS) {
1.160 ad 1281: sounlock(so);
1.159 ad 1282: splx(s);
1283: error = (*dom->dom_externalize)(cm, l);
1284: s = splsoftnet();
1.160 ad 1285: solock(so);
1.159 ad 1286: }
1287: *controlp = cm;
1288: while (*controlp != NULL)
1289: controlp = &(*controlp)->m_next;
1.1 cgd 1290: } else {
1.106 itojun 1291: /*
1292: * Dispose of any SCM_RIGHTS message that went
1293: * through the read path rather than recv.
1294: */
1.159 ad 1295: if (dom->dom_dispose != NULL &&
1296: type == SCM_RIGHTS) {
1.160 ad 1297: sounlock(so);
1.159 ad 1298: (*dom->dom_dispose)(cm);
1.160 ad 1299: solock(so);
1.159 ad 1300: }
1301: m_freem(cm);
1.1 cgd 1302: }
1303: }
1.159 ad 1304: if (m != NULL)
1305: nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1306: else
1307: nextrecord = so->so_rcv.sb_mb;
1308: orig_resid = 0;
1.1 cgd 1309: }
1.69 thorpej 1310:
1.159 ad 1311: /* If m is non-NULL, we have some data to read. */
1312: if (__predict_true(m != NULL)) {
1.1 cgd 1313: type = m->m_type;
1314: if (type == MT_OOBDATA)
1315: flags |= MSG_OOB;
1316: }
1.69 thorpej 1317: SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
1318: SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
1319:
1.1 cgd 1320: moff = 0;
1321: offset = 0;
1.144 dyoung 1322: while (m != NULL && uio->uio_resid > 0 && error == 0) {
1.1 cgd 1323: if (m->m_type == MT_OOBDATA) {
1324: if (type != MT_OOBDATA)
1325: break;
1326: } else if (type == MT_OOBDATA)
1327: break;
1328: #ifdef DIAGNOSTIC
1329: else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
1330: panic("receive 3");
1331: #endif
1332: so->so_state &= ~SS_RCVATMARK;
1333: len = uio->uio_resid;
1334: if (so->so_oobmark && len > so->so_oobmark - offset)
1335: len = so->so_oobmark - offset;
1336: if (len > m->m_len - moff)
1337: len = m->m_len - moff;
1338: /*
1339: * If mp is set, just pass back the mbufs.
1340: * Otherwise copy them out via the uio, then free.
1341: * Sockbuf must be consistent here (points to current mbuf,
1342: * it points to next record) when we drop priority;
1343: * we must note any additions to the sockbuf when we
1344: * block interrupts again.
1345: */
1.144 dyoung 1346: if (mp == NULL) {
1.69 thorpej 1347: SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove");
1348: SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove");
1.160 ad 1349: sounlock(so);
1.1 cgd 1350: splx(s);
1.134 christos 1351: error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1.20 mycroft 1352: s = splsoftnet();
1.160 ad 1353: solock(so);
1.144 dyoung 1354: if (error != 0) {
1.67 he 1355: /*
1356: * If any part of the record has been removed
1357: * (such as the MT_SONAME mbuf, which will
1358: * happen when PR_ADDR, and thus also
1359: * PR_ATOMIC, is set), then drop the entire
1360: * record to maintain the atomicity of the
1361: * receive operation.
1362: *
1363: * This avoids a later panic("receive 1a")
1364: * when compiled with DIAGNOSTIC.
1365: */
1.146 dyoung 1366: if (m && mbuf_removed && atomic)
1.67 he 1367: (void) sbdroprecord(&so->so_rcv);
1368:
1.57 jdolecek 1369: goto release;
1.67 he 1370: }
1.1 cgd 1371: } else
1372: uio->uio_resid -= len;
1373: if (len == m->m_len - moff) {
1374: if (m->m_flags & M_EOR)
1375: flags |= MSG_EOR;
1376: if (flags & MSG_PEEK) {
1377: m = m->m_next;
1378: moff = 0;
1379: } else {
1380: nextrecord = m->m_nextpkt;
1381: sbfree(&so->so_rcv, m);
1382: if (mp) {
1383: *mp = m;
1384: mp = &m->m_next;
1385: so->so_rcv.sb_mb = m = m->m_next;
1.140 dyoung 1386: *mp = NULL;
1.1 cgd 1387: } else {
1388: MFREE(m, so->so_rcv.sb_mb);
1389: m = so->so_rcv.sb_mb;
1390: }
1.69 thorpej 1391: /*
1392: * If m != NULL, we also know that
1393: * so->so_rcv.sb_mb != NULL.
1394: */
1395: KASSERT(so->so_rcv.sb_mb == m);
1396: if (m) {
1.1 cgd 1397: m->m_nextpkt = nextrecord;
1.69 thorpej 1398: if (nextrecord == NULL)
1399: so->so_rcv.sb_lastrecord = m;
1400: } else {
1401: so->so_rcv.sb_mb = nextrecord;
1.70 thorpej 1402: SB_EMPTY_FIXUP(&so->so_rcv);
1.69 thorpej 1403: }
1404: SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
1405: SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
1.1 cgd 1406: }
1.144 dyoung 1407: } else if (flags & MSG_PEEK)
1408: moff += len;
1409: else {
1.160 ad 1410: if (mp != NULL) {
1411: mt = m_copym(m, 0, len, M_NOWAIT);
1412: if (__predict_false(mt == NULL)) {
1413: sounlock(so);
1414: mt = m_copym(m, 0, len, M_WAIT);
1415: solock(so);
1416: }
1417: *mp = mt;
1418: }
1.144 dyoung 1419: m->m_data += len;
1420: m->m_len -= len;
1421: so->so_rcv.sb_cc -= len;
1.1 cgd 1422: }
1423: if (so->so_oobmark) {
1424: if ((flags & MSG_PEEK) == 0) {
1425: so->so_oobmark -= len;
1426: if (so->so_oobmark == 0) {
1427: so->so_state |= SS_RCVATMARK;
1428: break;
1429: }
1.7 cgd 1430: } else {
1.1 cgd 1431: offset += len;
1.7 cgd 1432: if (offset == so->so_oobmark)
1433: break;
1434: }
1.1 cgd 1435: }
1436: if (flags & MSG_EOR)
1437: break;
1438: /*
1439: * If the MSG_WAITALL flag is set (for non-atomic socket),
1440: * we must not quit until "uio->uio_resid == 0" or an error
1441: * termination. If a signal/timeout occurs, return
1442: * with a short count but without error.
1443: * Keep sockbuf locked against other readers.
1444: */
1.144 dyoung 1445: while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1.3 andrew 1446: !sosendallatonce(so) && !nextrecord) {
1.1 cgd 1447: if (so->so_error || so->so_state & SS_CANTRCVMORE)
1448: break;
1.68 matt 1449: /*
1450: * If we are peeking and the socket receive buffer is
1451: * full, stop since we can't get more data to peek at.
1452: */
1453: if ((flags & MSG_PEEK) && sbspace(&so->so_rcv) <= 0)
1454: break;
1455: /*
1456: * If we've drained the socket buffer, tell the
1457: * protocol in case it needs to do something to
1458: * get it filled again.
1459: */
1460: if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
1461: (*pr->pr_usrreq)(so, PRU_RCVD,
1.140 dyoung 1462: NULL, (struct mbuf *)(long)flags, NULL, l);
1.69 thorpej 1463: SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2");
1464: SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2");
1.1 cgd 1465: error = sbwait(&so->so_rcv);
1.144 dyoung 1466: if (error != 0) {
1.1 cgd 1467: sbunlock(&so->so_rcv);
1.160 ad 1468: sounlock(so);
1.1 cgd 1469: splx(s);
1.144 dyoung 1470: return 0;
1.1 cgd 1471: }
1.21 christos 1472: if ((m = so->so_rcv.sb_mb) != NULL)
1.1 cgd 1473: nextrecord = m->m_nextpkt;
1474: }
1475: }
1.3 andrew 1476:
1.146 dyoung 1477: if (m && atomic) {
1.3 andrew 1478: flags |= MSG_TRUNC;
1479: if ((flags & MSG_PEEK) == 0)
1480: (void) sbdroprecord(&so->so_rcv);
1481: }
1.1 cgd 1482: if ((flags & MSG_PEEK) == 0) {
1.144 dyoung 1483: if (m == NULL) {
1.69 thorpej 1484: /*
1.70 thorpej 1485: * First part is an inline SB_EMPTY_FIXUP(). Second
1.69 thorpej 1486: * part makes sure sb_lastrecord is up-to-date if
1487: * there is still data in the socket buffer.
1488: */
1.1 cgd 1489: so->so_rcv.sb_mb = nextrecord;
1.69 thorpej 1490: if (so->so_rcv.sb_mb == NULL) {
1491: so->so_rcv.sb_mbtail = NULL;
1492: so->so_rcv.sb_lastrecord = NULL;
1493: } else if (nextrecord->m_nextpkt == NULL)
1494: so->so_rcv.sb_lastrecord = nextrecord;
1495: }
1496: SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
1497: SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
1.1 cgd 1498: if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1.140 dyoung 1499: (*pr->pr_usrreq)(so, PRU_RCVD, NULL,
1500: (struct mbuf *)(long)flags, NULL, l);
1.1 cgd 1501: }
1.3 andrew 1502: if (orig_resid == uio->uio_resid && orig_resid &&
1503: (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
1504: sbunlock(&so->so_rcv);
1505: goto restart;
1506: }
1.108 perry 1507:
1.144 dyoung 1508: if (flagsp != NULL)
1.1 cgd 1509: *flagsp |= flags;
1.54 lukem 1510: release:
1.1 cgd 1511: sbunlock(&so->so_rcv);
1.160 ad 1512: sounlock(so);
1.1 cgd 1513: splx(s);
1.144 dyoung 1514: return error;
1.1 cgd 1515: }
1516:
1.14 mycroft 1517: int
1.54 lukem 1518: soshutdown(struct socket *so, int how)
1.1 cgd 1519: {
1.99 matt 1520: const struct protosw *pr;
1.160 ad 1521: int error;
1522:
1523: KASSERT(solocked(so));
1.34 kleink 1524:
1.54 lukem 1525: pr = so->so_proto;
1.34 kleink 1526: if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1527: return (EINVAL);
1.1 cgd 1528:
1.160 ad 1529: if (how == SHUT_RD || how == SHUT_RDWR) {
1.1 cgd 1530: sorflush(so);
1.160 ad 1531: error = 0;
1532: }
1.34 kleink 1533: if (how == SHUT_WR || how == SHUT_RDWR)
1.160 ad 1534: error = (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL,
1.140 dyoung 1535: NULL, NULL, NULL);
1.160 ad 1536:
1537: return error;
1.1 cgd 1538: }
1539:
1.14 mycroft 1540: void
1.54 lukem 1541: sorflush(struct socket *so)
1.1 cgd 1542: {
1.54 lukem 1543: struct sockbuf *sb, asb;
1.99 matt 1544: const struct protosw *pr;
1.160 ad 1545:
1546: KASSERT(solocked(so));
1.1 cgd 1547:
1.54 lukem 1548: sb = &so->so_rcv;
1549: pr = so->so_proto;
1.160 ad 1550: socantrcvmore(so);
1.1 cgd 1551: sb->sb_flags |= SB_NOINTR;
1.160 ad 1552: (void )sblock(sb, M_WAITOK);
1.1 cgd 1553: sbunlock(sb);
1554: asb = *sb;
1.86 wrstuden 1555: /*
1556: * Clear most of the sockbuf structure, but leave some of the
1557: * fields valid.
1558: */
1559: memset(&sb->sb_startzero, 0,
1560: sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
1.160 ad 1561: if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) {
1562: sounlock(so);
1.1 cgd 1563: (*pr->pr_domain->dom_dispose)(asb.sb_mb);
1.160 ad 1564: solock(so);
1565: }
1.98 christos 1566: sbrelease(&asb, so);
1.1 cgd 1567: }
1568:
1.171 plunky 1569: /*
1570: * internal set SOL_SOCKET options
1571: */
1.142 dyoung 1572: static int
1.171 plunky 1573: sosetopt1(struct socket *so, const struct sockopt *sopt)
1.1 cgd 1574: {
1.171 plunky 1575: int error, optval;
1576: struct linger l;
1577: struct timeval tv;
1.142 dyoung 1578:
1.171 plunky 1579: switch (sopt->sopt_name) {
1.142 dyoung 1580:
1.170 tls 1581: case SO_ACCEPTFILTER:
1.171 plunky 1582: error = do_setopt_accept_filter(so, sopt);
1.170 tls 1583: if (error)
1584: return error;
1585: break;
1586:
1.171 plunky 1587: case SO_LINGER:
1588: error = sockopt_get(sopt, &l, sizeof(l));
1589: if (error)
1590: return (error);
1591:
1592: if (l.l_linger < 0 || l.l_linger > USHRT_MAX ||
1593: l.l_linger > (INT_MAX / hz))
1.142 dyoung 1594: return EDOM;
1.171 plunky 1595: so->so_linger = l.l_linger;
1596: if (l.l_onoff)
1597: so->so_options |= SO_LINGER;
1598: else
1599: so->so_options &= ~SO_LINGER;
1600:
1601: break;
1.1 cgd 1602:
1.142 dyoung 1603: case SO_DEBUG:
1604: case SO_KEEPALIVE:
1605: case SO_DONTROUTE:
1606: case SO_USELOOPBACK:
1607: case SO_BROADCAST:
1608: case SO_REUSEADDR:
1609: case SO_REUSEPORT:
1610: case SO_OOBINLINE:
1611: case SO_TIMESTAMP:
1.171 plunky 1612: error = sockopt_getint(sopt, &optval);
1613: if (error)
1614: return (error);
1615:
1616: if (optval)
1617: so->so_options |= sopt->sopt_name;
1.142 dyoung 1618: else
1.171 plunky 1619: so->so_options &= ~sopt->sopt_name;
1.142 dyoung 1620: break;
1621:
1622: case SO_SNDBUF:
1623: case SO_RCVBUF:
1624: case SO_SNDLOWAT:
1625: case SO_RCVLOWAT:
1.171 plunky 1626: error = sockopt_getint(sopt, &optval);
1627: if (error)
1628: return (error);
1.1 cgd 1629:
1.142 dyoung 1630: /*
1631: * Values < 1 make no sense for any of these
1632: * options, so disallow them.
1633: */
1634: if (optval < 1)
1635: return EINVAL;
1.1 cgd 1636:
1.171 plunky 1637: switch (sopt->sopt_name) {
1638: case SO_SNDBUF:
1639: if (sbreserve(&so->so_snd, (u_long)optval, so) == 0)
1640: return ENOBUFS;
1641:
1642: so->so_snd.sb_flags &= ~SB_AUTOSIZE;
1643: break;
1.1 cgd 1644:
1645: case SO_RCVBUF:
1.171 plunky 1646: if (sbreserve(&so->so_rcv, (u_long)optval, so) == 0)
1.142 dyoung 1647: return ENOBUFS;
1.171 plunky 1648:
1649: so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1.142 dyoung 1650: break;
1651:
1652: /*
1653: * Make sure the low-water is never greater than
1654: * the high-water.
1655: */
1.1 cgd 1656: case SO_SNDLOWAT:
1.171 plunky 1657: if (optval > so->so_snd.sb_hiwat)
1658: optval = so->so_snd.sb_hiwat;
1659:
1660: so->so_snd.sb_lowat = optval;
1.142 dyoung 1661: break;
1.171 plunky 1662:
1.1 cgd 1663: case SO_RCVLOWAT:
1.171 plunky 1664: if (optval > so->so_rcv.sb_hiwat)
1665: optval = so->so_rcv.sb_hiwat;
1666:
1667: so->so_rcv.sb_lowat = optval;
1.142 dyoung 1668: break;
1669: }
1670: break;
1.28 thorpej 1671:
1.142 dyoung 1672: case SO_SNDTIMEO:
1673: case SO_RCVTIMEO:
1.171 plunky 1674: error = sockopt_get(sopt, &tv, sizeof(tv));
1675: if (error)
1676: return (error);
1677:
1678: if (tv.tv_sec > (INT_MAX - tv.tv_usec / tick) / hz)
1.142 dyoung 1679: return EDOM;
1.28 thorpej 1680:
1.171 plunky 1681: optval = tv.tv_sec * hz + tv.tv_usec / tick;
1682: if (optval == 0 && tv.tv_usec != 0)
1683: optval = 1;
1.28 thorpej 1684:
1.171 plunky 1685: switch (sopt->sopt_name) {
1.142 dyoung 1686: case SO_SNDTIMEO:
1.171 plunky 1687: so->so_snd.sb_timeo = optval;
1.1 cgd 1688: break;
1689: case SO_RCVTIMEO:
1.171 plunky 1690: so->so_rcv.sb_timeo = optval;
1.142 dyoung 1691: break;
1692: }
1693: break;
1.1 cgd 1694:
1.142 dyoung 1695: default:
1696: return ENOPROTOOPT;
1697: }
1698: return 0;
1699: }
1.1 cgd 1700:
1.142 dyoung 1701: int
1.171 plunky 1702: sosetopt(struct socket *so, struct sockopt *sopt)
1.142 dyoung 1703: {
1704: int error, prerr;
1.1 cgd 1705:
1.160 ad 1706: solock(so);
1.171 plunky 1707: if (sopt->sopt_level == SOL_SOCKET)
1708: error = sosetopt1(so, sopt);
1.142 dyoung 1709: else
1710: error = ENOPROTOOPT;
1.1 cgd 1711:
1.142 dyoung 1712: if ((error == 0 || error == ENOPROTOOPT) &&
1713: so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) {
1714: /* give the protocol stack a shot */
1.171 plunky 1715: prerr = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, sopt);
1.142 dyoung 1716: if (prerr == 0)
1717: error = 0;
1718: else if (prerr != ENOPROTOOPT)
1719: error = prerr;
1.171 plunky 1720: }
1.160 ad 1721: sounlock(so);
1.142 dyoung 1722: return error;
1.1 cgd 1723: }
1724:
1.171 plunky 1725: /*
1726: * so_setsockopt() is a wrapper providing a sockopt structure for sosetopt()
1727: */
1728: int
1729: so_setsockopt(struct lwp *l, struct socket *so, int level, int name,
1730: const void *val, size_t valsize)
1731: {
1732: struct sockopt sopt;
1733: int error;
1734:
1735: KASSERT(valsize == 0 || val != NULL);
1736:
1737: sockopt_init(&sopt, level, name, valsize);
1738: sockopt_set(&sopt, val, valsize);
1739:
1740: error = sosetopt(so, &sopt);
1741:
1742: sockopt_destroy(&sopt);
1743:
1744: return error;
1745: }
1746:
1747: /*
1748: * internal get SOL_SOCKET options
1749: */
1750: static int
1751: sogetopt1(struct socket *so, struct sockopt *sopt)
1752: {
1753: int error, optval;
1754: struct linger l;
1755: struct timeval tv;
1756:
1757: switch (sopt->sopt_name) {
1758:
1759: case SO_ACCEPTFILTER:
1760: error = do_getopt_accept_filter(so, sopt);
1761: break;
1762:
1763: case SO_LINGER:
1764: l.l_onoff = (so->so_options & SO_LINGER) ? 1 : 0;
1765: l.l_linger = so->so_linger;
1766:
1767: error = sockopt_set(sopt, &l, sizeof(l));
1768: break;
1769:
1770: case SO_USELOOPBACK:
1771: case SO_DONTROUTE:
1772: case SO_DEBUG:
1773: case SO_KEEPALIVE:
1774: case SO_REUSEADDR:
1775: case SO_REUSEPORT:
1776: case SO_BROADCAST:
1777: case SO_OOBINLINE:
1778: case SO_TIMESTAMP:
1779: error = sockopt_setint(sopt,
1780: (so->so_options & sopt->sopt_name) ? 1 : 0);
1781: break;
1782:
1783: case SO_TYPE:
1784: error = sockopt_setint(sopt, so->so_type);
1785: break;
1786:
1787: case SO_ERROR:
1788: error = sockopt_setint(sopt, so->so_error);
1789: so->so_error = 0;
1790: break;
1791:
1792: case SO_SNDBUF:
1793: error = sockopt_setint(sopt, so->so_snd.sb_hiwat);
1794: break;
1795:
1796: case SO_RCVBUF:
1797: error = sockopt_setint(sopt, so->so_rcv.sb_hiwat);
1798: break;
1799:
1800: case SO_SNDLOWAT:
1801: error = sockopt_setint(sopt, so->so_snd.sb_lowat);
1802: break;
1803:
1804: case SO_RCVLOWAT:
1805: error = sockopt_setint(sopt, so->so_rcv.sb_lowat);
1806: break;
1807:
1808: case SO_SNDTIMEO:
1809: case SO_RCVTIMEO:
1810: optval = (sopt->sopt_name == SO_SNDTIMEO ?
1811: so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1812:
1813: tv.tv_sec = optval / hz;
1814: tv.tv_usec = (optval % hz) * tick;
1815:
1816: error = sockopt_set(sopt, &tv, sizeof(tv));
1817: break;
1818:
1819: case SO_OVERFLOWED:
1820: error = sockopt_setint(sopt, so->so_rcv.sb_overflowed);
1821: break;
1822:
1823: default:
1824: error = ENOPROTOOPT;
1825: break;
1826: }
1827:
1828: return (error);
1829: }
1830:
1.14 mycroft 1831: int
1.171 plunky 1832: sogetopt(struct socket *so, struct sockopt *sopt)
1.1 cgd 1833: {
1.160 ad 1834: int error;
1.1 cgd 1835:
1.160 ad 1836: solock(so);
1.171 plunky 1837: if (sopt->sopt_level != SOL_SOCKET) {
1.1 cgd 1838: if (so->so_proto && so->so_proto->pr_ctloutput) {
1.160 ad 1839: error = ((*so->so_proto->pr_ctloutput)
1.171 plunky 1840: (PRCO_GETOPT, so, sopt));
1.1 cgd 1841: } else
1.160 ad 1842: error = (ENOPROTOOPT);
1.1 cgd 1843: } else {
1.171 plunky 1844: error = sogetopt1(so, sopt);
1845: }
1846: sounlock(so);
1847: return (error);
1848: }
1849:
1850: /*
1851: * alloc sockopt data buffer buffer
1852: * - will be released at destroy
1853: */
1.176 ! plunky 1854: static int
! 1855: sockopt_alloc(struct sockopt *sopt, size_t len, km_flag_t kmflag)
1.171 plunky 1856: {
1857:
1858: KASSERT(sopt->sopt_size == 0);
1859:
1.176 ! plunky 1860: if (len > sizeof(sopt->sopt_buf)) {
! 1861: sopt->sopt_data = kmem_zalloc(len, kmflag);
! 1862: if (sopt->sopt_data == NULL)
! 1863: return ENOMEM;
! 1864: } else
1.171 plunky 1865: sopt->sopt_data = sopt->sopt_buf;
1866:
1867: sopt->sopt_size = len;
1.176 ! plunky 1868: return 0;
1.171 plunky 1869: }
1870:
1871: /*
1872: * initialise sockopt storage
1.176 ! plunky 1873: * - MAY sleep during allocation
1.171 plunky 1874: */
1875: void
1876: sockopt_init(struct sockopt *sopt, int level, int name, size_t size)
1877: {
1.1 cgd 1878:
1.171 plunky 1879: memset(sopt, 0, sizeof(*sopt));
1.1 cgd 1880:
1.171 plunky 1881: sopt->sopt_level = level;
1882: sopt->sopt_name = name;
1.176 ! plunky 1883: (void)sockopt_alloc(sopt, size, KM_SLEEP);
1.171 plunky 1884: }
1885:
1886: /*
1887: * destroy sockopt storage
1888: * - will release any held memory references
1889: */
1890: void
1891: sockopt_destroy(struct sockopt *sopt)
1892: {
1893:
1894: if (sopt->sopt_data != sopt->sopt_buf)
1.173 plunky 1895: kmem_free(sopt->sopt_data, sopt->sopt_size);
1.171 plunky 1896:
1897: memset(sopt, 0, sizeof(*sopt));
1898: }
1899:
1900: /*
1901: * set sockopt value
1902: * - value is copied into sockopt
1.176 ! plunky 1903: * - memory is allocated when necessary, will not sleep
1.171 plunky 1904: */
1905: int
1906: sockopt_set(struct sockopt *sopt, const void *buf, size_t len)
1907: {
1.176 ! plunky 1908: int error;
1.171 plunky 1909:
1.176 ! plunky 1910: if (sopt->sopt_size == 0) {
! 1911: error = sockopt_alloc(sopt, len, KM_NOSLEEP);
! 1912: if (error)
! 1913: return error;
! 1914: }
1.171 plunky 1915:
1916: KASSERT(sopt->sopt_size == len);
1917: memcpy(sopt->sopt_data, buf, len);
1918: return 0;
1919: }
1920:
1921: /*
1922: * common case of set sockopt integer value
1923: */
1924: int
1925: sockopt_setint(struct sockopt *sopt, int val)
1926: {
1927:
1928: return sockopt_set(sopt, &val, sizeof(int));
1929: }
1930:
1931: /*
1932: * get sockopt value
1933: * - correct size must be given
1934: */
1935: int
1936: sockopt_get(const struct sockopt *sopt, void *buf, size_t len)
1937: {
1.170 tls 1938:
1.171 plunky 1939: if (sopt->sopt_size != len)
1940: return EINVAL;
1.1 cgd 1941:
1.171 plunky 1942: memcpy(buf, sopt->sopt_data, len);
1943: return 0;
1944: }
1.1 cgd 1945:
1.171 plunky 1946: /*
1947: * common case of get sockopt integer value
1948: */
1949: int
1950: sockopt_getint(const struct sockopt *sopt, int *valp)
1951: {
1.1 cgd 1952:
1.171 plunky 1953: return sockopt_get(sopt, valp, sizeof(int));
1954: }
1.1 cgd 1955:
1.171 plunky 1956: /*
1957: * set sockopt value from mbuf
1958: * - ONLY for legacy code
1959: * - mbuf is released by sockopt
1.176 ! plunky 1960: * - will not sleep
1.171 plunky 1961: */
1962: int
1963: sockopt_setmbuf(struct sockopt *sopt, struct mbuf *m)
1964: {
1965: size_t len;
1.176 ! plunky 1966: int error;
1.1 cgd 1967:
1.171 plunky 1968: len = m_length(m);
1.1 cgd 1969:
1.176 ! plunky 1970: if (sopt->sopt_size == 0) {
! 1971: error = sockopt_alloc(sopt, len, KM_NOSLEEP);
! 1972: if (error)
! 1973: return error;
! 1974: }
1.1 cgd 1975:
1.171 plunky 1976: KASSERT(sopt->sopt_size == len);
1977: m_copydata(m, 0, len, sopt->sopt_data);
1978: m_freem(m);
1.1 cgd 1979:
1.171 plunky 1980: return 0;
1981: }
1.1 cgd 1982:
1.171 plunky 1983: /*
1984: * get sockopt value into mbuf
1985: * - ONLY for legacy code
1986: * - mbuf to be released by the caller
1.176 ! plunky 1987: * - will not sleep
1.171 plunky 1988: */
1989: struct mbuf *
1990: sockopt_getmbuf(const struct sockopt *sopt)
1991: {
1992: struct mbuf *m;
1.107 darrenr 1993:
1.176 ! plunky 1994: if (sopt->sopt_size > MCLBYTES)
! 1995: return NULL;
! 1996:
! 1997: m = m_get(M_DONTWAIT, MT_SOOPTS);
1.171 plunky 1998: if (m == NULL)
1999: return NULL;
2000:
1.176 ! plunky 2001: if (sopt->sopt_size > MLEN) {
! 2002: MCLGET(m, M_DONTWAIT);
! 2003: if ((m->m_flags & M_EXT) == 0) {
! 2004: m_free(m);
! 2005: return NULL;
! 2006: }
1.1 cgd 2007: }
1.176 ! plunky 2008:
! 2009: memcpy(mtod(m, void *), sopt->sopt_data, sopt->sopt_size);
! 2010: m->m_len = sopt->sopt_size;
1.160 ad 2011:
1.171 plunky 2012: return m;
1.1 cgd 2013: }
2014:
1.14 mycroft 2015: void
1.54 lukem 2016: sohasoutofband(struct socket *so)
1.1 cgd 2017: {
1.153 rmind 2018:
1.90 christos 2019: fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so);
1.153 rmind 2020: selnotify(&so->so_rcv.sb_sel, POLLPRI | POLLRDBAND, 0);
1.1 cgd 2021: }
1.72 jdolecek 2022:
2023: static void
2024: filt_sordetach(struct knote *kn)
2025: {
2026: struct socket *so;
2027:
1.155 ad 2028: so = ((file_t *)kn->kn_obj)->f_data;
1.160 ad 2029: solock(so);
1.73 christos 2030: SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
2031: if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
1.72 jdolecek 2032: so->so_rcv.sb_flags &= ~SB_KNOTE;
1.160 ad 2033: sounlock(so);
1.72 jdolecek 2034: }
2035:
2036: /*ARGSUSED*/
2037: static int
1.129 yamt 2038: filt_soread(struct knote *kn, long hint)
1.72 jdolecek 2039: {
2040: struct socket *so;
1.160 ad 2041: int rv;
1.72 jdolecek 2042:
1.155 ad 2043: so = ((file_t *)kn->kn_obj)->f_data;
1.160 ad 2044: if (hint != NOTE_SUBMIT)
2045: solock(so);
1.72 jdolecek 2046: kn->kn_data = so->so_rcv.sb_cc;
2047: if (so->so_state & SS_CANTRCVMORE) {
1.108 perry 2048: kn->kn_flags |= EV_EOF;
1.72 jdolecek 2049: kn->kn_fflags = so->so_error;
1.160 ad 2050: rv = 1;
2051: } else if (so->so_error) /* temporary udp error */
2052: rv = 1;
2053: else if (kn->kn_sfflags & NOTE_LOWAT)
2054: rv = (kn->kn_data >= kn->kn_sdata);
2055: else
2056: rv = (kn->kn_data >= so->so_rcv.sb_lowat);
2057: if (hint != NOTE_SUBMIT)
2058: sounlock(so);
2059: return rv;
1.72 jdolecek 2060: }
2061:
2062: static void
2063: filt_sowdetach(struct knote *kn)
2064: {
2065: struct socket *so;
2066:
1.155 ad 2067: so = ((file_t *)kn->kn_obj)->f_data;
1.160 ad 2068: solock(so);
1.73 christos 2069: SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
2070: if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
1.72 jdolecek 2071: so->so_snd.sb_flags &= ~SB_KNOTE;
1.160 ad 2072: sounlock(so);
1.72 jdolecek 2073: }
2074:
2075: /*ARGSUSED*/
2076: static int
1.129 yamt 2077: filt_sowrite(struct knote *kn, long hint)
1.72 jdolecek 2078: {
2079: struct socket *so;
1.160 ad 2080: int rv;
1.72 jdolecek 2081:
1.155 ad 2082: so = ((file_t *)kn->kn_obj)->f_data;
1.160 ad 2083: if (hint != NOTE_SUBMIT)
2084: solock(so);
1.72 jdolecek 2085: kn->kn_data = sbspace(&so->so_snd);
2086: if (so->so_state & SS_CANTSENDMORE) {
1.108 perry 2087: kn->kn_flags |= EV_EOF;
1.72 jdolecek 2088: kn->kn_fflags = so->so_error;
1.160 ad 2089: rv = 1;
2090: } else if (so->so_error) /* temporary udp error */
2091: rv = 1;
2092: else if (((so->so_state & SS_ISCONNECTED) == 0) &&
1.72 jdolecek 2093: (so->so_proto->pr_flags & PR_CONNREQUIRED))
1.160 ad 2094: rv = 0;
2095: else if (kn->kn_sfflags & NOTE_LOWAT)
2096: rv = (kn->kn_data >= kn->kn_sdata);
2097: else
2098: rv = (kn->kn_data >= so->so_snd.sb_lowat);
2099: if (hint != NOTE_SUBMIT)
2100: sounlock(so);
2101: return rv;
1.72 jdolecek 2102: }
2103:
2104: /*ARGSUSED*/
2105: static int
1.129 yamt 2106: filt_solisten(struct knote *kn, long hint)
1.72 jdolecek 2107: {
2108: struct socket *so;
1.160 ad 2109: int rv;
1.72 jdolecek 2110:
1.155 ad 2111: so = ((file_t *)kn->kn_obj)->f_data;
1.72 jdolecek 2112:
2113: /*
2114: * Set kn_data to number of incoming connections, not
2115: * counting partial (incomplete) connections.
1.108 perry 2116: */
1.160 ad 2117: if (hint != NOTE_SUBMIT)
2118: solock(so);
1.72 jdolecek 2119: kn->kn_data = so->so_qlen;
1.160 ad 2120: rv = (kn->kn_data > 0);
2121: if (hint != NOTE_SUBMIT)
2122: sounlock(so);
2123: return rv;
1.72 jdolecek 2124: }
2125:
2126: static const struct filterops solisten_filtops =
2127: { 1, NULL, filt_sordetach, filt_solisten };
2128: static const struct filterops soread_filtops =
2129: { 1, NULL, filt_sordetach, filt_soread };
2130: static const struct filterops sowrite_filtops =
2131: { 1, NULL, filt_sowdetach, filt_sowrite };
2132:
2133: int
1.129 yamt 2134: soo_kqfilter(struct file *fp, struct knote *kn)
1.72 jdolecek 2135: {
2136: struct socket *so;
2137: struct sockbuf *sb;
2138:
1.155 ad 2139: so = ((file_t *)kn->kn_obj)->f_data;
1.160 ad 2140: solock(so);
1.72 jdolecek 2141: switch (kn->kn_filter) {
2142: case EVFILT_READ:
2143: if (so->so_options & SO_ACCEPTCONN)
2144: kn->kn_fop = &solisten_filtops;
2145: else
2146: kn->kn_fop = &soread_filtops;
2147: sb = &so->so_rcv;
2148: break;
2149: case EVFILT_WRITE:
2150: kn->kn_fop = &sowrite_filtops;
2151: sb = &so->so_snd;
2152: break;
2153: default:
1.160 ad 2154: sounlock(so);
1.149 pooka 2155: return (EINVAL);
1.72 jdolecek 2156: }
1.73 christos 2157: SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, kn, kn_selnext);
1.72 jdolecek 2158: sb->sb_flags |= SB_KNOTE;
1.160 ad 2159: sounlock(so);
1.72 jdolecek 2160: return (0);
2161: }
2162:
1.154 ad 2163: static int
2164: sodopoll(struct socket *so, int events)
2165: {
2166: int revents;
2167:
2168: revents = 0;
2169:
2170: if (events & (POLLIN | POLLRDNORM))
2171: if (soreadable(so))
2172: revents |= events & (POLLIN | POLLRDNORM);
2173:
2174: if (events & (POLLOUT | POLLWRNORM))
2175: if (sowritable(so))
2176: revents |= events & (POLLOUT | POLLWRNORM);
2177:
2178: if (events & (POLLPRI | POLLRDBAND))
2179: if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
2180: revents |= events & (POLLPRI | POLLRDBAND);
2181:
2182: return revents;
2183: }
2184:
2185: int
2186: sopoll(struct socket *so, int events)
2187: {
2188: int revents = 0;
2189:
1.160 ad 2190: #ifndef DIAGNOSTIC
2191: /*
2192: * Do a quick, unlocked check in expectation that the socket
2193: * will be ready for I/O. Don't do this check if DIAGNOSTIC,
2194: * as the solocked() assertions will fail.
2195: */
1.154 ad 2196: if ((revents = sodopoll(so, events)) != 0)
2197: return revents;
1.160 ad 2198: #endif
1.154 ad 2199:
1.160 ad 2200: solock(so);
1.154 ad 2201: if ((revents = sodopoll(so, events)) == 0) {
2202: if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
2203: selrecord(curlwp, &so->so_rcv.sb_sel);
1.160 ad 2204: so->so_rcv.sb_flags |= SB_NOTIFY;
1.154 ad 2205: }
2206:
2207: if (events & (POLLOUT | POLLWRNORM)) {
2208: selrecord(curlwp, &so->so_snd.sb_sel);
1.160 ad 2209: so->so_snd.sb_flags |= SB_NOTIFY;
1.154 ad 2210: }
2211: }
1.160 ad 2212: sounlock(so);
1.154 ad 2213:
2214: return revents;
2215: }
2216:
2217:
1.94 yamt 2218: #include <sys/sysctl.h>
2219:
2220: static int sysctl_kern_somaxkva(SYSCTLFN_PROTO);
2221:
2222: /*
2223: * sysctl helper routine for kern.somaxkva. ensures that the given
2224: * value is not too small.
2225: * (XXX should we maybe make sure it's not too large as well?)
2226: */
2227: static int
2228: sysctl_kern_somaxkva(SYSCTLFN_ARGS)
2229: {
2230: int error, new_somaxkva;
2231: struct sysctlnode node;
2232:
2233: new_somaxkva = somaxkva;
2234: node = *rnode;
2235: node.sysctl_data = &new_somaxkva;
2236: error = sysctl_lookup(SYSCTLFN_CALL(&node));
2237: if (error || newp == NULL)
2238: return (error);
2239:
2240: if (new_somaxkva < (16 * 1024 * 1024)) /* sanity */
2241: return (EINVAL);
2242:
1.136 ad 2243: mutex_enter(&so_pendfree_lock);
1.94 yamt 2244: somaxkva = new_somaxkva;
1.136 ad 2245: cv_broadcast(&socurkva_cv);
2246: mutex_exit(&so_pendfree_lock);
1.94 yamt 2247:
2248: return (error);
2249: }
2250:
2251: SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup")
2252: {
2253:
1.97 atatat 2254: sysctl_createv(clog, 0, NULL, NULL,
2255: CTLFLAG_PERMANENT,
2256: CTLTYPE_NODE, "kern", NULL,
2257: NULL, 0, NULL, 0,
2258: CTL_KERN, CTL_EOL);
2259:
2260: sysctl_createv(clog, 0, NULL, NULL,
2261: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.103 atatat 2262: CTLTYPE_INT, "somaxkva",
2263: SYSCTL_DESCR("Maximum amount of kernel memory to be "
2264: "used for socket buffers"),
1.94 yamt 2265: sysctl_kern_somaxkva, 0, NULL, 0,
2266: CTL_KERN, KERN_SOMAXKVA, CTL_EOL);
2267: }
CVSweb <webmaster@jp.NetBSD.org>