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