Annotation of src/sys/kern/uipc_socket.c, Revision 1.174
1.174 ! pooka 1: /* $NetBSD: uipc_socket.c,v 1.173 2008/10/10 19:49:49 plunky 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.174 ! pooka 66: __KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.173 2008/10/10 19:49:49 plunky 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: #ifdef INET
619: /* remove acccept filter if one is present. */
620: if (so->so_accf != NULL)
621: do_setopt_accept_filter(so, NULL);
622: #endif
1.160 ad 623: sounlock(so);
1.161 ad 624: if (refs == 0) /* XXX */
625: soput(so);
1.1 cgd 626: }
627:
628: /*
629: * Close a socket on last file table reference removal.
630: * Initiate disconnect if connected.
631: * Free socket when disconnect complete.
632: */
1.3 andrew 633: int
1.54 lukem 634: soclose(struct socket *so)
1.1 cgd 635: {
1.54 lukem 636: struct socket *so2;
1.160 ad 637: int error;
638: int error2;
1.1 cgd 639:
1.54 lukem 640: error = 0;
1.160 ad 641: solock(so);
1.1 cgd 642: if (so->so_options & SO_ACCEPTCONN) {
1.172 ad 643: for (;;) {
644: if ((so2 = TAILQ_FIRST(&so->so_q0)) != 0) {
1.160 ad 645: KASSERT(solocked2(so, so2));
646: (void) soqremque(so2, 0);
647: /* soabort drops the lock. */
648: (void) soabort(so2);
649: solock(so);
1.172 ad 650: continue;
1.160 ad 651: }
1.172 ad 652: if ((so2 = TAILQ_FIRST(&so->so_q)) != 0) {
1.160 ad 653: KASSERT(solocked2(so, so2));
654: (void) soqremque(so2, 1);
655: /* soabort drops the lock. */
656: (void) soabort(so2);
657: solock(so);
1.172 ad 658: continue;
1.160 ad 659: }
1.172 ad 660: break;
661: }
1.1 cgd 662: }
663: if (so->so_pcb == 0)
664: goto discard;
665: if (so->so_state & SS_ISCONNECTED) {
666: if ((so->so_state & SS_ISDISCONNECTING) == 0) {
667: error = sodisconnect(so);
668: if (error)
669: goto drop;
670: }
671: if (so->so_options & SO_LINGER) {
1.151 ad 672: if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio)
1.1 cgd 673: goto drop;
1.21 christos 674: while (so->so_state & SS_ISCONNECTED) {
1.160 ad 675: error = sowait(so, so->so_linger * hz);
1.21 christos 676: if (error)
1.1 cgd 677: break;
1.21 christos 678: }
1.1 cgd 679: }
680: }
1.54 lukem 681: drop:
1.1 cgd 682: if (so->so_pcb) {
1.160 ad 683: error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
1.140 dyoung 684: NULL, NULL, NULL, NULL);
1.1 cgd 685: if (error == 0)
686: error = error2;
687: }
1.54 lukem 688: discard:
1.1 cgd 689: if (so->so_state & SS_NOFDREF)
690: panic("soclose: NOFDREF");
691: so->so_state |= SS_NOFDREF;
692: sofree(so);
693: return (error);
694: }
695:
696: /*
1.160 ad 697: * Must be called with the socket locked.. Will return with it unlocked.
1.1 cgd 698: */
1.3 andrew 699: int
1.54 lukem 700: soabort(struct socket *so)
1.1 cgd 701: {
1.161 ad 702: u_int refs;
1.139 yamt 703: int error;
1.160 ad 704:
705: KASSERT(solocked(so));
706: KASSERT(so->so_head == NULL);
1.1 cgd 707:
1.161 ad 708: so->so_aborting++; /* XXX */
1.140 dyoung 709: error = (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL,
710: NULL, NULL, NULL);
1.161 ad 711: refs = --so->so_aborting; /* XXX */
1.164 drochner 712: if (error || (refs == 0)) {
1.139 yamt 713: sofree(so);
1.160 ad 714: } else {
715: sounlock(so);
1.139 yamt 716: }
717: return error;
1.1 cgd 718: }
719:
1.3 andrew 720: int
1.54 lukem 721: soaccept(struct socket *so, struct mbuf *nam)
1.1 cgd 722: {
1.160 ad 723: int error;
724:
725: KASSERT(solocked(so));
1.1 cgd 726:
1.54 lukem 727: error = 0;
1.1 cgd 728: if ((so->so_state & SS_NOFDREF) == 0)
729: panic("soaccept: !NOFDREF");
730: so->so_state &= ~SS_NOFDREF;
1.55 thorpej 731: if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
732: (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
1.41 mycroft 733: error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
1.140 dyoung 734: NULL, nam, NULL, NULL);
1.41 mycroft 735: else
1.53 itojun 736: error = ECONNABORTED;
1.52 itojun 737:
1.1 cgd 738: return (error);
739: }
740:
1.3 andrew 741: int
1.114 christos 742: soconnect(struct socket *so, struct mbuf *nam, struct lwp *l)
1.1 cgd 743: {
1.160 ad 744: int error;
745:
746: KASSERT(solocked(so));
1.1 cgd 747:
748: if (so->so_options & SO_ACCEPTCONN)
749: return (EOPNOTSUPP);
750: /*
751: * If protocol is connection-based, can only connect once.
752: * Otherwise, if connected, try to disconnect first.
753: * This allows user to disconnect by connecting to, e.g.,
754: * a null address.
755: */
756: if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
757: ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
758: (error = sodisconnect(so))))
759: error = EISCONN;
760: else
761: error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
1.140 dyoung 762: NULL, nam, NULL, l);
1.1 cgd 763: return (error);
764: }
765:
1.3 andrew 766: int
1.54 lukem 767: soconnect2(struct socket *so1, struct socket *so2)
1.1 cgd 768: {
1.160 ad 769: int error;
770:
771: KASSERT(solocked2(so1, so2));
1.1 cgd 772:
1.22 mycroft 773: error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
1.140 dyoung 774: NULL, (struct mbuf *)so2, NULL, NULL);
1.1 cgd 775: return (error);
776: }
777:
1.3 andrew 778: int
1.54 lukem 779: sodisconnect(struct socket *so)
1.1 cgd 780: {
1.160 ad 781: int error;
782:
783: KASSERT(solocked(so));
1.1 cgd 784:
785: if ((so->so_state & SS_ISCONNECTED) == 0) {
786: error = ENOTCONN;
1.160 ad 787: } else if (so->so_state & SS_ISDISCONNECTING) {
1.1 cgd 788: error = EALREADY;
1.160 ad 789: } else {
790: error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
791: NULL, NULL, NULL, NULL);
1.1 cgd 792: }
1.117 yamt 793: sodopendfree();
1.1 cgd 794: return (error);
795: }
796:
1.15 mycroft 797: #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
1.1 cgd 798: /*
799: * Send on a socket.
800: * If send must go all at once and message is larger than
801: * send buffering, then hard error.
802: * Lock against other senders.
803: * If must go all at once and not enough room now, then
804: * inform user that this would block and do nothing.
805: * Otherwise, if nonblocking, send as much as possible.
806: * The data to be sent is described by "uio" if nonzero,
807: * otherwise by the mbuf chain "top" (which must be null
808: * if uio is not). Data provided in mbuf chain must be small
809: * enough to send all at once.
810: *
811: * Returns nonzero on error, timeout or signal; callers
812: * must check for short counts if EINTR/ERESTART are returned.
813: * Data and control buffers are freed on return.
814: */
1.3 andrew 815: int
1.54 lukem 816: sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
1.114 christos 817: struct mbuf *control, int flags, struct lwp *l)
1.1 cgd 818: {
1.54 lukem 819: struct mbuf **mp, *m;
1.114 christos 820: struct proc *p;
1.58 jdolecek 821: long space, len, resid, clen, mlen;
822: int error, s, dontroute, atomic;
1.54 lukem 823:
1.114 christos 824: p = l->l_proc;
1.117 yamt 825: sodopendfree();
1.160 ad 826: clen = 0;
1.64 thorpej 827:
1.160 ad 828: /*
829: * solock() provides atomicity of access. splsoftnet() prevents
830: * protocol processing soft interrupts from interrupting us and
831: * blocking (expensive).
832: */
833: s = splsoftnet();
834: solock(so);
1.54 lukem 835: atomic = sosendallatonce(so) || top;
1.1 cgd 836: if (uio)
837: resid = uio->uio_resid;
838: else
839: resid = top->m_pkthdr.len;
1.7 cgd 840: /*
841: * In theory resid should be unsigned.
842: * However, space must be signed, as it might be less than 0
843: * if we over-committed, and we must use a signed comparison
844: * of space and resid. On the other hand, a negative resid
845: * causes us to loop sending 0-length segments to the protocol.
846: */
1.29 mycroft 847: if (resid < 0) {
848: error = EINVAL;
849: goto out;
850: }
1.1 cgd 851: dontroute =
852: (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
853: (so->so_proto->pr_flags & PR_ATOMIC);
1.165 christos 854: l->l_ru.ru_msgsnd++;
1.1 cgd 855: if (control)
856: clen = control->m_len;
1.54 lukem 857: restart:
1.21 christos 858: if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
1.1 cgd 859: goto out;
860: do {
1.160 ad 861: if (so->so_state & SS_CANTSENDMORE) {
862: error = EPIPE;
863: goto release;
864: }
1.48 thorpej 865: if (so->so_error) {
866: error = so->so_error;
867: so->so_error = 0;
868: goto release;
869: }
1.1 cgd 870: if ((so->so_state & SS_ISCONNECTED) == 0) {
871: if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
872: if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1.160 ad 873: !(resid == 0 && clen != 0)) {
874: error = ENOTCONN;
875: goto release;
876: }
877: } else if (addr == 0) {
878: error = EDESTADDRREQ;
879: goto release;
880: }
1.1 cgd 881: }
882: space = sbspace(&so->so_snd);
883: if (flags & MSG_OOB)
884: space += 1024;
1.21 christos 885: if ((atomic && resid > so->so_snd.sb_hiwat) ||
1.160 ad 886: clen > so->so_snd.sb_hiwat) {
887: error = EMSGSIZE;
888: goto release;
889: }
1.96 mycroft 890: if (space < resid + clen &&
1.1 cgd 891: (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1.160 ad 892: if (so->so_nbio) {
893: error = EWOULDBLOCK;
894: goto release;
895: }
1.1 cgd 896: sbunlock(&so->so_snd);
897: error = sbwait(&so->so_snd);
898: if (error)
899: goto out;
900: goto restart;
901: }
902: mp = ⊤
903: space -= clen;
904: do {
1.45 tv 905: if (uio == NULL) {
906: /*
907: * Data is prepackaged in "top".
908: */
909: resid = 0;
910: if (flags & MSG_EOR)
911: top->m_flags |= M_EOR;
912: } else do {
1.160 ad 913: sounlock(so);
914: splx(s);
1.144 dyoung 915: if (top == NULL) {
1.78 matt 916: m = m_gethdr(M_WAIT, MT_DATA);
1.45 tv 917: mlen = MHLEN;
918: m->m_pkthdr.len = 0;
1.140 dyoung 919: m->m_pkthdr.rcvif = NULL;
1.45 tv 920: } else {
1.78 matt 921: m = m_get(M_WAIT, MT_DATA);
1.45 tv 922: mlen = MLEN;
923: }
1.78 matt 924: MCLAIM(m, so->so_snd.sb_mowner);
1.121 yamt 925: if (sock_loan_thresh >= 0 &&
926: uio->uio_iov->iov_len >= sock_loan_thresh &&
927: space >= sock_loan_thresh &&
1.64 thorpej 928: (len = sosend_loan(so, uio, m,
929: space)) != 0) {
930: SOSEND_COUNTER_INCR(&sosend_loan_big);
931: space -= len;
932: goto have_data;
933: }
1.45 tv 934: if (resid >= MINCLSIZE && space >= MCLBYTES) {
1.64 thorpej 935: SOSEND_COUNTER_INCR(&sosend_copy_big);
1.78 matt 936: m_clget(m, M_WAIT);
1.45 tv 937: if ((m->m_flags & M_EXT) == 0)
938: goto nopages;
939: mlen = MCLBYTES;
940: if (atomic && top == 0) {
1.58 jdolecek 941: len = lmin(MCLBYTES - max_hdr,
1.54 lukem 942: resid);
1.45 tv 943: m->m_data += max_hdr;
944: } else
1.58 jdolecek 945: len = lmin(MCLBYTES, resid);
1.45 tv 946: space -= len;
947: } else {
1.64 thorpej 948: nopages:
949: SOSEND_COUNTER_INCR(&sosend_copy_small);
1.58 jdolecek 950: len = lmin(lmin(mlen, resid), space);
1.45 tv 951: space -= len;
952: /*
953: * For datagram protocols, leave room
954: * for protocol headers in first mbuf.
955: */
956: if (atomic && top == 0 && len < mlen)
957: MH_ALIGN(m, len);
958: }
1.144 dyoung 959: error = uiomove(mtod(m, void *), (int)len, uio);
1.64 thorpej 960: have_data:
1.45 tv 961: resid = uio->uio_resid;
962: m->m_len = len;
963: *mp = m;
964: top->m_pkthdr.len += len;
1.160 ad 965: s = splsoftnet();
966: solock(so);
1.144 dyoung 967: if (error != 0)
1.45 tv 968: goto release;
969: mp = &m->m_next;
970: if (resid <= 0) {
971: if (flags & MSG_EOR)
972: top->m_flags |= M_EOR;
973: break;
974: }
975: } while (space > 0 && atomic);
1.108 perry 976:
1.160 ad 977: if (so->so_state & SS_CANTSENDMORE) {
978: error = EPIPE;
979: goto release;
980: }
1.45 tv 981: if (dontroute)
982: so->so_options |= SO_DONTROUTE;
983: if (resid > 0)
984: so->so_state |= SS_MORETOCOME;
1.46 sommerfe 985: error = (*so->so_proto->pr_usrreq)(so,
986: (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
1.160 ad 987: top, addr, control, curlwp);
1.45 tv 988: if (dontroute)
989: so->so_options &= ~SO_DONTROUTE;
990: if (resid > 0)
991: so->so_state &= ~SS_MORETOCOME;
992: clen = 0;
1.144 dyoung 993: control = NULL;
994: top = NULL;
1.45 tv 995: mp = ⊤
1.144 dyoung 996: if (error != 0)
1.1 cgd 997: goto release;
998: } while (resid && space > 0);
999: } while (resid);
1000:
1.54 lukem 1001: release:
1.1 cgd 1002: sbunlock(&so->so_snd);
1.54 lukem 1003: out:
1.160 ad 1004: sounlock(so);
1005: splx(s);
1.1 cgd 1006: if (top)
1007: m_freem(top);
1008: if (control)
1009: m_freem(control);
1010: return (error);
1011: }
1012:
1013: /*
1.159 ad 1014: * Following replacement or removal of the first mbuf on the first
1015: * mbuf chain of a socket buffer, push necessary state changes back
1016: * into the socket buffer so that other consumers see the values
1017: * consistently. 'nextrecord' is the callers locally stored value of
1018: * the original value of sb->sb_mb->m_nextpkt which must be restored
1019: * when the lead mbuf changes. NOTE: 'nextrecord' may be NULL.
1020: */
1021: static void
1022: sbsync(struct sockbuf *sb, struct mbuf *nextrecord)
1023: {
1024:
1.160 ad 1025: KASSERT(solocked(sb->sb_so));
1026:
1.159 ad 1027: /*
1028: * First, update for the new value of nextrecord. If necessary,
1029: * make it the first record.
1030: */
1031: if (sb->sb_mb != NULL)
1032: sb->sb_mb->m_nextpkt = nextrecord;
1033: else
1034: sb->sb_mb = nextrecord;
1035:
1036: /*
1037: * Now update any dependent socket buffer fields to reflect
1038: * the new state. This is an inline of SB_EMPTY_FIXUP, with
1039: * the addition of a second clause that takes care of the
1040: * case where sb_mb has been updated, but remains the last
1041: * record.
1042: */
1043: if (sb->sb_mb == NULL) {
1044: sb->sb_mbtail = NULL;
1045: sb->sb_lastrecord = NULL;
1046: } else if (sb->sb_mb->m_nextpkt == NULL)
1047: sb->sb_lastrecord = sb->sb_mb;
1048: }
1049:
1050: /*
1.1 cgd 1051: * Implement receive operations on a socket.
1052: * We depend on the way that records are added to the sockbuf
1053: * by sbappend*. In particular, each record (mbufs linked through m_next)
1054: * must begin with an address if the protocol so specifies,
1055: * followed by an optional mbuf or mbufs containing ancillary data,
1056: * and then zero or more mbufs of data.
1057: * In order to avoid blocking network interrupts for the entire time here,
1058: * we splx() while doing the actual copy to user space.
1059: * Although the sockbuf is locked, new data may still be appended,
1060: * and thus we must maintain consistency of the sockbuf during that time.
1061: *
1062: * The caller may receive the data as a single mbuf chain by supplying
1063: * an mbuf **mp0 for use in returning the chain. The uio is then used
1064: * only for the count in uio_resid.
1065: */
1.3 andrew 1066: int
1.54 lukem 1067: soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
1068: struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1.1 cgd 1069: {
1.116 yamt 1070: struct lwp *l = curlwp;
1.160 ad 1071: struct mbuf *m, **mp, *mt;
1.146 dyoung 1072: int atomic, flags, len, error, s, offset, moff, type, orig_resid;
1.99 matt 1073: const struct protosw *pr;
1.54 lukem 1074: struct mbuf *nextrecord;
1.67 he 1075: int mbuf_removed = 0;
1.146 dyoung 1076: const struct domain *dom;
1.64 thorpej 1077:
1.54 lukem 1078: pr = so->so_proto;
1.146 dyoung 1079: atomic = pr->pr_flags & PR_ATOMIC;
1080: dom = pr->pr_domain;
1.1 cgd 1081: mp = mp0;
1.54 lukem 1082: type = 0;
1083: orig_resid = uio->uio_resid;
1.102 jonathan 1084:
1.144 dyoung 1085: if (paddr != NULL)
1086: *paddr = NULL;
1087: if (controlp != NULL)
1088: *controlp = NULL;
1089: if (flagsp != NULL)
1.1 cgd 1090: flags = *flagsp &~ MSG_EOR;
1091: else
1092: flags = 0;
1.66 enami 1093:
1094: if ((flags & MSG_DONTWAIT) == 0)
1.117 yamt 1095: sodopendfree();
1.66 enami 1096:
1.1 cgd 1097: if (flags & MSG_OOB) {
1098: m = m_get(M_WAIT, MT_DATA);
1.160 ad 1099: solock(so);
1.17 cgd 1100: error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
1.140 dyoung 1101: (struct mbuf *)(long)(flags & MSG_PEEK), NULL, l);
1.160 ad 1102: sounlock(so);
1.1 cgd 1103: if (error)
1104: goto bad;
1105: do {
1.134 christos 1106: error = uiomove(mtod(m, void *),
1.1 cgd 1107: (int) min(uio->uio_resid, m->m_len), uio);
1108: m = m_free(m);
1.144 dyoung 1109: } while (uio->uio_resid > 0 && error == 0 && m);
1.54 lukem 1110: bad:
1.144 dyoung 1111: if (m != NULL)
1.1 cgd 1112: m_freem(m);
1.144 dyoung 1113: return error;
1.1 cgd 1114: }
1.144 dyoung 1115: if (mp != NULL)
1.140 dyoung 1116: *mp = NULL;
1.160 ad 1117:
1118: /*
1119: * solock() provides atomicity of access. splsoftnet() prevents
1120: * protocol processing soft interrupts from interrupting us and
1121: * blocking (expensive).
1122: */
1123: s = splsoftnet();
1124: solock(so);
1.1 cgd 1125: if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
1.140 dyoung 1126: (*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l);
1.1 cgd 1127:
1.54 lukem 1128: restart:
1.160 ad 1129: if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) {
1130: sounlock(so);
1131: splx(s);
1.144 dyoung 1132: return error;
1.160 ad 1133: }
1.1 cgd 1134:
1135: m = so->so_rcv.sb_mb;
1136: /*
1137: * If we have less data than requested, block awaiting more
1138: * (subject to any timeout) if:
1.15 mycroft 1139: * 1. the current count is less than the low water mark,
1.1 cgd 1140: * 2. MSG_WAITALL is set, and it is possible to do the entire
1.15 mycroft 1141: * receive operation at once if we block (resid <= hiwat), or
1142: * 3. MSG_DONTWAIT is not set.
1.1 cgd 1143: * If MSG_WAITALL is set but resid is larger than the receive buffer,
1144: * we have to do the receive in sections, and thus risk returning
1145: * a short count if a timeout or signal occurs after we start.
1146: */
1.144 dyoung 1147: if (m == NULL ||
1148: ((flags & MSG_DONTWAIT) == 0 &&
1149: so->so_rcv.sb_cc < uio->uio_resid &&
1150: (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
1151: ((flags & MSG_WAITALL) &&
1152: uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1.146 dyoung 1153: m->m_nextpkt == NULL && !atomic)) {
1.1 cgd 1154: #ifdef DIAGNOSTIC
1.144 dyoung 1155: if (m == NULL && so->so_rcv.sb_cc)
1.1 cgd 1156: panic("receive 1");
1157: #endif
1158: if (so->so_error) {
1.144 dyoung 1159: if (m != NULL)
1.15 mycroft 1160: goto dontblock;
1.1 cgd 1161: error = so->so_error;
1162: if ((flags & MSG_PEEK) == 0)
1163: so->so_error = 0;
1164: goto release;
1165: }
1166: if (so->so_state & SS_CANTRCVMORE) {
1.144 dyoung 1167: if (m != NULL)
1.15 mycroft 1168: goto dontblock;
1.1 cgd 1169: else
1170: goto release;
1171: }
1.144 dyoung 1172: for (; m != NULL; m = m->m_next)
1.1 cgd 1173: if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1174: m = so->so_rcv.sb_mb;
1175: goto dontblock;
1176: }
1177: if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1178: (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1179: error = ENOTCONN;
1180: goto release;
1181: }
1182: if (uio->uio_resid == 0)
1183: goto release;
1.151 ad 1184: if (so->so_nbio || (flags & MSG_DONTWAIT)) {
1.1 cgd 1185: error = EWOULDBLOCK;
1186: goto release;
1187: }
1.69 thorpej 1188: SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1");
1189: SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1");
1.1 cgd 1190: sbunlock(&so->so_rcv);
1191: error = sbwait(&so->so_rcv);
1.160 ad 1192: if (error != 0) {
1193: sounlock(so);
1194: splx(s);
1.144 dyoung 1195: return error;
1.160 ad 1196: }
1.1 cgd 1197: goto restart;
1198: }
1.54 lukem 1199: dontblock:
1.69 thorpej 1200: /*
1201: * On entry here, m points to the first record of the socket buffer.
1.159 ad 1202: * From this point onward, we maintain 'nextrecord' as a cache of the
1203: * pointer to the next record in the socket buffer. We must keep the
1204: * various socket buffer pointers and local stack versions of the
1205: * pointers in sync, pushing out modifications before dropping the
1.160 ad 1206: * socket lock, and re-reading them when picking it up.
1.159 ad 1207: *
1208: * Otherwise, we will race with the network stack appending new data
1209: * or records onto the socket buffer by using inconsistent/stale
1210: * versions of the field, possibly resulting in socket buffer
1211: * corruption.
1212: *
1213: * By holding the high-level sblock(), we prevent simultaneous
1214: * readers from pulling off the front of the socket buffer.
1.69 thorpej 1215: */
1.144 dyoung 1216: if (l != NULL)
1.157 ad 1217: l->l_ru.ru_msgrcv++;
1.69 thorpej 1218: KASSERT(m == so->so_rcv.sb_mb);
1219: SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
1220: SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
1.1 cgd 1221: nextrecord = m->m_nextpkt;
1222: if (pr->pr_flags & PR_ADDR) {
1223: #ifdef DIAGNOSTIC
1224: if (m->m_type != MT_SONAME)
1225: panic("receive 1a");
1226: #endif
1.3 andrew 1227: orig_resid = 0;
1.1 cgd 1228: if (flags & MSG_PEEK) {
1229: if (paddr)
1230: *paddr = m_copy(m, 0, m->m_len);
1231: m = m->m_next;
1232: } else {
1233: sbfree(&so->so_rcv, m);
1.67 he 1234: mbuf_removed = 1;
1.144 dyoung 1235: if (paddr != NULL) {
1.1 cgd 1236: *paddr = m;
1237: so->so_rcv.sb_mb = m->m_next;
1.144 dyoung 1238: m->m_next = NULL;
1.1 cgd 1239: m = so->so_rcv.sb_mb;
1240: } else {
1241: MFREE(m, so->so_rcv.sb_mb);
1242: m = so->so_rcv.sb_mb;
1243: }
1.159 ad 1244: sbsync(&so->so_rcv, nextrecord);
1.1 cgd 1245: }
1246: }
1.159 ad 1247:
1248: /*
1249: * Process one or more MT_CONTROL mbufs present before any data mbufs
1250: * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1251: * just copy the data; if !MSG_PEEK, we call into the protocol to
1252: * perform externalization (or freeing if controlp == NULL).
1253: */
1254: if (__predict_false(m != NULL && m->m_type == MT_CONTROL)) {
1255: struct mbuf *cm = NULL, *cmn;
1256: struct mbuf **cme = &cm;
1257:
1258: do {
1259: if (flags & MSG_PEEK) {
1260: if (controlp != NULL) {
1261: *controlp = m_copy(m, 0, m->m_len);
1262: controlp = &(*controlp)->m_next;
1263: }
1264: m = m->m_next;
1265: } else {
1266: sbfree(&so->so_rcv, m);
1.1 cgd 1267: so->so_rcv.sb_mb = m->m_next;
1.144 dyoung 1268: m->m_next = NULL;
1.159 ad 1269: *cme = m;
1270: cme = &(*cme)->m_next;
1.1 cgd 1271: m = so->so_rcv.sb_mb;
1.159 ad 1272: }
1273: } while (m != NULL && m->m_type == MT_CONTROL);
1274: if ((flags & MSG_PEEK) == 0)
1275: sbsync(&so->so_rcv, nextrecord);
1276: for (; cm != NULL; cm = cmn) {
1277: cmn = cm->m_next;
1278: cm->m_next = NULL;
1279: type = mtod(cm, struct cmsghdr *)->cmsg_type;
1280: if (controlp != NULL) {
1281: if (dom->dom_externalize != NULL &&
1282: type == SCM_RIGHTS) {
1.160 ad 1283: sounlock(so);
1.159 ad 1284: splx(s);
1285: error = (*dom->dom_externalize)(cm, l);
1286: s = splsoftnet();
1.160 ad 1287: solock(so);
1.159 ad 1288: }
1289: *controlp = cm;
1290: while (*controlp != NULL)
1291: controlp = &(*controlp)->m_next;
1.1 cgd 1292: } else {
1.106 itojun 1293: /*
1294: * Dispose of any SCM_RIGHTS message that went
1295: * through the read path rather than recv.
1296: */
1.159 ad 1297: if (dom->dom_dispose != NULL &&
1298: type == SCM_RIGHTS) {
1.160 ad 1299: sounlock(so);
1.159 ad 1300: (*dom->dom_dispose)(cm);
1.160 ad 1301: solock(so);
1.159 ad 1302: }
1303: m_freem(cm);
1.1 cgd 1304: }
1305: }
1.159 ad 1306: if (m != NULL)
1307: nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1308: else
1309: nextrecord = so->so_rcv.sb_mb;
1310: orig_resid = 0;
1.1 cgd 1311: }
1.69 thorpej 1312:
1.159 ad 1313: /* If m is non-NULL, we have some data to read. */
1314: if (__predict_true(m != NULL)) {
1.1 cgd 1315: type = m->m_type;
1316: if (type == MT_OOBDATA)
1317: flags |= MSG_OOB;
1318: }
1.69 thorpej 1319: SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
1320: SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
1321:
1.1 cgd 1322: moff = 0;
1323: offset = 0;
1.144 dyoung 1324: while (m != NULL && uio->uio_resid > 0 && error == 0) {
1.1 cgd 1325: if (m->m_type == MT_OOBDATA) {
1326: if (type != MT_OOBDATA)
1327: break;
1328: } else if (type == MT_OOBDATA)
1329: break;
1330: #ifdef DIAGNOSTIC
1331: else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
1332: panic("receive 3");
1333: #endif
1334: so->so_state &= ~SS_RCVATMARK;
1335: len = uio->uio_resid;
1336: if (so->so_oobmark && len > so->so_oobmark - offset)
1337: len = so->so_oobmark - offset;
1338: if (len > m->m_len - moff)
1339: len = m->m_len - moff;
1340: /*
1341: * If mp is set, just pass back the mbufs.
1342: * Otherwise copy them out via the uio, then free.
1343: * Sockbuf must be consistent here (points to current mbuf,
1344: * it points to next record) when we drop priority;
1345: * we must note any additions to the sockbuf when we
1346: * block interrupts again.
1347: */
1.144 dyoung 1348: if (mp == NULL) {
1.69 thorpej 1349: SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove");
1350: SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove");
1.160 ad 1351: sounlock(so);
1.1 cgd 1352: splx(s);
1.134 christos 1353: error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1.20 mycroft 1354: s = splsoftnet();
1.160 ad 1355: solock(so);
1.144 dyoung 1356: if (error != 0) {
1.67 he 1357: /*
1358: * If any part of the record has been removed
1359: * (such as the MT_SONAME mbuf, which will
1360: * happen when PR_ADDR, and thus also
1361: * PR_ATOMIC, is set), then drop the entire
1362: * record to maintain the atomicity of the
1363: * receive operation.
1364: *
1365: * This avoids a later panic("receive 1a")
1366: * when compiled with DIAGNOSTIC.
1367: */
1.146 dyoung 1368: if (m && mbuf_removed && atomic)
1.67 he 1369: (void) sbdroprecord(&so->so_rcv);
1370:
1.57 jdolecek 1371: goto release;
1.67 he 1372: }
1.1 cgd 1373: } else
1374: uio->uio_resid -= len;
1375: if (len == m->m_len - moff) {
1376: if (m->m_flags & M_EOR)
1377: flags |= MSG_EOR;
1378: if (flags & MSG_PEEK) {
1379: m = m->m_next;
1380: moff = 0;
1381: } else {
1382: nextrecord = m->m_nextpkt;
1383: sbfree(&so->so_rcv, m);
1384: if (mp) {
1385: *mp = m;
1386: mp = &m->m_next;
1387: so->so_rcv.sb_mb = m = m->m_next;
1.140 dyoung 1388: *mp = NULL;
1.1 cgd 1389: } else {
1390: MFREE(m, so->so_rcv.sb_mb);
1391: m = so->so_rcv.sb_mb;
1392: }
1.69 thorpej 1393: /*
1394: * If m != NULL, we also know that
1395: * so->so_rcv.sb_mb != NULL.
1396: */
1397: KASSERT(so->so_rcv.sb_mb == m);
1398: if (m) {
1.1 cgd 1399: m->m_nextpkt = nextrecord;
1.69 thorpej 1400: if (nextrecord == NULL)
1401: so->so_rcv.sb_lastrecord = m;
1402: } else {
1403: so->so_rcv.sb_mb = nextrecord;
1.70 thorpej 1404: SB_EMPTY_FIXUP(&so->so_rcv);
1.69 thorpej 1405: }
1406: SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
1407: SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
1.1 cgd 1408: }
1.144 dyoung 1409: } else if (flags & MSG_PEEK)
1410: moff += len;
1411: else {
1.160 ad 1412: if (mp != NULL) {
1413: mt = m_copym(m, 0, len, M_NOWAIT);
1414: if (__predict_false(mt == NULL)) {
1415: sounlock(so);
1416: mt = m_copym(m, 0, len, M_WAIT);
1417: solock(so);
1418: }
1419: *mp = mt;
1420: }
1.144 dyoung 1421: m->m_data += len;
1422: m->m_len -= len;
1423: so->so_rcv.sb_cc -= len;
1.1 cgd 1424: }
1425: if (so->so_oobmark) {
1426: if ((flags & MSG_PEEK) == 0) {
1427: so->so_oobmark -= len;
1428: if (so->so_oobmark == 0) {
1429: so->so_state |= SS_RCVATMARK;
1430: break;
1431: }
1.7 cgd 1432: } else {
1.1 cgd 1433: offset += len;
1.7 cgd 1434: if (offset == so->so_oobmark)
1435: break;
1436: }
1.1 cgd 1437: }
1438: if (flags & MSG_EOR)
1439: break;
1440: /*
1441: * If the MSG_WAITALL flag is set (for non-atomic socket),
1442: * we must not quit until "uio->uio_resid == 0" or an error
1443: * termination. If a signal/timeout occurs, return
1444: * with a short count but without error.
1445: * Keep sockbuf locked against other readers.
1446: */
1.144 dyoung 1447: while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1.3 andrew 1448: !sosendallatonce(so) && !nextrecord) {
1.1 cgd 1449: if (so->so_error || so->so_state & SS_CANTRCVMORE)
1450: break;
1.68 matt 1451: /*
1452: * If we are peeking and the socket receive buffer is
1453: * full, stop since we can't get more data to peek at.
1454: */
1455: if ((flags & MSG_PEEK) && sbspace(&so->so_rcv) <= 0)
1456: break;
1457: /*
1458: * If we've drained the socket buffer, tell the
1459: * protocol in case it needs to do something to
1460: * get it filled again.
1461: */
1462: if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
1463: (*pr->pr_usrreq)(so, PRU_RCVD,
1.140 dyoung 1464: NULL, (struct mbuf *)(long)flags, NULL, l);
1.69 thorpej 1465: SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2");
1466: SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2");
1.1 cgd 1467: error = sbwait(&so->so_rcv);
1.144 dyoung 1468: if (error != 0) {
1.1 cgd 1469: sbunlock(&so->so_rcv);
1.160 ad 1470: sounlock(so);
1.1 cgd 1471: splx(s);
1.144 dyoung 1472: return 0;
1.1 cgd 1473: }
1.21 christos 1474: if ((m = so->so_rcv.sb_mb) != NULL)
1.1 cgd 1475: nextrecord = m->m_nextpkt;
1476: }
1477: }
1.3 andrew 1478:
1.146 dyoung 1479: if (m && atomic) {
1.3 andrew 1480: flags |= MSG_TRUNC;
1481: if ((flags & MSG_PEEK) == 0)
1482: (void) sbdroprecord(&so->so_rcv);
1483: }
1.1 cgd 1484: if ((flags & MSG_PEEK) == 0) {
1.144 dyoung 1485: if (m == NULL) {
1.69 thorpej 1486: /*
1.70 thorpej 1487: * First part is an inline SB_EMPTY_FIXUP(). Second
1.69 thorpej 1488: * part makes sure sb_lastrecord is up-to-date if
1489: * there is still data in the socket buffer.
1490: */
1.1 cgd 1491: so->so_rcv.sb_mb = nextrecord;
1.69 thorpej 1492: if (so->so_rcv.sb_mb == NULL) {
1493: so->so_rcv.sb_mbtail = NULL;
1494: so->so_rcv.sb_lastrecord = NULL;
1495: } else if (nextrecord->m_nextpkt == NULL)
1496: so->so_rcv.sb_lastrecord = nextrecord;
1497: }
1498: SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
1499: SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
1.1 cgd 1500: if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1.140 dyoung 1501: (*pr->pr_usrreq)(so, PRU_RCVD, NULL,
1502: (struct mbuf *)(long)flags, NULL, l);
1.1 cgd 1503: }
1.3 andrew 1504: if (orig_resid == uio->uio_resid && orig_resid &&
1505: (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
1506: sbunlock(&so->so_rcv);
1507: goto restart;
1508: }
1.108 perry 1509:
1.144 dyoung 1510: if (flagsp != NULL)
1.1 cgd 1511: *flagsp |= flags;
1.54 lukem 1512: release:
1.1 cgd 1513: sbunlock(&so->so_rcv);
1.160 ad 1514: sounlock(so);
1.1 cgd 1515: splx(s);
1.144 dyoung 1516: return error;
1.1 cgd 1517: }
1518:
1.14 mycroft 1519: int
1.54 lukem 1520: soshutdown(struct socket *so, int how)
1.1 cgd 1521: {
1.99 matt 1522: const struct protosw *pr;
1.160 ad 1523: int error;
1524:
1525: KASSERT(solocked(so));
1.34 kleink 1526:
1.54 lukem 1527: pr = so->so_proto;
1.34 kleink 1528: if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1529: return (EINVAL);
1.1 cgd 1530:
1.160 ad 1531: if (how == SHUT_RD || how == SHUT_RDWR) {
1.1 cgd 1532: sorflush(so);
1.160 ad 1533: error = 0;
1534: }
1.34 kleink 1535: if (how == SHUT_WR || how == SHUT_RDWR)
1.160 ad 1536: error = (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL,
1.140 dyoung 1537: NULL, NULL, NULL);
1.160 ad 1538:
1539: return error;
1.1 cgd 1540: }
1541:
1.14 mycroft 1542: void
1.54 lukem 1543: sorflush(struct socket *so)
1.1 cgd 1544: {
1.54 lukem 1545: struct sockbuf *sb, asb;
1.99 matt 1546: const struct protosw *pr;
1.160 ad 1547:
1548: KASSERT(solocked(so));
1.1 cgd 1549:
1.54 lukem 1550: sb = &so->so_rcv;
1551: pr = so->so_proto;
1.160 ad 1552: socantrcvmore(so);
1.1 cgd 1553: sb->sb_flags |= SB_NOINTR;
1.160 ad 1554: (void )sblock(sb, M_WAITOK);
1.1 cgd 1555: sbunlock(sb);
1556: asb = *sb;
1.86 wrstuden 1557: /*
1558: * Clear most of the sockbuf structure, but leave some of the
1559: * fields valid.
1560: */
1561: memset(&sb->sb_startzero, 0,
1562: sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
1.160 ad 1563: if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) {
1564: sounlock(so);
1.1 cgd 1565: (*pr->pr_domain->dom_dispose)(asb.sb_mb);
1.160 ad 1566: solock(so);
1567: }
1.98 christos 1568: sbrelease(&asb, so);
1.1 cgd 1569: }
1570:
1.171 plunky 1571: /*
1572: * internal set SOL_SOCKET options
1573: */
1.142 dyoung 1574: static int
1.171 plunky 1575: sosetopt1(struct socket *so, const struct sockopt *sopt)
1.1 cgd 1576: {
1.171 plunky 1577: int error, optval;
1578: struct linger l;
1579: struct timeval tv;
1.142 dyoung 1580:
1.171 plunky 1581: switch (sopt->sopt_name) {
1.142 dyoung 1582:
1.170 tls 1583: #ifdef INET
1584: case SO_ACCEPTFILTER:
1.171 plunky 1585: error = do_setopt_accept_filter(so, sopt);
1.170 tls 1586: if (error)
1587: return error;
1588: break;
1589: #endif
1590:
1.171 plunky 1591: case SO_LINGER:
1592: error = sockopt_get(sopt, &l, sizeof(l));
1593: if (error)
1594: return (error);
1595:
1596: if (l.l_linger < 0 || l.l_linger > USHRT_MAX ||
1597: l.l_linger > (INT_MAX / hz))
1.142 dyoung 1598: return EDOM;
1.171 plunky 1599: so->so_linger = l.l_linger;
1600: if (l.l_onoff)
1601: so->so_options |= SO_LINGER;
1602: else
1603: so->so_options &= ~SO_LINGER;
1604:
1605: break;
1.1 cgd 1606:
1.142 dyoung 1607: case SO_DEBUG:
1608: case SO_KEEPALIVE:
1609: case SO_DONTROUTE:
1610: case SO_USELOOPBACK:
1611: case SO_BROADCAST:
1612: case SO_REUSEADDR:
1613: case SO_REUSEPORT:
1614: case SO_OOBINLINE:
1615: case SO_TIMESTAMP:
1.171 plunky 1616: error = sockopt_getint(sopt, &optval);
1617: if (error)
1618: return (error);
1619:
1620: if (optval)
1621: so->so_options |= sopt->sopt_name;
1.142 dyoung 1622: else
1.171 plunky 1623: so->so_options &= ~sopt->sopt_name;
1.142 dyoung 1624: break;
1625:
1626: case SO_SNDBUF:
1627: case SO_RCVBUF:
1628: case SO_SNDLOWAT:
1629: case SO_RCVLOWAT:
1.171 plunky 1630: error = sockopt_getint(sopt, &optval);
1631: if (error)
1632: return (error);
1.1 cgd 1633:
1.142 dyoung 1634: /*
1635: * Values < 1 make no sense for any of these
1636: * options, so disallow them.
1637: */
1638: if (optval < 1)
1639: return EINVAL;
1.1 cgd 1640:
1.171 plunky 1641: switch (sopt->sopt_name) {
1642: case SO_SNDBUF:
1643: if (sbreserve(&so->so_snd, (u_long)optval, so) == 0)
1644: return ENOBUFS;
1645:
1646: so->so_snd.sb_flags &= ~SB_AUTOSIZE;
1647: break;
1.1 cgd 1648:
1649: case SO_RCVBUF:
1.171 plunky 1650: if (sbreserve(&so->so_rcv, (u_long)optval, so) == 0)
1.142 dyoung 1651: return ENOBUFS;
1.171 plunky 1652:
1653: so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1.142 dyoung 1654: break;
1655:
1656: /*
1657: * Make sure the low-water is never greater than
1658: * the high-water.
1659: */
1.1 cgd 1660: case SO_SNDLOWAT:
1.171 plunky 1661: if (optval > so->so_snd.sb_hiwat)
1662: optval = so->so_snd.sb_hiwat;
1663:
1664: so->so_snd.sb_lowat = optval;
1.142 dyoung 1665: break;
1.171 plunky 1666:
1.1 cgd 1667: case SO_RCVLOWAT:
1.171 plunky 1668: if (optval > so->so_rcv.sb_hiwat)
1669: optval = so->so_rcv.sb_hiwat;
1670:
1671: so->so_rcv.sb_lowat = optval;
1.142 dyoung 1672: break;
1673: }
1674: break;
1.28 thorpej 1675:
1.142 dyoung 1676: case SO_SNDTIMEO:
1677: case SO_RCVTIMEO:
1.171 plunky 1678: error = sockopt_get(sopt, &tv, sizeof(tv));
1679: if (error)
1680: return (error);
1681:
1682: if (tv.tv_sec > (INT_MAX - tv.tv_usec / tick) / hz)
1.142 dyoung 1683: return EDOM;
1.28 thorpej 1684:
1.171 plunky 1685: optval = tv.tv_sec * hz + tv.tv_usec / tick;
1686: if (optval == 0 && tv.tv_usec != 0)
1687: optval = 1;
1.28 thorpej 1688:
1.171 plunky 1689: switch (sopt->sopt_name) {
1.142 dyoung 1690: case SO_SNDTIMEO:
1.171 plunky 1691: so->so_snd.sb_timeo = optval;
1.1 cgd 1692: break;
1693: case SO_RCVTIMEO:
1.171 plunky 1694: so->so_rcv.sb_timeo = optval;
1.142 dyoung 1695: break;
1696: }
1697: break;
1.1 cgd 1698:
1.142 dyoung 1699: default:
1700: return ENOPROTOOPT;
1701: }
1702: return 0;
1703: }
1.1 cgd 1704:
1.142 dyoung 1705: int
1.171 plunky 1706: sosetopt(struct socket *so, struct sockopt *sopt)
1.142 dyoung 1707: {
1708: int error, prerr;
1.1 cgd 1709:
1.160 ad 1710: solock(so);
1.171 plunky 1711: if (sopt->sopt_level == SOL_SOCKET)
1712: error = sosetopt1(so, sopt);
1.142 dyoung 1713: else
1714: error = ENOPROTOOPT;
1.1 cgd 1715:
1.142 dyoung 1716: if ((error == 0 || error == ENOPROTOOPT) &&
1717: so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) {
1718: /* give the protocol stack a shot */
1.171 plunky 1719: prerr = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, sopt);
1.142 dyoung 1720: if (prerr == 0)
1721: error = 0;
1722: else if (prerr != ENOPROTOOPT)
1723: error = prerr;
1.171 plunky 1724: }
1.160 ad 1725: sounlock(so);
1.142 dyoung 1726: return error;
1.1 cgd 1727: }
1728:
1.171 plunky 1729: /*
1730: * so_setsockopt() is a wrapper providing a sockopt structure for sosetopt()
1731: */
1732: int
1733: so_setsockopt(struct lwp *l, struct socket *so, int level, int name,
1734: const void *val, size_t valsize)
1735: {
1736: struct sockopt sopt;
1737: int error;
1738:
1739: KASSERT(valsize == 0 || val != NULL);
1740:
1741: sockopt_init(&sopt, level, name, valsize);
1742: sockopt_set(&sopt, val, valsize);
1743:
1744: error = sosetopt(so, &sopt);
1745:
1746: sockopt_destroy(&sopt);
1747:
1748: return error;
1749: }
1750:
1751: /*
1752: * internal get SOL_SOCKET options
1753: */
1754: static int
1755: sogetopt1(struct socket *so, struct sockopt *sopt)
1756: {
1757: int error, optval;
1758: struct linger l;
1759: struct timeval tv;
1760:
1761: switch (sopt->sopt_name) {
1762:
1763: #ifdef INET
1764: case SO_ACCEPTFILTER:
1765: error = do_getopt_accept_filter(so, sopt);
1766: break;
1767: #endif
1768:
1769: case SO_LINGER:
1770: l.l_onoff = (so->so_options & SO_LINGER) ? 1 : 0;
1771: l.l_linger = so->so_linger;
1772:
1773: error = sockopt_set(sopt, &l, sizeof(l));
1774: break;
1775:
1776: case SO_USELOOPBACK:
1777: case SO_DONTROUTE:
1778: case SO_DEBUG:
1779: case SO_KEEPALIVE:
1780: case SO_REUSEADDR:
1781: case SO_REUSEPORT:
1782: case SO_BROADCAST:
1783: case SO_OOBINLINE:
1784: case SO_TIMESTAMP:
1785: error = sockopt_setint(sopt,
1786: (so->so_options & sopt->sopt_name) ? 1 : 0);
1787: break;
1788:
1789: case SO_TYPE:
1790: error = sockopt_setint(sopt, so->so_type);
1791: break;
1792:
1793: case SO_ERROR:
1794: error = sockopt_setint(sopt, so->so_error);
1795: so->so_error = 0;
1796: break;
1797:
1798: case SO_SNDBUF:
1799: error = sockopt_setint(sopt, so->so_snd.sb_hiwat);
1800: break;
1801:
1802: case SO_RCVBUF:
1803: error = sockopt_setint(sopt, so->so_rcv.sb_hiwat);
1804: break;
1805:
1806: case SO_SNDLOWAT:
1807: error = sockopt_setint(sopt, so->so_snd.sb_lowat);
1808: break;
1809:
1810: case SO_RCVLOWAT:
1811: error = sockopt_setint(sopt, so->so_rcv.sb_lowat);
1812: break;
1813:
1814: case SO_SNDTIMEO:
1815: case SO_RCVTIMEO:
1816: optval = (sopt->sopt_name == SO_SNDTIMEO ?
1817: so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1818:
1819: tv.tv_sec = optval / hz;
1820: tv.tv_usec = (optval % hz) * tick;
1821:
1822: error = sockopt_set(sopt, &tv, sizeof(tv));
1823: break;
1824:
1825: case SO_OVERFLOWED:
1826: error = sockopt_setint(sopt, so->so_rcv.sb_overflowed);
1827: break;
1828:
1829: default:
1830: error = ENOPROTOOPT;
1831: break;
1832: }
1833:
1834: return (error);
1835: }
1836:
1.14 mycroft 1837: int
1.171 plunky 1838: sogetopt(struct socket *so, struct sockopt *sopt)
1.1 cgd 1839: {
1.160 ad 1840: int error;
1.1 cgd 1841:
1.160 ad 1842: solock(so);
1.171 plunky 1843: if (sopt->sopt_level != SOL_SOCKET) {
1.1 cgd 1844: if (so->so_proto && so->so_proto->pr_ctloutput) {
1.160 ad 1845: error = ((*so->so_proto->pr_ctloutput)
1.171 plunky 1846: (PRCO_GETOPT, so, sopt));
1.1 cgd 1847: } else
1.160 ad 1848: error = (ENOPROTOOPT);
1.1 cgd 1849: } else {
1.171 plunky 1850: error = sogetopt1(so, sopt);
1851: }
1852: sounlock(so);
1853: return (error);
1854: }
1855:
1856: /*
1857: * alloc sockopt data buffer buffer
1858: * - will be released at destroy
1859: */
1860: static void
1861: sockopt_alloc(struct sockopt *sopt, size_t len)
1862: {
1863:
1864: KASSERT(sopt->sopt_size == 0);
1865:
1866: if (len > sizeof(sopt->sopt_buf))
1.173 plunky 1867: sopt->sopt_data = kmem_zalloc(len, KM_SLEEP);
1.171 plunky 1868: else
1869: sopt->sopt_data = sopt->sopt_buf;
1870:
1871: sopt->sopt_size = len;
1872: }
1873:
1874: /*
1875: * initialise sockopt storage
1876: */
1877: void
1878: sockopt_init(struct sockopt *sopt, int level, int name, size_t size)
1879: {
1.1 cgd 1880:
1.171 plunky 1881: memset(sopt, 0, sizeof(*sopt));
1.1 cgd 1882:
1.171 plunky 1883: sopt->sopt_level = level;
1884: sopt->sopt_name = name;
1885: sockopt_alloc(sopt, size);
1886: }
1887:
1888: /*
1889: * destroy sockopt storage
1890: * - will release any held memory references
1891: */
1892: void
1893: sockopt_destroy(struct sockopt *sopt)
1894: {
1895:
1896: if (sopt->sopt_data != sopt->sopt_buf)
1.173 plunky 1897: kmem_free(sopt->sopt_data, sopt->sopt_size);
1.171 plunky 1898:
1899: memset(sopt, 0, sizeof(*sopt));
1900: }
1901:
1902: /*
1903: * set sockopt value
1904: * - value is copied into sockopt
1905: * - memory is allocated when necessary
1906: */
1907: int
1908: sockopt_set(struct sockopt *sopt, const void *buf, size_t len)
1909: {
1910:
1911: if (sopt->sopt_size == 0)
1912: sockopt_alloc(sopt, len);
1913:
1914: KASSERT(sopt->sopt_size == len);
1915: memcpy(sopt->sopt_data, buf, len);
1916: return 0;
1917: }
1918:
1919: /*
1920: * common case of set sockopt integer value
1921: */
1922: int
1923: sockopt_setint(struct sockopt *sopt, int val)
1924: {
1925:
1926: return sockopt_set(sopt, &val, sizeof(int));
1927: }
1928:
1929: /*
1930: * get sockopt value
1931: * - correct size must be given
1932: */
1933: int
1934: sockopt_get(const struct sockopt *sopt, void *buf, size_t len)
1935: {
1.170 tls 1936:
1.171 plunky 1937: if (sopt->sopt_size != len)
1938: return EINVAL;
1.1 cgd 1939:
1.171 plunky 1940: memcpy(buf, sopt->sopt_data, len);
1941: return 0;
1942: }
1.1 cgd 1943:
1.171 plunky 1944: /*
1945: * common case of get sockopt integer value
1946: */
1947: int
1948: sockopt_getint(const struct sockopt *sopt, int *valp)
1949: {
1.1 cgd 1950:
1.171 plunky 1951: return sockopt_get(sopt, valp, sizeof(int));
1952: }
1.1 cgd 1953:
1.171 plunky 1954: /*
1955: * set sockopt value from mbuf
1956: * - ONLY for legacy code
1957: * - mbuf is released by sockopt
1958: */
1959: int
1960: sockopt_setmbuf(struct sockopt *sopt, struct mbuf *m)
1961: {
1962: size_t len;
1.1 cgd 1963:
1.171 plunky 1964: len = m_length(m);
1.1 cgd 1965:
1.171 plunky 1966: if (sopt->sopt_size == 0)
1967: sockopt_alloc(sopt, len);
1.1 cgd 1968:
1.171 plunky 1969: KASSERT(sopt->sopt_size == len);
1970: m_copydata(m, 0, len, sopt->sopt_data);
1971: m_freem(m);
1.1 cgd 1972:
1.171 plunky 1973: return 0;
1974: }
1.1 cgd 1975:
1.171 plunky 1976: /*
1977: * get sockopt value into mbuf
1978: * - ONLY for legacy code
1979: * - mbuf to be released by the caller
1980: */
1981: struct mbuf *
1982: sockopt_getmbuf(const struct sockopt *sopt)
1983: {
1984: struct mbuf *m;
1.107 darrenr 1985:
1.171 plunky 1986: m = m_get(M_WAIT, MT_SOOPTS);
1987: if (m == NULL)
1988: return NULL;
1989:
1990: m->m_len = MLEN;
1991: m_copyback(m, 0, sopt->sopt_size, sopt->sopt_data);
1992: if (m_length(m) != max(sopt->sopt_size, MLEN)) {
1993: m_freem(m);
1994: return NULL;
1.1 cgd 1995: }
1.171 plunky 1996: m->m_len = min(sopt->sopt_size, MLEN);
1.160 ad 1997:
1.171 plunky 1998: return m;
1.1 cgd 1999: }
2000:
1.14 mycroft 2001: void
1.54 lukem 2002: sohasoutofband(struct socket *so)
1.1 cgd 2003: {
1.153 rmind 2004:
1.90 christos 2005: fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so);
1.153 rmind 2006: selnotify(&so->so_rcv.sb_sel, POLLPRI | POLLRDBAND, 0);
1.1 cgd 2007: }
1.72 jdolecek 2008:
2009: static void
2010: filt_sordetach(struct knote *kn)
2011: {
2012: struct socket *so;
2013:
1.155 ad 2014: so = ((file_t *)kn->kn_obj)->f_data;
1.160 ad 2015: solock(so);
1.73 christos 2016: SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
2017: if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
1.72 jdolecek 2018: so->so_rcv.sb_flags &= ~SB_KNOTE;
1.160 ad 2019: sounlock(so);
1.72 jdolecek 2020: }
2021:
2022: /*ARGSUSED*/
2023: static int
1.129 yamt 2024: filt_soread(struct knote *kn, long hint)
1.72 jdolecek 2025: {
2026: struct socket *so;
1.160 ad 2027: int rv;
1.72 jdolecek 2028:
1.155 ad 2029: so = ((file_t *)kn->kn_obj)->f_data;
1.160 ad 2030: if (hint != NOTE_SUBMIT)
2031: solock(so);
1.72 jdolecek 2032: kn->kn_data = so->so_rcv.sb_cc;
2033: if (so->so_state & SS_CANTRCVMORE) {
1.108 perry 2034: kn->kn_flags |= EV_EOF;
1.72 jdolecek 2035: kn->kn_fflags = so->so_error;
1.160 ad 2036: rv = 1;
2037: } else if (so->so_error) /* temporary udp error */
2038: rv = 1;
2039: else if (kn->kn_sfflags & NOTE_LOWAT)
2040: rv = (kn->kn_data >= kn->kn_sdata);
2041: else
2042: rv = (kn->kn_data >= so->so_rcv.sb_lowat);
2043: if (hint != NOTE_SUBMIT)
2044: sounlock(so);
2045: return rv;
1.72 jdolecek 2046: }
2047:
2048: static void
2049: filt_sowdetach(struct knote *kn)
2050: {
2051: struct socket *so;
2052:
1.155 ad 2053: so = ((file_t *)kn->kn_obj)->f_data;
1.160 ad 2054: solock(so);
1.73 christos 2055: SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
2056: if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
1.72 jdolecek 2057: so->so_snd.sb_flags &= ~SB_KNOTE;
1.160 ad 2058: sounlock(so);
1.72 jdolecek 2059: }
2060:
2061: /*ARGSUSED*/
2062: static int
1.129 yamt 2063: filt_sowrite(struct knote *kn, long hint)
1.72 jdolecek 2064: {
2065: struct socket *so;
1.160 ad 2066: int rv;
1.72 jdolecek 2067:
1.155 ad 2068: so = ((file_t *)kn->kn_obj)->f_data;
1.160 ad 2069: if (hint != NOTE_SUBMIT)
2070: solock(so);
1.72 jdolecek 2071: kn->kn_data = sbspace(&so->so_snd);
2072: if (so->so_state & SS_CANTSENDMORE) {
1.108 perry 2073: kn->kn_flags |= EV_EOF;
1.72 jdolecek 2074: kn->kn_fflags = so->so_error;
1.160 ad 2075: rv = 1;
2076: } else if (so->so_error) /* temporary udp error */
2077: rv = 1;
2078: else if (((so->so_state & SS_ISCONNECTED) == 0) &&
1.72 jdolecek 2079: (so->so_proto->pr_flags & PR_CONNREQUIRED))
1.160 ad 2080: rv = 0;
2081: else if (kn->kn_sfflags & NOTE_LOWAT)
2082: rv = (kn->kn_data >= kn->kn_sdata);
2083: else
2084: rv = (kn->kn_data >= so->so_snd.sb_lowat);
2085: if (hint != NOTE_SUBMIT)
2086: sounlock(so);
2087: return rv;
1.72 jdolecek 2088: }
2089:
2090: /*ARGSUSED*/
2091: static int
1.129 yamt 2092: filt_solisten(struct knote *kn, long hint)
1.72 jdolecek 2093: {
2094: struct socket *so;
1.160 ad 2095: int rv;
1.72 jdolecek 2096:
1.155 ad 2097: so = ((file_t *)kn->kn_obj)->f_data;
1.72 jdolecek 2098:
2099: /*
2100: * Set kn_data to number of incoming connections, not
2101: * counting partial (incomplete) connections.
1.108 perry 2102: */
1.160 ad 2103: if (hint != NOTE_SUBMIT)
2104: solock(so);
1.72 jdolecek 2105: kn->kn_data = so->so_qlen;
1.160 ad 2106: rv = (kn->kn_data > 0);
2107: if (hint != NOTE_SUBMIT)
2108: sounlock(so);
2109: return rv;
1.72 jdolecek 2110: }
2111:
2112: static const struct filterops solisten_filtops =
2113: { 1, NULL, filt_sordetach, filt_solisten };
2114: static const struct filterops soread_filtops =
2115: { 1, NULL, filt_sordetach, filt_soread };
2116: static const struct filterops sowrite_filtops =
2117: { 1, NULL, filt_sowdetach, filt_sowrite };
2118:
2119: int
1.129 yamt 2120: soo_kqfilter(struct file *fp, struct knote *kn)
1.72 jdolecek 2121: {
2122: struct socket *so;
2123: struct sockbuf *sb;
2124:
1.155 ad 2125: so = ((file_t *)kn->kn_obj)->f_data;
1.160 ad 2126: solock(so);
1.72 jdolecek 2127: switch (kn->kn_filter) {
2128: case EVFILT_READ:
2129: if (so->so_options & SO_ACCEPTCONN)
2130: kn->kn_fop = &solisten_filtops;
2131: else
2132: kn->kn_fop = &soread_filtops;
2133: sb = &so->so_rcv;
2134: break;
2135: case EVFILT_WRITE:
2136: kn->kn_fop = &sowrite_filtops;
2137: sb = &so->so_snd;
2138: break;
2139: default:
1.160 ad 2140: sounlock(so);
1.149 pooka 2141: return (EINVAL);
1.72 jdolecek 2142: }
1.73 christos 2143: SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, kn, kn_selnext);
1.72 jdolecek 2144: sb->sb_flags |= SB_KNOTE;
1.160 ad 2145: sounlock(so);
1.72 jdolecek 2146: return (0);
2147: }
2148:
1.154 ad 2149: static int
2150: sodopoll(struct socket *so, int events)
2151: {
2152: int revents;
2153:
2154: revents = 0;
2155:
2156: if (events & (POLLIN | POLLRDNORM))
2157: if (soreadable(so))
2158: revents |= events & (POLLIN | POLLRDNORM);
2159:
2160: if (events & (POLLOUT | POLLWRNORM))
2161: if (sowritable(so))
2162: revents |= events & (POLLOUT | POLLWRNORM);
2163:
2164: if (events & (POLLPRI | POLLRDBAND))
2165: if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
2166: revents |= events & (POLLPRI | POLLRDBAND);
2167:
2168: return revents;
2169: }
2170:
2171: int
2172: sopoll(struct socket *so, int events)
2173: {
2174: int revents = 0;
2175:
1.160 ad 2176: #ifndef DIAGNOSTIC
2177: /*
2178: * Do a quick, unlocked check in expectation that the socket
2179: * will be ready for I/O. Don't do this check if DIAGNOSTIC,
2180: * as the solocked() assertions will fail.
2181: */
1.154 ad 2182: if ((revents = sodopoll(so, events)) != 0)
2183: return revents;
1.160 ad 2184: #endif
1.154 ad 2185:
1.160 ad 2186: solock(so);
1.154 ad 2187: if ((revents = sodopoll(so, events)) == 0) {
2188: if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
2189: selrecord(curlwp, &so->so_rcv.sb_sel);
1.160 ad 2190: so->so_rcv.sb_flags |= SB_NOTIFY;
1.154 ad 2191: }
2192:
2193: if (events & (POLLOUT | POLLWRNORM)) {
2194: selrecord(curlwp, &so->so_snd.sb_sel);
1.160 ad 2195: so->so_snd.sb_flags |= SB_NOTIFY;
1.154 ad 2196: }
2197: }
1.160 ad 2198: sounlock(so);
1.154 ad 2199:
2200: return revents;
2201: }
2202:
2203:
1.94 yamt 2204: #include <sys/sysctl.h>
2205:
2206: static int sysctl_kern_somaxkva(SYSCTLFN_PROTO);
2207:
2208: /*
2209: * sysctl helper routine for kern.somaxkva. ensures that the given
2210: * value is not too small.
2211: * (XXX should we maybe make sure it's not too large as well?)
2212: */
2213: static int
2214: sysctl_kern_somaxkva(SYSCTLFN_ARGS)
2215: {
2216: int error, new_somaxkva;
2217: struct sysctlnode node;
2218:
2219: new_somaxkva = somaxkva;
2220: node = *rnode;
2221: node.sysctl_data = &new_somaxkva;
2222: error = sysctl_lookup(SYSCTLFN_CALL(&node));
2223: if (error || newp == NULL)
2224: return (error);
2225:
2226: if (new_somaxkva < (16 * 1024 * 1024)) /* sanity */
2227: return (EINVAL);
2228:
1.136 ad 2229: mutex_enter(&so_pendfree_lock);
1.94 yamt 2230: somaxkva = new_somaxkva;
1.136 ad 2231: cv_broadcast(&socurkva_cv);
2232: mutex_exit(&so_pendfree_lock);
1.94 yamt 2233:
2234: return (error);
2235: }
2236:
2237: SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup")
2238: {
2239:
1.97 atatat 2240: sysctl_createv(clog, 0, NULL, NULL,
2241: CTLFLAG_PERMANENT,
2242: CTLTYPE_NODE, "kern", NULL,
2243: NULL, 0, NULL, 0,
2244: CTL_KERN, CTL_EOL);
2245:
2246: sysctl_createv(clog, 0, NULL, NULL,
2247: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.103 atatat 2248: CTLTYPE_INT, "somaxkva",
2249: SYSCTL_DESCR("Maximum amount of kernel memory to be "
2250: "used for socket buffers"),
1.94 yamt 2251: sysctl_kern_somaxkva, 0, NULL, 0,
2252: CTL_KERN, KERN_SOMAXKVA, CTL_EOL);
2253: }
CVSweb <webmaster@jp.NetBSD.org>