Home | History | Annotate | Line # | Download | only in netinet6
ip6_forward.c revision 1.100
      1  1.100     ozaki /*	$NetBSD: ip6_forward.c,v 1.100 2020/08/28 06:19:13 ozaki-r Exp $	*/
      2   1.32    itojun /*	$KAME: ip6_forward.c,v 1.109 2002/09/11 08:10:17 sakane Exp $	*/
      3    1.3   thorpej 
      4    1.2    itojun /*
      5    1.2    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6    1.2    itojun  * All rights reserved.
      7   1.10    itojun  *
      8    1.2    itojun  * Redistribution and use in source and binary forms, with or without
      9    1.2    itojun  * modification, are permitted provided that the following conditions
     10    1.2    itojun  * are met:
     11    1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     12    1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     13    1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14    1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     15    1.2    itojun  *    documentation and/or other materials provided with the distribution.
     16    1.2    itojun  * 3. Neither the name of the project nor the names of its contributors
     17    1.2    itojun  *    may be used to endorse or promote products derived from this software
     18    1.2    itojun  *    without specific prior written permission.
     19   1.10    itojun  *
     20    1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21    1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22    1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23    1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24    1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25    1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26    1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27    1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28    1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29    1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30    1.2    itojun  * SUCH DAMAGE.
     31    1.2    itojun  */
     32   1.26     lukem 
     33   1.26     lukem #include <sys/cdefs.h>
     34  1.100     ozaki __KERNEL_RCSID(0, "$NetBSD: ip6_forward.c,v 1.100 2020/08/28 06:19:13 ozaki-r Exp $");
     35    1.2    itojun 
     36   1.78     pooka #ifdef _KERNEL_OPT
     37   1.68     joerg #include "opt_gateway.h"
     38   1.15    itojun #include "opt_ipsec.h"
     39   1.78     pooka #endif
     40   1.15    itojun 
     41    1.2    itojun #include <sys/param.h>
     42    1.2    itojun #include <sys/systm.h>
     43    1.2    itojun #include <sys/mbuf.h>
     44    1.2    itojun #include <sys/errno.h>
     45    1.2    itojun #include <sys/time.h>
     46    1.2    itojun #include <sys/kernel.h>
     47    1.2    itojun #include <sys/syslog.h>
     48   1.81     ozaki #include <sys/percpu.h>
     49    1.2    itojun 
     50    1.2    itojun #include <net/if.h>
     51    1.2    itojun #include <net/route.h>
     52   1.72     rmind #include <net/pfil.h>
     53    1.2    itojun 
     54    1.2    itojun #include <netinet/in.h>
     55    1.2    itojun #include <netinet/in_var.h>
     56    1.7    itojun #include <netinet/ip_var.h>
     57    1.8    itojun #include <netinet/ip6.h>
     58    1.2    itojun #include <netinet6/ip6_var.h>
     59   1.64   thorpej #include <netinet6/ip6_private.h>
     60   1.47    rpaulo #include <netinet6/scope6_var.h>
     61    1.8    itojun #include <netinet/icmp6.h>
     62    1.5    itojun #include <netinet6/nd6.h>
     63    1.5    itojun 
     64   1.71  christos #ifdef IPSEC
     65   1.54  degroote #include <netipsec/ipsec.h>
     66   1.54  degroote #include <netipsec/ipsec6.h>
     67   1.54  degroote #include <netipsec/key.h>
     68   1.93      maxv #endif
     69   1.54  degroote 
     70   1.81     ozaki extern percpu_t *ip6_forward_rt_percpu;
     71    1.2    itojun 
     72   1.72     rmind extern pfil_head_t *inet6_pfil_hook;	/* XXX */
     73   1.20    itojun 
     74   1.76  christos static void __printflike(4, 5)
     75   1.75  christos ip6_cantforward(const struct ip6_hdr *ip6, const struct ifnet *srcifp,
     76   1.75  christos     const struct ifnet *dstifp, const char *fmt, ...)
     77   1.75  christos {
     78   1.75  christos 	char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
     79   1.75  christos 	char reason[256];
     80   1.75  christos 	va_list ap;
     81   1.75  christos 	uint64_t *ip6s;
     82   1.75  christos 
     83   1.75  christos 	/* update statistics */
     84   1.75  christos 	ip6s = IP6_STAT_GETREF();
     85   1.75  christos 	ip6s[IP6_STAT_CANTFORWARD]++;
     86   1.75  christos 	if (dstifp)
     87   1.75  christos 		ip6s[IP6_STAT_BADSCOPE]++;
     88   1.75  christos 	IP6_STAT_PUTREF();
     89   1.75  christos 
     90   1.75  christos 	if (dstifp)
     91   1.75  christos 		in6_ifstat_inc(dstifp, ifs6_in_discard);
     92   1.75  christos 
     93   1.77     ozaki 	if (ip6_log_time + ip6_log_interval >= time_uptime)
     94   1.75  christos 		return;
     95   1.77     ozaki 	ip6_log_time = time_uptime;
     96   1.75  christos 
     97   1.75  christos 	va_start(ap, fmt);
     98   1.76  christos 	vsnprintf(reason, sizeof(reason), fmt, ap);
     99   1.75  christos 	va_end(ap);
    100   1.75  christos 
    101   1.75  christos 	log(LOG_DEBUG, "Cannot forward from %s@%s to %s@%s nxt %d (%s)\n",
    102   1.75  christos 	    IN6_PRINT(sbuf, &ip6->ip6_src), srcifp ? if_name(srcifp) : "?",
    103   1.75  christos 	    IN6_PRINT(dbuf, &ip6->ip6_dst), dstifp ? if_name(dstifp) : "?",
    104   1.75  christos 	    ip6->ip6_nxt, reason);
    105   1.75  christos }
    106   1.75  christos 
    107    1.2    itojun /*
    108    1.2    itojun  * Forward a packet.  If some error occurs return the sender
    109    1.2    itojun  * an icmp packet.  Note we can't always generate a meaningful
    110    1.2    itojun  * icmp message because icmp doesn't have a large enough repertoire
    111    1.2    itojun  * of codes and types.
    112    1.2    itojun  *
    113    1.2    itojun  * If not forwarding, just drop the packet.  This could be confusing
    114    1.2    itojun  * if ipforwarding was zero but some routing protocol was advancing
    115    1.2    itojun  * us as a gateway to somewhere.  However, we must let the routing
    116    1.2    itojun  * protocol deal with that.
    117    1.2    itojun  */
    118    1.2    itojun void
    119   1.58  christos ip6_forward(struct mbuf *m, int srcrt)
    120    1.2    itojun {
    121    1.5    itojun 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    122   1.57    dyoung 	const struct sockaddr_in6 *dst;
    123   1.82     ozaki 	struct rtentry *rt = NULL;
    124   1.42    itojun 	int error = 0, type = 0, code = 0;
    125    1.5    itojun 	struct mbuf *mcopy = NULL;
    126   1.10    itojun 	struct ifnet *origifp;	/* maybe unnecessary */
    127   1.75  christos 	uint32_t inzone, outzone;
    128   1.47    rpaulo 	struct in6_addr src_in6, dst_in6;
    129   1.79     ozaki 	struct ifnet *rcvif = NULL;
    130   1.79     ozaki 	struct psref psref;
    131   1.82     ozaki 	struct route *ro = NULL;
    132   1.71  christos #ifdef IPSEC
    133   1.73  christos 	int needipsec = 0;
    134   1.71  christos 	struct secpolicy *sp = NULL;
    135   1.54  degroote #endif
    136   1.54  degroote 
    137   1.67     joerg 	/*
    138   1.67     joerg 	 * Clear any in-bound checksum flags for this packet.
    139   1.67     joerg 	 */
    140   1.67     joerg 	m->m_pkthdr.csum_flags = 0;
    141    1.5    itojun 
    142   1.79     ozaki 	rcvif = m_get_rcvif_psref(m, &psref);
    143   1.80     ozaki 	if (__predict_false(rcvif == NULL))
    144   1.80     ozaki 		goto drop;
    145   1.80     ozaki 
    146   1.16    itojun 	/*
    147   1.16    itojun 	 * Do not forward packets to multicast destination (should be handled
    148   1.92      maxv 	 * by ip6_mforward()). Do not forward packets with unspecified source.
    149   1.92      maxv 	 * It was discussed in July 2000, on ipngwg mailing list.
    150   1.16    itojun 	 */
    151    1.9    itojun 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
    152   1.16    itojun 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
    153   1.16    itojun 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
    154   1.79     ozaki 		ip6_cantforward(ip6, rcvif, NULL,
    155   1.75  christos 		    ((m->m_flags & (M_BCAST|M_MCAST)) != 0) ? "bcast/mcast" :
    156   1.75  christos 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ? "mcast/dst" :
    157   1.75  christos 		    "unspec/src");
    158   1.79     ozaki 		goto drop;
    159    1.2    itojun 	}
    160    1.2    itojun 
    161    1.2    itojun 	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
    162    1.5    itojun 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
    163    1.2    itojun 		icmp6_error(m, ICMP6_TIME_EXCEEDED,
    164    1.2    itojun 				ICMP6_TIME_EXCEED_TRANSIT, 0);
    165   1.79     ozaki 		goto out;
    166    1.2    itojun 	}
    167    1.2    itojun 	ip6->ip6_hlim -= IPV6_HLIMDEC;
    168    1.2    itojun 
    169    1.5    itojun 	/*
    170    1.5    itojun 	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
    171    1.5    itojun 	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
    172    1.5    itojun 	 * we need to generate an ICMP6 message to the src.
    173    1.5    itojun 	 * Thanks to M_EXT, in most cases copy will not occur.
    174    1.5    itojun 	 *
    175    1.5    itojun 	 * It is important to save it before IPsec processing as IPsec
    176    1.5    itojun 	 * processing may modify the mbuf.
    177    1.5    itojun 	 */
    178   1.94      maxv 	mcopy = m_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
    179   1.94      maxv 	    M_DONTWAIT);
    180    1.5    itojun 
    181   1.71  christos #ifdef IPSEC
    182   1.73  christos 	if (ipsec_used) {
    183   1.73  christos 		/* Check the security policy (SP) for the packet */
    184   1.73  christos 		sp = ipsec6_check_policy(m, NULL, 0, &needipsec, &error);
    185   1.73  christos 		if (error != 0) {
    186   1.73  christos 			/*
    187   1.73  christos 			 * Hack: -EINVAL is used to signal that a packet
    188   1.73  christos 			 * should be silently discarded.  This is typically
    189   1.73  christos 			 * because we asked key management for an SA and
    190   1.73  christos 			 * it was delayed (e.g. kicked up to IKE).
    191   1.73  christos 			 */
    192   1.73  christos 			if (error == -EINVAL)
    193   1.73  christos 				error = 0;
    194   1.91      maxv 			m_freem(m);
    195  1.100     ozaki 			IP6_STATINC(IP6_STAT_IPSECDROP_OUT);
    196   1.73  christos 			goto freecopy;
    197   1.73  christos 		}
    198   1.54  degroote 	}
    199   1.92      maxv #endif
    200   1.54  degroote 
    201   1.97     ozaki 	ro = rtcache_percpu_getref(ip6_forward_rt_percpu);
    202   1.61    dyoung 	if (srcrt) {
    203   1.57    dyoung 		union {
    204   1.57    dyoung 			struct sockaddr		dst;
    205   1.57    dyoung 			struct sockaddr_in6	dst6;
    206   1.57    dyoung 		} u;
    207   1.57    dyoung 
    208   1.57    dyoung 		sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
    209   1.82     ozaki 		rt = rtcache_lookup(ro, &u.dst);
    210   1.82     ozaki 		if (rt == NULL) {
    211   1.64   thorpej 			IP6_STATINC(IP6_STAT_NOROUTE);
    212    1.5    itojun 			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
    213    1.5    itojun 			if (mcopy) {
    214    1.5    itojun 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
    215    1.5    itojun 					    ICMP6_DST_UNREACH_NOROUTE, 0);
    216    1.5    itojun 			}
    217   1.79     ozaki 			goto drop;
    218    1.2    itojun 		}
    219   1.81     ozaki 	} else if ((rt = rtcache_validate(ro)) == NULL &&
    220   1.81     ozaki 	           (rt = rtcache_update(ro, 1)) == NULL) {
    221   1.61    dyoung 		/*
    222   1.61    dyoung 		 * rtcache_getdst(ip6_forward_rt)->sin6_addr was equal to
    223   1.61    dyoung 		 * ip6->ip6_dst
    224   1.61    dyoung 		 */
    225   1.64   thorpej 		IP6_STATINC(IP6_STAT_NOROUTE);
    226   1.61    dyoung 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
    227   1.61    dyoung 		if (mcopy) {
    228   1.61    dyoung 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
    229   1.61    dyoung 			    ICMP6_DST_UNREACH_NOROUTE, 0);
    230   1.61    dyoung 		}
    231   1.79     ozaki 		goto drop;
    232    1.2    itojun 	}
    233   1.81     ozaki 	dst = satocsin6(rtcache_getdst(ro));
    234    1.9    itojun 
    235    1.9    itojun 	/*
    236   1.47    rpaulo 	 * Source scope check: if a packet can't be delivered to its
    237   1.47    rpaulo 	 * destination for the reason that the destination is beyond the scope
    238   1.47    rpaulo 	 * of the source address, discard the packet and return an icmp6
    239   1.47    rpaulo 	 * destination unreachable error with Code 2 (beyond scope of source
    240   1.47    rpaulo 	 * address).  We use a local copy of ip6_src, since in6_setscope()
    241   1.47    rpaulo 	 * will possibly modify its first argument.
    242   1.47    rpaulo 	 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
    243   1.47    rpaulo 	 */
    244   1.47    rpaulo 	src_in6 = ip6->ip6_src;
    245   1.75  christos 	inzone = outzone = ~0;
    246   1.75  christos 	if (in6_setscope(&src_in6, rt->rt_ifp, &outzone) != 0 ||
    247   1.79     ozaki 	    in6_setscope(&src_in6, rcvif, &inzone) != 0 ||
    248   1.75  christos 	    inzone != outzone) {
    249   1.79     ozaki 		ip6_cantforward(ip6, rcvif, rt->rt_ifp,
    250   1.92      maxv 		    "src[%s] inzone %d outzone %d",
    251   1.76  christos 		    in6_getscopename(&ip6->ip6_src), inzone, outzone);
    252    1.9    itojun 		if (mcopy)
    253    1.9    itojun 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
    254    1.9    itojun 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
    255   1.79     ozaki 		goto drop;
    256    1.9    itojun 	}
    257   1.75  christos 
    258   1.71  christos #ifdef IPSEC
    259   1.71  christos 	/*
    260   1.92      maxv 	 * If we need to encapsulate the packet, do it here.
    261   1.92      maxv 	 * ipsec6_process_packet will send the packet using ip6_output.
    262   1.71  christos 	 */
    263   1.54  degroote 	if (needipsec) {
    264   1.73  christos 		int s = splsoftnet();
    265   1.98  knakahar 		error = ipsec6_process_packet(m, sp->req, 0);
    266   1.54  degroote 		splx(s);
    267   1.91      maxv 		/* m is freed */
    268   1.54  degroote 		if (mcopy)
    269   1.54  degroote 			goto freecopy;
    270   1.91      maxv 		goto out;
    271   1.71  christos 	}
    272   1.92      maxv #endif
    273   1.54  degroote 
    274   1.47    rpaulo 	/*
    275   1.47    rpaulo 	 * Destination scope check: if a packet is going to break the scope
    276   1.47    rpaulo 	 * zone of packet's destination address, discard it.  This case should
    277   1.47    rpaulo 	 * usually be prevented by appropriately-configured routing table, but
    278   1.47    rpaulo 	 * we need an explicit check because we may mistakenly forward the
    279   1.47    rpaulo 	 * packet to a different zone by (e.g.) a default route.
    280   1.47    rpaulo 	 */
    281   1.47    rpaulo 	dst_in6 = ip6->ip6_dst;
    282   1.75  christos 	inzone = outzone = ~0;
    283   1.79     ozaki 	if (in6_setscope(&dst_in6, rcvif, &inzone) != 0 ||
    284   1.47    rpaulo 	    in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
    285   1.47    rpaulo 	    inzone != outzone) {
    286   1.79     ozaki 		ip6_cantforward(ip6, rcvif, rt->rt_ifp,
    287   1.76  christos 		    "dst[%s] inzone %d outzone %d",
    288   1.76  christos 		    in6_getscopename(&ip6->ip6_dst), inzone, outzone);
    289   1.75  christos 		if (mcopy)
    290   1.75  christos 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
    291   1.75  christos 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
    292   1.79     ozaki 		goto drop;
    293   1.47    rpaulo 	}
    294   1.47    rpaulo 
    295   1.99       roy 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
    296    1.5    itojun 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
    297   1.99       roy 		if (mcopy)
    298   1.99       roy 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0,
    299   1.99       roy 			    rt->rt_ifp->if_mtu);
    300   1.79     ozaki 		goto drop;
    301   1.31    itojun 	}
    302    1.2    itojun 
    303    1.2    itojun 	if (rt->rt_flags & RTF_GATEWAY)
    304    1.2    itojun 		dst = (struct sockaddr_in6 *)rt->rt_gateway;
    305    1.2    itojun 
    306    1.2    itojun 	/*
    307    1.2    itojun 	 * If we are to forward the packet using the same interface
    308    1.2    itojun 	 * as one we got the packet from, perhaps we should send a redirect
    309    1.2    itojun 	 * to sender to shortcut a hop.
    310    1.2    itojun 	 * Only send redirect if source is sending directly to us,
    311    1.2    itojun 	 * and if packet was not source routed (or has any options).
    312    1.2    itojun 	 * Also, don't send redirect if forwarding using a route
    313    1.2    itojun 	 * modified by a redirect.
    314    1.2    itojun 	 */
    315   1.79     ozaki 	if (rt->rt_ifp == rcvif && !srcrt && ip6_sendredirects &&
    316   1.22    itojun 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
    317   1.23    itojun 		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
    318   1.81     ozaki 		    nd6_is_addr_neighbor(satocsin6(rtcache_getdst(ro)),
    319   1.81     ozaki 		                         rt->rt_ifp)) {
    320   1.22    itojun 			/*
    321   1.22    itojun 			 * If the incoming interface is equal to the outgoing
    322   1.23    itojun 			 * one, the link attached to the interface is
    323   1.23    itojun 			 * point-to-point, and the IPv6 destination is
    324   1.23    itojun 			 * regarded as on-link on the link, then it will be
    325   1.23    itojun 			 * highly probable that the destination address does
    326   1.23    itojun 			 * not exist on the link and that the packet is going
    327   1.23    itojun 			 * to loop.  Thus, we immediately drop the packet and
    328   1.23    itojun 			 * send an ICMPv6 error message.
    329   1.23    itojun 			 * For other routing loops, we dare to let the packet
    330   1.23    itojun 			 * go to the loop, so that a remote diagnosing host
    331   1.23    itojun 			 * can detect the loop by traceroute.
    332   1.22    itojun 			 * type/code is based on suggestion by Rich Draves.
    333   1.22    itojun 			 * not sure if it is the best pick.
    334   1.22    itojun 			 */
    335   1.22    itojun 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
    336   1.22    itojun 				    ICMP6_DST_UNREACH_ADDR, 0);
    337   1.79     ozaki 			goto drop;
    338   1.22    itojun 		}
    339    1.2    itojun 		type = ND_REDIRECT;
    340   1.22    itojun 	}
    341    1.2    itojun 
    342   1.10    itojun 	/*
    343   1.10    itojun 	 * Fake scoped addresses. Note that even link-local source or
    344   1.92      maxv 	 * destination can appear, if the originating node just sends the
    345   1.10    itojun 	 * packet to us (without address resolution for the destination).
    346   1.10    itojun 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
    347   1.18    itojun 	 * link identifiers, we can do this stuff after making a copy for
    348   1.18    itojun 	 * returning an error.
    349   1.10    itojun 	 */
    350   1.10    itojun 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
    351   1.10    itojun 		/*
    352   1.10    itojun 		 * See corresponding comments in ip6_output.
    353   1.10    itojun 		 * XXX: but is it possible that ip6_forward() sends a packet
    354   1.10    itojun 		 *      to a loopback interface? I don't think so, and thus
    355   1.10    itojun 		 *      I bark here. (jinmei (at) kame.net)
    356   1.12    itojun 		 * XXX: it is common to route invalid packets to loopback.
    357   1.13    itojun 		 *	also, the codepath will be visited on use of ::1 in
    358   1.13    itojun 		 *	rthdr. (itojun)
    359   1.10    itojun 		 */
    360   1.13    itojun #if 1
    361   1.13    itojun 		if (0)
    362   1.13    itojun #else
    363   1.13    itojun 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
    364   1.13    itojun #endif
    365   1.13    itojun 		{
    366   1.84       ryo 			char ip6bufs[INET6_ADDRSTRLEN];
    367   1.84       ryo 			char ip6bufd[INET6_ADDRSTRLEN];
    368   1.84       ryo 
    369   1.12    itojun 			printf("ip6_forward: outgoing interface is loopback. "
    370   1.12    itojun 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
    371   1.85  christos 			       IN6_PRINT(ip6bufs, &ip6->ip6_src),
    372   1.85  christos 			       IN6_PRINT(ip6bufd, &ip6->ip6_dst),
    373   1.79     ozaki 			       ip6->ip6_nxt, if_name(rcvif),
    374   1.12    itojun 			       if_name(rt->rt_ifp));
    375   1.12    itojun 		}
    376   1.13    itojun 
    377   1.19    itojun 		/* we can just use rcvif in forwarding. */
    378   1.79     ozaki 		origifp = rcvif;
    379   1.92      maxv 	} else {
    380   1.92      maxv 		origifp = rt->rt_ifp;
    381   1.10    itojun 	}
    382   1.92      maxv 
    383   1.47    rpaulo 	/*
    384   1.47    rpaulo 	 * clear embedded scope identifiers if necessary.
    385   1.47    rpaulo 	 * in6_clearscope will touch the addresses only when necessary.
    386   1.47    rpaulo 	 */
    387   1.47    rpaulo 	in6_clearscope(&ip6->ip6_src);
    388   1.47    rpaulo 	in6_clearscope(&ip6->ip6_dst);
    389   1.10    itojun 
    390   1.20    itojun 	/*
    391   1.20    itojun 	 * Run through list of hooks for output packets.
    392   1.20    itojun 	 */
    393   1.72     rmind 	if ((error = pfil_run_hooks(inet6_pfil_hook, &m, rt->rt_ifp,
    394   1.96     ozaki 	    PFIL_OUT)) != 0) {
    395   1.96     ozaki 		IP6_STATINC(IP6_STAT_PFILDROP_OUT);
    396   1.20    itojun 		goto senderr;
    397   1.96     ozaki 	}
    398   1.96     ozaki 	if (m == NULL) {
    399   1.96     ozaki 		IP6_STATINC(IP6_STAT_PFILDROP_OUT);
    400   1.20    itojun 		goto freecopy;
    401   1.96     ozaki 	}
    402   1.20    itojun 	ip6 = mtod(m, struct ip6_hdr *);
    403   1.20    itojun 
    404   1.86     ozaki 	error = ip6_if_output(rt->rt_ifp, origifp, m, dst, rt);
    405    1.5    itojun 	if (error) {
    406    1.5    itojun 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
    407   1.64   thorpej 		IP6_STATINC(IP6_STAT_CANTFORWARD);
    408    1.5    itojun 	} else {
    409   1.64   thorpej 		IP6_STATINC(IP6_STAT_FORWARD);
    410    1.5    itojun 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
    411    1.2    itojun 		if (type)
    412   1.64   thorpej 			IP6_STATINC(IP6_STAT_REDIRECTSENT);
    413    1.2    itojun 		else {
    414   1.56  liamjfoy #ifdef GATEWAY
    415   1.82     ozaki 			/* Need to release rt here */
    416   1.82     ozaki 			rtcache_unref(rt, ro);
    417   1.82     ozaki 			rt = NULL;
    418   1.90     ozaki 			if (mcopy->m_flags & M_CANFASTFWD)
    419   1.89     ozaki 				ip6flow_create(ro, mcopy);
    420   1.56  liamjfoy #endif
    421    1.2    itojun 			if (mcopy)
    422    1.2    itojun 				goto freecopy;
    423    1.2    itojun 		}
    424    1.2    itojun 	}
    425   1.20    itojun 
    426   1.92      maxv senderr:
    427    1.2    itojun 	if (mcopy == NULL)
    428   1.79     ozaki 		goto out;
    429    1.2    itojun 	switch (error) {
    430    1.2    itojun 	case 0:
    431    1.2    itojun 		if (type == ND_REDIRECT) {
    432    1.2    itojun 			icmp6_redirect_output(mcopy, rt);
    433   1.79     ozaki 			goto out;
    434    1.2    itojun 		}
    435    1.2    itojun 		goto freecopy;
    436    1.2    itojun 
    437    1.2    itojun 	case EMSGSIZE:
    438    1.2    itojun 		/* xxx MTU is constant in PPP? */
    439    1.2    itojun 		goto freecopy;
    440    1.2    itojun 
    441    1.2    itojun 	case ENOBUFS:
    442    1.2    itojun 		/* Tell source to slow down like source quench in IP? */
    443    1.2    itojun 		goto freecopy;
    444    1.2    itojun 
    445    1.2    itojun 	case ENETUNREACH:	/* shouldn't happen, checked above */
    446    1.2    itojun 	case EHOSTUNREACH:
    447    1.2    itojun 	case ENETDOWN:
    448    1.2    itojun 	case EHOSTDOWN:
    449    1.2    itojun 	default:
    450    1.2    itojun 		type = ICMP6_DST_UNREACH;
    451    1.2    itojun 		code = ICMP6_DST_UNREACH_ADDR;
    452    1.2    itojun 		break;
    453    1.2    itojun 	}
    454    1.2    itojun 	icmp6_error(mcopy, type, code, 0);
    455   1.79     ozaki 	goto out;
    456    1.2    itojun 
    457   1.92      maxv freecopy:
    458    1.2    itojun 	m_freem(mcopy);
    459   1.79     ozaki 	goto out;
    460   1.92      maxv 
    461   1.92      maxv drop:
    462   1.92      maxv 	m_freem(m);
    463   1.92      maxv 
    464   1.92      maxv out:
    465   1.87     ozaki #ifdef IPSEC
    466   1.87     ozaki 	if (sp != NULL)
    467   1.88     ozaki 		KEY_SP_UNREF(&sp);
    468   1.87     ozaki #endif
    469   1.82     ozaki 	rtcache_unref(rt, ro);
    470   1.82     ozaki 	if (ro != NULL)
    471   1.97     ozaki 		rtcache_percpu_putref(ip6_forward_rt_percpu);
    472   1.79     ozaki 	if (rcvif != NULL)
    473   1.79     ozaki 		m_put_rcvif_psref(rcvif, &psref);
    474    1.5    itojun 	return;
    475    1.2    itojun }
    476