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