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