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