ip6_forward.c revision 165118
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_forward.c 165118 2006-12-12 12:17:58Z bz $	*/
278064Sume/*	$KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $	*/
362587Sitojun
4139826Simp/*-
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_inet.h"
3462587Sitojun#include "opt_inet6.h"
3555009Sshin#include "opt_ipsec.h"
36148921Ssuz#include "opt_ipstealth.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#include <net/pfil.h>
5353541Sshin
5453541Sshin#include <netinet/in.h>
5553541Sshin#include <netinet/in_var.h>
5678064Sume#include <netinet/in_systm.h>
5778064Sume#include <netinet/ip.h>
5862587Sitojun#include <netinet/ip_var.h>
5978064Sume#include <netinet6/in6_var.h>
6062587Sitojun#include <netinet/ip6.h>
6153541Sshin#include <netinet6/ip6_var.h>
62148385Sume#include <netinet6/scope6_var.h>
6362587Sitojun#include <netinet/icmp6.h>
6453541Sshin#include <netinet6/nd6.h>
6553541Sshin
6678064Sume#include <netinet/in_pcb.h>
6778064Sume
6863256Sitojun#ifdef IPSEC
6953541Sshin#include <netinet6/ipsec.h>
7078064Sume#ifdef INET6
7162601Sitojun#include <netinet6/ipsec6.h>
7278064Sume#endif
7353541Sshin#include <netkey/key.h>
7463256Sitojun#endif /* IPSEC */
7553541Sshin
76105199Ssam#ifdef FAST_IPSEC
77105199Ssam#include <netipsec/ipsec.h>
78105199Ssam#include <netipsec/ipsec6.h>
79105199Ssam#include <netipsec/key.h>
80105199Ssam#define	IPSEC
81105199Ssam#endif /* FAST_IPSEC */
82105199Ssam
8384994Sdarrenr#include <netinet6/ip6protosw.h>
8484994Sdarrenr
8553541Sshinstruct	route_in6 ip6_forward_rt;
8653541Sshin
8753541Sshin/*
8853541Sshin * Forward a packet.  If some error occurs return the sender
8953541Sshin * an icmp packet.  Note we can't always generate a meaningful
9053541Sshin * icmp message because icmp doesn't have a large enough repertoire
9153541Sshin * of codes and types.
9253541Sshin *
9353541Sshin * If not forwarding, just drop the packet.  This could be confusing
9453541Sshin * if ipforwarding was zero but some routing protocol was advancing
9553541Sshin * us as a gateway to somewhere.  However, we must let the routing
9653541Sshin * protocol deal with that.
9753541Sshin *
9853541Sshin */
9953541Sshin
10053541Sshinvoid
10153541Sshinip6_forward(m, srcrt)
10253541Sshin	struct mbuf *m;
10353541Sshin	int srcrt;
10453541Sshin{
10553541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
106122062Sume	struct sockaddr_in6 *dst = NULL;
107122062Sume	struct rtentry *rt = NULL;
10853541Sshin	int error, type = 0, code = 0;
10962587Sitojun	struct mbuf *mcopy = NULL;
11062587Sitojun	struct ifnet *origifp;	/* maybe unnecessary */
111148385Sume	u_int32_t inzone, outzone;
112148385Sume	struct in6_addr src_in6, dst_in6;
11363256Sitojun#ifdef IPSEC
11453541Sshin	struct secpolicy *sp = NULL;
115122062Sume	int ipsecrt = 0;
11653541Sshin#endif
117165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
11853541Sshin
119158295Sbz	GIANT_REQUIRED; /* XXX bz: ip6_forward_rt */
120158295Sbz
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",
154165118Sbz			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
155165118Sbz			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
15653541Sshin			    ip6->ip6_nxt,
15753541Sshin			    if_name(m->m_pkthdr.rcvif));
15853541Sshin		}
15953541Sshin		m_freem(m);
16053541Sshin		return;
16153541Sshin	}
16253541Sshin
163148921Ssuz#ifdef IPSTEALTH
164148921Ssuz	if (!ip6stealth) {
165148921Ssuz#endif
16653541Sshin	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
16753541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
16853541Sshin		icmp6_error(m, ICMP6_TIME_EXCEEDED,
16953541Sshin				ICMP6_TIME_EXCEED_TRANSIT, 0);
17053541Sshin		return;
17153541Sshin	}
17253541Sshin	ip6->ip6_hlim -= IPV6_HLIMDEC;
17353541Sshin
174148921Ssuz#ifdef IPSTEALTH
175148921Ssuz	}
176148921Ssuz#endif
177148921Ssuz
17862587Sitojun	/*
17962587Sitojun	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
18062587Sitojun	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
18162587Sitojun	 * we need to generate an ICMP6 message to the src.
18262587Sitojun	 * Thanks to M_EXT, in most cases copy will not occur.
18362587Sitojun	 *
18462587Sitojun	 * It is important to save it before IPsec processing as IPsec
18562587Sitojun	 * processing may modify the mbuf.
18662587Sitojun	 */
18762587Sitojun	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
18862587Sitojun
18963256Sitojun#ifdef IPSEC
19053541Sshin	/* get a security policy for this packet */
191120913Sume	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
192120913Sume	    IP_FORWARDING, &error);
19353541Sshin	if (sp == NULL) {
19453541Sshin		ipsec6stat.out_inval++;
19553541Sshin		ip6stat.ip6s_cantforward++;
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
20753541Sshin	error = 0;
20853541Sshin
20953541Sshin	/* check policy */
21053541Sshin	switch (sp->policy) {
21153541Sshin	case IPSEC_POLICY_DISCARD:
21253541Sshin		/*
21353541Sshin		 * This packet is just discarded.
21453541Sshin		 */
21553541Sshin		ipsec6stat.out_polvio++;
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	case IPSEC_POLICY_BYPASS:
22953541Sshin	case IPSEC_POLICY_NONE:
23053541Sshin		/* no need to do IPsec. */
23153541Sshin		key_freesp(sp);
23253541Sshin		goto skip_ipsec;
23362587Sitojun
23453541Sshin	case IPSEC_POLICY_IPSEC:
23553541Sshin		if (sp->req == NULL) {
23653541Sshin			/* XXX should be panic ? */
23753541Sshin			printf("ip6_forward: No IPsec request specified.\n");
23853541Sshin			ip6stat.ip6s_cantforward++;
23953541Sshin			key_freesp(sp);
24062587Sitojun			if (mcopy) {
24162587Sitojun#if 0
24262587Sitojun				/* XXX: what icmp ? */
24362587Sitojun#else
24462587Sitojun				m_freem(mcopy);
24562587Sitojun#endif
24662587Sitojun			}
24753541Sshin			m_freem(m);
24853541Sshin			return;
24953541Sshin		}
25053541Sshin		/* do IPsec */
25153541Sshin		break;
25253541Sshin
25353541Sshin	case IPSEC_POLICY_ENTRUST:
25453541Sshin	default:
25553541Sshin		/* should be panic ?? */
25653541Sshin		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
25753541Sshin		key_freesp(sp);
25853541Sshin		goto skip_ipsec;
25953541Sshin	}
26053541Sshin
26153541Sshin    {
262122062Sume	struct ipsecrequest *isr = NULL;
26353541Sshin	struct ipsec_output_state state;
26453541Sshin
26553541Sshin	/*
266122062Sume	 * when the kernel forwards a packet, it is not proper to apply
267122062Sume	 * IPsec transport mode to the packet is not proper.  this check
268122062Sume	 * avoid from this.
269122062Sume	 * at present, if there is even a transport mode SA request in the
270122062Sume	 * security policy, the kernel does not apply IPsec to the packet.
271122062Sume	 * this check is not enough because the following case is valid.
272122062Sume	 *      ipsec esp/tunnel/xxx-xxx/require esp/transport//require;
273122062Sume	 */
274122062Sume	for (isr = sp->req; isr; isr = isr->next) {
275126006Sume		if (isr->saidx.mode == IPSEC_MODE_ANY)
276126006Sume			goto doipsectunnel;
277126006Sume		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
278126006Sume			goto doipsectunnel;
279122062Sume	}
280122062Sume
281122062Sume	/*
282126006Sume	 * if there's no need for tunnel mode IPsec, skip.
283126006Sume	 */
284126006Sume	if (!isr)
285126006Sume		goto skip_ipsec;
286126006Sume
287126006Sume    doipsectunnel:
288126006Sume	/*
28953541Sshin	 * All the extension headers will become inaccessible
29053541Sshin	 * (since they can be encrypted).
29153541Sshin	 * Don't panic, we need no more updates to extension headers
29253541Sshin	 * on inner IPv6 packet (since they are now encapsulated).
29353541Sshin	 *
29453541Sshin	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
29553541Sshin	 */
29653541Sshin	bzero(&state, sizeof(state));
29753541Sshin	state.m = m;
29853541Sshin	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
29953541Sshin	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
30053541Sshin
30153541Sshin	error = ipsec6_output_tunnel(&state, sp, 0);
30253541Sshin
30353541Sshin	m = state.m;
30453541Sshin	key_freesp(sp);
30553541Sshin
30653541Sshin	if (error) {
30753541Sshin		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
30853541Sshin		switch (error) {
30953541Sshin		case EHOSTUNREACH:
31053541Sshin		case ENETUNREACH:
31153541Sshin		case EMSGSIZE:
31253541Sshin		case ENOBUFS:
31353541Sshin		case ENOMEM:
31453541Sshin			break;
31553541Sshin		default:
31653541Sshin			printf("ip6_output (ipsec): error code %d\n", error);
317120913Sume			/* FALLTHROUGH */
31853541Sshin		case ENOENT:
31953541Sshin			/* don't show these error codes to the user */
32053541Sshin			break;
32153541Sshin		}
32253541Sshin		ip6stat.ip6s_cantforward++;
32362587Sitojun		if (mcopy) {
32462587Sitojun#if 0
32562587Sitojun			/* XXX: what icmp ? */
32662587Sitojun#else
32762587Sitojun			m_freem(mcopy);
32862587Sitojun#endif
32962587Sitojun		}
33053541Sshin		m_freem(m);
33153541Sshin		return;
33253541Sshin	}
333122062Sume
334126006Sume	if (ip6 != mtod(m, struct ip6_hdr *)) {
335126006Sume		/*
336126006Sume		 * now tunnel mode headers are added.  we are originating
337126006Sume		 * packet instead of forwarding the packet.
338126006Sume		 */
339126006Sume		ip6_output(m, NULL, NULL, IPV6_FORWARDING/*XXX*/, NULL, NULL,
340126006Sume		    NULL);
341126006Sume		goto freecopy;
342126006Sume	}
343126006Sume
344122062Sume	/* adjust pointer */
345122062Sume	dst = (struct sockaddr_in6 *)state.dst;
346122062Sume	rt = state.ro ? state.ro->ro_rt : NULL;
347122062Sume	if (dst != NULL && rt != NULL)
348122062Sume		ipsecrt = 1;
34953541Sshin    }
35053541Sshin    skip_ipsec:
35163256Sitojun#endif /* IPSEC */
35253541Sshin
353122062Sume#ifdef IPSEC
354122062Sume	if (ipsecrt)
355122062Sume		goto skip_routing;
356122062Sume#endif
357122062Sume
35878064Sume	dst = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
35953541Sshin	if (!srcrt) {
360120913Sume		/* ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst */
36153541Sshin		if (ip6_forward_rt.ro_rt == 0 ||
36253541Sshin		    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
36353541Sshin			if (ip6_forward_rt.ro_rt) {
36453541Sshin				RTFREE(ip6_forward_rt.ro_rt);
36553541Sshin				ip6_forward_rt.ro_rt = 0;
36653541Sshin			}
367120913Sume
36853541Sshin			/* this probably fails but give it a try again */
369122921Sandre			rtalloc((struct route *)&ip6_forward_rt);
37053541Sshin		}
37162587Sitojun
37253541Sshin		if (ip6_forward_rt.ro_rt == 0) {
37353541Sshin			ip6stat.ip6s_noroute++;
37478064Sume			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
37562587Sitojun			if (mcopy) {
37662587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
37762587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
37862587Sitojun			}
37962587Sitojun			m_freem(m);
38053541Sshin			return;
38153541Sshin		}
38253541Sshin	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
383120913Sume		   !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
38453541Sshin		if (ip6_forward_rt.ro_rt) {
38553541Sshin			RTFREE(ip6_forward_rt.ro_rt);
38653541Sshin			ip6_forward_rt.ro_rt = 0;
38753541Sshin		}
38853541Sshin		bzero(dst, sizeof(*dst));
38953541Sshin		dst->sin6_len = sizeof(struct sockaddr_in6);
39053541Sshin		dst->sin6_family = AF_INET6;
39153541Sshin		dst->sin6_addr = ip6->ip6_dst;
39253541Sshin
393122921Sandre  		rtalloc((struct route *)&ip6_forward_rt);
39453541Sshin		if (ip6_forward_rt.ro_rt == 0) {
39553541Sshin			ip6stat.ip6s_noroute++;
39678064Sume			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
39762587Sitojun			if (mcopy) {
39862587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
39962587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
40062587Sitojun			}
40162587Sitojun			m_freem(m);
40253541Sshin			return;
40353541Sshin		}
40453541Sshin	}
40553541Sshin	rt = ip6_forward_rt.ro_rt;
406122062Sume#ifdef IPSEC
407122062Sume    skip_routing:;
408122062Sume#endif
40962587Sitojun
41062587Sitojun	/*
411148385Sume	 * Source scope check: if a packet can't be delivered to its
412148385Sume	 * destination for the reason that the destination is beyond the scope
413148385Sume	 * of the source address, discard the packet and return an icmp6
414148385Sume	 * destination unreachable error with Code 2 (beyond scope of source
415148385Sume	 * address).  We use a local copy of ip6_src, since in6_setscope()
416148385Sume	 * will possibly modify its first argument.
417148385Sume	 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1]
41862587Sitojun	 */
419148385Sume	src_in6 = ip6->ip6_src;
420148385Sume	if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
421122062Sume		/* XXX: this should not happen */
42262587Sitojun		ip6stat.ip6s_cantforward++;
42362587Sitojun		ip6stat.ip6s_badscope++;
424122062Sume		m_freem(m);
425122062Sume		return;
426122062Sume	}
427148385Sume	if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
428148385Sume		ip6stat.ip6s_cantforward++;
429148385Sume		ip6stat.ip6s_badscope++;
430148385Sume		m_freem(m);
431148385Sume		return;
432148385Sume	}
433148385Sume	if (inzone != outzone
434122062Sume#ifdef IPSEC
435122062Sume	    && !ipsecrt
436122062Sume#endif
437122062Sume	    ) {
438122062Sume		ip6stat.ip6s_cantforward++;
439122062Sume		ip6stat.ip6s_badscope++;
44062587Sitojun		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
44162587Sitojun
44262587Sitojun		if (ip6_log_time + ip6_log_interval < time_second) {
44362587Sitojun			ip6_log_time = time_second;
44462587Sitojun			log(LOG_DEBUG,
44562587Sitojun			    "cannot forward "
44662587Sitojun			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
447165118Sbz			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
448165118Sbz			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
44962587Sitojun			    ip6->ip6_nxt,
45062587Sitojun			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
45162587Sitojun		}
45262587Sitojun		if (mcopy)
45362587Sitojun			icmp6_error(mcopy, ICMP6_DST_UNREACH,
45462587Sitojun				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
45562587Sitojun		m_freem(m);
45662587Sitojun		return;
45762587Sitojun	}
45862587Sitojun
459148385Sume	/*
460148385Sume	 * Destination scope check: if a packet is going to break the scope
461148385Sume	 * zone of packet's destination address, discard it.  This case should
462148385Sume	 * usually be prevented by appropriately-configured routing table, but
463148385Sume	 * we need an explicit check because we may mistakenly forward the
464148385Sume	 * packet to a different zone by (e.g.) a default route.
465148385Sume	 */
466148385Sume	dst_in6 = ip6->ip6_dst;
467148385Sume	if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
468148385Sume	    in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
469148385Sume	    inzone != outzone) {
470148385Sume		ip6stat.ip6s_cantforward++;
471148385Sume		ip6stat.ip6s_badscope++;
472148385Sume		m_freem(m);
473148385Sume		return;
474148385Sume	}
475148385Sume
476121283Sume	if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
47753541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
47862587Sitojun		if (mcopy) {
47962587Sitojun			u_long mtu;
48063256Sitojun#ifdef IPSEC
48162587Sitojun			struct secpolicy *sp;
48262587Sitojun			int ipsecerror;
48362587Sitojun			size_t ipsechdrsiz;
48462587Sitojun#endif
48562587Sitojun
486121283Sume			mtu = IN6_LINKMTU(rt->rt_ifp);
48763256Sitojun#ifdef IPSEC
48862587Sitojun			/*
48962587Sitojun			 * When we do IPsec tunnel ingress, we need to play
490122062Sume			 * with the link value (decrement IPsec header size
49162587Sitojun			 * from mtu value).  The code is much simpler than v4
49262587Sitojun			 * case, as we have the outgoing interface for
49362587Sitojun			 * encapsulated packet as "rt->rt_ifp".
49462587Sitojun			 */
49562587Sitojun			sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
49662587Sitojun				IP_FORWARDING, &ipsecerror);
49762587Sitojun			if (sp) {
49862587Sitojun				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
49962587Sitojun					IPSEC_DIR_OUTBOUND, NULL);
50062587Sitojun				if (ipsechdrsiz < mtu)
50162587Sitojun					mtu -= ipsechdrsiz;
50262587Sitojun			}
50362587Sitojun
50462587Sitojun			/*
50562587Sitojun			 * if mtu becomes less than minimum MTU,
50662587Sitojun			 * tell minimum MTU (and I'll need to fragment it).
50762587Sitojun			 */
50862587Sitojun			if (mtu < IPV6_MMTU)
50962587Sitojun				mtu = IPV6_MMTU;
51062587Sitojun#endif
51162587Sitojun			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
51262587Sitojun		}
51362587Sitojun		m_freem(m);
51453541Sshin		return;
515120913Sume	}
51653541Sshin
51753541Sshin	if (rt->rt_flags & RTF_GATEWAY)
51853541Sshin		dst = (struct sockaddr_in6 *)rt->rt_gateway;
51953541Sshin
52053541Sshin	/*
52153541Sshin	 * If we are to forward the packet using the same interface
52253541Sshin	 * as one we got the packet from, perhaps we should send a redirect
52353541Sshin	 * to sender to shortcut a hop.
52453541Sshin	 * Only send redirect if source is sending directly to us,
52553541Sshin	 * and if packet was not source routed (or has any options).
52653541Sshin	 * Also, don't send redirect if forwarding using a route
52753541Sshin	 * modified by a redirect.
52853541Sshin	 */
529162045Sjhay	if (ip6_sendredirects && rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
530122062Sume#ifdef IPSEC
531122062Sume	    !ipsecrt &&
532122062Sume#endif
53378064Sume	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
53478064Sume		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) {
53578064Sume			/*
53678064Sume			 * If the incoming interface is equal to the outgoing
53778064Sume			 * one, and the link attached to the interface is
53878064Sume			 * point-to-point, then it will be highly probable
53978064Sume			 * that a routing loop occurs. Thus, we immediately
54078064Sume			 * drop the packet and send an ICMPv6 error message.
54178064Sume			 *
54278064Sume			 * type/code is based on suggestion by Rich Draves.
54378064Sume			 * not sure if it is the best pick.
54478064Sume			 */
54578064Sume			icmp6_error(mcopy, ICMP6_DST_UNREACH,
54678064Sume				    ICMP6_DST_UNREACH_ADDR, 0);
54778064Sume			m_freem(m);
54878064Sume			return;
54978064Sume		}
55053541Sshin		type = ND_REDIRECT;
55178064Sume	}
55253541Sshin
55353541Sshin	/*
55462587Sitojun	 * Fake scoped addresses. Note that even link-local source or
55562587Sitojun	 * destinaion can appear, if the originating node just sends the
55662587Sitojun	 * packet to us (without address resolution for the destination).
55762587Sitojun	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
55878064Sume	 * link identifiers, we can do this stuff after making a copy for
55978064Sume	 * returning an error.
56062587Sitojun	 */
56162587Sitojun	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
56262587Sitojun		/*
56362587Sitojun		 * See corresponding comments in ip6_output.
56462587Sitojun		 * XXX: but is it possible that ip6_forward() sends a packet
56562587Sitojun		 *      to a loopback interface? I don't think so, and thus
56662587Sitojun		 *      I bark here. (jinmei@kame.net)
56762587Sitojun		 * XXX: it is common to route invalid packets to loopback.
56862587Sitojun		 *	also, the codepath will be visited on use of ::1 in
56962587Sitojun		 *	rthdr. (itojun)
57062587Sitojun		 */
57162587Sitojun#if 1
57262587Sitojun		if (0)
57362587Sitojun#else
57462587Sitojun		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
57562587Sitojun#endif
57662587Sitojun		{
57762587Sitojun			printf("ip6_forward: outgoing interface is loopback. "
578120913Sume			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
579165118Sbz			       ip6_sprintf(ip6bufs, &ip6->ip6_src),
580165118Sbz			       ip6_sprintf(ip6bufd, &ip6->ip6_dst),
581120913Sume			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
582120913Sume			       if_name(rt->rt_ifp));
58362587Sitojun		}
58462587Sitojun
58578064Sume		/* we can just use rcvif in forwarding. */
58678064Sume		origifp = m->m_pkthdr.rcvif;
58762587Sitojun	}
58862587Sitojun	else
58962587Sitojun		origifp = rt->rt_ifp;
59078064Sume	/*
59178064Sume	 * clear embedded scope identifiers if necessary.
59278064Sume	 * in6_clearscope will touch the addresses only when necessary.
59378064Sume	 */
59478064Sume	in6_clearscope(&ip6->ip6_src);
59578064Sume	in6_clearscope(&ip6->ip6_dst);
59662587Sitojun
597134383Sandre	/* Jump over all PFIL processing if hooks are not active. */
598155201Scsjp	if (!PFIL_HOOKED(&inet6_pfil_hook))
599134383Sandre		goto pass;
600134383Sandre
601134383Sandre	/* Run through list of hooks for output packets. */
602135920Smlaier	error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT, NULL);
603120593Ssam	if (error != 0)
604120593Ssam		goto senderr;
605120386Ssam	if (m == NULL)
606120386Ssam		goto freecopy;
607120386Ssam	ip6 = mtod(m, struct ip6_hdr *);
60884994Sdarrenr
609134383Sandrepass:
61062587Sitojun	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
61153541Sshin	if (error) {
61253541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
61353541Sshin		ip6stat.ip6s_cantforward++;
61453541Sshin	} else {
61553541Sshin		ip6stat.ip6s_forward++;
61653541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
61753541Sshin		if (type)
61853541Sshin			ip6stat.ip6s_redirectsent++;
61953541Sshin		else {
62053541Sshin			if (mcopy)
62153541Sshin				goto freecopy;
62253541Sshin		}
62353541Sshin	}
624120913Sume
625120593Ssamsenderr:
62653541Sshin	if (mcopy == NULL)
62753541Sshin		return;
62853541Sshin	switch (error) {
62953541Sshin	case 0:
63053541Sshin		if (type == ND_REDIRECT) {
63153541Sshin			icmp6_redirect_output(mcopy, rt);
63253541Sshin			return;
63353541Sshin		}
63453541Sshin		goto freecopy;
63553541Sshin
63653541Sshin	case EMSGSIZE:
63753541Sshin		/* xxx MTU is constant in PPP? */
63853541Sshin		goto freecopy;
63953541Sshin
64053541Sshin	case ENOBUFS:
64153541Sshin		/* Tell source to slow down like source quench in IP? */
64253541Sshin		goto freecopy;
64353541Sshin
64453541Sshin	case ENETUNREACH:	/* shouldn't happen, checked above */
64553541Sshin	case EHOSTUNREACH:
64653541Sshin	case ENETDOWN:
64753541Sshin	case EHOSTDOWN:
64853541Sshin	default:
64953541Sshin		type = ICMP6_DST_UNREACH;
65053541Sshin		code = ICMP6_DST_UNREACH_ADDR;
65153541Sshin		break;
65253541Sshin	}
65353541Sshin	icmp6_error(mcopy, type, code, 0);
65453541Sshin	return;
65553541Sshin
65653541Sshin freecopy:
65753541Sshin	m_freem(mcopy);
65853541Sshin	return;
65953541Sshin}
660