1 /* $FreeBSD$ */ 2 /* $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi 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 RFC3056. 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 * stf interface does not have, and will not need, a link-local address. 64 * It seems to have no real benefit and does not help the above symptoms much. 65 * Even if we assign link-locals to interface, we cannot really 66 * use link-local unicast/multicast on top of 6to4 cloud (since there's no 67 * encapsulation defined for link-local address), and the above analysis does 68 * not change. RFC3056 does not mandate the assignment of link-local address 69 * either. 70 * 71 * 6to4 interface has security issues. Refer to 72 * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt 73 * for details. The code tries to filter out some of malicious packets. 74 * Note that there is no way to be 100% secure. 75 */ 76 77 #include "opt_inet.h" 78 #include "opt_inet6.h" 79 #include "opt_mac.h" 80 81 #include <sys/param.h> 82 #include <sys/systm.h> 83 #include <sys/socket.h> 84 #include <sys/sockio.h> 85 #include <sys/mac.h> 86 #include <sys/mbuf.h> 87 #include <sys/errno.h> 88 #include <sys/kernel.h> 89 #include <sys/protosw.h> 90 #include <sys/queue.h> 91 #include <machine/cpu.h> 92 93 #include <sys/malloc.h> 94 95 #include <net/if.h> 96 #include <net/route.h> 97 #include <net/netisr.h> 98 #include <net/if_types.h> 99 #include <net/if_stf.h> 100 101 #include <netinet/in.h> 102 #include <netinet/in_systm.h> 103 #include <netinet/ip.h> 104 #include <netinet/ip_var.h> 105 #include <netinet/in_var.h> 106 107 #include <netinet/ip6.h> 108 #include <netinet6/ip6_var.h> 109 #include <netinet6/in6_var.h> 110 #include <netinet/ip_ecn.h> 111 112 #include <netinet/ip_encap.h> 113 114 #include <machine/stdarg.h> 115 116 #include <net/net_osdep.h> 117 118 #include <net/bpf.h> 119 120 #define STFNAME "stf" 121 122 #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002) 123 124 /* 125 * XXX: Return a pointer with 16-bit aligned. Don't cast it to 126 * struct in_addr *; use bcopy() instead. 127 */ 128 #define GET_V4(x) ((caddr_t)(&(x)->s6_addr16[1])) 129 130 struct stf_softc { 131 struct ifnet sc_if; /* common area */ 132 union { 133 struct route __sc_ro4; 134 struct route_in6 __sc_ro6; /* just for safety */ 135 } __sc_ro46; 136 #define sc_ro __sc_ro46.__sc_ro4 137 const struct encaptab *encap_cookie; 138 LIST_ENTRY(stf_softc) sc_list; /* all stf's are linked */ 139 }; 140 141 /* 142 * All mutable global variables in if_stf.c are protected by stf_mtx. 143 * XXXRW: Note that mutable fields in the softc are not currently locked: 144 * in particular, sc_ro needs to be protected from concurrent entrance 145 * of stf_output(). 146 */ 147 static struct mtx stf_mtx; 148 static LIST_HEAD(, stf_softc) stf_softc_list; 149 150 static MALLOC_DEFINE(M_STF, STFNAME, "6to4 Tunnel Interface"); 151 static const int ip_stf_ttl = 40; 152 153 extern struct domain inetdomain; 154 struct protosw in_stf_protosw = 155 { SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR, 156 in_stf_input, (pr_output_t*)rip_output, 0, rip_ctloutput, 157 0, 158 0, 0, 0, 0, 159 &rip_usrreqs 160 }; 161 162 static int stfmodevent(module_t, int, void *); 163 static int stf_encapcheck(const struct mbuf *, int, int, void *); 164 static struct in6_ifaddr *stf_getsrcifa6(struct ifnet *); 165 static int stf_output(struct ifnet *, struct mbuf *, struct sockaddr *, 166 struct rtentry *); 167 static int isrfc1918addr(struct in_addr *); 168 static int stf_checkaddr4(struct stf_softc *, struct in_addr *, 169 struct ifnet *); 170 static int stf_checkaddr6(struct stf_softc *, struct in6_addr *, 171 struct ifnet *); 172 static void stf_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 173 static int stf_ioctl(struct ifnet *, u_long, caddr_t); 174 175 int stf_clone_create(struct if_clone *, int); 176 void stf_clone_destroy(struct ifnet *); 177 178 /* only one clone is currently allowed */ 179 struct if_clone stf_cloner = 180 IF_CLONE_INITIALIZER(STFNAME, stf_clone_create, stf_clone_destroy, 0, 0); 181 182 int 183 stf_clone_create(ifc, unit) 184 struct if_clone *ifc; 185 int unit; 186 { 187 struct stf_softc *sc; 188 189 sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO); 190 if_initname(&sc->sc_if, ifc->ifc_name, unit); 191 192 sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6, 193 stf_encapcheck, &in_stf_protosw, sc); 194 if (sc->encap_cookie == NULL) { 195 printf("%s: attach failed\n", if_name(&sc->sc_if)); 196 free(sc, M_STF); 197 return (ENOMEM); 198 } 199 200 sc->sc_if.if_mtu = IPV6_MMTU; 201 sc->sc_if.if_ioctl = stf_ioctl; 202 sc->sc_if.if_output = stf_output; 203 sc->sc_if.if_type = IFT_STF; 204 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN; 205 if_attach(&sc->sc_if); 206 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int)); 207 mtx_lock(&stf_mtx); 208 LIST_INSERT_HEAD(&stf_softc_list, sc, sc_list); 209 mtx_unlock(&stf_mtx); 210 return (0); 211 } 212 213 static void 214 stf_destroy(struct stf_softc *sc) 215 { 216 int err; 217 218 err = encap_detach(sc->encap_cookie); 219 KASSERT(err == 0, ("Unexpected error detaching encap_cookie")); 220 bpfdetach(&sc->sc_if); 221 if_detach(&sc->sc_if); 222 223 free(sc, M_STF); 224 } 225 226 void 227 stf_clone_destroy(ifp) 228 struct ifnet *ifp; 229 { 230 struct stf_softc *sc = (void *) ifp; 231 232 mtx_lock(&stf_mtx); 233 LIST_REMOVE(sc, sc_list); 234 mtx_unlock(&stf_mtx); 235 236 stf_destroy(sc); 237 } 238 239 static int 240 stfmodevent(mod, type, data) 241 module_t mod; 242 int type; 243 void *data; 244 { 245 struct stf_softc *sc; 246 247 switch (type) { 248 case MOD_LOAD: 249 mtx_init(&stf_mtx, "stf_mtx", NULL, MTX_DEF); 250 LIST_INIT(&stf_softc_list); 251 if_clone_attach(&stf_cloner); 252 253 break; 254 case MOD_UNLOAD: 255 if_clone_detach(&stf_cloner); 256 257 mtx_lock(&stf_mtx); 258 while ((sc = LIST_FIRST(&stf_softc_list)) != NULL) { 259 LIST_REMOVE(sc, sc_list); 260 mtx_unlock(&stf_mtx); 261 stf_destroy(sc); 262 mtx_lock(&stf_mtx); 263 } 264 mtx_unlock(&stf_mtx); 265 mtx_destroy(&stf_mtx); 266 break; 267 } 268 269 return (0); 270 } 271 272 static moduledata_t stf_mod = { 273 "if_stf", 274 stfmodevent, 275 0 276 }; 277 278 DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 279 280 static int 281 stf_encapcheck(m, off, proto, arg) 282 const struct mbuf *m; 283 int off; 284 int proto; 285 void *arg; 286 { 287 struct ip ip; 288 struct in6_ifaddr *ia6; 289 struct stf_softc *sc; 290 struct in_addr a, b, mask; 291 292 sc = (struct stf_softc *)arg; 293 if (sc == NULL) 294 return 0; 295 296 if ((sc->sc_if.if_flags & IFF_UP) == 0) 297 return 0; 298 299 /* IFF_LINK0 means "no decapsulation" */ 300 if ((sc->sc_if.if_flags & IFF_LINK0) != 0) 301 return 0; 302 303 if (proto != IPPROTO_IPV6) 304 return 0; 305 306 /* LINTED const cast */ 307 m_copydata((struct mbuf *)(uintptr_t)m, 0, sizeof(ip), (caddr_t)&ip); 308 309 if (ip.ip_v != 4) 310 return 0; 311 312 ia6 = stf_getsrcifa6(&sc->sc_if); 313 if (ia6 == NULL) 314 return 0; 315 316 /* 317 * check if IPv4 dst matches the IPv4 address derived from the 318 * local 6to4 address. 319 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:... 320 */ 321 if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst, 322 sizeof(ip.ip_dst)) != 0) 323 return 0; 324 325 /* 326 * check if IPv4 src matches the IPv4 address derived from the 327 * local 6to4 address masked by prefixmask. 328 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24 329 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24 330 */ 331 bzero(&a, sizeof(a)); 332 bcopy(GET_V4(&ia6->ia_addr.sin6_addr), &a, sizeof(a)); 333 bcopy(GET_V4(&ia6->ia_prefixmask.sin6_addr), &mask, sizeof(mask)); 334 a.s_addr &= mask.s_addr; 335 b = ip.ip_src; 336 b.s_addr &= mask.s_addr; 337 if (a.s_addr != b.s_addr) 338 return 0; 339 340 /* stf interface makes single side match only */ 341 return 32; 342 } 343 344 static struct in6_ifaddr * 345 stf_getsrcifa6(ifp) 346 struct ifnet *ifp; 347 { 348 struct ifaddr *ia; 349 struct in_ifaddr *ia4; 350 struct sockaddr_in6 *sin6; 351 struct in_addr in; 352 353 for (ia = TAILQ_FIRST(&ifp->if_addrlist); 354 ia; 355 ia = TAILQ_NEXT(ia, ifa_list)) 356 { 357 if (ia->ifa_addr == NULL) 358 continue; 359 if (ia->ifa_addr->sa_family != AF_INET6) 360 continue; 361 sin6 = (struct sockaddr_in6 *)ia->ifa_addr; 362 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) 363 continue; 364 365 bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in)); 366 LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash) 367 if (ia4->ia_addr.sin_addr.s_addr == in.s_addr) 368 break; 369 if (ia4 == NULL) 370 continue; 371 372 return (struct in6_ifaddr *)ia; 373 } 374 375 return NULL; 376 } 377 378 static int 379 stf_output(ifp, m, dst, rt) 380 struct ifnet *ifp; 381 struct mbuf *m; 382 struct sockaddr *dst; 383 struct rtentry *rt; 384 { 385 struct stf_softc *sc; 386 struct sockaddr_in6 *dst6; 387 struct in_addr in4; 388 caddr_t ptr; 389 struct sockaddr_in *dst4; 390 u_int8_t tos; 391 struct ip *ip; 392 struct ip6_hdr *ip6; 393 struct in6_ifaddr *ia6; 394 #ifdef MAC 395 int error; 396 397 error = mac_check_ifnet_transmit(ifp, m); 398 if (error) { 399 m_freem(m); 400 return (error); 401 } 402 #endif 403 404 sc = (struct stf_softc*)ifp; 405 dst6 = (struct sockaddr_in6 *)dst; 406 407 /* just in case */ 408 if ((ifp->if_flags & IFF_UP) == 0) { 409 m_freem(m); 410 ifp->if_oerrors++; 411 return ENETDOWN; 412 } 413 414 /* 415 * If we don't have an ip4 address that match my inner ip6 address, 416 * we shouldn't generate output. Without this check, we'll end up 417 * using wrong IPv4 source. 418 */ 419 ia6 = stf_getsrcifa6(ifp); 420 if (ia6 == NULL) { 421 m_freem(m); 422 ifp->if_oerrors++; 423 return ENETDOWN; 424 } 425 426 if (m->m_len < sizeof(*ip6)) { 427 m = m_pullup(m, sizeof(*ip6)); 428 if (!m) { 429 ifp->if_oerrors++; 430 return ENOBUFS; 431 } 432 } 433 ip6 = mtod(m, struct ip6_hdr *); 434 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 435 436 /* 437 * Pickup the right outer dst addr from the list of candidates. 438 * ip6_dst has priority as it may be able to give us shorter IPv4 hops. 439 */ 440 ptr = NULL; 441 if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst)) 442 ptr = GET_V4(&ip6->ip6_dst); 443 else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr)) 444 ptr = GET_V4(&dst6->sin6_addr); 445 else { 446 m_freem(m); 447 ifp->if_oerrors++; 448 return ENETUNREACH; 449 } 450 bcopy(ptr, &in4, sizeof(in4)); 451 452 #if NBPFILTER > 0 453 if (ifp->if_bpf) { 454 /* 455 * We need to prepend the address family as 456 * a four byte field. Cons up a dummy header 457 * to pacify bpf. This is safe because bpf 458 * will only read from the mbuf (i.e., it won't 459 * try to free it or keep a pointer a to it). 460 */ 461 u_int32_t af = AF_INET6; 462 #ifdef HAVE_OLD_BPF 463 struct mbuf m0; 464 465 m0.m_next = m; 466 m0.m_len = 4; 467 m0.m_data = (char *)⁡ 468 469 BPF_MTAP(ifp, &m0); 470 #else 471 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 472 #endif 473 } 474 #endif /*NBPFILTER > 0*/ 475 476 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 477 if (m && m->m_len < sizeof(struct ip)) 478 m = m_pullup(m, sizeof(struct ip)); 479 if (m == NULL) { 480 ifp->if_oerrors++; 481 return ENOBUFS; 482 } 483 ip = mtod(m, struct ip *); 484 485 bzero(ip, sizeof(*ip)); 486 487 bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr), 488 &ip->ip_src, sizeof(ip->ip_src)); 489 bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst)); 490 ip->ip_p = IPPROTO_IPV6; 491 ip->ip_ttl = ip_stf_ttl; 492 ip->ip_len = m->m_pkthdr.len; /*host order*/ 493 if (ifp->if_flags & IFF_LINK1) 494 ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos); 495 else 496 ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos); 497 498 /* 499 * XXXRW: Locking of sc_ro required. 500 */ 501 dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst; 502 if (dst4->sin_family != AF_INET || 503 bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) { 504 /* cache route doesn't match */ 505 dst4->sin_family = AF_INET; 506 dst4->sin_len = sizeof(struct sockaddr_in); 507 bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr)); 508 if (sc->sc_ro.ro_rt) { 509 RTFREE(sc->sc_ro.ro_rt); 510 sc->sc_ro.ro_rt = NULL; 511 } 512 } 513 514 if (sc->sc_ro.ro_rt == NULL) { 515 rtalloc(&sc->sc_ro); 516 if (sc->sc_ro.ro_rt == NULL) { 517 m_freem(m); 518 ifp->if_oerrors++; 519 return ENETUNREACH; 520 } 521 } 522 523 ifp->if_opackets++; 524 return ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL); 525 } 526 527 static int 528 isrfc1918addr(in) 529 struct in_addr *in; 530 { 531 /* 532 * returns 1 if private address range: 533 * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 534 */ 535 if ((ntohl(in->s_addr) & 0xff000000) >> 24 == 10 || 536 (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 || 537 (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168) 538 return 1; 539 540 return 0; 541 } 542 543 static int 544 stf_checkaddr4(sc, in, inifp) 545 struct stf_softc *sc; 546 struct in_addr *in; 547 struct ifnet *inifp; /* incoming interface */ 548 { 549 struct in_ifaddr *ia4; 550 551 /* 552 * reject packets with the following address: 553 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8 554 */ 555 if (IN_MULTICAST(ntohl(in->s_addr))) 556 return -1; 557 switch ((ntohl(in->s_addr) & 0xff000000) >> 24) { 558 case 0: case 127: case 255: 559 return -1; 560 } 561 562 /* 563 * reject packets with private address range. 564 * (requirement from RFC3056 section 2 1st paragraph) 565 */ 566 if (isrfc1918addr(in)) 567 return -1; 568 569 /* 570 * reject packets with broadcast 571 */ 572 for (ia4 = TAILQ_FIRST(&in_ifaddrhead); 573 ia4; 574 ia4 = TAILQ_NEXT(ia4, ia_link)) 575 { 576 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0) 577 continue; 578 if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) 579 return -1; 580 } 581 582 /* 583 * perform ingress filter 584 */ 585 if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) { 586 struct sockaddr_in sin; 587 struct rtentry *rt; 588 589 bzero(&sin, sizeof(sin)); 590 sin.sin_family = AF_INET; 591 sin.sin_len = sizeof(struct sockaddr_in); 592 sin.sin_addr = *in; 593 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 594 if (!rt || rt->rt_ifp != inifp) { 595 #if 0 596 log(LOG_WARNING, "%s: packet from 0x%x dropped " 597 "due to ingress filter\n", if_name(&sc->sc_if), 598 (u_int32_t)ntohl(sin.sin_addr.s_addr)); 599 #endif 600 if (rt) 601 rtfree(rt); 602 return -1; 603 } 604 rtfree(rt); 605 } 606 607 return 0; 608 } 609 610 static int 611 stf_checkaddr6(sc, in6, inifp) 612 struct stf_softc *sc; 613 struct in6_addr *in6; 614 struct ifnet *inifp; /* incoming interface */ 615 { 616 /* 617 * check 6to4 addresses 618 */ 619 if (IN6_IS_ADDR_6TO4(in6)) { 620 struct in_addr in4; 621 bcopy(GET_V4(in6), &in4, sizeof(in4)); 622 return stf_checkaddr4(sc, &in4, inifp); 623 } 624 625 /* 626 * reject anything that look suspicious. the test is implemented 627 * in ip6_input too, but we check here as well to 628 * (1) reject bad packets earlier, and 629 * (2) to be safe against future ip6_input change. 630 */ 631 if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6)) 632 return -1; 633 634 return 0; 635 } 636 637 void 638 in_stf_input(m, off) 639 struct mbuf *m; 640 int off; 641 { 642 int proto; 643 struct stf_softc *sc; 644 struct ip *ip; 645 struct ip6_hdr *ip6; 646 u_int8_t otos, itos; 647 struct ifnet *ifp; 648 649 proto = mtod(m, struct ip *)->ip_p; 650 651 if (proto != IPPROTO_IPV6) { 652 m_freem(m); 653 return; 654 } 655 656 ip = mtod(m, struct ip *); 657 658 sc = (struct stf_softc *)encap_getarg(m); 659 660 if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) { 661 m_freem(m); 662 return; 663 } 664 665 ifp = &sc->sc_if; 666 667 #ifdef MAC 668 mac_create_mbuf_from_ifnet(ifp, m); 669 #endif 670 671 /* 672 * perform sanity check against outer src/dst. 673 * for source, perform ingress filter as well. 674 */ 675 if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 || 676 stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) { 677 m_freem(m); 678 return; 679 } 680 681 otos = ip->ip_tos; 682 m_adj(m, off); 683 684 if (m->m_len < sizeof(*ip6)) { 685 m = m_pullup(m, sizeof(*ip6)); 686 if (!m) 687 return; 688 } 689 ip6 = mtod(m, struct ip6_hdr *); 690 691 /* 692 * perform sanity check against inner src/dst. 693 * for source, perform ingress filter as well. 694 */ 695 if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 || 696 stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) { 697 m_freem(m); 698 return; 699 } 700 701 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 702 if ((ifp->if_flags & IFF_LINK1) != 0) 703 ip_ecn_egress(ECN_ALLOWED, &otos, &itos); 704 else 705 ip_ecn_egress(ECN_NOCARE, &otos, &itos); 706 ip6->ip6_flow &= ~htonl(0xff << 20); 707 ip6->ip6_flow |= htonl((u_int32_t)itos << 20); 708 709 m->m_pkthdr.rcvif = ifp; 710 711 if (ifp->if_bpf) { 712 /* 713 * We need to prepend the address family as 714 * a four byte field. Cons up a dummy header 715 * to pacify bpf. This is safe because bpf 716 * will only read from the mbuf (i.e., it won't 717 * try to free it or keep a pointer a to it). 718 */ 719 u_int32_t af = AF_INET6; 720 #ifdef HAVE_OLD_BPF 721 struct mbuf m0; 722 723 m0.m_next = m; 724 m0.m_len = 4; 725 m0.m_data = (char *)⁡ 726 727 BPF_MTAP(ifp, &m0); 728 #else 729 bpf_mtap2(ifp->if_bpf, &af, sizeof(ah), m); 730 #endif 731 } 732 733 /* 734 * Put the packet to the network layer input queue according to the 735 * specified address family. 736 * See net/if_gif.c for possible issues with packet processing 737 * reorder due to extra queueing. 738 */ 739 ifp->if_ipackets++; 740 ifp->if_ibytes += m->m_pkthdr.len; 741 netisr_dispatch(NETISR_IPV6, m); 742 } 743 744 /* ARGSUSED */ 745 static void 746 stf_rtrequest(cmd, rt, info) 747 int cmd; 748 struct rtentry *rt; 749 struct rt_addrinfo *info; 750 { 751 RT_LOCK_ASSERT(rt); 752 753 if (rt) 754 rt->rt_rmx.rmx_mtu = IPV6_MMTU; 755 } 756 757 static int 758 stf_ioctl(ifp, cmd, data) 759 struct ifnet *ifp; 760 u_long cmd; 761 caddr_t data; 762 { 763 struct ifaddr *ifa; 764 struct ifreq *ifr; 765 struct sockaddr_in6 *sin6; 766 struct in_addr addr; 767 int error; 768 769 error = 0; 770 switch (cmd) { 771 case SIOCSIFADDR: 772 ifa = (struct ifaddr *)data; 773 if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) { 774 error = EAFNOSUPPORT; 775 break; 776 } 777 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 778 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) { 779 error = EINVAL; 780 break; 781 } 782 bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr)); 783 if (isrfc1918addr(&addr)) { 784 error = EINVAL; 785 break; 786 } 787 788 ifa->ifa_rtrequest = stf_rtrequest; 789 ifp->if_flags |= IFF_UP; 790 break; 791 792 case SIOCADDMULTI: 793 case SIOCDELMULTI: 794 ifr = (struct ifreq *)data; 795 if (ifr && ifr->ifr_addr.sa_family == AF_INET6) 796 ; 797 else 798 error = EAFNOSUPPORT; 799 break; 800 801 default: 802 error = EINVAL; 803 break; 804 } 805 806 return error; 807 } 808