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