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