ip6_forward.c revision 122062
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_forward.c 122062 2003-11-04 16:02:05Z 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 *);
112122062Sume	struct sockaddr_in6 *dst = NULL;
113122062Sume	struct rtentry *rt = NULL;
11453541Sshin	int error, type = 0, code = 0;
11562587Sitojun	struct mbuf *mcopy = NULL;
11662587Sitojun	struct ifnet *origifp;	/* maybe unnecessary */
117121315Sume	u_int32_t srczone, dstzone;
11863256Sitojun#ifdef IPSEC
11953541Sshin	struct secpolicy *sp = NULL;
120122062Sume	int ipsecrt = 0;
12153541Sshin#endif
12253541Sshin
12363256Sitojun#ifdef IPSEC
12453541Sshin	/*
12553541Sshin	 * Check AH/ESP integrity.
12653541Sshin	 */
12753541Sshin	/*
12853541Sshin	 * Don't increment ip6s_cantforward because this is the check
12953541Sshin	 * before forwarding packet actually.
13053541Sshin	 */
13153541Sshin	if (ipsec6_in_reject(m, NULL)) {
132105199Ssam#if !defined(FAST_IPSEC)
13353541Sshin		ipsec6stat.in_polvio++;
134105199Ssam#endif
13553541Sshin		m_freem(m);
13653541Sshin		return;
13753541Sshin	}
13895023Ssuz#endif /* IPSEC */
13953541Sshin
14078064Sume	/*
14178064Sume	 * Do not forward packets to multicast destination (should be handled
14278064Sume	 * by ip6_mforward().
14378064Sume	 * Do not forward packets with unspecified source.  It was discussed
144120913Sume	 * in July 2000, on the ipngwg mailing list.
14578064Sume	 */
14662587Sitojun	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
14778064Sume	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
14878064Sume	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
14953541Sshin		ip6stat.ip6s_cantforward++;
15053541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
15153541Sshin		if (ip6_log_time + ip6_log_interval < time_second) {
15253541Sshin			ip6_log_time = time_second;
15353541Sshin			log(LOG_DEBUG,
15453541Sshin			    "cannot forward "
15553541Sshin			    "from %s to %s nxt %d received on %s\n",
15662587Sitojun			    ip6_sprintf(&ip6->ip6_src),
15762587Sitojun			    ip6_sprintf(&ip6->ip6_dst),
15853541Sshin			    ip6->ip6_nxt,
15953541Sshin			    if_name(m->m_pkthdr.rcvif));
16053541Sshin		}
16153541Sshin		m_freem(m);
16253541Sshin		return;
16353541Sshin	}
16453541Sshin
16553541Sshin	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
16653541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
16753541Sshin		icmp6_error(m, ICMP6_TIME_EXCEEDED,
16853541Sshin				ICMP6_TIME_EXCEED_TRANSIT, 0);
16953541Sshin		return;
17053541Sshin	}
17153541Sshin	ip6->ip6_hlim -= IPV6_HLIMDEC;
17253541Sshin
17362587Sitojun	/*
17462587Sitojun	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
17562587Sitojun	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
17662587Sitojun	 * we need to generate an ICMP6 message to the src.
17762587Sitojun	 * Thanks to M_EXT, in most cases copy will not occur.
17862587Sitojun	 *
17962587Sitojun	 * It is important to save it before IPsec processing as IPsec
18062587Sitojun	 * processing may modify the mbuf.
18162587Sitojun	 */
18262587Sitojun	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
18362587Sitojun
18463256Sitojun#ifdef IPSEC
18553541Sshin	/* get a security policy for this packet */
186120913Sume	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
187120913Sume	    IP_FORWARDING, &error);
18853541Sshin	if (sp == NULL) {
18953541Sshin		ipsec6stat.out_inval++;
19053541Sshin		ip6stat.ip6s_cantforward++;
19162587Sitojun		if (mcopy) {
19262587Sitojun#if 0
19362587Sitojun			/* XXX: what icmp ? */
19462587Sitojun#else
19562587Sitojun			m_freem(mcopy);
19662587Sitojun#endif
19762587Sitojun		}
19853541Sshin		m_freem(m);
19953541Sshin		return;
20053541Sshin	}
20153541Sshin
20253541Sshin	error = 0;
20353541Sshin
20453541Sshin	/* check policy */
20553541Sshin	switch (sp->policy) {
20653541Sshin	case IPSEC_POLICY_DISCARD:
20753541Sshin		/*
20853541Sshin		 * This packet is just discarded.
20953541Sshin		 */
21053541Sshin		ipsec6stat.out_polvio++;
21153541Sshin		ip6stat.ip6s_cantforward++;
21253541Sshin		key_freesp(sp);
21362587Sitojun		if (mcopy) {
21462587Sitojun#if 0
21562587Sitojun			/* XXX: what icmp ? */
21662587Sitojun#else
21762587Sitojun			m_freem(mcopy);
21862587Sitojun#endif
21962587Sitojun		}
22053541Sshin		m_freem(m);
22153541Sshin		return;
22253541Sshin
22353541Sshin	case IPSEC_POLICY_BYPASS:
22453541Sshin	case IPSEC_POLICY_NONE:
22553541Sshin		/* no need to do IPsec. */
22653541Sshin		key_freesp(sp);
22753541Sshin		goto skip_ipsec;
22862587Sitojun
22953541Sshin	case IPSEC_POLICY_IPSEC:
23053541Sshin		if (sp->req == NULL) {
23153541Sshin			/* XXX should be panic ? */
23253541Sshin			printf("ip6_forward: No IPsec request specified.\n");
23353541Sshin			ip6stat.ip6s_cantforward++;
23453541Sshin			key_freesp(sp);
23562587Sitojun			if (mcopy) {
23662587Sitojun#if 0
23762587Sitojun				/* XXX: what icmp ? */
23862587Sitojun#else
23962587Sitojun				m_freem(mcopy);
24062587Sitojun#endif
24162587Sitojun			}
24253541Sshin			m_freem(m);
24353541Sshin			return;
24453541Sshin		}
24553541Sshin		/* do IPsec */
24653541Sshin		break;
24753541Sshin
24853541Sshin	case IPSEC_POLICY_ENTRUST:
24953541Sshin	default:
25053541Sshin		/* should be panic ?? */
25153541Sshin		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
25253541Sshin		key_freesp(sp);
25353541Sshin		goto skip_ipsec;
25453541Sshin	}
25553541Sshin
25653541Sshin    {
257122062Sume	struct ipsecrequest *isr = NULL;
25853541Sshin	struct ipsec_output_state state;
25953541Sshin
26053541Sshin	/*
261122062Sume	 * when the kernel forwards a packet, it is not proper to apply
262122062Sume	 * IPsec transport mode to the packet is not proper.  this check
263122062Sume	 * avoid from this.
264122062Sume	 * at present, if there is even a transport mode SA request in the
265122062Sume	 * security policy, the kernel does not apply IPsec to the packet.
266122062Sume	 * this check is not enough because the following case is valid.
267122062Sume	 *      ipsec esp/tunnel/xxx-xxx/require esp/transport//require;
268122062Sume	 */
269122062Sume	for (isr = sp->req; isr; isr = isr->next) {
270122062Sume		if (isr->saidx.mode == IPSEC_MODE_TRANSPORT)
271122062Sume			goto skip_ipsec;
272122062Sume	}
273122062Sume
274122062Sume	/*
27553541Sshin	 * All the extension headers will become inaccessible
27653541Sshin	 * (since they can be encrypted).
27753541Sshin	 * Don't panic, we need no more updates to extension headers
27853541Sshin	 * on inner IPv6 packet (since they are now encapsulated).
27953541Sshin	 *
28053541Sshin	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
28153541Sshin	 */
28253541Sshin	bzero(&state, sizeof(state));
28353541Sshin	state.m = m;
28453541Sshin	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
28553541Sshin	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
28653541Sshin
28753541Sshin	error = ipsec6_output_tunnel(&state, sp, 0);
28853541Sshin
28953541Sshin	m = state.m;
29053541Sshin	key_freesp(sp);
29153541Sshin
29253541Sshin	if (error) {
29353541Sshin		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
29453541Sshin		switch (error) {
29553541Sshin		case EHOSTUNREACH:
29653541Sshin		case ENETUNREACH:
29753541Sshin		case EMSGSIZE:
29853541Sshin		case ENOBUFS:
29953541Sshin		case ENOMEM:
30053541Sshin			break;
30153541Sshin		default:
30253541Sshin			printf("ip6_output (ipsec): error code %d\n", error);
303120913Sume			/* FALLTHROUGH */
30453541Sshin		case ENOENT:
30553541Sshin			/* don't show these error codes to the user */
30653541Sshin			break;
30753541Sshin		}
30853541Sshin		ip6stat.ip6s_cantforward++;
30962587Sitojun		if (mcopy) {
31062587Sitojun#if 0
31162587Sitojun			/* XXX: what icmp ? */
31262587Sitojun#else
31362587Sitojun			m_freem(mcopy);
31462587Sitojun#endif
31562587Sitojun		}
31653541Sshin		m_freem(m);
31753541Sshin		return;
31853541Sshin	}
319122062Sume
320122062Sume	/* adjust pointer */
321122062Sume	ip6 = mtod(m, struct ip6_hdr *);
322122062Sume	dst = (struct sockaddr_in6 *)state.dst;
323122062Sume	rt = state.ro ? state.ro->ro_rt : NULL;
324122062Sume	if (dst != NULL && rt != NULL)
325122062Sume		ipsecrt = 1;
32653541Sshin    }
32753541Sshin    skip_ipsec:
32863256Sitojun#endif /* IPSEC */
32953541Sshin
330122062Sume#ifdef IPSEC
331122062Sume	if (ipsecrt)
332122062Sume		goto skip_routing;
333122062Sume#endif
334122062Sume
33578064Sume	dst = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
33653541Sshin	if (!srcrt) {
337120913Sume		/* ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst */
33853541Sshin		if (ip6_forward_rt.ro_rt == 0 ||
33953541Sshin		    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
34053541Sshin			if (ip6_forward_rt.ro_rt) {
34153541Sshin				RTFREE(ip6_forward_rt.ro_rt);
34253541Sshin				ip6_forward_rt.ro_rt = 0;
34353541Sshin			}
344120913Sume
34553541Sshin			/* this probably fails but give it a try again */
34653541Sshin			rtalloc_ign((struct route *)&ip6_forward_rt,
34753541Sshin				    RTF_PRCLONING);
34853541Sshin		}
34962587Sitojun
35053541Sshin		if (ip6_forward_rt.ro_rt == 0) {
35153541Sshin			ip6stat.ip6s_noroute++;
35278064Sume			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
35362587Sitojun			if (mcopy) {
35462587Sitojun				icmp6_error(mcopy, ICMP6_DST_UNREACH,
35562587Sitojun					    ICMP6_DST_UNREACH_NOROUTE, 0);
35662587Sitojun			}
35762587Sitojun			m_freem(m);
35853541Sshin			return;
35953541Sshin		}
36053541Sshin	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
361120913Sume		   !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
36253541Sshin		if (ip6_forward_rt.ro_rt) {
36353541Sshin			RTFREE(ip6_forward_rt.ro_rt);
36453541Sshin			ip6_forward_rt.ro_rt = 0;
36553541Sshin		}
36653541Sshin		bzero(dst, sizeof(*dst));
36753541Sshin		dst->sin6_len = sizeof(struct sockaddr_in6);
36853541Sshin		dst->sin6_family = AF_INET6;
36953541Sshin		dst->sin6_addr = ip6->ip6_dst;
37053541Sshin
37153541Sshin  		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
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	}
38353541Sshin	rt = ip6_forward_rt.ro_rt;
384122062Sume#ifdef IPSEC
385122062Sume    skip_routing:;
386122062Sume#endif
38762587Sitojun
38862587Sitojun	/*
38962587Sitojun	 * Scope check: if a packet can't be delivered to its destination
39062587Sitojun	 * for the reason that the destination is beyond the scope of the
39162587Sitojun	 * source address, discard the packet and return an icmp6 destination
39262587Sitojun	 * unreachable error with Code 2 (beyond scope of source address).
39395023Ssuz	 * [draft-ietf-ipngwg-icmp-v3-02.txt, Section 3.1]
39462587Sitojun	 */
395121315Sume	if (in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_src, &srczone) ||
396122062Sume	    in6_addr2zoneid(rt->rt_ifp, &ip6->ip6_src, &dstzone)) {
397122062Sume		/* XXX: this should not happen */
39862587Sitojun		ip6stat.ip6s_cantforward++;
39962587Sitojun		ip6stat.ip6s_badscope++;
400122062Sume		m_freem(m);
401122062Sume		return;
402122062Sume	}
403122062Sume	if (srczone != dstzone
404122062Sume#ifdef IPSEC
405122062Sume	    && !ipsecrt
406122062Sume#endif
407122062Sume	    ) {
408122062Sume		ip6stat.ip6s_cantforward++;
409122062Sume		ip6stat.ip6s_badscope++;
41062587Sitojun		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
41162587Sitojun
41262587Sitojun		if (ip6_log_time + ip6_log_interval < time_second) {
41362587Sitojun			ip6_log_time = time_second;
41462587Sitojun			log(LOG_DEBUG,
41562587Sitojun			    "cannot forward "
41662587Sitojun			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
41762587Sitojun			    ip6_sprintf(&ip6->ip6_src),
41862587Sitojun			    ip6_sprintf(&ip6->ip6_dst),
41962587Sitojun			    ip6->ip6_nxt,
42062587Sitojun			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
42162587Sitojun		}
42262587Sitojun		if (mcopy)
42362587Sitojun			icmp6_error(mcopy, ICMP6_DST_UNREACH,
42462587Sitojun				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
42562587Sitojun		m_freem(m);
42662587Sitojun		return;
42762587Sitojun	}
42862587Sitojun
429121283Sume	if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
43053541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
43162587Sitojun		if (mcopy) {
43262587Sitojun			u_long mtu;
43363256Sitojun#ifdef IPSEC
43462587Sitojun			struct secpolicy *sp;
43562587Sitojun			int ipsecerror;
43662587Sitojun			size_t ipsechdrsiz;
43762587Sitojun#endif
43862587Sitojun
439121283Sume			mtu = IN6_LINKMTU(rt->rt_ifp);
44063256Sitojun#ifdef IPSEC
44162587Sitojun			/*
44262587Sitojun			 * When we do IPsec tunnel ingress, we need to play
443122062Sume			 * with the link value (decrement IPsec header size
44462587Sitojun			 * from mtu value).  The code is much simpler than v4
44562587Sitojun			 * case, as we have the outgoing interface for
44662587Sitojun			 * encapsulated packet as "rt->rt_ifp".
44762587Sitojun			 */
44862587Sitojun			sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
44962587Sitojun				IP_FORWARDING, &ipsecerror);
45062587Sitojun			if (sp) {
45162587Sitojun				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
45262587Sitojun					IPSEC_DIR_OUTBOUND, NULL);
45362587Sitojun				if (ipsechdrsiz < mtu)
45462587Sitojun					mtu -= ipsechdrsiz;
45562587Sitojun			}
45662587Sitojun
45762587Sitojun			/*
45862587Sitojun			 * if mtu becomes less than minimum MTU,
45962587Sitojun			 * tell minimum MTU (and I'll need to fragment it).
46062587Sitojun			 */
46162587Sitojun			if (mtu < IPV6_MMTU)
46262587Sitojun				mtu = IPV6_MMTU;
46362587Sitojun#endif
46462587Sitojun			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
46562587Sitojun		}
46662587Sitojun		m_freem(m);
46753541Sshin		return;
468120913Sume	}
46953541Sshin
47053541Sshin	if (rt->rt_flags & RTF_GATEWAY)
47153541Sshin		dst = (struct sockaddr_in6 *)rt->rt_gateway;
47253541Sshin
47353541Sshin	/*
47453541Sshin	 * If we are to forward the packet using the same interface
47553541Sshin	 * as one we got the packet from, perhaps we should send a redirect
47653541Sshin	 * to sender to shortcut a hop.
47753541Sshin	 * Only send redirect if source is sending directly to us,
47853541Sshin	 * and if packet was not source routed (or has any options).
47953541Sshin	 * Also, don't send redirect if forwarding using a route
48053541Sshin	 * modified by a redirect.
48153541Sshin	 */
48253541Sshin	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
483122062Sume#ifdef IPSEC
484122062Sume	    !ipsecrt &&
485122062Sume#endif
48678064Sume	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
48778064Sume		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) {
48878064Sume			/*
48978064Sume			 * If the incoming interface is equal to the outgoing
49078064Sume			 * one, and the link attached to the interface is
49178064Sume			 * point-to-point, then it will be highly probable
49278064Sume			 * that a routing loop occurs. Thus, we immediately
49378064Sume			 * drop the packet and send an ICMPv6 error message.
49478064Sume			 *
49578064Sume			 * type/code is based on suggestion by Rich Draves.
49678064Sume			 * not sure if it is the best pick.
49778064Sume			 */
49878064Sume			icmp6_error(mcopy, ICMP6_DST_UNREACH,
49978064Sume				    ICMP6_DST_UNREACH_ADDR, 0);
50078064Sume			m_freem(m);
50178064Sume			return;
50278064Sume		}
50353541Sshin		type = ND_REDIRECT;
50478064Sume	}
50553541Sshin
50653541Sshin	/*
50753541Sshin	 * Check with the firewall...
50853541Sshin	 */
50966303Sume	if (ip6_fw_enable && ip6_fw_chk_ptr) {
51053541Sshin		u_short port = 0;
51153541Sshin		/* If ipfw says divert, we have to just drop packet */
51253541Sshin		if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
51353541Sshin			m_freem(m);
51453541Sshin			goto freecopy;
51553541Sshin		}
51653541Sshin		if (!m)
51753541Sshin			goto freecopy;
51853541Sshin	}
51953541Sshin
52062587Sitojun	/*
52162587Sitojun	 * Fake scoped addresses. Note that even link-local source or
52262587Sitojun	 * destinaion can appear, if the originating node just sends the
52362587Sitojun	 * packet to us (without address resolution for the destination).
52462587Sitojun	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
52578064Sume	 * link identifiers, we can do this stuff after making a copy for
52678064Sume	 * returning an error.
52762587Sitojun	 */
52862587Sitojun	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
52962587Sitojun		/*
53062587Sitojun		 * See corresponding comments in ip6_output.
53162587Sitojun		 * XXX: but is it possible that ip6_forward() sends a packet
53262587Sitojun		 *      to a loopback interface? I don't think so, and thus
53362587Sitojun		 *      I bark here. (jinmei@kame.net)
53462587Sitojun		 * XXX: it is common to route invalid packets to loopback.
53562587Sitojun		 *	also, the codepath will be visited on use of ::1 in
53662587Sitojun		 *	rthdr. (itojun)
53762587Sitojun		 */
53862587Sitojun#if 1
53962587Sitojun		if (0)
54062587Sitojun#else
54162587Sitojun		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
54262587Sitojun#endif
54362587Sitojun		{
54462587Sitojun			printf("ip6_forward: outgoing interface is loopback. "
545120913Sume			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
546120913Sume			       ip6_sprintf(&ip6->ip6_src),
547120913Sume			       ip6_sprintf(&ip6->ip6_dst),
548120913Sume			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
549120913Sume			       if_name(rt->rt_ifp));
55062587Sitojun		}
55162587Sitojun
55278064Sume		/* we can just use rcvif in forwarding. */
55378064Sume		origifp = m->m_pkthdr.rcvif;
55462587Sitojun	}
55562587Sitojun	else
55662587Sitojun		origifp = rt->rt_ifp;
55778064Sume	/*
55878064Sume	 * clear embedded scope identifiers if necessary.
55978064Sume	 * in6_clearscope will touch the addresses only when necessary.
56078064Sume	 */
56178064Sume	in6_clearscope(&ip6->ip6_src);
56278064Sume	in6_clearscope(&ip6->ip6_dst);
56362587Sitojun
56484994Sdarrenr#ifdef PFIL_HOOKS
56584994Sdarrenr	/*
56684994Sdarrenr	 * Run through list of hooks for output packets.
56784994Sdarrenr	 */
568120593Ssam	error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT);
569120593Ssam	if (error != 0)
570120593Ssam		goto senderr;
571120386Ssam	if (m == NULL)
572120386Ssam		goto freecopy;
573120386Ssam	ip6 = mtod(m, struct ip6_hdr *);
57484994Sdarrenr#endif /* PFIL_HOOKS */
57584994Sdarrenr
57662587Sitojun	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
57753541Sshin	if (error) {
57853541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
57953541Sshin		ip6stat.ip6s_cantforward++;
58053541Sshin	} else {
58153541Sshin		ip6stat.ip6s_forward++;
58253541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
58353541Sshin		if (type)
58453541Sshin			ip6stat.ip6s_redirectsent++;
58553541Sshin		else {
58653541Sshin			if (mcopy)
58753541Sshin				goto freecopy;
58853541Sshin		}
58953541Sshin	}
590120913Sume
591120593Ssam#ifdef PFIL_HOOKS
592120593Ssamsenderr:
593120593Ssam#endif
59453541Sshin	if (mcopy == NULL)
59553541Sshin		return;
59653541Sshin	switch (error) {
59753541Sshin	case 0:
59853541Sshin		if (type == ND_REDIRECT) {
59953541Sshin			icmp6_redirect_output(mcopy, rt);
60053541Sshin			return;
60153541Sshin		}
60253541Sshin		goto freecopy;
60353541Sshin
60453541Sshin	case EMSGSIZE:
60553541Sshin		/* xxx MTU is constant in PPP? */
60653541Sshin		goto freecopy;
60753541Sshin
60853541Sshin	case ENOBUFS:
60953541Sshin		/* Tell source to slow down like source quench in IP? */
61053541Sshin		goto freecopy;
61153541Sshin
61253541Sshin	case ENETUNREACH:	/* shouldn't happen, checked above */
61353541Sshin	case EHOSTUNREACH:
61453541Sshin	case ENETDOWN:
61553541Sshin	case EHOSTDOWN:
61653541Sshin	default:
61753541Sshin		type = ICMP6_DST_UNREACH;
61853541Sshin		code = ICMP6_DST_UNREACH_ADDR;
61953541Sshin		break;
62053541Sshin	}
62153541Sshin	icmp6_error(mcopy, type, code, 0);
62253541Sshin	return;
62353541Sshin
62453541Sshin freecopy:
62553541Sshin	m_freem(mcopy);
62653541Sshin	return;
62753541Sshin}
628