ip6_forward.c revision 139826
162587Sitojun/* $FreeBSD: head/sys/netinet6/ip6_forward.c 139826 2005-01-07 02:30:35Z imp $ */ 278064Sume/* $KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $ */ 362587Sitojun 4139826Simp/*- 553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 653541Sshin * All rights reserved. 753541Sshin * 853541Sshin * Redistribution and use in source and binary forms, with or without 953541Sshin * modification, are permitted provided that the following conditions 1053541Sshin * are met: 1153541Sshin * 1. Redistributions of source code must retain the above copyright 1253541Sshin * notice, this list of conditions and the following disclaimer. 1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1453541Sshin * notice, this list of conditions and the following disclaimer in the 1553541Sshin * documentation and/or other materials provided with the distribution. 1653541Sshin * 3. Neither the name of the project nor the names of its contributors 1753541Sshin * may be used to endorse or promote products derived from this software 1853541Sshin * without specific prior written permission. 1953541Sshin * 2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2353541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3053541Sshin * SUCH DAMAGE. 3153541Sshin */ 3253541Sshin 3362587Sitojun#include "opt_ip6fw.h" 3462587Sitojun#include "opt_inet.h" 3562587Sitojun#include "opt_inet6.h" 3655009Sshin#include "opt_ipsec.h" 3753541Sshin 3853541Sshin#include <sys/param.h> 3953541Sshin#include <sys/systm.h> 4078064Sume#include <sys/malloc.h> 4153541Sshin#include <sys/mbuf.h> 4253541Sshin#include <sys/domain.h> 4353541Sshin#include <sys/protosw.h> 4453541Sshin#include <sys/socket.h> 4553541Sshin#include <sys/errno.h> 4653541Sshin#include <sys/time.h> 4778064Sume#include <sys/kernel.h> 4853541Sshin#include <sys/syslog.h> 4953541Sshin 5053541Sshin#include <net/if.h> 5153541Sshin#include <net/route.h> 5284994Sdarrenr#include <net/pfil.h> 5353541Sshin 5453541Sshin#include <netinet/in.h> 5553541Sshin#include <netinet/in_var.h> 5678064Sume#include <netinet/in_systm.h> 5778064Sume#include <netinet/ip.h> 5862587Sitojun#include <netinet/ip_var.h> 5978064Sume#include <netinet6/in6_var.h> 6062587Sitojun#include <netinet/ip6.h> 6153541Sshin#include <netinet6/ip6_var.h> 6262587Sitojun#include <netinet/icmp6.h> 6353541Sshin#include <netinet6/nd6.h> 6453541Sshin 6578064Sume#include <netinet/in_pcb.h> 6678064Sume 6763256Sitojun#ifdef IPSEC 6853541Sshin#include <netinet6/ipsec.h> 6978064Sume#ifdef INET6 7062601Sitojun#include <netinet6/ipsec6.h> 7178064Sume#endif 7253541Sshin#include <netkey/key.h> 7363256Sitojun#endif /* IPSEC */ 7453541Sshin 75105199Ssam#ifdef FAST_IPSEC 76105199Ssam#include <netipsec/ipsec.h> 77105199Ssam#include <netipsec/ipsec6.h> 78105199Ssam#include <netipsec/key.h> 79105199Ssam#define IPSEC 80105199Ssam#endif /* FAST_IPSEC */ 81105199Ssam 8253541Sshin#include <netinet6/ip6_fw.h> 8353541Sshin 8453541Sshin#include <net/net_osdep.h> 8553541Sshin 8684994Sdarrenr#include <netinet6/ip6protosw.h> 8784994Sdarrenr 8853541Sshinstruct route_in6 ip6_forward_rt; 8953541Sshin 9053541Sshin/* 9153541Sshin * Forward a packet. If some error occurs return the sender 9253541Sshin * an icmp packet. Note we can't always generate a meaningful 9353541Sshin * icmp message because icmp doesn't have a large enough repertoire 9453541Sshin * of codes and types. 9553541Sshin * 9653541Sshin * If not forwarding, just drop the packet. This could be confusing 9753541Sshin * if ipforwarding was zero but some routing protocol was advancing 9853541Sshin * us as a gateway to somewhere. However, we must let the routing 9953541Sshin * protocol deal with that. 10053541Sshin * 10153541Sshin */ 10253541Sshin 10353541Sshinvoid 10453541Sshinip6_forward(m, srcrt) 10553541Sshin struct mbuf *m; 10653541Sshin int srcrt; 10753541Sshin{ 10853541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 109122062Sume struct sockaddr_in6 *dst = NULL; 110122062Sume struct rtentry *rt = NULL; 11153541Sshin int error, type = 0, code = 0; 11262587Sitojun struct mbuf *mcopy = NULL; 11362587Sitojun struct ifnet *origifp; /* maybe unnecessary */ 114121315Sume u_int32_t srczone, dstzone; 11563256Sitojun#ifdef IPSEC 11653541Sshin struct secpolicy *sp = NULL; 117122062Sume int ipsecrt = 0; 11853541Sshin#endif 11953541Sshin 12063256Sitojun#ifdef IPSEC 12153541Sshin /* 12253541Sshin * Check AH/ESP integrity. 12353541Sshin */ 12453541Sshin /* 12553541Sshin * Don't increment ip6s_cantforward because this is the check 12653541Sshin * before forwarding packet actually. 12753541Sshin */ 12853541Sshin if (ipsec6_in_reject(m, NULL)) { 129105199Ssam#if !defined(FAST_IPSEC) 13053541Sshin ipsec6stat.in_polvio++; 131105199Ssam#endif 13253541Sshin m_freem(m); 13353541Sshin return; 13453541Sshin } 13595023Ssuz#endif /* IPSEC */ 13653541Sshin 13778064Sume /* 13878064Sume * Do not forward packets to multicast destination (should be handled 13978064Sume * by ip6_mforward(). 14078064Sume * Do not forward packets with unspecified source. It was discussed 141120913Sume * in July 2000, on the ipngwg mailing list. 14278064Sume */ 14362587Sitojun if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 || 14478064Sume IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 14578064Sume IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 14653541Sshin ip6stat.ip6s_cantforward++; 14753541Sshin /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 14853541Sshin if (ip6_log_time + ip6_log_interval < time_second) { 14953541Sshin ip6_log_time = time_second; 15053541Sshin log(LOG_DEBUG, 15153541Sshin "cannot forward " 15253541Sshin "from %s to %s nxt %d received on %s\n", 15362587Sitojun ip6_sprintf(&ip6->ip6_src), 15462587Sitojun ip6_sprintf(&ip6->ip6_dst), 15553541Sshin ip6->ip6_nxt, 15653541Sshin if_name(m->m_pkthdr.rcvif)); 15753541Sshin } 15853541Sshin m_freem(m); 15953541Sshin return; 16053541Sshin } 16153541Sshin 16253541Sshin if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 16353541Sshin /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 16453541Sshin icmp6_error(m, ICMP6_TIME_EXCEEDED, 16553541Sshin ICMP6_TIME_EXCEED_TRANSIT, 0); 16653541Sshin return; 16753541Sshin } 16853541Sshin ip6->ip6_hlim -= IPV6_HLIMDEC; 16953541Sshin 17062587Sitojun /* 17162587Sitojun * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU - 17262587Sitojun * size of IPv6 + ICMPv6 headers) bytes of the packet in case 17362587Sitojun * we need to generate an ICMP6 message to the src. 17462587Sitojun * Thanks to M_EXT, in most cases copy will not occur. 17562587Sitojun * 17662587Sitojun * It is important to save it before IPsec processing as IPsec 17762587Sitojun * processing may modify the mbuf. 17862587Sitojun */ 17962587Sitojun mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN)); 18062587Sitojun 18163256Sitojun#ifdef IPSEC 18253541Sshin /* get a security policy for this packet */ 183120913Sume sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 184120913Sume IP_FORWARDING, &error); 18553541Sshin if (sp == NULL) { 18653541Sshin ipsec6stat.out_inval++; 18753541Sshin ip6stat.ip6s_cantforward++; 18862587Sitojun if (mcopy) { 18962587Sitojun#if 0 19062587Sitojun /* XXX: what icmp ? */ 19162587Sitojun#else 19262587Sitojun m_freem(mcopy); 19362587Sitojun#endif 19462587Sitojun } 19553541Sshin m_freem(m); 19653541Sshin return; 19753541Sshin } 19853541Sshin 19953541Sshin error = 0; 20053541Sshin 20153541Sshin /* check policy */ 20253541Sshin switch (sp->policy) { 20353541Sshin case IPSEC_POLICY_DISCARD: 20453541Sshin /* 20553541Sshin * This packet is just discarded. 20653541Sshin */ 20753541Sshin ipsec6stat.out_polvio++; 20853541Sshin ip6stat.ip6s_cantforward++; 20953541Sshin key_freesp(sp); 21062587Sitojun if (mcopy) { 21162587Sitojun#if 0 21262587Sitojun /* XXX: what icmp ? */ 21362587Sitojun#else 21462587Sitojun m_freem(mcopy); 21562587Sitojun#endif 21662587Sitojun } 21753541Sshin m_freem(m); 21853541Sshin return; 21953541Sshin 22053541Sshin case IPSEC_POLICY_BYPASS: 22153541Sshin case IPSEC_POLICY_NONE: 22253541Sshin /* no need to do IPsec. */ 22353541Sshin key_freesp(sp); 22453541Sshin goto skip_ipsec; 22562587Sitojun 22653541Sshin case IPSEC_POLICY_IPSEC: 22753541Sshin if (sp->req == NULL) { 22853541Sshin /* XXX should be panic ? */ 22953541Sshin printf("ip6_forward: No IPsec request specified.\n"); 23053541Sshin ip6stat.ip6s_cantforward++; 23153541Sshin key_freesp(sp); 23262587Sitojun if (mcopy) { 23362587Sitojun#if 0 23462587Sitojun /* XXX: what icmp ? */ 23562587Sitojun#else 23662587Sitojun m_freem(mcopy); 23762587Sitojun#endif 23862587Sitojun } 23953541Sshin m_freem(m); 24053541Sshin return; 24153541Sshin } 24253541Sshin /* do IPsec */ 24353541Sshin break; 24453541Sshin 24553541Sshin case IPSEC_POLICY_ENTRUST: 24653541Sshin default: 24753541Sshin /* should be panic ?? */ 24853541Sshin printf("ip6_forward: Invalid policy found. %d\n", sp->policy); 24953541Sshin key_freesp(sp); 25053541Sshin goto skip_ipsec; 25153541Sshin } 25253541Sshin 25353541Sshin { 254122062Sume struct ipsecrequest *isr = NULL; 25553541Sshin struct ipsec_output_state state; 25653541Sshin 25753541Sshin /* 258122062Sume * when the kernel forwards a packet, it is not proper to apply 259122062Sume * IPsec transport mode to the packet is not proper. this check 260122062Sume * avoid from this. 261122062Sume * at present, if there is even a transport mode SA request in the 262122062Sume * security policy, the kernel does not apply IPsec to the packet. 263122062Sume * this check is not enough because the following case is valid. 264122062Sume * ipsec esp/tunnel/xxx-xxx/require esp/transport//require; 265122062Sume */ 266122062Sume for (isr = sp->req; isr; isr = isr->next) { 267126006Sume if (isr->saidx.mode == IPSEC_MODE_ANY) 268126006Sume goto doipsectunnel; 269126006Sume if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 270126006Sume goto doipsectunnel; 271122062Sume } 272122062Sume 273122062Sume /* 274126006Sume * if there's no need for tunnel mode IPsec, skip. 275126006Sume */ 276126006Sume if (!isr) 277126006Sume goto skip_ipsec; 278126006Sume 279126006Sume doipsectunnel: 280126006Sume /* 28153541Sshin * All the extension headers will become inaccessible 28253541Sshin * (since they can be encrypted). 28353541Sshin * Don't panic, we need no more updates to extension headers 28453541Sshin * on inner IPv6 packet (since they are now encapsulated). 28553541Sshin * 28653541Sshin * IPv6 [ESP|AH] IPv6 [extension headers] payload 28753541Sshin */ 28853541Sshin bzero(&state, sizeof(state)); 28953541Sshin state.m = m; 29053541Sshin state.ro = NULL; /* update at ipsec6_output_tunnel() */ 29153541Sshin state.dst = NULL; /* update at ipsec6_output_tunnel() */ 29253541Sshin 29353541Sshin error = ipsec6_output_tunnel(&state, sp, 0); 29453541Sshin 29553541Sshin m = state.m; 29653541Sshin key_freesp(sp); 29753541Sshin 29853541Sshin if (error) { 29953541Sshin /* mbuf is already reclaimed in ipsec6_output_tunnel. */ 30053541Sshin switch (error) { 30153541Sshin case EHOSTUNREACH: 30253541Sshin case ENETUNREACH: 30353541Sshin case EMSGSIZE: 30453541Sshin case ENOBUFS: 30553541Sshin case ENOMEM: 30653541Sshin break; 30753541Sshin default: 30853541Sshin printf("ip6_output (ipsec): error code %d\n", error); 309120913Sume /* FALLTHROUGH */ 31053541Sshin case ENOENT: 31153541Sshin /* don't show these error codes to the user */ 31253541Sshin break; 31353541Sshin } 31453541Sshin ip6stat.ip6s_cantforward++; 31562587Sitojun if (mcopy) { 31662587Sitojun#if 0 31762587Sitojun /* XXX: what icmp ? */ 31862587Sitojun#else 31962587Sitojun m_freem(mcopy); 32062587Sitojun#endif 32162587Sitojun } 32253541Sshin m_freem(m); 32353541Sshin return; 32453541Sshin } 325122062Sume 326126006Sume if (ip6 != mtod(m, struct ip6_hdr *)) { 327126006Sume /* 328126006Sume * now tunnel mode headers are added. we are originating 329126006Sume * packet instead of forwarding the packet. 330126006Sume */ 331126006Sume ip6_output(m, NULL, NULL, IPV6_FORWARDING/*XXX*/, NULL, NULL, 332126006Sume NULL); 333126006Sume goto freecopy; 334126006Sume } 335126006Sume 336122062Sume /* adjust pointer */ 337122062Sume dst = (struct sockaddr_in6 *)state.dst; 338122062Sume rt = state.ro ? state.ro->ro_rt : NULL; 339122062Sume if (dst != NULL && rt != NULL) 340122062Sume ipsecrt = 1; 34153541Sshin } 34253541Sshin skip_ipsec: 34363256Sitojun#endif /* IPSEC */ 34453541Sshin 345122062Sume#ifdef IPSEC 346122062Sume if (ipsecrt) 347122062Sume goto skip_routing; 348122062Sume#endif 349122062Sume 35078064Sume dst = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; 35153541Sshin if (!srcrt) { 352120913Sume /* ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst */ 35353541Sshin if (ip6_forward_rt.ro_rt == 0 || 35453541Sshin (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) { 35553541Sshin if (ip6_forward_rt.ro_rt) { 35653541Sshin RTFREE(ip6_forward_rt.ro_rt); 35753541Sshin ip6_forward_rt.ro_rt = 0; 35853541Sshin } 359120913Sume 36053541Sshin /* this probably fails but give it a try again */ 361122921Sandre rtalloc((struct route *)&ip6_forward_rt); 36253541Sshin } 36362587Sitojun 36453541Sshin if (ip6_forward_rt.ro_rt == 0) { 36553541Sshin ip6stat.ip6s_noroute++; 36678064Sume in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute); 36762587Sitojun if (mcopy) { 36862587Sitojun icmp6_error(mcopy, ICMP6_DST_UNREACH, 36962587Sitojun ICMP6_DST_UNREACH_NOROUTE, 0); 37062587Sitojun } 37162587Sitojun m_freem(m); 37253541Sshin return; 37353541Sshin } 37453541Sshin } else if ((rt = ip6_forward_rt.ro_rt) == 0 || 375120913Sume !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) { 37653541Sshin if (ip6_forward_rt.ro_rt) { 37753541Sshin RTFREE(ip6_forward_rt.ro_rt); 37853541Sshin ip6_forward_rt.ro_rt = 0; 37953541Sshin } 38053541Sshin bzero(dst, sizeof(*dst)); 38153541Sshin dst->sin6_len = sizeof(struct sockaddr_in6); 38253541Sshin dst->sin6_family = AF_INET6; 38353541Sshin dst->sin6_addr = ip6->ip6_dst; 38453541Sshin 385122921Sandre rtalloc((struct route *)&ip6_forward_rt); 38653541Sshin if (ip6_forward_rt.ro_rt == 0) { 38753541Sshin ip6stat.ip6s_noroute++; 38878064Sume in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute); 38962587Sitojun if (mcopy) { 39062587Sitojun icmp6_error(mcopy, ICMP6_DST_UNREACH, 39162587Sitojun ICMP6_DST_UNREACH_NOROUTE, 0); 39262587Sitojun } 39362587Sitojun m_freem(m); 39453541Sshin return; 39553541Sshin } 39653541Sshin } 39753541Sshin rt = ip6_forward_rt.ro_rt; 398122062Sume#ifdef IPSEC 399122062Sume skip_routing:; 400122062Sume#endif 40162587Sitojun 40262587Sitojun /* 40362587Sitojun * Scope check: if a packet can't be delivered to its destination 40462587Sitojun * for the reason that the destination is beyond the scope of the 40562587Sitojun * source address, discard the packet and return an icmp6 destination 40662587Sitojun * unreachable error with Code 2 (beyond scope of source address). 40795023Ssuz * [draft-ietf-ipngwg-icmp-v3-02.txt, Section 3.1] 40862587Sitojun */ 409121315Sume if (in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_src, &srczone) || 410122062Sume in6_addr2zoneid(rt->rt_ifp, &ip6->ip6_src, &dstzone)) { 411122062Sume /* XXX: this should not happen */ 41262587Sitojun ip6stat.ip6s_cantforward++; 41362587Sitojun ip6stat.ip6s_badscope++; 414122062Sume m_freem(m); 415122062Sume return; 416122062Sume } 417122062Sume if (srczone != dstzone 418122062Sume#ifdef IPSEC 419122062Sume && !ipsecrt 420122062Sume#endif 421122062Sume ) { 422122062Sume ip6stat.ip6s_cantforward++; 423122062Sume ip6stat.ip6s_badscope++; 42462587Sitojun in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard); 42562587Sitojun 42662587Sitojun if (ip6_log_time + ip6_log_interval < time_second) { 42762587Sitojun ip6_log_time = time_second; 42862587Sitojun log(LOG_DEBUG, 42962587Sitojun "cannot forward " 43062587Sitojun "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 43162587Sitojun ip6_sprintf(&ip6->ip6_src), 43262587Sitojun ip6_sprintf(&ip6->ip6_dst), 43362587Sitojun ip6->ip6_nxt, 43462587Sitojun if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp)); 43562587Sitojun } 43662587Sitojun if (mcopy) 43762587Sitojun icmp6_error(mcopy, ICMP6_DST_UNREACH, 43862587Sitojun ICMP6_DST_UNREACH_BEYONDSCOPE, 0); 43962587Sitojun m_freem(m); 44062587Sitojun return; 44162587Sitojun } 44262587Sitojun 443121283Sume if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) { 44453541Sshin in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig); 44562587Sitojun if (mcopy) { 44662587Sitojun u_long mtu; 44763256Sitojun#ifdef IPSEC 44862587Sitojun struct secpolicy *sp; 44962587Sitojun int ipsecerror; 45062587Sitojun size_t ipsechdrsiz; 45162587Sitojun#endif 45262587Sitojun 453121283Sume mtu = IN6_LINKMTU(rt->rt_ifp); 45463256Sitojun#ifdef IPSEC 45562587Sitojun /* 45662587Sitojun * When we do IPsec tunnel ingress, we need to play 457122062Sume * with the link value (decrement IPsec header size 45862587Sitojun * from mtu value). The code is much simpler than v4 45962587Sitojun * case, as we have the outgoing interface for 46062587Sitojun * encapsulated packet as "rt->rt_ifp". 46162587Sitojun */ 46262587Sitojun sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND, 46362587Sitojun IP_FORWARDING, &ipsecerror); 46462587Sitojun if (sp) { 46562587Sitojun ipsechdrsiz = ipsec6_hdrsiz(mcopy, 46662587Sitojun IPSEC_DIR_OUTBOUND, NULL); 46762587Sitojun if (ipsechdrsiz < mtu) 46862587Sitojun mtu -= ipsechdrsiz; 46962587Sitojun } 47062587Sitojun 47162587Sitojun /* 47262587Sitojun * if mtu becomes less than minimum MTU, 47362587Sitojun * tell minimum MTU (and I'll need to fragment it). 47462587Sitojun */ 47562587Sitojun if (mtu < IPV6_MMTU) 47662587Sitojun mtu = IPV6_MMTU; 47762587Sitojun#endif 47862587Sitojun icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu); 47962587Sitojun } 48062587Sitojun m_freem(m); 48153541Sshin return; 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 */ 49653541Sshin if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && 497122062Sume#ifdef IPSEC 498122062Sume !ipsecrt && 499122062Sume#endif 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); 51478064Sume m_freem(m); 51578064Sume return; 51678064Sume } 51753541Sshin type = ND_REDIRECT; 51878064Sume } 51953541Sshin 52053541Sshin /* 52153541Sshin * Check with the firewall... 52253541Sshin */ 52366303Sume if (ip6_fw_enable && ip6_fw_chk_ptr) { 52453541Sshin u_short port = 0; 52553541Sshin /* If ipfw says divert, we have to just drop packet */ 52653541Sshin if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) { 52753541Sshin m_freem(m); 52853541Sshin goto freecopy; 52953541Sshin } 53053541Sshin if (!m) 53153541Sshin goto freecopy; 53253541Sshin } 53353541Sshin 53462587Sitojun /* 53562587Sitojun * Fake scoped addresses. Note that even link-local source or 53662587Sitojun * destinaion can appear, if the originating node just sends the 53762587Sitojun * packet to us (without address resolution for the destination). 53862587Sitojun * Since both icmp6_error and icmp6_redirect_output fill the embedded 53978064Sume * link identifiers, we can do this stuff after making a copy for 54078064Sume * returning an error. 54162587Sitojun */ 54262587Sitojun if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 54362587Sitojun /* 54462587Sitojun * See corresponding comments in ip6_output. 54562587Sitojun * XXX: but is it possible that ip6_forward() sends a packet 54662587Sitojun * to a loopback interface? I don't think so, and thus 54762587Sitojun * I bark here. (jinmei@kame.net) 54862587Sitojun * XXX: it is common to route invalid packets to loopback. 54962587Sitojun * also, the codepath will be visited on use of ::1 in 55062587Sitojun * rthdr. (itojun) 55162587Sitojun */ 55262587Sitojun#if 1 55362587Sitojun if (0) 55462587Sitojun#else 55562587Sitojun if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0) 55662587Sitojun#endif 55762587Sitojun { 55862587Sitojun printf("ip6_forward: outgoing interface is loopback. " 559120913Sume "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 560120913Sume ip6_sprintf(&ip6->ip6_src), 561120913Sume ip6_sprintf(&ip6->ip6_dst), 562120913Sume ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif), 563120913Sume if_name(rt->rt_ifp)); 56462587Sitojun } 56562587Sitojun 56678064Sume /* we can just use rcvif in forwarding. */ 56778064Sume origifp = m->m_pkthdr.rcvif; 56862587Sitojun } 56962587Sitojun else 57062587Sitojun origifp = rt->rt_ifp; 57178064Sume /* 57278064Sume * clear embedded scope identifiers if necessary. 57378064Sume * in6_clearscope will touch the addresses only when necessary. 57478064Sume */ 57578064Sume in6_clearscope(&ip6->ip6_src); 57678064Sume in6_clearscope(&ip6->ip6_dst); 57762587Sitojun 578134383Sandre /* Jump over all PFIL processing if hooks are not active. */ 579134383Sandre if (inet6_pfil_hook.ph_busy_count == -1) 580134383Sandre goto pass; 581134383Sandre 582134383Sandre /* Run through list of hooks for output packets. */ 583135920Smlaier error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT, NULL); 584120593Ssam if (error != 0) 585120593Ssam goto senderr; 586120386Ssam if (m == NULL) 587120386Ssam goto freecopy; 588120386Ssam ip6 = mtod(m, struct ip6_hdr *); 58984994Sdarrenr 590134383Sandrepass: 59162587Sitojun error = nd6_output(rt->rt_ifp, origifp, m, dst, rt); 59253541Sshin if (error) { 59353541Sshin in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard); 59453541Sshin ip6stat.ip6s_cantforward++; 59553541Sshin } else { 59653541Sshin ip6stat.ip6s_forward++; 59753541Sshin in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward); 59853541Sshin if (type) 59953541Sshin ip6stat.ip6s_redirectsent++; 60053541Sshin else { 60153541Sshin if (mcopy) 60253541Sshin goto freecopy; 60353541Sshin } 60453541Sshin } 605120913Sume 606120593Ssamsenderr: 60753541Sshin if (mcopy == NULL) 60853541Sshin return; 60953541Sshin switch (error) { 61053541Sshin case 0: 61153541Sshin if (type == ND_REDIRECT) { 61253541Sshin icmp6_redirect_output(mcopy, rt); 61353541Sshin return; 61453541Sshin } 61553541Sshin goto freecopy; 61653541Sshin 61753541Sshin case EMSGSIZE: 61853541Sshin /* xxx MTU is constant in PPP? */ 61953541Sshin goto freecopy; 62053541Sshin 62153541Sshin case ENOBUFS: 62253541Sshin /* Tell source to slow down like source quench in IP? */ 62353541Sshin goto freecopy; 62453541Sshin 62553541Sshin case ENETUNREACH: /* shouldn't happen, checked above */ 62653541Sshin case EHOSTUNREACH: 62753541Sshin case ENETDOWN: 62853541Sshin case EHOSTDOWN: 62953541Sshin default: 63053541Sshin type = ICMP6_DST_UNREACH; 63153541Sshin code = ICMP6_DST_UNREACH_ADDR; 63253541Sshin break; 63353541Sshin } 63453541Sshin icmp6_error(mcopy, type, code, 0); 63553541Sshin return; 63653541Sshin 63753541Sshin freecopy: 63853541Sshin m_freem(mcopy); 63953541Sshin return; 64053541Sshin} 641