if_gre.c revision 196019
1123992Ssobomax/* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */ 2103026Ssobomax/* $FreeBSD: head/sys/net/if_gre.c 196019 2009-08-01 19:26:27Z rwatson $ */ 3103026Ssobomax 4139823Simp/*- 5103026Ssobomax * Copyright (c) 1998 The NetBSD Foundation, Inc. 6103026Ssobomax * All rights reserved. 7103026Ssobomax * 8103026Ssobomax * This code is derived from software contributed to The NetBSD Foundation 9103026Ssobomax * by Heiko W.Rupp <hwr@pilhuhn.de> 10103026Ssobomax * 11148613Sbz * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de> 12148613Sbz * 13103026Ssobomax * Redistribution and use in source and binary forms, with or without 14103026Ssobomax * modification, are permitted provided that the following conditions 15103026Ssobomax * are met: 16103026Ssobomax * 1. Redistributions of source code must retain the above copyright 17103026Ssobomax * notice, this list of conditions and the following disclaimer. 18103026Ssobomax * 2. Redistributions in binary form must reproduce the above copyright 19103026Ssobomax * notice, this list of conditions and the following disclaimer in the 20103026Ssobomax * documentation and/or other materials provided with the distribution. 21103026Ssobomax * 3. All advertising materials mentioning features or use of this software 22103026Ssobomax * must display the following acknowledgement: 23103026Ssobomax * This product includes software developed by the NetBSD 24103026Ssobomax * Foundation, Inc. and its contributors. 25103026Ssobomax * 4. Neither the name of The NetBSD Foundation nor the names of its 26103026Ssobomax * contributors may be used to endorse or promote products derived 27103026Ssobomax * from this software without specific prior written permission. 28103026Ssobomax * 29103026Ssobomax * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 30103026Ssobomax * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31103026Ssobomax * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 32103026Ssobomax * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 33103026Ssobomax * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34103026Ssobomax * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35103026Ssobomax * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36103026Ssobomax * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37103026Ssobomax * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38103026Ssobomax * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39103026Ssobomax * POSSIBILITY OF SUCH DAMAGE. 40103026Ssobomax */ 41103026Ssobomax 42103026Ssobomax/* 43103026Ssobomax * Encapsulate L3 protocols into IP 44148613Sbz * See RFC 2784 (successor of RFC 1701 and 1702) for more details. 45103026Ssobomax * If_gre is compatible with Cisco GRE tunnels, so you can 46103026Ssobomax * have a NetBSD box as the other end of a tunnel interface of a Cisco 47103026Ssobomax * router. See gre(4) for more details. 48103026Ssobomax * Also supported: IP in IP encaps (proto 55) as of RFC 2004 49103026Ssobomax */ 50103026Ssobomax 51103394Sbde#include "opt_atalk.h" 52103026Ssobomax#include "opt_inet.h" 53122699Sbms#include "opt_inet6.h" 54103026Ssobomax 55103026Ssobomax#include <sys/param.h> 56103026Ssobomax#include <sys/kernel.h> 57103026Ssobomax#include <sys/malloc.h> 58129880Sphk#include <sys/module.h> 59103026Ssobomax#include <sys/mbuf.h> 60164033Srwatson#include <sys/priv.h> 61178888Sjulian#include <sys/proc.h> 62103026Ssobomax#include <sys/protosw.h> 63103026Ssobomax#include <sys/socket.h> 64103026Ssobomax#include <sys/sockio.h> 65103026Ssobomax#include <sys/sysctl.h> 66103344Sbde#include <sys/systm.h> 67103026Ssobomax 68103026Ssobomax#include <net/ethernet.h> 69103026Ssobomax#include <net/if.h> 70130933Sbrooks#include <net/if_clone.h> 71103026Ssobomax#include <net/if_types.h> 72103026Ssobomax#include <net/route.h> 73196019Srwatson#include <net/vnet.h> 74103026Ssobomax 75103026Ssobomax#ifdef INET 76103026Ssobomax#include <netinet/in.h> 77103026Ssobomax#include <netinet/in_systm.h> 78103026Ssobomax#include <netinet/in_var.h> 79103026Ssobomax#include <netinet/ip.h> 80103026Ssobomax#include <netinet/ip_gre.h> 81103026Ssobomax#include <netinet/ip_var.h> 82103026Ssobomax#include <netinet/ip_encap.h> 83103026Ssobomax#else 84103026Ssobomax#error "Huh? if_gre without inet?" 85103026Ssobomax#endif 86103026Ssobomax 87103026Ssobomax#include <net/bpf.h> 88103026Ssobomax 89103026Ssobomax#include <net/if_gre.h> 90103026Ssobomax 91103026Ssobomax/* 92103026Ssobomax * It is not easy to calculate the right value for a GRE MTU. 93103026Ssobomax * We leave this task to the admin and use the same default that 94103026Ssobomax * other vendors use. 95103026Ssobomax */ 96103026Ssobomax#define GREMTU 1476 97103026Ssobomax 98103026Ssobomax#define GRENAME "gre" 99103026Ssobomax 100127307Srwatson/* 101127307Srwatson * gre_mtx protects all global variables in if_gre.c. 102127307Srwatson * XXX: gre_softc data not protected yet. 103127307Srwatson */ 104127307Srwatsonstruct mtx gre_mtx; 105103026Ssobomaxstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation"); 106103026Ssobomax 107103026Ssobomaxstruct gre_softc_head gre_softc_list; 108103026Ssobomax 109160195Ssamstatic int gre_clone_create(struct if_clone *, int, caddr_t); 110105300Salfredstatic void gre_clone_destroy(struct ifnet *); 111103032Ssobomaxstatic int gre_ioctl(struct ifnet *, u_long, caddr_t); 112103032Ssobomaxstatic int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *, 113191148Skmacy struct route *ro); 114103026Ssobomax 115130933SbrooksIFC_SIMPLE_DECLARE(gre, 0); 116103026Ssobomax 117103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc); 118103026Ssobomax 119105300Salfredstatic void greattach(void); 120103026Ssobomax 121103026Ssobomax#ifdef INET 122103026Ssobomaxextern struct domain inetdomain; 123152242Srustatic const struct protosw in_gre_protosw = { 124152242Sru .pr_type = SOCK_RAW, 125152242Sru .pr_domain = &inetdomain, 126152242Sru .pr_protocol = IPPROTO_GRE, 127152242Sru .pr_flags = PR_ATOMIC|PR_ADDR, 128154625Sbz .pr_input = gre_input, 129152242Sru .pr_output = (pr_output_t *)rip_output, 130152242Sru .pr_ctlinput = rip_ctlinput, 131152242Sru .pr_ctloutput = rip_ctloutput, 132152242Sru .pr_usrreqs = &rip_usrreqs 133103026Ssobomax}; 134152242Srustatic const struct protosw in_mobile_protosw = { 135152242Sru .pr_type = SOCK_RAW, 136152242Sru .pr_domain = &inetdomain, 137152242Sru .pr_protocol = IPPROTO_MOBILE, 138152242Sru .pr_flags = PR_ATOMIC|PR_ADDR, 139154625Sbz .pr_input = gre_mobile_input, 140152242Sru .pr_output = (pr_output_t *)rip_output, 141152242Sru .pr_ctlinput = rip_ctlinput, 142152242Sru .pr_ctloutput = rip_ctloutput, 143152242Sru .pr_usrreqs = &rip_usrreqs 144103026Ssobomax}; 145103026Ssobomax#endif 146103026Ssobomax 147103026SsobomaxSYSCTL_DECL(_net_link); 148123338SbmsSYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0, 149103026Ssobomax "Generic Routing Encapsulation"); 150103026Ssobomax#ifndef MAX_GRE_NEST 151103026Ssobomax/* 152103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels. 153103026Ssobomax * Since, setting a large value to this macro with a careless configuration 154103026Ssobomax * may introduce system crash, we don't allow any nestings by default. 155103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro 156103026Ssobomax * in your kernel configuration file. However, if you do so, please be 157103026Ssobomax * careful to configure the tunnels so that it won't make a loop. 158103026Ssobomax */ 159103026Ssobomax#define MAX_GRE_NEST 1 160103026Ssobomax#endif 161103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST; 162103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW, 163103026Ssobomax &max_gre_nesting, 0, "Max nested tunnels"); 164103026Ssobomax 165103026Ssobomax/* ARGSUSED */ 166103032Ssobomaxstatic void 167103026Ssobomaxgreattach(void) 168103026Ssobomax{ 169103026Ssobomax 170127307Srwatson mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF); 171103026Ssobomax LIST_INIT(&gre_softc_list); 172103026Ssobomax if_clone_attach(&gre_cloner); 173103026Ssobomax} 174103026Ssobomax 175103032Ssobomaxstatic int 176160195Ssamgre_clone_create(ifc, unit, params) 177103026Ssobomax struct if_clone *ifc; 178103026Ssobomax int unit; 179160195Ssam caddr_t params; 180103026Ssobomax{ 181103026Ssobomax struct gre_softc *sc; 182103026Ssobomax 183131673Sbms sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO); 184103026Ssobomax 185147643Sbz GRE2IFP(sc) = if_alloc(IFT_TUNNEL); 186147643Sbz if (GRE2IFP(sc) == NULL) { 187147643Sbz free(sc, M_GRE); 188147643Sbz return (ENOSPC); 189147643Sbz } 190147643Sbz 191147643Sbz GRE2IFP(sc)->if_softc = sc; 192147256Sbrooks if_initname(GRE2IFP(sc), ifc->ifc_name, unit); 193147643Sbz 194147256Sbrooks GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 195147256Sbrooks GRE2IFP(sc)->if_addrlen = 0; 196147256Sbrooks GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */ 197147256Sbrooks GRE2IFP(sc)->if_mtu = GREMTU; 198147256Sbrooks GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 199147256Sbrooks GRE2IFP(sc)->if_output = gre_output; 200147256Sbrooks GRE2IFP(sc)->if_ioctl = gre_ioctl; 201103026Ssobomax sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 202103026Ssobomax sc->g_proto = IPPROTO_GRE; 203147256Sbrooks GRE2IFP(sc)->if_flags |= IFF_LINK0; 204103026Ssobomax sc->encap = NULL; 205103026Ssobomax sc->called = 0; 206178888Sjulian sc->gre_fibnum = curthread->td_proc->p_fibnum; 207125024Ssobomax sc->wccp_ver = WCCP_V1; 208179894Sthompsa sc->key = 0; 209147256Sbrooks if_attach(GRE2IFP(sc)); 210147256Sbrooks bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 211127307Srwatson mtx_lock(&gre_mtx); 212103026Ssobomax LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 213127307Srwatson mtx_unlock(&gre_mtx); 214103026Ssobomax return (0); 215103026Ssobomax} 216103026Ssobomax 217103032Ssobomaxstatic void 218127307Srwatsongre_clone_destroy(ifp) 219127307Srwatson struct ifnet *ifp; 220127307Srwatson{ 221127307Srwatson struct gre_softc *sc = ifp->if_softc; 222127307Srwatson 223127307Srwatson mtx_lock(&gre_mtx); 224127307Srwatson LIST_REMOVE(sc, sc_list); 225127307Srwatson mtx_unlock(&gre_mtx); 226151266Sthompsa 227151266Sthompsa#ifdef INET 228151266Sthompsa if (sc->encap != NULL) 229151266Sthompsa encap_detach(sc->encap); 230151266Sthompsa#endif 231151266Sthompsa bpfdetach(ifp); 232151266Sthompsa if_detach(ifp); 233151266Sthompsa if_free(ifp); 234151266Sthompsa free(sc, M_GRE); 235127307Srwatson} 236127307Srwatson 237103026Ssobomax/* 238103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol 239103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004 240103026Ssobomax */ 241103032Ssobomaxstatic int 242103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 243191148Skmacy struct route *ro) 244103026Ssobomax{ 245103026Ssobomax int error = 0; 246103026Ssobomax struct gre_softc *sc = ifp->if_softc; 247103026Ssobomax struct greip *gh; 248103026Ssobomax struct ip *ip; 249180041Sjulian u_short gre_ip_id = 0; 250180041Sjulian uint8_t gre_ip_tos = 0; 251123992Ssobomax u_int16_t etype = 0; 252103026Ssobomax struct mobile_h mob_h; 253147611Sdwmalone u_int32_t af; 254180639Sjulian int extra = 0; 255103026Ssobomax 256103026Ssobomax /* 257103026Ssobomax * gre may cause infinite recursion calls when misconfigured. 258103026Ssobomax * We'll prevent this by introducing upper limit. 259103026Ssobomax */ 260103026Ssobomax if (++(sc->called) > max_gre_nesting) { 261103026Ssobomax printf("%s: gre_output: recursively called too many " 262147256Sbrooks "times(%d)\n", if_name(GRE2IFP(sc)), sc->called); 263103026Ssobomax m_freem(m); 264103026Ssobomax error = EIO; /* is there better errno? */ 265103026Ssobomax goto end; 266103026Ssobomax } 267103026Ssobomax 268148887Srwatson if (!((ifp->if_flags & IFF_UP) && 269148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING)) || 270103026Ssobomax sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 271103026Ssobomax m_freem(m); 272103026Ssobomax error = ENETDOWN; 273103026Ssobomax goto end; 274103026Ssobomax } 275103026Ssobomax 276103026Ssobomax gh = NULL; 277103026Ssobomax ip = NULL; 278103026Ssobomax 279147611Sdwmalone /* BPF writes need to be handled specially. */ 280147611Sdwmalone if (dst->sa_family == AF_UNSPEC) { 281147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 282147611Sdwmalone dst->sa_family = af; 283147611Sdwmalone } 284147611Sdwmalone 285159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 286147611Sdwmalone af = dst->sa_family; 287123922Ssam bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 288103026Ssobomax } 289103026Ssobomax 290103026Ssobomax m->m_flags &= ~(M_BCAST|M_MCAST); 291103026Ssobomax 292103026Ssobomax if (sc->g_proto == IPPROTO_MOBILE) { 293103026Ssobomax if (dst->sa_family == AF_INET) { 294103026Ssobomax struct mbuf *m0; 295103026Ssobomax int msiz; 296103026Ssobomax 297103026Ssobomax ip = mtod(m, struct ip *); 298103026Ssobomax 299103026Ssobomax /* 300103026Ssobomax * RFC2004 specifies that fragmented diagrams shouldn't 301103026Ssobomax * be encapsulated. 302103026Ssobomax */ 303158416Shsu if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 304103026Ssobomax _IF_DROP(&ifp->if_snd); 305103026Ssobomax m_freem(m); 306103026Ssobomax error = EINVAL; /* is there better errno? */ 307103026Ssobomax goto end; 308103026Ssobomax } 309103026Ssobomax memset(&mob_h, 0, MOB_H_SIZ_L); 310103026Ssobomax mob_h.proto = (ip->ip_p) << 8; 311103026Ssobomax mob_h.odst = ip->ip_dst.s_addr; 312103026Ssobomax ip->ip_dst.s_addr = sc->g_dst.s_addr; 313103026Ssobomax 314103026Ssobomax /* 315103026Ssobomax * If the packet comes from our host, we only change 316103026Ssobomax * the destination address in the IP header. 317103026Ssobomax * Else we also need to save and change the source 318103026Ssobomax */ 319103026Ssobomax if (in_hosteq(ip->ip_src, sc->g_src)) { 320103026Ssobomax msiz = MOB_H_SIZ_S; 321103026Ssobomax } else { 322103026Ssobomax mob_h.proto |= MOB_H_SBIT; 323103026Ssobomax mob_h.osrc = ip->ip_src.s_addr; 324103026Ssobomax ip->ip_src.s_addr = sc->g_src.s_addr; 325103026Ssobomax msiz = MOB_H_SIZ_L; 326103026Ssobomax } 327103026Ssobomax mob_h.proto = htons(mob_h.proto); 328123992Ssobomax mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz); 329103026Ssobomax 330103026Ssobomax if ((m->m_data - msiz) < m->m_pktdat) { 331103026Ssobomax /* need new mbuf */ 332151967Sandre MGETHDR(m0, M_DONTWAIT, MT_DATA); 333103026Ssobomax if (m0 == NULL) { 334103026Ssobomax _IF_DROP(&ifp->if_snd); 335103026Ssobomax m_freem(m); 336103026Ssobomax error = ENOBUFS; 337103026Ssobomax goto end; 338103026Ssobomax } 339103026Ssobomax m0->m_next = m; 340103026Ssobomax m->m_data += sizeof(struct ip); 341103026Ssobomax m->m_len -= sizeof(struct ip); 342103026Ssobomax m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 343103026Ssobomax m0->m_len = msiz + sizeof(struct ip); 344103026Ssobomax m0->m_data += max_linkhdr; 345103026Ssobomax memcpy(mtod(m0, caddr_t), (caddr_t)ip, 346103026Ssobomax sizeof(struct ip)); 347103026Ssobomax m = m0; 348103026Ssobomax } else { /* we have some space left in the old one */ 349103026Ssobomax m->m_data -= msiz; 350103026Ssobomax m->m_len += msiz; 351103026Ssobomax m->m_pkthdr.len += msiz; 352103026Ssobomax bcopy(ip, mtod(m, caddr_t), 353103026Ssobomax sizeof(struct ip)); 354103026Ssobomax } 355103026Ssobomax ip = mtod(m, struct ip *); 356103026Ssobomax memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 357103026Ssobomax ip->ip_len = ntohs(ip->ip_len) + msiz; 358103026Ssobomax } else { /* AF_INET */ 359103026Ssobomax _IF_DROP(&ifp->if_snd); 360103026Ssobomax m_freem(m); 361103026Ssobomax error = EINVAL; 362103026Ssobomax goto end; 363103026Ssobomax } 364103026Ssobomax } else if (sc->g_proto == IPPROTO_GRE) { 365103026Ssobomax switch (dst->sa_family) { 366103026Ssobomax case AF_INET: 367103026Ssobomax ip = mtod(m, struct ip *); 368180041Sjulian gre_ip_tos = ip->ip_tos; 369180041Sjulian gre_ip_id = ip->ip_id; 370180639Sjulian if (sc->wccp_ver == WCCP_V2) { 371180639Sjulian extra = sizeof(uint32_t); 372180639Sjulian etype = WCCP_PROTOCOL_TYPE; 373180639Sjulian } else { 374180639Sjulian etype = ETHERTYPE_IP; 375180639Sjulian } 376103026Ssobomax break; 377148613Sbz#ifdef INET6 378148613Sbz case AF_INET6: 379180041Sjulian gre_ip_id = ip_newid(); 380148613Sbz etype = ETHERTYPE_IPV6; 381148613Sbz break; 382148613Sbz#endif 383103026Ssobomax#ifdef NETATALK 384103026Ssobomax case AF_APPLETALK: 385103026Ssobomax etype = ETHERTYPE_ATALK; 386103026Ssobomax break; 387103026Ssobomax#endif 388103026Ssobomax default: 389103026Ssobomax _IF_DROP(&ifp->if_snd); 390103026Ssobomax m_freem(m); 391103026Ssobomax error = EAFNOSUPPORT; 392103026Ssobomax goto end; 393103026Ssobomax } 394179894Sthompsa 395179894Sthompsa /* Reserve space for GRE header + optional GRE key */ 396180639Sjulian int hdrlen = sizeof(struct greip) + extra; 397179894Sthompsa if (sc->key) 398179894Sthompsa hdrlen += sizeof(uint32_t); 399179894Sthompsa M_PREPEND(m, hdrlen, M_DONTWAIT); 400103026Ssobomax } else { 401103026Ssobomax _IF_DROP(&ifp->if_snd); 402103026Ssobomax m_freem(m); 403103026Ssobomax error = EINVAL; 404103026Ssobomax goto end; 405103026Ssobomax } 406103026Ssobomax 407128580Sandre if (m == NULL) { /* mbuf allocation failed */ 408103026Ssobomax _IF_DROP(&ifp->if_snd); 409103026Ssobomax error = ENOBUFS; 410103026Ssobomax goto end; 411103026Ssobomax } 412103026Ssobomax 413178888Sjulian M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */ 414178888Sjulian 415103026Ssobomax gh = mtod(m, struct greip *); 416103026Ssobomax if (sc->g_proto == IPPROTO_GRE) { 417179894Sthompsa uint32_t *options = gh->gi_options; 418179894Sthompsa 419180639Sjulian memset((void *)gh, 0, sizeof(struct greip) + extra); 420103026Ssobomax gh->gi_ptype = htons(etype); 421179894Sthompsa gh->gi_flags = 0; 422179894Sthompsa 423179894Sthompsa /* Add key option */ 424179894Sthompsa if (sc->key) 425179894Sthompsa { 426179894Sthompsa gh->gi_flags |= htons(GRE_KP); 427179894Sthompsa *(options++) = htonl(sc->key); 428179894Sthompsa } 429103026Ssobomax } 430103026Ssobomax 431103026Ssobomax gh->gi_pr = sc->g_proto; 432103026Ssobomax if (sc->g_proto != IPPROTO_MOBILE) { 433103026Ssobomax gh->gi_src = sc->g_src; 434103026Ssobomax gh->gi_dst = sc->g_dst; 435133163Ssobomax ((struct ip*)gh)->ip_v = IPPROTO_IPV4; 436103026Ssobomax ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 437103032Ssobomax ((struct ip*)gh)->ip_ttl = GRE_TTL; 438180041Sjulian ((struct ip*)gh)->ip_tos = gre_ip_tos; 439180041Sjulian ((struct ip*)gh)->ip_id = gre_ip_id; 440125226Ssobomax gh->gi_len = m->m_pkthdr.len; 441103026Ssobomax } 442103026Ssobomax 443103026Ssobomax ifp->if_opackets++; 444103026Ssobomax ifp->if_obytes += m->m_pkthdr.len; 445128583Sandre /* 446128583Sandre * Send it off and with IP_FORWARD flag to prevent it from 447128583Sandre * overwriting the ip_id again. ip_id is already set to the 448128583Sandre * ip_id of the encapsulated packet. 449128583Sandre */ 450128580Sandre error = ip_output(m, NULL, &sc->route, IP_FORWARDING, 451123992Ssobomax (struct ip_moptions *)NULL, (struct inpcb *)NULL); 452103026Ssobomax end: 453103026Ssobomax sc->called = 0; 454103026Ssobomax if (error) 455103026Ssobomax ifp->if_oerrors++; 456103026Ssobomax return (error); 457103026Ssobomax} 458103026Ssobomax 459103032Ssobomaxstatic int 460103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 461103026Ssobomax{ 462103026Ssobomax struct ifreq *ifr = (struct ifreq *)data; 463103026Ssobomax struct if_laddrreq *lifr = (struct if_laddrreq *)data; 464103026Ssobomax struct in_aliasreq *aifr = (struct in_aliasreq *)data; 465103026Ssobomax struct gre_softc *sc = ifp->if_softc; 466103026Ssobomax int s; 467103026Ssobomax struct sockaddr_in si; 468103026Ssobomax struct sockaddr *sa = NULL; 469179894Sthompsa int error, adj; 470103026Ssobomax struct sockaddr_in sp, sm, dp, dm; 471179894Sthompsa uint32_t key; 472103026Ssobomax 473103026Ssobomax error = 0; 474179894Sthompsa adj = 0; 475103026Ssobomax 476103026Ssobomax s = splnet(); 477103026Ssobomax switch (cmd) { 478103026Ssobomax case SIOCSIFADDR: 479103026Ssobomax ifp->if_flags |= IFF_UP; 480103026Ssobomax break; 481125020Ssobomax case SIOCSIFDSTADDR: 482103026Ssobomax break; 483103026Ssobomax case SIOCSIFFLAGS: 484164033Srwatson /* 485171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 486171056Srwatson * layer check? 487164033Srwatson */ 488164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0) 489103026Ssobomax break; 490103026Ssobomax if ((ifr->ifr_flags & IFF_LINK0) != 0) 491103026Ssobomax sc->g_proto = IPPROTO_GRE; 492103026Ssobomax else 493103026Ssobomax sc->g_proto = IPPROTO_MOBILE; 494125024Ssobomax if ((ifr->ifr_flags & IFF_LINK2) != 0) 495125024Ssobomax sc->wccp_ver = WCCP_V2; 496125024Ssobomax else 497125024Ssobomax sc->wccp_ver = WCCP_V1; 498103026Ssobomax goto recompute; 499103026Ssobomax case SIOCSIFMTU: 500164033Srwatson /* 501171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 502171056Srwatson * layer check? 503164033Srwatson */ 504164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0) 505103026Ssobomax break; 506103026Ssobomax if (ifr->ifr_mtu < 576) { 507103026Ssobomax error = EINVAL; 508103026Ssobomax break; 509103026Ssobomax } 510103026Ssobomax ifp->if_mtu = ifr->ifr_mtu; 511103026Ssobomax break; 512103026Ssobomax case SIOCGIFMTU: 513147256Sbrooks ifr->ifr_mtu = GRE2IFP(sc)->if_mtu; 514103026Ssobomax break; 515103026Ssobomax case SIOCADDMULTI: 516164033Srwatson /* 517171056Srwatson * XXXRW: Isn't this priv_checkr() redundant to the ifnet 518171056Srwatson * layer check? 519164033Srwatson */ 520164033Srwatson if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0) 521164033Srwatson break; 522164033Srwatson if (ifr == 0) { 523164033Srwatson error = EAFNOSUPPORT; 524164033Srwatson break; 525164033Srwatson } 526164033Srwatson switch (ifr->ifr_addr.sa_family) { 527164033Srwatson#ifdef INET 528164033Srwatson case AF_INET: 529164033Srwatson break; 530164033Srwatson#endif 531164033Srwatson#ifdef INET6 532164033Srwatson case AF_INET6: 533164033Srwatson break; 534164033Srwatson#endif 535164033Srwatson default: 536164033Srwatson error = EAFNOSUPPORT; 537164033Srwatson break; 538164033Srwatson } 539164033Srwatson break; 540103026Ssobomax case SIOCDELMULTI: 541164033Srwatson /* 542171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 543171056Srwatson * layer check? 544164033Srwatson */ 545164033Srwatson if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0) 546103026Ssobomax break; 547103026Ssobomax if (ifr == 0) { 548103026Ssobomax error = EAFNOSUPPORT; 549103026Ssobomax break; 550103026Ssobomax } 551103026Ssobomax switch (ifr->ifr_addr.sa_family) { 552103026Ssobomax#ifdef INET 553103026Ssobomax case AF_INET: 554103026Ssobomax break; 555103026Ssobomax#endif 556148613Sbz#ifdef INET6 557148613Sbz case AF_INET6: 558148613Sbz break; 559148613Sbz#endif 560103026Ssobomax default: 561103026Ssobomax error = EAFNOSUPPORT; 562103026Ssobomax break; 563103026Ssobomax } 564103026Ssobomax break; 565103026Ssobomax case GRESPROTO: 566164033Srwatson /* 567171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 568171056Srwatson * layer check? 569164033Srwatson */ 570164033Srwatson if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0) 571103026Ssobomax break; 572103026Ssobomax sc->g_proto = ifr->ifr_flags; 573103026Ssobomax switch (sc->g_proto) { 574103026Ssobomax case IPPROTO_GRE: 575103026Ssobomax ifp->if_flags |= IFF_LINK0; 576103026Ssobomax break; 577103026Ssobomax case IPPROTO_MOBILE: 578103026Ssobomax ifp->if_flags &= ~IFF_LINK0; 579103026Ssobomax break; 580103026Ssobomax default: 581103026Ssobomax error = EPROTONOSUPPORT; 582103026Ssobomax break; 583103026Ssobomax } 584103026Ssobomax goto recompute; 585103026Ssobomax case GREGPROTO: 586103026Ssobomax ifr->ifr_flags = sc->g_proto; 587103026Ssobomax break; 588103026Ssobomax case GRESADDRS: 589103026Ssobomax case GRESADDRD: 590164033Srwatson error = priv_check(curthread, PRIV_NET_GRE); 591164033Srwatson if (error) 592164033Srwatson return (error); 593103026Ssobomax /* 594103026Ssobomax * set tunnel endpoints, compute a less specific route 595103026Ssobomax * to the remote end and mark if as up 596103026Ssobomax */ 597103026Ssobomax sa = &ifr->ifr_addr; 598103026Ssobomax if (cmd == GRESADDRS) 599103026Ssobomax sc->g_src = (satosin(sa))->sin_addr; 600103026Ssobomax if (cmd == GRESADDRD) 601103026Ssobomax sc->g_dst = (satosin(sa))->sin_addr; 602103026Ssobomax recompute: 603103026Ssobomax#ifdef INET 604103026Ssobomax if (sc->encap != NULL) { 605103026Ssobomax encap_detach(sc->encap); 606103026Ssobomax sc->encap = NULL; 607103026Ssobomax } 608103026Ssobomax#endif 609103026Ssobomax if ((sc->g_src.s_addr != INADDR_ANY) && 610103026Ssobomax (sc->g_dst.s_addr != INADDR_ANY)) { 611103026Ssobomax bzero(&sp, sizeof(sp)); 612103026Ssobomax bzero(&sm, sizeof(sm)); 613103026Ssobomax bzero(&dp, sizeof(dp)); 614103026Ssobomax bzero(&dm, sizeof(dm)); 615103026Ssobomax sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 616103026Ssobomax sizeof(struct sockaddr_in); 617103026Ssobomax sp.sin_family = sm.sin_family = dp.sin_family = 618103026Ssobomax dm.sin_family = AF_INET; 619103026Ssobomax sp.sin_addr = sc->g_src; 620103026Ssobomax dp.sin_addr = sc->g_dst; 621125020Ssobomax sm.sin_addr.s_addr = dm.sin_addr.s_addr = 622103026Ssobomax INADDR_BROADCAST; 623103026Ssobomax#ifdef INET 624103026Ssobomax sc->encap = encap_attach(AF_INET, sc->g_proto, 625103026Ssobomax sintosa(&sp), sintosa(&sm), sintosa(&dp), 626103026Ssobomax sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 627103026Ssobomax &in_gre_protosw : &in_mobile_protosw, sc); 628103026Ssobomax if (sc->encap == NULL) 629103026Ssobomax printf("%s: unable to attach encap\n", 630147256Sbrooks if_name(GRE2IFP(sc))); 631103026Ssobomax#endif 632103026Ssobomax if (sc->route.ro_rt != 0) /* free old route */ 633103026Ssobomax RTFREE(sc->route.ro_rt); 634103026Ssobomax if (gre_compute_route(sc) == 0) 635148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 636103026Ssobomax else 637148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 638103026Ssobomax } 639103026Ssobomax break; 640103026Ssobomax case GREGADDRS: 641103026Ssobomax memset(&si, 0, sizeof(si)); 642103026Ssobomax si.sin_family = AF_INET; 643103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 644103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 645103026Ssobomax sa = sintosa(&si); 646103026Ssobomax ifr->ifr_addr = *sa; 647103026Ssobomax break; 648103026Ssobomax case GREGADDRD: 649103026Ssobomax memset(&si, 0, sizeof(si)); 650103026Ssobomax si.sin_family = AF_INET; 651103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 652103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 653103026Ssobomax sa = sintosa(&si); 654103026Ssobomax ifr->ifr_addr = *sa; 655103026Ssobomax break; 656103026Ssobomax case SIOCSIFPHYADDR: 657164033Srwatson /* 658171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 659171056Srwatson * layer check? 660164033Srwatson */ 661164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 662103026Ssobomax break; 663103026Ssobomax if (aifr->ifra_addr.sin_family != AF_INET || 664103026Ssobomax aifr->ifra_dstaddr.sin_family != AF_INET) { 665103026Ssobomax error = EAFNOSUPPORT; 666103026Ssobomax break; 667103026Ssobomax } 668103026Ssobomax if (aifr->ifra_addr.sin_len != sizeof(si) || 669103026Ssobomax aifr->ifra_dstaddr.sin_len != sizeof(si)) { 670103026Ssobomax error = EINVAL; 671103026Ssobomax break; 672103026Ssobomax } 673103026Ssobomax sc->g_src = aifr->ifra_addr.sin_addr; 674103026Ssobomax sc->g_dst = aifr->ifra_dstaddr.sin_addr; 675103026Ssobomax goto recompute; 676103026Ssobomax case SIOCSLIFPHYADDR: 677164033Srwatson /* 678171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 679171056Srwatson * layer check? 680164033Srwatson */ 681164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 682103026Ssobomax break; 683103026Ssobomax if (lifr->addr.ss_family != AF_INET || 684103026Ssobomax lifr->dstaddr.ss_family != AF_INET) { 685103026Ssobomax error = EAFNOSUPPORT; 686103026Ssobomax break; 687103026Ssobomax } 688103026Ssobomax if (lifr->addr.ss_len != sizeof(si) || 689103026Ssobomax lifr->dstaddr.ss_len != sizeof(si)) { 690103026Ssobomax error = EINVAL; 691103026Ssobomax break; 692103026Ssobomax } 693155440Sqingli sc->g_src = (satosin(&lifr->addr))->sin_addr; 694103026Ssobomax sc->g_dst = 695155440Sqingli (satosin(&lifr->dstaddr))->sin_addr; 696103026Ssobomax goto recompute; 697103026Ssobomax case SIOCDIFPHYADDR: 698164033Srwatson /* 699171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 700171056Srwatson * layer check? 701164033Srwatson */ 702164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 703103026Ssobomax break; 704103026Ssobomax sc->g_src.s_addr = INADDR_ANY; 705103026Ssobomax sc->g_dst.s_addr = INADDR_ANY; 706103026Ssobomax goto recompute; 707103026Ssobomax case SIOCGLIFPHYADDR: 708103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY || 709103026Ssobomax sc->g_dst.s_addr == INADDR_ANY) { 710103026Ssobomax error = EADDRNOTAVAIL; 711103026Ssobomax break; 712103026Ssobomax } 713103026Ssobomax memset(&si, 0, sizeof(si)); 714103026Ssobomax si.sin_family = AF_INET; 715103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 716103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 717103026Ssobomax memcpy(&lifr->addr, &si, sizeof(si)); 718103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 719103026Ssobomax memcpy(&lifr->dstaddr, &si, sizeof(si)); 720103026Ssobomax break; 721103026Ssobomax case SIOCGIFPSRCADDR: 722122699Sbms#ifdef INET6 723122699Sbms case SIOCGIFPSRCADDR_IN6: 724122699Sbms#endif 725103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY) { 726103026Ssobomax error = EADDRNOTAVAIL; 727103026Ssobomax break; 728103026Ssobomax } 729103026Ssobomax memset(&si, 0, sizeof(si)); 730103026Ssobomax si.sin_family = AF_INET; 731103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 732103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 733103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 734103026Ssobomax break; 735103026Ssobomax case SIOCGIFPDSTADDR: 736122699Sbms#ifdef INET6 737122699Sbms case SIOCGIFPDSTADDR_IN6: 738122699Sbms#endif 739103026Ssobomax if (sc->g_dst.s_addr == INADDR_ANY) { 740103026Ssobomax error = EADDRNOTAVAIL; 741103026Ssobomax break; 742103026Ssobomax } 743103026Ssobomax memset(&si, 0, sizeof(si)); 744103026Ssobomax si.sin_family = AF_INET; 745103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 746103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 747103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 748103026Ssobomax break; 749179894Sthompsa case GRESKEY: 750179894Sthompsa error = priv_check(curthread, PRIV_NET_GRE); 751179894Sthompsa if (error) 752179894Sthompsa break; 753179894Sthompsa error = copyin(ifr->ifr_data, &key, sizeof(key)); 754179894Sthompsa if (error) 755179894Sthompsa break; 756179894Sthompsa /* adjust MTU for option header */ 757179894Sthompsa if (key == 0 && sc->key != 0) /* clear */ 758179894Sthompsa adj += sizeof(key); 759179894Sthompsa else if (key != 0 && sc->key == 0) /* set */ 760179894Sthompsa adj -= sizeof(key); 761179894Sthompsa 762179894Sthompsa if (ifp->if_mtu + adj < 576) { 763179894Sthompsa error = EINVAL; 764179894Sthompsa break; 765179894Sthompsa } 766179894Sthompsa ifp->if_mtu += adj; 767179894Sthompsa sc->key = key; 768179894Sthompsa break; 769179894Sthompsa case GREGKEY: 770179894Sthompsa error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key)); 771179894Sthompsa break; 772179894Sthompsa 773103026Ssobomax default: 774103026Ssobomax error = EINVAL; 775103026Ssobomax break; 776103026Ssobomax } 777103026Ssobomax 778103026Ssobomax splx(s); 779103026Ssobomax return (error); 780103026Ssobomax} 781103026Ssobomax 782103026Ssobomax/* 783103026Ssobomax * computes a route to our destination that is not the one 784103026Ssobomax * which would be taken by ip_output(), as this one will loop back to 785103026Ssobomax * us. If the interface is p2p as a--->b, then a routing entry exists 786103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here 787123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to 788103026Ssobomax * if_gre. 789103026Ssobomax * Goal here is to compute a route to b that is less specific than 790103026Ssobomax * a-->b. We know that this one exists as in normal operation we have 791103026Ssobomax * at least a default route which matches. 792103026Ssobomax */ 793103032Ssobomaxstatic int 794103026Ssobomaxgre_compute_route(struct gre_softc *sc) 795103026Ssobomax{ 796103026Ssobomax struct route *ro; 797103026Ssobomax 798103026Ssobomax ro = &sc->route; 799103026Ssobomax 800103026Ssobomax memset(ro, 0, sizeof(struct route)); 801103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 802103026Ssobomax ro->ro_dst.sa_family = AF_INET; 803103026Ssobomax ro->ro_dst.sa_len = sizeof(ro->ro_dst); 804103026Ssobomax 805103026Ssobomax /* 806103026Ssobomax * toggle last bit, so our interface is not found, but a less 807103026Ssobomax * specific route. I'd rather like to specify a shorter mask, 808103026Ssobomax * but this is not possible. Should work though. XXX 809178888Sjulian * XXX MRT Use a different FIB for the tunnel to solve this problem. 810103026Ssobomax */ 811147256Sbrooks if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) { 812177416Sjulian ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^= 813177416Sjulian htonl(0x01); 814103026Ssobomax } 815103026Ssobomax 816103026Ssobomax#ifdef DIAGNOSTIC 817147256Sbrooks printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)), 818103026Ssobomax inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 819103026Ssobomax#endif 820103026Ssobomax 821178888Sjulian rtalloc_fib(ro, sc->gre_fibnum); 822103026Ssobomax 823103026Ssobomax /* 824103026Ssobomax * check if this returned a route at all and this route is no 825103026Ssobomax * recursion to ourself 826103026Ssobomax */ 827103026Ssobomax if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 828103026Ssobomax#ifdef DIAGNOSTIC 829103026Ssobomax if (ro->ro_rt == NULL) 830103026Ssobomax printf(" - no route found!\n"); 831103026Ssobomax else 832103026Ssobomax printf(" - route loops back to ourself!\n"); 833103026Ssobomax#endif 834103026Ssobomax return EADDRNOTAVAIL; 835103026Ssobomax } 836103026Ssobomax 837103026Ssobomax /* 838103026Ssobomax * now change it back - else ip_output will just drop 839103026Ssobomax * the route and search one to this interface ... 840103026Ssobomax */ 841147256Sbrooks if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) 842103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 843103026Ssobomax 844103026Ssobomax#ifdef DIAGNOSTIC 845103026Ssobomax printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 846103026Ssobomax inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 847103026Ssobomax printf("\n"); 848103026Ssobomax#endif 849103026Ssobomax 850103026Ssobomax return 0; 851103026Ssobomax} 852103026Ssobomax 853103026Ssobomax/* 854103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on 855103026Ssobomax * mbufs. 856103026Ssobomax */ 857123992Ssobomaxu_int16_t 858123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len) 859103026Ssobomax{ 860123992Ssobomax u_int32_t sum = 0; 861103026Ssobomax int nwords = len >> 1; 862103026Ssobomax 863103026Ssobomax while (nwords-- != 0) 864103026Ssobomax sum += *p++; 865103026Ssobomax 866103026Ssobomax if (len & 1) { 867103026Ssobomax union { 868103026Ssobomax u_short w; 869103026Ssobomax u_char c[2]; 870103026Ssobomax } u; 871103026Ssobomax u.c[0] = *(u_char *)p; 872103026Ssobomax u.c[1] = 0; 873103026Ssobomax sum += u.w; 874103026Ssobomax } 875103026Ssobomax 876103026Ssobomax /* end-around-carry */ 877103026Ssobomax sum = (sum >> 16) + (sum & 0xffff); 878103026Ssobomax sum += (sum >> 16); 879103026Ssobomax return (~sum); 880103026Ssobomax} 881103026Ssobomax 882103026Ssobomaxstatic int 883103026Ssobomaxgremodevent(module_t mod, int type, void *data) 884103026Ssobomax{ 885103026Ssobomax 886103026Ssobomax switch (type) { 887103026Ssobomax case MOD_LOAD: 888103026Ssobomax greattach(); 889103026Ssobomax break; 890103026Ssobomax case MOD_UNLOAD: 891103026Ssobomax if_clone_detach(&gre_cloner); 892127307Srwatson mtx_destroy(&gre_mtx); 893103026Ssobomax break; 894132199Sphk default: 895132199Sphk return EOPNOTSUPP; 896103026Ssobomax } 897103026Ssobomax return 0; 898103026Ssobomax} 899103026Ssobomax 900103026Ssobomaxstatic moduledata_t gre_mod = { 901103026Ssobomax "if_gre", 902103026Ssobomax gremodevent, 903103026Ssobomax 0 904103026Ssobomax}; 905103026Ssobomax 906103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 907103026SsobomaxMODULE_VERSION(if_gre, 1); 908