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.13 2005/01/26 00:37:39 joerg 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 if (ifp->if_bpf) { 214 if (ifp->if_bpf->bif_dlt == DLT_NULL) { 215 uint32_t bpf_af = (uint32_t)af; 216 bpf_ptap(ifp->if_bpf, m, &bpf_af, 4); 217 } 218 else { 219 bpf_mtap(ifp->if_bpf, m); 220 } 221 } 222 223 /* Strip away media header */ 224 if (hlen > 0) { 225 m_adj(m, hlen); 226 #ifdef __alpha__ 227 /* The alpha doesn't like unaligned data. 228 * We move data down in the first mbuf */ 229 if (mtod(m, vm_offset_t) & 3) { 230 KASSERT(hlen >= 3, ("if_simloop: hlen too small")); 231 bcopy(m->m_data, 232 (char *)(mtod(m, vm_offset_t) 233 - (mtod(m, vm_offset_t) & 3)), 234 m->m_len); 235 mtod(m,vm_offset_t) -= (mtod(m, vm_offset_t) & 3); 236 } 237 #endif 238 } 239 240 /* Deliver to upper layer protocol */ 241 switch (af) { 242 #ifdef INET 243 case AF_INET: 244 isr = NETISR_IP; 245 break; 246 #endif 247 #ifdef INET6 248 case AF_INET6: 249 m->m_flags |= M_LOOP; 250 isr = NETISR_IPV6; 251 break; 252 #endif 253 #ifdef IPX 254 case AF_IPX: 255 isr = NETISR_IPX; 256 break; 257 #endif 258 #ifdef NS 259 case AF_NS: 260 isr = NETISR_NS; 261 break; 262 #endif 263 #ifdef NETATALK 264 case AF_APPLETALK: 265 isr = NETISR_ATALK2; 266 break; 267 #endif 268 default: 269 printf("if_simloop: can't handle af=%d\n", af); 270 m_freem(m); 271 return (EAFNOSUPPORT); 272 } 273 274 ifp->if_ipackets++; 275 ifp->if_ibytes += m->m_pkthdr.len; 276 netisr_queue(isr, m); 277 return (0); 278 } 279 280 /* ARGSUSED */ 281 static void 282 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 283 { 284 if (rt) { 285 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */ 286 /* 287 * For optimal performance, the send and receive buffers 288 * should be at least twice the MTU plus a little more for 289 * overhead. 290 */ 291 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU; 292 } 293 } 294 295 /* 296 * Process an ioctl request. 297 */ 298 /* ARGSUSED */ 299 int 300 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 301 { 302 struct ifaddr *ifa; 303 struct ifreq *ifr = (struct ifreq *)data; 304 int error = 0; 305 306 switch (cmd) { 307 308 case SIOCSIFADDR: 309 ifp->if_flags |= IFF_UP | IFF_RUNNING; 310 ifa = (struct ifaddr *)data; 311 ifa->ifa_rtrequest = lortrequest; 312 /* 313 * Everything else is done at a higher level. 314 */ 315 break; 316 317 case SIOCADDMULTI: 318 case SIOCDELMULTI: 319 if (ifr == 0) { 320 error = EAFNOSUPPORT; /* XXX */ 321 break; 322 } 323 switch (ifr->ifr_addr.sa_family) { 324 325 #ifdef INET 326 case AF_INET: 327 break; 328 #endif 329 #ifdef INET6 330 case AF_INET6: 331 break; 332 #endif 333 334 default: 335 error = EAFNOSUPPORT; 336 break; 337 } 338 break; 339 340 case SIOCSIFMTU: 341 ifp->if_mtu = ifr->ifr_mtu; 342 break; 343 344 case SIOCSIFFLAGS: 345 break; 346 347 default: 348 error = EINVAL; 349 } 350 return (error); 351 } 352