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