1 /* $NetBSD: if_gre.c,v 1.45 2003/05/06 17:54:27 grant Exp $ */ 2 3 /* 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Heiko W.Rupp <hwr@pilhuhn.de> 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Encapsulate L3 protocols into IP 41 * See RFC 1701 and 1702 for more details. 42 * If_gre is compatible with Cisco GRE tunnels, so you can 43 * have a NetBSD box as the other end of a tunnel interface of a Cisco 44 * router. See gre(4) for more details. 45 * Also supported: IP in IP encaps (proto 55) as of RFC 2004 46 */ 47 48 #include <sys/cdefs.h> 49 __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.45 2003/05/06 17:54:27 grant Exp $"); 50 51 #include "opt_inet.h" 52 #include "opt_ns.h" 53 #include "bpfilter.h" 54 55 #include <sys/param.h> 56 #include <sys/malloc.h> 57 #include <sys/mbuf.h> 58 #include <sys/proc.h> 59 #include <sys/protosw.h> 60 #include <sys/socket.h> 61 #include <sys/ioctl.h> 62 #include <sys/queue.h> 63 #if __NetBSD__ 64 #include <sys/systm.h> 65 #endif 66 67 #include <machine/cpu.h> 68 69 #include <net/ethertypes.h> 70 #include <net/if.h> 71 #include <net/if_types.h> 72 #include <net/netisr.h> 73 #include <net/route.h> 74 75 #ifdef INET 76 #include <netinet/in.h> 77 #include <netinet/in_systm.h> 78 #include <netinet/in_var.h> 79 #include <netinet/ip.h> 80 #include <netinet/ip_var.h> 81 #else 82 #error "Huh? if_gre without inet?" 83 #endif 84 85 #ifdef NS 86 #include <netns/ns.h> 87 #include <netns/ns_if.h> 88 #endif 89 90 #ifdef NETATALK 91 #include <netatalk/at.h> 92 #include <netatalk/at_var.h> 93 #include <netatalk/at_extern.h> 94 #endif 95 96 #if NBPFILTER > 0 97 #include <sys/time.h> 98 #include <net/bpf.h> 99 #endif 100 101 #include <net/if_gre.h> 102 103 /* 104 * It is not easy to calculate the right value for a GRE MTU. 105 * We leave this task to the admin and use the same default that 106 * other vendors use. 107 */ 108 #define GREMTU 1476 109 110 struct gre_softc_head gre_softc_list; 111 int ip_gre_ttl = GRE_TTL; 112 113 int gre_clone_create __P((struct if_clone *, int)); 114 void gre_clone_destroy __P((struct ifnet *)); 115 116 struct if_clone gre_cloner = 117 IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy); 118 119 int gre_compute_route(struct gre_softc *sc); 120 121 void greattach __P((int)); 122 123 /* ARGSUSED */ 124 void 125 greattach(count) 126 int count; 127 { 128 129 LIST_INIT(&gre_softc_list); 130 if_clone_attach(&gre_cloner); 131 } 132 133 int 134 gre_clone_create(ifc, unit) 135 struct if_clone *ifc; 136 int unit; 137 { 138 struct gre_softc *sc; 139 140 sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK); 141 memset(sc, 0, sizeof(struct gre_softc)); 142 143 sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit); 144 sc->sc_if.if_softc = sc; 145 sc->sc_if.if_type = IFT_OTHER; 146 sc->sc_if.if_addrlen = 0; 147 sc->sc_if.if_hdrlen = 24; /* IP + GRE */ 148 sc->sc_if.if_dlt = DLT_NULL; 149 sc->sc_if.if_mtu = GREMTU; 150 sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 151 sc->sc_if.if_output = gre_output; 152 sc->sc_if.if_ioctl = gre_ioctl; 153 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 154 sc->g_proto = IPPROTO_GRE; 155 sc->sc_if.if_flags |= IFF_LINK0; 156 if_attach(&sc->sc_if); 157 if_alloc_sadl(&sc->sc_if); 158 #if NBPFILTER > 0 159 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t)); 160 #endif 161 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 162 return (0); 163 } 164 165 void 166 gre_clone_destroy(ifp) 167 struct ifnet *ifp; 168 { 169 struct gre_softc *sc = ifp->if_softc; 170 171 LIST_REMOVE(sc, sc_list); 172 #if NBPFILTER > 0 173 bpfdetach(ifp); 174 #endif 175 if_detach(ifp); 176 free(sc, M_DEVBUF); 177 } 178 179 /* 180 * The output routine. Takes a packet and encapsulates it in the protocol 181 * given by sc->g_proto. See also RFC 1701 and RFC 2004 182 */ 183 int 184 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 185 struct rtentry *rt) 186 { 187 int error = 0; 188 struct gre_softc *sc = ifp->if_softc; 189 struct greip *gh; 190 struct ip *ip; 191 u_short etype = 0; 192 struct mobile_h mob_h; 193 194 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 || 195 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) { 196 m_freem(m); 197 error = ENETDOWN; 198 goto end; 199 } 200 201 gh = NULL; 202 ip = NULL; 203 204 #if NBPFILTER >0 205 if (ifp->if_bpf) { 206 /* see comment of other if_foo.c files */ 207 struct mbuf m0; 208 u_int32_t af = dst->sa_family; 209 210 m0.m_next = m; 211 m0.m_len = 4; 212 m0.m_data = (char *)⁡ 213 214 bpf_mtap(ifp->if_bpf, &m0); 215 } 216 #endif 217 218 m->m_flags &= ~(M_BCAST|M_MCAST); 219 220 if (sc->g_proto == IPPROTO_MOBILE) { 221 if (dst->sa_family == AF_INET) { 222 struct mbuf *m0; 223 int msiz; 224 225 ip = mtod(m, struct ip *); 226 227 memset(&mob_h, 0, MOB_H_SIZ_L); 228 mob_h.proto = (ip->ip_p) << 8; 229 mob_h.odst = ip->ip_dst.s_addr; 230 ip->ip_dst.s_addr = sc->g_dst.s_addr; 231 232 /* 233 * If the packet comes from our host, we only change 234 * the destination address in the IP header. 235 * Else we also need to save and change the source 236 */ 237 if (in_hosteq(ip->ip_src, sc->g_src)) { 238 msiz = MOB_H_SIZ_S; 239 } else { 240 mob_h.proto |= MOB_H_SBIT; 241 mob_h.osrc = ip->ip_src.s_addr; 242 ip->ip_src.s_addr = sc->g_src.s_addr; 243 msiz = MOB_H_SIZ_L; 244 } 245 HTONS(mob_h.proto); 246 mob_h.hcrc = gre_in_cksum((u_short *)&mob_h, msiz); 247 248 if ((m->m_data - msiz) < m->m_pktdat) { 249 /* need new mbuf */ 250 MGETHDR(m0, M_DONTWAIT, MT_HEADER); 251 if (m0 == NULL) { 252 IF_DROP(&ifp->if_snd); 253 m_freem(m); 254 error = ENOBUFS; 255 goto end; 256 } 257 m0->m_next = m; 258 m->m_data += sizeof(struct ip); 259 m->m_len -= sizeof(struct ip); 260 m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 261 m0->m_len = msiz + sizeof(struct ip); 262 m0->m_data += max_linkhdr; 263 memcpy(mtod(m0, caddr_t), (caddr_t)ip, 264 sizeof(struct ip)); 265 m = m0; 266 } else { /* we have some space left in the old one */ 267 m->m_data -= msiz; 268 m->m_len += msiz; 269 m->m_pkthdr.len += msiz; 270 memmove(mtod(m, caddr_t), ip, 271 sizeof(struct ip)); 272 } 273 ip = mtod(m, struct ip *); 274 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz); 275 ip->ip_len = htons(ntohs(ip->ip_len) + msiz); 276 } else { /* AF_INET */ 277 IF_DROP(&ifp->if_snd); 278 m_freem(m); 279 error = EINVAL; 280 goto end; 281 } 282 } else if (sc->g_proto == IPPROTO_GRE) { 283 switch (dst->sa_family) { 284 case AF_INET: 285 ip = mtod(m, struct ip *); 286 etype = ETHERTYPE_IP; 287 break; 288 #ifdef NETATALK 289 case AF_APPLETALK: 290 etype = ETHERTYPE_ATALK; 291 break; 292 #endif 293 #ifdef NS 294 case AF_NS: 295 etype = ETHERTYPE_NS; 296 break; 297 #endif 298 default: 299 IF_DROP(&ifp->if_snd); 300 m_freem(m); 301 error = EAFNOSUPPORT; 302 goto end; 303 } 304 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT); 305 } else { 306 IF_DROP(&ifp->if_snd); 307 m_freem(m); 308 error = EINVAL; 309 goto end; 310 } 311 312 if (m == NULL) { /* impossible */ 313 IF_DROP(&ifp->if_snd); 314 error = ENOBUFS; 315 goto end; 316 } 317 318 gh = mtod(m, struct greip *); 319 if (sc->g_proto == IPPROTO_GRE) { 320 /* we don't have any GRE flags for now */ 321 322 memset((void *)&gh->gi_g, 0, sizeof(struct gre_h)); 323 gh->gi_ptype = htons(etype); 324 } 325 326 gh->gi_pr = sc->g_proto; 327 if (sc->g_proto != IPPROTO_MOBILE) { 328 gh->gi_src = sc->g_src; 329 gh->gi_dst = sc->g_dst; 330 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 331 ((struct ip*)gh)->ip_ttl = ip_gre_ttl; 332 ((struct ip*)gh)->ip_tos = ip->ip_tos; 333 gh->gi_len = m->m_pkthdr.len; 334 } 335 336 ifp->if_opackets++; 337 ifp->if_obytes += m->m_pkthdr.len; 338 /* send it off */ 339 error = ip_output(m, NULL, &sc->route, 0, NULL); 340 end: 341 if (error) 342 ifp->if_oerrors++; 343 return (error); 344 } 345 346 int 347 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 348 { 349 struct proc *p = curproc; /* XXX */ 350 struct ifreq *ifr = (struct ifreq *)data; 351 struct if_laddrreq *lifr = (struct if_laddrreq *)data; 352 struct gre_softc *sc = ifp->if_softc; 353 int s; 354 struct sockaddr_in si; 355 struct sockaddr *sa = NULL; 356 int error; 357 358 error = 0; 359 360 s = splnet(); 361 switch (cmd) { 362 case SIOCSIFADDR: 363 ifp->if_flags |= IFF_UP; 364 break; 365 case SIOCSIFDSTADDR: 366 break; 367 case SIOCSIFFLAGS: 368 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 369 break; 370 if ((ifr->ifr_flags & IFF_LINK0) != 0) 371 sc->g_proto = IPPROTO_GRE; 372 else 373 sc->g_proto = IPPROTO_MOBILE; 374 break; 375 case SIOCSIFMTU: 376 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 377 break; 378 if (ifr->ifr_mtu < 576) { 379 error = EINVAL; 380 break; 381 } 382 ifp->if_mtu = ifr->ifr_mtu; 383 break; 384 case SIOCGIFMTU: 385 ifr->ifr_mtu = sc->sc_if.if_mtu; 386 break; 387 case SIOCADDMULTI: 388 case SIOCDELMULTI: 389 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 390 break; 391 if (ifr == 0) { 392 error = EAFNOSUPPORT; 393 break; 394 } 395 switch (ifr->ifr_addr.sa_family) { 396 #ifdef INET 397 case AF_INET: 398 break; 399 #endif 400 default: 401 error = EAFNOSUPPORT; 402 break; 403 } 404 break; 405 case GRESPROTO: 406 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 407 break; 408 sc->g_proto = ifr->ifr_flags; 409 switch (sc->g_proto) { 410 case IPPROTO_GRE: 411 ifp->if_flags |= IFF_LINK0; 412 break; 413 case IPPROTO_MOBILE: 414 ifp->if_flags &= ~IFF_LINK0; 415 break; 416 default: 417 error = EPROTONOSUPPORT; 418 break; 419 } 420 break; 421 case GREGPROTO: 422 ifr->ifr_flags = sc->g_proto; 423 break; 424 case GRESADDRS: 425 case GRESADDRD: 426 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 427 break; 428 /* 429 * set tunnel endpoints, compute a less specific route 430 * to the remote end and mark if as up 431 */ 432 sa = &ifr->ifr_addr; 433 if (cmd == GRESADDRS) 434 sc->g_src = (satosin(sa))->sin_addr; 435 if (cmd == GRESADDRD) 436 sc->g_dst = (satosin(sa))->sin_addr; 437 recompute: 438 if ((sc->g_src.s_addr != INADDR_ANY) && 439 (sc->g_dst.s_addr != INADDR_ANY)) { 440 if (sc->route.ro_rt != 0) /* free old route */ 441 RTFREE(sc->route.ro_rt); 442 if (gre_compute_route(sc) == 0) 443 ifp->if_flags |= IFF_RUNNING; 444 else 445 ifp->if_flags &= ~IFF_RUNNING; 446 } 447 break; 448 case GREGADDRS: 449 memset(&si, 0, sizeof(si)); 450 si.sin_family = AF_INET; 451 si.sin_len = sizeof(struct sockaddr_in); 452 si.sin_addr.s_addr = sc->g_src.s_addr; 453 sa = sintosa(&si); 454 ifr->ifr_addr = *sa; 455 break; 456 case GREGADDRD: 457 memset(&si, 0, sizeof(si)); 458 si.sin_family = AF_INET; 459 si.sin_len = sizeof(struct sockaddr_in); 460 si.sin_addr.s_addr = sc->g_dst.s_addr; 461 sa = sintosa(&si); 462 ifr->ifr_addr = *sa; 463 break; 464 case SIOCSLIFPHYADDR: 465 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 466 break; 467 if (lifr->addr.ss_family != AF_INET || 468 lifr->dstaddr.ss_family != AF_INET) { 469 error = EAFNOSUPPORT; 470 break; 471 } 472 if (lifr->addr.ss_len != sizeof(si) || 473 lifr->dstaddr.ss_len != sizeof(si)) { 474 error = EINVAL; 475 break; 476 } 477 sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr; 478 sc->g_dst = 479 (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr; 480 goto recompute; 481 case SIOCDIFPHYADDR: 482 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 483 break; 484 sc->g_src.s_addr = INADDR_ANY; 485 sc->g_dst.s_addr = INADDR_ANY; 486 break; 487 case SIOCGLIFPHYADDR: 488 if (sc->g_src.s_addr == INADDR_ANY || 489 sc->g_dst.s_addr == INADDR_ANY) { 490 error = EADDRNOTAVAIL; 491 break; 492 } 493 memset(&si, 0, sizeof(si)); 494 si.sin_family = AF_INET; 495 si.sin_len = sizeof(struct sockaddr_in); 496 si.sin_addr.s_addr = sc->g_src.s_addr; 497 memcpy(&lifr->addr, &si, sizeof(si)); 498 si.sin_addr.s_addr = sc->g_dst.s_addr; 499 memcpy(&lifr->dstaddr, &si, sizeof(si)); 500 break; 501 default: 502 error = EINVAL; 503 break; 504 } 505 506 splx(s); 507 return (error); 508 } 509 510 /* 511 * computes a route to our destination that is not the one 512 * which would be taken by ip_output(), as this one will loop back to 513 * us. If the interface is p2p as a--->b, then a routing entry exists 514 * If we now send a packet to b (e.g. ping b), this will come down here 515 * gets src=a, dst=b tacked on and would from ip_output() sent back to 516 * if_gre. 517 * Goal here is to compute a route to b that is less specific than 518 * a-->b. We know that this one exists as in normal operation we have 519 * at least a default route which matches. 520 */ 521 int 522 gre_compute_route(struct gre_softc *sc) 523 { 524 struct route *ro; 525 u_int32_t a, b, c; 526 527 ro = &sc->route; 528 529 memset(ro, 0, sizeof(struct route)); 530 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 531 ro->ro_dst.sa_family = AF_INET; 532 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 533 534 /* 535 * toggle last bit, so our interface is not found, but a less 536 * specific route. I'd rather like to specify a shorter mask, 537 * but this is not possible. Should work though. XXX 538 * there is a simpler way ... 539 */ 540 if ((sc->sc_if.if_flags & IFF_LINK1) == 0) { 541 a = ntohl(sc->g_dst.s_addr); 542 b = a & 0x01; 543 c = a & 0xfffffffe; 544 b = b ^ 0x01; 545 a = b | c; 546 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr 547 = htonl(a); 548 } 549 550 #ifdef DIAGNOSTIC 551 printf("%s: searching for a route to %s", sc->sc_if.if_xname, 552 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 553 #endif 554 555 rtalloc(ro); 556 557 /* 558 * check if this returned a route at all and this route is no 559 * recursion to ourself 560 */ 561 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 562 #ifdef DIAGNOSTIC 563 if (ro->ro_rt == NULL) 564 printf(" - no route found!\n"); 565 else 566 printf(" - route loops back to ourself!\n"); 567 #endif 568 return EADDRNOTAVAIL; 569 } 570 571 /* 572 * now change it back - else ip_output will just drop 573 * the route and search one to this interface ... 574 */ 575 if ((sc->sc_if.if_flags & IFF_LINK1) == 0) 576 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 577 578 #ifdef DIAGNOSTIC 579 printf(", choosing %s with gateway %s", ro->ro_rt->rt_ifp->if_xname, 580 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 581 printf("\n"); 582 #endif 583 584 return 0; 585 } 586 587 /* 588 * do a checksum of a buffer - much like in_cksum, which operates on 589 * mbufs. 590 */ 591 u_short 592 gre_in_cksum(u_short *p, u_int len) 593 { 594 u_int sum = 0; 595 int nwords = len >> 1; 596 597 while (nwords-- != 0) 598 sum += *p++; 599 600 if (len & 1) { 601 union { 602 u_short w; 603 u_char c[2]; 604 } u; 605 u.c[0] = *(u_char *)p; 606 u.c[1] = 0; 607 sum += u.w; 608 } 609 610 /* end-around-carry */ 611 sum = (sum >> 16) + (sum & 0xffff); 612 sum += (sum >> 16); 613 return (~sum); 614 } 615