in6_gif.c revision 243882
1139826Simp/*- 254263Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 354263Sshin * All rights reserved. 454263Sshin * 554263Sshin * Redistribution and use in source and binary forms, with or without 654263Sshin * modification, are permitted provided that the following conditions 754263Sshin * are met: 854263Sshin * 1. Redistributions of source code must retain the above copyright 954263Sshin * notice, this list of conditions and the following disclaimer. 1054263Sshin * 2. Redistributions in binary form must reproduce the above copyright 1154263Sshin * notice, this list of conditions and the following disclaimer in the 1254263Sshin * documentation and/or other materials provided with the distribution. 1354263Sshin * 3. Neither the name of the project nor the names of its contributors 1454263Sshin * may be used to endorse or promote products derived from this software 1554263Sshin * without specific prior written permission. 1654263Sshin * 1754263Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1854263Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1954263Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2054263Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2154263Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2254263Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2354263Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2454263Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2554263Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2654263Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2754263Sshin * SUCH DAMAGE. 28174510Sobrien * 29174510Sobrien * $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $ 3054263Sshin */ 3154263Sshin 32174510Sobrien#include <sys/cdefs.h> 33174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/in6_gif.c 243882 2012-12-05 08:04:20Z glebius $"); 34174510Sobrien 3554263Sshin#include "opt_inet.h" 3662587Sitojun#include "opt_inet6.h" 3754263Sshin 3854263Sshin#include <sys/param.h> 3954263Sshin#include <sys/systm.h> 4054263Sshin#include <sys/socket.h> 4154263Sshin#include <sys/sockio.h> 4254263Sshin#include <sys/mbuf.h> 4354263Sshin#include <sys/errno.h> 44207369Sbz#include <sys/kernel.h> 4578064Sume#include <sys/queue.h> 4678064Sume#include <sys/syslog.h> 47207369Sbz#include <sys/sysctl.h> 48105293Sume#include <sys/protosw.h> 4978064Sume#include <sys/malloc.h> 5078064Sume 5154263Sshin#include <net/if.h> 5254263Sshin#include <net/route.h> 5354263Sshin 5454263Sshin#include <netinet/in.h> 5554263Sshin#include <netinet/in_systm.h> 5654263Sshin#ifdef INET 5754263Sshin#include <netinet/ip.h> 5854263Sshin#endif 5962587Sitojun#include <netinet/ip_encap.h> 6062587Sitojun#ifdef INET6 6162587Sitojun#include <netinet/ip6.h> 6254263Sshin#include <netinet6/ip6_var.h> 6354263Sshin#include <netinet6/in6_gif.h> 6462587Sitojun#include <netinet6/in6_var.h> 6562587Sitojun#endif 66105293Sume#include <netinet6/ip6protosw.h> 6755009Sshin#include <netinet/ip_ecn.h> 6862587Sitojun#ifdef INET6 6955009Sshin#include <netinet6/ip6_ecn.h> 7062587Sitojun#endif 7154263Sshin 7254263Sshin#include <net/if_gif.h> 7354263Sshin 74207369SbzVNET_DEFINE(int, ip6_gif_hlim) = GIF_HLIM; 75207369Sbz#define V_ip6_gif_hlim VNET(ip6_gif_hlim) 76207369Sbz 77207369SbzSYSCTL_DECL(_net_inet6_ip6); 78207369SbzSYSCTL_VNET_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim, CTLFLAG_RW, 79207369Sbz &VNET_NAME(ip6_gif_hlim), 0, ""); 80207369Sbz 81105293Sumestatic int gif_validate6(const struct ip6_hdr *, struct gif_softc *, 82105293Sume struct ifnet *); 83105293Sume 84105293Sumeextern struct domain inet6domain; 85186791Sbzstruct ip6protosw in6_gif_protosw = { 86186791Sbz .pr_type = SOCK_RAW, 87186791Sbz .pr_domain = &inet6domain, 88186791Sbz .pr_protocol = 0, /* IPPROTO_IPV[46] */ 89186791Sbz .pr_flags = PR_ATOMIC|PR_ADDR, 90186791Sbz .pr_input = in6_gif_input, 91186791Sbz .pr_output = rip6_output, 92186791Sbz .pr_ctloutput = rip6_ctloutput, 93186791Sbz .pr_usrreqs = &rip6_usrreqs 94105293Sume}; 95105293Sume 9654263Sshinint 97171259Sdelphijin6_gif_output(struct ifnet *ifp, 98171259Sdelphij int family, /* family of the packet to be encapsulate */ 99171259Sdelphij struct mbuf *m) 10054263Sshin{ 101147256Sbrooks struct gif_softc *sc = ifp->if_softc; 10254263Sshin struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst; 10354263Sshin struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc; 10454263Sshin struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst; 10554263Sshin struct ip6_hdr *ip6; 106153621Sthompsa struct etherip_header eiphdr; 107189494Smarius int error, len, proto; 10855009Sshin u_int8_t itos, otos; 10954263Sshin 110155037Sglebius GIF_LOCK_ASSERT(sc); 111155037Sglebius 11254263Sshin if (sin6_src == NULL || sin6_dst == NULL || 11354263Sshin sin6_src->sin6_family != AF_INET6 || 11454263Sshin sin6_dst->sin6_family != AF_INET6) { 11554263Sshin m_freem(m); 11654263Sshin return EAFNOSUPPORT; 11754263Sshin } 11854263Sshin 11954263Sshin switch (family) { 12054263Sshin#ifdef INET 12154263Sshin case AF_INET: 12254263Sshin { 12354263Sshin struct ip *ip; 12454263Sshin 12554263Sshin proto = IPPROTO_IPV4; 12654263Sshin if (m->m_len < sizeof(*ip)) { 12754263Sshin m = m_pullup(m, sizeof(*ip)); 12854263Sshin if (!m) 12954263Sshin return ENOBUFS; 13054263Sshin } 13154263Sshin ip = mtod(m, struct ip *); 13255009Sshin itos = ip->ip_tos; 13354263Sshin break; 13454263Sshin } 13554263Sshin#endif 13662587Sitojun#ifdef INET6 13754263Sshin case AF_INET6: 13854263Sshin { 13954263Sshin struct ip6_hdr *ip6; 14054263Sshin proto = IPPROTO_IPV6; 14154263Sshin if (m->m_len < sizeof(*ip6)) { 14254263Sshin m = m_pullup(m, sizeof(*ip6)); 14354263Sshin if (!m) 14454263Sshin return ENOBUFS; 14554263Sshin } 14654263Sshin ip6 = mtod(m, struct ip6_hdr *); 14755009Sshin itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 14854263Sshin break; 14954263Sshin } 15062587Sitojun#endif 151153621Sthompsa case AF_LINK: 152171260Sdelphij proto = IPPROTO_ETHERIP; 153193664Shrs 154193664Shrs /* 155193664Shrs * GIF_SEND_REVETHIP (disabled by default) intentionally 156193664Shrs * sends an EtherIP packet with revered version field in 157193664Shrs * the header. This is a knob for backward compatibility 158193664Shrs * with FreeBSD 7.2R or prior. 159193664Shrs */ 160193664Shrs if ((sc->gif_options & GIF_SEND_REVETHIP)) { 161193664Shrs eiphdr.eip_ver = 0; 162193664Shrs eiphdr.eip_resvl = ETHERIP_VERSION; 163193664Shrs eiphdr.eip_resvh = 0; 164193664Shrs } else { 165193664Shrs eiphdr.eip_ver = ETHERIP_VERSION; 166193664Shrs eiphdr.eip_resvl = 0; 167193664Shrs eiphdr.eip_resvh = 0; 168193664Shrs } 169171260Sdelphij /* prepend Ethernet-in-IP header */ 170243882Sglebius M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT); 171171260Sdelphij if (m && m->m_len < sizeof(struct etherip_header)) 172171260Sdelphij m = m_pullup(m, sizeof(struct etherip_header)); 173171260Sdelphij if (m == NULL) 174171260Sdelphij return ENOBUFS; 175171260Sdelphij bcopy(&eiphdr, mtod(m, struct etherip_header *), 176153621Sthompsa sizeof(struct etherip_header)); 177153621Sthompsa break; 178153621Sthompsa 17954263Sshin default: 18062587Sitojun#ifdef DEBUG 18154263Sshin printf("in6_gif_output: warning: unknown family %d passed\n", 18254263Sshin family); 18354263Sshin#endif 18454263Sshin m_freem(m); 18554263Sshin return EAFNOSUPPORT; 18654263Sshin } 187120913Sume 18854263Sshin /* prepend new IP header */ 189189494Smarius len = sizeof(struct ip6_hdr); 190189494Smarius#ifndef __NO_STRICT_ALIGNMENT 191189494Smarius if (family == AF_LINK) 192189494Smarius len += ETHERIP_ALIGN; 193189494Smarius#endif 194243882Sglebius M_PREPEND(m, len, M_NOWAIT); 195189494Smarius if (m != NULL && m->m_len < len) 196189494Smarius m = m_pullup(m, len); 19754263Sshin if (m == NULL) { 19854263Sshin printf("ENOBUFS in in6_gif_output %d\n", __LINE__); 19954263Sshin return ENOBUFS; 20054263Sshin } 201189494Smarius#ifndef __NO_STRICT_ALIGNMENT 202189494Smarius if (family == AF_LINK) { 203189494Smarius len = mtod(m, vm_offset_t) & 3; 204189494Smarius KASSERT(len == 0 || len == ETHERIP_ALIGN, 205189494Smarius ("in6_gif_output: unexpected misalignment")); 206189494Smarius m->m_data += len; 207189494Smarius m->m_len -= ETHERIP_ALIGN; 208189494Smarius } 209189494Smarius#endif 21054263Sshin 21154263Sshin ip6 = mtod(m, struct ip6_hdr *); 21254263Sshin ip6->ip6_flow = 0; 21362587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 21462587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 21554263Sshin ip6->ip6_plen = htons((u_short)m->m_pkthdr.len); 21654263Sshin ip6->ip6_nxt = proto; 217181803Sbz ip6->ip6_hlim = V_ip6_gif_hlim; 21854263Sshin ip6->ip6_src = sin6_src->sin6_addr; 21978064Sume /* bidirectional configured tunnel mode */ 22078064Sume if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr)) 22178064Sume ip6->ip6_dst = sin6_dst->sin6_addr; 22278064Sume else { 22378064Sume m_freem(m); 22478064Sume return ENETUNREACH; 22554263Sshin } 226121684Sume ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE, 227121684Sume &otos, &itos); 228121684Sume ip6->ip6_flow &= ~htonl(0xff << 20); 22978064Sume ip6->ip6_flow |= htonl((u_int32_t)otos << 20); 23054263Sshin 231231852Sbz M_SETFIB(m, sc->gif_fibnum); 232231852Sbz 23354263Sshin if (dst->sin6_family != sin6_dst->sin6_family || 23454263Sshin !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) { 23554263Sshin /* cache route doesn't match */ 23654263Sshin bzero(dst, sizeof(*dst)); 23754263Sshin dst->sin6_family = sin6_dst->sin6_family; 23854263Sshin dst->sin6_len = sizeof(struct sockaddr_in6); 23954263Sshin dst->sin6_addr = sin6_dst->sin6_addr; 24054263Sshin if (sc->gif_ro6.ro_rt) { 24154263Sshin RTFREE(sc->gif_ro6.ro_rt); 24254263Sshin sc->gif_ro6.ro_rt = NULL; 24354263Sshin } 24462587Sitojun#if 0 245147256Sbrooks GIF2IFP(sc)->if_mtu = GIF_MTU; 24662587Sitojun#endif 24754263Sshin } 24854263Sshin 24954263Sshin if (sc->gif_ro6.ro_rt == NULL) { 250231852Sbz in6_rtalloc(&sc->gif_ro6, sc->gif_fibnum); 25154263Sshin if (sc->gif_ro6.ro_rt == NULL) { 25254263Sshin m_freem(m); 25354263Sshin return ENETUNREACH; 25454263Sshin } 25562587Sitojun 25662587Sitojun /* if it constitutes infinite encapsulation, punt. */ 25762587Sitojun if (sc->gif_ro.ro_rt->rt_ifp == ifp) { 25862587Sitojun m_freem(m); 25962587Sitojun return ENETUNREACH; /*XXX*/ 26062587Sitojun } 26162587Sitojun#if 0 26262587Sitojun ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu 26362587Sitojun - sizeof(struct ip6_hdr); 26462587Sitojun#endif 26554263Sshin } 266120913Sume 26762587Sitojun#ifdef IPV6_MINMTU 26862587Sitojun /* 26962587Sitojun * force fragmentation to minimum MTU, to avoid path MTU discovery. 27062587Sitojun * it is too painful to ask for resend of inner packet, to achieve 27162587Sitojun * path MTU discovery for encapsulated packets. 27262587Sitojun */ 273138620Sglebius error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL); 27462587Sitojun#else 275138620Sglebius error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL); 27662587Sitojun#endif 277138619Sglebius 278147256Sbrooks if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) && 279138653Sglebius sc->gif_ro6.ro_rt != NULL) { 280138619Sglebius RTFREE(sc->gif_ro6.ro_rt); 281138619Sglebius sc->gif_ro6.ro_rt = NULL; 282138619Sglebius } 283138619Sglebius 284138619Sglebius return (error); 28554263Sshin} 28654263Sshin 287120913Sumeint 288171259Sdelphijin6_gif_input(struct mbuf **mp, int *offp, int proto) 28954263Sshin{ 29054263Sshin struct mbuf *m = *mp; 29154263Sshin struct ifnet *gifp = NULL; 292147507Sume struct gif_softc *sc; 29354263Sshin struct ip6_hdr *ip6; 29454263Sshin int af = 0; 29555009Sshin u_int32_t otos; 29654263Sshin 29754263Sshin ip6 = mtod(m, struct ip6_hdr *); 29854263Sshin 299147507Sume sc = (struct gif_softc *)encap_getarg(m); 300147507Sume if (sc == NULL) { 301147507Sume m_freem(m); 302181803Sbz V_ip6stat.ip6s_nogif++; 303147507Sume return IPPROTO_DONE; 304147507Sume } 30554263Sshin 306147507Sume gifp = GIF2IFP(sc); 30762587Sitojun if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) { 30854263Sshin m_freem(m); 309181803Sbz V_ip6stat.ip6s_nogif++; 31054263Sshin return IPPROTO_DONE; 31154263Sshin } 31262587Sitojun 31355009Sshin otos = ip6->ip6_flow; 31454263Sshin m_adj(m, *offp); 31554263Sshin 31654263Sshin switch (proto) { 31754263Sshin#ifdef INET 31854263Sshin case IPPROTO_IPV4: 31954263Sshin { 32054263Sshin struct ip *ip; 32155009Sshin u_int8_t otos8; 32254263Sshin af = AF_INET; 32355009Sshin otos8 = (ntohl(otos) >> 20) & 0xff; 32454263Sshin if (m->m_len < sizeof(*ip)) { 32554263Sshin m = m_pullup(m, sizeof(*ip)); 32654263Sshin if (!m) 32754263Sshin return IPPROTO_DONE; 32854263Sshin } 32954263Sshin ip = mtod(m, struct ip *); 330121684Sume if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ? 331121684Sume ECN_ALLOWED : ECN_NOCARE, 332121684Sume &otos8, &ip->ip_tos) == 0) { 333121684Sume m_freem(m); 334121684Sume return IPPROTO_DONE; 335121684Sume } 33654263Sshin break; 33754263Sshin } 33854263Sshin#endif /* INET */ 33962587Sitojun#ifdef INET6 34054263Sshin case IPPROTO_IPV6: 34154263Sshin { 34254263Sshin struct ip6_hdr *ip6; 34354263Sshin af = AF_INET6; 34454263Sshin if (m->m_len < sizeof(*ip6)) { 34554263Sshin m = m_pullup(m, sizeof(*ip6)); 34654263Sshin if (!m) 34754263Sshin return IPPROTO_DONE; 34854263Sshin } 34954263Sshin ip6 = mtod(m, struct ip6_hdr *); 350121684Sume if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ? 351121684Sume ECN_ALLOWED : ECN_NOCARE, 352121684Sume &otos, &ip6->ip6_flow) == 0) { 353121684Sume m_freem(m); 354121684Sume return IPPROTO_DONE; 355121684Sume } 35654263Sshin break; 35754263Sshin } 35862587Sitojun#endif 359171260Sdelphij case IPPROTO_ETHERIP: 360171260Sdelphij af = AF_LINK; 361171260Sdelphij break; 362153621Sthompsa 36354263Sshin default: 364181803Sbz V_ip6stat.ip6s_nogif++; 36554263Sshin m_freem(m); 36654263Sshin return IPPROTO_DONE; 36754263Sshin } 368120913Sume 36954263Sshin gif_input(m, af, gifp); 37054263Sshin return IPPROTO_DONE; 37154263Sshin} 37262587Sitojun 37362587Sitojun/* 374105293Sume * validate outer address. 37562587Sitojun */ 376105293Sumestatic int 377171259Sdelphijgif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc, 378171259Sdelphij struct ifnet *ifp) 37962587Sitojun{ 38062587Sitojun struct sockaddr_in6 *src, *dst; 38162587Sitojun 38262587Sitojun src = (struct sockaddr_in6 *)sc->gif_psrc; 38362587Sitojun dst = (struct sockaddr_in6 *)sc->gif_pdst; 38462587Sitojun 385105293Sume /* 386105293Sume * Check for address match. Note that the check is for an incoming 387105293Sume * packet. We should compare the *source* address in our configuration 388105293Sume * and the *destination* address of the packet, and vice versa. 389105293Sume */ 390105293Sume if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) || 391105293Sume !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src)) 39262587Sitojun return 0; 39362587Sitojun 39462587Sitojun /* martian filters on outer source - done in ip6_input */ 39562587Sitojun 39662587Sitojun /* ingress filters on outer source */ 397147256Sbrooks if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) { 39862587Sitojun struct sockaddr_in6 sin6; 39962587Sitojun struct rtentry *rt; 40062587Sitojun 40162587Sitojun bzero(&sin6, sizeof(sin6)); 40262587Sitojun sin6.sin6_family = AF_INET6; 40362587Sitojun sin6.sin6_len = sizeof(struct sockaddr_in6); 404105293Sume sin6.sin6_addr = ip6->ip6_src; 405105293Sume sin6.sin6_scope_id = 0; /* XXX */ 406105293Sume 407231852Sbz rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL, 408231852Sbz sc->gif_fibnum); 409105293Sume if (!rt || rt->rt_ifp != ifp) { 41078064Sume#if 0 411165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 41278064Sume log(LOG_WARNING, "%s: packet from %s dropped " 413147256Sbrooks "due to ingress filter\n", if_name(GIF2IFP(sc)), 414165118Sbz ip6_sprintf(ip6buf, &sin6.sin6_addr)); 41578064Sume#endif 41678064Sume if (rt) 417187946Sbz RTFREE_LOCKED(rt); 41862587Sitojun return 0; 41962587Sitojun } 420187946Sbz RTFREE_LOCKED(rt); 42162587Sitojun } 42262587Sitojun 42378064Sume return 128 * 2; 42462587Sitojun} 425105293Sume 426105293Sume/* 427105293Sume * we know that we are in IFF_UP, outer address available, and outer family 428105293Sume * matched the physical addr family. see gif_encapcheck(). 429105293Sume * sanity check for arg should have been done in the caller. 430105293Sume */ 431105293Sumeint 432171259Sdelphijgif_encapcheck6(const struct mbuf *m, int off, int proto, void *arg) 433105293Sume{ 434105293Sume struct ip6_hdr ip6; 435105293Sume struct gif_softc *sc; 436105293Sume struct ifnet *ifp; 437105293Sume 438105293Sume /* sanity check done in caller */ 439105293Sume sc = (struct gif_softc *)arg; 440105293Sume 441105293Sume /* LINTED const cast */ 442105293Sume m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6); 443105293Sume ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL; 444105293Sume 445105293Sume return gif_validate6(&ip6, sc, ifp); 446105293Sume} 447105293Sume 448105293Sumeint 449171259Sdelphijin6_gif_attach(struct gif_softc *sc) 450105293Sume{ 451105293Sume sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck, 452155333Sume (void *)&in6_gif_protosw, sc); 453105293Sume if (sc->encap_cookie6 == NULL) 454105293Sume return EEXIST; 455105293Sume return 0; 456105293Sume} 457105293Sume 458105293Sumeint 459171259Sdelphijin6_gif_detach(struct gif_softc *sc) 460105293Sume{ 461105293Sume int error; 462105293Sume 463105293Sume error = encap_detach(sc->encap_cookie6); 464105293Sume if (error == 0) 465105293Sume sc->encap_cookie6 = NULL; 466105293Sume return error; 467105293Sume} 468