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