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