if_disc.c revision 147611
1139823Simp/*- 25191Swollman * Copyright (c) 1982, 1986, 1993 35191Swollman * The Regents of the University of California. All rights reserved. 45191Swollman * 55191Swollman * Redistribution and use in source and binary forms, with or without 65191Swollman * modification, are permitted provided that the following conditions 75191Swollman * are met: 85191Swollman * 1. Redistributions of source code must retain the above copyright 95191Swollman * notice, this list of conditions and the following disclaimer. 105191Swollman * 2. Redistributions in binary form must reproduce the above copyright 115191Swollman * notice, this list of conditions and the following disclaimer in the 125191Swollman * documentation and/or other materials provided with the distribution. 135191Swollman * 4. Neither the name of the University nor the names of its contributors 145191Swollman * may be used to endorse or promote products derived from this software 155191Swollman * without specific prior written permission. 165191Swollman * 175191Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 185191Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 195191Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 205191Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 215191Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 225191Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 235191Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 245191Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 255191Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 265191Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 275191Swollman * SUCH DAMAGE. 285191Swollman * 295191Swollman * From: @(#)if_loop.c 8.1 (Berkeley) 6/10/93 3050477Speter * $FreeBSD: head/sys/net/if_disc.c 147611 2005-06-26 18:11:11Z dwmalone $ 315191Swollman */ 325191Swollman 335191Swollman/* 345191Swollman * Discard interface driver for protocol testing and timing. 355191Swollman * (Based on the loopback.) 365191Swollman */ 375191Swollman 385191Swollman#include <sys/param.h> 395191Swollman#include <sys/systm.h> 405191Swollman#include <sys/kernel.h> 4197290Sbrooks#include <sys/malloc.h> 4271862Speter#include <sys/module.h> 435191Swollman#include <sys/mbuf.h> 445191Swollman#include <sys/socket.h> 4524204Sbde#include <sys/sockio.h> 465191Swollman 475191Swollman#include <net/if.h> 48130933Sbrooks#include <net/if_clone.h> 495191Swollman#include <net/if_types.h> 505191Swollman#include <net/route.h> 515191Swollman#include <net/bpf.h> 525191Swollman 5332350Seivind#include "opt_inet.h" 5454263Sshin#include "opt_inet6.h" 555191Swollman 565191Swollman#ifdef TINY_DSMTU 575191Swollman#define DSMTU (1024+512) 585191Swollman#else 595191Swollman#define DSMTU 65532 605191Swollman#endif 615191Swollman 6297290Sbrooks#define DISCNAME "disc" 6310429Sbde 6497290Sbrooksstruct disc_softc { 65147256Sbrooks struct ifnet *sc_ifp; /* must be first */ 6697290Sbrooks LIST_ENTRY(disc_softc) sc_list; 6797290Sbrooks}; 685191Swollman 6997290Sbrooksstatic int discoutput(struct ifnet *, struct mbuf *, 7097290Sbrooks struct sockaddr *, struct rtentry *); 7197290Sbrooksstatic void discrtrequest(int, struct rtentry *, struct rt_addrinfo *); 7297290Sbrooksstatic int discioctl(struct ifnet *, u_long, caddr_t); 7397290Sbrooksstatic int disc_clone_create(struct if_clone *, int); 7497290Sbrooksstatic void disc_clone_destroy(struct ifnet *); 7597290Sbrooks 76126777Srwatsonstatic struct mtx disc_mtx; 7797290Sbrooksstatic MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface"); 7897290Sbrooksstatic LIST_HEAD(, disc_softc) disc_softc_list; 7997290Sbrooks 80130933SbrooksIFC_SIMPLE_DECLARE(disc, 0); 81130933Sbrooks 8297290Sbrooksstatic int 8397290Sbrooksdisc_clone_create(struct if_clone *ifc, int unit) 845191Swollman{ 8597290Sbrooks struct ifnet *ifp; 8697290Sbrooks struct disc_softc *sc; 875191Swollman 88131670Sbms sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK | M_ZERO); 89147256Sbrooks ifp = sc->sc_ifp = if_alloc(IFT_LOOP); 90147256Sbrooks if (ifp == NULL) { 91147256Sbrooks free(sc, M_DISC); 92147256Sbrooks return (ENOSPC); 93147256Sbrooks } 9497290Sbrooks 9597290Sbrooks ifp->if_softc = sc; 96121816Sbrooks if_initname(ifp, ifc->ifc_name, unit); 975191Swollman ifp->if_mtu = DSMTU; 985191Swollman ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 9941757Seivind ifp->if_ioctl = discioctl; 10041757Seivind ifp->if_output = discoutput; 1015191Swollman ifp->if_hdrlen = 0; 1025191Swollman ifp->if_addrlen = 0; 10353115Sphk ifp->if_snd.ifq_maxlen = 20; 1045191Swollman if_attach(ifp); 105147611Sdwmalone bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 106126777Srwatson mtx_lock(&disc_mtx); 10797290Sbrooks LIST_INSERT_HEAD(&disc_softc_list, sc, sc_list); 108126777Srwatson mtx_unlock(&disc_mtx); 10997290Sbrooks 11097290Sbrooks return (0); 1115191Swollman} 1125191Swollman 11397290Sbrooksstatic void 114126777Srwatsondisc_destroy(struct disc_softc *sc) 115126777Srwatson{ 116126777Srwatson 117147256Sbrooks bpfdetach(sc->sc_ifp); 118147256Sbrooks if_detach(sc->sc_ifp); 119147256Sbrooks if_free(sc->sc_ifp); 120126777Srwatson 121126777Srwatson free(sc, M_DISC); 122126777Srwatson} 123126777Srwatson 124126777Srwatsonstatic void 12597290Sbrooksdisc_clone_destroy(struct ifnet *ifp) 12697290Sbrooks{ 12797290Sbrooks struct disc_softc *sc; 12897290Sbrooks 12997290Sbrooks sc = ifp->if_softc; 130126777Srwatson mtx_lock(&disc_mtx); 13197290Sbrooks LIST_REMOVE(sc, sc_list); 132126777Srwatson mtx_unlock(&disc_mtx); 13397290Sbrooks 134126777Srwatson disc_destroy(sc); 13597290Sbrooks} 13697290Sbrooks 1375191Swollmanstatic int 138131669Sbmsdisc_modevent(module_t mod, int type, void *data) 139126777Srwatson{ 140126777Srwatson struct disc_softc *sc; 141126777Srwatson 142131669Sbms switch (type) { 143131669Sbms case MOD_LOAD: 144126777Srwatson mtx_init(&disc_mtx, "disc_mtx", NULL, MTX_DEF); 14597290Sbrooks LIST_INIT(&disc_softc_list); 14697290Sbrooks if_clone_attach(&disc_cloner); 147131669Sbms break; 148131669Sbms case MOD_UNLOAD: 14997290Sbrooks if_clone_detach(&disc_cloner); 15097290Sbrooks 151126777Srwatson mtx_lock(&disc_mtx); 152126777Srwatson while ((sc = LIST_FIRST(&disc_softc_list)) != NULL) { 153126777Srwatson LIST_REMOVE(sc, sc_list); 154126777Srwatson mtx_unlock(&disc_mtx); 155126777Srwatson disc_destroy(sc); 156126777Srwatson mtx_lock(&disc_mtx); 157126777Srwatson } 158126777Srwatson mtx_unlock(&disc_mtx); 159126777Srwatson mtx_destroy(&disc_mtx); 16097290Sbrooks break; 161132199Sphk default: 162132199Sphk return (EOPNOTSUPP); 163131669Sbms } 164131669Sbms return (0); 165131669Sbms} 16671862Speter 167131669Sbmsstatic moduledata_t disc_mod = { 168131669Sbms "if_disc", 169131669Sbms disc_modevent, 17071862Speter NULL 171131669Sbms}; 17271862Speter 17371862SpeterDECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 17471862Speter 17571862Speterstatic int 17678351Smarkmdiscoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 17778351Smarkm struct rtentry *rt) 1785191Swollman{ 179147611Sdwmalone u_int32_t af; 180131669Sbms 181113255Sdes M_ASSERTPKTHDR(m); 182131669Sbms 183147611Sdwmalone /* BPF writes need to be handled specially. */ 18410957Swollman if (dst->sa_family == AF_UNSPEC) { 185147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 186147611Sdwmalone dst->sa_family = af; 18710957Swollman } 18810957Swollman 18997290Sbrooks if (ifp->if_bpf) { 1905191Swollman u_int af = dst->sa_family; 191123922Ssam bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 1925191Swollman } 1935191Swollman m->m_pkthdr.rcvif = ifp; 1945191Swollman 1955191Swollman ifp->if_opackets++; 1965191Swollman ifp->if_obytes += m->m_pkthdr.len; 1975191Swollman 1985191Swollman m_freem(m); 199131669Sbms return (0); 2005191Swollman} 2015191Swollman 2025191Swollman/* ARGSUSED */ 2035191Swollmanstatic void 20485074Srudiscrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 2055191Swollman{ 206120727Ssam RT_LOCK_ASSERT(rt); 207142352Ssam rt->rt_rmx.rmx_mtu = DSMTU; 2085191Swollman} 2095191Swollman 2105191Swollman/* 2115191Swollman * Process an ioctl request. 2125191Swollman */ 21312611Sbdestatic int 21478351Smarkmdiscioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2155191Swollman{ 21678351Smarkm struct ifaddr *ifa; 21778351Smarkm struct ifreq *ifr = (struct ifreq *)data; 21878351Smarkm int error = 0; 2195191Swollman 2205191Swollman switch (cmd) { 2215191Swollman 2225191Swollman case SIOCSIFADDR: 2235191Swollman ifp->if_flags |= IFF_UP; 2245191Swollman ifa = (struct ifaddr *)data; 2255191Swollman if (ifa != 0) 22641757Seivind ifa->ifa_rtrequest = discrtrequest; 2275191Swollman /* 2285191Swollman * Everything else is done at a higher level. 2295191Swollman */ 2305191Swollman break; 2315191Swollman 2325191Swollman case SIOCADDMULTI: 2335191Swollman case SIOCDELMULTI: 2345191Swollman if (ifr == 0) { 2355191Swollman error = EAFNOSUPPORT; /* XXX */ 2365191Swollman break; 2375191Swollman } 2385191Swollman switch (ifr->ifr_addr.sa_family) { 2395191Swollman 2405191Swollman#ifdef INET 2415191Swollman case AF_INET: 2425191Swollman break; 2435191Swollman#endif 24454263Sshin#ifdef INET6 24554263Sshin case AF_INET6: 24654263Sshin break; 24754263Sshin#endif 2485191Swollman 2495191Swollman default: 2505191Swollman error = EAFNOSUPPORT; 2515191Swollman break; 2525191Swollman } 2535191Swollman break; 2545191Swollman 2555191Swollman case SIOCSIFMTU: 2565191Swollman ifp->if_mtu = ifr->ifr_mtu; 2575191Swollman break; 2585191Swollman 2595191Swollman default: 2605191Swollman error = EINVAL; 2615191Swollman } 26287912Sjlemon return (error); 2635191Swollman} 264