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 4c398230bSWarner Losh /*- 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" 35cfa1ca9dSYoshinobu Inoue 36cfa1ca9dSYoshinobu Inoue #include <sys/param.h> 37cfa1ca9dSYoshinobu Inoue #include <sys/systm.h> 38e3416ab0SBjoern A. Zeeb #include <sys/jail.h> 39cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h> 40cfa1ca9dSYoshinobu Inoue #include <sys/malloc.h> 41cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h> 425dba30f1SPoul-Henning Kamp #include <sys/module.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> 49dbe59260SHiroki Sato #include <sys/priv.h> 508b07e49aSJulian Elischer #include <sys/proc.h> 51686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h> 5253dab5feSBrooks Davis #include <sys/conf.h> 53cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h> 54cfa1ca9dSYoshinobu Inoue 55cfa1ca9dSYoshinobu Inoue #include <net/if.h> 5676039bc8SGleb Smirnoff #include <net/if_var.h> 57f889d2efSBrooks Davis #include <net/if_clone.h> 58cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 59cfa1ca9dSYoshinobu Inoue #include <net/netisr.h> 60cfa1ca9dSYoshinobu Inoue #include <net/route.h> 61cfa1ca9dSYoshinobu Inoue #include <net/bpf.h> 62530c0060SRobert Watson #include <net/vnet.h> 63cfa1ca9dSYoshinobu Inoue 64cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 65cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h> 66cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h> 6733841545SHajimu UMEMOTO #ifdef INET 6833841545SHajimu UMEMOTO #include <netinet/in_var.h> 69cfa1ca9dSYoshinobu Inoue #include <netinet/in_gif.h> 7053dab5feSBrooks Davis #include <netinet/ip_var.h> 71cfa1ca9dSYoshinobu Inoue #endif /* INET */ 72cfa1ca9dSYoshinobu Inoue 73cfa1ca9dSYoshinobu Inoue #ifdef INET6 74cfa1ca9dSYoshinobu Inoue #ifndef INET 75cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 76cfa1ca9dSYoshinobu Inoue #endif 77cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h> 78cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 79cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 80a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 81cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_gif.h> 82686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h> 83cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 84cfa1ca9dSYoshinobu Inoue 85686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h> 8673ff045cSAndrew Thompson #include <net/ethernet.h> 8773ff045cSAndrew Thompson #include <net/if_bridgevar.h> 88cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h> 89cfa1ca9dSYoshinobu Inoue 90aed55708SRobert Watson #include <security/mac/mac_framework.h> 91aed55708SRobert Watson 9242a58907SGleb Smirnoff static const char gifname[] = "gif"; 93686cdd19SJun-ichiro itojun Hagino 9417d5cb2dSRobert Watson /* 958c7e1947SRuslan Ermilov * gif_mtx protects the global gif_softc_list. 9617d5cb2dSRobert Watson */ 9717d5cb2dSRobert Watson static struct mtx gif_mtx; 9853dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 993e288e62SDimitry Andric static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list); 1001e77c105SRobert Watson #define V_gif_softc_list VNET(gif_softc_list) 101eddfbb76SRobert Watson 10294408d94SBrooks Davis void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 10394408d94SBrooks Davis void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 10494408d94SBrooks Davis void (*ng_gif_attach_p)(struct ifnet *ifp); 10594408d94SBrooks Davis void (*ng_gif_detach_p)(struct ifnet *ifp); 10694408d94SBrooks Davis 10773ff045cSAndrew Thompson static void gif_start(struct ifnet *); 1086b7330e2SSam Leffler static int gif_clone_create(struct if_clone *, int, caddr_t); 109bb2bfb4fSBrooks Davis static void gif_clone_destroy(struct ifnet *); 11042a58907SGleb Smirnoff static struct if_clone *gif_cloner; 11153dab5feSBrooks Davis 112929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *); 113cfa1ca9dSYoshinobu Inoue 114872f786aSBrooks Davis SYSCTL_DECL(_net_link); 1156472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 116872f786aSBrooks Davis "Generic Tunnel Interface"); 117686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST 118686cdd19SJun-ichiro itojun Hagino /* 119872f786aSBrooks Davis * This macro controls the default upper limitation on nesting of gif tunnels. 120686cdd19SJun-ichiro itojun Hagino * Since, setting a large value to this macro with a careless configuration 121686cdd19SJun-ichiro itojun Hagino * may introduce system crash, we don't allow any nestings by default. 122686cdd19SJun-ichiro itojun Hagino * If you need to configure nested gif tunnels, you can define this macro 123686cdd19SJun-ichiro itojun Hagino * in your kernel configuration file. However, if you do so, please be 124686cdd19SJun-ichiro itojun Hagino * careful to configure the tunnels so that it won't make a loop. 125686cdd19SJun-ichiro itojun Hagino */ 126686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1 127686cdd19SJun-ichiro itojun Hagino #endif 1283e288e62SDimitry Andric static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST; 129d0728d71SRobert Watson #define V_max_gif_nesting VNET(max_gif_nesting) 130eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 131eddfbb76SRobert Watson &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels"); 1328b615593SMarko Zec 133872f786aSBrooks Davis /* 134872f786aSBrooks Davis * By default, we disallow creation of multiple tunnels between the same 135872f786aSBrooks Davis * pair of addresses. Some applications require this functionality so 136872f786aSBrooks Davis * we allow control over this check here. 137872f786aSBrooks Davis */ 138d0728d71SRobert Watson #ifdef XBONEHACK 1393e288e62SDimitry Andric static VNET_DEFINE(int, parallel_tunnels) = 1; 140d0728d71SRobert Watson #else 1413e288e62SDimitry Andric static VNET_DEFINE(int, parallel_tunnels) = 0; 142d0728d71SRobert Watson #endif 143d0728d71SRobert Watson #define V_parallel_tunnels VNET(parallel_tunnels) 144eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 145eddfbb76SRobert Watson &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?"); 146cfa1ca9dSYoshinobu Inoue 14756abdd33SAndrew Thompson /* copy from src/sys/net/if_ethersubr.c */ 14856abdd33SAndrew Thompson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 14956abdd33SAndrew Thompson { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 15056abdd33SAndrew Thompson #ifndef ETHER_IS_BROADCAST 15156abdd33SAndrew Thompson #define ETHER_IS_BROADCAST(addr) \ 15256abdd33SAndrew Thompson (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 15356abdd33SAndrew Thompson #endif 15456abdd33SAndrew Thompson 155bb2bfb4fSBrooks Davis static int 156*c72a5d5dSAndrey V. Elsukov gif_clone_create(struct if_clone *ifc, int unit, caddr_t params) 157cfa1ca9dSYoshinobu Inoue { 15833841545SHajimu UMEMOTO struct gif_softc *sc; 159cfa1ca9dSYoshinobu Inoue 160e1a8c3dcSBruce M Simpson sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 1618b07e49aSJulian Elischer sc->gif_fibnum = curthread->td_proc->p_fibnum; 162fc74a9f9SBrooks Davis GIF2IFP(sc) = if_alloc(IFT_GIF); 163fc74a9f9SBrooks Davis if (GIF2IFP(sc) == NULL) { 164fc74a9f9SBrooks Davis free(sc, M_GIF); 165fc74a9f9SBrooks Davis return (ENOSPC); 166fc74a9f9SBrooks Davis } 16753dab5feSBrooks Davis 16825af0bb5SGleb Smirnoff GIF_LOCK_INIT(sc); 16925af0bb5SGleb Smirnoff 170fc74a9f9SBrooks Davis GIF2IFP(sc)->if_softc = sc; 17142a58907SGleb Smirnoff if_initname(GIF2IFP(sc), gifname, unit); 172686cdd19SJun-ichiro itojun Hagino 1739426aedfSHajimu UMEMOTO sc->encap_cookie4 = sc->encap_cookie6 = NULL; 174e9f947e2SHiroki Sato sc->gif_options = 0; 1759426aedfSHajimu UMEMOTO 176fc74a9f9SBrooks Davis GIF2IFP(sc)->if_addrlen = 0; 177fc74a9f9SBrooks Davis GIF2IFP(sc)->if_mtu = GIF_MTU; 178fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 17933841545SHajimu UMEMOTO #if 0 18033841545SHajimu UMEMOTO /* turn off ingress filter */ 181fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags |= IFF_LINK2; 18233841545SHajimu UMEMOTO #endif 183fc74a9f9SBrooks Davis GIF2IFP(sc)->if_ioctl = gif_ioctl; 18473ff045cSAndrew Thompson GIF2IFP(sc)->if_start = gif_start; 185fc74a9f9SBrooks Davis GIF2IFP(sc)->if_output = gif_output; 186e50d35e6SMaxim Sobolev GIF2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen; 187fc74a9f9SBrooks Davis if_attach(GIF2IFP(sc)); 18801399f34SDavid Malone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 18994408d94SBrooks Davis if (ng_gif_attach_p != NULL) 190fc74a9f9SBrooks Davis (*ng_gif_attach_p)(GIF2IFP(sc)); 19125af0bb5SGleb Smirnoff 19225af0bb5SGleb Smirnoff mtx_lock(&gif_mtx); 193603724d3SBjoern A. Zeeb LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list); 19425af0bb5SGleb Smirnoff mtx_unlock(&gif_mtx); 19525af0bb5SGleb Smirnoff 19625af0bb5SGleb Smirnoff return (0); 197cfa1ca9dSYoshinobu Inoue } 198cfa1ca9dSYoshinobu Inoue 19917d5cb2dSRobert Watson static void 200*c72a5d5dSAndrey V. Elsukov gif_clone_destroy(struct ifnet *ifp) 20153dab5feSBrooks Davis { 202e0de57f9SBjoern A. Zeeb #if defined(INET) || defined(INET6) 20353dab5feSBrooks Davis int err; 204e0de57f9SBjoern A. Zeeb #endif 205febd0759SAndrew Thompson struct gif_softc *sc = ifp->if_softc; 206febd0759SAndrew Thompson 207febd0759SAndrew Thompson mtx_lock(&gif_mtx); 208febd0759SAndrew Thompson LIST_REMOVE(sc, gif_list); 209febd0759SAndrew Thompson mtx_unlock(&gif_mtx); 21053dab5feSBrooks Davis 21117d5cb2dSRobert Watson gif_delete_tunnel(ifp); 2129426aedfSHajimu UMEMOTO #ifdef INET6 21353dab5feSBrooks Davis if (sc->encap_cookie6 != NULL) { 21453dab5feSBrooks Davis err = encap_detach(sc->encap_cookie6); 21553dab5feSBrooks Davis KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 21653dab5feSBrooks Davis } 2179426aedfSHajimu UMEMOTO #endif 2189426aedfSHajimu UMEMOTO #ifdef INET 2199426aedfSHajimu UMEMOTO if (sc->encap_cookie4 != NULL) { 2209426aedfSHajimu UMEMOTO err = encap_detach(sc->encap_cookie4); 2219426aedfSHajimu UMEMOTO KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 2229426aedfSHajimu UMEMOTO } 2239426aedfSHajimu UMEMOTO #endif 22453dab5feSBrooks Davis 22594408d94SBrooks Davis if (ng_gif_detach_p != NULL) 22694408d94SBrooks Davis (*ng_gif_detach_p)(ifp); 22753dab5feSBrooks Davis bpfdetach(ifp); 22853dab5feSBrooks Davis if_detach(ifp); 229fc74a9f9SBrooks Davis if_free(ifp); 23053dab5feSBrooks Davis 23125af0bb5SGleb Smirnoff GIF_LOCK_DESTROY(sc); 23225af0bb5SGleb Smirnoff 23353dab5feSBrooks Davis free(sc, M_GIF); 23453dab5feSBrooks Davis } 23553dab5feSBrooks Davis 236d0728d71SRobert Watson static void 237d0728d71SRobert Watson vnet_gif_init(const void *unused __unused) 2381ed81b73SMarko Zec { 2391ed81b73SMarko Zec 2401ed81b73SMarko Zec LIST_INIT(&V_gif_softc_list); 2411ed81b73SMarko Zec } 242d0728d71SRobert Watson VNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_gif_init, 243d0728d71SRobert Watson NULL); 2441ed81b73SMarko Zec 2451ed81b73SMarko Zec static int 246*c72a5d5dSAndrey V. Elsukov gifmodevent(module_t mod, int type, void *data) 24753dab5feSBrooks Davis { 24853dab5feSBrooks Davis 24953dab5feSBrooks Davis switch (type) { 25053dab5feSBrooks Davis case MOD_LOAD: 25117d5cb2dSRobert Watson mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 25242a58907SGleb Smirnoff gif_cloner = if_clone_simple(gifname, gif_clone_create, 25342a58907SGleb Smirnoff gif_clone_destroy, 0); 25453dab5feSBrooks Davis break; 255d0728d71SRobert Watson 25653dab5feSBrooks Davis case MOD_UNLOAD: 25742a58907SGleb Smirnoff if_clone_detach(gif_cloner); 25817d5cb2dSRobert Watson mtx_destroy(&gif_mtx); 25953dab5feSBrooks Davis break; 2603e019deaSPoul-Henning Kamp default: 2613e019deaSPoul-Henning Kamp return EOPNOTSUPP; 26253dab5feSBrooks Davis } 26353dab5feSBrooks Davis return 0; 26453dab5feSBrooks Davis } 26553dab5feSBrooks Davis 26653dab5feSBrooks Davis static moduledata_t gif_mod = { 26753dab5feSBrooks Davis "if_gif", 26853dab5feSBrooks Davis gifmodevent, 2699823d527SKevin Lo 0 27053dab5feSBrooks Davis }; 27153dab5feSBrooks Davis 27253dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 27320af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1); 274cfa1ca9dSYoshinobu Inoue 2759426aedfSHajimu UMEMOTO int 276*c72a5d5dSAndrey V. Elsukov gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg) 277686cdd19SJun-ichiro itojun Hagino { 278686cdd19SJun-ichiro itojun Hagino struct ip ip; 279686cdd19SJun-ichiro itojun Hagino struct gif_softc *sc; 280686cdd19SJun-ichiro itojun Hagino 281686cdd19SJun-ichiro itojun Hagino sc = (struct gif_softc *)arg; 282686cdd19SJun-ichiro itojun Hagino if (sc == NULL) 283686cdd19SJun-ichiro itojun Hagino return 0; 284686cdd19SJun-ichiro itojun Hagino 285fc74a9f9SBrooks Davis if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 286686cdd19SJun-ichiro itojun Hagino return 0; 287686cdd19SJun-ichiro itojun Hagino 288686cdd19SJun-ichiro itojun Hagino /* no physical address */ 289686cdd19SJun-ichiro itojun Hagino if (!sc->gif_psrc || !sc->gif_pdst) 290686cdd19SJun-ichiro itojun Hagino return 0; 291686cdd19SJun-ichiro itojun Hagino 292686cdd19SJun-ichiro itojun Hagino switch (proto) { 293686cdd19SJun-ichiro itojun Hagino #ifdef INET 294686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV4: 295686cdd19SJun-ichiro itojun Hagino break; 296686cdd19SJun-ichiro itojun Hagino #endif 297686cdd19SJun-ichiro itojun Hagino #ifdef INET6 298686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV6: 299686cdd19SJun-ichiro itojun Hagino break; 300686cdd19SJun-ichiro itojun Hagino #endif 30173ff045cSAndrew Thompson case IPPROTO_ETHERIP: 30273ff045cSAndrew Thompson break; 30373ff045cSAndrew Thompson 304686cdd19SJun-ichiro itojun Hagino default: 305686cdd19SJun-ichiro itojun Hagino return 0; 306686cdd19SJun-ichiro itojun Hagino } 307686cdd19SJun-ichiro itojun Hagino 3083bb61ca6SHajimu UMEMOTO /* Bail on short packets */ 3093bb61ca6SHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(ip)) 3103bb61ca6SHajimu UMEMOTO return 0; 3113bb61ca6SHajimu UMEMOTO 3126f4ded3aSBrooks Davis m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 313686cdd19SJun-ichiro itojun Hagino 314686cdd19SJun-ichiro itojun Hagino switch (ip.ip_v) { 315686cdd19SJun-ichiro itojun Hagino #ifdef INET 316686cdd19SJun-ichiro itojun Hagino case 4: 317686cdd19SJun-ichiro itojun Hagino if (sc->gif_psrc->sa_family != AF_INET || 318686cdd19SJun-ichiro itojun Hagino sc->gif_pdst->sa_family != AF_INET) 319686cdd19SJun-ichiro itojun Hagino return 0; 320686cdd19SJun-ichiro itojun Hagino return gif_encapcheck4(m, off, proto, arg); 321686cdd19SJun-ichiro itojun Hagino #endif 322686cdd19SJun-ichiro itojun Hagino #ifdef INET6 323686cdd19SJun-ichiro itojun Hagino case 6: 3249426aedfSHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 3259426aedfSHajimu UMEMOTO return 0; 326686cdd19SJun-ichiro itojun Hagino if (sc->gif_psrc->sa_family != AF_INET6 || 327686cdd19SJun-ichiro itojun Hagino sc->gif_pdst->sa_family != AF_INET6) 328686cdd19SJun-ichiro itojun Hagino return 0; 329686cdd19SJun-ichiro itojun Hagino return gif_encapcheck6(m, off, proto, arg); 330686cdd19SJun-ichiro itojun Hagino #endif 331686cdd19SJun-ichiro itojun Hagino default: 332686cdd19SJun-ichiro itojun Hagino return 0; 333686cdd19SJun-ichiro itojun Hagino } 334686cdd19SJun-ichiro itojun Hagino } 335776b7288SRandall Stewart #ifdef INET 336776b7288SRandall Stewart #define GIF_HDR_LEN (ETHER_HDR_LEN + sizeof (struct ip)) 337776b7288SRandall Stewart #endif 338776b7288SRandall Stewart #ifdef INET6 339776b7288SRandall Stewart #define GIF_HDR_LEN6 (ETHER_HDR_LEN + sizeof (struct ip6_hdr)) 340776b7288SRandall Stewart #endif 341686cdd19SJun-ichiro itojun Hagino 34273ff045cSAndrew Thompson static void 34373ff045cSAndrew Thompson gif_start(struct ifnet *ifp) 34473ff045cSAndrew Thompson { 34573ff045cSAndrew Thompson struct gif_softc *sc; 34673ff045cSAndrew Thompson struct mbuf *m; 347776b7288SRandall Stewart uint32_t af; 348776b7288SRandall Stewart int error = 0; 34973ff045cSAndrew Thompson 35073ff045cSAndrew Thompson sc = ifp->if_softc; 351776b7288SRandall Stewart GIF_LOCK(sc); 35273ff045cSAndrew Thompson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 353776b7288SRandall Stewart while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) { 354776b7288SRandall Stewart 355776b7288SRandall Stewart IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 35673ff045cSAndrew Thompson if (m == 0) 35773ff045cSAndrew Thompson break; 35873ff045cSAndrew Thompson 359776b7288SRandall Stewart #ifdef ALTQ 360776b7288SRandall Stewart /* Take out those altq bytes we add in gif_output */ 361776b7288SRandall Stewart #ifdef INET 362776b7288SRandall Stewart if (sc->gif_psrc->sa_family == AF_INET) 363776b7288SRandall Stewart m->m_pkthdr.len -= GIF_HDR_LEN; 364776b7288SRandall Stewart #endif 365776b7288SRandall Stewart #ifdef INET6 366776b7288SRandall Stewart if (sc->gif_psrc->sa_family == AF_INET6) 367776b7288SRandall Stewart m->m_pkthdr.len -= GIF_HDR_LEN6; 368776b7288SRandall Stewart #endif 369776b7288SRandall Stewart #endif 37040138788SRandall Stewart /* 37140138788SRandall Stewart * Now pull back the af that we 37240138788SRandall Stewart * stashed in the csum_data. 373776b7288SRandall Stewart */ 374cef68c63SRandall Stewart af = m->m_pkthdr.csum_data; 375cef68c63SRandall Stewart 376776b7288SRandall Stewart if (ifp->if_bridge) 377776b7288SRandall Stewart af = AF_LINK; 378776b7288SRandall Stewart 379776b7288SRandall Stewart BPF_MTAP2(ifp, &af, sizeof(af), m); 380776b7288SRandall Stewart ifp->if_opackets++; 381776b7288SRandall Stewart 382776b7288SRandall Stewart /* Done by IFQ_HANDOFF */ 383776b7288SRandall Stewart /* ifp->if_obytes += m->m_pkthdr.len;*/ 384776b7288SRandall Stewart /* override to IPPROTO_ETHERIP for bridged traffic */ 385776b7288SRandall Stewart 386776b7288SRandall Stewart M_SETFIB(m, sc->gif_fibnum); 387776b7288SRandall Stewart /* inner AF-specific encapsulation */ 388776b7288SRandall Stewart /* XXX should we check if our outer source is legal? */ 389776b7288SRandall Stewart /* dispatch to output logic based on outer AF */ 390776b7288SRandall Stewart switch (sc->gif_psrc->sa_family) { 391776b7288SRandall Stewart #ifdef INET 392776b7288SRandall Stewart case AF_INET: 393776b7288SRandall Stewart error = in_gif_output(ifp, af, m); 394776b7288SRandall Stewart break; 395776b7288SRandall Stewart #endif 396776b7288SRandall Stewart #ifdef INET6 397776b7288SRandall Stewart case AF_INET6: 398776b7288SRandall Stewart error = in6_gif_output(ifp, af, m); 399776b7288SRandall Stewart break; 400776b7288SRandall Stewart #endif 401776b7288SRandall Stewart default: 402776b7288SRandall Stewart m_freem(m); 403776b7288SRandall Stewart error = ENETDOWN; 404776b7288SRandall Stewart } 405776b7288SRandall Stewart if (error) 406776b7288SRandall Stewart ifp->if_oerrors++; 40773ff045cSAndrew Thompson 40873ff045cSAndrew Thompson } 40973ff045cSAndrew Thompson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 410776b7288SRandall Stewart GIF_UNLOCK(sc); 41173ff045cSAndrew Thompson return; 41273ff045cSAndrew Thompson } 41373ff045cSAndrew Thompson 414cfa1ca9dSYoshinobu Inoue int 41547e8d432SGleb Smirnoff gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 41647e8d432SGleb Smirnoff struct route *ro) 417cfa1ca9dSYoshinobu Inoue { 418fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 4198c7e1947SRuslan Ermilov struct m_tag *mtag; 420cfa1ca9dSYoshinobu Inoue int error = 0; 4218c7e1947SRuslan Ermilov int gif_called; 422776b7288SRandall Stewart uint32_t af; 42310722b85SRobert Watson #ifdef MAC 42430d239bcSRobert Watson error = mac_ifnet_check_transmit(ifp, m); 425e0852ce2SRobert Watson if (error) { 426e0852ce2SRobert Watson m_freem(m); 427e0852ce2SRobert Watson goto end; 428e0852ce2SRobert Watson } 42910722b85SRobert Watson #endif 430e9f947e2SHiroki Sato if ((ifp->if_flags & IFF_MONITOR) != 0) { 431e9f947e2SHiroki Sato error = ENETDOWN; 432e9f947e2SHiroki Sato m_freem(m); 433e9f947e2SHiroki Sato goto end; 434e9f947e2SHiroki Sato } 43510722b85SRobert Watson 436cfa1ca9dSYoshinobu Inoue /* 437cfa1ca9dSYoshinobu Inoue * gif may cause infinite recursion calls when misconfigured. 4388c7e1947SRuslan Ermilov * We'll prevent this by detecting loops. 4398c7e1947SRuslan Ermilov * 4408c7e1947SRuslan Ermilov * High nesting level may cause stack exhaustion. 441cfa1ca9dSYoshinobu Inoue * We'll prevent this by introducing upper limit. 442cfa1ca9dSYoshinobu Inoue */ 4438c7e1947SRuslan Ermilov gif_called = 1; 4448c7e1947SRuslan Ermilov mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 4458c7e1947SRuslan Ermilov while (mtag != NULL) { 4468c7e1947SRuslan Ermilov if (*(struct ifnet **)(mtag + 1) == ifp) { 4478c7e1947SRuslan Ermilov log(LOG_NOTICE, 4488c7e1947SRuslan Ermilov "gif_output: loop detected on %s\n", 4498c7e1947SRuslan Ermilov (*(struct ifnet **)(mtag + 1))->if_xname); 4508c7e1947SRuslan Ermilov m_freem(m); 4518c7e1947SRuslan Ermilov error = EIO; /* is there better errno? */ 4528c7e1947SRuslan Ermilov goto end; 4538c7e1947SRuslan Ermilov } 4548c7e1947SRuslan Ermilov mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 4558c7e1947SRuslan Ermilov gif_called++; 4568c7e1947SRuslan Ermilov } 457603724d3SBjoern A. Zeeb if (gif_called > V_max_gif_nesting) { 458cfa1ca9dSYoshinobu Inoue log(LOG_NOTICE, 459cfa1ca9dSYoshinobu Inoue "gif_output: recursively called too many times(%d)\n", 460523ebc4eSRobert Watson gif_called); 461cfa1ca9dSYoshinobu Inoue m_freem(m); 462cfa1ca9dSYoshinobu Inoue error = EIO; /* is there better errno? */ 463cfa1ca9dSYoshinobu Inoue goto end; 464cfa1ca9dSYoshinobu Inoue } 4658c7e1947SRuslan Ermilov mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 4668c7e1947SRuslan Ermilov M_NOWAIT); 4678c7e1947SRuslan Ermilov if (mtag == NULL) { 4688c7e1947SRuslan Ermilov m_freem(m); 4698c7e1947SRuslan Ermilov error = ENOMEM; 4708c7e1947SRuslan Ermilov goto end; 4718c7e1947SRuslan Ermilov } 4728c7e1947SRuslan Ermilov *(struct ifnet **)(mtag + 1) = ifp; 4738c7e1947SRuslan Ermilov m_tag_prepend(m, mtag); 474686cdd19SJun-ichiro itojun Hagino 475cfa1ca9dSYoshinobu Inoue m->m_flags &= ~(M_BCAST|M_MCAST); 47601399f34SDavid Malone /* BPF writes need to be handled specially. */ 47747e8d432SGleb Smirnoff if (dst->sa_family == AF_UNSPEC) 47801399f34SDavid Malone bcopy(dst->sa_data, &af, sizeof(af)); 47947e8d432SGleb Smirnoff else 48001399f34SDavid Malone af = dst->sa_family; 48140138788SRandall Stewart /* 48240138788SRandall Stewart * Now save the af in the inbound pkt csum 48340138788SRandall Stewart * data, this is a cheat since we are using 48440138788SRandall Stewart * the inbound csum_data field to carry the 48540138788SRandall Stewart * af over to the gif_start() routine, avoiding 48640138788SRandall Stewart * using yet another mtag. 487776b7288SRandall Stewart */ 488cef68c63SRandall Stewart m->m_pkthdr.csum_data = af; 489776b7288SRandall Stewart if (!(ifp->if_flags & IFF_UP) || 490776b7288SRandall Stewart sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 491cfa1ca9dSYoshinobu Inoue m_freem(m); 492cfa1ca9dSYoshinobu Inoue error = ENETDOWN; 493776b7288SRandall Stewart goto end; 494cfa1ca9dSYoshinobu Inoue } 495776b7288SRandall Stewart #ifdef ALTQ 49640138788SRandall Stewart /* 49740138788SRandall Stewart * Make altq aware of the bytes we will add 498776b7288SRandall Stewart * when we actually send it. 499776b7288SRandall Stewart */ 500776b7288SRandall Stewart #ifdef INET 501776b7288SRandall Stewart if (sc->gif_psrc->sa_family == AF_INET) 502776b7288SRandall Stewart m->m_pkthdr.len += GIF_HDR_LEN; 503776b7288SRandall Stewart #endif 504776b7288SRandall Stewart #ifdef INET6 505776b7288SRandall Stewart if (sc->gif_psrc->sa_family == AF_INET6) 506776b7288SRandall Stewart m->m_pkthdr.len += GIF_HDR_LEN6; 507776b7288SRandall Stewart #endif 508776b7288SRandall Stewart #endif 509776b7288SRandall Stewart /* 510776b7288SRandall Stewart * Queue message on interface, update output statistics if 511776b7288SRandall Stewart * successful, and start output if interface not yet active. 512776b7288SRandall Stewart */ 513776b7288SRandall Stewart IFQ_HANDOFF(ifp, m, error); 514cfa1ca9dSYoshinobu Inoue end: 51533841545SHajimu UMEMOTO if (error) 51633841545SHajimu UMEMOTO ifp->if_oerrors++; 51725af0bb5SGleb Smirnoff return (error); 518cfa1ca9dSYoshinobu Inoue } 519cfa1ca9dSYoshinobu Inoue 520cfa1ca9dSYoshinobu Inoue void 521*c72a5d5dSAndrey V. Elsukov gif_input(struct mbuf *m, int af, struct ifnet *ifp) 522cfa1ca9dSYoshinobu Inoue { 52373ff045cSAndrew Thompson int isr, n; 5244382b068SChristian Brueffer struct gif_softc *sc; 52573ff045cSAndrew Thompson struct etherip_header *eip; 52656abdd33SAndrew Thompson struct ether_header *eh; 52756abdd33SAndrew Thompson struct ifnet *oldifp; 528cfa1ca9dSYoshinobu Inoue 52921fb391fSHajimu UMEMOTO if (ifp == NULL) { 530cfa1ca9dSYoshinobu Inoue /* just in case */ 531cfa1ca9dSYoshinobu Inoue m_freem(m); 532cfa1ca9dSYoshinobu Inoue return; 533cfa1ca9dSYoshinobu Inoue } 5344382b068SChristian Brueffer sc = ifp->if_softc; 53521fb391fSHajimu UMEMOTO m->m_pkthdr.rcvif = ifp; 536cfa1ca9dSYoshinobu Inoue 53710722b85SRobert Watson #ifdef MAC 53830d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m); 53910722b85SRobert Watson #endif 54010722b85SRobert Watson 54116d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 54233841545SHajimu UMEMOTO u_int32_t af1 = af; 543437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 544cfa1ca9dSYoshinobu Inoue } 545cfa1ca9dSYoshinobu Inoue 546e9f947e2SHiroki Sato if ((ifp->if_flags & IFF_MONITOR) != 0) { 547e9f947e2SHiroki Sato ifp->if_ipackets++; 548e9f947e2SHiroki Sato ifp->if_ibytes += m->m_pkthdr.len; 549e9f947e2SHiroki Sato m_freem(m); 550e9f947e2SHiroki Sato return; 551e9f947e2SHiroki Sato } 552e9f947e2SHiroki Sato 55394408d94SBrooks Davis if (ng_gif_input_p != NULL) { 55421fb391fSHajimu UMEMOTO (*ng_gif_input_p)(ifp, &m, af); 55594408d94SBrooks Davis if (m == NULL) 55694408d94SBrooks Davis return; 55794408d94SBrooks Davis } 55894408d94SBrooks Davis 559cfa1ca9dSYoshinobu Inoue /* 560cfa1ca9dSYoshinobu Inoue * Put the packet to the network layer input queue according to the 561cfa1ca9dSYoshinobu Inoue * specified address family. 562cfa1ca9dSYoshinobu Inoue * Note: older versions of gif_input directly called network layer 563cfa1ca9dSYoshinobu Inoue * input functions, e.g. ip6_input, here. We changed the policy to 564cfa1ca9dSYoshinobu Inoue * prevent too many recursive calls of such input functions, which 565cfa1ca9dSYoshinobu Inoue * might cause kernel panic. But the change may introduce another 566cfa1ca9dSYoshinobu Inoue * problem; if the input queue is full, packets are discarded. 56788ff5695SSUZUKI Shinsuke * The kernel stack overflow really happened, and we believed 56888ff5695SSUZUKI Shinsuke * queue-full rarely occurs, so we changed the policy. 569cfa1ca9dSYoshinobu Inoue */ 570cfa1ca9dSYoshinobu Inoue switch (af) { 571cfa1ca9dSYoshinobu Inoue #ifdef INET 572cfa1ca9dSYoshinobu Inoue case AF_INET: 573cfa1ca9dSYoshinobu Inoue isr = NETISR_IP; 574cfa1ca9dSYoshinobu Inoue break; 575cfa1ca9dSYoshinobu Inoue #endif 576cfa1ca9dSYoshinobu Inoue #ifdef INET6 577cfa1ca9dSYoshinobu Inoue case AF_INET6: 578cfa1ca9dSYoshinobu Inoue isr = NETISR_IPV6; 579cfa1ca9dSYoshinobu Inoue break; 580cfa1ca9dSYoshinobu Inoue #endif 58173ff045cSAndrew Thompson case AF_LINK: 58273ff045cSAndrew Thompson n = sizeof(struct etherip_header) + sizeof(struct ether_header); 58373ff045cSAndrew Thompson if (n > m->m_len) { 58473ff045cSAndrew Thompson m = m_pullup(m, n); 58573ff045cSAndrew Thompson if (m == NULL) { 58673ff045cSAndrew Thompson ifp->if_ierrors++; 58773ff045cSAndrew Thompson return; 58873ff045cSAndrew Thompson } 58973ff045cSAndrew Thompson } 59073ff045cSAndrew Thompson 59173ff045cSAndrew Thompson eip = mtod(m, struct etherip_header *); 592dbe59260SHiroki Sato /* 593dbe59260SHiroki Sato * GIF_ACCEPT_REVETHIP (enabled by default) intentionally 594dbe59260SHiroki Sato * accepts an EtherIP packet with revered version field in 595dbe59260SHiroki Sato * the header. This is a knob for backward compatibility 596dbe59260SHiroki Sato * with FreeBSD 7.2R or prior. 597dbe59260SHiroki Sato */ 598dbe59260SHiroki Sato if (sc->gif_options & GIF_ACCEPT_REVETHIP) { 599dbe59260SHiroki Sato if (eip->eip_resvl != ETHERIP_VERSION 600dbe59260SHiroki Sato && eip->eip_ver != ETHERIP_VERSION) { 60173ff045cSAndrew Thompson /* discard unknown versions */ 60273ff045cSAndrew Thompson m_freem(m); 60373ff045cSAndrew Thompson return; 60473ff045cSAndrew Thompson } 605dbe59260SHiroki Sato } else { 606dbe59260SHiroki Sato if (eip->eip_ver != ETHERIP_VERSION) { 607dbe59260SHiroki Sato /* discard unknown versions */ 608dbe59260SHiroki Sato m_freem(m); 609dbe59260SHiroki Sato return; 610dbe59260SHiroki Sato } 611dbe59260SHiroki Sato } 61273ff045cSAndrew Thompson m_adj(m, sizeof(struct etherip_header)); 61373ff045cSAndrew Thompson 61473ff045cSAndrew Thompson m->m_flags &= ~(M_BCAST|M_MCAST); 61573ff045cSAndrew Thompson m->m_pkthdr.rcvif = ifp; 61673ff045cSAndrew Thompson 61756abdd33SAndrew Thompson if (ifp->if_bridge) { 61856abdd33SAndrew Thompson oldifp = ifp; 61956abdd33SAndrew Thompson eh = mtod(m, struct ether_header *); 62056abdd33SAndrew Thompson if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 62156abdd33SAndrew Thompson if (ETHER_IS_BROADCAST(eh->ether_dhost)) 62256abdd33SAndrew Thompson m->m_flags |= M_BCAST; 62356abdd33SAndrew Thompson else 62456abdd33SAndrew Thompson m->m_flags |= M_MCAST; 62556abdd33SAndrew Thompson ifp->if_imcasts++; 62656abdd33SAndrew Thompson } 62773ff045cSAndrew Thompson BRIDGE_INPUT(ifp, m); 62873ff045cSAndrew Thompson 62956abdd33SAndrew Thompson if (m != NULL && ifp != oldifp) { 63056abdd33SAndrew Thompson /* 63156abdd33SAndrew Thompson * The bridge gave us back itself or one of the 63256abdd33SAndrew Thompson * members for which the frame is addressed. 63356abdd33SAndrew Thompson */ 63456abdd33SAndrew Thompson ether_demux(ifp, m); 63556abdd33SAndrew Thompson return; 63656abdd33SAndrew Thompson } 63756abdd33SAndrew Thompson } 63873ff045cSAndrew Thompson if (m != NULL) 63973ff045cSAndrew Thompson m_freem(m); 64073ff045cSAndrew Thompson return; 64173ff045cSAndrew Thompson 642cfa1ca9dSYoshinobu Inoue default: 64394408d94SBrooks Davis if (ng_gif_input_orphan_p != NULL) 64421fb391fSHajimu UMEMOTO (*ng_gif_input_orphan_p)(ifp, m, af); 64594408d94SBrooks Davis else 646cfa1ca9dSYoshinobu Inoue m_freem(m); 647cfa1ca9dSYoshinobu Inoue return; 648cfa1ca9dSYoshinobu Inoue } 649cfa1ca9dSYoshinobu Inoue 65021fb391fSHajimu UMEMOTO ifp->if_ipackets++; 65121fb391fSHajimu UMEMOTO ifp->if_ibytes += m->m_pkthdr.len; 652a34c6aebSBjoern A. Zeeb M_SETFIB(m, ifp->if_fib); 6531cafed39SJonathan Lemon netisr_dispatch(isr, m); 654cfa1ca9dSYoshinobu Inoue } 655cfa1ca9dSYoshinobu Inoue 656686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 657cfa1ca9dSYoshinobu Inoue int 658*c72a5d5dSAndrey V. Elsukov gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 659cfa1ca9dSYoshinobu Inoue { 660fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 661cfa1ca9dSYoshinobu Inoue struct ifreq *ifr = (struct ifreq*)data; 662cfa1ca9dSYoshinobu Inoue int error = 0, size; 663dbe59260SHiroki Sato u_int options; 664686cdd19SJun-ichiro itojun Hagino struct sockaddr *dst, *src; 6653bb61ca6SHajimu UMEMOTO #ifdef SIOCSIFMTU /* xxx */ 6663bb61ca6SHajimu UMEMOTO u_long mtu; 6673bb61ca6SHajimu UMEMOTO #endif 668cfa1ca9dSYoshinobu Inoue 669cfa1ca9dSYoshinobu Inoue switch (cmd) { 670cfa1ca9dSYoshinobu Inoue case SIOCSIFADDR: 6719426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_UP; 672cfa1ca9dSYoshinobu Inoue break; 673cfa1ca9dSYoshinobu Inoue 674cfa1ca9dSYoshinobu Inoue case SIOCADDMULTI: 675cfa1ca9dSYoshinobu Inoue case SIOCDELMULTI: 676cfa1ca9dSYoshinobu Inoue break; 677cfa1ca9dSYoshinobu Inoue 678686cdd19SJun-ichiro itojun Hagino #ifdef SIOCSIFMTU /* xxx */ 679cfa1ca9dSYoshinobu Inoue case SIOCGIFMTU: 680cfa1ca9dSYoshinobu Inoue break; 681686cdd19SJun-ichiro itojun Hagino 682cfa1ca9dSYoshinobu Inoue case SIOCSIFMTU: 683cfa1ca9dSYoshinobu Inoue mtu = ifr->ifr_mtu; 6843bb61ca6SHajimu UMEMOTO if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 685cfa1ca9dSYoshinobu Inoue return (EINVAL); 686cfa1ca9dSYoshinobu Inoue ifp->if_mtu = mtu; 687cfa1ca9dSYoshinobu Inoue break; 688686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */ 689cfa1ca9dSYoshinobu Inoue 6903bb61ca6SHajimu UMEMOTO #ifdef INET 691cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR: 6923bb61ca6SHajimu UMEMOTO #endif 693cfa1ca9dSYoshinobu Inoue #ifdef INET6 694cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR_IN6: 695cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 696686cdd19SJun-ichiro itojun Hagino switch (cmd) { 69733841545SHajimu UMEMOTO #ifdef INET 698686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 699cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 700cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_addr); 701cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 702cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_dstaddr); 703cfa1ca9dSYoshinobu Inoue break; 70433841545SHajimu UMEMOTO #endif 705cfa1ca9dSYoshinobu Inoue #ifdef INET6 706686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 707cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 708cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_addr); 709cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 710cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_dstaddr); 711686cdd19SJun-ichiro itojun Hagino break; 712686cdd19SJun-ichiro itojun Hagino #endif 7136f4ded3aSBrooks Davis default: 7149426aedfSHajimu UMEMOTO return EINVAL; 71533841545SHajimu UMEMOTO } 71633841545SHajimu UMEMOTO 71733841545SHajimu UMEMOTO /* sa_family must be equal */ 71833841545SHajimu UMEMOTO if (src->sa_family != dst->sa_family) 71933841545SHajimu UMEMOTO return EINVAL; 72033841545SHajimu UMEMOTO 72133841545SHajimu UMEMOTO /* validate sa_len */ 72233841545SHajimu UMEMOTO switch (src->sa_family) { 72333841545SHajimu UMEMOTO #ifdef INET 72433841545SHajimu UMEMOTO case AF_INET: 72533841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in)) 72633841545SHajimu UMEMOTO return EINVAL; 72733841545SHajimu UMEMOTO break; 72833841545SHajimu UMEMOTO #endif 72933841545SHajimu UMEMOTO #ifdef INET6 73033841545SHajimu UMEMOTO case AF_INET6: 73133841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in6)) 73233841545SHajimu UMEMOTO return EINVAL; 73333841545SHajimu UMEMOTO break; 73433841545SHajimu UMEMOTO #endif 73533841545SHajimu UMEMOTO default: 73633841545SHajimu UMEMOTO return EAFNOSUPPORT; 73733841545SHajimu UMEMOTO } 73833841545SHajimu UMEMOTO switch (dst->sa_family) { 73933841545SHajimu UMEMOTO #ifdef INET 74033841545SHajimu UMEMOTO case AF_INET: 74133841545SHajimu UMEMOTO if (dst->sa_len != sizeof(struct sockaddr_in)) 74233841545SHajimu UMEMOTO return EINVAL; 74333841545SHajimu UMEMOTO break; 74433841545SHajimu UMEMOTO #endif 74533841545SHajimu UMEMOTO #ifdef INET6 74633841545SHajimu UMEMOTO case AF_INET6: 74733841545SHajimu UMEMOTO if (dst->sa_len != sizeof(struct sockaddr_in6)) 74833841545SHajimu UMEMOTO return EINVAL; 74933841545SHajimu UMEMOTO break; 75033841545SHajimu UMEMOTO #endif 75133841545SHajimu UMEMOTO default: 75233841545SHajimu UMEMOTO return EAFNOSUPPORT; 75333841545SHajimu UMEMOTO } 75433841545SHajimu UMEMOTO 75533841545SHajimu UMEMOTO /* check sa_family looks sane for the cmd */ 75633841545SHajimu UMEMOTO switch (cmd) { 75733841545SHajimu UMEMOTO case SIOCSIFPHYADDR: 75833841545SHajimu UMEMOTO if (src->sa_family == AF_INET) 75933841545SHajimu UMEMOTO break; 76033841545SHajimu UMEMOTO return EAFNOSUPPORT; 76133841545SHajimu UMEMOTO #ifdef INET6 76233841545SHajimu UMEMOTO case SIOCSIFPHYADDR_IN6: 76333841545SHajimu UMEMOTO if (src->sa_family == AF_INET6) 76433841545SHajimu UMEMOTO break; 76533841545SHajimu UMEMOTO return EAFNOSUPPORT; 76633841545SHajimu UMEMOTO #endif /* INET6 */ 767686cdd19SJun-ichiro itojun Hagino } 768cfa1ca9dSYoshinobu Inoue 769fc74a9f9SBrooks Davis error = gif_set_tunnel(GIF2IFP(sc), src, dst); 770cfa1ca9dSYoshinobu Inoue break; 771cfa1ca9dSYoshinobu Inoue 772686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR 773686cdd19SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 774fc74a9f9SBrooks Davis gif_delete_tunnel(GIF2IFP(sc)); 775686cdd19SJun-ichiro itojun Hagino break; 776686cdd19SJun-ichiro itojun Hagino #endif 777686cdd19SJun-ichiro itojun Hagino 778cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR: 779cfa1ca9dSYoshinobu Inoue #ifdef INET6 780cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR_IN6: 781cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 782cfa1ca9dSYoshinobu Inoue if (sc->gif_psrc == NULL) { 783cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 784cfa1ca9dSYoshinobu Inoue goto bad; 785cfa1ca9dSYoshinobu Inoue } 786cfa1ca9dSYoshinobu Inoue src = sc->gif_psrc; 78733841545SHajimu UMEMOTO switch (cmd) { 788cfa1ca9dSYoshinobu Inoue #ifdef INET 78933841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 790cfa1ca9dSYoshinobu Inoue dst = &ifr->ifr_addr; 79133841545SHajimu UMEMOTO size = sizeof(ifr->ifr_addr); 792cfa1ca9dSYoshinobu Inoue break; 793cfa1ca9dSYoshinobu Inoue #endif /* INET */ 794cfa1ca9dSYoshinobu Inoue #ifdef INET6 79533841545SHajimu UMEMOTO case SIOCGIFPSRCADDR_IN6: 796cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 797cfa1ca9dSYoshinobu Inoue &(((struct in6_ifreq *)data)->ifr_addr); 79833841545SHajimu UMEMOTO size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 799cfa1ca9dSYoshinobu Inoue break; 800cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 801cfa1ca9dSYoshinobu Inoue default: 802cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 803cfa1ca9dSYoshinobu Inoue goto bad; 804cfa1ca9dSYoshinobu Inoue } 80533841545SHajimu UMEMOTO if (src->sa_len > size) 80633841545SHajimu UMEMOTO return EINVAL; 80733841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 808a1f7e5f8SHajimu UMEMOTO #ifdef INET6 809a1f7e5f8SHajimu UMEMOTO if (dst->sa_family == AF_INET6) { 810a1f7e5f8SHajimu UMEMOTO error = sa6_recoverscope((struct sockaddr_in6 *)dst); 811a1f7e5f8SHajimu UMEMOTO if (error != 0) 812a1f7e5f8SHajimu UMEMOTO return (error); 813a1f7e5f8SHajimu UMEMOTO } 814a1f7e5f8SHajimu UMEMOTO #endif 815cfa1ca9dSYoshinobu Inoue break; 816cfa1ca9dSYoshinobu Inoue 817cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR: 818cfa1ca9dSYoshinobu Inoue #ifdef INET6 819cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR_IN6: 820cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 821cfa1ca9dSYoshinobu Inoue if (sc->gif_pdst == NULL) { 822cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 823cfa1ca9dSYoshinobu Inoue goto bad; 824cfa1ca9dSYoshinobu Inoue } 825cfa1ca9dSYoshinobu Inoue src = sc->gif_pdst; 82633841545SHajimu UMEMOTO switch (cmd) { 827cfa1ca9dSYoshinobu Inoue #ifdef INET 82833841545SHajimu UMEMOTO case SIOCGIFPDSTADDR: 829cfa1ca9dSYoshinobu Inoue dst = &ifr->ifr_addr; 83033841545SHajimu UMEMOTO size = sizeof(ifr->ifr_addr); 831cfa1ca9dSYoshinobu Inoue break; 832cfa1ca9dSYoshinobu Inoue #endif /* INET */ 833cfa1ca9dSYoshinobu Inoue #ifdef INET6 83433841545SHajimu UMEMOTO case SIOCGIFPDSTADDR_IN6: 835cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 836cfa1ca9dSYoshinobu Inoue &(((struct in6_ifreq *)data)->ifr_addr); 83733841545SHajimu UMEMOTO size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 838cfa1ca9dSYoshinobu Inoue break; 839cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 840cfa1ca9dSYoshinobu Inoue default: 841cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 842cfa1ca9dSYoshinobu Inoue goto bad; 843cfa1ca9dSYoshinobu Inoue } 84433841545SHajimu UMEMOTO if (src->sa_len > size) 84533841545SHajimu UMEMOTO return EINVAL; 846e3416ab0SBjoern A. Zeeb error = prison_if(curthread->td_ucred, src); 847e3416ab0SBjoern A. Zeeb if (error != 0) 848e3416ab0SBjoern A. Zeeb return (error); 849e3416ab0SBjoern A. Zeeb error = prison_if(curthread->td_ucred, dst); 850e3416ab0SBjoern A. Zeeb if (error != 0) 851e3416ab0SBjoern A. Zeeb return (error); 85233841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 853a1f7e5f8SHajimu UMEMOTO #ifdef INET6 854a1f7e5f8SHajimu UMEMOTO if (dst->sa_family == AF_INET6) { 855a1f7e5f8SHajimu UMEMOTO error = sa6_recoverscope((struct sockaddr_in6 *)dst); 856a1f7e5f8SHajimu UMEMOTO if (error != 0) 857a1f7e5f8SHajimu UMEMOTO return (error); 858a1f7e5f8SHajimu UMEMOTO } 859a1f7e5f8SHajimu UMEMOTO #endif 86033841545SHajimu UMEMOTO break; 86133841545SHajimu UMEMOTO 862cfa1ca9dSYoshinobu Inoue case SIOCSIFFLAGS: 863686cdd19SJun-ichiro itojun Hagino /* if_ioctl() takes care of it */ 864cfa1ca9dSYoshinobu Inoue break; 865cfa1ca9dSYoshinobu Inoue 866dbe59260SHiroki Sato case GIFGOPTS: 867dbe59260SHiroki Sato options = sc->gif_options; 868dbe59260SHiroki Sato error = copyout(&options, ifr->ifr_data, 869dbe59260SHiroki Sato sizeof(options)); 870dbe59260SHiroki Sato break; 871dbe59260SHiroki Sato 872dbe59260SHiroki Sato case GIFSOPTS: 873dbe59260SHiroki Sato if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 874dbe59260SHiroki Sato break; 8754cd5f57dSHiroki Sato error = copyin(ifr->ifr_data, &options, sizeof(options)); 8764cd5f57dSHiroki Sato if (error) 8774cd5f57dSHiroki Sato break; 8784cd5f57dSHiroki Sato if (options & ~GIF_OPTMASK) 879dbe59260SHiroki Sato error = EINVAL; 8804cd5f57dSHiroki Sato else 8814cd5f57dSHiroki Sato sc->gif_options = options; 882dbe59260SHiroki Sato break; 883dbe59260SHiroki Sato 884cfa1ca9dSYoshinobu Inoue default: 885cfa1ca9dSYoshinobu Inoue error = EINVAL; 886cfa1ca9dSYoshinobu Inoue break; 887cfa1ca9dSYoshinobu Inoue } 888cfa1ca9dSYoshinobu Inoue bad: 889cfa1ca9dSYoshinobu Inoue return error; 890cfa1ca9dSYoshinobu Inoue } 89153dab5feSBrooks Davis 89217d5cb2dSRobert Watson /* 89317d5cb2dSRobert Watson * XXXRW: There's a general event-ordering issue here: the code to check 89417d5cb2dSRobert Watson * if a given tunnel is already present happens before we perform a 89517d5cb2dSRobert Watson * potentially blocking setup of the tunnel. This code needs to be 89617d5cb2dSRobert Watson * re-ordered so that the check and replacement can be atomic using 89717d5cb2dSRobert Watson * a mutex. 89817d5cb2dSRobert Watson */ 8999426aedfSHajimu UMEMOTO int 900*c72a5d5dSAndrey V. Elsukov gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst) 90153dab5feSBrooks Davis { 902fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 9039426aedfSHajimu UMEMOTO struct gif_softc *sc2; 9049426aedfSHajimu UMEMOTO struct sockaddr *osrc, *odst, *sa; 9059426aedfSHajimu UMEMOTO int error = 0; 9069426aedfSHajimu UMEMOTO 90717d5cb2dSRobert Watson mtx_lock(&gif_mtx); 908603724d3SBjoern A. Zeeb LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) { 9099426aedfSHajimu UMEMOTO if (sc2 == sc) 9109426aedfSHajimu UMEMOTO continue; 9119426aedfSHajimu UMEMOTO if (!sc2->gif_pdst || !sc2->gif_psrc) 9129426aedfSHajimu UMEMOTO continue; 9139426aedfSHajimu UMEMOTO if (sc2->gif_pdst->sa_family != dst->sa_family || 9149426aedfSHajimu UMEMOTO sc2->gif_pdst->sa_len != dst->sa_len || 9159426aedfSHajimu UMEMOTO sc2->gif_psrc->sa_family != src->sa_family || 9169426aedfSHajimu UMEMOTO sc2->gif_psrc->sa_len != src->sa_len) 9179426aedfSHajimu UMEMOTO continue; 9189426aedfSHajimu UMEMOTO 9199426aedfSHajimu UMEMOTO /* 9209426aedfSHajimu UMEMOTO * Disallow parallel tunnels unless instructed 9219426aedfSHajimu UMEMOTO * otherwise. 9229426aedfSHajimu UMEMOTO */ 923603724d3SBjoern A. Zeeb if (!V_parallel_tunnels && 9249426aedfSHajimu UMEMOTO bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 9259426aedfSHajimu UMEMOTO bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 9269426aedfSHajimu UMEMOTO error = EADDRNOTAVAIL; 92717d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 9289426aedfSHajimu UMEMOTO goto bad; 9299426aedfSHajimu UMEMOTO } 9309426aedfSHajimu UMEMOTO 9319426aedfSHajimu UMEMOTO /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 9329426aedfSHajimu UMEMOTO } 93317d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 9349426aedfSHajimu UMEMOTO 9359426aedfSHajimu UMEMOTO /* XXX we can detach from both, but be polite just in case */ 9369426aedfSHajimu UMEMOTO if (sc->gif_psrc) 9379426aedfSHajimu UMEMOTO switch (sc->gif_psrc->sa_family) { 9389426aedfSHajimu UMEMOTO #ifdef INET 9399426aedfSHajimu UMEMOTO case AF_INET: 9409426aedfSHajimu UMEMOTO (void)in_gif_detach(sc); 9419426aedfSHajimu UMEMOTO break; 9429426aedfSHajimu UMEMOTO #endif 9439426aedfSHajimu UMEMOTO #ifdef INET6 9449426aedfSHajimu UMEMOTO case AF_INET6: 9459426aedfSHajimu UMEMOTO (void)in6_gif_detach(sc); 9469426aedfSHajimu UMEMOTO break; 9479426aedfSHajimu UMEMOTO #endif 9489426aedfSHajimu UMEMOTO } 9499426aedfSHajimu UMEMOTO 9509426aedfSHajimu UMEMOTO osrc = sc->gif_psrc; 951a163d034SWarner Losh sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 9529426aedfSHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 9539426aedfSHajimu UMEMOTO sc->gif_psrc = sa; 9549426aedfSHajimu UMEMOTO 9559426aedfSHajimu UMEMOTO odst = sc->gif_pdst; 956a163d034SWarner Losh sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 9579426aedfSHajimu UMEMOTO bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 9589426aedfSHajimu UMEMOTO sc->gif_pdst = sa; 9599426aedfSHajimu UMEMOTO 9609426aedfSHajimu UMEMOTO switch (sc->gif_psrc->sa_family) { 9619426aedfSHajimu UMEMOTO #ifdef INET 9629426aedfSHajimu UMEMOTO case AF_INET: 9639426aedfSHajimu UMEMOTO error = in_gif_attach(sc); 9649426aedfSHajimu UMEMOTO break; 9659426aedfSHajimu UMEMOTO #endif 9669426aedfSHajimu UMEMOTO #ifdef INET6 9679426aedfSHajimu UMEMOTO case AF_INET6: 968a1f7e5f8SHajimu UMEMOTO /* 969a1f7e5f8SHajimu UMEMOTO * Check validity of the scope zone ID of the addresses, and 970a1f7e5f8SHajimu UMEMOTO * convert it into the kernel internal form if necessary. 971a1f7e5f8SHajimu UMEMOTO */ 972a1f7e5f8SHajimu UMEMOTO error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 973a1f7e5f8SHajimu UMEMOTO if (error != 0) 974a1f7e5f8SHajimu UMEMOTO break; 975a1f7e5f8SHajimu UMEMOTO error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 976a1f7e5f8SHajimu UMEMOTO if (error != 0) 977a1f7e5f8SHajimu UMEMOTO break; 9789426aedfSHajimu UMEMOTO error = in6_gif_attach(sc); 9799426aedfSHajimu UMEMOTO break; 9809426aedfSHajimu UMEMOTO #endif 9819426aedfSHajimu UMEMOTO } 9829426aedfSHajimu UMEMOTO if (error) { 9839426aedfSHajimu UMEMOTO /* rollback */ 9849426aedfSHajimu UMEMOTO free((caddr_t)sc->gif_psrc, M_IFADDR); 9859426aedfSHajimu UMEMOTO free((caddr_t)sc->gif_pdst, M_IFADDR); 9869426aedfSHajimu UMEMOTO sc->gif_psrc = osrc; 9879426aedfSHajimu UMEMOTO sc->gif_pdst = odst; 9889426aedfSHajimu UMEMOTO goto bad; 9899426aedfSHajimu UMEMOTO } 9909426aedfSHajimu UMEMOTO 9919426aedfSHajimu UMEMOTO if (osrc) 9929426aedfSHajimu UMEMOTO free((caddr_t)osrc, M_IFADDR); 9939426aedfSHajimu UMEMOTO if (odst) 9949426aedfSHajimu UMEMOTO free((caddr_t)odst, M_IFADDR); 9959426aedfSHajimu UMEMOTO 9969426aedfSHajimu UMEMOTO bad: 9979426aedfSHajimu UMEMOTO if (sc->gif_psrc && sc->gif_pdst) 99813f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 9999426aedfSHajimu UMEMOTO else 100013f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 10019426aedfSHajimu UMEMOTO 10029426aedfSHajimu UMEMOTO return error; 10039426aedfSHajimu UMEMOTO } 10049426aedfSHajimu UMEMOTO 10059426aedfSHajimu UMEMOTO void 1006*c72a5d5dSAndrey V. Elsukov gif_delete_tunnel(struct ifnet *ifp) 10079426aedfSHajimu UMEMOTO { 1008fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 100953dab5feSBrooks Davis 101053dab5feSBrooks Davis if (sc->gif_psrc) { 101153dab5feSBrooks Davis free((caddr_t)sc->gif_psrc, M_IFADDR); 101253dab5feSBrooks Davis sc->gif_psrc = NULL; 101353dab5feSBrooks Davis } 101453dab5feSBrooks Davis if (sc->gif_pdst) { 101553dab5feSBrooks Davis free((caddr_t)sc->gif_pdst, M_IFADDR); 101653dab5feSBrooks Davis sc->gif_pdst = NULL; 101753dab5feSBrooks Davis } 10189426aedfSHajimu UMEMOTO /* it is safe to detach from both */ 10199426aedfSHajimu UMEMOTO #ifdef INET 10209426aedfSHajimu UMEMOTO (void)in_gif_detach(sc); 10219426aedfSHajimu UMEMOTO #endif 10229426aedfSHajimu UMEMOTO #ifdef INET6 10239426aedfSHajimu UMEMOTO (void)in6_gif_detach(sc); 10249426aedfSHajimu UMEMOTO #endif 102513f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 102653dab5feSBrooks Davis } 1027