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