xref: /freebsd/sys/netinet6/ip6_forward.c (revision 1e9b8db9b254b98ae065889f8c33b0dcc18138a1)
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>
578a006adbSBjoern A. Zeeb #include <net/netisr.h>
5882cd038dSYoshinobu Inoue #include <net/route.h>
5953a4886dSAlexander V. Chernikov #include <net/route/nhop.h>
603a4d67f7SDarren Reed #include <net/pfil.h>
6182cd038dSYoshinobu Inoue 
6282cd038dSYoshinobu Inoue #include <netinet/in.h>
6382cd038dSYoshinobu Inoue #include <netinet/in_var.h>
6433841545SHajimu UMEMOTO #include <netinet/in_systm.h>
6533841545SHajimu UMEMOTO #include <netinet/ip.h>
66686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_var.h>
6733841545SHajimu UMEMOTO #include <netinet6/in6_var.h>
68686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
6953a4886dSAlexander V. Chernikov #include <netinet6/in6_fib.h>
7082cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
71a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
72686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
7382cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
7482cd038dSYoshinobu Inoue 
7533841545SHajimu UMEMOTO #include <netinet/in_pcb.h>
7633841545SHajimu UMEMOTO 
77fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
78b9234fafSSam Leffler 
7982cd038dSYoshinobu Inoue /*
8082cd038dSYoshinobu Inoue  * Forward a packet.  If some error occurs return the sender
8182cd038dSYoshinobu Inoue  * an icmp packet.  Note we can't always generate a meaningful
8282cd038dSYoshinobu Inoue  * icmp message because icmp doesn't have a large enough repertoire
8382cd038dSYoshinobu Inoue  * of codes and types.
8482cd038dSYoshinobu Inoue  *
8582cd038dSYoshinobu Inoue  * If not forwarding, just drop the packet.  This could be confusing
8682cd038dSYoshinobu Inoue  * if ipforwarding was zero but some routing protocol was advancing
8782cd038dSYoshinobu Inoue  * us as a gateway to somewhere.  However, we must let the routing
8882cd038dSYoshinobu Inoue  * protocol deal with that.
8982cd038dSYoshinobu Inoue  *
9082cd038dSYoshinobu Inoue  */
9182cd038dSYoshinobu Inoue void
921272577eSXin LI ip6_forward(struct mbuf *m, int srcrt)
9382cd038dSYoshinobu Inoue {
9482cd038dSYoshinobu Inoue 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
9553a4886dSAlexander V. Chernikov 	struct sockaddr_in6 dst;
9653a4886dSAlexander V. Chernikov 	struct nhop_object *nh = NULL;
9782cd038dSYoshinobu Inoue 	int error, type = 0, code = 0;
98686cdd19SJun-ichiro itojun Hagino 	struct mbuf *mcopy = NULL;
99686cdd19SJun-ichiro itojun Hagino 	struct ifnet *origifp;	/* maybe unnecessary */
100a1f7e5f8SHajimu UMEMOTO 	u_int32_t inzone, outzone;
10153a4886dSAlexander V. Chernikov 	struct in6_addr odst;
1028a006adbSBjoern A. Zeeb 	struct m_tag *fwd_tag;
1031d54aa3bSBjoern A. Zeeb 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
10482cd038dSYoshinobu Inoue 
10533841545SHajimu UMEMOTO 	/*
10633841545SHajimu UMEMOTO 	 * Do not forward packets to multicast destination (should be handled
10733841545SHajimu UMEMOTO 	 * by ip6_mforward().
10833841545SHajimu UMEMOTO 	 * Do not forward packets with unspecified source.  It was discussed
1097efe5d92SHajimu UMEMOTO 	 * in July 2000, on the ipngwg mailing list.
11033841545SHajimu UMEMOTO 	 */
111686cdd19SJun-ichiro itojun Hagino 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
11233841545SHajimu UMEMOTO 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
11333841545SHajimu UMEMOTO 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1149cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_cantforward);
11582cd038dSYoshinobu Inoue 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
1167d26db17SHiroki Sato 		if (V_ip6_log_time + V_ip6_log_interval < time_uptime) {
1177d26db17SHiroki Sato 			V_ip6_log_time = time_uptime;
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 
199686cdd19SJun-ichiro itojun Hagino 	/*
200a1f7e5f8SHajimu UMEMOTO 	 * Source scope check: if a packet can't be delivered to its
201a1f7e5f8SHajimu UMEMOTO 	 * destination for the reason that the destination is beyond the scope
202a1f7e5f8SHajimu UMEMOTO 	 * of the source address, discard the packet and return an icmp6
203a1f7e5f8SHajimu UMEMOTO 	 * destination unreachable error with Code 2 (beyond scope of source
20453a4886dSAlexander V. Chernikov 	 * address).
205a1f7e5f8SHajimu UMEMOTO 	 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1]
206686cdd19SJun-ichiro itojun Hagino 	 */
20753a4886dSAlexander V. Chernikov 	outzone = in6_get_unicast_scopeid(&ip6->ip6_src, nh->nh_ifp);
20853a4886dSAlexander V. Chernikov 	inzone = in6_get_unicast_scopeid(&ip6->ip6_src, m->m_pkthdr.rcvif);
20945b4fb04SAndrey V. Elsukov 	if (inzone != outzone) {
2109cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_cantforward);
2119cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badscope);
21253a4886dSAlexander V. Chernikov 		in6_ifstat_inc(nh->nh_ifp, ifs6_in_discard);
213686cdd19SJun-ichiro itojun Hagino 
2147d26db17SHiroki Sato 		if (V_ip6_log_time + V_ip6_log_interval < time_uptime) {
2157d26db17SHiroki Sato 			V_ip6_log_time = time_uptime;
216686cdd19SJun-ichiro itojun Hagino 			log(LOG_DEBUG,
217686cdd19SJun-ichiro itojun Hagino 			    "cannot forward "
218686cdd19SJun-ichiro itojun Hagino 			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
2191d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
2201d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
221686cdd19SJun-ichiro itojun Hagino 			    ip6->ip6_nxt,
22253a4886dSAlexander V. Chernikov 			    if_name(m->m_pkthdr.rcvif), if_name(nh->nh_ifp));
223686cdd19SJun-ichiro itojun Hagino 		}
224686cdd19SJun-ichiro itojun Hagino 		if (mcopy)
225686cdd19SJun-ichiro itojun Hagino 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
226686cdd19SJun-ichiro itojun Hagino 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
22709f8c3ffSBjoern A. Zeeb 		goto bad;
228686cdd19SJun-ichiro itojun Hagino 	}
229686cdd19SJun-ichiro itojun Hagino 
230a1f7e5f8SHajimu UMEMOTO 	/*
231a1f7e5f8SHajimu UMEMOTO 	 * Destination scope check: if a packet is going to break the scope
232a1f7e5f8SHajimu UMEMOTO 	 * zone of packet's destination address, discard it.  This case should
233a1f7e5f8SHajimu UMEMOTO 	 * usually be prevented by appropriately-configured routing table, but
234a1f7e5f8SHajimu UMEMOTO 	 * we need an explicit check because we may mistakenly forward the
235a1f7e5f8SHajimu UMEMOTO 	 * packet to a different zone by (e.g.) a default route.
236a1f7e5f8SHajimu UMEMOTO 	 */
23753a4886dSAlexander V. Chernikov 	inzone = in6_get_unicast_scopeid(&ip6->ip6_dst, m->m_pkthdr.rcvif);
23853a4886dSAlexander V. Chernikov 	outzone = in6_get_unicast_scopeid(&ip6->ip6_dst, nh->nh_ifp);
23953a4886dSAlexander V. Chernikov 
24053a4886dSAlexander V. Chernikov 	if (inzone != outzone) {
2419cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_cantforward);
2429cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badscope);
24309f8c3ffSBjoern A. Zeeb 		goto bad;
244a1f7e5f8SHajimu UMEMOTO 	}
245a1f7e5f8SHajimu UMEMOTO 
24653a4886dSAlexander V. Chernikov 	if (nh->nh_flags & NHF_GATEWAY) {
24753a4886dSAlexander V. Chernikov 		/* Store gateway address in deembedded form */
24853a4886dSAlexander V. Chernikov 		dst.sin6_addr = nh->gw6_sa.sin6_addr;
24953a4886dSAlexander V. Chernikov 		dst.sin6_scope_id = ntohs(in6_getscope(&dst.sin6_addr));
25053a4886dSAlexander V. Chernikov 		in6_clearscope(&dst.sin6_addr);
25153a4886dSAlexander V. Chernikov 	}
25282cd038dSYoshinobu Inoue 
25382cd038dSYoshinobu Inoue 	/*
25482cd038dSYoshinobu Inoue 	 * If we are to forward the packet using the same interface
25582cd038dSYoshinobu Inoue 	 * as one we got the packet from, perhaps we should send a redirect
25682cd038dSYoshinobu Inoue 	 * to sender to shortcut a hop.
25782cd038dSYoshinobu Inoue 	 * Only send redirect if source is sending directly to us,
25882cd038dSYoshinobu Inoue 	 * and if packet was not source routed (or has any options).
25982cd038dSYoshinobu Inoue 	 * Also, don't send redirect if forwarding using a route
26082cd038dSYoshinobu Inoue 	 * modified by a redirect.
26182cd038dSYoshinobu Inoue 	 */
26253a4886dSAlexander V. Chernikov 	if (V_ip6_sendredirects && nh->nh_ifp == m->m_pkthdr.rcvif && !srcrt &&
263*1e9b8db9SKyle Evans 	    (nh->nh_flags & NHF_REDIRECT) == 0)
26482cd038dSYoshinobu Inoue 		type = ND_REDIRECT;
26582cd038dSYoshinobu Inoue 
26682cd038dSYoshinobu Inoue 	/*
267686cdd19SJun-ichiro itojun Hagino 	 * Fake scoped addresses. Note that even link-local source or
268686cdd19SJun-ichiro itojun Hagino 	 * destinaion can appear, if the originating node just sends the
269686cdd19SJun-ichiro itojun Hagino 	 * packet to us (without address resolution for the destination).
270686cdd19SJun-ichiro itojun Hagino 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
27133841545SHajimu UMEMOTO 	 * link identifiers, we can do this stuff after making a copy for
27233841545SHajimu UMEMOTO 	 * returning an error.
273686cdd19SJun-ichiro itojun Hagino 	 */
27453a4886dSAlexander V. Chernikov 	if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) {
275686cdd19SJun-ichiro itojun Hagino 		/*
276686cdd19SJun-ichiro itojun Hagino 		 * See corresponding comments in ip6_output.
277686cdd19SJun-ichiro itojun Hagino 		 * XXX: but is it possible that ip6_forward() sends a packet
278686cdd19SJun-ichiro itojun Hagino 		 *      to a loopback interface? I don't think so, and thus
279686cdd19SJun-ichiro itojun Hagino 		 *      I bark here. (jinmei@kame.net)
280686cdd19SJun-ichiro itojun Hagino 		 * XXX: it is common to route invalid packets to loopback.
281686cdd19SJun-ichiro itojun Hagino 		 *	also, the codepath will be visited on use of ::1 in
282686cdd19SJun-ichiro itojun Hagino 		 *	rthdr. (itojun)
283686cdd19SJun-ichiro itojun Hagino 		 */
284686cdd19SJun-ichiro itojun Hagino #if 1
285686cdd19SJun-ichiro itojun Hagino 		if (0)
286686cdd19SJun-ichiro itojun Hagino #else
287686cdd19SJun-ichiro itojun Hagino 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
288686cdd19SJun-ichiro itojun Hagino #endif
289686cdd19SJun-ichiro itojun Hagino 		{
290686cdd19SJun-ichiro itojun Hagino 			printf("ip6_forward: outgoing interface is loopback. "
291686cdd19SJun-ichiro itojun Hagino 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
2921d54aa3bSBjoern A. Zeeb 			       ip6_sprintf(ip6bufs, &ip6->ip6_src),
2931d54aa3bSBjoern A. Zeeb 			       ip6_sprintf(ip6bufd, &ip6->ip6_dst),
294686cdd19SJun-ichiro itojun Hagino 			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
29553a4886dSAlexander V. Chernikov 			       if_name(nh->nh_ifp));
296686cdd19SJun-ichiro itojun Hagino 		}
297686cdd19SJun-ichiro itojun Hagino 
29833841545SHajimu UMEMOTO 		/* we can just use rcvif in forwarding. */
29933841545SHajimu UMEMOTO 		origifp = m->m_pkthdr.rcvif;
300686cdd19SJun-ichiro itojun Hagino 	}
301686cdd19SJun-ichiro itojun Hagino 	else
30253a4886dSAlexander V. Chernikov 		origifp = nh->nh_ifp;
30333841545SHajimu UMEMOTO 	/*
30433841545SHajimu UMEMOTO 	 * clear embedded scope identifiers if necessary.
30533841545SHajimu UMEMOTO 	 * in6_clearscope will touch the addresses only when necessary.
30633841545SHajimu UMEMOTO 	 */
30733841545SHajimu UMEMOTO 	in6_clearscope(&ip6->ip6_src);
30833841545SHajimu UMEMOTO 	in6_clearscope(&ip6->ip6_dst);
309686cdd19SJun-ichiro itojun Hagino 
310c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
311b252313fSGleb Smirnoff 	if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
312c21fd232SAndre Oppermann 		goto pass;
313c21fd232SAndre Oppermann 
3148a006adbSBjoern A. Zeeb 	odst = ip6->ip6_dst;
315effaab88SKristof Provost 	/* Run through list of hooks for forwarded packets. */
31653a4886dSAlexander V. Chernikov 	if (pfil_run_hooks(V_inet6_pfil_head, &m, nh->nh_ifp, PFIL_OUT |
317b252313fSGleb Smirnoff 	    PFIL_FWD, NULL) != PFIL_PASS)
318b252313fSGleb Smirnoff 		goto freecopy;
3193a4d67f7SDarren Reed 	ip6 = mtod(m, struct ip6_hdr *);
3203a4d67f7SDarren Reed 
3218a006adbSBjoern A. Zeeb 	/* See if destination IP address was changed by packet filter. */
3228a006adbSBjoern A. Zeeb 	if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
3238a006adbSBjoern A. Zeeb 		m->m_flags |= M_SKIP_FIREWALL;
3248a006adbSBjoern A. Zeeb 		/* If destination is now ourself drop to ip6_input(). */
32531e2e88cSKristof Provost 		if (in6_localip(&ip6->ip6_dst))
3268a006adbSBjoern A. Zeeb 			m->m_flags |= M_FASTFWD_OURS;
327af8451a5SAlexander V. Chernikov 		else {
32853a4886dSAlexander V. Chernikov 			NH_FREE(nh);
32953a4886dSAlexander V. Chernikov 
33053a4886dSAlexander V. Chernikov 			/* Update address and scopeid. Assume scope is embedded */
33153a4886dSAlexander V. Chernikov 			dst.sin6_scope_id = ntohs(in6_getscope(&ip6->ip6_dst));
33253a4886dSAlexander V. Chernikov 			dst.sin6_addr = ip6->ip6_dst;
33353a4886dSAlexander V. Chernikov 			in6_clearscope(&dst.sin6_addr);
3348a006adbSBjoern A. Zeeb 			goto again;	/* Redo the routing table lookup. */
3358a006adbSBjoern A. Zeeb 		}
336af8451a5SAlexander V. Chernikov 	}
3378a006adbSBjoern A. Zeeb 
3388a006adbSBjoern A. Zeeb 	/* See if local, if yes, send it to netisr. */
3398a006adbSBjoern A. Zeeb 	if (m->m_flags & M_FASTFWD_OURS) {
3408a006adbSBjoern A. Zeeb 		if (m->m_pkthdr.rcvif == NULL)
3418a006adbSBjoern A. Zeeb 			m->m_pkthdr.rcvif = V_loif;
342356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3438a006adbSBjoern A. Zeeb 			m->m_pkthdr.csum_flags |=
344356ab07eSBjoern A. Zeeb 			    CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
3458a006adbSBjoern A. Zeeb 			m->m_pkthdr.csum_data = 0xffff;
3468a006adbSBjoern A. Zeeb 		}
34795033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
348a6cff10fSMichael Tuexen 		if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
3498a006adbSBjoern A. Zeeb 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
3508a006adbSBjoern A. Zeeb #endif
3518a006adbSBjoern A. Zeeb 		error = netisr_queue(NETISR_IPV6, m);
3528a006adbSBjoern A. Zeeb 		goto out;
3538a006adbSBjoern A. Zeeb 	}
3548a006adbSBjoern A. Zeeb 	/* Or forward to some other address? */
355ffdbf9daSAndrey V. Elsukov 	if ((m->m_flags & M_IP6_NEXTHOP) &&
356ffdbf9daSAndrey V. Elsukov 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
35753a4886dSAlexander V. Chernikov 		struct sockaddr_in6 *gw6 = (struct sockaddr_in6 *)(fwd_tag + 1);
35853a4886dSAlexander V. Chernikov 
35953a4886dSAlexander V. Chernikov 		/* Update address and scopeid. Assume scope is embedded */
36053a4886dSAlexander V. Chernikov 		dst.sin6_scope_id = ntohs(in6_getscope(&gw6->sin6_addr));
36153a4886dSAlexander V. Chernikov 		dst.sin6_addr = gw6->sin6_addr;
36253a4886dSAlexander V. Chernikov 		in6_clearscope(&dst.sin6_addr);
36353a4886dSAlexander V. Chernikov 
3648a006adbSBjoern A. Zeeb 		m->m_flags |= M_SKIP_FIREWALL;
365ffdbf9daSAndrey V. Elsukov 		m->m_flags &= ~M_IP6_NEXTHOP;
3668a006adbSBjoern A. Zeeb 		m_tag_delete(m, fwd_tag);
36753a4886dSAlexander V. Chernikov 		NH_FREE(nh);
36853a4886dSAlexander V. Chernikov 		goto again;
3698a006adbSBjoern A. Zeeb 	}
3708a006adbSBjoern A. Zeeb 
371c21fd232SAndre Oppermann pass:
37253deb05cSKristof Provost 	/* See if the size was changed by the packet filter. */
37353a4886dSAlexander V. Chernikov 	/* TODO: change to nh->nh_mtu */
37453a4886dSAlexander V. Chernikov 	if (m->m_pkthdr.len > IN6_LINKMTU(nh->nh_ifp)) {
37553a4886dSAlexander V. Chernikov 		in6_ifstat_inc(nh->nh_ifp, ifs6_in_toobig);
376fcf59617SAndrey V. Elsukov 		if (mcopy)
377fcf59617SAndrey V. Elsukov 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0,
37853a4886dSAlexander V. Chernikov 			    IN6_LINKMTU(nh->nh_ifp));
37953deb05cSKristof Provost 		goto bad;
38053deb05cSKristof Provost 	}
38153deb05cSKristof Provost 
38253a4886dSAlexander V. Chernikov 	/* Currently LLE layer stores embedded IPv6 addresses */
38353a4886dSAlexander V. Chernikov 	if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6_addr)) {
38453a4886dSAlexander V. Chernikov 		in6_set_unicast_scopeid(&dst.sin6_addr, dst.sin6_scope_id);
38553a4886dSAlexander V. Chernikov 		dst.sin6_scope_id = 0;
38653a4886dSAlexander V. Chernikov 	}
38753a4886dSAlexander V. Chernikov 	error = nd6_output_ifp(nh->nh_ifp, origifp, m, &dst, NULL);
38882cd038dSYoshinobu Inoue 	if (error) {
38953a4886dSAlexander V. Chernikov 		in6_ifstat_inc(nh->nh_ifp, ifs6_out_discard);
3909cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_cantforward);
39182cd038dSYoshinobu Inoue 	} else {
3929cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_forward);
39353a4886dSAlexander V. Chernikov 		in6_ifstat_inc(nh->nh_ifp, ifs6_out_forward);
39482cd038dSYoshinobu Inoue 		if (type)
3959cb8d207SAndrey V. Elsukov 			IP6STAT_INC(ip6s_redirectsent);
39682cd038dSYoshinobu Inoue 		else {
39782cd038dSYoshinobu Inoue 			if (mcopy)
39882cd038dSYoshinobu Inoue 				goto freecopy;
39982cd038dSYoshinobu Inoue 		}
40082cd038dSYoshinobu Inoue 	}
4017efe5d92SHajimu UMEMOTO 
40282cd038dSYoshinobu Inoue 	if (mcopy == NULL)
40309f8c3ffSBjoern A. Zeeb 		goto out;
40482cd038dSYoshinobu Inoue 	switch (error) {
40582cd038dSYoshinobu Inoue 	case 0:
40682cd038dSYoshinobu Inoue 		if (type == ND_REDIRECT) {
40753a4886dSAlexander V. Chernikov 			icmp6_redirect_output(mcopy, nh);
40809f8c3ffSBjoern A. Zeeb 			goto out;
40982cd038dSYoshinobu Inoue 		}
41082cd038dSYoshinobu Inoue 		goto freecopy;
41182cd038dSYoshinobu Inoue 
41282cd038dSYoshinobu Inoue 	case EMSGSIZE:
41382cd038dSYoshinobu Inoue 		/* xxx MTU is constant in PPP? */
41482cd038dSYoshinobu Inoue 		goto freecopy;
41582cd038dSYoshinobu Inoue 
41682cd038dSYoshinobu Inoue 	case ENOBUFS:
41782cd038dSYoshinobu Inoue 		/* Tell source to slow down like source quench in IP? */
41882cd038dSYoshinobu Inoue 		goto freecopy;
41982cd038dSYoshinobu Inoue 
42082cd038dSYoshinobu Inoue 	case ENETUNREACH:	/* shouldn't happen, checked above */
42182cd038dSYoshinobu Inoue 	case EHOSTUNREACH:
42282cd038dSYoshinobu Inoue 	case ENETDOWN:
42382cd038dSYoshinobu Inoue 	case EHOSTDOWN:
42482cd038dSYoshinobu Inoue 	default:
42582cd038dSYoshinobu Inoue 		type = ICMP6_DST_UNREACH;
42682cd038dSYoshinobu Inoue 		code = ICMP6_DST_UNREACH_ADDR;
42782cd038dSYoshinobu Inoue 		break;
42882cd038dSYoshinobu Inoue 	}
42982cd038dSYoshinobu Inoue 	icmp6_error(mcopy, type, code, 0);
43009f8c3ffSBjoern A. Zeeb 	goto out;
43182cd038dSYoshinobu Inoue 
43282cd038dSYoshinobu Inoue  freecopy:
43382cd038dSYoshinobu Inoue 	m_freem(mcopy);
43409f8c3ffSBjoern A. Zeeb 	goto out;
43509f8c3ffSBjoern A. Zeeb bad:
43609f8c3ffSBjoern A. Zeeb 	m_freem(m);
43709f8c3ffSBjoern A. Zeeb out:
43853a4886dSAlexander V. Chernikov 	if (nh != NULL)
43953a4886dSAlexander V. Chernikov 		NH_FREE(nh);
44082cd038dSYoshinobu Inoue }
441