in6_gif.c revision 165118
162587Sitojun/* $FreeBSD: head/sys/netinet6/in6_gif.c 165118 2006-12-12 12:17:58Z bz $ */ 278064Sume/* $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $ */ 362587Sitojun 4139826Simp/*- 554263Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 654263Sshin * All rights reserved. 754263Sshin * 854263Sshin * Redistribution and use in source and binary forms, with or without 954263Sshin * modification, are permitted provided that the following conditions 1054263Sshin * are met: 1154263Sshin * 1. Redistributions of source code must retain the above copyright 1254263Sshin * notice, this list of conditions and the following disclaimer. 1354263Sshin * 2. Redistributions in binary form must reproduce the above copyright 1454263Sshin * notice, this list of conditions and the following disclaimer in the 1554263Sshin * documentation and/or other materials provided with the distribution. 1654263Sshin * 3. Neither the name of the project nor the names of its contributors 1754263Sshin * may be used to endorse or promote products derived from this software 1854263Sshin * without specific prior written permission. 1954263Sshin * 2054263Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2154263Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2254263Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2354263Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2454263Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2554263Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2654263Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2754263Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2854263Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2954263Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3054263Sshin * SUCH DAMAGE. 3154263Sshin */ 3254263Sshin 3354263Sshin#include "opt_inet.h" 3462587Sitojun#include "opt_inet6.h" 3554263Sshin 3654263Sshin#include <sys/param.h> 3754263Sshin#include <sys/systm.h> 3854263Sshin#include <sys/socket.h> 3954263Sshin#include <sys/sockio.h> 4054263Sshin#include <sys/mbuf.h> 4154263Sshin#include <sys/errno.h> 4278064Sume#include <sys/queue.h> 4378064Sume#include <sys/syslog.h> 44105293Sume#include <sys/protosw.h> 4554263Sshin 4678064Sume#include <sys/malloc.h> 4778064Sume 4854263Sshin#include <net/if.h> 4954263Sshin#include <net/route.h> 5054263Sshin 5154263Sshin#include <netinet/in.h> 5254263Sshin#include <netinet/in_systm.h> 5354263Sshin#ifdef INET 5454263Sshin#include <netinet/ip.h> 5554263Sshin#endif 5662587Sitojun#include <netinet/ip_encap.h> 5762587Sitojun#ifdef INET6 5862587Sitojun#include <netinet/ip6.h> 5954263Sshin#include <netinet6/ip6_var.h> 6054263Sshin#include <netinet6/in6_gif.h> 6162587Sitojun#include <netinet6/in6_var.h> 6262587Sitojun#endif 63105293Sume#include <netinet6/ip6protosw.h> 6455009Sshin#include <netinet/ip_ecn.h> 6562587Sitojun#ifdef INET6 6655009Sshin#include <netinet6/ip6_ecn.h> 6762587Sitojun#endif 6854263Sshin 6954263Sshin#include <net/if_gif.h> 7054263Sshin 71105293Sumestatic int gif_validate6(const struct ip6_hdr *, struct gif_softc *, 72105293Sume struct ifnet *); 73105293Sume 74105293Sumeextern struct domain inet6domain; 75105293Sumestruct ip6protosw in6_gif_protosw = 76120913Sume{ SOCK_RAW, &inet6domain, 0/* IPPROTO_IPV[46] */, PR_ATOMIC|PR_ADDR, 77105293Sume in6_gif_input, rip6_output, 0, rip6_ctloutput, 78105293Sume 0, 79105293Sume 0, 0, 0, 0, 80105293Sume &rip6_usrreqs 81105293Sume}; 82105293Sume 8354263Sshinint 84105340Sumein6_gif_output(ifp, family, m) 8554263Sshin struct ifnet *ifp; 8654263Sshin int family; /* family of the packet to be encapsulate. */ 8754263Sshin struct mbuf *m; 8854263Sshin{ 89147256Sbrooks struct gif_softc *sc = ifp->if_softc; 9054263Sshin struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst; 9154263Sshin struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc; 9254263Sshin struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst; 9354263Sshin struct ip6_hdr *ip6; 94153621Sthompsa struct etherip_header eiphdr; 95138619Sglebius int proto, error; 9655009Sshin u_int8_t itos, otos; 9754263Sshin 98155037Sglebius GIF_LOCK_ASSERT(sc); 99155037Sglebius 10054263Sshin if (sin6_src == NULL || sin6_dst == NULL || 10154263Sshin sin6_src->sin6_family != AF_INET6 || 10254263Sshin sin6_dst->sin6_family != AF_INET6) { 10354263Sshin m_freem(m); 10454263Sshin return EAFNOSUPPORT; 10554263Sshin } 10654263Sshin 10754263Sshin switch (family) { 10854263Sshin#ifdef INET 10954263Sshin case AF_INET: 11054263Sshin { 11154263Sshin struct ip *ip; 11254263Sshin 11354263Sshin proto = IPPROTO_IPV4; 11454263Sshin if (m->m_len < sizeof(*ip)) { 11554263Sshin m = m_pullup(m, sizeof(*ip)); 11654263Sshin if (!m) 11754263Sshin return ENOBUFS; 11854263Sshin } 11954263Sshin ip = mtod(m, struct ip *); 12055009Sshin itos = ip->ip_tos; 12154263Sshin break; 12254263Sshin } 12354263Sshin#endif 12462587Sitojun#ifdef INET6 12554263Sshin case AF_INET6: 12654263Sshin { 12754263Sshin struct ip6_hdr *ip6; 12854263Sshin proto = IPPROTO_IPV6; 12954263Sshin if (m->m_len < sizeof(*ip6)) { 13054263Sshin m = m_pullup(m, sizeof(*ip6)); 13154263Sshin if (!m) 13254263Sshin return ENOBUFS; 13354263Sshin } 13454263Sshin ip6 = mtod(m, struct ip6_hdr *); 13555009Sshin itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 13654263Sshin break; 13754263Sshin } 13862587Sitojun#endif 139153621Sthompsa case AF_LINK: 140153621Sthompsa proto = IPPROTO_ETHERIP; 141153621Sthompsa eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK; 142153621Sthompsa eiphdr.eip_pad = 0; 143153621Sthompsa /* prepend Ethernet-in-IP header */ 144153621Sthompsa M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT); 145153621Sthompsa if (m && m->m_len < sizeof(struct etherip_header)) 146153621Sthompsa m = m_pullup(m, sizeof(struct etherip_header)); 147153621Sthompsa if (m == NULL) 148153621Sthompsa return ENOBUFS; 149153621Sthompsa bcopy(&eiphdr, mtod(m, struct etherip_header *), 150153621Sthompsa sizeof(struct etherip_header)); 151153621Sthompsa break; 152153621Sthompsa 15354263Sshin default: 15462587Sitojun#ifdef DEBUG 15554263Sshin printf("in6_gif_output: warning: unknown family %d passed\n", 15654263Sshin family); 15754263Sshin#endif 15854263Sshin m_freem(m); 15954263Sshin return EAFNOSUPPORT; 16054263Sshin } 161120913Sume 16254263Sshin /* prepend new IP header */ 163111119Simp M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT); 16454263Sshin if (m && m->m_len < sizeof(struct ip6_hdr)) 16554263Sshin m = m_pullup(m, sizeof(struct ip6_hdr)); 16654263Sshin if (m == NULL) { 16754263Sshin printf("ENOBUFS in in6_gif_output %d\n", __LINE__); 16854263Sshin return ENOBUFS; 16954263Sshin } 17054263Sshin 17154263Sshin ip6 = mtod(m, struct ip6_hdr *); 17254263Sshin ip6->ip6_flow = 0; 17362587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 17462587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 17554263Sshin ip6->ip6_plen = htons((u_short)m->m_pkthdr.len); 17654263Sshin ip6->ip6_nxt = proto; 17754263Sshin ip6->ip6_hlim = ip6_gif_hlim; 17854263Sshin ip6->ip6_src = sin6_src->sin6_addr; 17978064Sume /* bidirectional configured tunnel mode */ 18078064Sume if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr)) 18178064Sume ip6->ip6_dst = sin6_dst->sin6_addr; 18278064Sume else { 18378064Sume m_freem(m); 18478064Sume return ENETUNREACH; 18554263Sshin } 186121684Sume ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE, 187121684Sume &otos, &itos); 188121684Sume ip6->ip6_flow &= ~htonl(0xff << 20); 18978064Sume ip6->ip6_flow |= htonl((u_int32_t)otos << 20); 19054263Sshin 19154263Sshin if (dst->sin6_family != sin6_dst->sin6_family || 19254263Sshin !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) { 19354263Sshin /* cache route doesn't match */ 19454263Sshin bzero(dst, sizeof(*dst)); 19554263Sshin dst->sin6_family = sin6_dst->sin6_family; 19654263Sshin dst->sin6_len = sizeof(struct sockaddr_in6); 19754263Sshin dst->sin6_addr = sin6_dst->sin6_addr; 19854263Sshin if (sc->gif_ro6.ro_rt) { 19954263Sshin RTFREE(sc->gif_ro6.ro_rt); 20054263Sshin sc->gif_ro6.ro_rt = NULL; 20154263Sshin } 20262587Sitojun#if 0 203147256Sbrooks GIF2IFP(sc)->if_mtu = GIF_MTU; 20462587Sitojun#endif 20554263Sshin } 20654263Sshin 20754263Sshin if (sc->gif_ro6.ro_rt == NULL) { 20854263Sshin rtalloc((struct route *)&sc->gif_ro6); 20954263Sshin if (sc->gif_ro6.ro_rt == NULL) { 21054263Sshin m_freem(m); 21154263Sshin return ENETUNREACH; 21254263Sshin } 21362587Sitojun 21462587Sitojun /* if it constitutes infinite encapsulation, punt. */ 21562587Sitojun if (sc->gif_ro.ro_rt->rt_ifp == ifp) { 21662587Sitojun m_freem(m); 21762587Sitojun return ENETUNREACH; /*XXX*/ 21862587Sitojun } 21962587Sitojun#if 0 22062587Sitojun ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu 22162587Sitojun - sizeof(struct ip6_hdr); 22262587Sitojun#endif 22354263Sshin } 224120913Sume 22562587Sitojun#ifdef IPV6_MINMTU 22662587Sitojun /* 22762587Sitojun * force fragmentation to minimum MTU, to avoid path MTU discovery. 22862587Sitojun * it is too painful to ask for resend of inner packet, to achieve 22962587Sitojun * path MTU discovery for encapsulated packets. 23062587Sitojun */ 231138620Sglebius error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL); 23262587Sitojun#else 233138620Sglebius error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL); 23462587Sitojun#endif 235138619Sglebius 236147256Sbrooks if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) && 237138653Sglebius sc->gif_ro6.ro_rt != NULL) { 238138619Sglebius RTFREE(sc->gif_ro6.ro_rt); 239138619Sglebius sc->gif_ro6.ro_rt = NULL; 240138619Sglebius } 241138619Sglebius 242138619Sglebius return (error); 24354263Sshin} 24454263Sshin 245120913Sumeint 246120913Sumein6_gif_input(mp, offp, proto) 24754263Sshin struct mbuf **mp; 24854263Sshin int *offp, proto; 24954263Sshin{ 25054263Sshin struct mbuf *m = *mp; 25154263Sshin struct ifnet *gifp = NULL; 252147507Sume struct gif_softc *sc; 25354263Sshin struct ip6_hdr *ip6; 25454263Sshin int af = 0; 25555009Sshin u_int32_t otos; 25654263Sshin 25754263Sshin ip6 = mtod(m, struct ip6_hdr *); 25854263Sshin 259147507Sume sc = (struct gif_softc *)encap_getarg(m); 260147507Sume if (sc == NULL) { 261147507Sume m_freem(m); 262147507Sume ip6stat.ip6s_nogif++; 263147507Sume return IPPROTO_DONE; 264147507Sume } 26554263Sshin 266147507Sume gifp = GIF2IFP(sc); 26762587Sitojun if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) { 26854263Sshin m_freem(m); 26954263Sshin ip6stat.ip6s_nogif++; 27054263Sshin return IPPROTO_DONE; 27154263Sshin } 27262587Sitojun 27355009Sshin otos = ip6->ip6_flow; 27454263Sshin m_adj(m, *offp); 27554263Sshin 27654263Sshin switch (proto) { 27754263Sshin#ifdef INET 27854263Sshin case IPPROTO_IPV4: 27954263Sshin { 28054263Sshin struct ip *ip; 28155009Sshin u_int8_t otos8; 28254263Sshin af = AF_INET; 28355009Sshin otos8 = (ntohl(otos) >> 20) & 0xff; 28454263Sshin if (m->m_len < sizeof(*ip)) { 28554263Sshin m = m_pullup(m, sizeof(*ip)); 28654263Sshin if (!m) 28754263Sshin return IPPROTO_DONE; 28854263Sshin } 28954263Sshin ip = mtod(m, struct ip *); 290121684Sume if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ? 291121684Sume ECN_ALLOWED : ECN_NOCARE, 292121684Sume &otos8, &ip->ip_tos) == 0) { 293121684Sume m_freem(m); 294121684Sume return IPPROTO_DONE; 295121684Sume } 29654263Sshin break; 29754263Sshin } 29854263Sshin#endif /* INET */ 29962587Sitojun#ifdef INET6 30054263Sshin case IPPROTO_IPV6: 30154263Sshin { 30254263Sshin struct ip6_hdr *ip6; 30354263Sshin af = AF_INET6; 30454263Sshin if (m->m_len < sizeof(*ip6)) { 30554263Sshin m = m_pullup(m, sizeof(*ip6)); 30654263Sshin if (!m) 30754263Sshin return IPPROTO_DONE; 30854263Sshin } 30954263Sshin ip6 = mtod(m, struct ip6_hdr *); 310121684Sume if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ? 311121684Sume ECN_ALLOWED : ECN_NOCARE, 312121684Sume &otos, &ip6->ip6_flow) == 0) { 313121684Sume m_freem(m); 314121684Sume return IPPROTO_DONE; 315121684Sume } 31654263Sshin break; 31754263Sshin } 31862587Sitojun#endif 319153621Sthompsa case IPPROTO_ETHERIP: 320153621Sthompsa af = AF_LINK; 321153621Sthompsa break; 322153621Sthompsa 32354263Sshin default: 32454263Sshin ip6stat.ip6s_nogif++; 32554263Sshin m_freem(m); 32654263Sshin return IPPROTO_DONE; 32754263Sshin } 328120913Sume 32954263Sshin gif_input(m, af, gifp); 33054263Sshin return IPPROTO_DONE; 33154263Sshin} 33262587Sitojun 33362587Sitojun/* 334105293Sume * validate outer address. 33562587Sitojun */ 336105293Sumestatic int 337105293Sumegif_validate6(ip6, sc, ifp) 338105293Sume const struct ip6_hdr *ip6; 339105293Sume struct gif_softc *sc; 340105293Sume struct ifnet *ifp; 34162587Sitojun{ 34262587Sitojun struct sockaddr_in6 *src, *dst; 34362587Sitojun 34462587Sitojun src = (struct sockaddr_in6 *)sc->gif_psrc; 34562587Sitojun dst = (struct sockaddr_in6 *)sc->gif_pdst; 34662587Sitojun 347105293Sume /* 348105293Sume * Check for address match. Note that the check is for an incoming 349105293Sume * packet. We should compare the *source* address in our configuration 350105293Sume * and the *destination* address of the packet, and vice versa. 351105293Sume */ 352105293Sume if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) || 353105293Sume !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src)) 35462587Sitojun return 0; 35562587Sitojun 35662587Sitojun /* martian filters on outer source - done in ip6_input */ 35762587Sitojun 35862587Sitojun /* ingress filters on outer source */ 359147256Sbrooks if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) { 36062587Sitojun struct sockaddr_in6 sin6; 36162587Sitojun struct rtentry *rt; 36262587Sitojun 36362587Sitojun bzero(&sin6, sizeof(sin6)); 36462587Sitojun sin6.sin6_family = AF_INET6; 36562587Sitojun sin6.sin6_len = sizeof(struct sockaddr_in6); 366105293Sume sin6.sin6_addr = ip6->ip6_src; 367105293Sume sin6.sin6_scope_id = 0; /* XXX */ 368105293Sume 36962587Sitojun rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 370105293Sume if (!rt || rt->rt_ifp != ifp) { 37178064Sume#if 0 372165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 37378064Sume log(LOG_WARNING, "%s: packet from %s dropped " 374147256Sbrooks "due to ingress filter\n", if_name(GIF2IFP(sc)), 375165118Sbz ip6_sprintf(ip6buf, &sin6.sin6_addr)); 37678064Sume#endif 37778064Sume if (rt) 37878064Sume rtfree(rt); 37962587Sitojun return 0; 38062587Sitojun } 38162587Sitojun rtfree(rt); 38262587Sitojun } 38362587Sitojun 38478064Sume return 128 * 2; 38562587Sitojun} 386105293Sume 387105293Sume/* 388105293Sume * we know that we are in IFF_UP, outer address available, and outer family 389105293Sume * matched the physical addr family. see gif_encapcheck(). 390105293Sume * sanity check for arg should have been done in the caller. 391105293Sume */ 392105293Sumeint 393105293Sumegif_encapcheck6(m, off, proto, arg) 394105293Sume const struct mbuf *m; 395105293Sume int off; 396105293Sume int proto; 397105293Sume void *arg; 398105293Sume{ 399105293Sume struct ip6_hdr ip6; 400105293Sume struct gif_softc *sc; 401105293Sume struct ifnet *ifp; 402105293Sume 403105293Sume /* sanity check done in caller */ 404105293Sume sc = (struct gif_softc *)arg; 405105293Sume 406105293Sume /* LINTED const cast */ 407105293Sume m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6); 408105293Sume ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL; 409105293Sume 410105293Sume return gif_validate6(&ip6, sc, ifp); 411105293Sume} 412105293Sume 413105293Sumeint 414105293Sumein6_gif_attach(sc) 415105293Sume struct gif_softc *sc; 416105293Sume{ 417105293Sume sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck, 418155333Sume (void *)&in6_gif_protosw, sc); 419105293Sume if (sc->encap_cookie6 == NULL) 420105293Sume return EEXIST; 421105293Sume return 0; 422105293Sume} 423105293Sume 424105293Sumeint 425105293Sumein6_gif_detach(sc) 426105293Sume struct gif_softc *sc; 427105293Sume{ 428105293Sume int error; 429105293Sume 430105293Sume error = encap_detach(sc->encap_cookie6); 431105293Sume if (error == 0) 432105293Sume sc->encap_cookie6 = NULL; 433105293Sume return error; 434105293Sume} 435