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