1139826Simp/*-
253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
353541Sshin * All rights reserved.
453541Sshin *
553541Sshin * Redistribution and use in source and binary forms, with or without
653541Sshin * modification, are permitted provided that the following conditions
753541Sshin * are met:
853541Sshin * 1. Redistributions of source code must retain the above copyright
953541Sshin *    notice, this list of conditions and the following disclaimer.
1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1153541Sshin *    notice, this list of conditions and the following disclaimer in the
1253541Sshin *    documentation and/or other materials provided with the distribution.
1353541Sshin * 3. Neither the name of the project nor the names of its contributors
1453541Sshin *    may be used to endorse or promote products derived from this software
1553541Sshin *    without specific prior written permission.
1653541Sshin *
1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2053541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2753541Sshin * SUCH DAMAGE.
28174510Sobrien *
29174510Sobrien *	$KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $
3053541Sshin */
3153541Sshin
32174510Sobrien#include <sys/cdefs.h>
33174510Sobrien__FBSDID("$FreeBSD: releng/10.3/sys/netinet6/ip6_forward.c 284576 2015-06-18 20:57:21Z kp $");
34174510Sobrien
3562587Sitojun#include "opt_inet.h"
3662587Sitojun#include "opt_inet6.h"
37225044Sbz#include "opt_ipfw.h"
3855009Sshin#include "opt_ipsec.h"
39148921Ssuz#include "opt_ipstealth.h"
4053541Sshin
4153541Sshin#include <sys/param.h>
4253541Sshin#include <sys/systm.h>
4378064Sume#include <sys/malloc.h>
4453541Sshin#include <sys/mbuf.h>
4553541Sshin#include <sys/domain.h>
4653541Sshin#include <sys/protosw.h>
4753541Sshin#include <sys/socket.h>
4853541Sshin#include <sys/errno.h>
4953541Sshin#include <sys/time.h>
5078064Sume#include <sys/kernel.h>
5153541Sshin#include <sys/syslog.h>
5253541Sshin
5353541Sshin#include <net/if.h>
54225044Sbz#include <net/netisr.h>
5553541Sshin#include <net/route.h>
5684994Sdarrenr#include <net/pfil.h>
5753541Sshin
5853541Sshin#include <netinet/in.h>
5953541Sshin#include <netinet/in_var.h>
6078064Sume#include <netinet/in_systm.h>
6178064Sume#include <netinet/ip.h>
6262587Sitojun#include <netinet/ip_var.h>
6378064Sume#include <netinet6/in6_var.h>
6462587Sitojun#include <netinet/ip6.h>
6553541Sshin#include <netinet6/ip6_var.h>
66148385Sume#include <netinet6/scope6_var.h>
6762587Sitojun#include <netinet/icmp6.h>
6853541Sshin#include <netinet6/nd6.h>
6953541Sshin
7078064Sume#include <netinet/in_pcb.h>
7178064Sume
72171167Sgnn#ifdef IPSEC
73105199Ssam#include <netipsec/ipsec.h>
74105199Ssam#include <netipsec/ipsec6.h>
75105199Ssam#include <netipsec/key.h>
76171167Sgnn#endif /* IPSEC */
77105199Ssam
7884994Sdarrenr#include <netinet6/ip6protosw.h>
7984994Sdarrenr
8053541Sshin/*
8153541Sshin * Forward a packet.  If some error occurs return the sender
8253541Sshin * an icmp packet.  Note we can't always generate a meaningful
8353541Sshin * icmp message because icmp doesn't have a large enough repertoire
8453541Sshin * of codes and types.
8553541Sshin *
8653541Sshin * If not forwarding, just drop the packet.  This could be confusing
8753541Sshin * if ipforwarding was zero but some routing protocol was advancing
8853541Sshin * us as a gateway to somewhere.  However, we must let the routing
8953541Sshin * protocol deal with that.
9053541Sshin *
9153541Sshin */
9253541Sshinvoid
93171259Sdelphijip6_forward(struct mbuf *m, int srcrt)
9453541Sshin{
9553541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
96122062Sume	struct sockaddr_in6 *dst = NULL;
97122062Sume	struct rtentry *rt = NULL;
98187989Sbz	struct route_in6 rin6;
9953541Sshin	int error, type = 0, code = 0;
10062587Sitojun	struct mbuf *mcopy = NULL;
10162587Sitojun	struct ifnet *origifp;	/* maybe unnecessary */
102148385Sume	u_int32_t inzone, outzone;
103225044Sbz	struct in6_addr src_in6, dst_in6, odst;
104171167Sgnn#ifdef IPSEC
10553541Sshin	struct secpolicy *sp = NULL;
106122062Sume	int ipsecrt = 0;
10753541Sshin#endif
108225044Sbz#ifdef SCTP
109225044Sbz	int sw_csum;
110225044Sbz#endif
111225044Sbz	struct m_tag *fwd_tag;
112165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
11353541Sshin
114171167Sgnn#ifdef IPSEC
11553541Sshin	/*
11653541Sshin	 * Check AH/ESP integrity.
11753541Sshin	 */
11853541Sshin	/*
11953541Sshin	 * Don't increment ip6s_cantforward because this is the check
12053541Sshin	 * before forwarding packet actually.
12153541Sshin	 */
12253541Sshin	if (ipsec6_in_reject(m, NULL)) {
123253571Sae		IPSEC6STAT_INC(ips_in_polvio);
12453541Sshin		m_freem(m);
12553541Sshin		return;
12653541Sshin	}
127171167Sgnn#endif /* IPSEC */
12853541Sshin
12978064Sume	/*
13078064Sume	 * Do not forward packets to multicast destination (should be handled
13178064Sume	 * by ip6_mforward().
13278064Sume	 * Do not forward packets with unspecified source.  It was discussed
133120913Sume	 * in July 2000, on the ipngwg mailing list.
13478064Sume	 */
13562587Sitojun	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
13678064Sume	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
13778064Sume	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
138249294Sae		IP6STAT_INC(ip6s_cantforward);
13953541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
140253970Shrs		if (V_ip6_log_time + V_ip6_log_interval < time_uptime) {
141253970Shrs			V_ip6_log_time = time_uptime;
14253541Sshin			log(LOG_DEBUG,
14353541Sshin			    "cannot forward "
14453541Sshin			    "from %s to %s nxt %d received on %s\n",
145165118Sbz			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
146165118Sbz			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
14753541Sshin			    ip6->ip6_nxt,
14853541Sshin			    if_name(m->m_pkthdr.rcvif));
14953541Sshin		}
15053541Sshin		m_freem(m);
15153541Sshin		return;
15253541Sshin	}
15353541Sshin
154148921Ssuz#ifdef IPSTEALTH
155181803Sbz	if (!V_ip6stealth) {
156148921Ssuz#endif
15753541Sshin	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
15853541Sshin		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
15953541Sshin		icmp6_error(m, ICMP6_TIME_EXCEEDED,
16053541Sshin				ICMP6_TIME_EXCEED_TRANSIT, 0);
16153541Sshin		return;
16253541Sshin	}
16353541Sshin	ip6->ip6_hlim -= IPV6_HLIMDEC;
16453541Sshin
165148921Ssuz#ifdef IPSTEALTH
166148921Ssuz	}
167148921Ssuz#endif
168148921Ssuz
16962587Sitojun	/*
17062587Sitojun	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
17162587Sitojun	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
17262587Sitojun	 * we need to generate an ICMP6 message to the src.
17362587Sitojun	 * Thanks to M_EXT, in most cases copy will not occur.
17462587Sitojun	 *
17562587Sitojun	 * It is important to save it before IPsec processing as IPsec
17662587Sitojun	 * processing may modify the mbuf.
17762587Sitojun	 */
17862587Sitojun	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
17962587Sitojun
180171167Sgnn#ifdef IPSEC
18153541Sshin	/* get a security policy for this packet */
182171133Sgnn	sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
183120913Sume	    IP_FORWARDING, &error);
18453541Sshin	if (sp == NULL) {
185253571Sae		IPSEC6STAT_INC(ips_out_inval);
186249294Sae		IP6STAT_INC(ip6s_cantforward);
18762587Sitojun		if (mcopy) {
18862587Sitojun#if 0
18962587Sitojun			/* XXX: what icmp ? */
19062587Sitojun#else
19162587Sitojun			m_freem(mcopy);
19262587Sitojun#endif
19362587Sitojun		}
19453541Sshin		m_freem(m);
19553541Sshin		return;
19653541Sshin	}
19753541Sshin
19853541Sshin	error = 0;
19953541Sshin
20053541Sshin	/* check policy */
20153541Sshin	switch (sp->policy) {
20253541Sshin	case IPSEC_POLICY_DISCARD:
20353541Sshin		/*
20453541Sshin		 * This packet is just discarded.
20553541Sshin		 */
206253571Sae		IPSEC6STAT_INC(ips_out_polvio);
207249294Sae		IP6STAT_INC(ip6s_cantforward);
208171133Sgnn		KEY_FREESP(&sp);
20962587Sitojun		if (mcopy) {
21062587Sitojun#if 0
21162587Sitojun			/* XXX: what icmp ? */
21262587Sitojun#else
21362587Sitojun			m_freem(mcopy);
21462587Sitojun#endif
21562587Sitojun		}
21653541Sshin		m_freem(m);
21753541Sshin		return;
21853541Sshin
21953541Sshin	case IPSEC_POLICY_BYPASS:
22053541Sshin	case IPSEC_POLICY_NONE:
22153541Sshin		/* no need to do IPsec. */
222171133Sgnn		KEY_FREESP(&sp);
22353541Sshin		goto skip_ipsec;
22462587Sitojun
22553541Sshin	case IPSEC_POLICY_IPSEC:
22653541Sshin		if (sp->req == NULL) {
22753541Sshin			/* XXX should be panic ? */
22853541Sshin			printf("ip6_forward: No IPsec request specified.\n");
229249294Sae			IP6STAT_INC(ip6s_cantforward);
230171133Sgnn			KEY_FREESP(&sp);
23162587Sitojun			if (mcopy) {
23262587Sitojun#if 0
23362587Sitojun				/* XXX: what icmp ? */
23462587Sitojun#else
23562587Sitojun				m_freem(mcopy);
23662587Sitojun#endif
23762587Sitojun			}
23853541Sshin			m_freem(m);
23953541Sshin			return;
24053541Sshin		}
24153541Sshin		/* do IPsec */
24253541Sshin		break;
24353541Sshin
24453541Sshin	case IPSEC_POLICY_ENTRUST:
24553541Sshin	default:
24653541Sshin		/* should be panic ?? */
24753541Sshin		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
248171133Sgnn		KEY_FREESP(&sp);
24953541Sshin		goto skip_ipsec;
25053541Sshin	}
25153541Sshin
25253541Sshin    {
253122062Sume	struct ipsecrequest *isr = NULL;
25453541Sshin
25553541Sshin	/*
256122062Sume	 * when the kernel forwards a packet, it is not proper to apply
257122062Sume	 * IPsec transport mode to the packet is not proper.  this check
258122062Sume	 * avoid from this.
259122062Sume	 * at present, if there is even a transport mode SA request in the
260122062Sume	 * security policy, the kernel does not apply IPsec to the packet.
261122062Sume	 * this check is not enough because the following case is valid.
262122062Sume	 *      ipsec esp/tunnel/xxx-xxx/require esp/transport//require;
263122062Sume	 */
264122062Sume	for (isr = sp->req; isr; isr = isr->next) {
265126006Sume		if (isr->saidx.mode == IPSEC_MODE_ANY)
266126006Sume			goto doipsectunnel;
267126006Sume		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
268126006Sume			goto doipsectunnel;
269122062Sume	}
270122062Sume
271122062Sume	/*
272126006Sume	 * if there's no need for tunnel mode IPsec, skip.
273126006Sume	 */
274126006Sume	if (!isr)
275126006Sume		goto skip_ipsec;
276126006Sume
277126006Sume    doipsectunnel:
278126006Sume	/*
27953541Sshin	 * All the extension headers will become inaccessible
28053541Sshin	 * (since they can be encrypted).
28153541Sshin	 * Don't panic, we need no more updates to extension headers
28253541Sshin	 * on inner IPv6 packet (since they are now encapsulated).
28353541Sshin	 *
28453541Sshin	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
28553541Sshin	 */
28653541Sshin
287274132Sae	/*
288274132Sae	 * If we need to encapsulate the packet, do it here
289274132Sae	 * ipsec6_proces_packet will send the packet using ip6_output
290274132Sae	 */
291274132Sae	error = ipsec6_process_packet(m, sp->req);
29253541Sshin
293171133Sgnn	KEY_FREESP(&sp);
29453541Sshin
295274132Sae	if (error == EJUSTRETURN) {
296274132Sae		/*
297274132Sae		 * We had a SP with a level of 'use' and no SA. We
298274132Sae		 * will just continue to process the packet without
299274132Sae		 * IPsec processing.
300274132Sae		 */
301274132Sae		error = 0;
302274132Sae		goto skip_ipsec;
303274132Sae	}
304274132Sae
30553541Sshin	if (error) {
306274132Sae		/* mbuf is already reclaimed in ipsec6_process_packet. */
30753541Sshin		switch (error) {
30853541Sshin		case EHOSTUNREACH:
30953541Sshin		case ENETUNREACH:
31053541Sshin		case EMSGSIZE:
31153541Sshin		case ENOBUFS:
31253541Sshin		case ENOMEM:
31353541Sshin			break;
31453541Sshin		default:
31553541Sshin			printf("ip6_output (ipsec): error code %d\n", error);
316120913Sume			/* FALLTHROUGH */
31753541Sshin		case ENOENT:
31853541Sshin			/* don't show these error codes to the user */
31953541Sshin			break;
32053541Sshin		}
321249294Sae		IP6STAT_INC(ip6s_cantforward);
32262587Sitojun		if (mcopy) {
32362587Sitojun#if 0
32462587Sitojun			/* XXX: what icmp ? */
32562587Sitojun#else
32662587Sitojun			m_freem(mcopy);
32762587Sitojun#endif
32862587Sitojun		}
32953541Sshin		return;
330171133Sgnn	} else {
331171260Sdelphij		/*
332171260Sdelphij		 * In the FAST IPSec case we have already
333171133Sgnn		 * re-injected the packet and it has been freed
334171260Sdelphij		 * by the ipsec_done() function.  So, just clean
335171133Sgnn		 * up after ourselves.
336171133Sgnn		 */
337171133Sgnn		m = NULL;
338171133Sgnn		goto freecopy;
33953541Sshin	}
34053541Sshin    }
341187949Sbzskip_ipsec:
342122062Sume#endif
343225044Sbzagain:
344187989Sbz	bzero(&rin6, sizeof(struct route_in6));
345187989Sbz	dst = (struct sockaddr_in6 *)&rin6.ro_dst;
346187989Sbz	dst->sin6_len = sizeof(struct sockaddr_in6);
347187989Sbz	dst->sin6_family = AF_INET6;
348187989Sbz	dst->sin6_addr = ip6->ip6_dst;
349225044Sbzagain2:
350231852Sbz	rin6.ro_rt = in6_rtalloc1((struct sockaddr *)dst, 0, 0, M_GETFIB(m));
351187989Sbz	if (rin6.ro_rt != NULL)
352187989Sbz		RT_UNLOCK(rin6.ro_rt);
353187989Sbz	else {
354249294Sae		IP6STAT_INC(ip6s_noroute);
355187989Sbz		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
356187989Sbz		if (mcopy) {
357187989Sbz			icmp6_error(mcopy, ICMP6_DST_UNREACH,
358187989Sbz			ICMP6_DST_UNREACH_NOROUTE, 0);
35953541Sshin		}
360187989Sbz		goto bad;
36153541Sshin	}
362187989Sbz	rt = rin6.ro_rt;
36362587Sitojun
36462587Sitojun	/*
365148385Sume	 * Source scope check: if a packet can't be delivered to its
366148385Sume	 * destination for the reason that the destination is beyond the scope
367148385Sume	 * of the source address, discard the packet and return an icmp6
368148385Sume	 * destination unreachable error with Code 2 (beyond scope of source
369148385Sume	 * address).  We use a local copy of ip6_src, since in6_setscope()
370148385Sume	 * will possibly modify its first argument.
371148385Sume	 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1]
37262587Sitojun	 */
373148385Sume	src_in6 = ip6->ip6_src;
374148385Sume	if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
375122062Sume		/* XXX: this should not happen */
376249294Sae		IP6STAT_INC(ip6s_cantforward);
377249294Sae		IP6STAT_INC(ip6s_badscope);
378187989Sbz		goto bad;
379122062Sume	}
380148385Sume	if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
381249294Sae		IP6STAT_INC(ip6s_cantforward);
382249294Sae		IP6STAT_INC(ip6s_badscope);
383187989Sbz		goto bad;
384148385Sume	}
385148385Sume	if (inzone != outzone
386171167Sgnn#ifdef IPSEC
387122062Sume	    && !ipsecrt
388122062Sume#endif
389122062Sume	    ) {
390249294Sae		IP6STAT_INC(ip6s_cantforward);
391249294Sae		IP6STAT_INC(ip6s_badscope);
39262587Sitojun		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
39362587Sitojun
394253970Shrs		if (V_ip6_log_time + V_ip6_log_interval < time_uptime) {
395253970Shrs			V_ip6_log_time = time_uptime;
39662587Sitojun			log(LOG_DEBUG,
39762587Sitojun			    "cannot forward "
39862587Sitojun			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
399165118Sbz			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
400165118Sbz			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
40162587Sitojun			    ip6->ip6_nxt,
40262587Sitojun			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
40362587Sitojun		}
40462587Sitojun		if (mcopy)
40562587Sitojun			icmp6_error(mcopy, ICMP6_DST_UNREACH,
40662587Sitojun				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
407187989Sbz		goto bad;
40862587Sitojun	}
40962587Sitojun
410148385Sume	/*
411148385Sume	 * Destination scope check: if a packet is going to break the scope
412148385Sume	 * zone of packet's destination address, discard it.  This case should
413148385Sume	 * usually be prevented by appropriately-configured routing table, but
414148385Sume	 * we need an explicit check because we may mistakenly forward the
415148385Sume	 * packet to a different zone by (e.g.) a default route.
416148385Sume	 */
417148385Sume	dst_in6 = ip6->ip6_dst;
418148385Sume	if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
419148385Sume	    in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
420148385Sume	    inzone != outzone) {
421249294Sae		IP6STAT_INC(ip6s_cantforward);
422249294Sae		IP6STAT_INC(ip6s_badscope);
423187989Sbz		goto bad;
424148385Sume	}
425148385Sume
42653541Sshin	if (rt->rt_flags & RTF_GATEWAY)
42753541Sshin		dst = (struct sockaddr_in6 *)rt->rt_gateway;
42853541Sshin
42953541Sshin	/*
43053541Sshin	 * If we are to forward the packet using the same interface
43153541Sshin	 * as one we got the packet from, perhaps we should send a redirect
43253541Sshin	 * to sender to shortcut a hop.
43353541Sshin	 * Only send redirect if source is sending directly to us,
43453541Sshin	 * and if packet was not source routed (or has any options).
43553541Sshin	 * Also, don't send redirect if forwarding using a route
43653541Sshin	 * modified by a redirect.
43753541Sshin	 */
438181803Sbz	if (V_ip6_sendredirects && rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
439171167Sgnn#ifdef IPSEC
440122062Sume	    !ipsecrt &&
441171167Sgnn#endif /* IPSEC */
44278064Sume	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
44378064Sume		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) {
44478064Sume			/*
44578064Sume			 * If the incoming interface is equal to the outgoing
44678064Sume			 * one, and the link attached to the interface is
44778064Sume			 * point-to-point, then it will be highly probable
44878064Sume			 * that a routing loop occurs. Thus, we immediately
44978064Sume			 * drop the packet and send an ICMPv6 error message.
45078064Sume			 *
45178064Sume			 * type/code is based on suggestion by Rich Draves.
45278064Sume			 * not sure if it is the best pick.
45378064Sume			 */
45478064Sume			icmp6_error(mcopy, ICMP6_DST_UNREACH,
45578064Sume				    ICMP6_DST_UNREACH_ADDR, 0);
456187989Sbz			goto bad;
45778064Sume		}
45853541Sshin		type = ND_REDIRECT;
45978064Sume	}
46053541Sshin
46153541Sshin	/*
46262587Sitojun	 * Fake scoped addresses. Note that even link-local source or
46362587Sitojun	 * destinaion can appear, if the originating node just sends the
46462587Sitojun	 * packet to us (without address resolution for the destination).
46562587Sitojun	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
46678064Sume	 * link identifiers, we can do this stuff after making a copy for
46778064Sume	 * returning an error.
46862587Sitojun	 */
46962587Sitojun	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
47062587Sitojun		/*
47162587Sitojun		 * See corresponding comments in ip6_output.
47262587Sitojun		 * XXX: but is it possible that ip6_forward() sends a packet
47362587Sitojun		 *      to a loopback interface? I don't think so, and thus
47462587Sitojun		 *      I bark here. (jinmei@kame.net)
47562587Sitojun		 * XXX: it is common to route invalid packets to loopback.
47662587Sitojun		 *	also, the codepath will be visited on use of ::1 in
47762587Sitojun		 *	rthdr. (itojun)
47862587Sitojun		 */
47962587Sitojun#if 1
48062587Sitojun		if (0)
48162587Sitojun#else
48262587Sitojun		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
48362587Sitojun#endif
48462587Sitojun		{
48562587Sitojun			printf("ip6_forward: outgoing interface is loopback. "
486120913Sume			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
487165118Sbz			       ip6_sprintf(ip6bufs, &ip6->ip6_src),
488165118Sbz			       ip6_sprintf(ip6bufd, &ip6->ip6_dst),
489120913Sume			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
490120913Sume			       if_name(rt->rt_ifp));
49162587Sitojun		}
49262587Sitojun
49378064Sume		/* we can just use rcvif in forwarding. */
49478064Sume		origifp = m->m_pkthdr.rcvif;
49562587Sitojun	}
49662587Sitojun	else
49762587Sitojun		origifp = rt->rt_ifp;
49878064Sume	/*
49978064Sume	 * clear embedded scope identifiers if necessary.
50078064Sume	 * in6_clearscope will touch the addresses only when necessary.
50178064Sume	 */
50278064Sume	in6_clearscope(&ip6->ip6_src);
50378064Sume	in6_clearscope(&ip6->ip6_dst);
50462587Sitojun
505134383Sandre	/* Jump over all PFIL processing if hooks are not active. */
506197952Sjulian	if (!PFIL_HOOKED(&V_inet6_pfil_hook))
507134383Sandre		goto pass;
508134383Sandre
509225044Sbz	odst = ip6->ip6_dst;
510134383Sandre	/* Run through list of hooks for output packets. */
511197952Sjulian	error = pfil_run_hooks(&V_inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT, NULL);
512264224Sae	if (error != 0 || m == NULL)
513264224Sae		goto freecopy;		/* consumed by filter */
514120386Ssam	ip6 = mtod(m, struct ip6_hdr *);
51584994Sdarrenr
516225044Sbz	/* See if destination IP address was changed by packet filter. */
517225044Sbz	if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
518225044Sbz		m->m_flags |= M_SKIP_FIREWALL;
519225044Sbz		/* If destination is now ourself drop to ip6_input(). */
520284575Skp		if (in6_localip(&ip6->ip6_dst))
521225044Sbz			m->m_flags |= M_FASTFWD_OURS;
522284575Skp		else
523225044Sbz			goto again;	/* Redo the routing table lookup. */
524225044Sbz	}
525225044Sbz
526225044Sbz	/* See if local, if yes, send it to netisr. */
527225044Sbz	if (m->m_flags & M_FASTFWD_OURS) {
528225044Sbz		if (m->m_pkthdr.rcvif == NULL)
529225044Sbz			m->m_pkthdr.rcvif = V_loif;
530236170Sbz		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
531225044Sbz			m->m_pkthdr.csum_flags |=
532236170Sbz			    CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
533225044Sbz			m->m_pkthdr.csum_data = 0xffff;
534225044Sbz		}
535225044Sbz#ifdef SCTP
536236332Stuexen		if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
537236332Stuexen			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
538236332Stuexen#endif
539225044Sbz		error = netisr_queue(NETISR_IPV6, m);
540225044Sbz		goto out;
541225044Sbz	}
542225044Sbz	/* Or forward to some other address? */
543242463Sae	if ((m->m_flags & M_IP6_NEXTHOP) &&
544242463Sae	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
545225044Sbz		dst = (struct sockaddr_in6 *)&rin6.ro_dst;
546225044Sbz		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in6));
547225044Sbz		m->m_flags |= M_SKIP_FIREWALL;
548242463Sae		m->m_flags &= ~M_IP6_NEXTHOP;
549225044Sbz		m_tag_delete(m, fwd_tag);
550225044Sbz		goto again2;
551225044Sbz	}
552225044Sbz
553134383Sandrepass:
554284576Skp	/* See if the size was changed by the packet filter. */
555284576Skp	if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
556284576Skp		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
557284576Skp		if (mcopy) {
558284576Skp			u_long mtu;
559284576Skp#ifdef IPSEC
560284576Skp			struct secpolicy *sp;
561284576Skp			int ipsecerror;
562284576Skp			size_t ipsechdrsiz;
563284576Skp#endif /* IPSEC */
564284576Skp
565284576Skp			mtu = IN6_LINKMTU(rt->rt_ifp);
566284576Skp#ifdef IPSEC
567284576Skp			/*
568284576Skp			 * When we do IPsec tunnel ingress, we need to play
569284576Skp			 * with the link value (decrement IPsec header size
570284576Skp			 * from mtu value).  The code is much simpler than v4
571284576Skp			 * case, as we have the outgoing interface for
572284576Skp			 * encapsulated packet as "rt->rt_ifp".
573284576Skp			 */
574284576Skp			sp = ipsec_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
575284576Skp				IP_FORWARDING, &ipsecerror);
576284576Skp			if (sp) {
577284576Skp				ipsechdrsiz = ipsec_hdrsiz(mcopy,
578284576Skp					IPSEC_DIR_OUTBOUND, NULL);
579284576Skp				if (ipsechdrsiz < mtu)
580284576Skp					mtu -= ipsechdrsiz;
581284576Skp			}
582284576Skp
583284576Skp			/*
584284576Skp			 * if mtu becomes less than minimum MTU,
585284576Skp			 * tell minimum MTU (and I'll need to fragment it).
586284576Skp			 */
587284576Skp			if (mtu < IPV6_MMTU)
588284576Skp				mtu = IPV6_MMTU;
589284576Skp#endif /* IPSEC */
590284576Skp			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
591284576Skp		}
592284576Skp		goto bad;
593284576Skp	}
594284576Skp
59562587Sitojun	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
59653541Sshin	if (error) {
59753541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
598249294Sae		IP6STAT_INC(ip6s_cantforward);
59953541Sshin	} else {
600249294Sae		IP6STAT_INC(ip6s_forward);
60153541Sshin		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
60253541Sshin		if (type)
603249294Sae			IP6STAT_INC(ip6s_redirectsent);
60453541Sshin		else {
60553541Sshin			if (mcopy)
60653541Sshin				goto freecopy;
60753541Sshin		}
60853541Sshin	}
609120913Sume
61053541Sshin	if (mcopy == NULL)
611187989Sbz		goto out;
61253541Sshin	switch (error) {
61353541Sshin	case 0:
61453541Sshin		if (type == ND_REDIRECT) {
61553541Sshin			icmp6_redirect_output(mcopy, rt);
616187989Sbz			goto out;
61753541Sshin		}
61853541Sshin		goto freecopy;
61953541Sshin
62053541Sshin	case EMSGSIZE:
62153541Sshin		/* xxx MTU is constant in PPP? */
62253541Sshin		goto freecopy;
62353541Sshin
62453541Sshin	case ENOBUFS:
62553541Sshin		/* Tell source to slow down like source quench in IP? */
62653541Sshin		goto freecopy;
62753541Sshin
62853541Sshin	case ENETUNREACH:	/* shouldn't happen, checked above */
62953541Sshin	case EHOSTUNREACH:
63053541Sshin	case ENETDOWN:
63153541Sshin	case EHOSTDOWN:
63253541Sshin	default:
63353541Sshin		type = ICMP6_DST_UNREACH;
63453541Sshin		code = ICMP6_DST_UNREACH_ADDR;
63553541Sshin		break;
63653541Sshin	}
63753541Sshin	icmp6_error(mcopy, type, code, 0);
638187989Sbz	goto out;
63953541Sshin
64053541Sshin freecopy:
64153541Sshin	m_freem(mcopy);
642187989Sbz	goto out;
643187989Sbzbad:
644187989Sbz	m_freem(m);
645187989Sbzout:
646187989Sbz	if (rt != NULL
647187989Sbz#ifdef IPSEC
648187989Sbz	    && !ipsecrt
649187989Sbz#endif
650187989Sbz	    )
651187989Sbz		RTFREE(rt);
65253541Sshin}
653