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