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