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