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