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