1686cdd19SJun-ichiro itojun Hagino /* $FreeBSD$ */ 288ff5695SSUZUKI Shinsuke /* $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ */ 3686cdd19SJun-ichiro itojun Hagino 4cfa1ca9dSYoshinobu Inoue /* 5cfa1ca9dSYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6cfa1ca9dSYoshinobu Inoue * All rights reserved. 7cfa1ca9dSYoshinobu Inoue * 8cfa1ca9dSYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 9cfa1ca9dSYoshinobu Inoue * modification, are permitted provided that the following conditions 10cfa1ca9dSYoshinobu Inoue * are met: 11cfa1ca9dSYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 12cfa1ca9dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 13cfa1ca9dSYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 14cfa1ca9dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 15cfa1ca9dSYoshinobu Inoue * documentation and/or other materials provided with the distribution. 16cfa1ca9dSYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 17cfa1ca9dSYoshinobu Inoue * may be used to endorse or promote products derived from this software 18cfa1ca9dSYoshinobu Inoue * without specific prior written permission. 19cfa1ca9dSYoshinobu Inoue * 20cfa1ca9dSYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21cfa1ca9dSYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22cfa1ca9dSYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23cfa1ca9dSYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24cfa1ca9dSYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25cfa1ca9dSYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26cfa1ca9dSYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27cfa1ca9dSYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28cfa1ca9dSYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29cfa1ca9dSYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30cfa1ca9dSYoshinobu Inoue * SUCH DAMAGE. 31cfa1ca9dSYoshinobu Inoue */ 32cfa1ca9dSYoshinobu Inoue 33cfa1ca9dSYoshinobu Inoue #include "opt_inet.h" 34cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 35e0852ce2SRobert Watson #include "opt_mac.h" 36cfa1ca9dSYoshinobu Inoue 37cfa1ca9dSYoshinobu Inoue #include <sys/param.h> 38cfa1ca9dSYoshinobu Inoue #include <sys/systm.h> 39cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h> 4010722b85SRobert Watson #include <sys/mac.h> 41cfa1ca9dSYoshinobu Inoue #include <sys/malloc.h> 42cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h> 43cfa1ca9dSYoshinobu Inoue #include <sys/socket.h> 44cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h> 45cfa1ca9dSYoshinobu Inoue #include <sys/errno.h> 46cfa1ca9dSYoshinobu Inoue #include <sys/time.h> 47872f786aSBrooks Davis #include <sys/sysctl.h> 48cfa1ca9dSYoshinobu Inoue #include <sys/syslog.h> 49686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h> 5053dab5feSBrooks Davis #include <sys/conf.h> 51cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h> 52cfa1ca9dSYoshinobu Inoue 53cfa1ca9dSYoshinobu Inoue #include <net/if.h> 54cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 55cfa1ca9dSYoshinobu Inoue #include <net/netisr.h> 56cfa1ca9dSYoshinobu Inoue #include <net/route.h> 57cfa1ca9dSYoshinobu Inoue #include <net/bpf.h> 58cfa1ca9dSYoshinobu Inoue 59cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 60cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h> 61cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h> 6233841545SHajimu UMEMOTO #ifdef INET 6333841545SHajimu UMEMOTO #include <netinet/in_var.h> 64cfa1ca9dSYoshinobu Inoue #include <netinet/in_gif.h> 6553dab5feSBrooks Davis #include <netinet/ip_var.h> 66cfa1ca9dSYoshinobu Inoue #endif /* INET */ 67cfa1ca9dSYoshinobu Inoue 68cfa1ca9dSYoshinobu Inoue #ifdef INET6 69cfa1ca9dSYoshinobu Inoue #ifndef INET 70cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 71cfa1ca9dSYoshinobu Inoue #endif 72cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h> 73cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 74cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 75cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_gif.h> 76686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h> 77cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 78cfa1ca9dSYoshinobu Inoue 79686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h> 80cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h> 81cfa1ca9dSYoshinobu Inoue 82cfa1ca9dSYoshinobu Inoue #include <net/net_osdep.h> 83cfa1ca9dSYoshinobu Inoue 8453dab5feSBrooks Davis #define GIFNAME "gif" 85686cdd19SJun-ichiro itojun Hagino 8653dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 87e8783c4dSMike Smith static LIST_HEAD(, gif_softc) gif_softc_list; 8853dab5feSBrooks Davis 8994408d94SBrooks Davis void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 9094408d94SBrooks Davis void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 9194408d94SBrooks Davis void (*ng_gif_attach_p)(struct ifnet *ifp); 9294408d94SBrooks Davis void (*ng_gif_detach_p)(struct ifnet *ifp); 9394408d94SBrooks Davis 94929ddbbbSAlfred Perlstein int gif_clone_create(struct if_clone *, int); 95ae5a19beSBrooks Davis void gif_clone_destroy(struct ifnet *); 9653dab5feSBrooks Davis 973b16e7b2SMaxime Henrion struct if_clone gif_cloner = IF_CLONE_INITIALIZER("gif", 98ae5a19beSBrooks Davis gif_clone_create, gif_clone_destroy, 0, IF_MAXUNIT); 9953dab5feSBrooks Davis 100929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *); 101cfa1ca9dSYoshinobu Inoue 102872f786aSBrooks Davis SYSCTL_DECL(_net_link); 103872f786aSBrooks Davis SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 104872f786aSBrooks Davis "Generic Tunnel Interface"); 105686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST 106686cdd19SJun-ichiro itojun Hagino /* 107872f786aSBrooks Davis * This macro controls the default upper limitation on nesting of gif tunnels. 108686cdd19SJun-ichiro itojun Hagino * Since, setting a large value to this macro with a careless configuration 109686cdd19SJun-ichiro itojun Hagino * may introduce system crash, we don't allow any nestings by default. 110686cdd19SJun-ichiro itojun Hagino * If you need to configure nested gif tunnels, you can define this macro 111686cdd19SJun-ichiro itojun Hagino * in your kernel configuration file. However, if you do so, please be 112686cdd19SJun-ichiro itojun Hagino * careful to configure the tunnels so that it won't make a loop. 113686cdd19SJun-ichiro itojun Hagino */ 114686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1 115686cdd19SJun-ichiro itojun Hagino #endif 116686cdd19SJun-ichiro itojun Hagino static int max_gif_nesting = MAX_GIF_NEST; 117872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 118872f786aSBrooks Davis &max_gif_nesting, 0, "Max nested tunnels"); 119872f786aSBrooks Davis 120872f786aSBrooks Davis /* 121872f786aSBrooks Davis * By default, we disallow creation of multiple tunnels between the same 122872f786aSBrooks Davis * pair of addresses. Some applications require this functionality so 123872f786aSBrooks Davis * we allow control over this check here. 124872f786aSBrooks Davis */ 125872f786aSBrooks Davis #ifdef XBONEHACK 126872f786aSBrooks Davis static int parallel_tunnels = 1; 127872f786aSBrooks Davis #else 128872f786aSBrooks Davis static int parallel_tunnels = 0; 129872f786aSBrooks Davis #endif 130872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 131872f786aSBrooks Davis ¶llel_tunnels, 0, "Allow parallel tunnels?"); 132cfa1ca9dSYoshinobu Inoue 13353dab5feSBrooks Davis int 13453dab5feSBrooks Davis gif_clone_create(ifc, unit) 13553dab5feSBrooks Davis struct if_clone *ifc; 1363b16e7b2SMaxime Henrion int unit; 137cfa1ca9dSYoshinobu Inoue { 13833841545SHajimu UMEMOTO struct gif_softc *sc; 139cfa1ca9dSYoshinobu Inoue 140a163d034SWarner Losh sc = malloc (sizeof(struct gif_softc), M_GIF, M_WAITOK); 14153dab5feSBrooks Davis bzero(sc, sizeof(struct gif_softc)); 14253dab5feSBrooks Davis 14353dab5feSBrooks Davis sc->gif_if.if_softc = sc; 14453dab5feSBrooks Davis sc->gif_if.if_name = GIFNAME; 1453b16e7b2SMaxime Henrion sc->gif_if.if_unit = unit; 146686cdd19SJun-ichiro itojun Hagino 1479426aedfSHajimu UMEMOTO gifattach0(sc); 148686cdd19SJun-ichiro itojun Hagino 1499426aedfSHajimu UMEMOTO LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list); 1509426aedfSHajimu UMEMOTO return (0); 1519426aedfSHajimu UMEMOTO } 1529426aedfSHajimu UMEMOTO 1539426aedfSHajimu UMEMOTO void 1549426aedfSHajimu UMEMOTO gifattach0(sc) 1559426aedfSHajimu UMEMOTO struct gif_softc *sc; 1569426aedfSHajimu UMEMOTO { 1579426aedfSHajimu UMEMOTO 1589426aedfSHajimu UMEMOTO sc->encap_cookie4 = sc->encap_cookie6 = NULL; 1599426aedfSHajimu UMEMOTO 1609426aedfSHajimu UMEMOTO sc->gif_if.if_addrlen = 0; 161cfa1ca9dSYoshinobu Inoue sc->gif_if.if_mtu = GIF_MTU; 162cfa1ca9dSYoshinobu Inoue sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 16333841545SHajimu UMEMOTO #if 0 16433841545SHajimu UMEMOTO /* turn off ingress filter */ 16533841545SHajimu UMEMOTO sc->gif_if.if_flags |= IFF_LINK2; 16633841545SHajimu UMEMOTO #endif 167cfa1ca9dSYoshinobu Inoue sc->gif_if.if_ioctl = gif_ioctl; 168cfa1ca9dSYoshinobu Inoue sc->gif_if.if_output = gif_output; 169cfa1ca9dSYoshinobu Inoue sc->gif_if.if_type = IFT_GIF; 170686cdd19SJun-ichiro itojun Hagino sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN; 171cfa1ca9dSYoshinobu Inoue if_attach(&sc->gif_if); 172cfa1ca9dSYoshinobu Inoue bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int)); 17394408d94SBrooks Davis if (ng_gif_attach_p != NULL) 17494408d94SBrooks Davis (*ng_gif_attach_p)(&sc->gif_if); 175cfa1ca9dSYoshinobu Inoue } 176cfa1ca9dSYoshinobu Inoue 177ae5a19beSBrooks Davis void 17853dab5feSBrooks Davis gif_clone_destroy(ifp) 17953dab5feSBrooks Davis struct ifnet *ifp; 18053dab5feSBrooks Davis { 18153dab5feSBrooks Davis int err; 18253dab5feSBrooks Davis struct gif_softc *sc = ifp->if_softc; 18353dab5feSBrooks Davis 1849426aedfSHajimu UMEMOTO gif_delete_tunnel(&sc->gif_if); 1859426aedfSHajimu UMEMOTO LIST_REMOVE(sc, gif_list); 1869426aedfSHajimu UMEMOTO #ifdef INET6 18753dab5feSBrooks Davis if (sc->encap_cookie6 != NULL) { 18853dab5feSBrooks Davis err = encap_detach(sc->encap_cookie6); 18953dab5feSBrooks Davis KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 19053dab5feSBrooks Davis } 1919426aedfSHajimu UMEMOTO #endif 1929426aedfSHajimu UMEMOTO #ifdef INET 1939426aedfSHajimu UMEMOTO if (sc->encap_cookie4 != NULL) { 1949426aedfSHajimu UMEMOTO err = encap_detach(sc->encap_cookie4); 1959426aedfSHajimu UMEMOTO KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 1969426aedfSHajimu UMEMOTO } 1979426aedfSHajimu UMEMOTO #endif 19853dab5feSBrooks Davis 19994408d94SBrooks Davis if (ng_gif_detach_p != NULL) 20094408d94SBrooks Davis (*ng_gif_detach_p)(ifp); 20153dab5feSBrooks Davis bpfdetach(ifp); 20253dab5feSBrooks Davis if_detach(ifp); 20353dab5feSBrooks Davis 20453dab5feSBrooks Davis free(sc, M_GIF); 20553dab5feSBrooks Davis } 20653dab5feSBrooks Davis 20753dab5feSBrooks Davis static int 20853dab5feSBrooks Davis gifmodevent(mod, type, data) 20953dab5feSBrooks Davis module_t mod; 21053dab5feSBrooks Davis int type; 21153dab5feSBrooks Davis void *data; 21253dab5feSBrooks Davis { 21353dab5feSBrooks Davis 21453dab5feSBrooks Davis switch (type) { 21553dab5feSBrooks Davis case MOD_LOAD: 21620af0ffaSBrooks Davis LIST_INIT(&gif_softc_list); 21753dab5feSBrooks Davis if_clone_attach(&gif_cloner); 21853dab5feSBrooks Davis 21953dab5feSBrooks Davis #ifdef INET6 22053dab5feSBrooks Davis ip6_gif_hlim = GIF_HLIM; 22153dab5feSBrooks Davis #endif 22253dab5feSBrooks Davis 22353dab5feSBrooks Davis break; 22453dab5feSBrooks Davis case MOD_UNLOAD: 22553dab5feSBrooks Davis if_clone_detach(&gif_cloner); 22653dab5feSBrooks Davis 22720af0ffaSBrooks Davis while (!LIST_EMPTY(&gif_softc_list)) 22820af0ffaSBrooks Davis gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if); 22953dab5feSBrooks Davis 23053dab5feSBrooks Davis #ifdef INET6 23153dab5feSBrooks Davis ip6_gif_hlim = 0; 23253dab5feSBrooks Davis #endif 23353dab5feSBrooks Davis break; 23453dab5feSBrooks Davis } 23553dab5feSBrooks Davis return 0; 23653dab5feSBrooks Davis } 23753dab5feSBrooks Davis 23853dab5feSBrooks Davis static moduledata_t gif_mod = { 23953dab5feSBrooks Davis "if_gif", 24053dab5feSBrooks Davis gifmodevent, 24153dab5feSBrooks Davis 0 24253dab5feSBrooks Davis }; 24353dab5feSBrooks Davis 24453dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 24520af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1); 246cfa1ca9dSYoshinobu Inoue 2479426aedfSHajimu UMEMOTO int 248686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg) 249686cdd19SJun-ichiro itojun Hagino const struct mbuf *m; 250686cdd19SJun-ichiro itojun Hagino int off; 251686cdd19SJun-ichiro itojun Hagino int proto; 252686cdd19SJun-ichiro itojun Hagino void *arg; 253686cdd19SJun-ichiro itojun Hagino { 254686cdd19SJun-ichiro itojun Hagino struct ip ip; 255686cdd19SJun-ichiro itojun Hagino struct gif_softc *sc; 256686cdd19SJun-ichiro itojun Hagino 257686cdd19SJun-ichiro itojun Hagino sc = (struct gif_softc *)arg; 258686cdd19SJun-ichiro itojun Hagino if (sc == NULL) 259686cdd19SJun-ichiro itojun Hagino return 0; 260686cdd19SJun-ichiro itojun Hagino 261686cdd19SJun-ichiro itojun Hagino if ((sc->gif_if.if_flags & IFF_UP) == 0) 262686cdd19SJun-ichiro itojun Hagino return 0; 263686cdd19SJun-ichiro itojun Hagino 264686cdd19SJun-ichiro itojun Hagino /* no physical address */ 265686cdd19SJun-ichiro itojun Hagino if (!sc->gif_psrc || !sc->gif_pdst) 266686cdd19SJun-ichiro itojun Hagino return 0; 267686cdd19SJun-ichiro itojun Hagino 268686cdd19SJun-ichiro itojun Hagino switch (proto) { 269686cdd19SJun-ichiro itojun Hagino #ifdef INET 270686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV4: 271686cdd19SJun-ichiro itojun Hagino break; 272686cdd19SJun-ichiro itojun Hagino #endif 273686cdd19SJun-ichiro itojun Hagino #ifdef INET6 274686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV6: 275686cdd19SJun-ichiro itojun Hagino break; 276686cdd19SJun-ichiro itojun Hagino #endif 277686cdd19SJun-ichiro itojun Hagino default: 278686cdd19SJun-ichiro itojun Hagino return 0; 279686cdd19SJun-ichiro itojun Hagino } 280686cdd19SJun-ichiro itojun Hagino 2813bb61ca6SHajimu UMEMOTO /* Bail on short packets */ 2823bb61ca6SHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(ip)) 2833bb61ca6SHajimu UMEMOTO return 0; 2843bb61ca6SHajimu UMEMOTO 2856f4ded3aSBrooks Davis m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 286686cdd19SJun-ichiro itojun Hagino 287686cdd19SJun-ichiro itojun Hagino switch (ip.ip_v) { 288686cdd19SJun-ichiro itojun Hagino #ifdef INET 289686cdd19SJun-ichiro itojun Hagino case 4: 290686cdd19SJun-ichiro itojun Hagino if (sc->gif_psrc->sa_family != AF_INET || 291686cdd19SJun-ichiro itojun Hagino sc->gif_pdst->sa_family != AF_INET) 292686cdd19SJun-ichiro itojun Hagino return 0; 293686cdd19SJun-ichiro itojun Hagino return gif_encapcheck4(m, off, proto, arg); 294686cdd19SJun-ichiro itojun Hagino #endif 295686cdd19SJun-ichiro itojun Hagino #ifdef INET6 296686cdd19SJun-ichiro itojun Hagino case 6: 2979426aedfSHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 2989426aedfSHajimu UMEMOTO return 0; 299686cdd19SJun-ichiro itojun Hagino if (sc->gif_psrc->sa_family != AF_INET6 || 300686cdd19SJun-ichiro itojun Hagino sc->gif_pdst->sa_family != AF_INET6) 301686cdd19SJun-ichiro itojun Hagino return 0; 302686cdd19SJun-ichiro itojun Hagino return gif_encapcheck6(m, off, proto, arg); 303686cdd19SJun-ichiro itojun Hagino #endif 304686cdd19SJun-ichiro itojun Hagino default: 305686cdd19SJun-ichiro itojun Hagino return 0; 306686cdd19SJun-ichiro itojun Hagino } 307686cdd19SJun-ichiro itojun Hagino } 308686cdd19SJun-ichiro itojun Hagino 309cfa1ca9dSYoshinobu Inoue int 310cfa1ca9dSYoshinobu Inoue gif_output(ifp, m, dst, rt) 311cfa1ca9dSYoshinobu Inoue struct ifnet *ifp; 312cfa1ca9dSYoshinobu Inoue struct mbuf *m; 313cfa1ca9dSYoshinobu Inoue struct sockaddr *dst; 314cfa1ca9dSYoshinobu Inoue struct rtentry *rt; /* added in net2 */ 315cfa1ca9dSYoshinobu Inoue { 31633841545SHajimu UMEMOTO struct gif_softc *sc = (struct gif_softc*)ifp; 317cfa1ca9dSYoshinobu Inoue int error = 0; 318f0133454SMaxim Sobolev static int called = 0; /* XXX: MUTEX */ 319cfa1ca9dSYoshinobu Inoue 32010722b85SRobert Watson #ifdef MAC 32110722b85SRobert Watson error = mac_check_ifnet_transmit(ifp, m); 322e0852ce2SRobert Watson if (error) { 323e0852ce2SRobert Watson m_freem(m); 324e0852ce2SRobert Watson goto end; 325e0852ce2SRobert Watson } 32610722b85SRobert Watson #endif 32710722b85SRobert Watson 328cfa1ca9dSYoshinobu Inoue /* 329cfa1ca9dSYoshinobu Inoue * gif may cause infinite recursion calls when misconfigured. 330cfa1ca9dSYoshinobu Inoue * We'll prevent this by introducing upper limit. 331f0133454SMaxim Sobolev * XXX: this mechanism may introduce another problem about 332f0133454SMaxim Sobolev * mutual exclusion of the variable CALLED, especially if we 333f0133454SMaxim Sobolev * use kernel thread. 334cfa1ca9dSYoshinobu Inoue */ 335f0133454SMaxim Sobolev if (++called > max_gif_nesting) { 336cfa1ca9dSYoshinobu Inoue log(LOG_NOTICE, 337cfa1ca9dSYoshinobu Inoue "gif_output: recursively called too many times(%d)\n", 338f0133454SMaxim Sobolev called); 339cfa1ca9dSYoshinobu Inoue m_freem(m); 340cfa1ca9dSYoshinobu Inoue error = EIO; /* is there better errno? */ 341cfa1ca9dSYoshinobu Inoue goto end; 342cfa1ca9dSYoshinobu Inoue } 343686cdd19SJun-ichiro itojun Hagino 344cfa1ca9dSYoshinobu Inoue m->m_flags &= ~(M_BCAST|M_MCAST); 345cfa1ca9dSYoshinobu Inoue if (!(ifp->if_flags & IFF_UP) || 346cfa1ca9dSYoshinobu Inoue sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 347cfa1ca9dSYoshinobu Inoue m_freem(m); 348cfa1ca9dSYoshinobu Inoue error = ENETDOWN; 349cfa1ca9dSYoshinobu Inoue goto end; 350cfa1ca9dSYoshinobu Inoue } 351cfa1ca9dSYoshinobu Inoue 352cfa1ca9dSYoshinobu Inoue if (ifp->if_bpf) { 353cfa1ca9dSYoshinobu Inoue /* 354cfa1ca9dSYoshinobu Inoue * We need to prepend the address family as 355cfa1ca9dSYoshinobu Inoue * a four byte field. Cons up a dummy header 356cfa1ca9dSYoshinobu Inoue * to pacify bpf. This is safe because bpf 357cfa1ca9dSYoshinobu Inoue * will only read from the mbuf (i.e., it won't 358cfa1ca9dSYoshinobu Inoue * try to free it or keep a pointer a to it). 359cfa1ca9dSYoshinobu Inoue */ 360cfa1ca9dSYoshinobu Inoue struct mbuf m0; 36133841545SHajimu UMEMOTO u_int32_t af = dst->sa_family; 362cfa1ca9dSYoshinobu Inoue 363cfa1ca9dSYoshinobu Inoue m0.m_next = m; 364cfa1ca9dSYoshinobu Inoue m0.m_len = 4; 365cfa1ca9dSYoshinobu Inoue m0.m_data = (char *)⁡ 366cfa1ca9dSYoshinobu Inoue 3676fc32a24SSam Leffler BPF_MTAP(ifp, &m0); 368cfa1ca9dSYoshinobu Inoue } 369cfa1ca9dSYoshinobu Inoue ifp->if_opackets++; 370cfa1ca9dSYoshinobu Inoue ifp->if_obytes += m->m_pkthdr.len; 371cfa1ca9dSYoshinobu Inoue 37233841545SHajimu UMEMOTO /* inner AF-specific encapsulation */ 37333841545SHajimu UMEMOTO 374686cdd19SJun-ichiro itojun Hagino /* XXX should we check if our outer source is legal? */ 375686cdd19SJun-ichiro itojun Hagino 37633841545SHajimu UMEMOTO /* dispatch to output logic based on outer AF */ 377cfa1ca9dSYoshinobu Inoue switch (sc->gif_psrc->sa_family) { 378cfa1ca9dSYoshinobu Inoue #ifdef INET 379cfa1ca9dSYoshinobu Inoue case AF_INET: 380b6e28453SHajimu UMEMOTO error = in_gif_output(ifp, dst->sa_family, m); 381cfa1ca9dSYoshinobu Inoue break; 382cfa1ca9dSYoshinobu Inoue #endif 383cfa1ca9dSYoshinobu Inoue #ifdef INET6 384cfa1ca9dSYoshinobu Inoue case AF_INET6: 385b6e28453SHajimu UMEMOTO error = in6_gif_output(ifp, dst->sa_family, m); 386cfa1ca9dSYoshinobu Inoue break; 387cfa1ca9dSYoshinobu Inoue #endif 388cfa1ca9dSYoshinobu Inoue default: 389cfa1ca9dSYoshinobu Inoue m_freem(m); 390cfa1ca9dSYoshinobu Inoue error = ENETDOWN; 39133841545SHajimu UMEMOTO goto end; 392cfa1ca9dSYoshinobu Inoue } 393cfa1ca9dSYoshinobu Inoue 394cfa1ca9dSYoshinobu Inoue end: 395f0133454SMaxim Sobolev called = 0; /* reset recursion counter */ 39633841545SHajimu UMEMOTO if (error) 39733841545SHajimu UMEMOTO ifp->if_oerrors++; 398cfa1ca9dSYoshinobu Inoue return error; 399cfa1ca9dSYoshinobu Inoue } 400cfa1ca9dSYoshinobu Inoue 401cfa1ca9dSYoshinobu Inoue void 40221fb391fSHajimu UMEMOTO gif_input(m, af, ifp) 403cfa1ca9dSYoshinobu Inoue struct mbuf *m; 404cfa1ca9dSYoshinobu Inoue int af; 40521fb391fSHajimu UMEMOTO struct ifnet *ifp; 406cfa1ca9dSYoshinobu Inoue { 407df5e1987SJonathan Lemon int isr; 408cfa1ca9dSYoshinobu Inoue 40921fb391fSHajimu UMEMOTO if (ifp == NULL) { 410cfa1ca9dSYoshinobu Inoue /* just in case */ 411cfa1ca9dSYoshinobu Inoue m_freem(m); 412cfa1ca9dSYoshinobu Inoue return; 413cfa1ca9dSYoshinobu Inoue } 414cfa1ca9dSYoshinobu Inoue 41521fb391fSHajimu UMEMOTO m->m_pkthdr.rcvif = ifp; 416cfa1ca9dSYoshinobu Inoue 41710722b85SRobert Watson #ifdef MAC 41821fb391fSHajimu UMEMOTO mac_create_mbuf_from_ifnet(ifp, m); 41910722b85SRobert Watson #endif 42010722b85SRobert Watson 42121fb391fSHajimu UMEMOTO if (ifp->if_bpf) { 422cfa1ca9dSYoshinobu Inoue /* 423cfa1ca9dSYoshinobu Inoue * We need to prepend the address family as 424cfa1ca9dSYoshinobu Inoue * a four byte field. Cons up a dummy header 425cfa1ca9dSYoshinobu Inoue * to pacify bpf. This is safe because bpf 426cfa1ca9dSYoshinobu Inoue * will only read from the mbuf (i.e., it won't 427cfa1ca9dSYoshinobu Inoue * try to free it or keep a pointer a to it). 428cfa1ca9dSYoshinobu Inoue */ 429cfa1ca9dSYoshinobu Inoue struct mbuf m0; 43033841545SHajimu UMEMOTO u_int32_t af1 = af; 431cfa1ca9dSYoshinobu Inoue 432cfa1ca9dSYoshinobu Inoue m0.m_next = m; 433cfa1ca9dSYoshinobu Inoue m0.m_len = 4; 43433841545SHajimu UMEMOTO m0.m_data = (char *)&af1; 435cfa1ca9dSYoshinobu Inoue 4366fc32a24SSam Leffler BPF_MTAP(ifp, &m0); 437cfa1ca9dSYoshinobu Inoue } 438cfa1ca9dSYoshinobu Inoue 43994408d94SBrooks Davis if (ng_gif_input_p != NULL) { 44021fb391fSHajimu UMEMOTO (*ng_gif_input_p)(ifp, &m, af); 44194408d94SBrooks Davis if (m == NULL) 44294408d94SBrooks Davis return; 44394408d94SBrooks Davis } 44494408d94SBrooks Davis 445cfa1ca9dSYoshinobu Inoue /* 446cfa1ca9dSYoshinobu Inoue * Put the packet to the network layer input queue according to the 447cfa1ca9dSYoshinobu Inoue * specified address family. 448cfa1ca9dSYoshinobu Inoue * Note: older versions of gif_input directly called network layer 449cfa1ca9dSYoshinobu Inoue * input functions, e.g. ip6_input, here. We changed the policy to 450cfa1ca9dSYoshinobu Inoue * prevent too many recursive calls of such input functions, which 451cfa1ca9dSYoshinobu Inoue * might cause kernel panic. But the change may introduce another 452cfa1ca9dSYoshinobu Inoue * problem; if the input queue is full, packets are discarded. 45388ff5695SSUZUKI Shinsuke * The kernel stack overflow really happened, and we believed 45488ff5695SSUZUKI Shinsuke * queue-full rarely occurs, so we changed the policy. 455cfa1ca9dSYoshinobu Inoue */ 456cfa1ca9dSYoshinobu Inoue switch (af) { 457cfa1ca9dSYoshinobu Inoue #ifdef INET 458cfa1ca9dSYoshinobu Inoue case AF_INET: 459cfa1ca9dSYoshinobu Inoue isr = NETISR_IP; 460cfa1ca9dSYoshinobu Inoue break; 461cfa1ca9dSYoshinobu Inoue #endif 462cfa1ca9dSYoshinobu Inoue #ifdef INET6 463cfa1ca9dSYoshinobu Inoue case AF_INET6: 464cfa1ca9dSYoshinobu Inoue isr = NETISR_IPV6; 465cfa1ca9dSYoshinobu Inoue break; 466cfa1ca9dSYoshinobu Inoue #endif 467cfa1ca9dSYoshinobu Inoue default: 46894408d94SBrooks Davis if (ng_gif_input_orphan_p != NULL) 46921fb391fSHajimu UMEMOTO (*ng_gif_input_orphan_p)(ifp, m, af); 47094408d94SBrooks Davis else 471cfa1ca9dSYoshinobu Inoue m_freem(m); 472cfa1ca9dSYoshinobu Inoue return; 473cfa1ca9dSYoshinobu Inoue } 474cfa1ca9dSYoshinobu Inoue 47521fb391fSHajimu UMEMOTO ifp->if_ipackets++; 47621fb391fSHajimu UMEMOTO ifp->if_ibytes += m->m_pkthdr.len; 4771cafed39SJonathan Lemon netisr_dispatch(isr, m); 478cfa1ca9dSYoshinobu Inoue } 479cfa1ca9dSYoshinobu Inoue 480686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 481cfa1ca9dSYoshinobu Inoue int 482cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data) 483cfa1ca9dSYoshinobu Inoue struct ifnet *ifp; 484cfa1ca9dSYoshinobu Inoue u_long cmd; 485cfa1ca9dSYoshinobu Inoue caddr_t data; 486cfa1ca9dSYoshinobu Inoue { 487cfa1ca9dSYoshinobu Inoue struct gif_softc *sc = (struct gif_softc*)ifp; 488cfa1ca9dSYoshinobu Inoue struct ifreq *ifr = (struct ifreq*)data; 489cfa1ca9dSYoshinobu Inoue int error = 0, size; 490686cdd19SJun-ichiro itojun Hagino struct sockaddr *dst, *src; 4913bb61ca6SHajimu UMEMOTO #ifdef SIOCSIFMTU /* xxx */ 4923bb61ca6SHajimu UMEMOTO u_long mtu; 4933bb61ca6SHajimu UMEMOTO #endif 494cfa1ca9dSYoshinobu Inoue 495cfa1ca9dSYoshinobu Inoue switch (cmd) { 496cfa1ca9dSYoshinobu Inoue case SIOCSIFADDR: 4979426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_UP; 498cfa1ca9dSYoshinobu Inoue break; 499cfa1ca9dSYoshinobu Inoue 500cfa1ca9dSYoshinobu Inoue case SIOCSIFDSTADDR: 501cfa1ca9dSYoshinobu Inoue break; 502cfa1ca9dSYoshinobu Inoue 503cfa1ca9dSYoshinobu Inoue case SIOCADDMULTI: 504cfa1ca9dSYoshinobu Inoue case SIOCDELMULTI: 505cfa1ca9dSYoshinobu Inoue break; 506cfa1ca9dSYoshinobu Inoue 507686cdd19SJun-ichiro itojun Hagino #ifdef SIOCSIFMTU /* xxx */ 508cfa1ca9dSYoshinobu Inoue case SIOCGIFMTU: 509cfa1ca9dSYoshinobu Inoue break; 510686cdd19SJun-ichiro itojun Hagino 511cfa1ca9dSYoshinobu Inoue case SIOCSIFMTU: 512cfa1ca9dSYoshinobu Inoue mtu = ifr->ifr_mtu; 5133bb61ca6SHajimu UMEMOTO if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 514cfa1ca9dSYoshinobu Inoue return (EINVAL); 515cfa1ca9dSYoshinobu Inoue ifp->if_mtu = mtu; 516cfa1ca9dSYoshinobu Inoue break; 517686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */ 518cfa1ca9dSYoshinobu Inoue 5193bb61ca6SHajimu UMEMOTO #ifdef INET 520cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR: 5213bb61ca6SHajimu UMEMOTO #endif 522cfa1ca9dSYoshinobu Inoue #ifdef INET6 523cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR_IN6: 524cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 52533841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 526686cdd19SJun-ichiro itojun Hagino switch (cmd) { 52733841545SHajimu UMEMOTO #ifdef INET 528686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 529cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 530cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_addr); 531cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 532cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_dstaddr); 533cfa1ca9dSYoshinobu Inoue break; 53433841545SHajimu UMEMOTO #endif 535cfa1ca9dSYoshinobu Inoue #ifdef INET6 536686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 537cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 538cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_addr); 539cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 540cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_dstaddr); 541686cdd19SJun-ichiro itojun Hagino break; 542686cdd19SJun-ichiro itojun Hagino #endif 54333841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 54433841545SHajimu UMEMOTO src = (struct sockaddr *) 54533841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->addr); 54633841545SHajimu UMEMOTO dst = (struct sockaddr *) 54733841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->dstaddr); 5489426aedfSHajimu UMEMOTO break; 5496f4ded3aSBrooks Davis default: 5509426aedfSHajimu UMEMOTO return EINVAL; 55133841545SHajimu UMEMOTO } 55233841545SHajimu UMEMOTO 55333841545SHajimu UMEMOTO /* sa_family must be equal */ 55433841545SHajimu UMEMOTO if (src->sa_family != dst->sa_family) 55533841545SHajimu UMEMOTO return EINVAL; 55633841545SHajimu UMEMOTO 55733841545SHajimu UMEMOTO /* validate sa_len */ 55833841545SHajimu UMEMOTO switch (src->sa_family) { 55933841545SHajimu UMEMOTO #ifdef INET 56033841545SHajimu UMEMOTO case AF_INET: 56133841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in)) 56233841545SHajimu UMEMOTO return EINVAL; 56333841545SHajimu UMEMOTO break; 56433841545SHajimu UMEMOTO #endif 56533841545SHajimu UMEMOTO #ifdef INET6 56633841545SHajimu UMEMOTO case AF_INET6: 56733841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in6)) 56833841545SHajimu UMEMOTO return EINVAL; 56933841545SHajimu UMEMOTO break; 57033841545SHajimu UMEMOTO #endif 57133841545SHajimu UMEMOTO default: 57233841545SHajimu UMEMOTO return EAFNOSUPPORT; 57333841545SHajimu UMEMOTO } 57433841545SHajimu UMEMOTO switch (dst->sa_family) { 57533841545SHajimu UMEMOTO #ifdef INET 57633841545SHajimu UMEMOTO case AF_INET: 57733841545SHajimu UMEMOTO if (dst->sa_len != sizeof(struct sockaddr_in)) 57833841545SHajimu UMEMOTO return EINVAL; 57933841545SHajimu UMEMOTO break; 58033841545SHajimu UMEMOTO #endif 58133841545SHajimu UMEMOTO #ifdef INET6 58233841545SHajimu UMEMOTO case AF_INET6: 58333841545SHajimu UMEMOTO if (dst->sa_len != sizeof(struct sockaddr_in6)) 58433841545SHajimu UMEMOTO return EINVAL; 58533841545SHajimu UMEMOTO break; 58633841545SHajimu UMEMOTO #endif 58733841545SHajimu UMEMOTO default: 58833841545SHajimu UMEMOTO return EAFNOSUPPORT; 58933841545SHajimu UMEMOTO } 59033841545SHajimu UMEMOTO 59133841545SHajimu UMEMOTO /* check sa_family looks sane for the cmd */ 59233841545SHajimu UMEMOTO switch (cmd) { 59333841545SHajimu UMEMOTO case SIOCSIFPHYADDR: 59433841545SHajimu UMEMOTO if (src->sa_family == AF_INET) 59533841545SHajimu UMEMOTO break; 59633841545SHajimu UMEMOTO return EAFNOSUPPORT; 59733841545SHajimu UMEMOTO #ifdef INET6 59833841545SHajimu UMEMOTO case SIOCSIFPHYADDR_IN6: 59933841545SHajimu UMEMOTO if (src->sa_family == AF_INET6) 60033841545SHajimu UMEMOTO break; 60133841545SHajimu UMEMOTO return EAFNOSUPPORT; 60233841545SHajimu UMEMOTO #endif /* INET6 */ 60333841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 60433841545SHajimu UMEMOTO /* checks done in the above */ 60533841545SHajimu UMEMOTO break; 606686cdd19SJun-ichiro itojun Hagino } 607cfa1ca9dSYoshinobu Inoue 6089426aedfSHajimu UMEMOTO error = gif_set_tunnel(&sc->gif_if, src, dst); 609cfa1ca9dSYoshinobu Inoue break; 610cfa1ca9dSYoshinobu Inoue 611686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR 612686cdd19SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 6139426aedfSHajimu UMEMOTO gif_delete_tunnel(&sc->gif_if); 614686cdd19SJun-ichiro itojun Hagino break; 615686cdd19SJun-ichiro itojun Hagino #endif 616686cdd19SJun-ichiro itojun Hagino 617cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR: 618cfa1ca9dSYoshinobu Inoue #ifdef INET6 619cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR_IN6: 620cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 621cfa1ca9dSYoshinobu Inoue if (sc->gif_psrc == NULL) { 622cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 623cfa1ca9dSYoshinobu Inoue goto bad; 624cfa1ca9dSYoshinobu Inoue } 625cfa1ca9dSYoshinobu Inoue src = sc->gif_psrc; 62633841545SHajimu UMEMOTO switch (cmd) { 627cfa1ca9dSYoshinobu Inoue #ifdef INET 62833841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 629cfa1ca9dSYoshinobu Inoue dst = &ifr->ifr_addr; 63033841545SHajimu UMEMOTO size = sizeof(ifr->ifr_addr); 631cfa1ca9dSYoshinobu Inoue break; 632cfa1ca9dSYoshinobu Inoue #endif /* INET */ 633cfa1ca9dSYoshinobu Inoue #ifdef INET6 63433841545SHajimu UMEMOTO case SIOCGIFPSRCADDR_IN6: 635cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 636cfa1ca9dSYoshinobu Inoue &(((struct in6_ifreq *)data)->ifr_addr); 63733841545SHajimu UMEMOTO size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 638cfa1ca9dSYoshinobu Inoue break; 639cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 640cfa1ca9dSYoshinobu Inoue default: 641cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 642cfa1ca9dSYoshinobu Inoue goto bad; 643cfa1ca9dSYoshinobu Inoue } 64433841545SHajimu UMEMOTO if (src->sa_len > size) 64533841545SHajimu UMEMOTO return EINVAL; 64633841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 647cfa1ca9dSYoshinobu Inoue break; 648cfa1ca9dSYoshinobu Inoue 649cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR: 650cfa1ca9dSYoshinobu Inoue #ifdef INET6 651cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR_IN6: 652cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 653cfa1ca9dSYoshinobu Inoue if (sc->gif_pdst == NULL) { 654cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 655cfa1ca9dSYoshinobu Inoue goto bad; 656cfa1ca9dSYoshinobu Inoue } 657cfa1ca9dSYoshinobu Inoue src = sc->gif_pdst; 65833841545SHajimu UMEMOTO switch (cmd) { 659cfa1ca9dSYoshinobu Inoue #ifdef INET 66033841545SHajimu UMEMOTO case SIOCGIFPDSTADDR: 661cfa1ca9dSYoshinobu Inoue dst = &ifr->ifr_addr; 66233841545SHajimu UMEMOTO size = sizeof(ifr->ifr_addr); 663cfa1ca9dSYoshinobu Inoue break; 664cfa1ca9dSYoshinobu Inoue #endif /* INET */ 665cfa1ca9dSYoshinobu Inoue #ifdef INET6 66633841545SHajimu UMEMOTO case SIOCGIFPDSTADDR_IN6: 667cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 668cfa1ca9dSYoshinobu Inoue &(((struct in6_ifreq *)data)->ifr_addr); 66933841545SHajimu UMEMOTO size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 670cfa1ca9dSYoshinobu Inoue break; 671cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 672cfa1ca9dSYoshinobu Inoue default: 673cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 674cfa1ca9dSYoshinobu Inoue goto bad; 675cfa1ca9dSYoshinobu Inoue } 67633841545SHajimu UMEMOTO if (src->sa_len > size) 67733841545SHajimu UMEMOTO return EINVAL; 67833841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 67933841545SHajimu UMEMOTO break; 68033841545SHajimu UMEMOTO 68133841545SHajimu UMEMOTO case SIOCGLIFPHYADDR: 68233841545SHajimu UMEMOTO if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 68333841545SHajimu UMEMOTO error = EADDRNOTAVAIL; 68433841545SHajimu UMEMOTO goto bad; 68533841545SHajimu UMEMOTO } 68633841545SHajimu UMEMOTO 68733841545SHajimu UMEMOTO /* copy src */ 68833841545SHajimu UMEMOTO src = sc->gif_psrc; 68933841545SHajimu UMEMOTO dst = (struct sockaddr *) 69033841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->addr); 69133841545SHajimu UMEMOTO size = sizeof(((struct if_laddrreq *)data)->addr); 69233841545SHajimu UMEMOTO if (src->sa_len > size) 69333841545SHajimu UMEMOTO return EINVAL; 69433841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 69533841545SHajimu UMEMOTO 69633841545SHajimu UMEMOTO /* copy dst */ 69733841545SHajimu UMEMOTO src = sc->gif_pdst; 69833841545SHajimu UMEMOTO dst = (struct sockaddr *) 69933841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->dstaddr); 70033841545SHajimu UMEMOTO size = sizeof(((struct if_laddrreq *)data)->dstaddr); 70133841545SHajimu UMEMOTO if (src->sa_len > size) 70233841545SHajimu UMEMOTO return EINVAL; 70333841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 704cfa1ca9dSYoshinobu Inoue break; 705cfa1ca9dSYoshinobu Inoue 706cfa1ca9dSYoshinobu Inoue case SIOCSIFFLAGS: 707686cdd19SJun-ichiro itojun Hagino /* if_ioctl() takes care of it */ 708cfa1ca9dSYoshinobu Inoue break; 709cfa1ca9dSYoshinobu Inoue 710cfa1ca9dSYoshinobu Inoue default: 711cfa1ca9dSYoshinobu Inoue error = EINVAL; 712cfa1ca9dSYoshinobu Inoue break; 713cfa1ca9dSYoshinobu Inoue } 714cfa1ca9dSYoshinobu Inoue bad: 715cfa1ca9dSYoshinobu Inoue return error; 716cfa1ca9dSYoshinobu Inoue } 71753dab5feSBrooks Davis 7189426aedfSHajimu UMEMOTO int 7199426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst) 7209426aedfSHajimu UMEMOTO struct ifnet *ifp; 7219426aedfSHajimu UMEMOTO struct sockaddr *src; 7229426aedfSHajimu UMEMOTO struct sockaddr *dst; 72353dab5feSBrooks Davis { 7249426aedfSHajimu UMEMOTO struct gif_softc *sc = (struct gif_softc *)ifp; 7259426aedfSHajimu UMEMOTO struct gif_softc *sc2; 7269426aedfSHajimu UMEMOTO struct sockaddr *osrc, *odst, *sa; 7279426aedfSHajimu UMEMOTO int s; 7289426aedfSHajimu UMEMOTO int error = 0; 7299426aedfSHajimu UMEMOTO 7309426aedfSHajimu UMEMOTO s = splnet(); 7319426aedfSHajimu UMEMOTO 7329426aedfSHajimu UMEMOTO LIST_FOREACH(sc2, &gif_softc_list, gif_list) { 7339426aedfSHajimu UMEMOTO if (sc2 == sc) 7349426aedfSHajimu UMEMOTO continue; 7359426aedfSHajimu UMEMOTO if (!sc2->gif_pdst || !sc2->gif_psrc) 7369426aedfSHajimu UMEMOTO continue; 7379426aedfSHajimu UMEMOTO if (sc2->gif_pdst->sa_family != dst->sa_family || 7389426aedfSHajimu UMEMOTO sc2->gif_pdst->sa_len != dst->sa_len || 7399426aedfSHajimu UMEMOTO sc2->gif_psrc->sa_family != src->sa_family || 7409426aedfSHajimu UMEMOTO sc2->gif_psrc->sa_len != src->sa_len) 7419426aedfSHajimu UMEMOTO continue; 7429426aedfSHajimu UMEMOTO 7439426aedfSHajimu UMEMOTO /* 7449426aedfSHajimu UMEMOTO * Disallow parallel tunnels unless instructed 7459426aedfSHajimu UMEMOTO * otherwise. 7469426aedfSHajimu UMEMOTO */ 7479426aedfSHajimu UMEMOTO if (!parallel_tunnels && 7489426aedfSHajimu UMEMOTO bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 7499426aedfSHajimu UMEMOTO bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 7509426aedfSHajimu UMEMOTO error = EADDRNOTAVAIL; 7519426aedfSHajimu UMEMOTO goto bad; 7529426aedfSHajimu UMEMOTO } 7539426aedfSHajimu UMEMOTO 7549426aedfSHajimu UMEMOTO /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 7559426aedfSHajimu UMEMOTO } 7569426aedfSHajimu UMEMOTO 7579426aedfSHajimu UMEMOTO /* XXX we can detach from both, but be polite just in case */ 7589426aedfSHajimu UMEMOTO if (sc->gif_psrc) 7599426aedfSHajimu UMEMOTO switch (sc->gif_psrc->sa_family) { 7609426aedfSHajimu UMEMOTO #ifdef INET 7619426aedfSHajimu UMEMOTO case AF_INET: 7629426aedfSHajimu UMEMOTO (void)in_gif_detach(sc); 7639426aedfSHajimu UMEMOTO break; 7649426aedfSHajimu UMEMOTO #endif 7659426aedfSHajimu UMEMOTO #ifdef INET6 7669426aedfSHajimu UMEMOTO case AF_INET6: 7679426aedfSHajimu UMEMOTO (void)in6_gif_detach(sc); 7689426aedfSHajimu UMEMOTO break; 7699426aedfSHajimu UMEMOTO #endif 7709426aedfSHajimu UMEMOTO } 7719426aedfSHajimu UMEMOTO 7729426aedfSHajimu UMEMOTO osrc = sc->gif_psrc; 773a163d034SWarner Losh sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 7749426aedfSHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 7759426aedfSHajimu UMEMOTO sc->gif_psrc = sa; 7769426aedfSHajimu UMEMOTO 7779426aedfSHajimu UMEMOTO odst = sc->gif_pdst; 778a163d034SWarner Losh sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 7799426aedfSHajimu UMEMOTO bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 7809426aedfSHajimu UMEMOTO sc->gif_pdst = sa; 7819426aedfSHajimu UMEMOTO 7829426aedfSHajimu UMEMOTO switch (sc->gif_psrc->sa_family) { 7839426aedfSHajimu UMEMOTO #ifdef INET 7849426aedfSHajimu UMEMOTO case AF_INET: 7859426aedfSHajimu UMEMOTO error = in_gif_attach(sc); 7869426aedfSHajimu UMEMOTO break; 7879426aedfSHajimu UMEMOTO #endif 7889426aedfSHajimu UMEMOTO #ifdef INET6 7899426aedfSHajimu UMEMOTO case AF_INET6: 7909426aedfSHajimu UMEMOTO error = in6_gif_attach(sc); 7919426aedfSHajimu UMEMOTO break; 7929426aedfSHajimu UMEMOTO #endif 7939426aedfSHajimu UMEMOTO } 7949426aedfSHajimu UMEMOTO if (error) { 7959426aedfSHajimu UMEMOTO /* rollback */ 7969426aedfSHajimu UMEMOTO free((caddr_t)sc->gif_psrc, M_IFADDR); 7979426aedfSHajimu UMEMOTO free((caddr_t)sc->gif_pdst, M_IFADDR); 7989426aedfSHajimu UMEMOTO sc->gif_psrc = osrc; 7999426aedfSHajimu UMEMOTO sc->gif_pdst = odst; 8009426aedfSHajimu UMEMOTO goto bad; 8019426aedfSHajimu UMEMOTO } 8029426aedfSHajimu UMEMOTO 8039426aedfSHajimu UMEMOTO if (osrc) 8049426aedfSHajimu UMEMOTO free((caddr_t)osrc, M_IFADDR); 8059426aedfSHajimu UMEMOTO if (odst) 8069426aedfSHajimu UMEMOTO free((caddr_t)odst, M_IFADDR); 8079426aedfSHajimu UMEMOTO 8089426aedfSHajimu UMEMOTO if (sc->gif_psrc && sc->gif_pdst) 8099426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_RUNNING; 8109426aedfSHajimu UMEMOTO else 8119426aedfSHajimu UMEMOTO ifp->if_flags &= ~IFF_RUNNING; 8129426aedfSHajimu UMEMOTO splx(s); 8139426aedfSHajimu UMEMOTO 8149426aedfSHajimu UMEMOTO return 0; 8159426aedfSHajimu UMEMOTO 8169426aedfSHajimu UMEMOTO bad: 8179426aedfSHajimu UMEMOTO if (sc->gif_psrc && sc->gif_pdst) 8189426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_RUNNING; 8199426aedfSHajimu UMEMOTO else 8209426aedfSHajimu UMEMOTO ifp->if_flags &= ~IFF_RUNNING; 8219426aedfSHajimu UMEMOTO splx(s); 8229426aedfSHajimu UMEMOTO 8239426aedfSHajimu UMEMOTO return error; 8249426aedfSHajimu UMEMOTO } 8259426aedfSHajimu UMEMOTO 8269426aedfSHajimu UMEMOTO void 8279426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp) 8289426aedfSHajimu UMEMOTO struct ifnet *ifp; 8299426aedfSHajimu UMEMOTO { 8309426aedfSHajimu UMEMOTO struct gif_softc *sc = (struct gif_softc *)ifp; 8319426aedfSHajimu UMEMOTO int s; 8329426aedfSHajimu UMEMOTO 8339426aedfSHajimu UMEMOTO s = splnet(); 83453dab5feSBrooks Davis 83553dab5feSBrooks Davis if (sc->gif_psrc) { 83653dab5feSBrooks Davis free((caddr_t)sc->gif_psrc, M_IFADDR); 83753dab5feSBrooks Davis sc->gif_psrc = NULL; 83853dab5feSBrooks Davis } 83953dab5feSBrooks Davis if (sc->gif_pdst) { 84053dab5feSBrooks Davis free((caddr_t)sc->gif_pdst, M_IFADDR); 84153dab5feSBrooks Davis sc->gif_pdst = NULL; 84253dab5feSBrooks Davis } 8439426aedfSHajimu UMEMOTO /* it is safe to detach from both */ 8449426aedfSHajimu UMEMOTO #ifdef INET 8459426aedfSHajimu UMEMOTO (void)in_gif_detach(sc); 8469426aedfSHajimu UMEMOTO #endif 8479426aedfSHajimu UMEMOTO #ifdef INET6 8489426aedfSHajimu UMEMOTO (void)in6_gif_detach(sc); 8499426aedfSHajimu UMEMOTO #endif 8509426aedfSHajimu UMEMOTO 8519426aedfSHajimu UMEMOTO if (sc->gif_psrc && sc->gif_pdst) 8529426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_RUNNING; 8539426aedfSHajimu UMEMOTO else 8549426aedfSHajimu UMEMOTO ifp->if_flags &= ~IFF_RUNNING; 8559426aedfSHajimu UMEMOTO splx(s); 85653dab5feSBrooks Davis } 857