in6_gif.c revision 139826
162587Sitojun/* $FreeBSD: head/sys/netinet6/in6_gif.c 139826 2005-01-07 02:30:35Z imp $ */ 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 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 */ 216138620Sglebius error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL); 21762587Sitojun#else 218138620Sglebius error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL); 21962587Sitojun#endif 220138619Sglebius 221138653Sglebius if (!(sc->gif_if.if_flags & IFF_LINK0) && 222138653Sglebius sc->gif_ro6.ro_rt != NULL) { 223138619Sglebius RTFREE(sc->gif_ro6.ro_rt); 224138619Sglebius sc->gif_ro6.ro_rt = NULL; 225138619Sglebius } 226138619Sglebius 227138619Sglebius return (error); 22854263Sshin} 22954263Sshin 230120913Sumeint 231120913Sumein6_gif_input(mp, offp, proto) 23254263Sshin struct mbuf **mp; 23354263Sshin int *offp, proto; 23454263Sshin{ 23554263Sshin struct mbuf *m = *mp; 23654263Sshin struct ifnet *gifp = NULL; 23754263Sshin struct ip6_hdr *ip6; 23854263Sshin int af = 0; 23955009Sshin u_int32_t otos; 24054263Sshin 24154263Sshin ip6 = mtod(m, struct ip6_hdr *); 24254263Sshin 24362587Sitojun gifp = (struct ifnet *)encap_getarg(m); 24454263Sshin 24562587Sitojun if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) { 24654263Sshin m_freem(m); 24754263Sshin ip6stat.ip6s_nogif++; 24854263Sshin return IPPROTO_DONE; 24954263Sshin } 25062587Sitojun 25155009Sshin otos = ip6->ip6_flow; 25254263Sshin m_adj(m, *offp); 25354263Sshin 25454263Sshin switch (proto) { 25554263Sshin#ifdef INET 25654263Sshin case IPPROTO_IPV4: 25754263Sshin { 25854263Sshin struct ip *ip; 25955009Sshin u_int8_t otos8; 26054263Sshin af = AF_INET; 26155009Sshin otos8 = (ntohl(otos) >> 20) & 0xff; 26254263Sshin if (m->m_len < sizeof(*ip)) { 26354263Sshin m = m_pullup(m, sizeof(*ip)); 26454263Sshin if (!m) 26554263Sshin return IPPROTO_DONE; 26654263Sshin } 26754263Sshin ip = mtod(m, struct ip *); 268121684Sume if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ? 269121684Sume ECN_ALLOWED : ECN_NOCARE, 270121684Sume &otos8, &ip->ip_tos) == 0) { 271121684Sume m_freem(m); 272121684Sume return IPPROTO_DONE; 273121684Sume } 27454263Sshin break; 27554263Sshin } 27654263Sshin#endif /* INET */ 27762587Sitojun#ifdef INET6 27854263Sshin case IPPROTO_IPV6: 27954263Sshin { 28054263Sshin struct ip6_hdr *ip6; 28154263Sshin af = AF_INET6; 28254263Sshin if (m->m_len < sizeof(*ip6)) { 28354263Sshin m = m_pullup(m, sizeof(*ip6)); 28454263Sshin if (!m) 28554263Sshin return IPPROTO_DONE; 28654263Sshin } 28754263Sshin ip6 = mtod(m, struct ip6_hdr *); 288121684Sume if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ? 289121684Sume ECN_ALLOWED : ECN_NOCARE, 290121684Sume &otos, &ip6->ip6_flow) == 0) { 291121684Sume m_freem(m); 292121684Sume return IPPROTO_DONE; 293121684Sume } 29454263Sshin break; 29554263Sshin } 29662587Sitojun#endif 29754263Sshin default: 29854263Sshin ip6stat.ip6s_nogif++; 29954263Sshin m_freem(m); 30054263Sshin return IPPROTO_DONE; 30154263Sshin } 302120913Sume 30354263Sshin gif_input(m, af, gifp); 30454263Sshin return IPPROTO_DONE; 30554263Sshin} 30662587Sitojun 30762587Sitojun/* 308105293Sume * validate outer address. 30962587Sitojun */ 310105293Sumestatic int 311105293Sumegif_validate6(ip6, sc, ifp) 312105293Sume const struct ip6_hdr *ip6; 313105293Sume struct gif_softc *sc; 314105293Sume struct ifnet *ifp; 31562587Sitojun{ 31662587Sitojun struct sockaddr_in6 *src, *dst; 31762587Sitojun 31862587Sitojun src = (struct sockaddr_in6 *)sc->gif_psrc; 31962587Sitojun dst = (struct sockaddr_in6 *)sc->gif_pdst; 32062587Sitojun 321105293Sume /* 322105293Sume * Check for address match. Note that the check is for an incoming 323105293Sume * packet. We should compare the *source* address in our configuration 324105293Sume * and the *destination* address of the packet, and vice versa. 325105293Sume */ 326105293Sume if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) || 327105293Sume !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src)) 32862587Sitojun return 0; 32962587Sitojun 33062587Sitojun /* martian filters on outer source - done in ip6_input */ 33162587Sitojun 33262587Sitojun /* ingress filters on outer source */ 333105293Sume if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) { 33462587Sitojun struct sockaddr_in6 sin6; 33562587Sitojun struct rtentry *rt; 33662587Sitojun 33762587Sitojun bzero(&sin6, sizeof(sin6)); 33862587Sitojun sin6.sin6_family = AF_INET6; 33962587Sitojun sin6.sin6_len = sizeof(struct sockaddr_in6); 340105293Sume sin6.sin6_addr = ip6->ip6_src; 341105293Sume sin6.sin6_scope_id = 0; /* XXX */ 342105293Sume 34362587Sitojun rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 344105293Sume if (!rt || rt->rt_ifp != ifp) { 34578064Sume#if 0 34678064Sume log(LOG_WARNING, "%s: packet from %s dropped " 34778064Sume "due to ingress filter\n", if_name(&sc->gif_if), 34878064Sume ip6_sprintf(&sin6.sin6_addr)); 34978064Sume#endif 35078064Sume if (rt) 35178064Sume rtfree(rt); 35262587Sitojun return 0; 35362587Sitojun } 35462587Sitojun rtfree(rt); 35562587Sitojun } 35662587Sitojun 35778064Sume return 128 * 2; 35862587Sitojun} 359105293Sume 360105293Sume/* 361105293Sume * we know that we are in IFF_UP, outer address available, and outer family 362105293Sume * matched the physical addr family. see gif_encapcheck(). 363105293Sume * sanity check for arg should have been done in the caller. 364105293Sume */ 365105293Sumeint 366105293Sumegif_encapcheck6(m, off, proto, arg) 367105293Sume const struct mbuf *m; 368105293Sume int off; 369105293Sume int proto; 370105293Sume void *arg; 371105293Sume{ 372105293Sume struct ip6_hdr ip6; 373105293Sume struct gif_softc *sc; 374105293Sume struct ifnet *ifp; 375105293Sume 376105293Sume /* sanity check done in caller */ 377105293Sume sc = (struct gif_softc *)arg; 378105293Sume 379105293Sume /* LINTED const cast */ 380105293Sume m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6); 381105293Sume ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL; 382105293Sume 383105293Sume return gif_validate6(&ip6, sc, ifp); 384105293Sume} 385105293Sume 386105293Sumeint 387105293Sumein6_gif_attach(sc) 388105293Sume struct gif_softc *sc; 389105293Sume{ 390105293Sume sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck, 391105293Sume (struct protosw *)&in6_gif_protosw, sc); 392105293Sume if (sc->encap_cookie6 == NULL) 393105293Sume return EEXIST; 394105293Sume return 0; 395105293Sume} 396105293Sume 397105293Sumeint 398105293Sumein6_gif_detach(sc) 399105293Sume struct gif_softc *sc; 400105293Sume{ 401105293Sume int error; 402105293Sume 403105293Sume error = encap_detach(sc->encap_cookie6); 404105293Sume if (error == 0) 405105293Sume sc->encap_cookie6 = NULL; 406105293Sume return error; 407105293Sume} 408