ip6_forward.c revision 1.84 1 /* $NetBSD: ip6_forward.c,v 1.84 2017/01/16 07:33:36 ryo 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.84 2017/01/16 07:33:36 ryo 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 #include <netipsec/xform.h>
69 #endif /* IPSEC */
70
71 #include <net/net_osdep.h>
72
73 extern percpu_t *ip6_forward_rt_percpu;
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_uptime)
97 return;
98 ip6_log_time = time_uptime;
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 = NULL;
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 struct ifnet *rcvif = NULL;
135 struct psref psref;
136 struct route *ro = NULL;
137 #ifdef IPSEC
138 int needipsec = 0;
139 struct secpolicy *sp = NULL;
140 #endif
141
142 /*
143 * Clear any in-bound checksum flags for this packet.
144 */
145 m->m_pkthdr.csum_flags = 0;
146
147 rcvif = m_get_rcvif_psref(m, &psref);
148 if (__predict_false(rcvif == NULL))
149 goto drop;
150
151 /*
152 * Do not forward packets to multicast destination (should be handled
153 * by ip6_mforward().
154 * Do not forward packets with unspecified source. It was discussed
155 * in July 2000, on ipngwg mailing list.
156 */
157 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
158 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
159 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
160 ip6_cantforward(ip6, rcvif, NULL,
161 ((m->m_flags & (M_BCAST|M_MCAST)) != 0) ? "bcast/mcast" :
162 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ? "mcast/dst" :
163 "unspec/src");
164 goto drop;
165 }
166
167 if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
168 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
169 icmp6_error(m, ICMP6_TIME_EXCEEDED,
170 ICMP6_TIME_EXCEED_TRANSIT, 0);
171 goto out;
172 }
173 ip6->ip6_hlim -= IPV6_HLIMDEC;
174
175 /*
176 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
177 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
178 * we need to generate an ICMP6 message to the src.
179 * Thanks to M_EXT, in most cases copy will not occur.
180 *
181 * It is important to save it before IPsec processing as IPsec
182 * processing may modify the mbuf.
183 */
184 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
185
186 #ifdef IPSEC
187 if (ipsec_used) {
188 /* Check the security policy (SP) for the packet */
189
190 sp = ipsec6_check_policy(m, NULL, 0, &needipsec, &error);
191 if (error != 0) {
192 /*
193 * Hack: -EINVAL is used to signal that a packet
194 * should be silently discarded. This is typically
195 * because we asked key management for an SA and
196 * it was delayed (e.g. kicked up to IKE).
197 */
198 if (error == -EINVAL)
199 error = 0;
200 goto freecopy;
201 }
202 }
203 #endif /* IPSEC */
204
205 ro = percpu_getref(ip6_forward_rt_percpu);
206 if (srcrt) {
207 union {
208 struct sockaddr dst;
209 struct sockaddr_in6 dst6;
210 } u;
211
212 sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
213 rt = rtcache_lookup(ro, &u.dst);
214 if (rt == NULL) {
215 IP6_STATINC(IP6_STAT_NOROUTE);
216 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
217 if (mcopy) {
218 icmp6_error(mcopy, ICMP6_DST_UNREACH,
219 ICMP6_DST_UNREACH_NOROUTE, 0);
220 }
221 goto drop;
222 }
223 } else if ((rt = rtcache_validate(ro)) == NULL &&
224 (rt = rtcache_update(ro, 1)) == NULL) {
225 /*
226 * rtcache_getdst(ip6_forward_rt)->sin6_addr was equal to
227 * ip6->ip6_dst
228 */
229 IP6_STATINC(IP6_STAT_NOROUTE);
230 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
231 if (mcopy) {
232 icmp6_error(mcopy, ICMP6_DST_UNREACH,
233 ICMP6_DST_UNREACH_NOROUTE, 0);
234 }
235 goto drop;
236 }
237 dst = satocsin6(rtcache_getdst(ro));
238
239 /*
240 * Source scope check: if a packet can't be delivered to its
241 * destination for the reason that the destination is beyond the scope
242 * of the source address, discard the packet and return an icmp6
243 * destination unreachable error with Code 2 (beyond scope of source
244 * address). We use a local copy of ip6_src, since in6_setscope()
245 * will possibly modify its first argument.
246 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
247 */
248 src_in6 = ip6->ip6_src;
249 inzone = outzone = ~0;
250 if (in6_setscope(&src_in6, rt->rt_ifp, &outzone) != 0 ||
251 in6_setscope(&src_in6, rcvif, &inzone) != 0 ||
252 inzone != outzone) {
253 ip6_cantforward(ip6, rcvif, rt->rt_ifp,
254 "src[%s] inzone %d outzone %d",
255 in6_getscopename(&ip6->ip6_src), inzone, outzone);
256 if (mcopy)
257 icmp6_error(mcopy, ICMP6_DST_UNREACH,
258 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
259 goto drop;
260 }
261
262 #ifdef IPSEC
263 /*
264 * If we need to encapsulate the packet, do it here
265 * ipsec6_proces_packet will send the packet using ip6_output
266 */
267 if (needipsec) {
268 int s = splsoftnet();
269 error = ipsec6_process_packet(m, sp->req);
270 splx(s);
271 if (mcopy)
272 goto freecopy;
273 }
274 #endif
275
276 /*
277 * Destination scope check: if a packet is going to break the scope
278 * zone of packet's destination address, discard it. This case should
279 * usually be prevented by appropriately-configured routing table, but
280 * we need an explicit check because we may mistakenly forward the
281 * packet to a different zone by (e.g.) a default route.
282 */
283 dst_in6 = ip6->ip6_dst;
284 inzone = outzone = ~0;
285 if (in6_setscope(&dst_in6, rcvif, &inzone) != 0 ||
286 in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
287 inzone != outzone) {
288 ip6_cantforward(ip6, rcvif, rt->rt_ifp,
289 "dst[%s] inzone %d outzone %d",
290 in6_getscopename(&ip6->ip6_dst), inzone, outzone);
291 if (mcopy)
292 icmp6_error(mcopy, ICMP6_DST_UNREACH,
293 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
294 goto drop;
295 }
296
297 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
298 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
299 if (mcopy) {
300 u_long mtu;
301
302 mtu = IN6_LINKMTU(rt->rt_ifp);
303 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
304 }
305 goto drop;
306 }
307
308 if (rt->rt_flags & RTF_GATEWAY)
309 dst = (struct sockaddr_in6 *)rt->rt_gateway;
310
311 /*
312 * If we are to forward the packet using the same interface
313 * as one we got the packet from, perhaps we should send a redirect
314 * to sender to shortcut a hop.
315 * Only send redirect if source is sending directly to us,
316 * and if packet was not source routed (or has any options).
317 * Also, don't send redirect if forwarding using a route
318 * modified by a redirect.
319 */
320 if (rt->rt_ifp == rcvif && !srcrt && ip6_sendredirects &&
321 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
322 if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
323 nd6_is_addr_neighbor(satocsin6(rtcache_getdst(ro)),
324 rt->rt_ifp)) {
325 /*
326 * If the incoming interface is equal to the outgoing
327 * one, the link attached to the interface is
328 * point-to-point, and the IPv6 destination is
329 * regarded as on-link on the link, then it will be
330 * highly probable that the destination address does
331 * not exist on the link and that the packet is going
332 * to loop. Thus, we immediately drop the packet and
333 * send an ICMPv6 error message.
334 * For other routing loops, we dare to let the packet
335 * go to the loop, so that a remote diagnosing host
336 * can detect the loop by traceroute.
337 * type/code is based on suggestion by Rich Draves.
338 * not sure if it is the best pick.
339 */
340 icmp6_error(mcopy, ICMP6_DST_UNREACH,
341 ICMP6_DST_UNREACH_ADDR, 0);
342 goto drop;
343 }
344 type = ND_REDIRECT;
345 }
346
347 /*
348 * Fake scoped addresses. Note that even link-local source or
349 * destinaion can appear, if the originating node just sends the
350 * packet to us (without address resolution for the destination).
351 * Since both icmp6_error and icmp6_redirect_output fill the embedded
352 * link identifiers, we can do this stuff after making a copy for
353 * returning an error.
354 */
355 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
356 /*
357 * See corresponding comments in ip6_output.
358 * XXX: but is it possible that ip6_forward() sends a packet
359 * to a loopback interface? I don't think so, and thus
360 * I bark here. (jinmei (at) kame.net)
361 * XXX: it is common to route invalid packets to loopback.
362 * also, the codepath will be visited on use of ::1 in
363 * rthdr. (itojun)
364 */
365 #if 1
366 if (0)
367 #else
368 if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
369 #endif
370 {
371 char ip6bufs[INET6_ADDRSTRLEN];
372 char ip6bufd[INET6_ADDRSTRLEN];
373
374 printf("ip6_forward: outgoing interface is loopback. "
375 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
376 ip6_sprintf(ip6bufs, &ip6->ip6_src),
377 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
378 ip6->ip6_nxt, if_name(rcvif),
379 if_name(rt->rt_ifp));
380 }
381
382 /* we can just use rcvif in forwarding. */
383 origifp = rcvif;
384 }
385 else
386 origifp = rt->rt_ifp;
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 = nd6_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 (m->m_flags & M_CANFASTFWD)
419 ip6flow_create(ro, m);
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 drop:
461 m_freem(m);
462 out:
463 rtcache_unref(rt, ro);
464 if (ro != NULL)
465 percpu_putref(ip6_forward_rt_percpu);
466 if (rcvif != NULL)
467 m_put_rcvif_psref(rcvif, &psref);
468 return;
469 }
470