ip6_forward.c revision 1.51 1 /* $NetBSD: ip6_forward.c,v 1.51 2006/12/09 05:33:08 dyoung 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.51 2006/12/09 05:33:08 dyoung Exp $");
35
36 #include "opt_ipsec.h"
37 #include "opt_pfil_hooks.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/errno.h>
47 #include <sys/time.h>
48 #include <sys/kernel.h>
49 #include <sys/syslog.h>
50
51 #include <net/if.h>
52 #include <net/route.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/scope6_var.h>
60 #include <netinet/icmp6.h>
61 #include <netinet6/nd6.h>
62
63 #ifdef IPSEC
64 #include <netinet6/ipsec.h>
65 #include <netkey/key.h>
66 #endif /* IPSEC */
67
68 #ifdef PFIL_HOOKS
69 #include <net/pfil.h>
70 #endif
71
72 #include <net/net_osdep.h>
73
74 struct route_in6 ip6_forward_rt;
75
76 #ifdef PFIL_HOOKS
77 extern struct pfil_head inet6_pfil_hook; /* XXX */
78 #endif
79
80 /*
81 * Forward a packet. If some error occurs return the sender
82 * an icmp packet. Note we can't always generate a meaningful
83 * icmp message because icmp doesn't have a large enough repertoire
84 * of codes and types.
85 *
86 * If not forwarding, just drop the packet. This could be confusing
87 * if ipforwarding was zero but some routing protocol was advancing
88 * us as a gateway to somewhere. However, we must let the routing
89 * protocol deal with that.
90 *
91 */
92
93 void
94 ip6_forward(m, srcrt)
95 struct mbuf *m;
96 int srcrt;
97 {
98 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
99 struct sockaddr_in6 *dst;
100 struct rtentry *rt;
101 int error = 0, type = 0, code = 0;
102 struct mbuf *mcopy = NULL;
103 struct ifnet *origifp; /* maybe unnecessary */
104 u_int32_t inzone, outzone;
105 struct in6_addr src_in6, dst_in6;
106 #ifdef IPSEC
107 struct secpolicy *sp = NULL;
108 int ipsecrt = 0;
109 #endif
110
111 #ifdef IPSEC
112 /*
113 * Check AH/ESP integrity.
114 */
115 /*
116 * Don't increment ip6s_cantforward because this is the check
117 * before forwarding packet actually.
118 */
119 if (ipsec6_in_reject(m, NULL)) {
120 ipsec6stat.in_polvio++;
121 m_freem(m);
122 return;
123 }
124 #endif /* IPSEC */
125
126 /*
127 * Do not forward packets to multicast destination (should be handled
128 * by ip6_mforward().
129 * Do not forward packets with unspecified source. It was discussed
130 * in July 2000, on ipngwg mailing list.
131 */
132 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
133 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
134 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
135 ip6stat.ip6s_cantforward++;
136 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
137 if (ip6_log_time + ip6_log_interval < time_second) {
138 ip6_log_time = time_second;
139 log(LOG_DEBUG,
140 "cannot forward "
141 "from %s to %s nxt %d received on %s\n",
142 ip6_sprintf(&ip6->ip6_src),
143 ip6_sprintf(&ip6->ip6_dst),
144 ip6->ip6_nxt,
145 if_name(m->m_pkthdr.rcvif));
146 }
147 m_freem(m);
148 return;
149 }
150
151 if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
152 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
153 icmp6_error(m, ICMP6_TIME_EXCEEDED,
154 ICMP6_TIME_EXCEED_TRANSIT, 0);
155 return;
156 }
157 ip6->ip6_hlim -= IPV6_HLIMDEC;
158
159 /*
160 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
161 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
162 * we need to generate an ICMP6 message to the src.
163 * Thanks to M_EXT, in most cases copy will not occur.
164 *
165 * It is important to save it before IPsec processing as IPsec
166 * processing may modify the mbuf.
167 */
168 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
169
170 #ifdef IPSEC
171 /* get a security policy for this packet */
172 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
173 IP_FORWARDING, &error);
174 if (sp == NULL) {
175 ipsec6stat.out_inval++;
176 ip6stat.ip6s_cantforward++;
177 if (mcopy) {
178 #if 0
179 /* XXX: what icmp ? */
180 #else
181 m_freem(mcopy);
182 #endif
183 }
184 m_freem(m);
185 return;
186 }
187
188 error = 0;
189
190 /* check policy */
191 switch (sp->policy) {
192 case IPSEC_POLICY_DISCARD:
193 /*
194 * This packet is just discarded.
195 */
196 ipsec6stat.out_polvio++;
197 ip6stat.ip6s_cantforward++;
198 key_freesp(sp);
199 if (mcopy) {
200 #if 0
201 /* XXX: what icmp ? */
202 #else
203 m_freem(mcopy);
204 #endif
205 }
206 m_freem(m);
207 return;
208
209 case IPSEC_POLICY_BYPASS:
210 case IPSEC_POLICY_NONE:
211 /* no need to do IPsec. */
212 key_freesp(sp);
213 goto skip_ipsec;
214
215 case IPSEC_POLICY_IPSEC:
216 if (sp->req == NULL) {
217 /* XXX should be panic ? */
218 printf("ip6_forward: No IPsec request specified.\n");
219 ip6stat.ip6s_cantforward++;
220 key_freesp(sp);
221 if (mcopy) {
222 #if 0
223 /* XXX: what icmp ? */
224 #else
225 m_freem(mcopy);
226 #endif
227 }
228 m_freem(m);
229 return;
230 }
231 /* do IPsec */
232 break;
233
234 case IPSEC_POLICY_ENTRUST:
235 default:
236 /* should be panic ?? */
237 printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
238 key_freesp(sp);
239 goto skip_ipsec;
240 }
241
242 {
243 struct ipsecrequest *isr = NULL;
244 struct ipsec_output_state state;
245
246 /*
247 * when the kernel forwards a packet, it is not proper to apply
248 * IPsec transport mode to the packet is not proper. this check
249 * avoid from this.
250 * at present, if there is even a transport mode SA request in the
251 * security policy, the kernel does not apply IPsec to the packet.
252 * this check is not enough because the following case is valid.
253 * ipsec esp/tunnel/xxx-xxx/require esp/transport//require;
254 */
255 for (isr = sp->req; isr; isr = isr->next) {
256 if (isr->saidx.mode == IPSEC_MODE_ANY)
257 goto doipsectunnel;
258 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
259 goto doipsectunnel;
260 }
261
262 /*
263 * if there's no need for tunnel mode IPsec, skip.
264 */
265 if (!isr)
266 goto skip_ipsec;
267
268 doipsectunnel:
269 /*
270 * All the extension headers will become inaccessible
271 * (since they can be encrypted).
272 * Don't panic, we need no more updates to extension headers
273 * on inner IPv6 packet (since they are now encapsulated).
274 *
275 * IPv6 [ESP|AH] IPv6 [extension headers] payload
276 */
277 bzero(&state, sizeof(state));
278 state.m = m;
279 state.ro = NULL; /* update at ipsec6_output_tunnel() */
280 state.dst = NULL; /* update at ipsec6_output_tunnel() */
281
282 error = ipsec6_output_tunnel(&state, sp, 0);
283
284 m = state.m;
285 key_freesp(sp);
286
287 if (error) {
288 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
289 switch (error) {
290 case EHOSTUNREACH:
291 case ENETUNREACH:
292 case EMSGSIZE:
293 case ENOBUFS:
294 case ENOMEM:
295 break;
296 default:
297 printf("ip6_forward (ipsec): error code %d\n", error);
298 /* FALLTHROUGH */
299 case ENOENT:
300 /* don't show these error codes to the user */
301 break;
302 }
303 ip6stat.ip6s_cantforward++;
304 if (mcopy) {
305 #if 0
306 /* XXX: what icmp ? */
307 #else
308 m_freem(mcopy);
309 #endif
310 }
311 m_freem(m);
312 return;
313 }
314
315 if (ip6 != mtod(m, struct ip6_hdr *)) {
316 /*
317 * now tunnel mode headers are added. we are originating
318 * packet instead of forwarding the packet.
319 */
320 ip6_output(m, NULL, NULL, IPV6_FORWARDING/*XXX*/, NULL, NULL,
321 NULL);
322 goto freecopy;
323 }
324
325 /* adjust pointer */
326 rt = state.ro ? state.ro->ro_rt : NULL;
327 dst = (struct sockaddr_in6 *)state.dst;
328 if (dst != NULL && rt != NULL) {
329 ipsecrt = 1;
330 goto skip_routing;
331 }
332 }
333 skip_ipsec:
334 #endif /* IPSEC */
335
336 dst = &ip6_forward_rt.ro_dst;
337 if (!srcrt) {
338 /*
339 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
340 */
341 if (ip6_forward_rt.ro_rt == NULL ||
342 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
343 if (ip6_forward_rt.ro_rt != NULL)
344 rtflush((struct route *)&ip6_forward_rt);
345 /* this probably fails but give it a try again */
346 rtalloc((struct route *)&ip6_forward_rt);
347 }
348
349 if (ip6_forward_rt.ro_rt == NULL) {
350 ip6stat.ip6s_noroute++;
351 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
352 if (mcopy) {
353 icmp6_error(mcopy, ICMP6_DST_UNREACH,
354 ICMP6_DST_UNREACH_NOROUTE, 0);
355 }
356 m_freem(m);
357 return;
358 }
359 } else if ((rt = ip6_forward_rt.ro_rt) == NULL ||
360 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
361 if (ip6_forward_rt.ro_rt != NULL)
362 rtflush((struct route *)&ip6_forward_rt);
363 bzero(dst, sizeof(*dst));
364 dst->sin6_len = sizeof(struct sockaddr_in6);
365 dst->sin6_family = AF_INET6;
366 dst->sin6_addr = ip6->ip6_dst;
367
368 rtalloc((struct route *)&ip6_forward_rt);
369 if (ip6_forward_rt.ro_rt == NULL) {
370 ip6stat.ip6s_noroute++;
371 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
372 if (mcopy) {
373 icmp6_error(mcopy, ICMP6_DST_UNREACH,
374 ICMP6_DST_UNREACH_NOROUTE, 0);
375 }
376 m_freem(m);
377 return;
378 }
379 }
380 rt = ip6_forward_rt.ro_rt;
381 #ifdef IPSEC
382 skip_routing:;
383 #endif /* IPSEC */
384
385 /*
386 * Source scope check: if a packet can't be delivered to its
387 * destination for the reason that the destination is beyond the scope
388 * of the source address, discard the packet and return an icmp6
389 * destination unreachable error with Code 2 (beyond scope of source
390 * address). We use a local copy of ip6_src, since in6_setscope()
391 * will possibly modify its first argument.
392 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
393 */
394 src_in6 = ip6->ip6_src;
395 if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
396 /* XXX: this should not happen */
397 ip6stat.ip6s_cantforward++;
398 ip6stat.ip6s_badscope++;
399 m_freem(m);
400 return;
401 }
402 if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
403 ip6stat.ip6s_cantforward++;
404 ip6stat.ip6s_badscope++;
405 m_freem(m);
406 return;
407 }
408 if (inzone != outzone
409 #ifdef IPSEC
410 && !ipsecrt
411 #endif
412 ) {
413 ip6stat.ip6s_cantforward++;
414 ip6stat.ip6s_badscope++;
415 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
416
417 if (ip6_log_time + ip6_log_interval < time_second) {
418 ip6_log_time = time_second;
419 log(LOG_DEBUG,
420 "cannot forward "
421 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
422 ip6_sprintf(&ip6->ip6_src),
423 ip6_sprintf(&ip6->ip6_dst),
424 ip6->ip6_nxt,
425 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
426 }
427 if (mcopy)
428 icmp6_error(mcopy, ICMP6_DST_UNREACH,
429 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
430 m_freem(m);
431 return;
432 }
433
434 /*
435 * Destination scope check: if a packet is going to break the scope
436 * zone of packet's destination address, discard it. This case should
437 * usually be prevented by appropriately-configured routing table, but
438 * we need an explicit check because we may mistakenly forward the
439 * packet to a different zone by (e.g.) a default route.
440 */
441 dst_in6 = ip6->ip6_dst;
442 if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
443 in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
444 inzone != outzone) {
445 ip6stat.ip6s_cantforward++;
446 ip6stat.ip6s_badscope++;
447 m_freem(m);
448 return;
449 }
450
451 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
452 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
453 if (mcopy) {
454 u_long mtu;
455 #ifdef IPSEC
456 struct secpolicy *xsp;
457 int ipsecerror;
458 size_t ipsechdrsiz;
459 #endif
460
461 mtu = IN6_LINKMTU(rt->rt_ifp);
462 #ifdef IPSEC
463 /*
464 * When we do IPsec tunnel ingress, we need to play
465 * with the link value (decrement IPsec header size
466 * from mtu value). The code is much simpler than v4
467 * case, as we have the outgoing interface for
468 * encapsulated packet as "rt->rt_ifp".
469 */
470 xsp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
471 IP_FORWARDING, &ipsecerror);
472 if (xsp) {
473 ipsechdrsiz = ipsec6_hdrsiz(mcopy,
474 IPSEC_DIR_OUTBOUND, NULL);
475 if (ipsechdrsiz < mtu)
476 mtu -= ipsechdrsiz;
477 }
478
479 /*
480 * if mtu becomes less than minimum MTU,
481 * tell minimum MTU (and I'll need to fragment it).
482 */
483 if (mtu < IPV6_MMTU)
484 mtu = IPV6_MMTU;
485 #endif
486 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
487 }
488 m_freem(m);
489 return;
490 }
491
492 if (rt->rt_flags & RTF_GATEWAY)
493 dst = (struct sockaddr_in6 *)rt->rt_gateway;
494
495 /*
496 * If we are to forward the packet using the same interface
497 * as one we got the packet from, perhaps we should send a redirect
498 * to sender to shortcut a hop.
499 * Only send redirect if source is sending directly to us,
500 * and if packet was not source routed (or has any options).
501 * Also, don't send redirect if forwarding using a route
502 * modified by a redirect.
503 */
504 if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && ip6_sendredirects &&
505 #ifdef IPSEC
506 !ipsecrt &&
507 #endif
508 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
509 if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
510 nd6_is_addr_neighbor((struct sockaddr_in6 *)&ip6_forward_rt.ro_dst, rt->rt_ifp)) {
511 /*
512 * If the incoming interface is equal to the outgoing
513 * one, the link attached to the interface is
514 * point-to-point, and the IPv6 destination is
515 * regarded as on-link on the link, then it will be
516 * highly probable that the destination address does
517 * not exist on the link and that the packet is going
518 * to loop. Thus, we immediately drop the packet and
519 * send an ICMPv6 error message.
520 * For other routing loops, we dare to let the packet
521 * go to the loop, so that a remote diagnosing host
522 * can detect the loop by traceroute.
523 * type/code is based on suggestion by Rich Draves.
524 * not sure if it is the best pick.
525 */
526 icmp6_error(mcopy, ICMP6_DST_UNREACH,
527 ICMP6_DST_UNREACH_ADDR, 0);
528 m_freem(m);
529 return;
530 }
531 type = ND_REDIRECT;
532 }
533
534 /*
535 * Fake scoped addresses. Note that even link-local source or
536 * destinaion can appear, if the originating node just sends the
537 * packet to us (without address resolution for the destination).
538 * Since both icmp6_error and icmp6_redirect_output fill the embedded
539 * link identifiers, we can do this stuff after making a copy for
540 * returning an error.
541 */
542 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
543 /*
544 * See corresponding comments in ip6_output.
545 * XXX: but is it possible that ip6_forward() sends a packet
546 * to a loopback interface? I don't think so, and thus
547 * I bark here. (jinmei (at) kame.net)
548 * XXX: it is common to route invalid packets to loopback.
549 * also, the codepath will be visited on use of ::1 in
550 * rthdr. (itojun)
551 */
552 #if 1
553 if (0)
554 #else
555 if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
556 #endif
557 {
558 printf("ip6_forward: outgoing interface is loopback. "
559 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
560 ip6_sprintf(&ip6->ip6_src),
561 ip6_sprintf(&ip6->ip6_dst),
562 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
563 if_name(rt->rt_ifp));
564 }
565
566 /* we can just use rcvif in forwarding. */
567 origifp = m->m_pkthdr.rcvif;
568 }
569 else
570 origifp = rt->rt_ifp;
571 /*
572 * clear embedded scope identifiers if necessary.
573 * in6_clearscope will touch the addresses only when necessary.
574 */
575 in6_clearscope(&ip6->ip6_src);
576 in6_clearscope(&ip6->ip6_dst);
577
578 #ifdef PFIL_HOOKS
579 /*
580 * Run through list of hooks for output packets.
581 */
582 if ((error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp,
583 PFIL_OUT)) != 0)
584 goto senderr;
585 if (m == NULL)
586 goto freecopy;
587 ip6 = mtod(m, struct ip6_hdr *);
588 #endif /* PFIL_HOOKS */
589
590 error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
591 if (error) {
592 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
593 ip6stat.ip6s_cantforward++;
594 } else {
595 ip6stat.ip6s_forward++;
596 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
597 if (type)
598 ip6stat.ip6s_redirectsent++;
599 else {
600 if (mcopy)
601 goto freecopy;
602 }
603 }
604
605 #ifdef PFIL_HOOKS
606 senderr:
607 #endif
608 if (mcopy == NULL)
609 return;
610 switch (error) {
611 case 0:
612 if (type == ND_REDIRECT) {
613 icmp6_redirect_output(mcopy, rt);
614 return;
615 }
616 goto freecopy;
617
618 case EMSGSIZE:
619 /* xxx MTU is constant in PPP? */
620 goto freecopy;
621
622 case ENOBUFS:
623 /* Tell source to slow down like source quench in IP? */
624 goto freecopy;
625
626 case ENETUNREACH: /* shouldn't happen, checked above */
627 case EHOSTUNREACH:
628 case ENETDOWN:
629 case EHOSTDOWN:
630 default:
631 type = ICMP6_DST_UNREACH;
632 code = ICMP6_DST_UNREACH_ADDR;
633 break;
634 }
635 icmp6_error(mcopy, type, code, 0);
636 return;
637
638 freecopy:
639 m_freem(mcopy);
640 return;
641 }
642