38#include <sys/kernel.h> 39#include <sys/malloc.h> 40#include <sys/mbuf.h> 41#include <sys/module.h> 42#include <sys/socket.h> 43#include <sys/sockio.h> 44#include <sys/errno.h> 45#include <sys/time.h> 46#include <sys/sysctl.h> 47#include <sys/syslog.h> 48#include <sys/priv.h> 49#include <sys/proc.h> 50#include <sys/protosw.h> 51#include <sys/conf.h> 52#include <machine/cpu.h> 53 54#include <net/if.h> 55#include <net/if_clone.h> 56#include <net/if_types.h> 57#include <net/netisr.h> 58#include <net/route.h> 59#include <net/bpf.h> 60#include <net/vnet.h> 61 62#include <netinet/in.h> 63#include <netinet/in_systm.h> 64#include <netinet/ip.h> 65#ifdef INET 66#include <netinet/in_var.h> 67#include <netinet/in_gif.h> 68#include <netinet/ip_var.h> 69#endif /* INET */ 70 71#ifdef INET6 72#ifndef INET 73#include <netinet/in.h> 74#endif 75#include <netinet6/in6_var.h> 76#include <netinet/ip6.h> 77#include <netinet6/ip6_var.h> 78#include <netinet6/scope6_var.h> 79#include <netinet6/in6_gif.h> 80#include <netinet6/ip6protosw.h> 81#endif /* INET6 */ 82 83#include <netinet/ip_encap.h> 84#include <net/ethernet.h> 85#include <net/if_bridgevar.h> 86#include <net/if_gif.h> 87 88#include <security/mac/mac_framework.h> 89 90#define GIFNAME "gif" 91 92/* 93 * gif_mtx protects the global gif_softc_list. 94 */ 95static struct mtx gif_mtx; 96static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 97static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list); 98#define V_gif_softc_list VNET(gif_softc_list) 99 100void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 101void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 102void (*ng_gif_attach_p)(struct ifnet *ifp); 103void (*ng_gif_detach_p)(struct ifnet *ifp); 104 105static void gif_start(struct ifnet *); 106static int gif_clone_create(struct if_clone *, int, caddr_t); 107static void gif_clone_destroy(struct ifnet *); 108 109IFC_SIMPLE_DECLARE(gif, 0); 110 111static int gifmodevent(module_t, int, void *); 112 113SYSCTL_DECL(_net_link); 114SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 115 "Generic Tunnel Interface"); 116#ifndef MAX_GIF_NEST 117/* 118 * This macro controls the default upper limitation on nesting of gif tunnels. 119 * Since, setting a large value to this macro with a careless configuration 120 * may introduce system crash, we don't allow any nestings by default. 121 * If you need to configure nested gif tunnels, you can define this macro 122 * in your kernel configuration file. However, if you do so, please be 123 * careful to configure the tunnels so that it won't make a loop. 124 */ 125#define MAX_GIF_NEST 1 126#endif 127static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST; 128#define V_max_gif_nesting VNET(max_gif_nesting) 129SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 130 &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels"); 131 132/* 133 * By default, we disallow creation of multiple tunnels between the same 134 * pair of addresses. Some applications require this functionality so 135 * we allow control over this check here. 136 */ 137#ifdef XBONEHACK 138static VNET_DEFINE(int, parallel_tunnels) = 1; 139#else 140static VNET_DEFINE(int, parallel_tunnels) = 0; 141#endif 142#define V_parallel_tunnels VNET(parallel_tunnels) 143SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 144 &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?"); 145 146/* copy from src/sys/net/if_ethersubr.c */ 147static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 148 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 149#ifndef ETHER_IS_BROADCAST 150#define ETHER_IS_BROADCAST(addr) \ 151 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 152#endif 153 154static int 155gif_clone_create(ifc, unit, params) 156 struct if_clone *ifc; 157 int unit; 158 caddr_t params; 159{ 160 struct gif_softc *sc; 161 162 sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 163 sc->gif_fibnum = curthread->td_proc->p_fibnum; 164 GIF2IFP(sc) = if_alloc(IFT_GIF); 165 if (GIF2IFP(sc) == NULL) { 166 free(sc, M_GIF); 167 return (ENOSPC); 168 } 169 170 GIF_LOCK_INIT(sc); 171 172 GIF2IFP(sc)->if_softc = sc; 173 if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 174 175 sc->encap_cookie4 = sc->encap_cookie6 = NULL; 176 sc->gif_options = GIF_ACCEPT_REVETHIP; 177 178 GIF2IFP(sc)->if_addrlen = 0; 179 GIF2IFP(sc)->if_mtu = GIF_MTU; 180 GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 181#if 0 182 /* turn off ingress filter */ 183 GIF2IFP(sc)->if_flags |= IFF_LINK2; 184#endif 185 GIF2IFP(sc)->if_ioctl = gif_ioctl; 186 GIF2IFP(sc)->if_start = gif_start; 187 GIF2IFP(sc)->if_output = gif_output; 188 GIF2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen; 189 if_attach(GIF2IFP(sc)); 190 bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 191 if (ng_gif_attach_p != NULL) 192 (*ng_gif_attach_p)(GIF2IFP(sc)); 193 194 mtx_lock(&gif_mtx); 195 LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list); 196 mtx_unlock(&gif_mtx); 197 198 return (0); 199} 200 201static void 202gif_clone_destroy(ifp) 203 struct ifnet *ifp; 204{ 205#if defined(INET) || defined(INET6) 206 int err; 207#endif 208 struct gif_softc *sc = ifp->if_softc; 209 210 mtx_lock(&gif_mtx); 211 LIST_REMOVE(sc, gif_list); 212 mtx_unlock(&gif_mtx); 213 214 gif_delete_tunnel(ifp); 215#ifdef INET6 216 if (sc->encap_cookie6 != NULL) { 217 err = encap_detach(sc->encap_cookie6); 218 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 219 } 220#endif 221#ifdef INET 222 if (sc->encap_cookie4 != NULL) { 223 err = encap_detach(sc->encap_cookie4); 224 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 225 } 226#endif 227 228 if (ng_gif_detach_p != NULL) 229 (*ng_gif_detach_p)(ifp); 230 bpfdetach(ifp); 231 if_detach(ifp); 232 if_free(ifp); 233 234 GIF_LOCK_DESTROY(sc); 235 236 free(sc, M_GIF); 237} 238 239static void 240vnet_gif_init(const void *unused __unused) 241{ 242 243 LIST_INIT(&V_gif_softc_list); 244} 245VNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_gif_init, 246 NULL); 247 248static int 249gifmodevent(mod, type, data) 250 module_t mod; 251 int type; 252 void *data; 253{ 254 255 switch (type) { 256 case MOD_LOAD: 257 mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 258 if_clone_attach(&gif_cloner); 259 break; 260 261 case MOD_UNLOAD: 262 if_clone_detach(&gif_cloner); 263 mtx_destroy(&gif_mtx); 264 break; 265 default: 266 return EOPNOTSUPP; 267 } 268 return 0; 269} 270 271static moduledata_t gif_mod = { 272 "if_gif", 273 gifmodevent, 274 0 275}; 276 277DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 278MODULE_VERSION(if_gif, 1); 279 280int 281gif_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 gif_softc *sc; 289 290 sc = (struct gif_softc *)arg; 291 if (sc == NULL) 292 return 0; 293 294 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 295 return 0; 296 297 /* no physical address */ 298 if (!sc->gif_psrc || !sc->gif_pdst) 299 return 0; 300 301 switch (proto) { 302#ifdef INET 303 case IPPROTO_IPV4: 304 break; 305#endif 306#ifdef INET6 307 case IPPROTO_IPV6: 308 break; 309#endif 310 case IPPROTO_ETHERIP: 311 break; 312 313 default: 314 return 0; 315 } 316 317 /* Bail on short packets */ 318 if (m->m_pkthdr.len < sizeof(ip)) 319 return 0; 320 321 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 322 323 switch (ip.ip_v) { 324#ifdef INET 325 case 4: 326 if (sc->gif_psrc->sa_family != AF_INET || 327 sc->gif_pdst->sa_family != AF_INET) 328 return 0; 329 return gif_encapcheck4(m, off, proto, arg); 330#endif 331#ifdef INET6 332 case 6: 333 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 334 return 0; 335 if (sc->gif_psrc->sa_family != AF_INET6 || 336 sc->gif_pdst->sa_family != AF_INET6) 337 return 0; 338 return gif_encapcheck6(m, off, proto, arg); 339#endif 340 default: 341 return 0; 342 } 343} 344 345static void 346gif_start(struct ifnet *ifp) 347{ 348 struct gif_softc *sc; 349 struct mbuf *m; 350 351 sc = ifp->if_softc; 352 353 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 354 for (;;) { 355 IFQ_DEQUEUE(&ifp->if_snd, m); 356 if (m == 0) 357 break; 358 359 gif_output(ifp, m, sc->gif_pdst, NULL); 360 361 } 362 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 363 364 return; 365} 366 367int 368gif_output(ifp, m, dst, ro) 369 struct ifnet *ifp; 370 struct mbuf *m; 371 struct sockaddr *dst; 372 struct route *ro; 373{ 374 struct gif_softc *sc = ifp->if_softc; 375 struct m_tag *mtag; 376 int error = 0; 377 int gif_called; 378 u_int32_t af; 379 380#ifdef MAC 381 error = mac_ifnet_check_transmit(ifp, m); 382 if (error) { 383 m_freem(m); 384 goto end; 385 } 386#endif 387 388 /* 389 * gif may cause infinite recursion calls when misconfigured. 390 * We'll prevent this by detecting loops. 391 * 392 * High nesting level may cause stack exhaustion. 393 * We'll prevent this by introducing upper limit. 394 */ 395 gif_called = 1; 396 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 397 while (mtag != NULL) { 398 if (*(struct ifnet **)(mtag + 1) == ifp) { 399 log(LOG_NOTICE, 400 "gif_output: loop detected on %s\n", 401 (*(struct ifnet **)(mtag + 1))->if_xname); 402 m_freem(m); 403 error = EIO; /* is there better errno? */ 404 goto end; 405 } 406 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 407 gif_called++; 408 } 409 if (gif_called > V_max_gif_nesting) { 410 log(LOG_NOTICE, 411 "gif_output: recursively called too many times(%d)\n", 412 gif_called); 413 m_freem(m); 414 error = EIO; /* is there better errno? */ 415 goto end; 416 } 417 mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 418 M_NOWAIT); 419 if (mtag == NULL) { 420 m_freem(m); 421 error = ENOMEM; 422 goto end; 423 } 424 *(struct ifnet **)(mtag + 1) = ifp; 425 m_tag_prepend(m, mtag); 426 427 m->m_flags &= ~(M_BCAST|M_MCAST); 428 429 GIF_LOCK(sc); 430 431 if (!(ifp->if_flags & IFF_UP) || 432 sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 433 GIF_UNLOCK(sc); 434 m_freem(m); 435 error = ENETDOWN; 436 goto end; 437 } 438 439 /* BPF writes need to be handled specially. */ 440 if (dst->sa_family == AF_UNSPEC) { 441 bcopy(dst->sa_data, &af, sizeof(af)); 442 dst->sa_family = af; 443 } 444 445 af = dst->sa_family; 446 BPF_MTAP2(ifp, &af, sizeof(af), m); 447 ifp->if_opackets++; 448 ifp->if_obytes += m->m_pkthdr.len; 449 450 /* override to IPPROTO_ETHERIP for bridged traffic */ 451 if (ifp->if_bridge) 452 af = AF_LINK; 453 454 M_SETFIB(m, sc->gif_fibnum); 455 /* inner AF-specific encapsulation */ 456 457 /* XXX should we check if our outer source is legal? */ 458 459 /* dispatch to output logic based on outer AF */ 460 switch (sc->gif_psrc->sa_family) { 461#ifdef INET 462 case AF_INET: 463 error = in_gif_output(ifp, af, m); 464 break; 465#endif 466#ifdef INET6 467 case AF_INET6: 468 error = in6_gif_output(ifp, af, m); 469 break; 470#endif 471 default: 472 m_freem(m); 473 error = ENETDOWN; 474 } 475 476 GIF_UNLOCK(sc); 477 end: 478 if (error) 479 ifp->if_oerrors++; 480 return (error); 481} 482 483void 484gif_input(m, af, ifp) 485 struct mbuf *m; 486 int af; 487 struct ifnet *ifp; 488{ 489 int isr, n; 490 struct gif_softc *sc; 491 struct etherip_header *eip; 492 struct ether_header *eh; 493 struct ifnet *oldifp; 494 495 if (ifp == NULL) { 496 /* just in case */ 497 m_freem(m); 498 return; 499 } 500 sc = ifp->if_softc; 501 m->m_pkthdr.rcvif = ifp; 502 503#ifdef MAC 504 mac_ifnet_create_mbuf(ifp, m); 505#endif 506 507 if (bpf_peers_present(ifp->if_bpf)) { 508 u_int32_t af1 = af; 509 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 510 } 511 512 if (ng_gif_input_p != NULL) { 513 (*ng_gif_input_p)(ifp, &m, af); 514 if (m == NULL) 515 return; 516 } 517 518 /* 519 * Put the packet to the network layer input queue according to the 520 * specified address family. 521 * Note: older versions of gif_input directly called network layer 522 * input functions, e.g. ip6_input, here. We changed the policy to 523 * prevent too many recursive calls of such input functions, which 524 * might cause kernel panic. But the change may introduce another 525 * problem; if the input queue is full, packets are discarded. 526 * The kernel stack overflow really happened, and we believed 527 * queue-full rarely occurs, so we changed the policy. 528 */ 529 switch (af) { 530#ifdef INET 531 case AF_INET: 532 isr = NETISR_IP; 533 break; 534#endif 535#ifdef INET6 536 case AF_INET6: 537 isr = NETISR_IPV6; 538 break; 539#endif 540 case AF_LINK: 541 n = sizeof(struct etherip_header) + sizeof(struct ether_header); 542 if (n > m->m_len) { 543 m = m_pullup(m, n); 544 if (m == NULL) { 545 ifp->if_ierrors++; 546 return; 547 } 548 } 549 550 eip = mtod(m, struct etherip_header *); 551 /* 552 * GIF_ACCEPT_REVETHIP (enabled by default) intentionally 553 * accepts an EtherIP packet with revered version field in 554 * the header. This is a knob for backward compatibility 555 * with FreeBSD 7.2R or prior. 556 */ 557 if (sc->gif_options & GIF_ACCEPT_REVETHIP) { 558 if (eip->eip_resvl != ETHERIP_VERSION 559 && eip->eip_ver != ETHERIP_VERSION) { 560 /* discard unknown versions */ 561 m_freem(m); 562 return; 563 } 564 } else { 565 if (eip->eip_ver != ETHERIP_VERSION) { 566 /* discard unknown versions */ 567 m_freem(m); 568 return; 569 } 570 } 571 m_adj(m, sizeof(struct etherip_header)); 572 573 m->m_flags &= ~(M_BCAST|M_MCAST); 574 m->m_pkthdr.rcvif = ifp; 575 576 if (ifp->if_bridge) { 577 oldifp = ifp; 578 eh = mtod(m, struct ether_header *); 579 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 580 if (ETHER_IS_BROADCAST(eh->ether_dhost)) 581 m->m_flags |= M_BCAST; 582 else 583 m->m_flags |= M_MCAST; 584 ifp->if_imcasts++; 585 } 586 BRIDGE_INPUT(ifp, m); 587 588 if (m != NULL && ifp != oldifp) { 589 /* 590 * The bridge gave us back itself or one of the 591 * members for which the frame is addressed. 592 */ 593 ether_demux(ifp, m); 594 return; 595 } 596 } 597 if (m != NULL) 598 m_freem(m); 599 return; 600 601 default: 602 if (ng_gif_input_orphan_p != NULL) 603 (*ng_gif_input_orphan_p)(ifp, m, af); 604 else 605 m_freem(m); 606 return; 607 } 608 609 ifp->if_ipackets++; 610 ifp->if_ibytes += m->m_pkthdr.len; 611 netisr_dispatch(isr, m); 612} 613 614/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 615int 616gif_ioctl(ifp, cmd, data) 617 struct ifnet *ifp; 618 u_long cmd; 619 caddr_t data; 620{ 621 struct gif_softc *sc = ifp->if_softc; 622 struct ifreq *ifr = (struct ifreq*)data; 623 int error = 0, size; 624 u_int options; 625 struct sockaddr *dst, *src; 626#ifdef SIOCSIFMTU /* xxx */ 627 u_long mtu; 628#endif 629 630 switch (cmd) { 631 case SIOCSIFADDR: 632 ifp->if_flags |= IFF_UP; 633 break; 634 635 case SIOCSIFDSTADDR: 636 break; 637 638 case SIOCADDMULTI: 639 case SIOCDELMULTI: 640 break; 641 642#ifdef SIOCSIFMTU /* xxx */ 643 case SIOCGIFMTU: 644 break; 645 646 case SIOCSIFMTU: 647 mtu = ifr->ifr_mtu; 648 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 649 return (EINVAL); 650 ifp->if_mtu = mtu; 651 break; 652#endif /* SIOCSIFMTU */ 653 654#ifdef INET 655 case SIOCSIFPHYADDR: 656#endif 657#ifdef INET6 658 case SIOCSIFPHYADDR_IN6: 659#endif /* INET6 */ 660 case SIOCSLIFPHYADDR: 661 switch (cmd) { 662#ifdef INET 663 case SIOCSIFPHYADDR: 664 src = (struct sockaddr *) 665 &(((struct in_aliasreq *)data)->ifra_addr); 666 dst = (struct sockaddr *) 667 &(((struct in_aliasreq *)data)->ifra_dstaddr); 668 break; 669#endif 670#ifdef INET6 671 case SIOCSIFPHYADDR_IN6: 672 src = (struct sockaddr *) 673 &(((struct in6_aliasreq *)data)->ifra_addr); 674 dst = (struct sockaddr *) 675 &(((struct in6_aliasreq *)data)->ifra_dstaddr); 676 break; 677#endif 678 case SIOCSLIFPHYADDR: 679 src = (struct sockaddr *) 680 &(((struct if_laddrreq *)data)->addr); 681 dst = (struct sockaddr *) 682 &(((struct if_laddrreq *)data)->dstaddr); 683 break; 684 default: 685 return EINVAL; 686 } 687 688 /* sa_family must be equal */ 689 if (src->sa_family != dst->sa_family) 690 return EINVAL; 691 692 /* validate sa_len */ 693 switch (src->sa_family) { 694#ifdef INET 695 case AF_INET: 696 if (src->sa_len != sizeof(struct sockaddr_in)) 697 return EINVAL; 698 break; 699#endif 700#ifdef INET6 701 case AF_INET6: 702 if (src->sa_len != sizeof(struct sockaddr_in6)) 703 return EINVAL; 704 break; 705#endif 706 default: 707 return EAFNOSUPPORT; 708 } 709 switch (dst->sa_family) { 710#ifdef INET 711 case AF_INET: 712 if (dst->sa_len != sizeof(struct sockaddr_in)) 713 return EINVAL; 714 break; 715#endif 716#ifdef INET6 717 case AF_INET6: 718 if (dst->sa_len != sizeof(struct sockaddr_in6)) 719 return EINVAL; 720 break; 721#endif 722 default: 723 return EAFNOSUPPORT; 724 } 725 726 /* check sa_family looks sane for the cmd */ 727 switch (cmd) { 728 case SIOCSIFPHYADDR: 729 if (src->sa_family == AF_INET) 730 break; 731 return EAFNOSUPPORT; 732#ifdef INET6 733 case SIOCSIFPHYADDR_IN6: 734 if (src->sa_family == AF_INET6) 735 break; 736 return EAFNOSUPPORT; 737#endif /* INET6 */ 738 case SIOCSLIFPHYADDR: 739 /* checks done in the above */ 740 break; 741 } 742 743 error = gif_set_tunnel(GIF2IFP(sc), src, dst); 744 break; 745 746#ifdef SIOCDIFPHYADDR 747 case SIOCDIFPHYADDR: 748 gif_delete_tunnel(GIF2IFP(sc)); 749 break; 750#endif 751 752 case SIOCGIFPSRCADDR: 753#ifdef INET6 754 case SIOCGIFPSRCADDR_IN6: 755#endif /* INET6 */ 756 if (sc->gif_psrc == NULL) { 757 error = EADDRNOTAVAIL; 758 goto bad; 759 } 760 src = sc->gif_psrc; 761 switch (cmd) { 762#ifdef INET 763 case SIOCGIFPSRCADDR: 764 dst = &ifr->ifr_addr; 765 size = sizeof(ifr->ifr_addr); 766 break; 767#endif /* INET */ 768#ifdef INET6 769 case SIOCGIFPSRCADDR_IN6: 770 dst = (struct sockaddr *) 771 &(((struct in6_ifreq *)data)->ifr_addr); 772 size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 773 break; 774#endif /* INET6 */ 775 default: 776 error = EADDRNOTAVAIL; 777 goto bad; 778 } 779 if (src->sa_len > size) 780 return EINVAL; 781 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 782#ifdef INET6 783 if (dst->sa_family == AF_INET6) { 784 error = sa6_recoverscope((struct sockaddr_in6 *)dst); 785 if (error != 0) 786 return (error); 787 } 788#endif 789 break; 790 791 case SIOCGIFPDSTADDR: 792#ifdef INET6 793 case SIOCGIFPDSTADDR_IN6: 794#endif /* INET6 */ 795 if (sc->gif_pdst == NULL) { 796 error = EADDRNOTAVAIL; 797 goto bad; 798 } 799 src = sc->gif_pdst; 800 switch (cmd) { 801#ifdef INET 802 case SIOCGIFPDSTADDR: 803 dst = &ifr->ifr_addr; 804 size = sizeof(ifr->ifr_addr); 805 break; 806#endif /* INET */ 807#ifdef INET6 808 case SIOCGIFPDSTADDR_IN6: 809 dst = (struct sockaddr *) 810 &(((struct in6_ifreq *)data)->ifr_addr); 811 size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 812 break; 813#endif /* INET6 */ 814 default: 815 error = EADDRNOTAVAIL; 816 goto bad; 817 } 818 if (src->sa_len > size) 819 return EINVAL;
| 39#include <sys/kernel.h> 40#include <sys/malloc.h> 41#include <sys/mbuf.h> 42#include <sys/module.h> 43#include <sys/socket.h> 44#include <sys/sockio.h> 45#include <sys/errno.h> 46#include <sys/time.h> 47#include <sys/sysctl.h> 48#include <sys/syslog.h> 49#include <sys/priv.h> 50#include <sys/proc.h> 51#include <sys/protosw.h> 52#include <sys/conf.h> 53#include <machine/cpu.h> 54 55#include <net/if.h> 56#include <net/if_clone.h> 57#include <net/if_types.h> 58#include <net/netisr.h> 59#include <net/route.h> 60#include <net/bpf.h> 61#include <net/vnet.h> 62 63#include <netinet/in.h> 64#include <netinet/in_systm.h> 65#include <netinet/ip.h> 66#ifdef INET 67#include <netinet/in_var.h> 68#include <netinet/in_gif.h> 69#include <netinet/ip_var.h> 70#endif /* INET */ 71 72#ifdef INET6 73#ifndef INET 74#include <netinet/in.h> 75#endif 76#include <netinet6/in6_var.h> 77#include <netinet/ip6.h> 78#include <netinet6/ip6_var.h> 79#include <netinet6/scope6_var.h> 80#include <netinet6/in6_gif.h> 81#include <netinet6/ip6protosw.h> 82#endif /* INET6 */ 83 84#include <netinet/ip_encap.h> 85#include <net/ethernet.h> 86#include <net/if_bridgevar.h> 87#include <net/if_gif.h> 88 89#include <security/mac/mac_framework.h> 90 91#define GIFNAME "gif" 92 93/* 94 * gif_mtx protects the global gif_softc_list. 95 */ 96static struct mtx gif_mtx; 97static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 98static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list); 99#define V_gif_softc_list VNET(gif_softc_list) 100 101void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 102void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 103void (*ng_gif_attach_p)(struct ifnet *ifp); 104void (*ng_gif_detach_p)(struct ifnet *ifp); 105 106static void gif_start(struct ifnet *); 107static int gif_clone_create(struct if_clone *, int, caddr_t); 108static void gif_clone_destroy(struct ifnet *); 109 110IFC_SIMPLE_DECLARE(gif, 0); 111 112static int gifmodevent(module_t, int, void *); 113 114SYSCTL_DECL(_net_link); 115SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 116 "Generic Tunnel Interface"); 117#ifndef MAX_GIF_NEST 118/* 119 * This macro controls the default upper limitation on nesting of gif tunnels. 120 * Since, setting a large value to this macro with a careless configuration 121 * may introduce system crash, we don't allow any nestings by default. 122 * If you need to configure nested gif tunnels, you can define this macro 123 * in your kernel configuration file. However, if you do so, please be 124 * careful to configure the tunnels so that it won't make a loop. 125 */ 126#define MAX_GIF_NEST 1 127#endif 128static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST; 129#define V_max_gif_nesting VNET(max_gif_nesting) 130SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 131 &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels"); 132 133/* 134 * By default, we disallow creation of multiple tunnels between the same 135 * pair of addresses. Some applications require this functionality so 136 * we allow control over this check here. 137 */ 138#ifdef XBONEHACK 139static VNET_DEFINE(int, parallel_tunnels) = 1; 140#else 141static VNET_DEFINE(int, parallel_tunnels) = 0; 142#endif 143#define V_parallel_tunnels VNET(parallel_tunnels) 144SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 145 &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?"); 146 147/* copy from src/sys/net/if_ethersubr.c */ 148static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 149 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 150#ifndef ETHER_IS_BROADCAST 151#define ETHER_IS_BROADCAST(addr) \ 152 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 153#endif 154 155static int 156gif_clone_create(ifc, unit, params) 157 struct if_clone *ifc; 158 int unit; 159 caddr_t params; 160{ 161 struct gif_softc *sc; 162 163 sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 164 sc->gif_fibnum = curthread->td_proc->p_fibnum; 165 GIF2IFP(sc) = if_alloc(IFT_GIF); 166 if (GIF2IFP(sc) == NULL) { 167 free(sc, M_GIF); 168 return (ENOSPC); 169 } 170 171 GIF_LOCK_INIT(sc); 172 173 GIF2IFP(sc)->if_softc = sc; 174 if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 175 176 sc->encap_cookie4 = sc->encap_cookie6 = NULL; 177 sc->gif_options = GIF_ACCEPT_REVETHIP; 178 179 GIF2IFP(sc)->if_addrlen = 0; 180 GIF2IFP(sc)->if_mtu = GIF_MTU; 181 GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 182#if 0 183 /* turn off ingress filter */ 184 GIF2IFP(sc)->if_flags |= IFF_LINK2; 185#endif 186 GIF2IFP(sc)->if_ioctl = gif_ioctl; 187 GIF2IFP(sc)->if_start = gif_start; 188 GIF2IFP(sc)->if_output = gif_output; 189 GIF2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen; 190 if_attach(GIF2IFP(sc)); 191 bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 192 if (ng_gif_attach_p != NULL) 193 (*ng_gif_attach_p)(GIF2IFP(sc)); 194 195 mtx_lock(&gif_mtx); 196 LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list); 197 mtx_unlock(&gif_mtx); 198 199 return (0); 200} 201 202static void 203gif_clone_destroy(ifp) 204 struct ifnet *ifp; 205{ 206#if defined(INET) || defined(INET6) 207 int err; 208#endif 209 struct gif_softc *sc = ifp->if_softc; 210 211 mtx_lock(&gif_mtx); 212 LIST_REMOVE(sc, gif_list); 213 mtx_unlock(&gif_mtx); 214 215 gif_delete_tunnel(ifp); 216#ifdef INET6 217 if (sc->encap_cookie6 != NULL) { 218 err = encap_detach(sc->encap_cookie6); 219 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 220 } 221#endif 222#ifdef INET 223 if (sc->encap_cookie4 != NULL) { 224 err = encap_detach(sc->encap_cookie4); 225 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 226 } 227#endif 228 229 if (ng_gif_detach_p != NULL) 230 (*ng_gif_detach_p)(ifp); 231 bpfdetach(ifp); 232 if_detach(ifp); 233 if_free(ifp); 234 235 GIF_LOCK_DESTROY(sc); 236 237 free(sc, M_GIF); 238} 239 240static void 241vnet_gif_init(const void *unused __unused) 242{ 243 244 LIST_INIT(&V_gif_softc_list); 245} 246VNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_gif_init, 247 NULL); 248 249static int 250gifmodevent(mod, type, data) 251 module_t mod; 252 int type; 253 void *data; 254{ 255 256 switch (type) { 257 case MOD_LOAD: 258 mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 259 if_clone_attach(&gif_cloner); 260 break; 261 262 case MOD_UNLOAD: 263 if_clone_detach(&gif_cloner); 264 mtx_destroy(&gif_mtx); 265 break; 266 default: 267 return EOPNOTSUPP; 268 } 269 return 0; 270} 271 272static moduledata_t gif_mod = { 273 "if_gif", 274 gifmodevent, 275 0 276}; 277 278DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 279MODULE_VERSION(if_gif, 1); 280 281int 282gif_encapcheck(m, off, proto, arg) 283 const struct mbuf *m; 284 int off; 285 int proto; 286 void *arg; 287{ 288 struct ip ip; 289 struct gif_softc *sc; 290 291 sc = (struct gif_softc *)arg; 292 if (sc == NULL) 293 return 0; 294 295 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 296 return 0; 297 298 /* no physical address */ 299 if (!sc->gif_psrc || !sc->gif_pdst) 300 return 0; 301 302 switch (proto) { 303#ifdef INET 304 case IPPROTO_IPV4: 305 break; 306#endif 307#ifdef INET6 308 case IPPROTO_IPV6: 309 break; 310#endif 311 case IPPROTO_ETHERIP: 312 break; 313 314 default: 315 return 0; 316 } 317 318 /* Bail on short packets */ 319 if (m->m_pkthdr.len < sizeof(ip)) 320 return 0; 321 322 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 323 324 switch (ip.ip_v) { 325#ifdef INET 326 case 4: 327 if (sc->gif_psrc->sa_family != AF_INET || 328 sc->gif_pdst->sa_family != AF_INET) 329 return 0; 330 return gif_encapcheck4(m, off, proto, arg); 331#endif 332#ifdef INET6 333 case 6: 334 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 335 return 0; 336 if (sc->gif_psrc->sa_family != AF_INET6 || 337 sc->gif_pdst->sa_family != AF_INET6) 338 return 0; 339 return gif_encapcheck6(m, off, proto, arg); 340#endif 341 default: 342 return 0; 343 } 344} 345 346static void 347gif_start(struct ifnet *ifp) 348{ 349 struct gif_softc *sc; 350 struct mbuf *m; 351 352 sc = ifp->if_softc; 353 354 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 355 for (;;) { 356 IFQ_DEQUEUE(&ifp->if_snd, m); 357 if (m == 0) 358 break; 359 360 gif_output(ifp, m, sc->gif_pdst, NULL); 361 362 } 363 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 364 365 return; 366} 367 368int 369gif_output(ifp, m, dst, ro) 370 struct ifnet *ifp; 371 struct mbuf *m; 372 struct sockaddr *dst; 373 struct route *ro; 374{ 375 struct gif_softc *sc = ifp->if_softc; 376 struct m_tag *mtag; 377 int error = 0; 378 int gif_called; 379 u_int32_t af; 380 381#ifdef MAC 382 error = mac_ifnet_check_transmit(ifp, m); 383 if (error) { 384 m_freem(m); 385 goto end; 386 } 387#endif 388 389 /* 390 * gif may cause infinite recursion calls when misconfigured. 391 * We'll prevent this by detecting loops. 392 * 393 * High nesting level may cause stack exhaustion. 394 * We'll prevent this by introducing upper limit. 395 */ 396 gif_called = 1; 397 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 398 while (mtag != NULL) { 399 if (*(struct ifnet **)(mtag + 1) == ifp) { 400 log(LOG_NOTICE, 401 "gif_output: loop detected on %s\n", 402 (*(struct ifnet **)(mtag + 1))->if_xname); 403 m_freem(m); 404 error = EIO; /* is there better errno? */ 405 goto end; 406 } 407 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 408 gif_called++; 409 } 410 if (gif_called > V_max_gif_nesting) { 411 log(LOG_NOTICE, 412 "gif_output: recursively called too many times(%d)\n", 413 gif_called); 414 m_freem(m); 415 error = EIO; /* is there better errno? */ 416 goto end; 417 } 418 mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 419 M_NOWAIT); 420 if (mtag == NULL) { 421 m_freem(m); 422 error = ENOMEM; 423 goto end; 424 } 425 *(struct ifnet **)(mtag + 1) = ifp; 426 m_tag_prepend(m, mtag); 427 428 m->m_flags &= ~(M_BCAST|M_MCAST); 429 430 GIF_LOCK(sc); 431 432 if (!(ifp->if_flags & IFF_UP) || 433 sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 434 GIF_UNLOCK(sc); 435 m_freem(m); 436 error = ENETDOWN; 437 goto end; 438 } 439 440 /* BPF writes need to be handled specially. */ 441 if (dst->sa_family == AF_UNSPEC) { 442 bcopy(dst->sa_data, &af, sizeof(af)); 443 dst->sa_family = af; 444 } 445 446 af = dst->sa_family; 447 BPF_MTAP2(ifp, &af, sizeof(af), m); 448 ifp->if_opackets++; 449 ifp->if_obytes += m->m_pkthdr.len; 450 451 /* override to IPPROTO_ETHERIP for bridged traffic */ 452 if (ifp->if_bridge) 453 af = AF_LINK; 454 455 M_SETFIB(m, sc->gif_fibnum); 456 /* inner AF-specific encapsulation */ 457 458 /* XXX should we check if our outer source is legal? */ 459 460 /* dispatch to output logic based on outer AF */ 461 switch (sc->gif_psrc->sa_family) { 462#ifdef INET 463 case AF_INET: 464 error = in_gif_output(ifp, af, m); 465 break; 466#endif 467#ifdef INET6 468 case AF_INET6: 469 error = in6_gif_output(ifp, af, m); 470 break; 471#endif 472 default: 473 m_freem(m); 474 error = ENETDOWN; 475 } 476 477 GIF_UNLOCK(sc); 478 end: 479 if (error) 480 ifp->if_oerrors++; 481 return (error); 482} 483 484void 485gif_input(m, af, ifp) 486 struct mbuf *m; 487 int af; 488 struct ifnet *ifp; 489{ 490 int isr, n; 491 struct gif_softc *sc; 492 struct etherip_header *eip; 493 struct ether_header *eh; 494 struct ifnet *oldifp; 495 496 if (ifp == NULL) { 497 /* just in case */ 498 m_freem(m); 499 return; 500 } 501 sc = ifp->if_softc; 502 m->m_pkthdr.rcvif = ifp; 503 504#ifdef MAC 505 mac_ifnet_create_mbuf(ifp, m); 506#endif 507 508 if (bpf_peers_present(ifp->if_bpf)) { 509 u_int32_t af1 = af; 510 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 511 } 512 513 if (ng_gif_input_p != NULL) { 514 (*ng_gif_input_p)(ifp, &m, af); 515 if (m == NULL) 516 return; 517 } 518 519 /* 520 * Put the packet to the network layer input queue according to the 521 * specified address family. 522 * Note: older versions of gif_input directly called network layer 523 * input functions, e.g. ip6_input, here. We changed the policy to 524 * prevent too many recursive calls of such input functions, which 525 * might cause kernel panic. But the change may introduce another 526 * problem; if the input queue is full, packets are discarded. 527 * The kernel stack overflow really happened, and we believed 528 * queue-full rarely occurs, so we changed the policy. 529 */ 530 switch (af) { 531#ifdef INET 532 case AF_INET: 533 isr = NETISR_IP; 534 break; 535#endif 536#ifdef INET6 537 case AF_INET6: 538 isr = NETISR_IPV6; 539 break; 540#endif 541 case AF_LINK: 542 n = sizeof(struct etherip_header) + sizeof(struct ether_header); 543 if (n > m->m_len) { 544 m = m_pullup(m, n); 545 if (m == NULL) { 546 ifp->if_ierrors++; 547 return; 548 } 549 } 550 551 eip = mtod(m, struct etherip_header *); 552 /* 553 * GIF_ACCEPT_REVETHIP (enabled by default) intentionally 554 * accepts an EtherIP packet with revered version field in 555 * the header. This is a knob for backward compatibility 556 * with FreeBSD 7.2R or prior. 557 */ 558 if (sc->gif_options & GIF_ACCEPT_REVETHIP) { 559 if (eip->eip_resvl != ETHERIP_VERSION 560 && eip->eip_ver != ETHERIP_VERSION) { 561 /* discard unknown versions */ 562 m_freem(m); 563 return; 564 } 565 } else { 566 if (eip->eip_ver != ETHERIP_VERSION) { 567 /* discard unknown versions */ 568 m_freem(m); 569 return; 570 } 571 } 572 m_adj(m, sizeof(struct etherip_header)); 573 574 m->m_flags &= ~(M_BCAST|M_MCAST); 575 m->m_pkthdr.rcvif = ifp; 576 577 if (ifp->if_bridge) { 578 oldifp = ifp; 579 eh = mtod(m, struct ether_header *); 580 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 581 if (ETHER_IS_BROADCAST(eh->ether_dhost)) 582 m->m_flags |= M_BCAST; 583 else 584 m->m_flags |= M_MCAST; 585 ifp->if_imcasts++; 586 } 587 BRIDGE_INPUT(ifp, m); 588 589 if (m != NULL && ifp != oldifp) { 590 /* 591 * The bridge gave us back itself or one of the 592 * members for which the frame is addressed. 593 */ 594 ether_demux(ifp, m); 595 return; 596 } 597 } 598 if (m != NULL) 599 m_freem(m); 600 return; 601 602 default: 603 if (ng_gif_input_orphan_p != NULL) 604 (*ng_gif_input_orphan_p)(ifp, m, af); 605 else 606 m_freem(m); 607 return; 608 } 609 610 ifp->if_ipackets++; 611 ifp->if_ibytes += m->m_pkthdr.len; 612 netisr_dispatch(isr, m); 613} 614 615/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 616int 617gif_ioctl(ifp, cmd, data) 618 struct ifnet *ifp; 619 u_long cmd; 620 caddr_t data; 621{ 622 struct gif_softc *sc = ifp->if_softc; 623 struct ifreq *ifr = (struct ifreq*)data; 624 int error = 0, size; 625 u_int options; 626 struct sockaddr *dst, *src; 627#ifdef SIOCSIFMTU /* xxx */ 628 u_long mtu; 629#endif 630 631 switch (cmd) { 632 case SIOCSIFADDR: 633 ifp->if_flags |= IFF_UP; 634 break; 635 636 case SIOCSIFDSTADDR: 637 break; 638 639 case SIOCADDMULTI: 640 case SIOCDELMULTI: 641 break; 642 643#ifdef SIOCSIFMTU /* xxx */ 644 case SIOCGIFMTU: 645 break; 646 647 case SIOCSIFMTU: 648 mtu = ifr->ifr_mtu; 649 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 650 return (EINVAL); 651 ifp->if_mtu = mtu; 652 break; 653#endif /* SIOCSIFMTU */ 654 655#ifdef INET 656 case SIOCSIFPHYADDR: 657#endif 658#ifdef INET6 659 case SIOCSIFPHYADDR_IN6: 660#endif /* INET6 */ 661 case SIOCSLIFPHYADDR: 662 switch (cmd) { 663#ifdef INET 664 case SIOCSIFPHYADDR: 665 src = (struct sockaddr *) 666 &(((struct in_aliasreq *)data)->ifra_addr); 667 dst = (struct sockaddr *) 668 &(((struct in_aliasreq *)data)->ifra_dstaddr); 669 break; 670#endif 671#ifdef INET6 672 case SIOCSIFPHYADDR_IN6: 673 src = (struct sockaddr *) 674 &(((struct in6_aliasreq *)data)->ifra_addr); 675 dst = (struct sockaddr *) 676 &(((struct in6_aliasreq *)data)->ifra_dstaddr); 677 break; 678#endif 679 case SIOCSLIFPHYADDR: 680 src = (struct sockaddr *) 681 &(((struct if_laddrreq *)data)->addr); 682 dst = (struct sockaddr *) 683 &(((struct if_laddrreq *)data)->dstaddr); 684 break; 685 default: 686 return EINVAL; 687 } 688 689 /* sa_family must be equal */ 690 if (src->sa_family != dst->sa_family) 691 return EINVAL; 692 693 /* validate sa_len */ 694 switch (src->sa_family) { 695#ifdef INET 696 case AF_INET: 697 if (src->sa_len != sizeof(struct sockaddr_in)) 698 return EINVAL; 699 break; 700#endif 701#ifdef INET6 702 case AF_INET6: 703 if (src->sa_len != sizeof(struct sockaddr_in6)) 704 return EINVAL; 705 break; 706#endif 707 default: 708 return EAFNOSUPPORT; 709 } 710 switch (dst->sa_family) { 711#ifdef INET 712 case AF_INET: 713 if (dst->sa_len != sizeof(struct sockaddr_in)) 714 return EINVAL; 715 break; 716#endif 717#ifdef INET6 718 case AF_INET6: 719 if (dst->sa_len != sizeof(struct sockaddr_in6)) 720 return EINVAL; 721 break; 722#endif 723 default: 724 return EAFNOSUPPORT; 725 } 726 727 /* check sa_family looks sane for the cmd */ 728 switch (cmd) { 729 case SIOCSIFPHYADDR: 730 if (src->sa_family == AF_INET) 731 break; 732 return EAFNOSUPPORT; 733#ifdef INET6 734 case SIOCSIFPHYADDR_IN6: 735 if (src->sa_family == AF_INET6) 736 break; 737 return EAFNOSUPPORT; 738#endif /* INET6 */ 739 case SIOCSLIFPHYADDR: 740 /* checks done in the above */ 741 break; 742 } 743 744 error = gif_set_tunnel(GIF2IFP(sc), src, dst); 745 break; 746 747#ifdef SIOCDIFPHYADDR 748 case SIOCDIFPHYADDR: 749 gif_delete_tunnel(GIF2IFP(sc)); 750 break; 751#endif 752 753 case SIOCGIFPSRCADDR: 754#ifdef INET6 755 case SIOCGIFPSRCADDR_IN6: 756#endif /* INET6 */ 757 if (sc->gif_psrc == NULL) { 758 error = EADDRNOTAVAIL; 759 goto bad; 760 } 761 src = sc->gif_psrc; 762 switch (cmd) { 763#ifdef INET 764 case SIOCGIFPSRCADDR: 765 dst = &ifr->ifr_addr; 766 size = sizeof(ifr->ifr_addr); 767 break; 768#endif /* INET */ 769#ifdef INET6 770 case SIOCGIFPSRCADDR_IN6: 771 dst = (struct sockaddr *) 772 &(((struct in6_ifreq *)data)->ifr_addr); 773 size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 774 break; 775#endif /* INET6 */ 776 default: 777 error = EADDRNOTAVAIL; 778 goto bad; 779 } 780 if (src->sa_len > size) 781 return EINVAL; 782 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 783#ifdef INET6 784 if (dst->sa_family == AF_INET6) { 785 error = sa6_recoverscope((struct sockaddr_in6 *)dst); 786 if (error != 0) 787 return (error); 788 } 789#endif 790 break; 791 792 case SIOCGIFPDSTADDR: 793#ifdef INET6 794 case SIOCGIFPDSTADDR_IN6: 795#endif /* INET6 */ 796 if (sc->gif_pdst == NULL) { 797 error = EADDRNOTAVAIL; 798 goto bad; 799 } 800 src = sc->gif_pdst; 801 switch (cmd) { 802#ifdef INET 803 case SIOCGIFPDSTADDR: 804 dst = &ifr->ifr_addr; 805 size = sizeof(ifr->ifr_addr); 806 break; 807#endif /* INET */ 808#ifdef INET6 809 case SIOCGIFPDSTADDR_IN6: 810 dst = (struct sockaddr *) 811 &(((struct in6_ifreq *)data)->ifr_addr); 812 size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 813 break; 814#endif /* INET6 */ 815 default: 816 error = EADDRNOTAVAIL; 817 goto bad; 818 } 819 if (src->sa_len > size) 820 return EINVAL;
|
820 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 821#ifdef INET6 822 if (dst->sa_family == AF_INET6) { 823 error = sa6_recoverscope((struct sockaddr_in6 *)dst); 824 if (error != 0) 825 return (error); 826 } 827#endif 828 break; 829 830 case SIOCGLIFPHYADDR: 831 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 832 error = EADDRNOTAVAIL; 833 goto bad; 834 } 835 836 /* copy src */ 837 src = sc->gif_psrc; 838 dst = (struct sockaddr *) 839 &(((struct if_laddrreq *)data)->addr); 840 size = sizeof(((struct if_laddrreq *)data)->addr); 841 if (src->sa_len > size) 842 return EINVAL; 843 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 844 845 /* copy dst */ 846 src = sc->gif_pdst; 847 dst = (struct sockaddr *) 848 &(((struct if_laddrreq *)data)->dstaddr); 849 size = sizeof(((struct if_laddrreq *)data)->dstaddr); 850 if (src->sa_len > size) 851 return EINVAL; 852 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 853 break; 854 855 case SIOCSIFFLAGS: 856 /* if_ioctl() takes care of it */ 857 break; 858 859 case GIFGOPTS: 860 options = sc->gif_options; 861 error = copyout(&options, ifr->ifr_data, 862 sizeof(options)); 863 break; 864 865 case GIFSOPTS: 866 if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 867 break; 868 error = copyin(ifr->ifr_data, &options, sizeof(options)); 869 if (error) 870 break; 871 if (options & ~GIF_OPTMASK) 872 error = EINVAL; 873 else 874 sc->gif_options = options; 875 break; 876 877 default: 878 error = EINVAL; 879 break; 880 } 881 bad: 882 return error; 883} 884 885/* 886 * XXXRW: There's a general event-ordering issue here: the code to check 887 * if a given tunnel is already present happens before we perform a 888 * potentially blocking setup of the tunnel. This code needs to be 889 * re-ordered so that the check and replacement can be atomic using 890 * a mutex. 891 */ 892int 893gif_set_tunnel(ifp, src, dst) 894 struct ifnet *ifp; 895 struct sockaddr *src; 896 struct sockaddr *dst; 897{ 898 struct gif_softc *sc = ifp->if_softc; 899 struct gif_softc *sc2; 900 struct sockaddr *osrc, *odst, *sa; 901 int error = 0; 902 903 mtx_lock(&gif_mtx); 904 LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) { 905 if (sc2 == sc) 906 continue; 907 if (!sc2->gif_pdst || !sc2->gif_psrc) 908 continue; 909 if (sc2->gif_pdst->sa_family != dst->sa_family || 910 sc2->gif_pdst->sa_len != dst->sa_len || 911 sc2->gif_psrc->sa_family != src->sa_family || 912 sc2->gif_psrc->sa_len != src->sa_len) 913 continue; 914 915 /* 916 * Disallow parallel tunnels unless instructed 917 * otherwise. 918 */ 919 if (!V_parallel_tunnels && 920 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 921 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 922 error = EADDRNOTAVAIL; 923 mtx_unlock(&gif_mtx); 924 goto bad; 925 } 926 927 /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 928 } 929 mtx_unlock(&gif_mtx); 930 931 /* XXX we can detach from both, but be polite just in case */ 932 if (sc->gif_psrc) 933 switch (sc->gif_psrc->sa_family) { 934#ifdef INET 935 case AF_INET: 936 (void)in_gif_detach(sc); 937 break; 938#endif 939#ifdef INET6 940 case AF_INET6: 941 (void)in6_gif_detach(sc); 942 break; 943#endif 944 } 945 946 osrc = sc->gif_psrc; 947 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 948 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 949 sc->gif_psrc = sa; 950 951 odst = sc->gif_pdst; 952 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 953 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 954 sc->gif_pdst = sa; 955 956 switch (sc->gif_psrc->sa_family) { 957#ifdef INET 958 case AF_INET: 959 error = in_gif_attach(sc); 960 break; 961#endif 962#ifdef INET6 963 case AF_INET6: 964 /* 965 * Check validity of the scope zone ID of the addresses, and 966 * convert it into the kernel internal form if necessary. 967 */ 968 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 969 if (error != 0) 970 break; 971 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 972 if (error != 0) 973 break; 974 error = in6_gif_attach(sc); 975 break; 976#endif 977 } 978 if (error) { 979 /* rollback */ 980 free((caddr_t)sc->gif_psrc, M_IFADDR); 981 free((caddr_t)sc->gif_pdst, M_IFADDR); 982 sc->gif_psrc = osrc; 983 sc->gif_pdst = odst; 984 goto bad; 985 } 986 987 if (osrc) 988 free((caddr_t)osrc, M_IFADDR); 989 if (odst) 990 free((caddr_t)odst, M_IFADDR); 991 992 bad: 993 if (sc->gif_psrc && sc->gif_pdst) 994 ifp->if_drv_flags |= IFF_DRV_RUNNING; 995 else 996 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 997 998 return error; 999} 1000 1001void 1002gif_delete_tunnel(ifp) 1003 struct ifnet *ifp; 1004{ 1005 struct gif_softc *sc = ifp->if_softc; 1006 1007 if (sc->gif_psrc) { 1008 free((caddr_t)sc->gif_psrc, M_IFADDR); 1009 sc->gif_psrc = NULL; 1010 } 1011 if (sc->gif_pdst) { 1012 free((caddr_t)sc->gif_pdst, M_IFADDR); 1013 sc->gif_pdst = NULL; 1014 } 1015 /* it is safe to detach from both */ 1016#ifdef INET 1017 (void)in_gif_detach(sc); 1018#endif 1019#ifdef INET6 1020 (void)in6_gif_detach(sc); 1021#endif 1022 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1023}
| 827 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 828#ifdef INET6 829 if (dst->sa_family == AF_INET6) { 830 error = sa6_recoverscope((struct sockaddr_in6 *)dst); 831 if (error != 0) 832 return (error); 833 } 834#endif 835 break; 836 837 case SIOCGLIFPHYADDR: 838 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 839 error = EADDRNOTAVAIL; 840 goto bad; 841 } 842 843 /* copy src */ 844 src = sc->gif_psrc; 845 dst = (struct sockaddr *) 846 &(((struct if_laddrreq *)data)->addr); 847 size = sizeof(((struct if_laddrreq *)data)->addr); 848 if (src->sa_len > size) 849 return EINVAL; 850 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 851 852 /* copy dst */ 853 src = sc->gif_pdst; 854 dst = (struct sockaddr *) 855 &(((struct if_laddrreq *)data)->dstaddr); 856 size = sizeof(((struct if_laddrreq *)data)->dstaddr); 857 if (src->sa_len > size) 858 return EINVAL; 859 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 860 break; 861 862 case SIOCSIFFLAGS: 863 /* if_ioctl() takes care of it */ 864 break; 865 866 case GIFGOPTS: 867 options = sc->gif_options; 868 error = copyout(&options, ifr->ifr_data, 869 sizeof(options)); 870 break; 871 872 case GIFSOPTS: 873 if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 874 break; 875 error = copyin(ifr->ifr_data, &options, sizeof(options)); 876 if (error) 877 break; 878 if (options & ~GIF_OPTMASK) 879 error = EINVAL; 880 else 881 sc->gif_options = options; 882 break; 883 884 default: 885 error = EINVAL; 886 break; 887 } 888 bad: 889 return error; 890} 891 892/* 893 * XXXRW: There's a general event-ordering issue here: the code to check 894 * if a given tunnel is already present happens before we perform a 895 * potentially blocking setup of the tunnel. This code needs to be 896 * re-ordered so that the check and replacement can be atomic using 897 * a mutex. 898 */ 899int 900gif_set_tunnel(ifp, src, dst) 901 struct ifnet *ifp; 902 struct sockaddr *src; 903 struct sockaddr *dst; 904{ 905 struct gif_softc *sc = ifp->if_softc; 906 struct gif_softc *sc2; 907 struct sockaddr *osrc, *odst, *sa; 908 int error = 0; 909 910 mtx_lock(&gif_mtx); 911 LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) { 912 if (sc2 == sc) 913 continue; 914 if (!sc2->gif_pdst || !sc2->gif_psrc) 915 continue; 916 if (sc2->gif_pdst->sa_family != dst->sa_family || 917 sc2->gif_pdst->sa_len != dst->sa_len || 918 sc2->gif_psrc->sa_family != src->sa_family || 919 sc2->gif_psrc->sa_len != src->sa_len) 920 continue; 921 922 /* 923 * Disallow parallel tunnels unless instructed 924 * otherwise. 925 */ 926 if (!V_parallel_tunnels && 927 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 928 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 929 error = EADDRNOTAVAIL; 930 mtx_unlock(&gif_mtx); 931 goto bad; 932 } 933 934 /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 935 } 936 mtx_unlock(&gif_mtx); 937 938 /* XXX we can detach from both, but be polite just in case */ 939 if (sc->gif_psrc) 940 switch (sc->gif_psrc->sa_family) { 941#ifdef INET 942 case AF_INET: 943 (void)in_gif_detach(sc); 944 break; 945#endif 946#ifdef INET6 947 case AF_INET6: 948 (void)in6_gif_detach(sc); 949 break; 950#endif 951 } 952 953 osrc = sc->gif_psrc; 954 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 955 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 956 sc->gif_psrc = sa; 957 958 odst = sc->gif_pdst; 959 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 960 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 961 sc->gif_pdst = sa; 962 963 switch (sc->gif_psrc->sa_family) { 964#ifdef INET 965 case AF_INET: 966 error = in_gif_attach(sc); 967 break; 968#endif 969#ifdef INET6 970 case AF_INET6: 971 /* 972 * Check validity of the scope zone ID of the addresses, and 973 * convert it into the kernel internal form if necessary. 974 */ 975 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 976 if (error != 0) 977 break; 978 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 979 if (error != 0) 980 break; 981 error = in6_gif_attach(sc); 982 break; 983#endif 984 } 985 if (error) { 986 /* rollback */ 987 free((caddr_t)sc->gif_psrc, M_IFADDR); 988 free((caddr_t)sc->gif_pdst, M_IFADDR); 989 sc->gif_psrc = osrc; 990 sc->gif_pdst = odst; 991 goto bad; 992 } 993 994 if (osrc) 995 free((caddr_t)osrc, M_IFADDR); 996 if (odst) 997 free((caddr_t)odst, M_IFADDR); 998 999 bad: 1000 if (sc->gif_psrc && sc->gif_pdst) 1001 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1002 else 1003 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1004 1005 return error; 1006} 1007 1008void 1009gif_delete_tunnel(ifp) 1010 struct ifnet *ifp; 1011{ 1012 struct gif_softc *sc = ifp->if_softc; 1013 1014 if (sc->gif_psrc) { 1015 free((caddr_t)sc->gif_psrc, M_IFADDR); 1016 sc->gif_psrc = NULL; 1017 } 1018 if (sc->gif_pdst) { 1019 free((caddr_t)sc->gif_pdst, M_IFADDR); 1020 sc->gif_pdst = NULL; 1021 } 1022 /* it is safe to detach from both */ 1023#ifdef INET 1024 (void)in_gif_detach(sc); 1025#endif 1026#ifdef INET6 1027 (void)in6_gif_detach(sc); 1028#endif 1029 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1030}
|