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