if_gre.c revision 207554
1123992Ssobomax/* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */ 2103026Ssobomax/* $FreeBSD: head/sys/net/if_gre.c 207554 2010-05-03 07:32:50Z sobomax $ */ 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 * 22103026Ssobomax * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23103026Ssobomax * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24103026Ssobomax * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25103026Ssobomax * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26103026Ssobomax * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27103026Ssobomax * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28103026Ssobomax * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29103026Ssobomax * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30103026Ssobomax * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31103026Ssobomax * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32103026Ssobomax * POSSIBILITY OF SUCH DAMAGE. 33103026Ssobomax */ 34103026Ssobomax 35103026Ssobomax/* 36103026Ssobomax * Encapsulate L3 protocols into IP 37148613Sbz * See RFC 2784 (successor of RFC 1701 and 1702) for more details. 38103026Ssobomax * If_gre is compatible with Cisco GRE tunnels, so you can 39103026Ssobomax * have a NetBSD box as the other end of a tunnel interface of a Cisco 40103026Ssobomax * router. See gre(4) for more details. 41103026Ssobomax * Also supported: IP in IP encaps (proto 55) as of RFC 2004 42103026Ssobomax */ 43103026Ssobomax 44103394Sbde#include "opt_atalk.h" 45103026Ssobomax#include "opt_inet.h" 46122699Sbms#include "opt_inet6.h" 47103026Ssobomax 48103026Ssobomax#include <sys/param.h> 49103026Ssobomax#include <sys/kernel.h> 50103026Ssobomax#include <sys/malloc.h> 51129880Sphk#include <sys/module.h> 52103026Ssobomax#include <sys/mbuf.h> 53164033Srwatson#include <sys/priv.h> 54178888Sjulian#include <sys/proc.h> 55103026Ssobomax#include <sys/protosw.h> 56103026Ssobomax#include <sys/socket.h> 57103026Ssobomax#include <sys/sockio.h> 58103026Ssobomax#include <sys/sysctl.h> 59103344Sbde#include <sys/systm.h> 60103026Ssobomax 61103026Ssobomax#include <net/ethernet.h> 62103026Ssobomax#include <net/if.h> 63130933Sbrooks#include <net/if_clone.h> 64103026Ssobomax#include <net/if_types.h> 65103026Ssobomax#include <net/route.h> 66196019Srwatson#include <net/vnet.h> 67103026Ssobomax 68103026Ssobomax#ifdef INET 69103026Ssobomax#include <netinet/in.h> 70103026Ssobomax#include <netinet/in_systm.h> 71103026Ssobomax#include <netinet/in_var.h> 72103026Ssobomax#include <netinet/ip.h> 73103026Ssobomax#include <netinet/ip_gre.h> 74103026Ssobomax#include <netinet/ip_var.h> 75103026Ssobomax#include <netinet/ip_encap.h> 76103026Ssobomax#else 77103026Ssobomax#error "Huh? if_gre without inet?" 78103026Ssobomax#endif 79103026Ssobomax 80103026Ssobomax#include <net/bpf.h> 81103026Ssobomax 82103026Ssobomax#include <net/if_gre.h> 83103026Ssobomax 84103026Ssobomax/* 85103026Ssobomax * It is not easy to calculate the right value for a GRE MTU. 86103026Ssobomax * We leave this task to the admin and use the same default that 87103026Ssobomax * other vendors use. 88103026Ssobomax */ 89103026Ssobomax#define GREMTU 1476 90103026Ssobomax 91103026Ssobomax#define GRENAME "gre" 92103026Ssobomax 93127307Srwatson/* 94127307Srwatson * gre_mtx protects all global variables in if_gre.c. 95127307Srwatson * XXX: gre_softc data not protected yet. 96127307Srwatson */ 97127307Srwatsonstruct mtx gre_mtx; 98103026Ssobomaxstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation"); 99103026Ssobomax 100103026Ssobomaxstruct gre_softc_head gre_softc_list; 101103026Ssobomax 102160195Ssamstatic int gre_clone_create(struct if_clone *, int, caddr_t); 103105300Salfredstatic void gre_clone_destroy(struct ifnet *); 104103032Ssobomaxstatic int gre_ioctl(struct ifnet *, u_long, caddr_t); 105103032Ssobomaxstatic int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *, 106191148Skmacy struct route *ro); 107103026Ssobomax 108130933SbrooksIFC_SIMPLE_DECLARE(gre, 0); 109103026Ssobomax 110103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc); 111103026Ssobomax 112105300Salfredstatic void greattach(void); 113103026Ssobomax 114103026Ssobomax#ifdef INET 115103026Ssobomaxextern struct domain inetdomain; 116152242Srustatic const struct protosw in_gre_protosw = { 117152242Sru .pr_type = SOCK_RAW, 118152242Sru .pr_domain = &inetdomain, 119152242Sru .pr_protocol = IPPROTO_GRE, 120152242Sru .pr_flags = PR_ATOMIC|PR_ADDR, 121154625Sbz .pr_input = gre_input, 122152242Sru .pr_output = (pr_output_t *)rip_output, 123152242Sru .pr_ctlinput = rip_ctlinput, 124152242Sru .pr_ctloutput = rip_ctloutput, 125152242Sru .pr_usrreqs = &rip_usrreqs 126103026Ssobomax}; 127152242Srustatic const struct protosw in_mobile_protosw = { 128152242Sru .pr_type = SOCK_RAW, 129152242Sru .pr_domain = &inetdomain, 130152242Sru .pr_protocol = IPPROTO_MOBILE, 131152242Sru .pr_flags = PR_ATOMIC|PR_ADDR, 132154625Sbz .pr_input = gre_mobile_input, 133152242Sru .pr_output = (pr_output_t *)rip_output, 134152242Sru .pr_ctlinput = rip_ctlinput, 135152242Sru .pr_ctloutput = rip_ctloutput, 136152242Sru .pr_usrreqs = &rip_usrreqs 137103026Ssobomax}; 138103026Ssobomax#endif 139103026Ssobomax 140103026SsobomaxSYSCTL_DECL(_net_link); 141123338SbmsSYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0, 142103026Ssobomax "Generic Routing Encapsulation"); 143103026Ssobomax#ifndef MAX_GRE_NEST 144103026Ssobomax/* 145103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels. 146103026Ssobomax * Since, setting a large value to this macro with a careless configuration 147103026Ssobomax * may introduce system crash, we don't allow any nestings by default. 148103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro 149103026Ssobomax * in your kernel configuration file. However, if you do so, please be 150103026Ssobomax * careful to configure the tunnels so that it won't make a loop. 151103026Ssobomax */ 152103026Ssobomax#define MAX_GRE_NEST 1 153103026Ssobomax#endif 154103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST; 155103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW, 156103026Ssobomax &max_gre_nesting, 0, "Max nested tunnels"); 157103026Ssobomax 158103026Ssobomax/* ARGSUSED */ 159103032Ssobomaxstatic void 160103026Ssobomaxgreattach(void) 161103026Ssobomax{ 162103026Ssobomax 163127307Srwatson mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF); 164103026Ssobomax LIST_INIT(&gre_softc_list); 165103026Ssobomax if_clone_attach(&gre_cloner); 166103026Ssobomax} 167103026Ssobomax 168103032Ssobomaxstatic int 169160195Ssamgre_clone_create(ifc, unit, params) 170103026Ssobomax struct if_clone *ifc; 171103026Ssobomax int unit; 172160195Ssam caddr_t params; 173103026Ssobomax{ 174103026Ssobomax struct gre_softc *sc; 175103026Ssobomax 176131673Sbms sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO); 177103026Ssobomax 178147643Sbz GRE2IFP(sc) = if_alloc(IFT_TUNNEL); 179147643Sbz if (GRE2IFP(sc) == NULL) { 180147643Sbz free(sc, M_GRE); 181147643Sbz return (ENOSPC); 182147643Sbz } 183147643Sbz 184147643Sbz GRE2IFP(sc)->if_softc = sc; 185147256Sbrooks if_initname(GRE2IFP(sc), ifc->ifc_name, unit); 186147643Sbz 187207554Ssobomax GRE2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen; 188147256Sbrooks GRE2IFP(sc)->if_addrlen = 0; 189147256Sbrooks GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */ 190147256Sbrooks GRE2IFP(sc)->if_mtu = GREMTU; 191147256Sbrooks GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 192147256Sbrooks GRE2IFP(sc)->if_output = gre_output; 193147256Sbrooks GRE2IFP(sc)->if_ioctl = gre_ioctl; 194103026Ssobomax sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 195103026Ssobomax sc->g_proto = IPPROTO_GRE; 196147256Sbrooks GRE2IFP(sc)->if_flags |= IFF_LINK0; 197103026Ssobomax sc->encap = NULL; 198103026Ssobomax sc->called = 0; 199178888Sjulian sc->gre_fibnum = curthread->td_proc->p_fibnum; 200125024Ssobomax sc->wccp_ver = WCCP_V1; 201179894Sthompsa sc->key = 0; 202147256Sbrooks if_attach(GRE2IFP(sc)); 203147256Sbrooks bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 204127307Srwatson mtx_lock(&gre_mtx); 205103026Ssobomax LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 206127307Srwatson mtx_unlock(&gre_mtx); 207103026Ssobomax return (0); 208103026Ssobomax} 209103026Ssobomax 210103032Ssobomaxstatic void 211127307Srwatsongre_clone_destroy(ifp) 212127307Srwatson struct ifnet *ifp; 213127307Srwatson{ 214127307Srwatson struct gre_softc *sc = ifp->if_softc; 215127307Srwatson 216127307Srwatson mtx_lock(&gre_mtx); 217127307Srwatson LIST_REMOVE(sc, sc_list); 218127307Srwatson mtx_unlock(&gre_mtx); 219151266Sthompsa 220151266Sthompsa#ifdef INET 221151266Sthompsa if (sc->encap != NULL) 222151266Sthompsa encap_detach(sc->encap); 223151266Sthompsa#endif 224151266Sthompsa bpfdetach(ifp); 225151266Sthompsa if_detach(ifp); 226151266Sthompsa if_free(ifp); 227151266Sthompsa free(sc, M_GRE); 228127307Srwatson} 229127307Srwatson 230103026Ssobomax/* 231103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol 232103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004 233103026Ssobomax */ 234103032Ssobomaxstatic int 235103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 236191148Skmacy struct route *ro) 237103026Ssobomax{ 238103026Ssobomax int error = 0; 239103026Ssobomax struct gre_softc *sc = ifp->if_softc; 240103026Ssobomax struct greip *gh; 241103026Ssobomax struct ip *ip; 242180041Sjulian u_short gre_ip_id = 0; 243180041Sjulian uint8_t gre_ip_tos = 0; 244123992Ssobomax u_int16_t etype = 0; 245103026Ssobomax struct mobile_h mob_h; 246147611Sdwmalone u_int32_t af; 247180639Sjulian int extra = 0; 248103026Ssobomax 249103026Ssobomax /* 250103026Ssobomax * gre may cause infinite recursion calls when misconfigured. 251103026Ssobomax * We'll prevent this by introducing upper limit. 252103026Ssobomax */ 253103026Ssobomax if (++(sc->called) > max_gre_nesting) { 254103026Ssobomax printf("%s: gre_output: recursively called too many " 255147256Sbrooks "times(%d)\n", if_name(GRE2IFP(sc)), sc->called); 256103026Ssobomax m_freem(m); 257103026Ssobomax error = EIO; /* is there better errno? */ 258103026Ssobomax goto end; 259103026Ssobomax } 260103026Ssobomax 261148887Srwatson if (!((ifp->if_flags & IFF_UP) && 262148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING)) || 263103026Ssobomax sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 264103026Ssobomax m_freem(m); 265103026Ssobomax error = ENETDOWN; 266103026Ssobomax goto end; 267103026Ssobomax } 268103026Ssobomax 269103026Ssobomax gh = NULL; 270103026Ssobomax ip = NULL; 271103026Ssobomax 272147611Sdwmalone /* BPF writes need to be handled specially. */ 273147611Sdwmalone if (dst->sa_family == AF_UNSPEC) { 274147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 275147611Sdwmalone dst->sa_family = af; 276147611Sdwmalone } 277147611Sdwmalone 278159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 279147611Sdwmalone af = dst->sa_family; 280123922Ssam bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 281103026Ssobomax } 282103026Ssobomax 283103026Ssobomax m->m_flags &= ~(M_BCAST|M_MCAST); 284103026Ssobomax 285103026Ssobomax if (sc->g_proto == IPPROTO_MOBILE) { 286103026Ssobomax if (dst->sa_family == AF_INET) { 287103026Ssobomax struct mbuf *m0; 288103026Ssobomax int msiz; 289103026Ssobomax 290103026Ssobomax ip = mtod(m, struct ip *); 291103026Ssobomax 292103026Ssobomax /* 293103026Ssobomax * RFC2004 specifies that fragmented diagrams shouldn't 294103026Ssobomax * be encapsulated. 295103026Ssobomax */ 296158416Shsu if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 297103026Ssobomax _IF_DROP(&ifp->if_snd); 298103026Ssobomax m_freem(m); 299103026Ssobomax error = EINVAL; /* is there better errno? */ 300103026Ssobomax goto end; 301103026Ssobomax } 302103026Ssobomax memset(&mob_h, 0, MOB_H_SIZ_L); 303103026Ssobomax mob_h.proto = (ip->ip_p) << 8; 304103026Ssobomax mob_h.odst = ip->ip_dst.s_addr; 305103026Ssobomax ip->ip_dst.s_addr = sc->g_dst.s_addr; 306103026Ssobomax 307103026Ssobomax /* 308103026Ssobomax * If the packet comes from our host, we only change 309103026Ssobomax * the destination address in the IP header. 310103026Ssobomax * Else we also need to save and change the source 311103026Ssobomax */ 312103026Ssobomax if (in_hosteq(ip->ip_src, sc->g_src)) { 313103026Ssobomax msiz = MOB_H_SIZ_S; 314103026Ssobomax } else { 315103026Ssobomax mob_h.proto |= MOB_H_SBIT; 316103026Ssobomax mob_h.osrc = ip->ip_src.s_addr; 317103026Ssobomax ip->ip_src.s_addr = sc->g_src.s_addr; 318103026Ssobomax msiz = MOB_H_SIZ_L; 319103026Ssobomax } 320103026Ssobomax mob_h.proto = htons(mob_h.proto); 321123992Ssobomax mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz); 322103026Ssobomax 323103026Ssobomax if ((m->m_data - msiz) < m->m_pktdat) { 324103026Ssobomax /* need new mbuf */ 325151967Sandre MGETHDR(m0, M_DONTWAIT, MT_DATA); 326103026Ssobomax if (m0 == NULL) { 327103026Ssobomax _IF_DROP(&ifp->if_snd); 328103026Ssobomax m_freem(m); 329103026Ssobomax error = ENOBUFS; 330103026Ssobomax goto end; 331103026Ssobomax } 332103026Ssobomax m0->m_next = m; 333103026Ssobomax m->m_data += sizeof(struct ip); 334103026Ssobomax m->m_len -= sizeof(struct ip); 335103026Ssobomax m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 336103026Ssobomax m0->m_len = msiz + sizeof(struct ip); 337103026Ssobomax m0->m_data += max_linkhdr; 338103026Ssobomax memcpy(mtod(m0, caddr_t), (caddr_t)ip, 339103026Ssobomax sizeof(struct ip)); 340103026Ssobomax m = m0; 341103026Ssobomax } else { /* we have some space left in the old one */ 342103026Ssobomax m->m_data -= msiz; 343103026Ssobomax m->m_len += msiz; 344103026Ssobomax m->m_pkthdr.len += msiz; 345103026Ssobomax bcopy(ip, mtod(m, caddr_t), 346103026Ssobomax sizeof(struct ip)); 347103026Ssobomax } 348103026Ssobomax ip = mtod(m, struct ip *); 349103026Ssobomax memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 350103026Ssobomax ip->ip_len = ntohs(ip->ip_len) + msiz; 351103026Ssobomax } else { /* AF_INET */ 352103026Ssobomax _IF_DROP(&ifp->if_snd); 353103026Ssobomax m_freem(m); 354103026Ssobomax error = EINVAL; 355103026Ssobomax goto end; 356103026Ssobomax } 357103026Ssobomax } else if (sc->g_proto == IPPROTO_GRE) { 358103026Ssobomax switch (dst->sa_family) { 359103026Ssobomax case AF_INET: 360103026Ssobomax ip = mtod(m, struct ip *); 361180041Sjulian gre_ip_tos = ip->ip_tos; 362180041Sjulian gre_ip_id = ip->ip_id; 363180639Sjulian if (sc->wccp_ver == WCCP_V2) { 364180639Sjulian extra = sizeof(uint32_t); 365180639Sjulian etype = WCCP_PROTOCOL_TYPE; 366180639Sjulian } else { 367180639Sjulian etype = ETHERTYPE_IP; 368180639Sjulian } 369103026Ssobomax break; 370148613Sbz#ifdef INET6 371148613Sbz case AF_INET6: 372180041Sjulian gre_ip_id = ip_newid(); 373148613Sbz etype = ETHERTYPE_IPV6; 374148613Sbz break; 375148613Sbz#endif 376103026Ssobomax#ifdef NETATALK 377103026Ssobomax case AF_APPLETALK: 378103026Ssobomax etype = ETHERTYPE_ATALK; 379103026Ssobomax break; 380103026Ssobomax#endif 381103026Ssobomax default: 382103026Ssobomax _IF_DROP(&ifp->if_snd); 383103026Ssobomax m_freem(m); 384103026Ssobomax error = EAFNOSUPPORT; 385103026Ssobomax goto end; 386103026Ssobomax } 387179894Sthompsa 388179894Sthompsa /* Reserve space for GRE header + optional GRE key */ 389180639Sjulian int hdrlen = sizeof(struct greip) + extra; 390179894Sthompsa if (sc->key) 391179894Sthompsa hdrlen += sizeof(uint32_t); 392179894Sthompsa M_PREPEND(m, hdrlen, M_DONTWAIT); 393103026Ssobomax } else { 394103026Ssobomax _IF_DROP(&ifp->if_snd); 395103026Ssobomax m_freem(m); 396103026Ssobomax error = EINVAL; 397103026Ssobomax goto end; 398103026Ssobomax } 399103026Ssobomax 400128580Sandre if (m == NULL) { /* mbuf allocation failed */ 401103026Ssobomax _IF_DROP(&ifp->if_snd); 402103026Ssobomax error = ENOBUFS; 403103026Ssobomax goto end; 404103026Ssobomax } 405103026Ssobomax 406178888Sjulian M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */ 407178888Sjulian 408103026Ssobomax gh = mtod(m, struct greip *); 409103026Ssobomax if (sc->g_proto == IPPROTO_GRE) { 410179894Sthompsa uint32_t *options = gh->gi_options; 411179894Sthompsa 412180639Sjulian memset((void *)gh, 0, sizeof(struct greip) + extra); 413103026Ssobomax gh->gi_ptype = htons(etype); 414179894Sthompsa gh->gi_flags = 0; 415179894Sthompsa 416179894Sthompsa /* Add key option */ 417179894Sthompsa if (sc->key) 418179894Sthompsa { 419179894Sthompsa gh->gi_flags |= htons(GRE_KP); 420179894Sthompsa *(options++) = htonl(sc->key); 421179894Sthompsa } 422103026Ssobomax } 423103026Ssobomax 424103026Ssobomax gh->gi_pr = sc->g_proto; 425103026Ssobomax if (sc->g_proto != IPPROTO_MOBILE) { 426103026Ssobomax gh->gi_src = sc->g_src; 427103026Ssobomax gh->gi_dst = sc->g_dst; 428133163Ssobomax ((struct ip*)gh)->ip_v = IPPROTO_IPV4; 429103026Ssobomax ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 430103032Ssobomax ((struct ip*)gh)->ip_ttl = GRE_TTL; 431180041Sjulian ((struct ip*)gh)->ip_tos = gre_ip_tos; 432180041Sjulian ((struct ip*)gh)->ip_id = gre_ip_id; 433125226Ssobomax gh->gi_len = m->m_pkthdr.len; 434103026Ssobomax } 435103026Ssobomax 436103026Ssobomax ifp->if_opackets++; 437103026Ssobomax ifp->if_obytes += m->m_pkthdr.len; 438128583Sandre /* 439128583Sandre * Send it off and with IP_FORWARD flag to prevent it from 440128583Sandre * overwriting the ip_id again. ip_id is already set to the 441128583Sandre * ip_id of the encapsulated packet. 442128583Sandre */ 443128580Sandre error = ip_output(m, NULL, &sc->route, IP_FORWARDING, 444123992Ssobomax (struct ip_moptions *)NULL, (struct inpcb *)NULL); 445103026Ssobomax end: 446103026Ssobomax sc->called = 0; 447103026Ssobomax if (error) 448103026Ssobomax ifp->if_oerrors++; 449103026Ssobomax return (error); 450103026Ssobomax} 451103026Ssobomax 452103032Ssobomaxstatic int 453103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 454103026Ssobomax{ 455103026Ssobomax struct ifreq *ifr = (struct ifreq *)data; 456103026Ssobomax struct if_laddrreq *lifr = (struct if_laddrreq *)data; 457103026Ssobomax struct in_aliasreq *aifr = (struct in_aliasreq *)data; 458103026Ssobomax struct gre_softc *sc = ifp->if_softc; 459103026Ssobomax int s; 460103026Ssobomax struct sockaddr_in si; 461103026Ssobomax struct sockaddr *sa = NULL; 462179894Sthompsa int error, adj; 463103026Ssobomax struct sockaddr_in sp, sm, dp, dm; 464179894Sthompsa uint32_t key; 465103026Ssobomax 466103026Ssobomax error = 0; 467179894Sthompsa adj = 0; 468103026Ssobomax 469103026Ssobomax s = splnet(); 470103026Ssobomax switch (cmd) { 471103026Ssobomax case SIOCSIFADDR: 472103026Ssobomax ifp->if_flags |= IFF_UP; 473103026Ssobomax break; 474125020Ssobomax case SIOCSIFDSTADDR: 475103026Ssobomax break; 476103026Ssobomax case SIOCSIFFLAGS: 477164033Srwatson /* 478171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 479171056Srwatson * layer check? 480164033Srwatson */ 481164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0) 482103026Ssobomax break; 483103026Ssobomax if ((ifr->ifr_flags & IFF_LINK0) != 0) 484103026Ssobomax sc->g_proto = IPPROTO_GRE; 485103026Ssobomax else 486103026Ssobomax sc->g_proto = IPPROTO_MOBILE; 487125024Ssobomax if ((ifr->ifr_flags & IFF_LINK2) != 0) 488125024Ssobomax sc->wccp_ver = WCCP_V2; 489125024Ssobomax else 490125024Ssobomax sc->wccp_ver = WCCP_V1; 491103026Ssobomax goto recompute; 492103026Ssobomax case SIOCSIFMTU: 493164033Srwatson /* 494171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 495171056Srwatson * layer check? 496164033Srwatson */ 497164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0) 498103026Ssobomax break; 499103026Ssobomax if (ifr->ifr_mtu < 576) { 500103026Ssobomax error = EINVAL; 501103026Ssobomax break; 502103026Ssobomax } 503103026Ssobomax ifp->if_mtu = ifr->ifr_mtu; 504103026Ssobomax break; 505103026Ssobomax case SIOCGIFMTU: 506147256Sbrooks ifr->ifr_mtu = GRE2IFP(sc)->if_mtu; 507103026Ssobomax break; 508103026Ssobomax case SIOCADDMULTI: 509164033Srwatson /* 510171056Srwatson * XXXRW: Isn't this priv_checkr() redundant to the ifnet 511171056Srwatson * layer check? 512164033Srwatson */ 513164033Srwatson if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0) 514164033Srwatson break; 515164033Srwatson if (ifr == 0) { 516164033Srwatson error = EAFNOSUPPORT; 517164033Srwatson break; 518164033Srwatson } 519164033Srwatson switch (ifr->ifr_addr.sa_family) { 520164033Srwatson#ifdef INET 521164033Srwatson case AF_INET: 522164033Srwatson break; 523164033Srwatson#endif 524164033Srwatson#ifdef INET6 525164033Srwatson case AF_INET6: 526164033Srwatson break; 527164033Srwatson#endif 528164033Srwatson default: 529164033Srwatson error = EAFNOSUPPORT; 530164033Srwatson break; 531164033Srwatson } 532164033Srwatson break; 533103026Ssobomax case SIOCDELMULTI: 534164033Srwatson /* 535171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 536171056Srwatson * layer check? 537164033Srwatson */ 538164033Srwatson if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0) 539103026Ssobomax break; 540103026Ssobomax if (ifr == 0) { 541103026Ssobomax error = EAFNOSUPPORT; 542103026Ssobomax break; 543103026Ssobomax } 544103026Ssobomax switch (ifr->ifr_addr.sa_family) { 545103026Ssobomax#ifdef INET 546103026Ssobomax case AF_INET: 547103026Ssobomax break; 548103026Ssobomax#endif 549148613Sbz#ifdef INET6 550148613Sbz case AF_INET6: 551148613Sbz break; 552148613Sbz#endif 553103026Ssobomax default: 554103026Ssobomax error = EAFNOSUPPORT; 555103026Ssobomax break; 556103026Ssobomax } 557103026Ssobomax break; 558103026Ssobomax case GRESPROTO: 559164033Srwatson /* 560171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 561171056Srwatson * layer check? 562164033Srwatson */ 563164033Srwatson if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0) 564103026Ssobomax break; 565103026Ssobomax sc->g_proto = ifr->ifr_flags; 566103026Ssobomax switch (sc->g_proto) { 567103026Ssobomax case IPPROTO_GRE: 568103026Ssobomax ifp->if_flags |= IFF_LINK0; 569103026Ssobomax break; 570103026Ssobomax case IPPROTO_MOBILE: 571103026Ssobomax ifp->if_flags &= ~IFF_LINK0; 572103026Ssobomax break; 573103026Ssobomax default: 574103026Ssobomax error = EPROTONOSUPPORT; 575103026Ssobomax break; 576103026Ssobomax } 577103026Ssobomax goto recompute; 578103026Ssobomax case GREGPROTO: 579103026Ssobomax ifr->ifr_flags = sc->g_proto; 580103026Ssobomax break; 581103026Ssobomax case GRESADDRS: 582103026Ssobomax case GRESADDRD: 583164033Srwatson error = priv_check(curthread, PRIV_NET_GRE); 584164033Srwatson if (error) 585164033Srwatson return (error); 586103026Ssobomax /* 587103026Ssobomax * set tunnel endpoints, compute a less specific route 588103026Ssobomax * to the remote end and mark if as up 589103026Ssobomax */ 590103026Ssobomax sa = &ifr->ifr_addr; 591103026Ssobomax if (cmd == GRESADDRS) 592103026Ssobomax sc->g_src = (satosin(sa))->sin_addr; 593103026Ssobomax if (cmd == GRESADDRD) 594103026Ssobomax sc->g_dst = (satosin(sa))->sin_addr; 595103026Ssobomax recompute: 596103026Ssobomax#ifdef INET 597103026Ssobomax if (sc->encap != NULL) { 598103026Ssobomax encap_detach(sc->encap); 599103026Ssobomax sc->encap = NULL; 600103026Ssobomax } 601103026Ssobomax#endif 602103026Ssobomax if ((sc->g_src.s_addr != INADDR_ANY) && 603103026Ssobomax (sc->g_dst.s_addr != INADDR_ANY)) { 604103026Ssobomax bzero(&sp, sizeof(sp)); 605103026Ssobomax bzero(&sm, sizeof(sm)); 606103026Ssobomax bzero(&dp, sizeof(dp)); 607103026Ssobomax bzero(&dm, sizeof(dm)); 608103026Ssobomax sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 609103026Ssobomax sizeof(struct sockaddr_in); 610103026Ssobomax sp.sin_family = sm.sin_family = dp.sin_family = 611103026Ssobomax dm.sin_family = AF_INET; 612103026Ssobomax sp.sin_addr = sc->g_src; 613103026Ssobomax dp.sin_addr = sc->g_dst; 614125020Ssobomax sm.sin_addr.s_addr = dm.sin_addr.s_addr = 615103026Ssobomax INADDR_BROADCAST; 616103026Ssobomax#ifdef INET 617103026Ssobomax sc->encap = encap_attach(AF_INET, sc->g_proto, 618103026Ssobomax sintosa(&sp), sintosa(&sm), sintosa(&dp), 619103026Ssobomax sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 620103026Ssobomax &in_gre_protosw : &in_mobile_protosw, sc); 621103026Ssobomax if (sc->encap == NULL) 622103026Ssobomax printf("%s: unable to attach encap\n", 623147256Sbrooks if_name(GRE2IFP(sc))); 624103026Ssobomax#endif 625103026Ssobomax if (sc->route.ro_rt != 0) /* free old route */ 626103026Ssobomax RTFREE(sc->route.ro_rt); 627103026Ssobomax if (gre_compute_route(sc) == 0) 628148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 629103026Ssobomax else 630148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 631103026Ssobomax } 632103026Ssobomax break; 633103026Ssobomax case GREGADDRS: 634103026Ssobomax memset(&si, 0, sizeof(si)); 635103026Ssobomax si.sin_family = AF_INET; 636103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 637103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 638103026Ssobomax sa = sintosa(&si); 639103026Ssobomax ifr->ifr_addr = *sa; 640103026Ssobomax break; 641103026Ssobomax case GREGADDRD: 642103026Ssobomax memset(&si, 0, sizeof(si)); 643103026Ssobomax si.sin_family = AF_INET; 644103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 645103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 646103026Ssobomax sa = sintosa(&si); 647103026Ssobomax ifr->ifr_addr = *sa; 648103026Ssobomax break; 649103026Ssobomax case SIOCSIFPHYADDR: 650164033Srwatson /* 651171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 652171056Srwatson * layer check? 653164033Srwatson */ 654164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 655103026Ssobomax break; 656103026Ssobomax if (aifr->ifra_addr.sin_family != AF_INET || 657103026Ssobomax aifr->ifra_dstaddr.sin_family != AF_INET) { 658103026Ssobomax error = EAFNOSUPPORT; 659103026Ssobomax break; 660103026Ssobomax } 661103026Ssobomax if (aifr->ifra_addr.sin_len != sizeof(si) || 662103026Ssobomax aifr->ifra_dstaddr.sin_len != sizeof(si)) { 663103026Ssobomax error = EINVAL; 664103026Ssobomax break; 665103026Ssobomax } 666103026Ssobomax sc->g_src = aifr->ifra_addr.sin_addr; 667103026Ssobomax sc->g_dst = aifr->ifra_dstaddr.sin_addr; 668103026Ssobomax goto recompute; 669103026Ssobomax case SIOCSLIFPHYADDR: 670164033Srwatson /* 671171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 672171056Srwatson * layer check? 673164033Srwatson */ 674164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 675103026Ssobomax break; 676103026Ssobomax if (lifr->addr.ss_family != AF_INET || 677103026Ssobomax lifr->dstaddr.ss_family != AF_INET) { 678103026Ssobomax error = EAFNOSUPPORT; 679103026Ssobomax break; 680103026Ssobomax } 681103026Ssobomax if (lifr->addr.ss_len != sizeof(si) || 682103026Ssobomax lifr->dstaddr.ss_len != sizeof(si)) { 683103026Ssobomax error = EINVAL; 684103026Ssobomax break; 685103026Ssobomax } 686155440Sqingli sc->g_src = (satosin(&lifr->addr))->sin_addr; 687103026Ssobomax sc->g_dst = 688155440Sqingli (satosin(&lifr->dstaddr))->sin_addr; 689103026Ssobomax goto recompute; 690103026Ssobomax case SIOCDIFPHYADDR: 691164033Srwatson /* 692171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 693171056Srwatson * layer check? 694164033Srwatson */ 695164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 696103026Ssobomax break; 697103026Ssobomax sc->g_src.s_addr = INADDR_ANY; 698103026Ssobomax sc->g_dst.s_addr = INADDR_ANY; 699103026Ssobomax goto recompute; 700103026Ssobomax case SIOCGLIFPHYADDR: 701103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY || 702103026Ssobomax sc->g_dst.s_addr == INADDR_ANY) { 703103026Ssobomax error = EADDRNOTAVAIL; 704103026Ssobomax break; 705103026Ssobomax } 706103026Ssobomax memset(&si, 0, sizeof(si)); 707103026Ssobomax si.sin_family = AF_INET; 708103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 709103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 710103026Ssobomax memcpy(&lifr->addr, &si, sizeof(si)); 711103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 712103026Ssobomax memcpy(&lifr->dstaddr, &si, sizeof(si)); 713103026Ssobomax break; 714103026Ssobomax case SIOCGIFPSRCADDR: 715122699Sbms#ifdef INET6 716122699Sbms case SIOCGIFPSRCADDR_IN6: 717122699Sbms#endif 718103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY) { 719103026Ssobomax error = EADDRNOTAVAIL; 720103026Ssobomax break; 721103026Ssobomax } 722103026Ssobomax memset(&si, 0, sizeof(si)); 723103026Ssobomax si.sin_family = AF_INET; 724103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 725103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 726103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 727103026Ssobomax break; 728103026Ssobomax case SIOCGIFPDSTADDR: 729122699Sbms#ifdef INET6 730122699Sbms case SIOCGIFPDSTADDR_IN6: 731122699Sbms#endif 732103026Ssobomax if (sc->g_dst.s_addr == INADDR_ANY) { 733103026Ssobomax error = EADDRNOTAVAIL; 734103026Ssobomax break; 735103026Ssobomax } 736103026Ssobomax memset(&si, 0, sizeof(si)); 737103026Ssobomax si.sin_family = AF_INET; 738103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 739103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 740103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 741103026Ssobomax break; 742179894Sthompsa case GRESKEY: 743179894Sthompsa error = priv_check(curthread, PRIV_NET_GRE); 744179894Sthompsa if (error) 745179894Sthompsa break; 746179894Sthompsa error = copyin(ifr->ifr_data, &key, sizeof(key)); 747179894Sthompsa if (error) 748179894Sthompsa break; 749179894Sthompsa /* adjust MTU for option header */ 750179894Sthompsa if (key == 0 && sc->key != 0) /* clear */ 751179894Sthompsa adj += sizeof(key); 752179894Sthompsa else if (key != 0 && sc->key == 0) /* set */ 753179894Sthompsa adj -= sizeof(key); 754179894Sthompsa 755179894Sthompsa if (ifp->if_mtu + adj < 576) { 756179894Sthompsa error = EINVAL; 757179894Sthompsa break; 758179894Sthompsa } 759179894Sthompsa ifp->if_mtu += adj; 760179894Sthompsa sc->key = key; 761179894Sthompsa break; 762179894Sthompsa case GREGKEY: 763179894Sthompsa error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key)); 764179894Sthompsa break; 765179894Sthompsa 766103026Ssobomax default: 767103026Ssobomax error = EINVAL; 768103026Ssobomax break; 769103026Ssobomax } 770103026Ssobomax 771103026Ssobomax splx(s); 772103026Ssobomax return (error); 773103026Ssobomax} 774103026Ssobomax 775103026Ssobomax/* 776103026Ssobomax * computes a route to our destination that is not the one 777103026Ssobomax * which would be taken by ip_output(), as this one will loop back to 778103026Ssobomax * us. If the interface is p2p as a--->b, then a routing entry exists 779103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here 780123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to 781103026Ssobomax * if_gre. 782103026Ssobomax * Goal here is to compute a route to b that is less specific than 783103026Ssobomax * a-->b. We know that this one exists as in normal operation we have 784103026Ssobomax * at least a default route which matches. 785103026Ssobomax */ 786103032Ssobomaxstatic int 787103026Ssobomaxgre_compute_route(struct gre_softc *sc) 788103026Ssobomax{ 789103026Ssobomax struct route *ro; 790103026Ssobomax 791103026Ssobomax ro = &sc->route; 792103026Ssobomax 793103026Ssobomax memset(ro, 0, sizeof(struct route)); 794103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 795103026Ssobomax ro->ro_dst.sa_family = AF_INET; 796103026Ssobomax ro->ro_dst.sa_len = sizeof(ro->ro_dst); 797103026Ssobomax 798103026Ssobomax /* 799103026Ssobomax * toggle last bit, so our interface is not found, but a less 800103026Ssobomax * specific route. I'd rather like to specify a shorter mask, 801103026Ssobomax * but this is not possible. Should work though. XXX 802178888Sjulian * XXX MRT Use a different FIB for the tunnel to solve this problem. 803103026Ssobomax */ 804147256Sbrooks if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) { 805177416Sjulian ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^= 806177416Sjulian htonl(0x01); 807103026Ssobomax } 808103026Ssobomax 809103026Ssobomax#ifdef DIAGNOSTIC 810147256Sbrooks printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)), 811103026Ssobomax inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 812103026Ssobomax#endif 813103026Ssobomax 814178888Sjulian rtalloc_fib(ro, sc->gre_fibnum); 815103026Ssobomax 816103026Ssobomax /* 817103026Ssobomax * check if this returned a route at all and this route is no 818103026Ssobomax * recursion to ourself 819103026Ssobomax */ 820103026Ssobomax if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 821103026Ssobomax#ifdef DIAGNOSTIC 822103026Ssobomax if (ro->ro_rt == NULL) 823103026Ssobomax printf(" - no route found!\n"); 824103026Ssobomax else 825103026Ssobomax printf(" - route loops back to ourself!\n"); 826103026Ssobomax#endif 827103026Ssobomax return EADDRNOTAVAIL; 828103026Ssobomax } 829103026Ssobomax 830103026Ssobomax /* 831103026Ssobomax * now change it back - else ip_output will just drop 832103026Ssobomax * the route and search one to this interface ... 833103026Ssobomax */ 834147256Sbrooks if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) 835103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 836103026Ssobomax 837103026Ssobomax#ifdef DIAGNOSTIC 838103026Ssobomax printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 839103026Ssobomax inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 840103026Ssobomax printf("\n"); 841103026Ssobomax#endif 842103026Ssobomax 843103026Ssobomax return 0; 844103026Ssobomax} 845103026Ssobomax 846103026Ssobomax/* 847103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on 848103026Ssobomax * mbufs. 849103026Ssobomax */ 850123992Ssobomaxu_int16_t 851123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len) 852103026Ssobomax{ 853123992Ssobomax u_int32_t sum = 0; 854103026Ssobomax int nwords = len >> 1; 855103026Ssobomax 856103026Ssobomax while (nwords-- != 0) 857103026Ssobomax sum += *p++; 858103026Ssobomax 859103026Ssobomax if (len & 1) { 860103026Ssobomax union { 861103026Ssobomax u_short w; 862103026Ssobomax u_char c[2]; 863103026Ssobomax } u; 864103026Ssobomax u.c[0] = *(u_char *)p; 865103026Ssobomax u.c[1] = 0; 866103026Ssobomax sum += u.w; 867103026Ssobomax } 868103026Ssobomax 869103026Ssobomax /* end-around-carry */ 870103026Ssobomax sum = (sum >> 16) + (sum & 0xffff); 871103026Ssobomax sum += (sum >> 16); 872103026Ssobomax return (~sum); 873103026Ssobomax} 874103026Ssobomax 875103026Ssobomaxstatic int 876103026Ssobomaxgremodevent(module_t mod, int type, void *data) 877103026Ssobomax{ 878103026Ssobomax 879103026Ssobomax switch (type) { 880103026Ssobomax case MOD_LOAD: 881103026Ssobomax greattach(); 882103026Ssobomax break; 883103026Ssobomax case MOD_UNLOAD: 884103026Ssobomax if_clone_detach(&gre_cloner); 885127307Srwatson mtx_destroy(&gre_mtx); 886103026Ssobomax break; 887132199Sphk default: 888132199Sphk return EOPNOTSUPP; 889103026Ssobomax } 890103026Ssobomax return 0; 891103026Ssobomax} 892103026Ssobomax 893103026Ssobomaxstatic moduledata_t gre_mod = { 894103026Ssobomax "if_gre", 895103026Ssobomax gremodevent, 896103026Ssobomax 0 897103026Ssobomax}; 898103026Ssobomax 899103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 900103026SsobomaxMODULE_VERSION(if_gre, 1); 901