ip6_forward.c revision 236332
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: head/sys/netinet6/ip6_forward.c 236332 2012-05-30 20:56:07Z tuexen $"); 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#ifdef IPFIREWALL_FORWARD 112225044Sbz struct m_tag *fwd_tag; 113225044Sbz#endif 114165118Sbz char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 11553541Sshin 116171167Sgnn#ifdef IPSEC 11753541Sshin /* 11853541Sshin * Check AH/ESP integrity. 11953541Sshin */ 12053541Sshin /* 12153541Sshin * Don't increment ip6s_cantforward because this is the check 12253541Sshin * before forwarding packet actually. 12353541Sshin */ 12453541Sshin if (ipsec6_in_reject(m, NULL)) { 125181803Sbz V_ipsec6stat.in_polvio++; 12653541Sshin m_freem(m); 12753541Sshin return; 12853541Sshin } 129171167Sgnn#endif /* IPSEC */ 13053541Sshin 13178064Sume /* 13278064Sume * Do not forward packets to multicast destination (should be handled 13378064Sume * by ip6_mforward(). 13478064Sume * Do not forward packets with unspecified source. It was discussed 135120913Sume * in July 2000, on the ipngwg mailing list. 13678064Sume */ 13762587Sitojun if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 || 13878064Sume IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 13978064Sume IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 140181803Sbz V_ip6stat.ip6s_cantforward++; 14153541Sshin /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 142181803Sbz if (V_ip6_log_time + V_ip6_log_interval < time_second) { 143181803Sbz V_ip6_log_time = time_second; 14453541Sshin log(LOG_DEBUG, 14553541Sshin "cannot forward " 14653541Sshin "from %s to %s nxt %d received on %s\n", 147165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src), 148165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), 14953541Sshin ip6->ip6_nxt, 15053541Sshin if_name(m->m_pkthdr.rcvif)); 15153541Sshin } 15253541Sshin m_freem(m); 15353541Sshin return; 15453541Sshin } 15553541Sshin 156148921Ssuz#ifdef IPSTEALTH 157181803Sbz if (!V_ip6stealth) { 158148921Ssuz#endif 15953541Sshin if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 16053541Sshin /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 16153541Sshin icmp6_error(m, ICMP6_TIME_EXCEEDED, 16253541Sshin ICMP6_TIME_EXCEED_TRANSIT, 0); 16353541Sshin return; 16453541Sshin } 16553541Sshin ip6->ip6_hlim -= IPV6_HLIMDEC; 16653541Sshin 167148921Ssuz#ifdef IPSTEALTH 168148921Ssuz } 169148921Ssuz#endif 170148921Ssuz 17162587Sitojun /* 17262587Sitojun * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU - 17362587Sitojun * size of IPv6 + ICMPv6 headers) bytes of the packet in case 17462587Sitojun * we need to generate an ICMP6 message to the src. 17562587Sitojun * Thanks to M_EXT, in most cases copy will not occur. 17662587Sitojun * 17762587Sitojun * It is important to save it before IPsec processing as IPsec 17862587Sitojun * processing may modify the mbuf. 17962587Sitojun */ 18062587Sitojun mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN)); 18162587Sitojun 182171167Sgnn#ifdef IPSEC 18353541Sshin /* get a security policy for this packet */ 184171133Sgnn sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 185120913Sume IP_FORWARDING, &error); 18653541Sshin if (sp == NULL) { 187181803Sbz V_ipsec6stat.out_inval++; 188181803Sbz V_ip6stat.ip6s_cantforward++; 18962587Sitojun if (mcopy) { 19062587Sitojun#if 0 19162587Sitojun /* XXX: what icmp ? */ 19262587Sitojun#else 19362587Sitojun m_freem(mcopy); 19462587Sitojun#endif 19562587Sitojun } 19653541Sshin m_freem(m); 19753541Sshin return; 19853541Sshin } 19953541Sshin 20053541Sshin error = 0; 20153541Sshin 20253541Sshin /* check policy */ 20353541Sshin switch (sp->policy) { 20453541Sshin case IPSEC_POLICY_DISCARD: 20553541Sshin /* 20653541Sshin * This packet is just discarded. 20753541Sshin */ 208181803Sbz V_ipsec6stat.out_polvio++; 209181803Sbz V_ip6stat.ip6s_cantforward++; 210171133Sgnn KEY_FREESP(&sp); 21162587Sitojun if (mcopy) { 21262587Sitojun#if 0 21362587Sitojun /* XXX: what icmp ? */ 21462587Sitojun#else 21562587Sitojun m_freem(mcopy); 21662587Sitojun#endif 21762587Sitojun } 21853541Sshin m_freem(m); 21953541Sshin return; 22053541Sshin 22153541Sshin case IPSEC_POLICY_BYPASS: 22253541Sshin case IPSEC_POLICY_NONE: 22353541Sshin /* no need to do IPsec. */ 224171133Sgnn KEY_FREESP(&sp); 22553541Sshin goto skip_ipsec; 22662587Sitojun 22753541Sshin case IPSEC_POLICY_IPSEC: 22853541Sshin if (sp->req == NULL) { 22953541Sshin /* XXX should be panic ? */ 23053541Sshin printf("ip6_forward: No IPsec request specified.\n"); 231181803Sbz V_ip6stat.ip6s_cantforward++; 232171133Sgnn KEY_FREESP(&sp); 23362587Sitojun if (mcopy) { 23462587Sitojun#if 0 23562587Sitojun /* XXX: what icmp ? */ 23662587Sitojun#else 23762587Sitojun m_freem(mcopy); 23862587Sitojun#endif 23962587Sitojun } 24053541Sshin m_freem(m); 24153541Sshin return; 24253541Sshin } 24353541Sshin /* do IPsec */ 24453541Sshin break; 24553541Sshin 24653541Sshin case IPSEC_POLICY_ENTRUST: 24753541Sshin default: 24853541Sshin /* should be panic ?? */ 24953541Sshin printf("ip6_forward: Invalid policy found. %d\n", sp->policy); 250171133Sgnn KEY_FREESP(&sp); 25153541Sshin goto skip_ipsec; 25253541Sshin } 25353541Sshin 25453541Sshin { 255122062Sume struct ipsecrequest *isr = NULL; 25653541Sshin struct ipsec_output_state state; 25753541Sshin 25853541Sshin /* 259122062Sume * when the kernel forwards a packet, it is not proper to apply 260122062Sume * IPsec transport mode to the packet is not proper. this check 261122062Sume * avoid from this. 262122062Sume * at present, if there is even a transport mode SA request in the 263122062Sume * security policy, the kernel does not apply IPsec to the packet. 264122062Sume * this check is not enough because the following case is valid. 265122062Sume * ipsec esp/tunnel/xxx-xxx/require esp/transport//require; 266122062Sume */ 267122062Sume for (isr = sp->req; isr; isr = isr->next) { 268126006Sume if (isr->saidx.mode == IPSEC_MODE_ANY) 269126006Sume goto doipsectunnel; 270126006Sume if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 271126006Sume goto doipsectunnel; 272122062Sume } 273122062Sume 274122062Sume /* 275126006Sume * if there's no need for tunnel mode IPsec, skip. 276126006Sume */ 277126006Sume if (!isr) 278126006Sume goto skip_ipsec; 279126006Sume 280126006Sume doipsectunnel: 281126006Sume /* 28253541Sshin * All the extension headers will become inaccessible 28353541Sshin * (since they can be encrypted). 28453541Sshin * Don't panic, we need no more updates to extension headers 28553541Sshin * on inner IPv6 packet (since they are now encapsulated). 28653541Sshin * 28753541Sshin * IPv6 [ESP|AH] IPv6 [extension headers] payload 28853541Sshin */ 28953541Sshin bzero(&state, sizeof(state)); 29053541Sshin state.m = m; 29153541Sshin state.ro = NULL; /* update at ipsec6_output_tunnel() */ 29253541Sshin state.dst = NULL; /* update at ipsec6_output_tunnel() */ 29353541Sshin 29453541Sshin error = ipsec6_output_tunnel(&state, sp, 0); 29553541Sshin 29653541Sshin m = state.m; 297171133Sgnn KEY_FREESP(&sp); 29853541Sshin 29953541Sshin if (error) { 30053541Sshin /* mbuf is already reclaimed in ipsec6_output_tunnel. */ 30153541Sshin switch (error) { 30253541Sshin case EHOSTUNREACH: 30353541Sshin case ENETUNREACH: 30453541Sshin case EMSGSIZE: 30553541Sshin case ENOBUFS: 30653541Sshin case ENOMEM: 30753541Sshin break; 30853541Sshin default: 30953541Sshin printf("ip6_output (ipsec): error code %d\n", error); 310120913Sume /* FALLTHROUGH */ 31153541Sshin case ENOENT: 31253541Sshin /* don't show these error codes to the user */ 31353541Sshin break; 31453541Sshin } 315181803Sbz V_ip6stat.ip6s_cantforward++; 31662587Sitojun if (mcopy) { 31762587Sitojun#if 0 31862587Sitojun /* XXX: what icmp ? */ 31962587Sitojun#else 32062587Sitojun m_freem(mcopy); 32162587Sitojun#endif 32262587Sitojun } 32353541Sshin m_freem(m); 32453541Sshin return; 325171133Sgnn } else { 326171260Sdelphij /* 327171260Sdelphij * In the FAST IPSec case we have already 328171133Sgnn * re-injected the packet and it has been freed 329171260Sdelphij * by the ipsec_done() function. So, just clean 330171133Sgnn * up after ourselves. 331171133Sgnn */ 332171133Sgnn m = NULL; 333171133Sgnn goto freecopy; 33453541Sshin } 335122062Sume 336171133Sgnn if ((m != NULL) && (ip6 != mtod(m, struct ip6_hdr *)) ){ 337126006Sume /* 338126006Sume * now tunnel mode headers are added. we are originating 339126006Sume * packet instead of forwarding the packet. 340126006Sume */ 341126006Sume ip6_output(m, NULL, NULL, IPV6_FORWARDING/*XXX*/, NULL, NULL, 342126006Sume NULL); 343126006Sume goto freecopy; 344126006Sume } 345126006Sume 346122062Sume /* adjust pointer */ 347122062Sume dst = (struct sockaddr_in6 *)state.dst; 348122062Sume rt = state.ro ? state.ro->ro_rt : NULL; 349122062Sume if (dst != NULL && rt != NULL) 350122062Sume ipsecrt = 1; 35153541Sshin } 352122062Sume if (ipsecrt) 353122062Sume goto skip_routing; 354187949Sbzskip_ipsec: 355122062Sume#endif 356225044Sbzagain: 357187989Sbz bzero(&rin6, sizeof(struct route_in6)); 358187989Sbz dst = (struct sockaddr_in6 *)&rin6.ro_dst; 359187989Sbz dst->sin6_len = sizeof(struct sockaddr_in6); 360187989Sbz dst->sin6_family = AF_INET6; 361187989Sbz dst->sin6_addr = ip6->ip6_dst; 362225044Sbz#ifdef IPFIREWALL_FORWARD 363225044Sbzagain2: 364225044Sbz#endif 365231852Sbz rin6.ro_rt = in6_rtalloc1((struct sockaddr *)dst, 0, 0, M_GETFIB(m)); 366187989Sbz if (rin6.ro_rt != NULL) 367187989Sbz RT_UNLOCK(rin6.ro_rt); 368187989Sbz else { 369187989Sbz V_ip6stat.ip6s_noroute++; 370187989Sbz in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute); 371187989Sbz if (mcopy) { 372187989Sbz icmp6_error(mcopy, ICMP6_DST_UNREACH, 373187989Sbz ICMP6_DST_UNREACH_NOROUTE, 0); 37453541Sshin } 375187989Sbz goto bad; 37653541Sshin } 377187989Sbz rt = rin6.ro_rt; 378171167Sgnn#ifdef IPSEC 379187989Sbzskip_routing: 380122062Sume#endif 38162587Sitojun 38262587Sitojun /* 383148385Sume * Source scope check: if a packet can't be delivered to its 384148385Sume * destination for the reason that the destination is beyond the scope 385148385Sume * of the source address, discard the packet and return an icmp6 386148385Sume * destination unreachable error with Code 2 (beyond scope of source 387148385Sume * address). We use a local copy of ip6_src, since in6_setscope() 388148385Sume * will possibly modify its first argument. 389148385Sume * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1] 39062587Sitojun */ 391148385Sume src_in6 = ip6->ip6_src; 392148385Sume if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) { 393122062Sume /* XXX: this should not happen */ 394181803Sbz V_ip6stat.ip6s_cantforward++; 395181803Sbz V_ip6stat.ip6s_badscope++; 396187989Sbz goto bad; 397122062Sume } 398148385Sume if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) { 399181803Sbz V_ip6stat.ip6s_cantforward++; 400181803Sbz V_ip6stat.ip6s_badscope++; 401187989Sbz goto bad; 402148385Sume } 403148385Sume if (inzone != outzone 404171167Sgnn#ifdef IPSEC 405122062Sume && !ipsecrt 406122062Sume#endif 407122062Sume ) { 408181803Sbz V_ip6stat.ip6s_cantforward++; 409181803Sbz V_ip6stat.ip6s_badscope++; 41062587Sitojun in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard); 41162587Sitojun 412181803Sbz if (V_ip6_log_time + V_ip6_log_interval < time_second) { 413181803Sbz V_ip6_log_time = time_second; 41462587Sitojun log(LOG_DEBUG, 41562587Sitojun "cannot forward " 41662587Sitojun "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 417165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src), 418165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), 41962587Sitojun ip6->ip6_nxt, 42062587Sitojun if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp)); 42162587Sitojun } 42262587Sitojun if (mcopy) 42362587Sitojun icmp6_error(mcopy, ICMP6_DST_UNREACH, 42462587Sitojun ICMP6_DST_UNREACH_BEYONDSCOPE, 0); 425187989Sbz goto bad; 42662587Sitojun } 42762587Sitojun 428148385Sume /* 429148385Sume * Destination scope check: if a packet is going to break the scope 430148385Sume * zone of packet's destination address, discard it. This case should 431148385Sume * usually be prevented by appropriately-configured routing table, but 432148385Sume * we need an explicit check because we may mistakenly forward the 433148385Sume * packet to a different zone by (e.g.) a default route. 434148385Sume */ 435148385Sume dst_in6 = ip6->ip6_dst; 436148385Sume if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 || 437148385Sume in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 || 438148385Sume inzone != outzone) { 439181803Sbz V_ip6stat.ip6s_cantforward++; 440181803Sbz V_ip6stat.ip6s_badscope++; 441187989Sbz goto bad; 442148385Sume } 443148385Sume 444121283Sume if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) { 44553541Sshin in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig); 44662587Sitojun if (mcopy) { 44762587Sitojun u_long mtu; 448171167Sgnn#ifdef IPSEC 44962587Sitojun struct secpolicy *sp; 45062587Sitojun int ipsecerror; 45162587Sitojun size_t ipsechdrsiz; 452171167Sgnn#endif /* IPSEC */ 45362587Sitojun 454121283Sume mtu = IN6_LINKMTU(rt->rt_ifp); 455171167Sgnn#ifdef IPSEC 45662587Sitojun /* 45762587Sitojun * When we do IPsec tunnel ingress, we need to play 458122062Sume * with the link value (decrement IPsec header size 45962587Sitojun * from mtu value). The code is much simpler than v4 46062587Sitojun * case, as we have the outgoing interface for 46162587Sitojun * encapsulated packet as "rt->rt_ifp". 46262587Sitojun */ 463171133Sgnn sp = ipsec_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND, 46462587Sitojun IP_FORWARDING, &ipsecerror); 46562587Sitojun if (sp) { 466188306Sbz ipsechdrsiz = ipsec_hdrsiz(mcopy, 46762587Sitojun IPSEC_DIR_OUTBOUND, NULL); 46862587Sitojun if (ipsechdrsiz < mtu) 46962587Sitojun mtu -= ipsechdrsiz; 47062587Sitojun } 47162587Sitojun 47262587Sitojun /* 47362587Sitojun * if mtu becomes less than minimum MTU, 47462587Sitojun * tell minimum MTU (and I'll need to fragment it). 47562587Sitojun */ 47662587Sitojun if (mtu < IPV6_MMTU) 47762587Sitojun mtu = IPV6_MMTU; 478171167Sgnn#endif /* IPSEC */ 47962587Sitojun icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu); 48062587Sitojun } 481187989Sbz goto bad; 482120913Sume } 48353541Sshin 48453541Sshin if (rt->rt_flags & RTF_GATEWAY) 48553541Sshin dst = (struct sockaddr_in6 *)rt->rt_gateway; 48653541Sshin 48753541Sshin /* 48853541Sshin * If we are to forward the packet using the same interface 48953541Sshin * as one we got the packet from, perhaps we should send a redirect 49053541Sshin * to sender to shortcut a hop. 49153541Sshin * Only send redirect if source is sending directly to us, 49253541Sshin * and if packet was not source routed (or has any options). 49353541Sshin * Also, don't send redirect if forwarding using a route 49453541Sshin * modified by a redirect. 49553541Sshin */ 496181803Sbz if (V_ip6_sendredirects && rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && 497171167Sgnn#ifdef IPSEC 498122062Sume !ipsecrt && 499171167Sgnn#endif /* IPSEC */ 50078064Sume (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) { 50178064Sume if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) { 50278064Sume /* 50378064Sume * If the incoming interface is equal to the outgoing 50478064Sume * one, and the link attached to the interface is 50578064Sume * point-to-point, then it will be highly probable 50678064Sume * that a routing loop occurs. Thus, we immediately 50778064Sume * drop the packet and send an ICMPv6 error message. 50878064Sume * 50978064Sume * type/code is based on suggestion by Rich Draves. 51078064Sume * not sure if it is the best pick. 51178064Sume */ 51278064Sume icmp6_error(mcopy, ICMP6_DST_UNREACH, 51378064Sume ICMP6_DST_UNREACH_ADDR, 0); 514187989Sbz goto bad; 51578064Sume } 51653541Sshin type = ND_REDIRECT; 51778064Sume } 51853541Sshin 51953541Sshin /* 52062587Sitojun * Fake scoped addresses. Note that even link-local source or 52162587Sitojun * destinaion can appear, if the originating node just sends the 52262587Sitojun * packet to us (without address resolution for the destination). 52362587Sitojun * Since both icmp6_error and icmp6_redirect_output fill the embedded 52478064Sume * link identifiers, we can do this stuff after making a copy for 52578064Sume * returning an error. 52662587Sitojun */ 52762587Sitojun if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 52862587Sitojun /* 52962587Sitojun * See corresponding comments in ip6_output. 53062587Sitojun * XXX: but is it possible that ip6_forward() sends a packet 53162587Sitojun * to a loopback interface? I don't think so, and thus 53262587Sitojun * I bark here. (jinmei@kame.net) 53362587Sitojun * XXX: it is common to route invalid packets to loopback. 53462587Sitojun * also, the codepath will be visited on use of ::1 in 53562587Sitojun * rthdr. (itojun) 53662587Sitojun */ 53762587Sitojun#if 1 53862587Sitojun if (0) 53962587Sitojun#else 54062587Sitojun if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0) 54162587Sitojun#endif 54262587Sitojun { 54362587Sitojun printf("ip6_forward: outgoing interface is loopback. " 544120913Sume "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 545165118Sbz ip6_sprintf(ip6bufs, &ip6->ip6_src), 546165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), 547120913Sume ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif), 548120913Sume if_name(rt->rt_ifp)); 54962587Sitojun } 55062587Sitojun 55178064Sume /* we can just use rcvif in forwarding. */ 55278064Sume origifp = m->m_pkthdr.rcvif; 55362587Sitojun } 55462587Sitojun else 55562587Sitojun origifp = rt->rt_ifp; 55678064Sume /* 55778064Sume * clear embedded scope identifiers if necessary. 55878064Sume * in6_clearscope will touch the addresses only when necessary. 55978064Sume */ 56078064Sume in6_clearscope(&ip6->ip6_src); 56178064Sume in6_clearscope(&ip6->ip6_dst); 56262587Sitojun 563134383Sandre /* Jump over all PFIL processing if hooks are not active. */ 564197952Sjulian if (!PFIL_HOOKED(&V_inet6_pfil_hook)) 565134383Sandre goto pass; 566134383Sandre 567225044Sbz odst = ip6->ip6_dst; 568134383Sandre /* Run through list of hooks for output packets. */ 569197952Sjulian error = pfil_run_hooks(&V_inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT, NULL); 570120593Ssam if (error != 0) 571120593Ssam goto senderr; 572120386Ssam if (m == NULL) 573120386Ssam goto freecopy; 574120386Ssam ip6 = mtod(m, struct ip6_hdr *); 57584994Sdarrenr 576225044Sbz /* See if destination IP address was changed by packet filter. */ 577225044Sbz if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) { 578225044Sbz m->m_flags |= M_SKIP_FIREWALL; 579225044Sbz /* If destination is now ourself drop to ip6_input(). */ 580225044Sbz if (in6_localip(&ip6->ip6_dst)) { 581225044Sbz m->m_flags |= M_FASTFWD_OURS; 582225044Sbz if (m->m_pkthdr.rcvif == NULL) 583225044Sbz m->m_pkthdr.rcvif = V_loif; 584236170Sbz if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 585225044Sbz m->m_pkthdr.csum_flags |= 586236170Sbz CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR; 587225044Sbz m->m_pkthdr.csum_data = 0xffff; 588225044Sbz } 589225044Sbz#ifdef SCTP 590236332Stuexen if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) 591225044Sbz m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 592225044Sbz#endif 593225044Sbz error = netisr_queue(NETISR_IPV6, m); 594225044Sbz goto out; 595225044Sbz } else 596225044Sbz goto again; /* Redo the routing table lookup. */ 597225044Sbz } 598225044Sbz 599225044Sbz#ifdef IPFIREWALL_FORWARD 600225044Sbz /* See if local, if yes, send it to netisr. */ 601225044Sbz if (m->m_flags & M_FASTFWD_OURS) { 602225044Sbz if (m->m_pkthdr.rcvif == NULL) 603225044Sbz m->m_pkthdr.rcvif = V_loif; 604236170Sbz if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 605225044Sbz m->m_pkthdr.csum_flags |= 606236170Sbz CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR; 607225044Sbz m->m_pkthdr.csum_data = 0xffff; 608225044Sbz } 609225044Sbz#ifdef SCTP 610236332Stuexen if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) 611236332Stuexen m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 612236332Stuexen#endif 613225044Sbz error = netisr_queue(NETISR_IPV6, m); 614225044Sbz goto out; 615225044Sbz } 616225044Sbz /* Or forward to some other address? */ 617225044Sbz fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 618225044Sbz if (fwd_tag) { 619225044Sbz dst = (struct sockaddr_in6 *)&rin6.ro_dst; 620225044Sbz bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in6)); 621225044Sbz m->m_flags |= M_SKIP_FIREWALL; 622225044Sbz m_tag_delete(m, fwd_tag); 623225044Sbz goto again2; 624225044Sbz } 625225044Sbz#endif /* IPFIREWALL_FORWARD */ 626225044Sbz 627134383Sandrepass: 62862587Sitojun error = nd6_output(rt->rt_ifp, origifp, m, dst, rt); 62953541Sshin if (error) { 63053541Sshin in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard); 631181803Sbz V_ip6stat.ip6s_cantforward++; 63253541Sshin } else { 633181803Sbz V_ip6stat.ip6s_forward++; 63453541Sshin in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward); 63553541Sshin if (type) 636181803Sbz V_ip6stat.ip6s_redirectsent++; 63753541Sshin else { 63853541Sshin if (mcopy) 63953541Sshin goto freecopy; 64053541Sshin } 64153541Sshin } 642120913Sume 643120593Ssamsenderr: 64453541Sshin if (mcopy == NULL) 645187989Sbz goto out; 64653541Sshin switch (error) { 64753541Sshin case 0: 64853541Sshin if (type == ND_REDIRECT) { 64953541Sshin icmp6_redirect_output(mcopy, rt); 650187989Sbz goto out; 65153541Sshin } 65253541Sshin goto freecopy; 65353541Sshin 65453541Sshin case EMSGSIZE: 65553541Sshin /* xxx MTU is constant in PPP? */ 65653541Sshin goto freecopy; 65753541Sshin 65853541Sshin case ENOBUFS: 65953541Sshin /* Tell source to slow down like source quench in IP? */ 66053541Sshin goto freecopy; 66153541Sshin 66253541Sshin case ENETUNREACH: /* shouldn't happen, checked above */ 66353541Sshin case EHOSTUNREACH: 66453541Sshin case ENETDOWN: 66553541Sshin case EHOSTDOWN: 66653541Sshin default: 66753541Sshin type = ICMP6_DST_UNREACH; 66853541Sshin code = ICMP6_DST_UNREACH_ADDR; 66953541Sshin break; 67053541Sshin } 67153541Sshin icmp6_error(mcopy, type, code, 0); 672187989Sbz goto out; 67353541Sshin 67453541Sshin freecopy: 67553541Sshin m_freem(mcopy); 676187989Sbz goto out; 677187989Sbzbad: 678187989Sbz m_freem(m); 679187989Sbzout: 680187989Sbz if (rt != NULL 681187989Sbz#ifdef IPSEC 682187989Sbz && !ipsecrt 683187989Sbz#endif 684187989Sbz ) 685187989Sbz RTFREE(rt); 68653541Sshin} 687