xref: /netbsd-src/sys/netinet6/ip6_forward.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /*	$NetBSD: ip6_forward.c,v 1.98 2019/11/01 04:23:21 knakahara 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.98 2019/11/01 04:23:21 knakahara Exp $");
35 
36 #ifdef _KERNEL_OPT
37 #include "opt_gateway.h"
38 #include "opt_ipsec.h"
39 #endif
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #include <sys/errno.h>
45 #include <sys/time.h>
46 #include <sys/kernel.h>
47 #include <sys/syslog.h>
48 #include <sys/percpu.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 #endif
69 
70 extern percpu_t *ip6_forward_rt_percpu;
71 
72 extern pfil_head_t *inet6_pfil_hook;	/* XXX */
73 
74 static void __printflike(4, 5)
75 ip6_cantforward(const struct ip6_hdr *ip6, const struct ifnet *srcifp,
76     const struct ifnet *dstifp, const char *fmt, ...)
77 {
78 	char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
79 	char reason[256];
80 	va_list ap;
81 	uint64_t *ip6s;
82 
83 	/* update statistics */
84 	ip6s = IP6_STAT_GETREF();
85 	ip6s[IP6_STAT_CANTFORWARD]++;
86 	if (dstifp)
87 		ip6s[IP6_STAT_BADSCOPE]++;
88 	IP6_STAT_PUTREF();
89 
90 	if (dstifp)
91 		in6_ifstat_inc(dstifp, ifs6_in_discard);
92 
93 	if (ip6_log_time + ip6_log_interval >= time_uptime)
94 		return;
95 	ip6_log_time = time_uptime;
96 
97 	va_start(ap, fmt);
98 	vsnprintf(reason, sizeof(reason), fmt, ap);
99 	va_end(ap);
100 
101 	log(LOG_DEBUG, "Cannot forward from %s@%s to %s@%s nxt %d (%s)\n",
102 	    IN6_PRINT(sbuf, &ip6->ip6_src), srcifp ? if_name(srcifp) : "?",
103 	    IN6_PRINT(dbuf, &ip6->ip6_dst), dstifp ? if_name(dstifp) : "?",
104 	    ip6->ip6_nxt, reason);
105 }
106 
107 /*
108  * Forward a packet.  If some error occurs return the sender
109  * an icmp packet.  Note we can't always generate a meaningful
110  * icmp message because icmp doesn't have a large enough repertoire
111  * of codes and types.
112  *
113  * If not forwarding, just drop the packet.  This could be confusing
114  * if ipforwarding was zero but some routing protocol was advancing
115  * us as a gateway to somewhere.  However, we must let the routing
116  * protocol deal with that.
117  */
118 void
119 ip6_forward(struct mbuf *m, int srcrt)
120 {
121 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
122 	const struct sockaddr_in6 *dst;
123 	struct rtentry *rt = NULL;
124 	int error = 0, type = 0, code = 0;
125 	struct mbuf *mcopy = NULL;
126 	struct ifnet *origifp;	/* maybe unnecessary */
127 	uint32_t inzone, outzone;
128 	struct in6_addr src_in6, dst_in6;
129 	struct ifnet *rcvif = NULL;
130 	struct psref psref;
131 	struct route *ro = NULL;
132 #ifdef IPSEC
133 	int needipsec = 0;
134 	struct secpolicy *sp = NULL;
135 #endif
136 
137 	/*
138 	 * Clear any in-bound checksum flags for this packet.
139 	 */
140 	m->m_pkthdr.csum_flags = 0;
141 
142 	rcvif = m_get_rcvif_psref(m, &psref);
143 	if (__predict_false(rcvif == NULL))
144 		goto drop;
145 
146 	/*
147 	 * Do not forward packets to multicast destination (should be handled
148 	 * by ip6_mforward()). Do not forward packets with unspecified source.
149 	 * It was discussed in July 2000, on ipngwg mailing list.
150 	 */
151 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
152 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
153 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
154 		ip6_cantforward(ip6, rcvif, NULL,
155 		    ((m->m_flags & (M_BCAST|M_MCAST)) != 0) ? "bcast/mcast" :
156 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ? "mcast/dst" :
157 		    "unspec/src");
158 		goto drop;
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 		goto out;
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_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
179 	    M_DONTWAIT);
180 
181 #ifdef IPSEC
182 	if (ipsec_used) {
183 		/* Check the security policy (SP) for the packet */
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 			m_freem(m);
195 			goto freecopy;
196 		}
197 	}
198 #endif
199 
200 	ro = rtcache_percpu_getref(ip6_forward_rt_percpu);
201 	if (srcrt) {
202 		union {
203 			struct sockaddr		dst;
204 			struct sockaddr_in6	dst6;
205 		} u;
206 
207 		sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
208 		rt = rtcache_lookup(ro, &u.dst);
209 		if (rt == NULL) {
210 			IP6_STATINC(IP6_STAT_NOROUTE);
211 			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
212 			if (mcopy) {
213 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
214 					    ICMP6_DST_UNREACH_NOROUTE, 0);
215 			}
216 			goto drop;
217 		}
218 	} else if ((rt = rtcache_validate(ro)) == NULL &&
219 	           (rt = rtcache_update(ro, 1)) == NULL) {
220 		/*
221 		 * rtcache_getdst(ip6_forward_rt)->sin6_addr was equal to
222 		 * ip6->ip6_dst
223 		 */
224 		IP6_STATINC(IP6_STAT_NOROUTE);
225 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
226 		if (mcopy) {
227 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
228 			    ICMP6_DST_UNREACH_NOROUTE, 0);
229 		}
230 		goto drop;
231 	}
232 	dst = satocsin6(rtcache_getdst(ro));
233 
234 	/*
235 	 * Source scope check: if a packet can't be delivered to its
236 	 * destination for the reason that the destination is beyond the scope
237 	 * of the source address, discard the packet and return an icmp6
238 	 * destination unreachable error with Code 2 (beyond scope of source
239 	 * address).  We use a local copy of ip6_src, since in6_setscope()
240 	 * will possibly modify its first argument.
241 	 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
242 	 */
243 	src_in6 = ip6->ip6_src;
244 	inzone = outzone = ~0;
245 	if (in6_setscope(&src_in6, rt->rt_ifp, &outzone) != 0 ||
246 	    in6_setscope(&src_in6, rcvif, &inzone) != 0 ||
247 	    inzone != outzone) {
248 		ip6_cantforward(ip6, rcvif, rt->rt_ifp,
249 		    "src[%s] inzone %d outzone %d",
250 		    in6_getscopename(&ip6->ip6_src), inzone, outzone);
251 		if (mcopy)
252 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
253 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
254 		goto drop;
255 	}
256 
257 #ifdef IPSEC
258 	/*
259 	 * If we need to encapsulate the packet, do it here.
260 	 * ipsec6_process_packet will send the packet using ip6_output.
261 	 */
262 	if (needipsec) {
263 		int s = splsoftnet();
264 		error = ipsec6_process_packet(m, sp->req, 0);
265 		splx(s);
266 		/* m is freed */
267 		if (mcopy)
268 			goto freecopy;
269 		goto out;
270 	}
271 #endif
272 
273 	/*
274 	 * Destination scope check: if a packet is going to break the scope
275 	 * zone of packet's destination address, discard it.  This case should
276 	 * usually be prevented by appropriately-configured routing table, but
277 	 * we need an explicit check because we may mistakenly forward the
278 	 * packet to a different zone by (e.g.) a default route.
279 	 */
280 	dst_in6 = ip6->ip6_dst;
281 	inzone = outzone = ~0;
282 	if (in6_setscope(&dst_in6, rcvif, &inzone) != 0 ||
283 	    in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
284 	    inzone != outzone) {
285 		ip6_cantforward(ip6, rcvif, rt->rt_ifp,
286 		    "dst[%s] inzone %d outzone %d",
287 		    in6_getscopename(&ip6->ip6_dst), inzone, outzone);
288 		if (mcopy)
289 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
290 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
291 		goto drop;
292 	}
293 
294 	if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
295 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
296 		if (mcopy) {
297 			u_long mtu;
298 
299 			mtu = IN6_LINKMTU(rt->rt_ifp);
300 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
301 		}
302 		goto drop;
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 == 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(satocsin6(rtcache_getdst(ro)),
321 		                         rt->rt_ifp)) {
322 			/*
323 			 * If the incoming interface is equal to the outgoing
324 			 * one, the link attached to the interface is
325 			 * point-to-point, and the IPv6 destination is
326 			 * regarded as on-link on the link, then it will be
327 			 * highly probable that the destination address does
328 			 * not exist on the link and that the packet is going
329 			 * to loop.  Thus, we immediately drop the packet and
330 			 * send an ICMPv6 error message.
331 			 * For other routing loops, we dare to let the packet
332 			 * go to the loop, so that a remote diagnosing host
333 			 * can detect the loop by traceroute.
334 			 * type/code is based on suggestion by Rich Draves.
335 			 * not sure if it is the best pick.
336 			 */
337 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
338 				    ICMP6_DST_UNREACH_ADDR, 0);
339 			goto drop;
340 		}
341 		type = ND_REDIRECT;
342 	}
343 
344 	/*
345 	 * Fake scoped addresses. Note that even link-local source or
346 	 * destination can appear, if the originating node just sends the
347 	 * packet to us (without address resolution for the destination).
348 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
349 	 * link identifiers, we can do this stuff after making a copy for
350 	 * returning an error.
351 	 */
352 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
353 		/*
354 		 * See corresponding comments in ip6_output.
355 		 * XXX: but is it possible that ip6_forward() sends a packet
356 		 *      to a loopback interface? I don't think so, and thus
357 		 *      I bark here. (jinmei@kame.net)
358 		 * XXX: it is common to route invalid packets to loopback.
359 		 *	also, the codepath will be visited on use of ::1 in
360 		 *	rthdr. (itojun)
361 		 */
362 #if 1
363 		if (0)
364 #else
365 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
366 #endif
367 		{
368 			char ip6bufs[INET6_ADDRSTRLEN];
369 			char ip6bufd[INET6_ADDRSTRLEN];
370 
371 			printf("ip6_forward: outgoing interface is loopback. "
372 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
373 			       IN6_PRINT(ip6bufs, &ip6->ip6_src),
374 			       IN6_PRINT(ip6bufd, &ip6->ip6_dst),
375 			       ip6->ip6_nxt, if_name(rcvif),
376 			       if_name(rt->rt_ifp));
377 		}
378 
379 		/* we can just use rcvif in forwarding. */
380 		origifp = rcvif;
381 	} else {
382 		origifp = rt->rt_ifp;
383 	}
384 
385 	/*
386 	 * clear embedded scope identifiers if necessary.
387 	 * in6_clearscope will touch the addresses only when necessary.
388 	 */
389 	in6_clearscope(&ip6->ip6_src);
390 	in6_clearscope(&ip6->ip6_dst);
391 
392 	/*
393 	 * Run through list of hooks for output packets.
394 	 */
395 	if ((error = pfil_run_hooks(inet6_pfil_hook, &m, rt->rt_ifp,
396 	    PFIL_OUT)) != 0) {
397 		IP6_STATINC(IP6_STAT_PFILDROP_OUT);
398 		goto senderr;
399 	}
400 	if (m == NULL) {
401 		IP6_STATINC(IP6_STAT_PFILDROP_OUT);
402 		goto freecopy;
403 	}
404 	ip6 = mtod(m, struct ip6_hdr *);
405 
406 	error = ip6_if_output(rt->rt_ifp, origifp, m, dst, rt);
407 	if (error) {
408 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
409 		IP6_STATINC(IP6_STAT_CANTFORWARD);
410 	} else {
411 		IP6_STATINC(IP6_STAT_FORWARD);
412 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
413 		if (type)
414 			IP6_STATINC(IP6_STAT_REDIRECTSENT);
415 		else {
416 #ifdef GATEWAY
417 			/* Need to release rt here */
418 			rtcache_unref(rt, ro);
419 			rt = NULL;
420 			if (mcopy->m_flags & M_CANFASTFWD)
421 				ip6flow_create(ro, mcopy);
422 #endif
423 			if (mcopy)
424 				goto freecopy;
425 		}
426 	}
427 
428 senderr:
429 	if (mcopy == NULL)
430 		goto out;
431 	switch (error) {
432 	case 0:
433 		if (type == ND_REDIRECT) {
434 			icmp6_redirect_output(mcopy, rt);
435 			goto out;
436 		}
437 		goto freecopy;
438 
439 	case EMSGSIZE:
440 		/* xxx MTU is constant in PPP? */
441 		goto freecopy;
442 
443 	case ENOBUFS:
444 		/* Tell source to slow down like source quench in IP? */
445 		goto freecopy;
446 
447 	case ENETUNREACH:	/* shouldn't happen, checked above */
448 	case EHOSTUNREACH:
449 	case ENETDOWN:
450 	case EHOSTDOWN:
451 	default:
452 		type = ICMP6_DST_UNREACH;
453 		code = ICMP6_DST_UNREACH_ADDR;
454 		break;
455 	}
456 	icmp6_error(mcopy, type, code, 0);
457 	goto out;
458 
459 freecopy:
460 	m_freem(mcopy);
461 	goto out;
462 
463 drop:
464 	m_freem(m);
465 
466 out:
467 #ifdef IPSEC
468 	if (sp != NULL)
469 		KEY_SP_UNREF(&sp);
470 #endif
471 	rtcache_unref(rt, ro);
472 	if (ro != NULL)
473 		rtcache_percpu_putref(ip6_forward_rt_percpu);
474 	if (rcvif != NULL)
475 		m_put_rcvif_psref(rcvif, &psref);
476 	return;
477 }
478