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.18 1996/02/06 18:51:11 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 52 #include <net/if.h> 53 #include <net/if_types.h> 54 #include <net/netisr.h> 55 #include <net/route.h> 56 #include <net/bpf.h> 57 58 #ifdef INET 59 #include <netinet/in.h> 60 #include <netinet/in_systm.h> 61 #include <netinet/in_var.h> 62 #include <netinet/ip.h> 63 #endif 64 65 #ifdef IPX 66 #include <netipx/ipx.h> 67 #include <netipx/ipx_if.h> 68 #endif 69 70 #ifdef NS 71 #include <netns/ns.h> 72 #include <netns/ns_if.h> 73 #endif 74 75 #ifdef ISO 76 #include <netiso/iso.h> 77 #include <netiso/iso_var.h> 78 #endif 79 80 #include "bpfilter.h" 81 82 static int loioctl __P((struct ifnet *, int, caddr_t)); 83 static void lortrequest __P((int, struct rtentry *, struct sockaddr *)); 84 85 static void loopattach __P((void *)); 86 PSEUDO_SET(loopattach, if_loop); 87 88 #ifdef TINY_LOMTU 89 #define LOMTU (1024+512) 90 #else 91 #define LOMTU 16384 92 #endif 93 94 struct ifnet loif[NLOOP]; 95 96 /* ARGSUSED */ 97 static void 98 loopattach(dummy) 99 void *dummy; 100 { 101 register struct ifnet *ifp; 102 register int i = 0; 103 104 for (ifp = loif; i < NLOOP; ifp++) { 105 ifp->if_name = "lo"; 106 ifp->if_next = NULL; 107 ifp->if_unit = i++; 108 ifp->if_mtu = LOMTU; 109 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 110 ifp->if_ioctl = loioctl; 111 ifp->if_output = looutput; 112 ifp->if_type = IFT_LOOP; 113 ifp->if_hdrlen = 0; 114 ifp->if_addrlen = 0; 115 if_attach(ifp); 116 #if NBPFILTER > 0 117 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 118 #endif 119 } 120 } 121 122 int 123 looutput(ifp, m, dst, rt) 124 struct ifnet *ifp; 125 register struct mbuf *m; 126 struct sockaddr *dst; 127 register struct rtentry *rt; 128 { 129 int s, isr; 130 register struct ifqueue *ifq = 0; 131 132 if ((m->m_flags & M_PKTHDR) == 0) 133 panic("looutput no HDR"); 134 ifp->if_lastchange = time; 135 #if NBPFILTER > 0 136 /* BPF write needs to be handled specially */ 137 if (dst->sa_family == AF_UNSPEC) { 138 dst->sa_family = *(mtod(m, int *)); 139 m->m_len -= sizeof(int); 140 m->m_pkthdr.len -= sizeof(int); 141 m->m_data += sizeof(int); 142 } 143 144 if (ifp->if_bpf) { 145 /* 146 * We need to prepend the address family as 147 * a four byte field. Cons up a dummy header 148 * to pacify bpf. This is safe because bpf 149 * will only read from the mbuf (i.e., it won't 150 * try to free it or keep a pointer a to it). 151 */ 152 struct mbuf m0; 153 u_int af = dst->sa_family; 154 155 m0.m_next = m; 156 m0.m_len = 4; 157 m0.m_data = (char *)⁡ 158 159 bpf_mtap(ifp, &m0); 160 } 161 #endif 162 m->m_pkthdr.rcvif = ifp; 163 164 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 165 m_freem(m); 166 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 167 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 168 } 169 ifp->if_opackets++; 170 ifp->if_obytes += m->m_pkthdr.len; 171 switch (dst->sa_family) { 172 173 #ifdef INET 174 case AF_INET: 175 ifq = &ipintrq; 176 isr = NETISR_IP; 177 break; 178 #endif 179 #ifdef IPX 180 case AF_IPX: 181 ifq = &ipxintrq; 182 isr = NETISR_IPX; 183 break; 184 #endif 185 #ifdef NS 186 case AF_NS: 187 ifq = &nsintrq; 188 isr = NETISR_NS; 189 break; 190 #endif 191 #ifdef ISO 192 case AF_ISO: 193 ifq = &clnlintrq; 194 isr = NETISR_ISO; 195 break; 196 #endif 197 default: 198 printf("lo%d: can't handle af%d\n", ifp->if_unit, 199 dst->sa_family); 200 m_freem(m); 201 return (EAFNOSUPPORT); 202 } 203 s = splimp(); 204 if (IF_QFULL(ifq)) { 205 IF_DROP(ifq); 206 m_freem(m); 207 splx(s); 208 return (ENOBUFS); 209 } 210 IF_ENQUEUE(ifq, m); 211 schednetisr(isr); 212 ifp->if_ipackets++; 213 ifp->if_ibytes += m->m_pkthdr.len; 214 splx(s); 215 return (0); 216 } 217 218 /* ARGSUSED */ 219 static void 220 lortrequest(cmd, rt, sa) 221 int cmd; 222 struct rtentry *rt; 223 struct sockaddr *sa; 224 { 225 if (rt) { 226 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */ 227 /* 228 * For optimal performance, the send and receive buffers 229 * should be at least twice the MTU plus a little more for 230 * overhead. 231 */ 232 rt->rt_rmx.rmx_recvpipe = 233 rt->rt_rmx.rmx_sendpipe = 3 * LOMTU; 234 } 235 } 236 237 /* 238 * Process an ioctl request. 239 */ 240 /* ARGSUSED */ 241 static int 242 loioctl(ifp, cmd, data) 243 register struct ifnet *ifp; 244 int cmd; 245 caddr_t data; 246 { 247 register struct ifaddr *ifa; 248 register struct ifreq *ifr = (struct ifreq *)data; 249 register int error = 0; 250 251 switch (cmd) { 252 253 case SIOCSIFADDR: 254 ifp->if_flags |= IFF_UP; 255 ifa = (struct ifaddr *)data; 256 ifa->ifa_rtrequest = lortrequest; 257 /* 258 * Everything else is done at a higher level. 259 */ 260 break; 261 262 case SIOCADDMULTI: 263 case SIOCDELMULTI: 264 if (ifr == 0) { 265 error = EAFNOSUPPORT; /* XXX */ 266 break; 267 } 268 switch (ifr->ifr_addr.sa_family) { 269 270 #ifdef INET 271 case AF_INET: 272 break; 273 #endif 274 275 default: 276 error = EAFNOSUPPORT; 277 break; 278 } 279 break; 280 281 case SIOCSIFMTU: 282 ifp->if_mtu = ifr->ifr_mtu; 283 break; 284 285 default: 286 error = EINVAL; 287 } 288 return (error); 289 } 290 #endif /* NLOOP > 0 */ 291