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