xref: /freebsd/sys/netinet6/ip6_forward.c (revision b50e1465e88dcf5f6f008892d802df010e7029d1)
1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
482cd038dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
582cd038dSYoshinobu Inoue  * All rights reserved.
682cd038dSYoshinobu Inoue  *
782cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
882cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
982cd038dSYoshinobu Inoue  * are met:
1082cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
1182cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1282cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1382cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1482cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1582cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
1682cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
1782cd038dSYoshinobu Inoue  *    without specific prior written permission.
1882cd038dSYoshinobu Inoue  *
1982cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2082cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2182cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2282cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2382cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2482cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2582cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2682cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2782cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2882cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2982cd038dSYoshinobu Inoue  * SUCH DAMAGE.
30b48287a3SDavid E. O'Brien  *
31b48287a3SDavid E. O'Brien  *	$KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $
3282cd038dSYoshinobu Inoue  */
3382cd038dSYoshinobu Inoue 
34b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
35b48287a3SDavid E. O'Brien __FBSDID("$FreeBSD$");
36b48287a3SDavid E. O'Brien 
37686cdd19SJun-ichiro itojun Hagino #include "opt_inet.h"
38686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
396a800098SYoshinobu Inoue #include "opt_ipsec.h"
40f8a8f9caSSUZUKI Shinsuke #include "opt_ipstealth.h"
4149c5659eSMark Johnston #include "opt_sctp.h"
4282cd038dSYoshinobu Inoue 
4382cd038dSYoshinobu Inoue #include <sys/param.h>
4482cd038dSYoshinobu Inoue #include <sys/systm.h>
4533841545SHajimu UMEMOTO #include <sys/malloc.h>
4682cd038dSYoshinobu Inoue #include <sys/mbuf.h>
4782cd038dSYoshinobu Inoue #include <sys/domain.h>
4882cd038dSYoshinobu Inoue #include <sys/protosw.h>
4982cd038dSYoshinobu Inoue #include <sys/socket.h>
5082cd038dSYoshinobu Inoue #include <sys/errno.h>
5182cd038dSYoshinobu Inoue #include <sys/time.h>
5233841545SHajimu UMEMOTO #include <sys/kernel.h>
5382cd038dSYoshinobu Inoue #include <sys/syslog.h>
5482cd038dSYoshinobu Inoue 
5582cd038dSYoshinobu Inoue #include <net/if.h>
5676039bc8SGleb Smirnoff #include <net/if_var.h>
573d0d5b21SJustin Hibbits #include <net/if_private.h>
588a006adbSBjoern A. Zeeb #include <net/netisr.h>
5982cd038dSYoshinobu Inoue #include <net/route.h>
6053a4886dSAlexander V. Chernikov #include <net/route/nhop.h>
613a4d67f7SDarren Reed #include <net/pfil.h>
6282cd038dSYoshinobu Inoue 
6382cd038dSYoshinobu Inoue #include <netinet/in.h>
6482cd038dSYoshinobu Inoue #include <netinet/in_var.h>
6533841545SHajimu UMEMOTO #include <netinet/in_systm.h>
6633841545SHajimu UMEMOTO #include <netinet/ip.h>
67686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_var.h>
6833841545SHajimu UMEMOTO #include <netinet6/in6_var.h>
69686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
7053a4886dSAlexander V. Chernikov #include <netinet6/in6_fib.h>
7182cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
72a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
73686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
7482cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
7582cd038dSYoshinobu Inoue 
7633841545SHajimu UMEMOTO #include <netinet/in_pcb.h>
7733841545SHajimu UMEMOTO 
78fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
79b9234fafSSam Leffler 
8082cd038dSYoshinobu Inoue /*
8182cd038dSYoshinobu Inoue  * Forward a packet.  If some error occurs return the sender
8282cd038dSYoshinobu Inoue  * an icmp packet.  Note we can't always generate a meaningful
8382cd038dSYoshinobu Inoue  * icmp message because icmp doesn't have a large enough repertoire
8482cd038dSYoshinobu Inoue  * of codes and types.
8582cd038dSYoshinobu Inoue  *
8682cd038dSYoshinobu Inoue  * If not forwarding, just drop the packet.  This could be confusing
8782cd038dSYoshinobu Inoue  * if ipforwarding was zero but some routing protocol was advancing
8882cd038dSYoshinobu Inoue  * us as a gateway to somewhere.  However, we must let the routing
8982cd038dSYoshinobu Inoue  * protocol deal with that.
9082cd038dSYoshinobu Inoue  *
9182cd038dSYoshinobu Inoue  */
9282cd038dSYoshinobu Inoue void
931272577eSXin LI ip6_forward(struct mbuf *m, int srcrt)
9482cd038dSYoshinobu Inoue {
9582cd038dSYoshinobu Inoue 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
9653a4886dSAlexander V. Chernikov 	struct sockaddr_in6 dst;
9753a4886dSAlexander V. Chernikov 	struct nhop_object *nh = NULL;
9882cd038dSYoshinobu Inoue 	int error, type = 0, code = 0;
99686cdd19SJun-ichiro itojun Hagino 	struct mbuf *mcopy = NULL;
100686cdd19SJun-ichiro itojun Hagino 	struct ifnet *origifp;	/* maybe unnecessary */
101a1f7e5f8SHajimu UMEMOTO 	u_int32_t inzone, outzone;
10253a4886dSAlexander V. Chernikov 	struct in6_addr odst;
1038a006adbSBjoern A. Zeeb 	struct m_tag *fwd_tag;
1041d54aa3bSBjoern A. Zeeb 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
10582cd038dSYoshinobu Inoue 
10633841545SHajimu UMEMOTO 	/*
10733841545SHajimu UMEMOTO 	 * Do not forward packets to multicast destination (should be handled
10833841545SHajimu UMEMOTO 	 * by ip6_mforward().
10933841545SHajimu UMEMOTO 	 * Do not forward packets with unspecified source.  It was discussed
1107efe5d92SHajimu UMEMOTO 	 * in July 2000, on the ipngwg mailing list.
11133841545SHajimu UMEMOTO 	 */
112686cdd19SJun-ichiro itojun Hagino 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
11333841545SHajimu UMEMOTO 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
11433841545SHajimu UMEMOTO 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1159cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_cantforward);
11682cd038dSYoshinobu Inoue 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
11735b6e52cSPawel Biernacki 		if (V_ip6_log_cannot_forward && ip6_log_ratelimit()) {
11882cd038dSYoshinobu Inoue 			log(LOG_DEBUG,
11982cd038dSYoshinobu Inoue 			    "cannot forward "
12082cd038dSYoshinobu Inoue 			    "from %s to %s nxt %d received on %s\n",
1211d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
1221d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
12382cd038dSYoshinobu Inoue 			    ip6->ip6_nxt,
12482cd038dSYoshinobu Inoue 			    if_name(m->m_pkthdr.rcvif));
12582cd038dSYoshinobu Inoue 		}
12682cd038dSYoshinobu Inoue 		m_freem(m);
12782cd038dSYoshinobu Inoue 		return;
12882cd038dSYoshinobu Inoue 	}
12982cd038dSYoshinobu Inoue 
130fcf59617SAndrey V. Elsukov 	if (
131f8a8f9caSSUZUKI Shinsuke #ifdef IPSTEALTH
132fcf59617SAndrey V. Elsukov 	    V_ip6stealth == 0 &&
133f8a8f9caSSUZUKI Shinsuke #endif
134fcf59617SAndrey V. Elsukov 	    ip6->ip6_hlim <= IPV6_HLIMDEC) {
13582cd038dSYoshinobu Inoue 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
13682cd038dSYoshinobu Inoue 		icmp6_error(m, ICMP6_TIME_EXCEEDED,
13782cd038dSYoshinobu Inoue 		    ICMP6_TIME_EXCEED_TRANSIT, 0);
13882cd038dSYoshinobu Inoue 		return;
13982cd038dSYoshinobu Inoue 	}
140f8a8f9caSSUZUKI Shinsuke 
141686cdd19SJun-ichiro itojun Hagino 	/*
142686cdd19SJun-ichiro itojun Hagino 	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
143686cdd19SJun-ichiro itojun Hagino 	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
144686cdd19SJun-ichiro itojun Hagino 	 * we need to generate an ICMP6 message to the src.
145686cdd19SJun-ichiro itojun Hagino 	 * Thanks to M_EXT, in most cases copy will not occur.
146686cdd19SJun-ichiro itojun Hagino 	 *
147686cdd19SJun-ichiro itojun Hagino 	 * It is important to save it before IPsec processing as IPsec
148686cdd19SJun-ichiro itojun Hagino 	 * processing may modify the mbuf.
149686cdd19SJun-ichiro itojun Hagino 	 */
150c3bef61eSKevin Lo 	mcopy = m_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
151c3bef61eSKevin Lo 	    M_NOWAIT);
152fcf59617SAndrey V. Elsukov #ifdef IPSTEALTH
153fcf59617SAndrey V. Elsukov 	if (V_ip6stealth == 0)
154686cdd19SJun-ichiro itojun Hagino #endif
155fcf59617SAndrey V. Elsukov 		ip6->ip6_hlim -= IPV6_HLIMDEC;
156fcf59617SAndrey V. Elsukov 
157fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
158fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv6)) {
159fcf59617SAndrey V. Elsukov 		if ((error = IPSEC_FORWARD(ipv6, m)) != 0) {
160fcf59617SAndrey V. Elsukov 			/* mbuf consumed by IPsec */
161fcf59617SAndrey V. Elsukov 			m_freem(mcopy);
162fcf59617SAndrey V. Elsukov 			if (error != EINPROGRESS)
163fcf59617SAndrey V. Elsukov 				IP6STAT_INC(ip6s_cantforward);
16482cd038dSYoshinobu Inoue 			return;
16582cd038dSYoshinobu Inoue 		}
166fcf59617SAndrey V. Elsukov 		/* No IPsec processing required */
167686cdd19SJun-ichiro itojun Hagino 	}
1680f9ade71SHajimu UMEMOTO #endif
16953a4886dSAlexander V. Chernikov 	/*
17053a4886dSAlexander V. Chernikov 	 * ip6_forward() operates with IPv6 addresses with deembedded scope.
17153a4886dSAlexander V. Chernikov 	 *
17253a4886dSAlexander V. Chernikov 	 * There are 3 sources of IPv6 destination address:
17353a4886dSAlexander V. Chernikov 	 *
17453a4886dSAlexander V. Chernikov 	 * 1) ip6_input(), where ip6_dst contains deembedded address.
17553a4886dSAlexander V. Chernikov 	 *   In order to deal with forwarding of link-local packets,
17653a4886dSAlexander V. Chernikov 	 *   calculate the scope based on input interface (RFC 4007, clause 9).
17753a4886dSAlexander V. Chernikov 	 * 2) packet filters changing ip6_dst directly. It would embed scope
17853a4886dSAlexander V. Chernikov 	 *   for LL addresses, so in6_localip() performs properly.
17953a4886dSAlexander V. Chernikov 	 * 3) packet filters attaching PACKET_TAG_IPFORWARD would embed
18053a4886dSAlexander V. Chernikov 	 *   scope for the nexthop.
18153a4886dSAlexander V. Chernikov 	 */
18253a4886dSAlexander V. Chernikov 	bzero(&dst, sizeof(struct sockaddr_in6));
18353a4886dSAlexander V. Chernikov 	dst.sin6_family = AF_INET6;
18453a4886dSAlexander V. Chernikov 	dst.sin6_addr = ip6->ip6_dst;
18553a4886dSAlexander V. Chernikov 	dst.sin6_scope_id = in6_get_unicast_scopeid(&ip6->ip6_dst, m->m_pkthdr.rcvif);
1868a006adbSBjoern A. Zeeb again:
18753a4886dSAlexander V. Chernikov 	nh = fib6_lookup(M_GETFIB(m), &dst.sin6_addr, dst.sin6_scope_id,
18853a4886dSAlexander V. Chernikov 	    NHR_REF, m->m_pkthdr.flowid);
18953a4886dSAlexander V. Chernikov 	if (nh == NULL) {
1909cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_noroute);
19133841545SHajimu UMEMOTO 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
192686cdd19SJun-ichiro itojun Hagino 		if (mcopy) {
193686cdd19SJun-ichiro itojun Hagino 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
19482cd038dSYoshinobu Inoue 			ICMP6_DST_UNREACH_NOROUTE, 0);
195686cdd19SJun-ichiro itojun Hagino 		}
19609f8c3ffSBjoern A. Zeeb 		goto bad;
19782cd038dSYoshinobu Inoue 	}
198686cdd19SJun-ichiro itojun Hagino 
19930dd227cSAlexander V. Chernikov 	if (nh->nh_flags & (NHF_BLACKHOLE | NHF_REJECT)) {
20030dd227cSAlexander V. Chernikov 		IP6STAT_INC(ip6s_cantforward);
201*b50e1465SAlexander V. Chernikov 		if (mcopy != NULL) {
202*b50e1465SAlexander V. Chernikov 			if (nh->nh_flags & NHF_REJECT) {
20330dd227cSAlexander V. Chernikov 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
20430dd227cSAlexander V. Chernikov 				    ICMP6_DST_UNREACH_REJECT, 0);
205*b50e1465SAlexander V. Chernikov 			} else
206*b50e1465SAlexander V. Chernikov 				m_freem(mcopy);
20730dd227cSAlexander V. Chernikov 		}
20830dd227cSAlexander V. Chernikov 		goto bad;
20930dd227cSAlexander V. Chernikov 	}
21030dd227cSAlexander V. Chernikov 
211686cdd19SJun-ichiro itojun Hagino 	/*
212a1f7e5f8SHajimu UMEMOTO 	 * Source scope check: if a packet can't be delivered to its
213a1f7e5f8SHajimu UMEMOTO 	 * destination for the reason that the destination is beyond the scope
214a1f7e5f8SHajimu UMEMOTO 	 * of the source address, discard the packet and return an icmp6
215a1f7e5f8SHajimu UMEMOTO 	 * destination unreachable error with Code 2 (beyond scope of source
21653a4886dSAlexander V. Chernikov 	 * address).
217a1f7e5f8SHajimu UMEMOTO 	 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1]
218686cdd19SJun-ichiro itojun Hagino 	 */
21953a4886dSAlexander V. Chernikov 	outzone = in6_get_unicast_scopeid(&ip6->ip6_src, nh->nh_ifp);
22053a4886dSAlexander V. Chernikov 	inzone = in6_get_unicast_scopeid(&ip6->ip6_src, m->m_pkthdr.rcvif);
22145b4fb04SAndrey V. Elsukov 	if (inzone != outzone) {
2229cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_cantforward);
2239cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badscope);
22453a4886dSAlexander V. Chernikov 		in6_ifstat_inc(nh->nh_ifp, ifs6_in_discard);
225686cdd19SJun-ichiro itojun Hagino 
22635b6e52cSPawel Biernacki 		if (V_ip6_log_cannot_forward && ip6_log_ratelimit()) {
227686cdd19SJun-ichiro itojun Hagino 			log(LOG_DEBUG,
228686cdd19SJun-ichiro itojun Hagino 			    "cannot forward "
229686cdd19SJun-ichiro itojun Hagino 			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
2301d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
2311d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
232686cdd19SJun-ichiro itojun Hagino 			    ip6->ip6_nxt,
23353a4886dSAlexander V. Chernikov 			    if_name(m->m_pkthdr.rcvif), if_name(nh->nh_ifp));
234686cdd19SJun-ichiro itojun Hagino 		}
235686cdd19SJun-ichiro itojun Hagino 		if (mcopy)
236686cdd19SJun-ichiro itojun Hagino 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
237686cdd19SJun-ichiro itojun Hagino 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
23809f8c3ffSBjoern A. Zeeb 		goto bad;
239686cdd19SJun-ichiro itojun Hagino 	}
240686cdd19SJun-ichiro itojun Hagino 
241a1f7e5f8SHajimu UMEMOTO 	/*
242a1f7e5f8SHajimu UMEMOTO 	 * Destination scope check: if a packet is going to break the scope
243a1f7e5f8SHajimu UMEMOTO 	 * zone of packet's destination address, discard it.  This case should
244a1f7e5f8SHajimu UMEMOTO 	 * usually be prevented by appropriately-configured routing table, but
245a1f7e5f8SHajimu UMEMOTO 	 * we need an explicit check because we may mistakenly forward the
246a1f7e5f8SHajimu UMEMOTO 	 * packet to a different zone by (e.g.) a default route.
247a1f7e5f8SHajimu UMEMOTO 	 */
24853a4886dSAlexander V. Chernikov 	inzone = in6_get_unicast_scopeid(&ip6->ip6_dst, m->m_pkthdr.rcvif);
24953a4886dSAlexander V. Chernikov 	outzone = in6_get_unicast_scopeid(&ip6->ip6_dst, nh->nh_ifp);
25053a4886dSAlexander V. Chernikov 
25153a4886dSAlexander V. Chernikov 	if (inzone != outzone) {
2529cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_cantforward);
2539cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badscope);
25409f8c3ffSBjoern A. Zeeb 		goto bad;
255a1f7e5f8SHajimu UMEMOTO 	}
256a1f7e5f8SHajimu UMEMOTO 
25753a4886dSAlexander V. Chernikov 	if (nh->nh_flags & NHF_GATEWAY) {
25853a4886dSAlexander V. Chernikov 		/* Store gateway address in deembedded form */
25953a4886dSAlexander V. Chernikov 		dst.sin6_addr = nh->gw6_sa.sin6_addr;
26053a4886dSAlexander V. Chernikov 		dst.sin6_scope_id = ntohs(in6_getscope(&dst.sin6_addr));
26153a4886dSAlexander V. Chernikov 		in6_clearscope(&dst.sin6_addr);
26253a4886dSAlexander V. Chernikov 	}
26382cd038dSYoshinobu Inoue 
26482cd038dSYoshinobu Inoue 	/*
26582cd038dSYoshinobu Inoue 	 * If we are to forward the packet using the same interface
26682cd038dSYoshinobu Inoue 	 * as one we got the packet from, perhaps we should send a redirect
26782cd038dSYoshinobu Inoue 	 * to sender to shortcut a hop.
26882cd038dSYoshinobu Inoue 	 * Only send redirect if source is sending directly to us,
26982cd038dSYoshinobu Inoue 	 * and if packet was not source routed (or has any options).
27082cd038dSYoshinobu Inoue 	 * Also, don't send redirect if forwarding using a route
27182cd038dSYoshinobu Inoue 	 * modified by a redirect.
27282cd038dSYoshinobu Inoue 	 */
27353a4886dSAlexander V. Chernikov 	if (V_ip6_sendredirects && nh->nh_ifp == m->m_pkthdr.rcvif && !srcrt &&
2741e9b8db9SKyle Evans 	    (nh->nh_flags & NHF_REDIRECT) == 0)
27582cd038dSYoshinobu Inoue 		type = ND_REDIRECT;
27682cd038dSYoshinobu Inoue 
27782cd038dSYoshinobu Inoue 	/*
278686cdd19SJun-ichiro itojun Hagino 	 * Fake scoped addresses. Note that even link-local source or
279686cdd19SJun-ichiro itojun Hagino 	 * destinaion can appear, if the originating node just sends the
280686cdd19SJun-ichiro itojun Hagino 	 * packet to us (without address resolution for the destination).
281686cdd19SJun-ichiro itojun Hagino 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
28233841545SHajimu UMEMOTO 	 * link identifiers, we can do this stuff after making a copy for
28333841545SHajimu UMEMOTO 	 * returning an error.
284686cdd19SJun-ichiro itojun Hagino 	 */
28553a4886dSAlexander V. Chernikov 	if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) {
286686cdd19SJun-ichiro itojun Hagino 		/*
287686cdd19SJun-ichiro itojun Hagino 		 * See corresponding comments in ip6_output.
288686cdd19SJun-ichiro itojun Hagino 		 * XXX: but is it possible that ip6_forward() sends a packet
289686cdd19SJun-ichiro itojun Hagino 		 *      to a loopback interface? I don't think so, and thus
290686cdd19SJun-ichiro itojun Hagino 		 *      I bark here. (jinmei@kame.net)
291686cdd19SJun-ichiro itojun Hagino 		 * XXX: it is common to route invalid packets to loopback.
292686cdd19SJun-ichiro itojun Hagino 		 *	also, the codepath will be visited on use of ::1 in
293686cdd19SJun-ichiro itojun Hagino 		 *	rthdr. (itojun)
294686cdd19SJun-ichiro itojun Hagino 		 */
295686cdd19SJun-ichiro itojun Hagino #if 1
296686cdd19SJun-ichiro itojun Hagino 		if (0)
297686cdd19SJun-ichiro itojun Hagino #else
298686cdd19SJun-ichiro itojun Hagino 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
299686cdd19SJun-ichiro itojun Hagino #endif
300686cdd19SJun-ichiro itojun Hagino 		{
301686cdd19SJun-ichiro itojun Hagino 			printf("ip6_forward: outgoing interface is loopback. "
302686cdd19SJun-ichiro itojun Hagino 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
3031d54aa3bSBjoern A. Zeeb 			       ip6_sprintf(ip6bufs, &ip6->ip6_src),
3041d54aa3bSBjoern A. Zeeb 			       ip6_sprintf(ip6bufd, &ip6->ip6_dst),
305686cdd19SJun-ichiro itojun Hagino 			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
30653a4886dSAlexander V. Chernikov 			       if_name(nh->nh_ifp));
307686cdd19SJun-ichiro itojun Hagino 		}
308686cdd19SJun-ichiro itojun Hagino 
30933841545SHajimu UMEMOTO 		/* we can just use rcvif in forwarding. */
31033841545SHajimu UMEMOTO 		origifp = m->m_pkthdr.rcvif;
311686cdd19SJun-ichiro itojun Hagino 	}
312686cdd19SJun-ichiro itojun Hagino 	else
31353a4886dSAlexander V. Chernikov 		origifp = nh->nh_ifp;
31433841545SHajimu UMEMOTO 	/*
31533841545SHajimu UMEMOTO 	 * clear embedded scope identifiers if necessary.
31633841545SHajimu UMEMOTO 	 * in6_clearscope will touch the addresses only when necessary.
31733841545SHajimu UMEMOTO 	 */
31833841545SHajimu UMEMOTO 	in6_clearscope(&ip6->ip6_src);
31933841545SHajimu UMEMOTO 	in6_clearscope(&ip6->ip6_dst);
320686cdd19SJun-ichiro itojun Hagino 
321c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
322b252313fSGleb Smirnoff 	if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
323c21fd232SAndre Oppermann 		goto pass;
324c21fd232SAndre Oppermann 
3258a006adbSBjoern A. Zeeb 	odst = ip6->ip6_dst;
326effaab88SKristof Provost 	/* Run through list of hooks for forwarded packets. */
327b52b61c0SKristof Provost 	if (pfil_mbuf_fwd(V_inet6_pfil_head, &m, nh->nh_ifp,
32814c9a2dbSMateusz Guzik 	    NULL) != PFIL_PASS)
329b252313fSGleb Smirnoff 		goto freecopy;
3303a4d67f7SDarren Reed 	ip6 = mtod(m, struct ip6_hdr *);
3313a4d67f7SDarren Reed 
3328a006adbSBjoern A. Zeeb 	/* See if destination IP address was changed by packet filter. */
3338a006adbSBjoern A. Zeeb 	if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
3348a006adbSBjoern A. Zeeb 		m->m_flags |= M_SKIP_FIREWALL;
3358a006adbSBjoern A. Zeeb 		/* If destination is now ourself drop to ip6_input(). */
33631e2e88cSKristof Provost 		if (in6_localip(&ip6->ip6_dst))
3378a006adbSBjoern A. Zeeb 			m->m_flags |= M_FASTFWD_OURS;
338af8451a5SAlexander V. Chernikov 		else {
33953a4886dSAlexander V. Chernikov 			NH_FREE(nh);
34053a4886dSAlexander V. Chernikov 
34153a4886dSAlexander V. Chernikov 			/* Update address and scopeid. Assume scope is embedded */
34253a4886dSAlexander V. Chernikov 			dst.sin6_scope_id = ntohs(in6_getscope(&ip6->ip6_dst));
34353a4886dSAlexander V. Chernikov 			dst.sin6_addr = ip6->ip6_dst;
34453a4886dSAlexander V. Chernikov 			in6_clearscope(&dst.sin6_addr);
3458a006adbSBjoern A. Zeeb 			goto again;	/* Redo the routing table lookup. */
3468a006adbSBjoern A. Zeeb 		}
347af8451a5SAlexander V. Chernikov 	}
3488a006adbSBjoern A. Zeeb 
3498a006adbSBjoern A. Zeeb 	/* See if local, if yes, send it to netisr. */
3508a006adbSBjoern A. Zeeb 	if (m->m_flags & M_FASTFWD_OURS) {
3518a006adbSBjoern A. Zeeb 		if (m->m_pkthdr.rcvif == NULL)
3528a006adbSBjoern A. Zeeb 			m->m_pkthdr.rcvif = V_loif;
353356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3548a006adbSBjoern A. Zeeb 			m->m_pkthdr.csum_flags |=
355356ab07eSBjoern A. Zeeb 			    CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
3568a006adbSBjoern A. Zeeb 			m->m_pkthdr.csum_data = 0xffff;
3578a006adbSBjoern A. Zeeb 		}
35895033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
359a6cff10fSMichael Tuexen 		if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
3608a006adbSBjoern A. Zeeb 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
3618a006adbSBjoern A. Zeeb #endif
3628a006adbSBjoern A. Zeeb 		error = netisr_queue(NETISR_IPV6, m);
3638a006adbSBjoern A. Zeeb 		goto out;
3648a006adbSBjoern A. Zeeb 	}
3658a006adbSBjoern A. Zeeb 	/* Or forward to some other address? */
366ffdbf9daSAndrey V. Elsukov 	if ((m->m_flags & M_IP6_NEXTHOP) &&
367ffdbf9daSAndrey V. Elsukov 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
36853a4886dSAlexander V. Chernikov 		struct sockaddr_in6 *gw6 = (struct sockaddr_in6 *)(fwd_tag + 1);
36953a4886dSAlexander V. Chernikov 
37053a4886dSAlexander V. Chernikov 		/* Update address and scopeid. Assume scope is embedded */
37153a4886dSAlexander V. Chernikov 		dst.sin6_scope_id = ntohs(in6_getscope(&gw6->sin6_addr));
37253a4886dSAlexander V. Chernikov 		dst.sin6_addr = gw6->sin6_addr;
37353a4886dSAlexander V. Chernikov 		in6_clearscope(&dst.sin6_addr);
37453a4886dSAlexander V. Chernikov 
3758a006adbSBjoern A. Zeeb 		m->m_flags |= M_SKIP_FIREWALL;
376ffdbf9daSAndrey V. Elsukov 		m->m_flags &= ~M_IP6_NEXTHOP;
3778a006adbSBjoern A. Zeeb 		m_tag_delete(m, fwd_tag);
37853a4886dSAlexander V. Chernikov 		NH_FREE(nh);
37953a4886dSAlexander V. Chernikov 		goto again;
3808a006adbSBjoern A. Zeeb 	}
3818a006adbSBjoern A. Zeeb 
382c21fd232SAndre Oppermann pass:
38353deb05cSKristof Provost 	/* See if the size was changed by the packet filter. */
38453a4886dSAlexander V. Chernikov 	/* TODO: change to nh->nh_mtu */
38553a4886dSAlexander V. Chernikov 	if (m->m_pkthdr.len > IN6_LINKMTU(nh->nh_ifp)) {
38653a4886dSAlexander V. Chernikov 		in6_ifstat_inc(nh->nh_ifp, ifs6_in_toobig);
387fcf59617SAndrey V. Elsukov 		if (mcopy)
388fcf59617SAndrey V. Elsukov 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0,
38953a4886dSAlexander V. Chernikov 			    IN6_LINKMTU(nh->nh_ifp));
39053deb05cSKristof Provost 		goto bad;
39153deb05cSKristof Provost 	}
39253deb05cSKristof Provost 
39353a4886dSAlexander V. Chernikov 	/* Currently LLE layer stores embedded IPv6 addresses */
39453a4886dSAlexander V. Chernikov 	if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6_addr)) {
39553a4886dSAlexander V. Chernikov 		in6_set_unicast_scopeid(&dst.sin6_addr, dst.sin6_scope_id);
39653a4886dSAlexander V. Chernikov 		dst.sin6_scope_id = 0;
39753a4886dSAlexander V. Chernikov 	}
39853a4886dSAlexander V. Chernikov 	error = nd6_output_ifp(nh->nh_ifp, origifp, m, &dst, NULL);
39982cd038dSYoshinobu Inoue 	if (error) {
40053a4886dSAlexander V. Chernikov 		in6_ifstat_inc(nh->nh_ifp, ifs6_out_discard);
4019cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_cantforward);
40282cd038dSYoshinobu Inoue 	} else {
4039cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_forward);
40453a4886dSAlexander V. Chernikov 		in6_ifstat_inc(nh->nh_ifp, ifs6_out_forward);
40582cd038dSYoshinobu Inoue 		if (type)
4069cb8d207SAndrey V. Elsukov 			IP6STAT_INC(ip6s_redirectsent);
40782cd038dSYoshinobu Inoue 		else {
40882cd038dSYoshinobu Inoue 			if (mcopy)
40982cd038dSYoshinobu Inoue 				goto freecopy;
41082cd038dSYoshinobu Inoue 		}
41182cd038dSYoshinobu Inoue 	}
4127efe5d92SHajimu UMEMOTO 
41382cd038dSYoshinobu Inoue 	if (mcopy == NULL)
41409f8c3ffSBjoern A. Zeeb 		goto out;
41582cd038dSYoshinobu Inoue 	switch (error) {
41682cd038dSYoshinobu Inoue 	case 0:
41782cd038dSYoshinobu Inoue 		if (type == ND_REDIRECT) {
41853a4886dSAlexander V. Chernikov 			icmp6_redirect_output(mcopy, nh);
41909f8c3ffSBjoern A. Zeeb 			goto out;
42082cd038dSYoshinobu Inoue 		}
42182cd038dSYoshinobu Inoue 		goto freecopy;
42282cd038dSYoshinobu Inoue 
42382cd038dSYoshinobu Inoue 	case EMSGSIZE:
42482cd038dSYoshinobu Inoue 		/* xxx MTU is constant in PPP? */
42582cd038dSYoshinobu Inoue 		goto freecopy;
42682cd038dSYoshinobu Inoue 
42782cd038dSYoshinobu Inoue 	case ENOBUFS:
42882cd038dSYoshinobu Inoue 		/* Tell source to slow down like source quench in IP? */
42982cd038dSYoshinobu Inoue 		goto freecopy;
43082cd038dSYoshinobu Inoue 
43182cd038dSYoshinobu Inoue 	case ENETUNREACH:	/* shouldn't happen, checked above */
43282cd038dSYoshinobu Inoue 	case EHOSTUNREACH:
43382cd038dSYoshinobu Inoue 	case ENETDOWN:
43482cd038dSYoshinobu Inoue 	case EHOSTDOWN:
43582cd038dSYoshinobu Inoue 	default:
43682cd038dSYoshinobu Inoue 		type = ICMP6_DST_UNREACH;
43782cd038dSYoshinobu Inoue 		code = ICMP6_DST_UNREACH_ADDR;
43882cd038dSYoshinobu Inoue 		break;
43982cd038dSYoshinobu Inoue 	}
44082cd038dSYoshinobu Inoue 	icmp6_error(mcopy, type, code, 0);
44109f8c3ffSBjoern A. Zeeb 	goto out;
44282cd038dSYoshinobu Inoue 
44382cd038dSYoshinobu Inoue  freecopy:
44482cd038dSYoshinobu Inoue 	m_freem(mcopy);
44509f8c3ffSBjoern A. Zeeb 	goto out;
44609f8c3ffSBjoern A. Zeeb bad:
44709f8c3ffSBjoern A. Zeeb 	m_freem(m);
44809f8c3ffSBjoern A. Zeeb out:
44953a4886dSAlexander V. Chernikov 	if (nh != NULL)
45053a4886dSAlexander V. Chernikov 		NH_FREE(nh);
45182cd038dSYoshinobu Inoue }
452