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