The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/ip6_forward.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 8a0ed41c15b96870752ded10b5691fb6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.