if_gre.c revision 160195
1123992Ssobomax/* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */ 2103026Ssobomax/* $FreeBSD: head/sys/net/if_gre.c 160195 2006-07-09 06:04:01Z sam $ */ 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 107160195Ssamstatic int gre_clone_create(struct if_clone *, int, caddr_t); 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 174160195Ssamgre_clone_create(ifc, unit, params) 175103026Ssobomax struct if_clone *ifc; 176103026Ssobomax int unit; 177160195Ssam caddr_t params; 178103026Ssobomax{ 179103026Ssobomax struct gre_softc *sc; 180103026Ssobomax 181131673Sbms sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO); 182103026Ssobomax 183147643Sbz GRE2IFP(sc) = if_alloc(IFT_TUNNEL); 184147643Sbz if (GRE2IFP(sc) == NULL) { 185147643Sbz free(sc, M_GRE); 186147643Sbz return (ENOSPC); 187147643Sbz } 188147643Sbz 189147643Sbz GRE2IFP(sc)->if_softc = sc; 190147256Sbrooks if_initname(GRE2IFP(sc), ifc->ifc_name, unit); 191147643Sbz 192147256Sbrooks GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 193147256Sbrooks GRE2IFP(sc)->if_addrlen = 0; 194147256Sbrooks GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */ 195147256Sbrooks GRE2IFP(sc)->if_mtu = GREMTU; 196147256Sbrooks GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 197147256Sbrooks GRE2IFP(sc)->if_output = gre_output; 198147256Sbrooks GRE2IFP(sc)->if_ioctl = gre_ioctl; 199103026Ssobomax sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 200103026Ssobomax sc->g_proto = IPPROTO_GRE; 201147256Sbrooks GRE2IFP(sc)->if_flags |= IFF_LINK0; 202103026Ssobomax sc->encap = NULL; 203103026Ssobomax sc->called = 0; 204125024Ssobomax sc->wccp_ver = WCCP_V1; 205147256Sbrooks if_attach(GRE2IFP(sc)); 206147256Sbrooks bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 207127307Srwatson mtx_lock(&gre_mtx); 208103026Ssobomax LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 209127307Srwatson mtx_unlock(&gre_mtx); 210103026Ssobomax return (0); 211103026Ssobomax} 212103026Ssobomax 213103032Ssobomaxstatic void 214127307Srwatsongre_clone_destroy(ifp) 215127307Srwatson struct ifnet *ifp; 216127307Srwatson{ 217127307Srwatson struct gre_softc *sc = ifp->if_softc; 218127307Srwatson 219127307Srwatson mtx_lock(&gre_mtx); 220127307Srwatson LIST_REMOVE(sc, sc_list); 221127307Srwatson mtx_unlock(&gre_mtx); 222151266Sthompsa 223151266Sthompsa#ifdef INET 224151266Sthompsa if (sc->encap != NULL) 225151266Sthompsa encap_detach(sc->encap); 226151266Sthompsa#endif 227151266Sthompsa bpfdetach(ifp); 228151266Sthompsa if_detach(ifp); 229151266Sthompsa if_free(ifp); 230151266Sthompsa free(sc, M_GRE); 231127307Srwatson} 232127307Srwatson 233103026Ssobomax/* 234103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol 235103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004 236103026Ssobomax */ 237103032Ssobomaxstatic int 238103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 239103026Ssobomax struct rtentry *rt) 240103026Ssobomax{ 241103026Ssobomax int error = 0; 242103026Ssobomax struct gre_softc *sc = ifp->if_softc; 243103026Ssobomax struct greip *gh; 244103026Ssobomax struct ip *ip; 245148613Sbz u_short ip_id = 0; 246148613Sbz uint8_t ip_tos = 0; 247123992Ssobomax u_int16_t etype = 0; 248103026Ssobomax struct mobile_h mob_h; 249147611Sdwmalone u_int32_t af; 250103026Ssobomax 251103026Ssobomax /* 252103026Ssobomax * gre may cause infinite recursion calls when misconfigured. 253103026Ssobomax * We'll prevent this by introducing upper limit. 254103026Ssobomax */ 255103026Ssobomax if (++(sc->called) > max_gre_nesting) { 256103026Ssobomax printf("%s: gre_output: recursively called too many " 257147256Sbrooks "times(%d)\n", if_name(GRE2IFP(sc)), sc->called); 258103026Ssobomax m_freem(m); 259103026Ssobomax error = EIO; /* is there better errno? */ 260103026Ssobomax goto end; 261103026Ssobomax } 262103026Ssobomax 263148887Srwatson if (!((ifp->if_flags & IFF_UP) && 264148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING)) || 265103026Ssobomax sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 266103026Ssobomax m_freem(m); 267103026Ssobomax error = ENETDOWN; 268103026Ssobomax goto end; 269103026Ssobomax } 270103026Ssobomax 271103026Ssobomax gh = NULL; 272103026Ssobomax ip = NULL; 273103026Ssobomax 274147611Sdwmalone /* BPF writes need to be handled specially. */ 275147611Sdwmalone if (dst->sa_family == AF_UNSPEC) { 276147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 277147611Sdwmalone dst->sa_family = af; 278147611Sdwmalone } 279147611Sdwmalone 280159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 281147611Sdwmalone af = dst->sa_family; 282123922Ssam bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 283103026Ssobomax } 284103026Ssobomax 285103026Ssobomax m->m_flags &= ~(M_BCAST|M_MCAST); 286103026Ssobomax 287103026Ssobomax if (sc->g_proto == IPPROTO_MOBILE) { 288103026Ssobomax if (dst->sa_family == AF_INET) { 289103026Ssobomax struct mbuf *m0; 290103026Ssobomax int msiz; 291103026Ssobomax 292103026Ssobomax ip = mtod(m, struct ip *); 293103026Ssobomax 294103026Ssobomax /* 295103026Ssobomax * RFC2004 specifies that fragmented diagrams shouldn't 296103026Ssobomax * be encapsulated. 297103026Ssobomax */ 298158416Shsu if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 299103026Ssobomax _IF_DROP(&ifp->if_snd); 300103026Ssobomax m_freem(m); 301103026Ssobomax error = EINVAL; /* is there better errno? */ 302103026Ssobomax goto end; 303103026Ssobomax } 304103026Ssobomax memset(&mob_h, 0, MOB_H_SIZ_L); 305103026Ssobomax mob_h.proto = (ip->ip_p) << 8; 306103026Ssobomax mob_h.odst = ip->ip_dst.s_addr; 307103026Ssobomax ip->ip_dst.s_addr = sc->g_dst.s_addr; 308103026Ssobomax 309103026Ssobomax /* 310103026Ssobomax * If the packet comes from our host, we only change 311103026Ssobomax * the destination address in the IP header. 312103026Ssobomax * Else we also need to save and change the source 313103026Ssobomax */ 314103026Ssobomax if (in_hosteq(ip->ip_src, sc->g_src)) { 315103026Ssobomax msiz = MOB_H_SIZ_S; 316103026Ssobomax } else { 317103026Ssobomax mob_h.proto |= MOB_H_SBIT; 318103026Ssobomax mob_h.osrc = ip->ip_src.s_addr; 319103026Ssobomax ip->ip_src.s_addr = sc->g_src.s_addr; 320103026Ssobomax msiz = MOB_H_SIZ_L; 321103026Ssobomax } 322103026Ssobomax mob_h.proto = htons(mob_h.proto); 323123992Ssobomax mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz); 324103026Ssobomax 325103026Ssobomax if ((m->m_data - msiz) < m->m_pktdat) { 326103026Ssobomax /* need new mbuf */ 327151967Sandre MGETHDR(m0, M_DONTWAIT, MT_DATA); 328103026Ssobomax if (m0 == NULL) { 329103026Ssobomax _IF_DROP(&ifp->if_snd); 330103026Ssobomax m_freem(m); 331103026Ssobomax error = ENOBUFS; 332103026Ssobomax goto end; 333103026Ssobomax } 334103026Ssobomax m0->m_next = m; 335103026Ssobomax m->m_data += sizeof(struct ip); 336103026Ssobomax m->m_len -= sizeof(struct ip); 337103026Ssobomax m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 338103026Ssobomax m0->m_len = msiz + sizeof(struct ip); 339103026Ssobomax m0->m_data += max_linkhdr; 340103026Ssobomax memcpy(mtod(m0, caddr_t), (caddr_t)ip, 341103026Ssobomax sizeof(struct ip)); 342103026Ssobomax m = m0; 343103026Ssobomax } else { /* we have some space left in the old one */ 344103026Ssobomax m->m_data -= msiz; 345103026Ssobomax m->m_len += msiz; 346103026Ssobomax m->m_pkthdr.len += msiz; 347103026Ssobomax bcopy(ip, mtod(m, caddr_t), 348103026Ssobomax sizeof(struct ip)); 349103026Ssobomax } 350103026Ssobomax ip = mtod(m, struct ip *); 351103026Ssobomax memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 352103026Ssobomax ip->ip_len = ntohs(ip->ip_len) + msiz; 353103026Ssobomax } else { /* AF_INET */ 354103026Ssobomax _IF_DROP(&ifp->if_snd); 355103026Ssobomax m_freem(m); 356103026Ssobomax error = EINVAL; 357103026Ssobomax goto end; 358103026Ssobomax } 359103026Ssobomax } else if (sc->g_proto == IPPROTO_GRE) { 360103026Ssobomax switch (dst->sa_family) { 361103026Ssobomax case AF_INET: 362103026Ssobomax ip = mtod(m, struct ip *); 363148613Sbz ip_tos = ip->ip_tos; 364148613Sbz ip_id = ip->ip_id; 365103026Ssobomax etype = ETHERTYPE_IP; 366103026Ssobomax break; 367148613Sbz#ifdef INET6 368148613Sbz case AF_INET6: 369148613Sbz ip_id = ip_newid(); 370148613Sbz etype = ETHERTYPE_IPV6; 371148613Sbz break; 372148613Sbz#endif 373103026Ssobomax#ifdef NETATALK 374103026Ssobomax case AF_APPLETALK: 375103026Ssobomax etype = ETHERTYPE_ATALK; 376103026Ssobomax break; 377103026Ssobomax#endif 378103026Ssobomax default: 379103026Ssobomax _IF_DROP(&ifp->if_snd); 380103026Ssobomax m_freem(m); 381103026Ssobomax error = EAFNOSUPPORT; 382103026Ssobomax goto end; 383103026Ssobomax } 384111119Simp M_PREPEND(m, sizeof(struct greip), M_DONTWAIT); 385103026Ssobomax } else { 386103026Ssobomax _IF_DROP(&ifp->if_snd); 387103026Ssobomax m_freem(m); 388103026Ssobomax error = EINVAL; 389103026Ssobomax goto end; 390103026Ssobomax } 391103026Ssobomax 392128580Sandre if (m == NULL) { /* mbuf allocation failed */ 393103026Ssobomax _IF_DROP(&ifp->if_snd); 394103026Ssobomax error = ENOBUFS; 395103026Ssobomax goto end; 396103026Ssobomax } 397103026Ssobomax 398103026Ssobomax gh = mtod(m, struct greip *); 399103026Ssobomax if (sc->g_proto == IPPROTO_GRE) { 400103026Ssobomax /* we don't have any GRE flags for now */ 401125226Ssobomax memset((void *)gh, 0, sizeof(struct greip)); 402103026Ssobomax gh->gi_ptype = htons(etype); 403103026Ssobomax } 404103026Ssobomax 405103026Ssobomax gh->gi_pr = sc->g_proto; 406103026Ssobomax if (sc->g_proto != IPPROTO_MOBILE) { 407103026Ssobomax gh->gi_src = sc->g_src; 408103026Ssobomax gh->gi_dst = sc->g_dst; 409133163Ssobomax ((struct ip*)gh)->ip_v = IPPROTO_IPV4; 410103026Ssobomax ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 411103032Ssobomax ((struct ip*)gh)->ip_ttl = GRE_TTL; 412148613Sbz ((struct ip*)gh)->ip_tos = ip_tos; 413148613Sbz ((struct ip*)gh)->ip_id = ip_id; 414125226Ssobomax gh->gi_len = m->m_pkthdr.len; 415103026Ssobomax } 416103026Ssobomax 417103026Ssobomax ifp->if_opackets++; 418103026Ssobomax ifp->if_obytes += m->m_pkthdr.len; 419128583Sandre /* 420128583Sandre * Send it off and with IP_FORWARD flag to prevent it from 421128583Sandre * overwriting the ip_id again. ip_id is already set to the 422128583Sandre * ip_id of the encapsulated packet. 423128583Sandre */ 424128580Sandre error = ip_output(m, NULL, &sc->route, IP_FORWARDING, 425123992Ssobomax (struct ip_moptions *)NULL, (struct inpcb *)NULL); 426103026Ssobomax end: 427103026Ssobomax sc->called = 0; 428103026Ssobomax if (error) 429103026Ssobomax ifp->if_oerrors++; 430103026Ssobomax return (error); 431103026Ssobomax} 432103026Ssobomax 433103032Ssobomaxstatic int 434103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 435103026Ssobomax{ 436103026Ssobomax struct ifreq *ifr = (struct ifreq *)data; 437103026Ssobomax struct if_laddrreq *lifr = (struct if_laddrreq *)data; 438103026Ssobomax struct in_aliasreq *aifr = (struct in_aliasreq *)data; 439103026Ssobomax struct gre_softc *sc = ifp->if_softc; 440103026Ssobomax int s; 441103026Ssobomax struct sockaddr_in si; 442103026Ssobomax struct sockaddr *sa = NULL; 443103026Ssobomax int error; 444103026Ssobomax struct sockaddr_in sp, sm, dp, dm; 445103026Ssobomax 446103026Ssobomax error = 0; 447103026Ssobomax 448103026Ssobomax s = splnet(); 449103026Ssobomax switch (cmd) { 450103026Ssobomax case SIOCSIFADDR: 451103026Ssobomax ifp->if_flags |= IFF_UP; 452103026Ssobomax break; 453125020Ssobomax case SIOCSIFDSTADDR: 454103026Ssobomax break; 455103026Ssobomax case SIOCSIFFLAGS: 456103026Ssobomax if ((error = suser(curthread)) != 0) 457103026Ssobomax break; 458103026Ssobomax if ((ifr->ifr_flags & IFF_LINK0) != 0) 459103026Ssobomax sc->g_proto = IPPROTO_GRE; 460103026Ssobomax else 461103026Ssobomax sc->g_proto = IPPROTO_MOBILE; 462125024Ssobomax if ((ifr->ifr_flags & IFF_LINK2) != 0) 463125024Ssobomax sc->wccp_ver = WCCP_V2; 464125024Ssobomax else 465125024Ssobomax sc->wccp_ver = WCCP_V1; 466103026Ssobomax goto recompute; 467103026Ssobomax case SIOCSIFMTU: 468103026Ssobomax if ((error = suser(curthread)) != 0) 469103026Ssobomax break; 470103026Ssobomax if (ifr->ifr_mtu < 576) { 471103026Ssobomax error = EINVAL; 472103026Ssobomax break; 473103026Ssobomax } 474103026Ssobomax ifp->if_mtu = ifr->ifr_mtu; 475103026Ssobomax break; 476103026Ssobomax case SIOCGIFMTU: 477147256Sbrooks ifr->ifr_mtu = GRE2IFP(sc)->if_mtu; 478103026Ssobomax break; 479103026Ssobomax case SIOCADDMULTI: 480103026Ssobomax case SIOCDELMULTI: 481103026Ssobomax if ((error = suser(curthread)) != 0) 482103026Ssobomax break; 483103026Ssobomax if (ifr == 0) { 484103026Ssobomax error = EAFNOSUPPORT; 485103026Ssobomax break; 486103026Ssobomax } 487103026Ssobomax switch (ifr->ifr_addr.sa_family) { 488103026Ssobomax#ifdef INET 489103026Ssobomax case AF_INET: 490103026Ssobomax break; 491103026Ssobomax#endif 492148613Sbz#ifdef INET6 493148613Sbz case AF_INET6: 494148613Sbz break; 495148613Sbz#endif 496103026Ssobomax default: 497103026Ssobomax error = EAFNOSUPPORT; 498103026Ssobomax break; 499103026Ssobomax } 500103026Ssobomax break; 501103026Ssobomax case GRESPROTO: 502103026Ssobomax if ((error = suser(curthread)) != 0) 503103026Ssobomax break; 504103026Ssobomax sc->g_proto = ifr->ifr_flags; 505103026Ssobomax switch (sc->g_proto) { 506103026Ssobomax case IPPROTO_GRE: 507103026Ssobomax ifp->if_flags |= IFF_LINK0; 508103026Ssobomax break; 509103026Ssobomax case IPPROTO_MOBILE: 510103026Ssobomax ifp->if_flags &= ~IFF_LINK0; 511103026Ssobomax break; 512103026Ssobomax default: 513103026Ssobomax error = EPROTONOSUPPORT; 514103026Ssobomax break; 515103026Ssobomax } 516103026Ssobomax goto recompute; 517103026Ssobomax case GREGPROTO: 518103026Ssobomax ifr->ifr_flags = sc->g_proto; 519103026Ssobomax break; 520103026Ssobomax case GRESADDRS: 521103026Ssobomax case GRESADDRD: 522103026Ssobomax if ((error = suser(curthread)) != 0) 523103026Ssobomax break; 524103026Ssobomax /* 525103026Ssobomax * set tunnel endpoints, compute a less specific route 526103026Ssobomax * to the remote end and mark if as up 527103026Ssobomax */ 528103026Ssobomax sa = &ifr->ifr_addr; 529103026Ssobomax if (cmd == GRESADDRS) 530103026Ssobomax sc->g_src = (satosin(sa))->sin_addr; 531103026Ssobomax if (cmd == GRESADDRD) 532103026Ssobomax sc->g_dst = (satosin(sa))->sin_addr; 533103026Ssobomax recompute: 534103026Ssobomax#ifdef INET 535103026Ssobomax if (sc->encap != NULL) { 536103026Ssobomax encap_detach(sc->encap); 537103026Ssobomax sc->encap = NULL; 538103026Ssobomax } 539103026Ssobomax#endif 540103026Ssobomax if ((sc->g_src.s_addr != INADDR_ANY) && 541103026Ssobomax (sc->g_dst.s_addr != INADDR_ANY)) { 542103026Ssobomax bzero(&sp, sizeof(sp)); 543103026Ssobomax bzero(&sm, sizeof(sm)); 544103026Ssobomax bzero(&dp, sizeof(dp)); 545103026Ssobomax bzero(&dm, sizeof(dm)); 546103026Ssobomax sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 547103026Ssobomax sizeof(struct sockaddr_in); 548103026Ssobomax sp.sin_family = sm.sin_family = dp.sin_family = 549103026Ssobomax dm.sin_family = AF_INET; 550103026Ssobomax sp.sin_addr = sc->g_src; 551103026Ssobomax dp.sin_addr = sc->g_dst; 552125020Ssobomax sm.sin_addr.s_addr = dm.sin_addr.s_addr = 553103026Ssobomax INADDR_BROADCAST; 554103026Ssobomax#ifdef INET 555103026Ssobomax sc->encap = encap_attach(AF_INET, sc->g_proto, 556103026Ssobomax sintosa(&sp), sintosa(&sm), sintosa(&dp), 557103026Ssobomax sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 558103026Ssobomax &in_gre_protosw : &in_mobile_protosw, sc); 559103026Ssobomax if (sc->encap == NULL) 560103026Ssobomax printf("%s: unable to attach encap\n", 561147256Sbrooks if_name(GRE2IFP(sc))); 562103026Ssobomax#endif 563103026Ssobomax if (sc->route.ro_rt != 0) /* free old route */ 564103026Ssobomax RTFREE(sc->route.ro_rt); 565103026Ssobomax if (gre_compute_route(sc) == 0) 566148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 567103026Ssobomax else 568148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 569103026Ssobomax } 570103026Ssobomax break; 571103026Ssobomax case GREGADDRS: 572103026Ssobomax memset(&si, 0, sizeof(si)); 573103026Ssobomax si.sin_family = AF_INET; 574103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 575103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 576103026Ssobomax sa = sintosa(&si); 577103026Ssobomax ifr->ifr_addr = *sa; 578103026Ssobomax break; 579103026Ssobomax case GREGADDRD: 580103026Ssobomax memset(&si, 0, sizeof(si)); 581103026Ssobomax si.sin_family = AF_INET; 582103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 583103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 584103026Ssobomax sa = sintosa(&si); 585103026Ssobomax ifr->ifr_addr = *sa; 586103026Ssobomax break; 587103026Ssobomax case SIOCSIFPHYADDR: 588103026Ssobomax if ((error = suser(curthread)) != 0) 589103026Ssobomax break; 590103026Ssobomax if (aifr->ifra_addr.sin_family != AF_INET || 591103026Ssobomax aifr->ifra_dstaddr.sin_family != AF_INET) { 592103026Ssobomax error = EAFNOSUPPORT; 593103026Ssobomax break; 594103026Ssobomax } 595103026Ssobomax if (aifr->ifra_addr.sin_len != sizeof(si) || 596103026Ssobomax aifr->ifra_dstaddr.sin_len != sizeof(si)) { 597103026Ssobomax error = EINVAL; 598103026Ssobomax break; 599103026Ssobomax } 600103026Ssobomax sc->g_src = aifr->ifra_addr.sin_addr; 601103026Ssobomax sc->g_dst = aifr->ifra_dstaddr.sin_addr; 602103026Ssobomax goto recompute; 603103026Ssobomax case SIOCSLIFPHYADDR: 604103026Ssobomax if ((error = suser(curthread)) != 0) 605103026Ssobomax break; 606103026Ssobomax if (lifr->addr.ss_family != AF_INET || 607103026Ssobomax lifr->dstaddr.ss_family != AF_INET) { 608103026Ssobomax error = EAFNOSUPPORT; 609103026Ssobomax break; 610103026Ssobomax } 611103026Ssobomax if (lifr->addr.ss_len != sizeof(si) || 612103026Ssobomax lifr->dstaddr.ss_len != sizeof(si)) { 613103026Ssobomax error = EINVAL; 614103026Ssobomax break; 615103026Ssobomax } 616155440Sqingli sc->g_src = (satosin(&lifr->addr))->sin_addr; 617103026Ssobomax sc->g_dst = 618155440Sqingli (satosin(&lifr->dstaddr))->sin_addr; 619103026Ssobomax goto recompute; 620103026Ssobomax case SIOCDIFPHYADDR: 621103026Ssobomax if ((error = suser(curthread)) != 0) 622103026Ssobomax break; 623103026Ssobomax sc->g_src.s_addr = INADDR_ANY; 624103026Ssobomax sc->g_dst.s_addr = INADDR_ANY; 625103026Ssobomax goto recompute; 626103026Ssobomax case SIOCGLIFPHYADDR: 627103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY || 628103026Ssobomax sc->g_dst.s_addr == INADDR_ANY) { 629103026Ssobomax error = EADDRNOTAVAIL; 630103026Ssobomax break; 631103026Ssobomax } 632103026Ssobomax memset(&si, 0, sizeof(si)); 633103026Ssobomax si.sin_family = AF_INET; 634103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 635103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 636103026Ssobomax memcpy(&lifr->addr, &si, sizeof(si)); 637103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 638103026Ssobomax memcpy(&lifr->dstaddr, &si, sizeof(si)); 639103026Ssobomax break; 640103026Ssobomax case SIOCGIFPSRCADDR: 641122699Sbms#ifdef INET6 642122699Sbms case SIOCGIFPSRCADDR_IN6: 643122699Sbms#endif 644103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY) { 645103026Ssobomax error = EADDRNOTAVAIL; 646103026Ssobomax break; 647103026Ssobomax } 648103026Ssobomax memset(&si, 0, sizeof(si)); 649103026Ssobomax si.sin_family = AF_INET; 650103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 651103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 652103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 653103026Ssobomax break; 654103026Ssobomax case SIOCGIFPDSTADDR: 655122699Sbms#ifdef INET6 656122699Sbms case SIOCGIFPDSTADDR_IN6: 657122699Sbms#endif 658103026Ssobomax if (sc->g_dst.s_addr == INADDR_ANY) { 659103026Ssobomax error = EADDRNOTAVAIL; 660103026Ssobomax break; 661103026Ssobomax } 662103026Ssobomax memset(&si, 0, sizeof(si)); 663103026Ssobomax si.sin_family = AF_INET; 664103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 665103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 666103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 667103026Ssobomax break; 668103026Ssobomax default: 669103026Ssobomax error = EINVAL; 670103026Ssobomax break; 671103026Ssobomax } 672103026Ssobomax 673103026Ssobomax splx(s); 674103026Ssobomax return (error); 675103026Ssobomax} 676103026Ssobomax 677103026Ssobomax/* 678103026Ssobomax * computes a route to our destination that is not the one 679103026Ssobomax * which would be taken by ip_output(), as this one will loop back to 680103026Ssobomax * us. If the interface is p2p as a--->b, then a routing entry exists 681103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here 682123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to 683103026Ssobomax * if_gre. 684103026Ssobomax * Goal here is to compute a route to b that is less specific than 685103026Ssobomax * a-->b. We know that this one exists as in normal operation we have 686103026Ssobomax * at least a default route which matches. 687103026Ssobomax */ 688103032Ssobomaxstatic int 689103026Ssobomaxgre_compute_route(struct gre_softc *sc) 690103026Ssobomax{ 691103026Ssobomax struct route *ro; 692103026Ssobomax u_int32_t a, b, c; 693103026Ssobomax 694103026Ssobomax ro = &sc->route; 695103026Ssobomax 696103026Ssobomax memset(ro, 0, sizeof(struct route)); 697103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 698103026Ssobomax ro->ro_dst.sa_family = AF_INET; 699103026Ssobomax ro->ro_dst.sa_len = sizeof(ro->ro_dst); 700103026Ssobomax 701103026Ssobomax /* 702103026Ssobomax * toggle last bit, so our interface is not found, but a less 703103026Ssobomax * specific route. I'd rather like to specify a shorter mask, 704103026Ssobomax * but this is not possible. Should work though. XXX 705103026Ssobomax * there is a simpler way ... 706103026Ssobomax */ 707147256Sbrooks if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) { 708103026Ssobomax a = ntohl(sc->g_dst.s_addr); 709103026Ssobomax b = a & 0x01; 710103026Ssobomax c = a & 0xfffffffe; 711103026Ssobomax b = b ^ 0x01; 712103026Ssobomax a = b | c; 713103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr 714103026Ssobomax = htonl(a); 715103026Ssobomax } 716103026Ssobomax 717103026Ssobomax#ifdef DIAGNOSTIC 718147256Sbrooks printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)), 719103026Ssobomax inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 720103026Ssobomax#endif 721103026Ssobomax 722103026Ssobomax rtalloc(ro); 723103026Ssobomax 724103026Ssobomax /* 725103026Ssobomax * check if this returned a route at all and this route is no 726103026Ssobomax * recursion to ourself 727103026Ssobomax */ 728103026Ssobomax if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 729103026Ssobomax#ifdef DIAGNOSTIC 730103026Ssobomax if (ro->ro_rt == NULL) 731103026Ssobomax printf(" - no route found!\n"); 732103026Ssobomax else 733103026Ssobomax printf(" - route loops back to ourself!\n"); 734103026Ssobomax#endif 735103026Ssobomax return EADDRNOTAVAIL; 736103026Ssobomax } 737103026Ssobomax 738103026Ssobomax /* 739103026Ssobomax * now change it back - else ip_output will just drop 740103026Ssobomax * the route and search one to this interface ... 741103026Ssobomax */ 742147256Sbrooks if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) 743103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 744103026Ssobomax 745103026Ssobomax#ifdef DIAGNOSTIC 746103026Ssobomax printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 747103026Ssobomax inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 748103026Ssobomax printf("\n"); 749103026Ssobomax#endif 750103026Ssobomax 751103026Ssobomax return 0; 752103026Ssobomax} 753103026Ssobomax 754103026Ssobomax/* 755103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on 756103026Ssobomax * mbufs. 757103026Ssobomax */ 758123992Ssobomaxu_int16_t 759123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len) 760103026Ssobomax{ 761123992Ssobomax u_int32_t sum = 0; 762103026Ssobomax int nwords = len >> 1; 763103026Ssobomax 764103026Ssobomax while (nwords-- != 0) 765103026Ssobomax sum += *p++; 766103026Ssobomax 767103026Ssobomax if (len & 1) { 768103026Ssobomax union { 769103026Ssobomax u_short w; 770103026Ssobomax u_char c[2]; 771103026Ssobomax } u; 772103026Ssobomax u.c[0] = *(u_char *)p; 773103026Ssobomax u.c[1] = 0; 774103026Ssobomax sum += u.w; 775103026Ssobomax } 776103026Ssobomax 777103026Ssobomax /* end-around-carry */ 778103026Ssobomax sum = (sum >> 16) + (sum & 0xffff); 779103026Ssobomax sum += (sum >> 16); 780103026Ssobomax return (~sum); 781103026Ssobomax} 782103026Ssobomax 783103026Ssobomaxstatic int 784103026Ssobomaxgremodevent(module_t mod, int type, void *data) 785103026Ssobomax{ 786103026Ssobomax 787103026Ssobomax switch (type) { 788103026Ssobomax case MOD_LOAD: 789103026Ssobomax greattach(); 790103026Ssobomax break; 791103026Ssobomax case MOD_UNLOAD: 792103026Ssobomax if_clone_detach(&gre_cloner); 793127307Srwatson mtx_destroy(&gre_mtx); 794103026Ssobomax break; 795132199Sphk default: 796132199Sphk return EOPNOTSUPP; 797103026Ssobomax } 798103026Ssobomax return 0; 799103026Ssobomax} 800103026Ssobomax 801103026Ssobomaxstatic moduledata_t gre_mod = { 802103026Ssobomax "if_gre", 803103026Ssobomax gremodevent, 804103026Ssobomax 0 805103026Ssobomax}; 806103026Ssobomax 807103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 808103026SsobomaxMODULE_VERSION(if_gre, 1); 809