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>
83 #include <machine/endian.h>
86 #include <net/if_dl.h>
87 #include <net/route.h>
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/ip.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip_var.h>
96 #include <netinet/kpi_ipfilter_var.h>
99 #include <security/mac_framework.h>
104 #include <net/dlil.h>
105 #include <sys/kdebug.h>
106 #include <libkern/OSAtomic.h>
108 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 1)
109 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 3)
110 #define DBG_FNC_IP_OUTPUT NETDBG_CODE(DBG_NETIP, (1 << 8) | 1)
111 #define DBG_FNC_IPSEC4_OUTPUT NETDBG_CODE(DBG_NETIP, (2 << 8) | 1)
113 #define SWAP16(v) ((((v) & 0xff) << 8) | ((v) >> 8))
116 #include <netinet6/ipsec.h>
117 #include <netkey/key.h>
119 #include <netkey/key_debug.h>
121 #define KEYDEBUG(lev,arg)
125 #include <netinet/ip_fw.h>
126 #include <netinet/ip_divert.h>
129 #include <netinet/ip_dummynet.h>
133 #include <net/pfvar.h>
136 #if IPFIREWALL_FORWARD_DEBUG
137 #define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
138 (ntohl(a.s_addr)>>16)&0xFF,\
139 (ntohl(a.s_addr)>>8)&0xFF,\
140 (ntohl(a.s_addr))&0xFF);
146 static struct mbuf
*ip_insertoptions(struct mbuf
*, struct mbuf
*, int *);
147 static struct ifnet
*ip_multicast_if(struct in_addr
*, int *);
148 static void ip_mloopback(struct ifnet
*, struct mbuf
*,
149 struct sockaddr_in
*, int);
150 static int ip_getmoptions(struct sockopt
*, struct ip_moptions
*);
151 static int ip_pcbopts(int, struct mbuf
**, struct mbuf
*);
152 static int ip_setmoptions(struct sockopt
*, struct ip_moptions
**);
154 static void ip_out_cksum_stats(int, u_int32_t
);
155 static struct ifaddr
*in_selectsrcif(struct ip
*, struct route
*, unsigned int);
156 static void ip_bindif(struct inpcb
*, unsigned int);
158 int ip_createmoptions(struct ip_moptions
**imop
);
159 int ip_addmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
160 int ip_dropmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
161 int ip_optcopy(struct ip
*, struct ip
*);
162 void in_delayed_cksum_offset(struct mbuf
*, int );
163 void in_cksum_offset(struct mbuf
* , size_t );
165 extern int (*fr_checkp
)(struct ip
*, int, struct ifnet
*, int, struct mbuf
**);
167 extern struct protosw inetsw
[];
169 extern struct ip_linklocal_stat ip_linklocal_stat
;
170 extern lck_mtx_t
*ip_mutex
;
172 /* temporary: for testing */
174 extern int ipsec_bypass
;
177 static int ip_maxchainsent
= 0;
178 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxchainsent
, CTLFLAG_RW
,
179 &ip_maxchainsent
, 0, "use dlil_output_list");
181 static int forge_ce
= 0;
182 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, forge_ce
, CTLFLAG_RW
,
183 &forge_ce
, 0, "Forge ECN CE");
186 static int ip_select_srcif_debug
= 0;
187 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, select_srcif_debug
, CTLFLAG_RW
,
188 &ip_select_srcif_debug
, 0, "log source interface selection debug info");
191 * IP output. The packet in mbuf chain m contains a skeletal IP
192 * header (with len, off, ttl, proto, tos, src, dst).
193 * The mbuf chain containing the packet will be freed.
194 * The mbuf opt, if present, will not be freed.
202 struct ip_moptions
*imo
,
203 struct ip_out_args
*ipoa
)
206 error
= ip_output_list(m0
, 0, opt
, ro
, flags
, imo
, ipoa
);
219 * ipsec4_getpolicybyaddr:??? [IPSEC 4th argument, contents modified]
220 * ipsec4_getpolicybysock:??? [IPSEC 4th argument, contents modified]
221 * key_spdacquire:??? [IPSEC]
222 * ipsec4_output:??? [IPSEC]
223 * <fr_checkp>:??? [firewall]
224 * ip_dn_io_ptr:??? [dummynet]
225 * dlil_output:??? [DLIL]
226 * dlil_output_list:??? [DLIL]
228 * Notes: The ipsec4_getpolicyby{addr|sock} function error returns are
229 * only used as the error return from this function where one of
230 * these functions fails to return a policy.
239 struct ip_moptions
*imo
,
240 struct ip_out_args
*ipoa
244 struct ifnet
*ifp
= NULL
;
245 struct mbuf
*m
= m0
, **mppn
= NULL
;
246 int hlen
= sizeof (struct ip
);
247 int len
= 0, off
, error
= 0;
248 struct sockaddr_in
*dst
= NULL
;
249 struct in_ifaddr
*ia
= NULL
, *src_ia
= NULL
;
250 int isbroadcast
, sw_csum
;
251 struct in_addr pkt_dst
;
253 struct route iproute
;
254 struct socket
*so
= NULL
;
255 struct secpolicy
*sp
= NULL
;
257 #if IPFIREWALL_FORWARD
258 int fwd_rewrite_src
= 0;
261 struct ip_fw_args args
;
264 ipfilter_t inject_filter_ref
= 0;
266 struct route saved_route
;
267 struct ip_out_args saved_ipoa
;
268 struct mbuf
* packetlist
;
269 int pktcnt
= 0, tso
= 0;
270 unsigned int ifscope
;
271 boolean_t select_srcif
;
273 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
277 args
.next_hop
= NULL
;
280 args
.divert_rule
= 0; /* divert cookie */
283 if (SLIST_EMPTY(&m0
->m_pkthdr
.tags
))
286 /* Grab info from mtags prepended to the chain */
288 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
,
289 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
290 struct dn_pkt_tag
*dn_tag
;
292 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
293 args
.rule
= dn_tag
->rule
;
295 saved_route
= dn_tag
->ro
;
299 dst
= dn_tag
->dn_dst
;
301 flags
= dn_tag
->flags
;
302 saved_ipoa
= dn_tag
->ipoa
;
305 m_tag_delete(m0
, tag
);
307 #endif /* DUMMYNET */
310 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
,
311 KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
312 struct divert_tag
*div_tag
;
314 div_tag
= (struct divert_tag
*)(tag
+1);
315 args
.divert_rule
= div_tag
->cookie
;
317 m_tag_delete(m0
, tag
);
319 #endif /* IPDIVERT */
321 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
,
322 KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
323 struct ip_fwd_tag
*ipfwd_tag
;
325 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
326 args
.next_hop
= ipfwd_tag
->next_hop
;
328 m_tag_delete(m0
, tag
);
331 #endif /* IPFIREWALL */
336 if ( !m
|| (m
->m_flags
& M_PKTHDR
) != 0)
337 panic("ip_output no HDR");
339 panic("ip_output no route, proto = %d",
340 mtod(m
, struct ip
*)->ip_p
);
344 * At present the IP_OUTARGS flag implies a request for IP to
345 * perform source interface selection. In the forwarding case,
346 * only the ifscope value is used, as source interface selection
347 * doesn't take place.
349 if (ip_doscopedroute
&& (flags
& IP_OUTARGS
)) {
350 select_srcif
= !(flags
& IP_FORWARDING
);
351 ifscope
= ipoa
->ipoa_ifscope
;
353 select_srcif
= FALSE
;
354 ifscope
= IFSCOPE_NONE
;
358 if (args
.rule
!= NULL
) { /* dummynet already saw us */
359 ip
= mtod(m
, struct ip
*);
360 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
361 if (ro
->ro_rt
!= NULL
) {
362 RT_LOCK_SPIN(ro
->ro_rt
);
363 ia
= (struct in_ifaddr
*)ro
->ro_rt
->rt_ifa
;
366 RT_UNLOCK(ro
->ro_rt
);
369 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
370 so
= ipsec_getsocket(m
);
371 (void)ipsec_setsocket(m
, NULL
);
376 #endif /* IPFIREWALL */
379 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
380 so
= ipsec_getsocket(m
);
381 (void)ipsec_setsocket(m
, NULL
);
386 * No need to proccess packet twice if we've
389 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
))
390 inject_filter_ref
= ipf_get_inject_filter(m
);
392 inject_filter_ref
= 0;
395 m
= ip_insertoptions(m
, opt
, &len
);
398 ip
= mtod(m
, struct ip
*);
400 pkt_dst
= args
.next_hop
? args
.next_hop
->sin_addr
: ip
->ip_dst
;
402 pkt_dst
= ip
->ip_dst
;
408 if ((flags
& (IP_FORWARDING
|IP_RAWOUTPUT
)) == 0) {
409 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, hlen
>> 2);
412 ip
->ip_id
= ip_randomid();
414 ip
->ip_id
= htons(ip_id
++);
416 OSAddAtomic(1, &ipstat
.ips_localout
);
418 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
422 /* For debugging, we let the stack forge congestion */
424 ((ip
->ip_tos
& IPTOS_ECN_MASK
) == IPTOS_ECN_ECT1
||
425 (ip
->ip_tos
& IPTOS_ECN_MASK
) == IPTOS_ECN_ECT0
)) {
426 ip
->ip_tos
= (ip
->ip_tos
& ~IPTOS_ECN_MASK
) | IPTOS_ECN_CE
;
431 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
432 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
434 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
437 * If there is a cached route,
438 * check that it is to the same destination
439 * and is still up. If not, free it and try again.
440 * The address family should also be checked in case of sharing the
444 if (ro
->ro_rt
!= NULL
) {
445 if (ro
->ro_rt
->generation_id
!= route_generation
&&
446 ((flags
& (IP_ROUTETOIF
| IP_FORWARDING
)) == 0) &&
447 (ip
->ip_src
.s_addr
!= INADDR_ANY
)) {
448 src_ia
= ifa_foraddr(ip
->ip_src
.s_addr
);
449 if (src_ia
== NULL
) {
450 error
= EADDRNOTAVAIL
;
453 ifafree(&src_ia
->ia_ifa
);
456 * Test rt_flags without holding rt_lock for performance
457 * reasons; if the route is down it will hopefully be
458 * caught by the layer below (since it uses this route
459 * as a hint) or during the next transmit.
461 if ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
462 dst
->sin_family
!= AF_INET
||
463 dst
->sin_addr
.s_addr
!= pkt_dst
.s_addr
) {
468 * If we're doing source interface selection, we may not
469 * want to use this route; only synch up the generation
472 if (!select_srcif
&& ro
->ro_rt
!= NULL
&&
473 ro
->ro_rt
->generation_id
!= route_generation
)
474 ro
->ro_rt
->generation_id
= route_generation
;
476 if (ro
->ro_rt
== NULL
) {
477 bzero(dst
, sizeof(*dst
));
478 dst
->sin_family
= AF_INET
;
479 dst
->sin_len
= sizeof(*dst
);
480 dst
->sin_addr
= pkt_dst
;
483 * If routing to interface only,
484 * short circuit routing lookup.
486 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
487 #define sintosa(sin) ((struct sockaddr *)(sin))
488 if (flags
& IP_ROUTETOIF
) {
490 ifafree(&ia
->ia_ifa
);
491 if ((ia
= ifatoia(ifa_ifwithdstaddr(sintosa(dst
)))) == 0) {
492 if ((ia
= ifatoia(ifa_ifwithnet(sintosa(dst
)))) == 0) {
493 OSAddAtomic(1, &ipstat
.ips_noroute
);
500 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
501 } else if (IN_MULTICAST(ntohl(pkt_dst
.s_addr
)) &&
502 imo
!= NULL
&& imo
->imo_multicast_ifp
!= NULL
) {
504 * Bypass the normal routing lookup for multicast
505 * packets if the interface is specified.
507 ifp
= imo
->imo_multicast_ifp
;
510 ifafree(&ia
->ia_ifa
);
512 /* Macro takes reference on ia */
515 boolean_t cloneok
= FALSE
;
517 * Perform source interface selection; the source IP address
518 * must belong to one of the addresses of the interface used
519 * by the route. For performance reasons, do this only if
520 * there is no route, or if the routing table has changed,
521 * or if we haven't done source interface selection on this
522 * route (for this PCB instance) before.
524 if (select_srcif
&& ip
->ip_src
.s_addr
!= INADDR_ANY
&&
525 (ro
->ro_rt
== NULL
|| !(ro
->ro_rt
->rt_flags
& RTF_UP
) ||
526 ro
->ro_rt
->generation_id
!= route_generation
||
527 !(ro
->ro_flags
& ROF_SRCIF_SELECTED
))) {
530 /* Find the source interface */
531 ifa
= in_selectsrcif(ip
, ro
, ifscope
);
534 * If the source address is spoofed (in the case
535 * of IP_RAWOUTPUT), or if this is destined for
536 * local/loopback, just let it go out using the
537 * interface of the route. Otherwise, there's no
538 * interface having such an address, so bail out.
540 if (ifa
== NULL
&& !(flags
& IP_RAWOUTPUT
) &&
541 ifscope
!= lo_ifp
->if_index
) {
542 error
= EADDRNOTAVAIL
;
547 * If the caller didn't explicitly specify the scope,
548 * pick it up from the source interface. If the cached
549 * route was wrong and was blown away as part of source
550 * interface selection, don't mask out RTF_PRCLONING
551 * since that route may have been allocated by the ULP,
552 * unless the IP header was created by the caller or
553 * the destination is IPv4 LLA. The check for the
554 * latter is needed because IPv4 LLAs are never scoped
555 * in the current implementation, and we don't want to
556 * replace the resolved IPv4 LLA route with one whose
557 * gateway points to that of the default gateway on
558 * the primary interface of the system.
561 if (ifscope
== IFSCOPE_NONE
)
562 ifscope
= ifa
->ifa_ifp
->if_index
;
564 cloneok
= (!(flags
& IP_RAWOUTPUT
) &&
565 !(IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
))));
570 * If this is the case, we probably don't want to allocate
571 * a protocol-cloned route since we didn't get one from the
572 * ULP. This lets TCP do its thing, while not burdening
573 * forwarding or ICMP with the overhead of cloning a route.
574 * Of course, we still want to do any cloning requested by
575 * the link layer, as this is probably required in all cases
576 * for correct operation (as it is for ARP).
578 if (ro
->ro_rt
== NULL
) {
579 unsigned long ign
= RTF_PRCLONING
;
581 * We make an exception here: if the destination
582 * address is INADDR_BROADCAST, allocate a protocol-
583 * cloned host route so that we end up with a route
584 * marked with the RTF_BROADCAST flag. Otherwise,
585 * we would end up referring to the default route,
586 * instead of creating a cloned host route entry.
587 * That would introduce inconsistencies between ULPs
588 * that allocate a route and those that don't. The
589 * RTF_BROADCAST route is important since we'd want
590 * to send out undirected IP broadcast packets using
591 * link-level broadcast address. Another exception
592 * is for ULP-created routes that got blown away by
593 * source interface selection (see above).
595 * These exceptions will no longer be necessary when
596 * the RTF_PRCLONING scheme is no longer present.
598 if (cloneok
|| dst
->sin_addr
.s_addr
== INADDR_BROADCAST
)
599 ign
&= ~RTF_PRCLONING
;
602 * Loosen the route lookup criteria if the ifscope
603 * corresponds to the loopback interface; this is
604 * needed to support Application Layer Gateways
605 * listening on loopback, in conjunction with packet
606 * filter redirection rules. The final source IP
607 * address will be rewritten by the packet filter
608 * prior to the RFC1122 loopback check below.
610 if (ifscope
== lo_ifp
->if_index
)
611 rtalloc_ign(ro
, ign
);
613 rtalloc_scoped_ign(ro
, ign
, ifscope
);
616 if (ro
->ro_rt
== NULL
) {
617 OSAddAtomic(1, &ipstat
.ips_noroute
);
618 error
= EHOSTUNREACH
;
623 ifafree(&ia
->ia_ifa
);
624 RT_LOCK_SPIN(ro
->ro_rt
);
625 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
628 ifp
= ro
->ro_rt
->rt_ifp
;
630 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
631 dst
= (struct sockaddr_in
*)ro
->ro_rt
->rt_gateway
;
632 if (ro
->ro_rt
->rt_flags
& RTF_HOST
)
633 isbroadcast
= (ro
->ro_rt
->rt_flags
& RTF_BROADCAST
);
635 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
636 RT_UNLOCK(ro
->ro_rt
);
639 if (IN_MULTICAST(ntohl(pkt_dst
.s_addr
))) {
640 struct in_multi
*inm
;
642 m
->m_flags
|= M_MCAST
;
644 * IP destination address is multicast. Make sure "dst"
645 * still points to the address in "ro". (It may have been
646 * changed to point to a gateway address, above.)
648 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
650 * See if the caller provided any multicast options
653 if ((flags
& IP_RAWOUTPUT
) == 0) ip
->ip_ttl
= imo
->imo_multicast_ttl
;
654 if (imo
->imo_multicast_ifp
!= NULL
) {
655 ifp
= imo
->imo_multicast_ifp
;
658 if (imo
->imo_multicast_vif
!= -1 &&
659 ((flags
& IP_RAWOUTPUT
) == 0 || ip
->ip_src
.s_addr
== INADDR_ANY
))
661 ip_mcast_src(imo
->imo_multicast_vif
);
662 #endif /* MROUTING */
664 if ((flags
& IP_RAWOUTPUT
) == 0) ip
->ip_ttl
= IP_DEFAULT_MULTICAST_TTL
;
666 * Confirm that the outgoing interface supports multicast.
668 if ((imo
== NULL
) || (imo
->imo_multicast_vif
== -1)) {
669 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
670 OSAddAtomic(1, &ipstat
.ips_noroute
);
676 * If source address not specified yet, use address
677 * of outgoing interface.
679 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
680 struct in_ifaddr
*ia1
;
681 lck_rw_lock_shared(in_ifaddr_rwlock
);
682 TAILQ_FOREACH(ia1
, &in_ifaddrhead
, ia_link
)
683 if (ia1
->ia_ifp
== ifp
) {
684 ip
->ip_src
= IA_SIN(ia1
)->sin_addr
;
687 lck_rw_done(in_ifaddr_rwlock
);
688 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
694 ifnet_lock_shared(ifp
);
695 IN_LOOKUP_MULTI(pkt_dst
, ifp
, inm
);
696 ifnet_lock_done(ifp
);
698 (imo
== NULL
|| imo
->imo_multicast_loop
)) {
700 * If we belong to the destination multicast group
701 * on the outgoing interface, and the caller did not
702 * forbid loopback, loop back a copy.
704 if (!TAILQ_EMPTY(&ipv4_filters
)) {
705 struct ipfilter
*filter
;
706 int seen
= (inject_filter_ref
== 0);
707 struct ipf_pktopts
*ippo
= 0, ipf_pktopts
;
711 ipf_pktopts
.ippo_mcast_ifnet
= imo
->imo_multicast_ifp
;
712 ipf_pktopts
.ippo_mcast_ttl
= imo
->imo_multicast_ttl
;
713 ipf_pktopts
.ippo_mcast_loop
= imo
->imo_multicast_loop
;
718 /* 4135317 - always pass network byte order to filter */
720 #if BYTE_ORDER != BIG_ENDIAN
725 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
727 if ((struct ipfilter
*)inject_filter_ref
== filter
)
729 } else if (filter
->ipf_filter
.ipf_output
) {
731 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, ippo
);
732 if (result
== EJUSTRETURN
) {
743 /* set back to host byte order */
744 ip
= mtod(m
, struct ip
*);
746 #if BYTE_ORDER != BIG_ENDIAN
754 ip_mloopback(ifp
, m
, dst
, hlen
);
759 * If we are acting as a multicast router, perform
760 * multicast forwarding as if the packet had just
761 * arrived on the interface to which we are about
762 * to send. The multicast forwarding function
763 * recursively calls this function, using the
764 * IP_FORWARDING flag to prevent infinite recursion.
766 * Multicasts that are looped back by ip_mloopback(),
767 * above, will be forwarded by the ip_input() routine,
770 if (ip_mrouter
&& (flags
& IP_FORWARDING
) == 0) {
772 * Check if rsvp daemon is running. If not, don't
773 * set ip_moptions. This ensures that the packet
774 * is multicast and not just sent down one link
775 * as prescribed by rsvpd.
779 if (ip_mforward(ip
, ifp
, m
, imo
) != 0) {
785 #endif /* MROUTING */
788 * Multicasts with a time-to-live of zero may be looped-
789 * back, above, but must not be transmitted on a network.
790 * Also, multicasts addressed to the loopback interface
791 * are not sent -- the above call to ip_mloopback() will
792 * loop back a copy if this host actually belongs to the
793 * destination group on the loopback interface.
795 if (ip
->ip_ttl
== 0 || ifp
->if_flags
& IFF_LOOPBACK
) {
804 * If source address not specified yet, use address
805 * of outgoing interface.
807 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
808 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
809 #if IPFIREWALL_FORWARD
810 /* Keep note that we did this - if the firewall changes
811 * the next-hop, our interface may change, changing the
812 * default source IP. It's a shame so much effort happens
816 #endif /* IPFIREWALL_FORWARD */
821 * Look for broadcast address and
822 * and verify user is allowed to send
826 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0) {
827 error
= EADDRNOTAVAIL
;
830 if ((flags
& IP_ALLOWBROADCAST
) == 0) {
834 /* don't allow broadcast messages to be fragmented */
835 if ((u_short
)ip
->ip_len
> ifp
->if_mtu
) {
839 m
->m_flags
|= M_BCAST
;
841 m
->m_flags
&= ~M_BCAST
;
846 /* Invoke outbound packet filter */
847 if (pf_af_hook(ifp
, mppn
, &m
, AF_INET
, FALSE
) != 0) {
848 if (packetlist
== m0
) {
854 /* Next packet in the chain */
856 } else if (packetlist
!= NULL
) {
857 /* No more packet; send down the chain */
860 /* Nothing left; we're done */
864 ip
= mtod(m
, struct ip
*);
865 pkt_dst
= ip
->ip_dst
;
866 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
869 * Force IP TTL to 255 following draft-ietf-zeroconf-ipv4-linklocal.txt
871 if (IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)) || IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
))) {
872 ip_linklocal_stat
.iplls_out_total
++;
873 if (ip
->ip_ttl
!= MAXTTL
) {
874 ip_linklocal_stat
.iplls_out_badttl
++;
879 if (!didfilter
&& !TAILQ_EMPTY(&ipv4_filters
)) {
880 struct ipfilter
*filter
;
881 int seen
= (inject_filter_ref
== 0);
883 /* Check that a TSO frame isn't passed to a filter.
884 * This could happen if a filter is inserted while
885 * TCP is sending the TSO packet.
887 if (m
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV4
) {
894 /* 4135317 - always pass network byte order to filter */
896 #if BYTE_ORDER != BIG_ENDIAN
901 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
903 if ((struct ipfilter
*)inject_filter_ref
== filter
)
905 } else if (filter
->ipf_filter
.ipf_output
) {
907 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, 0);
908 if (result
== EJUSTRETURN
) {
919 /* set back to host byte order */
920 ip
= mtod(m
, struct ip
*);
922 #if BYTE_ORDER != BIG_ENDIAN
931 /* temporary for testing only: bypass ipsec alltogether */
933 if (ipsec_bypass
!= 0 || (flags
& IP_NOIPSEC
) != 0)
936 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
939 /* get SP for this packet */
941 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, flags
, &error
);
943 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
946 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
947 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
954 switch (sp
->policy
) {
955 case IPSEC_POLICY_DISCARD
:
956 case IPSEC_POLICY_GENERATE
:
958 * This packet is just discarded.
960 IPSEC_STAT_INCREMENT(ipsecstat
.out_polvio
);
961 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 1,0,0,0,0);
964 case IPSEC_POLICY_BYPASS
:
965 case IPSEC_POLICY_NONE
:
966 /* no need to do IPsec. */
967 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 2,0,0,0,0);
970 case IPSEC_POLICY_IPSEC
:
971 if (sp
->req
== NULL
) {
972 /* acquire a policy */
973 error
= key_spdacquire(sp
);
974 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 3,0,0,0,0);
979 case IPSEC_POLICY_ENTRUST
:
981 printf("ip_output: Invalid policy found. %d\n", sp
->policy
);
984 struct ipsec_output_state state
;
985 bzero(&state
, sizeof(state
));
987 if (flags
& IP_ROUTETOIF
) {
989 bzero(&iproute
, sizeof(iproute
));
992 state
.dst
= (struct sockaddr
*)dst
;
998 * delayed checksums are not currently compatible with IPsec
1000 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1001 in_delayed_cksum(m
);
1002 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1006 #if BYTE_ORDER != BIG_ENDIAN
1011 error
= ipsec4_output(&state
, sp
, flags
);
1015 if (flags
& IP_ROUTETOIF
) {
1017 * if we have tunnel mode SA, we may need to ignore
1020 if (state
.ro
!= &iproute
|| state
.ro
->ro_rt
!= NULL
) {
1021 flags
&= ~IP_ROUTETOIF
;
1027 dst
= (struct sockaddr_in
*)state
.dst
;
1029 /* mbuf is already reclaimed in ipsec4_output. */
1039 printf("ip4_output (ipsec): error code %d\n", error
);
1042 /* don't show these error codes to the user */
1046 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 4,0,0,0,0);
1051 /* be sure to update variables that are affected by ipsec4_output() */
1052 ip
= mtod(m
, struct ip
*);
1055 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1057 hlen
= ip
->ip_hl
<< 2;
1059 /* Check that there wasn't a route change and src is still valid */
1060 if (ro
->ro_rt
!= NULL
&& ro
->ro_rt
->generation_id
!= route_generation
) {
1061 if ((src_ia
= ifa_foraddr(ip
->ip_src
.s_addr
)) == NULL
&&
1062 ((flags
& (IP_ROUTETOIF
| IP_FORWARDING
)) == 0)) {
1063 error
= EADDRNOTAVAIL
;
1064 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
,
1071 ifafree(&src_ia
->ia_ifa
);
1074 if (ro
->ro_rt
== NULL
) {
1075 if ((flags
& IP_ROUTETOIF
) == 0) {
1076 printf("ip_output: can't update route after "
1077 "IPsec processing\n");
1078 error
= EHOSTUNREACH
; /*XXX*/
1079 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
,
1085 ifafree(&ia
->ia_ifa
);
1086 RT_LOCK_SPIN(ro
->ro_rt
);
1087 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
1089 ifaref(&ia
->ia_ifa
);
1090 ifp
= ro
->ro_rt
->rt_ifp
;
1091 RT_UNLOCK(ro
->ro_rt
);
1094 /* make it flipped, again. */
1096 #if BYTE_ORDER != BIG_ENDIAN
1101 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 7,0xff,0xff,0xff,0xff);
1103 /* Pass to filters again */
1104 if (!TAILQ_EMPTY(&ipv4_filters
)) {
1105 struct ipfilter
*filter
;
1107 /* Check that a TSO frame isn't passed to a filter.
1108 * This could happen if a filter is inserted while
1109 * TCP is sending the TSO packet.
1111 if (m
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV4
) {
1118 /* 4135317 - always pass network byte order to filter */
1120 #if BYTE_ORDER != BIG_ENDIAN
1125 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
1126 if (filter
->ipf_filter
.ipf_output
) {
1128 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, 0);
1129 if (result
== EJUSTRETURN
) {
1140 /* set back to host byte order */
1141 ip
= mtod(m
, struct ip
*);
1143 #if BYTE_ORDER != BIG_ENDIAN
1156 * - Xlate: translate packet's addr/port (NAT).
1157 * - Firewall: deny/allow/etc.
1158 * - Wrap: fake packet's addr/port <unimpl.>
1159 * - Encapsulate: put it in another IP and send out. <unimp.>
1162 struct mbuf
*m1
= m
;
1164 if ((error
= (*fr_checkp
)(ip
, hlen
, ifp
, 1, &m1
)) || !m1
) {
1167 ip
= mtod(m0
= m
= m1
, struct ip
*);
1171 * Check with the firewall...
1172 * but not if we are already being fwd'd from a firewall.
1174 if (fw_enable
&& IPFW_LOADED
&& !args
.next_hop
) {
1175 struct sockaddr_in
*old
= dst
;
1178 args
.next_hop
= dst
;
1180 off
= ip_fw_chk_ptr(&args
);
1182 dst
= args
.next_hop
;
1185 * On return we must do the following:
1186 * IP_FW_PORT_DENY_FLAG -> drop the pkt (XXX new)
1187 * 1<=off<= 0xffff -> DIVERT
1188 * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe
1189 * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet
1190 * dst != old -> IPFIREWALL_FORWARD
1191 * off==0, dst==old -> accept
1192 * If some of the above modules is not compiled in, then
1193 * we should't have to check the corresponding condition
1194 * (because the ipfw control socket should not accept
1195 * unsupported rules), but better play safe and drop
1196 * packets in case of doubt.
1199 if ( (off
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) {
1205 ip
= mtod(m
, struct ip
*);
1207 if (off
== 0 && dst
== old
) {/* common case */
1211 if (DUMMYNET_LOADED
&& (off
& IP_FW_PORT_DYNT_FLAG
) != 0) {
1213 * pass the pkt to dummynet. Need to include
1214 * pipe number, m, ifp, ro, dst because these are
1215 * not recomputed in the next pass.
1216 * All other parameters have been already used and
1217 * so they are not needed anymore.
1218 * XXX note: if the ifp or ro entry are deleted
1219 * while a pkt is in dummynet, we are in trouble!
1224 if (flags
& IP_OUTARGS
)
1227 error
= ip_dn_io_ptr(m
, off
& 0xffff, DN_TO_IP_OUT
,
1231 #endif /* DUMMYNET */
1233 if (off
!= 0 && (off
& IP_FW_PORT_DYNT_FLAG
) == 0) {
1234 struct mbuf
*clone
= NULL
;
1236 /* Clone packet if we're doing a 'tee' */
1237 if ((off
& IP_FW_PORT_TEE_FLAG
) != 0)
1238 clone
= m_dup(m
, M_DONTWAIT
);
1241 * delayed checksums are not currently compatible
1242 * with divert sockets.
1244 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1245 in_delayed_cksum(m
);
1246 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1249 /* Restore packet header fields to original values */
1251 #if BYTE_ORDER != BIG_ENDIAN
1256 /* Deliver packet to divert input routine */
1257 divert_packet(m
, 0, off
& 0xffff, args
.divert_rule
);
1259 /* If 'tee', continue with original packet */
1260 if (clone
!= NULL
) {
1262 ip
= mtod(m
, struct ip
*);
1269 #if IPFIREWALL_FORWARD
1270 /* Here we check dst to make sure it's directly reachable on the
1271 * interface we previously thought it was.
1272 * If it isn't (which may be likely in some situations) we have
1273 * to re-route it (ie, find a route for the next-hop and the
1274 * associated interface) and set them here. This is nested
1275 * forwarding which in most cases is undesirable, except where
1276 * such control is nigh impossible. So we do it here.
1279 if (off
== 0 && old
!= dst
) {
1280 struct in_ifaddr
*ia_fw
;
1282 /* It's changed... */
1283 /* There must be a better way to do this next line... */
1284 static struct route sro_fwd
, *ro_fwd
= &sro_fwd
;
1285 #if IPFIREWALL_FORWARD_DEBUG
1286 printf("IPFIREWALL_FORWARD: New dst ip: ");
1287 print_ip(dst
->sin_addr
);
1291 * We need to figure out if we have been forwarded
1292 * to a local socket. If so then we should somehow
1293 * "loop back" to ip_input, and get directed to the
1294 * PCB as if we had received this packet. This is
1295 * because it may be dificult to identify the packets
1296 * you want to forward until they are being output
1297 * and have selected an interface. (e.g. locally
1298 * initiated packets) If we used the loopback inteface,
1299 * we would not be able to control what happens
1300 * as the packet runs through ip_input() as
1301 * it is done through a ISR.
1303 lck_rw_lock_shared(in_ifaddr_rwlock
);
1304 TAILQ_FOREACH(ia_fw
, &in_ifaddrhead
, ia_link
) {
1306 * If the addr to forward to is one
1307 * of ours, we pretend to
1308 * be the destination for this packet.
1310 if (IA_SIN(ia_fw
)->sin_addr
.s_addr
==
1311 dst
->sin_addr
.s_addr
)
1314 lck_rw_done(in_ifaddr_rwlock
);
1316 /* tell ip_input "dont filter" */
1317 struct m_tag
*fwd_tag
;
1318 struct ip_fwd_tag
*ipfwd_tag
;
1320 fwd_tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
,
1321 KERNEL_TAG_TYPE_IPFORWARD
,
1322 sizeof (*ipfwd_tag
), M_NOWAIT
);
1323 if (fwd_tag
== NULL
) {
1328 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1329 ipfwd_tag
->next_hop
= args
.next_hop
;
1331 m_tag_prepend(m
, fwd_tag
);
1333 if (m
->m_pkthdr
.rcvif
== NULL
)
1334 m
->m_pkthdr
.rcvif
= ifunit("lo0");
1335 if ((~IF_HWASSIST_CSUM_FLAGS(m
->m_pkthdr
.rcvif
->if_hwassist
) &
1336 m
->m_pkthdr
.csum_flags
) == 0) {
1337 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1338 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1339 m
->m_pkthdr
.csum_flags
|=
1340 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
1341 m
->m_pkthdr
.csum_data
= 0xffff;
1343 m
->m_pkthdr
.csum_flags
|=
1344 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
1346 else if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1347 in_delayed_cksum(m
);
1348 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1349 ip
->ip_sum
= in_cksum(m
, hlen
);
1352 #if BYTE_ORDER != BIG_ENDIAN
1357 /* we need to call dlil_output to run filters
1358 * and resync to avoid recursion loops.
1361 dlil_output(lo_ifp
, PF_INET
, m
, 0, (struct sockaddr
*)dst
, 0);
1364 printf("ip_output: no loopback ifp for forwarding!!!\n");
1368 /* Some of the logic for this was
1369 * nicked from above.
1371 * This rewrites the cached route in a local PCB.
1372 * Is this what we want to do?
1374 bcopy(dst
, &ro_fwd
->ro_dst
, sizeof(*dst
));
1376 ro_fwd
->ro_rt
= NULL
;
1377 rtalloc_ign(ro_fwd
, RTF_PRCLONING
);
1379 if (ro_fwd
->ro_rt
== NULL
) {
1380 OSAddAtomic(1, &ipstat
.ips_noroute
);
1381 error
= EHOSTUNREACH
;
1385 RT_LOCK_SPIN(ro_fwd
->ro_rt
);
1386 ia_fw
= ifatoia(ro_fwd
->ro_rt
->rt_ifa
);
1388 ifaref(&ia_fw
->ia_ifa
);
1389 ifp
= ro_fwd
->ro_rt
->rt_ifp
;
1390 ro_fwd
->ro_rt
->rt_use
++;
1391 if (ro_fwd
->ro_rt
->rt_flags
& RTF_GATEWAY
)
1392 dst
= (struct sockaddr_in
*)ro_fwd
->ro_rt
->rt_gateway
;
1393 if (ro_fwd
->ro_rt
->rt_flags
& RTF_HOST
)
1395 (ro_fwd
->ro_rt
->rt_flags
& RTF_BROADCAST
);
1397 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
1398 RT_UNLOCK(ro_fwd
->ro_rt
);
1400 ro
->ro_rt
= ro_fwd
->ro_rt
;
1401 dst
= (struct sockaddr_in
*)&ro_fwd
->ro_dst
;
1404 * If we added a default src ip earlier,
1405 * which would have been gotten from the-then
1406 * interface, do it again, from the new one.
1408 if (ia_fw
!= NULL
) {
1409 if (fwd_rewrite_src
)
1410 ip
->ip_src
= IA_SIN(ia_fw
)->sin_addr
;
1411 ifafree(&ia_fw
->ia_ifa
);
1415 #endif /* IPFIREWALL_FORWARD */
1417 * if we get here, none of the above matches, and
1418 * we have to drop the pkt
1421 error
= EACCES
; /* not sure this is the right error msg */
1424 #endif /* IPFIREWALL */
1428 /* Do not allow loopback address to wind up on a wire */
1429 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
1430 ((ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
1431 (ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
)) {
1432 OSAddAtomic(1, &ipstat
.ips_badaddr
);
1435 * Do not simply drop the packet just like a firewall -- we want the
1436 * the application to feel the pain.
1437 * Return ENETUNREACH like ip6_output does in some similar cases.
1438 * This can startle the otherwise clueless process that specifies
1439 * loopback as the source address.
1441 error
= ENETUNREACH
;
1445 m
->m_pkthdr
.csum_flags
|= CSUM_IP
;
1446 tso
= (ifp
->if_hwassist
& IFNET_TSO_IPV4
) && (m
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV4
);
1448 sw_csum
= m
->m_pkthdr
.csum_flags
1449 & ~IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
1451 if ((ifp
->if_hwassist
& CSUM_TCP_SUM16
) != 0) {
1453 * Special case code for GMACE
1454 * frames that can be checksumed by GMACE SUM16 HW:
1455 * frame >64, no fragments, no UDP
1457 if (apple_hwcksum_tx
&& (m
->m_pkthdr
.csum_flags
& CSUM_TCP
)
1458 && (ip
->ip_len
> 50) && (ip
->ip_len
<= ifp
->if_mtu
)) {
1459 /* Apple GMAC HW, expects STUFF_OFFSET << 16 | START_OFFSET */
1460 u_short offset
= (IP_VHL_HL(ip
->ip_vhl
) << 2) +14 ; /* IP+Enet header length */
1461 u_short csumprev
= m
->m_pkthdr
.csum_data
& 0xFFFF;
1462 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_TCP_SUM16
; /* for GMAC */
1463 m
->m_pkthdr
.csum_data
= (csumprev
+ offset
) << 16 ;
1464 m
->m_pkthdr
.csum_data
+= offset
;
1465 sw_csum
= CSUM_DELAY_IP
; /* do IP hdr chksum in software */
1468 /* let the software handle any UDP or TCP checksums */
1469 sw_csum
|= (CSUM_DELAY_DATA
& m
->m_pkthdr
.csum_flags
);
1471 } else if (apple_hwcksum_tx
== 0) {
1472 sw_csum
|= (CSUM_DELAY_DATA
| CSUM_DELAY_IP
) &
1473 m
->m_pkthdr
.csum_flags
;
1476 if (sw_csum
& CSUM_DELAY_DATA
) {
1477 in_delayed_cksum(m
);
1478 sw_csum
&= ~CSUM_DELAY_DATA
;
1479 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1482 if (apple_hwcksum_tx
!= 0) {
1483 m
->m_pkthdr
.csum_flags
&=
1484 IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
1486 m
->m_pkthdr
.csum_flags
= 0;
1490 * If small enough for interface, or the interface will take
1491 * care of the fragmentation for us, can just send directly.
1493 if ((u_short
)ip
->ip_len
<= ifp
->if_mtu
|| tso
||
1494 ifp
->if_hwassist
& CSUM_FRAGMENT
) {
1496 m
->m_pkthdr
.csum_flags
|= CSUM_TSO_IPV4
;
1499 #if BYTE_ORDER != BIG_ENDIAN
1505 if (sw_csum
& CSUM_DELAY_IP
) {
1506 ip
->ip_sum
= in_cksum(m
, hlen
);
1510 /* Record statistics for this interface address. */
1511 if (!(flags
& IP_FORWARDING
) && ia
!= NULL
) {
1512 ia
->ia_ifa
.if_opackets
++;
1513 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1518 /* clean ipsec history once it goes out of the node */
1519 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0)
1522 if (packetchain
== 0) {
1523 error
= ifnet_output(ifp
, PF_INET
, m
, ro
->ro_rt
,
1524 (struct sockaddr
*)dst
);
1527 else { /* packet chaining allows us to reuse the route for all packets */
1528 mppn
= &m
->m_nextpkt
;
1534 if (pktcnt
> ip_maxchainsent
)
1535 ip_maxchainsent
= pktcnt
;
1537 error
= ifnet_output(ifp
, PF_INET
, packetlist
,
1538 ro
->ro_rt
, (struct sockaddr
*)dst
);
1549 * Too large for interface; fragment if possible.
1550 * Must be able to put at least 8 bytes per fragment.
1553 if (ip
->ip_off
& IP_DF
|| (m
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV4
)) {
1556 * This case can happen if the user changed the MTU
1558 * of an interface after enabling IP on it. Because
1559 * most netifs don't keep track of routes pointing to
1560 * them, there is no way for one to update all its
1561 * routes when the MTU is changed.
1563 RT_LOCK_SPIN(ro
->ro_rt
);
1564 if (ro
->ro_rt
&& (ro
->ro_rt
->rt_flags
& (RTF_UP
| RTF_HOST
))
1565 && !(ro
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
1566 && (ro
->ro_rt
->rt_rmx
.rmx_mtu
> ifp
->if_mtu
)) {
1567 ro
->ro_rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
1569 RT_UNLOCK(ro
->ro_rt
);
1570 OSAddAtomic(1, &ipstat
.ips_cantfrag
);
1574 error
= ip_fragment(m
, ifp
, ifp
->if_mtu
, sw_csum
);
1580 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1581 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1583 for (m
= m0
; m
; m
= m0
) {
1587 /* clean ipsec history once it goes out of the node */
1588 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0)
1593 /* Record statistics for this interface address. */
1595 ia
->ia_ifa
.if_opackets
++;
1596 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1599 if ((packetchain
!= 0) && (pktcnt
> 0))
1600 panic("ip_output: mix of packet in packetlist is wrong=%p", packetlist
);
1601 error
= ifnet_output(ifp
, PF_INET
, m
, ro
->ro_rt
,
1602 (struct sockaddr
*)dst
);
1608 OSAddAtomic(1, &ipstat
.ips_fragmented
);
1612 ifafree(&ia
->ia_ifa
);
1616 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
1617 if (ro
== &iproute
&& ro
->ro_rt
) {
1622 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1623 printf("DP ip_output call free SP:%x\n", sp
));
1624 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1629 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_END
, error
,0,0,0,0);
1637 ip_fragment(struct mbuf
*m
, struct ifnet
*ifp
, unsigned long mtu
, int sw_csum
)
1639 struct ip
*ip
, *mhip
;
1640 int len
, hlen
, mhlen
, firstlen
, off
, error
= 0;
1641 struct mbuf
**mnext
= &m
->m_nextpkt
, *m0
;
1644 ip
= mtod(m
, struct ip
*);
1646 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1648 hlen
= ip
->ip_hl
<< 2;
1651 firstlen
= len
= (mtu
- hlen
) &~ 7;
1658 * if the interface will not calculate checksums on
1659 * fragmented packets, then do it here.
1661 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
&&
1662 (ifp
->if_hwassist
& CSUM_IP_FRAGS
) == 0) {
1663 in_delayed_cksum(m
);
1664 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1668 * Loop through length of segment after first fragment,
1669 * make new header and copy data of each part and link onto chain.
1672 mhlen
= sizeof (struct ip
);
1673 for (off
= hlen
+ len
; off
< (u_short
)ip
->ip_len
; off
+= len
) {
1674 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1677 OSAddAtomic(1, &ipstat
.ips_odropped
);
1680 m
->m_flags
|= (m0
->m_flags
& M_MCAST
) | M_FRAG
;
1681 m
->m_data
+= max_linkhdr
;
1682 mhip
= mtod(m
, struct ip
*);
1684 if (hlen
> sizeof (struct ip
)) {
1685 mhlen
= ip_optcopy(ip
, mhip
) + sizeof (struct ip
);
1686 mhip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, mhlen
>> 2);
1689 mhip
->ip_off
= ((off
- hlen
) >> 3) + (ip
->ip_off
& ~IP_MF
);
1690 if (ip
->ip_off
& IP_MF
)
1691 mhip
->ip_off
|= IP_MF
;
1692 if (off
+ len
>= (u_short
)ip
->ip_len
)
1693 len
= (u_short
)ip
->ip_len
- off
;
1695 mhip
->ip_off
|= IP_MF
;
1696 mhip
->ip_len
= htons((u_short
)(len
+ mhlen
));
1697 m
->m_next
= m_copy(m0
, off
, len
);
1698 if (m
->m_next
== 0) {
1700 error
= ENOBUFS
; /* ??? */
1701 OSAddAtomic(1, &ipstat
.ips_odropped
);
1704 m
->m_pkthdr
.len
= mhlen
+ len
;
1705 m
->m_pkthdr
.rcvif
= 0;
1706 m
->m_pkthdr
.csum_flags
= m0
->m_pkthdr
.csum_flags
;
1707 m
->m_pkthdr
.socket_id
= m0
->m_pkthdr
.socket_id
;
1709 mac_netinet_fragment(m0
, m
);
1712 #if BYTE_ORDER != BIG_ENDIAN
1713 HTONS(mhip
->ip_off
);
1717 if (sw_csum
& CSUM_DELAY_IP
) {
1718 mhip
->ip_sum
= in_cksum(m
, mhlen
);
1721 mnext
= &m
->m_nextpkt
;
1724 OSAddAtomic(nfrags
, &ipstat
.ips_ofragments
);
1726 /* set first/last markers for fragment chain */
1727 m
->m_flags
|= M_LASTFRAG
;
1728 m0
->m_flags
|= M_FIRSTFRAG
| M_FRAG
;
1729 m0
->m_pkthdr
.csum_data
= nfrags
;
1732 * Update first fragment by trimming what's been copied out
1733 * and updating header, then send each fragment (in order).
1736 m_adj(m
, hlen
+ firstlen
- (u_short
)ip
->ip_len
);
1737 m
->m_pkthdr
.len
= hlen
+ firstlen
;
1738 ip
->ip_len
= htons((u_short
)m
->m_pkthdr
.len
);
1739 ip
->ip_off
|= IP_MF
;
1741 #if BYTE_ORDER != BIG_ENDIAN
1746 if (sw_csum
& CSUM_DELAY_IP
) {
1747 ip
->ip_sum
= in_cksum(m
, hlen
);
1757 ip_out_cksum_stats(int proto
, u_int32_t len
)
1761 tcp_out_cksum_stats(len
);
1764 udp_out_cksum_stats(len
);
1767 /* keep only TCP or UDP stats for now */
1773 in_delayed_cksum_offset(struct mbuf
*m0
, int ip_offset
)
1776 unsigned char buf
[sizeof(struct ip
)];
1777 u_short csum
, offset
, ip_len
;
1778 struct mbuf
*m
= m0
;
1780 while (ip_offset
>= m
->m_len
) {
1781 ip_offset
-= m
->m_len
;
1784 printf("in_delayed_cksum_withoffset failed - ip_offset wasn't in the packet\n");
1789 /* Sometimes the IP header is not contiguous, yes this can happen! */
1790 if (ip_offset
+ sizeof(struct ip
) > m
->m_len
) {
1792 printf("delayed m_pullup, m->len: %d off: %d\n",
1793 m
->m_len
, ip_offset
);
1795 m_copydata(m
, ip_offset
, sizeof(struct ip
), (caddr_t
) buf
);
1797 ip
= (struct ip
*)buf
;
1799 ip
= (struct ip
*)(m
->m_data
+ ip_offset
);
1804 m
->m_len
-= ip_offset
;
1805 m
->m_data
+= ip_offset
;
1808 offset
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
1811 * We could be in the context of an IP or interface filter; in the
1812 * former case, ip_len would be in host (correct) order while for
1813 * the latter it would be in network order. Because of this, we
1814 * attempt to interpret the length field by comparing it against
1815 * the actual packet length. If the comparison fails, byte swap
1816 * the length and check again. If it still fails, then the packet
1817 * is bogus and we give up.
1819 ip_len
= ip
->ip_len
;
1820 if (ip_len
!= (m0
->m_pkthdr
.len
- ip_offset
)) {
1821 ip_len
= SWAP16(ip_len
);
1822 if (ip_len
!= (m0
->m_pkthdr
.len
- ip_offset
)) {
1823 printf("in_delayed_cksum_offset: ip_len %d (%d) "
1824 "doesn't match actual length %d\n", ip
->ip_len
,
1825 ip_len
, (m0
->m_pkthdr
.len
- ip_offset
));
1830 csum
= in_cksum_skip(m
, ip_len
, offset
);
1833 ip_out_cksum_stats(ip
->ip_p
, ip_len
- offset
);
1835 if (m0
->m_pkthdr
.csum_flags
& CSUM_UDP
&& csum
== 0)
1837 offset
+= m0
->m_pkthdr
.csum_data
& 0xFFFF; /* checksum offset */
1841 if (M_LEADINGSPACE(m
) < ip_offset
)
1842 panic("in_delayed_cksum_offset - chain modified!\n");
1843 m
->m_len
+= ip_offset
;
1844 m
->m_data
-= ip_offset
;
1847 if (offset
> ip_len
) /* bogus offset */
1850 /* Insert the checksum in the existing chain */
1851 if (offset
+ ip_offset
+ sizeof(u_short
) > m
->m_len
) {
1855 printf("delayed m_copyback, m->len: %d off: %d p: %d\n",
1856 m
->m_len
, offset
+ ip_offset
, ip
->ip_p
);
1858 *(u_short
*)tmp
= csum
;
1859 m_copyback(m
, offset
+ ip_offset
, 2, tmp
);
1861 *(u_short
*)(m
->m_data
+ offset
+ ip_offset
) = csum
;
1865 in_delayed_cksum(struct mbuf
*m
)
1867 in_delayed_cksum_offset(m
, 0);
1871 in_cksum_offset(struct mbuf
* m
, size_t ip_offset
)
1873 struct ip
* ip
= NULL
;
1875 unsigned char buf
[sizeof(struct ip
)];
1878 while (ip_offset
>= m
->m_len
) {
1879 ip_offset
-= m
->m_len
;
1882 printf("in_cksum_offset failed - ip_offset wasn't in the packet\n");
1887 /* Sometimes the IP header is not contiguous, yes this can happen! */
1888 if (ip_offset
+ sizeof(struct ip
) > m
->m_len
) {
1891 printf("in_cksum_offset - delayed m_pullup, m->len: %d off: %lu\n",
1892 m
->m_len
, ip_offset
);
1894 m_copydata(m
, ip_offset
, sizeof(struct ip
), (caddr_t
) buf
);
1896 ip
= (struct ip
*)buf
;
1898 m_copyback(m
, ip_offset
+ offsetof(struct ip
, ip_sum
), 2, (caddr_t
)&ip
->ip_sum
);
1900 ip
= (struct ip
*)(m
->m_data
+ ip_offset
);
1906 m
->m_len
-= ip_offset
;
1907 m
->m_data
+= ip_offset
;
1911 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1913 hlen
= ip
->ip_hl
<< 2;
1916 * We could be in the context of an IP or interface filter; in the
1917 * former case, ip_len would be in host order while for the latter
1918 * it would be in network (correct) order. Because of this, we
1919 * attempt to interpret the length field by comparing it against
1920 * the actual packet length. If the comparison fails, byte swap
1921 * the length and check again. If it still fails, then the packet
1922 * is bogus and we give up.
1924 if (ntohs(ip
->ip_len
) != (m
->m_pkthdr
.len
- ip_offset
)) {
1925 ip
->ip_len
= SWAP16(ip
->ip_len
);
1927 if (ntohs(ip
->ip_len
) != (m
->m_pkthdr
.len
- ip_offset
)) {
1928 ip
->ip_len
= SWAP16(ip
->ip_len
);
1929 printf("in_cksum_offset: ip_len %d (%d) "
1930 "doesn't match actual length %lu\n",
1931 ip
->ip_len
, SWAP16(ip
->ip_len
),
1932 (m
->m_pkthdr
.len
- ip_offset
));
1938 ip
->ip_sum
= in_cksum(m
, hlen
);
1940 ip
->ip_len
= SWAP16(ip
->ip_len
);
1944 if (M_LEADINGSPACE(m
) < ip_offset
)
1945 panic("in_cksum_offset - chain modified!\n");
1946 m
->m_len
+= ip_offset
;
1947 m
->m_data
-= ip_offset
;
1950 /* Insert the checksum in the existing chain if IP header not contiguous */
1951 if (ip_offset
+ sizeof(struct ip
) > m
->m_len
) {
1955 printf("in_cksum_offset m_copyback, m->len: %u off: %lu p: %d\n",
1956 m
->m_len
, ip_offset
+ offsetof(struct ip
, ip_sum
), ip
->ip_p
);
1958 *(u_short
*)tmp
= ip
->ip_sum
;
1959 m_copyback(m
, ip_offset
+ offsetof(struct ip
, ip_sum
), 2, tmp
);
1964 * Insert IP options into preformed packet.
1965 * Adjust IP destination as required for IP source routing,
1966 * as indicated by a non-zero in_addr at the start of the options.
1968 * XXX This routine assumes that the packet has no options in place.
1970 static struct mbuf
*
1971 ip_insertoptions(m
, opt
, phlen
)
1972 register struct mbuf
*m
;
1976 register struct ipoption
*p
= mtod(opt
, struct ipoption
*);
1978 register struct ip
*ip
= mtod(m
, struct ip
*);
1981 optlen
= opt
->m_len
- sizeof(p
->ipopt_dst
);
1982 if (optlen
+ (u_short
)ip
->ip_len
> IP_MAXPACKET
)
1983 return (m
); /* XXX should fail */
1984 if (p
->ipopt_dst
.s_addr
)
1985 ip
->ip_dst
= p
->ipopt_dst
;
1986 if (m
->m_flags
& M_EXT
|| m
->m_data
- optlen
< m
->m_pktdat
) {
1987 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1990 n
->m_pkthdr
.rcvif
= 0;
1992 mac_mbuf_label_copy(m
, n
);
1994 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
1995 m
->m_len
-= sizeof(struct ip
);
1996 m
->m_data
+= sizeof(struct ip
);
1999 m
->m_len
= optlen
+ sizeof(struct ip
);
2000 m
->m_data
+= max_linkhdr
;
2001 (void)memcpy(mtod(m
, void *), ip
, sizeof(struct ip
));
2003 m
->m_data
-= optlen
;
2005 m
->m_pkthdr
.len
+= optlen
;
2006 ovbcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
2008 ip
= mtod(m
, struct ip
*);
2009 bcopy(p
->ipopt_list
, ip
+ 1, optlen
);
2010 *phlen
= sizeof(struct ip
) + optlen
;
2011 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, *phlen
>> 2);
2012 ip
->ip_len
+= optlen
;
2017 * Copy options from ip to jp,
2018 * omitting those not copied during fragmentation.
2024 register u_char
*cp
, *dp
;
2025 int opt
, optlen
, cnt
;
2027 cp
= (u_char
*)(ip
+ 1);
2028 dp
= (u_char
*)(jp
+ 1);
2029 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
2030 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
2032 if (opt
== IPOPT_EOL
)
2034 if (opt
== IPOPT_NOP
) {
2035 /* Preserve for IP mcast tunnel's LSRR alignment. */
2041 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
2042 panic("malformed IPv4 option passed to ip_optcopy");
2044 optlen
= cp
[IPOPT_OLEN
];
2046 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
2047 panic("malformed IPv4 option passed to ip_optcopy");
2049 /* bogus lengths should have been caught by ip_dooptions */
2052 if (IPOPT_COPIED(opt
)) {
2053 bcopy(cp
, dp
, optlen
);
2057 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
2063 * IP socket option processing.
2066 ip_ctloutput(so
, sopt
)
2068 struct sockopt
*sopt
;
2070 struct inpcb
*inp
= sotoinpcb(so
);
2074 if (sopt
->sopt_level
!= IPPROTO_IP
) {
2078 switch (sopt
->sopt_dir
) {
2080 switch (sopt
->sopt_name
) {
2087 if (sopt
->sopt_valsize
> MLEN
) {
2091 MGET(m
, sopt
->sopt_p
!= kernproc
? M_WAIT
: M_DONTWAIT
,
2097 m
->m_len
= sopt
->sopt_valsize
;
2098 error
= sooptcopyin(sopt
, mtod(m
, char *), m
->m_len
,
2103 return (ip_pcbopts(sopt
->sopt_name
, &inp
->inp_options
,
2110 case IP_RECVRETOPTS
:
2111 case IP_RECVDSTADDR
:
2114 #if defined(NFAITH) && NFAITH > 0
2117 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2122 switch (sopt
->sopt_name
) {
2124 inp
->inp_ip_tos
= optval
;
2128 inp
->inp_ip_ttl
= optval
;
2130 #define OPTSET(bit) \
2132 inp->inp_flags |= bit; \
2134 inp->inp_flags &= ~bit;
2137 OPTSET(INP_RECVOPTS
);
2140 case IP_RECVRETOPTS
:
2141 OPTSET(INP_RECVRETOPTS
);
2144 case IP_RECVDSTADDR
:
2145 OPTSET(INP_RECVDSTADDR
);
2153 OPTSET(INP_RECVTTL
);
2156 #if defined(NFAITH) && NFAITH > 0
2165 #if CONFIG_FORCE_OUT_IFP
2167 * Apple private interface, similar to IP_BOUND_IF, except
2168 * that the parameter is a NULL-terminated string containing
2169 * the name of the network interface; an emptry string means
2170 * unbind. Applications are encouraged to use IP_BOUND_IF
2171 * instead, as that is the current "official" API.
2173 case IP_FORCE_OUT_IFP
: {
2174 char ifname
[IFNAMSIZ
];
2175 unsigned int ifscope
;
2177 /* This option is settable only for IPv4 */
2178 if (!(inp
->inp_vflag
& INP_IPV4
)) {
2183 /* Verify interface name parameter is sane */
2184 if (sopt
->sopt_valsize
> sizeof(ifname
)) {
2189 /* Copy the interface name */
2190 if (sopt
->sopt_valsize
!= 0) {
2191 error
= sooptcopyin(sopt
, ifname
,
2192 sizeof (ifname
), sopt
->sopt_valsize
);
2197 if (sopt
->sopt_valsize
== 0 || ifname
[0] == NULL
) {
2198 /* Unbind this socket from any interface */
2199 ifscope
= IFSCOPE_NONE
;
2203 /* Verify name is NULL terminated */
2204 if (ifname
[sopt
->sopt_valsize
- 1] != NULL
) {
2209 /* Bail out if given bogus interface name */
2210 if (ifnet_find_by_name(ifname
, &ifp
) != 0) {
2215 /* Bind this socket to this interface */
2216 ifscope
= ifp
->if_index
;
2219 * Won't actually free; since we don't release
2220 * this later, we should do it now.
2224 ip_bindif(inp
, ifscope
);
2228 case IP_MULTICAST_IF
:
2229 case IP_MULTICAST_VIF
:
2230 case IP_MULTICAST_TTL
:
2231 case IP_MULTICAST_LOOP
:
2232 case IP_ADD_MEMBERSHIP
:
2233 case IP_DROP_MEMBERSHIP
:
2234 error
= ip_setmoptions(sopt
, &inp
->inp_moptions
);
2238 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2244 case IP_PORTRANGE_DEFAULT
:
2245 inp
->inp_flags
&= ~(INP_LOWPORT
);
2246 inp
->inp_flags
&= ~(INP_HIGHPORT
);
2249 case IP_PORTRANGE_HIGH
:
2250 inp
->inp_flags
&= ~(INP_LOWPORT
);
2251 inp
->inp_flags
|= INP_HIGHPORT
;
2254 case IP_PORTRANGE_LOW
:
2255 inp
->inp_flags
&= ~(INP_HIGHPORT
);
2256 inp
->inp_flags
|= INP_LOWPORT
;
2266 case IP_IPSEC_POLICY
:
2274 if (sopt
->sopt_valsize
> MCLBYTES
) {
2278 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
2280 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) /* XXX */
2282 priv
= (proc_suser(sopt
->sopt_p
) == 0);
2284 req
= mtod(m
, caddr_t
);
2287 optname
= sopt
->sopt_name
;
2288 error
= ipsec4_set_policy(inp
, optname
, req
, len
, priv
);
2295 case IP_TRAFFIC_MGT_BACKGROUND
:
2297 unsigned background
= 0;
2298 error
= sooptcopyin(sopt
, &background
, sizeof(background
), sizeof(background
));
2303 so
->so_traffic_mgt_flags
|= TRAFFIC_MGT_SO_BACKGROUND
;
2305 so
->so_traffic_mgt_flags
&= ~TRAFFIC_MGT_SO_BACKGROUND
;
2309 #endif /* TRAFFIC_MGT */
2312 * On a multihomed system, scoped routing can be used to
2313 * restrict the source interface used for sending packets.
2314 * The socket option IP_BOUND_IF binds a particular AF_INET
2315 * socket to an interface such that data sent on the socket
2316 * is restricted to that interface. This is unlike the
2317 * SO_DONTROUTE option where the routing table is bypassed;
2318 * therefore it allows for a greater flexibility and control
2319 * over the system behavior, and does not place any restriction
2320 * on the destination address type (e.g. unicast, multicast,
2321 * or broadcast if applicable) or whether or not the host is
2322 * directly reachable. Note that in the multicast transmit
2323 * case, IP_MULTICAST_IF takes precedence over IP_BOUND_IF,
2324 * since the former practically bypasses the routing table;
2325 * in this case, IP_BOUND_IF sets the default interface used
2326 * for sending multicast packets in the absence of an explicit
2327 * transmit interface set via IP_MULTICAST_IF.
2330 /* This option is settable only for IPv4 */
2331 if (!(inp
->inp_vflag
& INP_IPV4
)) {
2336 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2342 ip_bindif(inp
, optval
);
2346 error
= ENOPROTOOPT
;
2352 switch (sopt
->sopt_name
) {
2355 if (inp
->inp_options
)
2356 error
= sooptcopyout(sopt
,
2357 mtod(inp
->inp_options
,
2359 inp
->inp_options
->m_len
);
2361 sopt
->sopt_valsize
= 0;
2367 case IP_RECVRETOPTS
:
2368 case IP_RECVDSTADDR
:
2372 #if defined(NFAITH) && NFAITH > 0
2375 switch (sopt
->sopt_name
) {
2378 optval
= inp
->inp_ip_tos
;
2382 optval
= inp
->inp_ip_ttl
;
2385 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
2388 optval
= OPTBIT(INP_RECVOPTS
);
2391 case IP_RECVRETOPTS
:
2392 optval
= OPTBIT(INP_RECVRETOPTS
);
2395 case IP_RECVDSTADDR
:
2396 optval
= OPTBIT(INP_RECVDSTADDR
);
2400 optval
= OPTBIT(INP_RECVIF
);
2404 optval
= OPTBIT(INP_RECVTTL
);
2408 if (inp
->inp_flags
& INP_HIGHPORT
)
2409 optval
= IP_PORTRANGE_HIGH
;
2410 else if (inp
->inp_flags
& INP_LOWPORT
)
2411 optval
= IP_PORTRANGE_LOW
;
2416 #if defined(NFAITH) && NFAITH > 0
2418 optval
= OPTBIT(INP_FAITH
);
2422 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2425 case IP_MULTICAST_IF
:
2426 case IP_MULTICAST_VIF
:
2427 case IP_MULTICAST_TTL
:
2428 case IP_MULTICAST_LOOP
:
2429 case IP_ADD_MEMBERSHIP
:
2430 case IP_DROP_MEMBERSHIP
:
2431 error
= ip_getmoptions(sopt
, inp
->inp_moptions
);
2435 case IP_IPSEC_POLICY
:
2437 struct mbuf
*m
= NULL
;
2442 req
= mtod(m
, caddr_t
);
2445 error
= ipsec4_get_policy(sotoinpcb(so
), req
, len
, &m
);
2447 error
= soopt_mcopyout(sopt
, m
); /* XXX */
2455 case IP_TRAFFIC_MGT_BACKGROUND
:
2457 unsigned background
= so
->so_traffic_mgt_flags
;
2458 return (sooptcopyout(sopt
, &background
, sizeof(background
)));
2461 #endif /* TRAFFIC_MGT */
2464 if (inp
->inp_flags
& INP_BOUND_IF
)
2465 optval
= inp
->inp_boundif
;
2466 error
= sooptcopyout(sopt
, &optval
, sizeof (optval
));
2470 error
= ENOPROTOOPT
;
2479 * Set up IP options in pcb for insertion in output packets.
2480 * Store in mbuf with pointer in pcbopt, adding pseudo-option
2481 * with destination address if source routed.
2485 __unused
int optname
,
2486 struct mbuf
**pcbopt
,
2487 register struct mbuf
*m
)
2489 register int cnt
, optlen
;
2490 register u_char
*cp
;
2493 /* turn off any old options */
2495 (void)m_free(*pcbopt
);
2497 if (m
== (struct mbuf
*)0 || m
->m_len
== 0) {
2499 * Only turning off any previous options.
2507 if (m
->m_len
% sizeof(int32_t))
2511 * IP first-hop destination address will be stored before
2512 * actual options; move other options back
2513 * and clear it when none present.
2515 if (m
->m_data
+ m
->m_len
+ sizeof(struct in_addr
) >= &m
->m_dat
[MLEN
])
2518 m
->m_len
+= sizeof(struct in_addr
);
2519 cp
= mtod(m
, u_char
*) + sizeof(struct in_addr
);
2520 ovbcopy(mtod(m
, caddr_t
), (caddr_t
)cp
, (unsigned)cnt
);
2521 bzero(mtod(m
, caddr_t
), sizeof(struct in_addr
));
2523 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
2524 opt
= cp
[IPOPT_OPTVAL
];
2525 if (opt
== IPOPT_EOL
)
2527 if (opt
== IPOPT_NOP
)
2530 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
2532 optlen
= cp
[IPOPT_OLEN
];
2533 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
2544 * user process specifies route as:
2546 * D must be our final destination (but we can't
2547 * check that since we may not have connected yet).
2548 * A is first hop destination, which doesn't appear in
2549 * actual IP option, but is stored before the options.
2551 if (optlen
< IPOPT_MINOFF
- 1 + sizeof(struct in_addr
))
2553 m
->m_len
-= sizeof(struct in_addr
);
2554 cnt
-= sizeof(struct in_addr
);
2555 optlen
-= sizeof(struct in_addr
);
2556 cp
[IPOPT_OLEN
] = optlen
;
2558 * Move first hop before start of options.
2560 bcopy((caddr_t
)&cp
[IPOPT_OFFSET
+1], mtod(m
, caddr_t
),
2561 sizeof(struct in_addr
));
2563 * Then copy rest of options back
2564 * to close up the deleted entry.
2566 ovbcopy((caddr_t
)(&cp
[IPOPT_OFFSET
+1] +
2567 sizeof(struct in_addr
)),
2568 (caddr_t
)&cp
[IPOPT_OFFSET
+1],
2569 (unsigned)cnt
+ sizeof(struct in_addr
));
2573 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
2585 * The whole multicast option thing needs to be re-thought.
2586 * Several of these options are equally applicable to non-multicast
2587 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
2588 * standard option (IP_TTL).
2592 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
2594 static struct ifnet
*
2595 ip_multicast_if(a
, ifindexp
)
2604 if (ntohl(a
->s_addr
) >> 24 == 0) {
2605 ifindex
= ntohl(a
->s_addr
) & 0xffffff;
2606 ifnet_head_lock_shared();
2607 if (ifindex
< 0 || if_index
< ifindex
) {
2611 ifp
= ifindex2ifnet
[ifindex
];
2614 *ifindexp
= ifindex
;
2616 INADDR_TO_IFP(*a
, ifp
);
2622 * Set the IP multicast options in response to user setsockopt().
2625 ip_setmoptions(sopt
, imop
)
2626 struct sockopt
*sopt
;
2627 struct ip_moptions
**imop
;
2631 struct in_addr addr
;
2632 struct ip_mreq mreq
;
2633 struct ifnet
*ifp
= NULL
;
2634 struct ip_moptions
*imo
= *imop
;
2639 * No multicast option buffer attached to the pcb;
2640 * allocate one and initialize to default values.
2642 error
= ip_createmoptions(imop
);
2648 switch (sopt
->sopt_name
) {
2649 /* store an index number for the vif you wanna use in the send */
2651 case IP_MULTICAST_VIF
:
2652 if (legal_vif_num
== 0) {
2656 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2659 if (!legal_vif_num(i
) && (i
!= -1)) {
2663 imo
->imo_multicast_vif
= i
;
2665 #endif /* MROUTING */
2667 case IP_MULTICAST_IF
:
2669 * Select the interface for outgoing multicast packets.
2671 error
= sooptcopyin(sopt
, &addr
, sizeof addr
, sizeof addr
);
2675 * INADDR_ANY is used to remove a previous selection.
2676 * When no interface is selected, a default one is
2677 * chosen every time a multicast packet is sent.
2679 if (addr
.s_addr
== INADDR_ANY
) {
2680 imo
->imo_multicast_ifp
= NULL
;
2684 * The selected interface is identified by its local
2685 * IP address. Find the interface and confirm that
2686 * it supports multicasting.
2688 ifp
= ip_multicast_if(&addr
, &ifindex
);
2689 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2690 error
= EADDRNOTAVAIL
;
2693 imo
->imo_multicast_ifp
= ifp
;
2695 imo
->imo_multicast_addr
= addr
;
2697 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2700 case IP_MULTICAST_TTL
:
2702 * Set the IP time-to-live for outgoing multicast packets.
2703 * The original multicast API required a char argument,
2704 * which is inconsistent with the rest of the socket API.
2705 * We allow either a char or an int.
2707 if (sopt
->sopt_valsize
== 1) {
2709 error
= sooptcopyin(sopt
, &ttl
, 1, 1);
2712 imo
->imo_multicast_ttl
= ttl
;
2715 error
= sooptcopyin(sopt
, &ttl
, sizeof ttl
,
2722 imo
->imo_multicast_ttl
= ttl
;
2726 case IP_MULTICAST_LOOP
:
2728 * Set the loopback flag for outgoing multicast packets.
2729 * Must be zero or one. The original multicast API required a
2730 * char argument, which is inconsistent with the rest
2731 * of the socket API. We allow either a char or an int.
2733 if (sopt
->sopt_valsize
== 1) {
2735 error
= sooptcopyin(sopt
, &loop
, 1, 1);
2738 imo
->imo_multicast_loop
= !!loop
;
2741 error
= sooptcopyin(sopt
, &loop
, sizeof loop
,
2745 imo
->imo_multicast_loop
= !!loop
;
2749 case IP_ADD_MEMBERSHIP
:
2751 * Add a multicast group membership.
2752 * Group must be a valid IP multicast address.
2754 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
2758 error
= ip_addmembership(imo
, &mreq
);
2761 case IP_DROP_MEMBERSHIP
:
2763 * Drop a multicast group membership.
2764 * Group must be a valid IP multicast address.
2766 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
2770 error
= ip_dropmembership(imo
, &mreq
);
2779 * If all options have default values, no need to keep the mbuf.
2781 if (imo
->imo_multicast_ifp
== NULL
&&
2782 imo
->imo_multicast_vif
== (u_int32_t
)-1 &&
2783 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
2784 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
2785 imo
->imo_num_memberships
== 0) {
2786 FREE(*imop
, M_IPMOPTS
);
2794 * Set the IP multicast options in response to user setsockopt().
2796 __private_extern__
int
2798 struct ip_moptions
**imop
)
2800 struct ip_moptions
*imo
;
2801 imo
= (struct ip_moptions
*) _MALLOC(sizeof(*imo
), M_IPMOPTS
,
2807 imo
->imo_multicast_ifp
= NULL
;
2808 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2809 imo
->imo_multicast_vif
= -1;
2810 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
2811 imo
->imo_multicast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
2812 imo
->imo_num_memberships
= 0;
2818 * Add membership to an IPv4 multicast.
2820 __private_extern__
int
2822 struct ip_moptions
*imo
,
2823 struct ip_mreq
*mreq
)
2826 struct sockaddr_in
*dst
;
2827 struct ifnet
*ifp
= NULL
;
2831 bzero((caddr_t
)&ro
, sizeof(ro
));
2833 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
2838 * If no interface address was provided, use the interface of
2839 * the route to the given multicast address.
2841 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
) {
2842 dst
= (struct sockaddr_in
*)&ro
.ro_dst
;
2843 dst
->sin_len
= sizeof(*dst
);
2844 dst
->sin_family
= AF_INET
;
2845 dst
->sin_addr
= mreq
->imr_multiaddr
;
2846 rtalloc_ign(&ro
, 0);
2847 if (ro
.ro_rt
!= NULL
) {
2848 ifp
= ro
.ro_rt
->rt_ifp
;
2850 /* If there's no default route, try using loopback */
2851 mreq
->imr_interface
.s_addr
= htonl(INADDR_LOOPBACK
);
2856 ifp
= ip_multicast_if(&mreq
->imr_interface
, NULL
);
2860 * See if we found an interface, and confirm that it
2861 * supports multicast.
2863 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2864 error
= EADDRNOTAVAIL
;
2868 * See if the membership already exists or if all the
2869 * membership slots are full.
2871 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
2872 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
&&
2873 imo
->imo_membership
[i
]->inm_addr
.s_addr
2874 == mreq
->imr_multiaddr
.s_addr
)
2877 if (i
< imo
->imo_num_memberships
) {
2881 if (i
== IP_MAX_MEMBERSHIPS
) {
2882 error
= ETOOMANYREFS
;
2886 * Everything looks good; add a new record to the multicast
2887 * address list for the given interface.
2889 if ((imo
->imo_membership
[i
] =
2890 in_addmulti(&mreq
->imr_multiaddr
, ifp
)) == NULL
) {
2894 ++imo
->imo_num_memberships
;
2897 if (ro
.ro_rt
!= NULL
)
2904 * Drop membership of an IPv4 multicast.
2906 __private_extern__
int
2908 struct ip_moptions
*imo
,
2909 struct ip_mreq
*mreq
)
2912 struct ifnet
* ifp
= NULL
;
2915 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
2921 * If an interface address was specified, get a pointer
2922 * to its ifnet structure.
2924 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
)
2927 ifp
= ip_multicast_if(&mreq
->imr_interface
, NULL
);
2929 error
= EADDRNOTAVAIL
;
2934 * Find the membership in the membership array.
2936 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
2938 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
2939 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
2940 mreq
->imr_multiaddr
.s_addr
)
2943 if (i
== imo
->imo_num_memberships
) {
2944 error
= EADDRNOTAVAIL
;
2948 * Give up the multicast address record to which the
2949 * membership points.
2951 in_delmulti(&imo
->imo_membership
[i
]);
2953 * Remove the gap in the membership array.
2955 for (++i
; i
< imo
->imo_num_memberships
; ++i
)
2956 imo
->imo_membership
[i
-1] = imo
->imo_membership
[i
];
2957 --imo
->imo_num_memberships
;
2963 * Return the IP multicast options in response to user getsockopt().
2966 ip_getmoptions(sopt
, imo
)
2967 struct sockopt
*sopt
;
2968 register struct ip_moptions
*imo
;
2970 struct in_addr addr
;
2971 struct in_ifaddr
*ia
;
2976 switch (sopt
->sopt_name
) {
2978 case IP_MULTICAST_VIF
:
2980 optval
= imo
->imo_multicast_vif
;
2983 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2985 #endif /* MROUTING */
2987 case IP_MULTICAST_IF
:
2988 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
)
2989 addr
.s_addr
= INADDR_ANY
;
2990 else if (imo
->imo_multicast_addr
.s_addr
) {
2991 /* return the value user has set */
2992 addr
= imo
->imo_multicast_addr
;
2994 IFP_TO_IA(imo
->imo_multicast_ifp
, ia
);
2995 addr
.s_addr
= (ia
== NULL
) ? INADDR_ANY
2996 : IA_SIN(ia
)->sin_addr
.s_addr
;
2998 ifafree(&ia
->ia_ifa
);
3000 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
3003 case IP_MULTICAST_TTL
:
3005 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
3007 optval
= coptval
= imo
->imo_multicast_ttl
;
3008 if (sopt
->sopt_valsize
== 1)
3009 error
= sooptcopyout(sopt
, &coptval
, 1);
3011 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
3014 case IP_MULTICAST_LOOP
:
3016 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
3018 optval
= coptval
= imo
->imo_multicast_loop
;
3019 if (sopt
->sopt_valsize
== 1)
3020 error
= sooptcopyout(sopt
, &coptval
, 1);
3022 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
3026 error
= ENOPROTOOPT
;
3033 * Discard the IP multicast options.
3036 ip_freemoptions(imo
)
3037 register struct ip_moptions
*imo
;
3042 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
)
3043 in_delmulti(&imo
->imo_membership
[i
]);
3044 FREE(imo
, M_IPMOPTS
);
3049 * Routine called from ip_output() to loop back a copy of an IP multicast
3050 * packet to the input queue of a specified interface. Note that this
3051 * calls the output routine of the loopback "driver", but with an interface
3052 * pointer that might NOT be a loopback interface -- evil, but easier than
3053 * replicating that code here.
3056 ip_mloopback(ifp
, m
, dst
, hlen
)
3058 register struct mbuf
*m
;
3059 register struct sockaddr_in
*dst
;
3062 register struct ip
*ip
;
3064 int sw_csum
= (apple_hwcksum_tx
== 0);
3066 copym
= m_copy(m
, 0, M_COPYALL
);
3067 if (copym
!= NULL
&& (copym
->m_flags
& M_EXT
|| copym
->m_len
< hlen
))
3068 copym
= m_pullup(copym
, hlen
);
3074 * We don't bother to fragment if the IP length is greater
3075 * than the interface's MTU. Can this possibly matter?
3077 ip
= mtod(copym
, struct ip
*);
3079 #if BYTE_ORDER != BIG_ENDIAN
3085 ip
->ip_sum
= in_cksum(copym
, hlen
);
3088 * It's not clear whether there are any lingering
3089 * reentrancy problems in other areas which might
3090 * be exposed by using ip_input directly (in
3091 * particular, everything which modifies the packet
3092 * in-place). Yet another option is using the
3093 * protosw directly to deliver the looped back
3094 * packet. For the moment, we'll err on the side
3095 * of safety by using if_simloop().
3098 if (dst
->sin_family
!= AF_INET
) {
3099 printf("ip_mloopback: bad address family %d\n",
3101 dst
->sin_family
= AF_INET
;
3106 * Mark checksum as valid or calculate checksum for loopback.
3108 * This is done this way because we have to embed the ifp of
3109 * the interface we will send the original copy of the packet
3110 * out on in the mbuf. ip_input will check if_hwassist of the
3111 * embedded ifp and ignore all csum_flags if if_hwassist is 0.
3112 * The UDP checksum has not been calculated yet.
3114 if (sw_csum
|| (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
)) {
3115 if (!sw_csum
&& IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
)) {
3116 copym
->m_pkthdr
.csum_flags
|=
3117 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
3118 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
3119 copym
->m_pkthdr
.csum_data
= 0xffff;
3122 #if BYTE_ORDER != BIG_ENDIAN
3126 in_delayed_cksum(copym
);
3128 #if BYTE_ORDER != BIG_ENDIAN
3137 * We need to send all loopback traffic down to dlil in case
3138 * a filter has tapped-in.
3142 * Stuff the 'real' ifp into the pkthdr, to be used in matching
3143 * in ip_input(); we need the loopback ifp/dl_tag passed as args
3144 * to make the loopback driver compliant with the data link
3148 copym
->m_pkthdr
.rcvif
= ifp
;
3149 dlil_output(lo_ifp
, PF_INET
, copym
, 0,
3150 (struct sockaddr
*) dst
, 0);
3152 printf("Warning: ip_output call to dlil_find_dltag failed!\n");
3158 * Given a source IP address (and route, if available), determine the best
3159 * interface to send the packet from. Checking for (and updating) the
3160 * ROF_SRCIF_SELECTED flag in the pcb-supplied route placeholder is done
3161 * without any locks based on the assumption that ip_output() is single-
3162 * threaded per-pcb, i.e. for any given pcb there can only be one thread
3163 * performing output at the IP layer.
3165 static struct ifaddr
*
3166 in_selectsrcif(struct ip
*ip
, struct route
*ro
, unsigned int ifscope
)
3168 struct ifaddr
*ifa
= NULL
;
3169 struct in_addr src
= ip
->ip_src
;
3170 struct in_addr dst
= ip
->ip_dst
;
3171 struct ifnet
*rt_ifp
;
3172 char s_src
[16], s_dst
[16];
3174 if (ip_select_srcif_debug
) {
3175 (void) inet_ntop(AF_INET
, &src
.s_addr
, s_src
, sizeof (s_src
));
3176 (void) inet_ntop(AF_INET
, &dst
.s_addr
, s_dst
, sizeof (s_dst
));
3179 if (ro
->ro_rt
!= NULL
)
3182 rt_ifp
= (ro
->ro_rt
!= NULL
) ? ro
->ro_rt
->rt_ifp
: NULL
;
3185 * Given the source IP address, find a suitable source interface
3186 * to use for transmission; if the caller has specified a scope,
3187 * optimize the search by looking at the addresses only for that
3188 * interface. This is still suboptimal, however, as we need to
3189 * traverse the per-interface list.
3191 if (ifscope
!= IFSCOPE_NONE
|| ro
->ro_rt
!= NULL
) {
3192 unsigned int scope
= ifscope
;
3195 * If no scope is specified and the route is stale (pointing
3196 * to a defunct interface) use the current primary interface;
3197 * this happens when switching between interfaces configured
3198 * with the same IP address. Otherwise pick up the scope
3199 * information from the route; the ULP may have looked up a
3200 * correct route and we just need to verify it here and mark
3201 * it with the ROF_SRCIF_SELECTED flag below.
3203 if (scope
== IFSCOPE_NONE
) {
3204 scope
= rt_ifp
->if_index
;
3205 if (scope
!= get_primary_ifscope() &&
3206 ro
->ro_rt
->generation_id
!= route_generation
)
3207 scope
= get_primary_ifscope();
3210 ifa
= (struct ifaddr
*)ifa_foraddr_scoped(src
.s_addr
, scope
);
3212 if (ip_select_srcif_debug
&& ifa
!= NULL
) {
3213 if (ro
->ro_rt
!= NULL
) {
3214 printf("%s->%s ifscope %d->%d ifa_if %s%d "
3215 "ro_if %s%d\n", s_src
, s_dst
, ifscope
,
3216 scope
, ifa
->ifa_ifp
->if_name
,
3217 ifa
->ifa_ifp
->if_unit
, rt_ifp
->if_name
,
3220 printf("%s->%s ifscope %d->%d ifa_if %s%d\n",
3221 s_src
, s_dst
, ifscope
, scope
,
3222 ifa
->ifa_ifp
->if_name
,
3223 ifa
->ifa_ifp
->if_unit
);
3229 * Slow path; search for an interface having the corresponding source
3230 * IP address if the scope was not specified by the caller, and:
3232 * 1) There currently isn't any route, or,
3233 * 2) The interface used by the route does not own that source
3234 * IP address; in this case, the route will get blown away
3235 * and we'll do a more specific scoped search using the newly
3238 if (ifa
== NULL
&& ifscope
== IFSCOPE_NONE
) {
3239 ifa
= (struct ifaddr
*)ifa_foraddr(src
.s_addr
);
3241 if (ip_select_srcif_debug
&& ifa
!= NULL
) {
3242 printf("%s->%s ifscope %d ifa_if %s%d\n",
3243 s_src
, s_dst
, ifscope
, ifa
->ifa_ifp
->if_name
,
3244 ifa
->ifa_ifp
->if_unit
);
3248 if (ro
->ro_rt
!= NULL
)
3249 RT_LOCK_ASSERT_HELD(ro
->ro_rt
);
3251 * If there is a non-loopback route with the wrong interface, or if
3252 * there is no interface configured with such an address, blow it
3253 * away. Except for local/loopback, we look for one with a matching
3254 * interface scope/index.
3256 if (ro
->ro_rt
!= NULL
&&
3257 (ifa
== NULL
|| (ifa
->ifa_ifp
!= rt_ifp
&& rt_ifp
!= lo_ifp
) ||
3258 !(ro
->ro_rt
->rt_flags
& RTF_UP
))) {
3259 if (ip_select_srcif_debug
) {
3261 printf("%s->%s ifscope %d ro_if %s%d != "
3262 "ifa_if %s%d (cached route cleared)\n",
3263 s_src
, s_dst
, ifscope
, rt_ifp
->if_name
,
3264 rt_ifp
->if_unit
, ifa
->ifa_ifp
->if_name
,
3265 ifa
->ifa_ifp
->if_unit
);
3267 printf("%s->%s ifscope %d ro_if %s%d "
3268 "(no ifa_if found)\n",
3269 s_src
, s_dst
, ifscope
, rt_ifp
->if_name
,
3274 RT_UNLOCK(ro
->ro_rt
);
3277 ro
->ro_flags
&= ~ROF_SRCIF_SELECTED
;
3280 * If the destination is IPv4 LLA and the route's interface
3281 * doesn't match the source interface, then the source IP
3282 * address is wrong; it most likely belongs to the primary
3283 * interface associated with the IPv4 LL subnet. Drop the
3284 * packet rather than letting it go out and return an error
3285 * to the ULP. This actually applies not only to IPv4 LL
3286 * but other shared subnets; for now we explicitly test only
3287 * for the former case and save the latter for future.
3289 if (IN_LINKLOCAL(ntohl(dst
.s_addr
)) &&
3290 !IN_LINKLOCAL(ntohl(src
.s_addr
)) && ifa
!= NULL
) {
3296 if (ip_select_srcif_debug
&& ifa
== NULL
) {
3297 printf("%s->%s ifscope %d (neither ro_if/ifa_if found)\n",
3298 s_src
, s_dst
, ifscope
);
3302 * If there is a route, mark it accordingly. If there isn't one,
3303 * we'll get here again during the next transmit (possibly with a
3304 * route) and the flag will get set at that point. For IPv4 LLA
3305 * destination, mark it only if the route has been fully resolved;
3306 * otherwise we want to come back here again when the route points
3307 * to the interface over which the ARP reply arrives on.
3309 if (ro
->ro_rt
!= NULL
&& (!IN_LINKLOCAL(ntohl(dst
.s_addr
)) ||
3310 (ro
->ro_rt
->rt_gateway
->sa_family
== AF_LINK
&&
3311 SDL(ro
->ro_rt
->rt_gateway
)->sdl_alen
!= 0))) {
3312 ro
->ro_flags
|= ROF_SRCIF_SELECTED
;
3313 ro
->ro_rt
->generation_id
= route_generation
;
3316 if (ro
->ro_rt
!= NULL
)
3317 RT_UNLOCK(ro
->ro_rt
);
3323 * Handler for setting IP_FORCE_OUT_IFP or IP_BOUND_IF socket option.
3326 ip_bindif(struct inpcb
*inp
, unsigned int ifscope
)
3329 * A zero interface scope value indicates an "unbind".
3330 * Otherwise, take in whatever value the app desires;
3331 * the app may already know the scope (or force itself
3332 * to such a scope) ahead of time before the interface
3333 * gets attached. It doesn't matter either way; any
3334 * route lookup from this point on will require an
3335 * exact match for the embedded interface scope.
3337 inp
->inp_boundif
= ifscope
;
3338 if (inp
->inp_boundif
== IFSCOPE_NONE
)
3339 inp
->inp_flags
&= ~INP_BOUND_IF
;
3341 inp
->inp_flags
|= INP_BOUND_IF
;
3343 /* Blow away any cached route in the PCB */
3344 if (inp
->inp_route
.ro_rt
!= NULL
) {
3345 rtfree(inp
->inp_route
.ro_rt
);
3346 inp
->inp_route
.ro_rt
= NULL
;