ip6_forward.c revision 1.73.2.3 1 /* $NetBSD: ip6_forward.c,v 1.73.2.3 2018/04/01 09:09:04 martin 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.73.2.3 2018/04/01 09:09:04 martin Exp $");
35
36 #include "opt_gateway.h"
37 #include "opt_ipsec.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mbuf.h>
42 #include <sys/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/errno.h>
46 #include <sys/time.h>
47 #include <sys/kernel.h>
48 #include <sys/syslog.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 struct route ip6_forward_rt;
74
75 extern pfil_head_t *inet6_pfil_hook; /* XXX */
76
77 /*
78 * Forward a packet. If some error occurs return the sender
79 * an icmp packet. Note we can't always generate a meaningful
80 * icmp message because icmp doesn't have a large enough repertoire
81 * of codes and types.
82 *
83 * If not forwarding, just drop the packet. This could be confusing
84 * if ipforwarding was zero but some routing protocol was advancing
85 * us as a gateway to somewhere. However, we must let the routing
86 * protocol deal with that.
87 *
88 */
89
90 void
91 ip6_forward(struct mbuf *m, int srcrt)
92 {
93 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
94 const struct sockaddr_in6 *dst;
95 struct rtentry *rt;
96 int error = 0, type = 0, code = 0;
97 struct mbuf *mcopy = NULL;
98 struct ifnet *origifp; /* maybe unnecessary */
99 u_int32_t inzone, outzone;
100 struct in6_addr src_in6, dst_in6;
101 #ifdef IPSEC
102 int needipsec = 0;
103 struct secpolicy *sp = NULL;
104 #endif
105
106 /*
107 * Clear any in-bound checksum flags for this packet.
108 */
109 m->m_pkthdr.csum_flags = 0;
110
111 /*
112 * Do not forward packets to multicast destination (should be handled
113 * by ip6_mforward().
114 * Do not forward packets with unspecified source. It was discussed
115 * in July 2000, on ipngwg mailing list.
116 */
117 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
118 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
119 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
120 IP6_STATINC(IP6_STAT_CANTFORWARD);
121 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
122 if (ip6_log_time + ip6_log_interval < time_second) {
123 ip6_log_time = time_second;
124 log(LOG_DEBUG,
125 "cannot forward "
126 "from %s to %s nxt %d received on %s\n",
127 ip6_sprintf(&ip6->ip6_src),
128 ip6_sprintf(&ip6->ip6_dst),
129 ip6->ip6_nxt,
130 if_name(m->m_pkthdr.rcvif));
131 }
132 m_freem(m);
133 return;
134 }
135
136 if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
137 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
138 icmp6_error(m, ICMP6_TIME_EXCEEDED,
139 ICMP6_TIME_EXCEED_TRANSIT, 0);
140 return;
141 }
142 ip6->ip6_hlim -= IPV6_HLIMDEC;
143
144 /*
145 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
146 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
147 * we need to generate an ICMP6 message to the src.
148 * Thanks to M_EXT, in most cases copy will not occur.
149 *
150 * It is important to save it before IPsec processing as IPsec
151 * processing may modify the mbuf.
152 */
153 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
154
155 #ifdef IPSEC
156 if (ipsec_used) {
157 /* Check the security policy (SP) for the packet */
158
159 sp = ipsec6_check_policy(m, NULL, 0, &needipsec, &error);
160 if (error != 0) {
161 /*
162 * Hack: -EINVAL is used to signal that a packet
163 * should be silently discarded. This is typically
164 * because we asked key management for an SA and
165 * it was delayed (e.g. kicked up to IKE).
166 */
167 if (error == -EINVAL)
168 error = 0;
169 m_freem(m);
170 goto freecopy;
171 }
172 }
173 #endif /* IPSEC */
174
175 if (srcrt) {
176 union {
177 struct sockaddr dst;
178 struct sockaddr_in6 dst6;
179 } u;
180
181 sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
182 if ((rt = rtcache_lookup(&ip6_forward_rt, &u.dst)) == NULL) {
183 IP6_STATINC(IP6_STAT_NOROUTE);
184 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
185 if (mcopy) {
186 icmp6_error(mcopy, ICMP6_DST_UNREACH,
187 ICMP6_DST_UNREACH_NOROUTE, 0);
188 }
189 m_freem(m);
190 return;
191 }
192 } else if ((rt = rtcache_validate(&ip6_forward_rt)) == NULL &&
193 (rt = rtcache_update(&ip6_forward_rt, 1)) == NULL) {
194 /*
195 * rtcache_getdst(ip6_forward_rt)->sin6_addr was equal to
196 * ip6->ip6_dst
197 */
198 IP6_STATINC(IP6_STAT_NOROUTE);
199 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
200 if (mcopy) {
201 icmp6_error(mcopy, ICMP6_DST_UNREACH,
202 ICMP6_DST_UNREACH_NOROUTE, 0);
203 }
204 m_freem(m);
205 return;
206 }
207 dst = satocsin6(rtcache_getdst(&ip6_forward_rt));
208
209 /*
210 * Source scope check: if a packet can't be delivered to its
211 * destination for the reason that the destination is beyond the scope
212 * of the source address, discard the packet and return an icmp6
213 * destination unreachable error with Code 2 (beyond scope of source
214 * address). We use a local copy of ip6_src, since in6_setscope()
215 * will possibly modify its first argument.
216 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
217 */
218 src_in6 = ip6->ip6_src;
219 if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
220 /* XXX: this should not happen */
221 uint64_t *ip6s = IP6_STAT_GETREF();
222 ip6s[IP6_STAT_CANTFORWARD]++;
223 ip6s[IP6_STAT_BADSCOPE]++;
224 IP6_STAT_PUTREF();
225 m_freem(m);
226 return;
227 }
228 if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
229 uint64_t *ip6s = IP6_STAT_GETREF();
230 ip6s[IP6_STAT_CANTFORWARD]++;
231 ip6s[IP6_STAT_BADSCOPE]++;
232 IP6_STAT_PUTREF();
233 m_freem(m);
234 return;
235 }
236 if (inzone != outzone) {
237 uint64_t *ip6s = IP6_STAT_GETREF();
238 ip6s[IP6_STAT_CANTFORWARD]++;
239 ip6s[IP6_STAT_BADSCOPE]++;
240 IP6_STAT_PUTREF();
241 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
242
243 if (ip6_log_time + ip6_log_interval < time_second) {
244 ip6_log_time = time_second;
245 log(LOG_DEBUG,
246 "cannot forward "
247 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
248 ip6_sprintf(&ip6->ip6_src),
249 ip6_sprintf(&ip6->ip6_dst),
250 ip6->ip6_nxt,
251 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
252 }
253 if (mcopy)
254 icmp6_error(mcopy, ICMP6_DST_UNREACH,
255 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
256 m_freem(m);
257 return;
258 }
259 #ifdef IPSEC
260 /*
261 * If we need to encapsulate the packet, do it here
262 * ipsec6_proces_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 return;
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 if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
284 in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
285 inzone != outzone) {
286 uint64_t *ip6s = IP6_STAT_GETREF();
287 ip6s[IP6_STAT_CANTFORWARD]++;
288 ip6s[IP6_STAT_BADSCOPE]++;
289 IP6_STAT_PUTREF();
290 m_freem(m);
291 return;
292 }
293
294 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
295 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
296 if (mcopy) {
297 u_long mtu;
298
299 mtu = IN6_LINKMTU(rt->rt_ifp);
300 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
301 }
302 m_freem(m);
303 return;
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 == m->m_pkthdr.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(
322 satocsin6(rtcache_getdst(&ip6_forward_rt)),
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 m_freem(m);
342 return;
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 printf("ip6_forward: outgoing interface is loopback. "
372 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
373 ip6_sprintf(&ip6->ip6_src),
374 ip6_sprintf(&ip6->ip6_dst),
375 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
376 if_name(rt->rt_ifp));
377 }
378
379 /* we can just use rcvif in forwarding. */
380 origifp = m->m_pkthdr.rcvif;
381 }
382 else
383 origifp = rt->rt_ifp;
384 /*
385 * clear embedded scope identifiers if necessary.
386 * in6_clearscope will touch the addresses only when necessary.
387 */
388 in6_clearscope(&ip6->ip6_src);
389 in6_clearscope(&ip6->ip6_dst);
390
391 /*
392 * Run through list of hooks for output packets.
393 */
394 if ((error = pfil_run_hooks(inet6_pfil_hook, &m, rt->rt_ifp,
395 PFIL_OUT)) != 0)
396 goto senderr;
397 if (m == NULL)
398 goto freecopy;
399 ip6 = mtod(m, struct ip6_hdr *);
400
401 error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
402 if (error) {
403 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
404 IP6_STATINC(IP6_STAT_CANTFORWARD);
405 } else {
406 IP6_STATINC(IP6_STAT_FORWARD);
407 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
408 if (type)
409 IP6_STATINC(IP6_STAT_REDIRECTSENT);
410 else {
411 #ifdef GATEWAY
412 if (mcopy->m_flags & M_CANFASTFWD)
413 ip6flow_create(&ip6_forward_rt, mcopy);
414 #endif
415 if (mcopy)
416 goto freecopy;
417 }
418 }
419
420 senderr:
421 if (mcopy == NULL)
422 return;
423 switch (error) {
424 case 0:
425 if (type == ND_REDIRECT) {
426 icmp6_redirect_output(mcopy, rt);
427 return;
428 }
429 goto freecopy;
430
431 case EMSGSIZE:
432 /* xxx MTU is constant in PPP? */
433 goto freecopy;
434
435 case ENOBUFS:
436 /* Tell source to slow down like source quench in IP? */
437 goto freecopy;
438
439 case ENETUNREACH: /* shouldn't happen, checked above */
440 case EHOSTUNREACH:
441 case ENETDOWN:
442 case EHOSTDOWN:
443 default:
444 type = ICMP6_DST_UNREACH;
445 code = ICMP6_DST_UNREACH_ADDR;
446 break;
447 }
448 icmp6_error(mcopy, type, code, 0);
449 return;
450
451 freecopy:
452 m_freem(mcopy);
453 return;
454 }
455