2 * Copyright (c) 2000-2016 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>
74 #include <sys/mcache.h>
76 #include <sys/domain.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/sysctl.h>
81 #include <libkern/OSAtomic.h>
82 #include <kern/zalloc.h>
84 #include <pexpert/pexpert.h>
87 #include <net/route.h>
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/in_tclass.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip_var.h>
99 #include <netinet6/in6_pcb.h>
102 #include <netinet/ip_fw.h>
105 #include <netinet6/ipsec.h>
109 #include <netinet/ip_dummynet.h>
113 #include <security/mac_framework.h>
117 int rip_detach(struct socket
*);
118 int rip_abort(struct socket
*);
119 int rip_disconnect(struct socket
*);
120 int rip_bind(struct socket
*, struct sockaddr
*, struct proc
*);
121 int rip_connect(struct socket
*, struct sockaddr
*, struct proc
*);
122 int rip_shutdown(struct socket
*);
124 struct inpcbhead ripcb
;
125 struct inpcbinfo ripcbinfo
;
127 /* control hooks for ipfw and dummynet */
129 ip_fw_ctl_t
*ip_fw_ctl_ptr
;
130 #endif /* IPFIREWALL */
132 ip_dn_ctl_t
*ip_dn_ctl_ptr
;
133 #endif /* DUMMYNET */
136 * Nominal space allocated to a raw ip socket.
142 * Raw interface to IP protocol.
146 * Initialize raw connection block q.
149 rip_init(struct protosw
*pp
, struct domain
*dp
)
152 static int rip_initialized
= 0;
153 struct inpcbinfo
*pcbinfo
;
155 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
|PR_ATTACHED
)) == PR_ATTACHED
);
162 ripcbinfo
.ipi_listhead
= &ripcb
;
164 * XXX We don't use the hash list for raw IP, but it's easier
165 * to allocate a one entry hash list than it is to check all
166 * over the place for ipi_hashbase == NULL.
168 ripcbinfo
.ipi_hashbase
= hashinit(1, M_PCB
, &ripcbinfo
.ipi_hashmask
);
169 ripcbinfo
.ipi_porthashbase
= hashinit(1, M_PCB
, &ripcbinfo
.ipi_porthashmask
);
171 ripcbinfo
.ipi_zone
= zinit(sizeof(struct inpcb
),
172 (4096 * sizeof(struct inpcb
)), 4096, "ripzone");
174 pcbinfo
= &ripcbinfo
;
176 * allocate lock group attribute and group for udp pcb mutexes
178 pcbinfo
->ipi_lock_grp_attr
= lck_grp_attr_alloc_init();
179 pcbinfo
->ipi_lock_grp
= lck_grp_alloc_init("ripcb", pcbinfo
->ipi_lock_grp_attr
);
182 * allocate the lock attribute for udp pcb mutexes
184 pcbinfo
->ipi_lock_attr
= lck_attr_alloc_init();
185 if ((pcbinfo
->ipi_lock
= lck_rw_alloc_init(pcbinfo
->ipi_lock_grp
,
186 pcbinfo
->ipi_lock_attr
)) == NULL
) {
187 panic("%s: unable to allocate PCB lock\n", __func__
);
191 in_pcbinfo_attach(&ripcbinfo
);
194 static struct sockaddr_in ripsrc
= { sizeof(ripsrc
), AF_INET
, 0, {0}, {0,0,0,0,0,0,0,0,} };
196 * Setup generic address and protocol structures
197 * for raw_input routine, then pass them along with
201 rip_input(struct mbuf
*m
, int iphlen
)
203 struct ip
*ip
= mtod(m
, struct ip
*);
205 struct inpcb
*last
= 0;
206 struct mbuf
*opts
= 0;
207 int skipit
= 0, ret
= 0;
208 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
210 /* Expect 32-bit aligned data pointer on strict-align platforms */
211 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
213 ripsrc
.sin_addr
= ip
->ip_src
;
214 lck_rw_lock_shared(ripcbinfo
.ipi_lock
);
215 LIST_FOREACH(inp
, &ripcb
, inp_list
) {
217 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
220 if (inp
->inp_ip_p
&& (inp
->inp_ip_p
!= ip
->ip_p
))
222 if (inp
->inp_laddr
.s_addr
&&
223 inp
->inp_laddr
.s_addr
!= ip
->ip_dst
.s_addr
)
225 if (inp
->inp_faddr
.s_addr
&&
226 inp
->inp_faddr
.s_addr
!= ip
->ip_src
.s_addr
)
228 if (inp_restricted_recv(inp
, ifp
))
231 struct mbuf
*n
= m_copy(m
, 0, (int)M_COPYALL
);
236 if (n
&& !necp_socket_is_allowed_to_send_recv_v4(last
, 0, 0,
237 &ip
->ip_dst
, &ip
->ip_src
, ifp
, NULL
, NULL
)) {
239 /* do not inject data to pcb */
244 if (n
&& skipit
== 0) {
245 if (mac_inpcb_check_deliver(last
, n
, AF_INET
,
252 if (n
&& skipit
== 0) {
254 if ((last
->inp_flags
& INP_CONTROLOPTS
) != 0 ||
255 (last
->inp_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
256 (last
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
257 ret
= ip_savecontrol(last
, &opts
, ip
, n
);
265 if (last
->inp_flags
& INP_STRIPHDR
) {
267 n
->m_pkthdr
.len
-= iphlen
;
270 so_recv_data_stat(last
->inp_socket
, m
, 0);
271 if (sbappendaddr(&last
->inp_socket
->so_rcv
,
272 (struct sockaddr
*)&ripsrc
, n
,
273 opts
, &error
) != 0) {
274 sorwakeup(last
->inp_socket
);
277 /* should notify about lost packet */
278 ipstat
.ips_raw_sappend_fail
++;
289 if (last
&& !necp_socket_is_allowed_to_send_recv_v4(last
, 0, 0,
290 &ip
->ip_dst
, &ip
->ip_src
, ifp
, NULL
, NULL
)) {
292 OSAddAtomic(1, &ipstat
.ips_delivered
);
293 /* do not inject data to pcb */
298 if (last
&& skipit
== 0) {
299 if (mac_inpcb_check_deliver(last
, m
, AF_INET
, SOCK_RAW
) != 0) {
307 if ((last
->inp_flags
& INP_CONTROLOPTS
) != 0 ||
308 (last
->inp_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
309 (last
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
310 ret
= ip_savecontrol(last
, &opts
, ip
, m
);
317 if (last
->inp_flags
& INP_STRIPHDR
) {
319 m
->m_pkthdr
.len
-= iphlen
;
322 so_recv_data_stat(last
->inp_socket
, m
, 0);
323 if (sbappendaddr(&last
->inp_socket
->so_rcv
,
324 (struct sockaddr
*)&ripsrc
, m
, opts
, NULL
) != 0) {
325 sorwakeup(last
->inp_socket
);
327 ipstat
.ips_raw_sappend_fail
++;
331 OSAddAtomic(1, &ipstat
.ips_noproto
);
332 OSAddAtomic(-1, &ipstat
.ips_delivered
);
337 * Keep the list locked because socket filter may force the socket lock
338 * to be released when calling sbappendaddr() -- see rdar://7627704
340 lck_rw_done(ripcbinfo
.ipi_lock
);
344 * Generate IP header and pass packet to ip_output.
345 * Tack on options user may have setup with control call.
352 struct mbuf
*control
)
355 struct inpcb
*inp
= sotoinpcb(so
);
356 int flags
= (so
->so_options
& SO_DONTROUTE
) | IP_ALLOWBROADCAST
;
357 struct ip_out_args ipoa
=
358 { IFSCOPE_NONE
, { 0 }, IPOAF_SELECT_SRCIF
, 0, 0, 0 };
359 struct ip_moptions
*imo
;
361 int sotc
= SO_TC_UNSPEC
;
362 int netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
364 if (control
!= NULL
) {
365 sotc
= so_tc_from_control(control
, &netsvctype
);
370 if (sotc
== SO_TC_UNSPEC
) {
371 sotc
= so
->so_traffic_class
;
372 netsvctype
= so
->so_netsvctype
;
377 || (necp_socket_should_use_flow_divert(inp
))
382 VERIFY(control
== NULL
);
383 return (inp
== NULL
? EINVAL
: EPROTOTYPE
);
387 /* If socket was bound to an ifindex, tell ip_output about it */
388 if (inp
->inp_flags
& INP_BOUND_IF
) {
389 ipoa
.ipoa_boundif
= inp
->inp_boundifp
->if_index
;
390 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
392 if (INP_NO_CELLULAR(inp
))
393 ipoa
.ipoa_flags
|= IPOAF_NO_CELLULAR
;
394 if (INP_NO_EXPENSIVE(inp
))
395 ipoa
.ipoa_flags
|= IPOAF_NO_EXPENSIVE
;
396 if (INP_AWDL_UNRESTRICTED(inp
))
397 ipoa
.ipoa_flags
|= IPOAF_AWDL_UNRESTRICTED
;
398 ipoa
.ipoa_sotc
= sotc
;
399 ipoa
.ipoa_netsvctype
= netsvctype
;
401 if (inp
->inp_flowhash
== 0)
402 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
405 * If the user handed us a complete IP packet, use it.
406 * Otherwise, allocate an mbuf for a header and fill it in.
408 if ((inp
->inp_flags
& INP_HDRINCL
) == 0) {
409 if (m
->m_pkthdr
.len
+ sizeof(struct ip
) > IP_MAXPACKET
) {
413 M_PREPEND(m
, sizeof(struct ip
), M_WAIT
, 1);
416 ip
= mtod(m
, struct ip
*);
417 ip
->ip_tos
= inp
->inp_ip_tos
;
419 ip
->ip_p
= inp
->inp_ip_p
;
420 ip
->ip_len
= m
->m_pkthdr
.len
;
421 ip
->ip_src
= inp
->inp_laddr
;
422 ip
->ip_dst
.s_addr
= dst
;
423 ip
->ip_ttl
= inp
->inp_ip_ttl
;
425 if (m
->m_pkthdr
.len
> IP_MAXPACKET
) {
429 ip
= mtod(m
, struct ip
*);
430 /* don't allow both user specified and setsockopt options,
431 and don't allow packet length sizes that will crash */
432 if (((IP_VHL_HL(ip
->ip_vhl
) != (sizeof (*ip
) >> 2))
434 || (ip
->ip_len
> m
->m_pkthdr
.len
)
435 || (ip
->ip_len
< (IP_VHL_HL(ip
->ip_vhl
) << 2))) {
440 ip
->ip_id
= ip_randomid();
441 /* XXX prevent ip_output from overwriting header fields */
442 flags
|= IP_RAWOUTPUT
;
443 OSAddAtomic(1, &ipstat
.ips_rawout
);
446 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
)
447 ipoa
.ipoa_flags
|= IPOAF_BOUND_SRCADDR
;
451 necp_kernel_policy_id policy_id
;
452 u_int32_t route_rule_id
;
455 * We need a route to perform NECP route rule checks
457 if (net_qos_policy_restricted
!= 0 &&
458 ROUTE_UNUSABLE(&inp
->inp_route
)) {
459 struct sockaddr_in to
;
460 struct sockaddr_in from
;
461 struct in_addr laddr
= ip
->ip_src
;
463 ROUTE_RELEASE(&inp
->inp_route
);
465 bzero(&from
, sizeof(struct sockaddr_in
));
466 from
.sin_family
= AF_INET
;
467 from
.sin_len
= sizeof(struct sockaddr_in
);
468 from
.sin_addr
= laddr
;
470 bzero(&to
, sizeof(struct sockaddr_in
));
471 to
.sin_family
= AF_INET
;
472 to
.sin_len
= sizeof(struct sockaddr_in
);
473 to
.sin_addr
.s_addr
= ip
->ip_dst
.s_addr
;
475 if ((error
= in_pcbladdr(inp
, (struct sockaddr
*)&to
,
476 &laddr
, ipoa
.ipoa_boundif
, NULL
, 1)) != 0) {
477 printf("%s in_pcbladdr(%p) error %d\n",
478 __func__
, inp
, error
);
483 inp_update_necp_policy(inp
, (struct sockaddr
*)&from
,
484 (struct sockaddr
*)&to
, ipoa
.ipoa_boundif
);
485 inp
->inp_policyresult
.results
.qos_marking_gencount
= 0;
488 if (!necp_socket_is_allowed_to_send_recv_v4(inp
, 0, 0,
489 &ip
->ip_src
, &ip
->ip_dst
, NULL
, &policy_id
, &route_rule_id
)) {
491 return(EHOSTUNREACH
);
494 necp_mark_packet_from_socket(m
, inp
, policy_id
, route_rule_id
);
496 if (net_qos_policy_restricted
!= 0) {
497 struct ifnet
*rt_ifp
= NULL
;
499 if (inp
->inp_route
.ro_rt
!= NULL
)
500 rt_ifp
= inp
->inp_route
.ro_rt
->rt_ifp
;
502 printf("%s inp %p last_pid %u inp_boundifp %d inp_last_outifp %d rt_ifp %d route_rule_id %u\n",
504 inp
->inp_socket
!= NULL
? inp
->inp_socket
->last_pid
: -1,
505 inp
->inp_boundifp
!= NULL
? inp
->inp_boundifp
->if_index
: -1,
506 inp
->inp_last_outifp
!= NULL
? inp
->inp_last_outifp
->if_index
: -1,
507 rt_ifp
!= NULL
? rt_ifp
->if_index
: -1,
509 necp_socket_update_qos_marking(inp
, inp
->inp_route
.ro_rt
,
510 NULL
, route_rule_id
);
514 if ((so
->so_flags1
& SOF1_QOSMARKING_ALLOWED
))
515 ipoa
.ipoa_flags
|= IPOAF_QOSMARKING_ALLOWED
;
518 if (inp
->inp_sp
!= NULL
&& ipsec_setsocket(m
, so
) != 0) {
524 if (ROUTE_UNUSABLE(&inp
->inp_route
))
525 ROUTE_RELEASE(&inp
->inp_route
);
527 set_packet_service_class(m
, so
, sotc
, 0);
528 m
->m_pkthdr
.pkt_flowsrc
= FLOWSRC_INPCB
;
529 m
->m_pkthdr
.pkt_flowid
= inp
->inp_flowhash
;
530 m
->m_pkthdr
.pkt_flags
|= (PKTF_FLOW_ID
| PKTF_FLOW_LOCALSRC
|
532 m
->m_pkthdr
.pkt_proto
= inp
->inp_ip_p
;
535 mac_mbuf_label_associate_inpcb(inp
, m
);
538 imo
= inp
->inp_moptions
;
542 * The domain lock is held across ip_output, so it is okay
543 * to pass the PCB cached route pointer directly to IP and
544 * the modules beneath it.
546 // TODO: PASS DOWN ROUTE RULE ID
547 error
= ip_output(m
, inp
->inp_options
, &inp
->inp_route
, flags
,
553 if (inp
->inp_route
.ro_rt
!= NULL
) {
554 struct rtentry
*rt
= inp
->inp_route
.ro_rt
;
557 if ((rt
->rt_flags
& (RTF_MULTICAST
|RTF_BROADCAST
)) ||
558 inp
->inp_socket
== NULL
||
559 !(inp
->inp_socket
->so_state
& SS_ISCONNECTED
)) {
560 rt
= NULL
; /* unusable */
563 * Always discard the cached route for unconnected
564 * socket or if it is a multicast route.
567 ROUTE_RELEASE(&inp
->inp_route
);
570 * If this is a connected socket and the destination
571 * route is unicast, update outif with that of the
572 * route interface used by IP.
574 if (rt
!= NULL
&& (outif
= rt
->rt_ifp
) != inp
->inp_last_outifp
)
575 inp
->inp_last_outifp
= outif
;
577 ROUTE_RELEASE(&inp
->inp_route
);
581 * If output interface was cellular/expensive, and this socket is
582 * denied access to it, generate an event.
584 if (error
!= 0 && (ipoa
.ipoa_retflags
& IPOARF_IFDENIED
) &&
585 (INP_NO_CELLULAR(inp
) || INP_NO_EXPENSIVE(inp
)))
586 soevent(so
, (SO_FILT_HINT_LOCKED
|SO_FILT_HINT_IFDENIED
));
600 if (!DUMMYNET_LOADED
)
602 #endif /* DUMMYNET */
605 return err
== 0 && ip_fw_ctl_ptr
== NULL
? -1 : err
;
607 #endif /* IPFIREWALL */
610 * Raw IP socket option processing.
613 rip_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
615 struct inpcb
*inp
= sotoinpcb(so
);
618 /* Allow <SOL_SOCKET,SO_FLUSH> at this level */
619 if (sopt
->sopt_level
!= IPPROTO_IP
&&
620 !(sopt
->sopt_level
== SOL_SOCKET
&& sopt
->sopt_name
== SO_FLUSH
))
625 switch (sopt
->sopt_dir
) {
627 switch (sopt
->sopt_name
) {
629 optval
= inp
->inp_flags
& INP_HDRINCL
;
630 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
634 optval
= inp
->inp_flags
& INP_STRIPHDR
;
635 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
643 if (ip_fw_ctl_ptr
== 0)
645 if (ip_fw_ctl_ptr
&& error
== 0)
646 error
= ip_fw_ctl_ptr(sopt
);
650 #endif /* IPFIREWALL */
653 case IP_DUMMYNET_GET
:
654 if (!DUMMYNET_LOADED
)
657 error
= ip_dn_ctl_ptr(sopt
);
661 #endif /* DUMMYNET */
664 error
= ip_ctloutput(so
, sopt
);
670 switch (sopt
->sopt_name
) {
672 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
677 inp
->inp_flags
|= INP_HDRINCL
;
679 inp
->inp_flags
&= ~INP_HDRINCL
;
683 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
688 inp
->inp_flags
|= INP_STRIPHDR
;
690 inp
->inp_flags
&= ~INP_STRIPHDR
;
701 case IP_OLD_FW_FLUSH
:
703 case IP_OLD_FW_RESETLOG
:
704 if (ip_fw_ctl_ptr
== 0)
706 if (ip_fw_ctl_ptr
&& error
== 0)
707 error
= ip_fw_ctl_ptr(sopt
);
711 #endif /* IPFIREWALL */
714 case IP_DUMMYNET_CONFIGURE
:
715 case IP_DUMMYNET_DEL
:
716 case IP_DUMMYNET_FLUSH
:
717 if (!DUMMYNET_LOADED
)
720 error
= ip_dn_ctl_ptr(sopt
);
722 error
= ENOPROTOOPT
;
727 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
728 sizeof (optval
))) != 0)
731 error
= inp_flush(inp
, optval
);
735 error
= ip_ctloutput(so
, sopt
);
745 * This function exists solely to receive the PRC_IFDOWN messages which
746 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
747 * and calls in_ifadown() to remove all routes corresponding to that address.
748 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
757 struct in_ifaddr
*ia
;
764 lck_rw_lock_shared(in_ifaddr_rwlock
);
765 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
766 ia
= ia
->ia_link
.tqe_next
) {
767 IFA_LOCK(&ia
->ia_ifa
);
768 if (ia
->ia_ifa
.ifa_addr
== sa
&&
769 (ia
->ia_flags
& IFA_ROUTE
)) {
771 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
772 IFA_UNLOCK(&ia
->ia_ifa
);
773 lck_rw_done(in_ifaddr_rwlock
);
774 lck_mtx_lock(rnh_lock
);
776 * in_ifscrub kills the interface route.
778 in_ifscrub(ia
->ia_ifp
, ia
, 1);
780 * in_ifadown gets rid of all the rest of
781 * the routes. This is not quite the right
782 * thing to do, but at least if we are running
783 * a routing process they will come back.
785 in_ifadown(&ia
->ia_ifa
, 1);
786 lck_mtx_unlock(rnh_lock
);
787 IFA_REMREF(&ia
->ia_ifa
);
790 IFA_UNLOCK(&ia
->ia_ifa
);
793 lck_rw_done(in_ifaddr_rwlock
);
797 lck_rw_lock_shared(in_ifaddr_rwlock
);
798 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
799 ia
= ia
->ia_link
.tqe_next
) {
800 IFA_LOCK(&ia
->ia_ifa
);
801 if (ia
->ia_ifa
.ifa_addr
== sa
) {
805 IFA_UNLOCK(&ia
->ia_ifa
);
807 if (ia
== NULL
|| (ia
->ia_flags
& IFA_ROUTE
) ||
808 (ia
->ia_ifa
.ifa_debug
& IFD_NOTREADY
)) {
810 IFA_UNLOCK(&ia
->ia_ifa
);
811 lck_rw_done(in_ifaddr_rwlock
);
814 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
815 IFA_UNLOCK(&ia
->ia_ifa
);
816 lck_rw_done(in_ifaddr_rwlock
);
819 ifp
= ia
->ia_ifa
.ifa_ifp
;
821 if ((ifp
->if_flags
& IFF_LOOPBACK
)
822 || (ifp
->if_flags
& IFF_POINTOPOINT
))
825 err
= rtinit(&ia
->ia_ifa
, RTM_ADD
, flags
);
827 IFA_LOCK_SPIN(&ia
->ia_ifa
);
828 ia
->ia_flags
|= IFA_ROUTE
;
829 IFA_UNLOCK(&ia
->ia_ifa
);
831 IFA_REMREF(&ia
->ia_ifa
);
836 u_int32_t rip_sendspace
= RIPSNDQ
;
837 u_int32_t rip_recvspace
= RIPRCVQ
;
839 SYSCTL_INT(_net_inet_raw
, OID_AUTO
, maxdgram
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
840 &rip_sendspace
, 0, "Maximum outgoing raw IP datagram size");
841 SYSCTL_INT(_net_inet_raw
, OID_AUTO
, recvspace
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
842 &rip_recvspace
, 0, "Maximum incoming raw IP datagram size");
843 SYSCTL_UINT(_net_inet_raw
, OID_AUTO
, pcbcount
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
844 &ripcbinfo
.ipi_count
, 0, "Number of active PCBs");
847 rip_attach(struct socket
*so
, int proto
, struct proc
*p
)
855 if ((so
->so_state
& SS_PRIV
) == 0)
858 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
861 error
= in_pcballoc(so
, &ripcbinfo
, p
);
864 inp
= (struct inpcb
*)so
->so_pcb
;
865 inp
->inp_vflag
|= INP_IPV4
;
866 inp
->inp_ip_p
= proto
;
867 inp
->inp_ip_ttl
= ip_defttl
;
871 __private_extern__
int
872 rip_detach(struct socket
*so
)
883 __private_extern__
int
884 rip_abort(struct socket
*so
)
886 soisdisconnected(so
);
887 return rip_detach(so
);
890 __private_extern__
int
891 rip_disconnect(struct socket
*so
)
893 if ((so
->so_state
& SS_ISCONNECTED
) == 0)
895 return rip_abort(so
);
898 __private_extern__
int
899 rip_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
902 struct inpcb
*inp
= sotoinpcb(so
);
903 struct sockaddr_in sin
;
904 struct ifaddr
*ifa
= NULL
;
905 struct ifnet
*outif
= NULL
;
909 || (necp_socket_should_use_flow_divert(inp
))
912 return (inp
== NULL
? EINVAL
: EPROTOTYPE
);
914 if (nam
->sa_len
!= sizeof (struct sockaddr_in
))
917 /* Sanitized local copy for interface address searches */
918 bzero(&sin
, sizeof (sin
));
919 sin
.sin_family
= AF_INET
;
920 sin
.sin_len
= sizeof (struct sockaddr_in
);
921 sin
.sin_addr
.s_addr
= SIN(nam
)->sin_addr
.s_addr
;
923 if (TAILQ_EMPTY(&ifnet_head
) ||
924 (sin
.sin_family
!= AF_INET
&& sin
.sin_family
!= AF_IMPLINK
) ||
925 (sin
.sin_addr
.s_addr
&& (ifa
= ifa_ifwithaddr(SA(&sin
))) == 0)) {
926 return (EADDRNOTAVAIL
);
929 * Opportunistically determine the outbound
930 * interface that may be used; this may not
931 * hold true if we end up using a route
932 * going over a different interface, e.g.
933 * when sending to a local address. This
934 * will get updated again after sending.
937 outif
= ifa
->ifa_ifp
;
941 inp
->inp_laddr
= sin
.sin_addr
;
942 inp
->inp_last_outifp
= outif
;
946 __private_extern__
int
947 rip_connect(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
949 struct inpcb
*inp
= sotoinpcb(so
);
950 struct sockaddr_in
*addr
= (struct sockaddr_in
*)(void *)nam
;
954 || (necp_socket_should_use_flow_divert(inp
))
957 return (inp
== NULL
? EINVAL
: EPROTOTYPE
);
958 if (nam
->sa_len
!= sizeof(*addr
))
960 if (TAILQ_EMPTY(&ifnet_head
))
961 return EADDRNOTAVAIL
;
962 if ((addr
->sin_family
!= AF_INET
) &&
963 (addr
->sin_family
!= AF_IMPLINK
))
965 inp
->inp_faddr
= addr
->sin_addr
;
971 __private_extern__
int
972 rip_shutdown(struct socket
*so
)
978 __private_extern__
int
979 rip_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
980 struct mbuf
*control
, struct proc
*p
)
982 #pragma unused(flags, p)
983 struct inpcb
*inp
= sotoinpcb(so
);
989 || (necp_socket_should_use_flow_divert(inp
) && (error
= EPROTOTYPE
))
999 if (so
->so_state
& SS_ISCONNECTED
) {
1004 dst
= inp
->inp_faddr
.s_addr
;
1010 dst
= ((struct sockaddr_in
*)(void *)nam
)->sin_addr
.s_addr
;
1012 return (rip_output(m
, so
, dst
, control
));
1019 if (control
!= NULL
)
1025 /* note: rip_unlock is called from different protos instead of the generic socket_unlock,
1026 * it will handle the socket dealloc on last reference
1029 rip_unlock(struct socket
*so
, int refcount
, void *debug
)
1032 struct inpcb
*inp
= sotoinpcb(so
);
1035 lr_saved
= __builtin_return_address(0);
1040 if (so
->so_usecount
<= 0) {
1041 panic("rip_unlock: bad refoucnt so=%p val=%x lrh= %s\n",
1042 so
, so
->so_usecount
, solockhistory_nr(so
));
1046 if (so
->so_usecount
== 0 && (inp
->inp_wantcnt
== WNT_STOPUSING
)) {
1047 /* cleanup after last reference */
1048 lck_mtx_unlock(so
->so_proto
->pr_domain
->dom_mtx
);
1049 lck_rw_lock_exclusive(ripcbinfo
.ipi_lock
);
1050 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
1052 if (SOCK_CHECK_DOM(so
, PF_INET6
))
1059 lck_rw_done(ripcbinfo
.ipi_lock
);
1063 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
1064 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
1065 lck_mtx_unlock(so
->so_proto
->pr_domain
->dom_mtx
);
1070 rip_pcblist SYSCTL_HANDLER_ARGS
1072 #pragma unused(oidp, arg1, arg2)
1074 struct inpcb
*inp
, **inp_list
;
1079 * The process of preparing the TCB list is too time-consuming and
1080 * resource-intensive to repeat twice on every request.
1082 lck_rw_lock_exclusive(ripcbinfo
.ipi_lock
);
1083 if (req
->oldptr
== USER_ADDR_NULL
) {
1084 n
= ripcbinfo
.ipi_count
;
1085 req
->oldidx
= 2 * (sizeof xig
)
1086 + (n
+ n
/8) * sizeof(struct xinpcb
);
1087 lck_rw_done(ripcbinfo
.ipi_lock
);
1091 if (req
->newptr
!= USER_ADDR_NULL
) {
1092 lck_rw_done(ripcbinfo
.ipi_lock
);
1097 * OK, now we're committed to doing something.
1099 gencnt
= ripcbinfo
.ipi_gencnt
;
1100 n
= ripcbinfo
.ipi_count
;
1102 bzero(&xig
, sizeof(xig
));
1103 xig
.xig_len
= sizeof xig
;
1105 xig
.xig_gen
= gencnt
;
1106 xig
.xig_sogen
= so_gencnt
;
1107 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1109 lck_rw_done(ripcbinfo
.ipi_lock
);
1113 * We are done if there is no pcb
1116 lck_rw_done(ripcbinfo
.ipi_lock
);
1120 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
1121 if (inp_list
== 0) {
1122 lck_rw_done(ripcbinfo
.ipi_lock
);
1126 for (inp
= ripcbinfo
.ipi_listhead
->lh_first
, i
= 0; inp
&& i
< n
;
1127 inp
= inp
->inp_list
.le_next
) {
1128 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
)
1129 inp_list
[i
++] = inp
;
1134 for (i
= 0; i
< n
; i
++) {
1136 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
1139 bzero(&xi
, sizeof(xi
));
1140 xi
.xi_len
= sizeof xi
;
1141 /* XXX should avoid extra copy */
1142 inpcb_to_compat(inp
, &xi
.xi_inp
);
1143 if (inp
->inp_socket
)
1144 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
1145 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
1150 * Give the user an updated idea of our state.
1151 * If the generation differs from what we told
1152 * her before, she knows that something happened
1153 * while we were processing this request, and it
1154 * might be necessary to retry.
1156 bzero(&xig
, sizeof(xig
));
1157 xig
.xig_len
= sizeof xig
;
1158 xig
.xig_gen
= ripcbinfo
.ipi_gencnt
;
1159 xig
.xig_sogen
= so_gencnt
;
1160 xig
.xig_count
= ripcbinfo
.ipi_count
;
1161 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1163 FREE(inp_list
, M_TEMP
);
1164 lck_rw_done(ripcbinfo
.ipi_lock
);
1168 SYSCTL_PROC(_net_inet_raw
, OID_AUTO
/*XXX*/, pcblist
,
1169 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
1170 rip_pcblist
, "S,xinpcb", "List of active raw IP sockets");
1174 rip_pcblist64 SYSCTL_HANDLER_ARGS
1176 #pragma unused(oidp, arg1, arg2)
1178 struct inpcb
*inp
, **inp_list
;
1183 * The process of preparing the TCB list is too time-consuming and
1184 * resource-intensive to repeat twice on every request.
1186 lck_rw_lock_exclusive(ripcbinfo
.ipi_lock
);
1187 if (req
->oldptr
== USER_ADDR_NULL
) {
1188 n
= ripcbinfo
.ipi_count
;
1189 req
->oldidx
= 2 * (sizeof xig
)
1190 + (n
+ n
/8) * sizeof(struct xinpcb64
);
1191 lck_rw_done(ripcbinfo
.ipi_lock
);
1195 if (req
->newptr
!= USER_ADDR_NULL
) {
1196 lck_rw_done(ripcbinfo
.ipi_lock
);
1201 * OK, now we're committed to doing something.
1203 gencnt
= ripcbinfo
.ipi_gencnt
;
1204 n
= ripcbinfo
.ipi_count
;
1206 bzero(&xig
, sizeof(xig
));
1207 xig
.xig_len
= sizeof xig
;
1209 xig
.xig_gen
= gencnt
;
1210 xig
.xig_sogen
= so_gencnt
;
1211 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1213 lck_rw_done(ripcbinfo
.ipi_lock
);
1217 * We are done if there is no pcb
1220 lck_rw_done(ripcbinfo
.ipi_lock
);
1224 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
1225 if (inp_list
== 0) {
1226 lck_rw_done(ripcbinfo
.ipi_lock
);
1230 for (inp
= ripcbinfo
.ipi_listhead
->lh_first
, i
= 0; inp
&& i
< n
;
1231 inp
= inp
->inp_list
.le_next
) {
1232 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
)
1233 inp_list
[i
++] = inp
;
1238 for (i
= 0; i
< n
; i
++) {
1240 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
1243 bzero(&xi
, sizeof(xi
));
1244 xi
.xi_len
= sizeof xi
;
1245 inpcb_to_xinpcb64(inp
, &xi
);
1246 if (inp
->inp_socket
)
1247 sotoxsocket64(inp
->inp_socket
, &xi
.xi_socket
);
1248 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
1253 * Give the user an updated idea of our state.
1254 * If the generation differs from what we told
1255 * her before, she knows that something happened
1256 * while we were processing this request, and it
1257 * might be necessary to retry.
1259 bzero(&xig
, sizeof(xig
));
1260 xig
.xig_len
= sizeof xig
;
1261 xig
.xig_gen
= ripcbinfo
.ipi_gencnt
;
1262 xig
.xig_sogen
= so_gencnt
;
1263 xig
.xig_count
= ripcbinfo
.ipi_count
;
1264 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1266 FREE(inp_list
, M_TEMP
);
1267 lck_rw_done(ripcbinfo
.ipi_lock
);
1271 SYSCTL_PROC(_net_inet_raw
, OID_AUTO
, pcblist64
,
1272 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
1273 rip_pcblist64
, "S,xinpcb64", "List of active raw IP sockets");
1278 rip_pcblist_n SYSCTL_HANDLER_ARGS
1280 #pragma unused(oidp, arg1, arg2)
1283 error
= get_pcblist_n(IPPROTO_IP
, req
, &ripcbinfo
);
1288 SYSCTL_PROC(_net_inet_raw
, OID_AUTO
, pcblist_n
,
1289 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
1290 rip_pcblist_n
, "S,xinpcb_n", "List of active raw IP sockets");
1292 struct pr_usrreqs rip_usrreqs
= {
1293 .pru_abort
= rip_abort
,
1294 .pru_attach
= rip_attach
,
1295 .pru_bind
= rip_bind
,
1296 .pru_connect
= rip_connect
,
1297 .pru_control
= in_control
,
1298 .pru_detach
= rip_detach
,
1299 .pru_disconnect
= rip_disconnect
,
1300 .pru_peeraddr
= in_getpeeraddr
,
1301 .pru_send
= rip_send
,
1302 .pru_shutdown
= rip_shutdown
,
1303 .pru_sockaddr
= in_getsockaddr
,
1304 .pru_sosend
= sosend
,
1305 .pru_soreceive
= soreceive
,
1307 /* DSEP Review Done pl-20051213-v02 @3253 */