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