ip6_forward.c revision 120386
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_forward.c 120386 2003-09-23 17:54:04Z sam $	*/
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"
37120386Ssam#include "opt_pfil_hooks.h"
3853541Sshin
3953541Sshin#include <sys/param.h>
4053541Sshin#include <sys/systm.h>
4178064Sume#include <sys/malloc.h>
4253541Sshin#include <sys/mbuf.h>
4353541Sshin#include <sys/domain.h>
4453541Sshin#include <sys/protosw.h>
4553541Sshin#include <sys/socket.h>
4653541Sshin#include <sys/errno.h>
4753541Sshin#include <sys/time.h>
4878064Sume#include <sys/kernel.h>
4953541Sshin#include <sys/syslog.h>
5053541Sshin
5153541Sshin#include <net/if.h>
5253541Sshin#include <net/route.h>
5384994Sdarrenr#ifdef PFIL_HOOKS
5484994Sdarrenr#include <net/pfil.h>
5584994Sdarrenr#endif
5653541Sshin
5753541Sshin#include <netinet/in.h>
5853541Sshin#include <netinet/in_var.h>
5978064Sume#include <netinet/in_systm.h>
6078064Sume#include <netinet/ip.h>
6162587Sitojun#include <netinet/ip_var.h>
6278064Sume#include <netinet6/in6_var.h>
6362587Sitojun#include <netinet/ip6.h>
6453541Sshin#include <netinet6/ip6_var.h>
6562587Sitojun#include <netinet/icmp6.h>
6653541Sshin#include <netinet6/nd6.h>
6753541Sshin
6878064Sume#include <netinet/in_pcb.h>
6978064Sume
7063256Sitojun#ifdef IPSEC
7153541Sshin#include <netinet6/ipsec.h>
7278064Sume#ifdef INET6
7362601Sitojun#include <netinet6/ipsec6.h>
7478064Sume#endif
7553541Sshin#include <netkey/key.h>
7663256Sitojun#endif /* IPSEC */
7753541Sshin
78105199Ssam#ifdef FAST_IPSEC
79105199Ssam#include <netipsec/ipsec.h>
80105199Ssam#include <netipsec/ipsec6.h>
81105199Ssam#include <netipsec/key.h>
82105199Ssam#define	IPSEC
83105199Ssam#endif /* FAST_IPSEC */
84105199Ssam
8553541Sshin#include <netinet6/ip6_fw.h>
8653541Sshin
8753541Sshin#include <net/net_osdep.h>
8853541Sshin
8984994Sdarrenr#include <netinet6/ip6protosw.h>
9084994Sdarrenr
9153541Sshinstruct	route_in6 ip6_forward_rt;
9253541Sshin
9353541Sshin/*
9453541Sshin * Forward a packet.  If some error occurs return the sender
9553541Sshin * an icmp packet.  Note we can't always generate a meaningful
9653541Sshin * icmp message because icmp doesn't have a large enough repertoire
9753541Sshin * of codes and types.
9853541Sshin *
9953541Sshin * If not forwarding, just drop the packet.  This could be confusing
10053541Sshin * if ipforwarding was zero but some routing protocol was advancing
10153541Sshin * us as a gateway to somewhere.  However, we must let the routing
10253541Sshin * protocol deal with that.
10353541Sshin *
10453541Sshin */
10553541Sshin
10653541Sshinvoid
10753541Sshinip6_forward(m, srcrt)
10853541Sshin	struct mbuf *m;
10953541Sshin	int srcrt;
11053541Sshin{
11153541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
11278064Sume	struct sockaddr_in6 *dst;
11378064Sume	struct rtentry *rt;
11453541Sshin	int error, type = 0, code = 0;
11562587Sitojun	struct mbuf *mcopy = NULL;
11662587Sitojun	struct ifnet *origifp;	/* maybe unnecessary */
11763256Sitojun#ifdef IPSEC
11853541Sshin	struct secpolicy *sp = NULL;
11953541Sshin#endif
12053541Sshin
12163256Sitojun#ifdef IPSEC
12253541Sshin	/*
12353541Sshin	 * Check AH/ESP integrity.
12453541Sshin	 */
12553541Sshin	/*
12653541Sshin	 * Don't increment ip6s_cantforward because this is the check
12753541Sshin	 * before forwarding packet actually.
12853541Sshin	 */
12953541Sshin	if (ipsec6_in_reject(m, NULL)) {
130105199Ssam#if !defined(FAST_IPSEC)
13153541Sshin		ipsec6stat.in_polvio++;
132105199Ssam#endif
13353541Sshin		m_freem(m);
13453541Sshin		return;
13553541Sshin	}
13695023Ssuz#endif /* IPSEC */
13753541Sshin
13878064Sume	/*
13978064Sume	 * Do not forward packets to multicast destination (should be handled
14078064Sume	 * by ip6_mforward().
14178064Sume	 * Do not forward packets with unspecified source.  It was discussed
14278064Sume	 * in July 2000, on ipngwg mailing list.
14378064Sume	 */
14462587Sitojun	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
14578064Sume	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
14678064Sume	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
14753541Sshin		ip6stat.ip6s_cantforward++;
14853541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
14953541Sshin		if (ip6_log_time + ip6_log_interval < time_second) {
15053541Sshin			ip6_log_time = time_second;
15153541Sshin			log(LOG_DEBUG,
15253541Sshin			    "cannot forward "
15353541Sshin			    "from %s to %s nxt %d received on %s\n",
15462587Sitojun			    ip6_sprintf(&ip6->ip6_src),
15562587Sitojun			    ip6_sprintf(&ip6->ip6_dst),
15653541Sshin			    ip6->ip6_nxt,
15753541Sshin			    if_name(m->m_pkthdr.rcvif));
15853541Sshin		}
15953541Sshin		m_freem(m);
16053541Sshin		return;
16153541Sshin	}
16253541Sshin
16353541Sshin	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
16453541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
16553541Sshin		icmp6_error(m, ICMP6_TIME_EXCEEDED,
16653541Sshin				ICMP6_TIME_EXCEED_TRANSIT, 0);
16753541Sshin		return;
16853541Sshin	}
16953541Sshin	ip6->ip6_hlim -= IPV6_HLIMDEC;
17053541Sshin
17162587Sitojun	/*
17262587Sitojun	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
17362587Sitojun	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
17462587Sitojun	 * we need to generate an ICMP6 message to the src.
17562587Sitojun	 * Thanks to M_EXT, in most cases copy will not occur.
17662587Sitojun	 *
17762587Sitojun	 * It is important to save it before IPsec processing as IPsec
17862587Sitojun	 * processing may modify the mbuf.
17962587Sitojun	 */
18062587Sitojun	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
18162587Sitojun
18263256Sitojun#ifdef IPSEC
18353541Sshin	/* get a security policy for this packet */
18478064Sume	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
18578064Sume	    &error);
18653541Sshin	if (sp == NULL) {
18753541Sshin		ipsec6stat.out_inval++;
18853541Sshin		ip6stat.ip6s_cantforward++;
18962587Sitojun		if (mcopy) {
19062587Sitojun#if 0
19162587Sitojun			/* XXX: what icmp ? */
19262587Sitojun#else
19362587Sitojun			m_freem(mcopy);
19462587Sitojun#endif
19562587Sitojun		}
19653541Sshin		m_freem(m);
19753541Sshin		return;
19853541Sshin	}
19953541Sshin
20053541Sshin	error = 0;
20153541Sshin
20253541Sshin	/* check policy */
20353541Sshin	switch (sp->policy) {
20453541Sshin	case IPSEC_POLICY_DISCARD:
20553541Sshin		/*
20653541Sshin		 * This packet is just discarded.
20753541Sshin		 */
20853541Sshin		ipsec6stat.out_polvio++;
20953541Sshin		ip6stat.ip6s_cantforward++;
21053541Sshin		key_freesp(sp);
21162587Sitojun		if (mcopy) {
21262587Sitojun#if 0
21362587Sitojun			/* XXX: what icmp ? */
21462587Sitojun#else
21562587Sitojun			m_freem(mcopy);
21662587Sitojun#endif
21762587Sitojun		}
21853541Sshin		m_freem(m);
21953541Sshin		return;
22053541Sshin
22153541Sshin	case IPSEC_POLICY_BYPASS:
22253541Sshin	case IPSEC_POLICY_NONE:
22353541Sshin		/* no need to do IPsec. */
22453541Sshin		key_freesp(sp);
22553541Sshin		goto skip_ipsec;
22662587Sitojun
22753541Sshin	case IPSEC_POLICY_IPSEC:
22853541Sshin		if (sp->req == NULL) {
22953541Sshin			/* XXX should be panic ? */
23053541Sshin			printf("ip6_forward: No IPsec request specified.\n");
23153541Sshin			ip6stat.ip6s_cantforward++;
23253541Sshin			key_freesp(sp);
23362587Sitojun			if (mcopy) {
23462587Sitojun#if 0
23562587Sitojun				/* XXX: what icmp ? */
23662587Sitojun#else
23762587Sitojun				m_freem(mcopy);
23862587Sitojun#endif
23962587Sitojun			}
24053541Sshin			m_freem(m);
24153541Sshin			return;
24253541Sshin		}
24353541Sshin		/* do IPsec */
24453541Sshin		break;
24553541Sshin
24653541Sshin	case IPSEC_POLICY_ENTRUST:
24753541Sshin	default:
24853541Sshin		/* should be panic ?? */
24953541Sshin		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
25053541Sshin		key_freesp(sp);
25153541Sshin		goto skip_ipsec;
25253541Sshin	}
25353541Sshin
25453541Sshin    {
25553541Sshin	struct ipsec_output_state state;
25653541Sshin
25753541Sshin	/*
25853541Sshin	 * All the extension headers will become inaccessible
25953541Sshin	 * (since they can be encrypted).
26053541Sshin	 * Don't panic, we need no more updates to extension headers
26153541Sshin	 * on inner IPv6 packet (since they are now encapsulated).
26253541Sshin	 *
26353541Sshin	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
26453541Sshin	 */
26553541Sshin	bzero(&state, sizeof(state));
26653541Sshin	state.m = m;
26753541Sshin	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
26853541Sshin	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
26953541Sshin
27053541Sshin	error = ipsec6_output_tunnel(&state, sp, 0);
27153541Sshin
27253541Sshin	m = state.m;
27353541Sshin	key_freesp(sp);
27453541Sshin
27553541Sshin	if (error) {
27653541Sshin		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
27753541Sshin		switch (error) {
27853541Sshin		case EHOSTUNREACH:
27953541Sshin		case ENETUNREACH:
28053541Sshin		case EMSGSIZE:
28153541Sshin		case ENOBUFS:
28253541Sshin		case ENOMEM:
28353541Sshin			break;
28453541Sshin		default:
28553541Sshin			printf("ip6_output (ipsec): error code %d\n", error);
28695023Ssuz			/* fall through */
28753541Sshin		case ENOENT:
28853541Sshin			/* don't show these error codes to the user */
28953541Sshin			break;
29053541Sshin		}
29153541Sshin		ip6stat.ip6s_cantforward++;
29262587Sitojun		if (mcopy) {
29362587Sitojun#if 0
29462587Sitojun			/* XXX: what icmp ? */
29562587Sitojun#else
29662587Sitojun			m_freem(mcopy);
29762587Sitojun#endif
29862587Sitojun		}
29953541Sshin		m_freem(m);
30053541Sshin		return;
30153541Sshin	}
30253541Sshin    }
30353541Sshin    skip_ipsec:
30463256Sitojun#endif /* IPSEC */
30553541Sshin
30678064Sume	dst = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
30753541Sshin	if (!srcrt) {
30853541Sshin		/*
30953541Sshin		 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
31053541Sshin		 */
31153541Sshin		if (ip6_forward_rt.ro_rt == 0 ||
31253541Sshin		    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
31353541Sshin			if (ip6_forward_rt.ro_rt) {
31453541Sshin				RTFREE(ip6_forward_rt.ro_rt);
31553541Sshin				ip6_forward_rt.ro_rt = 0;
31653541Sshin			}
31753541Sshin			/* this probably fails but give it a try again */
31853541Sshin			rtalloc_ign((struct route *)&ip6_forward_rt,
31953541Sshin				    RTF_PRCLONING);
32053541Sshin		}
32162587Sitojun
32253541Sshin		if (ip6_forward_rt.ro_rt == 0) {
32353541Sshin			ip6stat.ip6s_noroute++;
32478064Sume			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
32562587Sitojun			if (mcopy) {
32662587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
32762587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
32862587Sitojun			}
32962587Sitojun			m_freem(m);
33053541Sshin			return;
33153541Sshin		}
33253541Sshin	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
33353541Sshin		 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
33453541Sshin		if (ip6_forward_rt.ro_rt) {
33553541Sshin			RTFREE(ip6_forward_rt.ro_rt);
33653541Sshin			ip6_forward_rt.ro_rt = 0;
33753541Sshin		}
33853541Sshin		bzero(dst, sizeof(*dst));
33953541Sshin		dst->sin6_len = sizeof(struct sockaddr_in6);
34053541Sshin		dst->sin6_family = AF_INET6;
34153541Sshin		dst->sin6_addr = ip6->ip6_dst;
34253541Sshin
34353541Sshin  		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
34453541Sshin		if (ip6_forward_rt.ro_rt == 0) {
34553541Sshin			ip6stat.ip6s_noroute++;
34678064Sume			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
34762587Sitojun			if (mcopy) {
34862587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
34962587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
35062587Sitojun			}
35162587Sitojun			m_freem(m);
35253541Sshin			return;
35353541Sshin		}
35453541Sshin	}
35553541Sshin	rt = ip6_forward_rt.ro_rt;
35662587Sitojun
35762587Sitojun	/*
35862587Sitojun	 * Scope check: if a packet can't be delivered to its destination
35962587Sitojun	 * for the reason that the destination is beyond the scope of the
36062587Sitojun	 * source address, discard the packet and return an icmp6 destination
36162587Sitojun	 * unreachable error with Code 2 (beyond scope of source address).
36295023Ssuz	 * [draft-ietf-ipngwg-icmp-v3-02.txt, Section 3.1]
36362587Sitojun	 */
36462587Sitojun	if (in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_src) !=
36562587Sitojun	    in6_addr2scopeid(rt->rt_ifp, &ip6->ip6_src)) {
36662587Sitojun		ip6stat.ip6s_cantforward++;
36762587Sitojun		ip6stat.ip6s_badscope++;
36862587Sitojun		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
36962587Sitojun
37062587Sitojun		if (ip6_log_time + ip6_log_interval < time_second) {
37162587Sitojun			ip6_log_time = time_second;
37262587Sitojun			log(LOG_DEBUG,
37362587Sitojun			    "cannot forward "
37462587Sitojun			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
37562587Sitojun			    ip6_sprintf(&ip6->ip6_src),
37662587Sitojun			    ip6_sprintf(&ip6->ip6_dst),
37762587Sitojun			    ip6->ip6_nxt,
37862587Sitojun			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
37962587Sitojun		}
38062587Sitojun		if (mcopy)
38162587Sitojun			icmp6_error(mcopy, ICMP6_DST_UNREACH,
38262587Sitojun				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
38362587Sitojun		m_freem(m);
38462587Sitojun		return;
38562587Sitojun	}
38662587Sitojun
38762587Sitojun	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
38853541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
38962587Sitojun		if (mcopy) {
39062587Sitojun			u_long mtu;
39163256Sitojun#ifdef IPSEC
39262587Sitojun			struct secpolicy *sp;
39362587Sitojun			int ipsecerror;
39462587Sitojun			size_t ipsechdrsiz;
39562587Sitojun#endif
39662587Sitojun
39762587Sitojun			mtu = rt->rt_ifp->if_mtu;
39863256Sitojun#ifdef IPSEC
39962587Sitojun			/*
40062587Sitojun			 * When we do IPsec tunnel ingress, we need to play
40162587Sitojun			 * with if_mtu value (decrement IPsec header size
40262587Sitojun			 * from mtu value).  The code is much simpler than v4
40362587Sitojun			 * case, as we have the outgoing interface for
40462587Sitojun			 * encapsulated packet as "rt->rt_ifp".
40562587Sitojun			 */
40662587Sitojun			sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
40762587Sitojun				IP_FORWARDING, &ipsecerror);
40862587Sitojun			if (sp) {
40962587Sitojun				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
41062587Sitojun					IPSEC_DIR_OUTBOUND, NULL);
41162587Sitojun				if (ipsechdrsiz < mtu)
41262587Sitojun					mtu -= ipsechdrsiz;
41362587Sitojun			}
41462587Sitojun
41562587Sitojun			/*
41662587Sitojun			 * if mtu becomes less than minimum MTU,
41762587Sitojun			 * tell minimum MTU (and I'll need to fragment it).
41862587Sitojun			 */
41962587Sitojun			if (mtu < IPV6_MMTU)
42062587Sitojun				mtu = IPV6_MMTU;
42162587Sitojun#endif
42262587Sitojun			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
42362587Sitojun		}
42462587Sitojun		m_freem(m);
42553541Sshin		return;
42653541Sshin 	}
42753541Sshin
42853541Sshin	if (rt->rt_flags & RTF_GATEWAY)
42953541Sshin		dst = (struct sockaddr_in6 *)rt->rt_gateway;
43053541Sshin
43153541Sshin	/*
43253541Sshin	 * If we are to forward the packet using the same interface
43353541Sshin	 * as one we got the packet from, perhaps we should send a redirect
43453541Sshin	 * to sender to shortcut a hop.
43553541Sshin	 * Only send redirect if source is sending directly to us,
43653541Sshin	 * and if packet was not source routed (or has any options).
43753541Sshin	 * Also, don't send redirect if forwarding using a route
43853541Sshin	 * modified by a redirect.
43953541Sshin	 */
44053541Sshin	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
44178064Sume	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
44278064Sume		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) {
44378064Sume			/*
44478064Sume			 * If the incoming interface is equal to the outgoing
44578064Sume			 * one, and the link attached to the interface is
44678064Sume			 * point-to-point, then it will be highly probable
44778064Sume			 * that a routing loop occurs. Thus, we immediately
44878064Sume			 * drop the packet and send an ICMPv6 error message.
44978064Sume			 *
45078064Sume			 * type/code is based on suggestion by Rich Draves.
45178064Sume			 * not sure if it is the best pick.
45278064Sume			 */
45378064Sume			icmp6_error(mcopy, ICMP6_DST_UNREACH,
45478064Sume				    ICMP6_DST_UNREACH_ADDR, 0);
45578064Sume			m_freem(m);
45678064Sume			return;
45778064Sume		}
45853541Sshin		type = ND_REDIRECT;
45978064Sume	}
46053541Sshin
46153541Sshin	/*
46253541Sshin	 * Check with the firewall...
46353541Sshin	 */
46466303Sume	if (ip6_fw_enable && ip6_fw_chk_ptr) {
46553541Sshin		u_short port = 0;
46653541Sshin		/* If ipfw says divert, we have to just drop packet */
46753541Sshin		if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
46853541Sshin			m_freem(m);
46953541Sshin			goto freecopy;
47053541Sshin		}
47153541Sshin		if (!m)
47253541Sshin			goto freecopy;
47353541Sshin	}
47453541Sshin
47562587Sitojun	/*
47662587Sitojun	 * Fake scoped addresses. Note that even link-local source or
47762587Sitojun	 * destinaion can appear, if the originating node just sends the
47862587Sitojun	 * packet to us (without address resolution for the destination).
47962587Sitojun	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
48078064Sume	 * link identifiers, we can do this stuff after making a copy for
48178064Sume	 * returning an error.
48262587Sitojun	 */
48362587Sitojun	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
48462587Sitojun		/*
48562587Sitojun		 * See corresponding comments in ip6_output.
48662587Sitojun		 * XXX: but is it possible that ip6_forward() sends a packet
48762587Sitojun		 *      to a loopback interface? I don't think so, and thus
48862587Sitojun		 *      I bark here. (jinmei@kame.net)
48962587Sitojun		 * XXX: it is common to route invalid packets to loopback.
49062587Sitojun		 *	also, the codepath will be visited on use of ::1 in
49162587Sitojun		 *	rthdr. (itojun)
49262587Sitojun		 */
49362587Sitojun#if 1
49462587Sitojun		if (0)
49562587Sitojun#else
49662587Sitojun		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
49762587Sitojun#endif
49862587Sitojun		{
49962587Sitojun			printf("ip6_forward: outgoing interface is loopback. "
50088069Ssumikawa				"src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
50188069Ssumikawa				ip6_sprintf(&ip6->ip6_src),
50288069Ssumikawa				ip6_sprintf(&ip6->ip6_dst),
50388069Ssumikawa				ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
50488069Ssumikawa				if_name(rt->rt_ifp));
50562587Sitojun		}
50662587Sitojun
50778064Sume		/* we can just use rcvif in forwarding. */
50878064Sume		origifp = m->m_pkthdr.rcvif;
50962587Sitojun	}
51062587Sitojun	else
51162587Sitojun		origifp = rt->rt_ifp;
51278064Sume#ifndef SCOPEDROUTING
51378064Sume	/*
51478064Sume	 * clear embedded scope identifiers if necessary.
51578064Sume	 * in6_clearscope will touch the addresses only when necessary.
51678064Sume	 */
51778064Sume	in6_clearscope(&ip6->ip6_src);
51878064Sume	in6_clearscope(&ip6->ip6_dst);
51962587Sitojun#endif
52062587Sitojun
52184994Sdarrenr#ifdef PFIL_HOOKS
52284994Sdarrenr	/*
52384994Sdarrenr	 * Run through list of hooks for output packets.
52484994Sdarrenr	 */
525120386Ssam	if (pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT) != 0) {
526120386Ssam		error = EHOSTUNREACH;
527120386Ssam		goto freecopy;
528120386Ssam	}
529120386Ssam	if (m == NULL)
530120386Ssam		goto freecopy;
531120386Ssam	ip6 = mtod(m, struct ip6_hdr *);
53284994Sdarrenr#endif /* PFIL_HOOKS */
53384994Sdarrenr
53462587Sitojun	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
53553541Sshin	if (error) {
53653541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
53753541Sshin		ip6stat.ip6s_cantforward++;
53853541Sshin	} else {
53953541Sshin		ip6stat.ip6s_forward++;
54053541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
54153541Sshin		if (type)
54253541Sshin			ip6stat.ip6s_redirectsent++;
54353541Sshin		else {
54453541Sshin			if (mcopy)
54553541Sshin				goto freecopy;
54653541Sshin		}
54753541Sshin	}
54853541Sshin	if (mcopy == NULL)
54953541Sshin		return;
55053541Sshin	switch (error) {
55153541Sshin	case 0:
55262587Sitojun#if 1
55353541Sshin		if (type == ND_REDIRECT) {
55453541Sshin			icmp6_redirect_output(mcopy, rt);
55553541Sshin			return;
55653541Sshin		}
55762587Sitojun#endif
55853541Sshin		goto freecopy;
55953541Sshin
56053541Sshin	case EMSGSIZE:
56153541Sshin		/* xxx MTU is constant in PPP? */
56253541Sshin		goto freecopy;
56353541Sshin
56453541Sshin	case ENOBUFS:
56553541Sshin		/* Tell source to slow down like source quench in IP? */
56653541Sshin		goto freecopy;
56753541Sshin
56853541Sshin	case ENETUNREACH:	/* shouldn't happen, checked above */
56953541Sshin	case EHOSTUNREACH:
57053541Sshin	case ENETDOWN:
57153541Sshin	case EHOSTDOWN:
57253541Sshin	default:
57353541Sshin		type = ICMP6_DST_UNREACH;
57453541Sshin		code = ICMP6_DST_UNREACH_ADDR;
57553541Sshin		break;
57653541Sshin	}
57753541Sshin	icmp6_error(mcopy, type, code, 0);
57853541Sshin	return;
57953541Sshin
58053541Sshin freecopy:
58153541Sshin	m_freem(mcopy);
58253541Sshin	return;
58353541Sshin}
584