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