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