ip6_forward.c revision 1.62.2.1 1 /* $NetBSD: ip6_forward.c,v 1.62.2.1 2008/02/22 02:53:33 keiichi 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.62.2.1 2008/02/22 02:53:33 keiichi 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 MOBILE_IPV6
64 #include <netinet/ip6mh.h>
65 #include <netinet6/mip6.h>
66 #include <netinet6/mip6_var.h>
67 #endif /* MOBILE_IPV6 */
68
69 #ifdef IPSEC
70 #include <netinet6/ipsec.h>
71 #include <netkey/key.h>
72 #endif /* IPSEC */
73
74 #ifdef FAST_IPSEC
75 #include <netipsec/ipsec.h>
76 #include <netipsec/ipsec6.h>
77 #include <netipsec/key.h>
78 #include <netipsec/xform.h>
79 #endif /* FAST_IPSEC */
80
81 #ifdef PFIL_HOOKS
82 #include <net/pfil.h>
83 #endif
84
85 #include <net/net_osdep.h>
86
87 struct route ip6_forward_rt;
88
89 #ifdef PFIL_HOOKS
90 extern struct pfil_head inet6_pfil_hook; /* XXX */
91 #endif
92
93 /*
94 * Forward a packet. If some error occurs return the sender
95 * an icmp packet. Note we can't always generate a meaningful
96 * icmp message because icmp doesn't have a large enough repertoire
97 * of codes and types.
98 *
99 * If not forwarding, just drop the packet. This could be confusing
100 * if ipforwarding was zero but some routing protocol was advancing
101 * us as a gateway to somewhere. However, we must let the routing
102 * protocol deal with that.
103 *
104 */
105
106 void
107 ip6_forward(struct mbuf *m, int srcrt)
108 {
109 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
110 const struct sockaddr_in6 *dst;
111 struct rtentry *rt;
112 int error = 0, type = 0, code = 0;
113 struct mbuf *mcopy = NULL;
114 struct ifnet *origifp; /* maybe unnecessary */
115 u_int32_t inzone, outzone;
116 struct in6_addr src_in6, dst_in6;
117 #ifdef IPSEC
118 struct secpolicy *sp = NULL;
119 int ipsecrt = 0;
120 #endif
121 #ifdef MOBILE_IPV6
122 struct mip6_bc_internal *bce;
123 #endif /* MOBILE_IPV6 */
124 #ifdef FAST_IPSEC
125 struct secpolicy *sp = NULL;
126 int needipsec = 0;
127 int s;
128 #endif
129
130 #ifdef IPSEC
131 #if defined(MOBILE_IPV6) && NMIP > 0
132 {
133 /*
134 * XXX skip IPsec policy integrity check if the next
135 * hop is me. This is dirty but we need this trick
136 * when we use an IPsec tunnel mode policy for
137 * protocol 'any' between a home agent and a mobile
138 * node.
139 */
140 struct in6_ifaddr *ia;
141
142 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
143 if ((ia->ia6_flags & IN6_IFF_NOTREADY) == 0 &&
144 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
145 &ip6->ip6_dst))
146 goto skip_ipsec6_in_reject;
147 }
148 }
149 #endif /* MOBILE_IPV6 && NMIP > 0 */
150 /*
151 * Check AH/ESP integrity.
152 */
153 /*
154 * Don't increment ip6s_cantforward because this is the check
155 * before forwarding packet actually.
156 */
157 if (ipsec6_in_reject(m, NULL)) {
158 ipsec6stat.in_polvio++;
159 m_freem(m);
160 return;
161 }
162 #if defined(MOBILE_IPV6) && NMIP > 0
163 skip_ipsec6_in_reject:
164 #endif /* MOBILE_IPV6 && NMIP > 0 */
165 #endif /* IPSEC */
166
167 /*
168 * Do not forward packets to multicast destination (should be handled
169 * by ip6_mforward().
170 * Do not forward packets with unspecified source. It was discussed
171 * in July 2000, on ipngwg mailing list.
172 */
173 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
174 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
175 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
176 ip6stat.ip6s_cantforward++;
177 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
178 if (ip6_log_time + ip6_log_interval < time_second) {
179 ip6_log_time = time_second;
180 log(LOG_DEBUG,
181 "cannot forward "
182 "from %s to %s nxt %d received on %s\n",
183 ip6_sprintf(&ip6->ip6_src),
184 ip6_sprintf(&ip6->ip6_dst),
185 ip6->ip6_nxt,
186 if_name(m->m_pkthdr.rcvif));
187 }
188 m_freem(m);
189 return;
190 }
191
192 if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
193 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
194 icmp6_error(m, ICMP6_TIME_EXCEEDED,
195 ICMP6_TIME_EXCEED_TRANSIT, 0);
196 return;
197 }
198 ip6->ip6_hlim -= IPV6_HLIMDEC;
199
200 /*
201 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
202 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
203 * we need to generate an ICMP6 message to the src.
204 * Thanks to M_EXT, in most cases copy will not occur.
205 *
206 * It is important to save it before IPsec processing as IPsec
207 * processing may modify the mbuf.
208 */
209 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
210
211 #ifdef IPSEC
212 /* get a security policy for this packet */
213 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
214 IP_FORWARDING, &error);
215 if (sp == NULL) {
216 ipsec6stat.out_inval++;
217 ip6stat.ip6s_cantforward++;
218 if (mcopy) {
219 #if 0
220 /* XXX: what icmp ? */
221 #else
222 m_freem(mcopy);
223 #endif
224 }
225 m_freem(m);
226 return;
227 }
228
229 error = 0;
230
231 /* check policy */
232 switch (sp->policy) {
233 case IPSEC_POLICY_DISCARD:
234 /*
235 * This packet is just discarded.
236 */
237 ipsec6stat.out_polvio++;
238 ip6stat.ip6s_cantforward++;
239 key_freesp(sp);
240 if (mcopy) {
241 #if 0
242 /* XXX: what icmp ? */
243 #else
244 m_freem(mcopy);
245 #endif
246 }
247 m_freem(m);
248 return;
249
250 case IPSEC_POLICY_BYPASS:
251 case IPSEC_POLICY_NONE:
252 /* no need to do IPsec. */
253 key_freesp(sp);
254 goto skip_ipsec;
255
256 case IPSEC_POLICY_IPSEC:
257 if (sp->req == NULL) {
258 /* XXX should be panic ? */
259 printf("ip6_forward: No IPsec request specified.\n");
260 ip6stat.ip6s_cantforward++;
261 key_freesp(sp);
262 if (mcopy) {
263 #if 0
264 /* XXX: what icmp ? */
265 #else
266 m_freem(mcopy);
267 #endif
268 }
269 m_freem(m);
270 return;
271 }
272 /* do IPsec */
273 break;
274
275 case IPSEC_POLICY_ENTRUST:
276 default:
277 /* should be panic ?? */
278 printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
279 key_freesp(sp);
280 goto skip_ipsec;
281 }
282
283 {
284 struct ipsecrequest *isr = NULL;
285 struct ipsec_output_state state;
286
287 /*
288 * when the kernel forwards a packet, it is not proper to apply
289 * IPsec transport mode to the packet is not proper. this check
290 * avoid from this.
291 * at present, if there is even a transport mode SA request in the
292 * security policy, the kernel does not apply IPsec to the packet.
293 * this check is not enough because the following case is valid.
294 * ipsec esp/tunnel/xxx-xxx/require esp/transport//require;
295 */
296 for (isr = sp->req; isr; isr = isr->next) {
297 if (isr->saidx.mode == IPSEC_MODE_ANY)
298 goto doipsectunnel;
299 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
300 goto doipsectunnel;
301 }
302
303 /*
304 * if there's no need for tunnel mode IPsec, skip.
305 */
306 if (!isr)
307 goto skip_ipsec;
308
309 doipsectunnel:
310 /*
311 * All the extension headers will become inaccessible
312 * (since they can be encrypted).
313 * Don't panic, we need no more updates to extension headers
314 * on inner IPv6 packet (since they are now encapsulated).
315 *
316 * IPv6 [ESP|AH] IPv6 [extension headers] payload
317 */
318 bzero(&state, sizeof(state));
319 state.m = m;
320 state.ro = NULL; /* update at ipsec6_output_tunnel() */
321 state.dst = NULL; /* update at ipsec6_output_tunnel() */
322
323 error = ipsec6_output_tunnel(&state, sp, 0);
324
325 m = state.m;
326 key_freesp(sp);
327
328 if (error) {
329 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
330 switch (error) {
331 case EHOSTUNREACH:
332 case ENETUNREACH:
333 case EMSGSIZE:
334 case ENOBUFS:
335 case ENOMEM:
336 break;
337 default:
338 printf("ip6_forward (ipsec): error code %d\n", error);
339 /* FALLTHROUGH */
340 case ENOENT:
341 /* don't show these error codes to the user */
342 break;
343 }
344 ip6stat.ip6s_cantforward++;
345 if (mcopy) {
346 #if 0
347 /* XXX: what icmp ? */
348 #else
349 m_freem(mcopy);
350 #endif
351 }
352 m_freem(m);
353 return;
354 }
355
356 if (ip6 != mtod(m, struct ip6_hdr *)) {
357 /*
358 * now tunnel mode headers are added. we are originating
359 * packet instead of forwarding the packet.
360 */
361 ip6_output(m, NULL, NULL, IPV6_FORWARDING/*XXX*/, NULL, NULL,
362 NULL);
363 goto freecopy;
364 }
365
366 /* adjust pointer */
367 rt = state.ro ? rtcache_validate(state.ro) : NULL;
368 dst = (const struct sockaddr_in6 *)state.dst;
369 if (dst != NULL && rt != NULL) {
370 ipsecrt = 1;
371 goto skip_routing;
372 }
373 }
374 skip_ipsec:
375 #endif /* IPSEC */
376 #ifdef FAST_IPSEC
377 /* Check the security policy (SP) for the packet */
378
379 sp = ipsec6_check_policy(m,NULL,0,&needipsec,&error);
380 if (error != 0) {
381 /*
382 * Hack: -EINVAL is used to signal that a packet
383 * should be silently discarded. This is typically
384 * because we asked key management for an SA and
385 * it was delayed (e.g. kicked up to IKE).
386 */
387 if (error == -EINVAL)
388 error = 0;
389 goto freecopy;
390 }
391 #endif /* FAST_IPSEC */
392
393 if (srcrt) {
394 union {
395 struct sockaddr dst;
396 struct sockaddr_in6 dst6;
397 } u;
398
399 sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
400 if ((rt = rtcache_lookup(&ip6_forward_rt, &u.dst)) == NULL) {
401 ip6stat.ip6s_noroute++;
402 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
403 if (mcopy) {
404 icmp6_error(mcopy, ICMP6_DST_UNREACH,
405 ICMP6_DST_UNREACH_NOROUTE, 0);
406 }
407 m_freem(m);
408 return;
409 }
410 } else if ((rt = rtcache_validate(&ip6_forward_rt)) == NULL &&
411 (rt = rtcache_update(&ip6_forward_rt, 1)) == NULL) {
412 /*
413 * rtcache_getdst(ip6_forward_rt)->sin6_addr was equal to
414 * ip6->ip6_dst
415 */
416 ip6stat.ip6s_noroute++;
417 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
418 if (mcopy) {
419 icmp6_error(mcopy, ICMP6_DST_UNREACH,
420 ICMP6_DST_UNREACH_NOROUTE, 0);
421 }
422 m_freem(m);
423 return;
424 }
425 dst = satocsin6(rtcache_getdst(&ip6_forward_rt));
426 #ifdef IPSEC
427 skip_routing:;
428 #endif /* IPSEC */
429
430 /*
431 * Source scope check: if a packet can't be delivered to its
432 * destination for the reason that the destination is beyond the scope
433 * of the source address, discard the packet and return an icmp6
434 * destination unreachable error with Code 2 (beyond scope of source
435 * address). We use a local copy of ip6_src, since in6_setscope()
436 * will possibly modify its first argument.
437 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
438 */
439 src_in6 = ip6->ip6_src;
440 if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
441 /* XXX: this should not happen */
442 ip6stat.ip6s_cantforward++;
443 ip6stat.ip6s_badscope++;
444 m_freem(m);
445 return;
446 }
447 if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
448 ip6stat.ip6s_cantforward++;
449 ip6stat.ip6s_badscope++;
450 m_freem(m);
451 return;
452 }
453 if (inzone != outzone
454 #ifdef IPSEC
455 && !ipsecrt
456 #endif
457 ) {
458 ip6stat.ip6s_cantforward++;
459 ip6stat.ip6s_badscope++;
460 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
461
462 if (ip6_log_time + ip6_log_interval < time_second) {
463 ip6_log_time = time_second;
464 log(LOG_DEBUG,
465 "cannot forward "
466 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
467 ip6_sprintf(&ip6->ip6_src),
468 ip6_sprintf(&ip6->ip6_dst),
469 ip6->ip6_nxt,
470 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
471 }
472 if (mcopy)
473 icmp6_error(mcopy, ICMP6_DST_UNREACH,
474 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
475 m_freem(m);
476 return;
477 }
478 #ifdef FAST_IPSEC
479 /*
480 * If we need to encapsulate the packet, do it here
481 * ipsec6_proces_packet will send the packet using ip6_output
482 */
483 if (needipsec) {
484 s = splsoftnet();
485 error = ipsec6_process_packet(m,sp->req);
486 splx(s);
487 if (mcopy)
488 goto freecopy;
489 }
490 #endif
491
492 #ifdef MOBILE_IPV6
493 /* This codes are only for Home Agent */
494 if (!MIP6_IS_HA)
495 goto bc_check_done;
496 /*
497 * intercept and tunnel packets for home addresses
498 * which we are acting as a home agent for.
499 */
500
501 /* XXX need some policy to determine bid for MCOA */
502 if ((bce = mip6_bce_get(&ip6->ip6_dst, NULL, NULL, 0)) &&
503 (bce->mbc_flags & IP6_MH_BU_HOME)){
504 if (IN6_IS_ADDR_LINKLOCAL(&bce->mbc_hoa)
505 || IN6_IS_ADDR_SITELOCAL(&bce->mbc_hoa)) {
506 ip6stat.ip6s_cantforward++;
507 if (mcopy) {
508 icmp6_error(mcopy, ICMP6_DST_UNREACH,
509 ICMP6_DST_UNREACH_ADDR, 0);
510 }
511 m_freem(m);
512 return;
513 }
514
515 if (m->m_pkthdr.len > IPV6_MMTU) {
516 u_long mtu = IPV6_MMTU;
517 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig); */
518 if (mcopy) {
519 icmp6_error(mcopy,
520 ICMP6_PACKET_TOO_BIG, 0, mtu);
521 }
522 m_freem(m);
523 return;
524 }
525
526 /*
527 * if we have a binding cache entry for the
528 * ip6_dst, we are acting as a home agent for
529 * that node. before sending a packet as a
530 * tunneled packet, we must make sure that
531 * encaptab is ready. if dad is enabled and
532 * not completed yet, encaptab will be NULL.
533 */
534 if (mip6_encapsulate(&m, IFA_IN6(bce->mbc_ifaddr),
535 &bce->mbc_coa) != 0) {
536 ip6stat.ip6s_cantforward++;
537 }
538 if (mcopy)
539 m_freem(mcopy);
540 return;
541 bc_check_done:
542 ;
543 }
544 #endif /* MOBILE_IPV6 */
545
546 /*
547 * Destination scope check: if a packet is going to break the scope
548 * zone of packet's destination address, discard it. This case should
549 * usually be prevented by appropriately-configured routing table, but
550 * we need an explicit check because we may mistakenly forward the
551 * packet to a different zone by (e.g.) a default route.
552 */
553 dst_in6 = ip6->ip6_dst;
554 if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
555 in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
556 inzone != outzone) {
557 ip6stat.ip6s_cantforward++;
558 ip6stat.ip6s_badscope++;
559 m_freem(m);
560 return;
561 }
562
563 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
564 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
565 if (mcopy) {
566 u_long mtu;
567 #ifdef IPSEC
568 struct secpolicy *xsp;
569 int ipsecerror;
570 size_t ipsechdrsiz;
571 #endif
572
573 mtu = IN6_LINKMTU(rt->rt_ifp);
574 #ifdef IPSEC
575 /*
576 * When we do IPsec tunnel ingress, we need to play
577 * with the link value (decrement IPsec header size
578 * from mtu value). The code is much simpler than v4
579 * case, as we have the outgoing interface for
580 * encapsulated packet as "rt->rt_ifp".
581 */
582 xsp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
583 IP_FORWARDING, &ipsecerror);
584 if (xsp) {
585 ipsechdrsiz = ipsec6_hdrsiz(mcopy,
586 IPSEC_DIR_OUTBOUND, NULL);
587 if (ipsechdrsiz < mtu)
588 mtu -= ipsechdrsiz;
589 }
590
591 /*
592 * if mtu becomes less than minimum MTU,
593 * tell minimum MTU (and I'll need to fragment it).
594 */
595 if (mtu < IPV6_MMTU)
596 mtu = IPV6_MMTU;
597 #endif
598 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
599 }
600 m_freem(m);
601 return;
602 }
603
604 if (rt->rt_flags & RTF_GATEWAY)
605 dst = (struct sockaddr_in6 *)rt->rt_gateway;
606
607 /*
608 * If we are to forward the packet using the same interface
609 * as one we got the packet from, perhaps we should send a redirect
610 * to sender to shortcut a hop.
611 * Only send redirect if source is sending directly to us,
612 * and if packet was not source routed (or has any options).
613 * Also, don't send redirect if forwarding using a route
614 * modified by a redirect.
615 */
616 if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && ip6_sendredirects &&
617 #ifdef IPSEC
618 !ipsecrt &&
619 #endif
620 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
621 if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
622 nd6_is_addr_neighbor(
623 satocsin6(rtcache_getdst(&ip6_forward_rt)),
624 rt->rt_ifp)) {
625 /*
626 * If the incoming interface is equal to the outgoing
627 * one, the link attached to the interface is
628 * point-to-point, and the IPv6 destination is
629 * regarded as on-link on the link, then it will be
630 * highly probable that the destination address does
631 * not exist on the link and that the packet is going
632 * to loop. Thus, we immediately drop the packet and
633 * send an ICMPv6 error message.
634 * For other routing loops, we dare to let the packet
635 * go to the loop, so that a remote diagnosing host
636 * can detect the loop by traceroute.
637 * type/code is based on suggestion by Rich Draves.
638 * not sure if it is the best pick.
639 */
640 icmp6_error(mcopy, ICMP6_DST_UNREACH,
641 ICMP6_DST_UNREACH_ADDR, 0);
642 m_freem(m);
643 return;
644 }
645 #ifdef MOBILE_IPV6
646 /* if the node is HA, redirect must not be sent.
647 XXX this logic must be re-considerred.
648 (e.g. lookup BCE or check state or something...) */
649 if (!MIP6_IS_HA)
650 #endif /* defined(MOBILE_IPV6) */
651 type = ND_REDIRECT;
652 }
653
654 /*
655 * Fake scoped addresses. Note that even link-local source or
656 * destinaion can appear, if the originating node just sends the
657 * packet to us (without address resolution for the destination).
658 * Since both icmp6_error and icmp6_redirect_output fill the embedded
659 * link identifiers, we can do this stuff after making a copy for
660 * returning an error.
661 */
662 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
663 /*
664 * See corresponding comments in ip6_output.
665 * XXX: but is it possible that ip6_forward() sends a packet
666 * to a loopback interface? I don't think so, and thus
667 * I bark here. (jinmei (at) kame.net)
668 * XXX: it is common to route invalid packets to loopback.
669 * also, the codepath will be visited on use of ::1 in
670 * rthdr. (itojun)
671 */
672 #if 1
673 if (0)
674 #else
675 if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
676 #endif
677 {
678 printf("ip6_forward: outgoing interface is loopback. "
679 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
680 ip6_sprintf(&ip6->ip6_src),
681 ip6_sprintf(&ip6->ip6_dst),
682 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
683 if_name(rt->rt_ifp));
684 }
685
686 /* we can just use rcvif in forwarding. */
687 origifp = m->m_pkthdr.rcvif;
688 }
689 else
690 origifp = rt->rt_ifp;
691 /*
692 * clear embedded scope identifiers if necessary.
693 * in6_clearscope will touch the addresses only when necessary.
694 */
695 in6_clearscope(&ip6->ip6_src);
696 in6_clearscope(&ip6->ip6_dst);
697
698 #ifdef PFIL_HOOKS
699 /*
700 * Run through list of hooks for output packets.
701 */
702 if ((error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp,
703 PFIL_OUT)) != 0)
704 goto senderr;
705 if (m == NULL)
706 goto freecopy;
707 ip6 = mtod(m, struct ip6_hdr *);
708 #endif /* PFIL_HOOKS */
709
710 error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
711 if (error) {
712 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
713 ip6stat.ip6s_cantforward++;
714 } else {
715 ip6stat.ip6s_forward++;
716 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
717 if (type)
718 ip6stat.ip6s_redirectsent++;
719 else {
720 #ifdef GATEWAY
721 if (m->m_flags & M_CANFASTFWD)
722 ip6flow_create(&ip6_forward_rt, m);
723 #endif
724 if (mcopy)
725 goto freecopy;
726 }
727 }
728
729 #ifdef PFIL_HOOKS
730 senderr:
731 #endif
732 if (mcopy == NULL)
733 return;
734 switch (error) {
735 case 0:
736 if (type == ND_REDIRECT) {
737 icmp6_redirect_output(mcopy, rt);
738 return;
739 }
740 goto freecopy;
741
742 case EMSGSIZE:
743 /* xxx MTU is constant in PPP? */
744 goto freecopy;
745
746 case ENOBUFS:
747 /* Tell source to slow down like source quench in IP? */
748 goto freecopy;
749
750 case ENETUNREACH: /* shouldn't happen, checked above */
751 case EHOSTUNREACH:
752 case ENETDOWN:
753 case EHOSTDOWN:
754 default:
755 type = ICMP6_DST_UNREACH;
756 code = ICMP6_DST_UNREACH_ADDR;
757 break;
758 }
759 icmp6_error(mcopy, type, code, 0);
760 return;
761
762 freecopy:
763 m_freem(mcopy);
764 return;
765 }
766