xref: /freebsd/sys/netinet6/ip6_forward.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*	$FreeBSD$	*/
2 /*	$KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 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_ip6fw.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/errno.h>
46 #include <sys/time.h>
47 #include <sys/kernel.h>
48 #include <sys/syslog.h>
49 
50 #include <net/if.h>
51 #include <net/route.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip_var.h>
58 #include <netinet6/in6_var.h>
59 #include <netinet/ip6.h>
60 #include <netinet6/ip6_var.h>
61 #include <netinet/icmp6.h>
62 #include <netinet6/nd6.h>
63 
64 #include <netinet/in_pcb.h>
65 
66 #ifdef IPSEC
67 #include <netinet6/ipsec.h>
68 #ifdef INET6
69 #include <netinet6/ipsec6.h>
70 #endif
71 #include <netkey/key.h>
72 #endif /* IPSEC */
73 
74 #include <netinet6/ip6_fw.h>
75 
76 #include <net/net_osdep.h>
77 
78 struct	route_in6 ip6_forward_rt;
79 
80 /*
81  * Forward a packet.  If some error occurs return the sender
82  * an icmp packet.  Note we can't always generate a meaningful
83  * icmp message because icmp doesn't have a large enough repertoire
84  * of codes and types.
85  *
86  * If not forwarding, just drop the packet.  This could be confusing
87  * if ipforwarding was zero but some routing protocol was advancing
88  * us as a gateway to somewhere.  However, we must let the routing
89  * protocol deal with that.
90  *
91  */
92 
93 void
94 ip6_forward(m, srcrt)
95 	struct mbuf *m;
96 	int srcrt;
97 {
98 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
99 	struct sockaddr_in6 *dst;
100 	struct rtentry *rt;
101 	int error, type = 0, code = 0;
102 	struct mbuf *mcopy = NULL;
103 	struct ifnet *origifp;	/* maybe unnecessary */
104 #ifdef IPSEC
105 	struct secpolicy *sp = NULL;
106 #endif
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 ipsec_output_state state;
241 
242 	/*
243 	 * All the extension headers will become inaccessible
244 	 * (since they can be encrypted).
245 	 * Don't panic, we need no more updates to extension headers
246 	 * on inner IPv6 packet (since they are now encapsulated).
247 	 *
248 	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
249 	 */
250 	bzero(&state, sizeof(state));
251 	state.m = m;
252 	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
253 	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
254 
255 	error = ipsec6_output_tunnel(&state, sp, 0);
256 
257 	m = state.m;
258 	key_freesp(sp);
259 
260 	if (error) {
261 		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
262 		switch (error) {
263 		case EHOSTUNREACH:
264 		case ENETUNREACH:
265 		case EMSGSIZE:
266 		case ENOBUFS:
267 		case ENOMEM:
268 			break;
269 		default:
270 			printf("ip6_output (ipsec): error code %d\n", error);
271 			/*fall through*/
272 		case ENOENT:
273 			/* don't show these error codes to the user */
274 			break;
275 		}
276 		ip6stat.ip6s_cantforward++;
277 		if (mcopy) {
278 #if 0
279 			/* XXX: what icmp ? */
280 #else
281 			m_freem(mcopy);
282 #endif
283 		}
284 		m_freem(m);
285 		return;
286 	}
287     }
288     skip_ipsec:
289 #endif /* IPSEC */
290 
291 	dst = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
292 	if (!srcrt) {
293 		/*
294 		 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
295 		 */
296 		if (ip6_forward_rt.ro_rt == 0 ||
297 		    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
298 			if (ip6_forward_rt.ro_rt) {
299 				RTFREE(ip6_forward_rt.ro_rt);
300 				ip6_forward_rt.ro_rt = 0;
301 			}
302 			/* this probably fails but give it a try again */
303 			rtalloc_ign((struct route *)&ip6_forward_rt,
304 				    RTF_PRCLONING);
305 		}
306 
307 		if (ip6_forward_rt.ro_rt == 0) {
308 			ip6stat.ip6s_noroute++;
309 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
310 			if (mcopy) {
311 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
312 					    ICMP6_DST_UNREACH_NOROUTE, 0);
313 			}
314 			m_freem(m);
315 			return;
316 		}
317 	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
318 		 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
319 		if (ip6_forward_rt.ro_rt) {
320 			RTFREE(ip6_forward_rt.ro_rt);
321 			ip6_forward_rt.ro_rt = 0;
322 		}
323 		bzero(dst, sizeof(*dst));
324 		dst->sin6_len = sizeof(struct sockaddr_in6);
325 		dst->sin6_family = AF_INET6;
326 		dst->sin6_addr = ip6->ip6_dst;
327 
328   		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
329 		if (ip6_forward_rt.ro_rt == 0) {
330 			ip6stat.ip6s_noroute++;
331 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
332 			if (mcopy) {
333 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
334 					    ICMP6_DST_UNREACH_NOROUTE, 0);
335 			}
336 			m_freem(m);
337 			return;
338 		}
339 	}
340 	rt = ip6_forward_rt.ro_rt;
341 
342 	/*
343 	 * Scope check: if a packet can't be delivered to its destination
344 	 * for the reason that the destination is beyond the scope of the
345 	 * source address, discard the packet and return an icmp6 destination
346 	 * unreachable error with Code 2 (beyond scope of source address).
347 	 * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1]
348 	 */
349 	if (in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_src) !=
350 	    in6_addr2scopeid(rt->rt_ifp, &ip6->ip6_src)) {
351 		ip6stat.ip6s_cantforward++;
352 		ip6stat.ip6s_badscope++;
353 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
354 
355 		if (ip6_log_time + ip6_log_interval < time_second) {
356 			ip6_log_time = time_second;
357 			log(LOG_DEBUG,
358 			    "cannot forward "
359 			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
360 			    ip6_sprintf(&ip6->ip6_src),
361 			    ip6_sprintf(&ip6->ip6_dst),
362 			    ip6->ip6_nxt,
363 			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
364 		}
365 		if (mcopy)
366 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
367 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
368 		m_freem(m);
369 		return;
370 	}
371 
372 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
373 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
374 		if (mcopy) {
375 			u_long mtu;
376 #ifdef IPSEC
377 			struct secpolicy *sp;
378 			int ipsecerror;
379 			size_t ipsechdrsiz;
380 #endif
381 
382 			mtu = rt->rt_ifp->if_mtu;
383 #ifdef IPSEC
384 			/*
385 			 * When we do IPsec tunnel ingress, we need to play
386 			 * with if_mtu value (decrement IPsec header size
387 			 * from mtu value).  The code is much simpler than v4
388 			 * case, as we have the outgoing interface for
389 			 * encapsulated packet as "rt->rt_ifp".
390 			 */
391 			sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
392 				IP_FORWARDING, &ipsecerror);
393 			if (sp) {
394 				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
395 					IPSEC_DIR_OUTBOUND, NULL);
396 				if (ipsechdrsiz < mtu)
397 					mtu -= ipsechdrsiz;
398 			}
399 
400 			/*
401 			 * if mtu becomes less than minimum MTU,
402 			 * tell minimum MTU (and I'll need to fragment it).
403 			 */
404 			if (mtu < IPV6_MMTU)
405 				mtu = IPV6_MMTU;
406 #endif
407 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
408 		}
409 		m_freem(m);
410 		return;
411  	}
412 
413 	if (rt->rt_flags & RTF_GATEWAY)
414 		dst = (struct sockaddr_in6 *)rt->rt_gateway;
415 
416 	/*
417 	 * If we are to forward the packet using the same interface
418 	 * as one we got the packet from, perhaps we should send a redirect
419 	 * to sender to shortcut a hop.
420 	 * Only send redirect if source is sending directly to us,
421 	 * and if packet was not source routed (or has any options).
422 	 * Also, don't send redirect if forwarding using a route
423 	 * modified by a redirect.
424 	 */
425 	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
426 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
427 		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) {
428 			/*
429 			 * If the incoming interface is equal to the outgoing
430 			 * one, and the link attached to the interface is
431 			 * point-to-point, then it will be highly probable
432 			 * that a routing loop occurs. Thus, we immediately
433 			 * drop the packet and send an ICMPv6 error message.
434 			 *
435 			 * type/code is based on suggestion by Rich Draves.
436 			 * not sure if it is the best pick.
437 			 */
438 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
439 				    ICMP6_DST_UNREACH_ADDR, 0);
440 			m_freem(m);
441 			return;
442 		}
443 		type = ND_REDIRECT;
444 	}
445 
446 	/*
447 	 * Check with the firewall...
448 	 */
449 	if (ip6_fw_enable && ip6_fw_chk_ptr) {
450 		u_short port = 0;
451 		/* If ipfw says divert, we have to just drop packet */
452 		if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
453 			m_freem(m);
454 			goto freecopy;
455 		}
456 		if (!m)
457 			goto freecopy;
458 	}
459 
460 	/*
461 	 * Fake scoped addresses. Note that even link-local source or
462 	 * destinaion can appear, if the originating node just sends the
463 	 * packet to us (without address resolution for the destination).
464 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
465 	 * link identifiers, we can do this stuff after making a copy for
466 	 * returning an error.
467 	 */
468 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
469 		/*
470 		 * See corresponding comments in ip6_output.
471 		 * XXX: but is it possible that ip6_forward() sends a packet
472 		 *      to a loopback interface? I don't think so, and thus
473 		 *      I bark here. (jinmei@kame.net)
474 		 * XXX: it is common to route invalid packets to loopback.
475 		 *	also, the codepath will be visited on use of ::1 in
476 		 *	rthdr. (itojun)
477 		 */
478 #if 1
479 		if (0)
480 #else
481 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
482 #endif
483 		{
484 			printf("ip6_forward: outgoing interface is loopback. "
485 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
486 			       ip6_sprintf(&ip6->ip6_src),
487 			       ip6_sprintf(&ip6->ip6_dst),
488 			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
489 			       if_name(rt->rt_ifp));
490 		}
491 
492 		/* we can just use rcvif in forwarding. */
493 		origifp = m->m_pkthdr.rcvif;
494 	}
495 	else
496 		origifp = rt->rt_ifp;
497 #ifndef SCOPEDROUTING
498 	/*
499 	 * clear embedded scope identifiers if necessary.
500 	 * in6_clearscope will touch the addresses only when necessary.
501 	 */
502 	in6_clearscope(&ip6->ip6_src);
503 	in6_clearscope(&ip6->ip6_dst);
504 #endif
505 
506 	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
507 	if (error) {
508 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
509 		ip6stat.ip6s_cantforward++;
510 	} else {
511 		ip6stat.ip6s_forward++;
512 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
513 		if (type)
514 			ip6stat.ip6s_redirectsent++;
515 		else {
516 			if (mcopy)
517 				goto freecopy;
518 		}
519 	}
520 	if (mcopy == NULL)
521 		return;
522 
523 	switch (error) {
524 	case 0:
525 #if 1
526 		if (type == ND_REDIRECT) {
527 			icmp6_redirect_output(mcopy, rt);
528 			return;
529 		}
530 #endif
531 		goto freecopy;
532 
533 	case EMSGSIZE:
534 		/* xxx MTU is constant in PPP? */
535 		goto freecopy;
536 
537 	case ENOBUFS:
538 		/* Tell source to slow down like source quench in IP? */
539 		goto freecopy;
540 
541 	case ENETUNREACH:	/* shouldn't happen, checked above */
542 	case EHOSTUNREACH:
543 	case ENETDOWN:
544 	case EHOSTDOWN:
545 	default:
546 		type = ICMP6_DST_UNREACH;
547 		code = ICMP6_DST_UNREACH_ADDR;
548 		break;
549 	}
550 	icmp6_error(mcopy, type, code, 0);
551 	return;
552 
553  freecopy:
554 	m_freem(mcopy);
555 	return;
556 }
557