uipc_socket.c revision 1.174 1 /* $NetBSD: uipc_socket.c,v 1.174 2008/10/11 13:40:57 pooka Exp $ */
2
3 /*-
4 * Copyright (c) 2002, 2007, 2008 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 2004 The FreeBSD Foundation
34 * Copyright (c) 2004 Robert Watson
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95
63 */
64
65 #include <sys/cdefs.h>
66 __KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.174 2008/10/11 13:40:57 pooka Exp $");
67
68 #include "opt_inet.h"
69 #include "opt_sock_counters.h"
70 #include "opt_sosend_loan.h"
71 #include "opt_mbuftrace.h"
72 #include "opt_somaxkva.h"
73 #include "opt_multiprocessor.h" /* XXX */
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/proc.h>
78 #include <sys/file.h>
79 #include <sys/filedesc.h>
80 #include <sys/kmem.h>
81 #include <sys/mbuf.h>
82 #include <sys/domain.h>
83 #include <sys/kernel.h>
84 #include <sys/protosw.h>
85 #include <sys/socket.h>
86 #include <sys/socketvar.h>
87 #include <sys/signalvar.h>
88 #include <sys/resourcevar.h>
89 #include <sys/uidinfo.h>
90 #include <sys/event.h>
91 #include <sys/poll.h>
92 #include <sys/kauth.h>
93 #include <sys/mutex.h>
94 #include <sys/condvar.h>
95
96 #include <uvm/uvm.h>
97
98 MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options");
99 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
100
101 extern const struct fileops socketops;
102
103 extern int somaxconn; /* patchable (XXX sysctl) */
104 int somaxconn = SOMAXCONN;
105 kmutex_t *softnet_lock;
106
107 #ifdef SOSEND_COUNTERS
108 #include <sys/device.h>
109
110 static struct evcnt sosend_loan_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
111 NULL, "sosend", "loan big");
112 static struct evcnt sosend_copy_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
113 NULL, "sosend", "copy big");
114 static struct evcnt sosend_copy_small = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
115 NULL, "sosend", "copy small");
116 static struct evcnt sosend_kvalimit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
117 NULL, "sosend", "kva limit");
118
119 #define SOSEND_COUNTER_INCR(ev) (ev)->ev_count++
120
121 EVCNT_ATTACH_STATIC(sosend_loan_big);
122 EVCNT_ATTACH_STATIC(sosend_copy_big);
123 EVCNT_ATTACH_STATIC(sosend_copy_small);
124 EVCNT_ATTACH_STATIC(sosend_kvalimit);
125 #else
126
127 #define SOSEND_COUNTER_INCR(ev) /* nothing */
128
129 #endif /* SOSEND_COUNTERS */
130
131 static struct callback_entry sokva_reclaimerentry;
132
133 #if defined(SOSEND_NO_LOAN) || defined(MULTIPROCESSOR)
134 int sock_loan_thresh = -1;
135 #else
136 int sock_loan_thresh = 4096;
137 #endif
138
139 static kmutex_t so_pendfree_lock;
140 static struct mbuf *so_pendfree;
141
142 #ifndef SOMAXKVA
143 #define SOMAXKVA (16 * 1024 * 1024)
144 #endif
145 int somaxkva = SOMAXKVA;
146 static int socurkva;
147 static kcondvar_t socurkva_cv;
148
149 #define SOCK_LOAN_CHUNK 65536
150
151 static size_t sodopendfree(void);
152 static size_t sodopendfreel(void);
153
154 static vsize_t
155 sokvareserve(struct socket *so, vsize_t len)
156 {
157 int error;
158
159 mutex_enter(&so_pendfree_lock);
160 while (socurkva + len > somaxkva) {
161 size_t freed;
162
163 /*
164 * try to do pendfree.
165 */
166
167 freed = sodopendfreel();
168
169 /*
170 * if some kva was freed, try again.
171 */
172
173 if (freed)
174 continue;
175
176 SOSEND_COUNTER_INCR(&sosend_kvalimit);
177 error = cv_wait_sig(&socurkva_cv, &so_pendfree_lock);
178 if (error) {
179 len = 0;
180 break;
181 }
182 }
183 socurkva += len;
184 mutex_exit(&so_pendfree_lock);
185 return len;
186 }
187
188 static void
189 sokvaunreserve(vsize_t len)
190 {
191
192 mutex_enter(&so_pendfree_lock);
193 socurkva -= len;
194 cv_broadcast(&socurkva_cv);
195 mutex_exit(&so_pendfree_lock);
196 }
197
198 /*
199 * sokvaalloc: allocate kva for loan.
200 */
201
202 vaddr_t
203 sokvaalloc(vsize_t len, struct socket *so)
204 {
205 vaddr_t lva;
206
207 /*
208 * reserve kva.
209 */
210
211 if (sokvareserve(so, len) == 0)
212 return 0;
213
214 /*
215 * allocate kva.
216 */
217
218 lva = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
219 if (lva == 0) {
220 sokvaunreserve(len);
221 return (0);
222 }
223
224 return lva;
225 }
226
227 /*
228 * sokvafree: free kva for loan.
229 */
230
231 void
232 sokvafree(vaddr_t sva, vsize_t len)
233 {
234
235 /*
236 * free kva.
237 */
238
239 uvm_km_free(kernel_map, sva, len, UVM_KMF_VAONLY);
240
241 /*
242 * unreserve kva.
243 */
244
245 sokvaunreserve(len);
246 }
247
248 static void
249 sodoloanfree(struct vm_page **pgs, void *buf, size_t size)
250 {
251 vaddr_t sva, eva;
252 vsize_t len;
253 int npgs;
254
255 KASSERT(pgs != NULL);
256
257 eva = round_page((vaddr_t) buf + size);
258 sva = trunc_page((vaddr_t) buf);
259 len = eva - sva;
260 npgs = len >> PAGE_SHIFT;
261
262 pmap_kremove(sva, len);
263 pmap_update(pmap_kernel());
264 uvm_unloan(pgs, npgs, UVM_LOAN_TOPAGE);
265 sokvafree(sva, len);
266 }
267
268 static size_t
269 sodopendfree(void)
270 {
271 size_t rv;
272
273 if (__predict_true(so_pendfree == NULL))
274 return 0;
275
276 mutex_enter(&so_pendfree_lock);
277 rv = sodopendfreel();
278 mutex_exit(&so_pendfree_lock);
279
280 return rv;
281 }
282
283 /*
284 * sodopendfreel: free mbufs on "pendfree" list.
285 * unlock and relock so_pendfree_lock when freeing mbufs.
286 *
287 * => called with so_pendfree_lock held.
288 */
289
290 static size_t
291 sodopendfreel(void)
292 {
293 struct mbuf *m, *next;
294 size_t rv = 0;
295
296 KASSERT(mutex_owned(&so_pendfree_lock));
297
298 while (so_pendfree != NULL) {
299 m = so_pendfree;
300 so_pendfree = NULL;
301 mutex_exit(&so_pendfree_lock);
302
303 for (; m != NULL; m = next) {
304 next = m->m_next;
305 KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0);
306 KASSERT(m->m_ext.ext_refcnt == 0);
307
308 rv += m->m_ext.ext_size;
309 sodoloanfree(m->m_ext.ext_pgs, m->m_ext.ext_buf,
310 m->m_ext.ext_size);
311 pool_cache_put(mb_cache, m);
312 }
313
314 mutex_enter(&so_pendfree_lock);
315 }
316
317 return (rv);
318 }
319
320 void
321 soloanfree(struct mbuf *m, void *buf, size_t size, void *arg)
322 {
323
324 KASSERT(m != NULL);
325
326 /*
327 * postpone freeing mbuf.
328 *
329 * we can't do it in interrupt context
330 * because we need to put kva back to kernel_map.
331 */
332
333 mutex_enter(&so_pendfree_lock);
334 m->m_next = so_pendfree;
335 so_pendfree = m;
336 cv_broadcast(&socurkva_cv);
337 mutex_exit(&so_pendfree_lock);
338 }
339
340 static long
341 sosend_loan(struct socket *so, struct uio *uio, struct mbuf *m, long space)
342 {
343 struct iovec *iov = uio->uio_iov;
344 vaddr_t sva, eva;
345 vsize_t len;
346 vaddr_t lva;
347 int npgs, error;
348 vaddr_t va;
349 int i;
350
351 if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace))
352 return (0);
353
354 if (iov->iov_len < (size_t) space)
355 space = iov->iov_len;
356 if (space > SOCK_LOAN_CHUNK)
357 space = SOCK_LOAN_CHUNK;
358
359 eva = round_page((vaddr_t) iov->iov_base + space);
360 sva = trunc_page((vaddr_t) iov->iov_base);
361 len = eva - sva;
362 npgs = len >> PAGE_SHIFT;
363
364 KASSERT(npgs <= M_EXT_MAXPAGES);
365
366 lva = sokvaalloc(len, so);
367 if (lva == 0)
368 return 0;
369
370 error = uvm_loan(&uio->uio_vmspace->vm_map, sva, len,
371 m->m_ext.ext_pgs, UVM_LOAN_TOPAGE);
372 if (error) {
373 sokvafree(lva, len);
374 return (0);
375 }
376
377 for (i = 0, va = lva; i < npgs; i++, va += PAGE_SIZE)
378 pmap_kenter_pa(va, VM_PAGE_TO_PHYS(m->m_ext.ext_pgs[i]),
379 VM_PROT_READ);
380 pmap_update(pmap_kernel());
381
382 lva += (vaddr_t) iov->iov_base & PAGE_MASK;
383
384 MEXTADD(m, (void *) lva, space, M_MBUF, soloanfree, so);
385 m->m_flags |= M_EXT_PAGES | M_EXT_ROMAP;
386
387 uio->uio_resid -= space;
388 /* uio_offset not updated, not set/used for write(2) */
389 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + space;
390 uio->uio_iov->iov_len -= space;
391 if (uio->uio_iov->iov_len == 0) {
392 uio->uio_iov++;
393 uio->uio_iovcnt--;
394 }
395
396 return (space);
397 }
398
399 static int
400 sokva_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)
401 {
402
403 KASSERT(ce == &sokva_reclaimerentry);
404 KASSERT(obj == NULL);
405
406 sodopendfree();
407 if (!vm_map_starved_p(kernel_map)) {
408 return CALLBACK_CHAIN_ABORT;
409 }
410 return CALLBACK_CHAIN_CONTINUE;
411 }
412
413 struct mbuf *
414 getsombuf(struct socket *so, int type)
415 {
416 struct mbuf *m;
417
418 m = m_get(M_WAIT, type);
419 MCLAIM(m, so->so_mowner);
420 return m;
421 }
422
423 void
424 soinit(void)
425 {
426
427 mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM);
428 softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
429 cv_init(&socurkva_cv, "sokva");
430 soinit2();
431
432 /* Set the initial adjusted socket buffer size. */
433 if (sb_max_set(sb_max))
434 panic("bad initial sb_max value: %lu", sb_max);
435
436 callback_register(&vm_map_to_kernel(kernel_map)->vmk_reclaim_callback,
437 &sokva_reclaimerentry, NULL, sokva_reclaim_callback);
438 }
439
440 /*
441 * Socket operation routines.
442 * These routines are called by the routines in
443 * sys_socket.c or from a system process, and
444 * implement the semantics of socket operations by
445 * switching out to the protocol specific routines.
446 */
447 /*ARGSUSED*/
448 int
449 socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l,
450 struct socket *lockso)
451 {
452 const struct protosw *prp;
453 struct socket *so;
454 uid_t uid;
455 int error;
456 kmutex_t *lock;
457
458 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
459 KAUTH_REQ_NETWORK_SOCKET_OPEN, KAUTH_ARG(dom), KAUTH_ARG(type),
460 KAUTH_ARG(proto));
461 if (error != 0)
462 return error;
463
464 if (proto)
465 prp = pffindproto(dom, proto, type);
466 else
467 prp = pffindtype(dom, type);
468 if (prp == NULL) {
469 /* no support for domain */
470 if (pffinddomain(dom) == 0)
471 return EAFNOSUPPORT;
472 /* no support for socket type */
473 if (proto == 0 && type != 0)
474 return EPROTOTYPE;
475 return EPROTONOSUPPORT;
476 }
477 if (prp->pr_usrreq == NULL)
478 return EPROTONOSUPPORT;
479 if (prp->pr_type != type)
480 return EPROTOTYPE;
481
482 so = soget(true);
483 so->so_type = type;
484 so->so_proto = prp;
485 so->so_send = sosend;
486 so->so_receive = soreceive;
487 #ifdef MBUFTRACE
488 so->so_rcv.sb_mowner = &prp->pr_domain->dom_mowner;
489 so->so_snd.sb_mowner = &prp->pr_domain->dom_mowner;
490 so->so_mowner = &prp->pr_domain->dom_mowner;
491 #endif
492 uid = kauth_cred_geteuid(l->l_cred);
493 so->so_uidinfo = uid_find(uid);
494 so->so_egid = kauth_cred_getegid(l->l_cred);
495 so->so_cpid = l->l_proc->p_pid;
496 if (lockso != NULL) {
497 /* Caller wants us to share a lock. */
498 lock = lockso->so_lock;
499 so->so_lock = lock;
500 mutex_obj_hold(lock);
501 mutex_enter(lock);
502 } else {
503 /* Lock assigned and taken during PRU_ATTACH. */
504 }
505 error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL,
506 (struct mbuf *)(long)proto, NULL, l);
507 KASSERT(solocked(so));
508 if (error != 0) {
509 so->so_state |= SS_NOFDREF;
510 sofree(so);
511 return error;
512 }
513 sounlock(so);
514 *aso = so;
515 return 0;
516 }
517
518 /* On success, write file descriptor to fdout and return zero. On
519 * failure, return non-zero; *fdout will be undefined.
520 */
521 int
522 fsocreate(int domain, struct socket **sop, int type, int protocol,
523 struct lwp *l, int *fdout)
524 {
525 struct socket *so;
526 struct file *fp;
527 int fd, error;
528
529 if ((error = fd_allocfile(&fp, &fd)) != 0)
530 return (error);
531 fp->f_flag = FREAD|FWRITE;
532 fp->f_type = DTYPE_SOCKET;
533 fp->f_ops = &socketops;
534 error = socreate(domain, &so, type, protocol, l, NULL);
535 if (error != 0) {
536 fd_abort(curproc, fp, fd);
537 } else {
538 if (sop != NULL)
539 *sop = so;
540 fp->f_data = so;
541 fd_affix(curproc, fp, fd);
542 *fdout = fd;
543 }
544 return error;
545 }
546
547 int
548 sobind(struct socket *so, struct mbuf *nam, struct lwp *l)
549 {
550 int error;
551
552 solock(so);
553 error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, l);
554 sounlock(so);
555 return error;
556 }
557
558 int
559 solisten(struct socket *so, int backlog, struct lwp *l)
560 {
561 int error;
562
563 solock(so);
564 if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
565 SS_ISDISCONNECTING)) != 0) {
566 sounlock(so);
567 return (EOPNOTSUPP);
568 }
569 error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL,
570 NULL, NULL, l);
571 if (error != 0) {
572 sounlock(so);
573 return error;
574 }
575 if (TAILQ_EMPTY(&so->so_q))
576 so->so_options |= SO_ACCEPTCONN;
577 if (backlog < 0)
578 backlog = 0;
579 so->so_qlimit = min(backlog, somaxconn);
580 sounlock(so);
581 return 0;
582 }
583
584 void
585 sofree(struct socket *so)
586 {
587 u_int refs;
588
589 KASSERT(solocked(so));
590
591 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) {
592 sounlock(so);
593 return;
594 }
595 if (so->so_head) {
596 /*
597 * We must not decommission a socket that's on the accept(2)
598 * queue. If we do, then accept(2) may hang after select(2)
599 * indicated that the listening socket was ready.
600 */
601 if (!soqremque(so, 0)) {
602 sounlock(so);
603 return;
604 }
605 }
606 if (so->so_rcv.sb_hiwat)
607 (void)chgsbsize(so->so_uidinfo, &so->so_rcv.sb_hiwat, 0,
608 RLIM_INFINITY);
609 if (so->so_snd.sb_hiwat)
610 (void)chgsbsize(so->so_uidinfo, &so->so_snd.sb_hiwat, 0,
611 RLIM_INFINITY);
612 sbrelease(&so->so_snd, so);
613 KASSERT(!cv_has_waiters(&so->so_cv));
614 KASSERT(!cv_has_waiters(&so->so_rcv.sb_cv));
615 KASSERT(!cv_has_waiters(&so->so_snd.sb_cv));
616 sorflush(so);
617 refs = so->so_aborting; /* XXX */
618 #ifdef INET
619 /* remove acccept filter if one is present. */
620 if (so->so_accf != NULL)
621 do_setopt_accept_filter(so, NULL);
622 #endif
623 sounlock(so);
624 if (refs == 0) /* XXX */
625 soput(so);
626 }
627
628 /*
629 * Close a socket on last file table reference removal.
630 * Initiate disconnect if connected.
631 * Free socket when disconnect complete.
632 */
633 int
634 soclose(struct socket *so)
635 {
636 struct socket *so2;
637 int error;
638 int error2;
639
640 error = 0;
641 solock(so);
642 if (so->so_options & SO_ACCEPTCONN) {
643 for (;;) {
644 if ((so2 = TAILQ_FIRST(&so->so_q0)) != 0) {
645 KASSERT(solocked2(so, so2));
646 (void) soqremque(so2, 0);
647 /* soabort drops the lock. */
648 (void) soabort(so2);
649 solock(so);
650 continue;
651 }
652 if ((so2 = TAILQ_FIRST(&so->so_q)) != 0) {
653 KASSERT(solocked2(so, so2));
654 (void) soqremque(so2, 1);
655 /* soabort drops the lock. */
656 (void) soabort(so2);
657 solock(so);
658 continue;
659 }
660 break;
661 }
662 }
663 if (so->so_pcb == 0)
664 goto discard;
665 if (so->so_state & SS_ISCONNECTED) {
666 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
667 error = sodisconnect(so);
668 if (error)
669 goto drop;
670 }
671 if (so->so_options & SO_LINGER) {
672 if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio)
673 goto drop;
674 while (so->so_state & SS_ISCONNECTED) {
675 error = sowait(so, so->so_linger * hz);
676 if (error)
677 break;
678 }
679 }
680 }
681 drop:
682 if (so->so_pcb) {
683 error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
684 NULL, NULL, NULL, NULL);
685 if (error == 0)
686 error = error2;
687 }
688 discard:
689 if (so->so_state & SS_NOFDREF)
690 panic("soclose: NOFDREF");
691 so->so_state |= SS_NOFDREF;
692 sofree(so);
693 return (error);
694 }
695
696 /*
697 * Must be called with the socket locked.. Will return with it unlocked.
698 */
699 int
700 soabort(struct socket *so)
701 {
702 u_int refs;
703 int error;
704
705 KASSERT(solocked(so));
706 KASSERT(so->so_head == NULL);
707
708 so->so_aborting++; /* XXX */
709 error = (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL,
710 NULL, NULL, NULL);
711 refs = --so->so_aborting; /* XXX */
712 if (error || (refs == 0)) {
713 sofree(so);
714 } else {
715 sounlock(so);
716 }
717 return error;
718 }
719
720 int
721 soaccept(struct socket *so, struct mbuf *nam)
722 {
723 int error;
724
725 KASSERT(solocked(so));
726
727 error = 0;
728 if ((so->so_state & SS_NOFDREF) == 0)
729 panic("soaccept: !NOFDREF");
730 so->so_state &= ~SS_NOFDREF;
731 if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
732 (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
733 error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
734 NULL, nam, NULL, NULL);
735 else
736 error = ECONNABORTED;
737
738 return (error);
739 }
740
741 int
742 soconnect(struct socket *so, struct mbuf *nam, struct lwp *l)
743 {
744 int error;
745
746 KASSERT(solocked(so));
747
748 if (so->so_options & SO_ACCEPTCONN)
749 return (EOPNOTSUPP);
750 /*
751 * If protocol is connection-based, can only connect once.
752 * Otherwise, if connected, try to disconnect first.
753 * This allows user to disconnect by connecting to, e.g.,
754 * a null address.
755 */
756 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
757 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
758 (error = sodisconnect(so))))
759 error = EISCONN;
760 else
761 error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
762 NULL, nam, NULL, l);
763 return (error);
764 }
765
766 int
767 soconnect2(struct socket *so1, struct socket *so2)
768 {
769 int error;
770
771 KASSERT(solocked2(so1, so2));
772
773 error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
774 NULL, (struct mbuf *)so2, NULL, NULL);
775 return (error);
776 }
777
778 int
779 sodisconnect(struct socket *so)
780 {
781 int error;
782
783 KASSERT(solocked(so));
784
785 if ((so->so_state & SS_ISCONNECTED) == 0) {
786 error = ENOTCONN;
787 } else if (so->so_state & SS_ISDISCONNECTING) {
788 error = EALREADY;
789 } else {
790 error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
791 NULL, NULL, NULL, NULL);
792 }
793 sodopendfree();
794 return (error);
795 }
796
797 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
798 /*
799 * Send on a socket.
800 * If send must go all at once and message is larger than
801 * send buffering, then hard error.
802 * Lock against other senders.
803 * If must go all at once and not enough room now, then
804 * inform user that this would block and do nothing.
805 * Otherwise, if nonblocking, send as much as possible.
806 * The data to be sent is described by "uio" if nonzero,
807 * otherwise by the mbuf chain "top" (which must be null
808 * if uio is not). Data provided in mbuf chain must be small
809 * enough to send all at once.
810 *
811 * Returns nonzero on error, timeout or signal; callers
812 * must check for short counts if EINTR/ERESTART are returned.
813 * Data and control buffers are freed on return.
814 */
815 int
816 sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
817 struct mbuf *control, int flags, struct lwp *l)
818 {
819 struct mbuf **mp, *m;
820 struct proc *p;
821 long space, len, resid, clen, mlen;
822 int error, s, dontroute, atomic;
823
824 p = l->l_proc;
825 sodopendfree();
826 clen = 0;
827
828 /*
829 * solock() provides atomicity of access. splsoftnet() prevents
830 * protocol processing soft interrupts from interrupting us and
831 * blocking (expensive).
832 */
833 s = splsoftnet();
834 solock(so);
835 atomic = sosendallatonce(so) || top;
836 if (uio)
837 resid = uio->uio_resid;
838 else
839 resid = top->m_pkthdr.len;
840 /*
841 * In theory resid should be unsigned.
842 * However, space must be signed, as it might be less than 0
843 * if we over-committed, and we must use a signed comparison
844 * of space and resid. On the other hand, a negative resid
845 * causes us to loop sending 0-length segments to the protocol.
846 */
847 if (resid < 0) {
848 error = EINVAL;
849 goto out;
850 }
851 dontroute =
852 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
853 (so->so_proto->pr_flags & PR_ATOMIC);
854 l->l_ru.ru_msgsnd++;
855 if (control)
856 clen = control->m_len;
857 restart:
858 if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
859 goto out;
860 do {
861 if (so->so_state & SS_CANTSENDMORE) {
862 error = EPIPE;
863 goto release;
864 }
865 if (so->so_error) {
866 error = so->so_error;
867 so->so_error = 0;
868 goto release;
869 }
870 if ((so->so_state & SS_ISCONNECTED) == 0) {
871 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
872 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
873 !(resid == 0 && clen != 0)) {
874 error = ENOTCONN;
875 goto release;
876 }
877 } else if (addr == 0) {
878 error = EDESTADDRREQ;
879 goto release;
880 }
881 }
882 space = sbspace(&so->so_snd);
883 if (flags & MSG_OOB)
884 space += 1024;
885 if ((atomic && resid > so->so_snd.sb_hiwat) ||
886 clen > so->so_snd.sb_hiwat) {
887 error = EMSGSIZE;
888 goto release;
889 }
890 if (space < resid + clen &&
891 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
892 if (so->so_nbio) {
893 error = EWOULDBLOCK;
894 goto release;
895 }
896 sbunlock(&so->so_snd);
897 error = sbwait(&so->so_snd);
898 if (error)
899 goto out;
900 goto restart;
901 }
902 mp = ⊤
903 space -= clen;
904 do {
905 if (uio == NULL) {
906 /*
907 * Data is prepackaged in "top".
908 */
909 resid = 0;
910 if (flags & MSG_EOR)
911 top->m_flags |= M_EOR;
912 } else do {
913 sounlock(so);
914 splx(s);
915 if (top == NULL) {
916 m = m_gethdr(M_WAIT, MT_DATA);
917 mlen = MHLEN;
918 m->m_pkthdr.len = 0;
919 m->m_pkthdr.rcvif = NULL;
920 } else {
921 m = m_get(M_WAIT, MT_DATA);
922 mlen = MLEN;
923 }
924 MCLAIM(m, so->so_snd.sb_mowner);
925 if (sock_loan_thresh >= 0 &&
926 uio->uio_iov->iov_len >= sock_loan_thresh &&
927 space >= sock_loan_thresh &&
928 (len = sosend_loan(so, uio, m,
929 space)) != 0) {
930 SOSEND_COUNTER_INCR(&sosend_loan_big);
931 space -= len;
932 goto have_data;
933 }
934 if (resid >= MINCLSIZE && space >= MCLBYTES) {
935 SOSEND_COUNTER_INCR(&sosend_copy_big);
936 m_clget(m, M_WAIT);
937 if ((m->m_flags & M_EXT) == 0)
938 goto nopages;
939 mlen = MCLBYTES;
940 if (atomic && top == 0) {
941 len = lmin(MCLBYTES - max_hdr,
942 resid);
943 m->m_data += max_hdr;
944 } else
945 len = lmin(MCLBYTES, resid);
946 space -= len;
947 } else {
948 nopages:
949 SOSEND_COUNTER_INCR(&sosend_copy_small);
950 len = lmin(lmin(mlen, resid), space);
951 space -= len;
952 /*
953 * For datagram protocols, leave room
954 * for protocol headers in first mbuf.
955 */
956 if (atomic && top == 0 && len < mlen)
957 MH_ALIGN(m, len);
958 }
959 error = uiomove(mtod(m, void *), (int)len, uio);
960 have_data:
961 resid = uio->uio_resid;
962 m->m_len = len;
963 *mp = m;
964 top->m_pkthdr.len += len;
965 s = splsoftnet();
966 solock(so);
967 if (error != 0)
968 goto release;
969 mp = &m->m_next;
970 if (resid <= 0) {
971 if (flags & MSG_EOR)
972 top->m_flags |= M_EOR;
973 break;
974 }
975 } while (space > 0 && atomic);
976
977 if (so->so_state & SS_CANTSENDMORE) {
978 error = EPIPE;
979 goto release;
980 }
981 if (dontroute)
982 so->so_options |= SO_DONTROUTE;
983 if (resid > 0)
984 so->so_state |= SS_MORETOCOME;
985 error = (*so->so_proto->pr_usrreq)(so,
986 (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
987 top, addr, control, curlwp);
988 if (dontroute)
989 so->so_options &= ~SO_DONTROUTE;
990 if (resid > 0)
991 so->so_state &= ~SS_MORETOCOME;
992 clen = 0;
993 control = NULL;
994 top = NULL;
995 mp = ⊤
996 if (error != 0)
997 goto release;
998 } while (resid && space > 0);
999 } while (resid);
1000
1001 release:
1002 sbunlock(&so->so_snd);
1003 out:
1004 sounlock(so);
1005 splx(s);
1006 if (top)
1007 m_freem(top);
1008 if (control)
1009 m_freem(control);
1010 return (error);
1011 }
1012
1013 /*
1014 * Following replacement or removal of the first mbuf on the first
1015 * mbuf chain of a socket buffer, push necessary state changes back
1016 * into the socket buffer so that other consumers see the values
1017 * consistently. 'nextrecord' is the callers locally stored value of
1018 * the original value of sb->sb_mb->m_nextpkt which must be restored
1019 * when the lead mbuf changes. NOTE: 'nextrecord' may be NULL.
1020 */
1021 static void
1022 sbsync(struct sockbuf *sb, struct mbuf *nextrecord)
1023 {
1024
1025 KASSERT(solocked(sb->sb_so));
1026
1027 /*
1028 * First, update for the new value of nextrecord. If necessary,
1029 * make it the first record.
1030 */
1031 if (sb->sb_mb != NULL)
1032 sb->sb_mb->m_nextpkt = nextrecord;
1033 else
1034 sb->sb_mb = nextrecord;
1035
1036 /*
1037 * Now update any dependent socket buffer fields to reflect
1038 * the new state. This is an inline of SB_EMPTY_FIXUP, with
1039 * the addition of a second clause that takes care of the
1040 * case where sb_mb has been updated, but remains the last
1041 * record.
1042 */
1043 if (sb->sb_mb == NULL) {
1044 sb->sb_mbtail = NULL;
1045 sb->sb_lastrecord = NULL;
1046 } else if (sb->sb_mb->m_nextpkt == NULL)
1047 sb->sb_lastrecord = sb->sb_mb;
1048 }
1049
1050 /*
1051 * Implement receive operations on a socket.
1052 * We depend on the way that records are added to the sockbuf
1053 * by sbappend*. In particular, each record (mbufs linked through m_next)
1054 * must begin with an address if the protocol so specifies,
1055 * followed by an optional mbuf or mbufs containing ancillary data,
1056 * and then zero or more mbufs of data.
1057 * In order to avoid blocking network interrupts for the entire time here,
1058 * we splx() while doing the actual copy to user space.
1059 * Although the sockbuf is locked, new data may still be appended,
1060 * and thus we must maintain consistency of the sockbuf during that time.
1061 *
1062 * The caller may receive the data as a single mbuf chain by supplying
1063 * an mbuf **mp0 for use in returning the chain. The uio is then used
1064 * only for the count in uio_resid.
1065 */
1066 int
1067 soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
1068 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1069 {
1070 struct lwp *l = curlwp;
1071 struct mbuf *m, **mp, *mt;
1072 int atomic, flags, len, error, s, offset, moff, type, orig_resid;
1073 const struct protosw *pr;
1074 struct mbuf *nextrecord;
1075 int mbuf_removed = 0;
1076 const struct domain *dom;
1077
1078 pr = so->so_proto;
1079 atomic = pr->pr_flags & PR_ATOMIC;
1080 dom = pr->pr_domain;
1081 mp = mp0;
1082 type = 0;
1083 orig_resid = uio->uio_resid;
1084
1085 if (paddr != NULL)
1086 *paddr = NULL;
1087 if (controlp != NULL)
1088 *controlp = NULL;
1089 if (flagsp != NULL)
1090 flags = *flagsp &~ MSG_EOR;
1091 else
1092 flags = 0;
1093
1094 if ((flags & MSG_DONTWAIT) == 0)
1095 sodopendfree();
1096
1097 if (flags & MSG_OOB) {
1098 m = m_get(M_WAIT, MT_DATA);
1099 solock(so);
1100 error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
1101 (struct mbuf *)(long)(flags & MSG_PEEK), NULL, l);
1102 sounlock(so);
1103 if (error)
1104 goto bad;
1105 do {
1106 error = uiomove(mtod(m, void *),
1107 (int) min(uio->uio_resid, m->m_len), uio);
1108 m = m_free(m);
1109 } while (uio->uio_resid > 0 && error == 0 && m);
1110 bad:
1111 if (m != NULL)
1112 m_freem(m);
1113 return error;
1114 }
1115 if (mp != NULL)
1116 *mp = NULL;
1117
1118 /*
1119 * solock() provides atomicity of access. splsoftnet() prevents
1120 * protocol processing soft interrupts from interrupting us and
1121 * blocking (expensive).
1122 */
1123 s = splsoftnet();
1124 solock(so);
1125 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
1126 (*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l);
1127
1128 restart:
1129 if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) {
1130 sounlock(so);
1131 splx(s);
1132 return error;
1133 }
1134
1135 m = so->so_rcv.sb_mb;
1136 /*
1137 * If we have less data than requested, block awaiting more
1138 * (subject to any timeout) if:
1139 * 1. the current count is less than the low water mark,
1140 * 2. MSG_WAITALL is set, and it is possible to do the entire
1141 * receive operation at once if we block (resid <= hiwat), or
1142 * 3. MSG_DONTWAIT is not set.
1143 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1144 * we have to do the receive in sections, and thus risk returning
1145 * a short count if a timeout or signal occurs after we start.
1146 */
1147 if (m == NULL ||
1148 ((flags & MSG_DONTWAIT) == 0 &&
1149 so->so_rcv.sb_cc < uio->uio_resid &&
1150 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
1151 ((flags & MSG_WAITALL) &&
1152 uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1153 m->m_nextpkt == NULL && !atomic)) {
1154 #ifdef DIAGNOSTIC
1155 if (m == NULL && so->so_rcv.sb_cc)
1156 panic("receive 1");
1157 #endif
1158 if (so->so_error) {
1159 if (m != NULL)
1160 goto dontblock;
1161 error = so->so_error;
1162 if ((flags & MSG_PEEK) == 0)
1163 so->so_error = 0;
1164 goto release;
1165 }
1166 if (so->so_state & SS_CANTRCVMORE) {
1167 if (m != NULL)
1168 goto dontblock;
1169 else
1170 goto release;
1171 }
1172 for (; m != NULL; m = m->m_next)
1173 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1174 m = so->so_rcv.sb_mb;
1175 goto dontblock;
1176 }
1177 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1178 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1179 error = ENOTCONN;
1180 goto release;
1181 }
1182 if (uio->uio_resid == 0)
1183 goto release;
1184 if (so->so_nbio || (flags & MSG_DONTWAIT)) {
1185 error = EWOULDBLOCK;
1186 goto release;
1187 }
1188 SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1");
1189 SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1");
1190 sbunlock(&so->so_rcv);
1191 error = sbwait(&so->so_rcv);
1192 if (error != 0) {
1193 sounlock(so);
1194 splx(s);
1195 return error;
1196 }
1197 goto restart;
1198 }
1199 dontblock:
1200 /*
1201 * On entry here, m points to the first record of the socket buffer.
1202 * From this point onward, we maintain 'nextrecord' as a cache of the
1203 * pointer to the next record in the socket buffer. We must keep the
1204 * various socket buffer pointers and local stack versions of the
1205 * pointers in sync, pushing out modifications before dropping the
1206 * socket lock, and re-reading them when picking it up.
1207 *
1208 * Otherwise, we will race with the network stack appending new data
1209 * or records onto the socket buffer by using inconsistent/stale
1210 * versions of the field, possibly resulting in socket buffer
1211 * corruption.
1212 *
1213 * By holding the high-level sblock(), we prevent simultaneous
1214 * readers from pulling off the front of the socket buffer.
1215 */
1216 if (l != NULL)
1217 l->l_ru.ru_msgrcv++;
1218 KASSERT(m == so->so_rcv.sb_mb);
1219 SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
1220 SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
1221 nextrecord = m->m_nextpkt;
1222 if (pr->pr_flags & PR_ADDR) {
1223 #ifdef DIAGNOSTIC
1224 if (m->m_type != MT_SONAME)
1225 panic("receive 1a");
1226 #endif
1227 orig_resid = 0;
1228 if (flags & MSG_PEEK) {
1229 if (paddr)
1230 *paddr = m_copy(m, 0, m->m_len);
1231 m = m->m_next;
1232 } else {
1233 sbfree(&so->so_rcv, m);
1234 mbuf_removed = 1;
1235 if (paddr != NULL) {
1236 *paddr = m;
1237 so->so_rcv.sb_mb = m->m_next;
1238 m->m_next = NULL;
1239 m = so->so_rcv.sb_mb;
1240 } else {
1241 MFREE(m, so->so_rcv.sb_mb);
1242 m = so->so_rcv.sb_mb;
1243 }
1244 sbsync(&so->so_rcv, nextrecord);
1245 }
1246 }
1247
1248 /*
1249 * Process one or more MT_CONTROL mbufs present before any data mbufs
1250 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1251 * just copy the data; if !MSG_PEEK, we call into the protocol to
1252 * perform externalization (or freeing if controlp == NULL).
1253 */
1254 if (__predict_false(m != NULL && m->m_type == MT_CONTROL)) {
1255 struct mbuf *cm = NULL, *cmn;
1256 struct mbuf **cme = &cm;
1257
1258 do {
1259 if (flags & MSG_PEEK) {
1260 if (controlp != NULL) {
1261 *controlp = m_copy(m, 0, m->m_len);
1262 controlp = &(*controlp)->m_next;
1263 }
1264 m = m->m_next;
1265 } else {
1266 sbfree(&so->so_rcv, m);
1267 so->so_rcv.sb_mb = m->m_next;
1268 m->m_next = NULL;
1269 *cme = m;
1270 cme = &(*cme)->m_next;
1271 m = so->so_rcv.sb_mb;
1272 }
1273 } while (m != NULL && m->m_type == MT_CONTROL);
1274 if ((flags & MSG_PEEK) == 0)
1275 sbsync(&so->so_rcv, nextrecord);
1276 for (; cm != NULL; cm = cmn) {
1277 cmn = cm->m_next;
1278 cm->m_next = NULL;
1279 type = mtod(cm, struct cmsghdr *)->cmsg_type;
1280 if (controlp != NULL) {
1281 if (dom->dom_externalize != NULL &&
1282 type == SCM_RIGHTS) {
1283 sounlock(so);
1284 splx(s);
1285 error = (*dom->dom_externalize)(cm, l);
1286 s = splsoftnet();
1287 solock(so);
1288 }
1289 *controlp = cm;
1290 while (*controlp != NULL)
1291 controlp = &(*controlp)->m_next;
1292 } else {
1293 /*
1294 * Dispose of any SCM_RIGHTS message that went
1295 * through the read path rather than recv.
1296 */
1297 if (dom->dom_dispose != NULL &&
1298 type == SCM_RIGHTS) {
1299 sounlock(so);
1300 (*dom->dom_dispose)(cm);
1301 solock(so);
1302 }
1303 m_freem(cm);
1304 }
1305 }
1306 if (m != NULL)
1307 nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1308 else
1309 nextrecord = so->so_rcv.sb_mb;
1310 orig_resid = 0;
1311 }
1312
1313 /* If m is non-NULL, we have some data to read. */
1314 if (__predict_true(m != NULL)) {
1315 type = m->m_type;
1316 if (type == MT_OOBDATA)
1317 flags |= MSG_OOB;
1318 }
1319 SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
1320 SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
1321
1322 moff = 0;
1323 offset = 0;
1324 while (m != NULL && uio->uio_resid > 0 && error == 0) {
1325 if (m->m_type == MT_OOBDATA) {
1326 if (type != MT_OOBDATA)
1327 break;
1328 } else if (type == MT_OOBDATA)
1329 break;
1330 #ifdef DIAGNOSTIC
1331 else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
1332 panic("receive 3");
1333 #endif
1334 so->so_state &= ~SS_RCVATMARK;
1335 len = uio->uio_resid;
1336 if (so->so_oobmark && len > so->so_oobmark - offset)
1337 len = so->so_oobmark - offset;
1338 if (len > m->m_len - moff)
1339 len = m->m_len - moff;
1340 /*
1341 * If mp is set, just pass back the mbufs.
1342 * Otherwise copy them out via the uio, then free.
1343 * Sockbuf must be consistent here (points to current mbuf,
1344 * it points to next record) when we drop priority;
1345 * we must note any additions to the sockbuf when we
1346 * block interrupts again.
1347 */
1348 if (mp == NULL) {
1349 SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove");
1350 SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove");
1351 sounlock(so);
1352 splx(s);
1353 error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1354 s = splsoftnet();
1355 solock(so);
1356 if (error != 0) {
1357 /*
1358 * If any part of the record has been removed
1359 * (such as the MT_SONAME mbuf, which will
1360 * happen when PR_ADDR, and thus also
1361 * PR_ATOMIC, is set), then drop the entire
1362 * record to maintain the atomicity of the
1363 * receive operation.
1364 *
1365 * This avoids a later panic("receive 1a")
1366 * when compiled with DIAGNOSTIC.
1367 */
1368 if (m && mbuf_removed && atomic)
1369 (void) sbdroprecord(&so->so_rcv);
1370
1371 goto release;
1372 }
1373 } else
1374 uio->uio_resid -= len;
1375 if (len == m->m_len - moff) {
1376 if (m->m_flags & M_EOR)
1377 flags |= MSG_EOR;
1378 if (flags & MSG_PEEK) {
1379 m = m->m_next;
1380 moff = 0;
1381 } else {
1382 nextrecord = m->m_nextpkt;
1383 sbfree(&so->so_rcv, m);
1384 if (mp) {
1385 *mp = m;
1386 mp = &m->m_next;
1387 so->so_rcv.sb_mb = m = m->m_next;
1388 *mp = NULL;
1389 } else {
1390 MFREE(m, so->so_rcv.sb_mb);
1391 m = so->so_rcv.sb_mb;
1392 }
1393 /*
1394 * If m != NULL, we also know that
1395 * so->so_rcv.sb_mb != NULL.
1396 */
1397 KASSERT(so->so_rcv.sb_mb == m);
1398 if (m) {
1399 m->m_nextpkt = nextrecord;
1400 if (nextrecord == NULL)
1401 so->so_rcv.sb_lastrecord = m;
1402 } else {
1403 so->so_rcv.sb_mb = nextrecord;
1404 SB_EMPTY_FIXUP(&so->so_rcv);
1405 }
1406 SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
1407 SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
1408 }
1409 } else if (flags & MSG_PEEK)
1410 moff += len;
1411 else {
1412 if (mp != NULL) {
1413 mt = m_copym(m, 0, len, M_NOWAIT);
1414 if (__predict_false(mt == NULL)) {
1415 sounlock(so);
1416 mt = m_copym(m, 0, len, M_WAIT);
1417 solock(so);
1418 }
1419 *mp = mt;
1420 }
1421 m->m_data += len;
1422 m->m_len -= len;
1423 so->so_rcv.sb_cc -= len;
1424 }
1425 if (so->so_oobmark) {
1426 if ((flags & MSG_PEEK) == 0) {
1427 so->so_oobmark -= len;
1428 if (so->so_oobmark == 0) {
1429 so->so_state |= SS_RCVATMARK;
1430 break;
1431 }
1432 } else {
1433 offset += len;
1434 if (offset == so->so_oobmark)
1435 break;
1436 }
1437 }
1438 if (flags & MSG_EOR)
1439 break;
1440 /*
1441 * If the MSG_WAITALL flag is set (for non-atomic socket),
1442 * we must not quit until "uio->uio_resid == 0" or an error
1443 * termination. If a signal/timeout occurs, return
1444 * with a short count but without error.
1445 * Keep sockbuf locked against other readers.
1446 */
1447 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1448 !sosendallatonce(so) && !nextrecord) {
1449 if (so->so_error || so->so_state & SS_CANTRCVMORE)
1450 break;
1451 /*
1452 * If we are peeking and the socket receive buffer is
1453 * full, stop since we can't get more data to peek at.
1454 */
1455 if ((flags & MSG_PEEK) && sbspace(&so->so_rcv) <= 0)
1456 break;
1457 /*
1458 * If we've drained the socket buffer, tell the
1459 * protocol in case it needs to do something to
1460 * get it filled again.
1461 */
1462 if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
1463 (*pr->pr_usrreq)(so, PRU_RCVD,
1464 NULL, (struct mbuf *)(long)flags, NULL, l);
1465 SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2");
1466 SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2");
1467 error = sbwait(&so->so_rcv);
1468 if (error != 0) {
1469 sbunlock(&so->so_rcv);
1470 sounlock(so);
1471 splx(s);
1472 return 0;
1473 }
1474 if ((m = so->so_rcv.sb_mb) != NULL)
1475 nextrecord = m->m_nextpkt;
1476 }
1477 }
1478
1479 if (m && atomic) {
1480 flags |= MSG_TRUNC;
1481 if ((flags & MSG_PEEK) == 0)
1482 (void) sbdroprecord(&so->so_rcv);
1483 }
1484 if ((flags & MSG_PEEK) == 0) {
1485 if (m == NULL) {
1486 /*
1487 * First part is an inline SB_EMPTY_FIXUP(). Second
1488 * part makes sure sb_lastrecord is up-to-date if
1489 * there is still data in the socket buffer.
1490 */
1491 so->so_rcv.sb_mb = nextrecord;
1492 if (so->so_rcv.sb_mb == NULL) {
1493 so->so_rcv.sb_mbtail = NULL;
1494 so->so_rcv.sb_lastrecord = NULL;
1495 } else if (nextrecord->m_nextpkt == NULL)
1496 so->so_rcv.sb_lastrecord = nextrecord;
1497 }
1498 SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
1499 SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
1500 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1501 (*pr->pr_usrreq)(so, PRU_RCVD, NULL,
1502 (struct mbuf *)(long)flags, NULL, l);
1503 }
1504 if (orig_resid == uio->uio_resid && orig_resid &&
1505 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
1506 sbunlock(&so->so_rcv);
1507 goto restart;
1508 }
1509
1510 if (flagsp != NULL)
1511 *flagsp |= flags;
1512 release:
1513 sbunlock(&so->so_rcv);
1514 sounlock(so);
1515 splx(s);
1516 return error;
1517 }
1518
1519 int
1520 soshutdown(struct socket *so, int how)
1521 {
1522 const struct protosw *pr;
1523 int error;
1524
1525 KASSERT(solocked(so));
1526
1527 pr = so->so_proto;
1528 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1529 return (EINVAL);
1530
1531 if (how == SHUT_RD || how == SHUT_RDWR) {
1532 sorflush(so);
1533 error = 0;
1534 }
1535 if (how == SHUT_WR || how == SHUT_RDWR)
1536 error = (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL,
1537 NULL, NULL, NULL);
1538
1539 return error;
1540 }
1541
1542 void
1543 sorflush(struct socket *so)
1544 {
1545 struct sockbuf *sb, asb;
1546 const struct protosw *pr;
1547
1548 KASSERT(solocked(so));
1549
1550 sb = &so->so_rcv;
1551 pr = so->so_proto;
1552 socantrcvmore(so);
1553 sb->sb_flags |= SB_NOINTR;
1554 (void )sblock(sb, M_WAITOK);
1555 sbunlock(sb);
1556 asb = *sb;
1557 /*
1558 * Clear most of the sockbuf structure, but leave some of the
1559 * fields valid.
1560 */
1561 memset(&sb->sb_startzero, 0,
1562 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
1563 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) {
1564 sounlock(so);
1565 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
1566 solock(so);
1567 }
1568 sbrelease(&asb, so);
1569 }
1570
1571 /*
1572 * internal set SOL_SOCKET options
1573 */
1574 static int
1575 sosetopt1(struct socket *so, const struct sockopt *sopt)
1576 {
1577 int error, optval;
1578 struct linger l;
1579 struct timeval tv;
1580
1581 switch (sopt->sopt_name) {
1582
1583 #ifdef INET
1584 case SO_ACCEPTFILTER:
1585 error = do_setopt_accept_filter(so, sopt);
1586 if (error)
1587 return error;
1588 break;
1589 #endif
1590
1591 case SO_LINGER:
1592 error = sockopt_get(sopt, &l, sizeof(l));
1593 if (error)
1594 return (error);
1595
1596 if (l.l_linger < 0 || l.l_linger > USHRT_MAX ||
1597 l.l_linger > (INT_MAX / hz))
1598 return EDOM;
1599 so->so_linger = l.l_linger;
1600 if (l.l_onoff)
1601 so->so_options |= SO_LINGER;
1602 else
1603 so->so_options &= ~SO_LINGER;
1604
1605 break;
1606
1607 case SO_DEBUG:
1608 case SO_KEEPALIVE:
1609 case SO_DONTROUTE:
1610 case SO_USELOOPBACK:
1611 case SO_BROADCAST:
1612 case SO_REUSEADDR:
1613 case SO_REUSEPORT:
1614 case SO_OOBINLINE:
1615 case SO_TIMESTAMP:
1616 error = sockopt_getint(sopt, &optval);
1617 if (error)
1618 return (error);
1619
1620 if (optval)
1621 so->so_options |= sopt->sopt_name;
1622 else
1623 so->so_options &= ~sopt->sopt_name;
1624 break;
1625
1626 case SO_SNDBUF:
1627 case SO_RCVBUF:
1628 case SO_SNDLOWAT:
1629 case SO_RCVLOWAT:
1630 error = sockopt_getint(sopt, &optval);
1631 if (error)
1632 return (error);
1633
1634 /*
1635 * Values < 1 make no sense for any of these
1636 * options, so disallow them.
1637 */
1638 if (optval < 1)
1639 return EINVAL;
1640
1641 switch (sopt->sopt_name) {
1642 case SO_SNDBUF:
1643 if (sbreserve(&so->so_snd, (u_long)optval, so) == 0)
1644 return ENOBUFS;
1645
1646 so->so_snd.sb_flags &= ~SB_AUTOSIZE;
1647 break;
1648
1649 case SO_RCVBUF:
1650 if (sbreserve(&so->so_rcv, (u_long)optval, so) == 0)
1651 return ENOBUFS;
1652
1653 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1654 break;
1655
1656 /*
1657 * Make sure the low-water is never greater than
1658 * the high-water.
1659 */
1660 case SO_SNDLOWAT:
1661 if (optval > so->so_snd.sb_hiwat)
1662 optval = so->so_snd.sb_hiwat;
1663
1664 so->so_snd.sb_lowat = optval;
1665 break;
1666
1667 case SO_RCVLOWAT:
1668 if (optval > so->so_rcv.sb_hiwat)
1669 optval = so->so_rcv.sb_hiwat;
1670
1671 so->so_rcv.sb_lowat = optval;
1672 break;
1673 }
1674 break;
1675
1676 case SO_SNDTIMEO:
1677 case SO_RCVTIMEO:
1678 error = sockopt_get(sopt, &tv, sizeof(tv));
1679 if (error)
1680 return (error);
1681
1682 if (tv.tv_sec > (INT_MAX - tv.tv_usec / tick) / hz)
1683 return EDOM;
1684
1685 optval = tv.tv_sec * hz + tv.tv_usec / tick;
1686 if (optval == 0 && tv.tv_usec != 0)
1687 optval = 1;
1688
1689 switch (sopt->sopt_name) {
1690 case SO_SNDTIMEO:
1691 so->so_snd.sb_timeo = optval;
1692 break;
1693 case SO_RCVTIMEO:
1694 so->so_rcv.sb_timeo = optval;
1695 break;
1696 }
1697 break;
1698
1699 default:
1700 return ENOPROTOOPT;
1701 }
1702 return 0;
1703 }
1704
1705 int
1706 sosetopt(struct socket *so, struct sockopt *sopt)
1707 {
1708 int error, prerr;
1709
1710 solock(so);
1711 if (sopt->sopt_level == SOL_SOCKET)
1712 error = sosetopt1(so, sopt);
1713 else
1714 error = ENOPROTOOPT;
1715
1716 if ((error == 0 || error == ENOPROTOOPT) &&
1717 so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) {
1718 /* give the protocol stack a shot */
1719 prerr = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, sopt);
1720 if (prerr == 0)
1721 error = 0;
1722 else if (prerr != ENOPROTOOPT)
1723 error = prerr;
1724 }
1725 sounlock(so);
1726 return error;
1727 }
1728
1729 /*
1730 * so_setsockopt() is a wrapper providing a sockopt structure for sosetopt()
1731 */
1732 int
1733 so_setsockopt(struct lwp *l, struct socket *so, int level, int name,
1734 const void *val, size_t valsize)
1735 {
1736 struct sockopt sopt;
1737 int error;
1738
1739 KASSERT(valsize == 0 || val != NULL);
1740
1741 sockopt_init(&sopt, level, name, valsize);
1742 sockopt_set(&sopt, val, valsize);
1743
1744 error = sosetopt(so, &sopt);
1745
1746 sockopt_destroy(&sopt);
1747
1748 return error;
1749 }
1750
1751 /*
1752 * internal get SOL_SOCKET options
1753 */
1754 static int
1755 sogetopt1(struct socket *so, struct sockopt *sopt)
1756 {
1757 int error, optval;
1758 struct linger l;
1759 struct timeval tv;
1760
1761 switch (sopt->sopt_name) {
1762
1763 #ifdef INET
1764 case SO_ACCEPTFILTER:
1765 error = do_getopt_accept_filter(so, sopt);
1766 break;
1767 #endif
1768
1769 case SO_LINGER:
1770 l.l_onoff = (so->so_options & SO_LINGER) ? 1 : 0;
1771 l.l_linger = so->so_linger;
1772
1773 error = sockopt_set(sopt, &l, sizeof(l));
1774 break;
1775
1776 case SO_USELOOPBACK:
1777 case SO_DONTROUTE:
1778 case SO_DEBUG:
1779 case SO_KEEPALIVE:
1780 case SO_REUSEADDR:
1781 case SO_REUSEPORT:
1782 case SO_BROADCAST:
1783 case SO_OOBINLINE:
1784 case SO_TIMESTAMP:
1785 error = sockopt_setint(sopt,
1786 (so->so_options & sopt->sopt_name) ? 1 : 0);
1787 break;
1788
1789 case SO_TYPE:
1790 error = sockopt_setint(sopt, so->so_type);
1791 break;
1792
1793 case SO_ERROR:
1794 error = sockopt_setint(sopt, so->so_error);
1795 so->so_error = 0;
1796 break;
1797
1798 case SO_SNDBUF:
1799 error = sockopt_setint(sopt, so->so_snd.sb_hiwat);
1800 break;
1801
1802 case SO_RCVBUF:
1803 error = sockopt_setint(sopt, so->so_rcv.sb_hiwat);
1804 break;
1805
1806 case SO_SNDLOWAT:
1807 error = sockopt_setint(sopt, so->so_snd.sb_lowat);
1808 break;
1809
1810 case SO_RCVLOWAT:
1811 error = sockopt_setint(sopt, so->so_rcv.sb_lowat);
1812 break;
1813
1814 case SO_SNDTIMEO:
1815 case SO_RCVTIMEO:
1816 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1817 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1818
1819 tv.tv_sec = optval / hz;
1820 tv.tv_usec = (optval % hz) * tick;
1821
1822 error = sockopt_set(sopt, &tv, sizeof(tv));
1823 break;
1824
1825 case SO_OVERFLOWED:
1826 error = sockopt_setint(sopt, so->so_rcv.sb_overflowed);
1827 break;
1828
1829 default:
1830 error = ENOPROTOOPT;
1831 break;
1832 }
1833
1834 return (error);
1835 }
1836
1837 int
1838 sogetopt(struct socket *so, struct sockopt *sopt)
1839 {
1840 int error;
1841
1842 solock(so);
1843 if (sopt->sopt_level != SOL_SOCKET) {
1844 if (so->so_proto && so->so_proto->pr_ctloutput) {
1845 error = ((*so->so_proto->pr_ctloutput)
1846 (PRCO_GETOPT, so, sopt));
1847 } else
1848 error = (ENOPROTOOPT);
1849 } else {
1850 error = sogetopt1(so, sopt);
1851 }
1852 sounlock(so);
1853 return (error);
1854 }
1855
1856 /*
1857 * alloc sockopt data buffer buffer
1858 * - will be released at destroy
1859 */
1860 static void
1861 sockopt_alloc(struct sockopt *sopt, size_t len)
1862 {
1863
1864 KASSERT(sopt->sopt_size == 0);
1865
1866 if (len > sizeof(sopt->sopt_buf))
1867 sopt->sopt_data = kmem_zalloc(len, KM_SLEEP);
1868 else
1869 sopt->sopt_data = sopt->sopt_buf;
1870
1871 sopt->sopt_size = len;
1872 }
1873
1874 /*
1875 * initialise sockopt storage
1876 */
1877 void
1878 sockopt_init(struct sockopt *sopt, int level, int name, size_t size)
1879 {
1880
1881 memset(sopt, 0, sizeof(*sopt));
1882
1883 sopt->sopt_level = level;
1884 sopt->sopt_name = name;
1885 sockopt_alloc(sopt, size);
1886 }
1887
1888 /*
1889 * destroy sockopt storage
1890 * - will release any held memory references
1891 */
1892 void
1893 sockopt_destroy(struct sockopt *sopt)
1894 {
1895
1896 if (sopt->sopt_data != sopt->sopt_buf)
1897 kmem_free(sopt->sopt_data, sopt->sopt_size);
1898
1899 memset(sopt, 0, sizeof(*sopt));
1900 }
1901
1902 /*
1903 * set sockopt value
1904 * - value is copied into sockopt
1905 * - memory is allocated when necessary
1906 */
1907 int
1908 sockopt_set(struct sockopt *sopt, const void *buf, size_t len)
1909 {
1910
1911 if (sopt->sopt_size == 0)
1912 sockopt_alloc(sopt, len);
1913
1914 KASSERT(sopt->sopt_size == len);
1915 memcpy(sopt->sopt_data, buf, len);
1916 return 0;
1917 }
1918
1919 /*
1920 * common case of set sockopt integer value
1921 */
1922 int
1923 sockopt_setint(struct sockopt *sopt, int val)
1924 {
1925
1926 return sockopt_set(sopt, &val, sizeof(int));
1927 }
1928
1929 /*
1930 * get sockopt value
1931 * - correct size must be given
1932 */
1933 int
1934 sockopt_get(const struct sockopt *sopt, void *buf, size_t len)
1935 {
1936
1937 if (sopt->sopt_size != len)
1938 return EINVAL;
1939
1940 memcpy(buf, sopt->sopt_data, len);
1941 return 0;
1942 }
1943
1944 /*
1945 * common case of get sockopt integer value
1946 */
1947 int
1948 sockopt_getint(const struct sockopt *sopt, int *valp)
1949 {
1950
1951 return sockopt_get(sopt, valp, sizeof(int));
1952 }
1953
1954 /*
1955 * set sockopt value from mbuf
1956 * - ONLY for legacy code
1957 * - mbuf is released by sockopt
1958 */
1959 int
1960 sockopt_setmbuf(struct sockopt *sopt, struct mbuf *m)
1961 {
1962 size_t len;
1963
1964 len = m_length(m);
1965
1966 if (sopt->sopt_size == 0)
1967 sockopt_alloc(sopt, len);
1968
1969 KASSERT(sopt->sopt_size == len);
1970 m_copydata(m, 0, len, sopt->sopt_data);
1971 m_freem(m);
1972
1973 return 0;
1974 }
1975
1976 /*
1977 * get sockopt value into mbuf
1978 * - ONLY for legacy code
1979 * - mbuf to be released by the caller
1980 */
1981 struct mbuf *
1982 sockopt_getmbuf(const struct sockopt *sopt)
1983 {
1984 struct mbuf *m;
1985
1986 m = m_get(M_WAIT, MT_SOOPTS);
1987 if (m == NULL)
1988 return NULL;
1989
1990 m->m_len = MLEN;
1991 m_copyback(m, 0, sopt->sopt_size, sopt->sopt_data);
1992 if (m_length(m) != max(sopt->sopt_size, MLEN)) {
1993 m_freem(m);
1994 return NULL;
1995 }
1996 m->m_len = min(sopt->sopt_size, MLEN);
1997
1998 return m;
1999 }
2000
2001 void
2002 sohasoutofband(struct socket *so)
2003 {
2004
2005 fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so);
2006 selnotify(&so->so_rcv.sb_sel, POLLPRI | POLLRDBAND, 0);
2007 }
2008
2009 static void
2010 filt_sordetach(struct knote *kn)
2011 {
2012 struct socket *so;
2013
2014 so = ((file_t *)kn->kn_obj)->f_data;
2015 solock(so);
2016 SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
2017 if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
2018 so->so_rcv.sb_flags &= ~SB_KNOTE;
2019 sounlock(so);
2020 }
2021
2022 /*ARGSUSED*/
2023 static int
2024 filt_soread(struct knote *kn, long hint)
2025 {
2026 struct socket *so;
2027 int rv;
2028
2029 so = ((file_t *)kn->kn_obj)->f_data;
2030 if (hint != NOTE_SUBMIT)
2031 solock(so);
2032 kn->kn_data = so->so_rcv.sb_cc;
2033 if (so->so_state & SS_CANTRCVMORE) {
2034 kn->kn_flags |= EV_EOF;
2035 kn->kn_fflags = so->so_error;
2036 rv = 1;
2037 } else if (so->so_error) /* temporary udp error */
2038 rv = 1;
2039 else if (kn->kn_sfflags & NOTE_LOWAT)
2040 rv = (kn->kn_data >= kn->kn_sdata);
2041 else
2042 rv = (kn->kn_data >= so->so_rcv.sb_lowat);
2043 if (hint != NOTE_SUBMIT)
2044 sounlock(so);
2045 return rv;
2046 }
2047
2048 static void
2049 filt_sowdetach(struct knote *kn)
2050 {
2051 struct socket *so;
2052
2053 so = ((file_t *)kn->kn_obj)->f_data;
2054 solock(so);
2055 SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
2056 if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
2057 so->so_snd.sb_flags &= ~SB_KNOTE;
2058 sounlock(so);
2059 }
2060
2061 /*ARGSUSED*/
2062 static int
2063 filt_sowrite(struct knote *kn, long hint)
2064 {
2065 struct socket *so;
2066 int rv;
2067
2068 so = ((file_t *)kn->kn_obj)->f_data;
2069 if (hint != NOTE_SUBMIT)
2070 solock(so);
2071 kn->kn_data = sbspace(&so->so_snd);
2072 if (so->so_state & SS_CANTSENDMORE) {
2073 kn->kn_flags |= EV_EOF;
2074 kn->kn_fflags = so->so_error;
2075 rv = 1;
2076 } else if (so->so_error) /* temporary udp error */
2077 rv = 1;
2078 else if (((so->so_state & SS_ISCONNECTED) == 0) &&
2079 (so->so_proto->pr_flags & PR_CONNREQUIRED))
2080 rv = 0;
2081 else if (kn->kn_sfflags & NOTE_LOWAT)
2082 rv = (kn->kn_data >= kn->kn_sdata);
2083 else
2084 rv = (kn->kn_data >= so->so_snd.sb_lowat);
2085 if (hint != NOTE_SUBMIT)
2086 sounlock(so);
2087 return rv;
2088 }
2089
2090 /*ARGSUSED*/
2091 static int
2092 filt_solisten(struct knote *kn, long hint)
2093 {
2094 struct socket *so;
2095 int rv;
2096
2097 so = ((file_t *)kn->kn_obj)->f_data;
2098
2099 /*
2100 * Set kn_data to number of incoming connections, not
2101 * counting partial (incomplete) connections.
2102 */
2103 if (hint != NOTE_SUBMIT)
2104 solock(so);
2105 kn->kn_data = so->so_qlen;
2106 rv = (kn->kn_data > 0);
2107 if (hint != NOTE_SUBMIT)
2108 sounlock(so);
2109 return rv;
2110 }
2111
2112 static const struct filterops solisten_filtops =
2113 { 1, NULL, filt_sordetach, filt_solisten };
2114 static const struct filterops soread_filtops =
2115 { 1, NULL, filt_sordetach, filt_soread };
2116 static const struct filterops sowrite_filtops =
2117 { 1, NULL, filt_sowdetach, filt_sowrite };
2118
2119 int
2120 soo_kqfilter(struct file *fp, struct knote *kn)
2121 {
2122 struct socket *so;
2123 struct sockbuf *sb;
2124
2125 so = ((file_t *)kn->kn_obj)->f_data;
2126 solock(so);
2127 switch (kn->kn_filter) {
2128 case EVFILT_READ:
2129 if (so->so_options & SO_ACCEPTCONN)
2130 kn->kn_fop = &solisten_filtops;
2131 else
2132 kn->kn_fop = &soread_filtops;
2133 sb = &so->so_rcv;
2134 break;
2135 case EVFILT_WRITE:
2136 kn->kn_fop = &sowrite_filtops;
2137 sb = &so->so_snd;
2138 break;
2139 default:
2140 sounlock(so);
2141 return (EINVAL);
2142 }
2143 SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, kn, kn_selnext);
2144 sb->sb_flags |= SB_KNOTE;
2145 sounlock(so);
2146 return (0);
2147 }
2148
2149 static int
2150 sodopoll(struct socket *so, int events)
2151 {
2152 int revents;
2153
2154 revents = 0;
2155
2156 if (events & (POLLIN | POLLRDNORM))
2157 if (soreadable(so))
2158 revents |= events & (POLLIN | POLLRDNORM);
2159
2160 if (events & (POLLOUT | POLLWRNORM))
2161 if (sowritable(so))
2162 revents |= events & (POLLOUT | POLLWRNORM);
2163
2164 if (events & (POLLPRI | POLLRDBAND))
2165 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
2166 revents |= events & (POLLPRI | POLLRDBAND);
2167
2168 return revents;
2169 }
2170
2171 int
2172 sopoll(struct socket *so, int events)
2173 {
2174 int revents = 0;
2175
2176 #ifndef DIAGNOSTIC
2177 /*
2178 * Do a quick, unlocked check in expectation that the socket
2179 * will be ready for I/O. Don't do this check if DIAGNOSTIC,
2180 * as the solocked() assertions will fail.
2181 */
2182 if ((revents = sodopoll(so, events)) != 0)
2183 return revents;
2184 #endif
2185
2186 solock(so);
2187 if ((revents = sodopoll(so, events)) == 0) {
2188 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
2189 selrecord(curlwp, &so->so_rcv.sb_sel);
2190 so->so_rcv.sb_flags |= SB_NOTIFY;
2191 }
2192
2193 if (events & (POLLOUT | POLLWRNORM)) {
2194 selrecord(curlwp, &so->so_snd.sb_sel);
2195 so->so_snd.sb_flags |= SB_NOTIFY;
2196 }
2197 }
2198 sounlock(so);
2199
2200 return revents;
2201 }
2202
2203
2204 #include <sys/sysctl.h>
2205
2206 static int sysctl_kern_somaxkva(SYSCTLFN_PROTO);
2207
2208 /*
2209 * sysctl helper routine for kern.somaxkva. ensures that the given
2210 * value is not too small.
2211 * (XXX should we maybe make sure it's not too large as well?)
2212 */
2213 static int
2214 sysctl_kern_somaxkva(SYSCTLFN_ARGS)
2215 {
2216 int error, new_somaxkva;
2217 struct sysctlnode node;
2218
2219 new_somaxkva = somaxkva;
2220 node = *rnode;
2221 node.sysctl_data = &new_somaxkva;
2222 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2223 if (error || newp == NULL)
2224 return (error);
2225
2226 if (new_somaxkva < (16 * 1024 * 1024)) /* sanity */
2227 return (EINVAL);
2228
2229 mutex_enter(&so_pendfree_lock);
2230 somaxkva = new_somaxkva;
2231 cv_broadcast(&socurkva_cv);
2232 mutex_exit(&so_pendfree_lock);
2233
2234 return (error);
2235 }
2236
2237 SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup")
2238 {
2239
2240 sysctl_createv(clog, 0, NULL, NULL,
2241 CTLFLAG_PERMANENT,
2242 CTLTYPE_NODE, "kern", NULL,
2243 NULL, 0, NULL, 0,
2244 CTL_KERN, CTL_EOL);
2245
2246 sysctl_createv(clog, 0, NULL, NULL,
2247 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2248 CTLTYPE_INT, "somaxkva",
2249 SYSCTL_DESCR("Maximum amount of kernel memory to be "
2250 "used for socket buffers"),
2251 sysctl_kern_somaxkva, 0, NULL, 0,
2252 CTL_KERN, KERN_SOMAXKVA, CTL_EOL);
2253 }
2254