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