ip6_forward.c revision 155201
1136849Sscottl/* $FreeBSD: head/sys/netinet6/ip6_forward.c 155201 2006-02-02 03:13:16Z csjp $ */ 2149871Sscottl/* $KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $ */ 3136849Sscottl 4136849Sscottl/*- 5136849Sscottl * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6136849Sscottl * All rights reserved. 7136849Sscottl * 8136849Sscottl * Redistribution and use in source and binary forms, with or without 9136849Sscottl * modification, are permitted provided that the following conditions 10136849Sscottl * are met: 11136849Sscottl * 1. Redistributions of source code must retain the above copyright 12136849Sscottl * notice, this list of conditions and the following disclaimer. 13136849Sscottl * 2. Redistributions in binary form must reproduce the above copyright 14136849Sscottl * notice, this list of conditions and the following disclaimer in the 15136849Sscottl * documentation and/or other materials provided with the distribution. 16136849Sscottl * 3. Neither the name of the project nor the names of its contributors 17136849Sscottl * may be used to endorse or promote products derived from this software 18136849Sscottl * without specific prior written permission. 19136849Sscottl * 20136849Sscottl * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21136849Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22136849Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23136849Sscottl * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24136849Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25142988Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26142988Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27136849Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28136849Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29136849Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30136849Sscottl * SUCH DAMAGE. 31136849Sscottl */ 32136849Sscottl 33136849Sscottl#include "opt_ip6fw.h" 34136849Sscottl#include "opt_inet.h" 35136849Sscottl#include "opt_inet6.h" 36136849Sscottl#include "opt_ipsec.h" 37136849Sscottl#include "opt_ipstealth.h" 38149871Sscottl 39149871Sscottl#include <sys/param.h> 40149871Sscottl#include <sys/systm.h> 41149871Sscottl#include <sys/malloc.h> 42136849Sscottl#include <sys/mbuf.h> 43136849Sscottl#include <sys/domain.h> 44136849Sscottl#include <sys/protosw.h> 45143039Sscottl#include <sys/socket.h> 46136849Sscottl#include <sys/errno.h> 47136849Sscottl#include <sys/time.h> 48136849Sscottl#include <sys/kernel.h> 49136849Sscottl#include <sys/syslog.h> 50136849Sscottl 51136849Sscottl#include <net/if.h> 52149871Sscottl#include <net/route.h> 53136849Sscottl#include <net/pfil.h> 54219819Sjeff 55219819Sjeff#include <netinet/in.h> 56136849Sscottl#include <netinet/in_var.h> 57136849Sscottl#include <netinet/in_systm.h> 58136849Sscottl#include <netinet/ip.h> 59149871Sscottl#include <netinet/ip_var.h> 60149871Sscottl#include <netinet6/in6_var.h> 61136849Sscottl#include <netinet/ip6.h> 62136849Sscottl#include <netinet6/ip6_var.h> 63136849Sscottl#include <netinet6/scope6_var.h> 64136849Sscottl#include <netinet/icmp6.h> 65136849Sscottl#include <netinet6/nd6.h> 66136849Sscottl 67136849Sscottl#include <netinet/in_pcb.h> 68136849Sscottl 69136849Sscottl#ifdef IPSEC 70136849Sscottl#include <netinet6/ipsec.h> 71136849Sscottl#ifdef INET6 72136849Sscottl#include <netinet6/ipsec6.h> 73136849Sscottl#endif 74136849Sscottl#include <netkey/key.h> 75136849Sscottl#endif /* IPSEC */ 76136849Sscottl 77136849Sscottl#ifdef FAST_IPSEC 78136849Sscottl#include <netipsec/ipsec.h> 79136849Sscottl#include <netipsec/ipsec6.h> 80136849Sscottl#include <netipsec/key.h> 81136849Sscottl#define IPSEC 82136849Sscottl#endif /* FAST_IPSEC */ 83136849Sscottl 84136849Sscottl#include <netinet6/ip6_fw.h> 85136849Sscottl 86136849Sscottl#include <net/net_osdep.h> 87136849Sscottl 88136849Sscottl#include <netinet6/ip6protosw.h> 89136849Sscottl 90136849Sscottlstruct route_in6 ip6_forward_rt; 91136849Sscottl 92136849Sscottl/* 93136849Sscottl * Forward a packet. If some error occurs return the sender 94136849Sscottl * an icmp packet. Note we can't always generate a meaningful 95136849Sscottl * icmp message because icmp doesn't have a large enough repertoire 96136849Sscottl * of codes and types. 97136849Sscottl * 98136849Sscottl * If not forwarding, just drop the packet. This could be confusing 99136849Sscottl * if ipforwarding was zero but some routing protocol was advancing 100136849Sscottl * us as a gateway to somewhere. However, we must let the routing 101136849Sscottl * protocol deal with that. 102136849Sscottl * 103136849Sscottl */ 104136849Sscottl 105136849Sscottlvoid 106136849Sscottlip6_forward(m, srcrt) 107136849Sscottl struct mbuf *m; 108136849Sscottl int srcrt; 109136849Sscottl{ 110136849Sscottl struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 111136849Sscottl struct sockaddr_in6 *dst = NULL; 112136849Sscottl struct rtentry *rt = NULL; 113136849Sscottl int error, type = 0, code = 0; 114136849Sscottl struct mbuf *mcopy = NULL; 115136849Sscottl struct ifnet *origifp; /* maybe unnecessary */ 116136849Sscottl u_int32_t inzone, outzone; 117136849Sscottl struct in6_addr src_in6, dst_in6; 118136849Sscottl#ifdef IPSEC 119136849Sscottl struct secpolicy *sp = NULL; 120136849Sscottl int ipsecrt = 0; 121136849Sscottl#endif 122136849Sscottl 123136849Sscottl#ifdef IPSEC 124136849Sscottl /* 125136849Sscottl * Check AH/ESP integrity. 126136849Sscottl */ 127149871Sscottl /* 128136849Sscottl * Don't increment ip6s_cantforward because this is the check 129136849Sscottl * before forwarding packet actually. 130136849Sscottl */ 131136849Sscottl if (ipsec6_in_reject(m, NULL)) { 132136849Sscottl#if !defined(FAST_IPSEC) 133136849Sscottl ipsec6stat.in_polvio++; 134136849Sscottl#endif 135136849Sscottl m_freem(m); 136136849Sscottl return; 137136849Sscottl } 138136849Sscottl#endif /* IPSEC */ 139136849Sscottl 140136849Sscottl /* 141136849Sscottl * Do not forward packets to multicast destination (should be handled 142136849Sscottl * by ip6_mforward(). 143136849Sscottl * Do not forward packets with unspecified source. It was discussed 144136849Sscottl * in July 2000, on the ipngwg mailing list. 145136849Sscottl */ 146136849Sscottl if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 || 147149871Sscottl IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 148149871Sscottl IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 149149871Sscottl ip6stat.ip6s_cantforward++; 150136849Sscottl /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 151136849Sscottl if (ip6_log_time + ip6_log_interval < time_second) { 152136849Sscottl ip6_log_time = time_second; 153136849Sscottl log(LOG_DEBUG, 154136849Sscottl "cannot forward " 155136849Sscottl "from %s to %s nxt %d received on %s\n", 156136849Sscottl ip6_sprintf(&ip6->ip6_src), 157136849Sscottl ip6_sprintf(&ip6->ip6_dst), 158136849Sscottl ip6->ip6_nxt, 159136849Sscottl if_name(m->m_pkthdr.rcvif)); 160136849Sscottl } 161136849Sscottl m_freem(m); 162136849Sscottl return; 163136849Sscottl } 164136849Sscottl 165136849Sscottl#ifdef IPSTEALTH 166136849Sscottl if (!ip6stealth) { 167136849Sscottl#endif 168136849Sscottl if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 169136849Sscottl /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 170136849Sscottl icmp6_error(m, ICMP6_TIME_EXCEEDED, 171136849Sscottl ICMP6_TIME_EXCEED_TRANSIT, 0); 172136849Sscottl return; 173136849Sscottl } 174136849Sscottl ip6->ip6_hlim -= IPV6_HLIMDEC; 175136849Sscottl 176136849Sscottl#ifdef IPSTEALTH 177136849Sscottl } 178136849Sscottl#endif 179136849Sscottl 180136849Sscottl /* 181136849Sscottl * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU - 182136849Sscottl * size of IPv6 + ICMPv6 headers) bytes of the packet in case 183136849Sscottl * we need to generate an ICMP6 message to the src. 184136849Sscottl * Thanks to M_EXT, in most cases copy will not occur. 185136849Sscottl * 186136849Sscottl * It is important to save it before IPsec processing as IPsec 187136849Sscottl * processing may modify the mbuf. 188136849Sscottl */ 189136849Sscottl mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN)); 190136849Sscottl 191136849Sscottl#ifdef IPSEC 192136849Sscottl /* get a security policy for this packet */ 193136849Sscottl sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 194136849Sscottl IP_FORWARDING, &error); 195136849Sscottl if (sp == NULL) { 196136849Sscottl ipsec6stat.out_inval++; 197136849Sscottl ip6stat.ip6s_cantforward++; 198136849Sscottl if (mcopy) { 199136849Sscottl#if 0 200136849Sscottl /* XXX: what icmp ? */ 201136849Sscottl#else 202136849Sscottl m_freem(mcopy); 203136849Sscottl#endif 204136849Sscottl } 205136849Sscottl m_freem(m); 206136849Sscottl return; 207136849Sscottl } 208136849Sscottl 209136849Sscottl error = 0; 210136849Sscottl 211136849Sscottl /* check policy */ 212136849Sscottl switch (sp->policy) { 213136849Sscottl case IPSEC_POLICY_DISCARD: 214136849Sscottl /* 215136849Sscottl * This packet is just discarded. 216136849Sscottl */ 217136849Sscottl ipsec6stat.out_polvio++; 218136849Sscottl ip6stat.ip6s_cantforward++; 219136849Sscottl key_freesp(sp); 220136849Sscottl if (mcopy) { 221136849Sscottl#if 0 222136849Sscottl /* XXX: what icmp ? */ 223136849Sscottl#else 224136849Sscottl m_freem(mcopy); 225136849Sscottl#endif 226136849Sscottl } 227136849Sscottl m_freem(m); 228136849Sscottl return; 229136849Sscottl 230136849Sscottl case IPSEC_POLICY_BYPASS: 231136849Sscottl case IPSEC_POLICY_NONE: 232136849Sscottl /* no need to do IPsec. */ 233136849Sscottl key_freesp(sp); 234149871Sscottl goto skip_ipsec; 235136849Sscottl 236153072Sru case IPSEC_POLICY_IPSEC: 237136849Sscottl if (sp->req == NULL) { 238136849Sscottl /* XXX should be panic ? */ 239136849Sscottl printf("ip6_forward: No IPsec request specified.\n"); 240136849Sscottl ip6stat.ip6s_cantforward++; 241136849Sscottl key_freesp(sp); 242136849Sscottl if (mcopy) { 243136849Sscottl#if 0 244136849Sscottl /* XXX: what icmp ? */ 245136849Sscottl#else 246136849Sscottl m_freem(mcopy); 247136849Sscottl#endif 248149871Sscottl } 249136849Sscottl m_freem(m); 250136849Sscottl return; 251136849Sscottl } 252136849Sscottl /* do IPsec */ 253136849Sscottl break; 254136849Sscottl 255136849Sscottl case IPSEC_POLICY_ENTRUST: 256136849Sscottl default: 257149871Sscottl /* should be panic ?? */ 258149871Sscottl printf("ip6_forward: Invalid policy found. %d\n", sp->policy); 259136849Sscottl key_freesp(sp); 260136849Sscottl goto skip_ipsec; 261136849Sscottl } 262136849Sscottl 263136849Sscottl { 264136849Sscottl struct ipsecrequest *isr = NULL; 265136849Sscottl struct ipsec_output_state state; 266149871Sscottl 267136849Sscottl /* 268136849Sscottl * when the kernel forwards a packet, it is not proper to apply 269136849Sscottl * IPsec transport mode to the packet is not proper. this check 270136849Sscottl * avoid from this. 271136849Sscottl * at present, if there is even a transport mode SA request in the 272136849Sscottl * security policy, the kernel does not apply IPsec to the packet. 273136849Sscottl * this check is not enough because the following case is valid. 274136849Sscottl * ipsec esp/tunnel/xxx-xxx/require esp/transport//require; 275136849Sscottl */ 276136849Sscottl for (isr = sp->req; isr; isr = isr->next) { 277136849Sscottl if (isr->saidx.mode == IPSEC_MODE_ANY) 278136849Sscottl goto doipsectunnel; 279136849Sscottl if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 280149871Sscottl goto doipsectunnel; 281190809Sdelphij } 282190809Sdelphij 283149871Sscottl /* 284136849Sscottl * if there's no need for tunnel mode IPsec, skip. 285136849Sscottl */ 286136849Sscottl if (!isr) 287136849Sscottl goto skip_ipsec; 288149871Sscottl 289136849Sscottl doipsectunnel: 290136849Sscottl /* 291136849Sscottl * All the extension headers will become inaccessible 292136849Sscottl * (since they can be encrypted). 293136849Sscottl * Don't panic, we need no more updates to extension headers 294136849Sscottl * on inner IPv6 packet (since they are now encapsulated). 295136849Sscottl * 296136849Sscottl * IPv6 [ESP|AH] IPv6 [extension headers] payload 297136849Sscottl */ 298136849Sscottl bzero(&state, sizeof(state)); 299136849Sscottl state.m = m; 300136849Sscottl state.ro = NULL; /* update at ipsec6_output_tunnel() */ 301136849Sscottl state.dst = NULL; /* update at ipsec6_output_tunnel() */ 302136849Sscottl 303136849Sscottl error = ipsec6_output_tunnel(&state, sp, 0); 304136849Sscottl 305149871Sscottl m = state.m; 306136849Sscottl key_freesp(sp); 307136849Sscottl 308136849Sscottl if (error) { 309136849Sscottl /* mbuf is already reclaimed in ipsec6_output_tunnel. */ 310149871Sscottl switch (error) { 311149871Sscottl case EHOSTUNREACH: 312149871Sscottl case ENETUNREACH: 313136849Sscottl case EMSGSIZE: 314136849Sscottl case ENOBUFS: 315149871Sscottl case ENOMEM: 316149871Sscottl break; 317136849Sscottl default: 318149871Sscottl printf("ip6_output (ipsec): error code %d\n", error); 319149871Sscottl /* FALLTHROUGH */ 320136849Sscottl case ENOENT: 321136849Sscottl /* don't show these error codes to the user */ 322136849Sscottl break; 323136849Sscottl } 324136849Sscottl ip6stat.ip6s_cantforward++; 325136849Sscottl if (mcopy) { 326136849Sscottl#if 0 327136849Sscottl /* XXX: what icmp ? */ 328136849Sscottl#else 329136849Sscottl m_freem(mcopy); 330136849Sscottl#endif 331136849Sscottl } 332136849Sscottl m_freem(m); 333136849Sscottl return; 334136849Sscottl } 335136849Sscottl 336136849Sscottl if (ip6 != mtod(m, struct ip6_hdr *)) { 337136849Sscottl /* 338136849Sscottl * now tunnel mode headers are added. we are originating 339136849Sscottl * packet instead of forwarding the packet. 340149871Sscottl */ 341149871Sscottl ip6_output(m, NULL, NULL, IPV6_FORWARDING/*XXX*/, NULL, NULL, 342136849Sscottl NULL); 343136849Sscottl goto freecopy; 344136849Sscottl } 345136849Sscottl 346136849Sscottl /* adjust pointer */ 347136849Sscottl dst = (struct sockaddr_in6 *)state.dst; 348136849Sscottl rt = state.ro ? state.ro->ro_rt : NULL; 349136849Sscottl if (dst != NULL && rt != NULL) 350136849Sscottl ipsecrt = 1; 351136849Sscottl } 352149871Sscottl skip_ipsec: 353149871Sscottl#endif /* IPSEC */ 354136849Sscottl 355136849Sscottl#ifdef IPSEC 356136849Sscottl if (ipsecrt) 357136849Sscottl goto skip_routing; 358136849Sscottl#endif 359136849Sscottl 360136849Sscottl dst = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; 361136849Sscottl if (!srcrt) { 362136849Sscottl /* ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst */ 363136849Sscottl if (ip6_forward_rt.ro_rt == 0 || 364136849Sscottl (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) { 365136849Sscottl if (ip6_forward_rt.ro_rt) { 366136849Sscottl RTFREE(ip6_forward_rt.ro_rt); 367136849Sscottl ip6_forward_rt.ro_rt = 0; 368136849Sscottl } 369136849Sscottl 370136849Sscottl /* this probably fails but give it a try again */ 371149871Sscottl rtalloc((struct route *)&ip6_forward_rt); 372149871Sscottl } 373136849Sscottl 374149871Sscottl if (ip6_forward_rt.ro_rt == 0) { 375149871Sscottl ip6stat.ip6s_noroute++; 376149871Sscottl in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute); 377136849Sscottl if (mcopy) { 378149871Sscottl icmp6_error(mcopy, ICMP6_DST_UNREACH, 379149871Sscottl ICMP6_DST_UNREACH_NOROUTE, 0); 380149871Sscottl } 381149871Sscottl m_freem(m); 382149871Sscottl return; 383149871Sscottl } 384136849Sscottl } else if ((rt = ip6_forward_rt.ro_rt) == 0 || 385149871Sscottl !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) { 386149871Sscottl if (ip6_forward_rt.ro_rt) { 387136849Sscottl RTFREE(ip6_forward_rt.ro_rt); 388149871Sscottl ip6_forward_rt.ro_rt = 0; 389149871Sscottl } 390149871Sscottl bzero(dst, sizeof(*dst)); 391136849Sscottl dst->sin6_len = sizeof(struct sockaddr_in6); 392136849Sscottl dst->sin6_family = AF_INET6; 393149871Sscottl dst->sin6_addr = ip6->ip6_dst; 394136849Sscottl 395136849Sscottl rtalloc((struct route *)&ip6_forward_rt); 396136849Sscottl if (ip6_forward_rt.ro_rt == 0) { 397149871Sscottl ip6stat.ip6s_noroute++; 398136849Sscottl in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute); 399136849Sscottl if (mcopy) { 400136849Sscottl icmp6_error(mcopy, ICMP6_DST_UNREACH, 401149871Sscottl ICMP6_DST_UNREACH_NOROUTE, 0); 402149871Sscottl } 403136849Sscottl m_freem(m); 404136849Sscottl return; 405136849Sscottl } 406136849Sscottl } 407136849Sscottl rt = ip6_forward_rt.ro_rt; 408136849Sscottl#ifdef IPSEC 409136849Sscottl skip_routing:; 410136849Sscottl#endif 411136849Sscottl 412136849Sscottl /* 413136849Sscottl * Source scope check: if a packet can't be delivered to its 414136849Sscottl * destination for the reason that the destination is beyond the scope 415136849Sscottl * of the source address, discard the packet and return an icmp6 416136849Sscottl * destination unreachable error with Code 2 (beyond scope of source 417136849Sscottl * address). We use a local copy of ip6_src, since in6_setscope() 418136849Sscottl * will possibly modify its first argument. 419136849Sscottl * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1] 420136849Sscottl */ 421136849Sscottl src_in6 = ip6->ip6_src; 422136849Sscottl if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) { 423136849Sscottl /* XXX: this should not happen */ 424136849Sscottl ip6stat.ip6s_cantforward++; 425136849Sscottl ip6stat.ip6s_badscope++; 426136849Sscottl m_freem(m); 427136849Sscottl return; 428136849Sscottl } 429136849Sscottl if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) { 430136849Sscottl ip6stat.ip6s_cantforward++; 431136849Sscottl ip6stat.ip6s_badscope++; 432136849Sscottl m_freem(m); 433136849Sscottl return; 434136849Sscottl } 435136849Sscottl if (inzone != outzone 436136849Sscottl#ifdef IPSEC 437136849Sscottl && !ipsecrt 438136849Sscottl#endif 439136849Sscottl ) { 440136849Sscottl ip6stat.ip6s_cantforward++; 441136849Sscottl ip6stat.ip6s_badscope++; 442136849Sscottl in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard); 443136849Sscottl 444136849Sscottl if (ip6_log_time + ip6_log_interval < time_second) { 445136849Sscottl ip6_log_time = time_second; 446136849Sscottl log(LOG_DEBUG, 447136849Sscottl "cannot forward " 448136849Sscottl "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 449136849Sscottl ip6_sprintf(&ip6->ip6_src), 450136849Sscottl ip6_sprintf(&ip6->ip6_dst), 451136849Sscottl ip6->ip6_nxt, 452136849Sscottl if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp)); 453136849Sscottl } 454190809Sdelphij if (mcopy) 455149871Sscottl icmp6_error(mcopy, ICMP6_DST_UNREACH, 456190809Sdelphij ICMP6_DST_UNREACH_BEYONDSCOPE, 0); 457136849Sscottl m_freem(m); 458136849Sscottl return; 459136849Sscottl } 460136849Sscottl 461190809Sdelphij /* 462136849Sscottl * Destination scope check: if a packet is going to break the scope 463136849Sscottl * zone of packet's destination address, discard it. This case should 464136849Sscottl * usually be prevented by appropriately-configured routing table, but 465136849Sscottl * we need an explicit check because we may mistakenly forward the 466136849Sscottl * packet to a different zone by (e.g.) a default route. 467136849Sscottl */ 468136849Sscottl dst_in6 = ip6->ip6_dst; 469136849Sscottl if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 || 470136849Sscottl in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 || 471136849Sscottl inzone != outzone) { 472136849Sscottl ip6stat.ip6s_cantforward++; 473136849Sscottl ip6stat.ip6s_badscope++; 474136849Sscottl m_freem(m); 475136849Sscottl return; 476190809Sdelphij } 477136849Sscottl 478136849Sscottl if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) { 479136849Sscottl in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig); 480149871Sscottl if (mcopy) { 481149871Sscottl u_long mtu; 482136849Sscottl#ifdef IPSEC 483136849Sscottl struct secpolicy *sp; 484136849Sscottl int ipsecerror; 485136849Sscottl size_t ipsechdrsiz; 486136849Sscottl#endif 487136849Sscottl 488136849Sscottl mtu = IN6_LINKMTU(rt->rt_ifp); 489136849Sscottl#ifdef IPSEC 490136849Sscottl /* 491136849Sscottl * When we do IPsec tunnel ingress, we need to play 492136849Sscottl * with the link value (decrement IPsec header size 493136849Sscottl * from mtu value). The code is much simpler than v4 494136849Sscottl * case, as we have the outgoing interface for 495136849Sscottl * encapsulated packet as "rt->rt_ifp". 496136849Sscottl */ 497136849Sscottl sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND, 498136849Sscottl IP_FORWARDING, &ipsecerror); 499136849Sscottl if (sp) { 500136849Sscottl ipsechdrsiz = ipsec6_hdrsiz(mcopy, 501136849Sscottl IPSEC_DIR_OUTBOUND, NULL); 502136849Sscottl if (ipsechdrsiz < mtu) 503136849Sscottl mtu -= ipsechdrsiz; 504136849Sscottl } 505136849Sscottl 506136849Sscottl /* 507136849Sscottl * if mtu becomes less than minimum MTU, 508136849Sscottl * tell minimum MTU (and I'll need to fragment it). 509136849Sscottl */ 510136849Sscottl if (mtu < IPV6_MMTU) 511136849Sscottl mtu = IPV6_MMTU; 512136849Sscottl#endif 513136849Sscottl icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu); 514136849Sscottl } 515136849Sscottl m_freem(m); 516136849Sscottl return; 517136849Sscottl } 518136849Sscottl 519136849Sscottl if (rt->rt_flags & RTF_GATEWAY) 520136849Sscottl dst = (struct sockaddr_in6 *)rt->rt_gateway; 521136849Sscottl 522136849Sscottl /* 523136849Sscottl * If we are to forward the packet using the same interface 524136849Sscottl * as one we got the packet from, perhaps we should send a redirect 525136849Sscottl * to sender to shortcut a hop. 526136849Sscottl * Only send redirect if source is sending directly to us, 527136849Sscottl * and if packet was not source routed (or has any options). 528136849Sscottl * Also, don't send redirect if forwarding using a route 529136849Sscottl * modified by a redirect. 530136849Sscottl */ 531136849Sscottl if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && 532136849Sscottl#ifdef IPSEC 533136849Sscottl !ipsecrt && 534136849Sscottl#endif 535136849Sscottl (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) { 536136849Sscottl if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) { 537136849Sscottl /* 538136849Sscottl * If the incoming interface is equal to the outgoing 539136849Sscottl * one, and the link attached to the interface is 540136849Sscottl * point-to-point, then it will be highly probable 541136849Sscottl * that a routing loop occurs. Thus, we immediately 542136849Sscottl * drop the packet and send an ICMPv6 error message. 543136849Sscottl * 544136849Sscottl * type/code is based on suggestion by Rich Draves. 545136849Sscottl * not sure if it is the best pick. 546136849Sscottl */ 547136849Sscottl icmp6_error(mcopy, ICMP6_DST_UNREACH, 548136849Sscottl ICMP6_DST_UNREACH_ADDR, 0); 549136849Sscottl m_freem(m); 550136849Sscottl return; 551136849Sscottl } 552136849Sscottl type = ND_REDIRECT; 553149871Sscottl } 554149871Sscottl 555136849Sscottl /* 556136849Sscottl * Check with the firewall... 557136849Sscottl */ 558136849Sscottl if (ip6_fw_enable && ip6_fw_chk_ptr) { 559136849Sscottl u_short port = 0; 560136849Sscottl /* If ipfw says divert, we have to just drop packet */ 561136849Sscottl if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) { 562136849Sscottl m_freem(m); 563136849Sscottl goto freecopy; 564136849Sscottl } 565136849Sscottl if (!m) 566136849Sscottl goto freecopy; 567136849Sscottl } 568136849Sscottl 569136849Sscottl /* 570136849Sscottl * Fake scoped addresses. Note that even link-local source or 571149871Sscottl * destinaion can appear, if the originating node just sends the 572149871Sscottl * packet to us (without address resolution for the destination). 573136849Sscottl * Since both icmp6_error and icmp6_redirect_output fill the embedded 574136849Sscottl * link identifiers, we can do this stuff after making a copy for 575136849Sscottl * returning an error. 576136849Sscottl */ 577136849Sscottl if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 578136849Sscottl /* 579136849Sscottl * See corresponding comments in ip6_output. 580136849Sscottl * XXX: but is it possible that ip6_forward() sends a packet 581136849Sscottl * to a loopback interface? I don't think so, and thus 582136849Sscottl * I bark here. (jinmei@kame.net) 583136849Sscottl * XXX: it is common to route invalid packets to loopback. 584136849Sscottl * also, the codepath will be visited on use of ::1 in 585136849Sscottl * rthdr. (itojun) 586136849Sscottl */ 587136849Sscottl#if 1 588136849Sscottl if (0) 589136849Sscottl#else 590136849Sscottl if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0) 591136849Sscottl#endif 592136849Sscottl { 593136849Sscottl printf("ip6_forward: outgoing interface is loopback. " 594136849Sscottl "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 595136849Sscottl ip6_sprintf(&ip6->ip6_src), 596136849Sscottl ip6_sprintf(&ip6->ip6_dst), 597136849Sscottl ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif), 598136849Sscottl if_name(rt->rt_ifp)); 599136849Sscottl } 600136849Sscottl 601136849Sscottl /* we can just use rcvif in forwarding. */ 602136849Sscottl origifp = m->m_pkthdr.rcvif; 603136849Sscottl } 604136849Sscottl else 605136849Sscottl origifp = rt->rt_ifp; 606136849Sscottl /* 607136849Sscottl * clear embedded scope identifiers if necessary. 608136849Sscottl * in6_clearscope will touch the addresses only when necessary. 609136849Sscottl */ 610136849Sscottl in6_clearscope(&ip6->ip6_src); 611136849Sscottl in6_clearscope(&ip6->ip6_dst); 612136849Sscottl 613136849Sscottl /* Jump over all PFIL processing if hooks are not active. */ 614136849Sscottl if (!PFIL_HOOKED(&inet6_pfil_hook)) 615136849Sscottl goto pass; 616136849Sscottl 617136849Sscottl /* Run through list of hooks for output packets. */ 618149871Sscottl error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT, NULL); 619136849Sscottl if (error != 0) 620136849Sscottl goto senderr; 621136849Sscottl if (m == NULL) 622136849Sscottl goto freecopy; 623136849Sscottl ip6 = mtod(m, struct ip6_hdr *); 624136849Sscottl 625136849Sscottlpass: 626 error = nd6_output(rt->rt_ifp, origifp, m, dst, rt); 627 if (error) { 628 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard); 629 ip6stat.ip6s_cantforward++; 630 } else { 631 ip6stat.ip6s_forward++; 632 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward); 633 if (type) 634 ip6stat.ip6s_redirectsent++; 635 else { 636 if (mcopy) 637 goto freecopy; 638 } 639 } 640 641senderr: 642 if (mcopy == NULL) 643 return; 644 switch (error) { 645 case 0: 646 if (type == ND_REDIRECT) { 647 icmp6_redirect_output(mcopy, rt); 648 return; 649 } 650 goto freecopy; 651 652 case EMSGSIZE: 653 /* xxx MTU is constant in PPP? */ 654 goto freecopy; 655 656 case ENOBUFS: 657 /* Tell source to slow down like source quench in IP? */ 658 goto freecopy; 659 660 case ENETUNREACH: /* shouldn't happen, checked above */ 661 case EHOSTUNREACH: 662 case ENETDOWN: 663 case EHOSTDOWN: 664 default: 665 type = ICMP6_DST_UNREACH; 666 code = ICMP6_DST_UNREACH_ADDR; 667 break; 668 } 669 icmp6_error(mcopy, type, code, 0); 670 return; 671 672 freecopy: 673 m_freem(mcopy); 674 return; 675} 676