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