xref: /netbsd-src/sys/netinet6/ip6_forward.c (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
1 /*	$NetBSD: ip6_forward.c,v 1.76 2014/12/10 01:10:14 christos Exp $	*/
2 /*	$KAME: ip6_forward.c,v 1.109 2002/09/11 08:10:17 sakane 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 <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ip6_forward.c,v 1.76 2014/12/10 01:10:14 christos Exp $");
35 
36 #include "opt_gateway.h"
37 #include "opt_ipsec.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.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/ip_var.h>
57 #include <netinet/ip6.h>
58 #include <netinet6/ip6_var.h>
59 #include <netinet6/ip6_private.h>
60 #include <netinet6/scope6_var.h>
61 #include <netinet/icmp6.h>
62 #include <netinet6/nd6.h>
63 
64 #ifdef IPSEC
65 #include <netipsec/ipsec.h>
66 #include <netipsec/ipsec6.h>
67 #include <netipsec/key.h>
68 #include <netipsec/xform.h>
69 #endif /* IPSEC */
70 
71 #include <net/net_osdep.h>
72 
73 struct	route ip6_forward_rt;
74 
75 extern pfil_head_t *inet6_pfil_hook;	/* XXX */
76 
77 static void __printflike(4, 5)
78 ip6_cantforward(const struct ip6_hdr *ip6, const struct ifnet *srcifp,
79     const struct ifnet *dstifp, const char *fmt, ...)
80 {
81 	char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
82 	char reason[256];
83 	va_list ap;
84 	uint64_t *ip6s;
85 
86 	/* update statistics */
87 	ip6s = IP6_STAT_GETREF();
88 	ip6s[IP6_STAT_CANTFORWARD]++;
89 	if (dstifp)
90 		ip6s[IP6_STAT_BADSCOPE]++;
91 	IP6_STAT_PUTREF();
92 
93 	if (dstifp)
94 		in6_ifstat_inc(dstifp, ifs6_in_discard);
95 
96 	if (ip6_log_time + ip6_log_interval >= time_second)
97 		return;
98 	ip6_log_time = time_second;
99 
100 	va_start(ap, fmt);
101 	vsnprintf(reason, sizeof(reason), fmt, ap);
102 	va_end(ap);
103 
104 	log(LOG_DEBUG, "Cannot forward from %s@%s to %s@%s nxt %d (%s)\n",
105 	    IN6_PRINT(sbuf, &ip6->ip6_src), srcifp ? if_name(srcifp) : "?",
106 	    IN6_PRINT(dbuf, &ip6->ip6_dst), dstifp ? if_name(dstifp) : "?",
107 	    ip6->ip6_nxt, reason);
108 }
109 
110 /*
111  * Forward a packet.  If some error occurs return the sender
112  * an icmp packet.  Note we can't always generate a meaningful
113  * icmp message because icmp doesn't have a large enough repertoire
114  * of codes and types.
115  *
116  * If not forwarding, just drop the packet.  This could be confusing
117  * if ipforwarding was zero but some routing protocol was advancing
118  * us as a gateway to somewhere.  However, we must let the routing
119  * protocol deal with that.
120  *
121  */
122 
123 void
124 ip6_forward(struct mbuf *m, int srcrt)
125 {
126 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
127 	const struct sockaddr_in6 *dst;
128 	struct rtentry *rt;
129 	int error = 0, type = 0, code = 0;
130 	struct mbuf *mcopy = NULL;
131 	struct ifnet *origifp;	/* maybe unnecessary */
132 	uint32_t inzone, outzone;
133 	struct in6_addr src_in6, dst_in6;
134 #ifdef IPSEC
135 	int needipsec = 0;
136 	struct secpolicy *sp = NULL;
137 #endif
138 
139 	/*
140 	 * Clear any in-bound checksum flags for this packet.
141 	 */
142 	m->m_pkthdr.csum_flags = 0;
143 
144 	/*
145 	 * Do not forward packets to multicast destination (should be handled
146 	 * by ip6_mforward().
147 	 * Do not forward packets with unspecified source.  It was discussed
148 	 * in July 2000, on ipngwg mailing list.
149 	 */
150 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
151 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
152 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
153 		ip6_cantforward(ip6, m->m_pkthdr.rcvif, NULL,
154 		    ((m->m_flags & (M_BCAST|M_MCAST)) != 0) ? "bcast/mcast" :
155 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ? "mcast/dst" :
156 		    "unspec/src");
157 		m_freem(m);
158 		return;
159 	}
160 
161 	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
162 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
163 		icmp6_error(m, ICMP6_TIME_EXCEEDED,
164 				ICMP6_TIME_EXCEED_TRANSIT, 0);
165 		return;
166 	}
167 	ip6->ip6_hlim -= IPV6_HLIMDEC;
168 
169 	/*
170 	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
171 	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
172 	 * we need to generate an ICMP6 message to the src.
173 	 * Thanks to M_EXT, in most cases copy will not occur.
174 	 *
175 	 * It is important to save it before IPsec processing as IPsec
176 	 * processing may modify the mbuf.
177 	 */
178 	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
179 
180 #ifdef IPSEC
181 	if (ipsec_used) {
182 		/* Check the security policy (SP) for the packet */
183 
184 		sp = ipsec6_check_policy(m, NULL, 0, &needipsec, &error);
185 		if (error != 0) {
186 			/*
187 			 * Hack: -EINVAL is used to signal that a packet
188 			 * should be silently discarded.  This is typically
189 			 * because we asked key management for an SA and
190 			 * it was delayed (e.g. kicked up to IKE).
191 			 */
192 			if (error == -EINVAL)
193 				error = 0;
194 			goto freecopy;
195 		}
196 	}
197 #endif /* IPSEC */
198 
199 	if (srcrt) {
200 		union {
201 			struct sockaddr		dst;
202 			struct sockaddr_in6	dst6;
203 		} u;
204 
205 		sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
206 		if ((rt = rtcache_lookup(&ip6_forward_rt, &u.dst)) == NULL) {
207 			IP6_STATINC(IP6_STAT_NOROUTE);
208 			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
209 			if (mcopy) {
210 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
211 					    ICMP6_DST_UNREACH_NOROUTE, 0);
212 			}
213 			m_freem(m);
214 			return;
215 		}
216 	} else if ((rt = rtcache_validate(&ip6_forward_rt)) == NULL &&
217 	           (rt = rtcache_update(&ip6_forward_rt, 1)) == NULL) {
218 		/*
219 		 * rtcache_getdst(ip6_forward_rt)->sin6_addr was equal to
220 		 * ip6->ip6_dst
221 		 */
222 		IP6_STATINC(IP6_STAT_NOROUTE);
223 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
224 		if (mcopy) {
225 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
226 			    ICMP6_DST_UNREACH_NOROUTE, 0);
227 		}
228 		m_freem(m);
229 		return;
230 	}
231 	dst = satocsin6(rtcache_getdst(&ip6_forward_rt));
232 
233 	/*
234 	 * Source scope check: if a packet can't be delivered to its
235 	 * destination for the reason that the destination is beyond the scope
236 	 * of the source address, discard the packet and return an icmp6
237 	 * destination unreachable error with Code 2 (beyond scope of source
238 	 * address).  We use a local copy of ip6_src, since in6_setscope()
239 	 * will possibly modify its first argument.
240 	 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
241 	 */
242 	src_in6 = ip6->ip6_src;
243 	inzone = outzone = ~0;
244 	if (in6_setscope(&src_in6, rt->rt_ifp, &outzone) != 0 ||
245 	    in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
246 	    inzone != outzone) {
247 		ip6_cantforward(ip6, m->m_pkthdr.rcvif, rt->rt_ifp,
248 		    "src[%s] inzone %d outzone %d",
249 		    in6_getscopename(&ip6->ip6_src), inzone, outzone);
250 		if (mcopy)
251 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
252 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
253 		m_freem(m);
254 		return;
255 	}
256 
257 #ifdef IPSEC
258 	/*
259 	 * If we need to encapsulate the packet, do it here
260 	 * ipsec6_proces_packet will send the packet using ip6_output
261 	 */
262 	if (needipsec) {
263 		int s = splsoftnet();
264 		error = ipsec6_process_packet(m, sp->req);
265 		splx(s);
266 		if (mcopy)
267 			goto freecopy;
268 	}
269 #endif
270 
271 	/*
272 	 * Destination scope check: if a packet is going to break the scope
273 	 * zone of packet's destination address, discard it.  This case should
274 	 * usually be prevented by appropriately-configured routing table, but
275 	 * we need an explicit check because we may mistakenly forward the
276 	 * packet to a different zone by (e.g.) a default route.
277 	 */
278 	dst_in6 = ip6->ip6_dst;
279 	inzone = outzone = ~0;
280 	if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
281 	    in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
282 	    inzone != outzone) {
283 		ip6_cantforward(ip6, m->m_pkthdr.rcvif, rt->rt_ifp,
284 		    "dst[%s] inzone %d outzone %d",
285 		    in6_getscopename(&ip6->ip6_dst), inzone, outzone);
286 		if (mcopy)
287 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
288 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
289 		m_freem(m);
290 		return;
291 	}
292 
293 	if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
294 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
295 		if (mcopy) {
296 			u_long mtu;
297 
298 			mtu = IN6_LINKMTU(rt->rt_ifp);
299 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
300 		}
301 		m_freem(m);
302 		return;
303 	}
304 
305 	if (rt->rt_flags & RTF_GATEWAY)
306 		dst = (struct sockaddr_in6 *)rt->rt_gateway;
307 
308 	/*
309 	 * If we are to forward the packet using the same interface
310 	 * as one we got the packet from, perhaps we should send a redirect
311 	 * to sender to shortcut a hop.
312 	 * Only send redirect if source is sending directly to us,
313 	 * and if packet was not source routed (or has any options).
314 	 * Also, don't send redirect if forwarding using a route
315 	 * modified by a redirect.
316 	 */
317 	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && ip6_sendredirects &&
318 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
319 		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
320 		    nd6_is_addr_neighbor(
321 		        satocsin6(rtcache_getdst(&ip6_forward_rt)),
322 			rt->rt_ifp)) {
323 			/*
324 			 * If the incoming interface is equal to the outgoing
325 			 * one, the link attached to the interface is
326 			 * point-to-point, and the IPv6 destination is
327 			 * regarded as on-link on the link, then it will be
328 			 * highly probable that the destination address does
329 			 * not exist on the link and that the packet is going
330 			 * to loop.  Thus, we immediately drop the packet and
331 			 * send an ICMPv6 error message.
332 			 * For other routing loops, we dare to let the packet
333 			 * go to the loop, so that a remote diagnosing host
334 			 * can detect the loop by traceroute.
335 			 * type/code is based on suggestion by Rich Draves.
336 			 * not sure if it is the best pick.
337 			 */
338 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
339 				    ICMP6_DST_UNREACH_ADDR, 0);
340 			m_freem(m);
341 			return;
342 		}
343 		type = ND_REDIRECT;
344 	}
345 
346 	/*
347 	 * Fake scoped addresses. Note that even link-local source or
348 	 * destinaion can appear, if the originating node just sends the
349 	 * packet to us (without address resolution for the destination).
350 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
351 	 * link identifiers, we can do this stuff after making a copy for
352 	 * returning an error.
353 	 */
354 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
355 		/*
356 		 * See corresponding comments in ip6_output.
357 		 * XXX: but is it possible that ip6_forward() sends a packet
358 		 *      to a loopback interface? I don't think so, and thus
359 		 *      I bark here. (jinmei@kame.net)
360 		 * XXX: it is common to route invalid packets to loopback.
361 		 *	also, the codepath will be visited on use of ::1 in
362 		 *	rthdr. (itojun)
363 		 */
364 #if 1
365 		if (0)
366 #else
367 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
368 #endif
369 		{
370 			printf("ip6_forward: outgoing interface is loopback. "
371 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
372 			       ip6_sprintf(&ip6->ip6_src),
373 			       ip6_sprintf(&ip6->ip6_dst),
374 			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
375 			       if_name(rt->rt_ifp));
376 		}
377 
378 		/* we can just use rcvif in forwarding. */
379 		origifp = m->m_pkthdr.rcvif;
380 	}
381 	else
382 		origifp = rt->rt_ifp;
383 	/*
384 	 * clear embedded scope identifiers if necessary.
385 	 * in6_clearscope will touch the addresses only when necessary.
386 	 */
387 	in6_clearscope(&ip6->ip6_src);
388 	in6_clearscope(&ip6->ip6_dst);
389 
390 	/*
391 	 * Run through list of hooks for output packets.
392 	 */
393 	if ((error = pfil_run_hooks(inet6_pfil_hook, &m, rt->rt_ifp,
394 	    PFIL_OUT)) != 0)
395 		goto senderr;
396 	if (m == NULL)
397 		goto freecopy;
398 	ip6 = mtod(m, struct ip6_hdr *);
399 
400 	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
401 	if (error) {
402 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
403 		IP6_STATINC(IP6_STAT_CANTFORWARD);
404 	} else {
405 		IP6_STATINC(IP6_STAT_FORWARD);
406 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
407 		if (type)
408 			IP6_STATINC(IP6_STAT_REDIRECTSENT);
409 		else {
410 #ifdef GATEWAY
411 			if (m->m_flags & M_CANFASTFWD)
412 				ip6flow_create(&ip6_forward_rt, m);
413 #endif
414 			if (mcopy)
415 				goto freecopy;
416 		}
417 	}
418 
419  senderr:
420 	if (mcopy == NULL)
421 		return;
422 	switch (error) {
423 	case 0:
424 		if (type == ND_REDIRECT) {
425 			icmp6_redirect_output(mcopy, rt);
426 			return;
427 		}
428 		goto freecopy;
429 
430 	case EMSGSIZE:
431 		/* xxx MTU is constant in PPP? */
432 		goto freecopy;
433 
434 	case ENOBUFS:
435 		/* Tell source to slow down like source quench in IP? */
436 		goto freecopy;
437 
438 	case ENETUNREACH:	/* shouldn't happen, checked above */
439 	case EHOSTUNREACH:
440 	case ENETDOWN:
441 	case EHOSTDOWN:
442 	default:
443 		type = ICMP6_DST_UNREACH;
444 		code = ICMP6_DST_UNREACH_ADDR;
445 		break;
446 	}
447 	icmp6_error(mcopy, type, code, 0);
448 	return;
449 
450  freecopy:
451 	m_freem(mcopy);
452 	return;
453 }
454