173d7ddbcSMaxim Sobolev /* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */ 28e96e13eSMaxim Sobolev /* $FreeBSD$ */ 38e96e13eSMaxim Sobolev 4c398230bSWarner Losh /*- 58e96e13eSMaxim Sobolev * Copyright (c) 1998 The NetBSD Foundation, Inc. 68e96e13eSMaxim Sobolev * All rights reserved. 78e96e13eSMaxim Sobolev * 88e96e13eSMaxim Sobolev * This code is derived from software contributed to The NetBSD Foundation 98e96e13eSMaxim Sobolev * by Heiko W.Rupp <hwr@pilhuhn.de> 108e96e13eSMaxim Sobolev * 119e669156SBjoern A. Zeeb * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de> 129e669156SBjoern A. Zeeb * 138e96e13eSMaxim Sobolev * Redistribution and use in source and binary forms, with or without 148e96e13eSMaxim Sobolev * modification, are permitted provided that the following conditions 158e96e13eSMaxim Sobolev * are met: 168e96e13eSMaxim Sobolev * 1. Redistributions of source code must retain the above copyright 178e96e13eSMaxim Sobolev * notice, this list of conditions and the following disclaimer. 188e96e13eSMaxim Sobolev * 2. Redistributions in binary form must reproduce the above copyright 198e96e13eSMaxim Sobolev * notice, this list of conditions and the following disclaimer in the 208e96e13eSMaxim Sobolev * documentation and/or other materials provided with the distribution. 218e96e13eSMaxim Sobolev * 228e96e13eSMaxim Sobolev * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 238e96e13eSMaxim Sobolev * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 248e96e13eSMaxim Sobolev * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 258e96e13eSMaxim Sobolev * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 268e96e13eSMaxim Sobolev * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 278e96e13eSMaxim Sobolev * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 288e96e13eSMaxim Sobolev * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 298e96e13eSMaxim Sobolev * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 308e96e13eSMaxim Sobolev * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 318e96e13eSMaxim Sobolev * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 328e96e13eSMaxim Sobolev * POSSIBILITY OF SUCH DAMAGE. 338e96e13eSMaxim Sobolev */ 348e96e13eSMaxim Sobolev 358e96e13eSMaxim Sobolev /* 368e96e13eSMaxim Sobolev * Encapsulate L3 protocols into IP 379e669156SBjoern A. Zeeb * See RFC 2784 (successor of RFC 1701 and 1702) for more details. 388e96e13eSMaxim Sobolev * If_gre is compatible with Cisco GRE tunnels, so you can 398e96e13eSMaxim Sobolev * have a NetBSD box as the other end of a tunnel interface of a Cisco 408e96e13eSMaxim Sobolev * router. See gre(4) for more details. 418e96e13eSMaxim Sobolev * Also supported: IP in IP encaps (proto 55) as of RFC 2004 428e96e13eSMaxim Sobolev */ 438e96e13eSMaxim Sobolev 4412768622SBruce Evans #include "opt_atalk.h" 458e96e13eSMaxim Sobolev #include "opt_inet.h" 46f16770aeSBruce M Simpson #include "opt_inet6.h" 478e96e13eSMaxim Sobolev 488e96e13eSMaxim Sobolev #include <sys/param.h> 49e3416ab0SBjoern A. Zeeb #include <sys/jail.h> 508e96e13eSMaxim Sobolev #include <sys/kernel.h> 5152dcd04bSBjoern A. Zeeb #include <sys/libkern.h> 528e96e13eSMaxim Sobolev #include <sys/malloc.h> 535dba30f1SPoul-Henning Kamp #include <sys/module.h> 548e96e13eSMaxim Sobolev #include <sys/mbuf.h> 55acd3428bSRobert Watson #include <sys/priv.h> 568b07e49aSJulian Elischer #include <sys/proc.h> 578e96e13eSMaxim Sobolev #include <sys/protosw.h> 588e96e13eSMaxim Sobolev #include <sys/socket.h> 598e96e13eSMaxim Sobolev #include <sys/sockio.h> 608e96e13eSMaxim Sobolev #include <sys/sysctl.h> 611b861caaSBruce Evans #include <sys/systm.h> 628e96e13eSMaxim Sobolev 638e96e13eSMaxim Sobolev #include <net/ethernet.h> 648e96e13eSMaxim Sobolev #include <net/if.h> 65f889d2efSBrooks Davis #include <net/if_clone.h> 668e96e13eSMaxim Sobolev #include <net/if_types.h> 678e96e13eSMaxim Sobolev #include <net/route.h> 68530c0060SRobert Watson #include <net/vnet.h> 698e96e13eSMaxim Sobolev 708e96e13eSMaxim Sobolev #ifdef INET 718e96e13eSMaxim Sobolev #include <netinet/in.h> 728e96e13eSMaxim Sobolev #include <netinet/in_systm.h> 738e96e13eSMaxim Sobolev #include <netinet/in_var.h> 748e96e13eSMaxim Sobolev #include <netinet/ip.h> 758e96e13eSMaxim Sobolev #include <netinet/ip_gre.h> 768e96e13eSMaxim Sobolev #include <netinet/ip_var.h> 778e96e13eSMaxim Sobolev #include <netinet/ip_encap.h> 788e96e13eSMaxim Sobolev #else 798e96e13eSMaxim Sobolev #error "Huh? if_gre without inet?" 808e96e13eSMaxim Sobolev #endif 818e96e13eSMaxim Sobolev 828e96e13eSMaxim Sobolev #include <net/bpf.h> 838e96e13eSMaxim Sobolev 848e96e13eSMaxim Sobolev #include <net/if_gre.h> 858e96e13eSMaxim Sobolev 868e96e13eSMaxim Sobolev /* 878e96e13eSMaxim Sobolev * It is not easy to calculate the right value for a GRE MTU. 888e96e13eSMaxim Sobolev * We leave this task to the admin and use the same default that 898e96e13eSMaxim Sobolev * other vendors use. 908e96e13eSMaxim Sobolev */ 918e96e13eSMaxim Sobolev #define GREMTU 1476 928e96e13eSMaxim Sobolev 9352dcd04bSBjoern A. Zeeb #define MTAG_COOKIE_GRE 1307983903 9452dcd04bSBjoern A. Zeeb #define MTAG_GRE_NESTING 1 9552dcd04bSBjoern A. Zeeb struct mtag_gre_nesting { 9652dcd04bSBjoern A. Zeeb uint16_t count; 9752dcd04bSBjoern A. Zeeb uint16_t max; 9852dcd04bSBjoern A. Zeeb struct ifnet *ifp[]; 9952dcd04bSBjoern A. Zeeb }; 10052dcd04bSBjoern A. Zeeb 101bdae44a8SRobert Watson /* 102bdae44a8SRobert Watson * gre_mtx protects all global variables in if_gre.c. 103bdae44a8SRobert Watson * XXX: gre_softc data not protected yet. 104bdae44a8SRobert Watson */ 105bdae44a8SRobert Watson struct mtx gre_mtx; 10642a58907SGleb Smirnoff static const char grename[] = "gre"; 10742a58907SGleb Smirnoff static MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation"); 1088e96e13eSMaxim Sobolev 1098e96e13eSMaxim Sobolev struct gre_softc_head gre_softc_list; 1108e96e13eSMaxim Sobolev 1116b7330e2SSam Leffler static int gre_clone_create(struct if_clone *, int, caddr_t); 1129ee35470SAlfred Perlstein static void gre_clone_destroy(struct ifnet *); 11342a58907SGleb Smirnoff static struct if_clone *gre_cloner; 11442a58907SGleb Smirnoff 115c23d234cSMaxim Sobolev static int gre_ioctl(struct ifnet *, u_long, caddr_t); 116*47e8d432SGleb Smirnoff static int gre_output(struct ifnet *, struct mbuf *, 117*47e8d432SGleb Smirnoff const struct sockaddr *, struct route *); 1188e96e13eSMaxim Sobolev 119c23d234cSMaxim Sobolev static int gre_compute_route(struct gre_softc *sc); 1208e96e13eSMaxim Sobolev 1219ee35470SAlfred Perlstein static void greattach(void); 1228e96e13eSMaxim Sobolev 1238e96e13eSMaxim Sobolev #ifdef INET 1248e96e13eSMaxim Sobolev extern struct domain inetdomain; 125303989a2SRuslan Ermilov static const struct protosw in_gre_protosw = { 126303989a2SRuslan Ermilov .pr_type = SOCK_RAW, 127303989a2SRuslan Ermilov .pr_domain = &inetdomain, 128303989a2SRuslan Ermilov .pr_protocol = IPPROTO_GRE, 129303989a2SRuslan Ermilov .pr_flags = PR_ATOMIC|PR_ADDR, 1303f2e28feSBjoern A. Zeeb .pr_input = gre_input, 131303989a2SRuslan Ermilov .pr_output = (pr_output_t *)rip_output, 132303989a2SRuslan Ermilov .pr_ctlinput = rip_ctlinput, 133303989a2SRuslan Ermilov .pr_ctloutput = rip_ctloutput, 134303989a2SRuslan Ermilov .pr_usrreqs = &rip_usrreqs 1358e96e13eSMaxim Sobolev }; 136303989a2SRuslan Ermilov static const struct protosw in_mobile_protosw = { 137303989a2SRuslan Ermilov .pr_type = SOCK_RAW, 138303989a2SRuslan Ermilov .pr_domain = &inetdomain, 139303989a2SRuslan Ermilov .pr_protocol = IPPROTO_MOBILE, 140303989a2SRuslan Ermilov .pr_flags = PR_ATOMIC|PR_ADDR, 1413f2e28feSBjoern A. Zeeb .pr_input = gre_mobile_input, 142303989a2SRuslan Ermilov .pr_output = (pr_output_t *)rip_output, 143303989a2SRuslan Ermilov .pr_ctlinput = rip_ctlinput, 144303989a2SRuslan Ermilov .pr_ctloutput = rip_ctloutput, 145303989a2SRuslan Ermilov .pr_usrreqs = &rip_usrreqs 1468e96e13eSMaxim Sobolev }; 1478e96e13eSMaxim Sobolev #endif 1488e96e13eSMaxim Sobolev 1498e96e13eSMaxim Sobolev SYSCTL_DECL(_net_link); 1506472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0, 1518e96e13eSMaxim Sobolev "Generic Routing Encapsulation"); 1528e96e13eSMaxim Sobolev #ifndef MAX_GRE_NEST 1538e96e13eSMaxim Sobolev /* 1548e96e13eSMaxim Sobolev * This macro controls the default upper limitation on nesting of gre tunnels. 1558e96e13eSMaxim Sobolev * Since, setting a large value to this macro with a careless configuration 1568e96e13eSMaxim Sobolev * may introduce system crash, we don't allow any nestings by default. 1578e96e13eSMaxim Sobolev * If you need to configure nested gre tunnels, you can define this macro 1588e96e13eSMaxim Sobolev * in your kernel configuration file. However, if you do so, please be 1598e96e13eSMaxim Sobolev * careful to configure the tunnels so that it won't make a loop. 1608e96e13eSMaxim Sobolev */ 1618e96e13eSMaxim Sobolev #define MAX_GRE_NEST 1 1628e96e13eSMaxim Sobolev #endif 1638e96e13eSMaxim Sobolev static int max_gre_nesting = MAX_GRE_NEST; 1648e96e13eSMaxim Sobolev SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW, 1658e96e13eSMaxim Sobolev &max_gre_nesting, 0, "Max nested tunnels"); 1668e96e13eSMaxim Sobolev 1678e96e13eSMaxim Sobolev /* ARGSUSED */ 168c23d234cSMaxim Sobolev static void 1698e96e13eSMaxim Sobolev greattach(void) 1708e96e13eSMaxim Sobolev { 1718e96e13eSMaxim Sobolev 172bdae44a8SRobert Watson mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF); 1738e96e13eSMaxim Sobolev LIST_INIT(&gre_softc_list); 17442a58907SGleb Smirnoff gre_cloner = if_clone_simple(grename, gre_clone_create, 17542a58907SGleb Smirnoff gre_clone_destroy, 0); 1768e96e13eSMaxim Sobolev } 1778e96e13eSMaxim Sobolev 178c23d234cSMaxim Sobolev static int 1796b7330e2SSam Leffler gre_clone_create(ifc, unit, params) 1808e96e13eSMaxim Sobolev struct if_clone *ifc; 1818e96e13eSMaxim Sobolev int unit; 1826b7330e2SSam Leffler caddr_t params; 1838e96e13eSMaxim Sobolev { 1848e96e13eSMaxim Sobolev struct gre_softc *sc; 1858e96e13eSMaxim Sobolev 186b3c9a01eSBruce M Simpson sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO); 1878e96e13eSMaxim Sobolev 188066b192eSBjoern A. Zeeb GRE2IFP(sc) = if_alloc(IFT_TUNNEL); 189066b192eSBjoern A. Zeeb if (GRE2IFP(sc) == NULL) { 190066b192eSBjoern A. Zeeb free(sc, M_GRE); 191066b192eSBjoern A. Zeeb return (ENOSPC); 192066b192eSBjoern A. Zeeb } 193066b192eSBjoern A. Zeeb 194fc74a9f9SBrooks Davis GRE2IFP(sc)->if_softc = sc; 19542a58907SGleb Smirnoff if_initname(GRE2IFP(sc), grename, unit); 196066b192eSBjoern A. Zeeb 197e50d35e6SMaxim Sobolev GRE2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen; 198fc74a9f9SBrooks Davis GRE2IFP(sc)->if_addrlen = 0; 199fc74a9f9SBrooks Davis GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */ 200fc74a9f9SBrooks Davis GRE2IFP(sc)->if_mtu = GREMTU; 201fc74a9f9SBrooks Davis GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 202fc74a9f9SBrooks Davis GRE2IFP(sc)->if_output = gre_output; 203fc74a9f9SBrooks Davis GRE2IFP(sc)->if_ioctl = gre_ioctl; 2048e96e13eSMaxim Sobolev sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 2058e96e13eSMaxim Sobolev sc->g_proto = IPPROTO_GRE; 206fc74a9f9SBrooks Davis GRE2IFP(sc)->if_flags |= IFF_LINK0; 2078e96e13eSMaxim Sobolev sc->encap = NULL; 2088b07e49aSJulian Elischer sc->gre_fibnum = curthread->td_proc->p_fibnum; 2097735aeb9SMaxim Sobolev sc->wccp_ver = WCCP_V1; 210131c55bcSAndrew Thompson sc->key = 0; 211fc74a9f9SBrooks Davis if_attach(GRE2IFP(sc)); 212fc74a9f9SBrooks Davis bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 213bdae44a8SRobert Watson mtx_lock(&gre_mtx); 2148e96e13eSMaxim Sobolev LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 215bdae44a8SRobert Watson mtx_unlock(&gre_mtx); 2168e96e13eSMaxim Sobolev return (0); 2178e96e13eSMaxim Sobolev } 2188e96e13eSMaxim Sobolev 219c23d234cSMaxim Sobolev static void 2208e96e13eSMaxim Sobolev gre_clone_destroy(ifp) 2218e96e13eSMaxim Sobolev struct ifnet *ifp; 2228e96e13eSMaxim Sobolev { 2238e96e13eSMaxim Sobolev struct gre_softc *sc = ifp->if_softc; 2248e96e13eSMaxim Sobolev 225bdae44a8SRobert Watson mtx_lock(&gre_mtx); 2268e96e13eSMaxim Sobolev LIST_REMOVE(sc, sc_list); 227bdae44a8SRobert Watson mtx_unlock(&gre_mtx); 228febd0759SAndrew Thompson 229febd0759SAndrew Thompson #ifdef INET 230febd0759SAndrew Thompson if (sc->encap != NULL) 231febd0759SAndrew Thompson encap_detach(sc->encap); 232febd0759SAndrew Thompson #endif 233febd0759SAndrew Thompson bpfdetach(ifp); 234febd0759SAndrew Thompson if_detach(ifp); 235febd0759SAndrew Thompson if_free(ifp); 236febd0759SAndrew Thompson free(sc, M_GRE); 2378e96e13eSMaxim Sobolev } 2388e96e13eSMaxim Sobolev 2398e96e13eSMaxim Sobolev /* 2408e96e13eSMaxim Sobolev * The output routine. Takes a packet and encapsulates it in the protocol 2418e96e13eSMaxim Sobolev * given by sc->g_proto. See also RFC 1701 and RFC 2004 2428e96e13eSMaxim Sobolev */ 243c23d234cSMaxim Sobolev static int 244*47e8d432SGleb Smirnoff gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 245279aa3d4SKip Macy struct route *ro) 2468e96e13eSMaxim Sobolev { 2478e96e13eSMaxim Sobolev int error = 0; 2488e96e13eSMaxim Sobolev struct gre_softc *sc = ifp->if_softc; 2498e96e13eSMaxim Sobolev struct greip *gh; 2508e96e13eSMaxim Sobolev struct ip *ip; 25152dcd04bSBjoern A. Zeeb struct m_tag *mtag; 25252dcd04bSBjoern A. Zeeb struct mtag_gre_nesting *gt; 25352dcd04bSBjoern A. Zeeb size_t len; 254a54eadd8SJulian Elischer u_short gre_ip_id = 0; 255a54eadd8SJulian Elischer uint8_t gre_ip_tos = 0; 25673d7ddbcSMaxim Sobolev u_int16_t etype = 0; 2578e96e13eSMaxim Sobolev struct mobile_h mob_h; 25801399f34SDavid Malone u_int32_t af; 25952dcd04bSBjoern A. Zeeb int extra = 0, max; 2608e96e13eSMaxim Sobolev 2618e96e13eSMaxim Sobolev /* 26252dcd04bSBjoern A. Zeeb * gre may cause infinite recursion calls when misconfigured. High 26352dcd04bSBjoern A. Zeeb * nesting level may cause stack exhaustion. We'll prevent this by 26452dcd04bSBjoern A. Zeeb * detecting loops and by introducing upper limit. 2658e96e13eSMaxim Sobolev */ 26652dcd04bSBjoern A. Zeeb mtag = m_tag_locate(m, MTAG_COOKIE_GRE, MTAG_GRE_NESTING, NULL); 26752dcd04bSBjoern A. Zeeb if (mtag != NULL) { 26852dcd04bSBjoern A. Zeeb struct ifnet **ifp2; 26952dcd04bSBjoern A. Zeeb 27052dcd04bSBjoern A. Zeeb gt = (struct mtag_gre_nesting *)(mtag + 1); 27152dcd04bSBjoern A. Zeeb gt->count++; 27252dcd04bSBjoern A. Zeeb if (gt->count > min(gt->max,max_gre_nesting)) { 27352dcd04bSBjoern A. Zeeb printf("%s: hit maximum recursion limit %u on %s\n", 27452dcd04bSBjoern A. Zeeb __func__, gt->count - 1, ifp->if_xname); 2758e96e13eSMaxim Sobolev m_freem(m); 2768e96e13eSMaxim Sobolev error = EIO; /* is there better errno? */ 2778e96e13eSMaxim Sobolev goto end; 2788e96e13eSMaxim Sobolev } 2798e96e13eSMaxim Sobolev 28052dcd04bSBjoern A. Zeeb ifp2 = gt->ifp; 28152dcd04bSBjoern A. Zeeb for (max = gt->count - 1; max > 0; max--) { 28252dcd04bSBjoern A. Zeeb if (*ifp2 == ifp) 28352dcd04bSBjoern A. Zeeb break; 28452dcd04bSBjoern A. Zeeb ifp2++; 28552dcd04bSBjoern A. Zeeb } 28652dcd04bSBjoern A. Zeeb if (*ifp2 == ifp) { 28752dcd04bSBjoern A. Zeeb printf("%s: detected loop with nexting %u on %s\n", 28852dcd04bSBjoern A. Zeeb __func__, gt->count-1, ifp->if_xname); 28952dcd04bSBjoern A. Zeeb m_freem(m); 29052dcd04bSBjoern A. Zeeb error = EIO; /* is there better errno? */ 29152dcd04bSBjoern A. Zeeb goto end; 29252dcd04bSBjoern A. Zeeb } 29352dcd04bSBjoern A. Zeeb *ifp2 = ifp; 29452dcd04bSBjoern A. Zeeb 29552dcd04bSBjoern A. Zeeb } else { 29652dcd04bSBjoern A. Zeeb /* 29752dcd04bSBjoern A. Zeeb * Given that people should NOT increase max_gre_nesting beyond 29852dcd04bSBjoern A. Zeeb * their real needs, we allocate once per packet rather than 29952dcd04bSBjoern A. Zeeb * allocating an mtag once per passing through gre. 30052dcd04bSBjoern A. Zeeb * 30152dcd04bSBjoern A. Zeeb * Note: the sysctl does not actually check for saneness, so we 30252dcd04bSBjoern A. Zeeb * limit the maximum numbers of possible recursions here. 30352dcd04bSBjoern A. Zeeb */ 30452dcd04bSBjoern A. Zeeb max = imin(max_gre_nesting, 256); 30552dcd04bSBjoern A. Zeeb /* If someone sets the sysctl <= 0, we want at least 1. */ 30652dcd04bSBjoern A. Zeeb max = imax(max, 1); 30752dcd04bSBjoern A. Zeeb len = sizeof(struct mtag_gre_nesting) + 30852dcd04bSBjoern A. Zeeb max * sizeof(struct ifnet *); 30952dcd04bSBjoern A. Zeeb mtag = m_tag_alloc(MTAG_COOKIE_GRE, MTAG_GRE_NESTING, len, 31052dcd04bSBjoern A. Zeeb M_NOWAIT); 31152dcd04bSBjoern A. Zeeb if (mtag == NULL) { 31252dcd04bSBjoern A. Zeeb m_freem(m); 31352dcd04bSBjoern A. Zeeb error = ENOMEM; 31452dcd04bSBjoern A. Zeeb goto end; 31552dcd04bSBjoern A. Zeeb } 31652dcd04bSBjoern A. Zeeb gt = (struct mtag_gre_nesting *)(mtag + 1); 31752dcd04bSBjoern A. Zeeb bzero(gt, len); 31852dcd04bSBjoern A. Zeeb gt->count = 1; 31952dcd04bSBjoern A. Zeeb gt->max = max; 32052dcd04bSBjoern A. Zeeb *gt->ifp = ifp; 32152dcd04bSBjoern A. Zeeb m_tag_prepend(m, mtag); 32252dcd04bSBjoern A. Zeeb } 32352dcd04bSBjoern A. Zeeb 32413f4c340SRobert Watson if (!((ifp->if_flags & IFF_UP) && 32513f4c340SRobert Watson (ifp->if_drv_flags & IFF_DRV_RUNNING)) || 3268e96e13eSMaxim Sobolev sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 3278e96e13eSMaxim Sobolev m_freem(m); 3288e96e13eSMaxim Sobolev error = ENETDOWN; 3298e96e13eSMaxim Sobolev goto end; 3308e96e13eSMaxim Sobolev } 3318e96e13eSMaxim Sobolev 3328e96e13eSMaxim Sobolev gh = NULL; 3338e96e13eSMaxim Sobolev ip = NULL; 3348e96e13eSMaxim Sobolev 33501399f34SDavid Malone /* BPF writes need to be handled specially. */ 336*47e8d432SGleb Smirnoff if (dst->sa_family == AF_UNSPEC) 33701399f34SDavid Malone bcopy(dst->sa_data, &af, sizeof(af)); 338*47e8d432SGleb Smirnoff else 33901399f34SDavid Malone af = dst->sa_family; 340*47e8d432SGleb Smirnoff 341*47e8d432SGleb Smirnoff if (bpf_peers_present(ifp->if_bpf)) 342437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 3438e96e13eSMaxim Sobolev 3448e96e13eSMaxim Sobolev m->m_flags &= ~(M_BCAST|M_MCAST); 3458e96e13eSMaxim Sobolev 3468e96e13eSMaxim Sobolev if (sc->g_proto == IPPROTO_MOBILE) { 347*47e8d432SGleb Smirnoff if (af == AF_INET) { 3488e96e13eSMaxim Sobolev struct mbuf *m0; 3498e96e13eSMaxim Sobolev int msiz; 3508e96e13eSMaxim Sobolev 3518e96e13eSMaxim Sobolev ip = mtod(m, struct ip *); 3528e96e13eSMaxim Sobolev 3538e96e13eSMaxim Sobolev /* 3548e96e13eSMaxim Sobolev * RFC2004 specifies that fragmented diagrams shouldn't 3558e96e13eSMaxim Sobolev * be encapsulated. 3568e96e13eSMaxim Sobolev */ 3578f134647SGleb Smirnoff if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) { 3588e96e13eSMaxim Sobolev _IF_DROP(&ifp->if_snd); 3598e96e13eSMaxim Sobolev m_freem(m); 3608e96e13eSMaxim Sobolev error = EINVAL; /* is there better errno? */ 3618e96e13eSMaxim Sobolev goto end; 3628e96e13eSMaxim Sobolev } 3638e96e13eSMaxim Sobolev memset(&mob_h, 0, MOB_H_SIZ_L); 3648e96e13eSMaxim Sobolev mob_h.proto = (ip->ip_p) << 8; 3658e96e13eSMaxim Sobolev mob_h.odst = ip->ip_dst.s_addr; 3668e96e13eSMaxim Sobolev ip->ip_dst.s_addr = sc->g_dst.s_addr; 3678e96e13eSMaxim Sobolev 3688e96e13eSMaxim Sobolev /* 3698e96e13eSMaxim Sobolev * If the packet comes from our host, we only change 3708e96e13eSMaxim Sobolev * the destination address in the IP header. 3718e96e13eSMaxim Sobolev * Else we also need to save and change the source 3728e96e13eSMaxim Sobolev */ 3738e96e13eSMaxim Sobolev if (in_hosteq(ip->ip_src, sc->g_src)) { 3748e96e13eSMaxim Sobolev msiz = MOB_H_SIZ_S; 3758e96e13eSMaxim Sobolev } else { 3768e96e13eSMaxim Sobolev mob_h.proto |= MOB_H_SBIT; 3778e96e13eSMaxim Sobolev mob_h.osrc = ip->ip_src.s_addr; 3788e96e13eSMaxim Sobolev ip->ip_src.s_addr = sc->g_src.s_addr; 3798e96e13eSMaxim Sobolev msiz = MOB_H_SIZ_L; 3808e96e13eSMaxim Sobolev } 3818e96e13eSMaxim Sobolev mob_h.proto = htons(mob_h.proto); 38273d7ddbcSMaxim Sobolev mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz); 3838e96e13eSMaxim Sobolev 3848e96e13eSMaxim Sobolev if ((m->m_data - msiz) < m->m_pktdat) { 385dc4ad05eSGleb Smirnoff m0 = m_gethdr(M_NOWAIT, MT_DATA); 3868e96e13eSMaxim Sobolev if (m0 == NULL) { 3878e96e13eSMaxim Sobolev _IF_DROP(&ifp->if_snd); 3888e96e13eSMaxim Sobolev m_freem(m); 3898e96e13eSMaxim Sobolev error = ENOBUFS; 3908e96e13eSMaxim Sobolev goto end; 3918e96e13eSMaxim Sobolev } 3928e96e13eSMaxim Sobolev m0->m_next = m; 3938e96e13eSMaxim Sobolev m->m_data += sizeof(struct ip); 3948e96e13eSMaxim Sobolev m->m_len -= sizeof(struct ip); 3958e96e13eSMaxim Sobolev m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 3968e96e13eSMaxim Sobolev m0->m_len = msiz + sizeof(struct ip); 3978e96e13eSMaxim Sobolev m0->m_data += max_linkhdr; 3988e96e13eSMaxim Sobolev memcpy(mtod(m0, caddr_t), (caddr_t)ip, 3998e96e13eSMaxim Sobolev sizeof(struct ip)); 4008e96e13eSMaxim Sobolev m = m0; 4018e96e13eSMaxim Sobolev } else { /* we have some space left in the old one */ 4028e96e13eSMaxim Sobolev m->m_data -= msiz; 4038e96e13eSMaxim Sobolev m->m_len += msiz; 4048e96e13eSMaxim Sobolev m->m_pkthdr.len += msiz; 4058e96e13eSMaxim Sobolev bcopy(ip, mtod(m, caddr_t), 4068e96e13eSMaxim Sobolev sizeof(struct ip)); 4078e96e13eSMaxim Sobolev } 4088e96e13eSMaxim Sobolev ip = mtod(m, struct ip *); 4098e96e13eSMaxim Sobolev memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 4108f134647SGleb Smirnoff ip->ip_len = htons(ntohs(ip->ip_len) + msiz); 4118e96e13eSMaxim Sobolev } else { /* AF_INET */ 4128e96e13eSMaxim Sobolev _IF_DROP(&ifp->if_snd); 4138e96e13eSMaxim Sobolev m_freem(m); 4148e96e13eSMaxim Sobolev error = EINVAL; 4158e96e13eSMaxim Sobolev goto end; 4168e96e13eSMaxim Sobolev } 4178e96e13eSMaxim Sobolev } else if (sc->g_proto == IPPROTO_GRE) { 418*47e8d432SGleb Smirnoff switch (af) { 4198e96e13eSMaxim Sobolev case AF_INET: 4208e96e13eSMaxim Sobolev ip = mtod(m, struct ip *); 421a54eadd8SJulian Elischer gre_ip_tos = ip->ip_tos; 422a54eadd8SJulian Elischer gre_ip_id = ip->ip_id; 42344554a6dSJulian Elischer if (sc->wccp_ver == WCCP_V2) { 42444554a6dSJulian Elischer extra = sizeof(uint32_t); 42544554a6dSJulian Elischer etype = WCCP_PROTOCOL_TYPE; 42644554a6dSJulian Elischer } else { 4278e96e13eSMaxim Sobolev etype = ETHERTYPE_IP; 42844554a6dSJulian Elischer } 4298e96e13eSMaxim Sobolev break; 4309e669156SBjoern A. Zeeb #ifdef INET6 4319e669156SBjoern A. Zeeb case AF_INET6: 432a54eadd8SJulian Elischer gre_ip_id = ip_newid(); 4339e669156SBjoern A. Zeeb etype = ETHERTYPE_IPV6; 4349e669156SBjoern A. Zeeb break; 4359e669156SBjoern A. Zeeb #endif 4368e96e13eSMaxim Sobolev #ifdef NETATALK 4378e96e13eSMaxim Sobolev case AF_APPLETALK: 4388e96e13eSMaxim Sobolev etype = ETHERTYPE_ATALK; 4398e96e13eSMaxim Sobolev break; 4408e96e13eSMaxim Sobolev #endif 4418e96e13eSMaxim Sobolev default: 4428e96e13eSMaxim Sobolev _IF_DROP(&ifp->if_snd); 4438e96e13eSMaxim Sobolev m_freem(m); 4448e96e13eSMaxim Sobolev error = EAFNOSUPPORT; 4458e96e13eSMaxim Sobolev goto end; 4468e96e13eSMaxim Sobolev } 447131c55bcSAndrew Thompson 448131c55bcSAndrew Thompson /* Reserve space for GRE header + optional GRE key */ 44944554a6dSJulian Elischer int hdrlen = sizeof(struct greip) + extra; 450131c55bcSAndrew Thompson if (sc->key) 451131c55bcSAndrew Thompson hdrlen += sizeof(uint32_t); 452eb1b1807SGleb Smirnoff M_PREPEND(m, hdrlen, M_NOWAIT); 4538e96e13eSMaxim Sobolev } else { 4548e96e13eSMaxim Sobolev _IF_DROP(&ifp->if_snd); 4558e96e13eSMaxim Sobolev m_freem(m); 4568e96e13eSMaxim Sobolev error = EINVAL; 4578e96e13eSMaxim Sobolev goto end; 4588e96e13eSMaxim Sobolev } 4598e96e13eSMaxim Sobolev 4605efdd80aSAndre Oppermann if (m == NULL) { /* mbuf allocation failed */ 4618e96e13eSMaxim Sobolev _IF_DROP(&ifp->if_snd); 4628e96e13eSMaxim Sobolev error = ENOBUFS; 4638e96e13eSMaxim Sobolev goto end; 4648e96e13eSMaxim Sobolev } 4658e96e13eSMaxim Sobolev 4668b07e49aSJulian Elischer M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */ 4678b07e49aSJulian Elischer 4688e96e13eSMaxim Sobolev gh = mtod(m, struct greip *); 4698e96e13eSMaxim Sobolev if (sc->g_proto == IPPROTO_GRE) { 470131c55bcSAndrew Thompson uint32_t *options = gh->gi_options; 471131c55bcSAndrew Thompson 47244554a6dSJulian Elischer memset((void *)gh, 0, sizeof(struct greip) + extra); 4738e96e13eSMaxim Sobolev gh->gi_ptype = htons(etype); 474131c55bcSAndrew Thompson gh->gi_flags = 0; 475131c55bcSAndrew Thompson 476131c55bcSAndrew Thompson /* Add key option */ 477131c55bcSAndrew Thompson if (sc->key) 478131c55bcSAndrew Thompson { 479131c55bcSAndrew Thompson gh->gi_flags |= htons(GRE_KP); 480131c55bcSAndrew Thompson *(options++) = htonl(sc->key); 481131c55bcSAndrew Thompson } 4828e96e13eSMaxim Sobolev } 4838e96e13eSMaxim Sobolev 4848e96e13eSMaxim Sobolev gh->gi_pr = sc->g_proto; 4858e96e13eSMaxim Sobolev if (sc->g_proto != IPPROTO_MOBILE) { 4868e96e13eSMaxim Sobolev gh->gi_src = sc->g_src; 4878e96e13eSMaxim Sobolev gh->gi_dst = sc->g_dst; 48897c4cd98SMaxim Sobolev ((struct ip*)gh)->ip_v = IPPROTO_IPV4; 4898e96e13eSMaxim Sobolev ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 490c23d234cSMaxim Sobolev ((struct ip*)gh)->ip_ttl = GRE_TTL; 491a54eadd8SJulian Elischer ((struct ip*)gh)->ip_tos = gre_ip_tos; 492a54eadd8SJulian Elischer ((struct ip*)gh)->ip_id = gre_ip_id; 4938f134647SGleb Smirnoff gh->gi_len = htons(m->m_pkthdr.len); 4948e96e13eSMaxim Sobolev } 4958e96e13eSMaxim Sobolev 4968e96e13eSMaxim Sobolev ifp->if_opackets++; 4978e96e13eSMaxim Sobolev ifp->if_obytes += m->m_pkthdr.len; 4988b75eec1SAndre Oppermann /* 4998b75eec1SAndre Oppermann * Send it off and with IP_FORWARD flag to prevent it from 5008b75eec1SAndre Oppermann * overwriting the ip_id again. ip_id is already set to the 5018b75eec1SAndre Oppermann * ip_id of the encapsulated packet. 5028b75eec1SAndre Oppermann */ 5035efdd80aSAndre Oppermann error = ip_output(m, NULL, &sc->route, IP_FORWARDING, 50473d7ddbcSMaxim Sobolev (struct ip_moptions *)NULL, (struct inpcb *)NULL); 5058e96e13eSMaxim Sobolev end: 5068e96e13eSMaxim Sobolev if (error) 5078e96e13eSMaxim Sobolev ifp->if_oerrors++; 5088e96e13eSMaxim Sobolev return (error); 5098e96e13eSMaxim Sobolev } 5108e96e13eSMaxim Sobolev 511c23d234cSMaxim Sobolev static int 5128e96e13eSMaxim Sobolev gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 5138e96e13eSMaxim Sobolev { 5148e96e13eSMaxim Sobolev struct ifreq *ifr = (struct ifreq *)data; 5158e96e13eSMaxim Sobolev struct if_laddrreq *lifr = (struct if_laddrreq *)data; 5168e96e13eSMaxim Sobolev struct in_aliasreq *aifr = (struct in_aliasreq *)data; 5178e96e13eSMaxim Sobolev struct gre_softc *sc = ifp->if_softc; 5188e96e13eSMaxim Sobolev struct sockaddr_in si; 5198e96e13eSMaxim Sobolev struct sockaddr *sa = NULL; 520131c55bcSAndrew Thompson int error, adj; 5218e96e13eSMaxim Sobolev struct sockaddr_in sp, sm, dp, dm; 522131c55bcSAndrew Thompson uint32_t key; 5238e96e13eSMaxim Sobolev 5248e96e13eSMaxim Sobolev error = 0; 525131c55bcSAndrew Thompson adj = 0; 5268e96e13eSMaxim Sobolev 5278e96e13eSMaxim Sobolev switch (cmd) { 5288e96e13eSMaxim Sobolev case SIOCSIFADDR: 5298e96e13eSMaxim Sobolev ifp->if_flags |= IFF_UP; 5308e96e13eSMaxim Sobolev break; 5318e96e13eSMaxim Sobolev case SIOCSIFDSTADDR: 5328e96e13eSMaxim Sobolev break; 5338e96e13eSMaxim Sobolev case SIOCSIFFLAGS: 534acd3428bSRobert Watson /* 535cc9bdf2aSRobert Watson * XXXRW: Isn't this priv_check() redundant to the ifnet 536cc9bdf2aSRobert Watson * layer check? 537acd3428bSRobert Watson */ 538acd3428bSRobert Watson if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0) 5398e96e13eSMaxim Sobolev break; 5408e96e13eSMaxim Sobolev if ((ifr->ifr_flags & IFF_LINK0) != 0) 5418e96e13eSMaxim Sobolev sc->g_proto = IPPROTO_GRE; 5428e96e13eSMaxim Sobolev else 5438e96e13eSMaxim Sobolev sc->g_proto = IPPROTO_MOBILE; 5447735aeb9SMaxim Sobolev if ((ifr->ifr_flags & IFF_LINK2) != 0) 5457735aeb9SMaxim Sobolev sc->wccp_ver = WCCP_V2; 5467735aeb9SMaxim Sobolev else 5477735aeb9SMaxim Sobolev sc->wccp_ver = WCCP_V1; 5488e96e13eSMaxim Sobolev goto recompute; 5498e96e13eSMaxim Sobolev case SIOCSIFMTU: 550acd3428bSRobert Watson /* 551cc9bdf2aSRobert Watson * XXXRW: Isn't this priv_check() redundant to the ifnet 552cc9bdf2aSRobert Watson * layer check? 553acd3428bSRobert Watson */ 554acd3428bSRobert Watson if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0) 5558e96e13eSMaxim Sobolev break; 5568e96e13eSMaxim Sobolev if (ifr->ifr_mtu < 576) { 5578e96e13eSMaxim Sobolev error = EINVAL; 5588e96e13eSMaxim Sobolev break; 5598e96e13eSMaxim Sobolev } 5608e96e13eSMaxim Sobolev ifp->if_mtu = ifr->ifr_mtu; 5618e96e13eSMaxim Sobolev break; 5628e96e13eSMaxim Sobolev case SIOCGIFMTU: 563fc74a9f9SBrooks Davis ifr->ifr_mtu = GRE2IFP(sc)->if_mtu; 5648e96e13eSMaxim Sobolev break; 5658e96e13eSMaxim Sobolev case SIOCADDMULTI: 566acd3428bSRobert Watson /* 567cc9bdf2aSRobert Watson * XXXRW: Isn't this priv_checkr() redundant to the ifnet 568cc9bdf2aSRobert Watson * layer check? 569acd3428bSRobert Watson */ 570acd3428bSRobert Watson if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0) 571acd3428bSRobert Watson break; 572acd3428bSRobert Watson if (ifr == 0) { 573acd3428bSRobert Watson error = EAFNOSUPPORT; 574acd3428bSRobert Watson break; 575acd3428bSRobert Watson } 576acd3428bSRobert Watson switch (ifr->ifr_addr.sa_family) { 577acd3428bSRobert Watson #ifdef INET 578acd3428bSRobert Watson case AF_INET: 579acd3428bSRobert Watson break; 580acd3428bSRobert Watson #endif 581acd3428bSRobert Watson #ifdef INET6 582acd3428bSRobert Watson case AF_INET6: 583acd3428bSRobert Watson break; 584acd3428bSRobert Watson #endif 585acd3428bSRobert Watson default: 586acd3428bSRobert Watson error = EAFNOSUPPORT; 587acd3428bSRobert Watson break; 588acd3428bSRobert Watson } 589acd3428bSRobert Watson break; 5908e96e13eSMaxim Sobolev case SIOCDELMULTI: 591acd3428bSRobert Watson /* 592cc9bdf2aSRobert Watson * XXXRW: Isn't this priv_check() redundant to the ifnet 593cc9bdf2aSRobert Watson * layer check? 594acd3428bSRobert Watson */ 595acd3428bSRobert Watson if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0) 5968e96e13eSMaxim Sobolev break; 5978e96e13eSMaxim Sobolev if (ifr == 0) { 5988e96e13eSMaxim Sobolev error = EAFNOSUPPORT; 5998e96e13eSMaxim Sobolev break; 6008e96e13eSMaxim Sobolev } 6018e96e13eSMaxim Sobolev switch (ifr->ifr_addr.sa_family) { 6028e96e13eSMaxim Sobolev #ifdef INET 6038e96e13eSMaxim Sobolev case AF_INET: 6048e96e13eSMaxim Sobolev break; 6058e96e13eSMaxim Sobolev #endif 6069e669156SBjoern A. Zeeb #ifdef INET6 6079e669156SBjoern A. Zeeb case AF_INET6: 6089e669156SBjoern A. Zeeb break; 6099e669156SBjoern A. Zeeb #endif 6108e96e13eSMaxim Sobolev default: 6118e96e13eSMaxim Sobolev error = EAFNOSUPPORT; 6128e96e13eSMaxim Sobolev break; 6138e96e13eSMaxim Sobolev } 6148e96e13eSMaxim Sobolev break; 6158e96e13eSMaxim Sobolev case GRESPROTO: 616acd3428bSRobert Watson /* 617cc9bdf2aSRobert Watson * XXXRW: Isn't this priv_check() redundant to the ifnet 618cc9bdf2aSRobert Watson * layer check? 619acd3428bSRobert Watson */ 620acd3428bSRobert Watson if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0) 6218e96e13eSMaxim Sobolev break; 6228e96e13eSMaxim Sobolev sc->g_proto = ifr->ifr_flags; 6238e96e13eSMaxim Sobolev switch (sc->g_proto) { 6248e96e13eSMaxim Sobolev case IPPROTO_GRE: 6258e96e13eSMaxim Sobolev ifp->if_flags |= IFF_LINK0; 6268e96e13eSMaxim Sobolev break; 6278e96e13eSMaxim Sobolev case IPPROTO_MOBILE: 6288e96e13eSMaxim Sobolev ifp->if_flags &= ~IFF_LINK0; 6298e96e13eSMaxim Sobolev break; 6308e96e13eSMaxim Sobolev default: 6318e96e13eSMaxim Sobolev error = EPROTONOSUPPORT; 6328e96e13eSMaxim Sobolev break; 6338e96e13eSMaxim Sobolev } 6348e96e13eSMaxim Sobolev goto recompute; 6358e96e13eSMaxim Sobolev case GREGPROTO: 6368e96e13eSMaxim Sobolev ifr->ifr_flags = sc->g_proto; 6378e96e13eSMaxim Sobolev break; 6388e96e13eSMaxim Sobolev case GRESADDRS: 6398e96e13eSMaxim Sobolev case GRESADDRD: 640acd3428bSRobert Watson error = priv_check(curthread, PRIV_NET_GRE); 641acd3428bSRobert Watson if (error) 642acd3428bSRobert Watson return (error); 6438e96e13eSMaxim Sobolev /* 6448e96e13eSMaxim Sobolev * set tunnel endpoints, compute a less specific route 6458e96e13eSMaxim Sobolev * to the remote end and mark if as up 6468e96e13eSMaxim Sobolev */ 6478e96e13eSMaxim Sobolev sa = &ifr->ifr_addr; 6488e96e13eSMaxim Sobolev if (cmd == GRESADDRS) 6498e96e13eSMaxim Sobolev sc->g_src = (satosin(sa))->sin_addr; 6508e96e13eSMaxim Sobolev if (cmd == GRESADDRD) 6518e96e13eSMaxim Sobolev sc->g_dst = (satosin(sa))->sin_addr; 6528e96e13eSMaxim Sobolev recompute: 6538e96e13eSMaxim Sobolev #ifdef INET 6548e96e13eSMaxim Sobolev if (sc->encap != NULL) { 6558e96e13eSMaxim Sobolev encap_detach(sc->encap); 6568e96e13eSMaxim Sobolev sc->encap = NULL; 6578e96e13eSMaxim Sobolev } 6588e96e13eSMaxim Sobolev #endif 6598e96e13eSMaxim Sobolev if ((sc->g_src.s_addr != INADDR_ANY) && 6608e96e13eSMaxim Sobolev (sc->g_dst.s_addr != INADDR_ANY)) { 6618e96e13eSMaxim Sobolev bzero(&sp, sizeof(sp)); 6628e96e13eSMaxim Sobolev bzero(&sm, sizeof(sm)); 6638e96e13eSMaxim Sobolev bzero(&dp, sizeof(dp)); 6648e96e13eSMaxim Sobolev bzero(&dm, sizeof(dm)); 6658e96e13eSMaxim Sobolev sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 6668e96e13eSMaxim Sobolev sizeof(struct sockaddr_in); 6678e96e13eSMaxim Sobolev sp.sin_family = sm.sin_family = dp.sin_family = 6688e96e13eSMaxim Sobolev dm.sin_family = AF_INET; 6698e96e13eSMaxim Sobolev sp.sin_addr = sc->g_src; 6708e96e13eSMaxim Sobolev dp.sin_addr = sc->g_dst; 6718e96e13eSMaxim Sobolev sm.sin_addr.s_addr = dm.sin_addr.s_addr = 6728e96e13eSMaxim Sobolev INADDR_BROADCAST; 6738e96e13eSMaxim Sobolev #ifdef INET 6748e96e13eSMaxim Sobolev sc->encap = encap_attach(AF_INET, sc->g_proto, 6758e96e13eSMaxim Sobolev sintosa(&sp), sintosa(&sm), sintosa(&dp), 6768e96e13eSMaxim Sobolev sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 6778e96e13eSMaxim Sobolev &in_gre_protosw : &in_mobile_protosw, sc); 6788e96e13eSMaxim Sobolev if (sc->encap == NULL) 6798e96e13eSMaxim Sobolev printf("%s: unable to attach encap\n", 680fc74a9f9SBrooks Davis if_name(GRE2IFP(sc))); 6818e96e13eSMaxim Sobolev #endif 6828e96e13eSMaxim Sobolev if (sc->route.ro_rt != 0) /* free old route */ 6838e96e13eSMaxim Sobolev RTFREE(sc->route.ro_rt); 6848e96e13eSMaxim Sobolev if (gre_compute_route(sc) == 0) 68513f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 6868e96e13eSMaxim Sobolev else 68713f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 6888e96e13eSMaxim Sobolev } 6898e96e13eSMaxim Sobolev break; 6908e96e13eSMaxim Sobolev case GREGADDRS: 6918e96e13eSMaxim Sobolev memset(&si, 0, sizeof(si)); 6928e96e13eSMaxim Sobolev si.sin_family = AF_INET; 6938e96e13eSMaxim Sobolev si.sin_len = sizeof(struct sockaddr_in); 6948e96e13eSMaxim Sobolev si.sin_addr.s_addr = sc->g_src.s_addr; 6958e96e13eSMaxim Sobolev sa = sintosa(&si); 696e3416ab0SBjoern A. Zeeb error = prison_if(curthread->td_ucred, sa); 697e3416ab0SBjoern A. Zeeb if (error != 0) 698e3416ab0SBjoern A. Zeeb break; 6998e96e13eSMaxim Sobolev ifr->ifr_addr = *sa; 7008e96e13eSMaxim Sobolev break; 7018e96e13eSMaxim Sobolev case GREGADDRD: 7028e96e13eSMaxim Sobolev memset(&si, 0, sizeof(si)); 7038e96e13eSMaxim Sobolev si.sin_family = AF_INET; 7048e96e13eSMaxim Sobolev si.sin_len = sizeof(struct sockaddr_in); 7058e96e13eSMaxim Sobolev si.sin_addr.s_addr = sc->g_dst.s_addr; 7068e96e13eSMaxim Sobolev sa = sintosa(&si); 707e3416ab0SBjoern A. Zeeb error = prison_if(curthread->td_ucred, sa); 708e3416ab0SBjoern A. Zeeb if (error != 0) 709e3416ab0SBjoern A. Zeeb break; 7108e96e13eSMaxim Sobolev ifr->ifr_addr = *sa; 7118e96e13eSMaxim Sobolev break; 7128e96e13eSMaxim Sobolev case SIOCSIFPHYADDR: 713acd3428bSRobert Watson /* 714cc9bdf2aSRobert Watson * XXXRW: Isn't this priv_check() redundant to the ifnet 715cc9bdf2aSRobert Watson * layer check? 716acd3428bSRobert Watson */ 717acd3428bSRobert Watson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 7188e96e13eSMaxim Sobolev break; 7198e96e13eSMaxim Sobolev if (aifr->ifra_addr.sin_family != AF_INET || 7208e96e13eSMaxim Sobolev aifr->ifra_dstaddr.sin_family != AF_INET) { 7218e96e13eSMaxim Sobolev error = EAFNOSUPPORT; 7228e96e13eSMaxim Sobolev break; 7238e96e13eSMaxim Sobolev } 7248e96e13eSMaxim Sobolev if (aifr->ifra_addr.sin_len != sizeof(si) || 7258e96e13eSMaxim Sobolev aifr->ifra_dstaddr.sin_len != sizeof(si)) { 7268e96e13eSMaxim Sobolev error = EINVAL; 7278e96e13eSMaxim Sobolev break; 7288e96e13eSMaxim Sobolev } 7298e96e13eSMaxim Sobolev sc->g_src = aifr->ifra_addr.sin_addr; 7308e96e13eSMaxim Sobolev sc->g_dst = aifr->ifra_dstaddr.sin_addr; 7318e96e13eSMaxim Sobolev goto recompute; 7328e96e13eSMaxim Sobolev case SIOCSLIFPHYADDR: 733acd3428bSRobert Watson /* 734cc9bdf2aSRobert Watson * XXXRW: Isn't this priv_check() redundant to the ifnet 735cc9bdf2aSRobert Watson * layer check? 736acd3428bSRobert Watson */ 737acd3428bSRobert Watson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 7388e96e13eSMaxim Sobolev break; 7398e96e13eSMaxim Sobolev if (lifr->addr.ss_family != AF_INET || 7408e96e13eSMaxim Sobolev lifr->dstaddr.ss_family != AF_INET) { 7418e96e13eSMaxim Sobolev error = EAFNOSUPPORT; 7428e96e13eSMaxim Sobolev break; 7438e96e13eSMaxim Sobolev } 7448e96e13eSMaxim Sobolev if (lifr->addr.ss_len != sizeof(si) || 7458e96e13eSMaxim Sobolev lifr->dstaddr.ss_len != sizeof(si)) { 7468e96e13eSMaxim Sobolev error = EINVAL; 7478e96e13eSMaxim Sobolev break; 7488e96e13eSMaxim Sobolev } 749d03e5467SQing Li sc->g_src = (satosin(&lifr->addr))->sin_addr; 7508e96e13eSMaxim Sobolev sc->g_dst = 751d03e5467SQing Li (satosin(&lifr->dstaddr))->sin_addr; 7528e96e13eSMaxim Sobolev goto recompute; 7538e96e13eSMaxim Sobolev case SIOCDIFPHYADDR: 754acd3428bSRobert Watson /* 755cc9bdf2aSRobert Watson * XXXRW: Isn't this priv_check() redundant to the ifnet 756cc9bdf2aSRobert Watson * layer check? 757acd3428bSRobert Watson */ 758acd3428bSRobert Watson if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 7598e96e13eSMaxim Sobolev break; 7608e96e13eSMaxim Sobolev sc->g_src.s_addr = INADDR_ANY; 7618e96e13eSMaxim Sobolev sc->g_dst.s_addr = INADDR_ANY; 7628e96e13eSMaxim Sobolev goto recompute; 7638e96e13eSMaxim Sobolev case SIOCGLIFPHYADDR: 7648e96e13eSMaxim Sobolev if (sc->g_src.s_addr == INADDR_ANY || 7658e96e13eSMaxim Sobolev sc->g_dst.s_addr == INADDR_ANY) { 7668e96e13eSMaxim Sobolev error = EADDRNOTAVAIL; 7678e96e13eSMaxim Sobolev break; 7688e96e13eSMaxim Sobolev } 7698e96e13eSMaxim Sobolev memset(&si, 0, sizeof(si)); 7708e96e13eSMaxim Sobolev si.sin_family = AF_INET; 7718e96e13eSMaxim Sobolev si.sin_len = sizeof(struct sockaddr_in); 7728e96e13eSMaxim Sobolev si.sin_addr.s_addr = sc->g_src.s_addr; 773e3416ab0SBjoern A. Zeeb error = prison_if(curthread->td_ucred, (struct sockaddr *)&si); 774e3416ab0SBjoern A. Zeeb if (error != 0) 775e3416ab0SBjoern A. Zeeb break; 7768e96e13eSMaxim Sobolev memcpy(&lifr->addr, &si, sizeof(si)); 7778e96e13eSMaxim Sobolev si.sin_addr.s_addr = sc->g_dst.s_addr; 778e3416ab0SBjoern A. Zeeb error = prison_if(curthread->td_ucred, (struct sockaddr *)&si); 779e3416ab0SBjoern A. Zeeb if (error != 0) 780e3416ab0SBjoern A. Zeeb break; 7818e96e13eSMaxim Sobolev memcpy(&lifr->dstaddr, &si, sizeof(si)); 7828e96e13eSMaxim Sobolev break; 7838e96e13eSMaxim Sobolev case SIOCGIFPSRCADDR: 784f16770aeSBruce M Simpson #ifdef INET6 785f16770aeSBruce M Simpson case SIOCGIFPSRCADDR_IN6: 786f16770aeSBruce M Simpson #endif 7878e96e13eSMaxim Sobolev if (sc->g_src.s_addr == INADDR_ANY) { 7888e96e13eSMaxim Sobolev error = EADDRNOTAVAIL; 7898e96e13eSMaxim Sobolev break; 7908e96e13eSMaxim Sobolev } 7918e96e13eSMaxim Sobolev memset(&si, 0, sizeof(si)); 7928e96e13eSMaxim Sobolev si.sin_family = AF_INET; 7938e96e13eSMaxim Sobolev si.sin_len = sizeof(struct sockaddr_in); 7948e96e13eSMaxim Sobolev si.sin_addr.s_addr = sc->g_src.s_addr; 795e3416ab0SBjoern A. Zeeb error = prison_if(curthread->td_ucred, (struct sockaddr *)&si); 796e3416ab0SBjoern A. Zeeb if (error != 0) 797e3416ab0SBjoern A. Zeeb break; 7988e96e13eSMaxim Sobolev bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 7998e96e13eSMaxim Sobolev break; 8008e96e13eSMaxim Sobolev case SIOCGIFPDSTADDR: 801f16770aeSBruce M Simpson #ifdef INET6 802f16770aeSBruce M Simpson case SIOCGIFPDSTADDR_IN6: 803f16770aeSBruce M Simpson #endif 8048e96e13eSMaxim Sobolev if (sc->g_dst.s_addr == INADDR_ANY) { 8058e96e13eSMaxim Sobolev error = EADDRNOTAVAIL; 8068e96e13eSMaxim Sobolev break; 8078e96e13eSMaxim Sobolev } 8088e96e13eSMaxim Sobolev memset(&si, 0, sizeof(si)); 8098e96e13eSMaxim Sobolev si.sin_family = AF_INET; 8108e96e13eSMaxim Sobolev si.sin_len = sizeof(struct sockaddr_in); 8118e96e13eSMaxim Sobolev si.sin_addr.s_addr = sc->g_dst.s_addr; 812e3416ab0SBjoern A. Zeeb error = prison_if(curthread->td_ucred, (struct sockaddr *)&si); 813e3416ab0SBjoern A. Zeeb if (error != 0) 814e3416ab0SBjoern A. Zeeb break; 8158e96e13eSMaxim Sobolev bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 8168e96e13eSMaxim Sobolev break; 817131c55bcSAndrew Thompson case GRESKEY: 818131c55bcSAndrew Thompson error = priv_check(curthread, PRIV_NET_GRE); 819131c55bcSAndrew Thompson if (error) 820131c55bcSAndrew Thompson break; 821131c55bcSAndrew Thompson error = copyin(ifr->ifr_data, &key, sizeof(key)); 822131c55bcSAndrew Thompson if (error) 823131c55bcSAndrew Thompson break; 824131c55bcSAndrew Thompson /* adjust MTU for option header */ 825131c55bcSAndrew Thompson if (key == 0 && sc->key != 0) /* clear */ 826131c55bcSAndrew Thompson adj += sizeof(key); 827131c55bcSAndrew Thompson else if (key != 0 && sc->key == 0) /* set */ 828131c55bcSAndrew Thompson adj -= sizeof(key); 829131c55bcSAndrew Thompson 830131c55bcSAndrew Thompson if (ifp->if_mtu + adj < 576) { 831131c55bcSAndrew Thompson error = EINVAL; 832131c55bcSAndrew Thompson break; 833131c55bcSAndrew Thompson } 834131c55bcSAndrew Thompson ifp->if_mtu += adj; 835131c55bcSAndrew Thompson sc->key = key; 836131c55bcSAndrew Thompson break; 837131c55bcSAndrew Thompson case GREGKEY: 838131c55bcSAndrew Thompson error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key)); 839131c55bcSAndrew Thompson break; 840131c55bcSAndrew Thompson 8418e96e13eSMaxim Sobolev default: 8428e96e13eSMaxim Sobolev error = EINVAL; 8438e96e13eSMaxim Sobolev break; 8448e96e13eSMaxim Sobolev } 8458e96e13eSMaxim Sobolev 8468e96e13eSMaxim Sobolev return (error); 8478e96e13eSMaxim Sobolev } 8488e96e13eSMaxim Sobolev 8498e96e13eSMaxim Sobolev /* 8508e96e13eSMaxim Sobolev * computes a route to our destination that is not the one 8518e96e13eSMaxim Sobolev * which would be taken by ip_output(), as this one will loop back to 8528e96e13eSMaxim Sobolev * us. If the interface is p2p as a--->b, then a routing entry exists 8538e96e13eSMaxim Sobolev * If we now send a packet to b (e.g. ping b), this will come down here 85473d7ddbcSMaxim Sobolev * gets src=a, dst=b tacked on and would from ip_output() sent back to 8558e96e13eSMaxim Sobolev * if_gre. 8568e96e13eSMaxim Sobolev * Goal here is to compute a route to b that is less specific than 8578e96e13eSMaxim Sobolev * a-->b. We know that this one exists as in normal operation we have 8588e96e13eSMaxim Sobolev * at least a default route which matches. 8598e96e13eSMaxim Sobolev */ 860c23d234cSMaxim Sobolev static int 8618e96e13eSMaxim Sobolev gre_compute_route(struct gre_softc *sc) 8628e96e13eSMaxim Sobolev { 8638e96e13eSMaxim Sobolev struct route *ro; 8648e96e13eSMaxim Sobolev 8658e96e13eSMaxim Sobolev ro = &sc->route; 8668e96e13eSMaxim Sobolev 8678e96e13eSMaxim Sobolev memset(ro, 0, sizeof(struct route)); 8688e96e13eSMaxim Sobolev ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 8698e96e13eSMaxim Sobolev ro->ro_dst.sa_family = AF_INET; 8708e96e13eSMaxim Sobolev ro->ro_dst.sa_len = sizeof(ro->ro_dst); 8718e96e13eSMaxim Sobolev 8728e96e13eSMaxim Sobolev /* 8738e96e13eSMaxim Sobolev * toggle last bit, so our interface is not found, but a less 8748e96e13eSMaxim Sobolev * specific route. I'd rather like to specify a shorter mask, 8758e96e13eSMaxim Sobolev * but this is not possible. Should work though. XXX 8768b07e49aSJulian Elischer * XXX MRT Use a different FIB for the tunnel to solve this problem. 8778e96e13eSMaxim Sobolev */ 878fc74a9f9SBrooks Davis if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) { 87929481f88SJulian Elischer ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^= 88029481f88SJulian Elischer htonl(0x01); 8818e96e13eSMaxim Sobolev } 8828e96e13eSMaxim Sobolev 8838e96e13eSMaxim Sobolev #ifdef DIAGNOSTIC 884fc74a9f9SBrooks Davis printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)), 8858e96e13eSMaxim Sobolev inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 8868e96e13eSMaxim Sobolev #endif 8878e96e13eSMaxim Sobolev 8888b07e49aSJulian Elischer rtalloc_fib(ro, sc->gre_fibnum); 8898e96e13eSMaxim Sobolev 8908e96e13eSMaxim Sobolev /* 8918e96e13eSMaxim Sobolev * check if this returned a route at all and this route is no 8928e96e13eSMaxim Sobolev * recursion to ourself 8938e96e13eSMaxim Sobolev */ 8948e96e13eSMaxim Sobolev if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 8958e96e13eSMaxim Sobolev #ifdef DIAGNOSTIC 8968e96e13eSMaxim Sobolev if (ro->ro_rt == NULL) 8978e96e13eSMaxim Sobolev printf(" - no route found!\n"); 8988e96e13eSMaxim Sobolev else 8998e96e13eSMaxim Sobolev printf(" - route loops back to ourself!\n"); 9008e96e13eSMaxim Sobolev #endif 9018e96e13eSMaxim Sobolev return EADDRNOTAVAIL; 9028e96e13eSMaxim Sobolev } 9038e96e13eSMaxim Sobolev 9048e96e13eSMaxim Sobolev /* 9058e96e13eSMaxim Sobolev * now change it back - else ip_output will just drop 9068e96e13eSMaxim Sobolev * the route and search one to this interface ... 9078e96e13eSMaxim Sobolev */ 908fc74a9f9SBrooks Davis if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) 9098e96e13eSMaxim Sobolev ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 9108e96e13eSMaxim Sobolev 9118e96e13eSMaxim Sobolev #ifdef DIAGNOSTIC 9128e96e13eSMaxim Sobolev printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 9138e96e13eSMaxim Sobolev inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 9148e96e13eSMaxim Sobolev printf("\n"); 9158e96e13eSMaxim Sobolev #endif 9168e96e13eSMaxim Sobolev 9178e96e13eSMaxim Sobolev return 0; 9188e96e13eSMaxim Sobolev } 9198e96e13eSMaxim Sobolev 9208e96e13eSMaxim Sobolev /* 9218e96e13eSMaxim Sobolev * do a checksum of a buffer - much like in_cksum, which operates on 9228e96e13eSMaxim Sobolev * mbufs. 9238e96e13eSMaxim Sobolev */ 92473d7ddbcSMaxim Sobolev u_int16_t 92573d7ddbcSMaxim Sobolev gre_in_cksum(u_int16_t *p, u_int len) 9268e96e13eSMaxim Sobolev { 92773d7ddbcSMaxim Sobolev u_int32_t sum = 0; 9288e96e13eSMaxim Sobolev int nwords = len >> 1; 9298e96e13eSMaxim Sobolev 9308e96e13eSMaxim Sobolev while (nwords-- != 0) 9318e96e13eSMaxim Sobolev sum += *p++; 9328e96e13eSMaxim Sobolev 9338e96e13eSMaxim Sobolev if (len & 1) { 9348e96e13eSMaxim Sobolev union { 9358e96e13eSMaxim Sobolev u_short w; 9368e96e13eSMaxim Sobolev u_char c[2]; 9378e96e13eSMaxim Sobolev } u; 9388e96e13eSMaxim Sobolev u.c[0] = *(u_char *)p; 9398e96e13eSMaxim Sobolev u.c[1] = 0; 9408e96e13eSMaxim Sobolev sum += u.w; 9418e96e13eSMaxim Sobolev } 9428e96e13eSMaxim Sobolev 9438e96e13eSMaxim Sobolev /* end-around-carry */ 9448e96e13eSMaxim Sobolev sum = (sum >> 16) + (sum & 0xffff); 9458e96e13eSMaxim Sobolev sum += (sum >> 16); 9468e96e13eSMaxim Sobolev return (~sum); 9478e96e13eSMaxim Sobolev } 9488e96e13eSMaxim Sobolev 9498e96e13eSMaxim Sobolev static int 9508e96e13eSMaxim Sobolev gremodevent(module_t mod, int type, void *data) 9518e96e13eSMaxim Sobolev { 9528e96e13eSMaxim Sobolev 9538e96e13eSMaxim Sobolev switch (type) { 9548e96e13eSMaxim Sobolev case MOD_LOAD: 9558e96e13eSMaxim Sobolev greattach(); 9568e96e13eSMaxim Sobolev break; 9578e96e13eSMaxim Sobolev case MOD_UNLOAD: 95842a58907SGleb Smirnoff if_clone_detach(gre_cloner); 959bdae44a8SRobert Watson mtx_destroy(&gre_mtx); 9608e96e13eSMaxim Sobolev break; 9613e019deaSPoul-Henning Kamp default: 9623e019deaSPoul-Henning Kamp return EOPNOTSUPP; 9638e96e13eSMaxim Sobolev } 9648e96e13eSMaxim Sobolev return 0; 9658e96e13eSMaxim Sobolev } 9668e96e13eSMaxim Sobolev 9678e96e13eSMaxim Sobolev static moduledata_t gre_mod = { 9688e96e13eSMaxim Sobolev "if_gre", 9698e96e13eSMaxim Sobolev gremodevent, 9709823d527SKevin Lo 0 9718e96e13eSMaxim Sobolev }; 9728e96e13eSMaxim Sobolev 9738e96e13eSMaxim Sobolev DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 9748e96e13eSMaxim Sobolev MODULE_VERSION(if_gre, 1); 975