1/* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
| 1/* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
|
2/* $FreeBSD: head/sys/net/if_gre.c 147643 2005-06-28 06:55:45Z bz $ */
| 2/* $FreeBSD: head/sys/net/if_gre.c 148613 2005-08-01 08:14:21Z bz $ */
|
3 4/*- 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Heiko W.Rupp <hwr@pilhuhn.de> 10 *
| 3 4/*- 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Heiko W.Rupp <hwr@pilhuhn.de> 10 *
|
| 11 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de> 12 *
|
11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40/* 41 * Encapsulate L3 protocols into IP
| 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the NetBSD 24 * Foundation, Inc. and its contributors. 25 * 4. Neither the name of The NetBSD Foundation nor the names of its 26 * contributors may be used to endorse or promote products derived 27 * from this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 */ 41 42/* 43 * Encapsulate L3 protocols into IP
|
42 * See RFC 1701 and 1702 for more details.
| 44 * See RFC 2784 (successor of RFC 1701 and 1702) for more details.
|
43 * If_gre is compatible with Cisco GRE tunnels, so you can 44 * have a NetBSD box as the other end of a tunnel interface of a Cisco 45 * router. See gre(4) for more details. 46 * Also supported: IP in IP encaps (proto 55) as of RFC 2004 47 */ 48 49#include "opt_atalk.h" 50#include "opt_inet.h" 51#include "opt_inet6.h" 52 53#include <sys/param.h> 54#include <sys/kernel.h> 55#include <sys/malloc.h> 56#include <sys/module.h> 57#include <sys/mbuf.h> 58#include <sys/protosw.h> 59#include <sys/socket.h> 60#include <sys/sockio.h> 61#include <sys/sysctl.h> 62#include <sys/systm.h> 63 64#include <net/ethernet.h> 65#include <net/if.h> 66#include <net/if_clone.h> 67#include <net/if_types.h> 68#include <net/route.h> 69 70#ifdef INET 71#include <netinet/in.h> 72#include <netinet/in_systm.h> 73#include <netinet/in_var.h> 74#include <netinet/ip.h> 75#include <netinet/ip_gre.h> 76#include <netinet/ip_var.h> 77#include <netinet/ip_encap.h> 78#else 79#error "Huh? if_gre without inet?" 80#endif 81 82#include <net/bpf.h> 83 84#include <net/net_osdep.h> 85#include <net/if_gre.h> 86 87/* 88 * It is not easy to calculate the right value for a GRE MTU. 89 * We leave this task to the admin and use the same default that 90 * other vendors use. 91 */ 92#define GREMTU 1476 93 94#define GRENAME "gre" 95 96/* 97 * gre_mtx protects all global variables in if_gre.c. 98 * XXX: gre_softc data not protected yet. 99 */ 100struct mtx gre_mtx; 101static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation"); 102 103struct gre_softc_head gre_softc_list; 104 105static int gre_clone_create(struct if_clone *, int); 106static void gre_clone_destroy(struct ifnet *); 107static int gre_ioctl(struct ifnet *, u_long, caddr_t); 108static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *, 109 struct rtentry *rt); 110 111IFC_SIMPLE_DECLARE(gre, 0); 112 113static int gre_compute_route(struct gre_softc *sc); 114 115static void greattach(void); 116 117#ifdef INET 118extern struct domain inetdomain; 119static const struct protosw in_gre_protosw = 120{ SOCK_RAW, &inetdomain, IPPROTO_GRE, PR_ATOMIC|PR_ADDR, 121 (pr_input_t*)gre_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput, 122 0, 123 0, 0, 0, 0, 124 &rip_usrreqs 125}; 126static const struct protosw in_mobile_protosw = 127{ SOCK_RAW, &inetdomain, IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR, 128 (pr_input_t*)gre_mobile_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput, 129 0, 130 0, 0, 0, 0, 131 &rip_usrreqs 132}; 133#endif 134 135SYSCTL_DECL(_net_link); 136SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0, 137 "Generic Routing Encapsulation"); 138#ifndef MAX_GRE_NEST 139/* 140 * This macro controls the default upper limitation on nesting of gre tunnels. 141 * Since, setting a large value to this macro with a careless configuration 142 * may introduce system crash, we don't allow any nestings by default. 143 * If you need to configure nested gre tunnels, you can define this macro 144 * in your kernel configuration file. However, if you do so, please be 145 * careful to configure the tunnels so that it won't make a loop. 146 */ 147#define MAX_GRE_NEST 1 148#endif 149static int max_gre_nesting = MAX_GRE_NEST; 150SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW, 151 &max_gre_nesting, 0, "Max nested tunnels"); 152 153/* ARGSUSED */ 154static void 155greattach(void) 156{ 157 158 mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF); 159 LIST_INIT(&gre_softc_list); 160 if_clone_attach(&gre_cloner); 161} 162 163static int 164gre_clone_create(ifc, unit) 165 struct if_clone *ifc; 166 int unit; 167{ 168 struct gre_softc *sc; 169 170 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO); 171 172 GRE2IFP(sc) = if_alloc(IFT_TUNNEL); 173 if (GRE2IFP(sc) == NULL) { 174 free(sc, M_GRE); 175 return (ENOSPC); 176 } 177 178 GRE2IFP(sc)->if_softc = sc; 179 if_initname(GRE2IFP(sc), ifc->ifc_name, unit); 180 181 GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 182 GRE2IFP(sc)->if_addrlen = 0; 183 GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */ 184 GRE2IFP(sc)->if_mtu = GREMTU; 185 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 186 GRE2IFP(sc)->if_output = gre_output; 187 GRE2IFP(sc)->if_ioctl = gre_ioctl; 188 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 189 sc->g_proto = IPPROTO_GRE; 190 GRE2IFP(sc)->if_flags |= IFF_LINK0; 191 sc->encap = NULL; 192 sc->called = 0; 193 sc->wccp_ver = WCCP_V1; 194 if_attach(GRE2IFP(sc)); 195 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 196 mtx_lock(&gre_mtx); 197 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 198 mtx_unlock(&gre_mtx); 199 return (0); 200} 201 202static void 203gre_destroy(struct gre_softc *sc) 204{ 205 206#ifdef INET 207 if (sc->encap != NULL) 208 encap_detach(sc->encap); 209#endif 210 bpfdetach(GRE2IFP(sc)); 211 if_detach(GRE2IFP(sc)); 212 if_free(GRE2IFP(sc)); 213 free(sc, M_GRE); 214} 215 216static void 217gre_clone_destroy(ifp) 218 struct ifnet *ifp; 219{ 220 struct gre_softc *sc = ifp->if_softc; 221 222 mtx_lock(&gre_mtx); 223 LIST_REMOVE(sc, sc_list); 224 mtx_unlock(&gre_mtx); 225 gre_destroy(sc); 226} 227 228/* 229 * The output routine. Takes a packet and encapsulates it in the protocol 230 * given by sc->g_proto. See also RFC 1701 and RFC 2004 231 */ 232static int 233gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 234 struct rtentry *rt) 235{ 236 int error = 0; 237 struct gre_softc *sc = ifp->if_softc; 238 struct greip *gh; 239 struct ip *ip;
| 45 * If_gre is compatible with Cisco GRE tunnels, so you can 46 * have a NetBSD box as the other end of a tunnel interface of a Cisco 47 * router. See gre(4) for more details. 48 * Also supported: IP in IP encaps (proto 55) as of RFC 2004 49 */ 50 51#include "opt_atalk.h" 52#include "opt_inet.h" 53#include "opt_inet6.h" 54 55#include <sys/param.h> 56#include <sys/kernel.h> 57#include <sys/malloc.h> 58#include <sys/module.h> 59#include <sys/mbuf.h> 60#include <sys/protosw.h> 61#include <sys/socket.h> 62#include <sys/sockio.h> 63#include <sys/sysctl.h> 64#include <sys/systm.h> 65 66#include <net/ethernet.h> 67#include <net/if.h> 68#include <net/if_clone.h> 69#include <net/if_types.h> 70#include <net/route.h> 71 72#ifdef INET 73#include <netinet/in.h> 74#include <netinet/in_systm.h> 75#include <netinet/in_var.h> 76#include <netinet/ip.h> 77#include <netinet/ip_gre.h> 78#include <netinet/ip_var.h> 79#include <netinet/ip_encap.h> 80#else 81#error "Huh? if_gre without inet?" 82#endif 83 84#include <net/bpf.h> 85 86#include <net/net_osdep.h> 87#include <net/if_gre.h> 88 89/* 90 * It is not easy to calculate the right value for a GRE MTU. 91 * We leave this task to the admin and use the same default that 92 * other vendors use. 93 */ 94#define GREMTU 1476 95 96#define GRENAME "gre" 97 98/* 99 * gre_mtx protects all global variables in if_gre.c. 100 * XXX: gre_softc data not protected yet. 101 */ 102struct mtx gre_mtx; 103static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation"); 104 105struct gre_softc_head gre_softc_list; 106 107static int gre_clone_create(struct if_clone *, int); 108static void gre_clone_destroy(struct ifnet *); 109static int gre_ioctl(struct ifnet *, u_long, caddr_t); 110static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *, 111 struct rtentry *rt); 112 113IFC_SIMPLE_DECLARE(gre, 0); 114 115static int gre_compute_route(struct gre_softc *sc); 116 117static void greattach(void); 118 119#ifdef INET 120extern struct domain inetdomain; 121static const struct protosw in_gre_protosw = 122{ SOCK_RAW, &inetdomain, IPPROTO_GRE, PR_ATOMIC|PR_ADDR, 123 (pr_input_t*)gre_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput, 124 0, 125 0, 0, 0, 0, 126 &rip_usrreqs 127}; 128static const struct protosw in_mobile_protosw = 129{ SOCK_RAW, &inetdomain, IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR, 130 (pr_input_t*)gre_mobile_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput, 131 0, 132 0, 0, 0, 0, 133 &rip_usrreqs 134}; 135#endif 136 137SYSCTL_DECL(_net_link); 138SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0, 139 "Generic Routing Encapsulation"); 140#ifndef MAX_GRE_NEST 141/* 142 * This macro controls the default upper limitation on nesting of gre tunnels. 143 * Since, setting a large value to this macro with a careless configuration 144 * may introduce system crash, we don't allow any nestings by default. 145 * If you need to configure nested gre tunnels, you can define this macro 146 * in your kernel configuration file. However, if you do so, please be 147 * careful to configure the tunnels so that it won't make a loop. 148 */ 149#define MAX_GRE_NEST 1 150#endif 151static int max_gre_nesting = MAX_GRE_NEST; 152SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW, 153 &max_gre_nesting, 0, "Max nested tunnels"); 154 155/* ARGSUSED */ 156static void 157greattach(void) 158{ 159 160 mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF); 161 LIST_INIT(&gre_softc_list); 162 if_clone_attach(&gre_cloner); 163} 164 165static int 166gre_clone_create(ifc, unit) 167 struct if_clone *ifc; 168 int unit; 169{ 170 struct gre_softc *sc; 171 172 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO); 173 174 GRE2IFP(sc) = if_alloc(IFT_TUNNEL); 175 if (GRE2IFP(sc) == NULL) { 176 free(sc, M_GRE); 177 return (ENOSPC); 178 } 179 180 GRE2IFP(sc)->if_softc = sc; 181 if_initname(GRE2IFP(sc), ifc->ifc_name, unit); 182 183 GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 184 GRE2IFP(sc)->if_addrlen = 0; 185 GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */ 186 GRE2IFP(sc)->if_mtu = GREMTU; 187 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 188 GRE2IFP(sc)->if_output = gre_output; 189 GRE2IFP(sc)->if_ioctl = gre_ioctl; 190 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 191 sc->g_proto = IPPROTO_GRE; 192 GRE2IFP(sc)->if_flags |= IFF_LINK0; 193 sc->encap = NULL; 194 sc->called = 0; 195 sc->wccp_ver = WCCP_V1; 196 if_attach(GRE2IFP(sc)); 197 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 198 mtx_lock(&gre_mtx); 199 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 200 mtx_unlock(&gre_mtx); 201 return (0); 202} 203 204static void 205gre_destroy(struct gre_softc *sc) 206{ 207 208#ifdef INET 209 if (sc->encap != NULL) 210 encap_detach(sc->encap); 211#endif 212 bpfdetach(GRE2IFP(sc)); 213 if_detach(GRE2IFP(sc)); 214 if_free(GRE2IFP(sc)); 215 free(sc, M_GRE); 216} 217 218static void 219gre_clone_destroy(ifp) 220 struct ifnet *ifp; 221{ 222 struct gre_softc *sc = ifp->if_softc; 223 224 mtx_lock(&gre_mtx); 225 LIST_REMOVE(sc, sc_list); 226 mtx_unlock(&gre_mtx); 227 gre_destroy(sc); 228} 229 230/* 231 * The output routine. Takes a packet and encapsulates it in the protocol 232 * given by sc->g_proto. See also RFC 1701 and RFC 2004 233 */ 234static int 235gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 236 struct rtentry *rt) 237{ 238 int error = 0; 239 struct gre_softc *sc = ifp->if_softc; 240 struct greip *gh; 241 struct ip *ip;
|
| 242 u_short ip_id = 0; 243 uint8_t ip_tos = 0;
|
240 u_int16_t etype = 0; 241 struct mobile_h mob_h; 242 u_int32_t af; 243 244 /* 245 * gre may cause infinite recursion calls when misconfigured. 246 * We'll prevent this by introducing upper limit. 247 */ 248 if (++(sc->called) > max_gre_nesting) { 249 printf("%s: gre_output: recursively called too many " 250 "times(%d)\n", if_name(GRE2IFP(sc)), sc->called); 251 m_freem(m); 252 error = EIO; /* is there better errno? */ 253 goto end; 254 } 255 256 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 || 257 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 258 m_freem(m); 259 error = ENETDOWN; 260 goto end; 261 } 262 263 gh = NULL; 264 ip = NULL; 265 266 /* BPF writes need to be handled specially. */ 267 if (dst->sa_family == AF_UNSPEC) { 268 bcopy(dst->sa_data, &af, sizeof(af)); 269 dst->sa_family = af; 270 } 271 272 if (ifp->if_bpf) { 273 af = dst->sa_family; 274 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 275 } 276 277 m->m_flags &= ~(M_BCAST|M_MCAST); 278 279 if (sc->g_proto == IPPROTO_MOBILE) { 280 if (dst->sa_family == AF_INET) { 281 struct mbuf *m0; 282 int msiz; 283 284 ip = mtod(m, struct ip *); 285 286 /* 287 * RFC2004 specifies that fragmented diagrams shouldn't 288 * be encapsulated. 289 */ 290 if ((ip->ip_off & IP_MF) != 0) { 291 _IF_DROP(&ifp->if_snd); 292 m_freem(m); 293 error = EINVAL; /* is there better errno? */ 294 goto end; 295 } 296 memset(&mob_h, 0, MOB_H_SIZ_L); 297 mob_h.proto = (ip->ip_p) << 8; 298 mob_h.odst = ip->ip_dst.s_addr; 299 ip->ip_dst.s_addr = sc->g_dst.s_addr; 300 301 /* 302 * If the packet comes from our host, we only change 303 * the destination address in the IP header. 304 * Else we also need to save and change the source 305 */ 306 if (in_hosteq(ip->ip_src, sc->g_src)) { 307 msiz = MOB_H_SIZ_S; 308 } else { 309 mob_h.proto |= MOB_H_SBIT; 310 mob_h.osrc = ip->ip_src.s_addr; 311 ip->ip_src.s_addr = sc->g_src.s_addr; 312 msiz = MOB_H_SIZ_L; 313 } 314 mob_h.proto = htons(mob_h.proto); 315 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz); 316 317 if ((m->m_data - msiz) < m->m_pktdat) { 318 /* need new mbuf */ 319 MGETHDR(m0, M_DONTWAIT, MT_HEADER); 320 if (m0 == NULL) { 321 _IF_DROP(&ifp->if_snd); 322 m_freem(m); 323 error = ENOBUFS; 324 goto end; 325 } 326 m0->m_next = m; 327 m->m_data += sizeof(struct ip); 328 m->m_len -= sizeof(struct ip); 329 m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 330 m0->m_len = msiz + sizeof(struct ip); 331 m0->m_data += max_linkhdr; 332 memcpy(mtod(m0, caddr_t), (caddr_t)ip, 333 sizeof(struct ip)); 334 m = m0; 335 } else { /* we have some space left in the old one */ 336 m->m_data -= msiz; 337 m->m_len += msiz; 338 m->m_pkthdr.len += msiz; 339 bcopy(ip, mtod(m, caddr_t), 340 sizeof(struct ip)); 341 } 342 ip = mtod(m, struct ip *); 343 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 344 ip->ip_len = ntohs(ip->ip_len) + msiz; 345 } else { /* AF_INET */ 346 _IF_DROP(&ifp->if_snd); 347 m_freem(m); 348 error = EINVAL; 349 goto end; 350 } 351 } else if (sc->g_proto == IPPROTO_GRE) { 352 switch (dst->sa_family) { 353 case AF_INET: 354 ip = mtod(m, struct ip *);
| 244 u_int16_t etype = 0; 245 struct mobile_h mob_h; 246 u_int32_t af; 247 248 /* 249 * gre may cause infinite recursion calls when misconfigured. 250 * We'll prevent this by introducing upper limit. 251 */ 252 if (++(sc->called) > max_gre_nesting) { 253 printf("%s: gre_output: recursively called too many " 254 "times(%d)\n", if_name(GRE2IFP(sc)), sc->called); 255 m_freem(m); 256 error = EIO; /* is there better errno? */ 257 goto end; 258 } 259 260 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 || 261 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 262 m_freem(m); 263 error = ENETDOWN; 264 goto end; 265 } 266 267 gh = NULL; 268 ip = NULL; 269 270 /* BPF writes need to be handled specially. */ 271 if (dst->sa_family == AF_UNSPEC) { 272 bcopy(dst->sa_data, &af, sizeof(af)); 273 dst->sa_family = af; 274 } 275 276 if (ifp->if_bpf) { 277 af = dst->sa_family; 278 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 279 } 280 281 m->m_flags &= ~(M_BCAST|M_MCAST); 282 283 if (sc->g_proto == IPPROTO_MOBILE) { 284 if (dst->sa_family == AF_INET) { 285 struct mbuf *m0; 286 int msiz; 287 288 ip = mtod(m, struct ip *); 289 290 /* 291 * RFC2004 specifies that fragmented diagrams shouldn't 292 * be encapsulated. 293 */ 294 if ((ip->ip_off & IP_MF) != 0) { 295 _IF_DROP(&ifp->if_snd); 296 m_freem(m); 297 error = EINVAL; /* is there better errno? */ 298 goto end; 299 } 300 memset(&mob_h, 0, MOB_H_SIZ_L); 301 mob_h.proto = (ip->ip_p) << 8; 302 mob_h.odst = ip->ip_dst.s_addr; 303 ip->ip_dst.s_addr = sc->g_dst.s_addr; 304 305 /* 306 * If the packet comes from our host, we only change 307 * the destination address in the IP header. 308 * Else we also need to save and change the source 309 */ 310 if (in_hosteq(ip->ip_src, sc->g_src)) { 311 msiz = MOB_H_SIZ_S; 312 } else { 313 mob_h.proto |= MOB_H_SBIT; 314 mob_h.osrc = ip->ip_src.s_addr; 315 ip->ip_src.s_addr = sc->g_src.s_addr; 316 msiz = MOB_H_SIZ_L; 317 } 318 mob_h.proto = htons(mob_h.proto); 319 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz); 320 321 if ((m->m_data - msiz) < m->m_pktdat) { 322 /* need new mbuf */ 323 MGETHDR(m0, M_DONTWAIT, MT_HEADER); 324 if (m0 == NULL) { 325 _IF_DROP(&ifp->if_snd); 326 m_freem(m); 327 error = ENOBUFS; 328 goto end; 329 } 330 m0->m_next = m; 331 m->m_data += sizeof(struct ip); 332 m->m_len -= sizeof(struct ip); 333 m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 334 m0->m_len = msiz + sizeof(struct ip); 335 m0->m_data += max_linkhdr; 336 memcpy(mtod(m0, caddr_t), (caddr_t)ip, 337 sizeof(struct ip)); 338 m = m0; 339 } else { /* we have some space left in the old one */ 340 m->m_data -= msiz; 341 m->m_len += msiz; 342 m->m_pkthdr.len += msiz; 343 bcopy(ip, mtod(m, caddr_t), 344 sizeof(struct ip)); 345 } 346 ip = mtod(m, struct ip *); 347 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 348 ip->ip_len = ntohs(ip->ip_len) + msiz; 349 } else { /* AF_INET */ 350 _IF_DROP(&ifp->if_snd); 351 m_freem(m); 352 error = EINVAL; 353 goto end; 354 } 355 } else if (sc->g_proto == IPPROTO_GRE) { 356 switch (dst->sa_family) { 357 case AF_INET: 358 ip = mtod(m, struct ip *);
|
| 359 ip_tos = ip->ip_tos; 360 ip_id = ip->ip_id;
|
355 etype = ETHERTYPE_IP; 356 break;
| 361 etype = ETHERTYPE_IP; 362 break;
|
| 363#ifdef INET6 364 case AF_INET6: 365 ip_id = ip_newid(); 366 etype = ETHERTYPE_IPV6; 367 break; 368#endif
|
357#ifdef NETATALK 358 case AF_APPLETALK: 359 etype = ETHERTYPE_ATALK; 360 break; 361#endif 362 default: 363 _IF_DROP(&ifp->if_snd); 364 m_freem(m); 365 error = EAFNOSUPPORT; 366 goto end; 367 } 368 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT); 369 } else { 370 _IF_DROP(&ifp->if_snd); 371 m_freem(m); 372 error = EINVAL; 373 goto end; 374 } 375 376 if (m == NULL) { /* mbuf allocation failed */ 377 _IF_DROP(&ifp->if_snd); 378 error = ENOBUFS; 379 goto end; 380 } 381 382 gh = mtod(m, struct greip *); 383 if (sc->g_proto == IPPROTO_GRE) { 384 /* we don't have any GRE flags for now */ 385 memset((void *)gh, 0, sizeof(struct greip)); 386 gh->gi_ptype = htons(etype); 387 } 388 389 gh->gi_pr = sc->g_proto; 390 if (sc->g_proto != IPPROTO_MOBILE) { 391 gh->gi_src = sc->g_src; 392 gh->gi_dst = sc->g_dst; 393 ((struct ip*)gh)->ip_v = IPPROTO_IPV4; 394 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 395 ((struct ip*)gh)->ip_ttl = GRE_TTL;
| 369#ifdef NETATALK 370 case AF_APPLETALK: 371 etype = ETHERTYPE_ATALK; 372 break; 373#endif 374 default: 375 _IF_DROP(&ifp->if_snd); 376 m_freem(m); 377 error = EAFNOSUPPORT; 378 goto end; 379 } 380 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT); 381 } else { 382 _IF_DROP(&ifp->if_snd); 383 m_freem(m); 384 error = EINVAL; 385 goto end; 386 } 387 388 if (m == NULL) { /* mbuf allocation failed */ 389 _IF_DROP(&ifp->if_snd); 390 error = ENOBUFS; 391 goto end; 392 } 393 394 gh = mtod(m, struct greip *); 395 if (sc->g_proto == IPPROTO_GRE) { 396 /* we don't have any GRE flags for now */ 397 memset((void *)gh, 0, sizeof(struct greip)); 398 gh->gi_ptype = htons(etype); 399 } 400 401 gh->gi_pr = sc->g_proto; 402 if (sc->g_proto != IPPROTO_MOBILE) { 403 gh->gi_src = sc->g_src; 404 gh->gi_dst = sc->g_dst; 405 ((struct ip*)gh)->ip_v = IPPROTO_IPV4; 406 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 407 ((struct ip*)gh)->ip_ttl = GRE_TTL;
|
396 ((struct ip*)gh)->ip_tos = ip->ip_tos; 397 ((struct ip*)gh)->ip_id = ip->ip_id;
| 408 ((struct ip*)gh)->ip_tos = ip_tos; 409 ((struct ip*)gh)->ip_id = ip_id;
|
398 gh->gi_len = m->m_pkthdr.len; 399 } 400 401 ifp->if_opackets++; 402 ifp->if_obytes += m->m_pkthdr.len; 403 /* 404 * Send it off and with IP_FORWARD flag to prevent it from 405 * overwriting the ip_id again. ip_id is already set to the 406 * ip_id of the encapsulated packet. 407 */ 408 error = ip_output(m, NULL, &sc->route, IP_FORWARDING, 409 (struct ip_moptions *)NULL, (struct inpcb *)NULL); 410 end: 411 sc->called = 0; 412 if (error) 413 ifp->if_oerrors++; 414 return (error); 415} 416 417static int 418gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 419{ 420 struct ifreq *ifr = (struct ifreq *)data; 421 struct if_laddrreq *lifr = (struct if_laddrreq *)data; 422 struct in_aliasreq *aifr = (struct in_aliasreq *)data; 423 struct gre_softc *sc = ifp->if_softc; 424 int s; 425 struct sockaddr_in si; 426 struct sockaddr *sa = NULL; 427 int error; 428 struct sockaddr_in sp, sm, dp, dm; 429 430 error = 0; 431 432 s = splnet(); 433 switch (cmd) { 434 case SIOCSIFADDR: 435 ifp->if_flags |= IFF_UP; 436 break; 437 case SIOCSIFDSTADDR: 438 break; 439 case SIOCSIFFLAGS: 440 if ((error = suser(curthread)) != 0) 441 break; 442 if ((ifr->ifr_flags & IFF_LINK0) != 0) 443 sc->g_proto = IPPROTO_GRE; 444 else 445 sc->g_proto = IPPROTO_MOBILE; 446 if ((ifr->ifr_flags & IFF_LINK2) != 0) 447 sc->wccp_ver = WCCP_V2; 448 else 449 sc->wccp_ver = WCCP_V1; 450 goto recompute; 451 case SIOCSIFMTU: 452 if ((error = suser(curthread)) != 0) 453 break; 454 if (ifr->ifr_mtu < 576) { 455 error = EINVAL; 456 break; 457 } 458 ifp->if_mtu = ifr->ifr_mtu; 459 break; 460 case SIOCGIFMTU: 461 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu; 462 break; 463 case SIOCADDMULTI: 464 case SIOCDELMULTI: 465 if ((error = suser(curthread)) != 0) 466 break; 467 if (ifr == 0) { 468 error = EAFNOSUPPORT; 469 break; 470 } 471 switch (ifr->ifr_addr.sa_family) { 472#ifdef INET 473 case AF_INET: 474 break; 475#endif
| 410 gh->gi_len = m->m_pkthdr.len; 411 } 412 413 ifp->if_opackets++; 414 ifp->if_obytes += m->m_pkthdr.len; 415 /* 416 * Send it off and with IP_FORWARD flag to prevent it from 417 * overwriting the ip_id again. ip_id is already set to the 418 * ip_id of the encapsulated packet. 419 */ 420 error = ip_output(m, NULL, &sc->route, IP_FORWARDING, 421 (struct ip_moptions *)NULL, (struct inpcb *)NULL); 422 end: 423 sc->called = 0; 424 if (error) 425 ifp->if_oerrors++; 426 return (error); 427} 428 429static int 430gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 431{ 432 struct ifreq *ifr = (struct ifreq *)data; 433 struct if_laddrreq *lifr = (struct if_laddrreq *)data; 434 struct in_aliasreq *aifr = (struct in_aliasreq *)data; 435 struct gre_softc *sc = ifp->if_softc; 436 int s; 437 struct sockaddr_in si; 438 struct sockaddr *sa = NULL; 439 int error; 440 struct sockaddr_in sp, sm, dp, dm; 441 442 error = 0; 443 444 s = splnet(); 445 switch (cmd) { 446 case SIOCSIFADDR: 447 ifp->if_flags |= IFF_UP; 448 break; 449 case SIOCSIFDSTADDR: 450 break; 451 case SIOCSIFFLAGS: 452 if ((error = suser(curthread)) != 0) 453 break; 454 if ((ifr->ifr_flags & IFF_LINK0) != 0) 455 sc->g_proto = IPPROTO_GRE; 456 else 457 sc->g_proto = IPPROTO_MOBILE; 458 if ((ifr->ifr_flags & IFF_LINK2) != 0) 459 sc->wccp_ver = WCCP_V2; 460 else 461 sc->wccp_ver = WCCP_V1; 462 goto recompute; 463 case SIOCSIFMTU: 464 if ((error = suser(curthread)) != 0) 465 break; 466 if (ifr->ifr_mtu < 576) { 467 error = EINVAL; 468 break; 469 } 470 ifp->if_mtu = ifr->ifr_mtu; 471 break; 472 case SIOCGIFMTU: 473 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu; 474 break; 475 case SIOCADDMULTI: 476 case SIOCDELMULTI: 477 if ((error = suser(curthread)) != 0) 478 break; 479 if (ifr == 0) { 480 error = EAFNOSUPPORT; 481 break; 482 } 483 switch (ifr->ifr_addr.sa_family) { 484#ifdef INET 485 case AF_INET: 486 break; 487#endif
|
| 488#ifdef INET6 489 case AF_INET6: 490 break; 491#endif
|
476 default: 477 error = EAFNOSUPPORT; 478 break; 479 } 480 break; 481 case GRESPROTO: 482 if ((error = suser(curthread)) != 0) 483 break; 484 sc->g_proto = ifr->ifr_flags; 485 switch (sc->g_proto) { 486 case IPPROTO_GRE: 487 ifp->if_flags |= IFF_LINK0; 488 break; 489 case IPPROTO_MOBILE: 490 ifp->if_flags &= ~IFF_LINK0; 491 break; 492 default: 493 error = EPROTONOSUPPORT; 494 break; 495 } 496 goto recompute; 497 case GREGPROTO: 498 ifr->ifr_flags = sc->g_proto; 499 break; 500 case GRESADDRS: 501 case GRESADDRD: 502 if ((error = suser(curthread)) != 0) 503 break; 504 /* 505 * set tunnel endpoints, compute a less specific route 506 * to the remote end and mark if as up 507 */ 508 sa = &ifr->ifr_addr; 509 if (cmd == GRESADDRS) 510 sc->g_src = (satosin(sa))->sin_addr; 511 if (cmd == GRESADDRD) 512 sc->g_dst = (satosin(sa))->sin_addr; 513 recompute: 514#ifdef INET 515 if (sc->encap != NULL) { 516 encap_detach(sc->encap); 517 sc->encap = NULL; 518 } 519#endif 520 if ((sc->g_src.s_addr != INADDR_ANY) && 521 (sc->g_dst.s_addr != INADDR_ANY)) { 522 bzero(&sp, sizeof(sp)); 523 bzero(&sm, sizeof(sm)); 524 bzero(&dp, sizeof(dp)); 525 bzero(&dm, sizeof(dm)); 526 sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 527 sizeof(struct sockaddr_in); 528 sp.sin_family = sm.sin_family = dp.sin_family = 529 dm.sin_family = AF_INET; 530 sp.sin_addr = sc->g_src; 531 dp.sin_addr = sc->g_dst; 532 sm.sin_addr.s_addr = dm.sin_addr.s_addr = 533 INADDR_BROADCAST; 534#ifdef INET 535 sc->encap = encap_attach(AF_INET, sc->g_proto, 536 sintosa(&sp), sintosa(&sm), sintosa(&dp), 537 sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 538 &in_gre_protosw : &in_mobile_protosw, sc); 539 if (sc->encap == NULL) 540 printf("%s: unable to attach encap\n", 541 if_name(GRE2IFP(sc))); 542#endif 543 if (sc->route.ro_rt != 0) /* free old route */ 544 RTFREE(sc->route.ro_rt); 545 if (gre_compute_route(sc) == 0) 546 ifp->if_flags |= IFF_RUNNING; 547 else 548 ifp->if_flags &= ~IFF_RUNNING; 549 } 550 break; 551 case GREGADDRS: 552 memset(&si, 0, sizeof(si)); 553 si.sin_family = AF_INET; 554 si.sin_len = sizeof(struct sockaddr_in); 555 si.sin_addr.s_addr = sc->g_src.s_addr; 556 sa = sintosa(&si); 557 ifr->ifr_addr = *sa; 558 break; 559 case GREGADDRD: 560 memset(&si, 0, sizeof(si)); 561 si.sin_family = AF_INET; 562 si.sin_len = sizeof(struct sockaddr_in); 563 si.sin_addr.s_addr = sc->g_dst.s_addr; 564 sa = sintosa(&si); 565 ifr->ifr_addr = *sa; 566 break; 567 case SIOCSIFPHYADDR: 568 if ((error = suser(curthread)) != 0) 569 break; 570 if (aifr->ifra_addr.sin_family != AF_INET || 571 aifr->ifra_dstaddr.sin_family != AF_INET) { 572 error = EAFNOSUPPORT; 573 break; 574 } 575 if (aifr->ifra_addr.sin_len != sizeof(si) || 576 aifr->ifra_dstaddr.sin_len != sizeof(si)) { 577 error = EINVAL; 578 break; 579 } 580 sc->g_src = aifr->ifra_addr.sin_addr; 581 sc->g_dst = aifr->ifra_dstaddr.sin_addr; 582 goto recompute; 583 case SIOCSLIFPHYADDR: 584 if ((error = suser(curthread)) != 0) 585 break; 586 if (lifr->addr.ss_family != AF_INET || 587 lifr->dstaddr.ss_family != AF_INET) { 588 error = EAFNOSUPPORT; 589 break; 590 } 591 if (lifr->addr.ss_len != sizeof(si) || 592 lifr->dstaddr.ss_len != sizeof(si)) { 593 error = EINVAL; 594 break; 595 } 596 sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr; 597 sc->g_dst = 598 (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr; 599 goto recompute; 600 case SIOCDIFPHYADDR: 601 if ((error = suser(curthread)) != 0) 602 break; 603 sc->g_src.s_addr = INADDR_ANY; 604 sc->g_dst.s_addr = INADDR_ANY; 605 goto recompute; 606 case SIOCGLIFPHYADDR: 607 if (sc->g_src.s_addr == INADDR_ANY || 608 sc->g_dst.s_addr == INADDR_ANY) { 609 error = EADDRNOTAVAIL; 610 break; 611 } 612 memset(&si, 0, sizeof(si)); 613 si.sin_family = AF_INET; 614 si.sin_len = sizeof(struct sockaddr_in); 615 si.sin_addr.s_addr = sc->g_src.s_addr; 616 memcpy(&lifr->addr, &si, sizeof(si)); 617 si.sin_addr.s_addr = sc->g_dst.s_addr; 618 memcpy(&lifr->dstaddr, &si, sizeof(si)); 619 break; 620 case SIOCGIFPSRCADDR: 621#ifdef INET6 622 case SIOCGIFPSRCADDR_IN6: 623#endif 624 if (sc->g_src.s_addr == INADDR_ANY) { 625 error = EADDRNOTAVAIL; 626 break; 627 } 628 memset(&si, 0, sizeof(si)); 629 si.sin_family = AF_INET; 630 si.sin_len = sizeof(struct sockaddr_in); 631 si.sin_addr.s_addr = sc->g_src.s_addr; 632 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 633 break; 634 case SIOCGIFPDSTADDR: 635#ifdef INET6 636 case SIOCGIFPDSTADDR_IN6: 637#endif 638 if (sc->g_dst.s_addr == INADDR_ANY) { 639 error = EADDRNOTAVAIL; 640 break; 641 } 642 memset(&si, 0, sizeof(si)); 643 si.sin_family = AF_INET; 644 si.sin_len = sizeof(struct sockaddr_in); 645 si.sin_addr.s_addr = sc->g_dst.s_addr; 646 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 647 break; 648 default: 649 error = EINVAL; 650 break; 651 } 652 653 splx(s); 654 return (error); 655} 656 657/* 658 * computes a route to our destination that is not the one 659 * which would be taken by ip_output(), as this one will loop back to 660 * us. If the interface is p2p as a--->b, then a routing entry exists 661 * If we now send a packet to b (e.g. ping b), this will come down here 662 * gets src=a, dst=b tacked on and would from ip_output() sent back to 663 * if_gre. 664 * Goal here is to compute a route to b that is less specific than 665 * a-->b. We know that this one exists as in normal operation we have 666 * at least a default route which matches. 667 */ 668static int 669gre_compute_route(struct gre_softc *sc) 670{ 671 struct route *ro; 672 u_int32_t a, b, c; 673 674 ro = &sc->route; 675 676 memset(ro, 0, sizeof(struct route)); 677 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 678 ro->ro_dst.sa_family = AF_INET; 679 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 680 681 /* 682 * toggle last bit, so our interface is not found, but a less 683 * specific route. I'd rather like to specify a shorter mask, 684 * but this is not possible. Should work though. XXX 685 * there is a simpler way ... 686 */ 687 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) { 688 a = ntohl(sc->g_dst.s_addr); 689 b = a & 0x01; 690 c = a & 0xfffffffe; 691 b = b ^ 0x01; 692 a = b | c; 693 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr 694 = htonl(a); 695 } 696 697#ifdef DIAGNOSTIC 698 printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)), 699 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 700#endif 701 702 rtalloc(ro); 703 704 /* 705 * check if this returned a route at all and this route is no 706 * recursion to ourself 707 */ 708 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 709#ifdef DIAGNOSTIC 710 if (ro->ro_rt == NULL) 711 printf(" - no route found!\n"); 712 else 713 printf(" - route loops back to ourself!\n"); 714#endif 715 return EADDRNOTAVAIL; 716 } 717 718 /* 719 * now change it back - else ip_output will just drop 720 * the route and search one to this interface ... 721 */ 722 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) 723 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 724 725#ifdef DIAGNOSTIC 726 printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 727 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 728 printf("\n"); 729#endif 730 731 return 0; 732} 733 734/* 735 * do a checksum of a buffer - much like in_cksum, which operates on 736 * mbufs. 737 */ 738u_int16_t 739gre_in_cksum(u_int16_t *p, u_int len) 740{ 741 u_int32_t sum = 0; 742 int nwords = len >> 1; 743 744 while (nwords-- != 0) 745 sum += *p++; 746 747 if (len & 1) { 748 union { 749 u_short w; 750 u_char c[2]; 751 } u; 752 u.c[0] = *(u_char *)p; 753 u.c[1] = 0; 754 sum += u.w; 755 } 756 757 /* end-around-carry */ 758 sum = (sum >> 16) + (sum & 0xffff); 759 sum += (sum >> 16); 760 return (~sum); 761} 762 763static int 764gremodevent(module_t mod, int type, void *data) 765{ 766 struct gre_softc *sc; 767 768 switch (type) { 769 case MOD_LOAD: 770 greattach(); 771 break; 772 case MOD_UNLOAD: 773 if_clone_detach(&gre_cloner); 774 775 mtx_lock(&gre_mtx); 776 while ((sc = LIST_FIRST(&gre_softc_list)) != NULL) { 777 LIST_REMOVE(sc, sc_list); 778 mtx_unlock(&gre_mtx); 779 gre_destroy(sc); 780 mtx_lock(&gre_mtx); 781 } 782 mtx_unlock(&gre_mtx); 783 mtx_destroy(&gre_mtx); 784 break; 785 default: 786 return EOPNOTSUPP; 787 } 788 return 0; 789} 790 791static moduledata_t gre_mod = { 792 "if_gre", 793 gremodevent, 794 0 795}; 796 797DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 798MODULE_VERSION(if_gre, 1);
| 492 default: 493 error = EAFNOSUPPORT; 494 break; 495 } 496 break; 497 case GRESPROTO: 498 if ((error = suser(curthread)) != 0) 499 break; 500 sc->g_proto = ifr->ifr_flags; 501 switch (sc->g_proto) { 502 case IPPROTO_GRE: 503 ifp->if_flags |= IFF_LINK0; 504 break; 505 case IPPROTO_MOBILE: 506 ifp->if_flags &= ~IFF_LINK0; 507 break; 508 default: 509 error = EPROTONOSUPPORT; 510 break; 511 } 512 goto recompute; 513 case GREGPROTO: 514 ifr->ifr_flags = sc->g_proto; 515 break; 516 case GRESADDRS: 517 case GRESADDRD: 518 if ((error = suser(curthread)) != 0) 519 break; 520 /* 521 * set tunnel endpoints, compute a less specific route 522 * to the remote end and mark if as up 523 */ 524 sa = &ifr->ifr_addr; 525 if (cmd == GRESADDRS) 526 sc->g_src = (satosin(sa))->sin_addr; 527 if (cmd == GRESADDRD) 528 sc->g_dst = (satosin(sa))->sin_addr; 529 recompute: 530#ifdef INET 531 if (sc->encap != NULL) { 532 encap_detach(sc->encap); 533 sc->encap = NULL; 534 } 535#endif 536 if ((sc->g_src.s_addr != INADDR_ANY) && 537 (sc->g_dst.s_addr != INADDR_ANY)) { 538 bzero(&sp, sizeof(sp)); 539 bzero(&sm, sizeof(sm)); 540 bzero(&dp, sizeof(dp)); 541 bzero(&dm, sizeof(dm)); 542 sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 543 sizeof(struct sockaddr_in); 544 sp.sin_family = sm.sin_family = dp.sin_family = 545 dm.sin_family = AF_INET; 546 sp.sin_addr = sc->g_src; 547 dp.sin_addr = sc->g_dst; 548 sm.sin_addr.s_addr = dm.sin_addr.s_addr = 549 INADDR_BROADCAST; 550#ifdef INET 551 sc->encap = encap_attach(AF_INET, sc->g_proto, 552 sintosa(&sp), sintosa(&sm), sintosa(&dp), 553 sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 554 &in_gre_protosw : &in_mobile_protosw, sc); 555 if (sc->encap == NULL) 556 printf("%s: unable to attach encap\n", 557 if_name(GRE2IFP(sc))); 558#endif 559 if (sc->route.ro_rt != 0) /* free old route */ 560 RTFREE(sc->route.ro_rt); 561 if (gre_compute_route(sc) == 0) 562 ifp->if_flags |= IFF_RUNNING; 563 else 564 ifp->if_flags &= ~IFF_RUNNING; 565 } 566 break; 567 case GREGADDRS: 568 memset(&si, 0, sizeof(si)); 569 si.sin_family = AF_INET; 570 si.sin_len = sizeof(struct sockaddr_in); 571 si.sin_addr.s_addr = sc->g_src.s_addr; 572 sa = sintosa(&si); 573 ifr->ifr_addr = *sa; 574 break; 575 case GREGADDRD: 576 memset(&si, 0, sizeof(si)); 577 si.sin_family = AF_INET; 578 si.sin_len = sizeof(struct sockaddr_in); 579 si.sin_addr.s_addr = sc->g_dst.s_addr; 580 sa = sintosa(&si); 581 ifr->ifr_addr = *sa; 582 break; 583 case SIOCSIFPHYADDR: 584 if ((error = suser(curthread)) != 0) 585 break; 586 if (aifr->ifra_addr.sin_family != AF_INET || 587 aifr->ifra_dstaddr.sin_family != AF_INET) { 588 error = EAFNOSUPPORT; 589 break; 590 } 591 if (aifr->ifra_addr.sin_len != sizeof(si) || 592 aifr->ifra_dstaddr.sin_len != sizeof(si)) { 593 error = EINVAL; 594 break; 595 } 596 sc->g_src = aifr->ifra_addr.sin_addr; 597 sc->g_dst = aifr->ifra_dstaddr.sin_addr; 598 goto recompute; 599 case SIOCSLIFPHYADDR: 600 if ((error = suser(curthread)) != 0) 601 break; 602 if (lifr->addr.ss_family != AF_INET || 603 lifr->dstaddr.ss_family != AF_INET) { 604 error = EAFNOSUPPORT; 605 break; 606 } 607 if (lifr->addr.ss_len != sizeof(si) || 608 lifr->dstaddr.ss_len != sizeof(si)) { 609 error = EINVAL; 610 break; 611 } 612 sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr; 613 sc->g_dst = 614 (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr; 615 goto recompute; 616 case SIOCDIFPHYADDR: 617 if ((error = suser(curthread)) != 0) 618 break; 619 sc->g_src.s_addr = INADDR_ANY; 620 sc->g_dst.s_addr = INADDR_ANY; 621 goto recompute; 622 case SIOCGLIFPHYADDR: 623 if (sc->g_src.s_addr == INADDR_ANY || 624 sc->g_dst.s_addr == INADDR_ANY) { 625 error = EADDRNOTAVAIL; 626 break; 627 } 628 memset(&si, 0, sizeof(si)); 629 si.sin_family = AF_INET; 630 si.sin_len = sizeof(struct sockaddr_in); 631 si.sin_addr.s_addr = sc->g_src.s_addr; 632 memcpy(&lifr->addr, &si, sizeof(si)); 633 si.sin_addr.s_addr = sc->g_dst.s_addr; 634 memcpy(&lifr->dstaddr, &si, sizeof(si)); 635 break; 636 case SIOCGIFPSRCADDR: 637#ifdef INET6 638 case SIOCGIFPSRCADDR_IN6: 639#endif 640 if (sc->g_src.s_addr == INADDR_ANY) { 641 error = EADDRNOTAVAIL; 642 break; 643 } 644 memset(&si, 0, sizeof(si)); 645 si.sin_family = AF_INET; 646 si.sin_len = sizeof(struct sockaddr_in); 647 si.sin_addr.s_addr = sc->g_src.s_addr; 648 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 649 break; 650 case SIOCGIFPDSTADDR: 651#ifdef INET6 652 case SIOCGIFPDSTADDR_IN6: 653#endif 654 if (sc->g_dst.s_addr == INADDR_ANY) { 655 error = EADDRNOTAVAIL; 656 break; 657 } 658 memset(&si, 0, sizeof(si)); 659 si.sin_family = AF_INET; 660 si.sin_len = sizeof(struct sockaddr_in); 661 si.sin_addr.s_addr = sc->g_dst.s_addr; 662 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 663 break; 664 default: 665 error = EINVAL; 666 break; 667 } 668 669 splx(s); 670 return (error); 671} 672 673/* 674 * computes a route to our destination that is not the one 675 * which would be taken by ip_output(), as this one will loop back to 676 * us. If the interface is p2p as a--->b, then a routing entry exists 677 * If we now send a packet to b (e.g. ping b), this will come down here 678 * gets src=a, dst=b tacked on and would from ip_output() sent back to 679 * if_gre. 680 * Goal here is to compute a route to b that is less specific than 681 * a-->b. We know that this one exists as in normal operation we have 682 * at least a default route which matches. 683 */ 684static int 685gre_compute_route(struct gre_softc *sc) 686{ 687 struct route *ro; 688 u_int32_t a, b, c; 689 690 ro = &sc->route; 691 692 memset(ro, 0, sizeof(struct route)); 693 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 694 ro->ro_dst.sa_family = AF_INET; 695 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 696 697 /* 698 * toggle last bit, so our interface is not found, but a less 699 * specific route. I'd rather like to specify a shorter mask, 700 * but this is not possible. Should work though. XXX 701 * there is a simpler way ... 702 */ 703 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) { 704 a = ntohl(sc->g_dst.s_addr); 705 b = a & 0x01; 706 c = a & 0xfffffffe; 707 b = b ^ 0x01; 708 a = b | c; 709 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr 710 = htonl(a); 711 } 712 713#ifdef DIAGNOSTIC 714 printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)), 715 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 716#endif 717 718 rtalloc(ro); 719 720 /* 721 * check if this returned a route at all and this route is no 722 * recursion to ourself 723 */ 724 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 725#ifdef DIAGNOSTIC 726 if (ro->ro_rt == NULL) 727 printf(" - no route found!\n"); 728 else 729 printf(" - route loops back to ourself!\n"); 730#endif 731 return EADDRNOTAVAIL; 732 } 733 734 /* 735 * now change it back - else ip_output will just drop 736 * the route and search one to this interface ... 737 */ 738 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) 739 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 740 741#ifdef DIAGNOSTIC 742 printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 743 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 744 printf("\n"); 745#endif 746 747 return 0; 748} 749 750/* 751 * do a checksum of a buffer - much like in_cksum, which operates on 752 * mbufs. 753 */ 754u_int16_t 755gre_in_cksum(u_int16_t *p, u_int len) 756{ 757 u_int32_t sum = 0; 758 int nwords = len >> 1; 759 760 while (nwords-- != 0) 761 sum += *p++; 762 763 if (len & 1) { 764 union { 765 u_short w; 766 u_char c[2]; 767 } u; 768 u.c[0] = *(u_char *)p; 769 u.c[1] = 0; 770 sum += u.w; 771 } 772 773 /* end-around-carry */ 774 sum = (sum >> 16) + (sum & 0xffff); 775 sum += (sum >> 16); 776 return (~sum); 777} 778 779static int 780gremodevent(module_t mod, int type, void *data) 781{ 782 struct gre_softc *sc; 783 784 switch (type) { 785 case MOD_LOAD: 786 greattach(); 787 break; 788 case MOD_UNLOAD: 789 if_clone_detach(&gre_cloner); 790 791 mtx_lock(&gre_mtx); 792 while ((sc = LIST_FIRST(&gre_softc_list)) != NULL) { 793 LIST_REMOVE(sc, sc_list); 794 mtx_unlock(&gre_mtx); 795 gre_destroy(sc); 796 mtx_lock(&gre_mtx); 797 } 798 mtx_unlock(&gre_mtx); 799 mtx_destroy(&gre_mtx); 800 break; 801 default: 802 return EOPNOTSUPP; 803 } 804 return 0; 805} 806 807static moduledata_t gre_mod = { 808 "if_gre", 809 gremodevent, 810 0 811}; 812 813DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 814MODULE_VERSION(if_gre, 1);
|