1 /* $NetBSD: if_gre.c,v 1.26 2002/02/24 17:22:20 martin 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.26 2002/02/24 17:22:20 martin 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 * XXX this is below the standard MTU of 105 * 1500 Bytes, allowing for headers, 106 * but we should possibly do path mtu discovery 107 * before changing if state to up to find the 108 * correct value 109 */ 110 #define GREMTU 1450 111 #define LINK_MASK (IFF_LINK0|IFF_LINK1|IFF_LINK2) 112 113 struct gre_softc_head gre_softc_list; 114 int ip_gre_ttl = GRE_TTL; 115 116 int gre_clone_create __P((struct if_clone *, int)); 117 void gre_clone_destroy __P((struct ifnet *)); 118 119 struct if_clone gre_cloner = 120 IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy); 121 122 int gre_compute_route(struct gre_softc *sc); 123 124 void greattach __P((int)); 125 126 /* ARGSUSED */ 127 void 128 greattach(count) 129 int count; 130 { 131 132 LIST_INIT(&gre_softc_list); 133 if_clone_attach(&gre_cloner); 134 } 135 136 int 137 gre_clone_create(ifc, unit) 138 struct if_clone *ifc; 139 int unit; 140 { 141 struct gre_softc *sc; 142 143 sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK); 144 memset(sc, 0, sizeof(struct gre_softc)); 145 146 sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit); 147 sc->sc_if.if_softc = sc; 148 sc->sc_if.if_type = IFT_OTHER; 149 sc->sc_if.if_addrlen = 4; 150 sc->sc_if.if_hdrlen = 24; /* IP + GRE */ 151 sc->sc_if.if_dlt = DLT_NULL; 152 sc->sc_if.if_mtu = GREMTU; 153 sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST; 154 sc->sc_if.if_output = gre_output; 155 sc->sc_if.if_ioctl = gre_ioctl; 156 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY; 157 sc->g_proto = IPPROTO_GRE; 158 if_attach(&sc->sc_if); 159 if_alloc_sadl(&sc->sc_if); 160 #if NBPFILTER > 0 161 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t)); 162 #endif 163 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list); 164 return (0); 165 } 166 167 void 168 gre_clone_destroy(ifp) 169 struct ifnet *ifp; 170 { 171 struct gre_softc *sc = ifp->if_softc; 172 173 LIST_REMOVE(sc, sc_list); 174 #if NBPFILTER > 0 175 bpfdetach(ifp); 176 #endif 177 if_detach(ifp); 178 free(sc, M_DEVBUF); 179 } 180 181 /* 182 * The output routine. Takes a packet and encapsulates it in the protocol 183 * given by sc->g_proto. See also RFC 1701 and RFC 2004 184 */ 185 int 186 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 187 struct rtentry *rt) 188 { 189 int error = 0; 190 struct gre_softc *sc = ifp->if_softc; 191 struct greip *gh; 192 struct ip *inp; 193 u_char osrc; 194 u_short etype = 0; 195 struct mobile_h mob_h; 196 197 if ((ifp->if_flags & IFF_UP) == 0) 198 return ENETDOWN; 199 200 gh = NULL; 201 inp = NULL; 202 osrc = 0; 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 inp = mtod(m, struct ip *); 226 227 memset(&mob_h, 0, MOB_H_SIZ_L); 228 mob_h.proto = (inp->ip_p) << 8; 229 mob_h.odst = inp->ip_dst.s_addr; 230 inp->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(inp->ip_src, sc->g_src)) { 238 msiz = MOB_H_SIZ_S; 239 } else { 240 mob_h.proto |= MOB_H_SBIT; 241 mob_h.osrc = inp->ip_src.s_addr; 242 inp->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 return (ENOBUFS); 255 } 256 m0->m_next = m; 257 m->m_data += sizeof(struct ip); 258 m->m_len -= sizeof(struct ip); 259 m0->m_pkthdr.len = m->m_pkthdr.len + msiz; 260 m0->m_len = msiz + sizeof(struct ip); 261 m0->m_data += max_linkhdr; 262 memcpy(mtod(m0, caddr_t), (caddr_t)inp, 263 sizeof(struct ip)); 264 m = m0; 265 } else { /* we have some space left in the old one */ 266 m->m_data -= msiz; 267 m->m_len += msiz; 268 m->m_pkthdr.len += msiz; 269 memmove(mtod(m, caddr_t), inp, 270 sizeof(struct ip)); 271 } 272 inp=mtod(m, struct ip *); 273 memcpy((caddr_t)(inp + 1), &mob_h, (unsigned)msiz); 274 NTOHS(inp->ip_len); 275 inp->ip_len += msiz; 276 } else { /* AF_INET */ 277 IF_DROP(&ifp->if_snd); 278 m_freem(m); 279 return (EINVAL); 280 } 281 } else if (sc->g_proto == IPPROTO_GRE) { 282 switch (dst->sa_family) { 283 case AF_INET: 284 inp = mtod(m, struct ip *); 285 etype = ETHERTYPE_IP; 286 break; 287 #ifdef NETATALK 288 case AF_APPLETALK: 289 etype = ETHERTYPE_ATALK; 290 break; 291 #endif 292 #ifdef NS 293 case AF_NS: 294 etype = ETHERTYPE_NS; 295 break; 296 #endif 297 default: 298 IF_DROP(&ifp->if_snd); 299 m_freem(m); 300 return (EAFNOSUPPORT); 301 } 302 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT); 303 } else { 304 error = EINVAL; 305 IF_DROP(&ifp->if_snd); 306 m_freem(m); 307 return (error); 308 } 309 310 if (m == NULL) { 311 IF_DROP(&ifp->if_snd); 312 return (ENOBUFS); 313 } 314 315 gh = mtod(m, struct greip *); 316 if (sc->g_proto == IPPROTO_GRE) { 317 /* we don't have any GRE flags for now */ 318 319 memset((void *)&gh->gi_g, 0, sizeof(struct gre_h)); 320 gh->gi_ptype = htons(etype); 321 } 322 323 gh->gi_pr = sc->g_proto; 324 if (sc->g_proto != IPPROTO_MOBILE) { 325 gh->gi_src = sc->g_src; 326 gh->gi_dst = sc->g_dst; 327 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2; 328 ((struct ip*)gh)->ip_ttl = ip_gre_ttl; 329 ((struct ip*)gh)->ip_tos = inp->ip_tos; 330 gh->gi_len = m->m_pkthdr.len; 331 } 332 333 ifp->if_opackets++; 334 ifp->if_obytes += m->m_pkthdr.len; 335 /* send it off */ 336 error = ip_output(m, NULL, &sc->route, 0, NULL); 337 if (error) 338 ifp->if_oerrors++; 339 return (error); 340 } 341 342 int 343 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 344 { 345 struct proc *p = curproc; /* XXX */ 346 struct ifaddr *ifa = (struct ifaddr *)data; 347 struct ifreq *ifr = (struct ifreq *)data; 348 struct in_ifaddr *ia = (struct in_ifaddr *)data; 349 struct gre_softc *sc = ifp->if_softc; 350 int s; 351 struct sockaddr_in si; 352 struct sockaddr *sa = NULL; 353 int error; 354 355 error = 0; 356 357 s = splnet(); 358 switch (cmd) { 359 case SIOCSIFADDR: 360 case SIOCSIFDSTADDR: 361 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 362 break; 363 /* 364 * set tunnel endpoints in case that we "only" 365 * have ip over ip encapsulation. This allows to 366 * set tunnel endpoints with ifconfig. 367 */ 368 if (ifa->ifa_addr->sa_family == AF_INET) { 369 sa = ifa->ifa_addr; 370 sc->g_src = (satosin(sa))->sin_addr; 371 sc->g_dst = ia->ia_dstaddr.sin_addr; 372 if ((sc->g_src.s_addr != INADDR_ANY) && 373 (sc->g_dst.s_addr != INADDR_ANY)) { 374 if (sc->route.ro_rt != 0) /* free old route */ 375 RTFREE(sc->route.ro_rt); 376 if (gre_compute_route(sc) == 0) 377 ifp->if_flags |= IFF_UP; 378 } 379 } 380 break; 381 case SIOCSIFFLAGS: 382 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 383 break; 384 if ((sc->g_dst.s_addr == INADDR_ANY) || 385 (sc->g_src.s_addr == INADDR_ANY)) 386 ifp->if_flags &= ~IFF_UP; 387 388 switch (ifr->ifr_flags & LINK_MASK) { 389 case IFF_LINK0: 390 sc->g_proto = IPPROTO_GRE; 391 ifp->if_flags |= IFF_LINK0; 392 ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2); 393 break; 394 case IFF_LINK2: 395 sc->g_proto = IPPROTO_MOBILE; 396 ifp->if_flags |= IFF_LINK2; 397 ifp->if_flags &= ~(IFF_LINK0|IFF_LINK1); 398 break; 399 } 400 break; 401 case SIOCSIFMTU: 402 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 403 break; 404 if (ifr->ifr_mtu > GREMTU || ifr->ifr_mtu < 576) { 405 error = EINVAL; 406 break; 407 } 408 ifp->if_mtu = ifr->ifr_mtu; 409 break; 410 case SIOCGIFMTU: 411 ifr->ifr_mtu = sc->sc_if.if_mtu; 412 break; 413 case SIOCADDMULTI: 414 case SIOCDELMULTI: 415 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 416 break; 417 if (ifr == 0) { 418 error = EAFNOSUPPORT; 419 break; 420 } 421 switch (ifr->ifr_addr.sa_family) { 422 #ifdef INET 423 case AF_INET: 424 break; 425 #endif 426 default: 427 error = EAFNOSUPPORT; 428 break; 429 } 430 break; 431 case GRESPROTO: 432 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 433 break; 434 sc->g_proto = ifr->ifr_flags; 435 switch (sc->g_proto) { 436 case IPPROTO_GRE : 437 ifp->if_flags |= IFF_LINK0; 438 ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2); 439 break; 440 case IPPROTO_MOBILE : 441 ifp->if_flags |= IFF_LINK2; 442 ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2); 443 break; 444 default: 445 ifp->if_flags &= ~(IFF_LINK0|IFF_LINK1|IFF_LINK2); 446 } 447 break; 448 case GREGPROTO: 449 ifr->ifr_flags = sc->g_proto; 450 break; 451 case GRESADDRS: 452 case GRESADDRD: 453 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 454 break; 455 /* 456 * set tunnel endpoints, compute a less specific route 457 * to the remote end and mark if as up 458 */ 459 sa = &ifr->ifr_addr; 460 if (cmd == GRESADDRS ) 461 sc->g_src = (satosin(sa))->sin_addr; 462 if (cmd == GRESADDRD ) 463 sc->g_dst = (satosin(sa))->sin_addr; 464 if ((sc->g_src.s_addr != INADDR_ANY) && 465 (sc->g_dst.s_addr != INADDR_ANY)) { 466 if (sc->route.ro_rt != 0) /* free old route */ 467 RTFREE(sc->route.ro_rt); 468 if (gre_compute_route(sc) == 0) 469 ifp->if_flags |= IFF_UP; 470 } 471 break; 472 case GREGADDRS: 473 si.sin_addr.s_addr = sc->g_src.s_addr; 474 sa = sintosa(&si); 475 ifr->ifr_addr = *sa; 476 break; 477 case GREGADDRD: 478 si.sin_addr.s_addr = sc->g_dst.s_addr; 479 sa = sintosa(&si); 480 ifr->ifr_addr = *sa; 481 break; 482 default: 483 error = EINVAL; 484 } 485 486 splx(s); 487 return (error); 488 } 489 490 /* 491 * computes a route to our destination that is not the one 492 * which would be taken by ip_output(), as this one will loop back to 493 * us. If the interface is p2p as a--->b, then a routing entry exists 494 * If we now send a packet to b (e.g. ping b), this will come down here 495 * gets src=a, dst=b tacked on and would from ip_ouput() sent back to 496 * if_gre. 497 * Goal here is to compute a route to b that is less specific than 498 * a-->b. We know that this one exists as in normal operation we have 499 * at least a default route which matches. 500 */ 501 int 502 gre_compute_route(struct gre_softc *sc) 503 { 504 struct route *ro; 505 u_int32_t a, b, c; 506 507 ro = &sc->route; 508 509 memset(ro, 0, sizeof(struct route)); 510 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 511 ro->ro_dst.sa_family = AF_INET; 512 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 513 514 /* 515 * toggle last bit, so our interface is not found, but a less 516 * specific route. I'd rather like to specify a shorter mask, 517 * but this is not possible. Should work though. XXX 518 * there is a simpler way ... 519 */ 520 if ((sc->sc_if.if_flags & IFF_LINK1) == 0) { 521 a = ntohl(sc->g_dst.s_addr); 522 b = a & 0x01; 523 c = a & 0xfffffffe; 524 b = b ^ 0x01; 525 a = b | c; 526 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr 527 = htonl(a); 528 } 529 530 #ifdef DIAGNOSTIC 531 printf("%s: searching a route to %s", sc->sc_if.if_xname, 532 inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr)); 533 #endif 534 535 rtalloc(ro); 536 537 /* 538 * check if this returned a route at all and this route is no 539 * recursion to ourself 540 */ 541 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) { 542 #ifdef DIAGNOSTIC 543 if (ro->ro_rt == NULL) 544 printf(" - no route found!\n"); 545 else 546 printf(" - route loops back to ourself!\n"); 547 #endif 548 return EADDRNOTAVAIL; 549 } 550 551 /* 552 * now change it back - else ip_output will just drop 553 * the route and search one to this interface ... 554 */ 555 if ((sc->sc_if.if_flags & IFF_LINK1) == 0) 556 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst; 557 558 #ifdef DIAGNOSTIC 559 printf(", choosing %s with gateway %s", ro->ro_rt->rt_ifp->if_xname, 560 inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr)); 561 printf("\n"); 562 #endif 563 564 return 0; 565 } 566 567 /* 568 * do a checksum of a buffer - much like in_cksum, which operates on 569 * mbufs. 570 */ 571 u_short 572 gre_in_cksum(u_short *p, u_int len) 573 { 574 u_int sum = 0; 575 int nwords = len >> 1; 576 577 while (nwords-- != 0) 578 sum += *p++; 579 580 if (len & 1) { 581 union { 582 u_short w; 583 u_char c[2]; 584 } u; 585 u.c[0] = *(u_char *)p; 586 u.c[1] = 0; 587 sum += u.w; 588 } 589 590 /* end-around-carry */ 591 sum = (sum >> 16) + (sum & 0xffff); 592 sum += (sum >> 16); 593 return (~sum); 594 } 595