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