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