ip6_forward.c revision 1.80.2.2 1 /* $NetBSD: ip6_forward.c,v 1.80.2.2 2017/03/20 06:57:51 pgoyette 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.80.2.2 2017/03/20 06:57:51 pgoyette 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
49 #include <net/if.h>
50 #include <net/route.h>
51 #include <net/pfil.h>
52
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/ip6.h>
57 #include <netinet6/ip6_var.h>
58 #include <netinet6/ip6_private.h>
59 #include <netinet6/scope6_var.h>
60 #include <netinet/icmp6.h>
61 #include <netinet6/nd6.h>
62
63 #ifdef IPSEC
64 #include <netipsec/ipsec.h>
65 #include <netipsec/ipsec6.h>
66 #include <netipsec/key.h>
67 #include <netipsec/xform.h>
68 #endif /* IPSEC */
69
70 #include <net/net_osdep.h>
71
72 struct route ip6_forward_rt;
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 */
121
122 void
123 ip6_forward(struct mbuf *m, int srcrt)
124 {
125 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
126 const struct sockaddr_in6 *dst;
127 struct rtentry *rt = NULL;
128 int error = 0, type = 0, code = 0;
129 struct mbuf *mcopy = NULL;
130 struct ifnet *origifp; /* maybe unnecessary */
131 uint32_t inzone, outzone;
132 struct in6_addr src_in6, dst_in6;
133 struct ifnet *rcvif = NULL;
134 struct psref psref;
135 struct route *ro = NULL;
136 #ifdef IPSEC
137 int needipsec = 0;
138 struct secpolicy *sp = NULL;
139 #endif
140
141 /*
142 * Clear any in-bound checksum flags for this packet.
143 */
144 m->m_pkthdr.csum_flags = 0;
145
146 rcvif = m_get_rcvif_psref(m, &psref);
147 if (__predict_false(rcvif == NULL))
148 goto drop;
149
150 /*
151 * Do not forward packets to multicast destination (should be handled
152 * by ip6_mforward().
153 * Do not forward packets with unspecified source. It was discussed
154 * in July 2000, on ipngwg mailing list.
155 */
156 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
157 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
158 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
159 ip6_cantforward(ip6, rcvif, NULL,
160 ((m->m_flags & (M_BCAST|M_MCAST)) != 0) ? "bcast/mcast" :
161 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ? "mcast/dst" :
162 "unspec/src");
163 goto drop;
164 }
165
166 if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
167 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
168 icmp6_error(m, ICMP6_TIME_EXCEEDED,
169 ICMP6_TIME_EXCEED_TRANSIT, 0);
170 goto out;
171 }
172 ip6->ip6_hlim -= IPV6_HLIMDEC;
173
174 /*
175 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
176 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
177 * we need to generate an ICMP6 message to the src.
178 * Thanks to M_EXT, in most cases copy will not occur.
179 *
180 * It is important to save it before IPsec processing as IPsec
181 * processing may modify the mbuf.
182 */
183 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
184
185 #ifdef IPSEC
186 if (ipsec_used) {
187 /* Check the security policy (SP) for the packet */
188
189 sp = ipsec6_check_policy(m, NULL, 0, &needipsec, &error);
190 if (error != 0) {
191 /*
192 * Hack: -EINVAL is used to signal that a packet
193 * should be silently discarded. This is typically
194 * because we asked key management for an SA and
195 * it was delayed (e.g. kicked up to IKE).
196 */
197 if (error == -EINVAL)
198 error = 0;
199 goto freecopy;
200 }
201 }
202 #endif /* IPSEC */
203
204 if (srcrt) {
205 union {
206 struct sockaddr dst;
207 struct sockaddr_in6 dst6;
208 } u;
209
210 sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
211 rt = rtcache_lookup(ro, &u.dst);
212 if (rt == NULL) {
213 IP6_STATINC(IP6_STAT_NOROUTE);
214 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
215 if (mcopy) {
216 icmp6_error(mcopy, ICMP6_DST_UNREACH,
217 ICMP6_DST_UNREACH_NOROUTE, 0);
218 }
219 goto drop;
220 }
221 } else if ((rt = rtcache_validate(&ip6_forward_rt)) == NULL &&
222 (rt = rtcache_update(&ip6_forward_rt, 1)) == NULL) {
223 /*
224 * rtcache_getdst(ip6_forward_rt)->sin6_addr was equal to
225 * ip6->ip6_dst
226 */
227 IP6_STATINC(IP6_STAT_NOROUTE);
228 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
229 if (mcopy) {
230 icmp6_error(mcopy, ICMP6_DST_UNREACH,
231 ICMP6_DST_UNREACH_NOROUTE, 0);
232 }
233 goto drop;
234 }
235 dst = satocsin6(rtcache_getdst(ro));
236
237 /*
238 * Source scope check: if a packet can't be delivered to its
239 * destination for the reason that the destination is beyond the scope
240 * of the source address, discard the packet and return an icmp6
241 * destination unreachable error with Code 2 (beyond scope of source
242 * address). We use a local copy of ip6_src, since in6_setscope()
243 * will possibly modify its first argument.
244 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
245 */
246 src_in6 = ip6->ip6_src;
247 inzone = outzone = ~0;
248 if (in6_setscope(&src_in6, rt->rt_ifp, &outzone) != 0 ||
249 in6_setscope(&src_in6, rcvif, &inzone) != 0 ||
250 inzone != outzone) {
251 ip6_cantforward(ip6, rcvif, rt->rt_ifp,
252 "src[%s] inzone %d outzone %d",
253 in6_getscopename(&ip6->ip6_src), inzone, outzone);
254 if (mcopy)
255 icmp6_error(mcopy, ICMP6_DST_UNREACH,
256 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
257 goto drop;
258 }
259
260 #ifdef IPSEC
261 /*
262 * If we need to encapsulate the packet, do it here
263 * ipsec6_proces_packet will send the packet using ip6_output
264 */
265 if (needipsec) {
266 int s = splsoftnet();
267 error = ipsec6_process_packet(m, sp->req);
268 splx(s);
269 if (mcopy)
270 goto freecopy;
271 }
272 #endif
273
274 /*
275 * Destination scope check: if a packet is going to break the scope
276 * zone of packet's destination address, discard it. This case should
277 * usually be prevented by appropriately-configured routing table, but
278 * we need an explicit check because we may mistakenly forward the
279 * packet to a different zone by (e.g.) a default route.
280 */
281 dst_in6 = ip6->ip6_dst;
282 inzone = outzone = ~0;
283 if (in6_setscope(&dst_in6, rcvif, &inzone) != 0 ||
284 in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
285 inzone != outzone) {
286 ip6_cantforward(ip6, rcvif, rt->rt_ifp,
287 "dst[%s] inzone %d outzone %d",
288 in6_getscopename(&ip6->ip6_dst), inzone, outzone);
289 if (mcopy)
290 icmp6_error(mcopy, ICMP6_DST_UNREACH,
291 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
292 goto drop;
293 }
294
295 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
296 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
297 if (mcopy) {
298 u_long mtu;
299
300 mtu = IN6_LINKMTU(rt->rt_ifp);
301 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
302 }
303 goto drop;
304 }
305
306 if (rt->rt_flags & RTF_GATEWAY)
307 dst = (struct sockaddr_in6 *)rt->rt_gateway;
308
309 /*
310 * If we are to forward the packet using the same interface
311 * as one we got the packet from, perhaps we should send a redirect
312 * to sender to shortcut a hop.
313 * Only send redirect if source is sending directly to us,
314 * and if packet was not source routed (or has any options).
315 * Also, don't send redirect if forwarding using a route
316 * modified by a redirect.
317 */
318 if (rt->rt_ifp == rcvif && !srcrt && ip6_sendredirects &&
319 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
320 if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
321 nd6_is_addr_neighbor(satocsin6(rtcache_getdst(ro)),
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 goto drop;
341 }
342 type = ND_REDIRECT;
343 }
344
345 /*
346 * Fake scoped addresses. Note that even link-local source or
347 * destinaion can appear, if the originating node just sends the
348 * packet to us (without address resolution for the destination).
349 * Since both icmp6_error and icmp6_redirect_output fill the embedded
350 * link identifiers, we can do this stuff after making a copy for
351 * returning an error.
352 */
353 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
354 /*
355 * See corresponding comments in ip6_output.
356 * XXX: but is it possible that ip6_forward() sends a packet
357 * to a loopback interface? I don't think so, and thus
358 * I bark here. (jinmei (at) kame.net)
359 * XXX: it is common to route invalid packets to loopback.
360 * also, the codepath will be visited on use of ::1 in
361 * rthdr. (itojun)
362 */
363 #if 1
364 if (0)
365 #else
366 if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
367 #endif
368 {
369 char ip6bufs[INET6_ADDRSTRLEN];
370 char ip6bufd[INET6_ADDRSTRLEN];
371
372 printf("ip6_forward: outgoing interface is loopback. "
373 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
374 IN6_PRINT(ip6bufs, &ip6->ip6_src),
375 IN6_PRINT(ip6bufd, &ip6->ip6_dst),
376 ip6->ip6_nxt, if_name(rcvif),
377 if_name(rt->rt_ifp));
378 }
379
380 /* we can just use rcvif in forwarding. */
381 origifp = rcvif;
382 }
383 else
384 origifp = rt->rt_ifp;
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 goto senderr;
398 if (m == NULL)
399 goto freecopy;
400 ip6 = mtod(m, struct ip6_hdr *);
401
402 error = ip6_if_output(rt->rt_ifp, origifp, m, dst, rt);
403 if (error) {
404 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
405 IP6_STATINC(IP6_STAT_CANTFORWARD);
406 } else {
407 IP6_STATINC(IP6_STAT_FORWARD);
408 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
409 if (type)
410 IP6_STATINC(IP6_STAT_REDIRECTSENT);
411 else {
412 #ifdef GATEWAY
413 /* Need to release rt here */
414 rtcache_unref(rt, ro);
415 rt = NULL;
416 if (m->m_flags & M_CANFASTFWD)
417 ip6flow_create(ro, m);
418 #endif
419 if (mcopy)
420 goto freecopy;
421 }
422 }
423
424 senderr:
425 if (mcopy == NULL)
426 goto out;
427 switch (error) {
428 case 0:
429 if (type == ND_REDIRECT) {
430 icmp6_redirect_output(mcopy, rt);
431 goto out;
432 }
433 goto freecopy;
434
435 case EMSGSIZE:
436 /* xxx MTU is constant in PPP? */
437 goto freecopy;
438
439 case ENOBUFS:
440 /* Tell source to slow down like source quench in IP? */
441 goto freecopy;
442
443 case ENETUNREACH: /* shouldn't happen, checked above */
444 case EHOSTUNREACH:
445 case ENETDOWN:
446 case EHOSTDOWN:
447 default:
448 type = ICMP6_DST_UNREACH;
449 code = ICMP6_DST_UNREACH_ADDR;
450 break;
451 }
452 icmp6_error(mcopy, type, code, 0);
453 goto out;
454
455 freecopy:
456 m_freem(mcopy);
457 goto out;
458 drop:
459 m_freem(m);
460 out:
461 rtcache_unref(rt, ro);
462 if (ro != NULL)
463 percpu_putref(ip6_forward_rt_percpu);
464 if (rcvif != NULL)
465 m_put_rcvif_psref(rcvif, &psref);
466 return;
467 }
468