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