ip6_forward.c revision 1.70 1 /* $NetBSD: ip6_forward.c,v 1.70 2012/03/22 20:34:40 drochner 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.70 2012/03/22 20:34:40 drochner Exp $");
35
36 #include "opt_gateway.h"
37 #include "opt_ipsec.h"
38 #include "opt_pfil_hooks.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.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
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/ip6.h>
59 #include <netinet6/ip6_var.h>
60 #include <netinet6/ip6_private.h>
61 #include <netinet6/scope6_var.h>
62 #include <netinet/icmp6.h>
63 #include <netinet6/nd6.h>
64
65 #ifdef FAST_IPSEC
66 #include <netipsec/ipsec.h>
67 #include <netipsec/ipsec6.h>
68 #include <netipsec/key.h>
69 #include <netipsec/xform.h>
70 #endif /* FAST_IPSEC */
71
72 #ifdef PFIL_HOOKS
73 #include <net/pfil.h>
74 #endif
75
76 #include <net/net_osdep.h>
77
78 struct route ip6_forward_rt;
79
80 #ifdef PFIL_HOOKS
81 extern struct pfil_head inet6_pfil_hook; /* XXX */
82 #endif
83
84 /*
85 * Forward a packet. If some error occurs return the sender
86 * an icmp packet. Note we can't always generate a meaningful
87 * icmp message because icmp doesn't have a large enough repertoire
88 * of codes and types.
89 *
90 * If not forwarding, just drop the packet. This could be confusing
91 * if ipforwarding was zero but some routing protocol was advancing
92 * us as a gateway to somewhere. However, we must let the routing
93 * protocol deal with that.
94 *
95 */
96
97 void
98 ip6_forward(struct mbuf *m, int srcrt)
99 {
100 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
101 const struct sockaddr_in6 *dst;
102 struct rtentry *rt;
103 int error = 0, type = 0, code = 0;
104 struct mbuf *mcopy = NULL;
105 struct ifnet *origifp; /* maybe unnecessary */
106 u_int32_t inzone, outzone;
107 struct in6_addr src_in6, dst_in6;
108 #ifdef FAST_IPSEC
109 struct secpolicy *sp = NULL;
110 int needipsec = 0;
111 int s;
112 #endif
113
114 /*
115 * Clear any in-bound checksum flags for this packet.
116 */
117 m->m_pkthdr.csum_flags = 0;
118
119 /*
120 * Do not forward packets to multicast destination (should be handled
121 * by ip6_mforward().
122 * Do not forward packets with unspecified source. It was discussed
123 * in July 2000, on ipngwg mailing list.
124 */
125 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
126 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
127 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
128 IP6_STATINC(IP6_STAT_CANTFORWARD);
129 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
130 if (ip6_log_time + ip6_log_interval < time_second) {
131 ip6_log_time = time_second;
132 log(LOG_DEBUG,
133 "cannot forward "
134 "from %s to %s nxt %d received on %s\n",
135 ip6_sprintf(&ip6->ip6_src),
136 ip6_sprintf(&ip6->ip6_dst),
137 ip6->ip6_nxt,
138 if_name(m->m_pkthdr.rcvif));
139 }
140 m_freem(m);
141 return;
142 }
143
144 if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
145 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
146 icmp6_error(m, ICMP6_TIME_EXCEEDED,
147 ICMP6_TIME_EXCEED_TRANSIT, 0);
148 return;
149 }
150 ip6->ip6_hlim -= IPV6_HLIMDEC;
151
152 /*
153 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
154 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
155 * we need to generate an ICMP6 message to the src.
156 * Thanks to M_EXT, in most cases copy will not occur.
157 *
158 * It is important to save it before IPsec processing as IPsec
159 * processing may modify the mbuf.
160 */
161 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
162
163 #ifdef FAST_IPSEC
164 /* Check the security policy (SP) for the packet */
165
166 sp = ipsec6_check_policy(m,NULL,0,&needipsec,&error);
167 if (error != 0) {
168 /*
169 * Hack: -EINVAL is used to signal that a packet
170 * should be silently discarded. This is typically
171 * because we asked key management for an SA and
172 * it was delayed (e.g. kicked up to IKE).
173 */
174 if (error == -EINVAL)
175 error = 0;
176 goto freecopy;
177 }
178 #endif /* FAST_IPSEC */
179
180 if (srcrt) {
181 union {
182 struct sockaddr dst;
183 struct sockaddr_in6 dst6;
184 } u;
185
186 sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
187 if ((rt = rtcache_lookup(&ip6_forward_rt, &u.dst)) == NULL) {
188 IP6_STATINC(IP6_STAT_NOROUTE);
189 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
190 if (mcopy) {
191 icmp6_error(mcopy, ICMP6_DST_UNREACH,
192 ICMP6_DST_UNREACH_NOROUTE, 0);
193 }
194 m_freem(m);
195 return;
196 }
197 } else if ((rt = rtcache_validate(&ip6_forward_rt)) == NULL &&
198 (rt = rtcache_update(&ip6_forward_rt, 1)) == NULL) {
199 /*
200 * rtcache_getdst(ip6_forward_rt)->sin6_addr was equal to
201 * ip6->ip6_dst
202 */
203 IP6_STATINC(IP6_STAT_NOROUTE);
204 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
205 if (mcopy) {
206 icmp6_error(mcopy, ICMP6_DST_UNREACH,
207 ICMP6_DST_UNREACH_NOROUTE, 0);
208 }
209 m_freem(m);
210 return;
211 }
212 dst = satocsin6(rtcache_getdst(&ip6_forward_rt));
213
214 /*
215 * Source scope check: if a packet can't be delivered to its
216 * destination for the reason that the destination is beyond the scope
217 * of the source address, discard the packet and return an icmp6
218 * destination unreachable error with Code 2 (beyond scope of source
219 * address). We use a local copy of ip6_src, since in6_setscope()
220 * will possibly modify its first argument.
221 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
222 */
223 src_in6 = ip6->ip6_src;
224 if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
225 /* XXX: this should not happen */
226 uint64_t *ip6s = IP6_STAT_GETREF();
227 ip6s[IP6_STAT_CANTFORWARD]++;
228 ip6s[IP6_STAT_BADSCOPE]++;
229 IP6_STAT_PUTREF();
230 m_freem(m);
231 return;
232 }
233 if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
234 uint64_t *ip6s = IP6_STAT_GETREF();
235 ip6s[IP6_STAT_CANTFORWARD]++;
236 ip6s[IP6_STAT_BADSCOPE]++;
237 IP6_STAT_PUTREF();
238 m_freem(m);
239 return;
240 }
241 if (inzone != outzone) {
242 uint64_t *ip6s = IP6_STAT_GETREF();
243 ip6s[IP6_STAT_CANTFORWARD]++;
244 ip6s[IP6_STAT_BADSCOPE]++;
245 IP6_STAT_PUTREF();
246 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
247
248 if (ip6_log_time + ip6_log_interval < time_second) {
249 ip6_log_time = time_second;
250 log(LOG_DEBUG,
251 "cannot forward "
252 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
253 ip6_sprintf(&ip6->ip6_src),
254 ip6_sprintf(&ip6->ip6_dst),
255 ip6->ip6_nxt,
256 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
257 }
258 if (mcopy)
259 icmp6_error(mcopy, ICMP6_DST_UNREACH,
260 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
261 m_freem(m);
262 return;
263 }
264 #ifdef FAST_IPSEC
265 /*
266 * If we need to encapsulate the packet, do it here
267 * ipsec6_proces_packet will send the packet using ip6_output
268 */
269 if (needipsec) {
270 s = splsoftnet();
271 error = ipsec6_process_packet(m,sp->req);
272 splx(s);
273 if (mcopy)
274 goto freecopy;
275 }
276 #endif
277
278
279
280 /*
281 * Destination scope check: if a packet is going to break the scope
282 * zone of packet's destination address, discard it. This case should
283 * usually be prevented by appropriately-configured routing table, but
284 * we need an explicit check because we may mistakenly forward the
285 * packet to a different zone by (e.g.) a default route.
286 */
287 dst_in6 = ip6->ip6_dst;
288 if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
289 in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
290 inzone != outzone) {
291 uint64_t *ip6s = IP6_STAT_GETREF();
292 ip6s[IP6_STAT_CANTFORWARD]++;
293 ip6s[IP6_STAT_BADSCOPE]++;
294 IP6_STAT_PUTREF();
295 m_freem(m);
296 return;
297 }
298
299 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
300 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
301 if (mcopy) {
302 u_long mtu;
303
304 mtu = IN6_LINKMTU(rt->rt_ifp);
305 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
306 }
307 m_freem(m);
308 return;
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 == m->m_pkthdr.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(
327 satocsin6(rtcache_getdst(&ip6_forward_rt)),
328 rt->rt_ifp)) {
329 /*
330 * If the incoming interface is equal to the outgoing
331 * one, the link attached to the interface is
332 * point-to-point, and the IPv6 destination is
333 * regarded as on-link on the link, then it will be
334 * highly probable that the destination address does
335 * not exist on the link and that the packet is going
336 * to loop. Thus, we immediately drop the packet and
337 * send an ICMPv6 error message.
338 * For other routing loops, we dare to let the packet
339 * go to the loop, so that a remote diagnosing host
340 * can detect the loop by traceroute.
341 * type/code is based on suggestion by Rich Draves.
342 * not sure if it is the best pick.
343 */
344 icmp6_error(mcopy, ICMP6_DST_UNREACH,
345 ICMP6_DST_UNREACH_ADDR, 0);
346 m_freem(m);
347 return;
348 }
349 type = ND_REDIRECT;
350 }
351
352 /*
353 * Fake scoped addresses. Note that even link-local source or
354 * destinaion can appear, if the originating node just sends the
355 * packet to us (without address resolution for the destination).
356 * Since both icmp6_error and icmp6_redirect_output fill the embedded
357 * link identifiers, we can do this stuff after making a copy for
358 * returning an error.
359 */
360 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
361 /*
362 * See corresponding comments in ip6_output.
363 * XXX: but is it possible that ip6_forward() sends a packet
364 * to a loopback interface? I don't think so, and thus
365 * I bark here. (jinmei (at) kame.net)
366 * XXX: it is common to route invalid packets to loopback.
367 * also, the codepath will be visited on use of ::1 in
368 * rthdr. (itojun)
369 */
370 #if 1
371 if (0)
372 #else
373 if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
374 #endif
375 {
376 printf("ip6_forward: outgoing interface is loopback. "
377 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
378 ip6_sprintf(&ip6->ip6_src),
379 ip6_sprintf(&ip6->ip6_dst),
380 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
381 if_name(rt->rt_ifp));
382 }
383
384 /* we can just use rcvif in forwarding. */
385 origifp = m->m_pkthdr.rcvif;
386 }
387 else
388 origifp = rt->rt_ifp;
389 /*
390 * clear embedded scope identifiers if necessary.
391 * in6_clearscope will touch the addresses only when necessary.
392 */
393 in6_clearscope(&ip6->ip6_src);
394 in6_clearscope(&ip6->ip6_dst);
395
396 #ifdef PFIL_HOOKS
397 /*
398 * Run through list of hooks for output packets.
399 */
400 if ((error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp,
401 PFIL_OUT)) != 0)
402 goto senderr;
403 if (m == NULL)
404 goto freecopy;
405 ip6 = mtod(m, struct ip6_hdr *);
406 #endif /* PFIL_HOOKS */
407
408 error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
409 if (error) {
410 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
411 IP6_STATINC(IP6_STAT_CANTFORWARD);
412 } else {
413 IP6_STATINC(IP6_STAT_FORWARD);
414 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
415 if (type)
416 IP6_STATINC(IP6_STAT_REDIRECTSENT);
417 else {
418 #ifdef GATEWAY
419 if (m->m_flags & M_CANFASTFWD)
420 ip6flow_create(&ip6_forward_rt, m);
421 #endif
422 if (mcopy)
423 goto freecopy;
424 }
425 }
426
427 #ifdef PFIL_HOOKS
428 senderr:
429 #endif
430 if (mcopy == NULL)
431 return;
432 switch (error) {
433 case 0:
434 if (type == ND_REDIRECT) {
435 icmp6_redirect_output(mcopy, rt);
436 return;
437 }
438 goto freecopy;
439
440 case EMSGSIZE:
441 /* xxx MTU is constant in PPP? */
442 goto freecopy;
443
444 case ENOBUFS:
445 /* Tell source to slow down like source quench in IP? */
446 goto freecopy;
447
448 case ENETUNREACH: /* shouldn't happen, checked above */
449 case EHOSTUNREACH:
450 case ENETDOWN:
451 case EHOSTDOWN:
452 default:
453 type = ICMP6_DST_UNREACH;
454 code = ICMP6_DST_UNREACH_ADDR;
455 break;
456 }
457 icmp6_error(mcopy, type, code, 0);
458 return;
459
460 freecopy:
461 m_freem(mcopy);
462 return;
463 }
464