ip6_forward.c revision 62587
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_forward.c 62587 2000-07-04 16:35:15Z itojun $	*/
262587Sitojun/*	$KAME: ip6_forward.c,v 1.39 2000/07/03 13:23:28 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>
4053541Sshin#include <sys/mbuf.h>
4153541Sshin#include <sys/domain.h>
4253541Sshin#include <sys/protosw.h>
4353541Sshin#include <sys/socket.h>
4453541Sshin#include <sys/errno.h>
4553541Sshin#include <sys/time.h>
4653541Sshin#include <sys/syslog.h>
4753541Sshin
4853541Sshin#include <net/if.h>
4953541Sshin#include <net/route.h>
5053541Sshin
5153541Sshin#include <netinet/in.h>
5253541Sshin#include <netinet/in_var.h>
5362587Sitojun#include <netinet/ip_var.h>
5462587Sitojun#include <netinet/ip6.h>
5553541Sshin#include <netinet6/ip6_var.h>
5662587Sitojun#include <netinet/icmp6.h>
5753541Sshin#include <netinet6/nd6.h>
5853541Sshin
5953541Sshin#ifdef IPSEC_IPV6FWD
6053541Sshin#include <netinet6/ipsec.h>
6153541Sshin#include <netkey/key.h>
6253541Sshin#endif /* IPSEC_IPV6FWD */
6353541Sshin
6453541Sshin#ifdef IPV6FIREWALL
6553541Sshin#include <netinet6/ip6_fw.h>
6653541Sshin#endif
6753541Sshin
6853541Sshin#include <net/net_osdep.h>
6953541Sshin
7053541Sshinstruct	route_in6 ip6_forward_rt;
7153541Sshin
7253541Sshin/*
7353541Sshin * Forward a packet.  If some error occurs return the sender
7453541Sshin * an icmp packet.  Note we can't always generate a meaningful
7553541Sshin * icmp message because icmp doesn't have a large enough repertoire
7653541Sshin * of codes and types.
7753541Sshin *
7853541Sshin * If not forwarding, just drop the packet.  This could be confusing
7953541Sshin * if ipforwarding was zero but some routing protocol was advancing
8053541Sshin * us as a gateway to somewhere.  However, we must let the routing
8153541Sshin * protocol deal with that.
8253541Sshin *
8353541Sshin */
8453541Sshin
8553541Sshinvoid
8653541Sshinip6_forward(m, srcrt)
8753541Sshin	struct mbuf *m;
8853541Sshin	int srcrt;
8953541Sshin{
9053541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
9153541Sshin	register struct sockaddr_in6 *dst;
9253541Sshin	register struct rtentry *rt;
9353541Sshin	int error, type = 0, code = 0;
9462587Sitojun	struct mbuf *mcopy = NULL;
9562587Sitojun	struct ifnet *origifp;	/* maybe unnecessary */
9653541Sshin#ifdef IPSEC_IPV6FWD
9753541Sshin	struct secpolicy *sp = NULL;
9853541Sshin#endif
9953541Sshin
10053541Sshin#ifdef IPSEC_IPV6FWD
10153541Sshin	/*
10253541Sshin	 * Check AH/ESP integrity.
10353541Sshin	 */
10453541Sshin	/*
10553541Sshin	 * Don't increment ip6s_cantforward because this is the check
10653541Sshin	 * before forwarding packet actually.
10753541Sshin	 */
10853541Sshin	if (ipsec6_in_reject(m, NULL)) {
10953541Sshin		ipsec6stat.in_polvio++;
11053541Sshin		m_freem(m);
11153541Sshin		return;
11253541Sshin	}
11353541Sshin#endif /*IPSEC_IPV6FWD*/
11453541Sshin
11562587Sitojun	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
11662587Sitojun	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
11753541Sshin		ip6stat.ip6s_cantforward++;
11853541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
11953541Sshin		if (ip6_log_time + ip6_log_interval < time_second) {
12053541Sshin			ip6_log_time = time_second;
12153541Sshin			log(LOG_DEBUG,
12253541Sshin			    "cannot forward "
12353541Sshin			    "from %s to %s nxt %d received on %s\n",
12462587Sitojun			    ip6_sprintf(&ip6->ip6_src),
12562587Sitojun			    ip6_sprintf(&ip6->ip6_dst),
12653541Sshin			    ip6->ip6_nxt,
12753541Sshin			    if_name(m->m_pkthdr.rcvif));
12853541Sshin		}
12953541Sshin		m_freem(m);
13053541Sshin		return;
13153541Sshin	}
13253541Sshin
13353541Sshin	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
13453541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
13553541Sshin		icmp6_error(m, ICMP6_TIME_EXCEEDED,
13653541Sshin				ICMP6_TIME_EXCEED_TRANSIT, 0);
13753541Sshin		return;
13853541Sshin	}
13953541Sshin	ip6->ip6_hlim -= IPV6_HLIMDEC;
14053541Sshin
14162587Sitojun	/*
14262587Sitojun	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
14362587Sitojun	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
14462587Sitojun	 * we need to generate an ICMP6 message to the src.
14562587Sitojun	 * Thanks to M_EXT, in most cases copy will not occur.
14662587Sitojun	 *
14762587Sitojun	 * It is important to save it before IPsec processing as IPsec
14862587Sitojun	 * processing may modify the mbuf.
14962587Sitojun	 */
15062587Sitojun	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
15162587Sitojun
15253541Sshin#ifdef IPSEC_IPV6FWD
15353541Sshin	/* get a security policy for this packet */
15453541Sshin	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
15553541Sshin	if (sp == NULL) {
15653541Sshin		ipsec6stat.out_inval++;
15753541Sshin		ip6stat.ip6s_cantforward++;
15862587Sitojun		if (mcopy) {
15962587Sitojun#if 0
16062587Sitojun			/* XXX: what icmp ? */
16162587Sitojun#else
16262587Sitojun			m_freem(mcopy);
16362587Sitojun#endif
16462587Sitojun		}
16553541Sshin		m_freem(m);
16653541Sshin		return;
16753541Sshin	}
16853541Sshin
16953541Sshin	error = 0;
17053541Sshin
17153541Sshin	/* check policy */
17253541Sshin	switch (sp->policy) {
17353541Sshin	case IPSEC_POLICY_DISCARD:
17453541Sshin		/*
17553541Sshin		 * This packet is just discarded.
17653541Sshin		 */
17753541Sshin		ipsec6stat.out_polvio++;
17853541Sshin		ip6stat.ip6s_cantforward++;
17953541Sshin		key_freesp(sp);
18062587Sitojun		if (mcopy) {
18162587Sitojun#if 0
18262587Sitojun			/* XXX: what icmp ? */
18362587Sitojun#else
18462587Sitojun			m_freem(mcopy);
18562587Sitojun#endif
18662587Sitojun		}
18753541Sshin		m_freem(m);
18853541Sshin		return;
18953541Sshin
19053541Sshin	case IPSEC_POLICY_BYPASS:
19153541Sshin	case IPSEC_POLICY_NONE:
19253541Sshin		/* no need to do IPsec. */
19353541Sshin		key_freesp(sp);
19453541Sshin		goto skip_ipsec;
19562587Sitojun
19653541Sshin	case IPSEC_POLICY_IPSEC:
19753541Sshin		if (sp->req == NULL) {
19853541Sshin			/* XXX should be panic ? */
19953541Sshin			printf("ip6_forward: No IPsec request specified.\n");
20053541Sshin			ip6stat.ip6s_cantforward++;
20153541Sshin			key_freesp(sp);
20262587Sitojun			if (mcopy) {
20362587Sitojun#if 0
20462587Sitojun				/* XXX: what icmp ? */
20562587Sitojun#else
20662587Sitojun				m_freem(mcopy);
20762587Sitojun#endif
20862587Sitojun			}
20953541Sshin			m_freem(m);
21053541Sshin			return;
21153541Sshin		}
21253541Sshin		/* do IPsec */
21353541Sshin		break;
21453541Sshin
21553541Sshin	case IPSEC_POLICY_ENTRUST:
21653541Sshin	default:
21753541Sshin		/* should be panic ?? */
21853541Sshin		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
21953541Sshin		key_freesp(sp);
22053541Sshin		goto skip_ipsec;
22153541Sshin	}
22253541Sshin
22353541Sshin    {
22453541Sshin	struct ipsec_output_state state;
22553541Sshin
22653541Sshin	/*
22753541Sshin	 * All the extension headers will become inaccessible
22853541Sshin	 * (since they can be encrypted).
22953541Sshin	 * Don't panic, we need no more updates to extension headers
23053541Sshin	 * on inner IPv6 packet (since they are now encapsulated).
23153541Sshin	 *
23253541Sshin	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
23353541Sshin	 */
23453541Sshin	bzero(&state, sizeof(state));
23553541Sshin	state.m = m;
23653541Sshin	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
23753541Sshin	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
23853541Sshin
23953541Sshin	error = ipsec6_output_tunnel(&state, sp, 0);
24053541Sshin
24153541Sshin	m = state.m;
24262587Sitojun#if 0	/* XXX allocate a route (ro, dst) again later */
24362587Sitojun	ro = (struct route_in6 *)state.ro;
24462587Sitojun	dst = (struct sockaddr_in6 *)state.dst;
24562587Sitojun#endif
24653541Sshin	key_freesp(sp);
24753541Sshin
24853541Sshin	if (error) {
24953541Sshin		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
25053541Sshin		switch (error) {
25153541Sshin		case EHOSTUNREACH:
25253541Sshin		case ENETUNREACH:
25353541Sshin		case EMSGSIZE:
25453541Sshin		case ENOBUFS:
25553541Sshin		case ENOMEM:
25653541Sshin			break;
25753541Sshin		default:
25853541Sshin			printf("ip6_output (ipsec): error code %d\n", error);
25953541Sshin			/*fall through*/
26053541Sshin		case ENOENT:
26153541Sshin			/* don't show these error codes to the user */
26253541Sshin			break;
26353541Sshin		}
26453541Sshin		ip6stat.ip6s_cantforward++;
26562587Sitojun		if (mcopy) {
26662587Sitojun#if 0
26762587Sitojun			/* XXX: what icmp ? */
26862587Sitojun#else
26962587Sitojun			m_freem(mcopy);
27062587Sitojun#endif
27162587Sitojun		}
27253541Sshin		m_freem(m);
27353541Sshin		return;
27453541Sshin	}
27553541Sshin    }
27653541Sshin    skip_ipsec:
27753541Sshin#endif /* IPSEC_IPV6FWD */
27853541Sshin
27953541Sshin	dst = &ip6_forward_rt.ro_dst;
28053541Sshin	if (!srcrt) {
28153541Sshin		/*
28253541Sshin		 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
28353541Sshin		 */
28453541Sshin		if (ip6_forward_rt.ro_rt == 0 ||
28553541Sshin		    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
28653541Sshin			if (ip6_forward_rt.ro_rt) {
28753541Sshin				RTFREE(ip6_forward_rt.ro_rt);
28853541Sshin				ip6_forward_rt.ro_rt = 0;
28953541Sshin			}
29053541Sshin			/* this probably fails but give it a try again */
29153541Sshin			rtalloc_ign((struct route *)&ip6_forward_rt,
29253541Sshin				    RTF_PRCLONING);
29353541Sshin		}
29462587Sitojun
29553541Sshin		if (ip6_forward_rt.ro_rt == 0) {
29653541Sshin			ip6stat.ip6s_noroute++;
29753541Sshin			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
29862587Sitojun			if (mcopy) {
29962587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
30062587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
30162587Sitojun			}
30262587Sitojun			m_freem(m);
30353541Sshin			return;
30453541Sshin		}
30553541Sshin	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
30653541Sshin		 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
30753541Sshin		if (ip6_forward_rt.ro_rt) {
30853541Sshin			RTFREE(ip6_forward_rt.ro_rt);
30953541Sshin			ip6_forward_rt.ro_rt = 0;
31053541Sshin		}
31153541Sshin		bzero(dst, sizeof(*dst));
31253541Sshin		dst->sin6_len = sizeof(struct sockaddr_in6);
31353541Sshin		dst->sin6_family = AF_INET6;
31453541Sshin		dst->sin6_addr = ip6->ip6_dst;
31553541Sshin
31653541Sshin  		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
31753541Sshin		if (ip6_forward_rt.ro_rt == 0) {
31853541Sshin			ip6stat.ip6s_noroute++;
31953541Sshin			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
32062587Sitojun			if (mcopy) {
32162587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
32262587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
32362587Sitojun			}
32462587Sitojun			m_freem(m);
32553541Sshin			return;
32653541Sshin		}
32753541Sshin	}
32853541Sshin	rt = ip6_forward_rt.ro_rt;
32962587Sitojun
33062587Sitojun	/*
33162587Sitojun	 * Scope check: if a packet can't be delivered to its destination
33262587Sitojun	 * for the reason that the destination is beyond the scope of the
33362587Sitojun	 * source address, discard the packet and return an icmp6 destination
33462587Sitojun	 * unreachable error with Code 2 (beyond scope of source address).
33562587Sitojun	 * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1]
33662587Sitojun	 */
33762587Sitojun	if (in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_src) !=
33862587Sitojun	    in6_addr2scopeid(rt->rt_ifp, &ip6->ip6_src)) {
33962587Sitojun		ip6stat.ip6s_cantforward++;
34062587Sitojun		ip6stat.ip6s_badscope++;
34162587Sitojun		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
34262587Sitojun
34362587Sitojun		if (ip6_log_time + ip6_log_interval < time_second) {
34462587Sitojun			ip6_log_time = time_second;
34562587Sitojun			log(LOG_DEBUG,
34662587Sitojun			    "cannot forward "
34762587Sitojun			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
34862587Sitojun			    ip6_sprintf(&ip6->ip6_src),
34962587Sitojun			    ip6_sprintf(&ip6->ip6_dst),
35062587Sitojun			    ip6->ip6_nxt,
35162587Sitojun			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
35262587Sitojun		}
35362587Sitojun		if (mcopy)
35462587Sitojun			icmp6_error(mcopy, ICMP6_DST_UNREACH,
35562587Sitojun				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
35662587Sitojun		m_freem(m);
35762587Sitojun		return;
35862587Sitojun	}
35962587Sitojun
36062587Sitojun	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
36153541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
36262587Sitojun		if (mcopy) {
36362587Sitojun			u_long mtu;
36462587Sitojun#ifdef IPSEC_IPV6FWD
36562587Sitojun			struct secpolicy *sp;
36662587Sitojun			int ipsecerror;
36762587Sitojun			size_t ipsechdrsiz;
36862587Sitojun#endif
36962587Sitojun
37062587Sitojun			mtu = rt->rt_ifp->if_mtu;
37162587Sitojun#ifdef IPSEC_IPV6FWD
37262587Sitojun			/*
37362587Sitojun			 * When we do IPsec tunnel ingress, we need to play
37462587Sitojun			 * with if_mtu value (decrement IPsec header size
37562587Sitojun			 * from mtu value).  The code is much simpler than v4
37662587Sitojun			 * case, as we have the outgoing interface for
37762587Sitojun			 * encapsulated packet as "rt->rt_ifp".
37862587Sitojun			 */
37962587Sitojun			sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
38062587Sitojun				IP_FORWARDING, &ipsecerror);
38162587Sitojun			if (sp) {
38262587Sitojun				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
38362587Sitojun					IPSEC_DIR_OUTBOUND, NULL);
38462587Sitojun				if (ipsechdrsiz < mtu)
38562587Sitojun					mtu -= ipsechdrsiz;
38662587Sitojun			}
38762587Sitojun
38862587Sitojun			/*
38962587Sitojun			 * if mtu becomes less than minimum MTU,
39062587Sitojun			 * tell minimum MTU (and I'll need to fragment it).
39162587Sitojun			 */
39262587Sitojun			if (mtu < IPV6_MMTU)
39362587Sitojun				mtu = IPV6_MMTU;
39462587Sitojun#endif
39562587Sitojun			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
39662587Sitojun		}
39762587Sitojun		m_freem(m);
39853541Sshin		return;
39953541Sshin 	}
40053541Sshin
40153541Sshin	if (rt->rt_flags & RTF_GATEWAY)
40253541Sshin		dst = (struct sockaddr_in6 *)rt->rt_gateway;
40353541Sshin
40453541Sshin	/*
40553541Sshin	 * If we are to forward the packet using the same interface
40653541Sshin	 * as one we got the packet from, perhaps we should send a redirect
40753541Sshin	 * to sender to shortcut a hop.
40853541Sshin	 * Only send redirect if source is sending directly to us,
40953541Sshin	 * and if packet was not source routed (or has any options).
41053541Sshin	 * Also, don't send redirect if forwarding using a route
41153541Sshin	 * modified by a redirect.
41253541Sshin	 */
41353541Sshin	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
41453541Sshin	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0)
41553541Sshin		type = ND_REDIRECT;
41653541Sshin
41753541Sshin#ifdef IPV6FIREWALL
41853541Sshin	/*
41953541Sshin	 * Check with the firewall...
42053541Sshin	 */
42153541Sshin	if (ip6_fw_chk_ptr) {
42253541Sshin		u_short port = 0;
42353541Sshin		/* If ipfw says divert, we have to just drop packet */
42453541Sshin		if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
42553541Sshin			m_freem(m);
42653541Sshin			goto freecopy;
42753541Sshin		}
42853541Sshin		if (!m)
42953541Sshin			goto freecopy;
43053541Sshin	}
43153541Sshin#endif
43253541Sshin
43362587Sitojun	/*
43462587Sitojun	 * Fake scoped addresses. Note that even link-local source or
43562587Sitojun	 * destinaion can appear, if the originating node just sends the
43662587Sitojun	 * packet to us (without address resolution for the destination).
43762587Sitojun	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
43862587Sitojun	 * link identifiers, we can do this stuff after make a copy for
43962587Sitojun	 * returning error.
44062587Sitojun	 */
44162587Sitojun	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
44262587Sitojun		/*
44362587Sitojun		 * See corresponding comments in ip6_output.
44462587Sitojun		 * XXX: but is it possible that ip6_forward() sends a packet
44562587Sitojun		 *      to a loopback interface? I don't think so, and thus
44662587Sitojun		 *      I bark here. (jinmei@kame.net)
44762587Sitojun		 * XXX: it is common to route invalid packets to loopback.
44862587Sitojun		 *	also, the codepath will be visited on use of ::1 in
44962587Sitojun		 *	rthdr. (itojun)
45062587Sitojun		 */
45162587Sitojun#if 1
45262587Sitojun		if (0)
45362587Sitojun#else
45462587Sitojun		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
45562587Sitojun#endif
45662587Sitojun		{
45762587Sitojun			printf("ip6_forward: outgoing interface is loopback. "
45862587Sitojun			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
45962587Sitojun			       ip6_sprintf(&ip6->ip6_src),
46062587Sitojun			       ip6_sprintf(&ip6->ip6_dst),
46162587Sitojun			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
46262587Sitojun			       if_name(rt->rt_ifp));
46362587Sitojun		}
46462587Sitojun
46562587Sitojun		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
46662587Sitojun			origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
46762587Sitojun		else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
46862587Sitojun			origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
46962587Sitojun		else
47062587Sitojun			origifp = rt->rt_ifp;
47162587Sitojun	}
47262587Sitojun	else
47362587Sitojun		origifp = rt->rt_ifp;
47462587Sitojun#ifndef FAKE_LOOPBACK_IF
47562587Sitojun	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
47662587Sitojun#else
47762587Sitojun	if (1)
47862587Sitojun#endif
47962587Sitojun	{
48062587Sitojun		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
48162587Sitojun			ip6->ip6_src.s6_addr16[1] = 0;
48262587Sitojun		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
48362587Sitojun			ip6->ip6_dst.s6_addr16[1] = 0;
48462587Sitojun	}
48562587Sitojun
48662587Sitojun#ifdef OLDIP6OUTPUT
48762587Sitojun	error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m,
48862587Sitojun					 (struct sockaddr *)dst,
48962587Sitojun					 ip6_forward_rt.ro_rt);
49062587Sitojun#else
49162587Sitojun	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
49262587Sitojun#endif
49353541Sshin	if (error) {
49453541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
49553541Sshin		ip6stat.ip6s_cantforward++;
49653541Sshin	} else {
49753541Sshin		ip6stat.ip6s_forward++;
49853541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
49953541Sshin		if (type)
50053541Sshin			ip6stat.ip6s_redirectsent++;
50153541Sshin		else {
50253541Sshin			if (mcopy)
50353541Sshin				goto freecopy;
50453541Sshin		}
50553541Sshin	}
50653541Sshin	if (mcopy == NULL)
50753541Sshin		return;
50853541Sshin
50953541Sshin	switch (error) {
51053541Sshin	case 0:
51162587Sitojun#if 1
51253541Sshin		if (type == ND_REDIRECT) {
51353541Sshin			icmp6_redirect_output(mcopy, rt);
51453541Sshin			return;
51553541Sshin		}
51662587Sitojun#endif
51753541Sshin		goto freecopy;
51853541Sshin
51953541Sshin	case EMSGSIZE:
52053541Sshin		/* xxx MTU is constant in PPP? */
52153541Sshin		goto freecopy;
52253541Sshin
52353541Sshin	case ENOBUFS:
52453541Sshin		/* Tell source to slow down like source quench in IP? */
52553541Sshin		goto freecopy;
52653541Sshin
52753541Sshin	case ENETUNREACH:	/* shouldn't happen, checked above */
52853541Sshin	case EHOSTUNREACH:
52953541Sshin	case ENETDOWN:
53053541Sshin	case EHOSTDOWN:
53153541Sshin	default:
53253541Sshin		type = ICMP6_DST_UNREACH;
53353541Sshin		code = ICMP6_DST_UNREACH_ADDR;
53453541Sshin		break;
53553541Sshin	}
53653541Sshin	icmp6_error(mcopy, type, code, 0);
53753541Sshin	return;
53853541Sshin
53953541Sshin freecopy:
54053541Sshin	m_freem(mcopy);
54153541Sshin	return;
54253541Sshin}
543