159d8d13fSGarrett Wollman /* 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 * 3. All advertising materials mentioning features or use of this software 1459d8d13fSGarrett Wollman * must display the following acknowledgement: 1559d8d13fSGarrett Wollman * This product includes software developed by the University of 1659d8d13fSGarrett Wollman * California, Berkeley and its contributors. 1759d8d13fSGarrett Wollman * 4. Neither the name of the University nor the names of its contributors 1859d8d13fSGarrett Wollman * may be used to endorse or promote products derived from this software 1959d8d13fSGarrett Wollman * without specific prior written permission. 2059d8d13fSGarrett Wollman * 2159d8d13fSGarrett Wollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2259d8d13fSGarrett Wollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2359d8d13fSGarrett Wollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2459d8d13fSGarrett Wollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2559d8d13fSGarrett Wollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2659d8d13fSGarrett Wollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2759d8d13fSGarrett Wollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2859d8d13fSGarrett Wollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2959d8d13fSGarrett Wollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3059d8d13fSGarrett Wollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3159d8d13fSGarrett Wollman * SUCH DAMAGE. 3259d8d13fSGarrett Wollman * 3359d8d13fSGarrett Wollman * From: @(#)if_loop.c 8.1 (Berkeley) 6/10/93 34c3aac50fSPeter Wemm * $FreeBSD$ 3559d8d13fSGarrett Wollman */ 3659d8d13fSGarrett Wollman 3759d8d13fSGarrett Wollman /* 3859d8d13fSGarrett Wollman * Discard interface driver for protocol testing and timing. 3959d8d13fSGarrett Wollman * (Based on the loopback.) 4059d8d13fSGarrett Wollman */ 4159d8d13fSGarrett Wollman 4259d8d13fSGarrett Wollman #include <sys/param.h> 4359d8d13fSGarrett Wollman #include <sys/systm.h> 4459d8d13fSGarrett Wollman #include <sys/kernel.h> 45c69b7ffeSBrooks Davis #include <sys/malloc.h> 462b120974SPeter Wemm #include <sys/module.h> 4759d8d13fSGarrett Wollman #include <sys/mbuf.h> 4859d8d13fSGarrett Wollman #include <sys/socket.h> 4951a53488SBruce Evans #include <sys/sockio.h> 5059d8d13fSGarrett Wollman 5159d8d13fSGarrett Wollman #include <net/if.h> 5259d8d13fSGarrett Wollman #include <net/if_types.h> 5359d8d13fSGarrett Wollman #include <net/route.h> 5459d8d13fSGarrett Wollman #include <net/bpf.h> 5559d8d13fSGarrett Wollman 561d5e9e22SEivind Eklund #include "opt_inet.h" 57cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 5859d8d13fSGarrett Wollman 5959d8d13fSGarrett Wollman #ifdef TINY_DSMTU 6059d8d13fSGarrett Wollman #define DSMTU (1024+512) 6159d8d13fSGarrett Wollman #else 6259d8d13fSGarrett Wollman #define DSMTU 65532 6359d8d13fSGarrett Wollman #endif 6459d8d13fSGarrett Wollman 65c69b7ffeSBrooks Davis #define DISCNAME "disc" 66b6f5c0b8SBruce Evans 67c69b7ffeSBrooks Davis struct disc_softc { 68c69b7ffeSBrooks Davis struct ifnet sc_if; /* must be first */ 69c69b7ffeSBrooks Davis LIST_ENTRY(disc_softc) sc_list; 70c69b7ffeSBrooks Davis }; 71c69b7ffeSBrooks Davis 72eb6bd594SMark Murray static int discoutput(struct ifnet *, struct mbuf *, 73eb6bd594SMark Murray struct sockaddr *, struct rtentry *); 748071913dSRuslan Ermilov static void discrtrequest(int, struct rtentry *, struct rt_addrinfo *); 75537ad974SEivind Eklund static int discioctl(struct ifnet *, u_long, caddr_t); 76c69b7ffeSBrooks Davis static int disc_clone_create(struct if_clone *, int); 77c69b7ffeSBrooks Davis static void disc_clone_destroy(struct ifnet *); 7859d8d13fSGarrett Wollman 79c69b7ffeSBrooks Davis static MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface"); 80c69b7ffeSBrooks Davis static LIST_HEAD(, disc_softc) disc_softc_list; 81c69b7ffeSBrooks Davis static struct if_clone disc_cloner = IF_CLONE_INITIALIZER(DISCNAME, 82c69b7ffeSBrooks Davis disc_clone_create, disc_clone_destroy, 0, IF_MAXUNIT); 83c69b7ffeSBrooks Davis 84c69b7ffeSBrooks Davis static int 85c69b7ffeSBrooks Davis disc_clone_create(struct if_clone *ifc, int unit) 8659d8d13fSGarrett Wollman { 87c69b7ffeSBrooks Davis struct ifnet *ifp; 88c69b7ffeSBrooks Davis struct disc_softc *sc; 8959d8d13fSGarrett Wollman 90c69b7ffeSBrooks Davis sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK); 91c69b7ffeSBrooks Davis bzero(sc, sizeof(struct disc_softc)); 92c69b7ffeSBrooks Davis 93c69b7ffeSBrooks Davis ifp = &sc->sc_if; 94c69b7ffeSBrooks Davis 95c69b7ffeSBrooks Davis ifp->if_softc = sc; 96c69b7ffeSBrooks Davis ifp->if_name = DISCNAME; 97c69b7ffeSBrooks Davis ifp->if_unit = unit; 9859d8d13fSGarrett Wollman ifp->if_mtu = DSMTU; 9959d8d13fSGarrett Wollman ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 100537ad974SEivind Eklund ifp->if_ioctl = discioctl; 101537ad974SEivind Eklund ifp->if_output = discoutput; 10259d8d13fSGarrett Wollman ifp->if_type = IFT_LOOP; 10359d8d13fSGarrett Wollman ifp->if_hdrlen = 0; 10459d8d13fSGarrett Wollman ifp->if_addrlen = 0; 105422fd76fSPoul-Henning Kamp ifp->if_snd.ifq_maxlen = 20; 10659d8d13fSGarrett Wollman if_attach(ifp); 1079b44ff22SGarrett Wollman bpfattach(ifp, DLT_NULL, sizeof(u_int)); 108c69b7ffeSBrooks Davis LIST_INSERT_HEAD(&disc_softc_list, sc, sc_list); 109c69b7ffeSBrooks Davis 110c69b7ffeSBrooks Davis return (0); 111c69b7ffeSBrooks Davis } 112c69b7ffeSBrooks Davis 113c69b7ffeSBrooks Davis static void 114c69b7ffeSBrooks Davis disc_clone_destroy(struct ifnet *ifp) 115c69b7ffeSBrooks Davis { 116c69b7ffeSBrooks Davis struct disc_softc *sc; 117c69b7ffeSBrooks Davis 118c69b7ffeSBrooks Davis sc = ifp->if_softc; 119c69b7ffeSBrooks Davis 120c69b7ffeSBrooks Davis LIST_REMOVE(sc, sc_list); 121c69b7ffeSBrooks Davis bpfdetach(ifp); 122c69b7ffeSBrooks Davis if_detach(ifp); 123c69b7ffeSBrooks Davis 124c69b7ffeSBrooks Davis free(sc, M_DISC); 12559d8d13fSGarrett Wollman } 12659d8d13fSGarrett Wollman 12759d8d13fSGarrett Wollman static int 1282b120974SPeter Wemm disc_modevent(module_t mod, int type, void *data) 1292b120974SPeter Wemm { 1302b120974SPeter Wemm switch (type) { 1312b120974SPeter Wemm case MOD_LOAD: 132c69b7ffeSBrooks Davis LIST_INIT(&disc_softc_list); 133c69b7ffeSBrooks Davis if_clone_attach(&disc_cloner); 1342b120974SPeter Wemm break; 1352b120974SPeter Wemm case MOD_UNLOAD: 136c69b7ffeSBrooks Davis if_clone_detach(&disc_cloner); 137c69b7ffeSBrooks Davis 138c69b7ffeSBrooks Davis while (!LIST_EMPTY(&disc_softc_list)) 139c69b7ffeSBrooks Davis disc_clone_destroy( 140c69b7ffeSBrooks Davis &LIST_FIRST(&disc_softc_list)->sc_if); 141c69b7ffeSBrooks Davis break; 1422b120974SPeter Wemm } 1432b120974SPeter Wemm return 0; 1442b120974SPeter Wemm } 1452b120974SPeter Wemm 1462b120974SPeter Wemm static moduledata_t disc_mod = { 1472b120974SPeter Wemm "if_disc", 1482b120974SPeter Wemm disc_modevent, 1492b120974SPeter Wemm NULL 1502b120974SPeter Wemm }; 1512b120974SPeter Wemm 1522b120974SPeter Wemm DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 1532b120974SPeter Wemm 1542b120974SPeter Wemm static int 155eb6bd594SMark Murray discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 156eb6bd594SMark Murray struct rtentry *rt) 15759d8d13fSGarrett Wollman { 15859d8d13fSGarrett Wollman if ((m->m_flags & M_PKTHDR) == 0) 159537ad974SEivind Eklund panic("discoutput no HDR"); 160963e4c2aSGarrett Wollman /* BPF write needs to be handled specially */ 161963e4c2aSGarrett Wollman if (dst->sa_family == AF_UNSPEC) { 162963e4c2aSGarrett Wollman dst->sa_family = *(mtod(m, int *)); 163963e4c2aSGarrett Wollman m->m_len -= sizeof(int); 164963e4c2aSGarrett Wollman m->m_pkthdr.len -= sizeof(int); 165963e4c2aSGarrett Wollman m->m_data += sizeof(int); 166963e4c2aSGarrett Wollman } 167963e4c2aSGarrett Wollman 168c69b7ffeSBrooks Davis if (ifp->if_bpf) { 16959d8d13fSGarrett Wollman /* 17059d8d13fSGarrett Wollman * We need to prepend the address family as 17159d8d13fSGarrett Wollman * a four byte field. Cons up a dummy header 17259d8d13fSGarrett Wollman * to pacify bpf. This is safe because bpf 17359d8d13fSGarrett Wollman * will only read from the mbuf (i.e., it won't 17459d8d13fSGarrett Wollman * try to free it or keep a pointer a to it). 17559d8d13fSGarrett Wollman */ 17659d8d13fSGarrett Wollman struct mbuf m0; 17759d8d13fSGarrett Wollman u_int af = dst->sa_family; 17859d8d13fSGarrett Wollman 17959d8d13fSGarrett Wollman m0.m_next = m; 18059d8d13fSGarrett Wollman m0.m_len = 4; 18159d8d13fSGarrett Wollman m0.m_data = (char *)⁡ 18259d8d13fSGarrett Wollman 183c69b7ffeSBrooks Davis bpf_mtap(ifp, &m0); 18459d8d13fSGarrett Wollman } 18559d8d13fSGarrett Wollman m->m_pkthdr.rcvif = ifp; 18659d8d13fSGarrett Wollman 18759d8d13fSGarrett Wollman ifp->if_opackets++; 18859d8d13fSGarrett Wollman ifp->if_obytes += m->m_pkthdr.len; 18959d8d13fSGarrett Wollman 19059d8d13fSGarrett Wollman m_freem(m); 19159d8d13fSGarrett Wollman return 0; 19259d8d13fSGarrett Wollman } 19359d8d13fSGarrett Wollman 19459d8d13fSGarrett Wollman /* ARGSUSED */ 19559d8d13fSGarrett Wollman static void 1968071913dSRuslan Ermilov discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 19759d8d13fSGarrett Wollman { 19859d8d13fSGarrett Wollman if (rt) 19959d8d13fSGarrett Wollman rt->rt_rmx.rmx_mtu = DSMTU; 20059d8d13fSGarrett Wollman } 20159d8d13fSGarrett Wollman 20259d8d13fSGarrett Wollman /* 20359d8d13fSGarrett Wollman * Process an ioctl request. 20459d8d13fSGarrett Wollman */ 20588e2f526SBruce Evans static int 206eb6bd594SMark Murray discioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 20759d8d13fSGarrett Wollman { 208eb6bd594SMark Murray struct ifaddr *ifa; 209eb6bd594SMark Murray struct ifreq *ifr = (struct ifreq *)data; 210eb6bd594SMark Murray int error = 0; 21159d8d13fSGarrett Wollman 21259d8d13fSGarrett Wollman switch (cmd) { 21359d8d13fSGarrett Wollman 21459d8d13fSGarrett Wollman case SIOCSIFADDR: 21559d8d13fSGarrett Wollman ifp->if_flags |= IFF_UP; 21659d8d13fSGarrett Wollman ifa = (struct ifaddr *)data; 21759d8d13fSGarrett Wollman if (ifa != 0) 218537ad974SEivind Eklund ifa->ifa_rtrequest = discrtrequest; 21959d8d13fSGarrett Wollman /* 22059d8d13fSGarrett Wollman * Everything else is done at a higher level. 22159d8d13fSGarrett Wollman */ 22259d8d13fSGarrett Wollman break; 22359d8d13fSGarrett Wollman 22459d8d13fSGarrett Wollman case SIOCADDMULTI: 22559d8d13fSGarrett Wollman case SIOCDELMULTI: 22659d8d13fSGarrett Wollman if (ifr == 0) { 22759d8d13fSGarrett Wollman error = EAFNOSUPPORT; /* XXX */ 22859d8d13fSGarrett Wollman break; 22959d8d13fSGarrett Wollman } 23059d8d13fSGarrett Wollman switch (ifr->ifr_addr.sa_family) { 23159d8d13fSGarrett Wollman 23259d8d13fSGarrett Wollman #ifdef INET 23359d8d13fSGarrett Wollman case AF_INET: 23459d8d13fSGarrett Wollman break; 23559d8d13fSGarrett Wollman #endif 236cfa1ca9dSYoshinobu Inoue #ifdef INET6 237cfa1ca9dSYoshinobu Inoue case AF_INET6: 238cfa1ca9dSYoshinobu Inoue break; 239cfa1ca9dSYoshinobu Inoue #endif 24059d8d13fSGarrett Wollman 24159d8d13fSGarrett Wollman default: 24259d8d13fSGarrett Wollman error = EAFNOSUPPORT; 24359d8d13fSGarrett Wollman break; 24459d8d13fSGarrett Wollman } 24559d8d13fSGarrett Wollman break; 24659d8d13fSGarrett Wollman 24759d8d13fSGarrett Wollman case SIOCSIFMTU: 24859d8d13fSGarrett Wollman ifp->if_mtu = ifr->ifr_mtu; 24959d8d13fSGarrett Wollman break; 25059d8d13fSGarrett Wollman 25159d8d13fSGarrett Wollman default: 25259d8d13fSGarrett Wollman error = EINVAL; 25359d8d13fSGarrett Wollman } 25464b15424SJonathan Lemon return (error); 25559d8d13fSGarrett Wollman } 256