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$ 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 { 65167897Syar struct ifnet *sc_ifp; 6697290Sbrooks}; 675191Swollman 6897290Sbrooksstatic int discoutput(struct ifnet *, struct mbuf *, 69191148Skmacy struct sockaddr *, struct route *); 7097290Sbrooksstatic void discrtrequest(int, struct rtentry *, struct rt_addrinfo *); 7197290Sbrooksstatic int discioctl(struct ifnet *, u_long, caddr_t); 72160195Ssamstatic int disc_clone_create(struct if_clone *, int, caddr_t); 7397290Sbrooksstatic void disc_clone_destroy(struct ifnet *); 7497290Sbrooks 7597290Sbrooksstatic MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface"); 7697290Sbrooks 77130933SbrooksIFC_SIMPLE_DECLARE(disc, 0); 78130933Sbrooks 7997290Sbrooksstatic int 80160195Ssamdisc_clone_create(struct if_clone *ifc, int unit, caddr_t params) 815191Swollman{ 8297290Sbrooks struct ifnet *ifp; 8397290Sbrooks struct disc_softc *sc; 845191Swollman 85131670Sbms sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK | M_ZERO); 86147256Sbrooks ifp = sc->sc_ifp = if_alloc(IFT_LOOP); 87147256Sbrooks if (ifp == NULL) { 88147256Sbrooks free(sc, M_DISC); 89147256Sbrooks return (ENOSPC); 90147256Sbrooks } 9197290Sbrooks 9297290Sbrooks ifp->if_softc = sc; 93121816Sbrooks if_initname(ifp, ifc->ifc_name, unit); 945191Swollman ifp->if_mtu = DSMTU; 95173076Syar /* 96173076Syar * IFF_LOOPBACK should not be removed from disc's flags because 97173076Syar * it controls what PF-specific routes are magically added when 98173076Syar * a network address is assigned to the interface. Things just 99173076Syar * won't work as intended w/o such routes because the output 100173076Syar * interface selection for a packet is totally route-driven. 101173076Syar * A valid alternative to IFF_LOOPBACK can be IFF_BROADCAST or 102173076Syar * IFF_POINTOPOINT, but it would result in different properties 103173076Syar * of the interface. 104173076Syar */ 1055191Swollman ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 106152308Sglebius ifp->if_drv_flags = IFF_DRV_RUNNING; 10741757Seivind ifp->if_ioctl = discioctl; 10841757Seivind ifp->if_output = discoutput; 1095191Swollman ifp->if_hdrlen = 0; 1105191Swollman ifp->if_addrlen = 0; 11153115Sphk ifp->if_snd.ifq_maxlen = 20; 1125191Swollman if_attach(ifp); 113147611Sdwmalone bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 11497290Sbrooks 11597290Sbrooks return (0); 1165191Swollman} 1175191Swollman 11897290Sbrooksstatic void 11997290Sbrooksdisc_clone_destroy(struct ifnet *ifp) 12097290Sbrooks{ 12197290Sbrooks struct disc_softc *sc; 12297290Sbrooks 12397290Sbrooks sc = ifp->if_softc; 12497290Sbrooks 125151266Sthompsa bpfdetach(ifp); 126151266Sthompsa if_detach(ifp); 127151266Sthompsa if_free(ifp); 128151266Sthompsa 129151266Sthompsa free(sc, M_DISC); 13097290Sbrooks} 13197290Sbrooks 1325191Swollmanstatic int 133131669Sbmsdisc_modevent(module_t mod, int type, void *data) 134126777Srwatson{ 135126777Srwatson 136131669Sbms switch (type) { 137131669Sbms case MOD_LOAD: 13897290Sbrooks if_clone_attach(&disc_cloner); 139131669Sbms break; 140131669Sbms case MOD_UNLOAD: 14197290Sbrooks if_clone_detach(&disc_cloner); 14297290Sbrooks break; 143132199Sphk default: 144132199Sphk return (EOPNOTSUPP); 145131669Sbms } 146131669Sbms return (0); 147131669Sbms} 14871862Speter 149131669Sbmsstatic moduledata_t disc_mod = { 150131669Sbms "if_disc", 151131669Sbms disc_modevent, 15271862Speter NULL 153131669Sbms}; 15471862Speter 15571862SpeterDECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 15671862Speter 15771862Speterstatic int 15878351Smarkmdiscoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 159191148Skmacy struct route *ro) 1605191Swollman{ 161147611Sdwmalone u_int32_t af; 162131669Sbms 163113255Sdes M_ASSERTPKTHDR(m); 164131669Sbms 165147611Sdwmalone /* BPF writes need to be handled specially. */ 16610957Swollman if (dst->sa_family == AF_UNSPEC) { 167147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 168147611Sdwmalone dst->sa_family = af; 16910957Swollman } 17010957Swollman 171159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 1725191Swollman u_int af = dst->sa_family; 173123922Ssam bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 1745191Swollman } 1755191Swollman m->m_pkthdr.rcvif = ifp; 1765191Swollman 1775191Swollman ifp->if_opackets++; 1785191Swollman ifp->if_obytes += m->m_pkthdr.len; 1795191Swollman 1805191Swollman m_freem(m); 181131669Sbms return (0); 1825191Swollman} 1835191Swollman 1845191Swollman/* ARGSUSED */ 1855191Swollmanstatic void 18685074Srudiscrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1875191Swollman{ 188120727Ssam RT_LOCK_ASSERT(rt); 189142352Ssam rt->rt_rmx.rmx_mtu = DSMTU; 1905191Swollman} 1915191Swollman 1925191Swollman/* 1935191Swollman * Process an ioctl request. 1945191Swollman */ 19512611Sbdestatic int 19678351Smarkmdiscioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1975191Swollman{ 19878351Smarkm struct ifaddr *ifa; 19978351Smarkm struct ifreq *ifr = (struct ifreq *)data; 20078351Smarkm int error = 0; 2015191Swollman 2025191Swollman switch (cmd) { 2035191Swollman 2045191Swollman case SIOCSIFADDR: 2055191Swollman ifp->if_flags |= IFF_UP; 2065191Swollman ifa = (struct ifaddr *)data; 2075191Swollman if (ifa != 0) 20841757Seivind ifa->ifa_rtrequest = discrtrequest; 2095191Swollman /* 2105191Swollman * Everything else is done at a higher level. 2115191Swollman */ 2125191Swollman break; 2135191Swollman 2145191Swollman case SIOCADDMULTI: 2155191Swollman case SIOCDELMULTI: 2165191Swollman if (ifr == 0) { 2175191Swollman error = EAFNOSUPPORT; /* XXX */ 2185191Swollman break; 2195191Swollman } 2205191Swollman switch (ifr->ifr_addr.sa_family) { 2215191Swollman 2225191Swollman#ifdef INET 2235191Swollman case AF_INET: 2245191Swollman break; 2255191Swollman#endif 22654263Sshin#ifdef INET6 22754263Sshin case AF_INET6: 22854263Sshin break; 22954263Sshin#endif 2305191Swollman 2315191Swollman default: 2325191Swollman error = EAFNOSUPPORT; 2335191Swollman break; 2345191Swollman } 2355191Swollman break; 2365191Swollman 2375191Swollman case SIOCSIFMTU: 2385191Swollman ifp->if_mtu = ifr->ifr_mtu; 2395191Swollman break; 2405191Swollman 2415191Swollman default: 2425191Swollman error = EINVAL; 2435191Swollman } 24487912Sjlemon return (error); 2455191Swollman} 246