if_gre.c revision 241913
1123992Ssobomax/* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */ 2103026Ssobomax/* $FreeBSD: head/sys/net/if_gre.c 241913 2012-10-22 21:09:03Z glebius $ */ 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 93223223Sbz#define MTAG_COOKIE_GRE 1307983903 94223223Sbz#define MTAG_GRE_NESTING 1 95223223Sbzstruct mtag_gre_nesting { 96223223Sbz uint16_t count; 97223223Sbz uint16_t max; 98223223Sbz struct ifnet *ifp[]; 99223223Sbz}; 100223223Sbz 101127307Srwatson/* 102127307Srwatson * gre_mtx protects all global variables in if_gre.c. 103127307Srwatson * XXX: gre_softc data not protected yet. 104127307Srwatson */ 105127307Srwatsonstruct mtx gre_mtx; 106241610Sglebiusstatic const char grename[] = "gre"; 107241610Sglebiusstatic MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation"); 108103026Ssobomax 109103026Ssobomaxstruct gre_softc_head gre_softc_list; 110103026Ssobomax 111160195Ssamstatic int gre_clone_create(struct if_clone *, int, caddr_t); 112105300Salfredstatic void gre_clone_destroy(struct ifnet *); 113241610Sglebiusstatic struct if_clone *gre_cloner; 114241610Sglebius 115103032Ssobomaxstatic int gre_ioctl(struct ifnet *, u_long, caddr_t); 116103032Ssobomaxstatic int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *, 117191148Skmacy struct route *ro); 118103026Ssobomax 119103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc); 120103026Ssobomax 121105300Salfredstatic void greattach(void); 122103026Ssobomax 123103026Ssobomax#ifdef INET 124103026Ssobomaxextern struct domain inetdomain; 125152242Srustatic const struct protosw in_gre_protosw = { 126152242Sru .pr_type = SOCK_RAW, 127152242Sru .pr_domain = &inetdomain, 128152242Sru .pr_protocol = IPPROTO_GRE, 129152242Sru .pr_flags = PR_ATOMIC|PR_ADDR, 130154625Sbz .pr_input = gre_input, 131152242Sru .pr_output = (pr_output_t *)rip_output, 132152242Sru .pr_ctlinput = rip_ctlinput, 133152242Sru .pr_ctloutput = rip_ctloutput, 134152242Sru .pr_usrreqs = &rip_usrreqs 135103026Ssobomax}; 136152242Srustatic const struct protosw in_mobile_protosw = { 137152242Sru .pr_type = SOCK_RAW, 138152242Sru .pr_domain = &inetdomain, 139152242Sru .pr_protocol = IPPROTO_MOBILE, 140152242Sru .pr_flags = PR_ATOMIC|PR_ADDR, 141154625Sbz .pr_input = gre_mobile_input, 142152242Sru .pr_output = (pr_output_t *)rip_output, 143152242Sru .pr_ctlinput = rip_ctlinput, 144152242Sru .pr_ctloutput = rip_ctloutput, 145152242Sru .pr_usrreqs = &rip_usrreqs 146103026Ssobomax}; 147103026Ssobomax#endif 148103026Ssobomax 149103026SsobomaxSYSCTL_DECL(_net_link); 150227309Sedstatic SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0, 151103026Ssobomax "Generic Routing Encapsulation"); 152103026Ssobomax#ifndef MAX_GRE_NEST 153103026Ssobomax/* 154103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels. 155103026Ssobomax * Since, setting a large value to this macro with a careless configuration 156103026Ssobomax * may introduce system crash, we don't allow any nestings by default. 157103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro 158103026Ssobomax * in your kernel configuration file. However, if you do so, please be 159103026Ssobomax * careful to configure the tunnels so that it won't make a loop. 160103026Ssobomax */ 161103026Ssobomax#define MAX_GRE_NEST 1 162103026Ssobomax#endif 163103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST; 164103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW, 165103026Ssobomax &max_gre_nesting, 0, "Max nested tunnels"); 166103026Ssobomax 167103026Ssobomax/* ARGSUSED */ 168103032Ssobomaxstatic void 169103026Ssobomaxgreattach(void) 170103026Ssobomax{ 171103026Ssobomax 172127307Srwatson mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF); 173103026Ssobomax LIST_INIT(&gre_softc_list); 174241610Sglebius gre_cloner = if_clone_simple(grename, gre_clone_create, 175241610Sglebius gre_clone_destroy, 0); 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; 195241610Sglebius if_initname(GRE2IFP(sc), grename, 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 */ 359241913Sglebius if (ip->ip_off & htons(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); 413241913Sglebius ip->ip_len = htons(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; 496241913Sglebius gh->gi_len = htons(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 struct sockaddr_in si; 522103026Ssobomax struct sockaddr *sa = NULL; 523179894Sthompsa int error, adj; 524103026Ssobomax struct sockaddr_in sp, sm, dp, dm; 525179894Sthompsa uint32_t key; 526103026Ssobomax 527103026Ssobomax error = 0; 528179894Sthompsa adj = 0; 529103026Ssobomax 530103026Ssobomax switch (cmd) { 531103026Ssobomax case SIOCSIFADDR: 532103026Ssobomax ifp->if_flags |= IFF_UP; 533103026Ssobomax break; 534125020Ssobomax case SIOCSIFDSTADDR: 535103026Ssobomax break; 536103026Ssobomax case SIOCSIFFLAGS: 537164033Srwatson /* 538171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 539171056Srwatson * layer check? 540164033Srwatson */ 541164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0) 542103026Ssobomax break; 543103026Ssobomax if ((ifr->ifr_flags & IFF_LINK0) != 0) 544103026Ssobomax sc->g_proto = IPPROTO_GRE; 545103026Ssobomax else 546103026Ssobomax sc->g_proto = IPPROTO_MOBILE; 547125024Ssobomax if ((ifr->ifr_flags & IFF_LINK2) != 0) 548125024Ssobomax sc->wccp_ver = WCCP_V2; 549125024Ssobomax else 550125024Ssobomax sc->wccp_ver = WCCP_V1; 551103026Ssobomax goto recompute; 552103026Ssobomax case SIOCSIFMTU: 553164033Srwatson /* 554171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 555171056Srwatson * layer check? 556164033Srwatson */ 557164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0) 558103026Ssobomax break; 559103026Ssobomax if (ifr->ifr_mtu < 576) { 560103026Ssobomax error = EINVAL; 561103026Ssobomax break; 562103026Ssobomax } 563103026Ssobomax ifp->if_mtu = ifr->ifr_mtu; 564103026Ssobomax break; 565103026Ssobomax case SIOCGIFMTU: 566147256Sbrooks ifr->ifr_mtu = GRE2IFP(sc)->if_mtu; 567103026Ssobomax break; 568103026Ssobomax case SIOCADDMULTI: 569164033Srwatson /* 570171056Srwatson * XXXRW: Isn't this priv_checkr() redundant to the ifnet 571171056Srwatson * layer check? 572164033Srwatson */ 573164033Srwatson if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0) 574164033Srwatson break; 575164033Srwatson if (ifr == 0) { 576164033Srwatson error = EAFNOSUPPORT; 577164033Srwatson break; 578164033Srwatson } 579164033Srwatson switch (ifr->ifr_addr.sa_family) { 580164033Srwatson#ifdef INET 581164033Srwatson case AF_INET: 582164033Srwatson break; 583164033Srwatson#endif 584164033Srwatson#ifdef INET6 585164033Srwatson case AF_INET6: 586164033Srwatson break; 587164033Srwatson#endif 588164033Srwatson default: 589164033Srwatson error = EAFNOSUPPORT; 590164033Srwatson break; 591164033Srwatson } 592164033Srwatson break; 593103026Ssobomax case SIOCDELMULTI: 594164033Srwatson /* 595171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 596171056Srwatson * layer check? 597164033Srwatson */ 598164033Srwatson if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0) 599103026Ssobomax break; 600103026Ssobomax if (ifr == 0) { 601103026Ssobomax error = EAFNOSUPPORT; 602103026Ssobomax break; 603103026Ssobomax } 604103026Ssobomax switch (ifr->ifr_addr.sa_family) { 605103026Ssobomax#ifdef INET 606103026Ssobomax case AF_INET: 607103026Ssobomax break; 608103026Ssobomax#endif 609148613Sbz#ifdef INET6 610148613Sbz case AF_INET6: 611148613Sbz break; 612148613Sbz#endif 613103026Ssobomax default: 614103026Ssobomax error = EAFNOSUPPORT; 615103026Ssobomax break; 616103026Ssobomax } 617103026Ssobomax break; 618103026Ssobomax case GRESPROTO: 619164033Srwatson /* 620171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 621171056Srwatson * layer check? 622164033Srwatson */ 623164033Srwatson if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0) 624103026Ssobomax break; 625103026Ssobomax sc->g_proto = ifr->ifr_flags; 626103026Ssobomax switch (sc->g_proto) { 627103026Ssobomax case IPPROTO_GRE: 628103026Ssobomax ifp->if_flags |= IFF_LINK0; 629103026Ssobomax break; 630103026Ssobomax case IPPROTO_MOBILE: 631103026Ssobomax ifp->if_flags &= ~IFF_LINK0; 632103026Ssobomax break; 633103026Ssobomax default: 634103026Ssobomax error = EPROTONOSUPPORT; 635103026Ssobomax break; 636103026Ssobomax } 637103026Ssobomax goto recompute; 638103026Ssobomax case GREGPROTO: 639103026Ssobomax ifr->ifr_flags = sc->g_proto; 640103026Ssobomax break; 641103026Ssobomax case GRESADDRS: 642103026Ssobomax case GRESADDRD: 643164033Srwatson error = priv_check(curthread, PRIV_NET_GRE); 644164033Srwatson if (error) 645164033Srwatson return (error); 646103026Ssobomax /* 647103026Ssobomax * set tunnel endpoints, compute a less specific route 648103026Ssobomax * to the remote end and mark if as up 649103026Ssobomax */ 650103026Ssobomax sa = &ifr->ifr_addr; 651103026Ssobomax if (cmd == GRESADDRS) 652103026Ssobomax sc->g_src = (satosin(sa))->sin_addr; 653103026Ssobomax if (cmd == GRESADDRD) 654103026Ssobomax sc->g_dst = (satosin(sa))->sin_addr; 655103026Ssobomax recompute: 656103026Ssobomax#ifdef INET 657103026Ssobomax if (sc->encap != NULL) { 658103026Ssobomax encap_detach(sc->encap); 659103026Ssobomax sc->encap = NULL; 660103026Ssobomax } 661103026Ssobomax#endif 662103026Ssobomax if ((sc->g_src.s_addr != INADDR_ANY) && 663103026Ssobomax (sc->g_dst.s_addr != INADDR_ANY)) { 664103026Ssobomax bzero(&sp, sizeof(sp)); 665103026Ssobomax bzero(&sm, sizeof(sm)); 666103026Ssobomax bzero(&dp, sizeof(dp)); 667103026Ssobomax bzero(&dm, sizeof(dm)); 668103026Ssobomax sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 669103026Ssobomax sizeof(struct sockaddr_in); 670103026Ssobomax sp.sin_family = sm.sin_family = dp.sin_family = 671103026Ssobomax dm.sin_family = AF_INET; 672103026Ssobomax sp.sin_addr = sc->g_src; 673103026Ssobomax dp.sin_addr = sc->g_dst; 674125020Ssobomax sm.sin_addr.s_addr = dm.sin_addr.s_addr = 675103026Ssobomax INADDR_BROADCAST; 676103026Ssobomax#ifdef INET 677103026Ssobomax sc->encap = encap_attach(AF_INET, sc->g_proto, 678103026Ssobomax sintosa(&sp), sintosa(&sm), sintosa(&dp), 679103026Ssobomax sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 680103026Ssobomax &in_gre_protosw : &in_mobile_protosw, sc); 681103026Ssobomax if (sc->encap == NULL) 682103026Ssobomax printf("%s: unable to attach encap\n", 683147256Sbrooks if_name(GRE2IFP(sc))); 684103026Ssobomax#endif 685103026Ssobomax if (sc->route.ro_rt != 0) /* free old route */ 686103026Ssobomax RTFREE(sc->route.ro_rt); 687103026Ssobomax if (gre_compute_route(sc) == 0) 688148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 689103026Ssobomax else 690148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 691103026Ssobomax } 692103026Ssobomax break; 693103026Ssobomax case GREGADDRS: 694103026Ssobomax memset(&si, 0, sizeof(si)); 695103026Ssobomax si.sin_family = AF_INET; 696103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 697103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 698103026Ssobomax sa = sintosa(&si); 699219206Sbz error = prison_if(curthread->td_ucred, sa); 700219206Sbz if (error != 0) 701219206Sbz break; 702103026Ssobomax ifr->ifr_addr = *sa; 703103026Ssobomax break; 704103026Ssobomax case GREGADDRD: 705103026Ssobomax memset(&si, 0, sizeof(si)); 706103026Ssobomax si.sin_family = AF_INET; 707103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 708103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 709103026Ssobomax sa = sintosa(&si); 710219206Sbz error = prison_if(curthread->td_ucred, sa); 711219206Sbz if (error != 0) 712219206Sbz break; 713103026Ssobomax ifr->ifr_addr = *sa; 714103026Ssobomax break; 715103026Ssobomax case SIOCSIFPHYADDR: 716164033Srwatson /* 717171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 718171056Srwatson * layer check? 719164033Srwatson */ 720164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 721103026Ssobomax break; 722103026Ssobomax if (aifr->ifra_addr.sin_family != AF_INET || 723103026Ssobomax aifr->ifra_dstaddr.sin_family != AF_INET) { 724103026Ssobomax error = EAFNOSUPPORT; 725103026Ssobomax break; 726103026Ssobomax } 727103026Ssobomax if (aifr->ifra_addr.sin_len != sizeof(si) || 728103026Ssobomax aifr->ifra_dstaddr.sin_len != sizeof(si)) { 729103026Ssobomax error = EINVAL; 730103026Ssobomax break; 731103026Ssobomax } 732103026Ssobomax sc->g_src = aifr->ifra_addr.sin_addr; 733103026Ssobomax sc->g_dst = aifr->ifra_dstaddr.sin_addr; 734103026Ssobomax goto recompute; 735103026Ssobomax case SIOCSLIFPHYADDR: 736164033Srwatson /* 737171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 738171056Srwatson * layer check? 739164033Srwatson */ 740164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 741103026Ssobomax break; 742103026Ssobomax if (lifr->addr.ss_family != AF_INET || 743103026Ssobomax lifr->dstaddr.ss_family != AF_INET) { 744103026Ssobomax error = EAFNOSUPPORT; 745103026Ssobomax break; 746103026Ssobomax } 747103026Ssobomax if (lifr->addr.ss_len != sizeof(si) || 748103026Ssobomax lifr->dstaddr.ss_len != sizeof(si)) { 749103026Ssobomax error = EINVAL; 750103026Ssobomax break; 751103026Ssobomax } 752155440Sqingli sc->g_src = (satosin(&lifr->addr))->sin_addr; 753103026Ssobomax sc->g_dst = 754155440Sqingli (satosin(&lifr->dstaddr))->sin_addr; 755103026Ssobomax goto recompute; 756103026Ssobomax case SIOCDIFPHYADDR: 757164033Srwatson /* 758171056Srwatson * XXXRW: Isn't this priv_check() redundant to the ifnet 759171056Srwatson * layer check? 760164033Srwatson */ 761164033Srwatson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 762103026Ssobomax break; 763103026Ssobomax sc->g_src.s_addr = INADDR_ANY; 764103026Ssobomax sc->g_dst.s_addr = INADDR_ANY; 765103026Ssobomax goto recompute; 766103026Ssobomax case SIOCGLIFPHYADDR: 767103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY || 768103026Ssobomax sc->g_dst.s_addr == INADDR_ANY) { 769103026Ssobomax error = EADDRNOTAVAIL; 770103026Ssobomax break; 771103026Ssobomax } 772103026Ssobomax memset(&si, 0, sizeof(si)); 773103026Ssobomax si.sin_family = AF_INET; 774103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 775103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 776219206Sbz error = prison_if(curthread->td_ucred, (struct sockaddr *)&si); 777219206Sbz if (error != 0) 778219206Sbz break; 779103026Ssobomax memcpy(&lifr->addr, &si, sizeof(si)); 780103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 781219206Sbz error = prison_if(curthread->td_ucred, (struct sockaddr *)&si); 782219206Sbz if (error != 0) 783219206Sbz break; 784103026Ssobomax memcpy(&lifr->dstaddr, &si, sizeof(si)); 785103026Ssobomax break; 786103026Ssobomax case SIOCGIFPSRCADDR: 787122699Sbms#ifdef INET6 788122699Sbms case SIOCGIFPSRCADDR_IN6: 789122699Sbms#endif 790103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY) { 791103026Ssobomax error = EADDRNOTAVAIL; 792103026Ssobomax break; 793103026Ssobomax } 794103026Ssobomax memset(&si, 0, sizeof(si)); 795103026Ssobomax si.sin_family = AF_INET; 796103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 797103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 798219206Sbz error = prison_if(curthread->td_ucred, (struct sockaddr *)&si); 799219206Sbz if (error != 0) 800219206Sbz break; 801103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 802103026Ssobomax break; 803103026Ssobomax case SIOCGIFPDSTADDR: 804122699Sbms#ifdef INET6 805122699Sbms case SIOCGIFPDSTADDR_IN6: 806122699Sbms#endif 807103026Ssobomax if (sc->g_dst.s_addr == INADDR_ANY) { 808103026Ssobomax error = EADDRNOTAVAIL; 809103026Ssobomax break; 810103026Ssobomax } 811103026Ssobomax memset(&si, 0, sizeof(si)); 812103026Ssobomax si.sin_family = AF_INET; 813103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 814103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 815219206Sbz error = prison_if(curthread->td_ucred, (struct sockaddr *)&si); 816219206Sbz if (error != 0) 817219206Sbz break; 818103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 819103026Ssobomax break; 820179894Sthompsa case GRESKEY: 821179894Sthompsa error = priv_check(curthread, PRIV_NET_GRE); 822179894Sthompsa if (error) 823179894Sthompsa break; 824179894Sthompsa error = copyin(ifr->ifr_data, &key, sizeof(key)); 825179894Sthompsa if (error) 826179894Sthompsa break; 827179894Sthompsa /* adjust MTU for option header */ 828179894Sthompsa if (key == 0 && sc->key != 0) /* clear */ 829179894Sthompsa adj += sizeof(key); 830179894Sthompsa else if (key != 0 && sc->key == 0) /* set */ 831179894Sthompsa adj -= sizeof(key); 832179894Sthompsa 833179894Sthompsa if (ifp->if_mtu + adj < 576) { 834179894Sthompsa error = EINVAL; 835179894Sthompsa break; 836179894Sthompsa } 837179894Sthompsa ifp->if_mtu += adj; 838179894Sthompsa sc->key = key; 839179894Sthompsa break; 840179894Sthompsa case GREGKEY: 841179894Sthompsa error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key)); 842179894Sthompsa break; 843179894Sthompsa 844103026Ssobomax default: 845103026Ssobomax error = EINVAL; 846103026Ssobomax break; 847103026Ssobomax } 848103026Ssobomax 849103026Ssobomax return (error); 850103026Ssobomax} 851103026Ssobomax 852103026Ssobomax/* 853103026Ssobomax * computes a route to our destination that is not the one 854103026Ssobomax * which would be taken by ip_output(), as this one will loop back to 855103026Ssobomax * us. If the interface is p2p as a--->b, then a routing entry exists 856103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here 857123992Ssobomax * gets src=a, dst=b tacked on and would from ip_output() sent back to 858103026Ssobomax * if_gre. 859103026Ssobomax * Goal here is to compute a route to b that is less specific than 860103026Ssobomax * a-->b. We know that this one exists as in normal operation we have 861103026Ssobomax * at least a default route which matches. 862103026Ssobomax */ 863103032Ssobomaxstatic int 864103026Ssobomaxgre_compute_route(struct gre_softc *sc) 865103026Ssobomax{ 866103026Ssobomax struct route *ro; 867103026Ssobomax 868103026Ssobomax ro = &sc->route; 869103026Ssobomax 870103026Ssobomax memset(ro, 0, sizeof(struct route)); 871103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 872103026Ssobomax ro->ro_dst.sa_family = AF_INET; 873103026Ssobomax ro->ro_dst.sa_len = sizeof(ro->ro_dst); 874103026Ssobomax 875103026Ssobomax /* 876103026Ssobomax * toggle last bit, so our interface is not found, but a less 877103026Ssobomax * specific route. I'd rather like to specify a shorter mask, 878103026Ssobomax * but this is not possible. Should work though. XXX 879178888Sjulian * XXX MRT Use a different FIB for the tunnel to solve this problem. 880103026Ssobomax */ 881147256Sbrooks if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) { 882177416Sjulian ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^= 883177416Sjulian htonl(0x01); 884103026Ssobomax } 885103026Ssobomax 886103026Ssobomax#ifdef DIAGNOSTIC 887147256Sbrooks printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)), 888103026Ssobomax inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 889103026Ssobomax#endif 890103026Ssobomax 891178888Sjulian rtalloc_fib(ro, sc->gre_fibnum); 892103026Ssobomax 893103026Ssobomax /* 894103026Ssobomax * check if this returned a route at all and this route is no 895103026Ssobomax * recursion to ourself 896103026Ssobomax */ 897103026Ssobomax if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 898103026Ssobomax#ifdef DIAGNOSTIC 899103026Ssobomax if (ro->ro_rt == NULL) 900103026Ssobomax printf(" - no route found!\n"); 901103026Ssobomax else 902103026Ssobomax printf(" - route loops back to ourself!\n"); 903103026Ssobomax#endif 904103026Ssobomax return EADDRNOTAVAIL; 905103026Ssobomax } 906103026Ssobomax 907103026Ssobomax /* 908103026Ssobomax * now change it back - else ip_output will just drop 909103026Ssobomax * the route and search one to this interface ... 910103026Ssobomax */ 911147256Sbrooks if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) 912103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 913103026Ssobomax 914103026Ssobomax#ifdef DIAGNOSTIC 915103026Ssobomax printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 916103026Ssobomax inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 917103026Ssobomax printf("\n"); 918103026Ssobomax#endif 919103026Ssobomax 920103026Ssobomax return 0; 921103026Ssobomax} 922103026Ssobomax 923103026Ssobomax/* 924103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on 925103026Ssobomax * mbufs. 926103026Ssobomax */ 927123992Ssobomaxu_int16_t 928123992Ssobomaxgre_in_cksum(u_int16_t *p, u_int len) 929103026Ssobomax{ 930123992Ssobomax u_int32_t sum = 0; 931103026Ssobomax int nwords = len >> 1; 932103026Ssobomax 933103026Ssobomax while (nwords-- != 0) 934103026Ssobomax sum += *p++; 935103026Ssobomax 936103026Ssobomax if (len & 1) { 937103026Ssobomax union { 938103026Ssobomax u_short w; 939103026Ssobomax u_char c[2]; 940103026Ssobomax } u; 941103026Ssobomax u.c[0] = *(u_char *)p; 942103026Ssobomax u.c[1] = 0; 943103026Ssobomax sum += u.w; 944103026Ssobomax } 945103026Ssobomax 946103026Ssobomax /* end-around-carry */ 947103026Ssobomax sum = (sum >> 16) + (sum & 0xffff); 948103026Ssobomax sum += (sum >> 16); 949103026Ssobomax return (~sum); 950103026Ssobomax} 951103026Ssobomax 952103026Ssobomaxstatic int 953103026Ssobomaxgremodevent(module_t mod, int type, void *data) 954103026Ssobomax{ 955103026Ssobomax 956103026Ssobomax switch (type) { 957103026Ssobomax case MOD_LOAD: 958103026Ssobomax greattach(); 959103026Ssobomax break; 960103026Ssobomax case MOD_UNLOAD: 961241610Sglebius if_clone_detach(gre_cloner); 962127307Srwatson mtx_destroy(&gre_mtx); 963103026Ssobomax break; 964132199Sphk default: 965132199Sphk return EOPNOTSUPP; 966103026Ssobomax } 967103026Ssobomax return 0; 968103026Ssobomax} 969103026Ssobomax 970103026Ssobomaxstatic moduledata_t gre_mod = { 971103026Ssobomax "if_gre", 972103026Ssobomax gremodevent, 973241394Skevlo 0 974103026Ssobomax}; 975103026Ssobomax 976103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 977103026SsobomaxMODULE_VERSION(if_gre, 1); 978