1 /* 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: src/sys/net/if_gif.c,v 1.4.2.15 2002/11/08 16:57:13 ume Exp $ 30 * $DragonFly: src/sys/net/gif/if_gif.c,v 1.21 2008/05/14 11:59:23 sephe Exp $ 31 * $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ 32 */ 33 34 #include "opt_inet.h" 35 #include "opt_inet6.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/bus.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 <sys/thread2.h> 52 53 #include <machine/cpu.h> 54 55 #include <net/if.h> 56 #include <net/if_types.h> 57 #include <net/ifq_var.h> 58 #include <net/netisr.h> 59 #include <net/route.h> 60 #include <net/bpf.h> 61 #include <net/if_clone.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/in6_gif.h> 80 #include <netinet6/ip6protosw.h> 81 #endif /* INET6 */ 82 83 #include <netinet/ip_encap.h> 84 #include "if_gif.h" 85 86 #include <net/net_osdep.h> 87 88 #define GIFNAME "gif" 89 90 static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 91 LIST_HEAD(, gif_softc) gif_softc_list; 92 93 int gif_clone_create (struct if_clone *, int, caddr_t); 94 int gif_clone_destroy (struct ifnet *); 95 96 struct if_clone gif_cloner = IF_CLONE_INITIALIZER("gif", gif_clone_create, 97 gif_clone_destroy, 0, IF_MAXUNIT); 98 99 static int gifmodevent (module_t, int, void *); 100 static void gif_clear_cache(struct gif_softc *sc); 101 102 SYSCTL_DECL(_net_link); 103 SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 104 "Generic Tunnel Interface"); 105 #ifndef MAX_GIF_NEST 106 /* 107 * This macro controls the default upper limitation on nesting of gif tunnels. 108 * Since, setting a large value to this macro with a careless configuration 109 * may introduce system crash, we don't allow any nestings by default. 110 * If you need to configure nested gif tunnels, you can define this macro 111 * in your kernel configuration file. However, if you do so, please be 112 * careful to configure the tunnels so that it won't make a loop. 113 */ 114 #define MAX_GIF_NEST 1 115 #endif 116 static int max_gif_nesting = MAX_GIF_NEST; 117 SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 118 &max_gif_nesting, 0, "Max nested tunnels"); 119 120 /* 121 * By default, we disallow creation of multiple tunnels between the same 122 * pair of addresses. Some applications require this functionality so 123 * we allow control over this check here. 124 */ 125 #ifdef XBONEHACK 126 static int parallel_tunnels = 1; 127 #else 128 static int parallel_tunnels = 0; 129 #endif 130 SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 131 ¶llel_tunnels, 0, "Allow parallel tunnels?"); 132 133 int 134 gif_clone_create(struct if_clone *ifc, int unit, caddr_t params) 135 { 136 struct gif_softc *sc; 137 138 sc = kmalloc (sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 139 140 sc->gif_if.if_softc = sc; 141 if_initname(&(sc->gif_if), GIFNAME, unit); 142 143 gifattach0(sc); 144 145 LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list); 146 return (0); 147 } 148 149 void 150 gifattach0(struct gif_softc *sc) 151 { 152 153 sc->encap_cookie4 = sc->encap_cookie6 = NULL; 154 155 sc->gif_if.if_addrlen = 0; 156 sc->gif_if.if_mtu = GIF_MTU; 157 sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 158 #if 0 159 /* turn off ingress filter */ 160 sc->gif_if.if_flags |= IFF_LINK2; 161 #endif 162 sc->gif_if.if_ioctl = gif_ioctl; 163 sc->gif_if.if_output = gif_output; 164 sc->gif_if.if_type = IFT_GIF; 165 ifq_set_maxlen(&sc->gif_if.if_snd, IFQ_MAXLEN); 166 if_attach(&sc->gif_if, NULL); 167 bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int)); 168 } 169 170 int 171 gif_clone_destroy(struct ifnet *ifp) 172 { 173 struct gif_softc *sc = ifp->if_softc; 174 int err; 175 176 gif_delete_tunnel(&sc->gif_if); 177 LIST_REMOVE(sc, gif_list); 178 #ifdef INET6 179 if (sc->encap_cookie6 != NULL) { 180 err = encap_detach(sc->encap_cookie6); 181 KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 182 } 183 #endif 184 #ifdef INET 185 if (sc->encap_cookie4 != NULL) { 186 err = encap_detach(sc->encap_cookie4); 187 KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 188 } 189 #endif 190 gif_clear_cache(sc); 191 192 bpfdetach(ifp); 193 if_detach(ifp); 194 195 kfree(sc, M_GIF); 196 197 return 0; 198 } 199 200 static void 201 gif_clear_cache(struct gif_softc *sc) 202 { 203 int n; 204 205 for (n = 0; n < ncpus; ++n) { 206 if (sc->gif_ro[n].ro_rt) { 207 RTFREE(sc->gif_ro[n].ro_rt); 208 sc->gif_ro[n].ro_rt = NULL; 209 } 210 #ifdef INET6 211 if (sc->gif_ro6[n].ro_rt) { 212 RTFREE(sc->gif_ro6[n].ro_rt); 213 sc->gif_ro6[n].ro_rt = NULL; 214 } 215 #endif 216 } 217 } 218 219 static int 220 gifmodevent(module_t mod, int type, void *data) 221 { 222 223 switch (type) { 224 case MOD_LOAD: 225 LIST_INIT(&gif_softc_list); 226 if_clone_attach(&gif_cloner); 227 228 #ifdef INET6 229 ip6_gif_hlim = GIF_HLIM; 230 #endif 231 232 break; 233 case MOD_UNLOAD: 234 if_clone_detach(&gif_cloner); 235 236 while (!LIST_EMPTY(&gif_softc_list)) 237 gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if); 238 239 #ifdef INET6 240 ip6_gif_hlim = 0; 241 #endif 242 break; 243 } 244 return 0; 245 } 246 247 static moduledata_t gif_mod = { 248 "if_gif", 249 gifmodevent, 250 0 251 }; 252 253 DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 254 255 int 256 gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg) 257 { 258 struct ip ip; 259 struct gif_softc *sc; 260 261 sc = (struct gif_softc *)arg; 262 if (sc == NULL) 263 return 0; 264 265 if ((sc->gif_if.if_flags & IFF_UP) == 0) 266 return 0; 267 268 /* no physical address */ 269 if (!sc->gif_psrc || !sc->gif_pdst) 270 return 0; 271 272 switch (proto) { 273 #ifdef INET 274 case IPPROTO_IPV4: 275 break; 276 #endif 277 #ifdef INET6 278 case IPPROTO_IPV6: 279 break; 280 #endif 281 default: 282 return 0; 283 } 284 285 /* Bail on short packets */ 286 if (m->m_pkthdr.len < sizeof(ip)) 287 return 0; 288 289 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 290 291 switch (ip.ip_v) { 292 #ifdef INET 293 case 4: 294 if (sc->gif_psrc->sa_family != AF_INET || 295 sc->gif_pdst->sa_family != AF_INET) 296 return 0; 297 return gif_encapcheck4(m, off, proto, arg); 298 #endif 299 #ifdef INET6 300 case 6: 301 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 302 return 0; 303 if (sc->gif_psrc->sa_family != AF_INET6 || 304 sc->gif_pdst->sa_family != AF_INET6) 305 return 0; 306 return gif_encapcheck6(m, off, proto, arg); 307 #endif 308 default: 309 return 0; 310 } 311 } 312 313 /* 314 * Parameters: 315 * rt: added in net2 316 */ 317 static int 318 gif_output_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 319 struct rtentry *rt) 320 { 321 struct gif_softc *sc = (struct gif_softc*)ifp; 322 int error = 0; 323 static int called = 0; /* XXX: MUTEX */ 324 325 /* 326 * gif may cause infinite recursion calls when misconfigured. 327 * We'll prevent this by introducing upper limit. 328 * XXX: this mechanism may introduce another problem about 329 * mutual exclusion of the variable CALLED, especially if we 330 * use kernel thread. 331 */ 332 if (++called > max_gif_nesting) { 333 log(LOG_NOTICE, 334 "gif_output: recursively called too many times(%d)\n", 335 called); 336 m_freem(m); 337 error = EIO; /* is there better errno? */ 338 goto end; 339 } 340 341 m->m_flags &= ~(M_BCAST|M_MCAST); 342 if (!(ifp->if_flags & IFF_UP) || 343 sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 344 m_freem(m); 345 error = ENETDOWN; 346 goto end; 347 } 348 349 if (ifp->if_bpf) { 350 bpf_gettoken(); 351 if (ifp->if_bpf) { 352 /* 353 * We need to prepend the address family as 354 * a four byte field. 355 */ 356 uint32_t af = dst->sa_family; 357 358 bpf_ptap(ifp->if_bpf, m, &af, sizeof(af)); 359 } 360 bpf_reltoken(); 361 } 362 IFNET_STAT_INC(ifp, opackets, 1); 363 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len); 364 365 /* inner AF-specific encapsulation */ 366 367 /* XXX should we check if our outer source is legal? */ 368 369 /* dispatch to output logic based on outer AF */ 370 switch (sc->gif_psrc->sa_family) { 371 #ifdef INET 372 case AF_INET: 373 error = in_gif_output(ifp, dst->sa_family, m); 374 break; 375 #endif 376 #ifdef INET6 377 case AF_INET6: 378 error = in6_gif_output(ifp, dst->sa_family, m); 379 break; 380 #endif 381 default: 382 m_freem(m); 383 error = ENETDOWN; 384 goto end; 385 } 386 387 end: 388 called = 0; /* reset recursion counter */ 389 if (error) 390 IFNET_STAT_INC(ifp, oerrors, 1); 391 return error; 392 } 393 394 int 395 gif_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 396 struct rtentry *rt) 397 { 398 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd); 399 int error; 400 401 ifsq_serialize_hw(ifsq); 402 error = gif_output_serialized(ifp, m, dst, rt); 403 ifsq_deserialize_hw(ifsq); 404 return error; 405 } 406 407 void 408 gif_input(struct mbuf *m, int af, struct ifnet *ifp) 409 { 410 int isr; 411 412 if (ifp == NULL) { 413 /* just in case */ 414 m_freem(m); 415 return; 416 } 417 418 m->m_pkthdr.rcvif = ifp; 419 420 if (ifp->if_bpf) { 421 bpf_gettoken(); 422 if (ifp->if_bpf) { 423 /* 424 * We need to prepend the address family as 425 * a four byte field. 426 */ 427 uint32_t af1 = af; 428 429 bpf_ptap(ifp->if_bpf, m, &af1, sizeof(af1)); 430 } 431 bpf_reltoken(); 432 } 433 434 /* 435 * Put the packet to the network layer input queue according to the 436 * specified address family. 437 * Note: older versions of gif_input directly called network layer 438 * input functions, e.g. ip6_input, here. We changed the policy to 439 * prevent too many recursive calls of such input functions, which 440 * might cause kernel panic. But the change may introduce another 441 * problem; if the input queue is full, packets are discarded. 442 * The kernel stack overflow really happened, and we believed 443 * queue-full rarely occurs, so we changed the policy. 444 */ 445 switch (af) { 446 #ifdef INET 447 case AF_INET: 448 isr = NETISR_IP; 449 break; 450 #endif 451 #ifdef INET6 452 case AF_INET6: 453 isr = NETISR_IPV6; 454 break; 455 #endif 456 default: 457 m_freem(m); 458 return; 459 } 460 461 IFNET_STAT_INC(ifp, ipackets, 1); 462 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len); 463 m->m_flags &= ~M_HASH; 464 netisr_queue(isr, m); 465 466 return; 467 } 468 469 /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 470 int 471 gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 472 { 473 struct gif_softc *sc = (struct gif_softc*)ifp; 474 struct ifreq *ifr = (struct ifreq*)data; 475 int error = 0, size; 476 struct sockaddr *dst, *src; 477 #ifdef SIOCSIFMTU /* xxx */ 478 u_long mtu; 479 #endif 480 481 switch (cmd) { 482 case SIOCSIFADDR: 483 ifp->if_flags |= IFF_UP; 484 break; 485 486 case SIOCSIFDSTADDR: 487 break; 488 489 case SIOCADDMULTI: 490 case SIOCDELMULTI: 491 break; 492 493 #ifdef SIOCSIFMTU /* xxx */ 494 case SIOCGIFMTU: 495 break; 496 497 case SIOCSIFMTU: 498 mtu = ifr->ifr_mtu; 499 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 500 return (EINVAL); 501 ifp->if_mtu = mtu; 502 break; 503 #endif /* SIOCSIFMTU */ 504 505 #ifdef INET 506 case SIOCSIFPHYADDR: 507 #endif 508 #ifdef INET6 509 case SIOCSIFPHYADDR_IN6: 510 #endif /* INET6 */ 511 case SIOCSLIFPHYADDR: 512 switch (cmd) { 513 #ifdef INET 514 case SIOCSIFPHYADDR: 515 src = (struct sockaddr *) 516 &(((struct in_aliasreq *)data)->ifra_addr); 517 dst = (struct sockaddr *) 518 &(((struct in_aliasreq *)data)->ifra_dstaddr); 519 break; 520 #endif 521 #ifdef INET6 522 case SIOCSIFPHYADDR_IN6: 523 src = (struct sockaddr *) 524 &(((struct in6_aliasreq *)data)->ifra_addr); 525 dst = (struct sockaddr *) 526 &(((struct in6_aliasreq *)data)->ifra_dstaddr); 527 break; 528 #endif 529 case SIOCSLIFPHYADDR: 530 src = (struct sockaddr *) 531 &(((struct if_laddrreq *)data)->addr); 532 dst = (struct sockaddr *) 533 &(((struct if_laddrreq *)data)->dstaddr); 534 break; 535 default: 536 return EINVAL; 537 } 538 539 /* sa_family must be equal */ 540 if (src->sa_family != dst->sa_family) 541 return EINVAL; 542 543 /* validate sa_len */ 544 switch (src->sa_family) { 545 #ifdef INET 546 case AF_INET: 547 if (src->sa_len != sizeof(struct sockaddr_in)) 548 return EINVAL; 549 break; 550 #endif 551 #ifdef INET6 552 case AF_INET6: 553 if (src->sa_len != sizeof(struct sockaddr_in6)) 554 return EINVAL; 555 break; 556 #endif 557 default: 558 return EAFNOSUPPORT; 559 } 560 switch (dst->sa_family) { 561 #ifdef INET 562 case AF_INET: 563 if (dst->sa_len != sizeof(struct sockaddr_in)) 564 return EINVAL; 565 break; 566 #endif 567 #ifdef INET6 568 case AF_INET6: 569 if (dst->sa_len != sizeof(struct sockaddr_in6)) 570 return EINVAL; 571 break; 572 #endif 573 default: 574 return EAFNOSUPPORT; 575 } 576 577 /* check sa_family looks sane for the cmd */ 578 switch (cmd) { 579 case SIOCSIFPHYADDR: 580 if (src->sa_family == AF_INET) 581 break; 582 return EAFNOSUPPORT; 583 #ifdef INET6 584 case SIOCSIFPHYADDR_IN6: 585 if (src->sa_family == AF_INET6) 586 break; 587 return EAFNOSUPPORT; 588 #endif /* INET6 */ 589 case SIOCSLIFPHYADDR: 590 /* checks done in the above */ 591 break; 592 } 593 594 error = gif_set_tunnel(&sc->gif_if, src, dst); 595 break; 596 597 #ifdef SIOCDIFPHYADDR 598 case SIOCDIFPHYADDR: 599 gif_delete_tunnel(&sc->gif_if); 600 break; 601 #endif 602 603 case SIOCGIFPSRCADDR: 604 #ifdef INET6 605 case SIOCGIFPSRCADDR_IN6: 606 #endif /* INET6 */ 607 if (sc->gif_psrc == NULL) { 608 error = EADDRNOTAVAIL; 609 goto bad; 610 } 611 src = sc->gif_psrc; 612 switch (cmd) { 613 #ifdef INET 614 case SIOCGIFPSRCADDR: 615 dst = &ifr->ifr_addr; 616 size = sizeof(ifr->ifr_addr); 617 break; 618 #endif /* INET */ 619 #ifdef INET6 620 case SIOCGIFPSRCADDR_IN6: 621 dst = (struct sockaddr *) 622 &(((struct in6_ifreq *)data)->ifr_addr); 623 size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 624 break; 625 #endif /* INET6 */ 626 default: 627 error = EADDRNOTAVAIL; 628 goto bad; 629 } 630 if (src->sa_len > size) 631 return EINVAL; 632 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 633 break; 634 635 case SIOCGIFPDSTADDR: 636 #ifdef INET6 637 case SIOCGIFPDSTADDR_IN6: 638 #endif /* INET6 */ 639 if (sc->gif_pdst == NULL) { 640 error = EADDRNOTAVAIL; 641 goto bad; 642 } 643 src = sc->gif_pdst; 644 switch (cmd) { 645 #ifdef INET 646 case SIOCGIFPDSTADDR: 647 dst = &ifr->ifr_addr; 648 size = sizeof(ifr->ifr_addr); 649 break; 650 #endif /* INET */ 651 #ifdef INET6 652 case SIOCGIFPDSTADDR_IN6: 653 dst = (struct sockaddr *) 654 &(((struct in6_ifreq *)data)->ifr_addr); 655 size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 656 break; 657 #endif /* INET6 */ 658 default: 659 error = EADDRNOTAVAIL; 660 goto bad; 661 } 662 if (src->sa_len > size) 663 return EINVAL; 664 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 665 break; 666 667 case SIOCGLIFPHYADDR: 668 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 669 error = EADDRNOTAVAIL; 670 goto bad; 671 } 672 673 /* copy src */ 674 src = sc->gif_psrc; 675 dst = (struct sockaddr *) 676 &(((struct if_laddrreq *)data)->addr); 677 size = sizeof(((struct if_laddrreq *)data)->addr); 678 if (src->sa_len > size) 679 return EINVAL; 680 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 681 682 /* copy dst */ 683 src = sc->gif_pdst; 684 dst = (struct sockaddr *) 685 &(((struct if_laddrreq *)data)->dstaddr); 686 size = sizeof(((struct if_laddrreq *)data)->dstaddr); 687 if (src->sa_len > size) 688 return EINVAL; 689 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 690 break; 691 692 case SIOCSIFFLAGS: 693 /* if_ioctl() takes care of it */ 694 break; 695 696 default: 697 error = EINVAL; 698 break; 699 } 700 bad: 701 return error; 702 } 703 704 int 705 gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst) 706 { 707 struct gif_softc *sc = (struct gif_softc *)ifp; 708 struct gif_softc *sc2; 709 struct sockaddr *osrc, *odst, *sa; 710 int error = 0; 711 712 crit_enter(); 713 714 LIST_FOREACH(sc2, &gif_softc_list, gif_list) { 715 if (sc2 == sc) 716 continue; 717 if (!sc2->gif_pdst || !sc2->gif_psrc) 718 continue; 719 if (sc2->gif_pdst->sa_family != dst->sa_family || 720 sc2->gif_pdst->sa_len != dst->sa_len || 721 sc2->gif_psrc->sa_family != src->sa_family || 722 sc2->gif_psrc->sa_len != src->sa_len) 723 continue; 724 725 /* 726 * Disallow parallel tunnels unless instructed 727 * otherwise. 728 */ 729 if (!parallel_tunnels && 730 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 731 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 732 error = EADDRNOTAVAIL; 733 goto bad; 734 } 735 736 /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 737 } 738 739 /* XXX we can detach from both, but be polite just in case */ 740 if (sc->gif_psrc) { 741 switch (sc->gif_psrc->sa_family) { 742 #ifdef INET 743 case AF_INET: 744 in_gif_detach(sc); 745 break; 746 #endif 747 #ifdef INET6 748 case AF_INET6: 749 in6_gif_detach(sc); 750 break; 751 #endif 752 } 753 gif_clear_cache(sc); 754 } 755 756 osrc = sc->gif_psrc; 757 sa = (struct sockaddr *)kmalloc(src->sa_len, M_IFADDR, M_WAITOK); 758 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 759 sc->gif_psrc = sa; 760 761 odst = sc->gif_pdst; 762 sa = (struct sockaddr *)kmalloc(dst->sa_len, M_IFADDR, M_WAITOK); 763 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 764 sc->gif_pdst = sa; 765 766 switch (sc->gif_psrc->sa_family) { 767 #ifdef INET 768 case AF_INET: 769 error = in_gif_attach(sc); 770 break; 771 #endif 772 #ifdef INET6 773 case AF_INET6: 774 error = in6_gif_attach(sc); 775 break; 776 #endif 777 } 778 if (error) { 779 /* rollback */ 780 kfree((caddr_t)sc->gif_psrc, M_IFADDR); 781 kfree((caddr_t)sc->gif_pdst, M_IFADDR); 782 sc->gif_psrc = osrc; 783 sc->gif_pdst = odst; 784 goto bad; 785 } 786 787 if (osrc) 788 kfree((caddr_t)osrc, M_IFADDR); 789 if (odst) 790 kfree((caddr_t)odst, M_IFADDR); 791 792 if (sc->gif_psrc && sc->gif_pdst) 793 ifp->if_flags |= IFF_RUNNING; 794 else 795 ifp->if_flags &= ~IFF_RUNNING; 796 crit_exit(); 797 798 return 0; 799 800 bad: 801 if (sc->gif_psrc && sc->gif_pdst) 802 ifp->if_flags |= IFF_RUNNING; 803 else 804 ifp->if_flags &= ~IFF_RUNNING; 805 crit_exit(); 806 807 return error; 808 } 809 810 void 811 gif_delete_tunnel(struct ifnet *ifp) 812 { 813 struct gif_softc *sc = (struct gif_softc *)ifp; 814 815 crit_enter(); 816 817 if (sc->gif_psrc) { 818 kfree((caddr_t)sc->gif_psrc, M_IFADDR); 819 sc->gif_psrc = NULL; 820 } 821 if (sc->gif_pdst) { 822 kfree((caddr_t)sc->gif_pdst, M_IFADDR); 823 sc->gif_pdst = NULL; 824 } 825 /* it is safe to detach from both */ 826 #ifdef INET 827 in_gif_detach(sc); 828 #endif 829 #ifdef INET6 830 in6_gif_detach(sc); 831 #endif 832 gif_clear_cache(sc); 833 834 if (sc->gif_psrc && sc->gif_pdst) 835 ifp->if_flags |= IFF_RUNNING; 836 else 837 ifp->if_flags &= ~IFF_RUNNING; 838 crit_exit(); 839 } 840