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