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