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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93 34 * $Id: if_loop.c,v 1.13 1995/09/22 17:57:48 wollman Exp $ 35 */ 36 37 /* 38 * Loopback interface driver for protocol testing and timing. 39 */ 40 #include "loop.h" 41 #if NLOOP > 0 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/mbuf.h> 47 #include <sys/socket.h> 48 #include <sys/errno.h> 49 #include <sys/ioctl.h> 50 #include <sys/time.h> 51 #include <machine/cpu.h> 52 53 #include <net/if.h> 54 #include <net/if_types.h> 55 #include <net/netisr.h> 56 #include <net/route.h> 57 #include <net/bpf.h> 58 59 #ifdef INET 60 #include <netinet/in.h> 61 #include <netinet/in_systm.h> 62 #include <netinet/in_var.h> 63 #include <netinet/ip.h> 64 #endif 65 66 #ifdef IPX 67 #include <netipx/ipx.h> 68 #include <netipx/ipx_if.h> 69 #endif 70 71 #ifdef NS 72 #include <netns/ns.h> 73 #include <netns/ns_if.h> 74 #endif 75 76 #ifdef ISO 77 #include <netiso/iso.h> 78 #include <netiso/iso_var.h> 79 #endif 80 81 #include "bpfilter.h" 82 83 static void loopattach __P((void *)); 84 PSEUDO_SET(loopattach, if_loop); 85 86 #ifdef TINY_LOMTU 87 #define LOMTU (1024+512) 88 #else 89 #define LOMTU 16384 90 #endif 91 92 struct ifnet loif[NLOOP]; 93 94 /* ARGSUSED */ 95 static void 96 loopattach(udata) 97 void *udata; 98 { 99 register struct ifnet *ifp; 100 register int i = 0; 101 102 for (ifp = loif; i < NLOOP; ifp++) { 103 ifp->if_name = "lo"; 104 ifp->if_next = NULL; 105 ifp->if_unit = i++; 106 ifp->if_mtu = LOMTU; 107 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 108 ifp->if_ioctl = loioctl; 109 ifp->if_output = looutput; 110 ifp->if_type = IFT_LOOP; 111 ifp->if_hdrlen = 0; 112 ifp->if_addrlen = 0; 113 if_attach(ifp); 114 #if NBPFILTER > 0 115 bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int)); 116 #endif 117 } 118 } 119 120 int 121 looutput(ifp, m, dst, rt) 122 struct ifnet *ifp; 123 register struct mbuf *m; 124 struct sockaddr *dst; 125 register struct rtentry *rt; 126 { 127 int s, isr; 128 register struct ifqueue *ifq = 0; 129 130 if ((m->m_flags & M_PKTHDR) == 0) 131 panic("looutput no HDR"); 132 ifp->if_lastchange = time; 133 #if NBPFILTER > 0 134 /* BPF write needs to be handled specially */ 135 if (dst->sa_family == AF_UNSPEC) { 136 dst->sa_family = *(mtod(m, int *)); 137 m->m_len -= sizeof(int); 138 m->m_pkthdr.len -= sizeof(int); 139 m->m_data += sizeof(int); 140 } 141 142 if (ifp->if_bpf) { 143 /* 144 * We need to prepend the address family as 145 * a four byte field. Cons up a dummy header 146 * to pacify bpf. This is safe because bpf 147 * will only read from the mbuf (i.e., it won't 148 * try to free it or keep a pointer a to it). 149 */ 150 struct mbuf m0; 151 u_int af = dst->sa_family; 152 153 m0.m_next = m; 154 m0.m_len = 4; 155 m0.m_data = (char *)⁡ 156 157 bpf_mtap(ifp->if_bpf, &m0); 158 } 159 #endif 160 m->m_pkthdr.rcvif = ifp; 161 162 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 163 m_freem(m); 164 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 165 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 166 } 167 ifp->if_opackets++; 168 ifp->if_obytes += m->m_pkthdr.len; 169 switch (dst->sa_family) { 170 171 #ifdef INET 172 case AF_INET: 173 ifq = &ipintrq; 174 isr = NETISR_IP; 175 break; 176 #endif 177 #ifdef IPX 178 case AF_IPX: 179 ifq = &ipxintrq; 180 isr = NETISR_IPX; 181 break; 182 #endif 183 #ifdef NS 184 case AF_NS: 185 ifq = &nsintrq; 186 isr = NETISR_NS; 187 break; 188 #endif 189 #ifdef ISO 190 case AF_ISO: 191 ifq = &clnlintrq; 192 isr = NETISR_ISO; 193 break; 194 #endif 195 default: 196 printf("lo%d: can't handle af%d\n", ifp->if_unit, 197 dst->sa_family); 198 m_freem(m); 199 return (EAFNOSUPPORT); 200 } 201 s = splimp(); 202 if (IF_QFULL(ifq)) { 203 IF_DROP(ifq); 204 m_freem(m); 205 splx(s); 206 return (ENOBUFS); 207 } 208 IF_ENQUEUE(ifq, m); 209 schednetisr(isr); 210 ifp->if_ipackets++; 211 ifp->if_ibytes += m->m_pkthdr.len; 212 splx(s); 213 return (0); 214 } 215 216 /* ARGSUSED */ 217 void 218 lortrequest(cmd, rt, sa) 219 int cmd; 220 struct rtentry *rt; 221 struct sockaddr *sa; 222 { 223 224 if (rt) 225 rt->rt_rmx.rmx_mtu = LOMTU; 226 } 227 228 /* 229 * Process an ioctl request. 230 */ 231 /* ARGSUSED */ 232 int 233 loioctl(ifp, cmd, data) 234 register struct ifnet *ifp; 235 int cmd; 236 caddr_t data; 237 { 238 register struct ifaddr *ifa; 239 register struct ifreq *ifr = (struct ifreq *)data; 240 register int error = 0; 241 242 switch (cmd) { 243 244 case SIOCSIFADDR: 245 ifp->if_flags |= IFF_UP; 246 ifa = (struct ifaddr *)data; 247 if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO) 248 ifa->ifa_rtrequest = lortrequest; 249 /* 250 * Everything else is done at a higher level. 251 */ 252 break; 253 254 case SIOCADDMULTI: 255 case SIOCDELMULTI: 256 if (ifr == 0) { 257 error = EAFNOSUPPORT; /* XXX */ 258 break; 259 } 260 switch (ifr->ifr_addr.sa_family) { 261 262 #ifdef INET 263 case AF_INET: 264 break; 265 #endif 266 267 default: 268 error = EAFNOSUPPORT; 269 break; 270 } 271 break; 272 273 case SIOCSIFMTU: 274 ifp->if_mtu = ifr->ifr_mtu; 275 break; 276 277 default: 278 error = EINVAL; 279 } 280 return (error); 281 } 282 #endif /* NLOOP > 0 */ 283