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