if_gre.c revision 106939
1103026Ssobomax/* $NetBSD: if_gre.c,v 1.42 2002/08/14 00:23:27 itojun Exp $ */ 2103026Ssobomax/* $FreeBSD: head/sys/net/if_gre.c 106939 2002-11-15 00:00:15Z sam $ */ 3103026Ssobomax 4103026Ssobomax/* 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 * 11103026Ssobomax * Redistribution and use in source and binary forms, with or without 12103026Ssobomax * modification, are permitted provided that the following conditions 13103026Ssobomax * are met: 14103026Ssobomax * 1. Redistributions of source code must retain the above copyright 15103026Ssobomax * notice, this list of conditions and the following disclaimer. 16103026Ssobomax * 2. Redistributions in binary form must reproduce the above copyright 17103026Ssobomax * notice, this list of conditions and the following disclaimer in the 18103026Ssobomax * documentation and/or other materials provided with the distribution. 19103026Ssobomax * 3. All advertising materials mentioning features or use of this software 20103026Ssobomax * must display the following acknowledgement: 21103026Ssobomax * This product includes software developed by the NetBSD 22103026Ssobomax * Foundation, Inc. and its contributors. 23103026Ssobomax * 4. Neither the name of The NetBSD Foundation nor the names of its 24103026Ssobomax * contributors may be used to endorse or promote products derived 25103026Ssobomax * from this software without specific prior written permission. 26103026Ssobomax * 27103026Ssobomax * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28103026Ssobomax * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29103026Ssobomax * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30103026Ssobomax * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31103026Ssobomax * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32103026Ssobomax * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33103026Ssobomax * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34103026Ssobomax * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35103026Ssobomax * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36103026Ssobomax * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37103026Ssobomax * POSSIBILITY OF SUCH DAMAGE. 38103026Ssobomax */ 39103026Ssobomax 40103026Ssobomax/* 41103026Ssobomax * Encapsulate L3 protocols into IP 42103026Ssobomax * See RFC 1701 and 1702 for more details. 43103026Ssobomax * If_gre is compatible with Cisco GRE tunnels, so you can 44103026Ssobomax * have a NetBSD box as the other end of a tunnel interface of a Cisco 45103026Ssobomax * router. See gre(4) for more details. 46103026Ssobomax * Also supported: IP in IP encaps (proto 55) as of RFC 2004 47103026Ssobomax */ 48103026Ssobomax 49103394Sbde#include "opt_atalk.h" 50103026Ssobomax#include "opt_inet.h" 51103026Ssobomax#include "opt_ns.h" 52103026Ssobomax 53103026Ssobomax#include <sys/param.h> 54103026Ssobomax#include <sys/kernel.h> 55103026Ssobomax#include <sys/malloc.h> 56103026Ssobomax#include <sys/mbuf.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> 65103026Ssobomax#include <net/if_types.h> 66103026Ssobomax#include <net/route.h> 67103026Ssobomax 68103026Ssobomax#ifdef INET 69103026Ssobomax#include <netinet/in.h> 70103026Ssobomax#include <netinet/in_systm.h> 71103026Ssobomax#include <netinet/in_var.h> 72103026Ssobomax#include <netinet/ip.h> 73103026Ssobomax#include <netinet/ip_gre.h> 74103026Ssobomax#include <netinet/ip_var.h> 75103026Ssobomax#include <netinet/ip_encap.h> 76103026Ssobomax#else 77103026Ssobomax#error "Huh? if_gre without inet?" 78103026Ssobomax#endif 79103026Ssobomax 80103026Ssobomax#include <net/bpf.h> 81103026Ssobomax 82103026Ssobomax#include <net/net_osdep.h> 83103026Ssobomax#include <net/if_gre.h> 84103026Ssobomax 85103026Ssobomax/* 86103026Ssobomax * It is not easy to calculate the right value for a GRE MTU. 87103026Ssobomax * We leave this task to the admin and use the same default that 88103026Ssobomax * other vendors use. 89103026Ssobomax */ 90103026Ssobomax#define GREMTU 1476 91103026Ssobomax 92103026Ssobomax#define GRENAME "gre" 93103026Ssobomax 94103026Ssobomaxstatic MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation"); 95103026Ssobomax 96103026Ssobomaxstruct gre_softc_head gre_softc_list; 97103026Ssobomax 98105300Salfredstatic int gre_clone_create(struct if_clone *, int); 99105300Salfredstatic void gre_clone_destroy(struct ifnet *); 100103032Ssobomaxstatic int gre_ioctl(struct ifnet *, u_long, caddr_t); 101103032Ssobomaxstatic int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *, 102103032Ssobomax struct rtentry *rt); 103103026Ssobomax 104103032Ssobomaxstatic struct if_clone gre_cloner = 105103026Ssobomax IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy, 0, IF_MAXUNIT); 106103026Ssobomax 107103032Ssobomaxstatic int gre_compute_route(struct gre_softc *sc); 108103026Ssobomax 109105300Salfredstatic void greattach(void); 110103026Ssobomax 111103026Ssobomax#ifdef INET 112103026Ssobomaxextern struct domain inetdomain; 113103026Ssobomaxstatic const struct protosw in_gre_protosw = 114103026Ssobomax{ SOCK_RAW, &inetdomain, IPPROTO_GRE, PR_ATOMIC|PR_ADDR, 115103026Ssobomax (pr_input_t*)gre_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput, 116103026Ssobomax 0, 117103026Ssobomax 0, 0, 0, 0, 118103026Ssobomax &rip_usrreqs 119103026Ssobomax}; 120103026Ssobomaxstatic const struct protosw in_mobile_protosw = 121103026Ssobomax{ SOCK_RAW, &inetdomain, IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR, 122103026Ssobomax (pr_input_t*)gre_mobile_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput, 123103026Ssobomax 0, 124103026Ssobomax 0, 0, 0, 0, 125103026Ssobomax &rip_usrreqs 126103026Ssobomax}; 127103026Ssobomax#endif 128103026Ssobomax 129103026SsobomaxSYSCTL_DECL(_net_link); 130103026SsobomaxSYSCTL_NODE(_net_link, IFT_OTHER, gre, CTLFLAG_RW, 0, 131103026Ssobomax "Generic Routing Encapsulation"); 132103026Ssobomax#ifndef MAX_GRE_NEST 133103026Ssobomax/* 134103026Ssobomax * This macro controls the default upper limitation on nesting of gre tunnels. 135103026Ssobomax * Since, setting a large value to this macro with a careless configuration 136103026Ssobomax * may introduce system crash, we don't allow any nestings by default. 137103026Ssobomax * If you need to configure nested gre tunnels, you can define this macro 138103026Ssobomax * in your kernel configuration file. However, if you do so, please be 139103026Ssobomax * careful to configure the tunnels so that it won't make a loop. 140103026Ssobomax */ 141103026Ssobomax#define MAX_GRE_NEST 1 142103026Ssobomax#endif 143103026Ssobomaxstatic int max_gre_nesting = MAX_GRE_NEST; 144103026SsobomaxSYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW, 145103026Ssobomax &max_gre_nesting, 0, "Max nested tunnels"); 146103026Ssobomax 147103026Ssobomax/* ARGSUSED */ 148103032Ssobomaxstatic void 149103026Ssobomaxgreattach(void) 150103026Ssobomax{ 151103026Ssobomax 152103026Ssobomax LIST_INIT(&gre_softc_list); 153103026Ssobomax if_clone_attach(&gre_cloner); 154103026Ssobomax} 155103026Ssobomax 156103032Ssobomaxstatic int 157103026Ssobomaxgre_clone_create(ifc, unit) 158103026Ssobomax struct if_clone *ifc; 159103026Ssobomax int unit; 160103026Ssobomax{ 161103026Ssobomax struct gre_softc *sc; 162103026Ssobomax 163103026Ssobomax sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK); 164103026Ssobomax memset(sc, 0, sizeof(struct gre_softc)); 165103026Ssobomax 166103026Ssobomax sc->sc_if.if_name = GRENAME; 167103026Ssobomax sc->sc_if.if_softc = sc; 168103026Ssobomax sc->sc_if.if_unit = unit; 169103026Ssobomax sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN; 170103026Ssobomax sc->sc_if.if_type = IFT_OTHER; 171103026Ssobomax sc->sc_if.if_addrlen = 0; 172103026Ssobomax sc->sc_if.if_hdrlen = 24; /* IP + GRE */ 173103026Ssobomax sc->sc_if.if_mtu = GREMTU; 174103026Ssobomax sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 175103026Ssobomax sc->sc_if.if_output = gre_output; 176103026Ssobomax sc->sc_if.if_ioctl = gre_ioctl; 177103026Ssobomax sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 178103026Ssobomax sc->g_proto = IPPROTO_GRE; 179103026Ssobomax sc->sc_if.if_flags |= IFF_LINK0; 180103026Ssobomax sc->encap = NULL; 181103026Ssobomax sc->called = 0; 182103026Ssobomax if_attach(&sc->sc_if); 183103026Ssobomax bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t)); 184103026Ssobomax LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 185103026Ssobomax return (0); 186103026Ssobomax} 187103026Ssobomax 188103032Ssobomaxstatic void 189103026Ssobomaxgre_clone_destroy(ifp) 190103026Ssobomax struct ifnet *ifp; 191103026Ssobomax{ 192103026Ssobomax struct gre_softc *sc = ifp->if_softc; 193103026Ssobomax 194103026Ssobomax#ifdef INET 195103026Ssobomax if (sc->encap != NULL) 196103026Ssobomax encap_detach(sc->encap); 197103026Ssobomax#endif 198103026Ssobomax LIST_REMOVE(sc, sc_list); 199103026Ssobomax bpfdetach(ifp); 200103026Ssobomax if_detach(ifp); 201103026Ssobomax free(sc, M_GRE); 202103026Ssobomax} 203103026Ssobomax 204103026Ssobomax/* 205103026Ssobomax * The output routine. Takes a packet and encapsulates it in the protocol 206103026Ssobomax * given by sc->g_proto. See also RFC 1701 and RFC 2004 207103026Ssobomax */ 208103032Ssobomaxstatic int 209103026Ssobomaxgre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 210103026Ssobomax struct rtentry *rt) 211103026Ssobomax{ 212103026Ssobomax int error = 0; 213103026Ssobomax struct gre_softc *sc = ifp->if_softc; 214103026Ssobomax struct greip *gh; 215103026Ssobomax struct ip *ip; 216103026Ssobomax u_char osrc; 217103026Ssobomax u_short etype = 0; 218103026Ssobomax struct mobile_h mob_h; 219103026Ssobomax 220103026Ssobomax /* 221103026Ssobomax * gre may cause infinite recursion calls when misconfigured. 222103026Ssobomax * We'll prevent this by introducing upper limit. 223103026Ssobomax */ 224103026Ssobomax if (++(sc->called) > max_gre_nesting) { 225103026Ssobomax printf("%s: gre_output: recursively called too many " 226103026Ssobomax "times(%d)\n", if_name(&sc->sc_if), sc->called); 227103026Ssobomax m_freem(m); 228103026Ssobomax error = EIO; /* is there better errno? */ 229103026Ssobomax goto end; 230103026Ssobomax } 231103026Ssobomax 232103026Ssobomax if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 || 233103026Ssobomax sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 234103026Ssobomax m_freem(m); 235103026Ssobomax error = ENETDOWN; 236103026Ssobomax goto end; 237103026Ssobomax } 238103026Ssobomax 239103026Ssobomax gh = NULL; 240103026Ssobomax ip = NULL; 241103026Ssobomax osrc = 0; 242103026Ssobomax 243103026Ssobomax if (ifp->if_bpf) { 244103026Ssobomax /* see comment of other if_foo.c files */ 245103026Ssobomax struct mbuf m0; 246103026Ssobomax u_int32_t af = dst->sa_family; 247103026Ssobomax 248103026Ssobomax m0.m_next = m; 249103026Ssobomax m0.m_len = 4; 250103026Ssobomax m0.m_data = (char *)⁡ 251103026Ssobomax 252106939Ssam BPF_MTAP(ifp, &m0); 253103026Ssobomax } 254103026Ssobomax 255103026Ssobomax m->m_flags &= ~(M_BCAST|M_MCAST); 256103026Ssobomax 257103026Ssobomax if (sc->g_proto == IPPROTO_MOBILE) { 258103026Ssobomax if (dst->sa_family == AF_INET) { 259103026Ssobomax struct mbuf *m0; 260103026Ssobomax int msiz; 261103026Ssobomax 262103026Ssobomax ip = mtod(m, struct ip *); 263103026Ssobomax 264103026Ssobomax /* 265103026Ssobomax * RFC2004 specifies that fragmented diagrams shouldn't 266103026Ssobomax * be encapsulated. 267103026Ssobomax */ 268103026Ssobomax if ((ip->ip_off & IP_MF) != 0) { 269103026Ssobomax _IF_DROP(&ifp->if_snd); 270103026Ssobomax m_freem(m); 271103026Ssobomax error = EINVAL; /* is there better errno? */ 272103026Ssobomax goto end; 273103026Ssobomax } 274103026Ssobomax memset(&mob_h, 0, MOB_H_SIZ_L); 275103026Ssobomax mob_h.proto = (ip->ip_p) << 8; 276103026Ssobomax mob_h.odst = ip->ip_dst.s_addr; 277103026Ssobomax ip->ip_dst.s_addr = sc->g_dst.s_addr; 278103026Ssobomax 279103026Ssobomax /* 280103026Ssobomax * If the packet comes from our host, we only change 281103026Ssobomax * the destination address in the IP header. 282103026Ssobomax * Else we also need to save and change the source 283103026Ssobomax */ 284103026Ssobomax if (in_hosteq(ip->ip_src, sc->g_src)) { 285103026Ssobomax msiz = MOB_H_SIZ_S; 286103026Ssobomax } else { 287103026Ssobomax mob_h.proto |= MOB_H_SBIT; 288103026Ssobomax mob_h.osrc = ip->ip_src.s_addr; 289103026Ssobomax ip->ip_src.s_addr = sc->g_src.s_addr; 290103026Ssobomax msiz = MOB_H_SIZ_L; 291103026Ssobomax } 292103026Ssobomax mob_h.proto = htons(mob_h.proto); 293103026Ssobomax mob_h.hcrc = gre_in_cksum((u_short *)&mob_h, msiz); 294103026Ssobomax 295103026Ssobomax if ((m->m_data - msiz) < m->m_pktdat) { 296103026Ssobomax /* need new mbuf */ 297103026Ssobomax MGETHDR(m0, M_DONTWAIT, MT_HEADER); 298103026Ssobomax if (m0 == NULL) { 299103026Ssobomax _IF_DROP(&ifp->if_snd); 300103026Ssobomax m_freem(m); 301103026Ssobomax error = ENOBUFS; 302103026Ssobomax goto end; 303103026Ssobomax } 304103026Ssobomax m0->m_next = m; 305103026Ssobomax m->m_data += sizeof(struct ip); 306103026Ssobomax m->m_len -= sizeof(struct ip); 307103026Ssobomax m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 308103026Ssobomax m0->m_len = msiz + sizeof(struct ip); 309103026Ssobomax m0->m_data += max_linkhdr; 310103026Ssobomax memcpy(mtod(m0, caddr_t), (caddr_t)ip, 311103026Ssobomax sizeof(struct ip)); 312103026Ssobomax m = m0; 313103026Ssobomax } else { /* we have some space left in the old one */ 314103026Ssobomax m->m_data -= msiz; 315103026Ssobomax m->m_len += msiz; 316103026Ssobomax m->m_pkthdr.len += msiz; 317103026Ssobomax bcopy(ip, mtod(m, caddr_t), 318103026Ssobomax sizeof(struct ip)); 319103026Ssobomax } 320103026Ssobomax ip = mtod(m, struct ip *); 321103026Ssobomax memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 322103026Ssobomax ip->ip_len = ntohs(ip->ip_len) + msiz; 323103026Ssobomax } else { /* AF_INET */ 324103026Ssobomax _IF_DROP(&ifp->if_snd); 325103026Ssobomax m_freem(m); 326103026Ssobomax error = EINVAL; 327103026Ssobomax goto end; 328103026Ssobomax } 329103026Ssobomax } else if (sc->g_proto == IPPROTO_GRE) { 330103026Ssobomax switch (dst->sa_family) { 331103026Ssobomax case AF_INET: 332103026Ssobomax ip = mtod(m, struct ip *); 333103026Ssobomax etype = ETHERTYPE_IP; 334103026Ssobomax break; 335103026Ssobomax#ifdef NETATALK 336103026Ssobomax case AF_APPLETALK: 337103026Ssobomax etype = ETHERTYPE_ATALK; 338103026Ssobomax break; 339103026Ssobomax#endif 340103026Ssobomax#ifdef NS 341103026Ssobomax case AF_NS: 342103026Ssobomax etype = ETHERTYPE_NS; 343103026Ssobomax break; 344103026Ssobomax#endif 345103026Ssobomax default: 346103026Ssobomax _IF_DROP(&ifp->if_snd); 347103026Ssobomax m_freem(m); 348103026Ssobomax error = EAFNOSUPPORT; 349103026Ssobomax goto end; 350103026Ssobomax } 351103026Ssobomax M_PREPEND(m, sizeof(struct greip), M_DONTWAIT); 352103026Ssobomax } else { 353103026Ssobomax _IF_DROP(&ifp->if_snd); 354103026Ssobomax m_freem(m); 355103026Ssobomax error = EINVAL; 356103026Ssobomax goto end; 357103026Ssobomax } 358103026Ssobomax 359103026Ssobomax if (m == NULL) { /* impossible */ 360103026Ssobomax _IF_DROP(&ifp->if_snd); 361103026Ssobomax error = ENOBUFS; 362103026Ssobomax goto end; 363103026Ssobomax } 364103026Ssobomax 365103026Ssobomax gh = mtod(m, struct greip *); 366103026Ssobomax if (sc->g_proto == IPPROTO_GRE) { 367103026Ssobomax /* we don't have any GRE flags for now */ 368103026Ssobomax 369103026Ssobomax memset((void *)&gh->gi_g, 0, sizeof(struct gre_h)); 370103026Ssobomax gh->gi_ptype = htons(etype); 371103026Ssobomax } 372103026Ssobomax 373103026Ssobomax gh->gi_pr = sc->g_proto; 374103026Ssobomax if (sc->g_proto != IPPROTO_MOBILE) { 375103026Ssobomax gh->gi_src = sc->g_src; 376103026Ssobomax gh->gi_dst = sc->g_dst; 377103026Ssobomax ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 378103032Ssobomax ((struct ip*)gh)->ip_ttl = GRE_TTL; 379103026Ssobomax ((struct ip*)gh)->ip_tos = ip->ip_tos; 380103026Ssobomax ((struct ip*)gh)->ip_id = ip->ip_id; 381103026Ssobomax gh->gi_len = m->m_pkthdr.len; 382103026Ssobomax } 383103026Ssobomax 384103026Ssobomax ifp->if_opackets++; 385103026Ssobomax ifp->if_obytes += m->m_pkthdr.len; 386103026Ssobomax /* send it off */ 387105194Ssam error = ip_output(m, NULL, &sc->route, 0, NULL, NULL); 388103026Ssobomax end: 389103026Ssobomax sc->called = 0; 390103026Ssobomax if (error) 391103026Ssobomax ifp->if_oerrors++; 392103026Ssobomax return (error); 393103026Ssobomax} 394103026Ssobomax 395103032Ssobomaxstatic int 396103026Ssobomaxgre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 397103026Ssobomax{ 398103026Ssobomax struct ifreq *ifr = (struct ifreq *)data; 399103026Ssobomax struct if_laddrreq *lifr = (struct if_laddrreq *)data; 400103026Ssobomax struct in_aliasreq *aifr = (struct in_aliasreq *)data; 401103026Ssobomax struct gre_softc *sc = ifp->if_softc; 402103026Ssobomax int s; 403103026Ssobomax struct sockaddr_in si; 404103026Ssobomax struct sockaddr *sa = NULL; 405103026Ssobomax int error; 406103026Ssobomax struct sockaddr_in sp, sm, dp, dm; 407103026Ssobomax 408103026Ssobomax error = 0; 409103026Ssobomax 410103026Ssobomax s = splnet(); 411103026Ssobomax switch (cmd) { 412103026Ssobomax case SIOCSIFADDR: 413103026Ssobomax ifp->if_flags |= IFF_UP; 414103026Ssobomax break; 415103026Ssobomax case SIOCSIFDSTADDR: 416103026Ssobomax break; 417103026Ssobomax case SIOCSIFFLAGS: 418103026Ssobomax if ((error = suser(curthread)) != 0) 419103026Ssobomax break; 420103026Ssobomax if ((ifr->ifr_flags & IFF_LINK0) != 0) 421103026Ssobomax sc->g_proto = IPPROTO_GRE; 422103026Ssobomax else 423103026Ssobomax sc->g_proto = IPPROTO_MOBILE; 424103026Ssobomax goto recompute; 425103026Ssobomax case SIOCSIFMTU: 426103026Ssobomax if ((error = suser(curthread)) != 0) 427103026Ssobomax break; 428103026Ssobomax if (ifr->ifr_mtu < 576) { 429103026Ssobomax error = EINVAL; 430103026Ssobomax break; 431103026Ssobomax } 432103026Ssobomax ifp->if_mtu = ifr->ifr_mtu; 433103026Ssobomax break; 434103026Ssobomax case SIOCGIFMTU: 435103026Ssobomax ifr->ifr_mtu = sc->sc_if.if_mtu; 436103026Ssobomax break; 437103026Ssobomax case SIOCADDMULTI: 438103026Ssobomax case SIOCDELMULTI: 439103026Ssobomax if ((error = suser(curthread)) != 0) 440103026Ssobomax break; 441103026Ssobomax if (ifr == 0) { 442103026Ssobomax error = EAFNOSUPPORT; 443103026Ssobomax break; 444103026Ssobomax } 445103026Ssobomax switch (ifr->ifr_addr.sa_family) { 446103026Ssobomax#ifdef INET 447103026Ssobomax case AF_INET: 448103026Ssobomax break; 449103026Ssobomax#endif 450103026Ssobomax default: 451103026Ssobomax error = EAFNOSUPPORT; 452103026Ssobomax break; 453103026Ssobomax } 454103026Ssobomax break; 455103026Ssobomax case GRESPROTO: 456103026Ssobomax if ((error = suser(curthread)) != 0) 457103026Ssobomax break; 458103026Ssobomax sc->g_proto = ifr->ifr_flags; 459103026Ssobomax switch (sc->g_proto) { 460103026Ssobomax case IPPROTO_GRE: 461103026Ssobomax ifp->if_flags |= IFF_LINK0; 462103026Ssobomax break; 463103026Ssobomax case IPPROTO_MOBILE: 464103026Ssobomax ifp->if_flags &= ~IFF_LINK0; 465103026Ssobomax break; 466103026Ssobomax default: 467103026Ssobomax error = EPROTONOSUPPORT; 468103026Ssobomax break; 469103026Ssobomax } 470103026Ssobomax goto recompute; 471103026Ssobomax case GREGPROTO: 472103026Ssobomax ifr->ifr_flags = sc->g_proto; 473103026Ssobomax break; 474103026Ssobomax case GRESADDRS: 475103026Ssobomax case GRESADDRD: 476103026Ssobomax if ((error = suser(curthread)) != 0) 477103026Ssobomax break; 478103026Ssobomax /* 479103026Ssobomax * set tunnel endpoints, compute a less specific route 480103026Ssobomax * to the remote end and mark if as up 481103026Ssobomax */ 482103026Ssobomax sa = &ifr->ifr_addr; 483103026Ssobomax if (cmd == GRESADDRS) 484103026Ssobomax sc->g_src = (satosin(sa))->sin_addr; 485103026Ssobomax if (cmd == GRESADDRD) 486103026Ssobomax sc->g_dst = (satosin(sa))->sin_addr; 487103026Ssobomax recompute: 488103026Ssobomax#ifdef INET 489103026Ssobomax if (sc->encap != NULL) { 490103026Ssobomax encap_detach(sc->encap); 491103026Ssobomax sc->encap = NULL; 492103026Ssobomax } 493103026Ssobomax#endif 494103026Ssobomax if ((sc->g_src.s_addr != INADDR_ANY) && 495103026Ssobomax (sc->g_dst.s_addr != INADDR_ANY)) { 496103026Ssobomax bzero(&sp, sizeof(sp)); 497103026Ssobomax bzero(&sm, sizeof(sm)); 498103026Ssobomax bzero(&dp, sizeof(dp)); 499103026Ssobomax bzero(&dm, sizeof(dm)); 500103026Ssobomax sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 501103026Ssobomax sizeof(struct sockaddr_in); 502103026Ssobomax sp.sin_family = sm.sin_family = dp.sin_family = 503103026Ssobomax dm.sin_family = AF_INET; 504103026Ssobomax sp.sin_addr = sc->g_src; 505103026Ssobomax dp.sin_addr = sc->g_dst; 506103026Ssobomax sm.sin_addr.s_addr = dm.sin_addr.s_addr = 507103026Ssobomax INADDR_BROADCAST; 508103026Ssobomax#ifdef INET 509103026Ssobomax sc->encap = encap_attach(AF_INET, sc->g_proto, 510103026Ssobomax sintosa(&sp), sintosa(&sm), sintosa(&dp), 511103026Ssobomax sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 512103026Ssobomax &in_gre_protosw : &in_mobile_protosw, sc); 513103026Ssobomax if (sc->encap == NULL) 514103026Ssobomax printf("%s: unable to attach encap\n", 515103026Ssobomax if_name(&sc->sc_if)); 516103026Ssobomax#endif 517103026Ssobomax if (sc->route.ro_rt != 0) /* free old route */ 518103026Ssobomax RTFREE(sc->route.ro_rt); 519103026Ssobomax if (gre_compute_route(sc) == 0) 520103026Ssobomax ifp->if_flags |= IFF_RUNNING; 521103026Ssobomax else 522103026Ssobomax ifp->if_flags &= ~IFF_RUNNING; 523103026Ssobomax } 524103026Ssobomax break; 525103026Ssobomax case GREGADDRS: 526103026Ssobomax memset(&si, 0, sizeof(si)); 527103026Ssobomax si.sin_family = AF_INET; 528103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 529103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 530103026Ssobomax sa = sintosa(&si); 531103026Ssobomax ifr->ifr_addr = *sa; 532103026Ssobomax break; 533103026Ssobomax case GREGADDRD: 534103026Ssobomax memset(&si, 0, sizeof(si)); 535103026Ssobomax si.sin_family = AF_INET; 536103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 537103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 538103026Ssobomax sa = sintosa(&si); 539103026Ssobomax ifr->ifr_addr = *sa; 540103026Ssobomax break; 541103026Ssobomax case SIOCSIFPHYADDR: 542103026Ssobomax if ((error = suser(curthread)) != 0) 543103026Ssobomax break; 544103026Ssobomax if (aifr->ifra_addr.sin_family != AF_INET || 545103026Ssobomax aifr->ifra_dstaddr.sin_family != AF_INET) { 546103026Ssobomax error = EAFNOSUPPORT; 547103026Ssobomax break; 548103026Ssobomax } 549103026Ssobomax if (aifr->ifra_addr.sin_len != sizeof(si) || 550103026Ssobomax aifr->ifra_dstaddr.sin_len != sizeof(si)) { 551103026Ssobomax error = EINVAL; 552103026Ssobomax break; 553103026Ssobomax } 554103026Ssobomax sc->g_src = aifr->ifra_addr.sin_addr; 555103026Ssobomax sc->g_dst = aifr->ifra_dstaddr.sin_addr; 556103026Ssobomax goto recompute; 557103026Ssobomax case SIOCSLIFPHYADDR: 558103026Ssobomax if ((error = suser(curthread)) != 0) 559103026Ssobomax break; 560103026Ssobomax if (lifr->addr.ss_family != AF_INET || 561103026Ssobomax lifr->dstaddr.ss_family != AF_INET) { 562103026Ssobomax error = EAFNOSUPPORT; 563103026Ssobomax break; 564103026Ssobomax } 565103026Ssobomax if (lifr->addr.ss_len != sizeof(si) || 566103026Ssobomax lifr->dstaddr.ss_len != sizeof(si)) { 567103026Ssobomax error = EINVAL; 568103026Ssobomax break; 569103026Ssobomax } 570103026Ssobomax sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr; 571103026Ssobomax sc->g_dst = 572103026Ssobomax (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr; 573103026Ssobomax goto recompute; 574103026Ssobomax case SIOCDIFPHYADDR: 575103026Ssobomax if ((error = suser(curthread)) != 0) 576103026Ssobomax break; 577103026Ssobomax sc->g_src.s_addr = INADDR_ANY; 578103026Ssobomax sc->g_dst.s_addr = INADDR_ANY; 579103026Ssobomax goto recompute; 580103026Ssobomax case SIOCGLIFPHYADDR: 581103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY || 582103026Ssobomax sc->g_dst.s_addr == INADDR_ANY) { 583103026Ssobomax error = EADDRNOTAVAIL; 584103026Ssobomax break; 585103026Ssobomax } 586103026Ssobomax memset(&si, 0, sizeof(si)); 587103026Ssobomax si.sin_family = AF_INET; 588103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 589103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 590103026Ssobomax memcpy(&lifr->addr, &si, sizeof(si)); 591103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 592103026Ssobomax memcpy(&lifr->dstaddr, &si, sizeof(si)); 593103026Ssobomax break; 594103026Ssobomax case SIOCGIFPSRCADDR: 595103026Ssobomax if (sc->g_src.s_addr == INADDR_ANY) { 596103026Ssobomax error = EADDRNOTAVAIL; 597103026Ssobomax break; 598103026Ssobomax } 599103026Ssobomax memset(&si, 0, sizeof(si)); 600103026Ssobomax si.sin_family = AF_INET; 601103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 602103026Ssobomax si.sin_addr.s_addr = sc->g_src.s_addr; 603103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 604103026Ssobomax break; 605103026Ssobomax case SIOCGIFPDSTADDR: 606103026Ssobomax if (sc->g_dst.s_addr == INADDR_ANY) { 607103026Ssobomax error = EADDRNOTAVAIL; 608103026Ssobomax break; 609103026Ssobomax } 610103026Ssobomax memset(&si, 0, sizeof(si)); 611103026Ssobomax si.sin_family = AF_INET; 612103026Ssobomax si.sin_len = sizeof(struct sockaddr_in); 613103026Ssobomax si.sin_addr.s_addr = sc->g_dst.s_addr; 614103026Ssobomax bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 615103026Ssobomax break; 616103026Ssobomax default: 617103026Ssobomax error = EINVAL; 618103026Ssobomax break; 619103026Ssobomax } 620103026Ssobomax 621103026Ssobomax splx(s); 622103026Ssobomax return (error); 623103026Ssobomax} 624103026Ssobomax 625103026Ssobomax/* 626103026Ssobomax * computes a route to our destination that is not the one 627103026Ssobomax * which would be taken by ip_output(), as this one will loop back to 628103026Ssobomax * us. If the interface is p2p as a--->b, then a routing entry exists 629103026Ssobomax * If we now send a packet to b (e.g. ping b), this will come down here 630103026Ssobomax * gets src=a, dst=b tacked on and would from ip_ouput() sent back to 631103026Ssobomax * if_gre. 632103026Ssobomax * Goal here is to compute a route to b that is less specific than 633103026Ssobomax * a-->b. We know that this one exists as in normal operation we have 634103026Ssobomax * at least a default route which matches. 635103026Ssobomax */ 636103032Ssobomaxstatic int 637103026Ssobomaxgre_compute_route(struct gre_softc *sc) 638103026Ssobomax{ 639103026Ssobomax struct route *ro; 640103026Ssobomax u_int32_t a, b, c; 641103026Ssobomax 642103026Ssobomax ro = &sc->route; 643103026Ssobomax 644103026Ssobomax memset(ro, 0, sizeof(struct route)); 645103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 646103026Ssobomax ro->ro_dst.sa_family = AF_INET; 647103026Ssobomax ro->ro_dst.sa_len = sizeof(ro->ro_dst); 648103026Ssobomax 649103026Ssobomax /* 650103026Ssobomax * toggle last bit, so our interface is not found, but a less 651103026Ssobomax * specific route. I'd rather like to specify a shorter mask, 652103026Ssobomax * but this is not possible. Should work though. XXX 653103026Ssobomax * there is a simpler way ... 654103026Ssobomax */ 655103026Ssobomax if ((sc->sc_if.if_flags & IFF_LINK1) == 0) { 656103026Ssobomax a = ntohl(sc->g_dst.s_addr); 657103026Ssobomax b = a & 0x01; 658103026Ssobomax c = a & 0xfffffffe; 659103026Ssobomax b = b ^ 0x01; 660103026Ssobomax a = b | c; 661103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr 662103026Ssobomax = htonl(a); 663103026Ssobomax } 664103026Ssobomax 665103026Ssobomax#ifdef DIAGNOSTIC 666103026Ssobomax printf("%s: searching a route to %s", if_name(&sc->sc_if), 667103026Ssobomax inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 668103026Ssobomax#endif 669103026Ssobomax 670103026Ssobomax rtalloc(ro); 671103026Ssobomax 672103026Ssobomax /* 673103026Ssobomax * check if this returned a route at all and this route is no 674103026Ssobomax * recursion to ourself 675103026Ssobomax */ 676103026Ssobomax if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 677103026Ssobomax#ifdef DIAGNOSTIC 678103026Ssobomax if (ro->ro_rt == NULL) 679103026Ssobomax printf(" - no route found!\n"); 680103026Ssobomax else 681103026Ssobomax printf(" - route loops back to ourself!\n"); 682103026Ssobomax#endif 683103026Ssobomax return EADDRNOTAVAIL; 684103026Ssobomax } 685103026Ssobomax 686103026Ssobomax /* 687103026Ssobomax * now change it back - else ip_output will just drop 688103026Ssobomax * the route and search one to this interface ... 689103026Ssobomax */ 690103026Ssobomax if ((sc->sc_if.if_flags & IFF_LINK1) == 0) 691103026Ssobomax ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 692103026Ssobomax 693103026Ssobomax#ifdef DIAGNOSTIC 694103026Ssobomax printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 695103026Ssobomax inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 696103026Ssobomax printf("\n"); 697103026Ssobomax#endif 698103026Ssobomax 699103026Ssobomax return 0; 700103026Ssobomax} 701103026Ssobomax 702103026Ssobomax/* 703103026Ssobomax * do a checksum of a buffer - much like in_cksum, which operates on 704103026Ssobomax * mbufs. 705103026Ssobomax */ 706103026Ssobomaxu_short 707103026Ssobomaxgre_in_cksum(u_short *p, u_int len) 708103026Ssobomax{ 709103026Ssobomax u_int sum = 0; 710103026Ssobomax int nwords = len >> 1; 711103026Ssobomax 712103026Ssobomax while (nwords-- != 0) 713103026Ssobomax sum += *p++; 714103026Ssobomax 715103026Ssobomax if (len & 1) { 716103026Ssobomax union { 717103026Ssobomax u_short w; 718103026Ssobomax u_char c[2]; 719103026Ssobomax } u; 720103026Ssobomax u.c[0] = *(u_char *)p; 721103026Ssobomax u.c[1] = 0; 722103026Ssobomax sum += u.w; 723103026Ssobomax } 724103026Ssobomax 725103026Ssobomax /* end-around-carry */ 726103026Ssobomax sum = (sum >> 16) + (sum & 0xffff); 727103026Ssobomax sum += (sum >> 16); 728103026Ssobomax return (~sum); 729103026Ssobomax} 730103026Ssobomax 731103026Ssobomaxstatic int 732103026Ssobomaxgremodevent(module_t mod, int type, void *data) 733103026Ssobomax{ 734103026Ssobomax 735103026Ssobomax switch (type) { 736103026Ssobomax case MOD_LOAD: 737103026Ssobomax greattach(); 738103026Ssobomax break; 739103026Ssobomax case MOD_UNLOAD: 740103026Ssobomax if_clone_detach(&gre_cloner); 741103026Ssobomax 742103026Ssobomax while (!LIST_EMPTY(&gre_softc_list)) 743103026Ssobomax gre_clone_destroy(&LIST_FIRST(&gre_softc_list)->sc_if); 744103026Ssobomax break; 745103026Ssobomax } 746103026Ssobomax return 0; 747103026Ssobomax} 748103026Ssobomax 749103026Ssobomaxstatic moduledata_t gre_mod = { 750103026Ssobomax "if_gre", 751103026Ssobomax gremodevent, 752103026Ssobomax 0 753103026Ssobomax}; 754103026Ssobomax 755103026SsobomaxDECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 756103026SsobomaxMODULE_VERSION(if_gre, 1); 757