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