ip6_forward.c revision 120913
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_forward.c 120913 2003-10-08 18:26:08Z 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"
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
142120913Sume	 * in July 2000, on the 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 */
184120913Sume	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
185120913Sume	    IP_FORWARDING, &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);
286120913Sume			/* FALLTHROUGH */
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) {
308120913Sume		/* ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst */
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			}
315120913Sume
31653541Sshin			/* this probably fails but give it a try again */
31753541Sshin			rtalloc_ign((struct route *)&ip6_forward_rt,
31853541Sshin				    RTF_PRCLONING);
31953541Sshin		}
32062587Sitojun
32153541Sshin		if (ip6_forward_rt.ro_rt == 0) {
32253541Sshin			ip6stat.ip6s_noroute++;
32378064Sume			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
32462587Sitojun			if (mcopy) {
32562587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
32662587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
32762587Sitojun			}
32862587Sitojun			m_freem(m);
32953541Sshin			return;
33053541Sshin		}
33153541Sshin	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
332120913Sume		   !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
33353541Sshin		if (ip6_forward_rt.ro_rt) {
33453541Sshin			RTFREE(ip6_forward_rt.ro_rt);
33553541Sshin			ip6_forward_rt.ro_rt = 0;
33653541Sshin		}
33753541Sshin		bzero(dst, sizeof(*dst));
33853541Sshin		dst->sin6_len = sizeof(struct sockaddr_in6);
33953541Sshin		dst->sin6_family = AF_INET6;
34053541Sshin		dst->sin6_addr = ip6->ip6_dst;
34153541Sshin
34253541Sshin  		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
34353541Sshin		if (ip6_forward_rt.ro_rt == 0) {
34453541Sshin			ip6stat.ip6s_noroute++;
34578064Sume			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
34662587Sitojun			if (mcopy) {
34762587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
34862587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
34962587Sitojun			}
35062587Sitojun			m_freem(m);
35153541Sshin			return;
35253541Sshin		}
35353541Sshin	}
35453541Sshin	rt = ip6_forward_rt.ro_rt;
35562587Sitojun
35662587Sitojun	/*
35762587Sitojun	 * Scope check: if a packet can't be delivered to its destination
35862587Sitojun	 * for the reason that the destination is beyond the scope of the
35962587Sitojun	 * source address, discard the packet and return an icmp6 destination
36062587Sitojun	 * unreachable error with Code 2 (beyond scope of source address).
36195023Ssuz	 * [draft-ietf-ipngwg-icmp-v3-02.txt, Section 3.1]
36262587Sitojun	 */
36362587Sitojun	if (in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_src) !=
36462587Sitojun	    in6_addr2scopeid(rt->rt_ifp, &ip6->ip6_src)) {
36562587Sitojun		ip6stat.ip6s_cantforward++;
36662587Sitojun		ip6stat.ip6s_badscope++;
36762587Sitojun		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
36862587Sitojun
36962587Sitojun		if (ip6_log_time + ip6_log_interval < time_second) {
37062587Sitojun			ip6_log_time = time_second;
37162587Sitojun			log(LOG_DEBUG,
37262587Sitojun			    "cannot forward "
37362587Sitojun			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
37462587Sitojun			    ip6_sprintf(&ip6->ip6_src),
37562587Sitojun			    ip6_sprintf(&ip6->ip6_dst),
37662587Sitojun			    ip6->ip6_nxt,
37762587Sitojun			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
37862587Sitojun		}
37962587Sitojun		if (mcopy)
38062587Sitojun			icmp6_error(mcopy, ICMP6_DST_UNREACH,
38162587Sitojun				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
38262587Sitojun		m_freem(m);
38362587Sitojun		return;
38462587Sitojun	}
38562587Sitojun
38662587Sitojun	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
38753541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
38862587Sitojun		if (mcopy) {
38962587Sitojun			u_long mtu;
39063256Sitojun#ifdef IPSEC
39162587Sitojun			struct secpolicy *sp;
39262587Sitojun			int ipsecerror;
39362587Sitojun			size_t ipsechdrsiz;
39462587Sitojun#endif
39562587Sitojun
39662587Sitojun			mtu = rt->rt_ifp->if_mtu;
39763256Sitojun#ifdef IPSEC
39862587Sitojun			/*
39962587Sitojun			 * When we do IPsec tunnel ingress, we need to play
40062587Sitojun			 * with if_mtu value (decrement IPsec header size
40162587Sitojun			 * from mtu value).  The code is much simpler than v4
40262587Sitojun			 * case, as we have the outgoing interface for
40362587Sitojun			 * encapsulated packet as "rt->rt_ifp".
40462587Sitojun			 */
40562587Sitojun			sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
40662587Sitojun				IP_FORWARDING, &ipsecerror);
40762587Sitojun			if (sp) {
40862587Sitojun				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
40962587Sitojun					IPSEC_DIR_OUTBOUND, NULL);
41062587Sitojun				if (ipsechdrsiz < mtu)
41162587Sitojun					mtu -= ipsechdrsiz;
41262587Sitojun			}
41362587Sitojun
41462587Sitojun			/*
41562587Sitojun			 * if mtu becomes less than minimum MTU,
41662587Sitojun			 * tell minimum MTU (and I'll need to fragment it).
41762587Sitojun			 */
41862587Sitojun			if (mtu < IPV6_MMTU)
41962587Sitojun				mtu = IPV6_MMTU;
42062587Sitojun#endif
42162587Sitojun			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
42262587Sitojun		}
42362587Sitojun		m_freem(m);
42453541Sshin		return;
425120913Sume	}
42653541Sshin
42753541Sshin	if (rt->rt_flags & RTF_GATEWAY)
42853541Sshin		dst = (struct sockaddr_in6 *)rt->rt_gateway;
42953541Sshin
43053541Sshin	/*
43153541Sshin	 * If we are to forward the packet using the same interface
43253541Sshin	 * as one we got the packet from, perhaps we should send a redirect
43353541Sshin	 * to sender to shortcut a hop.
43453541Sshin	 * Only send redirect if source is sending directly to us,
43553541Sshin	 * and if packet was not source routed (or has any options).
43653541Sshin	 * Also, don't send redirect if forwarding using a route
43753541Sshin	 * modified by a redirect.
43853541Sshin	 */
43953541Sshin	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
44078064Sume	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
44178064Sume		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) {
44278064Sume			/*
44378064Sume			 * If the incoming interface is equal to the outgoing
44478064Sume			 * one, and the link attached to the interface is
44578064Sume			 * point-to-point, then it will be highly probable
44678064Sume			 * that a routing loop occurs. Thus, we immediately
44778064Sume			 * drop the packet and send an ICMPv6 error message.
44878064Sume			 *
44978064Sume			 * type/code is based on suggestion by Rich Draves.
45078064Sume			 * not sure if it is the best pick.
45178064Sume			 */
45278064Sume			icmp6_error(mcopy, ICMP6_DST_UNREACH,
45378064Sume				    ICMP6_DST_UNREACH_ADDR, 0);
45478064Sume			m_freem(m);
45578064Sume			return;
45678064Sume		}
45753541Sshin		type = ND_REDIRECT;
45878064Sume	}
45953541Sshin
46053541Sshin	/*
46153541Sshin	 * Check with the firewall...
46253541Sshin	 */
46366303Sume	if (ip6_fw_enable && ip6_fw_chk_ptr) {
46453541Sshin		u_short port = 0;
46553541Sshin		/* If ipfw says divert, we have to just drop packet */
46653541Sshin		if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
46753541Sshin			m_freem(m);
46853541Sshin			goto freecopy;
46953541Sshin		}
47053541Sshin		if (!m)
47153541Sshin			goto freecopy;
47253541Sshin	}
47353541Sshin
47462587Sitojun	/*
47562587Sitojun	 * Fake scoped addresses. Note that even link-local source or
47662587Sitojun	 * destinaion can appear, if the originating node just sends the
47762587Sitojun	 * packet to us (without address resolution for the destination).
47862587Sitojun	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
47978064Sume	 * link identifiers, we can do this stuff after making a copy for
48078064Sume	 * returning an error.
48162587Sitojun	 */
48262587Sitojun	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
48362587Sitojun		/*
48462587Sitojun		 * See corresponding comments in ip6_output.
48562587Sitojun		 * XXX: but is it possible that ip6_forward() sends a packet
48662587Sitojun		 *      to a loopback interface? I don't think so, and thus
48762587Sitojun		 *      I bark here. (jinmei@kame.net)
48862587Sitojun		 * XXX: it is common to route invalid packets to loopback.
48962587Sitojun		 *	also, the codepath will be visited on use of ::1 in
49062587Sitojun		 *	rthdr. (itojun)
49162587Sitojun		 */
49262587Sitojun#if 1
49362587Sitojun		if (0)
49462587Sitojun#else
49562587Sitojun		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
49662587Sitojun#endif
49762587Sitojun		{
49862587Sitojun			printf("ip6_forward: outgoing interface is loopback. "
499120913Sume			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
500120913Sume			       ip6_sprintf(&ip6->ip6_src),
501120913Sume			       ip6_sprintf(&ip6->ip6_dst),
502120913Sume			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
503120913Sume			       if_name(rt->rt_ifp));
50462587Sitojun		}
50562587Sitojun
50678064Sume		/* we can just use rcvif in forwarding. */
50778064Sume		origifp = m->m_pkthdr.rcvif;
50862587Sitojun	}
50962587Sitojun	else
51062587Sitojun		origifp = rt->rt_ifp;
51178064Sume#ifndef SCOPEDROUTING
51278064Sume	/*
51378064Sume	 * clear embedded scope identifiers if necessary.
51478064Sume	 * in6_clearscope will touch the addresses only when necessary.
51578064Sume	 */
51678064Sume	in6_clearscope(&ip6->ip6_src);
51778064Sume	in6_clearscope(&ip6->ip6_dst);
51862587Sitojun#endif
51962587Sitojun
52084994Sdarrenr#ifdef PFIL_HOOKS
52184994Sdarrenr	/*
52284994Sdarrenr	 * Run through list of hooks for output packets.
52384994Sdarrenr	 */
524120593Ssam	error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT);
525120593Ssam	if (error != 0)
526120593Ssam		goto senderr;
527120386Ssam	if (m == NULL)
528120386Ssam		goto freecopy;
529120386Ssam	ip6 = mtod(m, struct ip6_hdr *);
53084994Sdarrenr#endif /* PFIL_HOOKS */
53184994Sdarrenr
53262587Sitojun	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
53353541Sshin	if (error) {
53453541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
53553541Sshin		ip6stat.ip6s_cantforward++;
53653541Sshin	} else {
53753541Sshin		ip6stat.ip6s_forward++;
53853541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
53953541Sshin		if (type)
54053541Sshin			ip6stat.ip6s_redirectsent++;
54153541Sshin		else {
54253541Sshin			if (mcopy)
54353541Sshin				goto freecopy;
54453541Sshin		}
54553541Sshin	}
546120913Sume
547120593Ssam#ifdef PFIL_HOOKS
548120593Ssamsenderr:
549120593Ssam#endif
55053541Sshin	if (mcopy == NULL)
55153541Sshin		return;
55253541Sshin	switch (error) {
55353541Sshin	case 0:
55453541Sshin		if (type == ND_REDIRECT) {
55553541Sshin			icmp6_redirect_output(mcopy, rt);
55653541Sshin			return;
55753541Sshin		}
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