Home | History | Annotate | Line # | Download | only in netinet6
ip6_forward.c revision 1.8
      1 /*	$NetBSD: ip6_forward.c,v 1.8 2000/02/06 12:49:45 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/malloc.h>
     35 #include <sys/mbuf.h>
     36 #include <sys/domain.h>
     37 #include <sys/protosw.h>
     38 #include <sys/socket.h>
     39 #include <sys/errno.h>
     40 #include <sys/time.h>
     41 #include <sys/kernel.h>
     42 #include <sys/syslog.h>
     43 
     44 #include <net/if.h>
     45 #include <net/route.h>
     46 
     47 #include <netinet/in.h>
     48 #include <netinet/in_var.h>
     49 #include <netinet/ip_var.h>
     50 #include <netinet/ip6.h>
     51 #include <netinet6/ip6_var.h>
     52 #include <netinet/icmp6.h>
     53 #include <netinet6/nd6.h>
     54 
     55 #ifdef IPSEC_IPV6FWD
     56 #include <netinet6/ipsec.h>
     57 #include <netkey/key.h>
     58 #include <netkey/key_debug.h>
     59 #endif /* IPSEC_IPV6FWD */
     60 
     61 #ifdef IPV6FIREWALL
     62 #include <netinet6/ip6_fw.h>
     63 #endif
     64 
     65 #include <net/net_osdep.h>
     66 
     67 struct	route_in6 ip6_forward_rt;
     68 
     69 /*
     70  * Forward a packet.  If some error occurs return the sender
     71  * an icmp packet.  Note we can't always generate a meaningful
     72  * icmp message because icmp doesn't have a large enough repertoire
     73  * of codes and types.
     74  *
     75  * If not forwarding, just drop the packet.  This could be confusing
     76  * if ipforwarding was zero but some routing protocol was advancing
     77  * us as a gateway to somewhere.  However, we must let the routing
     78  * protocol deal with that.
     79  *
     80  */
     81 
     82 void
     83 ip6_forward(m, srcrt)
     84 	struct mbuf *m;
     85 	int srcrt;
     86 {
     87 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
     88 	register struct sockaddr_in6 *dst;
     89 	register struct rtentry *rt;
     90 	int error, type = 0, code = 0;
     91 	struct mbuf *mcopy = NULL;
     92 #ifdef IPSEC_IPV6FWD
     93 	struct secpolicy *sp = NULL;
     94 #endif
     95 	long time_second = time.tv_sec;
     96 
     97 #ifdef IPSEC_IPV6FWD
     98 	/*
     99 	 * Check AH/ESP integrity.
    100 	 */
    101 	/*
    102 	 * Don't increment ip6s_cantforward because this is the check
    103 	 * before forwarding packet actually.
    104 	 */
    105 	if (ipsec6_in_reject(m, NULL)) {
    106 		ipsec6stat.in_polvio++;
    107 		m_freem(m);
    108 		return;
    109 	}
    110 #endif /*IPSEC_IPV6FWD*/
    111 
    112 	if (m->m_flags & (M_BCAST|M_MCAST) ||
    113 	   in6_canforward(&ip6->ip6_src, &ip6->ip6_dst) == 0) {
    114 		ip6stat.ip6s_cantforward++;
    115 		ip6stat.ip6s_badscope++;
    116 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
    117 		if (ip6_log_time + ip6_log_interval < time_second) {
    118 			char addr[INET6_ADDRSTRLEN];
    119 			ip6_log_time = time_second;
    120 			strncpy(addr, ip6_sprintf(&ip6->ip6_src), sizeof(addr));
    121 			log(LOG_DEBUG,
    122 			    "cannot forward "
    123 			    "from %s to %s nxt %d received on %s\n",
    124 			    addr, 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 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
    328 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
    329 		if (mcopy) {
    330 			u_long mtu;
    331 #ifdef IPSEC_IPV6FWD
    332 			struct secpolicy *sp;
    333 			int ipsecerror;
    334 			size_t ipsechdrsiz;
    335 #endif
    336 
    337 			mtu = rt->rt_ifp->if_mtu;
    338 #ifdef IPSEC_IPV6FWD
    339 			/*
    340 			 * When we do IPsec tunnel ingress, we need to play
    341 			 * with if_mtu value (decrement IPsec header size
    342 			 * from mtu value).  The code is much simpler than v4
    343 			 * case, as we have the outgoing interface for
    344 			 * encapsulated packet as "rt->rt_ifp".
    345 			 */
    346 			sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
    347 				IP_FORWARDING, &ipsecerror);
    348 			if (sp) {
    349 				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
    350 					IPSEC_DIR_OUTBOUND, NULL);
    351 				if (ipsechdrsiz < mtu)
    352 					mtu -= ipsechdrsiz;
    353 			}
    354 
    355 			/*
    356 			 * if mtu becomes less than minimum MTU,
    357 			 * tell minimum MTU (and I'll need to fragment it).
    358 			 */
    359 			if (mtu < IPV6_MMTU)
    360 				mtu = IPV6_MMTU;
    361 #endif
    362 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
    363 		}
    364 		m_freem(m);
    365 		return;
    366  	}
    367 
    368 	if (rt->rt_flags & RTF_GATEWAY)
    369 		dst = (struct sockaddr_in6 *)rt->rt_gateway;
    370 
    371 	/*
    372 	 * If we are to forward the packet using the same interface
    373 	 * as one we got the packet from, perhaps we should send a redirect
    374 	 * to sender to shortcut a hop.
    375 	 * Only send redirect if source is sending directly to us,
    376 	 * and if packet was not source routed (or has any options).
    377 	 * Also, don't send redirect if forwarding using a route
    378 	 * modified by a redirect.
    379 	 */
    380 	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
    381 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0)
    382 		type = ND_REDIRECT;
    383 
    384 #ifdef IPV6FIREWALL
    385 	/*
    386 	 * Check with the firewall...
    387 	 */
    388 	if (ip6_fw_chk_ptr) {
    389 		u_short port = 0;
    390 		/* If ipfw says divert, we have to just drop packet */
    391 		if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
    392 			m_freem(m);
    393 			goto freecopy;
    394 		}
    395 		if (!m)
    396 			goto freecopy;
    397 	}
    398 #endif
    399 
    400 #ifdef OLDIP6OUTPUT
    401 	error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m,
    402 					 (struct sockaddr *)dst,
    403 					 ip6_forward_rt.ro_rt);
    404 #else
    405 	error = nd6_output(rt->rt_ifp, m, dst, rt);
    406 #endif
    407 	if (error) {
    408 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
    409 		ip6stat.ip6s_cantforward++;
    410 	} else {
    411 		ip6stat.ip6s_forward++;
    412 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
    413 		if (type)
    414 			ip6stat.ip6s_redirectsent++;
    415 		else {
    416 			if (mcopy)
    417 				goto freecopy;
    418 		}
    419 	}
    420 	if (mcopy == NULL)
    421 		return;
    422 
    423 	switch (error) {
    424 	case 0:
    425 #if 1
    426 		if (type == ND_REDIRECT) {
    427 			icmp6_redirect_output(mcopy, rt);
    428 			return;
    429 		}
    430 #endif
    431 		goto freecopy;
    432 
    433 	case EMSGSIZE:
    434 		/* xxx MTU is constant in PPP? */
    435 		goto freecopy;
    436 
    437 	case ENOBUFS:
    438 		/* Tell source to slow down like source quench in IP? */
    439 		goto freecopy;
    440 
    441 	case ENETUNREACH:	/* shouldn't happen, checked above */
    442 	case EHOSTUNREACH:
    443 	case ENETDOWN:
    444 	case EHOSTDOWN:
    445 	default:
    446 		type = ICMP6_DST_UNREACH;
    447 		code = ICMP6_DST_UNREACH_ADDR;
    448 		break;
    449 	}
    450 	icmp6_error(mcopy, type, code, 0);
    451 	return;
    452 
    453  freecopy:
    454 	m_freem(mcopy);
    455 	return;
    456 }
    457