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 179894 2008-06-20 17:26:34Z thompsa $ */
| 2/* $FreeBSD: head/sys/net/if_gre.c 180041 2008-06-26 22:59:49Z julian $ */
|
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 * 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 44 * See RFC 2784 (successor of RFC 1701 and 1702) for more details. 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/priv.h> 61#include <sys/proc.h> 62#include <sys/protosw.h> 63#include <sys/socket.h> 64#include <sys/sockio.h> 65#include <sys/sysctl.h> 66#include <sys/systm.h> 67 68#include <net/ethernet.h> 69#include <net/if.h> 70#include <net/if_clone.h> 71#include <net/if_types.h> 72#include <net/route.h> 73 74#ifdef INET 75#include <netinet/in.h> 76#include <netinet/in_systm.h> 77#include <netinet/in_var.h> 78#include <netinet/ip.h> 79#include <netinet/ip_gre.h> 80#include <netinet/ip_var.h> 81#include <netinet/ip_encap.h> 82#else 83#error "Huh? if_gre without inet?" 84#endif 85 86#include <net/bpf.h> 87 88#include <net/if_gre.h> 89 90/* 91 * It is not easy to calculate the right value for a GRE MTU. 92 * We leave this task to the admin and use the same default that 93 * other vendors use. 94 */ 95#define GREMTU 1476 96 97#define GRENAME "gre" 98 99/* 100 * gre_mtx protects all global variables in if_gre.c. 101 * XXX: gre_softc data not protected yet. 102 */ 103struct mtx gre_mtx; 104static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation"); 105 106struct gre_softc_head gre_softc_list; 107 108static int gre_clone_create(struct if_clone *, int, caddr_t); 109static void gre_clone_destroy(struct ifnet *); 110static int gre_ioctl(struct ifnet *, u_long, caddr_t); 111static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *, 112 struct rtentry *rt); 113 114IFC_SIMPLE_DECLARE(gre, 0); 115 116static int gre_compute_route(struct gre_softc *sc); 117 118static void greattach(void); 119 120#ifdef INET 121extern struct domain inetdomain; 122static const struct protosw in_gre_protosw = { 123 .pr_type = SOCK_RAW, 124 .pr_domain = &inetdomain, 125 .pr_protocol = IPPROTO_GRE, 126 .pr_flags = PR_ATOMIC|PR_ADDR, 127 .pr_input = gre_input, 128 .pr_output = (pr_output_t *)rip_output, 129 .pr_ctlinput = rip_ctlinput, 130 .pr_ctloutput = rip_ctloutput, 131 .pr_usrreqs = &rip_usrreqs 132}; 133static const struct protosw in_mobile_protosw = { 134 .pr_type = SOCK_RAW, 135 .pr_domain = &inetdomain, 136 .pr_protocol = IPPROTO_MOBILE, 137 .pr_flags = PR_ATOMIC|PR_ADDR, 138 .pr_input = gre_mobile_input, 139 .pr_output = (pr_output_t *)rip_output, 140 .pr_ctlinput = rip_ctlinput, 141 .pr_ctloutput = rip_ctloutput, 142 .pr_usrreqs = &rip_usrreqs 143}; 144#endif 145 146SYSCTL_DECL(_net_link); 147SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0, 148 "Generic Routing Encapsulation"); 149#ifndef MAX_GRE_NEST 150/* 151 * This macro controls the default upper limitation on nesting of gre tunnels. 152 * Since, setting a large value to this macro with a careless configuration 153 * may introduce system crash, we don't allow any nestings by default. 154 * If you need to configure nested gre tunnels, you can define this macro 155 * in your kernel configuration file. However, if you do so, please be 156 * careful to configure the tunnels so that it won't make a loop. 157 */ 158#define MAX_GRE_NEST 1 159#endif 160static int max_gre_nesting = MAX_GRE_NEST; 161SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW, 162 &max_gre_nesting, 0, "Max nested tunnels"); 163 164/* ARGSUSED */ 165static void 166greattach(void) 167{ 168 169 mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF); 170 LIST_INIT(&gre_softc_list); 171 if_clone_attach(&gre_cloner); 172} 173 174static int 175gre_clone_create(ifc, unit, params) 176 struct if_clone *ifc; 177 int unit; 178 caddr_t params; 179{ 180 struct gre_softc *sc; 181 182 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO); 183 184 GRE2IFP(sc) = if_alloc(IFT_TUNNEL); 185 if (GRE2IFP(sc) == NULL) { 186 free(sc, M_GRE); 187 return (ENOSPC); 188 } 189 190 GRE2IFP(sc)->if_softc = sc; 191 if_initname(GRE2IFP(sc), ifc->ifc_name, unit); 192 193 GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 194 GRE2IFP(sc)->if_addrlen = 0; 195 GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */ 196 GRE2IFP(sc)->if_mtu = GREMTU; 197 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 198 GRE2IFP(sc)->if_output = gre_output; 199 GRE2IFP(sc)->if_ioctl = gre_ioctl; 200 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 201 sc->g_proto = IPPROTO_GRE; 202 GRE2IFP(sc)->if_flags |= IFF_LINK0; 203 sc->encap = NULL; 204 sc->called = 0; 205 sc->gre_fibnum = curthread->td_proc->p_fibnum; 206 sc->wccp_ver = WCCP_V1; 207 sc->key = 0; 208 if_attach(GRE2IFP(sc)); 209 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 210 mtx_lock(&gre_mtx); 211 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 212 mtx_unlock(&gre_mtx); 213 return (0); 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 226#ifdef INET 227 if (sc->encap != NULL) 228 encap_detach(sc->encap); 229#endif 230 bpfdetach(ifp); 231 if_detach(ifp); 232 if_free(ifp); 233 free(sc, M_GRE); 234} 235 236/* 237 * The output routine. Takes a packet and encapsulates it in the protocol 238 * given by sc->g_proto. See also RFC 1701 and RFC 2004 239 */ 240static int 241gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 242 struct rtentry *rt) 243{ 244 int error = 0; 245 struct gre_softc *sc = ifp->if_softc; 246 struct greip *gh; 247 struct ip *ip;
| 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 * 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 44 * See RFC 2784 (successor of RFC 1701 and 1702) for more details. 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/priv.h> 61#include <sys/proc.h> 62#include <sys/protosw.h> 63#include <sys/socket.h> 64#include <sys/sockio.h> 65#include <sys/sysctl.h> 66#include <sys/systm.h> 67 68#include <net/ethernet.h> 69#include <net/if.h> 70#include <net/if_clone.h> 71#include <net/if_types.h> 72#include <net/route.h> 73 74#ifdef INET 75#include <netinet/in.h> 76#include <netinet/in_systm.h> 77#include <netinet/in_var.h> 78#include <netinet/ip.h> 79#include <netinet/ip_gre.h> 80#include <netinet/ip_var.h> 81#include <netinet/ip_encap.h> 82#else 83#error "Huh? if_gre without inet?" 84#endif 85 86#include <net/bpf.h> 87 88#include <net/if_gre.h> 89 90/* 91 * It is not easy to calculate the right value for a GRE MTU. 92 * We leave this task to the admin and use the same default that 93 * other vendors use. 94 */ 95#define GREMTU 1476 96 97#define GRENAME "gre" 98 99/* 100 * gre_mtx protects all global variables in if_gre.c. 101 * XXX: gre_softc data not protected yet. 102 */ 103struct mtx gre_mtx; 104static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation"); 105 106struct gre_softc_head gre_softc_list; 107 108static int gre_clone_create(struct if_clone *, int, caddr_t); 109static void gre_clone_destroy(struct ifnet *); 110static int gre_ioctl(struct ifnet *, u_long, caddr_t); 111static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *, 112 struct rtentry *rt); 113 114IFC_SIMPLE_DECLARE(gre, 0); 115 116static int gre_compute_route(struct gre_softc *sc); 117 118static void greattach(void); 119 120#ifdef INET 121extern struct domain inetdomain; 122static const struct protosw in_gre_protosw = { 123 .pr_type = SOCK_RAW, 124 .pr_domain = &inetdomain, 125 .pr_protocol = IPPROTO_GRE, 126 .pr_flags = PR_ATOMIC|PR_ADDR, 127 .pr_input = gre_input, 128 .pr_output = (pr_output_t *)rip_output, 129 .pr_ctlinput = rip_ctlinput, 130 .pr_ctloutput = rip_ctloutput, 131 .pr_usrreqs = &rip_usrreqs 132}; 133static const struct protosw in_mobile_protosw = { 134 .pr_type = SOCK_RAW, 135 .pr_domain = &inetdomain, 136 .pr_protocol = IPPROTO_MOBILE, 137 .pr_flags = PR_ATOMIC|PR_ADDR, 138 .pr_input = gre_mobile_input, 139 .pr_output = (pr_output_t *)rip_output, 140 .pr_ctlinput = rip_ctlinput, 141 .pr_ctloutput = rip_ctloutput, 142 .pr_usrreqs = &rip_usrreqs 143}; 144#endif 145 146SYSCTL_DECL(_net_link); 147SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0, 148 "Generic Routing Encapsulation"); 149#ifndef MAX_GRE_NEST 150/* 151 * This macro controls the default upper limitation on nesting of gre tunnels. 152 * Since, setting a large value to this macro with a careless configuration 153 * may introduce system crash, we don't allow any nestings by default. 154 * If you need to configure nested gre tunnels, you can define this macro 155 * in your kernel configuration file. However, if you do so, please be 156 * careful to configure the tunnels so that it won't make a loop. 157 */ 158#define MAX_GRE_NEST 1 159#endif 160static int max_gre_nesting = MAX_GRE_NEST; 161SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW, 162 &max_gre_nesting, 0, "Max nested tunnels"); 163 164/* ARGSUSED */ 165static void 166greattach(void) 167{ 168 169 mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF); 170 LIST_INIT(&gre_softc_list); 171 if_clone_attach(&gre_cloner); 172} 173 174static int 175gre_clone_create(ifc, unit, params) 176 struct if_clone *ifc; 177 int unit; 178 caddr_t params; 179{ 180 struct gre_softc *sc; 181 182 sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO); 183 184 GRE2IFP(sc) = if_alloc(IFT_TUNNEL); 185 if (GRE2IFP(sc) == NULL) { 186 free(sc, M_GRE); 187 return (ENOSPC); 188 } 189 190 GRE2IFP(sc)->if_softc = sc; 191 if_initname(GRE2IFP(sc), ifc->ifc_name, unit); 192 193 GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 194 GRE2IFP(sc)->if_addrlen = 0; 195 GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */ 196 GRE2IFP(sc)->if_mtu = GREMTU; 197 GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 198 GRE2IFP(sc)->if_output = gre_output; 199 GRE2IFP(sc)->if_ioctl = gre_ioctl; 200 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 201 sc->g_proto = IPPROTO_GRE; 202 GRE2IFP(sc)->if_flags |= IFF_LINK0; 203 sc->encap = NULL; 204 sc->called = 0; 205 sc->gre_fibnum = curthread->td_proc->p_fibnum; 206 sc->wccp_ver = WCCP_V1; 207 sc->key = 0; 208 if_attach(GRE2IFP(sc)); 209 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 210 mtx_lock(&gre_mtx); 211 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 212 mtx_unlock(&gre_mtx); 213 return (0); 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 226#ifdef INET 227 if (sc->encap != NULL) 228 encap_detach(sc->encap); 229#endif 230 bpfdetach(ifp); 231 if_detach(ifp); 232 if_free(ifp); 233 free(sc, M_GRE); 234} 235 236/* 237 * The output routine. Takes a packet and encapsulates it in the protocol 238 * given by sc->g_proto. See also RFC 1701 and RFC 2004 239 */ 240static int 241gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 242 struct rtentry *rt) 243{ 244 int error = 0; 245 struct gre_softc *sc = ifp->if_softc; 246 struct greip *gh; 247 struct ip *ip;
|
248 u_short ip_id = 0; 249 uint8_t ip_tos = 0;
| 248 u_short gre_ip_id = 0; 249 uint8_t gre_ip_tos = 0;
|
250 u_int16_t etype = 0; 251 struct mobile_h mob_h; 252 u_int32_t af; 253 254 /* 255 * gre may cause infinite recursion calls when misconfigured. 256 * We'll prevent this by introducing upper limit. 257 */ 258 if (++(sc->called) > max_gre_nesting) { 259 printf("%s: gre_output: recursively called too many " 260 "times(%d)\n", if_name(GRE2IFP(sc)), sc->called); 261 m_freem(m); 262 error = EIO; /* is there better errno? */ 263 goto end; 264 } 265 266 if (!((ifp->if_flags & IFF_UP) && 267 (ifp->if_drv_flags & IFF_DRV_RUNNING)) || 268 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 269 m_freem(m); 270 error = ENETDOWN; 271 goto end; 272 } 273 274 gh = NULL; 275 ip = NULL; 276 277 /* BPF writes need to be handled specially. */ 278 if (dst->sa_family == AF_UNSPEC) { 279 bcopy(dst->sa_data, &af, sizeof(af)); 280 dst->sa_family = af; 281 } 282 283 if (bpf_peers_present(ifp->if_bpf)) { 284 af = dst->sa_family; 285 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 286 } 287 288 m->m_flags &= ~(M_BCAST|M_MCAST); 289 290 if (sc->g_proto == IPPROTO_MOBILE) { 291 if (dst->sa_family == AF_INET) { 292 struct mbuf *m0; 293 int msiz; 294 295 ip = mtod(m, struct ip *); 296 297 /* 298 * RFC2004 specifies that fragmented diagrams shouldn't 299 * be encapsulated. 300 */ 301 if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 302 _IF_DROP(&ifp->if_snd); 303 m_freem(m); 304 error = EINVAL; /* is there better errno? */ 305 goto end; 306 } 307 memset(&mob_h, 0, MOB_H_SIZ_L); 308 mob_h.proto = (ip->ip_p) << 8; 309 mob_h.odst = ip->ip_dst.s_addr; 310 ip->ip_dst.s_addr = sc->g_dst.s_addr; 311 312 /* 313 * If the packet comes from our host, we only change 314 * the destination address in the IP header. 315 * Else we also need to save and change the source 316 */ 317 if (in_hosteq(ip->ip_src, sc->g_src)) { 318 msiz = MOB_H_SIZ_S; 319 } else { 320 mob_h.proto |= MOB_H_SBIT; 321 mob_h.osrc = ip->ip_src.s_addr; 322 ip->ip_src.s_addr = sc->g_src.s_addr; 323 msiz = MOB_H_SIZ_L; 324 } 325 mob_h.proto = htons(mob_h.proto); 326 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz); 327 328 if ((m->m_data - msiz) < m->m_pktdat) { 329 /* need new mbuf */ 330 MGETHDR(m0, M_DONTWAIT, MT_DATA); 331 if (m0 == NULL) { 332 _IF_DROP(&ifp->if_snd); 333 m_freem(m); 334 error = ENOBUFS; 335 goto end; 336 } 337 m0->m_next = m; 338 m->m_data += sizeof(struct ip); 339 m->m_len -= sizeof(struct ip); 340 m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 341 m0->m_len = msiz + sizeof(struct ip); 342 m0->m_data += max_linkhdr; 343 memcpy(mtod(m0, caddr_t), (caddr_t)ip, 344 sizeof(struct ip)); 345 m = m0; 346 } else { /* we have some space left in the old one */ 347 m->m_data -= msiz; 348 m->m_len += msiz; 349 m->m_pkthdr.len += msiz; 350 bcopy(ip, mtod(m, caddr_t), 351 sizeof(struct ip)); 352 } 353 ip = mtod(m, struct ip *); 354 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 355 ip->ip_len = ntohs(ip->ip_len) + msiz; 356 } else { /* AF_INET */ 357 _IF_DROP(&ifp->if_snd); 358 m_freem(m); 359 error = EINVAL; 360 goto end; 361 } 362 } else if (sc->g_proto == IPPROTO_GRE) { 363 switch (dst->sa_family) { 364 case AF_INET: 365 ip = mtod(m, struct ip *);
| 250 u_int16_t etype = 0; 251 struct mobile_h mob_h; 252 u_int32_t af; 253 254 /* 255 * gre may cause infinite recursion calls when misconfigured. 256 * We'll prevent this by introducing upper limit. 257 */ 258 if (++(sc->called) > max_gre_nesting) { 259 printf("%s: gre_output: recursively called too many " 260 "times(%d)\n", if_name(GRE2IFP(sc)), sc->called); 261 m_freem(m); 262 error = EIO; /* is there better errno? */ 263 goto end; 264 } 265 266 if (!((ifp->if_flags & IFF_UP) && 267 (ifp->if_drv_flags & IFF_DRV_RUNNING)) || 268 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 269 m_freem(m); 270 error = ENETDOWN; 271 goto end; 272 } 273 274 gh = NULL; 275 ip = NULL; 276 277 /* BPF writes need to be handled specially. */ 278 if (dst->sa_family == AF_UNSPEC) { 279 bcopy(dst->sa_data, &af, sizeof(af)); 280 dst->sa_family = af; 281 } 282 283 if (bpf_peers_present(ifp->if_bpf)) { 284 af = dst->sa_family; 285 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 286 } 287 288 m->m_flags &= ~(M_BCAST|M_MCAST); 289 290 if (sc->g_proto == IPPROTO_MOBILE) { 291 if (dst->sa_family == AF_INET) { 292 struct mbuf *m0; 293 int msiz; 294 295 ip = mtod(m, struct ip *); 296 297 /* 298 * RFC2004 specifies that fragmented diagrams shouldn't 299 * be encapsulated. 300 */ 301 if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 302 _IF_DROP(&ifp->if_snd); 303 m_freem(m); 304 error = EINVAL; /* is there better errno? */ 305 goto end; 306 } 307 memset(&mob_h, 0, MOB_H_SIZ_L); 308 mob_h.proto = (ip->ip_p) << 8; 309 mob_h.odst = ip->ip_dst.s_addr; 310 ip->ip_dst.s_addr = sc->g_dst.s_addr; 311 312 /* 313 * If the packet comes from our host, we only change 314 * the destination address in the IP header. 315 * Else we also need to save and change the source 316 */ 317 if (in_hosteq(ip->ip_src, sc->g_src)) { 318 msiz = MOB_H_SIZ_S; 319 } else { 320 mob_h.proto |= MOB_H_SBIT; 321 mob_h.osrc = ip->ip_src.s_addr; 322 ip->ip_src.s_addr = sc->g_src.s_addr; 323 msiz = MOB_H_SIZ_L; 324 } 325 mob_h.proto = htons(mob_h.proto); 326 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz); 327 328 if ((m->m_data - msiz) < m->m_pktdat) { 329 /* need new mbuf */ 330 MGETHDR(m0, M_DONTWAIT, MT_DATA); 331 if (m0 == NULL) { 332 _IF_DROP(&ifp->if_snd); 333 m_freem(m); 334 error = ENOBUFS; 335 goto end; 336 } 337 m0->m_next = m; 338 m->m_data += sizeof(struct ip); 339 m->m_len -= sizeof(struct ip); 340 m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 341 m0->m_len = msiz + sizeof(struct ip); 342 m0->m_data += max_linkhdr; 343 memcpy(mtod(m0, caddr_t), (caddr_t)ip, 344 sizeof(struct ip)); 345 m = m0; 346 } else { /* we have some space left in the old one */ 347 m->m_data -= msiz; 348 m->m_len += msiz; 349 m->m_pkthdr.len += msiz; 350 bcopy(ip, mtod(m, caddr_t), 351 sizeof(struct ip)); 352 } 353 ip = mtod(m, struct ip *); 354 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 355 ip->ip_len = ntohs(ip->ip_len) + msiz; 356 } else { /* AF_INET */ 357 _IF_DROP(&ifp->if_snd); 358 m_freem(m); 359 error = EINVAL; 360 goto end; 361 } 362 } else if (sc->g_proto == IPPROTO_GRE) { 363 switch (dst->sa_family) { 364 case AF_INET: 365 ip = mtod(m, struct ip *);
|
366 ip_tos = ip->ip_tos; 367 ip_id = ip->ip_id;
| 366 gre_ip_tos = ip->ip_tos; 367 gre_ip_id = ip->ip_id;
|
368 etype = ETHERTYPE_IP; 369 break; 370#ifdef INET6 371 case AF_INET6:
| 368 etype = ETHERTYPE_IP; 369 break; 370#ifdef INET6 371 case AF_INET6:
|
372 ip_id = ip_newid();
| 372 gre_ip_id = ip_newid();
|
373 etype = ETHERTYPE_IPV6; 374 break; 375#endif 376#ifdef NETATALK 377 case AF_APPLETALK: 378 etype = ETHERTYPE_ATALK; 379 break; 380#endif 381 default: 382 _IF_DROP(&ifp->if_snd); 383 m_freem(m); 384 error = EAFNOSUPPORT; 385 goto end; 386 } 387 388 /* Reserve space for GRE header + optional GRE key */ 389 int hdrlen = sizeof(struct greip); 390 if (sc->key) 391 hdrlen += sizeof(uint32_t); 392 M_PREPEND(m, hdrlen, M_DONTWAIT); 393 } else { 394 _IF_DROP(&ifp->if_snd); 395 m_freem(m); 396 error = EINVAL; 397 goto end; 398 } 399 400 if (m == NULL) { /* mbuf allocation failed */ 401 _IF_DROP(&ifp->if_snd); 402 error = ENOBUFS; 403 goto end; 404 } 405 406 M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */ 407 408 gh = mtod(m, struct greip *); 409 if (sc->g_proto == IPPROTO_GRE) { 410 uint32_t *options = gh->gi_options; 411 412 memset((void *)gh, 0, sizeof(struct greip)); 413 gh->gi_ptype = htons(etype); 414 gh->gi_flags = 0; 415 416 /* Add key option */ 417 if (sc->key) 418 { 419 gh->gi_flags |= htons(GRE_KP); 420 *(options++) = htonl(sc->key); 421 } 422 } 423 424 gh->gi_pr = sc->g_proto; 425 if (sc->g_proto != IPPROTO_MOBILE) { 426 gh->gi_src = sc->g_src; 427 gh->gi_dst = sc->g_dst; 428 ((struct ip*)gh)->ip_v = IPPROTO_IPV4; 429 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 430 ((struct ip*)gh)->ip_ttl = GRE_TTL;
| 373 etype = ETHERTYPE_IPV6; 374 break; 375#endif 376#ifdef NETATALK 377 case AF_APPLETALK: 378 etype = ETHERTYPE_ATALK; 379 break; 380#endif 381 default: 382 _IF_DROP(&ifp->if_snd); 383 m_freem(m); 384 error = EAFNOSUPPORT; 385 goto end; 386 } 387 388 /* Reserve space for GRE header + optional GRE key */ 389 int hdrlen = sizeof(struct greip); 390 if (sc->key) 391 hdrlen += sizeof(uint32_t); 392 M_PREPEND(m, hdrlen, M_DONTWAIT); 393 } else { 394 _IF_DROP(&ifp->if_snd); 395 m_freem(m); 396 error = EINVAL; 397 goto end; 398 } 399 400 if (m == NULL) { /* mbuf allocation failed */ 401 _IF_DROP(&ifp->if_snd); 402 error = ENOBUFS; 403 goto end; 404 } 405 406 M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */ 407 408 gh = mtod(m, struct greip *); 409 if (sc->g_proto == IPPROTO_GRE) { 410 uint32_t *options = gh->gi_options; 411 412 memset((void *)gh, 0, sizeof(struct greip)); 413 gh->gi_ptype = htons(etype); 414 gh->gi_flags = 0; 415 416 /* Add key option */ 417 if (sc->key) 418 { 419 gh->gi_flags |= htons(GRE_KP); 420 *(options++) = htonl(sc->key); 421 } 422 } 423 424 gh->gi_pr = sc->g_proto; 425 if (sc->g_proto != IPPROTO_MOBILE) { 426 gh->gi_src = sc->g_src; 427 gh->gi_dst = sc->g_dst; 428 ((struct ip*)gh)->ip_v = IPPROTO_IPV4; 429 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 430 ((struct ip*)gh)->ip_ttl = GRE_TTL;
|
431 ((struct ip*)gh)->ip_tos = ip_tos; 432 ((struct ip*)gh)->ip_id = ip_id;
| 431 ((struct ip*)gh)->ip_tos = gre_ip_tos; 432 ((struct ip*)gh)->ip_id = gre_ip_id;
|
433 gh->gi_len = m->m_pkthdr.len; 434 } 435 436 ifp->if_opackets++; 437 ifp->if_obytes += m->m_pkthdr.len; 438 /* 439 * Send it off and with IP_FORWARD flag to prevent it from 440 * overwriting the ip_id again. ip_id is already set to the 441 * ip_id of the encapsulated packet. 442 */ 443 error = ip_output(m, NULL, &sc->route, IP_FORWARDING, 444 (struct ip_moptions *)NULL, (struct inpcb *)NULL); 445 end: 446 sc->called = 0; 447 if (error) 448 ifp->if_oerrors++; 449 return (error); 450} 451 452static int 453gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 454{ 455 struct ifreq *ifr = (struct ifreq *)data; 456 struct if_laddrreq *lifr = (struct if_laddrreq *)data; 457 struct in_aliasreq *aifr = (struct in_aliasreq *)data; 458 struct gre_softc *sc = ifp->if_softc; 459 int s; 460 struct sockaddr_in si; 461 struct sockaddr *sa = NULL; 462 int error, adj; 463 struct sockaddr_in sp, sm, dp, dm; 464 uint32_t key; 465 466 error = 0; 467 adj = 0; 468 469 s = splnet(); 470 switch (cmd) { 471 case SIOCSIFADDR: 472 ifp->if_flags |= IFF_UP; 473 break; 474 case SIOCSIFDSTADDR: 475 break; 476 case SIOCSIFFLAGS: 477 /* 478 * XXXRW: Isn't this priv_check() redundant to the ifnet 479 * layer check? 480 */ 481 if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0) 482 break; 483 if ((ifr->ifr_flags & IFF_LINK0) != 0) 484 sc->g_proto = IPPROTO_GRE; 485 else 486 sc->g_proto = IPPROTO_MOBILE; 487 if ((ifr->ifr_flags & IFF_LINK2) != 0) 488 sc->wccp_ver = WCCP_V2; 489 else 490 sc->wccp_ver = WCCP_V1; 491 goto recompute; 492 case SIOCSIFMTU: 493 /* 494 * XXXRW: Isn't this priv_check() redundant to the ifnet 495 * layer check? 496 */ 497 if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0) 498 break; 499 if (ifr->ifr_mtu < 576) { 500 error = EINVAL; 501 break; 502 } 503 ifp->if_mtu = ifr->ifr_mtu; 504 break; 505 case SIOCGIFMTU: 506 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu; 507 break; 508 case SIOCADDMULTI: 509 /* 510 * XXXRW: Isn't this priv_checkr() redundant to the ifnet 511 * layer check? 512 */ 513 if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0) 514 break; 515 if (ifr == 0) { 516 error = EAFNOSUPPORT; 517 break; 518 } 519 switch (ifr->ifr_addr.sa_family) { 520#ifdef INET 521 case AF_INET: 522 break; 523#endif 524#ifdef INET6 525 case AF_INET6: 526 break; 527#endif 528 default: 529 error = EAFNOSUPPORT; 530 break; 531 } 532 break; 533 case SIOCDELMULTI: 534 /* 535 * XXXRW: Isn't this priv_check() redundant to the ifnet 536 * layer check? 537 */ 538 if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0) 539 break; 540 if (ifr == 0) { 541 error = EAFNOSUPPORT; 542 break; 543 } 544 switch (ifr->ifr_addr.sa_family) { 545#ifdef INET 546 case AF_INET: 547 break; 548#endif 549#ifdef INET6 550 case AF_INET6: 551 break; 552#endif 553 default: 554 error = EAFNOSUPPORT; 555 break; 556 } 557 break; 558 case GRESPROTO: 559 /* 560 * XXXRW: Isn't this priv_check() redundant to the ifnet 561 * layer check? 562 */ 563 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0) 564 break; 565 sc->g_proto = ifr->ifr_flags; 566 switch (sc->g_proto) { 567 case IPPROTO_GRE: 568 ifp->if_flags |= IFF_LINK0; 569 break; 570 case IPPROTO_MOBILE: 571 ifp->if_flags &= ~IFF_LINK0; 572 break; 573 default: 574 error = EPROTONOSUPPORT; 575 break; 576 } 577 goto recompute; 578 case GREGPROTO: 579 ifr->ifr_flags = sc->g_proto; 580 break; 581 case GRESADDRS: 582 case GRESADDRD: 583 error = priv_check(curthread, PRIV_NET_GRE); 584 if (error) 585 return (error); 586 /* 587 * set tunnel endpoints, compute a less specific route 588 * to the remote end and mark if as up 589 */ 590 sa = &ifr->ifr_addr; 591 if (cmd == GRESADDRS) 592 sc->g_src = (satosin(sa))->sin_addr; 593 if (cmd == GRESADDRD) 594 sc->g_dst = (satosin(sa))->sin_addr; 595 recompute: 596#ifdef INET 597 if (sc->encap != NULL) { 598 encap_detach(sc->encap); 599 sc->encap = NULL; 600 } 601#endif 602 if ((sc->g_src.s_addr != INADDR_ANY) && 603 (sc->g_dst.s_addr != INADDR_ANY)) { 604 bzero(&sp, sizeof(sp)); 605 bzero(&sm, sizeof(sm)); 606 bzero(&dp, sizeof(dp)); 607 bzero(&dm, sizeof(dm)); 608 sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 609 sizeof(struct sockaddr_in); 610 sp.sin_family = sm.sin_family = dp.sin_family = 611 dm.sin_family = AF_INET; 612 sp.sin_addr = sc->g_src; 613 dp.sin_addr = sc->g_dst; 614 sm.sin_addr.s_addr = dm.sin_addr.s_addr = 615 INADDR_BROADCAST; 616#ifdef INET 617 sc->encap = encap_attach(AF_INET, sc->g_proto, 618 sintosa(&sp), sintosa(&sm), sintosa(&dp), 619 sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 620 &in_gre_protosw : &in_mobile_protosw, sc); 621 if (sc->encap == NULL) 622 printf("%s: unable to attach encap\n", 623 if_name(GRE2IFP(sc))); 624#endif 625 if (sc->route.ro_rt != 0) /* free old route */ 626 RTFREE(sc->route.ro_rt); 627 if (gre_compute_route(sc) == 0) 628 ifp->if_drv_flags |= IFF_DRV_RUNNING; 629 else 630 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 631 } 632 break; 633 case GREGADDRS: 634 memset(&si, 0, sizeof(si)); 635 si.sin_family = AF_INET; 636 si.sin_len = sizeof(struct sockaddr_in); 637 si.sin_addr.s_addr = sc->g_src.s_addr; 638 sa = sintosa(&si); 639 ifr->ifr_addr = *sa; 640 break; 641 case GREGADDRD: 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 sa = sintosa(&si); 647 ifr->ifr_addr = *sa; 648 break; 649 case SIOCSIFPHYADDR: 650 /* 651 * XXXRW: Isn't this priv_check() redundant to the ifnet 652 * layer check? 653 */ 654 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 655 break; 656 if (aifr->ifra_addr.sin_family != AF_INET || 657 aifr->ifra_dstaddr.sin_family != AF_INET) { 658 error = EAFNOSUPPORT; 659 break; 660 } 661 if (aifr->ifra_addr.sin_len != sizeof(si) || 662 aifr->ifra_dstaddr.sin_len != sizeof(si)) { 663 error = EINVAL; 664 break; 665 } 666 sc->g_src = aifr->ifra_addr.sin_addr; 667 sc->g_dst = aifr->ifra_dstaddr.sin_addr; 668 goto recompute; 669 case SIOCSLIFPHYADDR: 670 /* 671 * XXXRW: Isn't this priv_check() redundant to the ifnet 672 * layer check? 673 */ 674 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 675 break; 676 if (lifr->addr.ss_family != AF_INET || 677 lifr->dstaddr.ss_family != AF_INET) { 678 error = EAFNOSUPPORT; 679 break; 680 } 681 if (lifr->addr.ss_len != sizeof(si) || 682 lifr->dstaddr.ss_len != sizeof(si)) { 683 error = EINVAL; 684 break; 685 } 686 sc->g_src = (satosin(&lifr->addr))->sin_addr; 687 sc->g_dst = 688 (satosin(&lifr->dstaddr))->sin_addr; 689 goto recompute; 690 case SIOCDIFPHYADDR: 691 /* 692 * XXXRW: Isn't this priv_check() redundant to the ifnet 693 * layer check? 694 */ 695 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 696 break; 697 sc->g_src.s_addr = INADDR_ANY; 698 sc->g_dst.s_addr = INADDR_ANY; 699 goto recompute; 700 case SIOCGLIFPHYADDR: 701 if (sc->g_src.s_addr == INADDR_ANY || 702 sc->g_dst.s_addr == INADDR_ANY) { 703 error = EADDRNOTAVAIL; 704 break; 705 } 706 memset(&si, 0, sizeof(si)); 707 si.sin_family = AF_INET; 708 si.sin_len = sizeof(struct sockaddr_in); 709 si.sin_addr.s_addr = sc->g_src.s_addr; 710 memcpy(&lifr->addr, &si, sizeof(si)); 711 si.sin_addr.s_addr = sc->g_dst.s_addr; 712 memcpy(&lifr->dstaddr, &si, sizeof(si)); 713 break; 714 case SIOCGIFPSRCADDR: 715#ifdef INET6 716 case SIOCGIFPSRCADDR_IN6: 717#endif 718 if (sc->g_src.s_addr == INADDR_ANY) { 719 error = EADDRNOTAVAIL; 720 break; 721 } 722 memset(&si, 0, sizeof(si)); 723 si.sin_family = AF_INET; 724 si.sin_len = sizeof(struct sockaddr_in); 725 si.sin_addr.s_addr = sc->g_src.s_addr; 726 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 727 break; 728 case SIOCGIFPDSTADDR: 729#ifdef INET6 730 case SIOCGIFPDSTADDR_IN6: 731#endif 732 if (sc->g_dst.s_addr == INADDR_ANY) { 733 error = EADDRNOTAVAIL; 734 break; 735 } 736 memset(&si, 0, sizeof(si)); 737 si.sin_family = AF_INET; 738 si.sin_len = sizeof(struct sockaddr_in); 739 si.sin_addr.s_addr = sc->g_dst.s_addr; 740 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 741 break; 742 case GRESKEY: 743 error = priv_check(curthread, PRIV_NET_GRE); 744 if (error) 745 break; 746 error = copyin(ifr->ifr_data, &key, sizeof(key)); 747 if (error) 748 break; 749 /* adjust MTU for option header */ 750 if (key == 0 && sc->key != 0) /* clear */ 751 adj += sizeof(key); 752 else if (key != 0 && sc->key == 0) /* set */ 753 adj -= sizeof(key); 754 755 if (ifp->if_mtu + adj < 576) { 756 error = EINVAL; 757 break; 758 } 759 ifp->if_mtu += adj; 760 sc->key = key; 761 break; 762 case GREGKEY: 763 error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key)); 764 break; 765 766 default: 767 error = EINVAL; 768 break; 769 } 770 771 splx(s); 772 return (error); 773} 774 775/* 776 * computes a route to our destination that is not the one 777 * which would be taken by ip_output(), as this one will loop back to 778 * us. If the interface is p2p as a--->b, then a routing entry exists 779 * If we now send a packet to b (e.g. ping b), this will come down here 780 * gets src=a, dst=b tacked on and would from ip_output() sent back to 781 * if_gre. 782 * Goal here is to compute a route to b that is less specific than 783 * a-->b. We know that this one exists as in normal operation we have 784 * at least a default route which matches. 785 */ 786static int 787gre_compute_route(struct gre_softc *sc) 788{ 789 struct route *ro; 790 791 ro = &sc->route; 792 793 memset(ro, 0, sizeof(struct route)); 794 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 795 ro->ro_dst.sa_family = AF_INET; 796 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 797 798 /* 799 * toggle last bit, so our interface is not found, but a less 800 * specific route. I'd rather like to specify a shorter mask, 801 * but this is not possible. Should work though. XXX 802 * XXX MRT Use a different FIB for the tunnel to solve this problem. 803 */ 804 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) { 805 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^= 806 htonl(0x01); 807 } 808 809#ifdef DIAGNOSTIC 810 printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)), 811 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 812#endif 813 814 rtalloc_fib(ro, sc->gre_fibnum); 815 816 /* 817 * check if this returned a route at all and this route is no 818 * recursion to ourself 819 */ 820 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 821#ifdef DIAGNOSTIC 822 if (ro->ro_rt == NULL) 823 printf(" - no route found!\n"); 824 else 825 printf(" - route loops back to ourself!\n"); 826#endif 827 return EADDRNOTAVAIL; 828 } 829 830 /* 831 * now change it back - else ip_output will just drop 832 * the route and search one to this interface ... 833 */ 834 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) 835 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 836 837#ifdef DIAGNOSTIC 838 printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 839 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 840 printf("\n"); 841#endif 842 843 return 0; 844} 845 846/* 847 * do a checksum of a buffer - much like in_cksum, which operates on 848 * mbufs. 849 */ 850u_int16_t 851gre_in_cksum(u_int16_t *p, u_int len) 852{ 853 u_int32_t sum = 0; 854 int nwords = len >> 1; 855 856 while (nwords-- != 0) 857 sum += *p++; 858 859 if (len & 1) { 860 union { 861 u_short w; 862 u_char c[2]; 863 } u; 864 u.c[0] = *(u_char *)p; 865 u.c[1] = 0; 866 sum += u.w; 867 } 868 869 /* end-around-carry */ 870 sum = (sum >> 16) + (sum & 0xffff); 871 sum += (sum >> 16); 872 return (~sum); 873} 874 875static int 876gremodevent(module_t mod, int type, void *data) 877{ 878 879 switch (type) { 880 case MOD_LOAD: 881 greattach(); 882 break; 883 case MOD_UNLOAD: 884 if_clone_detach(&gre_cloner); 885 mtx_destroy(&gre_mtx); 886 break; 887 default: 888 return EOPNOTSUPP; 889 } 890 return 0; 891} 892 893static moduledata_t gre_mod = { 894 "if_gre", 895 gremodevent, 896 0 897}; 898 899DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 900MODULE_VERSION(if_gre, 1);
| 433 gh->gi_len = m->m_pkthdr.len; 434 } 435 436 ifp->if_opackets++; 437 ifp->if_obytes += m->m_pkthdr.len; 438 /* 439 * Send it off and with IP_FORWARD flag to prevent it from 440 * overwriting the ip_id again. ip_id is already set to the 441 * ip_id of the encapsulated packet. 442 */ 443 error = ip_output(m, NULL, &sc->route, IP_FORWARDING, 444 (struct ip_moptions *)NULL, (struct inpcb *)NULL); 445 end: 446 sc->called = 0; 447 if (error) 448 ifp->if_oerrors++; 449 return (error); 450} 451 452static int 453gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 454{ 455 struct ifreq *ifr = (struct ifreq *)data; 456 struct if_laddrreq *lifr = (struct if_laddrreq *)data; 457 struct in_aliasreq *aifr = (struct in_aliasreq *)data; 458 struct gre_softc *sc = ifp->if_softc; 459 int s; 460 struct sockaddr_in si; 461 struct sockaddr *sa = NULL; 462 int error, adj; 463 struct sockaddr_in sp, sm, dp, dm; 464 uint32_t key; 465 466 error = 0; 467 adj = 0; 468 469 s = splnet(); 470 switch (cmd) { 471 case SIOCSIFADDR: 472 ifp->if_flags |= IFF_UP; 473 break; 474 case SIOCSIFDSTADDR: 475 break; 476 case SIOCSIFFLAGS: 477 /* 478 * XXXRW: Isn't this priv_check() redundant to the ifnet 479 * layer check? 480 */ 481 if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0) 482 break; 483 if ((ifr->ifr_flags & IFF_LINK0) != 0) 484 sc->g_proto = IPPROTO_GRE; 485 else 486 sc->g_proto = IPPROTO_MOBILE; 487 if ((ifr->ifr_flags & IFF_LINK2) != 0) 488 sc->wccp_ver = WCCP_V2; 489 else 490 sc->wccp_ver = WCCP_V1; 491 goto recompute; 492 case SIOCSIFMTU: 493 /* 494 * XXXRW: Isn't this priv_check() redundant to the ifnet 495 * layer check? 496 */ 497 if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0) 498 break; 499 if (ifr->ifr_mtu < 576) { 500 error = EINVAL; 501 break; 502 } 503 ifp->if_mtu = ifr->ifr_mtu; 504 break; 505 case SIOCGIFMTU: 506 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu; 507 break; 508 case SIOCADDMULTI: 509 /* 510 * XXXRW: Isn't this priv_checkr() redundant to the ifnet 511 * layer check? 512 */ 513 if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0) 514 break; 515 if (ifr == 0) { 516 error = EAFNOSUPPORT; 517 break; 518 } 519 switch (ifr->ifr_addr.sa_family) { 520#ifdef INET 521 case AF_INET: 522 break; 523#endif 524#ifdef INET6 525 case AF_INET6: 526 break; 527#endif 528 default: 529 error = EAFNOSUPPORT; 530 break; 531 } 532 break; 533 case SIOCDELMULTI: 534 /* 535 * XXXRW: Isn't this priv_check() redundant to the ifnet 536 * layer check? 537 */ 538 if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0) 539 break; 540 if (ifr == 0) { 541 error = EAFNOSUPPORT; 542 break; 543 } 544 switch (ifr->ifr_addr.sa_family) { 545#ifdef INET 546 case AF_INET: 547 break; 548#endif 549#ifdef INET6 550 case AF_INET6: 551 break; 552#endif 553 default: 554 error = EAFNOSUPPORT; 555 break; 556 } 557 break; 558 case GRESPROTO: 559 /* 560 * XXXRW: Isn't this priv_check() redundant to the ifnet 561 * layer check? 562 */ 563 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0) 564 break; 565 sc->g_proto = ifr->ifr_flags; 566 switch (sc->g_proto) { 567 case IPPROTO_GRE: 568 ifp->if_flags |= IFF_LINK0; 569 break; 570 case IPPROTO_MOBILE: 571 ifp->if_flags &= ~IFF_LINK0; 572 break; 573 default: 574 error = EPROTONOSUPPORT; 575 break; 576 } 577 goto recompute; 578 case GREGPROTO: 579 ifr->ifr_flags = sc->g_proto; 580 break; 581 case GRESADDRS: 582 case GRESADDRD: 583 error = priv_check(curthread, PRIV_NET_GRE); 584 if (error) 585 return (error); 586 /* 587 * set tunnel endpoints, compute a less specific route 588 * to the remote end and mark if as up 589 */ 590 sa = &ifr->ifr_addr; 591 if (cmd == GRESADDRS) 592 sc->g_src = (satosin(sa))->sin_addr; 593 if (cmd == GRESADDRD) 594 sc->g_dst = (satosin(sa))->sin_addr; 595 recompute: 596#ifdef INET 597 if (sc->encap != NULL) { 598 encap_detach(sc->encap); 599 sc->encap = NULL; 600 } 601#endif 602 if ((sc->g_src.s_addr != INADDR_ANY) && 603 (sc->g_dst.s_addr != INADDR_ANY)) { 604 bzero(&sp, sizeof(sp)); 605 bzero(&sm, sizeof(sm)); 606 bzero(&dp, sizeof(dp)); 607 bzero(&dm, sizeof(dm)); 608 sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 609 sizeof(struct sockaddr_in); 610 sp.sin_family = sm.sin_family = dp.sin_family = 611 dm.sin_family = AF_INET; 612 sp.sin_addr = sc->g_src; 613 dp.sin_addr = sc->g_dst; 614 sm.sin_addr.s_addr = dm.sin_addr.s_addr = 615 INADDR_BROADCAST; 616#ifdef INET 617 sc->encap = encap_attach(AF_INET, sc->g_proto, 618 sintosa(&sp), sintosa(&sm), sintosa(&dp), 619 sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 620 &in_gre_protosw : &in_mobile_protosw, sc); 621 if (sc->encap == NULL) 622 printf("%s: unable to attach encap\n", 623 if_name(GRE2IFP(sc))); 624#endif 625 if (sc->route.ro_rt != 0) /* free old route */ 626 RTFREE(sc->route.ro_rt); 627 if (gre_compute_route(sc) == 0) 628 ifp->if_drv_flags |= IFF_DRV_RUNNING; 629 else 630 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 631 } 632 break; 633 case GREGADDRS: 634 memset(&si, 0, sizeof(si)); 635 si.sin_family = AF_INET; 636 si.sin_len = sizeof(struct sockaddr_in); 637 si.sin_addr.s_addr = sc->g_src.s_addr; 638 sa = sintosa(&si); 639 ifr->ifr_addr = *sa; 640 break; 641 case GREGADDRD: 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 sa = sintosa(&si); 647 ifr->ifr_addr = *sa; 648 break; 649 case SIOCSIFPHYADDR: 650 /* 651 * XXXRW: Isn't this priv_check() redundant to the ifnet 652 * layer check? 653 */ 654 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 655 break; 656 if (aifr->ifra_addr.sin_family != AF_INET || 657 aifr->ifra_dstaddr.sin_family != AF_INET) { 658 error = EAFNOSUPPORT; 659 break; 660 } 661 if (aifr->ifra_addr.sin_len != sizeof(si) || 662 aifr->ifra_dstaddr.sin_len != sizeof(si)) { 663 error = EINVAL; 664 break; 665 } 666 sc->g_src = aifr->ifra_addr.sin_addr; 667 sc->g_dst = aifr->ifra_dstaddr.sin_addr; 668 goto recompute; 669 case SIOCSLIFPHYADDR: 670 /* 671 * XXXRW: Isn't this priv_check() redundant to the ifnet 672 * layer check? 673 */ 674 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 675 break; 676 if (lifr->addr.ss_family != AF_INET || 677 lifr->dstaddr.ss_family != AF_INET) { 678 error = EAFNOSUPPORT; 679 break; 680 } 681 if (lifr->addr.ss_len != sizeof(si) || 682 lifr->dstaddr.ss_len != sizeof(si)) { 683 error = EINVAL; 684 break; 685 } 686 sc->g_src = (satosin(&lifr->addr))->sin_addr; 687 sc->g_dst = 688 (satosin(&lifr->dstaddr))->sin_addr; 689 goto recompute; 690 case SIOCDIFPHYADDR: 691 /* 692 * XXXRW: Isn't this priv_check() redundant to the ifnet 693 * layer check? 694 */ 695 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 696 break; 697 sc->g_src.s_addr = INADDR_ANY; 698 sc->g_dst.s_addr = INADDR_ANY; 699 goto recompute; 700 case SIOCGLIFPHYADDR: 701 if (sc->g_src.s_addr == INADDR_ANY || 702 sc->g_dst.s_addr == INADDR_ANY) { 703 error = EADDRNOTAVAIL; 704 break; 705 } 706 memset(&si, 0, sizeof(si)); 707 si.sin_family = AF_INET; 708 si.sin_len = sizeof(struct sockaddr_in); 709 si.sin_addr.s_addr = sc->g_src.s_addr; 710 memcpy(&lifr->addr, &si, sizeof(si)); 711 si.sin_addr.s_addr = sc->g_dst.s_addr; 712 memcpy(&lifr->dstaddr, &si, sizeof(si)); 713 break; 714 case SIOCGIFPSRCADDR: 715#ifdef INET6 716 case SIOCGIFPSRCADDR_IN6: 717#endif 718 if (sc->g_src.s_addr == INADDR_ANY) { 719 error = EADDRNOTAVAIL; 720 break; 721 } 722 memset(&si, 0, sizeof(si)); 723 si.sin_family = AF_INET; 724 si.sin_len = sizeof(struct sockaddr_in); 725 si.sin_addr.s_addr = sc->g_src.s_addr; 726 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 727 break; 728 case SIOCGIFPDSTADDR: 729#ifdef INET6 730 case SIOCGIFPDSTADDR_IN6: 731#endif 732 if (sc->g_dst.s_addr == INADDR_ANY) { 733 error = EADDRNOTAVAIL; 734 break; 735 } 736 memset(&si, 0, sizeof(si)); 737 si.sin_family = AF_INET; 738 si.sin_len = sizeof(struct sockaddr_in); 739 si.sin_addr.s_addr = sc->g_dst.s_addr; 740 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 741 break; 742 case GRESKEY: 743 error = priv_check(curthread, PRIV_NET_GRE); 744 if (error) 745 break; 746 error = copyin(ifr->ifr_data, &key, sizeof(key)); 747 if (error) 748 break; 749 /* adjust MTU for option header */ 750 if (key == 0 && sc->key != 0) /* clear */ 751 adj += sizeof(key); 752 else if (key != 0 && sc->key == 0) /* set */ 753 adj -= sizeof(key); 754 755 if (ifp->if_mtu + adj < 576) { 756 error = EINVAL; 757 break; 758 } 759 ifp->if_mtu += adj; 760 sc->key = key; 761 break; 762 case GREGKEY: 763 error = copyout(&sc->key, ifr->ifr_data, sizeof(sc->key)); 764 break; 765 766 default: 767 error = EINVAL; 768 break; 769 } 770 771 splx(s); 772 return (error); 773} 774 775/* 776 * computes a route to our destination that is not the one 777 * which would be taken by ip_output(), as this one will loop back to 778 * us. If the interface is p2p as a--->b, then a routing entry exists 779 * If we now send a packet to b (e.g. ping b), this will come down here 780 * gets src=a, dst=b tacked on and would from ip_output() sent back to 781 * if_gre. 782 * Goal here is to compute a route to b that is less specific than 783 * a-->b. We know that this one exists as in normal operation we have 784 * at least a default route which matches. 785 */ 786static int 787gre_compute_route(struct gre_softc *sc) 788{ 789 struct route *ro; 790 791 ro = &sc->route; 792 793 memset(ro, 0, sizeof(struct route)); 794 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 795 ro->ro_dst.sa_family = AF_INET; 796 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 797 798 /* 799 * toggle last bit, so our interface is not found, but a less 800 * specific route. I'd rather like to specify a shorter mask, 801 * but this is not possible. Should work though. XXX 802 * XXX MRT Use a different FIB for the tunnel to solve this problem. 803 */ 804 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) { 805 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^= 806 htonl(0x01); 807 } 808 809#ifdef DIAGNOSTIC 810 printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)), 811 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 812#endif 813 814 rtalloc_fib(ro, sc->gre_fibnum); 815 816 /* 817 * check if this returned a route at all and this route is no 818 * recursion to ourself 819 */ 820 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 821#ifdef DIAGNOSTIC 822 if (ro->ro_rt == NULL) 823 printf(" - no route found!\n"); 824 else 825 printf(" - route loops back to ourself!\n"); 826#endif 827 return EADDRNOTAVAIL; 828 } 829 830 /* 831 * now change it back - else ip_output will just drop 832 * the route and search one to this interface ... 833 */ 834 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) 835 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 836 837#ifdef DIAGNOSTIC 838 printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 839 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 840 printf("\n"); 841#endif 842 843 return 0; 844} 845 846/* 847 * do a checksum of a buffer - much like in_cksum, which operates on 848 * mbufs. 849 */ 850u_int16_t 851gre_in_cksum(u_int16_t *p, u_int len) 852{ 853 u_int32_t sum = 0; 854 int nwords = len >> 1; 855 856 while (nwords-- != 0) 857 sum += *p++; 858 859 if (len & 1) { 860 union { 861 u_short w; 862 u_char c[2]; 863 } u; 864 u.c[0] = *(u_char *)p; 865 u.c[1] = 0; 866 sum += u.w; 867 } 868 869 /* end-around-carry */ 870 sum = (sum >> 16) + (sum & 0xffff); 871 sum += (sum >> 16); 872 return (~sum); 873} 874 875static int 876gremodevent(module_t mod, int type, void *data) 877{ 878 879 switch (type) { 880 case MOD_LOAD: 881 greattach(); 882 break; 883 case MOD_UNLOAD: 884 if_clone_detach(&gre_cloner); 885 mtx_destroy(&gre_mtx); 886 break; 887 default: 888 return EOPNOTSUPP; 889 } 890 return 0; 891} 892 893static moduledata_t gre_mod = { 894 "if_gre", 895 gremodevent, 896 0 897}; 898 899DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 900MODULE_VERSION(if_gre, 1);
|