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