if_gif.c revision 128209
1/* $FreeBSD: head/sys/net/if_gif.c 128209 2004-04-14 00:57:49Z brooks $ */ 2/* $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ */ 3 4/* 5 * Copyright (C) 1995, 1996, 1997, and 1998 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#include "opt_inet.h" 34#include "opt_inet6.h" 35#include "opt_mac.h" 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/kernel.h> 40#include <sys/mac.h> 41#include <sys/malloc.h> 42#include <sys/mbuf.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/protosw.h> 50#include <sys/conf.h> 51#include <machine/cpu.h> 52 53#include <net/if.h> 54#include <net/if_types.h> 55#include <net/netisr.h> 56#include <net/route.h> 57#include <net/bpf.h> 58 59#include <netinet/in.h> 60#include <netinet/in_systm.h> 61#include <netinet/ip.h> 62#ifdef INET 63#include <netinet/in_var.h> 64#include <netinet/in_gif.h> 65#include <netinet/ip_var.h> 66#endif /* INET */ 67 68#ifdef INET6 69#ifndef INET 70#include <netinet/in.h> 71#endif 72#include <netinet6/in6_var.h> 73#include <netinet/ip6.h> 74#include <netinet6/ip6_var.h> 75#include <netinet6/in6_gif.h> 76#include <netinet6/ip6protosw.h> 77#endif /* INET6 */ 78 79#include <netinet/ip_encap.h> 80#include <net/if_gif.h> 81 82#include <net/net_osdep.h> 83 84#define GIFNAME "gif" 85 86/* 87 * gif_mtx protects the global gif_softc_list. 88 * XXX: Per-softc locking is still required. 89 */ 90static struct mtx gif_mtx; 91static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 92static LIST_HEAD(, gif_softc) gif_softc_list; 93 94void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 95void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 96void (*ng_gif_attach_p)(struct ifnet *ifp); 97void (*ng_gif_detach_p)(struct ifnet *ifp); 98 99static int gif_clone_create(struct if_clone *, int); 100static void gif_clone_destroy(struct ifnet *); 101 102struct if_clone gif_cloner = IF_CLONE_INITIALIZER("gif", 103 gif_clone_create, gif_clone_destroy, 0, IF_MAXUNIT); 104 105static int gifmodevent(module_t, int, void *); 106 107SYSCTL_DECL(_net_link); 108SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 109 "Generic Tunnel Interface"); 110#ifndef MAX_GIF_NEST 111/* 112 * This macro controls the default upper limitation on nesting of gif tunnels. 113 * Since, setting a large value to this macro with a careless configuration 114 * may introduce system crash, we don't allow any nestings by default. 115 * If you need to configure nested gif tunnels, you can define this macro 116 * in your kernel configuration file. However, if you do so, please be 117 * careful to configure the tunnels so that it won't make a loop. 118 */ 119#define MAX_GIF_NEST 1 120#endif 121static int max_gif_nesting = MAX_GIF_NEST; 122SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 123 &max_gif_nesting, 0, "Max nested tunnels"); 124 125/* 126 * By default, we disallow creation of multiple tunnels between the same 127 * pair of addresses. Some applications require this functionality so 128 * we allow control over this check here. 129 */ 130#ifdef XBONEHACK 131static int parallel_tunnels = 1; 132#else 133static int parallel_tunnels = 0; 134#endif 135SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 136 ¶llel_tunnels, 0, "Allow parallel tunnels?"); 137 138static int 139gif_clone_create(ifc, unit) 140 struct if_clone *ifc; 141 int unit; 142{ 143 struct gif_softc *sc; 144 145 sc = malloc (sizeof(struct gif_softc), M_GIF, M_WAITOK); 146 bzero(sc, sizeof(struct gif_softc)); 147 148 sc->gif_if.if_softc = sc; 149 if_initname(&sc->gif_if, ifc->ifc_name, unit); 150 151 gifattach0(sc); 152 153 mtx_lock(&gif_mtx); 154 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list); 155 mtx_unlock(&gif_mtx); 156 return (0); 157} 158 159void 160gifattach0(sc) 161 struct gif_softc *sc; 162{ 163 164 sc->encap_cookie4 = sc->encap_cookie6 = NULL; 165 166 sc->gif_if.if_addrlen = 0; 167 sc->gif_if.if_mtu = GIF_MTU; 168 sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 169#if 0 170 /* turn off ingress filter */ 171 sc->gif_if.if_flags |= IFF_LINK2; 172#endif 173 sc->gif_if.if_ioctl = gif_ioctl; 174 sc->gif_if.if_output = gif_output; 175 sc->gif_if.if_type = IFT_GIF; 176 sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN; 177 if_attach(&sc->gif_if); 178 bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int)); 179 if (ng_gif_attach_p != NULL) 180 (*ng_gif_attach_p)(&sc->gif_if); 181} 182 183static void 184gif_destroy(struct gif_softc *sc) 185{ 186 struct ifnet *ifp = &sc->gif_if; 187 int err; 188 189 gif_delete_tunnel(ifp); 190#ifdef INET6 191 if (sc->encap_cookie6 != NULL) { 192 err = encap_detach(sc->encap_cookie6); 193 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 194 } 195#endif 196#ifdef INET 197 if (sc->encap_cookie4 != NULL) { 198 err = encap_detach(sc->encap_cookie4); 199 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 200 } 201#endif 202 203 if (ng_gif_detach_p != NULL) 204 (*ng_gif_detach_p)(ifp); 205 bpfdetach(ifp); 206 if_detach(ifp); 207 208 free(sc, M_GIF); 209} 210 211static void 212gif_clone_destroy(ifp) 213 struct ifnet *ifp; 214{ 215 struct gif_softc *sc = ifp->if_softc; 216 217 mtx_lock(&gif_mtx); 218 LIST_REMOVE(sc, gif_list); 219 mtx_unlock(&gif_mtx); 220 gif_destroy(sc); 221} 222 223static int 224gifmodevent(mod, type, data) 225 module_t mod; 226 int type; 227 void *data; 228{ 229 struct gif_softc *sc; 230 231 switch (type) { 232 case MOD_LOAD: 233 mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 234 LIST_INIT(&gif_softc_list); 235 if_clone_attach(&gif_cloner); 236 237#ifdef INET6 238 ip6_gif_hlim = GIF_HLIM; 239#endif 240 241 break; 242 case MOD_UNLOAD: 243 if_clone_detach(&gif_cloner); 244 245 mtx_lock(&gif_mtx); 246 while ((sc = LIST_FIRST(&gif_softc_list)) != NULL) { 247 LIST_REMOVE(sc, gif_list); 248 mtx_unlock(&gif_mtx); 249 gif_destroy(sc); 250 mtx_lock(&gif_mtx); 251 } 252 mtx_unlock(&gif_mtx); 253 mtx_destroy(&gif_mtx); 254#ifdef INET6 255 ip6_gif_hlim = 0; 256#endif 257 break; 258 } 259 return 0; 260} 261 262static moduledata_t gif_mod = { 263 "if_gif", 264 gifmodevent, 265 0 266}; 267 268DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 269MODULE_VERSION(if_gif, 1); 270 271int 272gif_encapcheck(m, off, proto, arg) 273 const struct mbuf *m; 274 int off; 275 int proto; 276 void *arg; 277{ 278 struct ip ip; 279 struct gif_softc *sc; 280 281 sc = (struct gif_softc *)arg; 282 if (sc == NULL) 283 return 0; 284 285 if ((sc->gif_if.if_flags & IFF_UP) == 0) 286 return 0; 287 288 /* no physical address */ 289 if (!sc->gif_psrc || !sc->gif_pdst) 290 return 0; 291 292 switch (proto) { 293#ifdef INET 294 case IPPROTO_IPV4: 295 break; 296#endif 297#ifdef INET6 298 case IPPROTO_IPV6: 299 break; 300#endif 301 default: 302 return 0; 303 } 304 305 /* Bail on short packets */ 306 if (m->m_pkthdr.len < sizeof(ip)) 307 return 0; 308 309 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 310 311 switch (ip.ip_v) { 312#ifdef INET 313 case 4: 314 if (sc->gif_psrc->sa_family != AF_INET || 315 sc->gif_pdst->sa_family != AF_INET) 316 return 0; 317 return gif_encapcheck4(m, off, proto, arg); 318#endif 319#ifdef INET6 320 case 6: 321 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 322 return 0; 323 if (sc->gif_psrc->sa_family != AF_INET6 || 324 sc->gif_pdst->sa_family != AF_INET6) 325 return 0; 326 return gif_encapcheck6(m, off, proto, arg); 327#endif 328 default: 329 return 0; 330 } 331} 332 333int 334gif_output(ifp, m, dst, rt) 335 struct ifnet *ifp; 336 struct mbuf *m; 337 struct sockaddr *dst; 338 struct rtentry *rt; /* added in net2 */ 339{ 340 struct gif_softc *sc = (struct gif_softc*)ifp; 341 struct m_tag *mtag; 342 int error = 0; 343 int gif_called; 344 345#ifdef MAC 346 error = mac_check_ifnet_transmit(ifp, m); 347 if (error) { 348 m_freem(m); 349 goto end; 350 } 351#endif 352 353 /* 354 * gif may cause infinite recursion calls when misconfigured. 355 * We'll prevent this by detecting loops. 356 * 357 * High nesting level may cause stack exhaustion. 358 * We'll prevent this by introducing upper limit. 359 */ 360 gif_called = 1; 361 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 362 while (mtag != NULL) { 363 if (*(struct ifnet **)(mtag + 1) == ifp) { 364 log(LOG_NOTICE, 365 "gif_output: loop detected on %s\n", 366 (*(struct ifnet **)(mtag + 1))->if_xname); 367 m_freem(m); 368 error = EIO; /* is there better errno? */ 369 goto end; 370 } 371 mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 372 gif_called++; 373 } 374 if (gif_called > max_gif_nesting) { 375 log(LOG_NOTICE, 376 "gif_output: recursively called too many times(%d)\n", 377 gif_called); 378 m_freem(m); 379 error = EIO; /* is there better errno? */ 380 goto end; 381 } 382 mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 383 M_NOWAIT); 384 if (mtag == NULL) { 385 m_freem(m); 386 error = ENOMEM; 387 goto end; 388 } 389 *(struct ifnet **)(mtag + 1) = ifp; 390 m_tag_prepend(m, mtag); 391 392 m->m_flags &= ~(M_BCAST|M_MCAST); 393 if (!(ifp->if_flags & IFF_UP) || 394 sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 395 m_freem(m); 396 error = ENETDOWN; 397 goto end; 398 } 399 400 if (ifp->if_bpf) { 401 u_int32_t af = dst->sa_family; 402 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 403 } 404 ifp->if_opackets++; 405 ifp->if_obytes += m->m_pkthdr.len; 406 407 /* inner AF-specific encapsulation */ 408 409 /* XXX should we check if our outer source is legal? */ 410 411 /* dispatch to output logic based on outer AF */ 412 switch (sc->gif_psrc->sa_family) { 413#ifdef INET 414 case AF_INET: 415 error = in_gif_output(ifp, dst->sa_family, m); 416 break; 417#endif 418#ifdef INET6 419 case AF_INET6: 420 error = in6_gif_output(ifp, dst->sa_family, m); 421 break; 422#endif 423 default: 424 m_freem(m); 425 error = ENETDOWN; 426 goto end; 427 } 428 429 end: 430 if (error) 431 ifp->if_oerrors++; 432 return error; 433} 434 435void 436gif_input(m, af, ifp) 437 struct mbuf *m; 438 int af; 439 struct ifnet *ifp; 440{ 441 int isr; 442 443 if (ifp == NULL) { 444 /* just in case */ 445 m_freem(m); 446 return; 447 } 448 449 m->m_pkthdr.rcvif = ifp; 450 451#ifdef MAC 452 mac_create_mbuf_from_ifnet(ifp, m); 453#endif 454 455 if (ifp->if_bpf) { 456 u_int32_t af1 = af; 457 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 458 } 459 460 if (ng_gif_input_p != NULL) { 461 (*ng_gif_input_p)(ifp, &m, af); 462 if (m == NULL) 463 return; 464 } 465 466 /* 467 * Put the packet to the network layer input queue according to the 468 * specified address family. 469 * Note: older versions of gif_input directly called network layer 470 * input functions, e.g. ip6_input, here. We changed the policy to 471 * prevent too many recursive calls of such input functions, which 472 * might cause kernel panic. But the change may introduce another 473 * problem; if the input queue is full, packets are discarded. 474 * The kernel stack overflow really happened, and we believed 475 * queue-full rarely occurs, so we changed the policy. 476 */ 477 switch (af) { 478#ifdef INET 479 case AF_INET: 480 isr = NETISR_IP; 481 break; 482#endif 483#ifdef INET6 484 case AF_INET6: 485 isr = NETISR_IPV6; 486 break; 487#endif 488 default: 489 if (ng_gif_input_orphan_p != NULL) 490 (*ng_gif_input_orphan_p)(ifp, m, af); 491 else 492 m_freem(m); 493 return; 494 } 495 496 ifp->if_ipackets++; 497 ifp->if_ibytes += m->m_pkthdr.len; 498 netisr_dispatch(isr, m); 499} 500 501/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 502int 503gif_ioctl(ifp, cmd, data) 504 struct ifnet *ifp; 505 u_long cmd; 506 caddr_t data; 507{ 508 struct gif_softc *sc = (struct gif_softc*)ifp; 509 struct ifreq *ifr = (struct ifreq*)data; 510 int error = 0, size; 511 struct sockaddr *dst, *src; 512#ifdef SIOCSIFMTU /* xxx */ 513 u_long mtu; 514#endif 515 516 switch (cmd) { 517 case SIOCSIFADDR: 518 ifp->if_flags |= IFF_UP; 519 break; 520 521 case SIOCSIFDSTADDR: 522 break; 523 524 case SIOCADDMULTI: 525 case SIOCDELMULTI: 526 break; 527 528#ifdef SIOCSIFMTU /* xxx */ 529 case SIOCGIFMTU: 530 break; 531 532 case SIOCSIFMTU: 533 mtu = ifr->ifr_mtu; 534 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 535 return (EINVAL); 536 ifp->if_mtu = mtu; 537 break; 538#endif /* SIOCSIFMTU */ 539 540#ifdef INET 541 case SIOCSIFPHYADDR: 542#endif 543#ifdef INET6 544 case SIOCSIFPHYADDR_IN6: 545#endif /* INET6 */ 546 case SIOCSLIFPHYADDR: 547 switch (cmd) { 548#ifdef INET 549 case SIOCSIFPHYADDR: 550 src = (struct sockaddr *) 551 &(((struct in_aliasreq *)data)->ifra_addr); 552 dst = (struct sockaddr *) 553 &(((struct in_aliasreq *)data)->ifra_dstaddr); 554 break; 555#endif 556#ifdef INET6 557 case SIOCSIFPHYADDR_IN6: 558 src = (struct sockaddr *) 559 &(((struct in6_aliasreq *)data)->ifra_addr); 560 dst = (struct sockaddr *) 561 &(((struct in6_aliasreq *)data)->ifra_dstaddr); 562 break; 563#endif 564 case SIOCSLIFPHYADDR: 565 src = (struct sockaddr *) 566 &(((struct if_laddrreq *)data)->addr); 567 dst = (struct sockaddr *) 568 &(((struct if_laddrreq *)data)->dstaddr); 569 break; 570 default: 571 return EINVAL; 572 } 573 574 /* sa_family must be equal */ 575 if (src->sa_family != dst->sa_family) 576 return EINVAL; 577 578 /* validate sa_len */ 579 switch (src->sa_family) { 580#ifdef INET 581 case AF_INET: 582 if (src->sa_len != sizeof(struct sockaddr_in)) 583 return EINVAL; 584 break; 585#endif 586#ifdef INET6 587 case AF_INET6: 588 if (src->sa_len != sizeof(struct sockaddr_in6)) 589 return EINVAL; 590 break; 591#endif 592 default: 593 return EAFNOSUPPORT; 594 } 595 switch (dst->sa_family) { 596#ifdef INET 597 case AF_INET: 598 if (dst->sa_len != sizeof(struct sockaddr_in)) 599 return EINVAL; 600 break; 601#endif 602#ifdef INET6 603 case AF_INET6: 604 if (dst->sa_len != sizeof(struct sockaddr_in6)) 605 return EINVAL; 606 break; 607#endif 608 default: 609 return EAFNOSUPPORT; 610 } 611 612 /* check sa_family looks sane for the cmd */ 613 switch (cmd) { 614 case SIOCSIFPHYADDR: 615 if (src->sa_family == AF_INET) 616 break; 617 return EAFNOSUPPORT; 618#ifdef INET6 619 case SIOCSIFPHYADDR_IN6: 620 if (src->sa_family == AF_INET6) 621 break; 622 return EAFNOSUPPORT; 623#endif /* INET6 */ 624 case SIOCSLIFPHYADDR: 625 /* checks done in the above */ 626 break; 627 } 628 629 error = gif_set_tunnel(&sc->gif_if, src, dst); 630 break; 631 632#ifdef SIOCDIFPHYADDR 633 case SIOCDIFPHYADDR: 634 gif_delete_tunnel(&sc->gif_if); 635 break; 636#endif 637 638 case SIOCGIFPSRCADDR: 639#ifdef INET6 640 case SIOCGIFPSRCADDR_IN6: 641#endif /* INET6 */ 642 if (sc->gif_psrc == NULL) { 643 error = EADDRNOTAVAIL; 644 goto bad; 645 } 646 src = sc->gif_psrc; 647 switch (cmd) { 648#ifdef INET 649 case SIOCGIFPSRCADDR: 650 dst = &ifr->ifr_addr; 651 size = sizeof(ifr->ifr_addr); 652 break; 653#endif /* INET */ 654#ifdef INET6 655 case SIOCGIFPSRCADDR_IN6: 656 dst = (struct sockaddr *) 657 &(((struct in6_ifreq *)data)->ifr_addr); 658 size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 659 break; 660#endif /* INET6 */ 661 default: 662 error = EADDRNOTAVAIL; 663 goto bad; 664 } 665 if (src->sa_len > size) 666 return EINVAL; 667 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 668 break; 669 670 case SIOCGIFPDSTADDR: 671#ifdef INET6 672 case SIOCGIFPDSTADDR_IN6: 673#endif /* INET6 */ 674 if (sc->gif_pdst == NULL) { 675 error = EADDRNOTAVAIL; 676 goto bad; 677 } 678 src = sc->gif_pdst; 679 switch (cmd) { 680#ifdef INET 681 case SIOCGIFPDSTADDR: 682 dst = &ifr->ifr_addr; 683 size = sizeof(ifr->ifr_addr); 684 break; 685#endif /* INET */ 686#ifdef INET6 687 case SIOCGIFPDSTADDR_IN6: 688 dst = (struct sockaddr *) 689 &(((struct in6_ifreq *)data)->ifr_addr); 690 size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 691 break; 692#endif /* INET6 */ 693 default: 694 error = EADDRNOTAVAIL; 695 goto bad; 696 } 697 if (src->sa_len > size) 698 return EINVAL; 699 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 700 break; 701 702 case SIOCGLIFPHYADDR: 703 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 704 error = EADDRNOTAVAIL; 705 goto bad; 706 } 707 708 /* copy src */ 709 src = sc->gif_psrc; 710 dst = (struct sockaddr *) 711 &(((struct if_laddrreq *)data)->addr); 712 size = sizeof(((struct if_laddrreq *)data)->addr); 713 if (src->sa_len > size) 714 return EINVAL; 715 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 716 717 /* copy dst */ 718 src = sc->gif_pdst; 719 dst = (struct sockaddr *) 720 &(((struct if_laddrreq *)data)->dstaddr); 721 size = sizeof(((struct if_laddrreq *)data)->dstaddr); 722 if (src->sa_len > size) 723 return EINVAL; 724 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 725 break; 726 727 case SIOCSIFFLAGS: 728 /* if_ioctl() takes care of it */ 729 break; 730 731 default: 732 error = EINVAL; 733 break; 734 } 735 bad: 736 return error; 737} 738 739/* 740 * XXXRW: There's a general event-ordering issue here: the code to check 741 * if a given tunnel is already present happens before we perform a 742 * potentially blocking setup of the tunnel. This code needs to be 743 * re-ordered so that the check and replacement can be atomic using 744 * a mutex. 745 */ 746int 747gif_set_tunnel(ifp, src, dst) 748 struct ifnet *ifp; 749 struct sockaddr *src; 750 struct sockaddr *dst; 751{ 752 struct gif_softc *sc = (struct gif_softc *)ifp; 753 struct gif_softc *sc2; 754 struct sockaddr *osrc, *odst, *sa; 755 int s; 756 int error = 0; 757 758 s = splnet(); 759 760 mtx_lock(&gif_mtx); 761 LIST_FOREACH(sc2, &gif_softc_list, gif_list) { 762 if (sc2 == sc) 763 continue; 764 if (!sc2->gif_pdst || !sc2->gif_psrc) 765 continue; 766 if (sc2->gif_pdst->sa_family != dst->sa_family || 767 sc2->gif_pdst->sa_len != dst->sa_len || 768 sc2->gif_psrc->sa_family != src->sa_family || 769 sc2->gif_psrc->sa_len != src->sa_len) 770 continue; 771 772 /* 773 * Disallow parallel tunnels unless instructed 774 * otherwise. 775 */ 776 if (!parallel_tunnels && 777 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 778 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 779 error = EADDRNOTAVAIL; 780 mtx_unlock(&gif_mtx); 781 goto bad; 782 } 783 784 /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 785 } 786 mtx_unlock(&gif_mtx); 787 788 /* XXX we can detach from both, but be polite just in case */ 789 if (sc->gif_psrc) 790 switch (sc->gif_psrc->sa_family) { 791#ifdef INET 792 case AF_INET: 793 (void)in_gif_detach(sc); 794 break; 795#endif 796#ifdef INET6 797 case AF_INET6: 798 (void)in6_gif_detach(sc); 799 break; 800#endif 801 } 802 803 osrc = sc->gif_psrc; 804 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 805 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 806 sc->gif_psrc = sa; 807 808 odst = sc->gif_pdst; 809 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 810 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 811 sc->gif_pdst = sa; 812 813 switch (sc->gif_psrc->sa_family) { 814#ifdef INET 815 case AF_INET: 816 error = in_gif_attach(sc); 817 break; 818#endif 819#ifdef INET6 820 case AF_INET6: 821 error = in6_gif_attach(sc); 822 break; 823#endif 824 } 825 if (error) { 826 /* rollback */ 827 free((caddr_t)sc->gif_psrc, M_IFADDR); 828 free((caddr_t)sc->gif_pdst, M_IFADDR); 829 sc->gif_psrc = osrc; 830 sc->gif_pdst = odst; 831 goto bad; 832 } 833 834 if (osrc) 835 free((caddr_t)osrc, M_IFADDR); 836 if (odst) 837 free((caddr_t)odst, M_IFADDR); 838 839 if (sc->gif_psrc && sc->gif_pdst) 840 ifp->if_flags |= IFF_RUNNING; 841 else 842 ifp->if_flags &= ~IFF_RUNNING; 843 splx(s); 844 845 return 0; 846 847 bad: 848 if (sc->gif_psrc && sc->gif_pdst) 849 ifp->if_flags |= IFF_RUNNING; 850 else 851 ifp->if_flags &= ~IFF_RUNNING; 852 splx(s); 853 854 return error; 855} 856 857void 858gif_delete_tunnel(ifp) 859 struct ifnet *ifp; 860{ 861 struct gif_softc *sc = (struct gif_softc *)ifp; 862 int s; 863 864 s = splnet(); 865 866 if (sc->gif_psrc) { 867 free((caddr_t)sc->gif_psrc, M_IFADDR); 868 sc->gif_psrc = NULL; 869 } 870 if (sc->gif_pdst) { 871 free((caddr_t)sc->gif_pdst, M_IFADDR); 872 sc->gif_pdst = NULL; 873 } 874 /* it is safe to detach from both */ 875#ifdef INET 876 (void)in_gif_detach(sc); 877#endif 878#ifdef INET6 879 (void)in6_gif_detach(sc); 880#endif 881 882 if (sc->gif_psrc && sc->gif_pdst) 883 ifp->if_flags |= IFF_RUNNING; 884 else 885 ifp->if_flags &= ~IFF_RUNNING; 886 splx(s); 887} 888