]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_socket.c
669d3487467e0fbb1cd704f7457afcbc33f9c0b7
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1988, 1990, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95
59 #include <sys/param.h>
60 #include <sys/systm.h>
62 #include <sys/fcntl.h>
63 #include <sys/malloc.h>
65 #include <sys/domain.h>
66 #include <sys/kernel.h>
68 #include <sys/protosw.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
71 #include <sys/resourcevar.h>
72 #include <sys/signalvar.h>
73 #include <sys/sysctl.h>
76 #include <sys/kdebug.h>
77 #include <net/route.h>
78 #include <netinet/in.h>
79 #include <netinet/in_pcb.h>
80 #include <kern/zalloc.h>
81 #include <machine/limits.h>
84 int so_cache_timeouts
= 0;
85 int so_cache_max_freed
= 0;
86 int cached_sock_count
= 0;
87 struct socket
*socket_cache_head
= 0;
88 struct socket
*socket_cache_tail
= 0;
89 u_long so_cache_time
= 0;
90 int so_cache_init_done
= 0;
91 struct zone
*so_cache_zone
;
92 extern int get_inpcb_str_size();
93 extern int get_tcp_str_size();
95 #include <machine/limits.h>
98 int socket_zone
= M_SOCKET
;
99 so_gen_t so_gencnt
; /* generation count for sockets */
101 MALLOC_DEFINE(M_SONAME
, "soname", "socket name");
102 MALLOC_DEFINE(M_PCB
, "pcb", "protocol control block");
104 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
105 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
106 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
107 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
108 #define DBG_FNC_SOSEND NETDBG_CODE(DBG_NETSOCK, (4 << 8) | 1)
109 #define DBG_FNC_SORECEIVE NETDBG_CODE(DBG_NETSOCK, (8 << 8))
110 #define DBG_FNC_SOSHUTDOWN NETDBG_CODE(DBG_NETSOCK, (9 << 8))
113 SYSCTL_DECL(_kern_ipc
);
115 static int somaxconn
= SOMAXCONN
;
116 SYSCTL_INT(_kern_ipc
, KIPC_SOMAXCONN
, somaxconn
, CTLFLAG_RW
, &somaxconn
,
119 /* Should we get a maximum also ??? */
120 static int sosendminchain
= 16384;
121 SYSCTL_INT(_kern_ipc
, OID_AUTO
, sosendminchain
, CTLFLAG_RW
, &sosendminchain
,
124 void so_cache_timer();
127 * Socket operation routines.
128 * These routines are called by the routines in
129 * sys_socket.c or from a system process, and
130 * implement the semantics of socket operations by
131 * switching out to the protocol specific routines.
138 so_cache_init_done
= 1;
140 timeout(so_cache_timer
, NULL
, (SO_CACHE_FLUSH_INTERVAL
* hz
));
141 str_size
= (vm_size_t
)( sizeof(struct socket
) + 4 +
142 get_inpcb_str_size() + 4 +
144 so_cache_zone
= zinit (str_size
, 120000*str_size
, 8192, "socache zone");
146 kprintf("cached_sock_alloc -- so_cache_zone size is %x\n", str_size
);
151 void cached_sock_alloc(so
, waitok
)
158 register u_long offset
;
162 if (cached_sock_count
) {
164 *so
= socket_cache_head
;
166 panic("cached_sock_alloc: cached sock is null");
168 socket_cache_head
= socket_cache_head
->cache_next
;
169 if (socket_cache_head
)
170 socket_cache_head
->cache_prev
= 0;
172 socket_cache_tail
= 0;
175 temp
= (*so
)->so_saved_pcb
;
176 bzero((caddr_t
)*so
, sizeof(struct socket
));
178 kprintf("cached_sock_alloc - retreiving cached sock %x - count == %d\n", *so
,
181 (*so
)->so_saved_pcb
= temp
;
185 kprintf("Allocating cached sock %x from memory\n", *so
);
190 *so
= (struct socket
*) zalloc(so_cache_zone
);
192 *so
= (struct socket
*) zalloc_noblock(so_cache_zone
);
197 bzero((caddr_t
)*so
, sizeof(struct socket
));
200 * Define offsets for extra structures into our single block of
201 * memory. Align extra structures on longword boundaries.
205 offset
= (u_long
) *so
;
206 offset
+= sizeof(struct socket
);
209 offset
&= 0xfffffffc;
211 (*so
)->so_saved_pcb
= (caddr_t
) offset
;
212 offset
+= get_inpcb_str_size();
215 offset
&= 0xfffffffc;
218 ((struct inpcb
*) (*so
)->so_saved_pcb
)->inp_saved_ppcb
= (caddr_t
) offset
;
220 kprintf("Allocating cached socket - %x, pcb=%x tcpcb=%x\n", *so
,
222 ((struct inpcb
*)(*so
)->so_saved_pcb
)->inp_saved_ppcb
);
226 (*so
)->cached_in_sock_layer
= 1;
230 void cached_sock_free(so
)
237 if (++cached_sock_count
> MAX_CACHED_SOCKETS
) {
241 kprintf("Freeing overflowed cached socket %x\n", so
);
243 zfree(so_cache_zone
, (vm_offset_t
) so
);
247 kprintf("Freeing socket %x into cache\n", so
);
249 if (so_cache_hw
< cached_sock_count
)
250 so_cache_hw
= cached_sock_count
;
252 so
->cache_next
= socket_cache_head
;
254 if (socket_cache_head
)
255 socket_cache_head
->cache_prev
= so
;
257 socket_cache_tail
= so
;
259 so
->cache_timestamp
= so_cache_time
;
260 socket_cache_head
= so
;
265 kprintf("Freed cached sock %x into cache - count is %d\n", so
, cached_sock_count
);
272 void so_cache_timer()
274 register struct socket
*p
;
276 register int n_freed
= 0;
277 boolean_t funnel_state
;
279 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
285 while (p
= socket_cache_tail
)
287 if ((so_cache_time
- p
->cache_timestamp
) < SO_CACHE_TIME_LIMIT
)
292 if (socket_cache_tail
= p
->cache_prev
)
293 p
->cache_prev
->cache_next
= 0;
294 if (--cached_sock_count
== 0)
295 socket_cache_head
= 0;
299 zfree(so_cache_zone
, (vm_offset_t
) p
);
302 if (++n_freed
>= SO_CACHE_MAX_FREE_BATCH
)
304 so_cache_max_freed
++;
310 timeout(so_cache_timer
, NULL
, (SO_CACHE_FLUSH_INTERVAL
* hz
));
312 (void) thread_funnel_set(network_flock
, FALSE
);
318 * Get a socket structure from our zone, and initialize it.
319 * We don't implement `waitok' yet (see comments in uipc_domain.c).
320 * Note that it would probably be better to allocate socket
321 * and PCB at the same time, but I'm not convinced that all
322 * the protocols can be easily modified to do this.
325 soalloc(waitok
, dom
, type
)
332 if ((dom
== PF_INET
) && (type
== SOCK_STREAM
))
333 cached_sock_alloc(&so
, waitok
);
336 so
= _MALLOC_ZONE(sizeof(*so
), socket_zone
, M_WAITOK
);
338 bzero(so
, sizeof *so
);
340 /* XXX race condition for reentrant kernel */
343 so
->so_gencnt
= ++so_gencnt
;
344 so
->so_zone
= socket_zone
;
351 socreate(dom
, aso
, type
, proto
)
358 struct proc
*p
= current_proc();
359 register struct protosw
*prp
;
361 register int error
= 0;
364 prp
= pffindproto(dom
, proto
, type
);
366 prp
= pffindtype(dom
, type
);
367 if (prp
== 0 || prp
->pr_usrreqs
->pru_attach
== 0)
368 return (EPROTONOSUPPORT
);
369 if (prp
->pr_type
!= type
)
371 so
= soalloc(p
!= 0, dom
, type
);
375 TAILQ_INIT(&so
->so_incomp
);
376 TAILQ_INIT(&so
->so_comp
);
380 if (p
->p_ucred
->cr_uid
== 0)
381 so
->so_state
= SS_PRIV
;
383 so
->so_uid
= p
->p_ucred
->cr_uid
;
387 so
->so_rcv
.sb_flags
|= SB_RECV
; /* XXX */
388 if (prp
->pr_sfilter
.tqh_first
)
389 error
= sfilter_init(so
);
391 error
= (*prp
->pr_usrreqs
->pru_attach
)(so
, proto
, p
);
394 so
->so_state
|= SS_NOFDREF
;
398 prp
->pr_domain
->dom_refs
++;
399 so
->so_rcv
.sb_so
= so
->so_snd
.sb_so
= so
;
400 TAILQ_INIT(&so
->so_evlist
);
408 struct sockaddr
*nam
;
411 struct proc
*p
= current_proc();
416 error
= (*so
->so_proto
->pr_usrreqs
->pru_bind
)(so
, nam
, p
);
417 if (error
== 0) /* ??? */
418 { kp
= sotokextcb(so
);
420 { if (kp
->e_soif
&& kp
->e_soif
->sf_sobind
)
421 { error
= (*kp
->e_soif
->sf_sobind
)(so
, nam
, kp
);
423 { if (error
== EJUSTRETURN
)
440 so
->so_gencnt
= ++so_gencnt
;
442 if (so
->cached_in_sock_layer
== 1)
443 cached_sock_free(so
);
445 _FREE_ZONE(so
, sizeof(*so
), so
->so_zone
);
449 solisten(so
, backlog
)
450 register struct socket
*so
;
455 struct proc
*p
= current_proc();
459 error
= (*so
->so_proto
->pr_usrreqs
->pru_listen
)(so
, p
);
464 if (so
->so_comp
.tqh_first
== NULL
)
465 so
->so_options
|= SO_ACCEPTCONN
;
466 if (backlog
< 0 || backlog
> somaxconn
)
468 so
->so_qlimit
= backlog
;
472 if (kp
->e_soif
&& kp
->e_soif
->sf_solisten
)
473 { error
= (*kp
->e_soif
->sf_solisten
)(so
, kp
);
475 { if (error
== EJUSTRETURN
)
491 register struct socket
*so
;
494 struct socket
*head
= so
->so_head
;
498 { if (kp
->e_soif
&& kp
->e_soif
->sf_sofree
)
499 { error
= (*kp
->e_soif
->sf_sofree
)(so
, kp
);
501 return; /* void fn */
506 if (so
->so_pcb
|| (so
->so_state
& SS_NOFDREF
) == 0)
509 if (so
->so_state
& SS_INCOMP
) {
510 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
512 } else if (so
->so_state
& SS_COMP
) {
513 TAILQ_REMOVE(&head
->so_comp
, so
, so_list
);
515 panic("sofree: not queued");
518 so
->so_state
&= ~(SS_INCOMP
|SS_COMP
);
522 sbrelease(&so
->so_snd
);
529 * Close a socket on last file table reference removal.
530 * Initiate disconnect if connected.
531 * Free socket when disconnect complete.
535 register struct socket
*so
;
537 int s
= splnet(); /* conservative */
542 funsetown(so
->so_pgid
);
546 { if (kp
->e_soif
&& kp
->e_soif
->sf_soclose
)
547 { error
= (*kp
->e_soif
->sf_soclose
)(so
, kp
);
550 return((error
== EJUSTRETURN
) ? 0 : error
);
556 if (so
->so_options
& SO_ACCEPTCONN
) {
557 struct socket
*sp
, *sonext
;
559 for (sp
= so
->so_incomp
.tqh_first
; sp
!= NULL
; sp
= sonext
) {
560 sonext
= sp
->so_list
.tqe_next
;
563 for (sp
= so
->so_comp
.tqh_first
; sp
!= NULL
; sp
= sonext
) {
564 sonext
= sp
->so_list
.tqe_next
;
570 if (so
->so_state
& SS_ISCONNECTED
) {
571 if ((so
->so_state
& SS_ISDISCONNECTING
) == 0) {
572 error
= sodisconnect(so
);
576 if (so
->so_options
& SO_LINGER
) {
577 if ((so
->so_state
& SS_ISDISCONNECTING
) &&
578 (so
->so_state
& SS_NBIO
))
580 while (so
->so_state
& SS_ISCONNECTED
) {
581 error
= tsleep((caddr_t
)&so
->so_timeo
,
582 PSOCK
| PCATCH
, "soclos", so
->so_linger
);
590 int error2
= (*so
->so_proto
->pr_usrreqs
->pru_detach
)(so
);
595 if (so
->so_state
& SS_NOFDREF
)
596 panic("soclose: NOFDREF");
597 so
->so_state
|= SS_NOFDREF
;
598 so
->so_proto
->pr_domain
->dom_refs
--;
606 * Must be called at splnet...
613 return (*so
->so_proto
->pr_usrreqs
->pru_abort
)(so
);
618 register struct socket
*so
;
619 struct sockaddr
**nam
;
624 if ((so
->so_state
& SS_NOFDREF
) == 0)
625 panic("soaccept: !NOFDREF");
626 so
->so_state
&= ~SS_NOFDREF
;
627 error
= (*so
->so_proto
->pr_usrreqs
->pru_accept
)(so
, nam
);
629 { kp
= sotokextcb(so
);
631 if (kp
->e_soif
&& kp
->e_soif
->sf_soaccept
)
632 { error
= (*kp
->e_soif
->sf_soaccept
)(so
, nam
, kp
);
634 { if (error
== EJUSTRETURN
)
651 register struct socket
*so
;
652 struct sockaddr
*nam
;
657 struct proc
*p
= current_proc();
660 if (so
->so_options
& SO_ACCEPTCONN
)
664 * If protocol is connection-based, can only connect once.
665 * Otherwise, if connected, try to disconnect first.
666 * This allows user to disconnect by connecting to, e.g.,
669 if (so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
) &&
670 ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ||
671 (error
= sodisconnect(so
))))
674 error
= (*so
->so_proto
->pr_usrreqs
->pru_connect
)(so
, nam
, p
);
680 if (kp
->e_soif
&& kp
->e_soif
->sf_soconnect
)
681 { error
= (*kp
->e_soif
->sf_soconnect
)(so
, nam
, kp
);
683 { if (error
== EJUSTRETURN
)
700 register struct socket
*so1
;
707 error
= (*so1
->so_proto
->pr_usrreqs
->pru_connect2
)(so1
, so2
);
709 { kp
= sotokextcb(so1
);
711 { if (kp
->e_soif
&& kp
->e_soif
->sf_soconnect2
)
712 { error
= (*kp
->e_soif
->sf_soconnect2
)(so1
, so2
, kp
);
714 { if (error
== EJUSTRETURN
)
729 register struct socket
*so
;
735 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
739 if (so
->so_state
& SS_ISDISCONNECTING
) {
743 error
= (*so
->so_proto
->pr_usrreqs
->pru_disconnect
)(so
);
746 { kp
= sotokextcb(so
);
748 { if (kp
->e_soif
&& kp
->e_soif
->sf_sodisconnect
)
749 { error
= (*kp
->e_soif
->sf_sodisconnect
)(so
, kp
);
751 { if (error
== EJUSTRETURN
)
766 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_DONTWAIT : M_WAIT)
769 * If send must go all at once and message is larger than
770 * send buffering, then hard error.
771 * Lock against other senders.
772 * If must go all at once and not enough room now, then
773 * inform user that this would block and do nothing.
774 * Otherwise, if nonblocking, send as much as possible.
775 * The data to be sent is described by "uio" if nonzero,
776 * otherwise by the mbuf chain "top" (which must be null
777 * if uio is not). Data provided in mbuf chain must be small
778 * enough to send all at once.
780 * Returns nonzero on error, timeout or signal; callers
781 * must check for short counts if EINTR/ERESTART are returned.
782 * Data and control buffers are freed on return.
784 * MSG_HOLD: go thru most of sosend(), but just enqueue the mbuf
785 * MSG_SEND: go thru as for MSG_HOLD on current fragment, then
786 * point at the mbuf chain being constructed and go from there.
789 sosend(so
, addr
, uio
, top
, control
, flags
)
790 register struct socket
*so
;
791 struct sockaddr
*addr
;
794 struct mbuf
*control
;
799 register struct mbuf
*m
;
800 register long space
, len
, resid
;
801 int clen
= 0, error
, s
, dontroute
, mlen
, sendflags
;
802 int atomic
= sosendallatonce(so
) || top
;
803 struct proc
*p
= current_proc();
807 resid
= uio
->uio_resid
;
809 resid
= top
->m_pkthdr
.len
;
811 KERNEL_DEBUG((DBG_FNC_SOSEND
| DBG_FUNC_START
),
816 so
->so_snd
.sb_hiwat
);
819 * In theory resid should be unsigned.
820 * However, space must be signed, as it might be less than 0
821 * if we over-committed, and we must use a signed comparison
822 * of space and resid. On the other hand, a negative resid
823 * causes us to loop sending 0-length segments to the protocol.
825 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
826 * type sockets since that's an error.
828 if (resid
< 0 || so
->so_type
== SOCK_STREAM
&& (flags
& MSG_EOR
)) {
834 (flags
& MSG_DONTROUTE
) && (so
->so_options
& SO_DONTROUTE
) == 0 &&
835 (so
->so_proto
->pr_flags
& PR_ATOMIC
);
837 p
->p_stats
->p_ru
.ru_msgsnd
++;
839 clen
= control
->m_len
;
840 #define snderr(errno) { error = errno; splx(s); goto release; }
843 error
= sblock(&so
->so_snd
, SBLOCKWAIT(flags
));
848 if (so
->so_state
& SS_CANTSENDMORE
)
851 error
= so
->so_error
;
856 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
858 * `sendto' and `sendmsg' is allowed on a connection-
859 * based socket if it supports implied connect.
860 * Return ENOTCONN if not connected and no address is
863 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) &&
864 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) == 0) {
865 if ((so
->so_state
& SS_ISCONFIRMING
) == 0 &&
866 !(resid
== 0 && clen
!= 0))
868 } else if (addr
== 0 && !(flags
&MSG_HOLD
))
869 snderr(so
->so_proto
->pr_flags
& PR_CONNREQUIRED
?
870 ENOTCONN
: EDESTADDRREQ
);
872 space
= sbspace(&so
->so_snd
);
875 if ((atomic
&& resid
> so
->so_snd
.sb_hiwat
) ||
876 clen
> so
->so_snd
.sb_hiwat
)
878 if (space
< resid
+ clen
&& uio
&&
879 (atomic
|| space
< so
->so_snd
.sb_lowat
|| space
< clen
)) {
880 if (so
->so_state
& SS_NBIO
)
882 sbunlock(&so
->so_snd
);
883 error
= sbwait(&so
->so_snd
);
895 * Data is prepackaged in "top".
899 top
->m_flags
|= M_EOR
;
901 boolean_t funnel_state
= TRUE
;
902 int chainmbufs
= (sosendminchain
> 0 && resid
>= sosendminchain
);
905 funnel_state
= thread_funnel_set(network_flock
, FALSE
);
907 KERNEL_DEBUG(DBG_FNC_SOSEND
| DBG_FUNC_NONE
, -1, 0, 0, 0, 0);
909 MGETHDR(m
, M_WAIT
, MT_DATA
);
912 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
914 MGET(m
, M_WAIT
, MT_DATA
);
917 if (resid
>= MINCLSIZE
) {
919 if ((m
->m_flags
& M_EXT
) == 0)
922 len
= min(min(mlen
, resid
), space
);
925 len
= min(min(mlen
, resid
), space
);
927 * For datagram protocols, leave room
928 * for protocol headers in first mbuf.
930 if (atomic
&& top
== 0 && len
< mlen
)
933 KERNEL_DEBUG(DBG_FNC_SOSEND
| DBG_FUNC_NONE
, -1, 0, 0, 0, 0);
935 error
= uiomove(mtod(m
, caddr_t
), (int)len
, uio
);
936 resid
= uio
->uio_resid
;
940 top
->m_pkthdr
.len
+= len
;
946 top
->m_flags
|= M_EOR
;
949 } while (space
> 0 && (chainmbufs
|| atomic
|| resid
< MINCLSIZE
));
951 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
956 if (flags
& (MSG_HOLD
|MSG_SEND
))
957 { /* Enqueue for later, go away if HOLD */
958 register struct mbuf
*mb1
;
959 if (so
->so_temp
&& (flags
& MSG_FLUSH
))
960 { m_freem(so
->so_temp
);
964 so
->so_tail
->m_next
= top
;
978 so
->so_options
|= SO_DONTROUTE
;
979 s
= splnet(); /* XXX */
981 /* Compute flags here, for pru_send and NKEs */
982 sendflags
= (flags
& MSG_OOB
) ? PRUS_OOB
:
984 * If the user set MSG_EOF, the protocol
985 * understands this flag and nothing left to
986 * send then use PRU_SEND_EOF instead of PRU_SEND.
988 ((flags
& MSG_EOF
) &&
989 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) &&
992 /* If there is more to send set PRUS_MORETOCOME */
993 (resid
> 0 && space
> 0) ? PRUS_MORETOCOME
: 0;
995 { if (kp
->e_soif
&& kp
->e_soif
->sf_sosend
)
996 { error
= (*kp
->e_soif
->sf_sosend
)(so
, &addr
,
1003 if (error
== EJUSTRETURN
)
1004 { sbunlock(&so
->so_snd
);
1013 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
,
1014 sendflags
, top
, addr
, control
, p
);
1016 if (flags
& MSG_SEND
)
1020 so
->so_options
&= ~SO_DONTROUTE
;
1027 } while (resid
&& space
> 0);
1031 sbunlock(&so
->so_snd
);
1038 KERNEL_DEBUG(DBG_FNC_SOSEND
| DBG_FUNC_END
,
1049 * Implement receive operations on a socket.
1050 * We depend on the way that records are added to the sockbuf
1051 * by sbappend*. In particular, each record (mbufs linked through m_next)
1052 * must begin with an address if the protocol so specifies,
1053 * followed by an optional mbuf or mbufs containing ancillary data,
1054 * and then zero or more mbufs of data.
1055 * In order to avoid blocking network interrupts for the entire time here,
1056 * we splx() while doing the actual copy to user space.
1057 * Although the sockbuf is locked, new data may still be appended,
1058 * and thus we must maintain consistency of the sockbuf during that time.
1060 * The caller may receive the data as a single mbuf chain by supplying
1061 * an mbuf **mp0 for use in returning the chain. The uio is then used
1062 * only for the count in uio_resid.
1065 soreceive(so
, psa
, uio
, mp0
, controlp
, flagsp
)
1066 register struct socket
*so
;
1067 struct sockaddr
**psa
;
1070 struct mbuf
**controlp
;
1073 register struct mbuf
*m
, **mp
;
1074 register int flags
, len
, error
, s
, offset
;
1075 struct protosw
*pr
= so
->so_proto
;
1076 struct mbuf
*nextrecord
;
1078 int orig_resid
= uio
->uio_resid
;
1081 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_START
,
1085 so
->so_rcv
.sb_lowat
,
1086 so
->so_rcv
.sb_hiwat
);
1088 kp
= sotokextcb(so
);
1090 { if (kp
->e_soif
&& kp
->e_soif
->sf_soreceive
)
1091 { error
= (*kp
->e_soif
->sf_soreceive
)(so
, psa
, &uio
,
1095 return((error
== EJUSTRETURN
) ? 0 : error
);
1106 flags
= *flagsp
&~ MSG_EOR
;
1110 * When SO_WANTOOBFLAG is set we try to get out-of-band data
1111 * regardless of the flags argument. Here is the case were
1112 * out-of-band data is not inline.
1114 if ((flags
& MSG_OOB
) ||
1115 ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1116 (so
->so_options
& SO_OOBINLINE
) == 0 &&
1117 (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
)))) {
1118 m
= m_get(M_WAIT
, MT_DATA
);
1119 error
= (*pr
->pr_usrreqs
->pru_rcvoob
)(so
, m
, flags
& MSG_PEEK
);
1123 error
= uiomove(mtod(m
, caddr_t
),
1124 (int) min(uio
->uio_resid
, m
->m_len
), uio
);
1126 } while (uio
->uio_resid
&& error
== 0 && m
);
1130 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0) {
1131 if (error
== EWOULDBLOCK
|| error
== EINVAL
) {
1133 * Let's try to get normal data:
1134 * EWOULDBLOCK: out-of-band data not receive yet;
1135 * EINVAL: out-of-band data already read.
1139 } else if (error
== 0 && flagsp
)
1142 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1147 *mp
= (struct mbuf
*)0;
1148 if (so
->so_state
& SS_ISCONFIRMING
&& uio
->uio_resid
)
1149 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, 0);
1152 if (error
= sblock(&so
->so_rcv
, SBLOCKWAIT(flags
)))
1154 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1159 m
= so
->so_rcv
.sb_mb
;
1161 * If we have less data than requested, block awaiting more
1162 * (subject to any timeout) if:
1163 * 1. the current count is less than the low water mark, or
1164 * 2. MSG_WAITALL is set, and it is possible to do the entire
1165 * receive operation at once if we block (resid <= hiwat).
1166 * 3. MSG_DONTWAIT is not set
1167 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1168 * we have to do the receive in sections, and thus risk returning
1169 * a short count if a timeout or signal occurs after we start.
1171 if (m
== 0 || (((flags
& MSG_DONTWAIT
) == 0 &&
1172 so
->so_rcv
.sb_cc
< uio
->uio_resid
) &&
1173 (so
->so_rcv
.sb_cc
< so
->so_rcv
.sb_lowat
||
1174 ((flags
& MSG_WAITALL
) && uio
->uio_resid
<= so
->so_rcv
.sb_hiwat
)) &&
1175 m
->m_nextpkt
== 0 && (pr
->pr_flags
& PR_ATOMIC
) == 0)) {
1176 KASSERT(m
!= 0 || !so
->so_rcv
.sb_cc
, ("receive 1"));
1180 error
= so
->so_error
;
1181 if ((flags
& MSG_PEEK
) == 0)
1185 if (so
->so_state
& SS_CANTRCVMORE
) {
1191 for (; m
; m
= m
->m_next
)
1192 if (m
->m_type
== MT_OOBDATA
|| (m
->m_flags
& M_EOR
)) {
1193 m
= so
->so_rcv
.sb_mb
;
1196 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
)) == 0 &&
1197 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)) {
1201 if (uio
->uio_resid
== 0)
1203 if ((so
->so_state
& SS_NBIO
) || (flags
& MSG_DONTWAIT
)) {
1204 error
= EWOULDBLOCK
;
1207 sbunlock(&so
->so_rcv
);
1209 printf("Waiting for socket data\n");
1210 error
= sbwait(&so
->so_rcv
);
1212 printf("SORECEIVE - sbwait returned %d\n", error
);
1216 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, error
,0,0,0,0);
1222 #ifdef notyet /* XXXX */
1224 uio
->uio_procp
->p_stats
->p_ru
.ru_msgrcv
++;
1226 nextrecord
= m
->m_nextpkt
;
1227 if ((pr
->pr_flags
& PR_ADDR
) && m
->m_type
== MT_SONAME
) {
1228 KASSERT(m
->m_type
== MT_SONAME
, ("receive 1a"));
1231 *psa
= dup_sockaddr(mtod(m
, struct sockaddr
*),
1233 if (flags
& MSG_PEEK
) {
1236 sbfree(&so
->so_rcv
, m
);
1237 MFREE(m
, so
->so_rcv
.sb_mb
);
1238 m
= so
->so_rcv
.sb_mb
;
1241 while (m
&& m
->m_type
== MT_CONTROL
&& error
== 0) {
1242 if (flags
& MSG_PEEK
) {
1244 *controlp
= m_copy(m
, 0, m
->m_len
);
1247 sbfree(&so
->so_rcv
, m
);
1249 if (pr
->pr_domain
->dom_externalize
&&
1250 mtod(m
, struct cmsghdr
*)->cmsg_type
==
1252 error
= (*pr
->pr_domain
->dom_externalize
)(m
);
1254 so
->so_rcv
.sb_mb
= m
->m_next
;
1256 m
= so
->so_rcv
.sb_mb
;
1258 MFREE(m
, so
->so_rcv
.sb_mb
);
1259 m
= so
->so_rcv
.sb_mb
;
1264 controlp
= &(*controlp
)->m_next
;
1268 if ((flags
& MSG_PEEK
) == 0)
1269 m
->m_nextpkt
= nextrecord
;
1271 if (type
== MT_OOBDATA
)
1276 while (m
&& uio
->uio_resid
> 0 && error
== 0) {
1277 if (m
->m_type
== MT_OOBDATA
) {
1278 if (type
!= MT_OOBDATA
)
1280 } else if (type
== MT_OOBDATA
)
1284 * This assertion needs rework. The trouble is Appletalk is uses many
1285 * mbuf types (NOT listed in mbuf.h!) which will trigger this panic.
1286 * For now just remove the assertion... CSM 9/98
1289 KASSERT(m
->m_type
== MT_DATA
|| m
->m_type
== MT_HEADER
,
1293 * Make sure to allways set MSG_OOB event when getting
1294 * out of band data inline.
1296 if ((so
->so_options
& SO_WANTOOBFLAG
) != 0 &&
1297 (so
->so_options
& SO_OOBINLINE
) != 0 &&
1298 (so
->so_state
& SS_RCVATMARK
) != 0) {
1301 so
->so_state
&= ~SS_RCVATMARK
;
1302 len
= uio
->uio_resid
;
1303 if (so
->so_oobmark
&& len
> so
->so_oobmark
- offset
)
1304 len
= so
->so_oobmark
- offset
;
1305 if (len
> m
->m_len
- moff
)
1306 len
= m
->m_len
- moff
;
1308 * If mp is set, just pass back the mbufs.
1309 * Otherwise copy them out via the uio, then free.
1310 * Sockbuf must be consistent here (points to current mbuf,
1311 * it points to next record) when we drop priority;
1312 * we must note any additions to the sockbuf when we
1313 * block interrupts again.
1317 error
= uiomove(mtod(m
, caddr_t
) + moff
, (int)len
, uio
);
1322 uio
->uio_resid
-= len
;
1323 if (len
== m
->m_len
- moff
) {
1324 if (m
->m_flags
& M_EOR
)
1326 if (flags
& MSG_PEEK
) {
1330 nextrecord
= m
->m_nextpkt
;
1331 sbfree(&so
->so_rcv
, m
);
1335 so
->so_rcv
.sb_mb
= m
= m
->m_next
;
1336 *mp
= (struct mbuf
*)0;
1338 MFREE(m
, so
->so_rcv
.sb_mb
);
1339 m
= so
->so_rcv
.sb_mb
;
1342 m
->m_nextpkt
= nextrecord
;
1345 if (flags
& MSG_PEEK
)
1349 *mp
= m_copym(m
, 0, len
, M_WAIT
);
1352 so
->so_rcv
.sb_cc
-= len
;
1355 if (so
->so_oobmark
) {
1356 if ((flags
& MSG_PEEK
) == 0) {
1357 so
->so_oobmark
-= len
;
1358 if (so
->so_oobmark
== 0) {
1359 so
->so_state
|= SS_RCVATMARK
;
1360 postevent(so
, 0, EV_OOB
);
1365 if (offset
== so
->so_oobmark
)
1369 if (flags
& MSG_EOR
)
1372 * If the MSG_WAITALL flag is set (for non-atomic socket),
1373 * we must not quit until "uio->uio_resid == 0" or an error
1374 * termination. If a signal/timeout occurs, return
1375 * with a short count but without error.
1376 * Keep sockbuf locked against other readers.
1378 while (flags
& MSG_WAITALL
&& m
== 0 && uio
->uio_resid
> 0 &&
1379 !sosendallatonce(so
) && !nextrecord
) {
1380 if (so
->so_error
|| so
->so_state
& SS_CANTRCVMORE
)
1382 error
= sbwait(&so
->so_rcv
);
1384 sbunlock(&so
->so_rcv
);
1386 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
, 0,0,0,0,0);
1389 m
= so
->so_rcv
.sb_mb
;
1391 nextrecord
= m
->m_nextpkt
;
1395 if (m
&& pr
->pr_flags
& PR_ATOMIC
) {
1396 if (so
->so_options
& SO_DONTTRUNC
)
1397 flags
|= MSG_RCVMORE
;
1399 { flags
|= MSG_TRUNC
;
1400 if ((flags
& MSG_PEEK
) == 0)
1401 (void) sbdroprecord(&so
->so_rcv
);
1404 if ((flags
& MSG_PEEK
) == 0) {
1406 so
->so_rcv
.sb_mb
= nextrecord
;
1407 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
)
1408 (*pr
->pr_usrreqs
->pru_rcvd
)(so
, flags
);
1410 if ((so
->so_options
& SO_WANTMORE
) && so
->so_rcv
.sb_cc
> 0)
1411 flags
|= MSG_HAVEMORE
;
1412 if (orig_resid
== uio
->uio_resid
&& orig_resid
&&
1413 (flags
& MSG_EOR
) == 0 && (so
->so_state
& SS_CANTRCVMORE
) == 0) {
1414 sbunlock(&so
->so_rcv
);
1422 sbunlock(&so
->so_rcv
);
1425 KERNEL_DEBUG(DBG_FNC_SORECEIVE
| DBG_FUNC_END
,
1437 register struct socket
*so
;
1440 register struct protosw
*pr
= so
->so_proto
;
1445 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_START
, 0,0,0,0,0);
1446 kp
= sotokextcb(so
);
1448 { if (kp
->e_soif
&& kp
->e_soif
->sf_soshutdown
)
1449 { ret
= (*kp
->e_soif
->sf_soshutdown
)(so
, how
, kp
);
1451 return((ret
== EJUSTRETURN
) ? 0 : ret
);
1459 postevent(so
, 0, EV_RCLOSED
);
1462 ret
= ((*pr
->pr_usrreqs
->pru_shutdown
)(so
));
1463 postevent(so
, 0, EV_WCLOSED
);
1464 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
1468 KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN
| DBG_FUNC_END
, 0,0,0,0,0);
1474 register struct socket
*so
;
1476 register struct sockbuf
*sb
= &so
->so_rcv
;
1477 register struct protosw
*pr
= so
->so_proto
;
1478 register int s
, error
;
1482 kp
= sotokextcb(so
);
1484 { if (kp
->e_soif
&& kp
->e_soif
->sf_sorflush
)
1485 { if ((*kp
->e_soif
->sf_sorflush
)(so
, kp
))
1491 sb
->sb_flags
|= SB_NOINTR
;
1492 (void) sblock(sb
, M_WAIT
);
1497 bzero((caddr_t
)sb
, sizeof (*sb
));
1499 if (pr
->pr_flags
& PR_RIGHTS
&& pr
->pr_domain
->dom_dispose
)
1500 (*pr
->pr_domain
->dom_dispose
)(asb
.sb_mb
);
1505 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1506 * an additional variant to handle the case where the option value needs
1507 * to be some kind of integer, but not a specific size.
1508 * In addition to their use here, these functions are also called by the
1509 * protocol-level pr_ctloutput() routines.
1512 sooptcopyin(sopt
, buf
, len
, minlen
)
1513 struct sockopt
*sopt
;
1521 * If the user gives us more than we wanted, we ignore it,
1522 * but if we don't get the minimum length the caller
1523 * wants, we return EINVAL. On success, sopt->sopt_valsize
1524 * is set to however much we actually retrieved.
1526 if ((valsize
= sopt
->sopt_valsize
) < minlen
)
1529 sopt
->sopt_valsize
= valsize
= len
;
1531 if (sopt
->sopt_p
!= 0)
1532 return (copyin(sopt
->sopt_val
, buf
, valsize
));
1534 bcopy(sopt
->sopt_val
, buf
, valsize
);
1541 struct sockopt
*sopt
;
1549 kp
= sotokextcb(so
);
1551 { if (kp
->e_soif
&& kp
->e_soif
->sf_socontrol
)
1552 { error
= (*kp
->e_soif
->sf_socontrol
)(so
, sopt
, kp
);
1554 return((error
== EJUSTRETURN
) ? 0 : error
);
1560 if (sopt
->sopt_level
!= SOL_SOCKET
) {
1561 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
)
1562 return ((*so
->so_proto
->pr_ctloutput
)
1564 error
= ENOPROTOOPT
;
1566 switch (sopt
->sopt_name
) {
1568 error
= sooptcopyin(sopt
, &l
, sizeof l
, sizeof l
);
1572 so
->so_linger
= l
.l_linger
;
1574 so
->so_options
|= SO_LINGER
;
1576 so
->so_options
&= ~SO_LINGER
;
1582 case SO_USELOOPBACK
:
1590 case SO_WANTOOBFLAG
:
1591 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1596 so
->so_options
|= sopt
->sopt_name
;
1598 so
->so_options
&= ~sopt
->sopt_name
;
1605 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1611 * Values < 1 make no sense for any of these
1612 * options, so disallow them.
1619 switch (sopt
->sopt_name
) {
1622 if (sbreserve(sopt
->sopt_name
== SO_SNDBUF
?
1623 &so
->so_snd
: &so
->so_rcv
,
1624 (u_long
) optval
) == 0) {
1631 * Make sure the low-water is never greater than
1635 so
->so_snd
.sb_lowat
=
1636 (optval
> so
->so_snd
.sb_hiwat
) ?
1637 so
->so_snd
.sb_hiwat
: optval
;
1640 so
->so_rcv
.sb_lowat
=
1641 (optval
> so
->so_rcv
.sb_hiwat
) ?
1642 so
->so_rcv
.sb_hiwat
: optval
;
1649 error
= sooptcopyin(sopt
, &tv
, sizeof tv
,
1654 if (tv
.tv_sec
> SHRT_MAX
/ hz
- hz
) {
1658 val
= tv
.tv_sec
* hz
+ tv
.tv_usec
/ tick
;
1660 switch (sopt
->sopt_name
) {
1662 so
->so_snd
.sb_timeo
= val
;
1665 so
->so_rcv
.sb_timeo
= val
;
1671 { struct so_nke nke
;
1672 struct NFDescriptor
*nf1
, *nf2
= NULL
;
1674 error
= sooptcopyin(sopt
, &nke
,
1675 sizeof nke
, sizeof nke
);
1679 error
= nke_insert(so
, &nke
);
1684 error
= ENOPROTOOPT
;
1687 if (error
== 0 && so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
1688 (void) ((*so
->so_proto
->pr_ctloutput
)
1696 /* Helper routine for getsockopt */
1698 sooptcopyout(sopt
, buf
, len
)
1699 struct sockopt
*sopt
;
1709 * Documented get behavior is that we always return a value,
1710 * possibly truncated to fit in the user's buffer.
1711 * Traditional behavior is that we always tell the user
1712 * precisely how much we copied, rather than something useful
1713 * like the total amount we had available for her.
1714 * Note that this interface is not idempotent; the entire answer must
1715 * generated ahead of time.
1717 valsize
= min(len
, sopt
->sopt_valsize
);
1718 sopt
->sopt_valsize
= valsize
;
1719 if (sopt
->sopt_val
!= 0) {
1720 if (sopt
->sopt_p
!= 0)
1721 error
= copyout(buf
, sopt
->sopt_val
, valsize
);
1723 bcopy(buf
, sopt
->sopt_val
, valsize
);
1731 struct sockopt
*sopt
;
1739 kp
= sotokextcb(so
);
1741 { if (kp
->e_soif
&& kp
->e_soif
->sf_socontrol
)
1742 { error
= (*kp
->e_soif
->sf_socontrol
)(so
, sopt
, kp
);
1744 return((error
== EJUSTRETURN
) ? 0 : error
);
1750 if (sopt
->sopt_level
!= SOL_SOCKET
) {
1751 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
1752 return ((*so
->so_proto
->pr_ctloutput
)
1755 return (ENOPROTOOPT
);
1757 switch (sopt
->sopt_name
) {
1759 l
.l_onoff
= so
->so_options
& SO_LINGER
;
1760 l
.l_linger
= so
->so_linger
;
1761 error
= sooptcopyout(sopt
, &l
, sizeof l
);
1764 case SO_USELOOPBACK
:
1775 case SO_WANTOOBFLAG
:
1776 optval
= so
->so_options
& sopt
->sopt_name
;
1778 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1782 optval
= so
->so_type
;
1790 m1
= so
->so_rcv
.sb_mb
;
1791 if (so
->so_proto
->pr_flags
& PR_ATOMIC
)
1794 kprintf("SKT CC: %d\n", so
->so_rcv
.sb_cc
);
1797 { if (m1
->m_type
== MT_DATA
)
1798 pkt_total
+= m1
->m_len
;
1800 kprintf("CNT: %d/%d\n", m1
->m_len
, pkt_total
);
1806 optval
= so
->so_rcv
.sb_cc
;
1808 kprintf("RTN: %d\n", optval
);
1813 optval
= so
->so_error
;
1818 optval
= so
->so_snd
.sb_hiwat
;
1822 optval
= so
->so_rcv
.sb_hiwat
;
1826 optval
= so
->so_snd
.sb_lowat
;
1830 optval
= so
->so_rcv
.sb_lowat
;
1835 optval
= (sopt
->sopt_name
== SO_SNDTIMEO
?
1836 so
->so_snd
.sb_timeo
: so
->so_rcv
.sb_timeo
);
1838 tv
.tv_sec
= optval
/ hz
;
1839 tv
.tv_usec
= (optval
% hz
) * tick
;
1840 error
= sooptcopyout(sopt
, &tv
, sizeof tv
);
1844 error
= ENOPROTOOPT
;
1853 register struct socket
*so
;
1859 kp
= sotokextcb(so
);
1861 { if (kp
->e_soif
&& kp
->e_soif
->sf_sohasoutofband
)
1862 { if ((*kp
->e_soif
->sf_sohasoutofband
)(so
, kp
))
1867 if (so
->so_pgid
< 0)
1868 gsignal(-so
->so_pgid
, SIGURG
);
1869 else if (so
->so_pgid
> 0 && (p
= pfind(so
->so_pgid
)) != 0)
1871 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1872 selwakeup(&so
->so_rcv
.sb_sel
);
1873 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1877 * Network filter support
1879 /* Run the list of filters, creating extension control blocks */
1880 sfilter_init(register struct socket
*so
)
1881 { struct kextcb
*kp
, **kpp
;
1882 struct protosw
*prp
;
1883 struct NFDescriptor
*nfp
;
1886 nfp
= prp
->pr_sfilter
.tqh_first
; /* non-null */
1890 { MALLOC(kp
, struct kextcb
*, sizeof(*kp
),
1893 return(ENOBUFS
); /* so_free will clean up */
1899 kp
->e_soif
= nfp
->nf_soif
;
1900 kp
->e_sout
= nfp
->nf_soutil
;
1902 * Ignore return value for create
1903 * Everyone gets a chance at startup
1905 if (kp
->e_soif
&& kp
->e_soif
->sf_socreate
)
1906 (*kp
->e_soif
->sf_socreate
)(so
, prp
, kp
);
1907 nfp
= nfp
->nf_next
.tqe_next
;
1914 * Run the list of filters, freeing extension control blocks
1915 * Assumes the soif/soutil blocks have been handled.
1917 sfilter_term(struct socket
*so
)
1918 { struct kextcb
*kp
, *kp1
;
1924 * Ignore return code on termination; everyone must
1927 if (kp
->e_soif
&& kp
->e_soif
->sf_sofree
)
1928 kp
->e_soif
->sf_sofree(so
, kp
);
1937 sopoll(struct socket
*so
, int events
, struct ucred
*cred
)
1939 struct proc
*p
= current_proc();
1943 if (events
& (POLLIN
| POLLRDNORM
))
1945 revents
|= events
& (POLLIN
| POLLRDNORM
);
1947 if (events
& (POLLOUT
| POLLWRNORM
))
1948 if (sowriteable(so
))
1949 revents
|= events
& (POLLOUT
| POLLWRNORM
);
1951 if (events
& (POLLPRI
| POLLRDBAND
))
1952 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
))
1953 revents
|= events
& (POLLPRI
| POLLRDBAND
);
1956 if (events
& (POLLIN
| POLLPRI
| POLLRDNORM
| POLLRDBAND
)) {
1957 selrecord(p
, &so
->so_rcv
.sb_sel
);
1958 so
->so_rcv
.sb_flags
|= SB_SEL
;
1961 if (events
& (POLLOUT
| POLLWRNORM
)) {
1962 selrecord(p
, &so
->so_snd
.sb_sel
);
1963 so
->so_snd
.sb_flags
|= SB_SEL
;
1971 /*#### IPv6 Integration. Added new routines */
1973 sooptgetm(struct sockopt
*sopt
, struct mbuf
**mp
)
1975 struct mbuf
*m
, *m_prev
;
1976 int sopt_size
= sopt
->sopt_valsize
;
1978 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
1981 if (sopt_size
> MLEN
) {
1982 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
1983 if ((m
->m_flags
& M_EXT
) == 0) {
1987 m
->m_len
= min(MCLBYTES
, sopt_size
);
1989 m
->m_len
= min(MLEN
, sopt_size
);
1991 sopt_size
-= m
->m_len
;
1996 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
2001 if (sopt_size
> MLEN
) {
2002 MCLGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
);
2003 if ((m
->m_flags
& M_EXT
) == 0) {
2007 m
->m_len
= min(MCLBYTES
, sopt_size
);
2009 m
->m_len
= min(MLEN
, sopt_size
);
2011 sopt_size
-= m
->m_len
;
2018 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2020 sooptmcopyin(struct sockopt
*sopt
, struct mbuf
*m
)
2022 struct mbuf
*m0
= m
;
2024 if (sopt
->sopt_val
== NULL
)
2026 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2027 if (sopt
->sopt_p
!= NULL
) {
2030 error
= copyin(sopt
->sopt_val
, mtod(m
, char *),
2037 bcopy(sopt
->sopt_val
, mtod(m
, char *), m
->m_len
);
2038 sopt
->sopt_valsize
-= m
->m_len
;
2039 (caddr_t
)sopt
->sopt_val
+= m
->m_len
;
2042 if (m
!= NULL
) /* should be allocated enoughly at ip6_sooptmcopyin() */
2043 panic("sooptmcopyin");
2047 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2049 sooptmcopyout(struct sockopt
*sopt
, struct mbuf
*m
)
2051 struct mbuf
*m0
= m
;
2054 if (sopt
->sopt_val
== NULL
)
2056 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
2057 if (sopt
->sopt_p
!= NULL
) {
2060 error
= copyout(mtod(m
, char *), sopt
->sopt_val
,
2067 bcopy(mtod(m
, char *), sopt
->sopt_val
, m
->m_len
);
2068 sopt
->sopt_valsize
-= m
->m_len
;
2069 (caddr_t
)sopt
->sopt_val
+= m
->m_len
;
2070 valsize
+= m
->m_len
;
2074 /* enough soopt buffer should be given from user-land */
2078 sopt
->sopt_valsize
= valsize
;