if_gre.c revision 159180
1123992Ssobomax/* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */ 2103026Ssobomax/* $FreeBSD: head/sys/net/if_gre.c 159180 2006-06-02 19:59:33Z csjp $ */ 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> 60103026Ssobomax#include <sys/protosw.h> 61103026Ssobomax#include <sys/socket.h> 62103026Ssobomax#include <sys/sockio.h> 63103026Ssobomax#include <sys/sysctl.h> 64103344Sbde#include <sys/systm.h> 65103026Ssobomax 66103026Ssobomax#include <net/ethernet.h> 67103026Ssobomax#include <net/if.h> 68130933Sbrooks#include <net/if_clone.h> 69103026Ssobomax#include <net/if_types.h> 70103026Ssobomax#include <net/route.h> 71103026Ssobomax 72103026Ssobomax#ifdef INET 73103026Ssobomax#include <netinet/in.h> 74103026Ssobomax#include <netinet/in_systm.h> 75103026Ssobomax#include <netinet/in_var.h> 76103026Ssobomax#include <netinet/ip.h> 77103026Ssobomax#include <netinet/ip_gre.h> 78103026Ssobomax#include <netinet/ip_var.h> 79103026Ssobomax#include <netinet/ip_encap.h> 80103026Ssobomax#else 81103026Ssobomax#error "Huh? if_gre without inet?" 82103026Ssobomax#endif 83103026Ssobomax 84103026Ssobomax#include <net/bpf.h> 85103026Ssobomax 86103026Ssobomax#include <net/net_osdep.h> 87103026Ssobomax#include <net/if_gre.h> 88103026Ssobomax 89103026Ssobomax/* 90103026Ssobomax * It is not easy to calculate the right value for a GRE MTU. 91103026Ssobomax * We leave this task to the admin and use the same default that 92103026Ssobomax * other vendors use. 93103026Ssobomax */ 94103026Ssobomax#define GREMTU 1476 95103026Ssobomax 96103026Ssobomax#define GRENAME "gre" 97103026Ssobomax 98127307Srwatson/* 99127307Srwatson * gre_mtx protects all global variables in if_gre.c. 100127307Srwatson * XXX: gre_softc data not protected yet. 101127307Srwatson */ 102127307Srwatsonstruct mtx gre_mtx; 103103026Ssobomaxstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation"); 104103026Ssobomax 105103026Ssobomaxstruct gre_softc_head gre_softc_list; 106103026Ssobomax 107105300Salfredstatic int gre_clone_create(struct if_clone *, int); 108105300Salfredstatic void gre_clone_destroy(struct ifnet *); 109103032Ssobomaxstatic int gre_ioctl(struct ifnet *, u_long, caddr_t); 110103032Ssobomaxstatic int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *, 111103032Ssobomax struct rtentry *rt); 112103026Ssobomax 113130933SbrooksIFC_SIMPLE_DECLARE(gre, 0); 114103026Ssobomax 115103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc); 116103026Ssobomax 117105300Salfredstatic void greattach(void); 118103026Ssobomax 119103026Ssobomax#ifdef INET 120103026Ssobomaxextern struct domain inetdomain; 121152242Srustatic const struct protosw in_gre_protosw = { 122152242Sru .pr_type = SOCK_RAW, 123152242Sru .pr_domain = &inetdomain, 124152242Sru .pr_protocol = IPPROTO_GRE, 125152242Sru .pr_flags = PR_ATOMIC|PR_ADDR, 126154625Sbz .pr_input = gre_input, 127152242Sru .pr_output = (pr_output_t *)rip_output, 128152242Sru .pr_ctlinput = rip_ctlinput, 129152242Sru .pr_ctloutput = rip_ctloutput, 130152242Sru .pr_usrreqs = &rip_usrreqs 131103026Ssobomax}; 132152242Srustatic const struct protosw in_mobile_protosw = { 133152242Sru .pr_type = SOCK_RAW, 134152242Sru .pr_domain = &inetdomain, 135152242Sru .pr_protocol = IPPROTO_MOBILE, 136152242Sru .pr_flags = PR_ATOMIC|PR_ADDR, 137154625Sbz .pr_input = gre_mobile_input, 138152242Sru .pr_output = (pr_output_t *)rip_output, 139152242Sru .pr_ctlinput = rip_ctlinput, 140152242Sru .pr_ctloutput = rip_ctloutput, 141152242Sru .pr_usrreqs = &rip_usrreqs 142103026Ssobomax}; 143103026Ssobomax#endif 144103026Ssobomax 145103026SsobomaxSYSCTL_DECL(_net_link); 146123338SbmsSYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0, 147103026Ssobomax "Generic Routing Encapsulation"); 148103026Ssobomax#ifndef MAX_GRE_NEST 149103026Ssobomax/* 150103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels. 151103026Ssobomax * Since, setting a large value to this macro with a careless configuration 152103026Ssobomax * may introduce system crash, we don't allow any nestings by default. 153103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro 154103026Ssobomax * in your kernel configuration file. However, if you do so, please be 155103026Ssobomax * careful to configure the tunnels so that it won't make a loop. 156103026Ssobomax */ 157103026Ssobomax#define MAX_GRE_NEST 1 158103026Ssobomax#endif 159103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST; 160103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW, 161103026Ssobomax &max_gre_nesting, 0, "Max nested tunnels"); 162103026Ssobomax 163103026Ssobomax/* ARGSUSED */ 164103032Ssobomaxstatic void 165103026Ssobomaxgreattach(void) 166103026Ssobomax{ 167103026Ssobomax 168127307Srwatson mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF); 169103026Ssobomax LIST_INIT(&gre_softc_list); 170103026Ssobomax if_clone_attach(&gre_cloner); 171103026Ssobomax} 172103026Ssobomax 173103032Ssobomaxstatic int 174103026Ssobomaxgre_clone_create(ifc, unit) 175103026Ssobomax struct if_clone *ifc; 176103026Ssobomax int unit; 177103026Ssobomax{ 178103026Ssobomax struct gre_softc *sc; 179103026Ssobomax 180131673Sbms sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO); 181103026Ssobomax 182147643Sbz GRE2IFP(sc) = if_alloc(IFT_TUNNEL); 183147643Sbz if (GRE2IFP(sc) == NULL) { 184147643Sbz free(sc, M_GRE); 185147643Sbz return (ENOSPC); 186147643Sbz } 187147643Sbz 188147643Sbz GRE2IFP(sc)->if_softc = sc; 189147256Sbrooks if_initname(GRE2IFP(sc), ifc->ifc_name, unit); 190147643Sbz 191147256Sbrooks GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 192147256Sbrooks GRE2IFP(sc)->if_addrlen = 0; 193147256Sbrooks GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */ 194147256Sbrooks GRE2IFP(sc)->if_mtu = GREMTU; 195147256Sbrooks GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 196147256Sbrooks GRE2IFP(sc)->if_output = gre_output; 197147256Sbrooks GRE2IFP(sc)->if_ioctl = gre_ioctl; 198103026Ssobomax sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 199103026Ssobomax sc->g_proto = IPPROTO_GRE; 200147256Sbrooks GRE2IFP(sc)->if_flags |= IFF_LINK0; 201103026Ssobomax sc->encap = NULL; 202103026Ssobomax sc->called = 0; 203125024Ssobomax sc->wccp_ver = WCCP_V1; 204147256Sbrooks if_attach(GRE2IFP(sc)); 205147256Sbrooks bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 206127307Srwatson mtx_lock(&gre_mtx); 207103026Ssobomax LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 208127307Srwatson mtx_unlock(&gre_mtx); 209103026Ssobomax return (0); 210103026Ssobomax} 211103026Ssobomax 212103032Ssobomaxstatic void 213127307Srwatsongre_clone_destroy(ifp) 214127307Srwatson struct ifnet *ifp; 215127307Srwatson{ 216127307Srwatson struct gre_softc *sc = ifp->if_softc; 217127307Srwatson 218127307Srwatson mtx_lock(&gre_mtx); 219127307Srwatson LIST_REMOVE(sc, sc_list); 220127307Srwatson mtx_unlock(&gre_mtx); 221151266Sthompsa 222151266Sthompsa#ifdef INET 223151266Sthompsa if (sc->encap != NULL) 224151266Sthompsa encap_detach(sc->encap); 225151266Sthompsa#endif 226151266Sthompsa bpfdetach(ifp); 227151266Sthompsa if_detach(ifp); 228151266Sthompsa if_free(ifp); 229151266Sthompsa free(sc, M_GRE); 230127307Srwatson} 231127307Srwatson 232103026Ssobomax/* 233103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol 234103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004 235103026Ssobomax */ 236103032Ssobomaxstatic int 237103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 238103026Ssobomax struct rtentry *rt) 239103026Ssobomax{ 240103026Ssobomax int error = 0; 241103026Ssobomax struct gre_softc *sc = ifp->if_softc; 242103026Ssobomax struct greip *gh; 243103026Ssobomax struct ip *ip; 244148613Sbz u_short ip_id = 0; 245148613Sbz uint8_t ip_tos = 0; 246123992Ssobomax u_int16_t etype = 0; 247103026Ssobomax struct mobile_h mob_h; 248147611Sdwmalone u_int32_t af; 249103026Ssobomax 250103026Ssobomax /* 251103026Ssobomax * gre may cause infinite recursion calls when misconfigured. 252103026Ssobomax * We'll prevent this by introducing upper limit. 253103026Ssobomax */ 254103026Ssobomax if (++(sc->called) > max_gre_nesting) { 255103026Ssobomax printf("%s: gre_output: recursively called too many " 256147256Sbrooks "times(%d)\n", if_name(GRE2IFP(sc)), sc->called); 257103026Ssobomax m_freem(m); 258103026Ssobomax error = EIO; /* is there better errno? */ 259103026Ssobomax goto end; 260103026Ssobomax } 261103026Ssobomax 262148887Srwatson if (!((ifp->if_flags & IFF_UP) && 263148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING)) || 264103026Ssobomax sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 265103026Ssobomax m_freem(m); 266103026Ssobomax error = ENETDOWN; 267103026Ssobomax goto end; 268103026Ssobomax } 269103026Ssobomax 270103026Ssobomax gh = NULL; 271103026Ssobomax ip = NULL; 272103026Ssobomax 273147611Sdwmalone /* BPF writes need to be handled specially. */ 274147611Sdwmalone if (dst->sa_family == AF_UNSPEC) { 275147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 276147611Sdwmalone dst->sa_family = af; 277147611Sdwmalone } 278147611Sdwmalone 279159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 280147611Sdwmalone af = dst->sa_family; 281123922Ssam bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 282103026Ssobomax } 283103026Ssobomax 284103026Ssobomax m->m_flags &= ~(M_BCAST|M_MCAST); 285103026Ssobomax 286103026Ssobomax if (sc->g_proto == IPPROTO_MOBILE) { 287103026Ssobomax if (dst->sa_family == AF_INET) { 288103026Ssobomax struct mbuf *m0; 289103026Ssobomax int msiz; 290103026Ssobomax 291103026Ssobomax ip = mtod(m, struct ip *); 292103026Ssobomax 293103026Ssobomax /* 294103026Ssobomax * RFC2004 specifies that fragmented diagrams shouldn't 295103026Ssobomax * be encapsulated. 296103026Ssobomax */ 297158416Shsu if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 298103026Ssobomax _IF_DROP(&ifp->if_snd); 299103026Ssobomax m_freem(m); 300103026Ssobomax error = EINVAL; /* is there better errno? */ 301103026Ssobomax goto end; 302103026Ssobomax } 303103026Ssobomax memset(&mob_h, 0, MOB_H_SIZ_L); 304103026Ssobomax mob_h.proto = (ip->ip_p) << 8; 305103026Ssobomax mob_h.odst = ip->ip_dst.s_addr; 306103026Ssobomax ip->ip_dst.s_addr = sc->g_dst.s_addr; 307103026Ssobomax 308103026Ssobomax /* 309103026Ssobomax * If the packet comes from our host, we only change 310103026Ssobomax * the destination address in the IP header. 311103026Ssobomax * Else we also need to save and change the source 312103026Ssobomax */ 313103026Ssobomax if (in_hosteq(ip->ip_src, sc->g_src)) { 314103026Ssobomax msiz = MOB_H_SIZ_S; 315103026Ssobomax } else { 316103026Ssobomax mob_h.proto |= MOB_H_SBIT; 317103026Ssobomax mob_h.osrc = ip->ip_src.s_addr; 318103026Ssobomax ip->ip_src.s_addr = sc->g_src.s_addr; 319103026Ssobomax msiz = MOB_H_SIZ_L; 320103026Ssobomax } 321103026Ssobomax mob_h.proto = htons(mob_h.proto); 322123992Ssobomax mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz); 323103026Ssobomax 324103026Ssobomax if ((m->m_data - msiz) < m->m_pktdat) { 325103026Ssobomax /* need new mbuf */ 326151967Sandre MGETHDR(m0, M_DONTWAIT, MT_DATA); 327103026Ssobomax if (m0 == NULL) { 328103026Ssobomax _IF_DROP(&ifp->if_snd); 329103026Ssobomax m_freem(m); 330103026Ssobomax error = ENOBUFS; 331103026Ssobomax goto end; 332103026Ssobomax } 333103026Ssobomax m0->m_next = m; 334103026Ssobomax m->m_data += sizeof(struct ip); 335103026Ssobomax m->m_len -= sizeof(struct ip); 336103026Ssobomax m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 337103026Ssobomax m0->m_len = msiz + sizeof(struct ip); 338103026Ssobomax m0->m_data += max_linkhdr; 339103026Ssobomax memcpy(mtod(m0, caddr_t), (caddr_t)ip, 340103026Ssobomax sizeof(struct ip)); 341103026Ssobomax m = m0; 342103026Ssobomax } else { /* we have some space left in the old one */ 343103026Ssobomax m->m_data -= msiz; 344103026Ssobomax m->m_len += msiz; 345103026Ssobomax m->m_pkthdr.len += msiz; 346103026Ssobomax bcopy(ip, mtod(m, caddr_t), 347103026Ssobomax sizeof(struct ip)); 348103026Ssobomax } 349103026Ssobomax ip = mtod(m, struct ip *); 350103026Ssobomax memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 351103026Ssobomax ip->ip_len = ntohs(ip->ip_len) + msiz; 352103026Ssobomax } else { /* AF_INET */ 353103026Ssobomax _IF_DROP(&ifp->if_snd); 354103026Ssobomax m_freem(m); 355103026Ssobomax error = EINVAL; 356103026Ssobomax goto end; 357103026Ssobomax } 358103026Ssobomax } else if (sc->g_proto == IPPROTO_GRE) { 359103026Ssobomax switch (dst->sa_family) { 360103026Ssobomax case AF_INET: 361103026Ssobomax ip = mtod(m, struct ip *); 362148613Sbz ip_tos = ip->ip_tos; 363148613Sbz ip_id = ip->ip_id; 364103026Ssobomax etype = ETHERTYPE_IP; 365103026Ssobomax break; 366148613Sbz#ifdef INET6 367148613Sbz case AF_INET6: 368148613Sbz ip_id = ip_newid(); 369148613Sbz etype = ETHERTYPE_IPV6; 370148613Sbz break; 371148613Sbz#endif 372103026Ssobomax#ifdef NETATALK 373103026Ssobomax case AF_APPLETALK: 374103026Ssobomax etype = ETHERTYPE_ATALK; 375103026Ssobomax break; 376103026Ssobomax#endif 377103026Ssobomax default: 378103026Ssobomax _IF_DROP(&ifp->if_snd); 379103026Ssobomax m_freem(m); 380103026Ssobomax error = EAFNOSUPPORT; 381103026Ssobomax goto end; 382103026Ssobomax } 383111119Simp M_PREPEND(m, sizeof(struct greip), M_DONTWAIT); 384103026Ssobomax } else { 385103026Ssobomax _IF_DROP(&ifp->if_snd); 386103026Ssobomax m_freem(m); 387103026Ssobomax error = EINVAL; 388103026Ssobomax goto end; 389103026Ssobomax } 390103026Ssobomax 391128580Sandre if (m == NULL) { /* mbuf allocation failed */ 392103026Ssobomax _IF_DROP(&ifp->if_snd); 393103026Ssobomax error = ENOBUFS; 394103026Ssobomax goto end; 395103026Ssobomax } 396103026Ssobomax 397103026Ssobomax gh = mtod(m, struct greip *); 398103026Ssobomax if (sc->g_proto == IPPROTO_GRE) { 399103026Ssobomax /* we don't have any GRE flags for now */ 400125226Ssobomax memset((void *)gh, 0, sizeof(struct greip)); 401103026Ssobomax gh->gi_ptype = htons(etype); 402103026Ssobomax } 403103026Ssobomax 404103026Ssobomax gh->gi_pr = sc->g_proto; 405103026Ssobomax if (sc->g_proto != IPPROTO_MOBILE) { 406103026Ssobomax gh->gi_src = sc->g_src; 407103026Ssobomax gh->gi_dst = sc->g_dst; 408133163Ssobomax ((struct ip*)gh)->ip_v = IPPROTO_IPV4; 409103026Ssobomax ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 410103032Ssobomax ((struct ip*)gh)->ip_ttl = GRE_TTL; 411148613Sbz ((struct ip*)gh)->ip_tos = ip_tos; 412148613Sbz ((struct ip*)gh)->ip_id = ip_id; 413125226Ssobomax gh->gi_len = m->m_pkthdr.len; 414103026Ssobomax } 415103026Ssobomax 416103026Ssobomax ifp->if_opackets++; 417103026Ssobomax ifp->if_obytes += m->m_pkthdr.len; 418128583Sandre /* 419128583Sandre * Send it off and with IP_FORWARD flag to prevent it from 420128583Sandre * overwriting the ip_id again. ip_id is already set to the 421128583Sandre * ip_id of the encapsulated packet. 422128583Sandre */ 423128580Sandre error = ip_output(m, NULL, &sc->route, IP_FORWARDING, 424123992Ssobomax (struct ip_moptions *)NULL, (struct inpcb *)NULL); 425103026Ssobomax end: 426103026Ssobomax sc->called = 0; 427103026Ssobomax if (error) 428103026Ssobomax ifp->if_oerrors++; 429103026Ssobomax return (error); 430103026Ssobomax} 431103026Ssobomax 432103032Ssobomaxstatic int 433103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 434103026Ssobomax{ 435103026Ssobomax struct ifreq *ifr = (struct ifreq *)data; 436103026Ssobomax struct if_laddrreq *lifr = (struct if_laddrreq *)data; 437103026Ssobomax struct in_aliasreq *aifr = (struct in_aliasreq *)data; 438103026Ssobomax struct gre_softc *sc = ifp->if_softc; 439103026Ssobomax int s; 440103026Ssobomax struct sockaddr_in si; 441103026Ssobomax struct sockaddr *sa = NULL; 442103026Ssobomax int error; 443103026Ssobomax struct sockaddr_in sp, sm, dp, dm; 444103026Ssobomax 445103026Ssobomax error = 0; 446103026Ssobomax 447103026Ssobomax s = splnet(); 448103026Ssobomax switch (cmd) { 449103026Ssobomax case SIOCSIFADDR: 450103026Ssobomax ifp->if_flags |= IFF_UP; 451103026Ssobomax break; 452125020Ssobomax case SIOCSIFDSTADDR: 453103026Ssobomax break; 454103026Ssobomax case SIOCSIFFLAGS: 455103026Ssobomax if ((error = suser(curthread)) != 0) 456103026Ssobomax break; 457103026Ssobomax if ((ifr->ifr_flags & IFF_LINK0) != 0) 458103026Ssobomax sc->g_proto = IPPROTO_GRE; 459103026Ssobomax else 460103026Ssobomax sc->g_proto = IPPROTO_MOBILE; 461125024Ssobomax if ((ifr->ifr_flags & IFF_LINK2) != 0) 462125024Ssobomax sc->wccp_ver = WCCP_V2; 463125024Ssobomax else 464125024Ssobomax sc->wccp_ver = WCCP_V1; 465103026Ssobomax goto recompute; 466103026Ssobomax case SIOCSIFMTU: 467103026Ssobomax if ((error = suser(curthread)) != 0) 468103026Ssobomax break; 469103026Ssobomax if (ifr->ifr_mtu < 576) { 470103026Ssobomax error = EINVAL; 471103026Ssobomax break; 472103026Ssobomax } 473103026Ssobomax ifp->if_mtu = ifr->ifr_mtu; 474103026Ssobomax break; 475103026Ssobomax case SIOCGIFMTU: 476147256Sbrooks ifr->ifr_mtu = GRE2IFP(sc)->if_mtu; 477103026Ssobomax break; 478103026Ssobomax case SIOCADDMULTI: 479103026Ssobomax case SIOCDELMULTI: 480103026Ssobomax if ((error = suser(curthread)) != 0) 481103026Ssobomax break; 482103026Ssobomax if (ifr == 0) { 483103026Ssobomax error = EAFNOSUPPORT; 484103026Ssobomax break; 485103026Ssobomax } 486103026Ssobomax switch (ifr->ifr_addr.sa_family) { 487103026Ssobomax#ifdef INET 488103026Ssobomax case AF_INET: 489103026Ssobomax break; 490103026Ssobomax#endif 491148613Sbz#ifdef INET6 492148613Sbz case AF_INET6: 493148613Sbz break; 494148613Sbz#endif 495103026Ssobomax default: 496103026Ssobomax error = EAFNOSUPPORT; 497103026Ssobomax break; 498103026Ssobomax } 499103026Ssobomax break; 500103026Ssobomax case GRESPROTO: 501103026Ssobomax if ((error = suser(curthread)) != 0) 502103026Ssobomax break; 503103026Ssobomax sc->g_proto = ifr->ifr_flags; 504103026Ssobomax switch (sc->g_proto) { 505103026Ssobomax case IPPROTO_GRE: 506103026Ssobomax ifp->if_flags |= IFF_LINK0; 507103026Ssobomax break; 508103026Ssobomax case IPPROTO_MOBILE: 509103026Ssobomax ifp->if_flags &= ~IFF_LINK0; 510103026Ssobomax break; 511103026Ssobomax default: 512103026Ssobomax error = EPROTONOSUPPORT; 513103026Ssobomax break; 514103026Ssobomax } 515103026Ssobomax goto recompute; 516103026Ssobomax case GREGPROTO: 517103026Ssobomax ifr->ifr_flags = sc->g_proto; 518103026Ssobomax break; 519103026Ssobomax case GRESADDRS: 520103026Ssobomax case GRESADDRD: 521103026Ssobomax if ((error = suser(curthread)) != 0) 522103026Ssobomax break; 523103026Ssobomax /* 524103026Ssobomax * set tunnel endpoints, compute a less specific route 525103026Ssobomax * to the remote end and mark if as up 526103026Ssobomax */ 527103026Ssobomax sa = &ifr->ifr_addr; 528103026Ssobomax if (cmd == GRESADDRS) 529103026Ssobomax sc->g_src = (satosin(sa))->sin_addr; 530103026Ssobomax if (cmd == GRESADDRD) 531103026Ssobomax sc->g_dst = (satosin(sa))->sin_addr; 532103026Ssobomax recompute: 533103026Ssobomax#ifdef INET 534103026Ssobomax if (sc->encap != NULL) { 535103026Ssobomax encap_detach(sc->encap); 536103026Ssobomax sc->encap = NULL; 537103026Ssobomax } 538103026Ssobomax#endif 539103026Ssobomax if ((sc->g_src.s_addr != INADDR_ANY) && 540103026Ssobomax (sc->g_dst.s_addr != INADDR_ANY)) { 541103026Ssobomax bzero(&sp, sizeof(sp)); 542103026Ssobomax bzero(&sm, sizeof(sm)); 543103026Ssobomax bzero(&dp, sizeof(dp)); 544103026Ssobomax bzero(&dm, sizeof(dm)); 545103026Ssobomax sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 546103026Ssobomax sizeof(struct sockaddr_in); 547103026Ssobomax sp.sin_family = sm.sin_family = dp.sin_family = 548103026Ssobomax dm.sin_family = AF_INET; 549103026Ssobomax sp.sin_addr = sc->g_src; 550103026Ssobomax dp.sin_addr = sc->g_dst; 551125020Ssobomax sm.sin_addr.s_addr = dm.sin_addr.s_addr = 552103026Ssobomax INADDR_BROADCAST; 553103026Ssobomax#ifdef INET 554103026Ssobomax sc->encap = encap_attach(AF_INET, sc->g_proto, 555103026Ssobomax sintosa(&sp), sintosa(&sm), sintosa(&dp), 556103026Ssobomax sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 557103026Ssobomax &in_gre_protosw : &in_mobile_protosw, sc); 558103026Ssobomax if (sc->encap == NULL) 559103026Ssobomax printf("%s: unable to attach encap\n", 560147256Sbrooks if_name(GRE2IFP(sc))); 561103026Ssobomax#endif 562103026Ssobomax if (sc->route.ro_rt != 0) /* free old route */ 563103026Ssobomax RTFREE(sc->route.ro_rt); 564103026Ssobomax if (gre_compute_route(sc) == 0) 565148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 566103026Ssobomax else 567148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 568103026Ssobomax } 569103026Ssobomax break; 570103026Ssobomax case GREGADDRS: 571103026Ssobomax memset(&si, 0, sizeof(si)); 572103026Ssobomax si.sin_family = AF_INET; 573103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 574103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 575103026Ssobomax sa = sintosa(&si); 576103026Ssobomax ifr->ifr_addr = *sa; 577103026Ssobomax break; 578103026Ssobomax case GREGADDRD: 579103026Ssobomax memset(&si, 0, sizeof(si)); 580103026Ssobomax si.sin_family = AF_INET; 581103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 582103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 583103026Ssobomax sa = sintosa(&si); 584103026Ssobomax ifr->ifr_addr = *sa; 585103026Ssobomax break; 586103026Ssobomax case SIOCSIFPHYADDR: 587103026Ssobomax if ((error = suser(curthread)) != 0) 588103026Ssobomax break; 589103026Ssobomax if (aifr->ifra_addr.sin_family != AF_INET || 590103026Ssobomax aifr->ifra_dstaddr.sin_family != AF_INET) { 591103026Ssobomax error = EAFNOSUPPORT; 592103026Ssobomax break; 593103026Ssobomax } 594103026Ssobomax if (aifr->ifra_addr.sin_len != sizeof(si) || 595103026Ssobomax aifr->ifra_dstaddr.sin_len != sizeof(si)) { 596103026Ssobomax error = EINVAL; 597103026Ssobomax break; 598103026Ssobomax } 599103026Ssobomax sc->g_src = aifr->ifra_addr.sin_addr; 600103026Ssobomax sc->g_dst = aifr->ifra_dstaddr.sin_addr; 601103026Ssobomax goto recompute; 602103026Ssobomax case SIOCSLIFPHYADDR: 603103026Ssobomax if ((error = suser(curthread)) != 0) 604103026Ssobomax break; 605103026Ssobomax if (lifr->addr.ss_family != AF_INET || 606103026Ssobomax lifr->dstaddr.ss_family != AF_INET) { 607103026Ssobomax error = EAFNOSUPPORT; 608103026Ssobomax break; 609103026Ssobomax } 610103026Ssobomax if (lifr->addr.ss_len != sizeof(si) || 611103026Ssobomax lifr->dstaddr.ss_len != sizeof(si)) { 612103026Ssobomax error = EINVAL; 613103026Ssobomax break; 614103026Ssobomax } 615155440Sqingli sc->g_src = (satosin(&lifr->addr))->sin_addr; 616103026Ssobomax sc->g_dst = 617155440Sqingli (satosin(&lifr->dstaddr))->sin_addr; 618103026Ssobomax goto recompute; 619103026Ssobomax case SIOCDIFPHYADDR: 620103026Ssobomax if ((error = suser(curthread)) != 0) 621103026Ssobomax break; 622103026Ssobomax sc->g_src.s_addr = INADDR_ANY; 623103026Ssobomax sc->g_dst.s_addr = INADDR_ANY; 624103026Ssobomax goto recompute; 625103026Ssobomax case SIOCGLIFPHYADDR: 626103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY || 627103026Ssobomax sc->g_dst.s_addr == INADDR_ANY) { 628103026Ssobomax error = EADDRNOTAVAIL; 629103026Ssobomax break; 630103026Ssobomax } 631103026Ssobomax memset(&si, 0, sizeof(si)); 632103026Ssobomax si.sin_family = AF_INET; 633103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 634103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 635103026Ssobomax memcpy(&lifr->addr, &si, sizeof(si)); 636103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 637103026Ssobomax memcpy(&lifr->dstaddr, &si, sizeof(si)); 638103026Ssobomax break; 639103026Ssobomax case SIOCGIFPSRCADDR: 640122699Sbms#ifdef INET6 641122699Sbms case SIOCGIFPSRCADDR_IN6: 642122699Sbms#endif 643103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY) { 644103026Ssobomax error = EADDRNOTAVAIL; 645103026Ssobomax break; 646103026Ssobomax } 647103026Ssobomax memset(&si, 0, sizeof(si)); 648103026Ssobomax si.sin_family = AF_INET; 649103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 650103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 651103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 652103026Ssobomax break; 653103026Ssobomax case SIOCGIFPDSTADDR: 654122699Sbms#ifdef INET6 655122699Sbms case SIOCGIFPDSTADDR_IN6: 656122699Sbms#endif 657103026Ssobomax if (sc->g_dst.s_addr == INADDR_ANY) { 658103026Ssobomax error = EADDRNOTAVAIL; 659103026Ssobomax break; 660103026Ssobomax } 661103026Ssobomax memset(&si, 0, sizeof(si)); 662103026Ssobomax si.sin_family = AF_INET; 663103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 664103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 665103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 666103026Ssobomax break; 667103026Ssobomax default: 668103026Ssobomax error = EINVAL; 669103026Ssobomax break; 670103026Ssobomax } 671103026Ssobomax 672103026Ssobomax splx(s); 673103026Ssobomax return (error); 674103026Ssobomax} 675103026Ssobomax 676103026Ssobomax/* 677103026Ssobomax * computes a route to our destination that is not the one 678103026Ssobomax * which would be taken by ip_output(), as this one will loop back to 679103026Ssobomax * us. If the interface is p2p as a--->b, then a routing entry exists 680103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here 681123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to 682103026Ssobomax * if_gre. 683103026Ssobomax * Goal here is to compute a route to b that is less specific than 684103026Ssobomax * a-->b. We know that this one exists as in normal operation we have 685103026Ssobomax * at least a default route which matches. 686103026Ssobomax */ 687103032Ssobomaxstatic int 688103026Ssobomaxgre_compute_route(struct gre_softc *sc) 689103026Ssobomax{ 690103026Ssobomax struct route *ro; 691103026Ssobomax u_int32_t a, b, c; 692103026Ssobomax 693103026Ssobomax ro = &sc->route; 694103026Ssobomax 695103026Ssobomax memset(ro, 0, sizeof(struct route)); 696103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 697103026Ssobomax ro->ro_dst.sa_family = AF_INET; 698103026Ssobomax ro->ro_dst.sa_len = sizeof(ro->ro_dst); 699103026Ssobomax 700103026Ssobomax /* 701103026Ssobomax * toggle last bit, so our interface is not found, but a less 702103026Ssobomax * specific route. I'd rather like to specify a shorter mask, 703103026Ssobomax * but this is not possible. Should work though. XXX 704103026Ssobomax * there is a simpler way ... 705103026Ssobomax */ 706147256Sbrooks if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) { 707103026Ssobomax a = ntohl(sc->g_dst.s_addr); 708103026Ssobomax b = a & 0x01; 709103026Ssobomax c = a & 0xfffffffe; 710103026Ssobomax b = b ^ 0x01; 711103026Ssobomax a = b | c; 712103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr 713103026Ssobomax = htonl(a); 714103026Ssobomax } 715103026Ssobomax 716103026Ssobomax#ifdef DIAGNOSTIC 717147256Sbrooks printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)), 718103026Ssobomax inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 719103026Ssobomax#endif 720103026Ssobomax 721103026Ssobomax rtalloc(ro); 722103026Ssobomax 723103026Ssobomax /* 724103026Ssobomax * check if this returned a route at all and this route is no 725103026Ssobomax * recursion to ourself 726103026Ssobomax */ 727103026Ssobomax if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 728103026Ssobomax#ifdef DIAGNOSTIC 729103026Ssobomax if (ro->ro_rt == NULL) 730103026Ssobomax printf(" - no route found!\n"); 731103026Ssobomax else 732103026Ssobomax printf(" - route loops back to ourself!\n"); 733103026Ssobomax#endif 734103026Ssobomax return EADDRNOTAVAIL; 735103026Ssobomax } 736103026Ssobomax 737103026Ssobomax /* 738103026Ssobomax * now change it back - else ip_output will just drop 739103026Ssobomax * the route and search one to this interface ... 740103026Ssobomax */ 741147256Sbrooks if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) 742103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 743103026Ssobomax 744103026Ssobomax#ifdef DIAGNOSTIC 745103026Ssobomax printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 746103026Ssobomax inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 747103026Ssobomax printf("\n"); 748103026Ssobomax#endif 749103026Ssobomax 750103026Ssobomax return 0; 751103026Ssobomax} 752103026Ssobomax 753103026Ssobomax/* 754103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on 755103026Ssobomax * mbufs. 756103026Ssobomax */ 757123992Ssobomaxu_int16_t 758123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len) 759103026Ssobomax{ 760123992Ssobomax u_int32_t sum = 0; 761103026Ssobomax int nwords = len >> 1; 762103026Ssobomax 763103026Ssobomax while (nwords-- != 0) 764103026Ssobomax sum += *p++; 765103026Ssobomax 766103026Ssobomax if (len & 1) { 767103026Ssobomax union { 768103026Ssobomax u_short w; 769103026Ssobomax u_char c[2]; 770103026Ssobomax } u; 771103026Ssobomax u.c[0] = *(u_char *)p; 772103026Ssobomax u.c[1] = 0; 773103026Ssobomax sum += u.w; 774103026Ssobomax } 775103026Ssobomax 776103026Ssobomax /* end-around-carry */ 777103026Ssobomax sum = (sum >> 16) + (sum & 0xffff); 778103026Ssobomax sum += (sum >> 16); 779103026Ssobomax return (~sum); 780103026Ssobomax} 781103026Ssobomax 782103026Ssobomaxstatic int 783103026Ssobomaxgremodevent(module_t mod, int type, void *data) 784103026Ssobomax{ 785103026Ssobomax 786103026Ssobomax switch (type) { 787103026Ssobomax case MOD_LOAD: 788103026Ssobomax greattach(); 789103026Ssobomax break; 790103026Ssobomax case MOD_UNLOAD: 791103026Ssobomax if_clone_detach(&gre_cloner); 792127307Srwatson mtx_destroy(&gre_mtx); 793103026Ssobomax break; 794132199Sphk default: 795132199Sphk return EOPNOTSUPP; 796103026Ssobomax } 797103026Ssobomax return 0; 798103026Ssobomax} 799103026Ssobomax 800103026Ssobomaxstatic moduledata_t gre_mod = { 801103026Ssobomax "if_gre", 802103026Ssobomax gremodevent, 803103026Ssobomax 0 804103026Ssobomax}; 805103026Ssobomax 806103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 807103026SsobomaxMODULE_VERSION(if_gre, 1); 808