ip6_forward.c revision 78064
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_forward.c 78064 2001-06-11 12:39:29Z ume $	*/
278064Sume/*	$KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $	*/
362587Sitojun
453541Sshin/*
553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
653541Sshin * All rights reserved.
753541Sshin *
853541Sshin * Redistribution and use in source and binary forms, with or without
953541Sshin * modification, are permitted provided that the following conditions
1053541Sshin * are met:
1153541Sshin * 1. Redistributions of source code must retain the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer.
1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1453541Sshin *    notice, this list of conditions and the following disclaimer in the
1553541Sshin *    documentation and/or other materials provided with the distribution.
1653541Sshin * 3. Neither the name of the project nor the names of its contributors
1753541Sshin *    may be used to endorse or promote products derived from this software
1853541Sshin *    without specific prior written permission.
1953541Sshin *
2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2353541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3053541Sshin * SUCH DAMAGE.
3153541Sshin */
3253541Sshin
3362587Sitojun#include "opt_ip6fw.h"
3462587Sitojun#include "opt_inet.h"
3562587Sitojun#include "opt_inet6.h"
3655009Sshin#include "opt_ipsec.h"
3753541Sshin
3853541Sshin#include <sys/param.h>
3953541Sshin#include <sys/systm.h>
4078064Sume#include <sys/malloc.h>
4153541Sshin#include <sys/mbuf.h>
4253541Sshin#include <sys/domain.h>
4353541Sshin#include <sys/protosw.h>
4453541Sshin#include <sys/socket.h>
4553541Sshin#include <sys/errno.h>
4653541Sshin#include <sys/time.h>
4778064Sume#include <sys/kernel.h>
4853541Sshin#include <sys/syslog.h>
4953541Sshin
5053541Sshin#include <net/if.h>
5153541Sshin#include <net/route.h>
5253541Sshin
5353541Sshin#include <netinet/in.h>
5453541Sshin#include <netinet/in_var.h>
5578064Sume#include <netinet/in_systm.h>
5678064Sume#include <netinet/ip.h>
5762587Sitojun#include <netinet/ip_var.h>
5878064Sume#include <netinet6/in6_var.h>
5962587Sitojun#include <netinet/ip6.h>
6053541Sshin#include <netinet6/ip6_var.h>
6162587Sitojun#include <netinet/icmp6.h>
6253541Sshin#include <netinet6/nd6.h>
6353541Sshin
6478064Sume#include <netinet/in_pcb.h>
6578064Sume
6663256Sitojun#ifdef IPSEC
6753541Sshin#include <netinet6/ipsec.h>
6878064Sume#ifdef INET6
6962601Sitojun#include <netinet6/ipsec6.h>
7078064Sume#endif
7153541Sshin#include <netkey/key.h>
7263256Sitojun#endif /* IPSEC */
7353541Sshin
7453541Sshin#include <netinet6/ip6_fw.h>
7553541Sshin
7653541Sshin#include <net/net_osdep.h>
7753541Sshin
7853541Sshinstruct	route_in6 ip6_forward_rt;
7953541Sshin
8053541Sshin/*
8153541Sshin * Forward a packet.  If some error occurs return the sender
8253541Sshin * an icmp packet.  Note we can't always generate a meaningful
8353541Sshin * icmp message because icmp doesn't have a large enough repertoire
8453541Sshin * of codes and types.
8553541Sshin *
8653541Sshin * If not forwarding, just drop the packet.  This could be confusing
8753541Sshin * if ipforwarding was zero but some routing protocol was advancing
8853541Sshin * us as a gateway to somewhere.  However, we must let the routing
8953541Sshin * protocol deal with that.
9053541Sshin *
9153541Sshin */
9253541Sshin
9353541Sshinvoid
9453541Sshinip6_forward(m, srcrt)
9553541Sshin	struct mbuf *m;
9653541Sshin	int srcrt;
9753541Sshin{
9853541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
9978064Sume	struct sockaddr_in6 *dst;
10078064Sume	struct rtentry *rt;
10153541Sshin	int error, type = 0, code = 0;
10262587Sitojun	struct mbuf *mcopy = NULL;
10362587Sitojun	struct ifnet *origifp;	/* maybe unnecessary */
10463256Sitojun#ifdef IPSEC
10553541Sshin	struct secpolicy *sp = NULL;
10653541Sshin#endif
10753541Sshin
10863256Sitojun#ifdef IPSEC
10953541Sshin	/*
11053541Sshin	 * Check AH/ESP integrity.
11153541Sshin	 */
11253541Sshin	/*
11353541Sshin	 * Don't increment ip6s_cantforward because this is the check
11453541Sshin	 * before forwarding packet actually.
11553541Sshin	 */
11653541Sshin	if (ipsec6_in_reject(m, NULL)) {
11753541Sshin		ipsec6stat.in_polvio++;
11853541Sshin		m_freem(m);
11953541Sshin		return;
12053541Sshin	}
12163256Sitojun#endif /*IPSEC*/
12253541Sshin
12378064Sume	/*
12478064Sume	 * Do not forward packets to multicast destination (should be handled
12578064Sume	 * by ip6_mforward().
12678064Sume	 * Do not forward packets with unspecified source.  It was discussed
12778064Sume	 * in July 2000, on ipngwg mailing list.
12878064Sume	 */
12962587Sitojun	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
13078064Sume	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
13178064Sume	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
13253541Sshin		ip6stat.ip6s_cantforward++;
13353541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
13453541Sshin		if (ip6_log_time + ip6_log_interval < time_second) {
13553541Sshin			ip6_log_time = time_second;
13653541Sshin			log(LOG_DEBUG,
13753541Sshin			    "cannot forward "
13853541Sshin			    "from %s to %s nxt %d received on %s\n",
13962587Sitojun			    ip6_sprintf(&ip6->ip6_src),
14062587Sitojun			    ip6_sprintf(&ip6->ip6_dst),
14153541Sshin			    ip6->ip6_nxt,
14253541Sshin			    if_name(m->m_pkthdr.rcvif));
14353541Sshin		}
14453541Sshin		m_freem(m);
14553541Sshin		return;
14653541Sshin	}
14753541Sshin
14853541Sshin	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
14953541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
15053541Sshin		icmp6_error(m, ICMP6_TIME_EXCEEDED,
15153541Sshin				ICMP6_TIME_EXCEED_TRANSIT, 0);
15253541Sshin		return;
15353541Sshin	}
15453541Sshin	ip6->ip6_hlim -= IPV6_HLIMDEC;
15553541Sshin
15662587Sitojun	/*
15762587Sitojun	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
15862587Sitojun	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
15962587Sitojun	 * we need to generate an ICMP6 message to the src.
16062587Sitojun	 * Thanks to M_EXT, in most cases copy will not occur.
16162587Sitojun	 *
16262587Sitojun	 * It is important to save it before IPsec processing as IPsec
16362587Sitojun	 * processing may modify the mbuf.
16462587Sitojun	 */
16562587Sitojun	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
16662587Sitojun
16763256Sitojun#ifdef IPSEC
16853541Sshin	/* get a security policy for this packet */
16978064Sume	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
17078064Sume	    &error);
17153541Sshin	if (sp == NULL) {
17253541Sshin		ipsec6stat.out_inval++;
17353541Sshin		ip6stat.ip6s_cantforward++;
17462587Sitojun		if (mcopy) {
17562587Sitojun#if 0
17662587Sitojun			/* XXX: what icmp ? */
17762587Sitojun#else
17862587Sitojun			m_freem(mcopy);
17962587Sitojun#endif
18062587Sitojun		}
18153541Sshin		m_freem(m);
18253541Sshin		return;
18353541Sshin	}
18453541Sshin
18553541Sshin	error = 0;
18653541Sshin
18753541Sshin	/* check policy */
18853541Sshin	switch (sp->policy) {
18953541Sshin	case IPSEC_POLICY_DISCARD:
19053541Sshin		/*
19153541Sshin		 * This packet is just discarded.
19253541Sshin		 */
19353541Sshin		ipsec6stat.out_polvio++;
19453541Sshin		ip6stat.ip6s_cantforward++;
19553541Sshin		key_freesp(sp);
19662587Sitojun		if (mcopy) {
19762587Sitojun#if 0
19862587Sitojun			/* XXX: what icmp ? */
19962587Sitojun#else
20062587Sitojun			m_freem(mcopy);
20162587Sitojun#endif
20262587Sitojun		}
20353541Sshin		m_freem(m);
20453541Sshin		return;
20553541Sshin
20653541Sshin	case IPSEC_POLICY_BYPASS:
20753541Sshin	case IPSEC_POLICY_NONE:
20853541Sshin		/* no need to do IPsec. */
20953541Sshin		key_freesp(sp);
21053541Sshin		goto skip_ipsec;
21162587Sitojun
21253541Sshin	case IPSEC_POLICY_IPSEC:
21353541Sshin		if (sp->req == NULL) {
21453541Sshin			/* XXX should be panic ? */
21553541Sshin			printf("ip6_forward: No IPsec request specified.\n");
21653541Sshin			ip6stat.ip6s_cantforward++;
21753541Sshin			key_freesp(sp);
21862587Sitojun			if (mcopy) {
21962587Sitojun#if 0
22062587Sitojun				/* XXX: what icmp ? */
22162587Sitojun#else
22262587Sitojun				m_freem(mcopy);
22362587Sitojun#endif
22462587Sitojun			}
22553541Sshin			m_freem(m);
22653541Sshin			return;
22753541Sshin		}
22853541Sshin		/* do IPsec */
22953541Sshin		break;
23053541Sshin
23153541Sshin	case IPSEC_POLICY_ENTRUST:
23253541Sshin	default:
23353541Sshin		/* should be panic ?? */
23453541Sshin		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
23553541Sshin		key_freesp(sp);
23653541Sshin		goto skip_ipsec;
23753541Sshin	}
23853541Sshin
23953541Sshin    {
24053541Sshin	struct ipsec_output_state state;
24153541Sshin
24253541Sshin	/*
24353541Sshin	 * All the extension headers will become inaccessible
24453541Sshin	 * (since they can be encrypted).
24553541Sshin	 * Don't panic, we need no more updates to extension headers
24653541Sshin	 * on inner IPv6 packet (since they are now encapsulated).
24753541Sshin	 *
24853541Sshin	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
24953541Sshin	 */
25053541Sshin	bzero(&state, sizeof(state));
25153541Sshin	state.m = m;
25253541Sshin	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
25353541Sshin	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
25453541Sshin
25553541Sshin	error = ipsec6_output_tunnel(&state, sp, 0);
25653541Sshin
25753541Sshin	m = state.m;
25853541Sshin	key_freesp(sp);
25953541Sshin
26053541Sshin	if (error) {
26153541Sshin		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
26253541Sshin		switch (error) {
26353541Sshin		case EHOSTUNREACH:
26453541Sshin		case ENETUNREACH:
26553541Sshin		case EMSGSIZE:
26653541Sshin		case ENOBUFS:
26753541Sshin		case ENOMEM:
26853541Sshin			break;
26953541Sshin		default:
27053541Sshin			printf("ip6_output (ipsec): error code %d\n", error);
27153541Sshin			/*fall through*/
27253541Sshin		case ENOENT:
27353541Sshin			/* don't show these error codes to the user */
27453541Sshin			break;
27553541Sshin		}
27653541Sshin		ip6stat.ip6s_cantforward++;
27762587Sitojun		if (mcopy) {
27862587Sitojun#if 0
27962587Sitojun			/* XXX: what icmp ? */
28062587Sitojun#else
28162587Sitojun			m_freem(mcopy);
28262587Sitojun#endif
28362587Sitojun		}
28453541Sshin		m_freem(m);
28553541Sshin		return;
28653541Sshin	}
28753541Sshin    }
28853541Sshin    skip_ipsec:
28963256Sitojun#endif /* IPSEC */
29053541Sshin
29178064Sume	dst = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
29253541Sshin	if (!srcrt) {
29353541Sshin		/*
29453541Sshin		 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
29553541Sshin		 */
29653541Sshin		if (ip6_forward_rt.ro_rt == 0 ||
29753541Sshin		    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
29853541Sshin			if (ip6_forward_rt.ro_rt) {
29953541Sshin				RTFREE(ip6_forward_rt.ro_rt);
30053541Sshin				ip6_forward_rt.ro_rt = 0;
30153541Sshin			}
30253541Sshin			/* this probably fails but give it a try again */
30353541Sshin			rtalloc_ign((struct route *)&ip6_forward_rt,
30453541Sshin				    RTF_PRCLONING);
30553541Sshin		}
30662587Sitojun
30753541Sshin		if (ip6_forward_rt.ro_rt == 0) {
30853541Sshin			ip6stat.ip6s_noroute++;
30978064Sume			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
31062587Sitojun			if (mcopy) {
31162587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
31262587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
31362587Sitojun			}
31462587Sitojun			m_freem(m);
31553541Sshin			return;
31653541Sshin		}
31753541Sshin	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
31853541Sshin		 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
31953541Sshin		if (ip6_forward_rt.ro_rt) {
32053541Sshin			RTFREE(ip6_forward_rt.ro_rt);
32153541Sshin			ip6_forward_rt.ro_rt = 0;
32253541Sshin		}
32353541Sshin		bzero(dst, sizeof(*dst));
32453541Sshin		dst->sin6_len = sizeof(struct sockaddr_in6);
32553541Sshin		dst->sin6_family = AF_INET6;
32653541Sshin		dst->sin6_addr = ip6->ip6_dst;
32753541Sshin
32853541Sshin  		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
32953541Sshin		if (ip6_forward_rt.ro_rt == 0) {
33053541Sshin			ip6stat.ip6s_noroute++;
33178064Sume			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
33262587Sitojun			if (mcopy) {
33362587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
33462587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
33562587Sitojun			}
33662587Sitojun			m_freem(m);
33753541Sshin			return;
33853541Sshin		}
33953541Sshin	}
34053541Sshin	rt = ip6_forward_rt.ro_rt;
34162587Sitojun
34262587Sitojun	/*
34362587Sitojun	 * Scope check: if a packet can't be delivered to its destination
34462587Sitojun	 * for the reason that the destination is beyond the scope of the
34562587Sitojun	 * source address, discard the packet and return an icmp6 destination
34662587Sitojun	 * unreachable error with Code 2 (beyond scope of source address).
34762587Sitojun	 * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1]
34862587Sitojun	 */
34962587Sitojun	if (in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_src) !=
35062587Sitojun	    in6_addr2scopeid(rt->rt_ifp, &ip6->ip6_src)) {
35162587Sitojun		ip6stat.ip6s_cantforward++;
35262587Sitojun		ip6stat.ip6s_badscope++;
35362587Sitojun		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
35462587Sitojun
35562587Sitojun		if (ip6_log_time + ip6_log_interval < time_second) {
35662587Sitojun			ip6_log_time = time_second;
35762587Sitojun			log(LOG_DEBUG,
35862587Sitojun			    "cannot forward "
35962587Sitojun			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
36062587Sitojun			    ip6_sprintf(&ip6->ip6_src),
36162587Sitojun			    ip6_sprintf(&ip6->ip6_dst),
36262587Sitojun			    ip6->ip6_nxt,
36362587Sitojun			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
36462587Sitojun		}
36562587Sitojun		if (mcopy)
36662587Sitojun			icmp6_error(mcopy, ICMP6_DST_UNREACH,
36762587Sitojun				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
36862587Sitojun		m_freem(m);
36962587Sitojun		return;
37062587Sitojun	}
37162587Sitojun
37262587Sitojun	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
37353541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
37462587Sitojun		if (mcopy) {
37562587Sitojun			u_long mtu;
37663256Sitojun#ifdef IPSEC
37762587Sitojun			struct secpolicy *sp;
37862587Sitojun			int ipsecerror;
37962587Sitojun			size_t ipsechdrsiz;
38062587Sitojun#endif
38162587Sitojun
38262587Sitojun			mtu = rt->rt_ifp->if_mtu;
38363256Sitojun#ifdef IPSEC
38462587Sitojun			/*
38562587Sitojun			 * When we do IPsec tunnel ingress, we need to play
38662587Sitojun			 * with if_mtu value (decrement IPsec header size
38762587Sitojun			 * from mtu value).  The code is much simpler than v4
38862587Sitojun			 * case, as we have the outgoing interface for
38962587Sitojun			 * encapsulated packet as "rt->rt_ifp".
39062587Sitojun			 */
39162587Sitojun			sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
39262587Sitojun				IP_FORWARDING, &ipsecerror);
39362587Sitojun			if (sp) {
39462587Sitojun				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
39562587Sitojun					IPSEC_DIR_OUTBOUND, NULL);
39662587Sitojun				if (ipsechdrsiz < mtu)
39762587Sitojun					mtu -= ipsechdrsiz;
39862587Sitojun			}
39962587Sitojun
40062587Sitojun			/*
40162587Sitojun			 * if mtu becomes less than minimum MTU,
40262587Sitojun			 * tell minimum MTU (and I'll need to fragment it).
40362587Sitojun			 */
40462587Sitojun			if (mtu < IPV6_MMTU)
40562587Sitojun				mtu = IPV6_MMTU;
40662587Sitojun#endif
40762587Sitojun			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
40862587Sitojun		}
40962587Sitojun		m_freem(m);
41053541Sshin		return;
41153541Sshin 	}
41253541Sshin
41353541Sshin	if (rt->rt_flags & RTF_GATEWAY)
41453541Sshin		dst = (struct sockaddr_in6 *)rt->rt_gateway;
41553541Sshin
41653541Sshin	/*
41753541Sshin	 * If we are to forward the packet using the same interface
41853541Sshin	 * as one we got the packet from, perhaps we should send a redirect
41953541Sshin	 * to sender to shortcut a hop.
42053541Sshin	 * Only send redirect if source is sending directly to us,
42153541Sshin	 * and if packet was not source routed (or has any options).
42253541Sshin	 * Also, don't send redirect if forwarding using a route
42353541Sshin	 * modified by a redirect.
42453541Sshin	 */
42553541Sshin	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
42678064Sume	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
42778064Sume		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) {
42878064Sume			/*
42978064Sume			 * If the incoming interface is equal to the outgoing
43078064Sume			 * one, and the link attached to the interface is
43178064Sume			 * point-to-point, then it will be highly probable
43278064Sume			 * that a routing loop occurs. Thus, we immediately
43378064Sume			 * drop the packet and send an ICMPv6 error message.
43478064Sume			 *
43578064Sume			 * type/code is based on suggestion by Rich Draves.
43678064Sume			 * not sure if it is the best pick.
43778064Sume			 */
43878064Sume			icmp6_error(mcopy, ICMP6_DST_UNREACH,
43978064Sume				    ICMP6_DST_UNREACH_ADDR, 0);
44078064Sume			m_freem(m);
44178064Sume			return;
44278064Sume		}
44353541Sshin		type = ND_REDIRECT;
44478064Sume	}
44553541Sshin
44653541Sshin	/*
44753541Sshin	 * Check with the firewall...
44853541Sshin	 */
44966303Sume	if (ip6_fw_enable && ip6_fw_chk_ptr) {
45053541Sshin		u_short port = 0;
45153541Sshin		/* If ipfw says divert, we have to just drop packet */
45253541Sshin		if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
45353541Sshin			m_freem(m);
45453541Sshin			goto freecopy;
45553541Sshin		}
45653541Sshin		if (!m)
45753541Sshin			goto freecopy;
45853541Sshin	}
45953541Sshin
46062587Sitojun	/*
46162587Sitojun	 * Fake scoped addresses. Note that even link-local source or
46262587Sitojun	 * destinaion can appear, if the originating node just sends the
46362587Sitojun	 * packet to us (without address resolution for the destination).
46462587Sitojun	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
46578064Sume	 * link identifiers, we can do this stuff after making a copy for
46678064Sume	 * returning an error.
46762587Sitojun	 */
46862587Sitojun	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
46962587Sitojun		/*
47062587Sitojun		 * See corresponding comments in ip6_output.
47162587Sitojun		 * XXX: but is it possible that ip6_forward() sends a packet
47262587Sitojun		 *      to a loopback interface? I don't think so, and thus
47362587Sitojun		 *      I bark here. (jinmei@kame.net)
47462587Sitojun		 * XXX: it is common to route invalid packets to loopback.
47562587Sitojun		 *	also, the codepath will be visited on use of ::1 in
47662587Sitojun		 *	rthdr. (itojun)
47762587Sitojun		 */
47862587Sitojun#if 1
47962587Sitojun		if (0)
48062587Sitojun#else
48162587Sitojun		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
48262587Sitojun#endif
48362587Sitojun		{
48462587Sitojun			printf("ip6_forward: outgoing interface is loopback. "
48562587Sitojun			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
48662587Sitojun			       ip6_sprintf(&ip6->ip6_src),
48762587Sitojun			       ip6_sprintf(&ip6->ip6_dst),
48862587Sitojun			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
48962587Sitojun			       if_name(rt->rt_ifp));
49062587Sitojun		}
49162587Sitojun
49278064Sume		/* we can just use rcvif in forwarding. */
49378064Sume		origifp = m->m_pkthdr.rcvif;
49462587Sitojun	}
49562587Sitojun	else
49662587Sitojun		origifp = rt->rt_ifp;
49778064Sume#ifndef SCOPEDROUTING
49878064Sume	/*
49978064Sume	 * clear embedded scope identifiers if necessary.
50078064Sume	 * in6_clearscope will touch the addresses only when necessary.
50178064Sume	 */
50278064Sume	in6_clearscope(&ip6->ip6_src);
50378064Sume	in6_clearscope(&ip6->ip6_dst);
50462587Sitojun#endif
50562587Sitojun
50662587Sitojun	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
50753541Sshin	if (error) {
50853541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
50953541Sshin		ip6stat.ip6s_cantforward++;
51053541Sshin	} else {
51153541Sshin		ip6stat.ip6s_forward++;
51253541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
51353541Sshin		if (type)
51453541Sshin			ip6stat.ip6s_redirectsent++;
51553541Sshin		else {
51653541Sshin			if (mcopy)
51753541Sshin				goto freecopy;
51853541Sshin		}
51953541Sshin	}
52053541Sshin	if (mcopy == NULL)
52153541Sshin		return;
52253541Sshin
52353541Sshin	switch (error) {
52453541Sshin	case 0:
52562587Sitojun#if 1
52653541Sshin		if (type == ND_REDIRECT) {
52753541Sshin			icmp6_redirect_output(mcopy, rt);
52853541Sshin			return;
52953541Sshin		}
53062587Sitojun#endif
53153541Sshin		goto freecopy;
53253541Sshin
53353541Sshin	case EMSGSIZE:
53453541Sshin		/* xxx MTU is constant in PPP? */
53553541Sshin		goto freecopy;
53653541Sshin
53753541Sshin	case ENOBUFS:
53853541Sshin		/* Tell source to slow down like source quench in IP? */
53953541Sshin		goto freecopy;
54053541Sshin
54153541Sshin	case ENETUNREACH:	/* shouldn't happen, checked above */
54253541Sshin	case EHOSTUNREACH:
54353541Sshin	case ENETDOWN:
54453541Sshin	case EHOSTDOWN:
54553541Sshin	default:
54653541Sshin		type = ICMP6_DST_UNREACH;
54753541Sshin		code = ICMP6_DST_UNREACH_ADDR;
54853541Sshin		break;
54953541Sshin	}
55053541Sshin	icmp6_error(mcopy, type, code, 0);
55153541Sshin	return;
55253541Sshin
55353541Sshin freecopy:
55453541Sshin	m_freem(mcopy);
55553541Sshin	return;
55653541Sshin}
557