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