1 /* $FreeBSD$ */ 2 /* $KAME: if_stf.c,v 1.40 2000/06/20 19:44:42 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 2000 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * 6to4 interface, based on draft-ietf-ngtrans-6to4-06.txt. 35 * 36 * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting. 37 * There is no address mapping defined from IPv6 multicast address to IPv4 38 * address. Therefore, we do not have IFF_MULTICAST on the interface. 39 * 40 * Due to the lack of address mapping for link-local addresses, we cannot 41 * throw packets toward link-local addresses (fe80::x). Also, we cannot throw 42 * packets to link-local multicast addresses (ff02::x). 43 * 44 * Here are interesting symptoms due to the lack of link-local address: 45 * 46 * Unicast routing exchange: 47 * - RIPng: Impossible. Uses link-local multicast packet toward ff02::9, 48 * and link-local addresses as nexthop. 49 * - OSPFv6: Impossible. OSPFv6 assumes that there's link-local address 50 * assigned to the link, and makes use of them. Also, HELLO packets use 51 * link-local multicast addresses (ff02::5 and ff02::6). 52 * - BGP4+: Maybe. You can only use global address as nexthop, and global 53 * address as TCP endpoint address. 54 * 55 * Multicast routing protocols: 56 * - PIM: Hello packet cannot be used to discover adjacent PIM routers. 57 * Adjacent PIM routers must be configured manually (is it really spec-wise 58 * correct thing to do?). 59 * 60 * ICMPv6: 61 * - Redirects cannot be used due to the lack of link-local address. 62 * 63 * Starting from 04 draft, the specification suggests how to construct 64 * link-local address for 6to4 interface. 65 * However, it seems to have no real use and does not help the above symptom 66 * much. Even if we assign link-locals to interface, we cannot really 67 * use link-local unicast/multicast on top of 6to4 cloud, and the above 68 * analysis does not change. 69 * 70 * 6to4 interface has security issues. Refer to 71 * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt 72 * for details. The code tries to filter out some of malicious packets. 73 * Note that there is no way to be 100% secure. 74 */ 75 76 #include "opt_inet.h" 77 #include "opt_inet6.h" 78 79 #include <sys/param.h> 80 #include <sys/systm.h> 81 #include <sys/socket.h> 82 #include <sys/sockio.h> 83 #include <sys/mbuf.h> 84 #include <sys/errno.h> 85 #include <sys/protosw.h> 86 #include <sys/kernel.h> 87 #include <machine/cpu.h> 88 89 #include <sys/malloc.h> 90 91 #include <net/if.h> 92 #include <net/route.h> 93 #include <net/netisr.h> 94 #include <net/if_types.h> 95 #include <net/if_stf.h> 96 97 #include <netinet/in.h> 98 #include <netinet/in_systm.h> 99 #include <netinet/ip.h> 100 #include <netinet/ip_var.h> 101 #include <netinet/in_var.h> 102 103 #include <netinet/ip6.h> 104 #include <netinet6/ip6_var.h> 105 #include <netinet6/in6_gif.h> 106 #include <netinet6/in6_var.h> 107 #include <netinet/ip_ecn.h> 108 109 #include <netinet/ip_encap.h> 110 111 #include <machine/stdarg.h> 112 113 #include <net/net_osdep.h> 114 115 #include "bpf.h" 116 #define NBPFILTER NBPF 117 #include "stf.h" 118 #include "gif.h" /*XXX*/ 119 120 #if NBPFILTER > 0 121 #include <net/bpf.h> 122 #endif 123 124 #if NGIF > 0 125 #include <net/if_gif.h> 126 #endif 127 128 #if NSTF > 0 129 #if NSTF != 1 130 # error only single stf interface allowed 131 #endif 132 133 #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002) 134 #define GET_V4(x) ((struct in_addr *)(&(x)->s6_addr16[1])) 135 136 struct stf_softc { 137 struct ifnet sc_if; /* common area */ 138 union { 139 struct route __sc_ro4; 140 struct route_in6 __sc_ro6; /* just for safety */ 141 } __sc_ro46; 142 #define sc_ro __sc_ro46.__sc_ro4 143 const struct encaptab *encap_cookie; 144 }; 145 146 static struct stf_softc *stf; 147 static int nstf; 148 149 #if NGIF > 0 150 extern int ip_gif_ttl; /*XXX*/ 151 #else 152 static int ip_gif_ttl = 40; /*XXX*/ 153 #endif 154 155 extern struct protosw in_stf_protosw; 156 157 void stfattach __P((void *)); 158 static int stf_encapcheck __P((const struct mbuf *, int, int, void *)); 159 static struct in6_ifaddr *stf_getsrcifa6 __P((struct ifnet *)); 160 static int stf_output __P((struct ifnet *, struct mbuf *, struct sockaddr *, 161 struct rtentry *)); 162 static int stf_checkaddr4 __P((struct in_addr *, struct ifnet *)); 163 static int stf_checkaddr6 __P((struct in6_addr *, struct ifnet *)); 164 static void stf_rtrequest __P((int, struct rtentry *, struct sockaddr *)); 165 static int stf_ioctl __P((struct ifnet *, u_long, caddr_t)); 166 167 void 168 stfattach(dummy) 169 void *dummy; 170 { 171 struct stf_softc *sc; 172 int i; 173 const struct encaptab *p; 174 175 nstf = NSTF; 176 stf = malloc(nstf * sizeof(struct stf_softc), M_DEVBUF, M_WAIT); 177 bzero(stf, nstf * sizeof(struct stf_softc)); 178 sc = stf; 179 180 /* XXX just in case... */ 181 for (i = 0; i < nstf; i++) { 182 sc = &stf[i]; 183 bzero(sc, sizeof(*sc)); 184 sc->sc_if.if_name = "stf"; 185 sc->sc_if.if_unit = i; 186 187 p = encap_attach_func(AF_INET, IPPROTO_IPV6, stf_encapcheck, 188 &in_stf_protosw, sc); 189 if (p == NULL) { 190 printf("%s: attach failed\n", if_name(&sc->sc_if)); 191 continue; 192 } 193 sc->encap_cookie = p; 194 195 sc->sc_if.if_mtu = IPV6_MMTU; 196 sc->sc_if.if_flags = 0; 197 sc->sc_if.if_ioctl = stf_ioctl; 198 sc->sc_if.if_output = stf_output; 199 sc->sc_if.if_type = IFT_STF; 200 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN; 201 if_attach(&sc->sc_if); 202 #if NBPFILTER > 0 203 #ifdef HAVE_OLD_BPF 204 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int)); 205 #else 206 bpfattach(&sc->sc_if.if_bpf, &sc->sc_if, DLT_NULL, sizeof(u_int)); 207 #endif 208 #endif 209 } 210 } 211 212 PSEUDO_SET(stfattach, if_stf); 213 214 static int 215 stf_encapcheck(m, off, proto, arg) 216 const struct mbuf *m; 217 int off; 218 int proto; 219 void *arg; 220 { 221 struct ip ip; 222 struct in6_ifaddr *ia6; 223 struct stf_softc *sc; 224 struct in_addr a, b; 225 226 sc = (struct stf_softc *)arg; 227 if (sc == NULL) 228 return 0; 229 230 if ((sc->sc_if.if_flags & IFF_UP) == 0) 231 return 0; 232 233 if (proto != IPPROTO_IPV6) 234 return 0; 235 236 /* LINTED const cast */ 237 m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip); 238 239 if (ip.ip_v != 4) 240 return 0; 241 242 ia6 = stf_getsrcifa6(&sc->sc_if); 243 if (ia6 == NULL) 244 return 0; 245 246 /* 247 * check if IPv4 dst matches the IPv4 address derived from the 248 * local 6to4 address. 249 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:... 250 */ 251 if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst, 252 sizeof(ip.ip_dst)) != 0) 253 return 0; 254 255 /* 256 * check if IPv4 src matches the IPv4 address derived from the 257 * local 6to4 address masked by prefixmask. 258 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24 259 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24 260 */ 261 bzero(&a, sizeof(a)); 262 a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr; 263 a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr; 264 b = ip.ip_src; 265 b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr; 266 if (a.s_addr != b.s_addr) 267 return 0; 268 269 /* stf interface makes single side match only */ 270 return 32; 271 } 272 273 static struct in6_ifaddr * 274 stf_getsrcifa6(ifp) 275 struct ifnet *ifp; 276 { 277 struct ifaddr *ia; 278 struct in_ifaddr *ia4; 279 struct sockaddr_in6 *sin6; 280 struct in_addr in; 281 282 for (ia = ifp->if_addrlist.tqh_first; 283 ia; 284 ia = ia->ifa_list.tqe_next) 285 { 286 if (ia->ifa_addr == NULL) 287 continue; 288 if (ia->ifa_addr->sa_family != AF_INET6) 289 continue; 290 sin6 = (struct sockaddr_in6 *)ia->ifa_addr; 291 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) 292 continue; 293 294 bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in)); 295 for (ia4 = TAILQ_FIRST(&in_ifaddrhead); 296 ia4; 297 ia4 = TAILQ_NEXT(ia4, ia_link)) 298 { 299 if (ia4->ia_addr.sin_addr.s_addr == in.s_addr) 300 break; 301 } 302 if (ia4 == NULL) 303 continue; 304 305 return (struct in6_ifaddr *)ia; 306 } 307 308 return NULL; 309 } 310 311 #ifndef offsetof 312 #define offsetof(s, e) ((int)&((s *)0)->e) 313 #endif 314 315 static int 316 stf_output(ifp, m, dst, rt) 317 struct ifnet *ifp; 318 struct mbuf *m; 319 struct sockaddr *dst; 320 struct rtentry *rt; 321 { 322 struct stf_softc *sc; 323 struct sockaddr_in6 *dst6; 324 struct sockaddr_in *dst4; 325 u_int8_t tos; 326 struct ip *ip; 327 struct ip6_hdr *ip6; 328 struct in6_ifaddr *ia6; 329 330 sc = (struct stf_softc*)ifp; 331 dst6 = (struct sockaddr_in6 *)dst; 332 333 /* just in case */ 334 if ((ifp->if_flags & IFF_UP) == 0) { 335 m_freem(m); 336 return ENETDOWN; 337 } 338 339 /* 340 * If we don't have an ip4 address that match my inner ip6 address, 341 * we shouldn't generate output. Without this check, we'll end up 342 * using wrong IPv4 source. 343 */ 344 ia6 = stf_getsrcifa6(ifp); 345 if (ia6 == NULL) { 346 m_freem(m); 347 return ENETDOWN; 348 } 349 350 if (m->m_len < sizeof(*ip6)) { 351 m = m_pullup(m, sizeof(*ip6)); 352 if (!m) 353 return ENOBUFS; 354 } 355 ip6 = mtod(m, struct ip6_hdr *); 356 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 357 358 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 359 if (m && m->m_len < sizeof(struct ip)) 360 m = m_pullup(m, sizeof(struct ip)); 361 if (m == NULL) 362 return ENOBUFS; 363 ip = mtod(m, struct ip *); 364 365 bzero(ip, sizeof(*ip)); 366 367 bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr), 368 &ip->ip_src, sizeof(ip->ip_src)); 369 bcopy(GET_V4(&dst6->sin6_addr), &ip->ip_dst, sizeof(ip->ip_dst)); 370 ip->ip_p = IPPROTO_IPV6; 371 ip->ip_ttl = ip_gif_ttl; /*XXX*/ 372 ip->ip_len = m->m_pkthdr.len; /*host order*/ 373 if (ifp->if_flags & IFF_LINK1) 374 ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos); 375 376 dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst; 377 if (dst4->sin_family != AF_INET || 378 bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) { 379 /* cache route doesn't match */ 380 dst4->sin_family = AF_INET; 381 dst4->sin_len = sizeof(struct sockaddr_in); 382 bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr)); 383 if (sc->sc_ro.ro_rt) { 384 RTFREE(sc->sc_ro.ro_rt); 385 sc->sc_ro.ro_rt = NULL; 386 } 387 } 388 389 if (sc->sc_ro.ro_rt == NULL) { 390 rtalloc(&sc->sc_ro); 391 if (sc->sc_ro.ro_rt == NULL) { 392 m_freem(m); 393 return ENETUNREACH; 394 } 395 } 396 397 return ip_output(m, NULL, &sc->sc_ro, 0, NULL); 398 } 399 400 static int 401 stf_checkaddr4(in, ifp) 402 struct in_addr *in; 403 struct ifnet *ifp; /* incoming interface */ 404 { 405 struct in_ifaddr *ia4; 406 407 /* 408 * reject packets with the following address: 409 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8 410 */ 411 if (IN_MULTICAST(in->s_addr)) 412 return -1; 413 switch ((ntohl(in->s_addr) & 0xff000000) >> 24) { 414 case 0: case 127: case 255: 415 return -1; 416 } 417 418 /* 419 * reject packets with broadcast 420 */ 421 for (ia4 = TAILQ_FIRST(&in_ifaddrhead); 422 ia4; 423 ia4 = TAILQ_NEXT(ia4, ia_link)) 424 { 425 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0) 426 continue; 427 if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) 428 return -1; 429 } 430 431 /* 432 * perform ingress filter 433 */ 434 if (ifp) { 435 struct sockaddr_in sin; 436 struct rtentry *rt; 437 438 bzero(&sin, sizeof(sin)); 439 sin.sin_family = AF_INET; 440 sin.sin_len = sizeof(struct sockaddr_in); 441 sin.sin_addr = *in; 442 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 443 if (!rt) 444 return -1; 445 if (rt->rt_ifp != ifp) { 446 rtfree(rt); 447 return -1; 448 } 449 rtfree(rt); 450 } 451 452 return 0; 453 } 454 455 static int 456 stf_checkaddr6(in6, ifp) 457 struct in6_addr *in6; 458 struct ifnet *ifp; /* incoming interface */ 459 { 460 /* 461 * check 6to4 addresses 462 */ 463 if (IN6_IS_ADDR_6TO4(in6)) 464 return stf_checkaddr4(GET_V4(in6), ifp); 465 466 /* 467 * reject anything that look suspicious. the test is implemented 468 * in ip6_input too, but we check here as well to 469 * (1) reject bad packets earlier, and 470 * (2) to be safe against future ip6_input change. 471 */ 472 if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6)) 473 return -1; 474 475 return 0; 476 } 477 478 void 479 #if __STDC__ 480 in_stf_input(struct mbuf *m, ...) 481 #else 482 in_stf_input(m, va_alist) 483 register struct mbuf *m; 484 #endif 485 { 486 int off, proto; 487 struct stf_softc *sc; 488 struct ip *ip; 489 struct ip6_hdr *ip6; 490 u_int8_t otos, itos; 491 int s, isr; 492 struct ifqueue *ifq = NULL; 493 struct ifnet *ifp; 494 va_list ap; 495 496 va_start(ap, m); 497 off = va_arg(ap, int); 498 proto = va_arg(ap, int); 499 va_end(ap); 500 501 if (proto != IPPROTO_IPV6) { 502 m_freem(m); 503 return; 504 } 505 506 ip = mtod(m, struct ip *); 507 508 sc = (struct stf_softc *)encap_getarg(m); 509 510 if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) { 511 m_freem(m); 512 return; 513 } 514 515 ifp = &sc->sc_if; 516 517 /* 518 * perform sanity check against outer src/dst. 519 * for source, perform ingress filter as well. 520 */ 521 if (stf_checkaddr4(&ip->ip_dst, NULL) < 0 || 522 stf_checkaddr4(&ip->ip_src, m->m_pkthdr.rcvif) < 0) { 523 m_freem(m); 524 return; 525 } 526 527 otos = ip->ip_tos; 528 m_adj(m, off); 529 530 if (m->m_len < sizeof(*ip6)) { 531 m = m_pullup(m, sizeof(*ip6)); 532 if (!m) 533 return; 534 } 535 ip6 = mtod(m, struct ip6_hdr *); 536 537 /* 538 * perform sanity check against inner src/dst. 539 * for source, perform ingress filter as well. 540 */ 541 if (stf_checkaddr6(&ip6->ip6_dst, NULL) < 0 || 542 stf_checkaddr6(&ip6->ip6_src, m->m_pkthdr.rcvif) < 0) { 543 m_freem(m); 544 return; 545 } 546 547 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 548 if ((ifp->if_flags & IFF_LINK1) != 0) 549 ip_ecn_egress(ECN_ALLOWED, &otos, &itos); 550 ip6->ip6_flow &= ~htonl(0xff << 20); 551 ip6->ip6_flow |= htonl((u_int32_t)itos << 20); 552 553 m->m_pkthdr.rcvif = ifp; 554 555 #if NBPFILTER > 0 556 if (ifp->if_bpf) { 557 /* 558 * We need to prepend the address family as 559 * a four byte field. Cons up a dummy header 560 * to pacify bpf. This is safe because bpf 561 * will only read from the mbuf (i.e., it won't 562 * try to free it or keep a pointer a to it). 563 */ 564 struct mbuf m0; 565 u_int af = AF_INET6; 566 567 m0.m_next = m; 568 m0.m_len = 4; 569 m0.m_data = (char *)⁡ 570 571 #ifdef HAVE_OLD_BPF 572 bpf_mtap(ifp, &m0); 573 #else 574 bpf_mtap(ifp->if_bpf, &m0); 575 #endif 576 } 577 #endif /*NBPFILTER > 0*/ 578 579 /* 580 * Put the packet to the network layer input queue according to the 581 * specified address family. 582 * See net/if_gif.c for possible issues with packet processing 583 * reorder due to extra queueing. 584 */ 585 ifq = &ip6intrq; 586 isr = NETISR_IPV6; 587 588 s = splimp(); 589 if (IF_QFULL(ifq)) { 590 IF_DROP(ifq); /* update statistics */ 591 m_freem(m); 592 splx(s); 593 return; 594 } 595 IF_ENQUEUE(ifq, m); 596 schednetisr(isr); 597 ifp->if_ipackets++; 598 ifp->if_ibytes += m->m_pkthdr.len; 599 splx(s); 600 } 601 602 /* ARGSUSED */ 603 static void 604 stf_rtrequest(cmd, rt, sa) 605 int cmd; 606 struct rtentry *rt; 607 #if defined(__bsdi__) && _BSDI_VERSION >= 199802 608 struct rt_addrinfo *sa; 609 #else 610 struct sockaddr *sa; 611 #endif 612 { 613 614 if (rt) 615 rt->rt_rmx.rmx_mtu = IPV6_MMTU; 616 } 617 618 static int 619 stf_ioctl(ifp, cmd, data) 620 struct ifnet *ifp; 621 u_long cmd; 622 caddr_t data; 623 { 624 struct ifaddr *ifa; 625 struct ifreq *ifr; 626 struct sockaddr_in6 *sin6; 627 int error; 628 629 error = 0; 630 switch (cmd) { 631 case SIOCSIFADDR: 632 ifa = (struct ifaddr *)data; 633 if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) { 634 error = EAFNOSUPPORT; 635 break; 636 } 637 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 638 if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) { 639 ifa->ifa_rtrequest = stf_rtrequest; 640 ifp->if_flags |= IFF_UP; 641 } else 642 error = EINVAL; 643 break; 644 645 case SIOCADDMULTI: 646 case SIOCDELMULTI: 647 ifr = (struct ifreq *)data; 648 if (ifr && ifr->ifr_addr.sa_family == AF_INET6) 649 ; 650 else 651 error = EAFNOSUPPORT; 652 break; 653 654 default: 655 error = EINVAL; 656 break; 657 } 658 659 return error; 660 } 661 662 #endif /* NSTF > 0 */ 663