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_clone.h> 49 #include <net/if_types.h> 50 #include <net/route.h> 51 #include <net/bpf.h> 52 53 #include "opt_inet.h" 54 #include "opt_inet6.h" 55 56 #ifdef TINY_DSMTU 57 #define DSMTU (1024+512) 58 #else 59 #define DSMTU 65532 60 #endif 61 62 struct disc_softc { 63 struct ifnet *sc_ifp; 64 }; 65 66 static int discoutput(struct ifnet *, struct mbuf *, 67 const struct sockaddr *, struct route *); 68 static void discrtrequest(int, struct rtentry *, struct rt_addrinfo *); 69 static int discioctl(struct ifnet *, u_long, caddr_t); 70 static int disc_clone_create(struct if_clone *, int, caddr_t); 71 static void disc_clone_destroy(struct ifnet *); 72 73 static const char discname[] = "disc"; 74 static MALLOC_DEFINE(M_DISC, discname, "Discard interface"); 75 76 static struct if_clone *disc_cloner; 77 78 static int 79 disc_clone_create(struct if_clone *ifc, int unit, caddr_t params) 80 { 81 struct ifnet *ifp; 82 struct disc_softc *sc; 83 84 sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK | M_ZERO); 85 ifp = sc->sc_ifp = if_alloc(IFT_LOOP); 86 if (ifp == NULL) { 87 free(sc, M_DISC); 88 return (ENOSPC); 89 } 90 91 ifp->if_softc = sc; 92 if_initname(ifp, discname, unit); 93 ifp->if_mtu = DSMTU; 94 /* 95 * IFF_LOOPBACK should not be removed from disc's flags because 96 * it controls what PF-specific routes are magically added when 97 * a network address is assigned to the interface. Things just 98 * won't work as intended w/o such routes because the output 99 * interface selection for a packet is totally route-driven. 100 * A valid alternative to IFF_LOOPBACK can be IFF_BROADCAST or 101 * IFF_POINTOPOINT, but it would result in different properties 102 * of the interface. 103 */ 104 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 105 ifp->if_drv_flags = IFF_DRV_RUNNING; 106 ifp->if_ioctl = discioctl; 107 ifp->if_output = discoutput; 108 ifp->if_hdrlen = 0; 109 ifp->if_addrlen = 0; 110 ifp->if_snd.ifq_maxlen = 20; 111 if_attach(ifp); 112 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 113 114 return (0); 115 } 116 117 static void 118 disc_clone_destroy(struct ifnet *ifp) 119 { 120 struct disc_softc *sc; 121 122 sc = ifp->if_softc; 123 124 bpfdetach(ifp); 125 if_detach(ifp); 126 if_free(ifp); 127 128 free(sc, M_DISC); 129 } 130 131 static int 132 disc_modevent(module_t mod, int type, void *data) 133 { 134 135 switch (type) { 136 case MOD_LOAD: 137 disc_cloner = if_clone_simple(discname, disc_clone_create, 138 disc_clone_destroy, 0); 139 break; 140 case MOD_UNLOAD: 141 if_clone_detach(disc_cloner); 142 break; 143 default: 144 return (EOPNOTSUPP); 145 } 146 return (0); 147 } 148 149 static moduledata_t disc_mod = { 150 "if_disc", 151 disc_modevent, 152 NULL 153 }; 154 155 DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 156 157 static int 158 discoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 159 struct route *ro) 160 { 161 u_int32_t af; 162 163 M_ASSERTPKTHDR(m); 164 165 /* BPF writes need to be handled specially. */ 166 if (dst->sa_family == AF_UNSPEC) 167 bcopy(dst->sa_data, &af, sizeof(af)); 168 else 169 af = dst->sa_family; 170 171 if (bpf_peers_present(ifp->if_bpf)) 172 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 173 174 m->m_pkthdr.rcvif = ifp; 175 176 ifp->if_opackets++; 177 ifp->if_obytes += m->m_pkthdr.len; 178 179 m_freem(m); 180 return (0); 181 } 182 183 /* ARGSUSED */ 184 static void 185 discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 186 { 187 RT_LOCK_ASSERT(rt); 188 rt->rt_rmx.rmx_mtu = DSMTU; 189 } 190 191 /* 192 * Process an ioctl request. 193 */ 194 static int 195 discioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 196 { 197 struct ifaddr *ifa; 198 struct ifreq *ifr = (struct ifreq *)data; 199 int error = 0; 200 201 switch (cmd) { 202 203 case SIOCSIFADDR: 204 ifp->if_flags |= IFF_UP; 205 ifa = (struct ifaddr *)data; 206 if (ifa != 0) 207 ifa->ifa_rtrequest = discrtrequest; 208 /* 209 * Everything else is done at a higher level. 210 */ 211 break; 212 213 case SIOCADDMULTI: 214 case SIOCDELMULTI: 215 if (ifr == 0) { 216 error = EAFNOSUPPORT; /* XXX */ 217 break; 218 } 219 switch (ifr->ifr_addr.sa_family) { 220 221 #ifdef INET 222 case AF_INET: 223 break; 224 #endif 225 #ifdef INET6 226 case AF_INET6: 227 break; 228 #endif 229 230 default: 231 error = EAFNOSUPPORT; 232 break; 233 } 234 break; 235 236 case SIOCSIFMTU: 237 ifp->if_mtu = ifr->ifr_mtu; 238 break; 239 240 default: 241 error = EINVAL; 242 } 243 return (error); 244 } 245