2 * Copyright (c) 2009-2010 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 /* $FreeBSD: src/sys/netinet6/ip6_forward.c,v 1.16 2002/10/16 02:25:05 sam Exp $ */
30 /* $KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project 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 PROJECT 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 PROJECT 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
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/malloc.h>
66 #include <sys/domain.h>
67 #include <sys/protosw.h>
68 #include <sys/socket.h>
69 #include <sys/errno.h>
71 #include <sys/kernel.h>
72 #include <sys/syslog.h>
75 #include <net/route.h>
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_var.h>
82 #include <netinet6/in6_var.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_var.h>
85 #include <netinet/icmp6.h>
86 #include <netinet6/nd6.h>
87 #include <netinet6/scope6_var.h>
89 #include <netinet/in_pcb.h>
92 #include <netinet6/ipsec.h>
94 #include <netinet6/ipsec6.h>
96 #include <netkey/key.h>
97 extern int ipsec_bypass
;
100 #include <netinet6/ip6_fw.h>
102 #include <net/net_osdep.h>
105 #include <net/pfvar.h>
109 * Forward a packet. If some error occurs return the sender
110 * an icmp packet. Note we can't always generate a meaningful
111 * icmp message because icmp doesn't have a large enough repertoire
112 * of codes and types.
114 * If not forwarding, just drop the packet. This could be confusing
115 * if ipforwarding was zero but some routing protocol was advancing
116 * us as a gateway to somewhere. However, we must let the routing
117 * protocol deal with that.
122 ip6_forward(struct mbuf
*m
, struct route_in6
*ip6forward_rt
,
125 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
126 struct sockaddr_in6
*dst
;
128 int error
, type
= 0, code
= 0;
129 struct mbuf
*mcopy
= NULL
;
130 struct ifnet
*ifp
, *origifp
; /* maybe unnecessary */
131 u_int32_t inzone
, outzone
;
132 struct in6_addr src_in6
, dst_in6
;
134 struct secpolicy
*sp
= NULL
;
136 struct timeval timenow
;
137 unsigned int ifscope
= IFSCOPE_NONE
;
139 struct pf_mtag
*pf_mtag
;
142 getmicrotime(&timenow
);
144 pf_mtag
= pf_find_mtag(m
);
145 if (pf_mtag
!= NULL
&& pf_mtag
->rtableid
!= IFSCOPE_NONE
)
146 ifscope
= pf_mtag
->rtableid
;
151 * Check AH/ESP integrity.
154 * Don't increment ip6s_cantforward because this is the check
155 * before forwarding packet actually.
157 if (ipsec_bypass
== 0) {
158 if (ipsec6_in_reject(m
, NULL
)) {
159 IPSEC_STAT_INCREMENT(ipsec6stat
.in_polvio
);
167 * Do not forward packets to multicast destination (should be handled
169 * Do not forward packets with unspecified source. It was discussed
170 * in July 2000, on ipngwg mailing list.
172 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) != 0 ||
173 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
174 IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
175 ip6stat
.ip6s_cantforward
++;
176 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
177 if (ip6_log_time
+ ip6_log_interval
< timenow
.tv_sec
) {
178 ip6_log_time
= timenow
.tv_sec
;
181 "from %s to %s nxt %d received on %s\n",
182 ip6_sprintf(&ip6
->ip6_src
),
183 ip6_sprintf(&ip6
->ip6_dst
),
185 if_name(m
->m_pkthdr
.rcvif
));
191 if (ip6
->ip6_hlim
<= IPV6_HLIMDEC
) {
192 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
193 icmp6_error(m
, ICMP6_TIME_EXCEEDED
,
194 ICMP6_TIME_EXCEED_TRANSIT
, 0);
197 ip6
->ip6_hlim
-= IPV6_HLIMDEC
;
200 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
201 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
202 * we need to generate an ICMP6 message to the src.
203 * Thanks to M_EXT, in most cases copy will not occur.
205 * It is important to save it before IPsec processing as IPsec
206 * processing may modify the mbuf.
208 mcopy
= m_copy(m
, 0, imin(m
->m_pkthdr
.len
, ICMPV6_PLD_MAXLEN
));
211 if (ipsec_bypass
!= 0)
213 /* get a security policy for this packet */
214 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, IP_FORWARDING
,
217 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
218 ip6stat
.ip6s_cantforward
++;
221 /* XXX: what icmp ? */
233 switch (sp
->policy
) {
234 case IPSEC_POLICY_DISCARD
:
235 case IPSEC_POLICY_GENERATE
:
237 * This packet is just discarded.
239 IPSEC_STAT_INCREMENT(ipsec6stat
.out_polvio
);
240 ip6stat
.ip6s_cantforward
++;
241 key_freesp(sp
, KEY_SADB_UNLOCKED
);
244 /* XXX: what icmp ? */
252 case IPSEC_POLICY_BYPASS
:
253 case IPSEC_POLICY_NONE
:
254 /* no need to do IPsec. */
255 key_freesp(sp
, KEY_SADB_UNLOCKED
);
258 case IPSEC_POLICY_IPSEC
:
259 if (sp
->req
== NULL
) {
260 /* XXX should be panic ? */
261 printf("ip6_forward: No IPsec request specified.\n");
262 ip6stat
.ip6s_cantforward
++;
263 key_freesp(sp
, KEY_SADB_UNLOCKED
);
266 /* XXX: what icmp ? */
277 case IPSEC_POLICY_ENTRUST
:
279 /* should be panic ?? */
280 printf("ip6_forward: Invalid policy found. %d\n", sp
->policy
);
281 key_freesp(sp
, KEY_SADB_UNLOCKED
);
286 struct ipsec_output_state state
;
289 * All the extension headers will become inaccessible
290 * (since they can be encrypted).
291 * Don't panic, we need no more updates to extension headers
292 * on inner IPv6 packet (since they are now encapsulated).
294 * IPv6 [ESP|AH] IPv6 [extension headers] payload
296 bzero(&state
, sizeof(state
));
298 state
.dst
= NULL
; /* update at ipsec6_output_tunnel() */
300 error
= ipsec6_output_tunnel(&state
, sp
, 0);
301 key_freesp(sp
, KEY_SADB_UNLOCKED
);
302 if (state
.tunneled
== 4)
303 return; /* packet is gone - sent over IPv4 */
306 if (state
.ro
.ro_rt
) {
307 rtfree(state
.ro
.ro_rt
);
308 state
.ro
.ro_rt
= NULL
;
311 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
320 printf("ip6_output (ipsec): error code %d\n", error
);
323 /* don't show these error codes to the user */
326 ip6stat
.ip6s_cantforward
++;
329 /* XXX: what icmp ? */
341 dst
= (struct sockaddr_in6
*)&ip6forward_rt
->ro_dst
;
342 if ((rt
= ip6forward_rt
->ro_rt
) != NULL
) {
344 /* Take an extra ref for ourselves */
345 RT_ADDREF_LOCKED(rt
);
350 * ip6forward_rt->ro_dst.sin6_addr is equal to ip6->ip6_dst
352 if (rt
== NULL
|| !(rt
->rt_flags
& RTF_UP
) ||
353 rt
->generation_id
!= route_generation
) {
355 /* Release extra ref */
356 RT_REMREF_LOCKED(rt
);
359 ip6forward_rt
->ro_rt
= NULL
;
361 /* this probably fails but give it a try again */
362 rtalloc_scoped_ign((struct route
*)ip6forward_rt
,
363 RTF_PRCLONING
, ifscope
);
364 if ((rt
= ip6forward_rt
->ro_rt
) != NULL
) {
366 /* Take an extra ref for ourselves */
367 RT_ADDREF_LOCKED(rt
);
372 ip6stat
.ip6s_noroute
++;
373 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_noroute
);
375 icmp6_error(mcopy
, ICMP6_DST_UNREACH
,
376 ICMP6_DST_UNREACH_NOROUTE
, 0);
380 RT_LOCK_ASSERT_HELD(rt
);
381 } else if (rt
== NULL
|| !(rt
->rt_flags
& RTF_UP
) ||
382 !IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &dst
->sin6_addr
) ||
383 rt
->generation_id
!= route_generation
) {
385 /* Release extra ref */
386 RT_REMREF_LOCKED(rt
);
389 ip6forward_rt
->ro_rt
= NULL
;
391 bzero(dst
, sizeof(*dst
));
392 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
393 dst
->sin6_family
= AF_INET6
;
394 dst
->sin6_addr
= ip6
->ip6_dst
;
396 rtalloc_scoped_ign((struct route
*)ip6forward_rt
,
397 RTF_PRCLONING
, ifscope
);
398 if ((rt
= ip6forward_rt
->ro_rt
) == NULL
) {
399 ip6stat
.ip6s_noroute
++;
400 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_noroute
);
402 icmp6_error(mcopy
, ICMP6_DST_UNREACH
,
403 ICMP6_DST_UNREACH_NOROUTE
, 0);
408 /* Take an extra ref for ourselves */
409 RT_ADDREF_LOCKED(rt
);
413 * Source scope check: if a packet can't be delivered to its
414 * destination for the reason that the destination is beyond the scope
415 * of the source address, discard the packet and return an icmp6
416 * destination unreachable error with Code 2 (beyond scope of source
417 * address). We use a local copy of ip6_src, since in6_setscope()
418 * will possibly modify its first argument.
419 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1]
421 src_in6
= ip6
->ip6_src
;
422 if (in6_setscope(&src_in6
, rt
->rt_ifp
, &outzone
)) {
423 /* XXX: this should not happen */
424 ip6stat
.ip6s_cantforward
++;
425 ip6stat
.ip6s_badscope
++;
429 if (in6_setscope(&src_in6
, m
->m_pkthdr
.rcvif
, &inzone
)) {
430 ip6stat
.ip6s_cantforward
++;
431 ip6stat
.ip6s_badscope
++;
435 if (inzone
!= outzone
) {
436 ip6stat
.ip6s_cantforward
++;
437 ip6stat
.ip6s_badscope
++;
438 in6_ifstat_inc(rt
->rt_ifp
, ifs6_in_discard
);
440 if (ip6_log_time
+ ip6_log_interval
< timenow
.tv_sec
) {
441 ip6_log_time
= timenow
.tv_sec
;
444 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
445 ip6_sprintf(&ip6
->ip6_src
),
446 ip6_sprintf(&ip6
->ip6_dst
),
448 if_name(m
->m_pkthdr
.rcvif
), if_name(rt
->rt_ifp
));
450 /* Release extra ref */
451 RT_REMREF_LOCKED(rt
);
454 icmp6_error(mcopy
, ICMP6_DST_UNREACH
,
455 ICMP6_DST_UNREACH_BEYONDSCOPE
, 0);
462 * Destination scope check: if a packet is going to break the scope
463 * zone of packet's destination address, discard it. This case should
464 * usually be prevented by appropriately-configured routing table, but
465 * we need an explicit check because we may mistakenly forward the
466 * packet to a different zone by (e.g.) a default route.
468 dst_in6
= ip6
->ip6_dst
;
469 if (in6_setscope(&dst_in6
, m
->m_pkthdr
.rcvif
, &inzone
) != 0 ||
470 in6_setscope(&dst_in6
, rt
->rt_ifp
, &outzone
) != 0 ||
472 ip6stat
.ip6s_cantforward
++;
473 ip6stat
.ip6s_badscope
++;
478 if (m
->m_pkthdr
.len
> rt
->rt_ifp
->if_mtu
) {
479 in6_ifstat_inc(rt
->rt_ifp
, ifs6_in_toobig
);
483 struct secpolicy
*sp2
;
488 mtu
= rt
->rt_ifp
->if_mtu
;
491 * When we do IPsec tunnel ingress, we need to play
492 * with the link value (decrement IPsec header size
493 * from mtu value). The code is much simpler than v4
494 * case, as we have the outgoing interface for
495 * encapsulated packet as "rt->rt_ifp".
497 sp2
= ipsec6_getpolicybyaddr(mcopy
, IPSEC_DIR_OUTBOUND
,
498 IP_FORWARDING
, &ipsecerror
);
500 ipsechdrsiz
= ipsec6_hdrsiz(mcopy
,
501 IPSEC_DIR_OUTBOUND
, NULL
);
502 if (ipsechdrsiz
< mtu
)
504 key_freesp(sp2
, KEY_SADB_UNLOCKED
);
507 * if mtu becomes less than minimum MTU,
508 * tell minimum MTU (and I'll need to fragment it).
513 /* Release extra ref */
514 RT_REMREF_LOCKED(rt
);
516 icmp6_error(mcopy
, ICMP6_PACKET_TOO_BIG
, 0, mtu
);
518 /* Release extra ref */
519 RT_REMREF_LOCKED(rt
);
526 if (rt
->rt_flags
& RTF_GATEWAY
)
527 dst
= (struct sockaddr_in6
*)rt
->rt_gateway
;
530 * If we are to forward the packet using the same interface
531 * as one we got the packet from, perhaps we should send a redirect
532 * to sender to shortcut a hop.
533 * Only send redirect if source is sending directly to us,
534 * and if packet was not source routed (or has any options).
535 * Also, don't send redirect if forwarding using a route
536 * modified by a redirect.
538 if (ip6_sendredirects
&& rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&& !srcrt
&&
539 (rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0) {
540 if ((rt
->rt_ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
542 * If the incoming interface is equal to the outgoing
543 * one, and the link attached to the interface is
544 * point-to-point, then it will be highly probable
545 * that a routing loop occurs. Thus, we immediately
546 * drop the packet and send an ICMPv6 error message.
548 * type/code is based on suggestion by Rich Draves.
549 * not sure if it is the best pick.
551 RT_REMREF_LOCKED(rt
); /* Release extra ref */
553 icmp6_error(mcopy
, ICMP6_DST_UNREACH
,
554 ICMP6_DST_UNREACH_ADDR
, 0);
563 * Check with the firewall...
565 if (ip6_fw_enable
&& ip6_fw_chk_ptr
) {
568 /* Drop the lock but retain the extra ref */
570 /* If ipfw says divert, we have to just drop packet */
571 if (ip6_fw_chk_ptr(&ip6
, ifp
, &port
, &m
)) {
578 /* We still have the extra ref on rt */
584 * Fake scoped addresses. Note that even link-local source or
585 * destinaion can appear, if the originating node just sends the
586 * packet to us (without address resolution for the destination).
587 * Since both icmp6_error and icmp6_redirect_output fill the embedded
588 * link identifiers, we can do this stuff after making a copy for
589 * returning an error.
591 if ((rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
593 * See corresponding comments in ip6_output.
594 * XXX: but is it possible that ip6_forward() sends a packet
595 * to a loopback interface? I don't think so, and thus
596 * I bark here. (jinmei@kame.net)
597 * XXX: it is common to route invalid packets to loopback.
598 * also, the codepath will be visited on use of ::1 in
604 if ((rt
->rt_flags
& (RTF_BLACKHOLE
|RTF_REJECT
)) == 0)
607 printf("ip6_forward: outgoing interface is loopback. "
608 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
609 ip6_sprintf(&ip6
->ip6_src
),
610 ip6_sprintf(&ip6
->ip6_dst
),
611 ip6
->ip6_nxt
, if_name(m
->m_pkthdr
.rcvif
),
612 if_name(rt
->rt_ifp
));
615 /* we can just use rcvif in forwarding. */
616 origifp
= m
->m_pkthdr
.rcvif
;
619 origifp
= rt
->rt_ifp
;
621 * clear embedded scope identifiers if necessary.
622 * in6_clearscope will touch the addresses only when necessary.
624 in6_clearscope(&ip6
->ip6_src
);
625 in6_clearscope(&ip6
->ip6_dst
);
628 /* Drop the lock but retain the extra ref */
632 /* Invoke outbound packet filter */
633 error
= pf_af_hook(ifp
, NULL
, &m
, AF_INET6
, FALSE
);
637 panic("%s: unexpected packet %p\n", __func__
, m
);
640 /* Already freed by callee */
643 ip6
= mtod(m
, struct ip6_hdr
*);
646 error
= nd6_output(ifp
, origifp
, m
, dst
, rt
);
648 in6_ifstat_inc(ifp
, ifs6_out_discard
);
649 ip6stat
.ip6s_cantforward
++;
651 ip6stat
.ip6s_forward
++;
652 in6_ifstat_inc(ifp
, ifs6_out_forward
);
654 ip6stat
.ip6s_redirectsent
++;
665 /* Release extra ref */
672 if (type
== ND_REDIRECT
) {
673 icmp6_redirect_output(mcopy
, rt
);
674 /* Release extra ref */
682 /* xxx MTU is constant in PPP? */
686 /* Tell source to slow down like source quench in IP? */
689 case ENETUNREACH
: /* shouldn't happen, checked above */
694 type
= ICMP6_DST_UNREACH
;
695 code
= ICMP6_DST_UNREACH_ADDR
;
698 icmp6_error(mcopy
, type
, code
, 0);
699 /* Release extra ref */
705 /* Release extra ref */