Home | History | Annotate | Line # | Download | only in netinet6
ip6_forward.c revision 1.55
      1 /*	$NetBSD: ip6_forward.c,v 1.55 2007/02/17 22:34:13 dyoung Exp $	*/
      2 /*	$KAME: ip6_forward.c,v 1.109 2002/09/11 08:10:17 sakane Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: ip6_forward.c,v 1.55 2007/02/17 22:34:13 dyoung Exp $");
     35 
     36 #include "opt_ipsec.h"
     37 #include "opt_pfil_hooks.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/malloc.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/domain.h>
     44 #include <sys/protosw.h>
     45 #include <sys/socket.h>
     46 #include <sys/errno.h>
     47 #include <sys/time.h>
     48 #include <sys/kernel.h>
     49 #include <sys/syslog.h>
     50 
     51 #include <net/if.h>
     52 #include <net/route.h>
     53 
     54 #include <netinet/in.h>
     55 #include <netinet/in_var.h>
     56 #include <netinet/ip_var.h>
     57 #include <netinet/ip6.h>
     58 #include <netinet6/ip6_var.h>
     59 #include <netinet6/scope6_var.h>
     60 #include <netinet/icmp6.h>
     61 #include <netinet6/nd6.h>
     62 
     63 #ifdef IPSEC
     64 #include <netinet6/ipsec.h>
     65 #include <netkey/key.h>
     66 #endif /* IPSEC */
     67 
     68 #ifdef FAST_IPSEC
     69 #include <netipsec/ipsec.h>
     70 #include <netipsec/ipsec6.h>
     71 #include <netipsec/key.h>
     72 #include <netipsec/xform.h>
     73 #endif /* FAST_IPSEC */
     74 
     75 #ifdef PFIL_HOOKS
     76 #include <net/pfil.h>
     77 #endif
     78 
     79 #include <net/net_osdep.h>
     80 
     81 struct	route_in6 ip6_forward_rt;
     82 
     83 #ifdef PFIL_HOOKS
     84 extern struct pfil_head inet6_pfil_hook;	/* XXX */
     85 #endif
     86 
     87 /*
     88  * Forward a packet.  If some error occurs return the sender
     89  * an icmp packet.  Note we can't always generate a meaningful
     90  * icmp message because icmp doesn't have a large enough repertoire
     91  * of codes and types.
     92  *
     93  * If not forwarding, just drop the packet.  This could be confusing
     94  * if ipforwarding was zero but some routing protocol was advancing
     95  * us as a gateway to somewhere.  However, we must let the routing
     96  * protocol deal with that.
     97  *
     98  */
     99 
    100 void
    101 ip6_forward(m, srcrt)
    102 	struct mbuf *m;
    103 	int srcrt;
    104 {
    105 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    106 	struct sockaddr_in6 *dst;
    107 	struct rtentry *rt;
    108 	int error = 0, type = 0, code = 0;
    109 	struct mbuf *mcopy = NULL;
    110 	struct ifnet *origifp;	/* maybe unnecessary */
    111 	u_int32_t inzone, outzone;
    112 	struct in6_addr src_in6, dst_in6;
    113 #ifdef IPSEC
    114 	struct secpolicy *sp = NULL;
    115 	int ipsecrt = 0;
    116 #endif
    117 #ifdef FAST_IPSEC
    118     struct secpolicy *sp = NULL;
    119     int needipsec = 0;
    120     int s;
    121 #endif
    122 
    123 
    124 #ifdef IPSEC
    125 	/*
    126 	 * Check AH/ESP integrity.
    127 	 */
    128 	/*
    129 	 * Don't increment ip6s_cantforward because this is the check
    130 	 * before forwarding packet actually.
    131 	 */
    132 	if (ipsec6_in_reject(m, NULL)) {
    133 		ipsec6stat.in_polvio++;
    134 		m_freem(m);
    135 		return;
    136 	}
    137 #endif /* IPSEC */
    138 
    139 	/*
    140 	 * Do not forward packets to multicast destination (should be handled
    141 	 * by ip6_mforward().
    142 	 * Do not forward packets with unspecified source.  It was discussed
    143 	 * in July 2000, on ipngwg mailing list.
    144 	 */
    145 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
    146 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
    147 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
    148 		ip6stat.ip6s_cantforward++;
    149 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
    150 		if (ip6_log_time + ip6_log_interval < time_second) {
    151 			ip6_log_time = time_second;
    152 			log(LOG_DEBUG,
    153 			    "cannot forward "
    154 			    "from %s to %s nxt %d received on %s\n",
    155 			    ip6_sprintf(&ip6->ip6_src),
    156 			    ip6_sprintf(&ip6->ip6_dst),
    157 			    ip6->ip6_nxt,
    158 			    if_name(m->m_pkthdr.rcvif));
    159 		}
    160 		m_freem(m);
    161 		return;
    162 	}
    163 
    164 	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
    165 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
    166 		icmp6_error(m, ICMP6_TIME_EXCEEDED,
    167 				ICMP6_TIME_EXCEED_TRANSIT, 0);
    168 		return;
    169 	}
    170 	ip6->ip6_hlim -= IPV6_HLIMDEC;
    171 
    172 	/*
    173 	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
    174 	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
    175 	 * we need to generate an ICMP6 message to the src.
    176 	 * Thanks to M_EXT, in most cases copy will not occur.
    177 	 *
    178 	 * It is important to save it before IPsec processing as IPsec
    179 	 * processing may modify the mbuf.
    180 	 */
    181 	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
    182 
    183 #ifdef IPSEC
    184 	/* get a security policy for this packet */
    185 	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
    186 	    IP_FORWARDING, &error);
    187 	if (sp == NULL) {
    188 		ipsec6stat.out_inval++;
    189 		ip6stat.ip6s_cantforward++;
    190 		if (mcopy) {
    191 #if 0
    192 			/* XXX: what icmp ? */
    193 #else
    194 			m_freem(mcopy);
    195 #endif
    196 		}
    197 		m_freem(m);
    198 		return;
    199 	}
    200 
    201 	error = 0;
    202 
    203 	/* check policy */
    204 	switch (sp->policy) {
    205 	case IPSEC_POLICY_DISCARD:
    206 		/*
    207 		 * This packet is just discarded.
    208 		 */
    209 		ipsec6stat.out_polvio++;
    210 		ip6stat.ip6s_cantforward++;
    211 		key_freesp(sp);
    212 		if (mcopy) {
    213 #if 0
    214 			/* XXX: what icmp ? */
    215 #else
    216 			m_freem(mcopy);
    217 #endif
    218 		}
    219 		m_freem(m);
    220 		return;
    221 
    222 	case IPSEC_POLICY_BYPASS:
    223 	case IPSEC_POLICY_NONE:
    224 		/* no need to do IPsec. */
    225 		key_freesp(sp);
    226 		goto skip_ipsec;
    227 
    228 	case IPSEC_POLICY_IPSEC:
    229 		if (sp->req == NULL) {
    230 			/* XXX should be panic ? */
    231 			printf("ip6_forward: No IPsec request specified.\n");
    232 			ip6stat.ip6s_cantforward++;
    233 			key_freesp(sp);
    234 			if (mcopy) {
    235 #if 0
    236 				/* XXX: what icmp ? */
    237 #else
    238 				m_freem(mcopy);
    239 #endif
    240 			}
    241 			m_freem(m);
    242 			return;
    243 		}
    244 		/* do IPsec */
    245 		break;
    246 
    247 	case IPSEC_POLICY_ENTRUST:
    248 	default:
    249 		/* should be panic ?? */
    250 		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
    251 		key_freesp(sp);
    252 		goto skip_ipsec;
    253 	}
    254 
    255     {
    256 	struct ipsecrequest *isr = NULL;
    257 	struct ipsec_output_state state;
    258 
    259 	/*
    260 	 * when the kernel forwards a packet, it is not proper to apply
    261 	 * IPsec transport mode to the packet is not proper.  this check
    262 	 * avoid from this.
    263 	 * at present, if there is even a transport mode SA request in the
    264 	 * security policy, the kernel does not apply IPsec to the packet.
    265 	 * this check is not enough because the following case is valid.
    266 	 *      ipsec esp/tunnel/xxx-xxx/require esp/transport//require;
    267 	 */
    268 	for (isr = sp->req; isr; isr = isr->next) {
    269 		if (isr->saidx.mode == IPSEC_MODE_ANY)
    270 			goto doipsectunnel;
    271 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
    272 			goto doipsectunnel;
    273 	}
    274 
    275 	/*
    276 	 * if there's no need for tunnel mode IPsec, skip.
    277 	 */
    278 	if (!isr)
    279 		goto skip_ipsec;
    280 
    281     doipsectunnel:
    282 	/*
    283 	 * All the extension headers will become inaccessible
    284 	 * (since they can be encrypted).
    285 	 * Don't panic, we need no more updates to extension headers
    286 	 * on inner IPv6 packet (since they are now encapsulated).
    287 	 *
    288 	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
    289 	 */
    290 	bzero(&state, sizeof(state));
    291 	state.m = m;
    292 	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
    293 	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
    294 
    295 	error = ipsec6_output_tunnel(&state, sp, 0);
    296 
    297 	m = state.m;
    298 	key_freesp(sp);
    299 
    300 	if (error) {
    301 		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
    302 		switch (error) {
    303 		case EHOSTUNREACH:
    304 		case ENETUNREACH:
    305 		case EMSGSIZE:
    306 		case ENOBUFS:
    307 		case ENOMEM:
    308 			break;
    309 		default:
    310 			printf("ip6_forward (ipsec): error code %d\n", error);
    311 			/* FALLTHROUGH */
    312 		case ENOENT:
    313 			/* don't show these error codes to the user */
    314 			break;
    315 		}
    316 		ip6stat.ip6s_cantforward++;
    317 		if (mcopy) {
    318 #if 0
    319 			/* XXX: what icmp ? */
    320 #else
    321 			m_freem(mcopy);
    322 #endif
    323 		}
    324 		m_freem(m);
    325 		return;
    326 	}
    327 
    328 	if (ip6 != mtod(m, struct ip6_hdr *)) {
    329 		/*
    330 		 * now tunnel mode headers are added.  we are originating
    331 		 * packet instead of forwarding the packet.
    332 		 */
    333 		ip6_output(m, NULL, NULL, IPV6_FORWARDING/*XXX*/, NULL, NULL,
    334 		    NULL);
    335 		goto freecopy;
    336 	}
    337 
    338 	/* adjust pointer */
    339 	rt = state.ro ? state.ro->ro_rt : NULL;
    340 	dst = (struct sockaddr_in6 *)state.dst;
    341 	if (dst != NULL && rt != NULL) {
    342 		ipsecrt = 1;
    343 		goto skip_routing;
    344 	}
    345     }
    346     skip_ipsec:
    347 #endif /* IPSEC */
    348 #ifdef FAST_IPSEC
    349 	/* Check the security policy (SP) for the packet */
    350 
    351 	sp = ipsec6_check_policy(m,NULL,0,&needipsec,&error);
    352 	if (error != 0) {
    353 		/*
    354 		 * Hack: -EINVAL is used to signal that a packet
    355 		 * should be silently discarded.  This is typically
    356 		 * because we asked key management for an SA and
    357 		 * it was delayed (e.g. kicked up to IKE).
    358 		 */
    359 	if (error == -EINVAL)
    360 		error = 0;
    361 	goto freecopy;
    362 	}
    363 #endif /* FAST_IPSEC */
    364 
    365 
    366 
    367 	dst = &ip6_forward_rt.ro_dst;
    368 	if (!srcrt) {
    369 		/*
    370 		 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
    371 		 */
    372 		rtcache_check((struct route *)&ip6_forward_rt);
    373 		if (ip6_forward_rt.ro_rt == NULL) {
    374 			rtcache_init((struct route *)&ip6_forward_rt);
    375 
    376 			if (ip6_forward_rt.ro_rt == NULL) {
    377 				ip6stat.ip6s_noroute++;
    378 				/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
    379 				if (mcopy) {
    380 					icmp6_error(mcopy, ICMP6_DST_UNREACH,
    381 					    ICMP6_DST_UNREACH_NOROUTE, 0);
    382 				}
    383 				m_freem(m);
    384 				return;
    385 			}
    386 		}
    387 	} else {
    388 		if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr))
    389 			rtcache_free((struct route *)&ip6_forward_rt);
    390 		else
    391 			rtcache_check((struct route *)&ip6_forward_rt);
    392 		if (ip6_forward_rt.ro_rt == NULL) {
    393 			memset(dst, 0, sizeof(*dst));
    394 			dst->sin6_len = sizeof(struct sockaddr_in6);
    395 			dst->sin6_family = AF_INET6;
    396 			dst->sin6_addr = ip6->ip6_dst;
    397 			rtcache_init((struct route *)&ip6_forward_rt);
    398 		}
    399 		if (ip6_forward_rt.ro_rt == NULL) {
    400 			ip6stat.ip6s_noroute++;
    401 			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
    402 			if (mcopy) {
    403 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
    404 					    ICMP6_DST_UNREACH_NOROUTE, 0);
    405 			}
    406 			m_freem(m);
    407 			return;
    408 		}
    409 	}
    410 	rt = ip6_forward_rt.ro_rt;
    411 #ifdef IPSEC
    412     skip_routing:;
    413 #endif /* IPSEC */
    414 
    415 	/*
    416 	 * Source scope check: if a packet can't be delivered to its
    417 	 * destination for the reason that the destination is beyond the scope
    418 	 * of the source address, discard the packet and return an icmp6
    419 	 * destination unreachable error with Code 2 (beyond scope of source
    420 	 * address).  We use a local copy of ip6_src, since in6_setscope()
    421 	 * will possibly modify its first argument.
    422 	 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
    423 	 */
    424 	src_in6 = ip6->ip6_src;
    425 	if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
    426 		/* XXX: this should not happen */
    427 		ip6stat.ip6s_cantforward++;
    428 		ip6stat.ip6s_badscope++;
    429 		m_freem(m);
    430 		return;
    431 	}
    432 	if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
    433 		ip6stat.ip6s_cantforward++;
    434 		ip6stat.ip6s_badscope++;
    435 		m_freem(m);
    436 		return;
    437 	}
    438 	if (inzone != outzone
    439 #ifdef IPSEC
    440 	    && !ipsecrt
    441 #endif
    442 	    ) {
    443 		ip6stat.ip6s_cantforward++;
    444 		ip6stat.ip6s_badscope++;
    445 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
    446 
    447 		if (ip6_log_time + ip6_log_interval < time_second) {
    448 			ip6_log_time = time_second;
    449 			log(LOG_DEBUG,
    450 			    "cannot forward "
    451 			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
    452 			    ip6_sprintf(&ip6->ip6_src),
    453 			    ip6_sprintf(&ip6->ip6_dst),
    454 			    ip6->ip6_nxt,
    455 			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
    456 		}
    457 		if (mcopy)
    458 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
    459 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
    460 		m_freem(m);
    461 		return;
    462 	}
    463 #ifdef FAST_IPSEC
    464     /*
    465      * If we need to encapsulate the packet, do it here
    466      * ipsec6_proces_packet will send the packet using ip6_output
    467      */
    468 	if (needipsec) {
    469 		s = splsoftnet();
    470 		error = ipsec6_process_packet(m,sp->req);
    471 		splx(s);
    472 		if (mcopy)
    473 			goto freecopy;
    474     }
    475 #endif
    476 
    477 
    478 
    479 	/*
    480 	 * Destination scope check: if a packet is going to break the scope
    481 	 * zone of packet's destination address, discard it.  This case should
    482 	 * usually be prevented by appropriately-configured routing table, but
    483 	 * we need an explicit check because we may mistakenly forward the
    484 	 * packet to a different zone by (e.g.) a default route.
    485 	 */
    486 	dst_in6 = ip6->ip6_dst;
    487 	if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
    488 	    in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
    489 	    inzone != outzone) {
    490 		ip6stat.ip6s_cantforward++;
    491 		ip6stat.ip6s_badscope++;
    492 		m_freem(m);
    493 		return;
    494 	}
    495 
    496 	if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
    497 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
    498 		if (mcopy) {
    499 			u_long mtu;
    500 #ifdef IPSEC
    501 			struct secpolicy *xsp;
    502 			int ipsecerror;
    503 			size_t ipsechdrsiz;
    504 #endif
    505 
    506 			mtu = IN6_LINKMTU(rt->rt_ifp);
    507 #ifdef IPSEC
    508 			/*
    509 			 * When we do IPsec tunnel ingress, we need to play
    510 			 * with the link value (decrement IPsec header size
    511 			 * from mtu value).  The code is much simpler than v4
    512 			 * case, as we have the outgoing interface for
    513 			 * encapsulated packet as "rt->rt_ifp".
    514 			 */
    515 			xsp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
    516 				IP_FORWARDING, &ipsecerror);
    517 			if (xsp) {
    518 				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
    519 					IPSEC_DIR_OUTBOUND, NULL);
    520 				if (ipsechdrsiz < mtu)
    521 					mtu -= ipsechdrsiz;
    522 			}
    523 
    524 			/*
    525 			 * if mtu becomes less than minimum MTU,
    526 			 * tell minimum MTU (and I'll need to fragment it).
    527 			 */
    528 			if (mtu < IPV6_MMTU)
    529 				mtu = IPV6_MMTU;
    530 #endif
    531 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
    532 		}
    533 		m_freem(m);
    534 		return;
    535 	}
    536 
    537 	if (rt->rt_flags & RTF_GATEWAY)
    538 		dst = (struct sockaddr_in6 *)rt->rt_gateway;
    539 
    540 	/*
    541 	 * If we are to forward the packet using the same interface
    542 	 * as one we got the packet from, perhaps we should send a redirect
    543 	 * to sender to shortcut a hop.
    544 	 * Only send redirect if source is sending directly to us,
    545 	 * and if packet was not source routed (or has any options).
    546 	 * Also, don't send redirect if forwarding using a route
    547 	 * modified by a redirect.
    548 	 */
    549 	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && ip6_sendredirects &&
    550 #ifdef IPSEC
    551 	    !ipsecrt &&
    552 #endif
    553 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
    554 		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
    555 		    nd6_is_addr_neighbor(
    556 		        satocsin6(rtcache_getdst((struct route *)&ip6_forward_rt)),
    557 			rt->rt_ifp)) {
    558 			/*
    559 			 * If the incoming interface is equal to the outgoing
    560 			 * one, the link attached to the interface is
    561 			 * point-to-point, and the IPv6 destination is
    562 			 * regarded as on-link on the link, then it will be
    563 			 * highly probable that the destination address does
    564 			 * not exist on the link and that the packet is going
    565 			 * to loop.  Thus, we immediately drop the packet and
    566 			 * send an ICMPv6 error message.
    567 			 * For other routing loops, we dare to let the packet
    568 			 * go to the loop, so that a remote diagnosing host
    569 			 * can detect the loop by traceroute.
    570 			 * type/code is based on suggestion by Rich Draves.
    571 			 * not sure if it is the best pick.
    572 			 */
    573 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
    574 				    ICMP6_DST_UNREACH_ADDR, 0);
    575 			m_freem(m);
    576 			return;
    577 		}
    578 		type = ND_REDIRECT;
    579 	}
    580 
    581 	/*
    582 	 * Fake scoped addresses. Note that even link-local source or
    583 	 * destinaion can appear, if the originating node just sends the
    584 	 * packet to us (without address resolution for the destination).
    585 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
    586 	 * link identifiers, we can do this stuff after making a copy for
    587 	 * returning an error.
    588 	 */
    589 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
    590 		/*
    591 		 * See corresponding comments in ip6_output.
    592 		 * XXX: but is it possible that ip6_forward() sends a packet
    593 		 *      to a loopback interface? I don't think so, and thus
    594 		 *      I bark here. (jinmei (at) kame.net)
    595 		 * XXX: it is common to route invalid packets to loopback.
    596 		 *	also, the codepath will be visited on use of ::1 in
    597 		 *	rthdr. (itojun)
    598 		 */
    599 #if 1
    600 		if (0)
    601 #else
    602 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
    603 #endif
    604 		{
    605 			printf("ip6_forward: outgoing interface is loopback. "
    606 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
    607 			       ip6_sprintf(&ip6->ip6_src),
    608 			       ip6_sprintf(&ip6->ip6_dst),
    609 			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
    610 			       if_name(rt->rt_ifp));
    611 		}
    612 
    613 		/* we can just use rcvif in forwarding. */
    614 		origifp = m->m_pkthdr.rcvif;
    615 	}
    616 	else
    617 		origifp = rt->rt_ifp;
    618 	/*
    619 	 * clear embedded scope identifiers if necessary.
    620 	 * in6_clearscope will touch the addresses only when necessary.
    621 	 */
    622 	in6_clearscope(&ip6->ip6_src);
    623 	in6_clearscope(&ip6->ip6_dst);
    624 
    625 #ifdef PFIL_HOOKS
    626 	/*
    627 	 * Run through list of hooks for output packets.
    628 	 */
    629 	if ((error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp,
    630 	    PFIL_OUT)) != 0)
    631 		goto senderr;
    632 	if (m == NULL)
    633 		goto freecopy;
    634 	ip6 = mtod(m, struct ip6_hdr *);
    635 #endif /* PFIL_HOOKS */
    636 
    637 	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
    638 	if (error) {
    639 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
    640 		ip6stat.ip6s_cantforward++;
    641 	} else {
    642 		ip6stat.ip6s_forward++;
    643 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
    644 		if (type)
    645 			ip6stat.ip6s_redirectsent++;
    646 		else {
    647 			if (mcopy)
    648 				goto freecopy;
    649 		}
    650 	}
    651 
    652 #ifdef PFIL_HOOKS
    653  senderr:
    654 #endif
    655 	if (mcopy == NULL)
    656 		return;
    657 	switch (error) {
    658 	case 0:
    659 		if (type == ND_REDIRECT) {
    660 			icmp6_redirect_output(mcopy, rt);
    661 			return;
    662 		}
    663 		goto freecopy;
    664 
    665 	case EMSGSIZE:
    666 		/* xxx MTU is constant in PPP? */
    667 		goto freecopy;
    668 
    669 	case ENOBUFS:
    670 		/* Tell source to slow down like source quench in IP? */
    671 		goto freecopy;
    672 
    673 	case ENETUNREACH:	/* shouldn't happen, checked above */
    674 	case EHOSTUNREACH:
    675 	case ENETDOWN:
    676 	case EHOSTDOWN:
    677 	default:
    678 		type = ICMP6_DST_UNREACH;
    679 		code = ICMP6_DST_UNREACH_ADDR;
    680 		break;
    681 	}
    682 	icmp6_error(mcopy, type, code, 0);
    683 	return;
    684 
    685  freecopy:
    686 	m_freem(mcopy);
    687 	return;
    688 }
    689