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$"); 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 */ 170171260Sdelphij M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT); 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 194189494Smarius M_PREPEND(m, len, M_DONTWAIT); 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 231232292Sbz M_SETFIB(m, sc->gif_fibnum); 232232292Sbz 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) { 250232292Sbz 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 267223637Sbz m_addr_changed(m); 268223637Sbz 26962587Sitojun#ifdef IPV6_MINMTU 27062587Sitojun /* 27162587Sitojun * force fragmentation to minimum MTU, to avoid path MTU discovery. 27262587Sitojun * it is too painful to ask for resend of inner packet, to achieve 27362587Sitojun * path MTU discovery for encapsulated packets. 27462587Sitojun */ 275138620Sglebius error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL); 27662587Sitojun#else 277138620Sglebius error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL); 27862587Sitojun#endif 279138619Sglebius 280147256Sbrooks if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) && 281138653Sglebius sc->gif_ro6.ro_rt != NULL) { 282138619Sglebius RTFREE(sc->gif_ro6.ro_rt); 283138619Sglebius sc->gif_ro6.ro_rt = NULL; 284138619Sglebius } 285138619Sglebius 286138619Sglebius return (error); 28754263Sshin} 28854263Sshin 289120913Sumeint 290171259Sdelphijin6_gif_input(struct mbuf **mp, int *offp, int proto) 29154263Sshin{ 29254263Sshin struct mbuf *m = *mp; 29354263Sshin struct ifnet *gifp = NULL; 294147507Sume struct gif_softc *sc; 29554263Sshin struct ip6_hdr *ip6; 29654263Sshin int af = 0; 29755009Sshin u_int32_t otos; 29854263Sshin 29954263Sshin ip6 = mtod(m, struct ip6_hdr *); 30054263Sshin 301147507Sume sc = (struct gif_softc *)encap_getarg(m); 302147507Sume if (sc == NULL) { 303147507Sume m_freem(m); 304250044Sae IP6STAT_INC(ip6s_nogif); 305147507Sume return IPPROTO_DONE; 306147507Sume } 30754263Sshin 308147507Sume gifp = GIF2IFP(sc); 30962587Sitojun if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) { 31054263Sshin m_freem(m); 311250044Sae IP6STAT_INC(ip6s_nogif); 31254263Sshin return IPPROTO_DONE; 31354263Sshin } 31462587Sitojun 31555009Sshin otos = ip6->ip6_flow; 31654263Sshin m_adj(m, *offp); 31754263Sshin 31854263Sshin switch (proto) { 31954263Sshin#ifdef INET 32054263Sshin case IPPROTO_IPV4: 32154263Sshin { 32254263Sshin struct ip *ip; 32355009Sshin u_int8_t otos8; 32454263Sshin af = AF_INET; 32555009Sshin otos8 = (ntohl(otos) >> 20) & 0xff; 32654263Sshin if (m->m_len < sizeof(*ip)) { 32754263Sshin m = m_pullup(m, sizeof(*ip)); 32854263Sshin if (!m) 32954263Sshin return IPPROTO_DONE; 33054263Sshin } 33154263Sshin ip = mtod(m, struct ip *); 332121684Sume if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ? 333121684Sume ECN_ALLOWED : ECN_NOCARE, 334121684Sume &otos8, &ip->ip_tos) == 0) { 335121684Sume m_freem(m); 336121684Sume return IPPROTO_DONE; 337121684Sume } 33854263Sshin break; 33954263Sshin } 34054263Sshin#endif /* INET */ 34162587Sitojun#ifdef INET6 34254263Sshin case IPPROTO_IPV6: 34354263Sshin { 34454263Sshin struct ip6_hdr *ip6; 34554263Sshin af = AF_INET6; 34654263Sshin if (m->m_len < sizeof(*ip6)) { 34754263Sshin m = m_pullup(m, sizeof(*ip6)); 34854263Sshin if (!m) 34954263Sshin return IPPROTO_DONE; 35054263Sshin } 35154263Sshin ip6 = mtod(m, struct ip6_hdr *); 352121684Sume if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ? 353121684Sume ECN_ALLOWED : ECN_NOCARE, 354121684Sume &otos, &ip6->ip6_flow) == 0) { 355121684Sume m_freem(m); 356121684Sume return IPPROTO_DONE; 357121684Sume } 35854263Sshin break; 35954263Sshin } 36062587Sitojun#endif 361171260Sdelphij case IPPROTO_ETHERIP: 362171260Sdelphij af = AF_LINK; 363171260Sdelphij break; 364153621Sthompsa 36554263Sshin default: 366250044Sae IP6STAT_INC(ip6s_nogif); 36754263Sshin m_freem(m); 36854263Sshin return IPPROTO_DONE; 36954263Sshin } 370120913Sume 37154263Sshin gif_input(m, af, gifp); 37254263Sshin return IPPROTO_DONE; 37354263Sshin} 37462587Sitojun 37562587Sitojun/* 376105293Sume * validate outer address. 37762587Sitojun */ 378105293Sumestatic int 379171259Sdelphijgif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc, 380171259Sdelphij struct ifnet *ifp) 38162587Sitojun{ 38262587Sitojun struct sockaddr_in6 *src, *dst; 38362587Sitojun 38462587Sitojun src = (struct sockaddr_in6 *)sc->gif_psrc; 38562587Sitojun dst = (struct sockaddr_in6 *)sc->gif_pdst; 38662587Sitojun 387105293Sume /* 388105293Sume * Check for address match. Note that the check is for an incoming 389105293Sume * packet. We should compare the *source* address in our configuration 390105293Sume * and the *destination* address of the packet, and vice versa. 391105293Sume */ 392105293Sume if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) || 393105293Sume !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src)) 39462587Sitojun return 0; 39562587Sitojun 39662587Sitojun /* martian filters on outer source - done in ip6_input */ 39762587Sitojun 39862587Sitojun /* ingress filters on outer source */ 399147256Sbrooks if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) { 40062587Sitojun struct sockaddr_in6 sin6; 40162587Sitojun struct rtentry *rt; 40262587Sitojun 40362587Sitojun bzero(&sin6, sizeof(sin6)); 40462587Sitojun sin6.sin6_family = AF_INET6; 40562587Sitojun sin6.sin6_len = sizeof(struct sockaddr_in6); 406105293Sume sin6.sin6_addr = ip6->ip6_src; 407105293Sume sin6.sin6_scope_id = 0; /* XXX */ 408105293Sume 409232292Sbz rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL, 410232292Sbz sc->gif_fibnum); 411105293Sume if (!rt || rt->rt_ifp != ifp) { 41278064Sume#if 0 413165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 41478064Sume log(LOG_WARNING, "%s: packet from %s dropped " 415147256Sbrooks "due to ingress filter\n", if_name(GIF2IFP(sc)), 416165118Sbz ip6_sprintf(ip6buf, &sin6.sin6_addr)); 41778064Sume#endif 41878064Sume if (rt) 419187946Sbz RTFREE_LOCKED(rt); 42062587Sitojun return 0; 42162587Sitojun } 422187946Sbz RTFREE_LOCKED(rt); 42362587Sitojun } 42462587Sitojun 42578064Sume return 128 * 2; 42662587Sitojun} 427105293Sume 428105293Sume/* 429105293Sume * we know that we are in IFF_UP, outer address available, and outer family 430105293Sume * matched the physical addr family. see gif_encapcheck(). 431105293Sume * sanity check for arg should have been done in the caller. 432105293Sume */ 433105293Sumeint 434171259Sdelphijgif_encapcheck6(const struct mbuf *m, int off, int proto, void *arg) 435105293Sume{ 436105293Sume struct ip6_hdr ip6; 437105293Sume struct gif_softc *sc; 438105293Sume struct ifnet *ifp; 439105293Sume 440105293Sume /* sanity check done in caller */ 441105293Sume sc = (struct gif_softc *)arg; 442105293Sume 443105293Sume /* LINTED const cast */ 444105293Sume m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6); 445105293Sume ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL; 446105293Sume 447105293Sume return gif_validate6(&ip6, sc, ifp); 448105293Sume} 449105293Sume 450105293Sumeint 451171259Sdelphijin6_gif_attach(struct gif_softc *sc) 452105293Sume{ 453105293Sume sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck, 454155333Sume (void *)&in6_gif_protosw, sc); 455105293Sume if (sc->encap_cookie6 == NULL) 456105293Sume return EEXIST; 457105293Sume return 0; 458105293Sume} 459105293Sume 460105293Sumeint 461171259Sdelphijin6_gif_detach(struct gif_softc *sc) 462105293Sume{ 463105293Sume int error; 464105293Sume 465105293Sume error = encap_detach(sc->encap_cookie6); 466105293Sume if (error == 0) 467105293Sume sc->encap_cookie6 = NULL; 468105293Sume return error; 469105293Sume} 470