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