2 * Copyright (c) 2000-2008 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, 1990, 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 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
61 * $FreeBSD: src/sys/netinet/ip_output.c,v 1.99.2.16 2001/07/19 06:37:26 kris Exp $
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <kern/locks.h>
81 #include <sys/sysctl.h>
84 #include <net/if_dl.h>
85 #include <net/route.h>
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip_var.h>
94 #include <netinet/kpi_ipfilter_var.h>
97 #include <security/mac_framework.h>
102 #include <net/dlil.h>
103 #include <sys/kdebug.h>
104 #include <libkern/OSAtomic.h>
106 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 1)
107 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 3)
108 #define DBG_FNC_IP_OUTPUT NETDBG_CODE(DBG_NETIP, (1 << 8) | 1)
109 #define DBG_FNC_IPSEC4_OUTPUT NETDBG_CODE(DBG_NETIP, (2 << 8) | 1)
111 #define SWAP16(v) ((((v) & 0xff) << 8) | ((v) >> 8))
114 #include <netinet6/ipsec.h>
115 #include <netkey/key.h>
117 #include <netkey/key_debug.h>
119 #define KEYDEBUG(lev,arg)
123 #include <netinet/ip_fw.h>
124 #include <netinet/ip_divert.h>
127 #include <netinet/ip_dummynet.h>
130 #if IPFIREWALL_FORWARD_DEBUG
131 #define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
132 (ntohl(a.s_addr)>>16)&0xFF,\
133 (ntohl(a.s_addr)>>8)&0xFF,\
134 (ntohl(a.s_addr))&0xFF);
140 static struct mbuf
*ip_insertoptions(struct mbuf
*, struct mbuf
*, int *);
141 static struct ifnet
*ip_multicast_if(struct in_addr
*, int *);
142 static void ip_mloopback(struct ifnet
*, struct mbuf
*,
143 struct sockaddr_in
*, int);
144 static int ip_getmoptions(struct sockopt
*, struct ip_moptions
*);
145 static int ip_pcbopts(int, struct mbuf
**, struct mbuf
*);
146 static int ip_setmoptions(struct sockopt
*, struct ip_moptions
**);
148 static void ip_out_cksum_stats(int, u_int32_t
);
149 static struct ifaddr
*in_selectsrcif(struct ip
*, struct route
*, unsigned int);
150 static void ip_bindif(struct inpcb
*, unsigned int);
152 int ip_createmoptions(struct ip_moptions
**imop
);
153 int ip_addmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
154 int ip_dropmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
155 int ip_optcopy(struct ip
*, struct ip
*);
156 void in_delayed_cksum_offset(struct mbuf
*, int );
157 void in_cksum_offset(struct mbuf
* , size_t );
159 extern int (*fr_checkp
)(struct ip
*, int, struct ifnet
*, int, struct mbuf
**);
161 extern u_long route_generation
;
163 extern struct protosw inetsw
[];
165 extern struct ip_linklocal_stat ip_linklocal_stat
;
166 extern lck_mtx_t
*ip_mutex
;
168 /* temporary: for testing */
170 extern int ipsec_bypass
;
173 static int ip_maxchainsent
= 0;
174 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxchainsent
, CTLFLAG_RW
,
175 &ip_maxchainsent
, 0, "use dlil_output_list");
177 static int forge_ce
= 0;
178 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, forge_ce
, CTLFLAG_RW
,
179 &forge_ce
, 0, "Forge ECN CE");
182 static int ip_select_srcif_debug
= 0;
183 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, select_srcif_debug
, CTLFLAG_RW
,
184 &ip_select_srcif_debug
, 0, "log source interface selection debug info");
187 * IP output. The packet in mbuf chain m contains a skeletal IP
188 * header (with len, off, ttl, proto, tos, src, dst).
189 * The mbuf chain containing the packet will be freed.
190 * The mbuf opt, if present, will not be freed.
198 struct ip_moptions
*imo
,
199 struct ip_out_args
*ipoa
)
202 error
= ip_output_list(m0
, 0, opt
, ro
, flags
, imo
, ipoa
);
215 * ipsec4_getpolicybyaddr:??? [IPSEC 4th argument, contents modified]
216 * ipsec4_getpolicybysock:??? [IPSEC 4th argument, contents modified]
217 * key_spdacquire:??? [IPSEC]
218 * ipsec4_output:??? [IPSEC]
219 * <fr_checkp>:??? [firewall]
220 * ip_dn_io_ptr:??? [dummynet]
221 * dlil_output:??? [DLIL]
222 * dlil_output_list:??? [DLIL]
224 * Notes: The ipsec4_getpolicyby{addr|sock} function error returns are
225 * only used as the error return from this function where one of
226 * these functions fails to return a policy.
235 struct ip_moptions
*imo
,
236 struct ip_out_args
*ipoa
239 struct ip
*ip
, *mhip
;
240 struct ifnet
*ifp
= NULL
;
242 int hlen
= sizeof (struct ip
);
243 int len
= 0, off
, error
= 0;
244 struct sockaddr_in
*dst
= NULL
;
245 struct in_ifaddr
*ia
= NULL
;
246 int isbroadcast
, sw_csum
;
247 struct in_addr pkt_dst
;
249 struct route iproute
;
250 struct socket
*so
= NULL
;
251 struct secpolicy
*sp
= NULL
;
253 #if IPFIREWALL_FORWARD
254 int fwd_rewrite_src
= 0;
257 struct ip_fw_args args
;
260 ipfilter_t inject_filter_ref
= 0;
262 struct route saved_route
;
263 struct ip_out_args saved_ipoa
;
264 struct mbuf
* packetlist
;
266 unsigned int ifscope
;
267 boolean_t select_srcif
;
269 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
273 args
.next_hop
= NULL
;
276 args
.divert_rule
= 0; /* divert cookie */
279 /* Grab info from mtags prepended to the chain */
281 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
282 struct dn_pkt_tag
*dn_tag
;
284 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
285 args
.rule
= dn_tag
->rule
;
287 saved_route
= dn_tag
->ro
;
291 dst
= dn_tag
->dn_dst
;
293 flags
= dn_tag
->flags
;
294 saved_ipoa
= dn_tag
->ipoa
;
297 m_tag_delete(m0
, tag
);
299 #endif /* DUMMYNET */
302 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
303 struct divert_tag
*div_tag
;
305 div_tag
= (struct divert_tag
*)(tag
+1);
306 args
.divert_rule
= div_tag
->cookie
;
308 m_tag_delete(m0
, tag
);
310 #endif /* IPDIVERT */
312 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
313 struct ip_fwd_tag
*ipfwd_tag
;
315 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
316 args
.next_hop
= ipfwd_tag
->next_hop
;
318 m_tag_delete(m0
, tag
);
320 #endif /* IPFIREWALL */
325 if ( !m
|| (m
->m_flags
& M_PKTHDR
) != 0)
326 panic("ip_output no HDR");
328 panic("ip_output no route, proto = %d",
329 mtod(m
, struct ip
*)->ip_p
);
333 * Do not perform source interface selection when forwarding.
334 * At present the IP_OUTARGS flag implies a request for IP to
335 * perform source interface selection.
337 if (ip_doscopedroute
&&
338 (flags
& (IP_OUTARGS
| IP_FORWARDING
)) == IP_OUTARGS
) {
340 ifscope
= ipoa
->ipoa_ifscope
;
342 select_srcif
= FALSE
;
343 ifscope
= IFSCOPE_NONE
;
347 if (args
.rule
!= NULL
) { /* dummynet already saw us */
348 ip
= mtod(m
, struct ip
*);
349 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
350 lck_mtx_lock(rt_mtx
);
351 if (ro
->ro_rt
!= NULL
)
352 ia
= (struct in_ifaddr
*)ro
->ro_rt
->rt_ifa
;
355 lck_mtx_unlock(rt_mtx
);
357 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
358 so
= ipsec_getsocket(m
);
359 (void)ipsec_setsocket(m
, NULL
);
364 #endif /* IPFIREWALL */
367 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
368 so
= ipsec_getsocket(m
);
369 (void)ipsec_setsocket(m
, NULL
);
374 * No need to proccess packet twice if we've
377 inject_filter_ref
= ipf_get_inject_filter(m
);
380 m
= ip_insertoptions(m
, opt
, &len
);
383 ip
= mtod(m
, struct ip
*);
385 pkt_dst
= args
.next_hop
? args
.next_hop
->sin_addr
: ip
->ip_dst
;
387 pkt_dst
= ip
->ip_dst
;
393 if ((flags
& (IP_FORWARDING
|IP_RAWOUTPUT
)) == 0) {
394 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, hlen
>> 2);
397 ip
->ip_id
= ip_randomid();
399 ip
->ip_id
= htons(ip_id
++);
401 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_localout
);
403 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
407 /* For debugging, we let the stack forge congestion */
409 ((ip
->ip_tos
& IPTOS_ECN_MASK
) == IPTOS_ECN_ECT1
||
410 (ip
->ip_tos
& IPTOS_ECN_MASK
) == IPTOS_ECN_ECT0
)) {
411 ip
->ip_tos
= (ip
->ip_tos
& ~IPTOS_ECN_MASK
) | IPTOS_ECN_CE
;
416 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
417 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
419 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
422 * If there is a cached route,
423 * check that it is to the same destination
424 * and is still up. If not, free it and try again.
425 * The address family should also be checked in case of sharing the
429 lck_mtx_lock(rt_mtx
);
430 if (ro
->ro_rt
!= NULL
) {
431 if (ro
->ro_rt
->generation_id
!= route_generation
&&
432 ((flags
& (IP_ROUTETOIF
| IP_FORWARDING
)) == 0) &&
433 (ip
->ip_src
.s_addr
!= INADDR_ANY
) &&
434 (ifa_foraddr(ip
->ip_src
.s_addr
) == 0)) {
435 error
= EADDRNOTAVAIL
;
436 lck_mtx_unlock(rt_mtx
);
439 if ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
440 dst
->sin_family
!= AF_INET
||
441 dst
->sin_addr
.s_addr
!= pkt_dst
.s_addr
) {
442 rtfree_locked(ro
->ro_rt
);
446 * If we're doing source interface selection, we may not
447 * want to use this route; only synch up the generation
450 if (!select_srcif
&& ro
->ro_rt
!= NULL
&&
451 ro
->ro_rt
->generation_id
!= route_generation
)
452 ro
->ro_rt
->generation_id
= route_generation
;
454 if (ro
->ro_rt
== NULL
) {
455 bzero(dst
, sizeof(*dst
));
456 dst
->sin_family
= AF_INET
;
457 dst
->sin_len
= sizeof(*dst
);
458 dst
->sin_addr
= pkt_dst
;
461 * If routing to interface only,
462 * short circuit routing lookup.
464 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
465 #define sintosa(sin) ((struct sockaddr *)(sin))
466 if (flags
& IP_ROUTETOIF
) {
468 ifafree(&ia
->ia_ifa
);
469 if ((ia
= ifatoia(ifa_ifwithdstaddr(sintosa(dst
)))) == 0) {
470 if ((ia
= ifatoia(ifa_ifwithnet(sintosa(dst
)))) == 0) {
471 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_noroute
);
473 lck_mtx_unlock(rt_mtx
);
479 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
480 } else if (IN_MULTICAST(ntohl(pkt_dst
.s_addr
)) &&
481 imo
!= NULL
&& imo
->imo_multicast_ifp
!= NULL
) {
483 * Bypass the normal routing lookup for multicast
484 * packets if the interface is specified.
486 ifp
= imo
->imo_multicast_ifp
;
489 ifafree(&ia
->ia_ifa
);
491 /* Could use IFP_TO_IA instead but rt_mtx is already held */
492 for (ia
= TAILQ_FIRST(&in_ifaddrhead
);
493 ia
!= NULL
&& ia
->ia_ifp
!= ifp
;
494 ia
= TAILQ_NEXT(ia
, ia_link
))
500 boolean_t cloneok
= FALSE
;
502 * Perform source interface selection; the source IP address
503 * must belong to one of the addresses of the interface used
504 * by the route. For performance reasons, do this only if
505 * there is no route, or if the routing table has changed,
506 * or if we haven't done source interface selection on this
507 * route (for this PCB instance) before.
509 if (select_srcif
&& ip
->ip_src
.s_addr
!= INADDR_ANY
&&
510 (ro
->ro_rt
== NULL
||
511 ro
->ro_rt
->generation_id
!= route_generation
||
512 !(ro
->ro_flags
& ROF_SRCIF_SELECTED
))) {
515 /* Find the source interface */
516 ifa
= in_selectsrcif(ip
, ro
, ifscope
);
519 * If the source address is spoofed (in the case
520 * of IP_RAWOUTPUT), or if this is destined for
521 * local/loopback, just let it go out using the
522 * interface of the route. Otherwise, there's no
523 * interface having such an address, so bail out.
525 if (ifa
== NULL
&& !(flags
& IP_RAWOUTPUT
) &&
526 ifscope
!= lo_ifp
->if_index
) {
527 error
= EADDRNOTAVAIL
;
528 lck_mtx_unlock(rt_mtx
);
533 * If the caller didn't explicitly specify the scope,
534 * pick it up from the source interface. If the cached
535 * route was wrong and was blown away as part of source
536 * interface selection, don't mask out RTF_PRCLONING
537 * since that route may have been allocated by the ULP,
538 * unless the IP header was created by the caller or
539 * the destination is IPv4 LLA. The check for the
540 * latter is needed because IPv4 LLAs are never scoped
541 * in the current implementation, and we don't want to
542 * replace the resolved IPv4 LLA route with one whose
543 * gateway points to that of the default gateway on
544 * the primary interface of the system.
547 if (ifscope
== IFSCOPE_NONE
)
548 ifscope
= ifa
->ifa_ifp
->if_index
;
550 cloneok
= (!(flags
& IP_RAWOUTPUT
) &&
551 !(IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
))));
556 * If this is the case, we probably don't want to allocate
557 * a protocol-cloned route since we didn't get one from the
558 * ULP. This lets TCP do its thing, while not burdening
559 * forwarding or ICMP with the overhead of cloning a route.
560 * Of course, we still want to do any cloning requested by
561 * the link layer, as this is probably required in all cases
562 * for correct operation (as it is for ARP).
564 if (ro
->ro_rt
== NULL
) {
565 unsigned long ign
= RTF_PRCLONING
;
567 * We make an exception here: if the destination
568 * address is INADDR_BROADCAST, allocate a protocol-
569 * cloned host route so that we end up with a route
570 * marked with the RTF_BROADCAST flag. Otherwise,
571 * we would end up referring to the default route,
572 * instead of creating a cloned host route entry.
573 * That would introduce inconsistencies between ULPs
574 * that allocate a route and those that don't. The
575 * RTF_BROADCAST route is important since we'd want
576 * to send out undirected IP broadcast packets using
577 * link-level broadcast address. Another exception
578 * is for ULP-created routes that got blown away by
579 * source interface selection (see above).
581 * These exceptions will no longer be necessary when
582 * the RTF_PRCLONING scheme is no longer present.
584 if (cloneok
|| dst
->sin_addr
.s_addr
== INADDR_BROADCAST
)
585 ign
&= ~RTF_PRCLONING
;
587 rtalloc_scoped_ign_locked(ro
, ign
, ifscope
);
590 if (ro
->ro_rt
== NULL
) {
591 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_noroute
);
592 error
= EHOSTUNREACH
;
593 lck_mtx_unlock(rt_mtx
);
598 ifafree(&ia
->ia_ifa
);
599 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
602 ifp
= ro
->ro_rt
->rt_ifp
;
604 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
605 dst
= (struct sockaddr_in
*)ro
->ro_rt
->rt_gateway
;
606 if (ro
->ro_rt
->rt_flags
& RTF_HOST
)
607 isbroadcast
= (ro
->ro_rt
->rt_flags
& RTF_BROADCAST
);
609 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
611 lck_mtx_unlock(rt_mtx
);
612 if (IN_MULTICAST(ntohl(pkt_dst
.s_addr
))) {
613 struct in_multi
*inm
;
615 m
->m_flags
|= M_MCAST
;
617 * IP destination address is multicast. Make sure "dst"
618 * still points to the address in "ro". (It may have been
619 * changed to point to a gateway address, above.)
621 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
623 * See if the caller provided any multicast options
626 if ((flags
& IP_RAWOUTPUT
) == 0) ip
->ip_ttl
= imo
->imo_multicast_ttl
;
627 if (imo
->imo_multicast_ifp
!= NULL
) {
628 ifp
= imo
->imo_multicast_ifp
;
631 if (imo
->imo_multicast_vif
!= -1 &&
632 ((flags
& IP_RAWOUTPUT
) == 0 || ip
->ip_src
.s_addr
== INADDR_ANY
))
634 ip_mcast_src(imo
->imo_multicast_vif
);
635 #endif /* MROUTING */
637 if ((flags
& IP_RAWOUTPUT
) == 0) ip
->ip_ttl
= IP_DEFAULT_MULTICAST_TTL
;
639 * Confirm that the outgoing interface supports multicast.
641 if ((imo
== NULL
) || (imo
->imo_multicast_vif
== -1)) {
642 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
643 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_noroute
);
649 * If source address not specified yet, use address
650 * of outgoing interface.
652 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
653 register struct in_ifaddr
*ia1
;
654 lck_mtx_lock(rt_mtx
);
655 TAILQ_FOREACH(ia1
, &in_ifaddrhead
, ia_link
)
656 if (ia1
->ia_ifp
== ifp
) {
657 ip
->ip_src
= IA_SIN(ia1
)->sin_addr
;
661 lck_mtx_unlock(rt_mtx
);
662 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
668 ifnet_lock_shared(ifp
);
669 IN_LOOKUP_MULTI(pkt_dst
, ifp
, inm
);
670 ifnet_lock_done(ifp
);
672 (imo
== NULL
|| imo
->imo_multicast_loop
)) {
674 * If we belong to the destination multicast group
675 * on the outgoing interface, and the caller did not
676 * forbid loopback, loop back a copy.
678 if (!TAILQ_EMPTY(&ipv4_filters
)) {
679 struct ipfilter
*filter
;
680 int seen
= (inject_filter_ref
== 0);
681 struct ipf_pktopts
*ippo
= 0, ipf_pktopts
;
685 ipf_pktopts
.ippo_mcast_ifnet
= imo
->imo_multicast_ifp
;
686 ipf_pktopts
.ippo_mcast_ttl
= imo
->imo_multicast_ttl
;
687 ipf_pktopts
.ippo_mcast_loop
= imo
->imo_multicast_loop
;
692 /* 4135317 - always pass network byte order to filter */
696 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
698 if ((struct ipfilter
*)inject_filter_ref
== filter
)
700 } else if (filter
->ipf_filter
.ipf_output
) {
702 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, ippo
);
703 if (result
== EJUSTRETURN
) {
714 /* set back to host byte order */
715 ip
= mtod(m
, struct ip
*);
722 ip_mloopback(ifp
, m
, dst
, hlen
);
727 * If we are acting as a multicast router, perform
728 * multicast forwarding as if the packet had just
729 * arrived on the interface to which we are about
730 * to send. The multicast forwarding function
731 * recursively calls this function, using the
732 * IP_FORWARDING flag to prevent infinite recursion.
734 * Multicasts that are looped back by ip_mloopback(),
735 * above, will be forwarded by the ip_input() routine,
738 if (ip_mrouter
&& (flags
& IP_FORWARDING
) == 0) {
740 * Check if rsvp daemon is running. If not, don't
741 * set ip_moptions. This ensures that the packet
742 * is multicast and not just sent down one link
743 * as prescribed by rsvpd.
747 if (ip_mforward(ip
, ifp
, m
, imo
) != 0) {
753 #endif /* MROUTING */
756 * Multicasts with a time-to-live of zero may be looped-
757 * back, above, but must not be transmitted on a network.
758 * Also, multicasts addressed to the loopback interface
759 * are not sent -- the above call to ip_mloopback() will
760 * loop back a copy if this host actually belongs to the
761 * destination group on the loopback interface.
763 if (ip
->ip_ttl
== 0 || ifp
->if_flags
& IFF_LOOPBACK
) {
772 * If source address not specified yet, use address
773 * of outgoing interface.
775 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
776 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
777 #if IPFIREWALL_FORWARD
778 /* Keep note that we did this - if the firewall changes
779 * the next-hop, our interface may change, changing the
780 * default source IP. It's a shame so much effort happens
784 #endif /* IPFIREWALL_FORWARD */
789 * Look for broadcast address and
790 * and verify user is allowed to send
794 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0) {
795 error
= EADDRNOTAVAIL
;
798 if ((flags
& IP_ALLOWBROADCAST
) == 0) {
802 /* don't allow broadcast messages to be fragmented */
803 if ((u_short
)ip
->ip_len
> ifp
->if_mtu
) {
807 m
->m_flags
|= M_BCAST
;
809 m
->m_flags
&= ~M_BCAST
;
814 * Force IP TTL to 255 following draft-ietf-zeroconf-ipv4-linklocal.txt
816 if (IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)) || IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
))) {
817 ip_linklocal_stat
.iplls_out_total
++;
818 if (ip
->ip_ttl
!= MAXTTL
) {
819 ip_linklocal_stat
.iplls_out_badttl
++;
824 if (!didfilter
&& !TAILQ_EMPTY(&ipv4_filters
)) {
825 struct ipfilter
*filter
;
826 int seen
= (inject_filter_ref
== 0);
830 /* 4135317 - always pass network byte order to filter */
834 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
836 if ((struct ipfilter
*)inject_filter_ref
== filter
)
838 } else if (filter
->ipf_filter
.ipf_output
) {
840 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, 0);
841 if (result
== EJUSTRETURN
) {
852 /* set back to host byte order */
853 ip
= mtod(m
, struct ip
*);
861 /* temporary for testing only: bypass ipsec alltogether */
863 if (ipsec_bypass
!= 0 || (flags
& IP_NOIPSEC
) != 0)
866 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
869 /* get SP for this packet */
871 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, flags
, &error
);
873 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
876 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
877 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
884 switch (sp
->policy
) {
885 case IPSEC_POLICY_DISCARD
:
886 case IPSEC_POLICY_GENERATE
:
888 * This packet is just discarded.
890 IPSEC_STAT_INCREMENT(ipsecstat
.out_polvio
);
891 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 1,0,0,0,0);
894 case IPSEC_POLICY_BYPASS
:
895 case IPSEC_POLICY_NONE
:
896 /* no need to do IPsec. */
897 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 2,0,0,0,0);
900 case IPSEC_POLICY_IPSEC
:
901 if (sp
->req
== NULL
) {
902 /* acquire a policy */
903 error
= key_spdacquire(sp
);
904 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 3,0,0,0,0);
909 case IPSEC_POLICY_ENTRUST
:
911 printf("ip_output: Invalid policy found. %d\n", sp
->policy
);
914 struct ipsec_output_state state
;
915 bzero(&state
, sizeof(state
));
917 if (flags
& IP_ROUTETOIF
) {
919 bzero(&iproute
, sizeof(iproute
));
922 state
.dst
= (struct sockaddr
*)dst
;
928 * delayed checksums are not currently compatible with IPsec
930 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
932 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
938 error
= ipsec4_output(&state
, sp
, flags
);
942 if (flags
& IP_ROUTETOIF
) {
944 * if we have tunnel mode SA, we may need to ignore
947 if (state
.ro
!= &iproute
|| state
.ro
->ro_rt
!= NULL
) {
948 flags
&= ~IP_ROUTETOIF
;
954 dst
= (struct sockaddr_in
*)state
.dst
;
956 /* mbuf is already reclaimed in ipsec4_output. */
966 printf("ip4_output (ipsec): error code %d\n", error
);
969 /* don't show these error codes to the user */
973 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 4,0,0,0,0);
978 /* be sure to update variables that are affected by ipsec4_output() */
979 ip
= mtod(m
, struct ip
*);
982 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
984 hlen
= ip
->ip_hl
<< 2;
986 /* Check that there wasn't a route change and src is still valid */
988 lck_mtx_lock(rt_mtx
);
989 if (ro
->ro_rt
&& ro
->ro_rt
->generation_id
!= route_generation
) {
990 if (ifa_foraddr(ip
->ip_src
.s_addr
) == 0 && ((flags
& (IP_ROUTETOIF
| IP_FORWARDING
)) == 0)) {
991 error
= EADDRNOTAVAIL
;
992 lck_mtx_unlock(rt_mtx
);
993 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 5,0,0,0,0);
996 rtfree_locked(ro
->ro_rt
);
1000 if (ro
->ro_rt
== NULL
) {
1001 if ((flags
& IP_ROUTETOIF
) == 0) {
1002 printf("ip_output: "
1003 "can't update route after IPsec processing\n");
1004 error
= EHOSTUNREACH
; /*XXX*/
1005 lck_mtx_unlock(rt_mtx
);
1006 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 6,0,0,0,0);
1011 ifafree(&ia
->ia_ifa
);
1012 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
1014 ifaref(&ia
->ia_ifa
);
1015 ifp
= ro
->ro_rt
->rt_ifp
;
1017 lck_mtx_unlock(rt_mtx
);
1019 /* make it flipped, again. */
1022 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 7,0xff,0xff,0xff,0xff);
1024 /* Pass to filters again */
1025 if (!TAILQ_EMPTY(&ipv4_filters
)) {
1026 struct ipfilter
*filter
;
1030 /* 4135317 - always pass network byte order to filter */
1034 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
1035 if (filter
->ipf_filter
.ipf_output
) {
1037 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, 0);
1038 if (result
== EJUSTRETURN
) {
1049 /* set back to host byte order */
1050 ip
= mtod(m
, struct ip
*);
1062 * - Xlate: translate packet's addr/port (NAT).
1063 * - Firewall: deny/allow/etc.
1064 * - Wrap: fake packet's addr/port <unimpl.>
1065 * - Encapsulate: put it in another IP and send out. <unimp.>
1068 struct mbuf
*m1
= m
;
1070 if ((error
= (*fr_checkp
)(ip
, hlen
, ifp
, 1, &m1
)) || !m1
) {
1073 ip
= mtod(m0
= m
= m1
, struct ip
*);
1077 * Check with the firewall...
1078 * but not if we are already being fwd'd from a firewall.
1080 if (fw_enable
&& IPFW_LOADED
&& !args
.next_hop
) {
1081 struct sockaddr_in
*old
= dst
;
1084 args
.next_hop
= dst
;
1086 off
= ip_fw_chk_ptr(&args
);
1088 dst
= args
.next_hop
;
1091 * On return we must do the following:
1092 * IP_FW_PORT_DENY_FLAG -> drop the pkt (XXX new)
1093 * 1<=off<= 0xffff -> DIVERT
1094 * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe
1095 * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet
1096 * dst != old -> IPFIREWALL_FORWARD
1097 * off==0, dst==old -> accept
1098 * If some of the above modules is not compiled in, then
1099 * we should't have to check the corresponding condition
1100 * (because the ipfw control socket should not accept
1101 * unsupported rules), but better play safe and drop
1102 * packets in case of doubt.
1105 if ( (off
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) {
1111 ip
= mtod(m
, struct ip
*);
1113 if (off
== 0 && dst
== old
) {/* common case */
1117 if (DUMMYNET_LOADED
&& (off
& IP_FW_PORT_DYNT_FLAG
) != 0) {
1119 * pass the pkt to dummynet. Need to include
1120 * pipe number, m, ifp, ro, dst because these are
1121 * not recomputed in the next pass.
1122 * All other parameters have been already used and
1123 * so they are not needed anymore.
1124 * XXX note: if the ifp or ro entry are deleted
1125 * while a pkt is in dummynet, we are in trouble!
1130 if (flags
& IP_OUTARGS
)
1133 error
= ip_dn_io_ptr(m
, off
& 0xffff, DN_TO_IP_OUT
,
1137 #endif /* DUMMYNET */
1139 if (off
!= 0 && (off
& IP_FW_PORT_DYNT_FLAG
) == 0) {
1140 struct mbuf
*clone
= NULL
;
1142 /* Clone packet if we're doing a 'tee' */
1143 if ((off
& IP_FW_PORT_TEE_FLAG
) != 0)
1144 clone
= m_dup(m
, M_DONTWAIT
);
1147 * delayed checksums are not currently compatible
1148 * with divert sockets.
1150 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1151 in_delayed_cksum(m
);
1152 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1155 /* Restore packet header fields to original values */
1159 /* Deliver packet to divert input routine */
1160 divert_packet(m
, 0, off
& 0xffff, args
.divert_rule
);
1162 /* If 'tee', continue with original packet */
1163 if (clone
!= NULL
) {
1165 ip
= mtod(m
, struct ip
*);
1172 #if IPFIREWALL_FORWARD
1173 /* Here we check dst to make sure it's directly reachable on the
1174 * interface we previously thought it was.
1175 * If it isn't (which may be likely in some situations) we have
1176 * to re-route it (ie, find a route for the next-hop and the
1177 * associated interface) and set them here. This is nested
1178 * forwarding which in most cases is undesirable, except where
1179 * such control is nigh impossible. So we do it here.
1182 if (off
== 0 && old
!= dst
) {
1183 struct in_ifaddr
*ia_fw
;
1185 /* It's changed... */
1186 /* There must be a better way to do this next line... */
1187 static struct route sro_fwd
, *ro_fwd
= &sro_fwd
;
1188 #if IPFIREWALL_FORWARD_DEBUG
1189 printf("IPFIREWALL_FORWARD: New dst ip: ");
1190 print_ip(dst
->sin_addr
);
1194 * We need to figure out if we have been forwarded
1195 * to a local socket. If so then we should somehow
1196 * "loop back" to ip_input, and get directed to the
1197 * PCB as if we had received this packet. This is
1198 * because it may be dificult to identify the packets
1199 * you want to forward until they are being output
1200 * and have selected an interface. (e.g. locally
1201 * initiated packets) If we used the loopback inteface,
1202 * we would not be able to control what happens
1203 * as the packet runs through ip_input() as
1204 * it is done through a ISR.
1206 TAILQ_FOREACH(ia_fw
, &in_ifaddrhead
, ia_link
) {
1208 * If the addr to forward to is one
1209 * of ours, we pretend to
1210 * be the destination for this packet.
1212 if (IA_SIN(ia_fw
)->sin_addr
.s_addr
==
1213 dst
->sin_addr
.s_addr
)
1217 /* tell ip_input "dont filter" */
1218 struct m_tag
*fwd_tag
;
1219 struct ip_fwd_tag
*ipfwd_tag
;
1221 fwd_tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
,
1222 sizeof(struct sockaddr_in
), M_NOWAIT
);
1223 if (fwd_tag
== NULL
) {
1228 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1229 ipfwd_tag
->next_hop
= args
.next_hop
;
1231 m_tag_prepend(m
, fwd_tag
);
1233 if (m
->m_pkthdr
.rcvif
== NULL
)
1234 m
->m_pkthdr
.rcvif
= ifunit("lo0");
1235 if ((~IF_HWASSIST_CSUM_FLAGS(m
->m_pkthdr
.rcvif
->if_hwassist
) &
1236 m
->m_pkthdr
.csum_flags
) == 0) {
1237 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1238 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1239 m
->m_pkthdr
.csum_flags
|=
1240 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
1241 m
->m_pkthdr
.csum_data
= 0xffff;
1243 m
->m_pkthdr
.csum_flags
|=
1244 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
1246 else if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1247 in_delayed_cksum(m
);
1248 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1249 ip
->ip_sum
= in_cksum(m
, hlen
);
1255 /* we need to call dlil_output to run filters
1256 * and resync to avoid recursion loops.
1259 dlil_output(lo_ifp
, PF_INET
, m
, 0, (struct sockaddr
*)dst
, 0);
1262 printf("ip_output: no loopback ifp for forwarding!!!\n");
1266 /* Some of the logic for this was
1267 * nicked from above.
1269 * This rewrites the cached route in a local PCB.
1270 * Is this what we want to do?
1272 bcopy(dst
, &ro_fwd
->ro_dst
, sizeof(*dst
));
1275 lck_mtx_lock(rt_mtx
);
1276 rtalloc_ign_locked(ro_fwd
, RTF_PRCLONING
);
1278 if (ro_fwd
->ro_rt
== 0) {
1279 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_noroute
);
1280 error
= EHOSTUNREACH
;
1281 lck_mtx_unlock(rt_mtx
);
1285 ia_fw
= ifatoia(ro_fwd
->ro_rt
->rt_ifa
);
1286 ifp
= ro_fwd
->ro_rt
->rt_ifp
;
1287 ro_fwd
->ro_rt
->rt_use
++;
1288 if (ro_fwd
->ro_rt
->rt_flags
& RTF_GATEWAY
)
1289 dst
= (struct sockaddr_in
*)ro_fwd
->ro_rt
->rt_gateway
;
1290 if (ro_fwd
->ro_rt
->rt_flags
& RTF_HOST
)
1292 (ro_fwd
->ro_rt
->rt_flags
& RTF_BROADCAST
);
1294 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
1295 rtfree_locked(ro
->ro_rt
);
1296 ro
->ro_rt
= ro_fwd
->ro_rt
;
1297 dst
= (struct sockaddr_in
*)&ro_fwd
->ro_dst
;
1298 lck_mtx_unlock(rt_mtx
);
1301 * If we added a default src ip earlier,
1302 * which would have been gotten from the-then
1303 * interface, do it again, from the new one.
1305 if (fwd_rewrite_src
)
1306 ip
->ip_src
= IA_SIN(ia_fw
)->sin_addr
;
1309 #endif /* IPFIREWALL_FORWARD */
1311 * if we get here, none of the above matches, and
1312 * we have to drop the pkt
1315 error
= EACCES
; /* not sure this is the right error msg */
1318 #endif /* IPFIREWALL */
1322 /* Do not allow loopback address to wind up on a wire */
1323 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
1324 ((ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
1325 (ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
)) {
1326 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_badaddr
);
1329 * Do not simply drop the packet just like a firewall -- we want the
1330 * the application to feel the pain.
1331 * Return ENETUNREACH like ip6_output does in some similar cases.
1332 * This can startle the otherwise clueless process that specifies
1333 * loopback as the source address.
1335 error
= ENETUNREACH
;
1339 m
->m_pkthdr
.csum_flags
|= CSUM_IP
;
1340 sw_csum
= m
->m_pkthdr
.csum_flags
1341 & ~IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
1343 if ((ifp
->if_hwassist
& CSUM_TCP_SUM16
) != 0) {
1345 * Special case code for GMACE
1346 * frames that can be checksumed by GMACE SUM16 HW:
1347 * frame >64, no fragments, no UDP
1349 if (apple_hwcksum_tx
&& (m
->m_pkthdr
.csum_flags
& CSUM_TCP
)
1350 && (ip
->ip_len
> 50) && (ip
->ip_len
<= ifp
->if_mtu
)) {
1351 /* Apple GMAC HW, expects STUFF_OFFSET << 16 | START_OFFSET */
1352 u_short offset
= (IP_VHL_HL(ip
->ip_vhl
) << 2) +14 ; /* IP+Enet header length */
1353 u_short csumprev
= m
->m_pkthdr
.csum_data
& 0xFFFF;
1354 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_TCP_SUM16
; /* for GMAC */
1355 m
->m_pkthdr
.csum_data
= (csumprev
+ offset
) << 16 ;
1356 m
->m_pkthdr
.csum_data
+= offset
;
1357 sw_csum
= CSUM_DELAY_IP
; /* do IP hdr chksum in software */
1360 /* let the software handle any UDP or TCP checksums */
1361 sw_csum
|= (CSUM_DELAY_DATA
& m
->m_pkthdr
.csum_flags
);
1363 } else if (apple_hwcksum_tx
== 0) {
1364 sw_csum
|= (CSUM_DELAY_DATA
| CSUM_DELAY_IP
) &
1365 m
->m_pkthdr
.csum_flags
;
1368 if (sw_csum
& CSUM_DELAY_DATA
) {
1369 in_delayed_cksum(m
);
1370 sw_csum
&= ~CSUM_DELAY_DATA
;
1371 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1374 if (apple_hwcksum_tx
!= 0) {
1375 m
->m_pkthdr
.csum_flags
&=
1376 IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
1378 m
->m_pkthdr
.csum_flags
= 0;
1382 * If small enough for interface, or the interface will take
1383 * care of the fragmentation for us, can just send directly.
1385 if ((u_short
)ip
->ip_len
<= ifp
->if_mtu
||
1386 ifp
->if_hwassist
& CSUM_FRAGMENT
) {
1387 struct rtentry
*rte
;
1392 if (sw_csum
& CSUM_DELAY_IP
) {
1393 ip
->ip_sum
= in_cksum(m
, hlen
);
1397 /* Record statistics for this interface address. */
1398 if (!(flags
& IP_FORWARDING
) && ia
!= NULL
) {
1399 ia
->ia_ifa
.if_opackets
++;
1400 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1405 /* clean ipsec history once it goes out of the node */
1406 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0)
1409 if (packetchain
== 0) {
1410 lck_mtx_lock(rt_mtx
);
1411 if ((rte
= ro
->ro_rt
) != NULL
)
1413 lck_mtx_unlock(rt_mtx
);
1414 error
= ifnet_output(ifp
, PF_INET
, m
, rte
,
1415 (struct sockaddr
*)dst
);
1420 else { /* packet chaining allows us to reuse the route for all packets */
1423 if (pktcnt
> ip_maxchainsent
)
1424 ip_maxchainsent
= pktcnt
;
1425 lck_mtx_lock(rt_mtx
);
1426 if ((rte
= ro
->ro_rt
) != NULL
)
1428 lck_mtx_unlock(rt_mtx
);
1430 error
= ifnet_output(ifp
, PF_INET
, packetlist
,
1431 rte
, (struct sockaddr
*)dst
);
1444 * Too large for interface; fragment if possible.
1445 * Must be able to put at least 8 bytes per fragment.
1447 if (ip
->ip_off
& IP_DF
) {
1450 * This case can happen if the user changed the MTU
1451 * of an interface after enabling IP on it. Because
1452 * most netifs don't keep track of routes pointing to
1453 * them, there is no way for one to update all its
1454 * routes when the MTU is changed.
1457 lck_mtx_lock(rt_mtx
);
1458 if (ro
->ro_rt
&& (ro
->ro_rt
->rt_flags
& (RTF_UP
| RTF_HOST
))
1459 && !(ro
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
1460 && (ro
->ro_rt
->rt_rmx
.rmx_mtu
> ifp
->if_mtu
)) {
1461 ro
->ro_rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
1463 lck_mtx_unlock(rt_mtx
);
1464 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_cantfrag
);
1467 len
= (ifp
->if_mtu
- hlen
) &~ 7;
1474 * if the interface will not calculate checksums on
1475 * fragmented packets, then do it here.
1477 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
&&
1478 (ifp
->if_hwassist
& CSUM_IP_FRAGS
) == 0) {
1479 in_delayed_cksum(m
);
1480 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1485 int mhlen
, firstlen
= len
;
1486 struct mbuf
**mnext
= &m
->m_nextpkt
;
1490 * Loop through length of segment after first fragment,
1491 * make new header and copy data of each part and link onto chain.
1494 mhlen
= sizeof (struct ip
);
1495 for (off
= hlen
+ len
; off
< (u_short
)ip
->ip_len
; off
+= len
) {
1496 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1499 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_odropped
);
1502 m
->m_flags
|= (m0
->m_flags
& M_MCAST
) | M_FRAG
;
1503 m
->m_data
+= max_linkhdr
;
1504 mhip
= mtod(m
, struct ip
*);
1506 if (hlen
> sizeof (struct ip
)) {
1507 mhlen
= ip_optcopy(ip
, mhip
) + sizeof (struct ip
);
1508 mhip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, mhlen
>> 2);
1511 mhip
->ip_off
= ((off
- hlen
) >> 3) + (ip
->ip_off
& ~IP_MF
);
1512 if (ip
->ip_off
& IP_MF
)
1513 mhip
->ip_off
|= IP_MF
;
1514 if (off
+ len
>= (u_short
)ip
->ip_len
)
1515 len
= (u_short
)ip
->ip_len
- off
;
1517 mhip
->ip_off
|= IP_MF
;
1518 mhip
->ip_len
= htons((u_short
)(len
+ mhlen
));
1519 m
->m_next
= m_copy(m0
, off
, len
);
1520 if (m
->m_next
== 0) {
1522 error
= ENOBUFS
; /* ??? */
1523 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_odropped
);
1526 m
->m_pkthdr
.len
= mhlen
+ len
;
1527 m
->m_pkthdr
.rcvif
= 0;
1528 m
->m_pkthdr
.csum_flags
= m0
->m_pkthdr
.csum_flags
;
1529 m
->m_pkthdr
.socket_id
= m0
->m_pkthdr
.socket_id
;
1531 mac_netinet_fragment(m0
, m
);
1533 HTONS(mhip
->ip_off
);
1535 if (sw_csum
& CSUM_DELAY_IP
) {
1536 mhip
->ip_sum
= in_cksum(m
, mhlen
);
1539 mnext
= &m
->m_nextpkt
;
1542 OSAddAtomic(nfrags
, (SInt32
*)&ipstat
.ips_ofragments
);
1544 /* set first/last markers for fragment chain */
1545 m
->m_flags
|= M_LASTFRAG
;
1546 m0
->m_flags
|= M_FIRSTFRAG
| M_FRAG
;
1547 m0
->m_pkthdr
.csum_data
= nfrags
;
1550 * Update first fragment by trimming what's been copied out
1551 * and updating header, then send each fragment (in order).
1554 m_adj(m
, hlen
+ firstlen
- (u_short
)ip
->ip_len
);
1555 m
->m_pkthdr
.len
= hlen
+ firstlen
;
1556 ip
->ip_len
= htons((u_short
)m
->m_pkthdr
.len
);
1557 ip
->ip_off
|= IP_MF
;
1560 if (sw_csum
& CSUM_DELAY_IP
) {
1561 ip
->ip_sum
= in_cksum(m
, hlen
);
1565 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1566 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1568 for (m
= m0
; m
; m
= m0
) {
1572 /* clean ipsec history once it goes out of the node */
1573 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0)
1577 struct rtentry
*rte
;
1579 /* Record statistics for this interface address. */
1581 ia
->ia_ifa
.if_opackets
++;
1582 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1585 if ((packetchain
!= 0) && (pktcnt
> 0))
1586 panic("ip_output: mix of packet in packetlist is wrong=%p", packetlist
);
1587 lck_mtx_lock(rt_mtx
);
1588 if ((rte
= ro
->ro_rt
) != NULL
)
1590 lck_mtx_unlock(rt_mtx
);
1591 error
= ifnet_output(ifp
, PF_INET
, m
, rte
,
1592 (struct sockaddr
*)dst
);
1600 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_fragmented
);
1604 ifafree(&ia
->ia_ifa
);
1608 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
1609 if (ro
== &iproute
&& ro
->ro_rt
) {
1614 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1615 printf("DP ip_output call free SP:%x\n", sp
));
1616 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1621 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_END
, error
,0,0,0,0);
1629 ip_out_cksum_stats(int proto
, u_int32_t len
)
1633 tcp_out_cksum_stats(len
);
1636 udp_out_cksum_stats(len
);
1639 /* keep only TCP or UDP stats for now */
1645 in_delayed_cksum_offset(struct mbuf
*m0
, int ip_offset
)
1648 unsigned char buf
[sizeof(struct ip
)];
1649 u_short csum
, offset
, ip_len
;
1650 struct mbuf
*m
= m0
;
1652 while (ip_offset
>= m
->m_len
) {
1653 ip_offset
-= m
->m_len
;
1656 printf("in_delayed_cksum_withoffset failed - ip_offset wasn't in the packet\n");
1661 /* Sometimes the IP header is not contiguous, yes this can happen! */
1662 if (ip_offset
+ sizeof(struct ip
) > m
->m_len
) {
1664 printf("delayed m_pullup, m->len: %ld off: %d\n",
1665 m
->m_len
, ip_offset
);
1667 m_copydata(m
, ip_offset
, sizeof(struct ip
), (caddr_t
) buf
);
1669 ip
= (struct ip
*)buf
;
1671 ip
= (struct ip
*)(m
->m_data
+ ip_offset
);
1676 m
->m_len
-= ip_offset
;
1677 m
->m_data
+= ip_offset
;
1680 offset
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
1683 * We could be in the context of an IP or interface filter; in the
1684 * former case, ip_len would be in host (correct) order while for
1685 * the latter it would be in network order. Because of this, we
1686 * attempt to interpret the length field by comparing it against
1687 * the actual packet length. If the comparison fails, byte swap
1688 * the length and check again. If it still fails, then the packet
1689 * is bogus and we give up.
1691 ip_len
= ip
->ip_len
;
1692 if (ip_len
!= (m0
->m_pkthdr
.len
- ip_offset
)) {
1693 ip_len
= SWAP16(ip_len
);
1694 if (ip_len
!= (m0
->m_pkthdr
.len
- ip_offset
)) {
1695 printf("in_delayed_cksum_offset: ip_len %d (%d) "
1696 "doesn't match actual length %d\n", ip
->ip_len
,
1697 ip_len
, (m0
->m_pkthdr
.len
- ip_offset
));
1702 csum
= in_cksum_skip(m
, ip_len
, offset
);
1705 ip_out_cksum_stats(ip
->ip_p
, ip_len
- offset
);
1707 if (m0
->m_pkthdr
.csum_flags
& CSUM_UDP
&& csum
== 0)
1709 offset
+= m0
->m_pkthdr
.csum_data
& 0xFFFF; /* checksum offset */
1713 if (M_LEADINGSPACE(m
) < ip_offset
)
1714 panic("in_delayed_cksum_offset - chain modified!\n");
1715 m
->m_len
+= ip_offset
;
1716 m
->m_data
-= ip_offset
;
1719 if (offset
> ip_len
) /* bogus offset */
1722 /* Insert the checksum in the existing chain */
1723 if (offset
+ ip_offset
+ sizeof(u_short
) > m
->m_len
) {
1727 printf("delayed m_copyback, m->len: %ld off: %d p: %d\n",
1728 m
->m_len
, offset
+ ip_offset
, ip
->ip_p
);
1730 *(u_short
*)tmp
= csum
;
1731 m_copyback(m
, offset
+ ip_offset
, 2, tmp
);
1733 *(u_short
*)(m
->m_data
+ offset
+ ip_offset
) = csum
;
1737 in_delayed_cksum(struct mbuf
*m
)
1739 in_delayed_cksum_offset(m
, 0);
1743 in_cksum_offset(struct mbuf
* m
, size_t ip_offset
)
1745 struct ip
* ip
= NULL
;
1747 unsigned char buf
[sizeof(struct ip
)];
1750 while (ip_offset
>= m
->m_len
) {
1751 ip_offset
-= m
->m_len
;
1754 printf("in_cksum_offset failed - ip_offset wasn't in the packet\n");
1759 /* Sometimes the IP header is not contiguous, yes this can happen! */
1760 if (ip_offset
+ sizeof(struct ip
) > m
->m_len
) {
1763 printf("in_cksum_offset - delayed m_pullup, m->len: %ld off: %lu\n",
1764 m
->m_len
, ip_offset
);
1766 m_copydata(m
, ip_offset
, sizeof(struct ip
), (caddr_t
) buf
);
1768 ip
= (struct ip
*)buf
;
1770 m_copyback(m
, ip_offset
+ offsetof(struct ip
, ip_sum
), 2, (caddr_t
)&ip
->ip_sum
);
1772 ip
= (struct ip
*)(m
->m_data
+ ip_offset
);
1778 m
->m_len
-= ip_offset
;
1779 m
->m_data
+= ip_offset
;
1783 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1785 hlen
= ip
->ip_hl
<< 2;
1788 * We could be in the context of an IP or interface filter; in the
1789 * former case, ip_len would be in host order while for the latter
1790 * it would be in network (correct) order. Because of this, we
1791 * attempt to interpret the length field by comparing it against
1792 * the actual packet length. If the comparison fails, byte swap
1793 * the length and check again. If it still fails, then the packet
1794 * is bogus and we give up.
1796 if (ntohs(ip
->ip_len
) != (m
->m_pkthdr
.len
- ip_offset
)) {
1797 ip
->ip_len
= SWAP16(ip
->ip_len
);
1799 if (ntohs(ip
->ip_len
) != (m
->m_pkthdr
.len
- ip_offset
)) {
1800 ip
->ip_len
= SWAP16(ip
->ip_len
);
1801 printf("in_cksum_offset: ip_len %d (%d) "
1802 "doesn't match actual length %lu\n",
1803 ip
->ip_len
, SWAP16(ip
->ip_len
),
1804 (m
->m_pkthdr
.len
- ip_offset
));
1810 ip
->ip_sum
= in_cksum(m
, hlen
);
1812 ip
->ip_len
= SWAP16(ip
->ip_len
);
1816 if (M_LEADINGSPACE(m
) < ip_offset
)
1817 panic("in_cksum_offset - chain modified!\n");
1818 m
->m_len
+= ip_offset
;
1819 m
->m_data
-= ip_offset
;
1822 /* Insert the checksum in the existing chain if IP header not contiguous */
1823 if (ip_offset
+ sizeof(struct ip
) > m
->m_len
) {
1827 printf("in_cksum_offset m_copyback, m->len: %lu off: %lu p: %d\n",
1828 m
->m_len
, ip_offset
+ offsetof(struct ip
, ip_sum
), ip
->ip_p
);
1830 *(u_short
*)tmp
= ip
->ip_sum
;
1831 m_copyback(m
, ip_offset
+ offsetof(struct ip
, ip_sum
), 2, tmp
);
1836 * Insert IP options into preformed packet.
1837 * Adjust IP destination as required for IP source routing,
1838 * as indicated by a non-zero in_addr at the start of the options.
1840 * XXX This routine assumes that the packet has no options in place.
1842 static struct mbuf
*
1843 ip_insertoptions(m
, opt
, phlen
)
1844 register struct mbuf
*m
;
1848 register struct ipoption
*p
= mtod(opt
, struct ipoption
*);
1850 register struct ip
*ip
= mtod(m
, struct ip
*);
1853 optlen
= opt
->m_len
- sizeof(p
->ipopt_dst
);
1854 if (optlen
+ (u_short
)ip
->ip_len
> IP_MAXPACKET
)
1855 return (m
); /* XXX should fail */
1856 if (p
->ipopt_dst
.s_addr
)
1857 ip
->ip_dst
= p
->ipopt_dst
;
1858 if (m
->m_flags
& M_EXT
|| m
->m_data
- optlen
< m
->m_pktdat
) {
1859 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1862 n
->m_pkthdr
.rcvif
= 0;
1864 mac_mbuf_label_copy(m
, n
);
1866 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
1867 m
->m_len
-= sizeof(struct ip
);
1868 m
->m_data
+= sizeof(struct ip
);
1871 m
->m_len
= optlen
+ sizeof(struct ip
);
1872 m
->m_data
+= max_linkhdr
;
1873 (void)memcpy(mtod(m
, void *), ip
, sizeof(struct ip
));
1875 m
->m_data
-= optlen
;
1877 m
->m_pkthdr
.len
+= optlen
;
1878 ovbcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
1880 ip
= mtod(m
, struct ip
*);
1881 bcopy(p
->ipopt_list
, ip
+ 1, optlen
);
1882 *phlen
= sizeof(struct ip
) + optlen
;
1883 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, *phlen
>> 2);
1884 ip
->ip_len
+= optlen
;
1889 * Copy options from ip to jp,
1890 * omitting those not copied during fragmentation.
1896 register u_char
*cp
, *dp
;
1897 int opt
, optlen
, cnt
;
1899 cp
= (u_char
*)(ip
+ 1);
1900 dp
= (u_char
*)(jp
+ 1);
1901 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1902 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1904 if (opt
== IPOPT_EOL
)
1906 if (opt
== IPOPT_NOP
) {
1907 /* Preserve for IP mcast tunnel's LSRR alignment. */
1913 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
1914 panic("malformed IPv4 option passed to ip_optcopy");
1916 optlen
= cp
[IPOPT_OLEN
];
1918 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
1919 panic("malformed IPv4 option passed to ip_optcopy");
1921 /* bogus lengths should have been caught by ip_dooptions */
1924 if (IPOPT_COPIED(opt
)) {
1925 bcopy(cp
, dp
, optlen
);
1929 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
1935 * IP socket option processing.
1938 ip_ctloutput(so
, sopt
)
1940 struct sockopt
*sopt
;
1942 struct inpcb
*inp
= sotoinpcb(so
);
1946 if (sopt
->sopt_level
!= IPPROTO_IP
) {
1950 switch (sopt
->sopt_dir
) {
1952 switch (sopt
->sopt_name
) {
1959 if (sopt
->sopt_valsize
> MLEN
) {
1963 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_HEADER
);
1968 m
->m_len
= sopt
->sopt_valsize
;
1969 error
= sooptcopyin(sopt
, mtod(m
, char *), m
->m_len
,
1974 return (ip_pcbopts(sopt
->sopt_name
, &inp
->inp_options
,
1981 case IP_RECVRETOPTS
:
1982 case IP_RECVDSTADDR
:
1985 #if defined(NFAITH) && NFAITH > 0
1988 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1993 switch (sopt
->sopt_name
) {
1995 inp
->inp_ip_tos
= optval
;
1999 inp
->inp_ip_ttl
= optval
;
2001 #define OPTSET(bit) \
2003 inp->inp_flags |= bit; \
2005 inp->inp_flags &= ~bit;
2008 OPTSET(INP_RECVOPTS
);
2011 case IP_RECVRETOPTS
:
2012 OPTSET(INP_RECVRETOPTS
);
2015 case IP_RECVDSTADDR
:
2016 OPTSET(INP_RECVDSTADDR
);
2024 OPTSET(INP_RECVTTL
);
2027 #if defined(NFAITH) && NFAITH > 0
2036 #if CONFIG_FORCE_OUT_IFP
2038 * Apple private interface, similar to IP_BOUND_IF, except
2039 * that the parameter is a NULL-terminated string containing
2040 * the name of the network interface; an emptry string means
2041 * unbind. Applications are encouraged to use IP_BOUND_IF
2042 * instead, as that is the current "official" API.
2044 case IP_FORCE_OUT_IFP
: {
2045 char ifname
[IFNAMSIZ
];
2046 unsigned int ifscope
;
2048 /* This option is settable only for IPv4 */
2049 if (!(inp
->inp_vflag
& INP_IPV4
)) {
2054 /* Verify interface name parameter is sane */
2055 if (sopt
->sopt_valsize
> sizeof(ifname
)) {
2060 /* Copy the interface name */
2061 if (sopt
->sopt_valsize
!= 0) {
2062 error
= sooptcopyin(sopt
, ifname
,
2063 sizeof (ifname
), sopt
->sopt_valsize
);
2068 if (sopt
->sopt_valsize
== 0 || ifname
[0] == NULL
) {
2069 /* Unbind this socket from any interface */
2070 ifscope
= IFSCOPE_NONE
;
2074 /* Verify name is NULL terminated */
2075 if (ifname
[sopt
->sopt_valsize
- 1] != NULL
) {
2080 /* Bail out if given bogus interface name */
2081 if (ifnet_find_by_name(ifname
, &ifp
) != 0) {
2086 /* Bind this socket to this interface */
2087 ifscope
= ifp
->if_index
;
2090 * Won't actually free; since we don't release
2091 * this later, we should do it now.
2095 ip_bindif(inp
, ifscope
);
2099 case IP_MULTICAST_IF
:
2100 case IP_MULTICAST_VIF
:
2101 case IP_MULTICAST_TTL
:
2102 case IP_MULTICAST_LOOP
:
2103 case IP_ADD_MEMBERSHIP
:
2104 case IP_DROP_MEMBERSHIP
:
2105 error
= ip_setmoptions(sopt
, &inp
->inp_moptions
);
2109 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2115 case IP_PORTRANGE_DEFAULT
:
2116 inp
->inp_flags
&= ~(INP_LOWPORT
);
2117 inp
->inp_flags
&= ~(INP_HIGHPORT
);
2120 case IP_PORTRANGE_HIGH
:
2121 inp
->inp_flags
&= ~(INP_LOWPORT
);
2122 inp
->inp_flags
|= INP_HIGHPORT
;
2125 case IP_PORTRANGE_LOW
:
2126 inp
->inp_flags
&= ~(INP_HIGHPORT
);
2127 inp
->inp_flags
|= INP_LOWPORT
;
2137 case IP_IPSEC_POLICY
:
2145 if (sopt
->sopt_valsize
> MCLBYTES
) {
2149 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
2151 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) /* XXX */
2153 priv
= (sopt
->sopt_p
!= NULL
&&
2154 proc_suser(sopt
->sopt_p
) != 0) ? 0 : 1;
2156 req
= mtod(m
, caddr_t
);
2159 optname
= sopt
->sopt_name
;
2160 error
= ipsec4_set_policy(inp
, optname
, req
, len
, priv
);
2167 case IP_TRAFFIC_MGT_BACKGROUND
:
2169 unsigned background
= 0;
2170 error
= sooptcopyin(sopt
, &background
, sizeof(background
), sizeof(background
));
2175 so
->so_traffic_mgt_flags
|= TRAFFIC_MGT_SO_BACKGROUND
;
2177 so
->so_traffic_mgt_flags
&= ~TRAFFIC_MGT_SO_BACKGROUND
;
2181 #endif /* TRAFFIC_MGT */
2184 * On a multihomed system, scoped routing can be used to
2185 * restrict the source interface used for sending packets.
2186 * The socket option IP_BOUND_IF binds a particular AF_INET
2187 * socket to an interface such that data sent on the socket
2188 * is restricted to that interface. This is unlike the
2189 * SO_DONTROUTE option where the routing table is bypassed;
2190 * therefore it allows for a greater flexibility and control
2191 * over the system behavior, and does not place any restriction
2192 * on the destination address type (e.g. unicast, multicast,
2193 * or broadcast if applicable) or whether or not the host is
2194 * directly reachable. Note that in the multicast transmit
2195 * case, IP_MULTICAST_IF takes precedence over IP_BOUND_IF,
2196 * since the former practically bypasses the routing table;
2197 * in this case, IP_BOUND_IF sets the default interface used
2198 * for sending multicast packets in the absence of an explicit
2199 * transmit interface set via IP_MULTICAST_IF.
2202 /* This option is settable only for IPv4 */
2203 if (!(inp
->inp_vflag
& INP_IPV4
)) {
2208 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2214 ip_bindif(inp
, optval
);
2218 error
= ENOPROTOOPT
;
2224 switch (sopt
->sopt_name
) {
2227 if (inp
->inp_options
)
2228 error
= sooptcopyout(sopt
,
2229 mtod(inp
->inp_options
,
2231 inp
->inp_options
->m_len
);
2233 sopt
->sopt_valsize
= 0;
2239 case IP_RECVRETOPTS
:
2240 case IP_RECVDSTADDR
:
2244 #if defined(NFAITH) && NFAITH > 0
2247 switch (sopt
->sopt_name
) {
2250 optval
= inp
->inp_ip_tos
;
2254 optval
= inp
->inp_ip_ttl
;
2257 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
2260 optval
= OPTBIT(INP_RECVOPTS
);
2263 case IP_RECVRETOPTS
:
2264 optval
= OPTBIT(INP_RECVRETOPTS
);
2267 case IP_RECVDSTADDR
:
2268 optval
= OPTBIT(INP_RECVDSTADDR
);
2272 optval
= OPTBIT(INP_RECVIF
);
2276 optval
= OPTBIT(INP_RECVTTL
);
2280 if (inp
->inp_flags
& INP_HIGHPORT
)
2281 optval
= IP_PORTRANGE_HIGH
;
2282 else if (inp
->inp_flags
& INP_LOWPORT
)
2283 optval
= IP_PORTRANGE_LOW
;
2288 #if defined(NFAITH) && NFAITH > 0
2290 optval
= OPTBIT(INP_FAITH
);
2294 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2297 case IP_MULTICAST_IF
:
2298 case IP_MULTICAST_VIF
:
2299 case IP_MULTICAST_TTL
:
2300 case IP_MULTICAST_LOOP
:
2301 case IP_ADD_MEMBERSHIP
:
2302 case IP_DROP_MEMBERSHIP
:
2303 error
= ip_getmoptions(sopt
, inp
->inp_moptions
);
2307 case IP_IPSEC_POLICY
:
2309 struct mbuf
*m
= NULL
;
2314 req
= mtod(m
, caddr_t
);
2317 error
= ipsec4_get_policy(sotoinpcb(so
), req
, len
, &m
);
2319 error
= soopt_mcopyout(sopt
, m
); /* XXX */
2327 case IP_TRAFFIC_MGT_BACKGROUND
:
2329 unsigned background
= so
->so_traffic_mgt_flags
;
2330 return (sooptcopyout(sopt
, &background
, sizeof(background
)));
2333 #endif /* TRAFFIC_MGT */
2336 if (inp
->inp_flags
& INP_BOUND_IF
)
2337 optval
= inp
->inp_boundif
;
2338 error
= sooptcopyout(sopt
, &optval
, sizeof (optval
));
2342 error
= ENOPROTOOPT
;
2351 * Set up IP options in pcb for insertion in output packets.
2352 * Store in mbuf with pointer in pcbopt, adding pseudo-option
2353 * with destination address if source routed.
2357 __unused
int optname
,
2358 struct mbuf
**pcbopt
,
2359 register struct mbuf
*m
)
2361 register int cnt
, optlen
;
2362 register u_char
*cp
;
2365 /* turn off any old options */
2367 (void)m_free(*pcbopt
);
2369 if (m
== (struct mbuf
*)0 || m
->m_len
== 0) {
2371 * Only turning off any previous options.
2379 if (m
->m_len
% sizeof(int32_t))
2383 * IP first-hop destination address will be stored before
2384 * actual options; move other options back
2385 * and clear it when none present.
2387 if (m
->m_data
+ m
->m_len
+ sizeof(struct in_addr
) >= &m
->m_dat
[MLEN
])
2390 m
->m_len
+= sizeof(struct in_addr
);
2391 cp
= mtod(m
, u_char
*) + sizeof(struct in_addr
);
2392 ovbcopy(mtod(m
, caddr_t
), (caddr_t
)cp
, (unsigned)cnt
);
2393 bzero(mtod(m
, caddr_t
), sizeof(struct in_addr
));
2395 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
2396 opt
= cp
[IPOPT_OPTVAL
];
2397 if (opt
== IPOPT_EOL
)
2399 if (opt
== IPOPT_NOP
)
2402 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
2404 optlen
= cp
[IPOPT_OLEN
];
2405 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
2416 * user process specifies route as:
2418 * D must be our final destination (but we can't
2419 * check that since we may not have connected yet).
2420 * A is first hop destination, which doesn't appear in
2421 * actual IP option, but is stored before the options.
2423 if (optlen
< IPOPT_MINOFF
- 1 + sizeof(struct in_addr
))
2425 m
->m_len
-= sizeof(struct in_addr
);
2426 cnt
-= sizeof(struct in_addr
);
2427 optlen
-= sizeof(struct in_addr
);
2428 cp
[IPOPT_OLEN
] = optlen
;
2430 * Move first hop before start of options.
2432 bcopy((caddr_t
)&cp
[IPOPT_OFFSET
+1], mtod(m
, caddr_t
),
2433 sizeof(struct in_addr
));
2435 * Then copy rest of options back
2436 * to close up the deleted entry.
2438 ovbcopy((caddr_t
)(&cp
[IPOPT_OFFSET
+1] +
2439 sizeof(struct in_addr
)),
2440 (caddr_t
)&cp
[IPOPT_OFFSET
+1],
2441 (unsigned)cnt
+ sizeof(struct in_addr
));
2445 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
2457 * The whole multicast option thing needs to be re-thought.
2458 * Several of these options are equally applicable to non-multicast
2459 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
2460 * standard option (IP_TTL).
2464 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
2466 static struct ifnet
*
2467 ip_multicast_if(a
, ifindexp
)
2476 if (ntohl(a
->s_addr
) >> 24 == 0) {
2477 ifindex
= ntohl(a
->s_addr
) & 0xffffff;
2478 ifnet_head_lock_shared();
2479 if (ifindex
< 0 || if_index
< ifindex
) {
2483 ifp
= ifindex2ifnet
[ifindex
];
2486 *ifindexp
= ifindex
;
2488 INADDR_TO_IFP(*a
, ifp
);
2494 * Set the IP multicast options in response to user setsockopt().
2497 ip_setmoptions(sopt
, imop
)
2498 struct sockopt
*sopt
;
2499 struct ip_moptions
**imop
;
2503 struct in_addr addr
;
2504 struct ip_mreq mreq
;
2505 struct ifnet
*ifp
= NULL
;
2506 struct ip_moptions
*imo
= *imop
;
2511 * No multicast option buffer attached to the pcb;
2512 * allocate one and initialize to default values.
2514 error
= ip_createmoptions(imop
);
2520 switch (sopt
->sopt_name
) {
2521 /* store an index number for the vif you wanna use in the send */
2523 case IP_MULTICAST_VIF
:
2524 if (legal_vif_num
== 0) {
2528 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2531 if (!legal_vif_num(i
) && (i
!= -1)) {
2535 imo
->imo_multicast_vif
= i
;
2537 #endif /* MROUTING */
2539 case IP_MULTICAST_IF
:
2541 * Select the interface for outgoing multicast packets.
2543 error
= sooptcopyin(sopt
, &addr
, sizeof addr
, sizeof addr
);
2547 * INADDR_ANY is used to remove a previous selection.
2548 * When no interface is selected, a default one is
2549 * chosen every time a multicast packet is sent.
2551 if (addr
.s_addr
== INADDR_ANY
) {
2552 imo
->imo_multicast_ifp
= NULL
;
2556 * The selected interface is identified by its local
2557 * IP address. Find the interface and confirm that
2558 * it supports multicasting.
2560 ifp
= ip_multicast_if(&addr
, &ifindex
);
2561 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2562 error
= EADDRNOTAVAIL
;
2565 imo
->imo_multicast_ifp
= ifp
;
2567 imo
->imo_multicast_addr
= addr
;
2569 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2572 case IP_MULTICAST_TTL
:
2574 * Set the IP time-to-live for outgoing multicast packets.
2575 * The original multicast API required a char argument,
2576 * which is inconsistent with the rest of the socket API.
2577 * We allow either a char or an int.
2579 if (sopt
->sopt_valsize
== 1) {
2581 error
= sooptcopyin(sopt
, &ttl
, 1, 1);
2584 imo
->imo_multicast_ttl
= ttl
;
2587 error
= sooptcopyin(sopt
, &ttl
, sizeof ttl
,
2594 imo
->imo_multicast_ttl
= ttl
;
2598 case IP_MULTICAST_LOOP
:
2600 * Set the loopback flag for outgoing multicast packets.
2601 * Must be zero or one. The original multicast API required a
2602 * char argument, which is inconsistent with the rest
2603 * of the socket API. We allow either a char or an int.
2605 if (sopt
->sopt_valsize
== 1) {
2607 error
= sooptcopyin(sopt
, &loop
, 1, 1);
2610 imo
->imo_multicast_loop
= !!loop
;
2613 error
= sooptcopyin(sopt
, &loop
, sizeof loop
,
2617 imo
->imo_multicast_loop
= !!loop
;
2621 case IP_ADD_MEMBERSHIP
:
2623 * Add a multicast group membership.
2624 * Group must be a valid IP multicast address.
2626 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
2630 error
= ip_addmembership(imo
, &mreq
);
2633 case IP_DROP_MEMBERSHIP
:
2635 * Drop a multicast group membership.
2636 * Group must be a valid IP multicast address.
2638 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
2642 error
= ip_dropmembership(imo
, &mreq
);
2651 * If all options have default values, no need to keep the mbuf.
2653 if (imo
->imo_multicast_ifp
== NULL
&&
2654 imo
->imo_multicast_vif
== (u_long
)-1 &&
2655 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
2656 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
2657 imo
->imo_num_memberships
== 0) {
2658 FREE(*imop
, M_IPMOPTS
);
2666 * Set the IP multicast options in response to user setsockopt().
2668 __private_extern__
int
2670 struct ip_moptions
**imop
)
2672 struct ip_moptions
*imo
;
2673 imo
= (struct ip_moptions
*) _MALLOC(sizeof(*imo
), M_IPMOPTS
,
2679 imo
->imo_multicast_ifp
= NULL
;
2680 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2681 imo
->imo_multicast_vif
= -1;
2682 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
2683 imo
->imo_multicast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
2684 imo
->imo_num_memberships
= 0;
2690 * Add membership to an IPv4 multicast.
2692 __private_extern__
int
2694 struct ip_moptions
*imo
,
2695 struct ip_mreq
*mreq
)
2698 struct sockaddr_in
*dst
;
2699 struct ifnet
*ifp
= NULL
;
2703 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
2708 * If no interface address was provided, use the interface of
2709 * the route to the given multicast address.
2711 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
) {
2712 bzero((caddr_t
)&ro
, sizeof(ro
));
2713 dst
= (struct sockaddr_in
*)&ro
.ro_dst
;
2714 dst
->sin_len
= sizeof(*dst
);
2715 dst
->sin_family
= AF_INET
;
2716 dst
->sin_addr
= mreq
->imr_multiaddr
;
2717 lck_mtx_lock(rt_mtx
);
2718 rtalloc_ign_locked(&ro
, 0UL);
2719 if (ro
.ro_rt
!= NULL
) {
2720 ifp
= ro
.ro_rt
->rt_ifp
;
2721 rtfree_locked(ro
.ro_rt
);
2724 /* If there's no default route, try using loopback */
2725 mreq
->imr_interface
.s_addr
= INADDR_LOOPBACK
;
2727 lck_mtx_unlock(rt_mtx
);
2731 ifp
= ip_multicast_if(&mreq
->imr_interface
, NULL
);
2735 * See if we found an interface, and confirm that it
2736 * supports multicast.
2738 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2739 error
= EADDRNOTAVAIL
;
2743 * See if the membership already exists or if all the
2744 * membership slots are full.
2746 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
2747 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
&&
2748 imo
->imo_membership
[i
]->inm_addr
.s_addr
2749 == mreq
->imr_multiaddr
.s_addr
)
2752 if (i
< imo
->imo_num_memberships
) {
2756 if (i
== IP_MAX_MEMBERSHIPS
) {
2757 error
= ETOOMANYREFS
;
2761 * Everything looks good; add a new record to the multicast
2762 * address list for the given interface.
2764 if ((imo
->imo_membership
[i
] =
2765 in_addmulti(&mreq
->imr_multiaddr
, ifp
)) == NULL
) {
2769 ++imo
->imo_num_memberships
;
2775 * Drop membership of an IPv4 multicast.
2777 __private_extern__
int
2779 struct ip_moptions
*imo
,
2780 struct ip_mreq
*mreq
)
2783 struct ifnet
* ifp
= NULL
;
2786 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
2792 * If an interface address was specified, get a pointer
2793 * to its ifnet structure.
2795 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
)
2798 ifp
= ip_multicast_if(&mreq
->imr_interface
, NULL
);
2800 error
= EADDRNOTAVAIL
;
2805 * Find the membership in the membership array.
2807 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
2809 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
2810 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
2811 mreq
->imr_multiaddr
.s_addr
)
2814 if (i
== imo
->imo_num_memberships
) {
2815 error
= EADDRNOTAVAIL
;
2819 * Give up the multicast address record to which the
2820 * membership points.
2822 in_delmulti(&imo
->imo_membership
[i
]);
2824 * Remove the gap in the membership array.
2826 for (++i
; i
< imo
->imo_num_memberships
; ++i
)
2827 imo
->imo_membership
[i
-1] = imo
->imo_membership
[i
];
2828 --imo
->imo_num_memberships
;
2834 * Return the IP multicast options in response to user getsockopt().
2837 ip_getmoptions(sopt
, imo
)
2838 struct sockopt
*sopt
;
2839 register struct ip_moptions
*imo
;
2841 struct in_addr addr
;
2842 struct in_ifaddr
*ia
;
2847 switch (sopt
->sopt_name
) {
2849 case IP_MULTICAST_VIF
:
2851 optval
= imo
->imo_multicast_vif
;
2854 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2856 #endif /* MROUTING */
2858 case IP_MULTICAST_IF
:
2859 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
)
2860 addr
.s_addr
= INADDR_ANY
;
2861 else if (imo
->imo_multicast_addr
.s_addr
) {
2862 /* return the value user has set */
2863 addr
= imo
->imo_multicast_addr
;
2865 IFP_TO_IA(imo
->imo_multicast_ifp
, ia
);
2866 addr
.s_addr
= (ia
== NULL
) ? INADDR_ANY
2867 : IA_SIN(ia
)->sin_addr
.s_addr
;
2869 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
2872 case IP_MULTICAST_TTL
:
2874 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
2876 optval
= coptval
= imo
->imo_multicast_ttl
;
2877 if (sopt
->sopt_valsize
== 1)
2878 error
= sooptcopyout(sopt
, &coptval
, 1);
2880 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2883 case IP_MULTICAST_LOOP
:
2885 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
2887 optval
= coptval
= imo
->imo_multicast_loop
;
2888 if (sopt
->sopt_valsize
== 1)
2889 error
= sooptcopyout(sopt
, &coptval
, 1);
2891 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2895 error
= ENOPROTOOPT
;
2902 * Discard the IP multicast options.
2905 ip_freemoptions(imo
)
2906 register struct ip_moptions
*imo
;
2911 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
)
2912 in_delmulti(&imo
->imo_membership
[i
]);
2913 FREE(imo
, M_IPMOPTS
);
2918 * Routine called from ip_output() to loop back a copy of an IP multicast
2919 * packet to the input queue of a specified interface. Note that this
2920 * calls the output routine of the loopback "driver", but with an interface
2921 * pointer that might NOT be a loopback interface -- evil, but easier than
2922 * replicating that code here.
2925 ip_mloopback(ifp
, m
, dst
, hlen
)
2927 register struct mbuf
*m
;
2928 register struct sockaddr_in
*dst
;
2931 register struct ip
*ip
;
2933 int sw_csum
= (apple_hwcksum_tx
== 0);
2935 copym
= m_copy(m
, 0, M_COPYALL
);
2936 if (copym
!= NULL
&& (copym
->m_flags
& M_EXT
|| copym
->m_len
< hlen
))
2937 copym
= m_pullup(copym
, hlen
);
2943 * We don't bother to fragment if the IP length is greater
2944 * than the interface's MTU. Can this possibly matter?
2946 ip
= mtod(copym
, struct ip
*);
2950 ip
->ip_sum
= in_cksum(copym
, hlen
);
2953 * It's not clear whether there are any lingering
2954 * reentrancy problems in other areas which might
2955 * be exposed by using ip_input directly (in
2956 * particular, everything which modifies the packet
2957 * in-place). Yet another option is using the
2958 * protosw directly to deliver the looped back
2959 * packet. For the moment, we'll err on the side
2960 * of safety by using if_simloop().
2963 if (dst
->sin_family
!= AF_INET
) {
2964 printf("ip_mloopback: bad address family %d\n",
2966 dst
->sin_family
= AF_INET
;
2971 * Mark checksum as valid or calculate checksum for loopback.
2973 * This is done this way because we have to embed the ifp of
2974 * the interface we will send the original copy of the packet
2975 * out on in the mbuf. ip_input will check if_hwassist of the
2976 * embedded ifp and ignore all csum_flags if if_hwassist is 0.
2977 * The UDP checksum has not been calculated yet.
2979 if (sw_csum
|| (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
)) {
2980 if (!sw_csum
&& IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
)) {
2981 copym
->m_pkthdr
.csum_flags
|=
2982 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
2983 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
2984 copym
->m_pkthdr
.csum_data
= 0xffff;
2987 in_delayed_cksum(copym
);
2994 * We need to send all loopback traffic down to dlil in case
2995 * a filter has tapped-in.
2999 * Stuff the 'real' ifp into the pkthdr, to be used in matching
3000 * in ip_input(); we need the loopback ifp/dl_tag passed as args
3001 * to make the loopback driver compliant with the data link
3005 copym
->m_pkthdr
.rcvif
= ifp
;
3006 dlil_output(lo_ifp
, PF_INET
, copym
, 0,
3007 (struct sockaddr
*) dst
, 0);
3009 printf("Warning: ip_output call to dlil_find_dltag failed!\n");
3015 * Given a source IP address (and route, if available), determine the best
3016 * interface to send the packet from.
3018 static struct ifaddr
*
3019 in_selectsrcif(struct ip
*ip
, struct route
*ro
, unsigned int ifscope
)
3021 struct ifaddr
*ifa
= NULL
;
3022 struct sockaddr src
= { sizeof (struct sockaddr_in
), AF_INET
, { 0, } };
3023 struct ifnet
*rt_ifp
;
3024 char ip_src
[16], ip_dst
[16];
3026 if (ip_select_srcif_debug
) {
3027 (void) inet_ntop(AF_INET
, &ip
->ip_src
.s_addr
, ip_src
,
3029 (void) inet_ntop(AF_INET
, &ip
->ip_dst
.s_addr
, ip_dst
,
3033 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
3035 ((struct sockaddr_in
*)&src
)->sin_addr
.s_addr
= ip
->ip_src
.s_addr
;
3036 rt_ifp
= (ro
->ro_rt
!= NULL
) ? ro
->ro_rt
->rt_ifp
: NULL
;
3039 * Given the source IP address, find a suitable source interface
3040 * to use for transmission; if the caller has specified a scope,
3041 * optimize the search by looking at the addresses only for that
3042 * interface. This is still suboptimal, however, as we need to
3043 * traverse the per-interface list.
3045 if (ifscope
!= IFSCOPE_NONE
|| ro
->ro_rt
!= NULL
) {
3046 unsigned int scope
= ifscope
;
3049 * If no scope is specified and the route is stale (pointing
3050 * to a defunct interface) use the current primary interface;
3051 * this happens when switching between interfaces configured
3052 * with the same IP address. Otherwise pick up the scope
3053 * information from the route; the ULP may have looked up a
3054 * correct route and we just need to verify it here and mark
3055 * it with the ROF_SRCIF_SELECTED flag below.
3057 if (scope
== IFSCOPE_NONE
) {
3058 scope
= rt_ifp
->if_index
;
3059 if (scope
!= get_primary_ifscope() &&
3060 ro
->ro_rt
->generation_id
!= route_generation
)
3061 scope
= get_primary_ifscope();
3064 ifa
= ifa_ifwithaddr_scoped(&src
, scope
);
3066 if (ip_select_srcif_debug
&& ifa
!= NULL
) {
3067 if (ro
->ro_rt
!= NULL
) {
3068 printf("%s->%s ifscope %d->%d ifa_if %s%d "
3069 "ro_if %s%d\n", ip_src
, ip_dst
, ifscope
,
3070 scope
, ifa
->ifa_ifp
->if_name
,
3071 ifa
->ifa_ifp
->if_unit
, rt_ifp
->if_name
,
3074 printf("%s->%s ifscope %d->%d ifa_if %s%d\n",
3075 ip_src
, ip_dst
, ifscope
, scope
,
3076 ifa
->ifa_ifp
->if_name
,
3077 ifa
->ifa_ifp
->if_unit
);
3083 * Slow path; search for an interface having the corresponding source
3084 * IP address if the scope was not specified by the caller, and:
3086 * 1) There currently isn't any route, or,
3087 * 2) The interface used by the route does not own that source
3088 * IP address; in this case, the route will get blown away
3089 * and we'll do a more specific scoped search using the newly
3092 if (ifa
== NULL
&& ifscope
== IFSCOPE_NONE
) {
3093 ifa
= ifa_ifwithaddr(&src
);
3095 if (ip_select_srcif_debug
&& ifa
!= NULL
) {
3096 printf("%s->%s ifscope %d ifa_if %s%d\n",
3097 ip_src
, ip_dst
, ifscope
, ifa
->ifa_ifp
->if_name
,
3098 ifa
->ifa_ifp
->if_unit
);
3103 * If there is a non-loopback route with the wrong interface, or if
3104 * there is no interface configured with such an address, blow it
3105 * away. Except for local/loopback, we look for one with a matching
3106 * interface scope/index.
3108 if (ro
->ro_rt
!= NULL
&&
3109 (ifa
== NULL
|| (ifa
->ifa_ifp
!= rt_ifp
&& rt_ifp
!= lo_ifp
) ||
3110 !(ro
->ro_rt
->rt_flags
& RTF_UP
))) {
3111 if (ip_select_srcif_debug
) {
3113 printf("%s->%s ifscope %d ro_if %s%d != "
3114 "ifa_if %s%d (cached route cleared)\n",
3115 ip_src
, ip_dst
, ifscope
, rt_ifp
->if_name
,
3116 rt_ifp
->if_unit
, ifa
->ifa_ifp
->if_name
,
3117 ifa
->ifa_ifp
->if_unit
);
3119 printf("%s->%s ifscope %d ro_if %s%d "
3120 "(no ifa_if found)\n",
3121 ip_src
, ip_dst
, ifscope
, rt_ifp
->if_name
,
3126 rtfree_locked(ro
->ro_rt
);
3128 ro
->ro_flags
&= ~ROF_SRCIF_SELECTED
;
3131 * If the destination is IPv4 LLA and the route's interface
3132 * doesn't match the source interface, then the source IP
3133 * address is wrong; it most likely belongs to the primary
3134 * interface associated with the IPv4 LL subnet. Drop the
3135 * packet rather than letting it go out and return an error
3136 * to the ULP. This actually applies not only to IPv4 LL
3137 * but other shared subnets; for now we explicitly test only
3138 * for the former case and save the latter for future.
3140 if (IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) &&
3141 !IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)) && ifa
!= NULL
) {
3147 if (ip_select_srcif_debug
&& ifa
== NULL
) {
3148 printf("%s->%s ifscope %d (neither ro_if/ifa_if found)\n",
3149 ip_src
, ip_dst
, ifscope
);
3153 * If there is a route, mark it accordingly. If there isn't one,
3154 * we'll get here again during the next transmit (possibly with a
3155 * route) and the flag will get set at that point. For IPv4 LLA
3156 * destination, mark it only if the route has been fully resolved;
3157 * otherwise we want to come back here again when the route points
3158 * to the interface over which the ARP reply arrives on.
3160 if (ro
->ro_rt
!= NULL
&& (!IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
3161 (ro
->ro_rt
->rt_gateway
->sa_family
== AF_LINK
&&
3162 SDL(ro
->ro_rt
->rt_gateway
)->sdl_alen
!= 0))) {
3163 ro
->ro_flags
|= ROF_SRCIF_SELECTED
;
3164 ro
->ro_rt
->generation_id
= route_generation
;
3171 * Handler for setting IP_FORCE_OUT_IFP or IP_BOUND_IF socket option.
3174 ip_bindif(struct inpcb
*inp
, unsigned int ifscope
)
3177 * A zero interface scope value indicates an "unbind".
3178 * Otherwise, take in whatever value the app desires;
3179 * the app may already know the scope (or force itself
3180 * to such a scope) ahead of time before the interface
3181 * gets attached. It doesn't matter either way; any
3182 * route lookup from this point on will require an
3183 * exact match for the embedded interface scope.
3185 inp
->inp_boundif
= ifscope
;
3186 if (inp
->inp_boundif
== IFSCOPE_NONE
)
3187 inp
->inp_flags
&= ~INP_BOUND_IF
;
3189 inp
->inp_flags
|= INP_BOUND_IF
;
3191 lck_mtx_lock(rt_mtx
);
3192 /* Blow away any cached route in the PCB */
3193 if (inp
->inp_route
.ro_rt
!= NULL
) {
3194 rtfree_locked(inp
->inp_route
.ro_rt
);
3195 inp
->inp_route
.ro_rt
= NULL
;
3197 lck_mtx_unlock(rt_mtx
);