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 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 */ 103 struct mtx gre_mtx; 104 static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation"); 105 106 struct gre_softc_head gre_softc_list; 107 108 static int gre_clone_create(struct if_clone *, int, caddr_t); 109 static void gre_clone_destroy(struct ifnet *); 110 static int gre_ioctl(struct ifnet *, u_long, caddr_t); 111 static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *, 112 struct rtentry *rt); 113 114 IFC_SIMPLE_DECLARE(gre, 0); 115 116 static int gre_compute_route(struct gre_softc *sc); 117 118 static void greattach(void); 119 120 #ifdef INET 121 extern struct domain inetdomain; 122 static 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 }; 133 static 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 146 SYSCTL_DECL(_net_link); 147 SYSCTL_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 160 static int max_gre_nesting = MAX_GRE_NEST; 161 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW, 162 &max_gre_nesting, 0, "Max nested tunnels"); 163 164 /* ARGSUSED */ 165 static void 166 greattach(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 174 static int 175 gre_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 if_attach(GRE2IFP(sc)); 208 bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 209 mtx_lock(&gre_mtx); 210 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 211 mtx_unlock(&gre_mtx); 212 return (0); 213 } 214 215 static void 216 gre_clone_destroy(ifp) 217 struct ifnet *ifp; 218 { 219 struct gre_softc *sc = ifp->if_softc; 220 221 mtx_lock(&gre_mtx); 222 LIST_REMOVE(sc, sc_list); 223 mtx_unlock(&gre_mtx); 224 225 #ifdef INET 226 if (sc->encap != NULL) 227 encap_detach(sc->encap); 228 #endif 229 bpfdetach(ifp); 230 if_detach(ifp); 231 if_free(ifp); 232 free(sc, M_GRE); 233 } 234 235 /* 236 * The output routine. Takes a packet and encapsulates it in the protocol 237 * given by sc->g_proto. See also RFC 1701 and RFC 2004 238 */ 239 static int 240 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 241 struct rtentry *rt) 242 { 243 int error = 0; 244 struct gre_softc *sc = ifp->if_softc; 245 struct greip *gh; 246 struct ip *ip; 247 u_short ip_id = 0; 248 uint8_t ip_tos = 0; 249 u_int16_t etype = 0; 250 struct mobile_h mob_h; 251 u_int32_t af; 252 253 /* 254 * gre may cause infinite recursion calls when misconfigured. 255 * We'll prevent this by introducing upper limit. 256 */ 257 if (++(sc->called) > max_gre_nesting) { 258 printf("%s: gre_output: recursively called too many " 259 "times(%d)\n", if_name(GRE2IFP(sc)), sc->called); 260 m_freem(m); 261 error = EIO; /* is there better errno? */ 262 goto end; 263 } 264 265 if (!((ifp->if_flags & IFF_UP) && 266 (ifp->if_drv_flags & IFF_DRV_RUNNING)) || 267 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 268 m_freem(m); 269 error = ENETDOWN; 270 goto end; 271 } 272 273 gh = NULL; 274 ip = NULL; 275 276 /* BPF writes need to be handled specially. */ 277 if (dst->sa_family == AF_UNSPEC) { 278 bcopy(dst->sa_data, &af, sizeof(af)); 279 dst->sa_family = af; 280 } 281 282 if (bpf_peers_present(ifp->if_bpf)) { 283 af = dst->sa_family; 284 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 285 } 286 287 m->m_flags &= ~(M_BCAST|M_MCAST); 288 289 if (sc->g_proto == IPPROTO_MOBILE) { 290 if (dst->sa_family == AF_INET) { 291 struct mbuf *m0; 292 int msiz; 293 294 ip = mtod(m, struct ip *); 295 296 /* 297 * RFC2004 specifies that fragmented diagrams shouldn't 298 * be encapsulated. 299 */ 300 if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 301 _IF_DROP(&ifp->if_snd); 302 m_freem(m); 303 error = EINVAL; /* is there better errno? */ 304 goto end; 305 } 306 memset(&mob_h, 0, MOB_H_SIZ_L); 307 mob_h.proto = (ip->ip_p) << 8; 308 mob_h.odst = ip->ip_dst.s_addr; 309 ip->ip_dst.s_addr = sc->g_dst.s_addr; 310 311 /* 312 * If the packet comes from our host, we only change 313 * the destination address in the IP header. 314 * Else we also need to save and change the source 315 */ 316 if (in_hosteq(ip->ip_src, sc->g_src)) { 317 msiz = MOB_H_SIZ_S; 318 } else { 319 mob_h.proto |= MOB_H_SBIT; 320 mob_h.osrc = ip->ip_src.s_addr; 321 ip->ip_src.s_addr = sc->g_src.s_addr; 322 msiz = MOB_H_SIZ_L; 323 } 324 mob_h.proto = htons(mob_h.proto); 325 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz); 326 327 if ((m->m_data - msiz) < m->m_pktdat) { 328 /* need new mbuf */ 329 MGETHDR(m0, M_DONTWAIT, MT_DATA); 330 if (m0 == NULL) { 331 _IF_DROP(&ifp->if_snd); 332 m_freem(m); 333 error = ENOBUFS; 334 goto end; 335 } 336 m0->m_next = m; 337 m->m_data += sizeof(struct ip); 338 m->m_len -= sizeof(struct ip); 339 m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 340 m0->m_len = msiz + sizeof(struct ip); 341 m0->m_data += max_linkhdr; 342 memcpy(mtod(m0, caddr_t), (caddr_t)ip, 343 sizeof(struct ip)); 344 m = m0; 345 } else { /* we have some space left in the old one */ 346 m->m_data -= msiz; 347 m->m_len += msiz; 348 m->m_pkthdr.len += msiz; 349 bcopy(ip, mtod(m, caddr_t), 350 sizeof(struct ip)); 351 } 352 ip = mtod(m, struct ip *); 353 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 354 ip->ip_len = ntohs(ip->ip_len) + msiz; 355 } else { /* AF_INET */ 356 _IF_DROP(&ifp->if_snd); 357 m_freem(m); 358 error = EINVAL; 359 goto end; 360 } 361 } else if (sc->g_proto == IPPROTO_GRE) { 362 switch (dst->sa_family) { 363 case AF_INET: 364 ip = mtod(m, struct ip *); 365 ip_tos = ip->ip_tos; 366 ip_id = ip->ip_id; 367 etype = ETHERTYPE_IP; 368 break; 369 #ifdef INET6 370 case AF_INET6: 371 ip_id = ip_newid(); 372 etype = ETHERTYPE_IPV6; 373 break; 374 #endif 375 #ifdef NETATALK 376 case AF_APPLETALK: 377 etype = ETHERTYPE_ATALK; 378 break; 379 #endif 380 default: 381 _IF_DROP(&ifp->if_snd); 382 m_freem(m); 383 error = EAFNOSUPPORT; 384 goto end; 385 } 386 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT); 387 } else { 388 _IF_DROP(&ifp->if_snd); 389 m_freem(m); 390 error = EINVAL; 391 goto end; 392 } 393 394 if (m == NULL) { /* mbuf allocation failed */ 395 _IF_DROP(&ifp->if_snd); 396 error = ENOBUFS; 397 goto end; 398 } 399 400 M_SETFIB(m, sc->gre_fibnum); /* The envelope may use a different FIB */ 401 402 gh = mtod(m, struct greip *); 403 if (sc->g_proto == IPPROTO_GRE) { 404 /* we don't have any GRE flags for now */ 405 memset((void *)gh, 0, sizeof(struct greip)); 406 gh->gi_ptype = htons(etype); 407 } 408 409 gh->gi_pr = sc->g_proto; 410 if (sc->g_proto != IPPROTO_MOBILE) { 411 gh->gi_src = sc->g_src; 412 gh->gi_dst = sc->g_dst; 413 ((struct ip*)gh)->ip_v = IPPROTO_IPV4; 414 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 415 ((struct ip*)gh)->ip_ttl = GRE_TTL; 416 ((struct ip*)gh)->ip_tos = ip_tos; 417 ((struct ip*)gh)->ip_id = ip_id; 418 gh->gi_len = m->m_pkthdr.len; 419 } 420 421 ifp->if_opackets++; 422 ifp->if_obytes += m->m_pkthdr.len; 423 /* 424 * Send it off and with IP_FORWARD flag to prevent it from 425 * overwriting the ip_id again. ip_id is already set to the 426 * ip_id of the encapsulated packet. 427 */ 428 error = ip_output(m, NULL, &sc->route, IP_FORWARDING, 429 (struct ip_moptions *)NULL, (struct inpcb *)NULL); 430 end: 431 sc->called = 0; 432 if (error) 433 ifp->if_oerrors++; 434 return (error); 435 } 436 437 static int 438 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 439 { 440 struct ifreq *ifr = (struct ifreq *)data; 441 struct if_laddrreq *lifr = (struct if_laddrreq *)data; 442 struct in_aliasreq *aifr = (struct in_aliasreq *)data; 443 struct gre_softc *sc = ifp->if_softc; 444 int s; 445 struct sockaddr_in si; 446 struct sockaddr *sa = NULL; 447 int error; 448 struct sockaddr_in sp, sm, dp, dm; 449 450 error = 0; 451 452 s = splnet(); 453 switch (cmd) { 454 case SIOCSIFADDR: 455 ifp->if_flags |= IFF_UP; 456 break; 457 case SIOCSIFDSTADDR: 458 break; 459 case SIOCSIFFLAGS: 460 /* 461 * XXXRW: Isn't this priv_check() redundant to the ifnet 462 * layer check? 463 */ 464 if ((error = priv_check(curthread, PRIV_NET_SETIFFLAGS)) != 0) 465 break; 466 if ((ifr->ifr_flags & IFF_LINK0) != 0) 467 sc->g_proto = IPPROTO_GRE; 468 else 469 sc->g_proto = IPPROTO_MOBILE; 470 if ((ifr->ifr_flags & IFF_LINK2) != 0) 471 sc->wccp_ver = WCCP_V2; 472 else 473 sc->wccp_ver = WCCP_V1; 474 goto recompute; 475 case SIOCSIFMTU: 476 /* 477 * XXXRW: Isn't this priv_check() redundant to the ifnet 478 * layer check? 479 */ 480 if ((error = priv_check(curthread, PRIV_NET_SETIFMTU)) != 0) 481 break; 482 if (ifr->ifr_mtu < 576) { 483 error = EINVAL; 484 break; 485 } 486 ifp->if_mtu = ifr->ifr_mtu; 487 break; 488 case SIOCGIFMTU: 489 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu; 490 break; 491 case SIOCADDMULTI: 492 /* 493 * XXXRW: Isn't this priv_checkr() redundant to the ifnet 494 * layer check? 495 */ 496 if ((error = priv_check(curthread, PRIV_NET_ADDMULTI)) != 0) 497 break; 498 if (ifr == 0) { 499 error = EAFNOSUPPORT; 500 break; 501 } 502 switch (ifr->ifr_addr.sa_family) { 503 #ifdef INET 504 case AF_INET: 505 break; 506 #endif 507 #ifdef INET6 508 case AF_INET6: 509 break; 510 #endif 511 default: 512 error = EAFNOSUPPORT; 513 break; 514 } 515 break; 516 case SIOCDELMULTI: 517 /* 518 * XXXRW: Isn't this priv_check() redundant to the ifnet 519 * layer check? 520 */ 521 if ((error = priv_check(curthread, PRIV_NET_DELIFGROUP)) != 0) 522 break; 523 if (ifr == 0) { 524 error = EAFNOSUPPORT; 525 break; 526 } 527 switch (ifr->ifr_addr.sa_family) { 528 #ifdef INET 529 case AF_INET: 530 break; 531 #endif 532 #ifdef INET6 533 case AF_INET6: 534 break; 535 #endif 536 default: 537 error = EAFNOSUPPORT; 538 break; 539 } 540 break; 541 case GRESPROTO: 542 /* 543 * XXXRW: Isn't this priv_check() redundant to the ifnet 544 * layer check? 545 */ 546 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0) 547 break; 548 sc->g_proto = ifr->ifr_flags; 549 switch (sc->g_proto) { 550 case IPPROTO_GRE: 551 ifp->if_flags |= IFF_LINK0; 552 break; 553 case IPPROTO_MOBILE: 554 ifp->if_flags &= ~IFF_LINK0; 555 break; 556 default: 557 error = EPROTONOSUPPORT; 558 break; 559 } 560 goto recompute; 561 case GREGPROTO: 562 ifr->ifr_flags = sc->g_proto; 563 break; 564 case GRESADDRS: 565 case GRESADDRD: 566 error = priv_check(curthread, PRIV_NET_GRE); 567 if (error) 568 return (error); 569 /* 570 * set tunnel endpoints, compute a less specific route 571 * to the remote end and mark if as up 572 */ 573 sa = &ifr->ifr_addr; 574 if (cmd == GRESADDRS) 575 sc->g_src = (satosin(sa))->sin_addr; 576 if (cmd == GRESADDRD) 577 sc->g_dst = (satosin(sa))->sin_addr; 578 recompute: 579 #ifdef INET 580 if (sc->encap != NULL) { 581 encap_detach(sc->encap); 582 sc->encap = NULL; 583 } 584 #endif 585 if ((sc->g_src.s_addr != INADDR_ANY) && 586 (sc->g_dst.s_addr != INADDR_ANY)) { 587 bzero(&sp, sizeof(sp)); 588 bzero(&sm, sizeof(sm)); 589 bzero(&dp, sizeof(dp)); 590 bzero(&dm, sizeof(dm)); 591 sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len = 592 sizeof(struct sockaddr_in); 593 sp.sin_family = sm.sin_family = dp.sin_family = 594 dm.sin_family = AF_INET; 595 sp.sin_addr = sc->g_src; 596 dp.sin_addr = sc->g_dst; 597 sm.sin_addr.s_addr = dm.sin_addr.s_addr = 598 INADDR_BROADCAST; 599 #ifdef INET 600 sc->encap = encap_attach(AF_INET, sc->g_proto, 601 sintosa(&sp), sintosa(&sm), sintosa(&dp), 602 sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ? 603 &in_gre_protosw : &in_mobile_protosw, sc); 604 if (sc->encap == NULL) 605 printf("%s: unable to attach encap\n", 606 if_name(GRE2IFP(sc))); 607 #endif 608 if (sc->route.ro_rt != 0) /* free old route */ 609 RTFREE(sc->route.ro_rt); 610 if (gre_compute_route(sc) == 0) 611 ifp->if_drv_flags |= IFF_DRV_RUNNING; 612 else 613 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 614 } 615 break; 616 case GREGADDRS: 617 memset(&si, 0, sizeof(si)); 618 si.sin_family = AF_INET; 619 si.sin_len = sizeof(struct sockaddr_in); 620 si.sin_addr.s_addr = sc->g_src.s_addr; 621 sa = sintosa(&si); 622 ifr->ifr_addr = *sa; 623 break; 624 case GREGADDRD: 625 memset(&si, 0, sizeof(si)); 626 si.sin_family = AF_INET; 627 si.sin_len = sizeof(struct sockaddr_in); 628 si.sin_addr.s_addr = sc->g_dst.s_addr; 629 sa = sintosa(&si); 630 ifr->ifr_addr = *sa; 631 break; 632 case SIOCSIFPHYADDR: 633 /* 634 * XXXRW: Isn't this priv_check() redundant to the ifnet 635 * layer check? 636 */ 637 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 638 break; 639 if (aifr->ifra_addr.sin_family != AF_INET || 640 aifr->ifra_dstaddr.sin_family != AF_INET) { 641 error = EAFNOSUPPORT; 642 break; 643 } 644 if (aifr->ifra_addr.sin_len != sizeof(si) || 645 aifr->ifra_dstaddr.sin_len != sizeof(si)) { 646 error = EINVAL; 647 break; 648 } 649 sc->g_src = aifr->ifra_addr.sin_addr; 650 sc->g_dst = aifr->ifra_dstaddr.sin_addr; 651 goto recompute; 652 case SIOCSLIFPHYADDR: 653 /* 654 * XXXRW: Isn't this priv_check() redundant to the ifnet 655 * layer check? 656 */ 657 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 658 break; 659 if (lifr->addr.ss_family != AF_INET || 660 lifr->dstaddr.ss_family != AF_INET) { 661 error = EAFNOSUPPORT; 662 break; 663 } 664 if (lifr->addr.ss_len != sizeof(si) || 665 lifr->dstaddr.ss_len != sizeof(si)) { 666 error = EINVAL; 667 break; 668 } 669 sc->g_src = (satosin(&lifr->addr))->sin_addr; 670 sc->g_dst = 671 (satosin(&lifr->dstaddr))->sin_addr; 672 goto recompute; 673 case SIOCDIFPHYADDR: 674 /* 675 * XXXRW: Isn't this priv_check() redundant to the ifnet 676 * layer check? 677 */ 678 if ((error = priv_check(curthread, PRIV_NET_SETIFPHYS)) != 0) 679 break; 680 sc->g_src.s_addr = INADDR_ANY; 681 sc->g_dst.s_addr = INADDR_ANY; 682 goto recompute; 683 case SIOCGLIFPHYADDR: 684 if (sc->g_src.s_addr == INADDR_ANY || 685 sc->g_dst.s_addr == INADDR_ANY) { 686 error = EADDRNOTAVAIL; 687 break; 688 } 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 memcpy(&lifr->addr, &si, sizeof(si)); 694 si.sin_addr.s_addr = sc->g_dst.s_addr; 695 memcpy(&lifr->dstaddr, &si, sizeof(si)); 696 break; 697 case SIOCGIFPSRCADDR: 698 #ifdef INET6 699 case SIOCGIFPSRCADDR_IN6: 700 #endif 701 if (sc->g_src.s_addr == INADDR_ANY) { 702 error = EADDRNOTAVAIL; 703 break; 704 } 705 memset(&si, 0, sizeof(si)); 706 si.sin_family = AF_INET; 707 si.sin_len = sizeof(struct sockaddr_in); 708 si.sin_addr.s_addr = sc->g_src.s_addr; 709 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 710 break; 711 case SIOCGIFPDSTADDR: 712 #ifdef INET6 713 case SIOCGIFPDSTADDR_IN6: 714 #endif 715 if (sc->g_dst.s_addr == INADDR_ANY) { 716 error = EADDRNOTAVAIL; 717 break; 718 } 719 memset(&si, 0, sizeof(si)); 720 si.sin_family = AF_INET; 721 si.sin_len = sizeof(struct sockaddr_in); 722 si.sin_addr.s_addr = sc->g_dst.s_addr; 723 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); 724 break; 725 default: 726 error = EINVAL; 727 break; 728 } 729 730 splx(s); 731 return (error); 732 } 733 734 /* 735 * computes a route to our destination that is not the one 736 * which would be taken by ip_output(), as this one will loop back to 737 * us. If the interface is p2p as a--->b, then a routing entry exists 738 * If we now send a packet to b (e.g. ping b), this will come down here 739 * gets src=a, dst=b tacked on and would from ip_output() sent back to 740 * if_gre. 741 * Goal here is to compute a route to b that is less specific than 742 * a-->b. We know that this one exists as in normal operation we have 743 * at least a default route which matches. 744 */ 745 static int 746 gre_compute_route(struct gre_softc *sc) 747 { 748 struct route *ro; 749 750 ro = &sc->route; 751 752 memset(ro, 0, sizeof(struct route)); 753 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 754 ro->ro_dst.sa_family = AF_INET; 755 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 756 757 /* 758 * toggle last bit, so our interface is not found, but a less 759 * specific route. I'd rather like to specify a shorter mask, 760 * but this is not possible. Should work though. XXX 761 * XXX MRT Use a different FIB for the tunnel to solve this problem. 762 */ 763 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) { 764 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr ^= 765 htonl(0x01); 766 } 767 768 #ifdef DIAGNOSTIC 769 printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)), 770 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 771 #endif 772 773 rtalloc_fib(ro, sc->gre_fibnum); 774 775 /* 776 * check if this returned a route at all and this route is no 777 * recursion to ourself 778 */ 779 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 780 #ifdef DIAGNOSTIC 781 if (ro->ro_rt == NULL) 782 printf(" - no route found!\n"); 783 else 784 printf(" - route loops back to ourself!\n"); 785 #endif 786 return EADDRNOTAVAIL; 787 } 788 789 /* 790 * now change it back - else ip_output will just drop 791 * the route and search one to this interface ... 792 */ 793 if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) 794 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 795 796 #ifdef DIAGNOSTIC 797 printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp), 798 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 799 printf("\n"); 800 #endif 801 802 return 0; 803 } 804 805 /* 806 * do a checksum of a buffer - much like in_cksum, which operates on 807 * mbufs. 808 */ 809 u_int16_t 810 gre_in_cksum(u_int16_t *p, u_int len) 811 { 812 u_int32_t sum = 0; 813 int nwords = len >> 1; 814 815 while (nwords-- != 0) 816 sum += *p++; 817 818 if (len & 1) { 819 union { 820 u_short w; 821 u_char c[2]; 822 } u; 823 u.c[0] = *(u_char *)p; 824 u.c[1] = 0; 825 sum += u.w; 826 } 827 828 /* end-around-carry */ 829 sum = (sum >> 16) + (sum & 0xffff); 830 sum += (sum >> 16); 831 return (~sum); 832 } 833 834 static int 835 gremodevent(module_t mod, int type, void *data) 836 { 837 838 switch (type) { 839 case MOD_LOAD: 840 greattach(); 841 break; 842 case MOD_UNLOAD: 843 if_clone_detach(&gre_cloner); 844 mtx_destroy(&gre_mtx); 845 break; 846 default: 847 return EOPNOTSUPP; 848 } 849 return 0; 850 } 851 852 static moduledata_t gre_mod = { 853 "if_gre", 854 gremodevent, 855 0 856 }; 857 858 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 859 MODULE_VERSION(if_gre, 1); 860