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 * $FreeBSD$ 35 */ 36 37 /* 38 * Loopback interface driver for protocol testing and timing. 39 */ 40 #include "loop.h" 41 #if NLOOP > 0 42 43 #include "opt_atalk.h" 44 #include "opt_inet.h" 45 #include "opt_inet6.h" 46 #include "opt_ipx.h" 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/mbuf.h> 52 #include <sys/socket.h> 53 #include <sys/sockio.h> 54 55 #include <net/if.h> 56 #include <net/if_types.h> 57 #include <net/netisr.h> 58 #include <net/route.h> 59 #include <net/bpf.h> 60 61 #ifdef INET 62 #include <netinet/in.h> 63 #include <netinet/in_var.h> 64 #endif 65 66 #ifdef IPX 67 #include <netipx/ipx.h> 68 #include <netipx/ipx_if.h> 69 #endif 70 71 #ifdef INET6 72 #ifndef INET 73 #include <netinet/in.h> 74 #endif 75 #include <netinet6/in6_var.h> 76 #include <netinet6/ip6.h> 77 #endif 78 79 #ifdef NS 80 #include <netns/ns.h> 81 #include <netns/ns_if.h> 82 #endif 83 84 #ifdef ISO 85 #include <netiso/iso.h> 86 #include <netiso/iso_var.h> 87 #endif 88 89 #ifdef NETATALK 90 #include <netatalk/at.h> 91 #include <netatalk/at_var.h> 92 #endif NETATALK 93 94 int loioctl __P((struct ifnet *, u_long, caddr_t)); 95 static void lortrequest __P((int, struct rtentry *, struct sockaddr *)); 96 97 static void loopattach __P((void *)); 98 PSEUDO_SET(loopattach, if_loop); 99 100 int looutput __P((struct ifnet *ifp, 101 struct mbuf *m, struct sockaddr *dst, struct rtentry *rt)); 102 103 #ifdef TINY_LOMTU 104 #define LOMTU (1024+512) 105 #elif defined(LARGE_LOMTU) 106 #define LOMTU 131072 107 #else 108 #define LOMTU 16384 109 #endif 110 111 struct ifnet loif[NLOOP]; 112 113 /* ARGSUSED */ 114 static void 115 loopattach(dummy) 116 void *dummy; 117 { 118 register struct ifnet *ifp; 119 register int i = 0; 120 121 for (ifp = loif; i < NLOOP; ifp++) { 122 ifp->if_name = "lo"; 123 ifp->if_unit = i++; 124 ifp->if_mtu = LOMTU; 125 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 126 ifp->if_ioctl = loioctl; 127 ifp->if_output = looutput; 128 ifp->if_type = IFT_LOOP; 129 ifp->if_snd.ifq_maxlen = ifqmaxlen; 130 if_attach(ifp); 131 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 132 } 133 } 134 135 int 136 looutput(ifp, m, dst, rt) 137 struct ifnet *ifp; 138 register struct mbuf *m; 139 struct sockaddr *dst; 140 register struct rtentry *rt; 141 { 142 if ((m->m_flags & M_PKTHDR) == 0) 143 panic("looutput no HDR"); 144 145 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 146 m_freem(m); 147 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 148 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 149 } 150 /* 151 * KAME requires that the packet to be contiguous on the 152 * mbuf. We need to make that sure. 153 * this kind of code should be avoided. 154 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 155 */ 156 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 157 struct mbuf *n; 158 159 MGETHDR(n, M_DONTWAIT, MT_HEADER); 160 if (!n) 161 goto contiguousfail; 162 MCLGET(n, M_DONTWAIT); 163 if (! (n->m_flags & M_EXT)) { 164 m_freem(n); 165 goto contiguousfail; 166 } 167 168 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t)); 169 n->m_pkthdr = m->m_pkthdr; 170 n->m_len = m->m_pkthdr.len; 171 m_freem(m); 172 m = n; 173 } 174 if (0) { 175 contiguousfail: 176 printf("looutput: mbuf allocation failed\n"); 177 } 178 179 ifp->if_opackets++; 180 ifp->if_obytes += m->m_pkthdr.len; 181 #if 1 /* XXX */ 182 switch (dst->sa_family) { 183 case AF_INET: 184 case AF_INET6: 185 case AF_IPX: 186 case AF_NS: 187 case AF_ISO: 188 case AF_APPLETALK: 189 break; 190 default: 191 printf("looutput: af=%d unexpected", dst->sa_family); 192 m_freem(m); 193 return (EAFNOSUPPORT); 194 } 195 #endif 196 return(if_simloop(ifp, m, dst, 0)); 197 } 198 199 /* 200 * if_simloop() 201 * 202 * This function is to support software emulation of hardware loopback, 203 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't 204 * hear their own broadcasts, we create a copy of the packet that we 205 * would normally receive via a hardware loopback. 206 * 207 * This function expects the packet to include the media header of length hlen. 208 */ 209 210 int 211 if_simloop(ifp, m, dst, hlen) 212 struct ifnet *ifp; 213 register struct mbuf *m; 214 struct sockaddr *dst; 215 int hlen; 216 { 217 int s, isr; 218 register struct ifqueue *ifq = 0; 219 220 if ((m->m_flags & M_PKTHDR) == 0) 221 panic("if_simloop: no HDR"); 222 m->m_pkthdr.rcvif = ifp; 223 /* BPF write needs to be handled specially */ 224 if (dst->sa_family == AF_UNSPEC) { 225 dst->sa_family = *(mtod(m, int *)); 226 m->m_len -= sizeof(int); 227 m->m_pkthdr.len -= sizeof(int); 228 m->m_data += sizeof(int); 229 } 230 231 if (ifp->if_bpf) { 232 struct mbuf m0, *n = m; 233 u_int af = dst->sa_family; 234 235 /* 236 * We need to prepend the address family as 237 * a four byte field. Cons up a dummy header 238 * to pacify bpf. This is safe because bpf 239 * will only read from the mbuf (i.e., it won't 240 * try to free it or keep a pointer a to it). 241 */ 242 m0.m_next = m; 243 m0.m_len = 4; 244 m0.m_data = (char *)⁡ 245 n = &m0; 246 bpf_mtap(ifp, n); 247 } 248 249 /* Strip away media header */ 250 if (hlen > 0) { 251 #ifdef __alpha__ 252 /* The alpha doesn't like unaligned data. 253 * We move data down in the first mbuf */ 254 if (hlen & 3) { 255 bcopy(m->m_data + hlen, m->m_data, m->m_len - hlen); 256 m->m_len -= hlen; 257 if (m->m_flags & M_PKTHDR) 258 m->m_pkthdr.len -= hlen; 259 } else 260 #endif 261 m_adj(m, hlen); 262 } 263 264 switch (dst->sa_family) { 265 #ifdef INET 266 case AF_INET: 267 ifq = &ipintrq; 268 isr = NETISR_IP; 269 break; 270 #endif 271 #ifdef INET6 272 case AF_INET6: 273 m->m_flags |= M_LOOP; 274 ifq = &ip6intrq; 275 isr = NETISR_IPV6; 276 break; 277 #endif 278 #ifdef IPX 279 case AF_IPX: 280 ifq = &ipxintrq; 281 isr = NETISR_IPX; 282 break; 283 #endif 284 #ifdef NS 285 case AF_NS: 286 ifq = &nsintrq; 287 isr = NETISR_NS; 288 break; 289 #endif 290 #ifdef ISO 291 case AF_ISO: 292 ifq = &clnlintrq; 293 isr = NETISR_ISO; 294 break; 295 #endif 296 #ifdef NETATALK 297 case AF_APPLETALK: 298 ifq = &atintrq2; 299 isr = NETISR_ATALK; 300 break; 301 #endif NETATALK 302 default: 303 printf("if_simloop: can't handle af=%d\n", dst->sa_family); 304 m_freem(m); 305 return (EAFNOSUPPORT); 306 } 307 s = splimp(); 308 if (IF_QFULL(ifq)) { 309 IF_DROP(ifq); 310 m_freem(m); 311 splx(s); 312 return (ENOBUFS); 313 } 314 IF_ENQUEUE(ifq, m); 315 schednetisr(isr); 316 ifp->if_ipackets++; 317 ifp->if_ibytes += m->m_pkthdr.len; 318 splx(s); 319 return (0); 320 } 321 322 /* ARGSUSED */ 323 static void 324 lortrequest(cmd, rt, sa) 325 int cmd; 326 struct rtentry *rt; 327 struct sockaddr *sa; 328 { 329 if (rt) { 330 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */ 331 /* 332 * For optimal performance, the send and receive buffers 333 * should be at least twice the MTU plus a little more for 334 * overhead. 335 */ 336 rt->rt_rmx.rmx_recvpipe = 337 rt->rt_rmx.rmx_sendpipe = 3 * LOMTU; 338 } 339 } 340 341 /* 342 * Process an ioctl request. 343 */ 344 /* ARGSUSED */ 345 int 346 loioctl(ifp, cmd, data) 347 register struct ifnet *ifp; 348 u_long cmd; 349 caddr_t data; 350 { 351 register struct ifaddr *ifa; 352 register struct ifreq *ifr = (struct ifreq *)data; 353 register int error = 0; 354 355 switch (cmd) { 356 357 case SIOCSIFADDR: 358 ifp->if_flags |= IFF_UP | IFF_RUNNING; 359 ifa = (struct ifaddr *)data; 360 ifa->ifa_rtrequest = lortrequest; 361 /* 362 * Everything else is done at a higher level. 363 */ 364 break; 365 366 case SIOCADDMULTI: 367 case SIOCDELMULTI: 368 if (ifr == 0) { 369 error = EAFNOSUPPORT; /* XXX */ 370 break; 371 } 372 switch (ifr->ifr_addr.sa_family) { 373 374 #ifdef INET 375 case AF_INET: 376 break; 377 #endif 378 #ifdef INET6 379 case AF_INET6: 380 break; 381 #endif 382 383 default: 384 error = EAFNOSUPPORT; 385 break; 386 } 387 break; 388 389 case SIOCSIFMTU: 390 ifp->if_mtu = ifr->ifr_mtu; 391 break; 392 393 case SIOCSIFFLAGS: 394 break; 395 396 default: 397 error = EINVAL; 398 } 399 return (error); 400 } 401 #endif /* NLOOP > 0 */ 402