1 /*- 2 * Copyright (c) 1982, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * From: @(#)if_loop.c 8.1 (Berkeley) 6/10/93 30 * $FreeBSD$ 31 */ 32 33 /* 34 * Discard interface driver for protocol testing and timing. 35 * (Based on the loopback.) 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/malloc.h> 42 #include <sys/module.h> 43 #include <sys/mbuf.h> 44 #include <sys/socket.h> 45 #include <sys/sockio.h> 46 47 #include <net/if.h> 48 #include <net/if_var.h> 49 #include <net/if_clone.h> 50 #include <net/if_types.h> 51 #include <net/route.h> 52 #include <net/route_internal.h> 53 #include <net/bpf.h> 54 #include <net/vnet.h> 55 56 #include "opt_inet.h" 57 #include "opt_inet6.h" 58 59 #ifdef TINY_DSMTU 60 #define DSMTU (1024+512) 61 #else 62 #define DSMTU 65532 63 #endif 64 65 struct disc_softc { 66 struct ifnet *sc_ifp; 67 }; 68 69 static int discoutput(struct ifnet *, struct mbuf *, 70 const struct sockaddr *, struct route *); 71 static void discrtrequest(int, struct rtentry *, struct rt_addrinfo *); 72 static int discioctl(struct ifnet *, u_long, caddr_t); 73 static int disc_clone_create(struct if_clone *, int, caddr_t); 74 static void disc_clone_destroy(struct ifnet *); 75 76 static const char discname[] = "disc"; 77 static MALLOC_DEFINE(M_DISC, discname, "Discard interface"); 78 79 static VNET_DEFINE(struct if_clone *, disc_cloner); 80 #define V_disc_cloner VNET(disc_cloner) 81 82 static int 83 disc_clone_create(struct if_clone *ifc, int unit, caddr_t params) 84 { 85 struct ifnet *ifp; 86 struct disc_softc *sc; 87 88 sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK | M_ZERO); 89 ifp = sc->sc_ifp = if_alloc(IFT_LOOP); 90 if (ifp == NULL) { 91 free(sc, M_DISC); 92 return (ENOSPC); 93 } 94 95 ifp->if_softc = sc; 96 if_initname(ifp, discname, unit); 97 ifp->if_mtu = DSMTU; 98 /* 99 * IFF_LOOPBACK should not be removed from disc's flags because 100 * it controls what PF-specific routes are magically added when 101 * a network address is assigned to the interface. Things just 102 * won't work as intended w/o such routes because the output 103 * interface selection for a packet is totally route-driven. 104 * A valid alternative to IFF_LOOPBACK can be IFF_BROADCAST or 105 * IFF_POINTOPOINT, but it would result in different properties 106 * of the interface. 107 */ 108 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 109 ifp->if_drv_flags = IFF_DRV_RUNNING; 110 ifp->if_ioctl = discioctl; 111 ifp->if_output = discoutput; 112 ifp->if_hdrlen = 0; 113 ifp->if_addrlen = 0; 114 ifp->if_snd.ifq_maxlen = 20; 115 if_attach(ifp); 116 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 117 118 return (0); 119 } 120 121 static void 122 disc_clone_destroy(struct ifnet *ifp) 123 { 124 struct disc_softc *sc; 125 126 sc = ifp->if_softc; 127 128 bpfdetach(ifp); 129 if_detach(ifp); 130 if_free(ifp); 131 132 free(sc, M_DISC); 133 } 134 135 static void 136 vnet_disc_init(const void *unused __unused) 137 { 138 139 V_disc_cloner = if_clone_simple(discname, disc_clone_create, 140 disc_clone_destroy, 0); 141 } 142 VNET_SYSINIT(vnet_disc_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 143 vnet_disc_init, NULL); 144 145 static void 146 vnet_disc_uninit(const void *unused __unused) 147 { 148 149 if_clone_detach(V_disc_cloner); 150 } 151 VNET_SYSUNINIT(vnet_disc_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 152 vnet_disc_uninit, NULL); 153 154 static int 155 disc_modevent(module_t mod, int type, void *data) 156 { 157 158 switch (type) { 159 case MOD_LOAD: 160 case MOD_UNLOAD: 161 break; 162 default: 163 return (EOPNOTSUPP); 164 } 165 return (0); 166 } 167 168 static moduledata_t disc_mod = { 169 "if_disc", 170 disc_modevent, 171 NULL 172 }; 173 174 DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 175 176 static int 177 discoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 178 struct route *ro) 179 { 180 u_int32_t af; 181 182 M_ASSERTPKTHDR(m); 183 184 /* BPF writes need to be handled specially. */ 185 if (dst->sa_family == AF_UNSPEC) 186 bcopy(dst->sa_data, &af, sizeof(af)); 187 else 188 af = dst->sa_family; 189 190 if (bpf_peers_present(ifp->if_bpf)) 191 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 192 193 m->m_pkthdr.rcvif = ifp; 194 195 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 196 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len); 197 198 m_freem(m); 199 return (0); 200 } 201 202 /* ARGSUSED */ 203 static void 204 discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 205 { 206 207 RT_LOCK_ASSERT(rt); 208 rt->rt_mtu = DSMTU; 209 } 210 211 /* 212 * Process an ioctl request. 213 */ 214 static int 215 discioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 216 { 217 struct ifaddr *ifa; 218 struct ifreq *ifr = (struct ifreq *)data; 219 int error = 0; 220 221 switch (cmd) { 222 case SIOCSIFADDR: 223 ifp->if_flags |= IFF_UP; 224 ifa = (struct ifaddr *)data; 225 if (ifa != 0) 226 ifa->ifa_rtrequest = discrtrequest; 227 /* 228 * Everything else is done at a higher level. 229 */ 230 break; 231 case SIOCADDMULTI: 232 case SIOCDELMULTI: 233 if (ifr == 0) { 234 error = EAFNOSUPPORT; /* XXX */ 235 break; 236 } 237 switch (ifr->ifr_addr.sa_family) { 238 #ifdef INET 239 case AF_INET: 240 break; 241 #endif 242 #ifdef INET6 243 case AF_INET6: 244 break; 245 #endif 246 default: 247 error = EAFNOSUPPORT; 248 break; 249 } 250 break; 251 case SIOCSIFMTU: 252 ifp->if_mtu = ifr->ifr_mtu; 253 break; 254 default: 255 error = EINVAL; 256 } 257 return (error); 258 } 259