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