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