2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
63 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
64 * support for mandatory and extensible security protections. This notice
65 * is included in support of clause 2.2 (b) of the Apple Public License,
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
75 #include <sys/domain.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/sysctl.h>
80 #include <libkern/OSAtomic.h>
81 #include <kern/zalloc.h>
83 #include <pexpert/pexpert.h>
86 #include <net/route.h>
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/ip.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip_var.h>
95 #include <netinet/ip_mroute.h>
98 #include <netinet6/in6_pcb.h>
101 #include <netinet/ip_fw.h>
104 #include <netinet6/ipsec.h>
108 #include <netinet/ip_dummynet.h>
112 #include <security/mac_framework.h>
116 int rip_detach(struct socket
*);
117 int rip_abort(struct socket
*);
118 int rip_disconnect(struct socket
*);
119 int rip_bind(struct socket
*, struct sockaddr
*, struct proc
*);
120 int rip_connect(struct socket
*, struct sockaddr
*, struct proc
*);
121 int rip_shutdown(struct socket
*);
124 extern int ipsec_bypass
;
127 struct inpcbhead ripcb
;
128 struct inpcbinfo ripcbinfo
;
130 /* control hooks for ipfw and dummynet */
132 ip_fw_ctl_t
*ip_fw_ctl_ptr
;
134 ip_dn_ctl_t
*ip_dn_ctl_ptr
;
135 #endif /* DUMMYNET */
136 #endif /* IPFIREWALL */
139 * Nominal space allocated to a raw ip socket.
145 * Raw interface to IP protocol.
149 * Initialize raw connection block q.
154 struct inpcbinfo
*pcbinfo
;
157 ripcbinfo
.listhead
= &ripcb
;
159 * XXX We don't use the hash list for raw IP, but it's easier
160 * to allocate a one entry hash list than it is to check all
161 * over the place for hashbase == NULL.
163 ripcbinfo
.hashbase
= hashinit(1, M_PCB
, &ripcbinfo
.hashmask
);
164 ripcbinfo
.porthashbase
= hashinit(1, M_PCB
, &ripcbinfo
.porthashmask
);
166 ripcbinfo
.ipi_zone
= (void *) zinit(sizeof(struct inpcb
),
167 (4096 * sizeof(struct inpcb
)),
170 pcbinfo
= &ripcbinfo
;
172 * allocate lock group attribute and group for udp pcb mutexes
174 pcbinfo
->mtx_grp_attr
= lck_grp_attr_alloc_init();
176 pcbinfo
->mtx_grp
= lck_grp_alloc_init("ripcb", pcbinfo
->mtx_grp_attr
);
179 * allocate the lock attribute for udp pcb mutexes
181 pcbinfo
->mtx_attr
= lck_attr_alloc_init();
183 if ((pcbinfo
->mtx
= lck_rw_alloc_init(pcbinfo
->mtx_grp
, pcbinfo
->mtx_attr
)) == NULL
)
184 return; /* pretty much dead if this fails... */
188 static struct sockaddr_in ripsrc
= { sizeof(ripsrc
), AF_INET
, 0, {0}, {0,0,0,0,0,0,0,0,} };
190 * Setup generic address and protocol structures
191 * for raw_input routine, then pass them along with
199 register struct ip
*ip
= mtod(m
, struct ip
*);
200 register struct inpcb
*inp
;
201 struct inpcb
*last
= 0;
202 struct mbuf
*opts
= 0;
205 ripsrc
.sin_addr
= ip
->ip_src
;
206 lck_rw_lock_shared(ripcbinfo
.mtx
);
207 LIST_FOREACH(inp
, &ripcb
, inp_list
) {
209 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
212 if (inp
->inp_ip_p
&& (inp
->inp_ip_p
!= ip
->ip_p
))
214 if (inp
->inp_laddr
.s_addr
&&
215 inp
->inp_laddr
.s_addr
!= ip
->ip_dst
.s_addr
)
217 if (inp
->inp_faddr
.s_addr
&&
218 inp
->inp_faddr
.s_addr
!= ip
->ip_src
.s_addr
)
221 struct mbuf
*n
= m_copy(m
, 0, (int)M_COPYALL
);
224 /* check AH/ESP integrity. */
226 if (ipsec_bypass
== 0 && n
) {
227 if (ipsec4_in_reject_so(n
, last
->inp_socket
)) {
229 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
230 /* do not inject data to pcb */
236 if (n
&& skipit
== 0) {
237 if (mac_inpcb_check_deliver(last
, n
, AF_INET
,
242 if (n
&& skipit
== 0) {
244 if (last
->inp_flags
& INP_CONTROLOPTS
||
245 last
->inp_socket
->so_options
& SO_TIMESTAMP
)
246 ip_savecontrol(last
, &opts
, ip
, n
);
247 if (last
->inp_flags
& INP_STRIPHDR
) {
249 n
->m_pkthdr
.len
-= iphlen
;
252 // ###LOCK need to lock that socket?
253 if (sbappendaddr(&last
->inp_socket
->so_rcv
,
254 (struct sockaddr
*)&ripsrc
, n
,
255 opts
, &error
) != 0) {
256 sorwakeup(last
->inp_socket
);
260 /* should notify about lost packet */
261 kprintf("rip_input can't append to socket\n");
269 lck_rw_done(ripcbinfo
.mtx
);
271 /* check AH/ESP integrity. */
273 if (ipsec_bypass
== 0 && last
) {
274 if (ipsec4_in_reject_so(m
, last
->inp_socket
)) {
276 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
277 OSAddAtomic(1, &ipstat
.ips_delivered
);
278 /* do not inject data to pcb */
284 if (last
&& skipit
== 0) {
285 if (mac_inpcb_check_deliver(last
, m
, AF_INET
, SOCK_RAW
) != 0)
291 if (last
->inp_flags
& INP_CONTROLOPTS
||
292 last
->inp_socket
->so_options
& SO_TIMESTAMP
)
293 ip_savecontrol(last
, &opts
, ip
, m
);
294 if (last
->inp_flags
& INP_STRIPHDR
) {
296 m
->m_pkthdr
.len
-= iphlen
;
299 if (sbappendaddr(&last
->inp_socket
->so_rcv
,
300 (struct sockaddr
*)&ripsrc
, m
, opts
, NULL
) != 0) {
301 sorwakeup(last
->inp_socket
);
303 kprintf("rip_input(2) can't append to socket\n");
307 OSAddAtomic(1, &ipstat
.ips_noproto
);
308 OSAddAtomic(-1, &ipstat
.ips_delivered
);
314 * Generate IP header and pass packet to ip_output.
315 * Tack on options user may have setup with control call.
318 rip_output(m
, so
, dst
)
319 register struct mbuf
*m
;
323 register struct ip
*ip
;
324 register struct inpcb
*inp
= sotoinpcb(so
);
325 int flags
= (so
->so_options
& SO_DONTROUTE
) | IP_ALLOWBROADCAST
;
326 struct ip_out_args ipoa
;
329 /* If socket was bound to an ifindex, tell ip_output about it */
330 ipoa
.ipoa_ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
331 inp
->inp_boundif
: IFSCOPE_NONE
;
335 * If the user handed us a complete IP packet, use it.
336 * Otherwise, allocate an mbuf for a header and fill it in.
338 if ((inp
->inp_flags
& INP_HDRINCL
) == 0) {
339 if (m
->m_pkthdr
.len
+ sizeof(struct ip
) > IP_MAXPACKET
) {
343 M_PREPEND(m
, sizeof(struct ip
), M_WAIT
);
346 ip
= mtod(m
, struct ip
*);
347 ip
->ip_tos
= inp
->inp_ip_tos
;
349 ip
->ip_p
= inp
->inp_ip_p
;
350 ip
->ip_len
= m
->m_pkthdr
.len
;
351 ip
->ip_src
= inp
->inp_laddr
;
352 ip
->ip_dst
.s_addr
= dst
;
353 ip
->ip_ttl
= inp
->inp_ip_ttl
;
355 if (m
->m_pkthdr
.len
> IP_MAXPACKET
) {
359 ip
= mtod(m
, struct ip
*);
360 /* don't allow both user specified and setsockopt options,
361 and don't allow packet length sizes that will crash */
362 if (((IP_VHL_HL(ip
->ip_vhl
) != (sizeof (*ip
) >> 2))
364 || (ip
->ip_len
> m
->m_pkthdr
.len
)
365 || (ip
->ip_len
< (IP_VHL_HL(ip
->ip_vhl
) << 2))) {
371 ip
->ip_id
= ip_randomid();
373 ip
->ip_id
= htons(ip_id
++);
375 /* XXX prevent ip_output from overwriting header fields */
376 flags
|= IP_RAWOUTPUT
;
377 OSAddAtomic(1, &ipstat
.ips_rawout
);
381 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, so
) != 0) {
387 if (inp
->inp_route
.ro_rt
!= NULL
&&
388 inp
->inp_route
.ro_rt
->generation_id
!= route_generation
) {
389 rtfree(inp
->inp_route
.ro_rt
);
390 inp
->inp_route
.ro_rt
= NULL
;
394 if (soisbackground(so
))
395 m_prio_background(m
);
396 #endif /* PKT_PRIORITY */
399 mac_mbuf_label_associate_inpcb(inp
, m
);
403 * The domain lock is held across ip_output, so it is okay
404 * to pass the PCB cached route pointer directly to IP and
405 * the modules beneath it.
407 error
= ip_output(m
, inp
->inp_options
, &inp
->inp_route
, flags
,
408 inp
->inp_moptions
, &ipoa
);
410 #if IFNET_ROUTE_REFCNT
412 * Always discard the cached route for unconnected socket
413 * or if it is a non-unicast route.
415 if (inp
->inp_route
.ro_rt
!= NULL
&&
416 ((inp
->inp_route
.ro_rt
->rt_flags
& (RTF_MULTICAST
|RTF_BROADCAST
)) ||
417 inp
->inp_socket
== NULL
||
418 inp
->inp_socket
->so_state
!= SS_ISCONNECTED
)) {
419 rtfree(inp
->inp_route
.ro_rt
);
420 inp
->inp_route
.ro_rt
= NULL
;
422 #endif /* IFNET_ROUTE_REFCNT */
436 if (!DUMMYNET_LOADED
)
438 #endif /* DUMMYNET */
441 return err
== 0 && ip_fw_ctl_ptr
== NULL
? -1 : err
;
443 #endif /* IPFIREWALL */
446 * Raw IP socket option processing.
449 rip_ctloutput(so
, sopt
)
451 struct sockopt
*sopt
;
453 struct inpcb
*inp
= sotoinpcb(so
);
456 if (sopt
->sopt_level
!= IPPROTO_IP
)
461 switch (sopt
->sopt_dir
) {
463 switch (sopt
->sopt_name
) {
465 optval
= inp
->inp_flags
& INP_HDRINCL
;
466 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
470 optval
= inp
->inp_flags
& INP_STRIPHDR
;
471 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
479 if (ip_fw_ctl_ptr
== 0)
481 if (ip_fw_ctl_ptr
&& error
== 0)
482 error
= ip_fw_ctl_ptr(sopt
);
486 #endif /* IPFIREWALL */
489 case IP_DUMMYNET_GET
:
491 error
= ip_dn_ctl_ptr(sopt
);
495 #endif /* DUMMYNET */
506 error
= ip_mrouter_get(so
, sopt
);
508 #endif /* MROUTING */
511 error
= ip_ctloutput(so
, sopt
);
517 switch (sopt
->sopt_name
) {
519 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
524 inp
->inp_flags
|= INP_HDRINCL
;
526 inp
->inp_flags
&= ~INP_HDRINCL
;
530 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
535 inp
->inp_flags
|= INP_STRIPHDR
;
537 inp
->inp_flags
&= ~INP_STRIPHDR
;
549 case IP_OLD_FW_FLUSH
:
551 case IP_OLD_FW_RESETLOG
:
552 if (ip_fw_ctl_ptr
== 0)
554 if (ip_fw_ctl_ptr
&& error
== 0)
555 error
= ip_fw_ctl_ptr(sopt
);
559 #endif /* IPFIREWALL */
562 case IP_DUMMYNET_CONFIGURE
:
563 case IP_DUMMYNET_DEL
:
564 case IP_DUMMYNET_FLUSH
:
566 error
= ip_dn_ctl_ptr(sopt
);
568 error
= ENOPROTOOPT
;
574 error
= ip_rsvp_init(so
);
578 error
= ip_rsvp_done();
581 /* XXX - should be combined */
583 error
= ip_rsvp_vif_init(so
, sopt
);
586 case IP_RSVP_VIF_OFF
:
587 error
= ip_rsvp_vif_done(so
, sopt
);
598 error
= ip_mrouter_set(so
, sopt
);
600 #endif /* MROUTING */
603 error
= ip_ctloutput(so
, sopt
);
613 * This function exists solely to receive the PRC_IFDOWN messages which
614 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
615 * and calls in_ifadown() to remove all routes corresponding to that address.
616 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
625 struct in_ifaddr
*ia
;
632 lck_rw_lock_shared(in_ifaddr_rwlock
);
633 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
634 ia
= ia
->ia_link
.tqe_next
) {
635 if (ia
->ia_ifa
.ifa_addr
== sa
636 && (ia
->ia_flags
& IFA_ROUTE
)) {
639 lck_rw_done(in_ifaddr_rwlock
);
640 lck_mtx_lock(rnh_lock
);
642 * in_ifscrub kills the interface route.
644 in_ifscrub(ia
->ia_ifp
, ia
, 1);
646 * in_ifadown gets rid of all the rest of
647 * the routes. This is not quite the right
648 * thing to do, but at least if we are running
649 * a routing process they will come back.
651 in_ifadown(&ia
->ia_ifa
, 1);
652 lck_mtx_unlock(rnh_lock
);
653 ifafree(&ia
->ia_ifa
);
658 lck_rw_done(in_ifaddr_rwlock
);
662 lck_rw_lock_shared(in_ifaddr_rwlock
);
663 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
664 ia
= ia
->ia_link
.tqe_next
) {
665 if (ia
->ia_ifa
.ifa_addr
== sa
)
668 if (ia
== 0 || (ia
->ia_flags
& IFA_ROUTE
)) {
669 lck_rw_done(in_ifaddr_rwlock
);
673 lck_rw_done(in_ifaddr_rwlock
);
676 ifp
= ia
->ia_ifa
.ifa_ifp
;
678 if ((ifp
->if_flags
& IFF_LOOPBACK
)
679 || (ifp
->if_flags
& IFF_POINTOPOINT
))
682 err
= rtinit(&ia
->ia_ifa
, RTM_ADD
, flags
);
684 ia
->ia_flags
|= IFA_ROUTE
;
685 ifafree(&ia
->ia_ifa
);
690 u_int32_t rip_sendspace
= RIPSNDQ
;
691 u_int32_t rip_recvspace
= RIPRCVQ
;
693 SYSCTL_INT(_net_inet_raw
, OID_AUTO
, maxdgram
, CTLFLAG_RW
,
694 &rip_sendspace
, 0, "Maximum outgoing raw IP datagram size");
695 SYSCTL_INT(_net_inet_raw
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
696 &rip_recvspace
, 0, "Maximum incoming raw IP datagram size");
699 rip_attach(struct socket
*so
, int proto
, struct proc
*p
)
707 if ((so
->so_state
& SS_PRIV
) == 0)
710 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
713 error
= in_pcballoc(so
, &ripcbinfo
, p
);
716 inp
= (struct inpcb
*)so
->so_pcb
;
717 inp
->inp_vflag
|= INP_IPV4
;
718 inp
->inp_ip_p
= proto
;
719 inp
->inp_ip_ttl
= ip_defttl
;
723 __private_extern__
int
724 rip_detach(struct socket
*so
)
732 if (so
== ip_mrouter
)
734 ip_rsvp_force_done(so
);
737 #endif /* MROUTING */
742 __private_extern__
int
743 rip_abort(struct socket
*so
)
745 soisdisconnected(so
);
746 return rip_detach(so
);
749 __private_extern__
int
750 rip_disconnect(struct socket
*so
)
752 if ((so
->so_state
& SS_ISCONNECTED
) == 0)
754 return rip_abort(so
);
757 __private_extern__
int
758 rip_bind(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
760 struct inpcb
*inp
= sotoinpcb(so
);
761 struct sockaddr_in
*addr
= (struct sockaddr_in
*)nam
;
762 struct ifaddr
*ifa
= NULL
;
764 if (nam
->sa_len
!= sizeof(*addr
))
767 if (TAILQ_EMPTY(&ifnet_head
) || ((addr
->sin_family
!= AF_INET
) &&
768 (addr
->sin_family
!= AF_IMPLINK
)) ||
769 (addr
->sin_addr
.s_addr
&&
770 (ifa
= ifa_ifwithaddr((struct sockaddr
*)addr
)) == 0)) {
771 return EADDRNOTAVAIL
;
777 inp
->inp_laddr
= addr
->sin_addr
;
781 __private_extern__
int
782 rip_connect(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
784 struct inpcb
*inp
= sotoinpcb(so
);
785 struct sockaddr_in
*addr
= (struct sockaddr_in
*)nam
;
787 if (nam
->sa_len
!= sizeof(*addr
))
789 if (TAILQ_EMPTY(&ifnet_head
))
790 return EADDRNOTAVAIL
;
791 if ((addr
->sin_family
!= AF_INET
) &&
792 (addr
->sin_family
!= AF_IMPLINK
))
794 inp
->inp_faddr
= addr
->sin_addr
;
799 __private_extern__
int
800 rip_shutdown(struct socket
*so
)
806 __private_extern__
int
807 rip_send(struct socket
*so
, __unused
int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
808 __unused
struct mbuf
*control
, __unused
struct proc
*p
)
810 struct inpcb
*inp
= sotoinpcb(so
);
811 register u_int32_t dst
;
813 if (so
->so_state
& SS_ISCONNECTED
) {
818 dst
= inp
->inp_faddr
.s_addr
;
824 dst
= ((struct sockaddr_in
*)nam
)->sin_addr
.s_addr
;
826 return rip_output(m
, so
, dst
);
829 /* note: rip_unlock is called from different protos instead of the generic socket_unlock,
830 * it will handle the socket dealloc on last reference
833 rip_unlock(struct socket
*so
, int refcount
, void *debug
)
836 struct inpcb
*inp
= sotoinpcb(so
);
839 lr_saved
= __builtin_return_address(0);
844 if (so
->so_usecount
<= 0) {
845 panic("rip_unlock: bad refoucnt so=%p val=%x lrh= %s\n",
846 so
, so
->so_usecount
, solockhistory_nr(so
));
850 if (so
->so_usecount
== 0 && (inp
->inp_wantcnt
== WNT_STOPUSING
)) {
851 /* cleanup after last reference */
852 lck_mtx_unlock(so
->so_proto
->pr_domain
->dom_mtx
);
853 lck_rw_lock_exclusive(ripcbinfo
.mtx
);
854 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
856 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
863 lck_rw_done(ripcbinfo
.mtx
);
867 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
868 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
869 lck_mtx_unlock(so
->so_proto
->pr_domain
->dom_mtx
);
874 rip_pcblist SYSCTL_HANDLER_ARGS
876 #pragma unused(oidp, arg1, arg2)
878 struct inpcb
*inp
, **inp_list
;
883 * The process of preparing the TCB list is too time-consuming and
884 * resource-intensive to repeat twice on every request.
886 lck_rw_lock_exclusive(ripcbinfo
.mtx
);
887 if (req
->oldptr
== USER_ADDR_NULL
) {
888 n
= ripcbinfo
.ipi_count
;
889 req
->oldidx
= 2 * (sizeof xig
)
890 + (n
+ n
/8) * sizeof(struct xinpcb
);
891 lck_rw_done(ripcbinfo
.mtx
);
895 if (req
->newptr
!= USER_ADDR_NULL
) {
896 lck_rw_done(ripcbinfo
.mtx
);
901 * OK, now we're committed to doing something.
903 gencnt
= ripcbinfo
.ipi_gencnt
;
904 n
= ripcbinfo
.ipi_count
;
906 bzero(&xig
, sizeof(xig
));
907 xig
.xig_len
= sizeof xig
;
909 xig
.xig_gen
= gencnt
;
910 xig
.xig_sogen
= so_gencnt
;
911 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
913 lck_rw_done(ripcbinfo
.mtx
);
917 * We are done if there is no pcb
920 lck_rw_done(ripcbinfo
.mtx
);
924 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
926 lck_rw_done(ripcbinfo
.mtx
);
930 for (inp
= ripcbinfo
.listhead
->lh_first
, i
= 0; inp
&& i
< n
;
931 inp
= inp
->inp_list
.le_next
) {
932 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
)
938 for (i
= 0; i
< n
; i
++) {
940 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
943 bzero(&xi
, sizeof(xi
));
944 xi
.xi_len
= sizeof xi
;
945 /* XXX should avoid extra copy */
946 inpcb_to_compat(inp
, &xi
.xi_inp
);
948 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
949 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
954 * Give the user an updated idea of our state.
955 * If the generation differs from what we told
956 * her before, she knows that something happened
957 * while we were processing this request, and it
958 * might be necessary to retry.
960 bzero(&xig
, sizeof(xig
));
961 xig
.xig_len
= sizeof xig
;
962 xig
.xig_gen
= ripcbinfo
.ipi_gencnt
;
963 xig
.xig_sogen
= so_gencnt
;
964 xig
.xig_count
= ripcbinfo
.ipi_count
;
965 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
967 FREE(inp_list
, M_TEMP
);
968 lck_rw_done(ripcbinfo
.mtx
);
972 SYSCTL_PROC(_net_inet_raw
, OID_AUTO
/*XXX*/, pcblist
, CTLFLAG_RD
, 0, 0,
973 rip_pcblist
, "S,xinpcb", "List of active raw IP sockets");
978 rip_pcblist64 SYSCTL_HANDLER_ARGS
980 #pragma unused(oidp, arg1, arg2)
982 struct inpcb
*inp
, **inp_list
;
987 * The process of preparing the TCB list is too time-consuming and
988 * resource-intensive to repeat twice on every request.
990 lck_rw_lock_exclusive(ripcbinfo
.mtx
);
991 if (req
->oldptr
== USER_ADDR_NULL
) {
992 n
= ripcbinfo
.ipi_count
;
993 req
->oldidx
= 2 * (sizeof xig
)
994 + (n
+ n
/8) * sizeof(struct xinpcb64
);
995 lck_rw_done(ripcbinfo
.mtx
);
999 if (req
->newptr
!= USER_ADDR_NULL
) {
1000 lck_rw_done(ripcbinfo
.mtx
);
1005 * OK, now we're committed to doing something.
1007 gencnt
= ripcbinfo
.ipi_gencnt
;
1008 n
= ripcbinfo
.ipi_count
;
1010 bzero(&xig
, sizeof(xig
));
1011 xig
.xig_len
= sizeof xig
;
1013 xig
.xig_gen
= gencnt
;
1014 xig
.xig_sogen
= so_gencnt
;
1015 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1017 lck_rw_done(ripcbinfo
.mtx
);
1021 * We are done if there is no pcb
1024 lck_rw_done(ripcbinfo
.mtx
);
1028 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
1029 if (inp_list
== 0) {
1030 lck_rw_done(ripcbinfo
.mtx
);
1034 for (inp
= ripcbinfo
.listhead
->lh_first
, i
= 0; inp
&& i
< n
;
1035 inp
= inp
->inp_list
.le_next
) {
1036 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
)
1037 inp_list
[i
++] = inp
;
1042 for (i
= 0; i
< n
; i
++) {
1044 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
1047 bzero(&xi
, sizeof(xi
));
1048 xi
.xi_len
= sizeof xi
;
1049 inpcb_to_xinpcb64(inp
, &xi
);
1050 if (inp
->inp_socket
)
1051 sotoxsocket64(inp
->inp_socket
, &xi
.xi_socket
);
1052 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
1057 * Give the user an updated idea of our state.
1058 * If the generation differs from what we told
1059 * her before, she knows that something happened
1060 * while we were processing this request, and it
1061 * might be necessary to retry.
1063 bzero(&xig
, sizeof(xig
));
1064 xig
.xig_len
= sizeof xig
;
1065 xig
.xig_gen
= ripcbinfo
.ipi_gencnt
;
1066 xig
.xig_sogen
= so_gencnt
;
1067 xig
.xig_count
= ripcbinfo
.ipi_count
;
1068 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1070 FREE(inp_list
, M_TEMP
);
1071 lck_rw_done(ripcbinfo
.mtx
);
1075 SYSCTL_PROC(_net_inet_raw
, OID_AUTO
, pcblist64
, CTLFLAG_RD
, 0, 0,
1076 rip_pcblist64
, "S,xinpcb64", "List of active raw IP sockets");
1078 #endif /* !CONFIG_EMBEDDED */
1080 struct pr_usrreqs rip_usrreqs
= {
1081 rip_abort
, pru_accept_notsupp
, rip_attach
, rip_bind
, rip_connect
,
1082 pru_connect2_notsupp
, in_control
, rip_detach
, rip_disconnect
,
1083 pru_listen_notsupp
, in_setpeeraddr
, pru_rcvd_notsupp
,
1084 pru_rcvoob_notsupp
, rip_send
, pru_sense_null
, rip_shutdown
,
1085 in_setsockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
1087 /* DSEP Review Done pl-20051213-v02 @3253 */