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