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