ip6_forward.c revision 62601
1/*	$FreeBSD: head/sys/netinet6/ip6_forward.c 62601 2000-07-05 01:14:45Z itojun $	*/
2/*	$KAME: ip6_forward.c,v 1.39 2000/07/03 13:23:28 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/mbuf.h>
41#include <sys/domain.h>
42#include <sys/protosw.h>
43#include <sys/socket.h>
44#include <sys/errno.h>
45#include <sys/time.h>
46#include <sys/syslog.h>
47
48#include <net/if.h>
49#include <net/route.h>
50
51#include <netinet/in.h>
52#include <netinet/in_var.h>
53#include <netinet/ip_var.h>
54#include <netinet/ip6.h>
55#include <netinet6/ip6_var.h>
56#include <netinet/icmp6.h>
57#include <netinet6/nd6.h>
58
59#ifdef IPSEC_IPV6FWD
60#include <netinet6/ipsec.h>
61#include <netinet6/ipsec6.h>
62#include <netkey/key.h>
63#endif /* IPSEC_IPV6FWD */
64
65#ifdef IPV6FIREWALL
66#include <netinet6/ip6_fw.h>
67#endif
68
69#include <net/net_osdep.h>
70
71struct	route_in6 ip6_forward_rt;
72
73/*
74 * Forward a packet.  If some error occurs return the sender
75 * an icmp packet.  Note we can't always generate a meaningful
76 * icmp message because icmp doesn't have a large enough repertoire
77 * of codes and types.
78 *
79 * If not forwarding, just drop the packet.  This could be confusing
80 * if ipforwarding was zero but some routing protocol was advancing
81 * us as a gateway to somewhere.  However, we must let the routing
82 * protocol deal with that.
83 *
84 */
85
86void
87ip6_forward(m, srcrt)
88	struct mbuf *m;
89	int srcrt;
90{
91	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
92	register struct sockaddr_in6 *dst;
93	register struct rtentry *rt;
94	int error, type = 0, code = 0;
95	struct mbuf *mcopy = NULL;
96	struct ifnet *origifp;	/* maybe unnecessary */
97#ifdef IPSEC_IPV6FWD
98	struct secpolicy *sp = NULL;
99#endif
100
101#ifdef IPSEC_IPV6FWD
102	/*
103	 * Check AH/ESP integrity.
104	 */
105	/*
106	 * Don't increment ip6s_cantforward because this is the check
107	 * before forwarding packet actually.
108	 */
109	if (ipsec6_in_reject(m, NULL)) {
110		ipsec6stat.in_polvio++;
111		m_freem(m);
112		return;
113	}
114#endif /*IPSEC_IPV6FWD*/
115
116	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
117	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
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			    if_name(m->m_pkthdr.rcvif));
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_ign((struct route *)&ip6_forward_rt,
293				    RTF_PRCLONING);
294		}
295
296		if (ip6_forward_rt.ro_rt == 0) {
297			ip6stat.ip6s_noroute++;
298			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
299			if (mcopy) {
300				icmp6_error(mcopy, ICMP6_DST_UNREACH,
301					    ICMP6_DST_UNREACH_NOROUTE, 0);
302			}
303			m_freem(m);
304			return;
305		}
306	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
307		 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
308		if (ip6_forward_rt.ro_rt) {
309			RTFREE(ip6_forward_rt.ro_rt);
310			ip6_forward_rt.ro_rt = 0;
311		}
312		bzero(dst, sizeof(*dst));
313		dst->sin6_len = sizeof(struct sockaddr_in6);
314		dst->sin6_family = AF_INET6;
315		dst->sin6_addr = ip6->ip6_dst;
316
317  		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
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			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
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		type = ND_REDIRECT;
417
418#ifdef IPV6FIREWALL
419	/*
420	 * Check with the firewall...
421	 */
422	if (ip6_fw_chk_ptr) {
423		u_short port = 0;
424		/* If ipfw says divert, we have to just drop packet */
425		if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
426			m_freem(m);
427			goto freecopy;
428		}
429		if (!m)
430			goto freecopy;
431	}
432#endif
433
434	/*
435	 * Fake scoped addresses. Note that even link-local source or
436	 * destinaion can appear, if the originating node just sends the
437	 * packet to us (without address resolution for the destination).
438	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
439	 * link identifiers, we can do this stuff after make a copy for
440	 * returning error.
441	 */
442	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
443		/*
444		 * See corresponding comments in ip6_output.
445		 * XXX: but is it possible that ip6_forward() sends a packet
446		 *      to a loopback interface? I don't think so, and thus
447		 *      I bark here. (jinmei@kame.net)
448		 * XXX: it is common to route invalid packets to loopback.
449		 *	also, the codepath will be visited on use of ::1 in
450		 *	rthdr. (itojun)
451		 */
452#if 1
453		if (0)
454#else
455		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
456#endif
457		{
458			printf("ip6_forward: outgoing interface is loopback. "
459			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
460			       ip6_sprintf(&ip6->ip6_src),
461			       ip6_sprintf(&ip6->ip6_dst),
462			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
463			       if_name(rt->rt_ifp));
464		}
465
466		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
467			origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
468		else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
469			origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
470		else
471			origifp = rt->rt_ifp;
472	}
473	else
474		origifp = rt->rt_ifp;
475#ifndef FAKE_LOOPBACK_IF
476	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
477#else
478	if (1)
479#endif
480	{
481		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
482			ip6->ip6_src.s6_addr16[1] = 0;
483		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
484			ip6->ip6_dst.s6_addr16[1] = 0;
485	}
486
487#ifdef OLDIP6OUTPUT
488	error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m,
489					 (struct sockaddr *)dst,
490					 ip6_forward_rt.ro_rt);
491#else
492	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
493#endif
494	if (error) {
495		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
496		ip6stat.ip6s_cantforward++;
497	} else {
498		ip6stat.ip6s_forward++;
499		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
500		if (type)
501			ip6stat.ip6s_redirectsent++;
502		else {
503			if (mcopy)
504				goto freecopy;
505		}
506	}
507	if (mcopy == NULL)
508		return;
509
510	switch (error) {
511	case 0:
512#if 1
513		if (type == ND_REDIRECT) {
514			icmp6_redirect_output(mcopy, rt);
515			return;
516		}
517#endif
518		goto freecopy;
519
520	case EMSGSIZE:
521		/* xxx MTU is constant in PPP? */
522		goto freecopy;
523
524	case ENOBUFS:
525		/* Tell source to slow down like source quench in IP? */
526		goto freecopy;
527
528	case ENETUNREACH:	/* shouldn't happen, checked above */
529	case EHOSTUNREACH:
530	case ENETDOWN:
531	case EHOSTDOWN:
532	default:
533		type = ICMP6_DST_UNREACH;
534		code = ICMP6_DST_UNREACH_ADDR;
535		break;
536	}
537	icmp6_error(mcopy, type, code, 0);
538	return;
539
540 freecopy:
541	m_freem(mcopy);
542	return;
543}
544