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