in6_gif.c revision 138619
162587Sitojun/* $FreeBSD: head/sys/netinet6/in6_gif.c 138619 2004-12-09 09:48:47Z glebius $ */ 278064Sume/* $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $ */ 362587Sitojun 454263Sshin/* 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 7154263Sshin#include <net/net_osdep.h> 7254263Sshin 73105293Sumestatic int gif_validate6(const struct ip6_hdr *, struct gif_softc *, 74105293Sume struct ifnet *); 75105293Sume 76105293Sumeextern struct domain inet6domain; 77105293Sumestruct ip6protosw in6_gif_protosw = 78120913Sume{ SOCK_RAW, &inet6domain, 0/* IPPROTO_IPV[46] */, PR_ATOMIC|PR_ADDR, 79105293Sume in6_gif_input, rip6_output, 0, rip6_ctloutput, 80105293Sume 0, 81105293Sume 0, 0, 0, 0, 82105293Sume &rip6_usrreqs 83105293Sume}; 84105293Sume 8554263Sshinint 86105340Sumein6_gif_output(ifp, family, m) 8754263Sshin struct ifnet *ifp; 8854263Sshin int family; /* family of the packet to be encapsulate. */ 8954263Sshin struct mbuf *m; 9054263Sshin{ 9154263Sshin struct gif_softc *sc = (struct gif_softc*)ifp; 9254263Sshin struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst; 9354263Sshin struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc; 9454263Sshin struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst; 9554263Sshin struct ip6_hdr *ip6; 96138619Sglebius int proto, error; 9755009Sshin u_int8_t itos, otos; 9854263Sshin 9954263Sshin if (sin6_src == NULL || sin6_dst == NULL || 10054263Sshin sin6_src->sin6_family != AF_INET6 || 10154263Sshin sin6_dst->sin6_family != AF_INET6) { 10254263Sshin m_freem(m); 10354263Sshin return EAFNOSUPPORT; 10454263Sshin } 10554263Sshin 10654263Sshin switch (family) { 10754263Sshin#ifdef INET 10854263Sshin case AF_INET: 10954263Sshin { 11054263Sshin struct ip *ip; 11154263Sshin 11254263Sshin proto = IPPROTO_IPV4; 11354263Sshin if (m->m_len < sizeof(*ip)) { 11454263Sshin m = m_pullup(m, sizeof(*ip)); 11554263Sshin if (!m) 11654263Sshin return ENOBUFS; 11754263Sshin } 11854263Sshin ip = mtod(m, struct ip *); 11955009Sshin itos = ip->ip_tos; 12054263Sshin break; 12154263Sshin } 12254263Sshin#endif 12362587Sitojun#ifdef INET6 12454263Sshin case AF_INET6: 12554263Sshin { 12654263Sshin struct ip6_hdr *ip6; 12754263Sshin proto = IPPROTO_IPV6; 12854263Sshin if (m->m_len < sizeof(*ip6)) { 12954263Sshin m = m_pullup(m, sizeof(*ip6)); 13054263Sshin if (!m) 13154263Sshin return ENOBUFS; 13254263Sshin } 13354263Sshin ip6 = mtod(m, struct ip6_hdr *); 13455009Sshin itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 13554263Sshin break; 13654263Sshin } 13762587Sitojun#endif 13854263Sshin default: 13962587Sitojun#ifdef DEBUG 14054263Sshin printf("in6_gif_output: warning: unknown family %d passed\n", 14154263Sshin family); 14254263Sshin#endif 14354263Sshin m_freem(m); 14454263Sshin return EAFNOSUPPORT; 14554263Sshin } 146120913Sume 14754263Sshin /* prepend new IP header */ 148111119Simp M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT); 14954263Sshin if (m && m->m_len < sizeof(struct ip6_hdr)) 15054263Sshin m = m_pullup(m, sizeof(struct ip6_hdr)); 15154263Sshin if (m == NULL) { 15254263Sshin printf("ENOBUFS in in6_gif_output %d\n", __LINE__); 15354263Sshin return ENOBUFS; 15454263Sshin } 15554263Sshin 15654263Sshin ip6 = mtod(m, struct ip6_hdr *); 15754263Sshin ip6->ip6_flow = 0; 15862587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 15962587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 16054263Sshin ip6->ip6_plen = htons((u_short)m->m_pkthdr.len); 16154263Sshin ip6->ip6_nxt = proto; 16254263Sshin ip6->ip6_hlim = ip6_gif_hlim; 16354263Sshin ip6->ip6_src = sin6_src->sin6_addr; 16478064Sume /* bidirectional configured tunnel mode */ 16578064Sume if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr)) 16678064Sume ip6->ip6_dst = sin6_dst->sin6_addr; 16778064Sume else { 16878064Sume m_freem(m); 16978064Sume return ENETUNREACH; 17054263Sshin } 171121684Sume ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE, 172121684Sume &otos, &itos); 173121684Sume ip6->ip6_flow &= ~htonl(0xff << 20); 17478064Sume ip6->ip6_flow |= htonl((u_int32_t)otos << 20); 17554263Sshin 17654263Sshin if (dst->sin6_family != sin6_dst->sin6_family || 17754263Sshin !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) { 17854263Sshin /* cache route doesn't match */ 17954263Sshin bzero(dst, sizeof(*dst)); 18054263Sshin dst->sin6_family = sin6_dst->sin6_family; 18154263Sshin dst->sin6_len = sizeof(struct sockaddr_in6); 18254263Sshin dst->sin6_addr = sin6_dst->sin6_addr; 18354263Sshin if (sc->gif_ro6.ro_rt) { 18454263Sshin RTFREE(sc->gif_ro6.ro_rt); 18554263Sshin sc->gif_ro6.ro_rt = NULL; 18654263Sshin } 18762587Sitojun#if 0 18862587Sitojun sc->gif_if.if_mtu = GIF_MTU; 18962587Sitojun#endif 19054263Sshin } 19154263Sshin 19254263Sshin if (sc->gif_ro6.ro_rt == NULL) { 19354263Sshin rtalloc((struct route *)&sc->gif_ro6); 19454263Sshin if (sc->gif_ro6.ro_rt == NULL) { 19554263Sshin m_freem(m); 19654263Sshin return ENETUNREACH; 19754263Sshin } 19862587Sitojun 19962587Sitojun /* if it constitutes infinite encapsulation, punt. */ 20062587Sitojun if (sc->gif_ro.ro_rt->rt_ifp == ifp) { 20162587Sitojun m_freem(m); 20262587Sitojun return ENETUNREACH; /*XXX*/ 20362587Sitojun } 20462587Sitojun#if 0 20562587Sitojun ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu 20662587Sitojun - sizeof(struct ip6_hdr); 20762587Sitojun#endif 20854263Sshin } 209120913Sume 21062587Sitojun#ifdef IPV6_MINMTU 21162587Sitojun /* 21262587Sitojun * force fragmentation to minimum MTU, to avoid path MTU discovery. 21362587Sitojun * it is too painful to ask for resend of inner packet, to achieve 21462587Sitojun * path MTU discovery for encapsulated packets. 21562587Sitojun */ 216138619Sglebius error = (ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL)); 21762587Sitojun#else 218138619Sglebius error = (ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL)); 21962587Sitojun#endif 220138619Sglebius 221138619Sglebius if ((sc->gif_if.if_flags & IFF_LINK0) == 0) { 222138619Sglebius RTFREE(sc->gif_ro6.ro_rt); 223138619Sglebius sc->gif_ro6.ro_rt = NULL; 224138619Sglebius } 225138619Sglebius 226138619Sglebius return (error); 22754263Sshin} 22854263Sshin 229120913Sumeint 230120913Sumein6_gif_input(mp, offp, proto) 23154263Sshin struct mbuf **mp; 23254263Sshin int *offp, proto; 23354263Sshin{ 23454263Sshin struct mbuf *m = *mp; 23554263Sshin struct ifnet *gifp = NULL; 23654263Sshin struct ip6_hdr *ip6; 23754263Sshin int af = 0; 23855009Sshin u_int32_t otos; 23954263Sshin 24054263Sshin ip6 = mtod(m, struct ip6_hdr *); 24154263Sshin 24262587Sitojun gifp = (struct ifnet *)encap_getarg(m); 24354263Sshin 24462587Sitojun if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) { 24554263Sshin m_freem(m); 24654263Sshin ip6stat.ip6s_nogif++; 24754263Sshin return IPPROTO_DONE; 24854263Sshin } 24962587Sitojun 25055009Sshin otos = ip6->ip6_flow; 25154263Sshin m_adj(m, *offp); 25254263Sshin 25354263Sshin switch (proto) { 25454263Sshin#ifdef INET 25554263Sshin case IPPROTO_IPV4: 25654263Sshin { 25754263Sshin struct ip *ip; 25855009Sshin u_int8_t otos8; 25954263Sshin af = AF_INET; 26055009Sshin otos8 = (ntohl(otos) >> 20) & 0xff; 26154263Sshin if (m->m_len < sizeof(*ip)) { 26254263Sshin m = m_pullup(m, sizeof(*ip)); 26354263Sshin if (!m) 26454263Sshin return IPPROTO_DONE; 26554263Sshin } 26654263Sshin ip = mtod(m, struct ip *); 267121684Sume if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ? 268121684Sume ECN_ALLOWED : ECN_NOCARE, 269121684Sume &otos8, &ip->ip_tos) == 0) { 270121684Sume m_freem(m); 271121684Sume return IPPROTO_DONE; 272121684Sume } 27354263Sshin break; 27454263Sshin } 27554263Sshin#endif /* INET */ 27662587Sitojun#ifdef INET6 27754263Sshin case IPPROTO_IPV6: 27854263Sshin { 27954263Sshin struct ip6_hdr *ip6; 28054263Sshin af = AF_INET6; 28154263Sshin if (m->m_len < sizeof(*ip6)) { 28254263Sshin m = m_pullup(m, sizeof(*ip6)); 28354263Sshin if (!m) 28454263Sshin return IPPROTO_DONE; 28554263Sshin } 28654263Sshin ip6 = mtod(m, struct ip6_hdr *); 287121684Sume if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ? 288121684Sume ECN_ALLOWED : ECN_NOCARE, 289121684Sume &otos, &ip6->ip6_flow) == 0) { 290121684Sume m_freem(m); 291121684Sume return IPPROTO_DONE; 292121684Sume } 29354263Sshin break; 29454263Sshin } 29562587Sitojun#endif 29654263Sshin default: 29754263Sshin ip6stat.ip6s_nogif++; 29854263Sshin m_freem(m); 29954263Sshin return IPPROTO_DONE; 30054263Sshin } 301120913Sume 30254263Sshin gif_input(m, af, gifp); 30354263Sshin return IPPROTO_DONE; 30454263Sshin} 30562587Sitojun 30662587Sitojun/* 307105293Sume * validate outer address. 30862587Sitojun */ 309105293Sumestatic int 310105293Sumegif_validate6(ip6, sc, ifp) 311105293Sume const struct ip6_hdr *ip6; 312105293Sume struct gif_softc *sc; 313105293Sume struct ifnet *ifp; 31462587Sitojun{ 31562587Sitojun struct sockaddr_in6 *src, *dst; 31662587Sitojun 31762587Sitojun src = (struct sockaddr_in6 *)sc->gif_psrc; 31862587Sitojun dst = (struct sockaddr_in6 *)sc->gif_pdst; 31962587Sitojun 320105293Sume /* 321105293Sume * Check for address match. Note that the check is for an incoming 322105293Sume * packet. We should compare the *source* address in our configuration 323105293Sume * and the *destination* address of the packet, and vice versa. 324105293Sume */ 325105293Sume if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) || 326105293Sume !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src)) 32762587Sitojun return 0; 32862587Sitojun 32962587Sitojun /* martian filters on outer source - done in ip6_input */ 33062587Sitojun 33162587Sitojun /* ingress filters on outer source */ 332105293Sume if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) { 33362587Sitojun struct sockaddr_in6 sin6; 33462587Sitojun struct rtentry *rt; 33562587Sitojun 33662587Sitojun bzero(&sin6, sizeof(sin6)); 33762587Sitojun sin6.sin6_family = AF_INET6; 33862587Sitojun sin6.sin6_len = sizeof(struct sockaddr_in6); 339105293Sume sin6.sin6_addr = ip6->ip6_src; 340105293Sume sin6.sin6_scope_id = 0; /* XXX */ 341105293Sume 34262587Sitojun rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 343105293Sume if (!rt || rt->rt_ifp != ifp) { 34478064Sume#if 0 34578064Sume log(LOG_WARNING, "%s: packet from %s dropped " 34678064Sume "due to ingress filter\n", if_name(&sc->gif_if), 34778064Sume ip6_sprintf(&sin6.sin6_addr)); 34878064Sume#endif 34978064Sume if (rt) 35078064Sume rtfree(rt); 35162587Sitojun return 0; 35262587Sitojun } 35362587Sitojun rtfree(rt); 35462587Sitojun } 35562587Sitojun 35678064Sume return 128 * 2; 35762587Sitojun} 358105293Sume 359105293Sume/* 360105293Sume * we know that we are in IFF_UP, outer address available, and outer family 361105293Sume * matched the physical addr family. see gif_encapcheck(). 362105293Sume * sanity check for arg should have been done in the caller. 363105293Sume */ 364105293Sumeint 365105293Sumegif_encapcheck6(m, off, proto, arg) 366105293Sume const struct mbuf *m; 367105293Sume int off; 368105293Sume int proto; 369105293Sume void *arg; 370105293Sume{ 371105293Sume struct ip6_hdr ip6; 372105293Sume struct gif_softc *sc; 373105293Sume struct ifnet *ifp; 374105293Sume 375105293Sume /* sanity check done in caller */ 376105293Sume sc = (struct gif_softc *)arg; 377105293Sume 378105293Sume /* LINTED const cast */ 379105293Sume m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6); 380105293Sume ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL; 381105293Sume 382105293Sume return gif_validate6(&ip6, sc, ifp); 383105293Sume} 384105293Sume 385105293Sumeint 386105293Sumein6_gif_attach(sc) 387105293Sume struct gif_softc *sc; 388105293Sume{ 389105293Sume sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck, 390105293Sume (struct protosw *)&in6_gif_protosw, sc); 391105293Sume if (sc->encap_cookie6 == NULL) 392105293Sume return EEXIST; 393105293Sume return 0; 394105293Sume} 395105293Sume 396105293Sumeint 397105293Sumein6_gif_detach(sc) 398105293Sume struct gif_softc *sc; 399105293Sume{ 400105293Sume int error; 401105293Sume 402105293Sume error = encap_detach(sc->encap_cookie6); 403105293Sume if (error == 0) 404105293Sume sc->encap_cookie6 = NULL; 405105293Sume return error; 406105293Sume} 407