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