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