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