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