1c398230bSWarner Losh /*- 259d8d13fSGarrett Wollman * Copyright (c) 1982, 1986, 1993 359d8d13fSGarrett Wollman * The Regents of the University of California. All rights reserved. 459d8d13fSGarrett Wollman * 559d8d13fSGarrett Wollman * Redistribution and use in source and binary forms, with or without 659d8d13fSGarrett Wollman * modification, are permitted provided that the following conditions 759d8d13fSGarrett Wollman * are met: 859d8d13fSGarrett Wollman * 1. Redistributions of source code must retain the above copyright 959d8d13fSGarrett Wollman * notice, this list of conditions and the following disclaimer. 1059d8d13fSGarrett Wollman * 2. Redistributions in binary form must reproduce the above copyright 1159d8d13fSGarrett Wollman * notice, this list of conditions and the following disclaimer in the 1259d8d13fSGarrett Wollman * documentation and/or other materials provided with the distribution. 1359d8d13fSGarrett Wollman * 4. Neither the name of the University nor the names of its contributors 1459d8d13fSGarrett Wollman * may be used to endorse or promote products derived from this software 1559d8d13fSGarrett Wollman * without specific prior written permission. 1659d8d13fSGarrett Wollman * 1759d8d13fSGarrett Wollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1859d8d13fSGarrett Wollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1959d8d13fSGarrett Wollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2059d8d13fSGarrett Wollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2159d8d13fSGarrett Wollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2259d8d13fSGarrett Wollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2359d8d13fSGarrett Wollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2459d8d13fSGarrett Wollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2559d8d13fSGarrett Wollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2659d8d13fSGarrett Wollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2759d8d13fSGarrett Wollman * SUCH DAMAGE. 2859d8d13fSGarrett Wollman * 2959d8d13fSGarrett Wollman * From: @(#)if_loop.c 8.1 (Berkeley) 6/10/93 30c3aac50fSPeter Wemm * $FreeBSD$ 3159d8d13fSGarrett Wollman */ 3259d8d13fSGarrett Wollman 3359d8d13fSGarrett Wollman /* 3459d8d13fSGarrett Wollman * Discard interface driver for protocol testing and timing. 3559d8d13fSGarrett Wollman * (Based on the loopback.) 3659d8d13fSGarrett Wollman */ 3759d8d13fSGarrett Wollman 3859d8d13fSGarrett Wollman #include <sys/param.h> 3959d8d13fSGarrett Wollman #include <sys/systm.h> 4059d8d13fSGarrett Wollman #include <sys/kernel.h> 41c69b7ffeSBrooks Davis #include <sys/malloc.h> 422b120974SPeter Wemm #include <sys/module.h> 4359d8d13fSGarrett Wollman #include <sys/mbuf.h> 4459d8d13fSGarrett Wollman #include <sys/socket.h> 4551a53488SBruce Evans #include <sys/sockio.h> 4659d8d13fSGarrett Wollman 4759d8d13fSGarrett Wollman #include <net/if.h> 48f889d2efSBrooks Davis #include <net/if_clone.h> 4959d8d13fSGarrett Wollman #include <net/if_types.h> 5059d8d13fSGarrett Wollman #include <net/route.h> 5159d8d13fSGarrett Wollman #include <net/bpf.h> 5259d8d13fSGarrett Wollman 531d5e9e22SEivind Eklund #include "opt_inet.h" 54cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 5559d8d13fSGarrett Wollman 5659d8d13fSGarrett Wollman #ifdef TINY_DSMTU 5759d8d13fSGarrett Wollman #define DSMTU (1024+512) 5859d8d13fSGarrett Wollman #else 5959d8d13fSGarrett Wollman #define DSMTU 65532 6059d8d13fSGarrett Wollman #endif 6159d8d13fSGarrett Wollman 62c69b7ffeSBrooks Davis #define DISCNAME "disc" 63b6f5c0b8SBruce Evans 64c69b7ffeSBrooks Davis struct disc_softc { 65fc74a9f9SBrooks Davis struct ifnet *sc_ifp; /* must be first */ 66c69b7ffeSBrooks Davis }; 67c69b7ffeSBrooks Davis 68eb6bd594SMark Murray static int discoutput(struct ifnet *, struct mbuf *, 69eb6bd594SMark Murray struct sockaddr *, struct rtentry *); 708071913dSRuslan Ermilov static void discrtrequest(int, struct rtentry *, struct rt_addrinfo *); 71537ad974SEivind Eklund static int discioctl(struct ifnet *, u_long, caddr_t); 726b7330e2SSam Leffler static int disc_clone_create(struct if_clone *, int, caddr_t); 73c69b7ffeSBrooks Davis static void disc_clone_destroy(struct ifnet *); 7459d8d13fSGarrett Wollman 75c69b7ffeSBrooks Davis static MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface"); 76f889d2efSBrooks Davis 77f889d2efSBrooks Davis IFC_SIMPLE_DECLARE(disc, 0); 78c69b7ffeSBrooks Davis 79c69b7ffeSBrooks Davis static int 806b7330e2SSam Leffler disc_clone_create(struct if_clone *ifc, int unit, caddr_t params) 8159d8d13fSGarrett Wollman { 82c69b7ffeSBrooks Davis struct ifnet *ifp; 83c69b7ffeSBrooks Davis struct disc_softc *sc; 8459d8d13fSGarrett Wollman 8560323f48SBruce M Simpson sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK | M_ZERO); 86fc74a9f9SBrooks Davis ifp = sc->sc_ifp = if_alloc(IFT_LOOP); 87fc74a9f9SBrooks Davis if (ifp == NULL) { 88fc74a9f9SBrooks Davis free(sc, M_DISC); 89fc74a9f9SBrooks Davis return (ENOSPC); 90fc74a9f9SBrooks Davis } 91c69b7ffeSBrooks Davis 92c69b7ffeSBrooks Davis ifp->if_softc = sc; 939bf40edeSBrooks Davis if_initname(ifp, ifc->ifc_name, unit); 9459d8d13fSGarrett Wollman ifp->if_mtu = DSMTU; 9559d8d13fSGarrett Wollman ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 96d314617eSGleb Smirnoff ifp->if_drv_flags = IFF_DRV_RUNNING; 97537ad974SEivind Eklund ifp->if_ioctl = discioctl; 98537ad974SEivind Eklund ifp->if_output = discoutput; 9959d8d13fSGarrett Wollman ifp->if_hdrlen = 0; 10059d8d13fSGarrett Wollman ifp->if_addrlen = 0; 101422fd76fSPoul-Henning Kamp ifp->if_snd.ifq_maxlen = 20; 10259d8d13fSGarrett Wollman if_attach(ifp); 10301399f34SDavid Malone bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 104c69b7ffeSBrooks Davis 105c69b7ffeSBrooks Davis return (0); 106c69b7ffeSBrooks Davis } 107c69b7ffeSBrooks Davis 108c69b7ffeSBrooks Davis static void 109c69b7ffeSBrooks Davis disc_clone_destroy(struct ifnet *ifp) 110c69b7ffeSBrooks Davis { 111c69b7ffeSBrooks Davis struct disc_softc *sc; 112c69b7ffeSBrooks Davis 113c69b7ffeSBrooks Davis sc = ifp->if_softc; 114c69b7ffeSBrooks Davis 115febd0759SAndrew Thompson bpfdetach(ifp); 116febd0759SAndrew Thompson if_detach(ifp); 117febd0759SAndrew Thompson if_free(ifp); 118febd0759SAndrew Thompson 119febd0759SAndrew Thompson free(sc, M_DISC); 12059d8d13fSGarrett Wollman } 12159d8d13fSGarrett Wollman 12259d8d13fSGarrett Wollman static int 1232b120974SPeter Wemm disc_modevent(module_t mod, int type, void *data) 1242b120974SPeter Wemm { 125d6e2616aSRobert Watson 1262b120974SPeter Wemm switch (type) { 1272b120974SPeter Wemm case MOD_LOAD: 128c69b7ffeSBrooks Davis if_clone_attach(&disc_cloner); 1292b120974SPeter Wemm break; 1302b120974SPeter Wemm case MOD_UNLOAD: 131c69b7ffeSBrooks Davis if_clone_detach(&disc_cloner); 132c69b7ffeSBrooks Davis break; 1333e019deaSPoul-Henning Kamp default: 1343e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 1352b120974SPeter Wemm } 136832cb4aeSBruce M Simpson return (0); 1372b120974SPeter Wemm } 1382b120974SPeter Wemm 1392b120974SPeter Wemm static moduledata_t disc_mod = { 1402b120974SPeter Wemm "if_disc", 1412b120974SPeter Wemm disc_modevent, 1422b120974SPeter Wemm NULL 1432b120974SPeter Wemm }; 1442b120974SPeter Wemm 1452b120974SPeter Wemm DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 1462b120974SPeter Wemm 1472b120974SPeter Wemm static int 148eb6bd594SMark Murray discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 149eb6bd594SMark Murray struct rtentry *rt) 15059d8d13fSGarrett Wollman { 15101399f34SDavid Malone u_int32_t af; 152832cb4aeSBruce M Simpson 153fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(m); 154832cb4aeSBruce M Simpson 15501399f34SDavid Malone /* BPF writes need to be handled specially. */ 156963e4c2aSGarrett Wollman if (dst->sa_family == AF_UNSPEC) { 15701399f34SDavid Malone bcopy(dst->sa_data, &af, sizeof(af)); 15801399f34SDavid Malone dst->sa_family = af; 159963e4c2aSGarrett Wollman } 160963e4c2aSGarrett Wollman 16116d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 16259d8d13fSGarrett Wollman u_int af = dst->sa_family; 163437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 16459d8d13fSGarrett Wollman } 16559d8d13fSGarrett Wollman m->m_pkthdr.rcvif = ifp; 16659d8d13fSGarrett Wollman 16759d8d13fSGarrett Wollman ifp->if_opackets++; 16859d8d13fSGarrett Wollman ifp->if_obytes += m->m_pkthdr.len; 16959d8d13fSGarrett Wollman 17059d8d13fSGarrett Wollman m_freem(m); 171832cb4aeSBruce M Simpson return (0); 17259d8d13fSGarrett Wollman } 17359d8d13fSGarrett Wollman 17459d8d13fSGarrett Wollman /* ARGSUSED */ 17559d8d13fSGarrett Wollman static void 1768071913dSRuslan Ermilov discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 17759d8d13fSGarrett Wollman { 178d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 17959d8d13fSGarrett Wollman rt->rt_rmx.rmx_mtu = DSMTU; 18059d8d13fSGarrett Wollman } 18159d8d13fSGarrett Wollman 18259d8d13fSGarrett Wollman /* 18359d8d13fSGarrett Wollman * Process an ioctl request. 18459d8d13fSGarrett Wollman */ 18588e2f526SBruce Evans static int 186eb6bd594SMark Murray discioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 18759d8d13fSGarrett Wollman { 188eb6bd594SMark Murray struct ifaddr *ifa; 189eb6bd594SMark Murray struct ifreq *ifr = (struct ifreq *)data; 190eb6bd594SMark Murray int error = 0; 19159d8d13fSGarrett Wollman 19259d8d13fSGarrett Wollman switch (cmd) { 19359d8d13fSGarrett Wollman 19459d8d13fSGarrett Wollman case SIOCSIFADDR: 19559d8d13fSGarrett Wollman ifp->if_flags |= IFF_UP; 19659d8d13fSGarrett Wollman ifa = (struct ifaddr *)data; 19759d8d13fSGarrett Wollman if (ifa != 0) 198537ad974SEivind Eklund ifa->ifa_rtrequest = discrtrequest; 19959d8d13fSGarrett Wollman /* 20059d8d13fSGarrett Wollman * Everything else is done at a higher level. 20159d8d13fSGarrett Wollman */ 20259d8d13fSGarrett Wollman break; 20359d8d13fSGarrett Wollman 20459d8d13fSGarrett Wollman case SIOCADDMULTI: 20559d8d13fSGarrett Wollman case SIOCDELMULTI: 20659d8d13fSGarrett Wollman if (ifr == 0) { 20759d8d13fSGarrett Wollman error = EAFNOSUPPORT; /* XXX */ 20859d8d13fSGarrett Wollman break; 20959d8d13fSGarrett Wollman } 21059d8d13fSGarrett Wollman switch (ifr->ifr_addr.sa_family) { 21159d8d13fSGarrett Wollman 21259d8d13fSGarrett Wollman #ifdef INET 21359d8d13fSGarrett Wollman case AF_INET: 21459d8d13fSGarrett Wollman break; 21559d8d13fSGarrett Wollman #endif 216cfa1ca9dSYoshinobu Inoue #ifdef INET6 217cfa1ca9dSYoshinobu Inoue case AF_INET6: 218cfa1ca9dSYoshinobu Inoue break; 219cfa1ca9dSYoshinobu Inoue #endif 22059d8d13fSGarrett Wollman 22159d8d13fSGarrett Wollman default: 22259d8d13fSGarrett Wollman error = EAFNOSUPPORT; 22359d8d13fSGarrett Wollman break; 22459d8d13fSGarrett Wollman } 22559d8d13fSGarrett Wollman break; 22659d8d13fSGarrett Wollman 22759d8d13fSGarrett Wollman case SIOCSIFMTU: 22859d8d13fSGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 22959d8d13fSGarrett Wollman break; 23059d8d13fSGarrett Wollman 23159d8d13fSGarrett Wollman default: 23259d8d13fSGarrett Wollman error = EINVAL; 23359d8d13fSGarrett Wollman } 23464b15424SJonathan Lemon return (error); 23559d8d13fSGarrett Wollman } 236