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