1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
26 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 /*
28 * Copyright (c) 1982, 1986, 1988, 1990, 1993
29 * The Regents of the University of California. All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
60 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.16 2001/06/14 20:46:06 ume Exp $
61 */
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/filedesc.h>
66 #include <sys/proc.h>
67 #include <sys/file.h>
68 #include <sys/fcntl.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/domain.h>
72 #include <sys/kernel.h>
73 #include <sys/event.h>
74 #include <sys/poll.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/resourcevar.h>
79 #include <sys/signalvar.h>
80 #include <sys/sysctl.h>
81 #include <sys/uio.h>
82 #include <sys/ev.h>
83 #include <sys/kdebug.h>
84 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_pcb.h>
87 #include <kern/zalloc.h>
88 #include <machine/limits.h>
89
90 int so_cache_hw = 0;
91 int so_cache_timeouts = 0;
92 int so_cache_max_freed = 0;
93 int cached_sock_count = 0;
94 struct socket *socket_cache_head = 0;
95 struct socket *socket_cache_tail = 0;
96 u_long so_cache_time = 0;
97 int so_cache_init_done = 0;
98 struct zone *so_cache_zone;
99 extern int get_inpcb_str_size();
100 extern int get_tcp_str_size();
101
102 #include <machine/limits.h>
103
104 static void filt_sordetach(struct knote *kn);
105 static int filt_soread(struct knote *kn, long hint);
106 static void filt_sowdetach(struct knote *kn);
107 static int filt_sowrite(struct knote *kn, long hint);
108 static int filt_solisten(struct knote *kn, long hint);
109
110 static struct filterops solisten_filtops =
111 { 1, NULL, filt_sordetach, filt_solisten };
112 static struct filterops soread_filtops =
113 { 1, NULL, filt_sordetach, filt_soread };
114 static struct filterops sowrite_filtops =
115 { 1, NULL, filt_sowdetach, filt_sowrite };
116
117 int socket_debug = 0;
118 int socket_zone = M_SOCKET;
119 so_gen_t so_gencnt; /* generation count for sockets */
120
121 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
122 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
123
124 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
125 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
126 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
127 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
128 #define DBG_FNC_SOSEND NETDBG_CODE(DBG_NETSOCK, (4 << 8) | 1)
129 #define DBG_FNC_SORECEIVE NETDBG_CODE(DBG_NETSOCK, (8 << 8))
130 #define DBG_FNC_SOSHUTDOWN NETDBG_CODE(DBG_NETSOCK, (9 << 8))
131
132
133 SYSCTL_DECL(_kern_ipc);
134
135 static int somaxconn = SOMAXCONN;
136 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW, &somaxconn,
137 0, "");
138
139 /* Should we get a maximum also ??? */
140 static int sosendmaxchain = 65536;
141 static int sosendminchain = 16384;
142 static int sorecvmincopy = 16384;
143 SYSCTL_INT(_kern_ipc, OID_AUTO, sosendminchain, CTLFLAG_RW, &sosendminchain,
144 0, "");
145 SYSCTL_INT(_kern_ipc, OID_AUTO, sorecvmincopy, CTLFLAG_RW, &sorecvmincopy,
146 0, "");
147
148 void so_cache_timer();
149 struct mbuf *m_getpackets(int, int, int);
150
151
152 /*
153 * Socket operation routines.
154 * These routines are called by the routines in
155 * sys_socket.c or from a system process, and
156 * implement the semantics of socket operations by
157 * switching out to the protocol specific routines.
158 */
159
160 #ifdef __APPLE__
161 void socketinit()
162 {
163 vm_size_t str_size;
164
165 so_cache_init_done = 1;
166
167 timeout(so_cache_timer, NULL, (SO_CACHE_FLUSH_INTERVAL * hz));
168 str_size = (vm_size_t)( sizeof(struct socket) + 4 +
169 get_inpcb_str_size() + 4 +
170 get_tcp_str_size());
171 so_cache_zone = zinit (str_size, 120000*str_size, 8192, "socache zone");
172 #if TEMPDEBUG
173 kprintf("cached_sock_alloc -- so_cache_zone size is %x\n", str_size);
174 #endif
175
176 }
177
178 void cached_sock_alloc(so, waitok)
179 struct socket **so;
180 int waitok;
181
182 {
183 caddr_t temp;
184 int s;
185 register u_long offset;
186
187
188 s = splnet();
189 if (cached_sock_count) {
190 cached_sock_count--;
191 *so = socket_cache_head;
192 if (*so == 0)
193 panic("cached_sock_alloc: cached sock is null");
194
195 socket_cache_head = socket_cache_head->cache_next;
196 if (socket_cache_head)
197 socket_cache_head->cache_prev = 0;
198 else
199 socket_cache_tail = 0;
200 splx(s);
201
202 temp = (*so)->so_saved_pcb;
203 bzero((caddr_t)*so, sizeof(struct socket));
204 #if TEMPDEBUG
205 kprintf("cached_sock_alloc - retreiving cached sock %x - count == %d\n", *so,
206 cached_sock_count);
207 #endif
208 (*so)->so_saved_pcb = temp;
209 }
210 else {
211 #if TEMPDEBUG
212 kprintf("Allocating cached sock %x from memory\n", *so);
213 #endif
214
215 splx(s);
216 if (waitok)
217 *so = (struct socket *) zalloc(so_cache_zone);
218 else
219 *so = (struct socket *) zalloc_noblock(so_cache_zone);
220
221 if (*so == 0)
222 return;
223
224 bzero((caddr_t)*so, sizeof(struct socket));
225
226 /*
227 * Define offsets for extra structures into our single block of
228 * memory. Align extra structures on longword boundaries.
229 */
230
231
232 offset = (u_long) *so;
233 offset += sizeof(struct socket);
234 if (offset & 0x3) {
235 offset += 4;
236 offset &= 0xfffffffc;
237 }
238 (*so)->so_saved_pcb = (caddr_t) offset;
239 offset += get_inpcb_str_size();
240 if (offset & 0x3) {
241 offset += 4;
242 offset &= 0xfffffffc;
243 }
244
245 ((struct inpcb *) (*so)->so_saved_pcb)->inp_saved_ppcb = (caddr_t) offset;
246 #if TEMPDEBUG
247 kprintf("Allocating cached socket - %x, pcb=%x tcpcb=%x\n", *so,
248 (*so)->so_saved_pcb,
249 ((struct inpcb *)(*so)->so_saved_pcb)->inp_saved_ppcb);
250 #endif
251 }
252
253 (*so)->cached_in_sock_layer = 1;
254 }
255
256
257 void cached_sock_free(so)
258 struct socket *so;
259 {
260 int s;
261
262
263 s = splnet();
264 if (++cached_sock_count > MAX_CACHED_SOCKETS) {
265 --cached_sock_count;
266 splx(s);
267 #if TEMPDEBUG
268 kprintf("Freeing overflowed cached socket %x\n", so);
269 #endif
270 zfree(so_cache_zone, (vm_offset_t) so);
271 }
272 else {
273 #if TEMPDEBUG
274 kprintf("Freeing socket %x into cache\n", so);
275 #endif
276 if (so_cache_hw < cached_sock_count)
277 so_cache_hw = cached_sock_count;
278
279 so->cache_next = socket_cache_head;
280 so->cache_prev = 0;
281 if (socket_cache_head)
282 socket_cache_head->cache_prev = so;
283 else
284 socket_cache_tail = so;
285
286 so->cache_timestamp = so_cache_time;
287 socket_cache_head = so;
288 splx(s);
289 }
290
291 #if TEMPDEBUG
292 kprintf("Freed cached sock %x into cache - count is %d\n", so, cached_sock_count);
293 #endif
294
295
296 }
297
298
299 void so_cache_timer()
300 {
301 register struct socket *p;
302 register int s;
303 register int n_freed = 0;
304 boolean_t funnel_state;
305
306 funnel_state = thread_funnel_set(network_flock, TRUE);
307
308 ++so_cache_time;
309
310 s = splnet();
311
312 while (p = socket_cache_tail)
313 {
314 if ((so_cache_time - p->cache_timestamp) < SO_CACHE_TIME_LIMIT)
315 break;
316
317 so_cache_timeouts++;
318
319 if (socket_cache_tail = p->cache_prev)
320 p->cache_prev->cache_next = 0;
321 if (--cached_sock_count == 0)
322 socket_cache_head = 0;
323
324 splx(s);
325
326 zfree(so_cache_zone, (vm_offset_t) p);
327
328 splnet();
329 if (++n_freed >= SO_CACHE_MAX_FREE_BATCH)
330 {
331 so_cache_max_freed++;
332 break;
333 }
334 }
335 splx(s);
336
337 timeout(so_cache_timer, NULL, (SO_CACHE_FLUSH_INTERVAL * hz));
338
339 (void) thread_funnel_set(network_flock, FALSE);
340
341 }
342 #endif /* __APPLE__ */
343
344 /*
345 * Get a socket structure from our zone, and initialize it.
346 * We don't implement `waitok' yet (see comments in uipc_domain.c).
347 * Note that it would probably be better to allocate socket
348 * and PCB at the same time, but I'm not convinced that all
349 * the protocols can be easily modified to do this.
350 */
351 struct socket *
352 soalloc(waitok, dom, type)
353 int waitok;
354 int dom;
355 int type;
356 {
357 struct socket *so;
358
359 if ((dom == PF_INET) && (type == SOCK_STREAM))
360 cached_sock_alloc(&so, waitok);
361 else
362 {
363 so = _MALLOC_ZONE(sizeof(*so), socket_zone, M_WAITOK);
364 if (so)
365 bzero(so, sizeof *so);
366 }
367 /* XXX race condition for reentrant kernel */
368
369 if (so) {
370 so->so_gencnt = ++so_gencnt;
371 so->so_zone = socket_zone;
372 }
373
374 return so;
375 }
376
377 int
378 socreate(dom, aso, type, proto)
379 int dom;
380 struct socket **aso;
381 register int type;
382 int proto;
383 {
384 struct proc *p = current_proc();
385 register struct protosw *prp;
386 register struct socket *so;
387 register int error = 0;
388 #if TCPDEBUG
389 extern int tcpconsdebug;
390 #endif
391 if (proto)
392 prp = pffindproto(dom, proto, type);
393 else
394 prp = pffindtype(dom, type);
395
396 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
397 return (EPROTONOSUPPORT);
398 #ifndef __APPLE__
399
400 if (p->p_prison && jail_socket_unixiproute_only &&
401 prp->pr_domain->dom_family != PF_LOCAL &&
402 prp->pr_domain->dom_family != PF_INET &&
403 prp->pr_domain->dom_family != PF_ROUTE) {
404 return (EPROTONOSUPPORT);
405 }
406
407 #endif
408 if (prp->pr_type != type)
409 return (EPROTOTYPE);
410 so = soalloc(p != 0, dom, type);
411 if (so == 0)
412 return (ENOBUFS);
413
414 TAILQ_INIT(&so->so_incomp);
415 TAILQ_INIT(&so->so_comp);
416 so->so_type = type;
417
418 #ifdef __APPLE__
419 if (p != 0) {
420 if (p->p_ucred->cr_uid == 0)
421 so->so_state = SS_PRIV;
422
423 so->so_uid = p->p_ucred->cr_uid;
424 }
425 #else
426 so->so_cred = p->p_ucred;
427 crhold(so->so_cred);
428 #endif
429 so->so_proto = prp;
430 #ifdef __APPLE__
431 so->so_rcv.sb_flags |= SB_RECV; /* XXX */
432 if (prp->pr_sfilter.tqh_first)
433 error = sfilter_init(so);
434 if (error == 0)
435 #endif
436 error = (*prp->pr_usrreqs->pru_attach)(so, proto, p);
437 if (error) {
438 /*
439 * Warning:
440 * If so_pcb is not zero, the socket will be leaked,
441 * so protocol attachment handler must be coded carefuly
442 */
443 so->so_state |= SS_NOFDREF;
444 sofree(so);
445 return (error);
446 }
447 #ifdef __APPLE__
448 prp->pr_domain->dom_refs++;
449 so->so_rcv.sb_so = so->so_snd.sb_so = so;
450 TAILQ_INIT(&so->so_evlist);
451 #if TCPDEBUG
452 if (tcpconsdebug == 2)
453 so->so_options |= SO_DEBUG;
454 #endif
455 #endif
456
457 *aso = so;
458 return (0);
459 }
460
461 int
462 sobind(so, nam)
463 struct socket *so;
464 struct sockaddr *nam;
465
466 {
467 struct proc *p = current_proc();
468 int error;
469 struct kextcb *kp;
470 int s = splnet();
471
472 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
473 if (error == 0) {
474 kp = sotokextcb(so);
475 while (kp) {
476 if (kp->e_soif && kp->e_soif->sf_sobind) {
477 error = (*kp->e_soif->sf_sobind)(so, nam, kp);
478 if (error) {
479 if (error == EJUSTRETURN) {
480 error = 0;
481 break;
482 }
483 splx(s);
484 return(error);
485 }
486 }
487 kp = kp->e_next;
488 }
489 }
490 splx(s);
491 return (error);
492 }
493
494 void
495 sodealloc(so)
496 struct socket *so;
497 {
498 so->so_gencnt = ++so_gencnt;
499
500 #ifndef __APPLE__
501 if (so->so_rcv.sb_hiwat)
502 (void)chgsbsize(so->so_cred->cr_uidinfo,
503 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
504 if (so->so_snd.sb_hiwat)
505 (void)chgsbsize(so->so_cred->cr_uidinfo,
506 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
507 #ifdef INET
508 if (so->so_accf != NULL) {
509 if (so->so_accf->so_accept_filter != NULL &&
510 so->so_accf->so_accept_filter->accf_destroy != NULL) {
511 so->so_accf->so_accept_filter->accf_destroy(so);
512 }
513 if (so->so_accf->so_accept_filter_str != NULL)
514 FREE(so->so_accf->so_accept_filter_str, M_ACCF);
515 FREE(so->so_accf, M_ACCF);
516 }
517 #endif /* INET */
518 crfree(so->so_cred);
519 zfreei(so->so_zone, so);
520 #else
521 if (so->cached_in_sock_layer == 1)
522 cached_sock_free(so);
523 else
524 _FREE_ZONE(so, sizeof(*so), so->so_zone);
525 #endif /* __APPLE__ */
526 }
527
528 int
529 solisten(so, backlog)
530 register struct socket *so;
531 int backlog;
532
533 {
534 struct kextcb *kp;
535 struct proc *p = current_proc();
536 int s, error;
537
538 s = splnet();
539 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p);
540 if (error) {
541 splx(s);
542 return (error);
543 }
544 if (TAILQ_EMPTY(&so->so_comp))
545 so->so_options |= SO_ACCEPTCONN;
546 if (backlog < 0 || backlog > somaxconn)
547 backlog = somaxconn;
548 so->so_qlimit = backlog;
549 kp = sotokextcb(so);
550 while (kp) {
551 if (kp->e_soif && kp->e_soif->sf_solisten) {
552 error = (*kp->e_soif->sf_solisten)(so, kp);
553 if (error) {
554 if (error == EJUSTRETURN) {
555 error = 0;
556 break;
557 }
558 splx(s);
559 return(error);
560 }
561 }
562 kp = kp->e_next;
563 }
564
565 splx(s);
566 return (0);
567 }
568
569
570 void
571 sofree(so)
572 register struct socket *so;
573 {
574 int error;
575 struct kextcb *kp;
576 struct socket *head = so->so_head;
577
578 kp = sotokextcb(so);
579 while (kp) {
580 if (kp->e_soif && kp->e_soif->sf_sofree) {
581 error = (*kp->e_soif->sf_sofree)(so, kp);
582 if (error) {
583 selthreadclear(&so->so_snd.sb_sel);
584 selthreadclear(&so->so_rcv.sb_sel);
585 return; /* void fn */
586 }
587 }
588 kp = kp->e_next;
589 }
590
591 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) {
592 #ifdef __APPLE__
593 selthreadclear(&so->so_snd.sb_sel);
594 selthreadclear(&so->so_rcv.sb_sel);
595 #endif
596 return;
597 }
598 if (head != NULL) {
599 if (so->so_state & SS_INCOMP) {
600 TAILQ_REMOVE(&head->so_incomp, so, so_list);
601 head->so_incqlen--;
602 } else if (so->so_state & SS_COMP) {
603 /*
604 * We must not decommission a socket that's
605 * on the accept(2) queue. If we do, then
606 * accept(2) may hang after select(2) indicated
607 * that the listening socket was ready.
608 */
609 #ifdef __APPLE__
610 selthreadclear(&so->so_snd.sb_sel);
611 selthreadclear(&so->so_rcv.sb_sel);
612 #endif
613 return;
614 } else {
615 panic("sofree: not queued");
616 }
617 head->so_qlen--;
618 so->so_state &= ~SS_INCOMP;
619 so->so_head = NULL;
620 }
621 #ifdef __APPLE__
622 selthreadclear(&so->so_snd.sb_sel);
623 sbrelease(&so->so_snd);
624 #endif
625 sorflush(so);
626 sfilter_term(so);
627 sodealloc(so);
628 }
629
630 /*
631 * Close a socket on last file table reference removal.
632 * Initiate disconnect if connected.
633 * Free socket when disconnect complete.
634 */
635 int
636 soclose(so)
637 register struct socket *so;
638 {
639 int s = splnet(); /* conservative */
640 int error = 0;
641 struct kextcb *kp;
642
643 #ifndef __APPLE__
644 funsetown(so->so_sigio);
645 #endif
646 kp = sotokextcb(so);
647 while (kp) {
648 if (kp->e_soif && kp->e_soif->sf_soclose) {
649 error = (*kp->e_soif->sf_soclose)(so, kp);
650 if (error) {
651 splx(s);
652 return((error == EJUSTRETURN) ? 0 : error);
653 }
654 }
655 kp = kp->e_next;
656 }
657
658 if (so->so_options & SO_ACCEPTCONN) {
659 struct socket *sp, *sonext;
660
661 sp = TAILQ_FIRST(&so->so_incomp);
662 for (; sp != NULL; sp = sonext) {
663 sonext = TAILQ_NEXT(sp, so_list);
664 (void) soabort(sp);
665 }
666 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
667 sonext = TAILQ_NEXT(sp, so_list);
668 /* Dequeue from so_comp since sofree() won't do it */
669 TAILQ_REMOVE(&so->so_comp, sp, so_list);
670 so->so_qlen--;
671 sp->so_state &= ~SS_COMP;
672 sp->so_head = NULL;
673 (void) soabort(sp);
674 }
675
676 }
677 if (so->so_pcb == 0)
678 goto discard;
679 if (so->so_state & SS_ISCONNECTED) {
680 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
681 error = sodisconnect(so);
682 if (error)
683 goto drop;
684 }
685 if (so->so_options & SO_LINGER) {
686 if ((so->so_state & SS_ISDISCONNECTING) &&
687 (so->so_state & SS_NBIO))
688 goto drop;
689 while (so->so_state & SS_ISCONNECTED) {
690 error = tsleep((caddr_t)&so->so_timeo,
691 PSOCK | PCATCH, "soclos", so->so_linger);
692 if (error)
693 break;
694 }
695 }
696 }
697 drop:
698 if (so->so_pcb) {
699 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
700 if (error == 0)
701 error = error2;
702 }
703 discard:
704 if (so->so_pcb && so->so_state & SS_NOFDREF)
705 panic("soclose: NOFDREF");
706 so->so_state |= SS_NOFDREF;
707 #ifdef __APPLE__
708 so->so_proto->pr_domain->dom_refs--;
709 evsofree(so);
710 #endif
711 sofree(so);
712 splx(s);
713 return (error);
714 }
715
716 /*
717 * Must be called at splnet...
718 */
719 int
720 soabort(so)
721 struct socket *so;
722 {
723 int error;
724
725 error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
726 if (error) {
727 sofree(so);
728 return error;
729 }
730 return (0);
731 }
732
733 int
734 soaccept(so, nam)
735 register struct socket *so;
736 struct sockaddr **nam;
737 {
738 int s = splnet();
739 int error;
740 struct kextcb *kp;
741
742 if ((so->so_state & SS_NOFDREF) == 0)
743 panic("soaccept: !NOFDREF");
744 so->so_state &= ~SS_NOFDREF;
745 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
746 if (error == 0) {
747 kp = sotokextcb(so);
748 while (kp) {
749 if (kp->e_soif && kp->e_soif->sf_soaccept) {
750 error = (*kp->e_soif->sf_soaccept)(so, nam, kp);
751 if (error) {
752 if (error == EJUSTRETURN) {
753 error = 0;
754 break;
755 }
756 splx(s);
757 return(error);
758 }
759 }
760 kp = kp->e_next;
761 }
762 }
763
764
765 splx(s);
766 return (error);
767 }
768
769 int
770 soconnect(so, nam)
771 register struct socket *so;
772 struct sockaddr *nam;
773
774 {
775 int s;
776 int error;
777 struct proc *p = current_proc();
778 struct kextcb *kp;
779
780 if (so->so_options & SO_ACCEPTCONN)
781 return (EOPNOTSUPP);
782 s = splnet();
783 /*
784 * If protocol is connection-based, can only connect once.
785 * Otherwise, if connected, try to disconnect first.
786 * This allows user to disconnect by connecting to, e.g.,
787 * a null address.
788 */
789 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
790 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
791 (error = sodisconnect(so))))
792 error = EISCONN;
793 else {
794 /*
795 * Run connect filter before calling protocol:
796 * - non-blocking connect returns before completion;
797 * - allows filters to modify address.
798 */
799 kp = sotokextcb(so);
800 while (kp) {
801 if (kp->e_soif && kp->e_soif->sf_soconnect) {
802 error = (*kp->e_soif->sf_soconnect)(so, nam, kp);
803 if (error) {
804 if (error == EJUSTRETURN) {
805 error = 0;
806 }
807 splx(s);
808 return(error);
809 }
810 }
811 kp = kp->e_next;
812 }
813 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p);
814 }
815 splx(s);
816 return (error);
817 }
818
819 int
820 soconnect2(so1, so2)
821 register struct socket *so1;
822 struct socket *so2;
823 {
824 int s = splnet();
825 int error;
826 struct kextcb *kp;
827
828 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
829 if (error == 0) {
830 kp = sotokextcb(so1);
831 while (kp) {
832 if (kp->e_soif && kp->e_soif->sf_soconnect2) {
833 error = (*kp->e_soif->sf_soconnect2)(so1, so2, kp);
834 if (error) {
835 if (error == EJUSTRETURN) {
836 return 0;
837 break;
838 }
839 splx(s);
840 return(error);
841 }
842 }
843 kp = kp->e_next;
844 }
845 }
846 splx(s);
847 return (error);
848 }
849
850 int
851 sodisconnect(so)
852 register struct socket *so;
853 {
854 int s = splnet();
855 int error;
856 struct kextcb *kp;
857
858 if ((so->so_state & SS_ISCONNECTED) == 0) {
859 error = ENOTCONN;
860 goto bad;
861 }
862 if (so->so_state & SS_ISDISCONNECTING) {
863 error = EALREADY;
864 goto bad;
865 }
866 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
867 if (error == 0) {
868 kp = sotokextcb(so);
869 while (kp) {
870 if (kp->e_soif && kp->e_soif->sf_sodisconnect) {
871 error = (*kp->e_soif->sf_sodisconnect)(so, kp);
872 if (error) {
873 if (error == EJUSTRETURN) {
874 error = 0;
875 break;
876 }
877 splx(s);
878 return(error);
879 }
880 }
881 kp = kp->e_next;
882 }
883 }
884
885 bad:
886 splx(s);
887 return (error);
888 }
889
890 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_DONTWAIT : M_WAIT)
891 /*
892 * Send on a socket.
893 * If send must go all at once and message is larger than
894 * send buffering, then hard error.
895 * Lock against other senders.
896 * If must go all at once and not enough room now, then
897 * inform user that this would block and do nothing.
898 * Otherwise, if nonblocking, send as much as possible.
899 * The data to be sent is described by "uio" if nonzero,
900 * otherwise by the mbuf chain "top" (which must be null
901 * if uio is not). Data provided in mbuf chain must be small
902 * enough to send all at once.
903 *
904 * Returns nonzero on error, timeout or signal; callers
905 * must check for short counts if EINTR/ERESTART are returned.
906 * Data and control buffers are freed on return.
907 * Experiment:
908 * MSG_HOLD: go thru most of sosend(), but just enqueue the mbuf
909 * MSG_SEND: go thru as for MSG_HOLD on current fragment, then
910 * point at the mbuf chain being constructed and go from there.
911 */
912 int
913 sosend(so, addr, uio, top, control, flags)
914 register struct socket *so;
915 struct sockaddr *addr;
916 struct uio *uio;
917 struct mbuf *top;
918 struct mbuf *control;
919 int flags;
920
921 {
922 struct mbuf **mp;
923 register struct mbuf *m, *freelist = NULL;
924 register long space, len, resid;
925 int clen = 0, error, s, dontroute, mlen, sendflags;
926 int atomic = sosendallatonce(so) || top;
927 struct proc *p = current_proc();
928 struct kextcb *kp;
929
930 if (uio)
931 resid = uio->uio_resid;
932 else
933 resid = top->m_pkthdr.len;
934
935 KERNEL_DEBUG((DBG_FNC_SOSEND | DBG_FUNC_START),
936 so,
937 resid,
938 so->so_snd.sb_cc,
939 so->so_snd.sb_lowat,
940 so->so_snd.sb_hiwat);
941
942 /*
943 * In theory resid should be unsigned.
944 * However, space must be signed, as it might be less than 0
945 * if we over-committed, and we must use a signed comparison
946 * of space and resid. On the other hand, a negative resid
947 * causes us to loop sending 0-length segments to the protocol.
948 *
949 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
950 * type sockets since that's an error.
951 */
952 if (resid < 0 || so->so_type == SOCK_STREAM && (flags & MSG_EOR)) {
953 error = EINVAL;
954 goto out;
955 }
956
957 dontroute =
958 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
959 (so->so_proto->pr_flags & PR_ATOMIC);
960 if (p)
961 p->p_stats->p_ru.ru_msgsnd++;
962 if (control)
963 clen = control->m_len;
964 #define snderr(errno) { error = errno; splx(s); goto release; }
965
966 restart:
967 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
968 if (error)
969 goto out;
970 do {
971 s = splnet();
972 if (so->so_state & SS_CANTSENDMORE)
973 snderr(EPIPE);
974 if (so->so_error) {
975 error = so->so_error;
976 so->so_error = 0;
977 splx(s);
978 goto release;
979 }
980 if ((so->so_state & SS_ISCONNECTED) == 0) {
981 /*
982 * `sendto' and `sendmsg' is allowed on a connection-
983 * based socket if it supports implied connect.
984 * Return ENOTCONN if not connected and no address is
985 * supplied.
986 */
987 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
988 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
989 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
990 !(resid == 0 && clen != 0))
991 snderr(ENOTCONN);
992 } else if (addr == 0 && !(flags&MSG_HOLD))
993 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
994 ENOTCONN : EDESTADDRREQ);
995 }
996 space = sbspace(&so->so_snd);
997 if (flags & MSG_OOB)
998 space += 1024;
999 if ((atomic && resid > so->so_snd.sb_hiwat) ||
1000 clen > so->so_snd.sb_hiwat)
1001 snderr(EMSGSIZE);
1002 if (space < resid + clen &&
1003 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1004 if (so->so_state & SS_NBIO)
1005 snderr(EWOULDBLOCK);
1006 sbunlock(&so->so_snd);
1007 error = sbwait(&so->so_snd);
1008 splx(s);
1009 if (error)
1010 goto out;
1011 goto restart;
1012 }
1013 splx(s);
1014 mp = ⊤
1015 space -= clen;
1016
1017 do {
1018 if (uio == NULL) {
1019 /*
1020 * Data is prepackaged in "top".
1021 */
1022 resid = 0;
1023 if (flags & MSG_EOR)
1024 top->m_flags |= M_EOR;
1025 } else {
1026 boolean_t dropped_funnel = FALSE;
1027 int chainlength;
1028 int bytes_to_copy;
1029
1030 bytes_to_copy = min(resid, space);
1031
1032 if (sosendminchain > 0) {
1033 if (bytes_to_copy >= sosendminchain) {
1034 dropped_funnel = TRUE;
1035 (void)thread_funnel_set(network_flock, FALSE);
1036 }
1037 chainlength = 0;
1038 } else
1039 chainlength = sosendmaxchain;
1040
1041 do {
1042
1043 if (bytes_to_copy >= MINCLSIZE) {
1044 /*
1045 * try to maintain a local cache of mbuf clusters needed to complete this write
1046 * the list is further limited to the number that are currently needed to fill the socket
1047 * this mechanism allows a large number of mbufs/clusters to be grabbed under a single
1048 * mbuf lock... if we can't get any clusters, than fall back to trying for mbufs
1049 * if we fail early (or miscalcluate the number needed) make sure to release any clusters
1050 * we haven't yet consumed.
1051 */
1052 if ((m = freelist) == NULL) {
1053 int num_needed;
1054 int hdrs_needed = 0;
1055
1056 if (top == 0)
1057 hdrs_needed = 1;
1058 num_needed = bytes_to_copy / MCLBYTES;
1059
1060 if ((bytes_to_copy - (num_needed * MCLBYTES)) >= MINCLSIZE)
1061 num_needed++;
1062
1063 if ((freelist = m_getpackets(num_needed, hdrs_needed, M_WAIT)) == NULL)
1064 goto getpackets_failed;
1065 m = freelist;
1066 }
1067 freelist = m->m_next;
1068 m->m_next = NULL;
1069
1070 mlen = MCLBYTES;
1071 len = min(mlen, bytes_to_copy);
1072 } else {
1073 getpackets_failed:
1074 if (top == 0) {
1075 MGETHDR(m, M_WAIT, MT_DATA);
1076 mlen = MHLEN;
1077 m->m_pkthdr.len = 0;
1078 m->m_pkthdr.rcvif = (struct ifnet *)0;
1079 } else {
1080 MGET(m, M_WAIT, MT_DATA);
1081 mlen = MLEN;
1082 }
1083 len = min(mlen, bytes_to_copy);
1084 /*
1085 * For datagram protocols, leave room
1086 * for protocol headers in first mbuf.
1087 */
1088 if (atomic && top == 0 && len < mlen)
1089 MH_ALIGN(m, len);
1090 }
1091 chainlength += len;
1092
1093 space -= len;
1094
1095 error = uiomove(mtod(m, caddr_t), (int)len, uio);
1096
1097 resid = uio->uio_resid;
1098
1099 m->m_len = len;
1100 *mp = m;
1101 top->m_pkthdr.len += len;
1102 if (error)
1103 break;
1104 mp = &m->m_next;
1105 if (resid <= 0) {
1106 if (flags & MSG_EOR)
1107 top->m_flags |= M_EOR;
1108 break;
1109 }
1110 bytes_to_copy = min(resid, space);
1111
1112 } while (space > 0 && (chainlength < sosendmaxchain || atomic || resid < MINCLSIZE));
1113
1114 if (dropped_funnel == TRUE)
1115 (void)thread_funnel_set(network_flock, TRUE);
1116 if (error)
1117 goto release;
1118 }
1119
1120 if (flags & (MSG_HOLD|MSG_SEND))
1121 { /* Enqueue for later, go away if HOLD */
1122 register struct mbuf *mb1;
1123 if (so->so_temp && (flags & MSG_FLUSH))
1124 { m_freem(so->so_temp);
1125 so->so_temp = NULL;
1126 }
1127 if (so->so_temp)
1128 so->so_tail->m_next = top;
1129 else
1130 so->so_temp = top;
1131 mb1 = top;
1132 while (mb1->m_next)
1133 mb1 = mb1->m_next;
1134 so->so_tail = mb1;
1135 if (flags&MSG_HOLD)
1136 { top = NULL;
1137 goto release;
1138 }
1139 top = so->so_temp;
1140 }
1141 if (dontroute)
1142 so->so_options |= SO_DONTROUTE;
1143 s = splnet(); /* XXX */
1144 /* Compute flags here, for pru_send and NKEs */
1145 sendflags = (flags & MSG_OOB) ? PRUS_OOB :
1146 /*
1147 * If the user set MSG_EOF, the protocol
1148 * understands this flag and nothing left to
1149 * send then use PRU_SEND_EOF instead of PRU_SEND.
1150 */
1151 ((flags & MSG_EOF) &&
1152 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1153 (resid <= 0)) ?
1154 PRUS_EOF :
1155 /* If there is more to send set PRUS_MORETOCOME */
1156 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0;
1157 kp = sotokextcb(so);
1158 while (kp)
1159 { if (kp->e_soif && kp->e_soif->sf_sosend) {
1160 error = (*kp->e_soif->sf_sosend)(so, &addr,
1161 &uio, &top,
1162 &control,
1163 &sendflags,
1164 kp);
1165 if (error) {
1166 splx(s);
1167 if (error == EJUSTRETURN) {
1168 sbunlock(&so->so_snd);
1169
1170 if (freelist)
1171 m_freem_list(freelist);
1172 return(0);
1173 }
1174 goto release;
1175 }
1176 }
1177 kp = kp->e_next;
1178 }
1179
1180 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1181 sendflags, top, addr, control, p);
1182 splx(s);
1183 #ifdef __APPLE__
1184 if (flags & MSG_SEND)
1185 so->so_temp = NULL;
1186 #endif
1187 if (dontroute)
1188 so->so_options &= ~SO_DONTROUTE;
1189 clen = 0;
1190 control = 0;
1191 top = 0;
1192 mp = ⊤
1193 if (error)
1194 goto release;
1195 } while (resid && space > 0);
1196 } while (resid);
1197
1198 release:
1199 sbunlock(&so->so_snd);
1200 out:
1201 if (top)
1202 m_freem(top);
1203 if (control)
1204 m_freem(control);
1205 if (freelist)
1206 m_freem_list(freelist);
1207
1208 KERNEL_DEBUG(DBG_FNC_SOSEND | DBG_FUNC_END,
1209 so,
1210 resid,
1211 so->so_snd.sb_cc,
1212 space,
1213 error);
1214
1215 return (error);
1216 }
1217
1218 /*
1219 * Implement receive operations on a socket.
1220 * We depend on the way that records are added to the sockbuf
1221 * by sbappend*. In particular, each record (mbufs linked through m_next)
1222 * must begin with an address if the protocol so specifies,
1223 * followed by an optional mbuf or mbufs containing ancillary data,
1224 * and then zero or more mbufs of data.
1225 * In order to avoid blocking network interrupts for the entire time here,
1226 * we splx() while doing the actual copy to user space.
1227 * Although the sockbuf is locked, new data may still be appended,
1228 * and thus we must maintain consistency of the sockbuf during that time.
1229 *
1230 * The caller may receive the data as a single mbuf chain by supplying
1231 * an mbuf **mp0 for use in returning the chain. The uio is then used
1232 * only for the count in uio_resid.
1233 */
1234 int
1235 soreceive(so, psa, uio, mp0, controlp, flagsp)
1236 register struct socket *so;
1237 struct sockaddr **psa;
1238 struct uio *uio;
1239 struct mbuf **mp0;
1240 struct mbuf **controlp;
1241 int *flagsp;
1242 {
1243 register struct mbuf *m, **mp, *ml;
1244 register int flags, len, error, s, offset;
1245 struct protosw *pr = so->so_proto;
1246 struct mbuf *nextrecord;
1247 int moff, type = 0;
1248 int orig_resid = uio->uio_resid;
1249 struct kextcb *kp;
1250 volatile struct mbuf *free_list;
1251 volatile int delayed_copy_len;
1252 int can_delay;
1253 int need_event;
1254 struct proc *p = current_proc();
1255
1256
1257 KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_START,
1258 so,
1259 uio->uio_resid,
1260 so->so_rcv.sb_cc,
1261 so->so_rcv.sb_lowat,
1262 so->so_rcv.sb_hiwat);
1263
1264 kp = sotokextcb(so);
1265 while (kp) {
1266 if (kp->e_soif && kp->e_soif->sf_soreceive) {
1267 error = (*kp->e_soif->sf_soreceive)(so, psa, &uio,
1268 mp0, controlp,
1269 flagsp, kp);
1270 if (error) {
1271 KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_END, error,0,0,0,0);
1272 return((error == EJUSTRETURN) ? 0 : error);
1273 }
1274 }
1275 kp = kp->e_next;
1276 }
1277
1278 mp = mp0;
1279 if (psa)
1280 *psa = 0;
1281 if (controlp)
1282 *controlp = 0;
1283 if (flagsp)
1284 flags = *flagsp &~ MSG_EOR;
1285 else
1286 flags = 0;
1287 /*
1288 * When SO_WANTOOBFLAG is set we try to get out-of-band data
1289 * regardless of the flags argument. Here is the case were
1290 * out-of-band data is not inline.
1291 */
1292 if ((flags & MSG_OOB) ||
1293 ((so->so_options & SO_WANTOOBFLAG) != 0 &&
1294 (so->so_options & SO_OOBINLINE) == 0 &&
1295 (so->so_oobmark || (so->so_state & SS_RCVATMARK)))) {
1296 m = m_get(M_WAIT, MT_DATA);
1297 if (m == NULL) {
1298 KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_END, ENOBUFS,0,0,0,0);
1299 return (ENOBUFS);
1300 }
1301 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
1302 if (error)
1303 goto bad;
1304 do {
1305 error = uiomove(mtod(m, caddr_t),
1306 (int) min(uio->uio_resid, m->m_len), uio);
1307 m = m_free(m);
1308 } while (uio->uio_resid && error == 0 && m);
1309 bad:
1310 if (m)
1311 m_freem(m);
1312 #ifdef __APPLE__
1313 if ((so->so_options & SO_WANTOOBFLAG) != 0) {
1314 if (error == EWOULDBLOCK || error == EINVAL) {
1315 /*
1316 * Let's try to get normal data:
1317 * EWOULDBLOCK: out-of-band data not receive yet;
1318 * EINVAL: out-of-band data already read.
1319 */
1320 error = 0;
1321 goto nooob;
1322 } else if (error == 0 && flagsp)
1323 *flagsp |= MSG_OOB;
1324 }
1325 KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_END, error,0,0,0,0);
1326 #endif
1327 return (error);
1328 }
1329 nooob:
1330 if (mp)
1331 *mp = (struct mbuf *)0;
1332 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
1333 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
1334
1335
1336 free_list = (struct mbuf *)0;
1337 delayed_copy_len = 0;
1338 restart:
1339 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1340 if (error) {
1341 KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_END, error,0,0,0,0);
1342 return (error);
1343 }
1344 s = splnet();
1345
1346 m = so->so_rcv.sb_mb;
1347 /*
1348 * If we have less data than requested, block awaiting more
1349 * (subject to any timeout) if:
1350 * 1. the current count is less than the low water mark, or
1351 * 2. MSG_WAITALL is set, and it is possible to do the entire
1352 * receive operation at once if we block (resid <= hiwat).
1353 * 3. MSG_DONTWAIT is not set
1354 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1355 * we have to do the receive in sections, and thus risk returning
1356 * a short count if a timeout or signal occurs after we start.
1357 */
1358 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
1359 so->so_rcv.sb_cc < uio->uio_resid) &&
1360 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
1361 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1362 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
1363
1364 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
1365 if (so->so_error) {
1366 if (m)
1367 goto dontblock;
1368 error = so->so_error;
1369 if ((flags & MSG_PEEK) == 0)
1370 so->so_error = 0;
1371 goto release;
1372 }
1373 if (so->so_state & SS_CANTRCVMORE) {
1374 if (m)
1375 goto dontblock;
1376 else
1377 goto release;
1378 }
1379 for (; m; m = m->m_next)
1380 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1381 m = so->so_rcv.sb_mb;
1382 goto dontblock;
1383 }
1384 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1385 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1386 error = ENOTCONN;
1387 goto release;
1388 }
1389 if (uio->uio_resid == 0)
1390 goto release;
1391 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
1392 error = EWOULDBLOCK;
1393 goto release;
1394 }
1395 sbunlock(&so->so_rcv);
1396 if (socket_debug)
1397 printf("Waiting for socket data\n");
1398
1399 error = sbwait(&so->so_rcv);
1400 if (socket_debug)
1401 printf("SORECEIVE - sbwait returned %d\n", error);
1402 splx(s);
1403 if (error) {
1404 KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_END, error,0,0,0,0);
1405 return (error);
1406 }
1407 goto restart;
1408 }
1409 dontblock:
1410 #ifndef __APPLE__
1411 if (uio->uio_procp)
1412 uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
1413 #else /* __APPLE__ */
1414 /*
1415 * 2207985
1416 * This should be uio->uio-procp; however, some callers of this
1417 * function use auto variables with stack garbage, and fail to
1418 * fill out the uio structure properly.
1419 */
1420 if (p)
1421 p->p_stats->p_ru.ru_msgrcv++;
1422 #endif /* __APPLE__ */
1423 nextrecord = m->m_nextpkt;
1424 if ((pr->pr_flags & PR_ADDR) && m->m_type == MT_SONAME) {
1425 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
1426 orig_resid = 0;
1427 if (psa)
1428 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
1429 mp0 == 0);
1430 if (flags & MSG_PEEK) {
1431 m = m->m_next;
1432 } else {
1433 sbfree(&so->so_rcv, m);
1434 MFREE(m, so->so_rcv.sb_mb);
1435 m = so->so_rcv.sb_mb;
1436 }
1437 }
1438 while (m && m->m_type == MT_CONTROL && error == 0) {
1439 if (flags & MSG_PEEK) {
1440 if (controlp)
1441 *controlp = m_copy(m, 0, m->m_len);
1442 m = m->m_next;
1443 } else {
1444 sbfree(&so->so_rcv, m);
1445 if (controlp) {
1446 if (pr->pr_domain->dom_externalize &&
1447 mtod(m, struct cmsghdr *)->cmsg_type ==
1448 SCM_RIGHTS)
1449 error = (*pr->pr_domain->dom_externalize)(m);
1450 *controlp = m;
1451 so->so_rcv.sb_mb = m->m_next;
1452 m->m_next = 0;
1453 m = so->so_rcv.sb_mb;
1454 } else {
1455 MFREE(m, so->so_rcv.sb_mb);
1456 m = so->so_rcv.sb_mb;
1457 }
1458 }
1459 if (controlp) {
1460 orig_resid = 0;
1461 controlp = &(*controlp)->m_next;
1462 }
1463 }
1464 if (m) {
1465 if ((flags & MSG_PEEK) == 0)
1466 m->m_nextpkt = nextrecord;
1467 type = m->m_type;
1468 if (type == MT_OOBDATA)
1469 flags |= MSG_OOB;
1470 }
1471 moff = 0;
1472 offset = 0;
1473
1474 if (!(flags & MSG_PEEK) && uio->uio_resid > sorecvmincopy)
1475 can_delay = 1;
1476 else
1477 can_delay = 0;
1478
1479 need_event = 0;
1480
1481
1482 while (m && (uio->uio_resid - delayed_copy_len) > 0 && error == 0) {
1483 if (m->m_type == MT_OOBDATA) {
1484 if (type != MT_OOBDATA)
1485 break;
1486 } else if (type == MT_OOBDATA)
1487 break;
1488 #ifndef __APPLE__
1489 /*
1490 * This assertion needs rework. The trouble is Appletalk is uses many
1491 * mbuf types (NOT listed in mbuf.h!) which will trigger this panic.
1492 * For now just remove the assertion... CSM 9/98
1493 */
1494 else
1495 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
1496 ("receive 3"));
1497 #else
1498 /*
1499 * Make sure to allways set MSG_OOB event when getting
1500 * out of band data inline.
1501 */
1502 if ((so->so_options & SO_WANTOOBFLAG) != 0 &&
1503 (so->so_options & SO_OOBINLINE) != 0 &&
1504 (so->so_state & SS_RCVATMARK) != 0) {
1505 flags |= MSG_OOB;
1506 }
1507 #endif
1508 so->so_state &= ~SS_RCVATMARK;
1509 len = uio->uio_resid - delayed_copy_len;
1510 if (so->so_oobmark && len > so->so_oobmark - offset)
1511 len = so->so_oobmark - offset;
1512 if (len > m->m_len - moff)
1513 len = m->m_len - moff;
1514 /*
1515 * If mp is set, just pass back the mbufs.
1516 * Otherwise copy them out via the uio, then free.
1517 * Sockbuf must be consistent here (points to current mbuf,
1518 * it points to next record) when we drop priority;
1519 * we must note any additions to the sockbuf when we
1520 * block interrupts again.
1521 */
1522 if (mp == 0) {
1523 if (can_delay && len == m->m_len) {
1524 /*
1525 * only delay the copy if we're consuming the
1526 * mbuf and we're NOT in MSG_PEEK mode
1527 * and we have enough data to make it worthwile
1528 * to drop and retake the funnel... can_delay
1529 * reflects the state of the 2 latter constraints
1530 * moff should always be zero in these cases
1531 */
1532 delayed_copy_len += len;
1533 } else {
1534 splx(s);
1535
1536 if (delayed_copy_len) {
1537 error = sodelayed_copy(uio, &free_list, &delayed_copy_len);
1538
1539 if (error) {
1540 s = splnet();
1541 goto release;
1542 }
1543 if (m != so->so_rcv.sb_mb) {
1544 /*
1545 * can only get here if MSG_PEEK is not set
1546 * therefore, m should point at the head of the rcv queue...
1547 * if it doesn't, it means something drastically changed
1548 * while we were out from behind the funnel in sodelayed_copy...
1549 * perhaps a RST on the stream... in any event, the stream has
1550 * been interrupted... it's probably best just to return
1551 * whatever data we've moved and let the caller sort it out...
1552 */
1553 break;
1554 }
1555 }
1556 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
1557
1558 s = splnet();
1559 if (error)
1560 goto release;
1561 }
1562 } else
1563 uio->uio_resid -= len;
1564
1565 if (len == m->m_len - moff) {
1566 if (m->m_flags & M_EOR)
1567 flags |= MSG_EOR;
1568 if (flags & MSG_PEEK) {
1569 m = m->m_next;
1570 moff = 0;
1571 } else {
1572 nextrecord = m->m_nextpkt;
1573 sbfree(&so->so_rcv, m);
1574
1575 if (mp) {
1576 *mp = m;
1577 mp = &m->m_next;
1578 so->so_rcv.sb_mb = m = m->m_next;
1579 *mp = (struct mbuf *)0;
1580 } else {
1581 m->m_nextpkt = 0;
1582 if (free_list == NULL)
1583 free_list = m;
1584 else
1585 ml->m_next = m;
1586 ml = m;
1587 so->so_rcv.sb_mb = m = m->m_next;
1588 ml->m_next = 0;
1589 }
1590 if (m)
1591 m->m_nextpkt = nextrecord;
1592 }
1593 } else {
1594 if (flags & MSG_PEEK)
1595 moff += len;
1596 else {
1597 if (mp)
1598 *mp = m_copym(m, 0, len, M_WAIT);
1599 m->m_data += len;
1600 m->m_len -= len;
1601 so->so_rcv.sb_cc -= len;
1602 }
1603 }
1604 if (so->so_oobmark) {
1605 if ((flags & MSG_PEEK) == 0) {
1606 so->so_oobmark -= len;
1607 if (so->so_oobmark == 0) {
1608 so->so_state |= SS_RCVATMARK;
1609 /*
1610 * delay posting the actual event until after
1611 * any delayed copy processing has finished
1612 */
1613 need_event = 1;
1614 break;
1615 }
1616 } else {
1617 offset += len;
1618 if (offset == so->so_oobmark)
1619 break;
1620 }
1621 }
1622 if (flags & MSG_EOR)
1623 break;
1624 /*
1625 * If the MSG_WAITALL or MSG_WAITSTREAM flag is set (for non-atomic socket),
1626 * we must not quit until "uio->uio_resid == 0" or an error
1627 * termination. If a signal/timeout occurs, return
1628 * with a short count but without error.
1629 * Keep sockbuf locked against other readers.
1630 */
1631 while (flags & (MSG_WAITALL|MSG_WAITSTREAM) && m == 0 && (uio->uio_resid - delayed_copy_len) > 0 &&
1632 !sosendallatonce(so) && !nextrecord) {
1633 if (so->so_error || so->so_state & SS_CANTRCVMORE)
1634 goto release;
1635
1636 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1637 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1638 if (sbwait(&so->so_rcv)) {
1639 error = 0;
1640 goto release;
1641 }
1642 /*
1643 * have to wait until after we get back from the sbwait to do the copy because
1644 * we will drop the funnel if we have enough data that has been delayed... by dropping
1645 * the funnel we open up a window allowing the netisr thread to process the incoming packets
1646 * and to change the state of this socket... we're issuing the sbwait because
1647 * the socket is empty and we're expecting the netisr thread to wake us up when more
1648 * packets arrive... if we allow that processing to happen and then sbwait, we
1649 * could stall forever with packets sitting in the socket if no further packets
1650 * arrive from the remote side.
1651 *
1652 * we want to copy before we've collected all the data to satisfy this request to
1653 * allow the copy to overlap the incoming packet processing on an MP system
1654 */
1655 if (delayed_copy_len > sorecvmincopy && (delayed_copy_len > (so->so_rcv.sb_hiwat / 2))) {
1656
1657 error = sodelayed_copy(uio, &free_list, &delayed_copy_len);
1658
1659 if (error)
1660 goto release;
1661 }
1662 m = so->so_rcv.sb_mb;
1663 if (m) {
1664 nextrecord = m->m_nextpkt;
1665 }
1666 }
1667 }
1668
1669 if (m && pr->pr_flags & PR_ATOMIC) {
1670 #ifdef __APPLE__
1671 if (so->so_options & SO_DONTTRUNC)
1672 flags |= MSG_RCVMORE;
1673 else {
1674 #endif
1675 flags |= MSG_TRUNC;
1676 if ((flags & MSG_PEEK) == 0)
1677 (void) sbdroprecord(&so->so_rcv);
1678 #ifdef __APPLE__
1679 }
1680 #endif
1681 }
1682 if ((flags & MSG_PEEK) == 0) {
1683 if (m == 0)
1684 so->so_rcv.sb_mb = nextrecord;
1685 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1686 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1687 }
1688 #ifdef __APPLE__
1689 if ((so->so_options & SO_WANTMORE) && so->so_rcv.sb_cc > 0)
1690 flags |= MSG_HAVEMORE;
1691
1692 if (delayed_copy_len) {
1693 error = sodelayed_copy(uio, &free_list, &delayed_copy_len);
1694
1695 if (error)
1696 goto release;
1697 }
1698 if (free_list) {
1699 m_freem_list((struct mbuf *)free_list);
1700 free_list = (struct mbuf *)0;
1701 }
1702 if (need_event)
1703 postevent(so, 0, EV_OOB);
1704 #endif
1705 if (orig_resid == uio->uio_resid && orig_resid &&
1706 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
1707 sbunlock(&so->so_rcv);
1708 splx(s);
1709 goto restart;
1710 }
1711
1712 if (flagsp)
1713 *flagsp |= flags;
1714 release:
1715 if (delayed_copy_len) {
1716 error = sodelayed_copy(uio, &free_list, &delayed_copy_len);
1717 }
1718 if (free_list) {
1719 m_freem_list((struct mbuf *)free_list);
1720 }
1721 sbunlock(&so->so_rcv);
1722 splx(s);
1723
1724 KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_END,
1725 so,
1726 uio->uio_resid,
1727 so->so_rcv.sb_cc,
1728 0,
1729 error);
1730
1731 return (error);
1732 }
1733
1734
1735 int sodelayed_copy(struct uio *uio, struct mbuf **free_list, int *resid)
1736 {
1737 int error = 0;
1738 boolean_t dropped_funnel = FALSE;
1739 struct mbuf *m;
1740
1741 m = *free_list;
1742
1743 if (*resid >= sorecvmincopy) {
1744 dropped_funnel = TRUE;
1745
1746 (void)thread_funnel_set(network_flock, FALSE);
1747 }
1748 while (m && error == 0) {
1749
1750 error = uiomove(mtod(m, caddr_t), (int)m->m_len, uio);
1751
1752 m = m->m_next;
1753 }
1754 m_freem_list(*free_list);
1755
1756 *free_list = (struct mbuf *)NULL;
1757 *resid = 0;
1758
1759 if (dropped_funnel == TRUE)
1760 (void)thread_funnel_set(network_flock, TRUE);
1761
1762 return (error);
1763 }
1764
1765
1766 int
1767 soshutdown(so, how)
1768 register struct socket *so;
1769 register int how;
1770 {
1771 register struct protosw *pr = so->so_proto;
1772 struct kextcb *kp;
1773 int ret;
1774
1775
1776 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN | DBG_FUNC_START, 0,0,0,0,0);
1777 kp = sotokextcb(so);
1778 while (kp) {
1779 if (kp->e_soif && kp->e_soif->sf_soshutdown) {
1780 ret = (*kp->e_soif->sf_soshutdown)(so, how, kp);
1781 if (ret) {
1782 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN | DBG_FUNC_END, 0,0,0,0,0);
1783 return((ret == EJUSTRETURN) ? 0 : ret);
1784 }
1785 }
1786 kp = kp->e_next;
1787 }
1788
1789 if (how != SHUT_WR) {
1790 sorflush(so);
1791 postevent(so, 0, EV_RCLOSED);
1792 }
1793 if (how != SHUT_RD) {
1794 ret = ((*pr->pr_usrreqs->pru_shutdown)(so));
1795 postevent(so, 0, EV_WCLOSED);
1796 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN | DBG_FUNC_END, 0,0,0,0,0);
1797 return(ret);
1798 }
1799
1800 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN | DBG_FUNC_END, 0,0,0,0,0);
1801 return (0);
1802 }
1803
1804 void
1805 sorflush(so)
1806 register struct socket *so;
1807 {
1808 register struct sockbuf *sb = &so->so_rcv;
1809 register struct protosw *pr = so->so_proto;
1810 register int s, error;
1811 struct sockbuf asb;
1812 struct kextcb *kp;
1813
1814 kp = sotokextcb(so);
1815 while (kp) {
1816 if (kp->e_soif && kp->e_soif->sf_sorflush) {
1817 if ((*kp->e_soif->sf_sorflush)(so, kp))
1818 return;
1819 }
1820 kp = kp->e_next;
1821 }
1822
1823 sb->sb_flags |= SB_NOINTR;
1824 (void) sblock(sb, M_WAIT);
1825 s = splimp();
1826 socantrcvmore(so);
1827 sbunlock(sb);
1828 #ifdef __APPLE__
1829 selthreadclear(&sb->sb_sel);
1830 #endif
1831 asb = *sb;
1832 bzero((caddr_t)sb, sizeof (*sb));
1833 if (asb.sb_flags & SB_KNOTE) {
1834 sb->sb_sel.si_note = asb.sb_sel.si_note;
1835 sb->sb_flags = SB_KNOTE;
1836 }
1837 splx(s);
1838 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
1839 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
1840
1841 sbrelease(&asb);
1842 }
1843
1844 /*
1845 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1846 * an additional variant to handle the case where the option value needs
1847 * to be some kind of integer, but not a specific size.
1848 * In addition to their use here, these functions are also called by the
1849 * protocol-level pr_ctloutput() routines.
1850 */
1851 int
1852 sooptcopyin(sopt, buf, len, minlen)
1853 struct sockopt *sopt;
1854 void *buf;
1855 size_t len;
1856 size_t minlen;
1857 {
1858 size_t valsize;
1859
1860 /*
1861 * If the user gives us more than we wanted, we ignore it,
1862 * but if we don't get the minimum length the caller
1863 * wants, we return EINVAL. On success, sopt->sopt_valsize
1864 * is set to however much we actually retrieved.
1865 */
1866 if ((valsize = sopt->sopt_valsize) < minlen)
1867 return EINVAL;
1868 if (valsize > len)
1869 sopt->sopt_valsize = valsize = len;
1870
1871 if (sopt->sopt_p != 0)
1872 return (copyin(sopt->sopt_val, buf, valsize));
1873
1874 bcopy(sopt->sopt_val, buf, valsize);
1875 return 0;
1876 }
1877
1878 int
1879 sosetopt(so, sopt)
1880 struct socket *so;
1881 struct sockopt *sopt;
1882 {
1883 int error, optval;
1884 struct linger l;
1885 struct timeval tv;
1886 short val;
1887 struct kextcb *kp;
1888
1889 if (sopt->sopt_dir != SOPT_SET) {
1890 sopt->sopt_dir = SOPT_SET;
1891 }
1892
1893 kp = sotokextcb(so);
1894 while (kp) {
1895 if (kp->e_soif && kp->e_soif->sf_socontrol) {
1896 error = (*kp->e_soif->sf_socontrol)(so, sopt, kp);
1897 if (error)
1898 return((error == EJUSTRETURN) ? 0 : error);
1899 }
1900 kp = kp->e_next;
1901 }
1902
1903 error = 0;
1904 if (sopt->sopt_level != SOL_SOCKET) {
1905 if (so->so_proto && so->so_proto->pr_ctloutput)
1906 return ((*so->so_proto->pr_ctloutput)
1907 (so, sopt));
1908 error = ENOPROTOOPT;
1909 } else {
1910 switch (sopt->sopt_name) {
1911 case SO_LINGER:
1912 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1913 if (error)
1914 goto bad;
1915
1916 so->so_linger = l.l_linger;
1917 if (l.l_onoff)
1918 so->so_options |= SO_LINGER;
1919 else
1920 so->so_options &= ~SO_LINGER;
1921 break;
1922
1923 case SO_DEBUG:
1924 case SO_KEEPALIVE:
1925 case SO_DONTROUTE:
1926 case SO_USELOOPBACK:
1927 case SO_BROADCAST:
1928 case SO_REUSEADDR:
1929 case SO_REUSEPORT:
1930 case SO_OOBINLINE:
1931 case SO_TIMESTAMP:
1932 #ifdef __APPLE__
1933 case SO_DONTTRUNC:
1934 case SO_WANTMORE:
1935 case SO_WANTOOBFLAG:
1936 #endif
1937 error = sooptcopyin(sopt, &optval, sizeof optval,
1938 sizeof optval);
1939 if (error)
1940 goto bad;
1941 if (optval)
1942 so->so_options |= sopt->sopt_name;
1943 else
1944 so->so_options &= ~sopt->sopt_name;
1945 break;
1946
1947 case SO_SNDBUF:
1948 case SO_RCVBUF:
1949 case SO_SNDLOWAT:
1950 case SO_RCVLOWAT:
1951 error = sooptcopyin(sopt, &optval, sizeof optval,
1952 sizeof optval);
1953 if (error)
1954 goto bad;
1955
1956 /*
1957 * Values < 1 make no sense for any of these
1958 * options, so disallow them.
1959 */
1960 if (optval < 1) {
1961 error = EINVAL;
1962 goto bad;
1963 }
1964
1965 switch (sopt->sopt_name) {
1966 case SO_SNDBUF:
1967 case SO_RCVBUF:
1968 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1969 &so->so_snd : &so->so_rcv,
1970 (u_long) optval) == 0) {
1971 error = ENOBUFS;
1972 goto bad;
1973 }
1974 break;
1975
1976 /*
1977 * Make sure the low-water is never greater than
1978 * the high-water.
1979 */
1980 case SO_SNDLOWAT:
1981 so->so_snd.sb_lowat =
1982 (optval > so->so_snd.sb_hiwat) ?
1983 so->so_snd.sb_hiwat : optval;
1984 break;
1985 case SO_RCVLOWAT:
1986 so->so_rcv.sb_lowat =
1987 (optval > so->so_rcv.sb_hiwat) ?
1988 so->so_rcv.sb_hiwat : optval;
1989 break;
1990 }
1991 break;
1992
1993 case SO_SNDTIMEO:
1994 case SO_RCVTIMEO:
1995 error = sooptcopyin(sopt, &tv, sizeof tv,
1996 sizeof tv);
1997 if (error)
1998 goto bad;
1999
2000 /* assert(hz > 0); */
2001 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
2002 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
2003 error = EDOM;
2004 goto bad;
2005 }
2006 /* assert(tick > 0); */
2007 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
2008 {
2009 long tmp = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
2010 if (tmp > SHRT_MAX) {
2011 error = EDOM;
2012 goto bad;
2013 }
2014 val = tmp;
2015 }
2016
2017 switch (sopt->sopt_name) {
2018 case SO_SNDTIMEO:
2019 so->so_snd.sb_timeo = val;
2020 break;
2021 case SO_RCVTIMEO:
2022 so->so_rcv.sb_timeo = val;
2023 break;
2024 }
2025 break;
2026
2027 case SO_NKE:
2028 {
2029 struct so_nke nke;
2030 struct NFDescriptor *nf1, *nf2 = NULL;
2031
2032 error = sooptcopyin(sopt, &nke,
2033 sizeof nke, sizeof nke);
2034 if (error)
2035 goto bad;
2036
2037 error = nke_insert(so, &nke);
2038 break;
2039 }
2040
2041 case SO_NOSIGPIPE:
2042 error = sooptcopyin(sopt, &optval, sizeof optval,
2043 sizeof optval);
2044 if (error)
2045 goto bad;
2046 if (optval)
2047 so->so_flags |= SOF_NOSIGPIPE;
2048 else
2049 so->so_flags &= ~SOF_NOSIGPIPE;
2050
2051 break;
2052
2053 case SO_NOADDRERR:
2054 error = sooptcopyin(sopt, &optval, sizeof optval,
2055 sizeof optval);
2056 if (error)
2057 goto bad;
2058 if (optval)
2059 so->so_flags |= SOF_NOADDRAVAIL;
2060 else
2061 so->so_flags &= ~SOF_NOADDRAVAIL;
2062
2063 break;
2064
2065 default:
2066 error = ENOPROTOOPT;
2067 break;
2068 }
2069 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
2070 (void) ((*so->so_proto->pr_ctloutput)
2071 (so, sopt));
2072 }
2073 }
2074 bad:
2075 return (error);
2076 }
2077
2078 /* Helper routine for getsockopt */
2079 int
2080 sooptcopyout(sopt, buf, len)
2081 struct sockopt *sopt;
2082 void *buf;
2083 size_t len;
2084 {
2085 int error;
2086 size_t valsize;
2087
2088 error = 0;
2089
2090 /*
2091 * Documented get behavior is that we always return a value,
2092 * possibly truncated to fit in the user's buffer.
2093 * Traditional behavior is that we always tell the user
2094 * precisely how much we copied, rather than something useful
2095 * like the total amount we had available for her.
2096 * Note that this interface is not idempotent; the entire answer must
2097 * generated ahead of time.
2098 */
2099 valsize = min(len, sopt->sopt_valsize);
2100 sopt->sopt_valsize = valsize;
2101 if (sopt->sopt_val != 0) {
2102 if (sopt->sopt_p != 0)
2103 error = copyout(buf, sopt->sopt_val, valsize);
2104 else
2105 bcopy(buf, sopt->sopt_val, valsize);
2106 }
2107 return error;
2108 }
2109
2110 int
2111 sogetopt(so, sopt)
2112 struct socket *so;
2113 struct sockopt *sopt;
2114 {
2115 int error, optval;
2116 struct linger l;
2117 struct timeval tv;
2118 struct mbuf *m;
2119 struct kextcb *kp;
2120
2121 if (sopt->sopt_dir != SOPT_GET) {
2122 sopt->sopt_dir = SOPT_GET;
2123 }
2124
2125 kp = sotokextcb(so);
2126 while (kp) {
2127 if (kp->e_soif && kp->e_soif->sf_socontrol) {
2128 error = (*kp->e_soif->sf_socontrol)(so, sopt, kp);
2129 if (error)
2130 return((error == EJUSTRETURN) ? 0 : error);
2131 }
2132 kp = kp->e_next;
2133 }
2134
2135 error = 0;
2136 if (sopt->sopt_level != SOL_SOCKET) {
2137 if (so->so_proto && so->so_proto->pr_ctloutput) {
2138 return ((*so->so_proto->pr_ctloutput)
2139 (so, sopt));
2140 } else
2141 return (ENOPROTOOPT);
2142 } else {
2143 switch (sopt->sopt_name) {
2144 case SO_LINGER:
2145 l.l_onoff = so->so_options & SO_LINGER;
2146 l.l_linger = so->so_linger;
2147 error = sooptcopyout(sopt, &l, sizeof l);
2148 break;
2149
2150 case SO_USELOOPBACK:
2151 case SO_DONTROUTE:
2152 case SO_DEBUG:
2153 case SO_KEEPALIVE:
2154 case SO_REUSEADDR:
2155 case SO_REUSEPORT:
2156 case SO_BROADCAST:
2157 case SO_OOBINLINE:
2158 case SO_TIMESTAMP:
2159 #ifdef __APPLE__
2160 case SO_DONTTRUNC:
2161 case SO_WANTMORE:
2162 case SO_WANTOOBFLAG:
2163 #endif
2164 optval = so->so_options & sopt->sopt_name;
2165 integer:
2166 error = sooptcopyout(sopt, &optval, sizeof optval);
2167 break;
2168
2169 case SO_TYPE:
2170 optval = so->so_type;
2171 goto integer;
2172
2173 #ifdef __APPLE__
2174 case SO_NREAD:
2175 {
2176 int pkt_total;
2177 struct mbuf *m1;
2178
2179 pkt_total = 0;
2180 m1 = so->so_rcv.sb_mb;
2181 if (so->so_proto->pr_flags & PR_ATOMIC)
2182 {
2183 #if 0
2184 kprintf("SKT CC: %d\n", so->so_rcv.sb_cc);
2185 #endif
2186 while (m1) {
2187 if (m1->m_type == MT_DATA)
2188 pkt_total += m1->m_len;
2189 #if 0
2190 kprintf("CNT: %d/%d\n", m1->m_len, pkt_total);
2191 #endif
2192 m1 = m1->m_next;
2193 }
2194 optval = pkt_total;
2195 } else
2196 optval = so->so_rcv.sb_cc;
2197 #if 0
2198 kprintf("RTN: %d\n", optval);
2199 #endif
2200 goto integer;
2201 }
2202 #endif
2203 case SO_ERROR:
2204 optval = so->so_error;
2205 so->so_error = 0;
2206 goto integer;
2207
2208 case SO_SNDBUF:
2209 optval = so->so_snd.sb_hiwat;
2210 goto integer;
2211
2212 case SO_RCVBUF:
2213 optval = so->so_rcv.sb_hiwat;
2214 goto integer;
2215
2216 case SO_SNDLOWAT:
2217 optval = so->so_snd.sb_lowat;
2218 goto integer;
2219
2220 case SO_RCVLOWAT:
2221 optval = so->so_rcv.sb_lowat;
2222 goto integer;
2223
2224 case SO_SNDTIMEO:
2225 case SO_RCVTIMEO:
2226 optval = (sopt->sopt_name == SO_SNDTIMEO ?
2227 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
2228
2229 tv.tv_sec = optval / hz;
2230 tv.tv_usec = (optval % hz) * tick;
2231 error = sooptcopyout(sopt, &tv, sizeof tv);
2232 break;
2233
2234 case SO_NOSIGPIPE:
2235 optval = (so->so_flags & SOF_NOSIGPIPE);
2236 goto integer;
2237
2238 case SO_NOADDRERR:
2239 optval = (so->so_flags & SOF_NOADDRAVAIL);
2240 goto integer;
2241
2242 default:
2243 error = ENOPROTOOPT;
2244 break;
2245 }
2246 return (error);
2247 }
2248 }
2249
2250 #ifdef __APPLE__
2251 /*
2252 * Network filter support
2253 */
2254 /* Run the list of filters, creating extension control blocks */
2255 sfilter_init(register struct socket *so)
2256 { struct kextcb *kp, **kpp;
2257 struct protosw *prp;
2258 struct NFDescriptor *nfp;
2259
2260 prp = so->so_proto;
2261 nfp = prp->pr_sfilter.tqh_first; /* non-null */
2262 kpp = &so->so_ext;
2263 kp = NULL;
2264 while (nfp)
2265 { MALLOC(kp, struct kextcb *, sizeof(*kp),
2266 M_TEMP, M_WAITOK);
2267 if (kp == NULL)
2268 return(ENOBUFS); /* so_free will clean up */
2269 *kpp = kp;
2270 kpp = &kp->e_next;
2271 kp->e_next = NULL;
2272 kp->e_fcb = NULL;
2273 kp->e_nfd = nfp;
2274 kp->e_soif = nfp->nf_soif;
2275 kp->e_sout = nfp->nf_soutil;
2276 /*
2277 * Ignore return value for create
2278 * Everyone gets a chance at startup
2279 */
2280 if (kp->e_soif && kp->e_soif->sf_socreate)
2281 (*kp->e_soif->sf_socreate)(so, prp, kp);
2282 nfp = nfp->nf_next.tqe_next;
2283 }
2284 return(0);
2285 }
2286
2287 /*
2288 * Run the list of filters, freeing extension control blocks
2289 * Assumes the soif/soutil blocks have been handled.
2290 */
2291 sfilter_term(struct socket *so)
2292 { struct kextcb *kp, *kp1;
2293
2294 kp = so->so_ext;
2295 while (kp)
2296 { kp1 = kp->e_next;
2297 /*
2298 * Ignore return code on termination; everyone must
2299 * get terminated.
2300 */
2301 if (kp->e_soif && kp->e_soif->sf_sofree)
2302 kp->e_soif->sf_sofree(so, kp);
2303 FREE(kp, M_TEMP);
2304 kp = kp1;
2305 }
2306 return(0);
2307 }
2308 #endif __APPLE__
2309
2310 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2311 int
2312 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2313 {
2314 struct mbuf *m, *m_prev;
2315 int sopt_size = sopt->sopt_valsize;
2316
2317 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
2318 if (m == 0)
2319 return ENOBUFS;
2320 if (sopt_size > MLEN) {
2321 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
2322 if ((m->m_flags & M_EXT) == 0) {
2323 m_free(m);
2324 return ENOBUFS;
2325 }
2326 m->m_len = min(MCLBYTES, sopt_size);
2327 } else {
2328 m->m_len = min(MLEN, sopt_size);
2329 }
2330 sopt_size -= m->m_len;
2331 *mp = m;
2332 m_prev = m;
2333
2334 while (sopt_size) {
2335 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
2336 if (m == 0) {
2337 m_freem(*mp);
2338 return ENOBUFS;
2339 }
2340 if (sopt_size > MLEN) {
2341 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
2342 if ((m->m_flags & M_EXT) == 0) {
2343 m_freem(*mp);
2344 return ENOBUFS;
2345 }
2346 m->m_len = min(MCLBYTES, sopt_size);
2347 } else {
2348 m->m_len = min(MLEN, sopt_size);
2349 }
2350 sopt_size -= m->m_len;
2351 m_prev->m_next = m;
2352 m_prev = m;
2353 }
2354 return 0;
2355 }
2356
2357 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2358 int
2359 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
2360 {
2361 struct mbuf *m0 = m;
2362
2363 if (sopt->sopt_val == NULL)
2364 return 0;
2365 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2366 if (sopt->sopt_p != NULL) {
2367 int error;
2368
2369 error = copyin(sopt->sopt_val, mtod(m, char *),
2370 m->m_len);
2371 if (error != 0) {
2372 m_freem(m0);
2373 return(error);
2374 }
2375 } else
2376 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
2377 sopt->sopt_valsize -= m->m_len;
2378 (caddr_t)sopt->sopt_val += m->m_len;
2379 m = m->m_next;
2380 }
2381 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2382 panic("soopt_mcopyin");
2383 return 0;
2384 }
2385
2386 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2387 int
2388 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
2389 {
2390 struct mbuf *m0 = m;
2391 size_t valsize = 0;
2392
2393 if (sopt->sopt_val == NULL)
2394 return 0;
2395 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2396 if (sopt->sopt_p != NULL) {
2397 int error;
2398
2399 error = copyout(mtod(m, char *), sopt->sopt_val,
2400 m->m_len);
2401 if (error != 0) {
2402 m_freem(m0);
2403 return(error);
2404 }
2405 } else
2406 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
2407 sopt->sopt_valsize -= m->m_len;
2408 (caddr_t)sopt->sopt_val += m->m_len;
2409 valsize += m->m_len;
2410 m = m->m_next;
2411 }
2412 if (m != NULL) {
2413 /* enough soopt buffer should be given from user-land */
2414 m_freem(m0);
2415 return(EINVAL);
2416 }
2417 sopt->sopt_valsize = valsize;
2418 return 0;
2419 }
2420
2421 void
2422 sohasoutofband(so)
2423 register struct socket *so;
2424 {
2425 struct proc *p;
2426 struct kextcb *kp;
2427
2428 kp = sotokextcb(so);
2429 while (kp) {
2430 if (kp->e_soif && kp->e_soif->sf_sohasoutofband) {
2431 if ((*kp->e_soif->sf_sohasoutofband)(so, kp))
2432 return;
2433 }
2434 kp = kp->e_next;
2435 }
2436 if (so->so_pgid < 0)
2437 gsignal(-so->so_pgid, SIGURG);
2438 else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
2439 psignal(p, SIGURG);
2440 selwakeup(&so->so_rcv.sb_sel);
2441 }
2442
2443 int
2444 sopoll(struct socket *so, int events, struct ucred *cred, void * wql)
2445 {
2446 struct proc *p = current_proc();
2447 int revents = 0;
2448 int s = splnet();
2449
2450 if (events & (POLLIN | POLLRDNORM))
2451 if (soreadable(so))
2452 revents |= events & (POLLIN | POLLRDNORM);
2453
2454 if (events & (POLLOUT | POLLWRNORM))
2455 if (sowriteable(so))
2456 revents |= events & (POLLOUT | POLLWRNORM);
2457
2458 if (events & (POLLPRI | POLLRDBAND))
2459 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
2460 revents |= events & (POLLPRI | POLLRDBAND);
2461
2462 if (revents == 0) {
2463 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
2464 /* Darwin sets the flag first, BSD calls selrecord first */
2465 so->so_rcv.sb_flags |= SB_SEL;
2466 selrecord(p, &so->so_rcv.sb_sel, wql);
2467 }
2468
2469 if (events & (POLLOUT | POLLWRNORM)) {
2470 /* Darwin sets the flag first, BSD calls selrecord first */
2471 so->so_snd.sb_flags |= SB_SEL;
2472 selrecord(p, &so->so_snd.sb_sel, wql);
2473 }
2474 }
2475
2476 splx(s);
2477 return (revents);
2478 }
2479
2480
2481 int
2482 soo_kqfilter(struct file *fp, struct knote *kn, struct proc *p)
2483 {
2484 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2485 struct sockbuf *sb;
2486 int s;
2487
2488 switch (kn->kn_filter) {
2489 case EVFILT_READ:
2490 if (so->so_options & SO_ACCEPTCONN)
2491 kn->kn_fop = &solisten_filtops;
2492 else
2493 kn->kn_fop = &soread_filtops;
2494 sb = &so->so_rcv;
2495 break;
2496 case EVFILT_WRITE:
2497 kn->kn_fop = &sowrite_filtops;
2498 sb = &so->so_snd;
2499 break;
2500 default:
2501 return (1);
2502 }
2503
2504 if (sb->sb_sel.si_flags & SI_INITED)
2505 return (1);
2506
2507 s = splnet();
2508 if (KNOTE_ATTACH(&sb->sb_sel.si_note, kn))
2509 sb->sb_flags |= SB_KNOTE;
2510 splx(s);
2511 return (0);
2512 }
2513
2514 static void
2515 filt_sordetach(struct knote *kn)
2516 {
2517 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2518 int s = splnet();
2519
2520 if (so->so_rcv.sb_flags & SB_KNOTE &&
2521 !(so->so_rcv.sb_sel.si_flags & SI_INITED))
2522 if (KNOTE_DETACH(&so->so_rcv.sb_sel.si_note, kn))
2523 so->so_rcv.sb_flags &= ~SB_KNOTE;
2524 splx(s);
2525 }
2526
2527 /*ARGSUSED*/
2528 static int
2529 filt_soread(struct knote *kn, long hint)
2530 {
2531 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2532
2533 kn->kn_data = so->so_rcv.sb_cc;
2534 if (so->so_state & SS_CANTRCVMORE) {
2535 kn->kn_flags |= EV_EOF;
2536 kn->kn_fflags = so->so_error;
2537 return (1);
2538 }
2539 if (so->so_error) /* temporary udp error */
2540 return (1);
2541 if (kn->kn_sfflags & NOTE_LOWAT)
2542 return (kn->kn_data >= kn->kn_sdata);
2543 return (kn->kn_data >= so->so_rcv.sb_lowat);
2544 }
2545
2546 static void
2547 filt_sowdetach(struct knote *kn)
2548 {
2549 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2550 int s = splnet();
2551
2552 if(so->so_snd.sb_flags & SB_KNOTE &&
2553 !(so->so_snd.sb_sel.si_flags & SI_INITED))
2554 if (KNOTE_DETACH(&so->so_snd.sb_sel.si_note, kn))
2555 so->so_snd.sb_flags &= ~SB_KNOTE;
2556 splx(s);
2557 }
2558
2559 /*ARGSUSED*/
2560 static int
2561 filt_sowrite(struct knote *kn, long hint)
2562 {
2563 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2564
2565 kn->kn_data = sbspace(&so->so_snd);
2566 if (so->so_state & SS_CANTSENDMORE) {
2567 kn->kn_flags |= EV_EOF;
2568 kn->kn_fflags = so->so_error;
2569 return (1);
2570 }
2571 if (so->so_error) /* temporary udp error */
2572 return (1);
2573 if (((so->so_state & SS_ISCONNECTED) == 0) &&
2574 (so->so_proto->pr_flags & PR_CONNREQUIRED))
2575 return (0);
2576 if (kn->kn_sfflags & NOTE_LOWAT)
2577 return (kn->kn_data >= kn->kn_sdata);
2578 return (kn->kn_data >= so->so_snd.sb_lowat);
2579 }
2580
2581 /*ARGSUSED*/
2582 static int
2583 filt_solisten(struct knote *kn, long hint)
2584 {
2585 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2586
2587 kn->kn_data = so->so_qlen;
2588 return (! TAILQ_EMPTY(&so->so_comp));
2589 }
2590
Cache object: 6fc1b8196f3741e28aac371983fffb6d
|