if_gre.c revision 227309
1123992Ssobomax/* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */ 2103026Ssobomax/* $FreeBSD: head/sys/net/if_gre.c 227309 2011-11-07 15:43:11Z ed $ */ 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> 49219206Sbz#include <sys/jail.h> 50103026Ssobomax#include <sys/kernel.h> 51223223Sbz#include <sys/libkern.h> 52103026Ssobomax#include <sys/malloc.h> 53129880Sphk#include <sys/module.h> 54103026Ssobomax#include <sys/mbuf.h> 55164033Srwatson#include <sys/priv.h> 56178888Sjulian#include <sys/proc.h> 57103026Ssobomax#include <sys/protosw.h> 58103026Ssobomax#include <sys/socket.h> 59103026Ssobomax#include <sys/sockio.h> 60103026Ssobomax#include <sys/sysctl.h> 61103344Sbde#include <sys/systm.h> 62103026Ssobomax 63103026Ssobomax#include <net/ethernet.h> 64103026Ssobomax#include <net/if.h> 65130933Sbrooks#include <net/if_clone.h> 66103026Ssobomax#include <net/if_types.h> 67103026Ssobomax#include <net/route.h> 68196019Srwatson#include <net/vnet.h> 69103026Ssobomax 70103026Ssobomax#ifdef INET 71103026Ssobomax#include <netinet/in.h> 72103026Ssobomax#include <netinet/in_systm.h> 73103026Ssobomax#include <netinet/in_var.h> 74103026Ssobomax#include <netinet/ip.h> 75103026Ssobomax#include <netinet/ip_gre.h> 76103026Ssobomax#include <netinet/ip_var.h> 77103026Ssobomax#include <netinet/ip_encap.h> 78103026Ssobomax#else 79103026Ssobomax#error "Huh? if_gre without inet?" 80103026Ssobomax#endif 81103026Ssobomax 82103026Ssobomax#include <net/bpf.h> 83103026Ssobomax 84103026Ssobomax#include <net/if_gre.h> 85103026Ssobomax 86103026Ssobomax/* 87103026Ssobomax * It is not easy to calculate the right value for a GRE MTU. 88103026Ssobomax * We leave this task to the admin and use the same default that 89103026Ssobomax * other vendors use. 90103026Ssobomax */ 91103026Ssobomax#define GREMTU 1476 92103026Ssobomax 93103026Ssobomax#define GRENAME "gre" 94103026Ssobomax 95223223Sbz#define MTAG_COOKIE_GRE 1307983903 96223223Sbz#define MTAG_GRE_NESTING 1 97223223Sbzstruct mtag_gre_nesting { 98223223Sbz uint16_t count; 99223223Sbz uint16_t max; 100223223Sbz struct ifnet *ifp[]; 101223223Sbz}; 102223223Sbz 103127307Srwatson/* 104127307Srwatson * gre_mtx protects all global variables in if_gre.c. 105127307Srwatson * XXX: gre_softc data not protected yet. 106127307Srwatson */ 107127307Srwatsonstruct mtx gre_mtx; 108103026Ssobomaxstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation"); 109103026Ssobomax 110103026Ssobomaxstruct gre_softc_head gre_softc_list; 111103026Ssobomax 112160195Ssamstatic int gre_clone_create(struct if_clone *, int, caddr_t); 113105300Salfredstatic void gre_clone_destroy(struct ifnet *); 114103032Ssobomaxstatic int gre_ioctl(struct ifnet *, u_long, caddr_t); 115103032Ssobomaxstatic int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *, 116191148Skmacy struct route *ro); 117103026Ssobomax 118130933SbrooksIFC_SIMPLE_DECLARE(gre, 0); 119103026Ssobomax 120103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc); 121103026Ssobomax 122105300Salfredstatic void greattach(void); 123103026Ssobomax 124103026Ssobomax#ifdef INET 125103026Ssobomaxextern struct domain inetdomain; 126152242Srustatic const struct protosw in_gre_protosw = { 127152242Sru .pr_type = SOCK_RAW, 128152242Sru .pr_domain = &inetdomain, 129152242Sru .pr_protocol = IPPROTO_GRE, 130152242Sru .pr_flags = PR_ATOMIC|PR_ADDR, 131154625Sbz .pr_input = gre_input, 132152242Sru .pr_output = (pr_output_t *)rip_output, 133152242Sru .pr_ctlinput = rip_ctlinput, 134152242Sru .pr_ctloutput = rip_ctloutput, 135152242Sru .pr_usrreqs = &rip_usrreqs 136103026Ssobomax}; 137152242Srustatic const struct protosw in_mobile_protosw = { 138152242Sru .pr_type = SOCK_RAW, 139152242Sru .pr_domain = &inetdomain, 140152242Sru .pr_protocol = IPPROTO_MOBILE, 141152242Sru .pr_flags = PR_ATOMIC|PR_ADDR, 142154625Sbz .pr_input = gre_mobile_input, 143152242Sru .pr_output = (pr_output_t *)rip_output, 144152242Sru .pr_ctlinput = rip_ctlinput, 145152242Sru .pr_ctloutput = rip_ctloutput, 146152242Sru .pr_usrreqs = &rip_usrreqs 147103026Ssobomax}; 148103026Ssobomax#endif 149103026Ssobomax 150103026SsobomaxSYSCTL_DECL(_net_link); 151227309Sedstatic SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0, 152103026Ssobomax "Generic Routing Encapsulation"); 153103026Ssobomax#ifndef MAX_GRE_NEST 154103026Ssobomax/* 155103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels. 156103026Ssobomax * Since, setting a large value to this macro with a careless configuration 157103026Ssobomax * may introduce system crash, we don't allow any nestings by default. 158103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro 159103026Ssobomax * in your kernel configuration file. However, if you do so, please be 160103026Ssobomax * careful to configure the tunnels so that it won't make a loop. 161103026Ssobomax */ 162103026Ssobomax#define MAX_GRE_NEST 1 163103026Ssobomax#endif 164103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST; 165103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW, 166103026Ssobomax &max_gre_nesting, 0, "Max nested tunnels"); 167103026Ssobomax 168103026Ssobomax/* ARGSUSED */ 169103032Ssobomaxstatic void 170103026Ssobomaxgreattach(void) 171103026Ssobomax{ 172103026Ssobomax 173127307Srwatson mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF); 174103026Ssobomax LIST_INIT(&gre_softc_list); 175103026Ssobomax if_clone_attach(&gre_cloner); 176103026Ssobomax} 177103026Ssobomax 178103032Ssobomaxstatic int 179160195Ssamgre_clone_create(ifc, unit, params) 180103026Ssobomax struct if_clone *ifc; 181103026Ssobomax int unit; 182160195Ssam caddr_t params; 183103026Ssobomax{ 184103026Ssobomax struct gre_softc *sc; 185103026Ssobomax 186131673Sbms sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO); 187103026Ssobomax 188147643Sbz GRE2IFP(sc) = if_alloc(IFT_TUNNEL); 189147643Sbz if (GRE2IFP(sc) == NULL) { 190147643Sbz free(sc, M_GRE); 191147643Sbz return (ENOSPC); 192147643Sbz } 193147643Sbz 194147643Sbz GRE2IFP(sc)->if_softc = sc; 195147256Sbrooks if_initname(GRE2IFP(sc), ifc->ifc_name, unit); 196147643Sbz 197207554Ssobomax GRE2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen; 198147256Sbrooks GRE2IFP(sc)->if_addrlen = 0; 199147256Sbrooks GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */ 200147256Sbrooks GRE2IFP(sc)->if_mtu = GREMTU; 201147256Sbrooks GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 202147256Sbrooks GRE2IFP(sc)->if_output = gre_output; 203147256Sbrooks GRE2IFP(sc)->if_ioctl = gre_ioctl; 204103026Ssobomax sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 205103026Ssobomax sc->g_proto = IPPROTO_GRE; 206147256Sbrooks GRE2IFP(sc)->if_flags |= IFF_LINK0; 207103026Ssobomax sc->encap = NULL; 208178888Sjulian sc->gre_fibnum = curthread->td_proc->p_fibnum; 209125024Ssobomax sc->wccp_ver = WCCP_V1; 210179894Sthompsa sc->key = 0; 211147256Sbrooks if_attach(GRE2IFP(sc)); 212147256Sbrooks bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 213127307Srwatson mtx_lock(&gre_mtx); 214103026Ssobomax LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 215127307Srwatson mtx_unlock(&gre_mtx); 216103026Ssobomax return (0); 217103026Ssobomax} 218103026Ssobomax 219103032Ssobomaxstatic void 220127307Srwatsongre_clone_destroy(ifp) 221127307Srwatson struct ifnet *ifp; 222127307Srwatson{ 223127307Srwatson struct gre_softc *sc = ifp->if_softc; 224127307Srwatson 225127307Srwatson mtx_lock(&gre_mtx); 226127307Srwatson LIST_REMOVE(sc, sc_list); 227127307Srwatson mtx_unlock(&gre_mtx); 228151266Sthompsa 229151266Sthompsa#ifdef INET 230151266Sthompsa if (sc->encap != NULL) 231151266Sthompsa encap_detach(sc->encap); 232151266Sthompsa#endif 233151266Sthompsa bpfdetach(ifp); 234151266Sthompsa if_detach(ifp); 235151266Sthompsa if_free(ifp); 236151266Sthompsa free(sc, M_GRE); 237127307Srwatson} 238127307Srwatson 239103026Ssobomax/* 240103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol 241103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004 242103026Ssobomax */ 243103032Ssobomaxstatic int 244103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 245191148Skmacy struct route *ro) 246103026Ssobomax{ 247103026Ssobomax int error = 0; 248103026Ssobomax struct gre_softc *sc = ifp->if_softc; 249103026Ssobomax struct greip *gh; 250103026Ssobomax struct ip *ip; 251223223Sbz struct m_tag *mtag; 252223223Sbz struct mtag_gre_nesting *gt; 253223223Sbz size_t len; 254180041Sjulian u_short gre_ip_id = 0; 255180041Sjulian uint8_t gre_ip_tos = 0; 256123992Ssobomax u_int16_t etype = 0; 257103026Ssobomax struct mobile_h mob_h; 258147611Sdwmalone u_int32_t af; 259223223Sbz int extra = 0, max; 260103026Ssobomax 261103026Ssobomax /* 262223223Sbz * gre may cause infinite recursion calls when misconfigured. High 263223223Sbz * nesting level may cause stack exhaustion. We'll prevent this by 264223223Sbz * detecting loops and by introducing upper limit. 265103026Ssobomax */ 266223223Sbz mtag = m_tag_locate(m, MTAG_COOKIE_GRE, MTAG_GRE_NESTING, NULL); 267223223Sbz if (mtag != NULL) { 268223223Sbz struct ifnet **ifp2; 269223223Sbz 270223223Sbz gt = (struct mtag_gre_nesting *)(mtag + 1); 271223223Sbz gt->count++; 272223223Sbz if (gt->count > min(gt->max,max_gre_nesting)) { 273223223Sbz printf("%s: hit maximum recursion limit %u on %s\n", 274223223Sbz __func__, gt->count - 1, ifp->if_xname); 275223223Sbz m_freem(m); 276223223Sbz error = EIO; /* is there better errno? */ 277223223Sbz goto end; 278223223Sbz } 279223223Sbz 280223223Sbz ifp2 = gt->ifp; 281223223Sbz for (max = gt->count - 1; max > 0; max--) { 282223223Sbz if (*ifp2 == ifp) 283223223Sbz break; 284223223Sbz ifp2++; 285223223Sbz } 286223223Sbz if (*ifp2 == ifp) { 287223223Sbz printf("%s: detected loop with nexting %u on %s\n", 288223223Sbz __func__, gt->count-1, ifp->if_xname); 289223223Sbz m_freem(m); 290223223Sbz error = EIO; /* is there better errno? */ 291223223Sbz goto end; 292223223Sbz } 293223223Sbz *ifp2 = ifp; 294223223Sbz 295223223Sbz } else { 296223223Sbz /* 297223223Sbz * Given that people should NOT increase max_gre_nesting beyond 298223223Sbz * their real needs, we allocate once per packet rather than 299223223Sbz * allocating an mtag once per passing through gre. 300223223Sbz * 301223223Sbz * Note: the sysctl does not actually check for saneness, so we 302223223Sbz * limit the maximum numbers of possible recursions here. 303223223Sbz */ 304223223Sbz max = imin(max_gre_nesting, 256); 305223223Sbz /* If someone sets the sysctl <= 0, we want at least 1. */ 306223223Sbz max = imax(max, 1); 307223223Sbz len = sizeof(struct mtag_gre_nesting) + 308223223Sbz max * sizeof(struct ifnet *); 309223223Sbz mtag = m_tag_alloc(MTAG_COOKIE_GRE, MTAG_GRE_NESTING, len, 310223223Sbz M_NOWAIT); 311223223Sbz if (mtag == NULL) { 312223223Sbz m_freem(m); 313223223Sbz error = ENOMEM; 314223223Sbz goto end; 315223223Sbz } 316223223Sbz gt = (struct mtag_gre_nesting *)(mtag + 1); 317223223Sbz bzero(gt, len); 318223223Sbz gt->count = 1; 319223223Sbz gt->max = max; 320223223Sbz *gt->ifp = ifp; 321223223Sbz m_tag_prepend(m, mtag); 322103026Ssobomax } 323103026Ssobomax 324148887Srwatson if (!((ifp->if_flags & IFF_UP) && 325148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING)) || 326103026Ssobomax sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 327103026Ssobomax m_freem(m); 328103026Ssobomax error = ENETDOWN; 329103026Ssobomax goto end; 330103026Ssobomax } 331103026Ssobomax 332103026Ssobomax gh = NULL; 333103026Ssobomax ip = NULL; 334103026Ssobomax 335147611Sdwmalone /* BPF writes need to be handled specially. */ 336147611Sdwmalone if (dst->sa_family == AF_UNSPEC) { 337147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 338147611Sdwmalone dst->sa_family = af; 339147611Sdwmalone } 340147611Sdwmalone 341159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 342147611Sdwmalone af = dst->sa_family; 343123922Ssam bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 344103026Ssobomax } 345103026Ssobomax 346103026Ssobomax m->m_flags &= ~(M_BCAST|M_MCAST); 347103026Ssobomax 348103026Ssobomax if (sc->g_proto == IPPROTO_MOBILE) { 349103026Ssobomax if (dst->sa_family == AF_INET) { 350103026Ssobomax struct mbuf *m0; 351103026Ssobomax int msiz; 352103026Ssobomax 353103026Ssobomax ip = mtod(m, struct ip *); 354103026Ssobomax 355103026Ssobomax /* 356103026Ssobomax * RFC2004 specifies that fragmented diagrams shouldn't 357103026Ssobomax * be encapsulated. 358103026Ssobomax */ 359158416Shsu if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 360103026Ssobomax _IF_DROP(&ifp->if_snd); 361103026Ssobomax m_freem(m); 362103026Ssobomax error = EINVAL; /* is there better errno? */ 363103026Ssobomax goto end; 364103026Ssobomax } 365103026Ssobomax memset(&mob_h, 0, MOB_H_SIZ_L); 366103026Ssobomax mob_h.proto = (ip->ip_p) << 8; 367103026Ssobomax mob_h.odst = ip->ip_dst.s_addr; 368103026Ssobomax ip->ip_dst.s_addr = sc->g_dst.s_addr; 369103026Ssobomax 370103026Ssobomax /* 371103026Ssobomax * If the packet comes from our host, we only change 372103026Ssobomax * the destination address in the IP header. 373103026Ssobomax * Else we also need to save and change the source 374103026Ssobomax */ 375103026Ssobomax if (in_hosteq(ip->ip_src, sc->g_src)) { 376103026Ssobomax msiz = MOB_H_SIZ_S; 377103026Ssobomax } else { 378103026Ssobomax mob_h.proto |= MOB_H_SBIT; 379103026Ssobomax mob_h.osrc = ip->ip_src.s_addr; 380103026Ssobomax ip->ip_src.s_addr = sc->g_src.s_addr; 381103026Ssobomax msiz = MOB_H_SIZ_L; 382103026Ssobomax } 383103026Ssobomax mob_h.proto = htons(mob_h.proto); 384123992Ssobomax mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz); 385103026Ssobomax 386103026Ssobomax if ((m->m_data - msiz) < m->m_pktdat) { 387103026Ssobomax /* need new mbuf */ 388151967Sandre MGETHDR(m0, M_DONTWAIT, MT_DATA); 389103026Ssobomax if (m0 == NULL) { 390103026Ssobomax _IF_DROP(&ifp->if_snd); 391103026Ssobomax m_freem(m); 392103026Ssobomax error = ENOBUFS; 393103026Ssobomax goto end; 394103026Ssobomax } 395103026Ssobomax m0->m_next = m; 396103026Ssobomax m->m_data += sizeof(struct ip); 397103026Ssobomax m->m_len -= sizeof(struct ip); 398103026Ssobomax m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 399103026Ssobomax m0->m_len = msiz + sizeof(struct ip); 400103026Ssobomax m0->m_data += max_linkhdr; 401103026Ssobomax memcpy(mtod(m0, caddr_t), (caddr_t)ip, 402103026Ssobomax sizeof(struct ip)); 403103026Ssobomax m = m0; 404103026Ssobomax } else { /* we have some space left in the old one */ 405103026Ssobomax m->m_data -= msiz; 406103026Ssobomax m->m_len += msiz; 407103026Ssobomax m->m_pkthdr.len += msiz; 408103026Ssobomax bcopy(ip, mtod(m, caddr_t), 409103026Ssobomax sizeof(struct ip)); 410103026Ssobomax } 411103026Ssobomax ip = mtod(m, struct ip *); 412103026Ssobomax memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 413103026Ssobomax ip->ip_len = ntohs(ip->ip_len) + msiz; 414103026Ssobomax } else { /* AF_INET */ 415103026Ssobomax _IF_DROP(&ifp->if_snd); 416103026Ssobomax m_freem(m); 417103026Ssobomax error = EINVAL; 418103026Ssobomax goto end; 419103026Ssobomax } 420103026Ssobomax } else if (sc->g_proto == IPPROTO_GRE) { 421103026Ssobomax switch (dst->sa_family) { 422103026Ssobomax case AF_INET: 423103026Ssobomax ip = mtod(m, struct ip *); 424180041Sjulian gre_ip_tos = ip->ip_tos; 425180041Sjulian gre_ip_id = ip->ip_id; 426180639Sjulian if (sc->wccp_ver == WCCP_V2) { 427180639Sjulian extra = sizeof(uint32_t); 428180639Sjulian etype = WCCP_PROTOCOL_TYPE; 429180639Sjulian } else { 430180639Sjulian etype = ETHERTYPE_IP; 431180639Sjulian } 432103026Ssobomax break; 433148613Sbz#ifdef INET6 434148613Sbz case AF_INET6: 435180041Sjulian gre_ip_id = ip_newid(); 436148613Sbz etype = ETHERTYPE_IPV6; 437148613Sbz break; 438148613Sbz#endif 439103026Ssobomax#ifdef NETATALK 440103026Ssobomax case AF_APPLETALK: 441103026Ssobomax etype = ETHERTYPE_ATALK; 442103026Ssobomax break; 443103026Ssobomax#endif 444103026Ssobomax default: 445103026Ssobomax _IF_DROP(&ifp->if_snd); 446103026Ssobomax m_freem(m); 447103026Ssobomax error = EAFNOSUPPORT; 448103026Ssobomax goto end; 449103026Ssobomax } 450179894Sthompsa 451179894Sthompsa /* Reserve space for GRE header + optional GRE key */ 452180639Sjulian int hdrlen = sizeof(struct greip) + extra; 453179894Sthompsa if (sc->key) 454179894Sthompsa hdrlen += sizeof(uint32_t); 455179894Sthompsa M_PREPEND(m, hdrlen, M_DONTWAIT); 456103026Ssobomax } else { 457103026Ssobomax _IF_DROP(&ifp->if_snd); 458103026Ssobomax m_freem(m); 459103026Ssobomax error = EINVAL; 460103026Ssobomax goto end; 461103026Ssobomax } 462103026Ssobomax 463128580Sandre if (m == NULL) { /* mbuf allocation failed */ 464103026Ssobomax _IF_DROP(&ifp->if_snd); 465103026Ssobomax error = ENOBUFS; 466103026Ssobomax goto end; 467103026Ssobomax } 468103026Ssobomax 469178888Sjulian M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */ 470178888Sjulian 471103026Ssobomax gh = mtod(m, struct greip *); 472103026Ssobomax if (sc->g_proto == IPPROTO_GRE) { 473179894Sthompsa uint32_t *options = gh->gi_options; 474179894Sthompsa 475180639Sjulian memset((void *)gh, 0, sizeof(struct greip) + extra); 476103026Ssobomax gh->gi_ptype = htons(etype); 477179894Sthompsa gh->gi_flags = 0; 478179894Sthompsa 479179894Sthompsa /* Add key option */ 480179894Sthompsa if (sc->key) 481179894Sthompsa { 482179894Sthompsa gh->gi_flags |= htons(GRE_KP); 483179894Sthompsa *(options++) = htonl(sc->key); 484179894Sthompsa } 485103026Ssobomax } 486103026Ssobomax 487103026Ssobomax gh->gi_pr = sc->g_proto; 488103026Ssobomax if (sc->g_proto != IPPROTO_MOBILE) { 489103026Ssobomax gh->gi_src = sc->g_src; 490103026Ssobomax gh->gi_dst = sc->g_dst; 491133163Ssobomax ((struct ip*)gh)->ip_v = IPPROTO_IPV4; 492103026Ssobomax ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 493103032Ssobomax ((struct ip*)gh)->ip_ttl = GRE_TTL; 494180041Sjulian ((struct ip*)gh)->ip_tos = gre_ip_tos; 495180041Sjulian ((struct ip*)gh)->ip_id = gre_ip_id; 496125226Ssobomax gh->gi_len = m->m_pkthdr.len; 497103026Ssobomax } 498103026Ssobomax 499103026Ssobomax ifp->if_opackets++; 500103026Ssobomax ifp->if_obytes += m->m_pkthdr.len; 501128583Sandre /* 502128583Sandre * Send it off and with IP_FORWARD flag to prevent it from 503128583Sandre * overwriting the ip_id again. ip_id is already set to the 504128583Sandre * ip_id of the encapsulated packet. 505128583Sandre */ 506128580Sandre error = ip_output(m, NULL, &sc->route, IP_FORWARDING, 507123992Ssobomax (struct ip_moptions *)NULL, (struct inpcb *)NULL); 508103026Ssobomax end: 509103026Ssobomax if (error) 510103026Ssobomax ifp->if_oerrors++; 511103026Ssobomax return (error); 512103026Ssobomax} 513103026Ssobomax 514103032Ssobomaxstatic int 515103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 516103026Ssobomax{ 517103026Ssobomax struct ifreq *ifr = (struct ifreq *)data; 518103026Ssobomax struct if_laddrreq *lifr = (struct if_laddrreq *)data; 519103026Ssobomax struct in_aliasreq *aifr = (struct in_aliasreq *)data; 520103026Ssobomax struct gre_softc *sc = ifp->if_softc; 521103026Ssobomax int s; 522103026Ssobomax struct sockaddr_in si; 523103026Ssobomax struct sockaddr *sa = NULL; 524179894Sthompsa int error, adj; 525103026Ssobomax struct sockaddr_in sp, sm, dp, dm; 526179894Sthompsa uint32_t key; 527103026Ssobomax 528103026Ssobomax error = 0; 529179894Sthompsa adj = 0; 530103026Ssobomax 531103026Ssobomax s = splnet(); 532103026Ssobomax switch (cmd) { 533103026Ssobomax case SIOCSIFADDR: 534103026Ssobomax ifp->if_flags |= IFF_UP; 535103026Ssobomax break; 536125020Ssobomax case SIOCSIFDSTADDR: 537103026Ssobomax break; 538103026Ssobomax case SIOCSIFFLAGS: 539164033Srwatson /* 540171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 541171056Srwatson * layer check? 542164033Srwatson */ 543164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0) 544103026Ssobomax break; 545103026Ssobomax if ((ifr->ifr_flags & IFF_LINK0) != 0) 546103026Ssobomax sc->g_proto = IPPROTO_GRE; 547103026Ssobomax else 548103026Ssobomax sc->g_proto = IPPROTO_MOBILE; 549125024Ssobomax if ((ifr->ifr_flags & IFF_LINK2) != 0) 550125024Ssobomax sc->wccp_ver = WCCP_V2; 551125024Ssobomax else 552125024Ssobomax sc->wccp_ver = WCCP_V1; 553103026Ssobomax goto recompute; 554103026Ssobomax case SIOCSIFMTU: 555164033Srwatson /* 556171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 557171056Srwatson * layer check? 558164033Srwatson */ 559164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0) 560103026Ssobomax break; 561103026Ssobomax if (ifr->ifr_mtu < 576) { 562103026Ssobomax error = EINVAL; 563103026Ssobomax break; 564103026Ssobomax } 565103026Ssobomax ifp->if_mtu = ifr->ifr_mtu; 566103026Ssobomax break; 567103026Ssobomax case SIOCGIFMTU: 568147256Sbrooks ifr->ifr_mtu = GRE2IFP(sc)->if_mtu; 569103026Ssobomax break; 570103026Ssobomax case SIOCADDMULTI: 571164033Srwatson /* 572171056Srwatson * XXXRW: Isn't this priv_checkr() redundant to the ifnet 573171056Srwatson * layer check? 574164033Srwatson */ 575164033Srwatson if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0) 576164033Srwatson break; 577164033Srwatson if (ifr == 0) { 578164033Srwatson error = EAFNOSUPPORT; 579164033Srwatson break; 580164033Srwatson } 581164033Srwatson switch (ifr->ifr_addr.sa_family) { 582164033Srwatson#ifdef INET 583164033Srwatson case AF_INET: 584164033Srwatson break; 585164033Srwatson#endif 586164033Srwatson#ifdef INET6 587164033Srwatson case AF_INET6: 588164033Srwatson break; 589164033Srwatson#endif 590164033Srwatson default: 591164033Srwatson error = EAFNOSUPPORT; 592164033Srwatson break; 593164033Srwatson } 594164033Srwatson break; 595103026Ssobomax case SIOCDELMULTI: 596164033Srwatson /* 597171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 598171056Srwatson * layer check? 599164033Srwatson */ 600164033Srwatson if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0) 601103026Ssobomax break; 602103026Ssobomax if (ifr == 0) { 603103026Ssobomax error = EAFNOSUPPORT; 604103026Ssobomax break; 605103026Ssobomax } 606103026Ssobomax switch (ifr->ifr_addr.sa_family) { 607103026Ssobomax#ifdef INET 608103026Ssobomax case AF_INET: 609103026Ssobomax break; 610103026Ssobomax#endif 611148613Sbz#ifdef INET6 612148613Sbz case AF_INET6: 613148613Sbz break; 614148613Sbz#endif 615103026Ssobomax default: 616103026Ssobomax error = EAFNOSUPPORT; 617103026Ssobomax break; 618103026Ssobomax } 619103026Ssobomax break; 620103026Ssobomax case GRESPROTO: 621164033Srwatson /* 622171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 623171056Srwatson * layer check? 624164033Srwatson */ 625164033Srwatson if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0) 626103026Ssobomax break; 627103026Ssobomax sc->g_proto = ifr->ifr_flags; 628103026Ssobomax switch (sc->g_proto) { 629103026Ssobomax case IPPROTO_GRE: 630103026Ssobomax ifp->if_flags |= IFF_LINK0; 631103026Ssobomax break; 632103026Ssobomax case IPPROTO_MOBILE: 633103026Ssobomax ifp->if_flags &= ~IFF_LINK0; 634103026Ssobomax break; 635103026Ssobomax default: 636103026Ssobomax error = EPROTONOSUPPORT; 637103026Ssobomax break; 638103026Ssobomax } 639103026Ssobomax goto recompute; 640103026Ssobomax case GREGPROTO: 641103026Ssobomax ifr->ifr_flags = sc->g_proto; 642103026Ssobomax break; 643103026Ssobomax case GRESADDRS: 644103026Ssobomax case GRESADDRD: 645164033Srwatson error = priv_check(curthread, PRIV_NET_GRE); 646164033Srwatson if (error) 647164033Srwatson return (error); 648103026Ssobomax /* 649103026Ssobomax * set tunnel endpoints, compute a less specific route 650103026Ssobomax * to the remote end and mark if as up 651103026Ssobomax */ 652103026Ssobomax sa = &ifr->ifr_addr; 653103026Ssobomax if (cmd == GRESADDRS) 654103026Ssobomax sc->g_src = (satosin(sa))->sin_addr; 655103026Ssobomax if (cmd == GRESADDRD) 656103026Ssobomax sc->g_dst = (satosin(sa))->sin_addr; 657103026Ssobomax recompute: 658103026Ssobomax#ifdef INET 659103026Ssobomax if (sc->encap != NULL) { 660103026Ssobomax encap_detach(sc->encap); 661103026Ssobomax sc->encap = NULL; 662103026Ssobomax } 663103026Ssobomax#endif 664103026Ssobomax if ((sc->g_src.s_addr != INADDR_ANY) && 665103026Ssobomax (sc->g_dst.s_addr != INADDR_ANY)) { 666103026Ssobomax bzero(&sp, sizeof(sp)); 667103026Ssobomax bzero(&sm, sizeof(sm)); 668103026Ssobomax bzero(&dp, sizeof(dp)); 669103026Ssobomax bzero(&dm, sizeof(dm)); 670103026Ssobomax sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 671103026Ssobomax sizeof(struct sockaddr_in); 672103026Ssobomax sp.sin_family = sm.sin_family = dp.sin_family = 673103026Ssobomax dm.sin_family = AF_INET; 674103026Ssobomax sp.sin_addr = sc->g_src; 675103026Ssobomax dp.sin_addr = sc->g_dst; 676125020Ssobomax sm.sin_addr.s_addr = dm.sin_addr.s_addr = 677103026Ssobomax INADDR_BROADCAST; 678103026Ssobomax#ifdef INET 679103026Ssobomax sc->encap = encap_attach(AF_INET, sc->g_proto, 680103026Ssobomax sintosa(&sp), sintosa(&sm), sintosa(&dp), 681103026Ssobomax sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 682103026Ssobomax &in_gre_protosw : &in_mobile_protosw, sc); 683103026Ssobomax if (sc->encap == NULL) 684103026Ssobomax printf("%s: unable to attach encap\n", 685147256Sbrooks if_name(GRE2IFP(sc))); 686103026Ssobomax#endif 687103026Ssobomax if (sc->route.ro_rt != 0) /* free old route */ 688103026Ssobomax RTFREE(sc->route.ro_rt); 689103026Ssobomax if (gre_compute_route(sc) == 0) 690148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 691103026Ssobomax else 692148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 693103026Ssobomax } 694103026Ssobomax break; 695103026Ssobomax case GREGADDRS: 696103026Ssobomax memset(&si, 0, sizeof(si)); 697103026Ssobomax si.sin_family = AF_INET; 698103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 699103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 700103026Ssobomax sa = sintosa(&si); 701219206Sbz error = prison_if(curthread->td_ucred, sa); 702219206Sbz if (error != 0) 703219206Sbz break; 704103026Ssobomax ifr->ifr_addr = *sa; 705103026Ssobomax break; 706103026Ssobomax case GREGADDRD: 707103026Ssobomax memset(&si, 0, sizeof(si)); 708103026Ssobomax si.sin_family = AF_INET; 709103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 710103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 711103026Ssobomax sa = sintosa(&si); 712219206Sbz error = prison_if(curthread->td_ucred, sa); 713219206Sbz if (error != 0) 714219206Sbz break; 715103026Ssobomax ifr->ifr_addr = *sa; 716103026Ssobomax break; 717103026Ssobomax case SIOCSIFPHYADDR: 718164033Srwatson /* 719171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 720171056Srwatson * layer check? 721164033Srwatson */ 722164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 723103026Ssobomax break; 724103026Ssobomax if (aifr->ifra_addr.sin_family != AF_INET || 725103026Ssobomax aifr->ifra_dstaddr.sin_family != AF_INET) { 726103026Ssobomax error = EAFNOSUPPORT; 727103026Ssobomax break; 728103026Ssobomax } 729103026Ssobomax if (aifr->ifra_addr.sin_len != sizeof(si) || 730103026Ssobomax aifr->ifra_dstaddr.sin_len != sizeof(si)) { 731103026Ssobomax error = EINVAL; 732103026Ssobomax break; 733103026Ssobomax } 734103026Ssobomax sc->g_src = aifr->ifra_addr.sin_addr; 735103026Ssobomax sc->g_dst = aifr->ifra_dstaddr.sin_addr; 736103026Ssobomax goto recompute; 737103026Ssobomax case SIOCSLIFPHYADDR: 738164033Srwatson /* 739171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 740171056Srwatson * layer check? 741164033Srwatson */ 742164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 743103026Ssobomax break; 744103026Ssobomax if (lifr->addr.ss_family != AF_INET || 745103026Ssobomax lifr->dstaddr.ss_family != AF_INET) { 746103026Ssobomax error = EAFNOSUPPORT; 747103026Ssobomax break; 748103026Ssobomax } 749103026Ssobomax if (lifr->addr.ss_len != sizeof(si) || 750103026Ssobomax lifr->dstaddr.ss_len != sizeof(si)) { 751103026Ssobomax error = EINVAL; 752103026Ssobomax break; 753103026Ssobomax } 754155440Sqingli sc->g_src = (satosin(&lifr->addr))->sin_addr; 755103026Ssobomax sc->g_dst = 756155440Sqingli (satosin(&lifr->dstaddr))->sin_addr; 757103026Ssobomax goto recompute; 758103026Ssobomax case SIOCDIFPHYADDR: 759164033Srwatson /* 760171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 761171056Srwatson * layer check? 762164033Srwatson */ 763164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 764103026Ssobomax break; 765103026Ssobomax sc->g_src.s_addr = INADDR_ANY; 766103026Ssobomax sc->g_dst.s_addr = INADDR_ANY; 767103026Ssobomax goto recompute; 768103026Ssobomax case SIOCGLIFPHYADDR: 769103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY || 770103026Ssobomax sc->g_dst.s_addr == INADDR_ANY) { 771103026Ssobomax error = EADDRNOTAVAIL; 772103026Ssobomax break; 773103026Ssobomax } 774103026Ssobomax memset(&si, 0, sizeof(si)); 775103026Ssobomax si.sin_family = AF_INET; 776103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 777103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 778219206Sbz error = prison_if(curthread->td_ucred, (struct sockaddr *)&si); 779219206Sbz if (error != 0) 780219206Sbz break; 781103026Ssobomax memcpy(&lifr->addr, &si, sizeof(si)); 782103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 783219206Sbz error = prison_if(curthread->td_ucred, (struct sockaddr *)&si); 784219206Sbz if (error != 0) 785219206Sbz break; 786103026Ssobomax memcpy(&lifr->dstaddr, &si, sizeof(si)); 787103026Ssobomax break; 788103026Ssobomax case SIOCGIFPSRCADDR: 789122699Sbms#ifdef INET6 790122699Sbms case SIOCGIFPSRCADDR_IN6: 791122699Sbms#endif 792103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY) { 793103026Ssobomax error = EADDRNOTAVAIL; 794103026Ssobomax break; 795103026Ssobomax } 796103026Ssobomax memset(&si, 0, sizeof(si)); 797103026Ssobomax si.sin_family = AF_INET; 798103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 799103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 800219206Sbz error = prison_if(curthread->td_ucred, (struct sockaddr *)&si); 801219206Sbz if (error != 0) 802219206Sbz break; 803103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 804103026Ssobomax break; 805103026Ssobomax case SIOCGIFPDSTADDR: 806122699Sbms#ifdef INET6 807122699Sbms case SIOCGIFPDSTADDR_IN6: 808122699Sbms#endif 809103026Ssobomax if (sc->g_dst.s_addr == INADDR_ANY) { 810103026Ssobomax error = EADDRNOTAVAIL; 811103026Ssobomax break; 812103026Ssobomax } 813103026Ssobomax memset(&si, 0, sizeof(si)); 814103026Ssobomax si.sin_family = AF_INET; 815103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 816103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 817219206Sbz error = prison_if(curthread->td_ucred, (struct sockaddr *)&si); 818219206Sbz if (error != 0) 819219206Sbz break; 820103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 821103026Ssobomax break; 822179894Sthompsa case GRESKEY: 823179894Sthompsa error = priv_check(curthread, PRIV_NET_GRE); 824179894Sthompsa if (error) 825179894Sthompsa break; 826179894Sthompsa error = copyin(ifr->ifr_data, &key, sizeof(key)); 827179894Sthompsa if (error) 828179894Sthompsa break; 829179894Sthompsa /* adjust MTU for option header */ 830179894Sthompsa if (key == 0 && sc->key != 0) /* clear */ 831179894Sthompsa adj += sizeof(key); 832179894Sthompsa else if (key != 0 && sc->key == 0) /* set */ 833179894Sthompsa adj -= sizeof(key); 834179894Sthompsa 835179894Sthompsa if (ifp->if_mtu + adj < 576) { 836179894Sthompsa error = EINVAL; 837179894Sthompsa break; 838179894Sthompsa } 839179894Sthompsa ifp->if_mtu += adj; 840179894Sthompsa sc->key = key; 841179894Sthompsa break; 842179894Sthompsa case GREGKEY: 843179894Sthompsa error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key)); 844179894Sthompsa break; 845179894Sthompsa 846103026Ssobomax default: 847103026Ssobomax error = EINVAL; 848103026Ssobomax break; 849103026Ssobomax } 850103026Ssobomax 851103026Ssobomax splx(s); 852103026Ssobomax return (error); 853103026Ssobomax} 854103026Ssobomax 855103026Ssobomax/* 856103026Ssobomax * computes a route to our destination that is not the one 857103026Ssobomax * which would be taken by ip_output(), as this one will loop back to 858103026Ssobomax * us. If the interface is p2p as a--->b, then a routing entry exists 859103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here 860123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to 861103026Ssobomax * if_gre. 862103026Ssobomax * Goal here is to compute a route to b that is less specific than 863103026Ssobomax * a-->b. We know that this one exists as in normal operation we have 864103026Ssobomax * at least a default route which matches. 865103026Ssobomax */ 866103032Ssobomaxstatic int 867103026Ssobomaxgre_compute_route(struct gre_softc *sc) 868103026Ssobomax{ 869103026Ssobomax struct route *ro; 870103026Ssobomax 871103026Ssobomax ro = &sc->route; 872103026Ssobomax 873103026Ssobomax memset(ro, 0, sizeof(struct route)); 874103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 875103026Ssobomax ro->ro_dst.sa_family = AF_INET; 876103026Ssobomax ro->ro_dst.sa_len = sizeof(ro->ro_dst); 877103026Ssobomax 878103026Ssobomax /* 879103026Ssobomax * toggle last bit, so our interface is not found, but a less 880103026Ssobomax * specific route. I'd rather like to specify a shorter mask, 881103026Ssobomax * but this is not possible. Should work though. XXX 882178888Sjulian * XXX MRT Use a different FIB for the tunnel to solve this problem. 883103026Ssobomax */ 884147256Sbrooks if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) { 885177416Sjulian ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^= 886177416Sjulian htonl(0x01); 887103026Ssobomax } 888103026Ssobomax 889103026Ssobomax#ifdef DIAGNOSTIC 890147256Sbrooks printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)), 891103026Ssobomax inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 892103026Ssobomax#endif 893103026Ssobomax 894178888Sjulian rtalloc_fib(ro, sc->gre_fibnum); 895103026Ssobomax 896103026Ssobomax /* 897103026Ssobomax * check if this returned a route at all and this route is no 898103026Ssobomax * recursion to ourself 899103026Ssobomax */ 900103026Ssobomax if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 901103026Ssobomax#ifdef DIAGNOSTIC 902103026Ssobomax if (ro->ro_rt == NULL) 903103026Ssobomax printf(" - no route found!\n"); 904103026Ssobomax else 905103026Ssobomax printf(" - route loops back to ourself!\n"); 906103026Ssobomax#endif 907103026Ssobomax return EADDRNOTAVAIL; 908103026Ssobomax } 909103026Ssobomax 910103026Ssobomax /* 911103026Ssobomax * now change it back - else ip_output will just drop 912103026Ssobomax * the route and search one to this interface ... 913103026Ssobomax */ 914147256Sbrooks if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) 915103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 916103026Ssobomax 917103026Ssobomax#ifdef DIAGNOSTIC 918103026Ssobomax printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 919103026Ssobomax inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 920103026Ssobomax printf("\n"); 921103026Ssobomax#endif 922103026Ssobomax 923103026Ssobomax return 0; 924103026Ssobomax} 925103026Ssobomax 926103026Ssobomax/* 927103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on 928103026Ssobomax * mbufs. 929103026Ssobomax */ 930123992Ssobomaxu_int16_t 931123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len) 932103026Ssobomax{ 933123992Ssobomax u_int32_t sum = 0; 934103026Ssobomax int nwords = len >> 1; 935103026Ssobomax 936103026Ssobomax while (nwords-- != 0) 937103026Ssobomax sum += *p++; 938103026Ssobomax 939103026Ssobomax if (len & 1) { 940103026Ssobomax union { 941103026Ssobomax u_short w; 942103026Ssobomax u_char c[2]; 943103026Ssobomax } u; 944103026Ssobomax u.c[0] = *(u_char *)p; 945103026Ssobomax u.c[1] = 0; 946103026Ssobomax sum += u.w; 947103026Ssobomax } 948103026Ssobomax 949103026Ssobomax /* end-around-carry */ 950103026Ssobomax sum = (sum >> 16) + (sum & 0xffff); 951103026Ssobomax sum += (sum >> 16); 952103026Ssobomax return (~sum); 953103026Ssobomax} 954103026Ssobomax 955103026Ssobomaxstatic int 956103026Ssobomaxgremodevent(module_t mod, int type, void *data) 957103026Ssobomax{ 958103026Ssobomax 959103026Ssobomax switch (type) { 960103026Ssobomax case MOD_LOAD: 961103026Ssobomax greattach(); 962103026Ssobomax break; 963103026Ssobomax case MOD_UNLOAD: 964103026Ssobomax if_clone_detach(&gre_cloner); 965127307Srwatson mtx_destroy(&gre_mtx); 966103026Ssobomax break; 967132199Sphk default: 968132199Sphk return EOPNOTSUPP; 969103026Ssobomax } 970103026Ssobomax return 0; 971103026Ssobomax} 972103026Ssobomax 973103026Ssobomaxstatic moduledata_t gre_mod = { 974103026Ssobomax "if_gre", 975103026Ssobomax gremodevent, 976103026Ssobomax 0 977103026Ssobomax}; 978103026Ssobomax 979103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 980103026SsobomaxMODULE_VERSION(if_gre, 1); 981