2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if_loop.c 8.2 (Berkeley) 1/9/95
34 * Loopback interface driver for protocol testing and timing.
37 #include "opt_atalk.h"
39 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <machine/bus.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
55 #include <net/if_clone.h>
56 #include <net/if_types.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
60 #include <net/bpfdesc.h>
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
68 #include <netipx/ipx.h>
69 #include <netipx/ipx_if.h>
74 #include <netinet/in.h>
76 #include <netinet6/in6_var.h>
77 #include <netinet/ip6.h>
81 #include <netatalk/at.h>
82 #include <netatalk/at_var.h>
86 #define LOMTU (1024+512)
87 #elif defined(LARGE_LOMTU)
97 LIST_ENTRY(lo_softc) sc_next;
100 int loioctl(struct ifnet *, u_long, caddr_t);
101 static void lortrequest(int, struct rtentry *, struct rt_addrinfo *);
102 int looutput(struct ifnet *ifp, struct mbuf *m,
103 struct sockaddr *dst, struct rtentry *rt);
104 static int lo_clone_create(struct if_clone *, int, caddr_t);
105 static void lo_clone_destroy(struct ifnet *);
107 struct ifnet *loif = NULL; /* Used externally */
109 static MALLOC_DEFINE(M_LO, LONAME, "Loopback Interface");
111 static struct mtx lo_mtx;
112 static LIST_HEAD(lo_list, lo_softc) lo_list;
114 IFC_SIMPLE_DECLARE(lo, 1);
117 lo_clone_destroy(ifp)
124 /* XXX: destroying lo0 will lead to panics. */
125 KASSERT(loif != ifp, ("%s: destroying lo0", __func__));
128 LIST_REMOVE(sc, sc_next);
137 lo_clone_create(ifc, unit, params)
138 struct if_clone *ifc;
145 MALLOC(sc, struct lo_softc *, sizeof(*sc), M_LO, M_WAITOK | M_ZERO);
146 ifp = sc->sc_ifp = if_alloc(IFT_LOOP);
152 if_initname(ifp, ifc->ifc_name, unit);
154 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
155 ifp->if_ioctl = loioctl;
156 ifp->if_output = looutput;
157 ifp->if_snd.ifq_maxlen = ifqmaxlen;
160 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
162 LIST_INSERT_HEAD(&lo_list, sc, sc_next);
171 loop_modevent(module_t mod, int type, void *data)
175 mtx_init(&lo_mtx, "lo_mtx", NULL, MTX_DEF);
177 if_clone_attach(&lo_cloner);
180 printf("loop module unload - not possible for this module type\n");
188 static moduledata_t loop_mod = {
194 DECLARE_MODULE(loop, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
197 looutput(ifp, m, dst, rt)
199 register struct mbuf *m;
200 struct sockaddr *dst;
201 register struct rtentry *rt;
205 M_ASSERTPKTHDR(m); /* check if we have the packet header */
207 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
209 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
210 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
214 ifp->if_obytes += m->m_pkthdr.len;
216 /* BPF writes need to be handled specially. */
217 if (dst->sa_family == AF_UNSPEC) {
218 bcopy(dst->sa_data, &af, sizeof(af));
223 switch (dst->sa_family) {
230 printf("looutput: af=%d unexpected\n", dst->sa_family);
232 return (EAFNOSUPPORT);
235 return(if_simloop(ifp, m, dst->sa_family, 0));
241 * This function is to support software emulation of hardware loopback,
242 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
243 * hear their own broadcasts, we create a copy of the packet that we
244 * would normally receive via a hardware loopback.
246 * This function expects the packet to include the media header of length hlen.
250 if_simloop(ifp, m, af, hlen)
259 m_tag_delete_nonpersistent(m);
260 m->m_pkthdr.rcvif = ifp;
263 * Let BPF see incoming packet in the following manner:
264 * - Emulated packet loopback for a simplex interface
265 * (net/if_ethersubr.c)
266 * -> passes it to ifp's BPF
267 * - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
268 * -> not passes it to any BPF
269 * - Normal packet loopback from myself to myself (net/if_loop.c)
270 * -> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
273 if (bpf_peers_present(ifp->if_bpf)) {
274 bpf_mtap(ifp->if_bpf, m);
277 if (bpf_peers_present(loif->if_bpf)) {
278 if ((m->m_flags & M_MCAST) == 0 || loif == ifp) {
279 /* XXX beware sizeof(af) != 4 */
283 * We need to prepend the address family.
285 bpf_mtap2(loif->if_bpf, &af1, sizeof(af1), m);
290 /* Strip away media header */
293 #ifndef __NO_STRICT_ALIGNMENT
295 * Some archs do not like unaligned data, so
296 * we move data down in the first mbuf.
298 if (mtod(m, vm_offset_t) & 3) {
299 KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
301 (char *)(mtod(m, vm_offset_t)
302 - (mtod(m, vm_offset_t) & 3)),
304 m->m_data -= (mtod(m,vm_offset_t) & 3);
309 /* Deliver to upper layer protocol */
318 m->m_flags |= M_LOOP;
333 printf("if_simloop: can't handle af=%d\n", af);
335 return (EAFNOSUPPORT);
338 ifp->if_ibytes += m->m_pkthdr.len;
339 netisr_queue(isr, m); /* mbuf is free'd on failure. */
345 lortrequest(cmd, rt, info)
348 struct rt_addrinfo *info;
351 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
355 * Process an ioctl request.
359 loioctl(ifp, cmd, data)
360 register struct ifnet *ifp;
364 register struct ifaddr *ifa;
365 register struct ifreq *ifr = (struct ifreq *)data;
366 register int error = 0;
371 ifp->if_flags |= IFF_UP;
372 ifp->if_drv_flags |= IFF_DRV_RUNNING;
373 ifa = (struct ifaddr *)data;
374 ifa->ifa_rtrequest = lortrequest;
376 * Everything else is done at a higher level.
383 error = EAFNOSUPPORT; /* XXX */
386 switch (ifr->ifr_addr.sa_family) {
398 error = EAFNOSUPPORT;
404 ifp->if_mtu = ifr->ifr_mtu;