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