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