if_gif.c revision 148385
14Srgrimes/* $FreeBSD: head/sys/net/if_gif.c 148385 2005-07-25 12:31:43Z ume $ */ 21690Sdg/* $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ */ 31690Sdg 41690Sdg/*- 5174395Sjkoshy * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 64Srgrimes * All rights reserved. 74Srgrimes * 84Srgrimes * Redistribution and use in source and binary forms, with or without 94Srgrimes * modification, are permitted provided that the following conditions 10174395Sjkoshy * are met: 11174395Sjkoshy * 1. Redistributions of source code must retain the above copyright 12174395Sjkoshy * notice, this list of conditions and the following disclaimer. 134Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 144Srgrimes * notice, this list of conditions and the following disclaimer in the 154Srgrimes * documentation and/or other materials provided with the distribution. 164Srgrimes * 3. Neither the name of the project nor the names of its contributors 174Srgrimes * may be used to endorse or promote products derived from this software 184Srgrimes * without specific prior written permission. 194Srgrimes * 204Srgrimes * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 214Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 244Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304Srgrimes * SUCH DAMAGE. 314Srgrimes */ 324Srgrimes 334Srgrimes#include "opt_inet.h" 344Srgrimes#include "opt_inet6.h" 354Srgrimes#include "opt_mac.h" 364Srgrimes 374Srgrimes#include <sys/param.h> 384Srgrimes#include <sys/systm.h> 394Srgrimes#include <sys/kernel.h> 404Srgrimes#include <sys/mac.h> 41608Srgrimes#include <sys/malloc.h> 424Srgrimes#include <sys/mbuf.h> 434Srgrimes#include <sys/module.h> 44116182Sobrien#include <sys/socket.h> 45116182Sobrien#include <sys/sockio.h> 46116182Sobrien#include <sys/errno.h> 47118240Speter#include <sys/time.h> 48209258Srpaulo#include <sys/sysctl.h> 49170640Sjeff#include <sys/syslog.h> 5013203Swollman#include <sys/protosw.h> 511549Srgrimes#include <sys/conf.h> 5265557Sjasone#include <machine/cpu.h> 531549Srgrimes 5478983Sjhb#include <net/if.h> 5567365Sjhb#include <net/if_clone.h> 56174395Sjkoshy#include <net/if_types.h> 5778983Sjhb#include <net/netisr.h> 5899072Sjulian#include <net/route.h> 59208453Skib#include <net/bpf.h> 60208453Skib 6131389Sbde#include <netinet/in.h> 62104964Sjeff#include <netinet/in_systm.h> 6331389Sbde#include <netinet/ip.h> 64208453Skib#ifdef INET 65209613Sjhb#include <netinet/in_var.h> 66208453Skib#include <netinet/in_gif.h> 6778983Sjhb#include <netinet/ip_var.h> 6812662Sdg#endif /* INET */ 69118240Speter 70118240Speter#ifdef INET6 71118240Speter#ifndef INET 72118240Speter#include <netinet/in.h> 73208453Skib#endif 74118240Speter#include <netinet6/in6_var.h> 751549Srgrimes#include <netinet/ip6.h> 761549Srgrimes#include <netinet6/ip6_var.h> 77218688Sbz#include <netinet6/scope6_var.h> 78218688Sbz#include <netinet6/in6_gif.h> 79218688Sbz#include <netinet6/ip6protosw.h> 80218688Sbz#endif /* INET6 */ 81184042Skmacy 82184042Skmacy#include <netinet/ip_encap.h> 83184042Skmacy#include <net/if_gif.h> 84184042Skmacy 85184042Skmacy#include <net/net_osdep.h> 86184042Skmacy 87163606Srwatson#define GIFNAME "gif" 88163606Srwatson 8978983Sjhb/* 90167211Srwatson * gif_mtx protects the global gif_softc_list. 91167211Srwatson * XXX: Per-softc locking is still required. 9278983Sjhb */ 9371527Sjhbstatic struct mtx gif_mtx; 94155455Sphkstatic MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 951690Sdgstatic LIST_HEAD(, gif_softc) gif_softc_list; 9683366Sjulian 97757Sdgvoid (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 9899072Sjulianvoid (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 99173601Sjulianvoid (*ng_gif_attach_p)(struct ifnet *ifp); 100197963Skibvoid (*ng_gif_detach_p)(struct ifnet *ifp); 101126661Srwatson 102110190Sjulianstatic int gif_clone_create(struct if_clone *, int); 10378636Sjhbstatic void gif_clone_destroy(struct ifnet *); 104170307Sjeff 105112888SjeffIFC_SIMPLE_DECLARE(gif, 0); 106111032Sjulian 107102266Srwatsonstatic int gifmodevent(module_t, int, void *); 108170307Sjeff 10982585SdillonSYSCTL_DECL(_net_link); 11093793SbdeSYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 111197963Skib "Generic Tunnel Interface"); 112152376Srwatson#ifndef MAX_GIF_NEST 113152376Srwatson/* 114152376Srwatson * This macro controls the default upper limitation on nesting of gif tunnels. 11593793Sbde * Since, setting a large value to this macro with a careless configuration 116136837Sphk * may introduce system crash, we don't allow any nestings by default. 117136837Sphk * If you need to configure nested gif tunnels, you can define this macro 118136837Sphk * in your kernel configuration file. However, if you do so, please be 119136837Sphk * careful to configure the tunnels so that it won't make a loop. 120136837Sphk */ 121136837Sphk#define MAX_GIF_NEST 1 122136837Sphk#endif 123110190Sjulianstatic int max_gif_nesting = MAX_GIF_NEST; 124110190SjulianSYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 125213236Semaste &max_gif_nesting, 0, "Max nested tunnels"); 126155455Sphk 127139324Sjeff/* 128139324Sjeff * By default, we disallow creation of multiple tunnels between the same 129139324Sjeff * pair of addresses. Some applications require this functionality so 130139324Sjeff * we allow control over this check here. 131144061Sjeff */ 132144061Sjeff#ifdef XBONEHACK 133218688Sbzstatic int parallel_tunnels = 1; 134218688Sbz#else 135218688Sbzstatic int parallel_tunnels = 0; 136218688Sbz#endif 137218688SbzSYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 138218688Sbz ¶llel_tunnels, 0, "Allow parallel tunnels?"); 139218688Sbz 140184042Skmacystatic int 141184042Skmacygif_clone_create(ifc, unit) 142184042Skmacy struct if_clone *ifc; 1431690Sdg int unit; 1441690Sdg{ 1454Srgrimes struct gif_softc *sc; 14678983Sjhb 14778983Sjhb sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 14881493Sjhb GIF2IFP(sc) = if_alloc(IFT_GIF); 1494Srgrimes if (GIF2IFP(sc) == NULL) { 150798Swollman free(sc, M_GIF); 15199072Sjulian return (ENOSPC); 15265557Sjasone } 153104297Sjhb 154104297Sjhb GIF2IFP(sc)->if_softc = sc; 15583366Sjulian if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 15693793Sbde 15765557Sjasone gifattach0(sc); 158104297Sjhb 159104297Sjhb mtx_lock(&gif_mtx); 160104378Sjmallett LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list); 16199072Sjulian mtx_unlock(&gif_mtx); 16299072Sjulian return (0); 16372911Sjhb} 164111883Sjhb 16581493Sjhbvoid 166170307Sjeffgifattach0(sc) 16793390Sjake struct gif_softc *sc; 168155455Sphk{ 169104297Sjhb 17093390Sjake sc->encap_cookie4 = sc->encap_cookie6 = NULL; 171172207Sjeff 17293390Sjake GIF2IFP(sc)->if_addrlen = 0; 17393390Sjake GIF2IFP(sc)->if_mtu = GIF_MTU; 174172207Sjeff GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 17593390Sjake#if 0 17693390Sjake /* turn off ingress filter */ 177170307Sjeff GIF2IFP(sc)->if_flags |= IFF_LINK2; 178170307Sjeff#endif 179177471Sjeff GIF2IFP(sc)->if_ioctl = gif_ioctl; 180177471Sjeff GIF2IFP(sc)->if_output = gif_output; 181170307Sjeff GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 182170292Sattilio if_attach(GIF2IFP(sc)); 183135573Sjhb bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 18493390Sjake if (ng_gif_attach_p != NULL) 18593390Sjake (*ng_gif_attach_p)(GIF2IFP(sc)); 186132266Sjhb} 187132266Sjhb 188132266Sjhbstatic void 189132266Sjhbgif_destroy(struct gif_softc *sc) 190131437Sjhb{ 191172207Sjeff struct ifnet *ifp = GIF2IFP(sc); 19293390Sjake int err; 19393390Sjake 19493390Sjake gif_delete_tunnel(ifp); 19593390Sjake#ifdef INET6 196172207Sjeff if (sc->encap_cookie6 != NULL) { 19793390Sjake err = encap_detach(sc->encap_cookie6); 19893390Sjake KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 19993390Sjake } 20093390Sjake#endif 201106655Srwatson#ifdef INET 202172207Sjeff if (sc->encap_cookie4 != NULL) { 203106655Srwatson err = encap_detach(sc->encap_cookie4); 204106655Srwatson KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 205111032Sjulian } 206118240Speter#endif 207118240Speter 208119781Speter if (ng_gif_detach_p != NULL) 209118240Speter (*ng_gif_detach_p)(ifp); 210170307Sjeff bpfdetach(ifp); 211163709Sjb if_detach(ifp); 212178272Sjeff if_free(ifp); 213170307Sjeff 214118240Speter free(sc, M_GIF); 215118240Speter} 216119781Speter 217118240Speterstatic void 21893793Sbdegif_clone_destroy(ifp) 219197963Skib struct ifnet *ifp; 220197963Skib{ 221197963Skib struct gif_softc *sc = ifp->if_softc; 222197963Skib 223197963Skib mtx_lock(&gif_mtx); 224197963Skib LIST_REMOVE(sc, gif_list); 225197963Skib mtx_unlock(&gif_mtx); 226197963Skib gif_destroy(sc); 22793793Sbde} 228114983Sjhb 229195702Skibstatic int 23093793Sbdegifmodevent(mod, type, data) 231114983Sjhb module_t mod; 23293793Sbde int type; 23393793Sbde void *data; 234177471Sjeff{ 235177471Sjeff struct gif_softc *sc; 236177471Sjeff 237177471Sjeff switch (type) { 238177471Sjeff case MOD_LOAD: 239177471Sjeff mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 240177471Sjeff LIST_INIT(&gif_softc_list); 241177471Sjeff if_clone_attach(&gif_cloner); 242177471Sjeff 24365557Sjasone#ifdef INET6 244198508Skib ip6_gif_hlim = GIF_HLIM; 245198508Skib#endif 246198508Skib 247198508Skib break; 248198508Skib case MOD_UNLOAD: 249155455Sphk if_clone_detach(&gif_cloner); 25081493Sjhb 25124691Speter mtx_lock(&gif_mtx); 252208453Skib while ((sc = LIST_FIRST(&gif_softc_list)) != NULL) { 253208453Skib LIST_REMOVE(sc, gif_list); 254208566Skib mtx_unlock(&gif_mtx); 255208453Skib gif_destroy(sc); 256208453Skib mtx_lock(&gif_mtx); 257208453Skib } 258209697Skib mtx_unlock(&gif_mtx); 259208453Skib mtx_destroy(&gif_mtx); 260209697Skib#ifdef INET6 261209697Skib ip6_gif_hlim = 0; 262208453Skib#endif 263209697Skib break; 264208453Skib default: 265208453Skib return EOPNOTSUPP; 266208453Skib } 267208453Skib return 0; 268208453Skib} 269208453Skib 270208453Skibstatic moduledata_t gif_mod = { 271208453Skib "if_gif", 272208453Skib gifmodevent, 273208453Skib 0 274208453Skib}; 275208453Skib 276208453SkibDECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 277208453SkibMODULE_VERSION(if_gif, 1); 278208453Skib 279208453Skibint 280208453Skibgif_encapcheck(m, off, proto, arg) 281208453Skib const struct mbuf *m; 282208453Skib int off; 283208453Skib int proto; 284208453Skib void *arg; 285208453Skib{ 286208453Skib struct ip ip; 287208453Skib struct gif_softc *sc; 288208453Skib 289208453Skib sc = (struct gif_softc *)arg; 290208453Skib if (sc == NULL) 291208453Skib return 0; 292208453Skib 293208453Skib if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 294208453Skib return 0; 295208453Skib 296208453Skib /* no physical address */ 297208453Skib if (!sc->gif_psrc || !sc->gif_pdst) 298208453Skib return 0; 299208453Skib 300208453Skib switch (proto) { 301208453Skib#ifdef INET 302208453Skib case IPPROTO_IPV4: 303208453Skib break; 304208453Skib#endif 305208453Skib#ifdef INET6 306208453Skib case IPPROTO_IPV6: 307208453Skib break; 308208453Skib#endif 309208453Skib default: 310208453Skib return 0; 311208453Skib } 312208453Skib 313209579Skib /* Bail on short packets */ 314209579Skib if (m->m_pkthdr.len < sizeof(ip)) 315209579Skib return 0; 316208453Skib 317208453Skib m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 318208453Skib 319208453Skib switch (ip.ip_v) { 320208453Skib#ifdef INET 321208453Skib case 4: 322208453Skib if (sc->gif_psrc->sa_family != AF_INET || 323208453Skib sc->gif_pdst->sa_family != AF_INET) 324208453Skib return 0; 325211617Srpaulo return gif_encapcheck4(m, off, proto, arg); 326208453Skib#endif 327208453Skib#ifdef INET6 328208453Skib case 6: 329208453Skib if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 330208453Skib return 0; 331208453Skib if (sc->gif_psrc->sa_family != AF_INET6 || 332208453Skib sc->gif_pdst->sa_family != AF_INET6) 333208453Skib return 0; 334208453Skib return gif_encapcheck6(m, off, proto, arg); 335208453Skib#endif 336208453Skib default: 337208453Skib return 0; 338208453Skib } 339208453Skib} 340208453Skib 341208453Skibint 342208453Skibgif_output(ifp, m, dst, rt) 343211617Srpaulo struct ifnet *ifp; 344208453Skib struct mbuf *m; 345209579Skib struct sockaddr *dst; 346208453Skib struct rtentry *rt; /* added in net2 */ 347208453Skib{ 348208453Skib struct gif_softc *sc = ifp->if_softc; 349208453Skib struct m_tag *mtag; 350208453Skib int error = 0; 351208453Skib int gif_called; 352208453Skib u_int32_t af; 353208453Skib 354208453Skib#ifdef MAC 355208453Skib error = mac_check_ifnet_transmit(ifp, m); 356208453Skib if (error) { 357208453Skib m_freem(m); 358208453Skib goto end; 359208453Skib } 360208453Skib#endif 361208453Skib 362208453Skib /* 363208453Skib * gif may cause infinite recursion calls when misconfigured. 364208453Skib * We'll prevent this by detecting loops. 365208453Skib * 366208453Skib * High nesting level may cause stack exhaustion. 367208453Skib * We'll prevent this by introducing upper limit. 368208453Skib */ 369208453Skib gif_called = 1; 370208453Skib mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 371208453Skib while (mtag != NULL) { 372208453Skib if (*(struct ifnet **)(mtag + 1) == ifp) { 373208453Skib log(LOG_NOTICE, 374208453Skib "gif_output: loop detected on %s\n", 375208453Skib (*(struct ifnet **)(mtag + 1))->if_xname); 376208453Skib m_freem(m); 377208453Skib error = EIO; /* is there better errno? */ 378208453Skib goto end; 379208453Skib } 380208453Skib mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 381208453Skib gif_called++; 382208453Skib } 383208453Skib if (gif_called > max_gif_nesting) { 384208453Skib log(LOG_NOTICE, 385208453Skib "gif_output: recursively called too many times(%d)\n", 386208453Skib gif_called); 387208453Skib m_freem(m); 388208453Skib error = EIO; /* is there better errno? */ 389208453Skib goto end; 390208453Skib } 391208453Skib mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 392208453Skib M_NOWAIT); 393208453Skib if (mtag == NULL) { 394208453Skib m_freem(m); 395208453Skib error = ENOMEM; 396208453Skib goto end; 397208453Skib } 398208453Skib *(struct ifnet **)(mtag + 1) = ifp; 399208453Skib m_tag_prepend(m, mtag); 400208453Skib 401208453Skib m->m_flags &= ~(M_BCAST|M_MCAST); 402208453Skib if (!(ifp->if_flags & IFF_UP) || 403208453Skib sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 404208453Skib m_freem(m); 405208453Skib error = ENETDOWN; 406217819Skib goto end; 407208453Skib } 408217819Skib 409208453Skib /* BPF writes need to be handled specially. */ 410208453Skib if (dst->sa_family == AF_UNSPEC) { 411208453Skib bcopy(dst->sa_data, &af, sizeof(af)); 412208453Skib dst->sa_family = af; 413 } 414 415 if (ifp->if_bpf) { 416 af = dst->sa_family; 417 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 418 } 419 ifp->if_opackets++; 420 ifp->if_obytes += m->m_pkthdr.len; 421 422 /* inner AF-specific encapsulation */ 423 424 /* XXX should we check if our outer source is legal? */ 425 426 /* dispatch to output logic based on outer AF */ 427 switch (sc->gif_psrc->sa_family) { 428#ifdef INET 429 case AF_INET: 430 error = in_gif_output(ifp, dst->sa_family, m); 431 break; 432#endif 433#ifdef INET6 434 case AF_INET6: 435 error = in6_gif_output(ifp, dst->sa_family, m); 436 break; 437#endif 438 default: 439 m_freem(m); 440 error = ENETDOWN; 441 goto end; 442 } 443 444 end: 445 if (error) 446 ifp->if_oerrors++; 447 return error; 448} 449 450void 451gif_input(m, af, ifp) 452 struct mbuf *m; 453 int af; 454 struct ifnet *ifp; 455{ 456 int isr; 457 458 if (ifp == NULL) { 459 /* just in case */ 460 m_freem(m); 461 return; 462 } 463 464 m->m_pkthdr.rcvif = ifp; 465 466#ifdef MAC 467 mac_create_mbuf_from_ifnet(ifp, m); 468#endif 469 470 if (ifp->if_bpf) { 471 u_int32_t af1 = af; 472 bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 473 } 474 475 if (ng_gif_input_p != NULL) { 476 (*ng_gif_input_p)(ifp, &m, af); 477 if (m == NULL) 478 return; 479 } 480 481 /* 482 * Put the packet to the network layer input queue according to the 483 * specified address family. 484 * Note: older versions of gif_input directly called network layer 485 * input functions, e.g. ip6_input, here. We changed the policy to 486 * prevent too many recursive calls of such input functions, which 487 * might cause kernel panic. But the change may introduce another 488 * problem; if the input queue is full, packets are discarded. 489 * The kernel stack overflow really happened, and we believed 490 * queue-full rarely occurs, so we changed the policy. 491 */ 492 switch (af) { 493#ifdef INET 494 case AF_INET: 495 isr = NETISR_IP; 496 break; 497#endif 498#ifdef INET6 499 case AF_INET6: 500 isr = NETISR_IPV6; 501 break; 502#endif 503 default: 504 if (ng_gif_input_orphan_p != NULL) 505 (*ng_gif_input_orphan_p)(ifp, m, af); 506 else 507 m_freem(m); 508 return; 509 } 510 511 ifp->if_ipackets++; 512 ifp->if_ibytes += m->m_pkthdr.len; 513 netisr_dispatch(isr, m); 514} 515 516/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 517int 518gif_ioctl(ifp, cmd, data) 519 struct ifnet *ifp; 520 u_long cmd; 521 caddr_t data; 522{ 523 struct gif_softc *sc = ifp->if_softc; 524 struct ifreq *ifr = (struct ifreq*)data; 525 int error = 0, size; 526 struct sockaddr *dst, *src; 527#ifdef SIOCSIFMTU /* xxx */ 528 u_long mtu; 529#endif 530 531 switch (cmd) { 532 case SIOCSIFADDR: 533 ifp->if_flags |= IFF_UP; 534 break; 535 536 case SIOCSIFDSTADDR: 537 break; 538 539 case SIOCADDMULTI: 540 case SIOCDELMULTI: 541 break; 542 543#ifdef SIOCSIFMTU /* xxx */ 544 case SIOCGIFMTU: 545 break; 546 547 case SIOCSIFMTU: 548 mtu = ifr->ifr_mtu; 549 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 550 return (EINVAL); 551 ifp->if_mtu = mtu; 552 break; 553#endif /* SIOCSIFMTU */ 554 555#ifdef INET 556 case SIOCSIFPHYADDR: 557#endif 558#ifdef INET6 559 case SIOCSIFPHYADDR_IN6: 560#endif /* INET6 */ 561 case SIOCSLIFPHYADDR: 562 switch (cmd) { 563#ifdef INET 564 case SIOCSIFPHYADDR: 565 src = (struct sockaddr *) 566 &(((struct in_aliasreq *)data)->ifra_addr); 567 dst = (struct sockaddr *) 568 &(((struct in_aliasreq *)data)->ifra_dstaddr); 569 break; 570#endif 571#ifdef INET6 572 case SIOCSIFPHYADDR_IN6: 573 src = (struct sockaddr *) 574 &(((struct in6_aliasreq *)data)->ifra_addr); 575 dst = (struct sockaddr *) 576 &(((struct in6_aliasreq *)data)->ifra_dstaddr); 577 break; 578#endif 579 case SIOCSLIFPHYADDR: 580 src = (struct sockaddr *) 581 &(((struct if_laddrreq *)data)->addr); 582 dst = (struct sockaddr *) 583 &(((struct if_laddrreq *)data)->dstaddr); 584 break; 585 default: 586 return EINVAL; 587 } 588 589 /* sa_family must be equal */ 590 if (src->sa_family != dst->sa_family) 591 return EINVAL; 592 593 /* validate sa_len */ 594 switch (src->sa_family) { 595#ifdef INET 596 case AF_INET: 597 if (src->sa_len != sizeof(struct sockaddr_in)) 598 return EINVAL; 599 break; 600#endif 601#ifdef INET6 602 case AF_INET6: 603 if (src->sa_len != sizeof(struct sockaddr_in6)) 604 return EINVAL; 605 break; 606#endif 607 default: 608 return EAFNOSUPPORT; 609 } 610 switch (dst->sa_family) { 611#ifdef INET 612 case AF_INET: 613 if (dst->sa_len != sizeof(struct sockaddr_in)) 614 return EINVAL; 615 break; 616#endif 617#ifdef INET6 618 case AF_INET6: 619 if (dst->sa_len != sizeof(struct sockaddr_in6)) 620 return EINVAL; 621 break; 622#endif 623 default: 624 return EAFNOSUPPORT; 625 } 626 627 /* check sa_family looks sane for the cmd */ 628 switch (cmd) { 629 case SIOCSIFPHYADDR: 630 if (src->sa_family == AF_INET) 631 break; 632 return EAFNOSUPPORT; 633#ifdef INET6 634 case SIOCSIFPHYADDR_IN6: 635 if (src->sa_family == AF_INET6) 636 break; 637 return EAFNOSUPPORT; 638#endif /* INET6 */ 639 case SIOCSLIFPHYADDR: 640 /* checks done in the above */ 641 break; 642 } 643 644 error = gif_set_tunnel(GIF2IFP(sc), src, dst); 645 break; 646 647#ifdef SIOCDIFPHYADDR 648 case SIOCDIFPHYADDR: 649 gif_delete_tunnel(GIF2IFP(sc)); 650 break; 651#endif 652 653 case SIOCGIFPSRCADDR: 654#ifdef INET6 655 case SIOCGIFPSRCADDR_IN6: 656#endif /* INET6 */ 657 if (sc->gif_psrc == NULL) { 658 error = EADDRNOTAVAIL; 659 goto bad; 660 } 661 src = sc->gif_psrc; 662 switch (cmd) { 663#ifdef INET 664 case SIOCGIFPSRCADDR: 665 dst = &ifr->ifr_addr; 666 size = sizeof(ifr->ifr_addr); 667 break; 668#endif /* INET */ 669#ifdef INET6 670 case SIOCGIFPSRCADDR_IN6: 671 dst = (struct sockaddr *) 672 &(((struct in6_ifreq *)data)->ifr_addr); 673 size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 674 break; 675#endif /* INET6 */ 676 default: 677 error = EADDRNOTAVAIL; 678 goto bad; 679 } 680 if (src->sa_len > size) 681 return EINVAL; 682 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 683#ifdef INET6 684 if (dst->sa_family == AF_INET6) { 685 error = sa6_recoverscope((struct sockaddr_in6 *)dst); 686 if (error != 0) 687 return (error); 688 } 689#endif 690 break; 691 692 case SIOCGIFPDSTADDR: 693#ifdef INET6 694 case SIOCGIFPDSTADDR_IN6: 695#endif /* INET6 */ 696 if (sc->gif_pdst == NULL) { 697 error = EADDRNOTAVAIL; 698 goto bad; 699 } 700 src = sc->gif_pdst; 701 switch (cmd) { 702#ifdef INET 703 case SIOCGIFPDSTADDR: 704 dst = &ifr->ifr_addr; 705 size = sizeof(ifr->ifr_addr); 706 break; 707#endif /* INET */ 708#ifdef INET6 709 case SIOCGIFPDSTADDR_IN6: 710 dst = (struct sockaddr *) 711 &(((struct in6_ifreq *)data)->ifr_addr); 712 size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 713 break; 714#endif /* INET6 */ 715 default: 716 error = EADDRNOTAVAIL; 717 goto bad; 718 } 719 if (src->sa_len > size) 720 return EINVAL; 721 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 722#ifdef INET6 723 if (dst->sa_family == AF_INET6) { 724 error = sa6_recoverscope((struct sockaddr_in6 *)dst); 725 if (error != 0) 726 return (error); 727 } 728#endif 729 break; 730 731 case SIOCGLIFPHYADDR: 732 if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 733 error = EADDRNOTAVAIL; 734 goto bad; 735 } 736 737 /* copy src */ 738 src = sc->gif_psrc; 739 dst = (struct sockaddr *) 740 &(((struct if_laddrreq *)data)->addr); 741 size = sizeof(((struct if_laddrreq *)data)->addr); 742 if (src->sa_len > size) 743 return EINVAL; 744 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 745 746 /* copy dst */ 747 src = sc->gif_pdst; 748 dst = (struct sockaddr *) 749 &(((struct if_laddrreq *)data)->dstaddr); 750 size = sizeof(((struct if_laddrreq *)data)->dstaddr); 751 if (src->sa_len > size) 752 return EINVAL; 753 bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 754 break; 755 756 case SIOCSIFFLAGS: 757 /* if_ioctl() takes care of it */ 758 break; 759 760 default: 761 error = EINVAL; 762 break; 763 } 764 bad: 765 return error; 766} 767 768/* 769 * XXXRW: There's a general event-ordering issue here: the code to check 770 * if a given tunnel is already present happens before we perform a 771 * potentially blocking setup of the tunnel. This code needs to be 772 * re-ordered so that the check and replacement can be atomic using 773 * a mutex. 774 */ 775int 776gif_set_tunnel(ifp, src, dst) 777 struct ifnet *ifp; 778 struct sockaddr *src; 779 struct sockaddr *dst; 780{ 781 struct gif_softc *sc = ifp->if_softc; 782 struct gif_softc *sc2; 783 struct sockaddr *osrc, *odst, *sa; 784 int s; 785 int error = 0; 786 787 s = splnet(); 788 789 mtx_lock(&gif_mtx); 790 LIST_FOREACH(sc2, &gif_softc_list, gif_list) { 791 if (sc2 == sc) 792 continue; 793 if (!sc2->gif_pdst || !sc2->gif_psrc) 794 continue; 795 if (sc2->gif_pdst->sa_family != dst->sa_family || 796 sc2->gif_pdst->sa_len != dst->sa_len || 797 sc2->gif_psrc->sa_family != src->sa_family || 798 sc2->gif_psrc->sa_len != src->sa_len) 799 continue; 800 801 /* 802 * Disallow parallel tunnels unless instructed 803 * otherwise. 804 */ 805 if (!parallel_tunnels && 806 bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 807 bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 808 error = EADDRNOTAVAIL; 809 mtx_unlock(&gif_mtx); 810 goto bad; 811 } 812 813 /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 814 } 815 mtx_unlock(&gif_mtx); 816 817 /* XXX we can detach from both, but be polite just in case */ 818 if (sc->gif_psrc) 819 switch (sc->gif_psrc->sa_family) { 820#ifdef INET 821 case AF_INET: 822 (void)in_gif_detach(sc); 823 break; 824#endif 825#ifdef INET6 826 case AF_INET6: 827 (void)in6_gif_detach(sc); 828 break; 829#endif 830 } 831 832 osrc = sc->gif_psrc; 833 sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 834 bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 835 sc->gif_psrc = sa; 836 837 odst = sc->gif_pdst; 838 sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 839 bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 840 sc->gif_pdst = sa; 841 842 switch (sc->gif_psrc->sa_family) { 843#ifdef INET 844 case AF_INET: 845 error = in_gif_attach(sc); 846 break; 847#endif 848#ifdef INET6 849 case AF_INET6: 850 /* 851 * Check validity of the scope zone ID of the addresses, and 852 * convert it into the kernel internal form if necessary. 853 */ 854 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 855 if (error != 0) 856 break; 857 error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 858 if (error != 0) 859 break; 860 error = in6_gif_attach(sc); 861 break; 862#endif 863 } 864 if (error) { 865 /* rollback */ 866 free((caddr_t)sc->gif_psrc, M_IFADDR); 867 free((caddr_t)sc->gif_pdst, M_IFADDR); 868 sc->gif_psrc = osrc; 869 sc->gif_pdst = odst; 870 goto bad; 871 } 872 873 if (osrc) 874 free((caddr_t)osrc, M_IFADDR); 875 if (odst) 876 free((caddr_t)odst, M_IFADDR); 877 878 if (sc->gif_psrc && sc->gif_pdst) 879 ifp->if_flags |= IFF_RUNNING; 880 else 881 ifp->if_flags &= ~IFF_RUNNING; 882 splx(s); 883 884 return 0; 885 886 bad: 887 if (sc->gif_psrc && sc->gif_pdst) 888 ifp->if_flags |= IFF_RUNNING; 889 else 890 ifp->if_flags &= ~IFF_RUNNING; 891 splx(s); 892 893 return error; 894} 895 896void 897gif_delete_tunnel(ifp) 898 struct ifnet *ifp; 899{ 900 struct gif_softc *sc = ifp->if_softc; 901 int s; 902 903 s = splnet(); 904 905 if (sc->gif_psrc) { 906 free((caddr_t)sc->gif_psrc, M_IFADDR); 907 sc->gif_psrc = NULL; 908 } 909 if (sc->gif_pdst) { 910 free((caddr_t)sc->gif_pdst, M_IFADDR); 911 sc->gif_pdst = NULL; 912 } 913 /* it is safe to detach from both */ 914#ifdef INET 915 (void)in_gif_detach(sc); 916#endif 917#ifdef INET6 918 (void)in6_gif_detach(sc); 919#endif 920 921 if (sc->gif_psrc && sc->gif_pdst) 922 ifp->if_flags |= IFF_RUNNING; 923 else 924 ifp->if_flags &= ~IFF_RUNNING; 925 splx(s); 926} 927