Home | History | Annotate | Line # | Download | only in netinet6
ip6_forward.c revision 1.17
      1 /*	$NetBSD: ip6_forward.c,v 1.17 2000/09/22 05:50:23 itojun Exp $	*/
      2 /*	$KAME: ip6_forward.c,v 1.56 2000/09/22 04:01:37 itojun 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 "opt_ipsec.h"
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/malloc.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/domain.h>
     40 #include <sys/protosw.h>
     41 #include <sys/socket.h>
     42 #include <sys/errno.h>
     43 #include <sys/time.h>
     44 #include <sys/kernel.h>
     45 #include <sys/syslog.h>
     46 
     47 #include <net/if.h>
     48 #include <net/route.h>
     49 
     50 #include <netinet/in.h>
     51 #include <netinet/in_var.h>
     52 #include <netinet/ip_var.h>
     53 #include <netinet/ip6.h>
     54 #include <netinet6/ip6_var.h>
     55 #include <netinet/icmp6.h>
     56 #include <netinet6/nd6.h>
     57 
     58 #ifdef IPSEC
     59 #include <netinet6/ipsec.h>
     60 #include <netkey/key.h>
     61 #endif /* IPSEC */
     62 
     63 #ifdef IPV6FIREWALL
     64 #include <netinet6/ip6_fw.h>
     65 #endif
     66 
     67 #include <net/net_osdep.h>
     68 
     69 struct	route_in6 ip6_forward_rt;
     70 
     71 /*
     72  * Forward a packet.  If some error occurs return the sender
     73  * an icmp packet.  Note we can't always generate a meaningful
     74  * icmp message because icmp doesn't have a large enough repertoire
     75  * of codes and types.
     76  *
     77  * If not forwarding, just drop the packet.  This could be confusing
     78  * if ipforwarding was zero but some routing protocol was advancing
     79  * us as a gateway to somewhere.  However, we must let the routing
     80  * protocol deal with that.
     81  *
     82  */
     83 
     84 void
     85 ip6_forward(m, srcrt)
     86 	struct mbuf *m;
     87 	int srcrt;
     88 {
     89 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
     90 	register struct sockaddr_in6 *dst;
     91 	register struct rtentry *rt;
     92 	int error, type = 0, code = 0;
     93 	struct mbuf *mcopy = NULL;
     94 	struct ifnet *origifp;	/* maybe unnecessary */
     95 #ifdef IPSEC
     96 	struct secpolicy *sp = NULL;
     97 #endif
     98 	long time_second = time.tv_sec;
     99 
    100 #ifdef IPSEC
    101 	/*
    102 	 * Check AH/ESP integrity.
    103 	 */
    104 	/*
    105 	 * Don't increment ip6s_cantforward because this is the check
    106 	 * before forwarding packet actually.
    107 	 */
    108 	if (ipsec6_in_reject(m, NULL)) {
    109 		ipsec6stat.in_polvio++;
    110 		m_freem(m);
    111 		return;
    112 	}
    113 #endif /*IPSEC*/
    114 
    115 	/*
    116 	 * Do not forward packets to multicast destination (should be handled
    117 	 * by ip6_mforward().
    118 	 * Do not forward packets with unspecified source.  It was discussed
    119 	 * in July 2000, on ipngwg mailing list.
    120 	 */
    121 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
    122 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
    123 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
    124 		ip6stat.ip6s_cantforward++;
    125 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
    126 		if (ip6_log_time + ip6_log_interval < time_second) {
    127 			ip6_log_time = time_second;
    128 			log(LOG_DEBUG,
    129 			    "cannot forward "
    130 			    "from %s to %s nxt %d received on %s\n",
    131 			    ip6_sprintf(&ip6->ip6_src),
    132 			    ip6_sprintf(&ip6->ip6_dst),
    133 			    ip6->ip6_nxt,
    134 			    if_name(m->m_pkthdr.rcvif));
    135 		}
    136 		m_freem(m);
    137 		return;
    138 	}
    139 
    140 	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
    141 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
    142 		icmp6_error(m, ICMP6_TIME_EXCEEDED,
    143 				ICMP6_TIME_EXCEED_TRANSIT, 0);
    144 		return;
    145 	}
    146 	ip6->ip6_hlim -= IPV6_HLIMDEC;
    147 
    148 	/*
    149 	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
    150 	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
    151 	 * we need to generate an ICMP6 message to the src.
    152 	 * Thanks to M_EXT, in most cases copy will not occur.
    153 	 *
    154 	 * It is important to save it before IPsec processing as IPsec
    155 	 * processing may modify the mbuf.
    156 	 */
    157 	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
    158 
    159 #ifdef IPSEC
    160 	/* get a security policy for this packet */
    161 	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
    162 	    &error);
    163 	if (sp == NULL) {
    164 		ipsec6stat.out_inval++;
    165 		ip6stat.ip6s_cantforward++;
    166 		if (mcopy) {
    167 #if 0
    168 			/* XXX: what icmp ? */
    169 #else
    170 			m_freem(mcopy);
    171 #endif
    172 		}
    173 		m_freem(m);
    174 		return;
    175 	}
    176 
    177 	error = 0;
    178 
    179 	/* check policy */
    180 	switch (sp->policy) {
    181 	case IPSEC_POLICY_DISCARD:
    182 		/*
    183 		 * This packet is just discarded.
    184 		 */
    185 		ipsec6stat.out_polvio++;
    186 		ip6stat.ip6s_cantforward++;
    187 		key_freesp(sp);
    188 		if (mcopy) {
    189 #if 0
    190 			/* XXX: what icmp ? */
    191 #else
    192 			m_freem(mcopy);
    193 #endif
    194 		}
    195 		m_freem(m);
    196 		return;
    197 
    198 	case IPSEC_POLICY_BYPASS:
    199 	case IPSEC_POLICY_NONE:
    200 		/* no need to do IPsec. */
    201 		key_freesp(sp);
    202 		goto skip_ipsec;
    203 
    204 	case IPSEC_POLICY_IPSEC:
    205 		if (sp->req == NULL) {
    206 			/* XXX should be panic ? */
    207 			printf("ip6_forward: No IPsec request specified.\n");
    208 			ip6stat.ip6s_cantforward++;
    209 			key_freesp(sp);
    210 			if (mcopy) {
    211 #if 0
    212 				/* XXX: what icmp ? */
    213 #else
    214 				m_freem(mcopy);
    215 #endif
    216 			}
    217 			m_freem(m);
    218 			return;
    219 		}
    220 		/* do IPsec */
    221 		break;
    222 
    223 	case IPSEC_POLICY_ENTRUST:
    224 	default:
    225 		/* should be panic ?? */
    226 		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
    227 		key_freesp(sp);
    228 		goto skip_ipsec;
    229 	}
    230 
    231     {
    232 	struct ipsec_output_state state;
    233 
    234 	/*
    235 	 * All the extension headers will become inaccessible
    236 	 * (since they can be encrypted).
    237 	 * Don't panic, we need no more updates to extension headers
    238 	 * on inner IPv6 packet (since they are now encapsulated).
    239 	 *
    240 	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
    241 	 */
    242 	bzero(&state, sizeof(state));
    243 	state.m = m;
    244 	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
    245 	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
    246 
    247 	error = ipsec6_output_tunnel(&state, sp, 0);
    248 
    249 	m = state.m;
    250 #if 0	/* XXX allocate a route (ro, dst) again later */
    251 	ro = (struct route_in6 *)state.ro;
    252 	dst = (struct sockaddr_in6 *)state.dst;
    253 #endif
    254 	key_freesp(sp);
    255 
    256 	if (error) {
    257 		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
    258 		switch (error) {
    259 		case EHOSTUNREACH:
    260 		case ENETUNREACH:
    261 		case EMSGSIZE:
    262 		case ENOBUFS:
    263 		case ENOMEM:
    264 			break;
    265 		default:
    266 			printf("ip6_output (ipsec): error code %d\n", error);
    267 			/*fall through*/
    268 		case ENOENT:
    269 			/* don't show these error codes to the user */
    270 			break;
    271 		}
    272 		ip6stat.ip6s_cantforward++;
    273 		if (mcopy) {
    274 #if 0
    275 			/* XXX: what icmp ? */
    276 #else
    277 			m_freem(mcopy);
    278 #endif
    279 		}
    280 		m_freem(m);
    281 		return;
    282 	}
    283     }
    284     skip_ipsec:
    285 #endif /* IPSEC */
    286 
    287 	dst = &ip6_forward_rt.ro_dst;
    288 	if (!srcrt) {
    289 		/*
    290 		 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
    291 		 */
    292 		if (ip6_forward_rt.ro_rt == 0 ||
    293 		    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
    294 			if (ip6_forward_rt.ro_rt) {
    295 				RTFREE(ip6_forward_rt.ro_rt);
    296 				ip6_forward_rt.ro_rt = 0;
    297 			}
    298 			/* this probably fails but give it a try again */
    299 			rtalloc((struct route *)&ip6_forward_rt);
    300 		}
    301 
    302 		if (ip6_forward_rt.ro_rt == 0) {
    303 			ip6stat.ip6s_noroute++;
    304 			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
    305 			if (mcopy) {
    306 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
    307 					    ICMP6_DST_UNREACH_NOROUTE, 0);
    308 			}
    309 			m_freem(m);
    310 			return;
    311 		}
    312 	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
    313 		 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
    314 		if (ip6_forward_rt.ro_rt) {
    315 			RTFREE(ip6_forward_rt.ro_rt);
    316 			ip6_forward_rt.ro_rt = 0;
    317 		}
    318 		bzero(dst, sizeof(*dst));
    319 		dst->sin6_len = sizeof(struct sockaddr_in6);
    320 		dst->sin6_family = AF_INET6;
    321 		dst->sin6_addr = ip6->ip6_dst;
    322 
    323 		rtalloc((struct route *)&ip6_forward_rt);
    324 		if (ip6_forward_rt.ro_rt == 0) {
    325 			ip6stat.ip6s_noroute++;
    326 			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
    327 			if (mcopy) {
    328 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
    329 					    ICMP6_DST_UNREACH_NOROUTE, 0);
    330 			}
    331 			m_freem(m);
    332 			return;
    333 		}
    334 	}
    335 	rt = ip6_forward_rt.ro_rt;
    336 
    337 	/*
    338 	 * Scope check: if a packet can't be delivered to its destination
    339 	 * for the reason that the destination is beyond the scope of the
    340 	 * source address, discard the packet and return an icmp6 destination
    341 	 * unreachable error with Code 2 (beyond scope of source address).
    342 	 * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1]
    343 	 */
    344 	if (in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_src) !=
    345 	    in6_addr2scopeid(rt->rt_ifp, &ip6->ip6_src)) {
    346 		ip6stat.ip6s_cantforward++;
    347 		ip6stat.ip6s_badscope++;
    348 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
    349 
    350 		if (ip6_log_time + ip6_log_interval < time_second) {
    351 			ip6_log_time = time_second;
    352 			log(LOG_DEBUG,
    353 			    "cannot forward "
    354 			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
    355 			    ip6_sprintf(&ip6->ip6_src),
    356 			    ip6_sprintf(&ip6->ip6_dst),
    357 			    ip6->ip6_nxt,
    358 			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
    359 		}
    360 		if (mcopy)
    361 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
    362 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
    363 		m_freem(m);
    364 		return;
    365 	}
    366 
    367 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
    368 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
    369 		if (mcopy) {
    370 			u_long mtu;
    371 #ifdef IPSEC
    372 			struct secpolicy *sp;
    373 			int ipsecerror;
    374 			size_t ipsechdrsiz;
    375 #endif
    376 
    377 			mtu = rt->rt_ifp->if_mtu;
    378 #ifdef IPSEC
    379 			/*
    380 			 * When we do IPsec tunnel ingress, we need to play
    381 			 * with if_mtu value (decrement IPsec header size
    382 			 * from mtu value).  The code is much simpler than v4
    383 			 * case, as we have the outgoing interface for
    384 			 * encapsulated packet as "rt->rt_ifp".
    385 			 */
    386 			sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
    387 				IP_FORWARDING, &ipsecerror);
    388 			if (sp) {
    389 				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
    390 					IPSEC_DIR_OUTBOUND, NULL);
    391 				if (ipsechdrsiz < mtu)
    392 					mtu -= ipsechdrsiz;
    393 			}
    394 
    395 			/*
    396 			 * if mtu becomes less than minimum MTU,
    397 			 * tell minimum MTU (and I'll need to fragment it).
    398 			 */
    399 			if (mtu < IPV6_MMTU)
    400 				mtu = IPV6_MMTU;
    401 #endif
    402 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
    403 		}
    404 		m_freem(m);
    405 		return;
    406  	}
    407 
    408 	if (rt->rt_flags & RTF_GATEWAY)
    409 		dst = (struct sockaddr_in6 *)rt->rt_gateway;
    410 
    411 	/*
    412 	 * If we are to forward the packet using the same interface
    413 	 * as one we got the packet from, perhaps we should send a redirect
    414 	 * to sender to shortcut a hop.
    415 	 * Only send redirect if source is sending directly to us,
    416 	 * and if packet was not source routed (or has any options).
    417 	 * Also, don't send redirect if forwarding using a route
    418 	 * modified by a redirect.
    419 	 */
    420 	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
    421 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0)
    422 		type = ND_REDIRECT;
    423 
    424 #ifdef IPV6FIREWALL
    425 	/*
    426 	 * Check with the firewall...
    427 	 */
    428 	if (ip6_fw_chk_ptr) {
    429 		u_short port = 0;
    430 		/* If ipfw says divert, we have to just drop packet */
    431 		if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
    432 			m_freem(m);
    433 			goto freecopy;
    434 		}
    435 		if (!m)
    436 			goto freecopy;
    437 	}
    438 #endif
    439 
    440 	/*
    441 	 * Fake scoped addresses. Note that even link-local source or
    442 	 * destinaion can appear, if the originating node just sends the
    443 	 * packet to us (without address resolution for the destination).
    444 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
    445 	 * link identifiers, we can do this stuff after make a copy for
    446 	 * returning error.
    447 	 */
    448 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
    449 		/*
    450 		 * See corresponding comments in ip6_output.
    451 		 * XXX: but is it possible that ip6_forward() sends a packet
    452 		 *      to a loopback interface? I don't think so, and thus
    453 		 *      I bark here. (jinmei (at) kame.net)
    454 		 * XXX: it is common to route invalid packets to loopback.
    455 		 *	also, the codepath will be visited on use of ::1 in
    456 		 *	rthdr. (itojun)
    457 		 */
    458 #if 1
    459 		if (0)
    460 #else
    461 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
    462 #endif
    463 		{
    464 			printf("ip6_forward: outgoing interface is loopback. "
    465 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
    466 			       ip6_sprintf(&ip6->ip6_src),
    467 			       ip6_sprintf(&ip6->ip6_dst),
    468 			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
    469 			       if_name(rt->rt_ifp));
    470 		}
    471 
    472 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    473 			origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
    474 		else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    475 			origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
    476 		else
    477 			origifp = rt->rt_ifp;
    478 	}
    479 	else
    480 		origifp = rt->rt_ifp;
    481 #ifndef FAKE_LOOPBACK_IF
    482 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
    483 #else
    484 	if (1)
    485 #endif
    486 	{
    487 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    488 			ip6->ip6_src.s6_addr16[1] = 0;
    489 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    490 			ip6->ip6_dst.s6_addr16[1] = 0;
    491 	}
    492 
    493 #ifdef OLDIP6OUTPUT
    494 	error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m,
    495 					 (struct sockaddr *)dst,
    496 					 ip6_forward_rt.ro_rt);
    497 #else
    498 	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
    499 #endif
    500 	if (error) {
    501 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
    502 		ip6stat.ip6s_cantforward++;
    503 	} else {
    504 		ip6stat.ip6s_forward++;
    505 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
    506 		if (type)
    507 			ip6stat.ip6s_redirectsent++;
    508 		else {
    509 			if (mcopy)
    510 				goto freecopy;
    511 		}
    512 	}
    513 	if (mcopy == NULL)
    514 		return;
    515 
    516 	switch (error) {
    517 	case 0:
    518 #if 1
    519 		if (type == ND_REDIRECT) {
    520 			icmp6_redirect_output(mcopy, rt);
    521 			return;
    522 		}
    523 #endif
    524 		goto freecopy;
    525 
    526 	case EMSGSIZE:
    527 		/* xxx MTU is constant in PPP? */
    528 		goto freecopy;
    529 
    530 	case ENOBUFS:
    531 		/* Tell source to slow down like source quench in IP? */
    532 		goto freecopy;
    533 
    534 	case ENETUNREACH:	/* shouldn't happen, checked above */
    535 	case EHOSTUNREACH:
    536 	case ENETDOWN:
    537 	case EHOSTDOWN:
    538 	default:
    539 		type = ICMP6_DST_UNREACH;
    540 		code = ICMP6_DST_UNREACH_ADDR;
    541 		break;
    542 	}
    543 	icmp6_error(mcopy, type, code, 0);
    544 	return;
    545 
    546  freecopy:
    547 	m_freem(mcopy);
    548 	return;
    549 }
    550