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