if_disc.c revision 106939
1114402Sru/* 2114402Sru * Copyright (c) 1982, 1986, 1993 3114402Sru * The Regents of the University of California. All rights reserved. 4114402Sru * 5114402Sru * Redistribution and use in source and binary forms, with or without 6114402Sru * modification, are permitted provided that the following conditions 7114402Sru * are met: 8114402Sru * 1. Redistributions of source code must retain the above copyright 9114402Sru * notice, this list of conditions and the following disclaimer. 10114402Sru * 2. Redistributions in binary form must reproduce the above copyright 11114402Sru * notice, this list of conditions and the following disclaimer in the 12114402Sru * documentation and/or other materials provided with the distribution. 13114402Sru * 3. All advertising materials mentioning features or use of this software 14114402Sru * must display the following acknowledgement: 15114402Sru * This product includes software developed by the University of 16114402Sru * California, Berkeley and its contributors. 17114402Sru * 4. Neither the name of the University nor the names of its contributors 18114402Sru * may be used to endorse or promote products derived from this software 19114402Sru * without specific prior written permission. 20151497Sru * 21114402Sru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22114402Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23114402Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24114402Sru * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25114402Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26114402Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27114402Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28114402Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29114402Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30114402Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31114402Sru * SUCH DAMAGE. 32114402Sru * 33114402Sru * From: @(#)if_loop.c 8.1 (Berkeley) 6/10/93 34114402Sru * $FreeBSD: head/sys/net/if_disc.c 106939 2002-11-15 00:00:15Z sam $ 35114402Sru */ 36114402Sru 37114402Sru/* 38114402Sru * Discard interface driver for protocol testing and timing. 39114402Sru * (Based on the loopback.) 40114402Sru */ 41114402Sru 42114402Sru#include <sys/param.h> 43114402Sru#include <sys/systm.h> 44114402Sru#include <sys/kernel.h> 45114402Sru#include <sys/malloc.h> 46114402Sru#include <sys/module.h> 47114402Sru#include <sys/mbuf.h> 48114402Sru#include <sys/socket.h> 49114402Sru#include <sys/sockio.h> 50114402Sru 51114402Sru#include <net/if.h> 52114402Sru#include <net/if_types.h> 53114402Sru#include <net/route.h> 54114402Sru#include <net/bpf.h> 55114402Sru 56114402Sru#include "opt_inet.h" 57114402Sru#include "opt_inet6.h" 58114402Sru 59114402Sru#ifdef TINY_DSMTU 60114402Sru#define DSMTU (1024+512) 61114402Sru#else 62114402Sru#define DSMTU 65532 63114402Sru#endif 64114402Sru 65114402Sru#define DISCNAME "disc" 66114402Sru 67114402Srustruct disc_softc { 68114402Sru struct ifnet sc_if; /* must be first */ 69114402Sru LIST_ENTRY(disc_softc) sc_list; 70114402Sru}; 71114402Sru 72114402Srustatic int discoutput(struct ifnet *, struct mbuf *, 73114402Sru struct sockaddr *, struct rtentry *); 74114402Srustatic void discrtrequest(int, struct rtentry *, struct rt_addrinfo *); 75114402Srustatic int discioctl(struct ifnet *, u_long, caddr_t); 76114402Srustatic int disc_clone_create(struct if_clone *, int); 77114402Srustatic void disc_clone_destroy(struct ifnet *); 78114402Sru 79114402Srustatic MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface"); 80114402Srustatic LIST_HEAD(, disc_softc) disc_softc_list; 81114402Srustatic struct if_clone disc_cloner = IF_CLONE_INITIALIZER(DISCNAME, 82114402Sru disc_clone_create, disc_clone_destroy, 0, IF_MAXUNIT); 83114402Sru 84114402Srustatic int 85114402Srudisc_clone_create(struct if_clone *ifc, int unit) 86114402Sru{ 87114402Sru struct ifnet *ifp; 88114402Sru struct disc_softc *sc; 89114402Sru 90114402Sru sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK); 91114402Sru bzero(sc, sizeof(struct disc_softc)); 92114402Sru 93114402Sru ifp = &sc->sc_if; 94114402Sru 95114402Sru ifp->if_softc = sc; 96114402Sru ifp->if_name = DISCNAME; 97114402Sru ifp->if_unit = unit; 98114402Sru ifp->if_mtu = DSMTU; 99114402Sru ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 100114402Sru ifp->if_ioctl = discioctl; 101114402Sru ifp->if_output = discoutput; 102114402Sru ifp->if_type = IFT_LOOP; 103114402Sru ifp->if_hdrlen = 0; 104114402Sru ifp->if_addrlen = 0; 105114402Sru ifp->if_snd.ifq_maxlen = 20; 106114402Sru if_attach(ifp); 107114402Sru bpfattach(ifp, DLT_NULL, sizeof(u_int)); 108114402Sru LIST_INSERT_HEAD(&disc_softc_list, sc, sc_list); 109114402Sru 110114402Sru return (0); 111114402Sru} 112114402Sru 113114402Srustatic void 114114402Srudisc_clone_destroy(struct ifnet *ifp) 115114402Sru{ 116114402Sru struct disc_softc *sc; 117114402Sru 118114402Sru sc = ifp->if_softc; 119114402Sru 120114402Sru LIST_REMOVE(sc, sc_list); 121114402Sru bpfdetach(ifp); 122114402Sru if_detach(ifp); 123114402Sru 124114402Sru free(sc, M_DISC); 125114402Sru} 126114402Sru 127114402Srustatic int 128114402Srudisc_modevent(module_t mod, int type, void *data) 129114402Sru{ 130114402Sru switch (type) { 131114402Sru case MOD_LOAD: 132114402Sru LIST_INIT(&disc_softc_list); 133114402Sru if_clone_attach(&disc_cloner); 134114402Sru break; 135114402Sru case MOD_UNLOAD: 136114402Sru if_clone_detach(&disc_cloner); 137114402Sru 138114402Sru while (!LIST_EMPTY(&disc_softc_list)) 139114402Sru disc_clone_destroy( 140114402Sru &LIST_FIRST(&disc_softc_list)->sc_if); 141114402Sru break; 142114402Sru } 143114402Sru return 0; 144114402Sru} 145114402Sru 146114402Srustatic moduledata_t disc_mod = { 147114402Sru "if_disc", 148114402Sru disc_modevent, 149114402Sru NULL 150114402Sru}; 151114402Sru 152114402SruDECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 153114402Sru 154114402Srustatic int 155114402Srudiscoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 156114402Sru struct rtentry *rt) 157114402Sru{ 158114402Sru if ((m->m_flags & M_PKTHDR) == 0) 159114402Sru panic("discoutput no HDR"); 160114402Sru /* BPF write needs to be handled specially */ 161114402Sru if (dst->sa_family == AF_UNSPEC) { 162114402Sru dst->sa_family = *(mtod(m, int *)); 163114402Sru m->m_len -= sizeof(int); 164114402Sru m->m_pkthdr.len -= sizeof(int); 165114402Sru m->m_data += sizeof(int); 166114402Sru } 167114402Sru 168114402Sru if (ifp->if_bpf) { 169114402Sru /* 170114402Sru * We need to prepend the address family as 171114402Sru * a four byte field. Cons up a dummy header 172114402Sru * to pacify bpf. This is safe because bpf 173114402Sru * will only read from the mbuf (i.e., it won't 174114402Sru * try to free it or keep a pointer a to it). 175114402Sru */ 176114402Sru struct mbuf m0; 177114402Sru u_int af = dst->sa_family; 178114402Sru 179114402Sru m0.m_next = m; 180114402Sru m0.m_len = 4; 181114402Sru m0.m_data = (char *)⁡ 182114402Sru 183114402Sru BPF_MTAP(ifp, &m0); 184114402Sru } 185114402Sru m->m_pkthdr.rcvif = ifp; 186114402Sru 187114402Sru ifp->if_opackets++; 188114402Sru ifp->if_obytes += m->m_pkthdr.len; 189114402Sru 190114402Sru m_freem(m); 191114402Sru return 0; 192114402Sru} 193114402Sru 194114402Sru/* ARGSUSED */ 195114402Srustatic void 196114402Srudiscrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 197114402Sru{ 198 if (rt) 199 rt->rt_rmx.rmx_mtu = DSMTU; 200} 201 202/* 203 * Process an ioctl request. 204 */ 205static int 206discioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 207{ 208 struct ifaddr *ifa; 209 struct ifreq *ifr = (struct ifreq *)data; 210 int error = 0; 211 212 switch (cmd) { 213 214 case SIOCSIFADDR: 215 ifp->if_flags |= IFF_UP; 216 ifa = (struct ifaddr *)data; 217 if (ifa != 0) 218 ifa->ifa_rtrequest = discrtrequest; 219 /* 220 * Everything else is done at a higher level. 221 */ 222 break; 223 224 case SIOCADDMULTI: 225 case SIOCDELMULTI: 226 if (ifr == 0) { 227 error = EAFNOSUPPORT; /* XXX */ 228 break; 229 } 230 switch (ifr->ifr_addr.sa_family) { 231 232#ifdef INET 233 case AF_INET: 234 break; 235#endif 236#ifdef INET6 237 case AF_INET6: 238 break; 239#endif 240 241 default: 242 error = EAFNOSUPPORT; 243 break; 244 } 245 break; 246 247 case SIOCSIFMTU: 248 ifp->if_mtu = ifr->ifr_mtu; 249 break; 250 251 default: 252 error = EINVAL; 253 } 254 return (error); 255} 256