1139826Simp/*- 253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 353541Sshin * All rights reserved. 453541Sshin * 553541Sshin * Redistribution and use in source and binary forms, with or without 653541Sshin * modification, are permitted provided that the following conditions 753541Sshin * are met: 853541Sshin * 1. Redistributions of source code must retain the above copyright 953541Sshin * notice, this list of conditions and the following disclaimer. 1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1153541Sshin * notice, this list of conditions and the following disclaimer in the 1253541Sshin * documentation and/or other materials provided with the distribution. 1353541Sshin * 3. Neither the name of the project nor the names of its contributors 1453541Sshin * may be used to endorse or promote products derived from this software 1553541Sshin * without specific prior written permission. 1653541Sshin * 1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2053541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2753541Sshin * SUCH DAMAGE. 28174510Sobrien * 29174510Sobrien * $KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $ 3053541Sshin */ 3153541Sshin 32174510Sobrien#include <sys/cdefs.h> 33174510Sobrien__FBSDID("$FreeBSD$"); 34174510Sobrien 3562587Sitojun#include "opt_inet.h" 3662587Sitojun#include "opt_inet6.h" 37225044Sbz#include "opt_ipfw.h" 3855009Sshin#include "opt_ipsec.h" 39148921Ssuz#include "opt_ipstealth.h" 4053541Sshin 4153541Sshin#include <sys/param.h> 4253541Sshin#include <sys/systm.h> 4378064Sume#include <sys/malloc.h> 4453541Sshin#include <sys/mbuf.h> 4553541Sshin#include <sys/domain.h> 4653541Sshin#include <sys/protosw.h> 4753541Sshin#include <sys/socket.h> 4853541Sshin#include <sys/errno.h> 4953541Sshin#include <sys/time.h> 5078064Sume#include <sys/kernel.h> 5153541Sshin#include <sys/syslog.h> 5253541Sshin 5353541Sshin#include <net/if.h> 54225044Sbz#include <net/netisr.h> 5553541Sshin#include <net/route.h> 5684994Sdarrenr#include <net/pfil.h> 5753541Sshin 5853541Sshin#include <netinet/in.h> 5953541Sshin#include <netinet/in_var.h> 6078064Sume#include <netinet/in_systm.h> 6178064Sume#include <netinet/ip.h> 6262587Sitojun#include <netinet/ip_var.h> 6378064Sume#include <netinet6/in6_var.h> 6462587Sitojun#include <netinet/ip6.h> 6553541Sshin#include <netinet6/ip6_var.h> 66148385Sume#include <netinet6/scope6_var.h> 6762587Sitojun#include <netinet/icmp6.h> 6853541Sshin#include <netinet6/nd6.h> 6953541Sshin 7078064Sume#include <netinet/in_pcb.h> 7178064Sume 72171167Sgnn#ifdef IPSEC 73105199Ssam#include <netipsec/ipsec.h> 74105199Ssam#include <netipsec/ipsec6.h> 75105199Ssam#include <netipsec/key.h> 76171167Sgnn#endif /* IPSEC */ 77105199Ssam 7884994Sdarrenr#include <netinet6/ip6protosw.h> 7984994Sdarrenr 8053541Sshin/* 8153541Sshin * Forward a packet. If some error occurs return the sender 8253541Sshin * an icmp packet. Note we can't always generate a meaningful 8353541Sshin * icmp message because icmp doesn't have a large enough repertoire 8453541Sshin * of codes and types. 8553541Sshin * 8653541Sshin * If not forwarding, just drop the packet. This could be confusing 8753541Sshin * if ipforwarding was zero but some routing protocol was advancing 8853541Sshin * us as a gateway to somewhere. However, we must let the routing 8953541Sshin * protocol deal with that. 9053541Sshin * 9153541Sshin */ 9253541Sshinvoid 93171259Sdelphijip6_forward(struct mbuf *m, int srcrt) 9453541Sshin{ 9553541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 96122062Sume struct sockaddr_in6 *dst = NULL; 97122062Sume struct rtentry *rt = NULL; 98187989Sbz struct route_in6 rin6; 9953541Sshin int error, type = 0, code = 0; 10062587Sitojun struct mbuf *mcopy = NULL; 10162587Sitojun struct ifnet *origifp; /* maybe unnecessary */ 102148385Sume u_int32_t inzone, outzone; 103225044Sbz struct in6_addr src_in6, dst_in6, odst; 104171167Sgnn#ifdef IPSEC 10553541Sshin struct secpolicy *sp = NULL; 106122062Sume int ipsecrt = 0; 10753541Sshin#endif 108225044Sbz#ifdef SCTP 109225044Sbz int sw_csum; 110225044Sbz#endif 111225044Sbz struct m_tag *fwd_tag; 112165118Sbz char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 11353541Sshin 114171167Sgnn#ifdef IPSEC 11553541Sshin /* 11653541Sshin * Check AH/ESP integrity. 11753541Sshin */ 11853541Sshin /* 11953541Sshin * Don't increment ip6s_cantforward because this is the check 12053541Sshin * before forwarding packet actually. 12153541Sshin */ 12253541Sshin if (ipsec6_in_reject(m, NULL)) { 123253571Sae IPSEC6STAT_INC(ips_in_polvio); 12453541Sshin m_freem(m); 12553541Sshin return; 12653541Sshin } 127171167Sgnn#endif /* IPSEC */ 12853541Sshin 12978064Sume /* 13078064Sume * Do not forward packets to multicast destination (should be handled 13178064Sume * by ip6_mforward(). 13278064Sume * Do not forward packets with unspecified source. It was discussed 133120913Sume * in July 2000, on the ipngwg mailing list. 13478064Sume */ 13562587Sitojun if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 || 13678064Sume IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 13778064Sume IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 138249294Sae IP6STAT_INC(ip6s_cantforward); 13953541Sshin /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 140253970Shrs if (V_ip6_log_time + V_ip6_log_interval < time_uptime) { 141253970Shrs V_ip6_log_time = time_uptime; 14253541Sshin log(LOG_DEBUG, 14353541Sshin "cannot forward " 14453541Sshin "from %s to %s nxt %d received on %s\n", 145165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src), 146165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), 14753541Sshin ip6->ip6_nxt, 14853541Sshin if_name(m->m_pkthdr.rcvif)); 14953541Sshin } 15053541Sshin m_freem(m); 15153541Sshin return; 15253541Sshin } 15353541Sshin 154148921Ssuz#ifdef IPSTEALTH 155181803Sbz if (!V_ip6stealth) { 156148921Ssuz#endif 15753541Sshin if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 15853541Sshin /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 15953541Sshin icmp6_error(m, ICMP6_TIME_EXCEEDED, 16053541Sshin ICMP6_TIME_EXCEED_TRANSIT, 0); 16153541Sshin return; 16253541Sshin } 16353541Sshin ip6->ip6_hlim -= IPV6_HLIMDEC; 16453541Sshin 165148921Ssuz#ifdef IPSTEALTH 166148921Ssuz } 167148921Ssuz#endif 168148921Ssuz 16962587Sitojun /* 17062587Sitojun * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU - 17162587Sitojun * size of IPv6 + ICMPv6 headers) bytes of the packet in case 17262587Sitojun * we need to generate an ICMP6 message to the src. 17362587Sitojun * Thanks to M_EXT, in most cases copy will not occur. 17462587Sitojun * 17562587Sitojun * It is important to save it before IPsec processing as IPsec 17662587Sitojun * processing may modify the mbuf. 17762587Sitojun */ 17862587Sitojun mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN)); 17962587Sitojun 180171167Sgnn#ifdef IPSEC 18153541Sshin /* get a security policy for this packet */ 182171133Sgnn sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 183120913Sume IP_FORWARDING, &error); 18453541Sshin if (sp == NULL) { 185253571Sae IPSEC6STAT_INC(ips_out_inval); 186249294Sae IP6STAT_INC(ip6s_cantforward); 18762587Sitojun if (mcopy) { 18862587Sitojun#if 0 18962587Sitojun /* XXX: what icmp ? */ 19062587Sitojun#else 19162587Sitojun m_freem(mcopy); 19262587Sitojun#endif 19362587Sitojun } 19453541Sshin m_freem(m); 19553541Sshin return; 19653541Sshin } 19753541Sshin 19853541Sshin error = 0; 19953541Sshin 20053541Sshin /* check policy */ 20153541Sshin switch (sp->policy) { 20253541Sshin case IPSEC_POLICY_DISCARD: 20353541Sshin /* 20453541Sshin * This packet is just discarded. 20553541Sshin */ 206253571Sae IPSEC6STAT_INC(ips_out_polvio); 207249294Sae IP6STAT_INC(ip6s_cantforward); 208171133Sgnn KEY_FREESP(&sp); 20962587Sitojun if (mcopy) { 21062587Sitojun#if 0 21162587Sitojun /* XXX: what icmp ? */ 21262587Sitojun#else 21362587Sitojun m_freem(mcopy); 21462587Sitojun#endif 21562587Sitojun } 21653541Sshin m_freem(m); 21753541Sshin return; 21853541Sshin 21953541Sshin case IPSEC_POLICY_BYPASS: 22053541Sshin case IPSEC_POLICY_NONE: 22153541Sshin /* no need to do IPsec. */ 222171133Sgnn KEY_FREESP(&sp); 22353541Sshin goto skip_ipsec; 22462587Sitojun 22553541Sshin case IPSEC_POLICY_IPSEC: 22653541Sshin if (sp->req == NULL) { 22753541Sshin /* XXX should be panic ? */ 22853541Sshin printf("ip6_forward: No IPsec request specified.\n"); 229249294Sae IP6STAT_INC(ip6s_cantforward); 230171133Sgnn KEY_FREESP(&sp); 23162587Sitojun if (mcopy) { 23262587Sitojun#if 0 23362587Sitojun /* XXX: what icmp ? */ 23462587Sitojun#else 23562587Sitojun m_freem(mcopy); 23662587Sitojun#endif 23762587Sitojun } 23853541Sshin m_freem(m); 23953541Sshin return; 24053541Sshin } 24153541Sshin /* do IPsec */ 24253541Sshin break; 24353541Sshin 24453541Sshin case IPSEC_POLICY_ENTRUST: 24553541Sshin default: 24653541Sshin /* should be panic ?? */ 24753541Sshin printf("ip6_forward: Invalid policy found. %d\n", sp->policy); 248171133Sgnn KEY_FREESP(&sp); 24953541Sshin goto skip_ipsec; 25053541Sshin } 25153541Sshin 25253541Sshin { 253122062Sume struct ipsecrequest *isr = NULL; 25453541Sshin struct ipsec_output_state state; 25553541Sshin 25653541Sshin /* 257122062Sume * when the kernel forwards a packet, it is not proper to apply 258122062Sume * IPsec transport mode to the packet is not proper. this check 259122062Sume * avoid from this. 260122062Sume * at present, if there is even a transport mode SA request in the 261122062Sume * security policy, the kernel does not apply IPsec to the packet. 262122062Sume * this check is not enough because the following case is valid. 263122062Sume * ipsec esp/tunnel/xxx-xxx/require esp/transport//require; 264122062Sume */ 265122062Sume for (isr = sp->req; isr; isr = isr->next) { 266126006Sume if (isr->saidx.mode == IPSEC_MODE_ANY) 267126006Sume goto doipsectunnel; 268126006Sume if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 269126006Sume goto doipsectunnel; 270122062Sume } 271122062Sume 272122062Sume /* 273126006Sume * if there's no need for tunnel mode IPsec, skip. 274126006Sume */ 275126006Sume if (!isr) 276126006Sume goto skip_ipsec; 277126006Sume 278126006Sume doipsectunnel: 279126006Sume /* 28053541Sshin * All the extension headers will become inaccessible 28153541Sshin * (since they can be encrypted). 28253541Sshin * Don't panic, we need no more updates to extension headers 28353541Sshin * on inner IPv6 packet (since they are now encapsulated). 28453541Sshin * 28553541Sshin * IPv6 [ESP|AH] IPv6 [extension headers] payload 28653541Sshin */ 28753541Sshin bzero(&state, sizeof(state)); 28853541Sshin state.m = m; 28953541Sshin state.ro = NULL; /* update at ipsec6_output_tunnel() */ 29053541Sshin state.dst = NULL; /* update at ipsec6_output_tunnel() */ 29153541Sshin 29253541Sshin error = ipsec6_output_tunnel(&state, sp, 0); 29353541Sshin 29453541Sshin m = state.m; 295171133Sgnn KEY_FREESP(&sp); 29653541Sshin 29753541Sshin if (error) { 29853541Sshin /* mbuf is already reclaimed in ipsec6_output_tunnel. */ 29953541Sshin switch (error) { 30053541Sshin case EHOSTUNREACH: 30153541Sshin case ENETUNREACH: 30253541Sshin case EMSGSIZE: 30353541Sshin case ENOBUFS: 30453541Sshin case ENOMEM: 30553541Sshin break; 30653541Sshin default: 30753541Sshin printf("ip6_output (ipsec): error code %d\n", error); 308120913Sume /* FALLTHROUGH */ 30953541Sshin case ENOENT: 31053541Sshin /* don't show these error codes to the user */ 31153541Sshin break; 31253541Sshin } 313249294Sae IP6STAT_INC(ip6s_cantforward); 31462587Sitojun if (mcopy) { 31562587Sitojun#if 0 31662587Sitojun /* XXX: what icmp ? */ 31762587Sitojun#else 31862587Sitojun m_freem(mcopy); 31962587Sitojun#endif 32062587Sitojun } 32153541Sshin m_freem(m); 32253541Sshin return; 323171133Sgnn } else { 324171260Sdelphij /* 325171260Sdelphij * In the FAST IPSec case we have already 326171133Sgnn * re-injected the packet and it has been freed 327171260Sdelphij * by the ipsec_done() function. So, just clean 328171133Sgnn * up after ourselves. 329171133Sgnn */ 330171133Sgnn m = NULL; 331171133Sgnn goto freecopy; 33253541Sshin } 333122062Sume 334171133Sgnn if ((m != NULL) && (ip6 != mtod(m, struct ip6_hdr *)) ){ 335126006Sume /* 336126006Sume * now tunnel mode headers are added. we are originating 337126006Sume * packet instead of forwarding the packet. 338126006Sume */ 339126006Sume ip6_output(m, NULL, NULL, IPV6_FORWARDING/*XXX*/, NULL, NULL, 340126006Sume NULL); 341126006Sume goto freecopy; 342126006Sume } 343126006Sume 344122062Sume /* adjust pointer */ 345122062Sume dst = (struct sockaddr_in6 *)state.dst; 346122062Sume rt = state.ro ? state.ro->ro_rt : NULL; 347122062Sume if (dst != NULL && rt != NULL) 348122062Sume ipsecrt = 1; 34953541Sshin } 350122062Sume if (ipsecrt) 351122062Sume goto skip_routing; 352187949Sbzskip_ipsec: 353122062Sume#endif 354225044Sbzagain: 355187989Sbz bzero(&rin6, sizeof(struct route_in6)); 356187989Sbz dst = (struct sockaddr_in6 *)&rin6.ro_dst; 357187989Sbz dst->sin6_len = sizeof(struct sockaddr_in6); 358187989Sbz dst->sin6_family = AF_INET6; 359187989Sbz dst->sin6_addr = ip6->ip6_dst; 360225044Sbzagain2: 361231852Sbz rin6.ro_rt = in6_rtalloc1((struct sockaddr *)dst, 0, 0, M_GETFIB(m)); 362187989Sbz if (rin6.ro_rt != NULL) 363187989Sbz RT_UNLOCK(rin6.ro_rt); 364187989Sbz else { 365249294Sae IP6STAT_INC(ip6s_noroute); 366187989Sbz in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute); 367187989Sbz if (mcopy) { 368187989Sbz icmp6_error(mcopy, ICMP6_DST_UNREACH, 369187989Sbz ICMP6_DST_UNREACH_NOROUTE, 0); 37053541Sshin } 371187989Sbz goto bad; 37253541Sshin } 373187989Sbz rt = rin6.ro_rt; 374171167Sgnn#ifdef IPSEC 375187989Sbzskip_routing: 376122062Sume#endif 37762587Sitojun 37862587Sitojun /* 379148385Sume * Source scope check: if a packet can't be delivered to its 380148385Sume * destination for the reason that the destination is beyond the scope 381148385Sume * of the source address, discard the packet and return an icmp6 382148385Sume * destination unreachable error with Code 2 (beyond scope of source 383148385Sume * address). We use a local copy of ip6_src, since in6_setscope() 384148385Sume * will possibly modify its first argument. 385148385Sume * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1] 38662587Sitojun */ 387148385Sume src_in6 = ip6->ip6_src; 388148385Sume if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) { 389122062Sume /* XXX: this should not happen */ 390249294Sae IP6STAT_INC(ip6s_cantforward); 391249294Sae IP6STAT_INC(ip6s_badscope); 392187989Sbz goto bad; 393122062Sume } 394148385Sume if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) { 395249294Sae IP6STAT_INC(ip6s_cantforward); 396249294Sae IP6STAT_INC(ip6s_badscope); 397187989Sbz goto bad; 398148385Sume } 399148385Sume if (inzone != outzone 400171167Sgnn#ifdef IPSEC 401122062Sume && !ipsecrt 402122062Sume#endif 403122062Sume ) { 404249294Sae IP6STAT_INC(ip6s_cantforward); 405249294Sae IP6STAT_INC(ip6s_badscope); 40662587Sitojun in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard); 40762587Sitojun 408253970Shrs if (V_ip6_log_time + V_ip6_log_interval < time_uptime) { 409253970Shrs V_ip6_log_time = time_uptime; 41062587Sitojun log(LOG_DEBUG, 41162587Sitojun "cannot forward " 41262587Sitojun "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 413165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src), 414165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), 41562587Sitojun ip6->ip6_nxt, 41662587Sitojun if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp)); 41762587Sitojun } 41862587Sitojun if (mcopy) 41962587Sitojun icmp6_error(mcopy, ICMP6_DST_UNREACH, 42062587Sitojun ICMP6_DST_UNREACH_BEYONDSCOPE, 0); 421187989Sbz goto bad; 42262587Sitojun } 42362587Sitojun 424148385Sume /* 425148385Sume * Destination scope check: if a packet is going to break the scope 426148385Sume * zone of packet's destination address, discard it. This case should 427148385Sume * usually be prevented by appropriately-configured routing table, but 428148385Sume * we need an explicit check because we may mistakenly forward the 429148385Sume * packet to a different zone by (e.g.) a default route. 430148385Sume */ 431148385Sume dst_in6 = ip6->ip6_dst; 432148385Sume if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 || 433148385Sume in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 || 434148385Sume inzone != outzone) { 435249294Sae IP6STAT_INC(ip6s_cantforward); 436249294Sae IP6STAT_INC(ip6s_badscope); 437187989Sbz goto bad; 438148385Sume } 439148385Sume 440121283Sume if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) { 44153541Sshin in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig); 44262587Sitojun if (mcopy) { 44362587Sitojun u_long mtu; 444171167Sgnn#ifdef IPSEC 44562587Sitojun struct secpolicy *sp; 44662587Sitojun int ipsecerror; 44762587Sitojun size_t ipsechdrsiz; 448171167Sgnn#endif /* IPSEC */ 44962587Sitojun 450121283Sume mtu = IN6_LINKMTU(rt->rt_ifp); 451171167Sgnn#ifdef IPSEC 45262587Sitojun /* 45362587Sitojun * When we do IPsec tunnel ingress, we need to play 454122062Sume * with the link value (decrement IPsec header size 45562587Sitojun * from mtu value). The code is much simpler than v4 45662587Sitojun * case, as we have the outgoing interface for 45762587Sitojun * encapsulated packet as "rt->rt_ifp". 45862587Sitojun */ 459171133Sgnn sp = ipsec_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND, 46062587Sitojun IP_FORWARDING, &ipsecerror); 46162587Sitojun if (sp) { 462188306Sbz ipsechdrsiz = ipsec_hdrsiz(mcopy, 46362587Sitojun IPSEC_DIR_OUTBOUND, NULL); 46462587Sitojun if (ipsechdrsiz < mtu) 46562587Sitojun mtu -= ipsechdrsiz; 46662587Sitojun } 46762587Sitojun 46862587Sitojun /* 46962587Sitojun * if mtu becomes less than minimum MTU, 47062587Sitojun * tell minimum MTU (and I'll need to fragment it). 47162587Sitojun */ 47262587Sitojun if (mtu < IPV6_MMTU) 47362587Sitojun mtu = IPV6_MMTU; 474171167Sgnn#endif /* IPSEC */ 47562587Sitojun icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu); 47662587Sitojun } 477187989Sbz goto bad; 478120913Sume } 47953541Sshin 48053541Sshin if (rt->rt_flags & RTF_GATEWAY) 48153541Sshin dst = (struct sockaddr_in6 *)rt->rt_gateway; 48253541Sshin 48353541Sshin /* 48453541Sshin * If we are to forward the packet using the same interface 48553541Sshin * as one we got the packet from, perhaps we should send a redirect 48653541Sshin * to sender to shortcut a hop. 48753541Sshin * Only send redirect if source is sending directly to us, 48853541Sshin * and if packet was not source routed (or has any options). 48953541Sshin * Also, don't send redirect if forwarding using a route 49053541Sshin * modified by a redirect. 49153541Sshin */ 492181803Sbz if (V_ip6_sendredirects && rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && 493171167Sgnn#ifdef IPSEC 494122062Sume !ipsecrt && 495171167Sgnn#endif /* IPSEC */ 49678064Sume (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) { 49778064Sume if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) { 49878064Sume /* 49978064Sume * If the incoming interface is equal to the outgoing 50078064Sume * one, and the link attached to the interface is 50178064Sume * point-to-point, then it will be highly probable 50278064Sume * that a routing loop occurs. Thus, we immediately 50378064Sume * drop the packet and send an ICMPv6 error message. 50478064Sume * 50578064Sume * type/code is based on suggestion by Rich Draves. 50678064Sume * not sure if it is the best pick. 50778064Sume */ 50878064Sume icmp6_error(mcopy, ICMP6_DST_UNREACH, 50978064Sume ICMP6_DST_UNREACH_ADDR, 0); 510187989Sbz goto bad; 51178064Sume } 51253541Sshin type = ND_REDIRECT; 51378064Sume } 51453541Sshin 51553541Sshin /* 51662587Sitojun * Fake scoped addresses. Note that even link-local source or 51762587Sitojun * destinaion can appear, if the originating node just sends the 51862587Sitojun * packet to us (without address resolution for the destination). 51962587Sitojun * Since both icmp6_error and icmp6_redirect_output fill the embedded 52078064Sume * link identifiers, we can do this stuff after making a copy for 52178064Sume * returning an error. 52262587Sitojun */ 52362587Sitojun if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 52462587Sitojun /* 52562587Sitojun * See corresponding comments in ip6_output. 52662587Sitojun * XXX: but is it possible that ip6_forward() sends a packet 52762587Sitojun * to a loopback interface? I don't think so, and thus 52862587Sitojun * I bark here. (jinmei@kame.net) 52962587Sitojun * XXX: it is common to route invalid packets to loopback. 53062587Sitojun * also, the codepath will be visited on use of ::1 in 53162587Sitojun * rthdr. (itojun) 53262587Sitojun */ 53362587Sitojun#if 1 53462587Sitojun if (0) 53562587Sitojun#else 53662587Sitojun if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0) 53762587Sitojun#endif 53862587Sitojun { 53962587Sitojun printf("ip6_forward: outgoing interface is loopback. " 540120913Sume "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 541165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src), 542165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), 543120913Sume ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif), 544120913Sume if_name(rt->rt_ifp)); 54562587Sitojun } 54662587Sitojun 54778064Sume /* we can just use rcvif in forwarding. */ 54878064Sume origifp = m->m_pkthdr.rcvif; 54962587Sitojun } 55062587Sitojun else 55162587Sitojun origifp = rt->rt_ifp; 55278064Sume /* 55378064Sume * clear embedded scope identifiers if necessary. 55478064Sume * in6_clearscope will touch the addresses only when necessary. 55578064Sume */ 55678064Sume in6_clearscope(&ip6->ip6_src); 55778064Sume in6_clearscope(&ip6->ip6_dst); 55862587Sitojun 559134383Sandre /* Jump over all PFIL processing if hooks are not active. */ 560197952Sjulian if (!PFIL_HOOKED(&V_inet6_pfil_hook)) 561134383Sandre goto pass; 562134383Sandre 563225044Sbz odst = ip6->ip6_dst; 564134383Sandre /* Run through list of hooks for output packets. */ 565197952Sjulian error = pfil_run_hooks(&V_inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT, NULL); 566264224Sae if (error != 0 || m == NULL) 567264224Sae goto freecopy; /* consumed by filter */ 568120386Ssam ip6 = mtod(m, struct ip6_hdr *); 56984994Sdarrenr 570225044Sbz /* See if destination IP address was changed by packet filter. */ 571225044Sbz if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) { 572225044Sbz m->m_flags |= M_SKIP_FIREWALL; 573225044Sbz /* If destination is now ourself drop to ip6_input(). */ 574225044Sbz if (in6_localip(&ip6->ip6_dst)) { 575225044Sbz m->m_flags |= M_FASTFWD_OURS; 576225044Sbz if (m->m_pkthdr.rcvif == NULL) 577225044Sbz m->m_pkthdr.rcvif = V_loif; 578236170Sbz if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 579225044Sbz m->m_pkthdr.csum_flags |= 580236170Sbz CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR; 581225044Sbz m->m_pkthdr.csum_data = 0xffff; 582225044Sbz } 583225044Sbz#ifdef SCTP 584236332Stuexen if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) 585225044Sbz m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 586225044Sbz#endif 587225044Sbz error = netisr_queue(NETISR_IPV6, m); 588225044Sbz goto out; 589225044Sbz } else 590225044Sbz goto again; /* Redo the routing table lookup. */ 591225044Sbz } 592225044Sbz 593225044Sbz /* See if local, if yes, send it to netisr. */ 594225044Sbz if (m->m_flags & M_FASTFWD_OURS) { 595225044Sbz if (m->m_pkthdr.rcvif == NULL) 596225044Sbz m->m_pkthdr.rcvif = V_loif; 597236170Sbz if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 598225044Sbz m->m_pkthdr.csum_flags |= 599236170Sbz CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR; 600225044Sbz m->m_pkthdr.csum_data = 0xffff; 601225044Sbz } 602225044Sbz#ifdef SCTP 603236332Stuexen if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) 604236332Stuexen m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 605236332Stuexen#endif 606225044Sbz error = netisr_queue(NETISR_IPV6, m); 607225044Sbz goto out; 608225044Sbz } 609225044Sbz /* Or forward to some other address? */ 610242463Sae if ((m->m_flags & M_IP6_NEXTHOP) && 611242463Sae (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 612225044Sbz dst = (struct sockaddr_in6 *)&rin6.ro_dst; 613225044Sbz bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in6)); 614225044Sbz m->m_flags |= M_SKIP_FIREWALL; 615242463Sae m->m_flags &= ~M_IP6_NEXTHOP; 616225044Sbz m_tag_delete(m, fwd_tag); 617225044Sbz goto again2; 618225044Sbz } 619225044Sbz 620134383Sandrepass: 62162587Sitojun error = nd6_output(rt->rt_ifp, origifp, m, dst, rt); 62253541Sshin if (error) { 62353541Sshin in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard); 624249294Sae IP6STAT_INC(ip6s_cantforward); 62553541Sshin } else { 626249294Sae IP6STAT_INC(ip6s_forward); 62753541Sshin in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward); 62853541Sshin if (type) 629249294Sae IP6STAT_INC(ip6s_redirectsent); 63053541Sshin else { 63153541Sshin if (mcopy) 63253541Sshin goto freecopy; 63353541Sshin } 63453541Sshin } 635120913Sume 63653541Sshin if (mcopy == NULL) 637187989Sbz goto out; 63853541Sshin switch (error) { 63953541Sshin case 0: 64053541Sshin if (type == ND_REDIRECT) { 64153541Sshin icmp6_redirect_output(mcopy, rt); 642187989Sbz goto out; 64353541Sshin } 64453541Sshin goto freecopy; 64553541Sshin 64653541Sshin case EMSGSIZE: 64753541Sshin /* xxx MTU is constant in PPP? */ 64853541Sshin goto freecopy; 64953541Sshin 65053541Sshin case ENOBUFS: 65153541Sshin /* Tell source to slow down like source quench in IP? */ 65253541Sshin goto freecopy; 65353541Sshin 65453541Sshin case ENETUNREACH: /* shouldn't happen, checked above */ 65553541Sshin case EHOSTUNREACH: 65653541Sshin case ENETDOWN: 65753541Sshin case EHOSTDOWN: 65853541Sshin default: 65953541Sshin type = ICMP6_DST_UNREACH; 66053541Sshin code = ICMP6_DST_UNREACH_ADDR; 66153541Sshin break; 66253541Sshin } 66353541Sshin icmp6_error(mcopy, type, code, 0); 664187989Sbz goto out; 66553541Sshin 66653541Sshin freecopy: 66753541Sshin m_freem(mcopy); 668187989Sbz goto out; 669187989Sbzbad: 670187989Sbz m_freem(m); 671187989Sbzout: 672187989Sbz if (rt != NULL 673187989Sbz#ifdef IPSEC 674187989Sbz && !ipsecrt 675187989Sbz#endif 676187989Sbz ) 677187989Sbz RTFREE(rt); 67853541Sshin} 679