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