ip6_forward.c revision 84994
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_forward.c 84994 2001-10-15 14:16:18Z darrenr $	*/
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>
5284994Sdarrenr#ifdef PFIL_HOOKS
5384994Sdarrenr#include <net/pfil.h>
5484994Sdarrenr#endif
5553541Sshin
5653541Sshin#include <netinet/in.h>
5753541Sshin#include <netinet/in_var.h>
5878064Sume#include <netinet/in_systm.h>
5978064Sume#include <netinet/ip.h>
6062587Sitojun#include <netinet/ip_var.h>
6178064Sume#include <netinet6/in6_var.h>
6262587Sitojun#include <netinet/ip6.h>
6353541Sshin#include <netinet6/ip6_var.h>
6462587Sitojun#include <netinet/icmp6.h>
6553541Sshin#include <netinet6/nd6.h>
6653541Sshin
6778064Sume#include <netinet/in_pcb.h>
6878064Sume
6963256Sitojun#ifdef IPSEC
7053541Sshin#include <netinet6/ipsec.h>
7178064Sume#ifdef INET6
7262601Sitojun#include <netinet6/ipsec6.h>
7378064Sume#endif
7453541Sshin#include <netkey/key.h>
7563256Sitojun#endif /* IPSEC */
7653541Sshin
7753541Sshin#include <netinet6/ip6_fw.h>
7853541Sshin
7953541Sshin#include <net/net_osdep.h>
8053541Sshin
8184994Sdarrenr#include <netinet6/ip6protosw.h>
8284994Sdarrenr
8384994Sdarrenrextern struct ip6protosw inet6sw[];
8484994Sdarrenrextern u_char ip6_protox[IPPROTO_MAX];
8584994Sdarrenr
8653541Sshinstruct	route_in6 ip6_forward_rt;
8753541Sshin
8853541Sshin/*
8953541Sshin * Forward a packet.  If some error occurs return the sender
9053541Sshin * an icmp packet.  Note we can't always generate a meaningful
9153541Sshin * icmp message because icmp doesn't have a large enough repertoire
9253541Sshin * of codes and types.
9353541Sshin *
9453541Sshin * If not forwarding, just drop the packet.  This could be confusing
9553541Sshin * if ipforwarding was zero but some routing protocol was advancing
9653541Sshin * us as a gateway to somewhere.  However, we must let the routing
9753541Sshin * protocol deal with that.
9853541Sshin *
9953541Sshin */
10053541Sshin
10153541Sshinvoid
10253541Sshinip6_forward(m, srcrt)
10353541Sshin	struct mbuf *m;
10453541Sshin	int srcrt;
10553541Sshin{
10653541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
10778064Sume	struct sockaddr_in6 *dst;
10878064Sume	struct rtentry *rt;
10953541Sshin	int error, type = 0, code = 0;
11062587Sitojun	struct mbuf *mcopy = NULL;
11162587Sitojun	struct ifnet *origifp;	/* maybe unnecessary */
11284994Sdarrenr#ifdef PFIL_HOOKS
11384994Sdarrenr	struct packet_filter_hook *pfh;
11484994Sdarrenr	struct mbuf *m1;
11584994Sdarrenr	int rv;
11684994Sdarrenr#endif /* PFIL_HOOKS */
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)) {
13053541Sshin		ipsec6stat.in_polvio++;
13153541Sshin		m_freem(m);
13253541Sshin		return;
13353541Sshin	}
13463256Sitojun#endif /*IPSEC*/
13553541Sshin
13678064Sume	/*
13778064Sume	 * Do not forward packets to multicast destination (should be handled
13878064Sume	 * by ip6_mforward().
13978064Sume	 * Do not forward packets with unspecified source.  It was discussed
14078064Sume	 * in July 2000, on ipngwg mailing list.
14178064Sume	 */
14262587Sitojun	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
14378064Sume	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
14478064Sume	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
14553541Sshin		ip6stat.ip6s_cantforward++;
14653541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
14753541Sshin		if (ip6_log_time + ip6_log_interval < time_second) {
14853541Sshin			ip6_log_time = time_second;
14953541Sshin			log(LOG_DEBUG,
15053541Sshin			    "cannot forward "
15153541Sshin			    "from %s to %s nxt %d received on %s\n",
15262587Sitojun			    ip6_sprintf(&ip6->ip6_src),
15362587Sitojun			    ip6_sprintf(&ip6->ip6_dst),
15453541Sshin			    ip6->ip6_nxt,
15553541Sshin			    if_name(m->m_pkthdr.rcvif));
15653541Sshin		}
15753541Sshin		m_freem(m);
15853541Sshin		return;
15953541Sshin	}
16053541Sshin
16153541Sshin	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
16253541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
16353541Sshin		icmp6_error(m, ICMP6_TIME_EXCEEDED,
16453541Sshin				ICMP6_TIME_EXCEED_TRANSIT, 0);
16553541Sshin		return;
16653541Sshin	}
16753541Sshin	ip6->ip6_hlim -= IPV6_HLIMDEC;
16853541Sshin
16962587Sitojun	/*
17062587Sitojun	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
17162587Sitojun	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
17262587Sitojun	 * we need to generate an ICMP6 message to the src.
17362587Sitojun	 * Thanks to M_EXT, in most cases copy will not occur.
17462587Sitojun	 *
17562587Sitojun	 * It is important to save it before IPsec processing as IPsec
17662587Sitojun	 * processing may modify the mbuf.
17762587Sitojun	 */
17862587Sitojun	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
17962587Sitojun
18063256Sitojun#ifdef IPSEC
18153541Sshin	/* get a security policy for this packet */
18278064Sume	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
18378064Sume	    &error);
18453541Sshin	if (sp == NULL) {
18553541Sshin		ipsec6stat.out_inval++;
18653541Sshin		ip6stat.ip6s_cantforward++;
18762587Sitojun		if (mcopy) {
18862587Sitojun#if 0
18962587Sitojun			/* XXX: what icmp ? */
19062587Sitojun#else
19162587Sitojun			m_freem(mcopy);
19262587Sitojun#endif
19362587Sitojun		}
19453541Sshin		m_freem(m);
19553541Sshin		return;
19653541Sshin	}
19753541Sshin
19853541Sshin	error = 0;
19953541Sshin
20053541Sshin	/* check policy */
20153541Sshin	switch (sp->policy) {
20253541Sshin	case IPSEC_POLICY_DISCARD:
20353541Sshin		/*
20453541Sshin		 * This packet is just discarded.
20553541Sshin		 */
20653541Sshin		ipsec6stat.out_polvio++;
20753541Sshin		ip6stat.ip6s_cantforward++;
20853541Sshin		key_freesp(sp);
20962587Sitojun		if (mcopy) {
21062587Sitojun#if 0
21162587Sitojun			/* XXX: what icmp ? */
21262587Sitojun#else
21362587Sitojun			m_freem(mcopy);
21462587Sitojun#endif
21562587Sitojun		}
21653541Sshin		m_freem(m);
21753541Sshin		return;
21853541Sshin
21953541Sshin	case IPSEC_POLICY_BYPASS:
22053541Sshin	case IPSEC_POLICY_NONE:
22153541Sshin		/* no need to do IPsec. */
22253541Sshin		key_freesp(sp);
22353541Sshin		goto skip_ipsec;
22462587Sitojun
22553541Sshin	case IPSEC_POLICY_IPSEC:
22653541Sshin		if (sp->req == NULL) {
22753541Sshin			/* XXX should be panic ? */
22853541Sshin			printf("ip6_forward: No IPsec request specified.\n");
22953541Sshin			ip6stat.ip6s_cantforward++;
23053541Sshin			key_freesp(sp);
23162587Sitojun			if (mcopy) {
23262587Sitojun#if 0
23362587Sitojun				/* XXX: what icmp ? */
23462587Sitojun#else
23562587Sitojun				m_freem(mcopy);
23662587Sitojun#endif
23762587Sitojun			}
23853541Sshin			m_freem(m);
23953541Sshin			return;
24053541Sshin		}
24153541Sshin		/* do IPsec */
24253541Sshin		break;
24353541Sshin
24453541Sshin	case IPSEC_POLICY_ENTRUST:
24553541Sshin	default:
24653541Sshin		/* should be panic ?? */
24753541Sshin		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
24853541Sshin		key_freesp(sp);
24953541Sshin		goto skip_ipsec;
25053541Sshin	}
25153541Sshin
25253541Sshin    {
25353541Sshin	struct ipsec_output_state state;
25453541Sshin
25553541Sshin	/*
25653541Sshin	 * All the extension headers will become inaccessible
25753541Sshin	 * (since they can be encrypted).
25853541Sshin	 * Don't panic, we need no more updates to extension headers
25953541Sshin	 * on inner IPv6 packet (since they are now encapsulated).
26053541Sshin	 *
26153541Sshin	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
26253541Sshin	 */
26353541Sshin	bzero(&state, sizeof(state));
26453541Sshin	state.m = m;
26553541Sshin	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
26653541Sshin	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
26753541Sshin
26853541Sshin	error = ipsec6_output_tunnel(&state, sp, 0);
26953541Sshin
27053541Sshin	m = state.m;
27153541Sshin	key_freesp(sp);
27253541Sshin
27353541Sshin	if (error) {
27453541Sshin		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
27553541Sshin		switch (error) {
27653541Sshin		case EHOSTUNREACH:
27753541Sshin		case ENETUNREACH:
27853541Sshin		case EMSGSIZE:
27953541Sshin		case ENOBUFS:
28053541Sshin		case ENOMEM:
28153541Sshin			break;
28253541Sshin		default:
28353541Sshin			printf("ip6_output (ipsec): error code %d\n", error);
28453541Sshin			/*fall through*/
28553541Sshin		case ENOENT:
28653541Sshin			/* don't show these error codes to the user */
28753541Sshin			break;
28853541Sshin		}
28953541Sshin		ip6stat.ip6s_cantforward++;
29062587Sitojun		if (mcopy) {
29162587Sitojun#if 0
29262587Sitojun			/* XXX: what icmp ? */
29362587Sitojun#else
29462587Sitojun			m_freem(mcopy);
29562587Sitojun#endif
29662587Sitojun		}
29753541Sshin		m_freem(m);
29853541Sshin		return;
29953541Sshin	}
30053541Sshin    }
30153541Sshin    skip_ipsec:
30263256Sitojun#endif /* IPSEC */
30353541Sshin
30478064Sume	dst = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
30553541Sshin	if (!srcrt) {
30653541Sshin		/*
30753541Sshin		 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
30853541Sshin		 */
30953541Sshin		if (ip6_forward_rt.ro_rt == 0 ||
31053541Sshin		    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
31153541Sshin			if (ip6_forward_rt.ro_rt) {
31253541Sshin				RTFREE(ip6_forward_rt.ro_rt);
31353541Sshin				ip6_forward_rt.ro_rt = 0;
31453541Sshin			}
31553541Sshin			/* this probably fails but give it a try again */
31653541Sshin			rtalloc_ign((struct route *)&ip6_forward_rt,
31753541Sshin				    RTF_PRCLONING);
31853541Sshin		}
31962587Sitojun
32053541Sshin		if (ip6_forward_rt.ro_rt == 0) {
32153541Sshin			ip6stat.ip6s_noroute++;
32278064Sume			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
32362587Sitojun			if (mcopy) {
32462587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
32562587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
32662587Sitojun			}
32762587Sitojun			m_freem(m);
32853541Sshin			return;
32953541Sshin		}
33053541Sshin	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
33153541Sshin		 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
33253541Sshin		if (ip6_forward_rt.ro_rt) {
33353541Sshin			RTFREE(ip6_forward_rt.ro_rt);
33453541Sshin			ip6_forward_rt.ro_rt = 0;
33553541Sshin		}
33653541Sshin		bzero(dst, sizeof(*dst));
33753541Sshin		dst->sin6_len = sizeof(struct sockaddr_in6);
33853541Sshin		dst->sin6_family = AF_INET6;
33953541Sshin		dst->sin6_addr = ip6->ip6_dst;
34053541Sshin
34153541Sshin  		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
34253541Sshin		if (ip6_forward_rt.ro_rt == 0) {
34353541Sshin			ip6stat.ip6s_noroute++;
34478064Sume			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
34562587Sitojun			if (mcopy) {
34662587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
34762587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
34862587Sitojun			}
34962587Sitojun			m_freem(m);
35053541Sshin			return;
35153541Sshin		}
35253541Sshin	}
35353541Sshin	rt = ip6_forward_rt.ro_rt;
35462587Sitojun
35562587Sitojun	/*
35662587Sitojun	 * Scope check: if a packet can't be delivered to its destination
35762587Sitojun	 * for the reason that the destination is beyond the scope of the
35862587Sitojun	 * source address, discard the packet and return an icmp6 destination
35962587Sitojun	 * unreachable error with Code 2 (beyond scope of source address).
36062587Sitojun	 * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1]
36162587Sitojun	 */
36262587Sitojun	if (in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_src) !=
36362587Sitojun	    in6_addr2scopeid(rt->rt_ifp, &ip6->ip6_src)) {
36462587Sitojun		ip6stat.ip6s_cantforward++;
36562587Sitojun		ip6stat.ip6s_badscope++;
36662587Sitojun		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
36762587Sitojun
36862587Sitojun		if (ip6_log_time + ip6_log_interval < time_second) {
36962587Sitojun			ip6_log_time = time_second;
37062587Sitojun			log(LOG_DEBUG,
37162587Sitojun			    "cannot forward "
37262587Sitojun			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
37362587Sitojun			    ip6_sprintf(&ip6->ip6_src),
37462587Sitojun			    ip6_sprintf(&ip6->ip6_dst),
37562587Sitojun			    ip6->ip6_nxt,
37662587Sitojun			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
37762587Sitojun		}
37862587Sitojun		if (mcopy)
37962587Sitojun			icmp6_error(mcopy, ICMP6_DST_UNREACH,
38062587Sitojun				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
38162587Sitojun		m_freem(m);
38262587Sitojun		return;
38362587Sitojun	}
38462587Sitojun
38562587Sitojun	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
38653541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
38762587Sitojun		if (mcopy) {
38862587Sitojun			u_long mtu;
38963256Sitojun#ifdef IPSEC
39062587Sitojun			struct secpolicy *sp;
39162587Sitojun			int ipsecerror;
39262587Sitojun			size_t ipsechdrsiz;
39362587Sitojun#endif
39462587Sitojun
39562587Sitojun			mtu = rt->rt_ifp->if_mtu;
39663256Sitojun#ifdef IPSEC
39762587Sitojun			/*
39862587Sitojun			 * When we do IPsec tunnel ingress, we need to play
39962587Sitojun			 * with if_mtu value (decrement IPsec header size
40062587Sitojun			 * from mtu value).  The code is much simpler than v4
40162587Sitojun			 * case, as we have the outgoing interface for
40262587Sitojun			 * encapsulated packet as "rt->rt_ifp".
40362587Sitojun			 */
40462587Sitojun			sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
40562587Sitojun				IP_FORWARDING, &ipsecerror);
40662587Sitojun			if (sp) {
40762587Sitojun				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
40862587Sitojun					IPSEC_DIR_OUTBOUND, NULL);
40962587Sitojun				if (ipsechdrsiz < mtu)
41062587Sitojun					mtu -= ipsechdrsiz;
41162587Sitojun			}
41262587Sitojun
41362587Sitojun			/*
41462587Sitojun			 * if mtu becomes less than minimum MTU,
41562587Sitojun			 * tell minimum MTU (and I'll need to fragment it).
41662587Sitojun			 */
41762587Sitojun			if (mtu < IPV6_MMTU)
41862587Sitojun				mtu = IPV6_MMTU;
41962587Sitojun#endif
42062587Sitojun			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
42162587Sitojun		}
42262587Sitojun		m_freem(m);
42353541Sshin		return;
42453541Sshin 	}
42553541Sshin
42653541Sshin	if (rt->rt_flags & RTF_GATEWAY)
42753541Sshin		dst = (struct sockaddr_in6 *)rt->rt_gateway;
42853541Sshin
42953541Sshin	/*
43053541Sshin	 * If we are to forward the packet using the same interface
43153541Sshin	 * as one we got the packet from, perhaps we should send a redirect
43253541Sshin	 * to sender to shortcut a hop.
43353541Sshin	 * Only send redirect if source is sending directly to us,
43453541Sshin	 * and if packet was not source routed (or has any options).
43553541Sshin	 * Also, don't send redirect if forwarding using a route
43653541Sshin	 * modified by a redirect.
43753541Sshin	 */
43853541Sshin	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
43978064Sume	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
44078064Sume		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) {
44178064Sume			/*
44278064Sume			 * If the incoming interface is equal to the outgoing
44378064Sume			 * one, and the link attached to the interface is
44478064Sume			 * point-to-point, then it will be highly probable
44578064Sume			 * that a routing loop occurs. Thus, we immediately
44678064Sume			 * drop the packet and send an ICMPv6 error message.
44778064Sume			 *
44878064Sume			 * type/code is based on suggestion by Rich Draves.
44978064Sume			 * not sure if it is the best pick.
45078064Sume			 */
45178064Sume			icmp6_error(mcopy, ICMP6_DST_UNREACH,
45278064Sume				    ICMP6_DST_UNREACH_ADDR, 0);
45378064Sume			m_freem(m);
45478064Sume			return;
45578064Sume		}
45653541Sshin		type = ND_REDIRECT;
45778064Sume	}
45853541Sshin
45953541Sshin	/*
46053541Sshin	 * Check with the firewall...
46153541Sshin	 */
46266303Sume	if (ip6_fw_enable && ip6_fw_chk_ptr) {
46353541Sshin		u_short port = 0;
46453541Sshin		/* If ipfw says divert, we have to just drop packet */
46553541Sshin		if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
46653541Sshin			m_freem(m);
46753541Sshin			goto freecopy;
46853541Sshin		}
46953541Sshin		if (!m)
47053541Sshin			goto freecopy;
47153541Sshin	}
47253541Sshin
47362587Sitojun	/*
47462587Sitojun	 * Fake scoped addresses. Note that even link-local source or
47562587Sitojun	 * destinaion can appear, if the originating node just sends the
47662587Sitojun	 * packet to us (without address resolution for the destination).
47762587Sitojun	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
47878064Sume	 * link identifiers, we can do this stuff after making a copy for
47978064Sume	 * returning an error.
48062587Sitojun	 */
48162587Sitojun	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
48262587Sitojun		/*
48362587Sitojun		 * See corresponding comments in ip6_output.
48462587Sitojun		 * XXX: but is it possible that ip6_forward() sends a packet
48562587Sitojun		 *      to a loopback interface? I don't think so, and thus
48662587Sitojun		 *      I bark here. (jinmei@kame.net)
48762587Sitojun		 * XXX: it is common to route invalid packets to loopback.
48862587Sitojun		 *	also, the codepath will be visited on use of ::1 in
48962587Sitojun		 *	rthdr. (itojun)
49062587Sitojun		 */
49162587Sitojun#if 1
49262587Sitojun		if (0)
49362587Sitojun#else
49462587Sitojun		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
49562587Sitojun#endif
49662587Sitojun		{
49762587Sitojun			printf("ip6_forward: outgoing interface is loopback. "
49862587Sitojun			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
49962587Sitojun			       ip6_sprintf(&ip6->ip6_src),
50062587Sitojun			       ip6_sprintf(&ip6->ip6_dst),
50162587Sitojun			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
50262587Sitojun			       if_name(rt->rt_ifp));
50362587Sitojun		}
50462587Sitojun
50578064Sume		/* we can just use rcvif in forwarding. */
50678064Sume		origifp = m->m_pkthdr.rcvif;
50762587Sitojun	}
50862587Sitojun	else
50962587Sitojun		origifp = rt->rt_ifp;
51078064Sume#ifndef SCOPEDROUTING
51178064Sume	/*
51278064Sume	 * clear embedded scope identifiers if necessary.
51378064Sume	 * in6_clearscope will touch the addresses only when necessary.
51478064Sume	 */
51578064Sume	in6_clearscope(&ip6->ip6_src);
51678064Sume	in6_clearscope(&ip6->ip6_dst);
51762587Sitojun#endif
51862587Sitojun
51984994Sdarrenr#ifdef PFIL_HOOKS
52084994Sdarrenr	/*
52184994Sdarrenr	 * Run through list of hooks for output packets.
52284994Sdarrenr	 */
52384994Sdarrenr	m1 = m;
52484994Sdarrenr	pfh = pfil_hook_get(PFIL_OUT, &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
52584994Sdarrenr	for (; pfh; pfh = pfh->pfil_link.tqe_next)
52684994Sdarrenr		if (pfh->pfil_func) {
52784994Sdarrenr			rv = pfh->pfil_func(ip6, sizeof(*ip6),
52884994Sdarrenr					    rt->rt_ifp, 1, &m1);
52984994Sdarrenr			if (rv) {
53084994Sdarrenr				error = EHOSTUNREACH;
53184994Sdarrenr				goto freecopy;
53284994Sdarrenr			}
53384994Sdarrenr			m = m1;
53484994Sdarrenr			if (m == NULL)
53584994Sdarrenr				goto freecopy;
53684994Sdarrenr			ip6 = mtod(m, struct ip6_hdr *);
53784994Sdarrenr		}
53884994Sdarrenr#endif /* PFIL_HOOKS */
53984994Sdarrenr
54062587Sitojun	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
54153541Sshin	if (error) {
54253541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
54353541Sshin		ip6stat.ip6s_cantforward++;
54453541Sshin	} else {
54553541Sshin		ip6stat.ip6s_forward++;
54653541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
54753541Sshin		if (type)
54853541Sshin			ip6stat.ip6s_redirectsent++;
54953541Sshin		else {
55053541Sshin			if (mcopy)
55153541Sshin				goto freecopy;
55253541Sshin		}
55353541Sshin	}
55453541Sshin	if (mcopy == NULL)
55553541Sshin		return;
55653541Sshin
55753541Sshin	switch (error) {
55853541Sshin	case 0:
55962587Sitojun#if 1
56053541Sshin		if (type == ND_REDIRECT) {
56153541Sshin			icmp6_redirect_output(mcopy, rt);
56253541Sshin			return;
56353541Sshin		}
56462587Sitojun#endif
56553541Sshin		goto freecopy;
56653541Sshin
56753541Sshin	case EMSGSIZE:
56853541Sshin		/* xxx MTU is constant in PPP? */
56953541Sshin		goto freecopy;
57053541Sshin
57153541Sshin	case ENOBUFS:
57253541Sshin		/* Tell source to slow down like source quench in IP? */
57353541Sshin		goto freecopy;
57453541Sshin
57553541Sshin	case ENETUNREACH:	/* shouldn't happen, checked above */
57653541Sshin	case EHOSTUNREACH:
57753541Sshin	case ENETDOWN:
57853541Sshin	case EHOSTDOWN:
57953541Sshin	default:
58053541Sshin		type = ICMP6_DST_UNREACH;
58153541Sshin		code = ICMP6_DST_UNREACH_ADDR;
58253541Sshin		break;
58353541Sshin	}
58453541Sshin	icmp6_error(mcopy, type, code, 0);
58553541Sshin	return;
58653541Sshin
58753541Sshin freecopy:
58853541Sshin	m_freem(mcopy);
58953541Sshin	return;
59053541Sshin}
591