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