xref: /freebsd/sys/net/if_loop.c (revision 9162f64b58d01ec01481d60b6cdc06ffd8e8c7fc)
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  * 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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  *	@(#)if_loop.c	8.2 (Berkeley) 1/9/95
30  * $FreeBSD$
31  */
32 
33 /*
34  * Loopback interface driver for protocol testing and timing.
35  */
36 
37 #include "opt_atalk.h"
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_ipx.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/mbuf.h>
46 #include <sys/module.h>
47 #include <machine/bus.h>
48 #include <sys/rman.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
52 #include <sys/vimage.h>
53 
54 #include <net/if.h>
55 #include <net/if_clone.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/vnet.h>
61 
62 #ifdef	INET
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #endif
66 
67 #ifdef IPX
68 #include <netipx/ipx.h>
69 #include <netipx/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 NETATALK
81 #include <netatalk/at.h>
82 #include <netatalk/at_var.h>
83 #endif
84 
85 #ifdef TINY_LOMTU
86 #define	LOMTU	(1024+512)
87 #elif defined(LARGE_LOMTU)
88 #define LOMTU	131072
89 #else
90 #define LOMTU	16384
91 #endif
92 
93 int		loioctl(struct ifnet *, u_long, caddr_t);
94 static void	lortrequest(int, struct rtentry *, struct rt_addrinfo *);
95 int		looutput(struct ifnet *ifp, struct mbuf *m,
96 		    struct sockaddr *dst, struct rtentry *rt);
97 static int	lo_clone_create(struct if_clone *, int, caddr_t);
98 static void	lo_clone_destroy(struct ifnet *);
99 
100 #ifdef VIMAGE_GLOBALS
101 struct ifnet *loif;			/* Used externally */
102 #endif
103 
104 IFC_SIMPLE_DECLARE(lo, 1);
105 
106 static void
107 lo_clone_destroy(struct ifnet *ifp)
108 {
109 #ifdef INVARIANTS
110 	INIT_VNET_NET(ifp->if_vnet);
111 #endif
112 
113 	/* XXX: destroying lo0 will lead to panics. */
114 	KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
115 
116 	bpfdetach(ifp);
117 	if_detach(ifp);
118 	if_free(ifp);
119 }
120 
121 static int
122 lo_clone_create(struct if_clone *ifc, int unit, caddr_t params)
123 {
124 	INIT_VNET_NET(curvnet);
125 	struct ifnet *ifp;
126 
127 	ifp = if_alloc(IFT_LOOP);
128 	if (ifp == NULL)
129 		return (ENOSPC);
130 
131 	if_initname(ifp, ifc->ifc_name, unit);
132 	ifp->if_mtu = LOMTU;
133 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
134 	ifp->if_ioctl = loioctl;
135 	ifp->if_output = looutput;
136 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
137 	if_attach(ifp);
138 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
139 	if (V_loif == NULL)
140 		V_loif = ifp;
141 
142 	return (0);
143 }
144 
145 static int
146 loop_modevent(module_t mod, int type, void *data)
147 {
148 	INIT_VNET_NET(curvnet);
149 
150 	switch (type) {
151 	case MOD_LOAD:
152 		V_loif = NULL;
153 		if_clone_attach(&lo_cloner);
154 		break;
155 
156 	case MOD_UNLOAD:
157 		printf("loop module unload - not possible for this module type\n");
158 		return (EINVAL);
159 
160 	default:
161 		return (EOPNOTSUPP);
162 	}
163 	return (0);
164 }
165 
166 static moduledata_t loop_mod = {
167 	"loop",
168 	loop_modevent,
169 	0
170 };
171 
172 DECLARE_MODULE(loop, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
173 
174 int
175 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
176     struct rtentry *rt)
177 {
178 	u_int32_t af;
179 
180 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
181 
182 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
183 		m_freem(m);
184 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
185 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
186 	}
187 
188 	ifp->if_opackets++;
189 	ifp->if_obytes += m->m_pkthdr.len;
190 
191 	/* BPF writes need to be handled specially. */
192 	if (dst->sa_family == AF_UNSPEC) {
193 		bcopy(dst->sa_data, &af, sizeof(af));
194 		dst->sa_family = af;
195 	}
196 
197 #if 1	/* XXX */
198 	switch (dst->sa_family) {
199 	case AF_INET:
200 	case AF_INET6:
201 	case AF_IPX:
202 	case AF_APPLETALK:
203 		break;
204 	default:
205 		printf("looutput: af=%d unexpected\n", dst->sa_family);
206 		m_freem(m);
207 		return (EAFNOSUPPORT);
208 	}
209 #endif
210 	return (if_simloop(ifp, m, dst->sa_family, 0));
211 }
212 
213 /*
214  * if_simloop()
215  *
216  * This function is to support software emulation of hardware loopback,
217  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
218  * hear their own broadcasts, we create a copy of the packet that we
219  * would normally receive via a hardware loopback.
220  *
221  * This function expects the packet to include the media header of length hlen.
222  */
223 int
224 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
225 {
226 	INIT_VNET_NET(ifp->if_vnet);
227 	int isr;
228 
229 	M_ASSERTPKTHDR(m);
230 	m_tag_delete_nonpersistent(m);
231 	m->m_pkthdr.rcvif = ifp;
232 
233 	/*
234 	 * Let BPF see incoming packet in the following manner:
235 	 *  - Emulated packet loopback for a simplex interface
236 	 *    (net/if_ethersubr.c)
237 	 *	-> passes it to ifp's BPF
238 	 *  - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
239 	 *	-> not passes it to any BPF
240 	 *  - Normal packet loopback from myself to myself (net/if_loop.c)
241 	 *	-> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
242 	 */
243 	if (hlen > 0) {
244 		if (bpf_peers_present(ifp->if_bpf)) {
245 			bpf_mtap(ifp->if_bpf, m);
246 		}
247 	} else {
248 		if (bpf_peers_present(V_loif->if_bpf)) {
249 			if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
250 				/* XXX beware sizeof(af) != 4 */
251 				u_int32_t af1 = af;
252 
253 				/*
254 				 * We need to prepend the address family.
255 				 */
256 				bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
257 			}
258 		}
259 	}
260 
261 	/* Strip away media header */
262 	if (hlen > 0) {
263 		m_adj(m, hlen);
264 #ifndef __NO_STRICT_ALIGNMENT
265 		/*
266 		 * Some archs do not like unaligned data, so
267 		 * we move data down in the first mbuf.
268 		 */
269 		if (mtod(m, vm_offset_t) & 3) {
270 			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
271 			bcopy(m->m_data,
272 			    (char *)(mtod(m, vm_offset_t)
273 				- (mtod(m, vm_offset_t) & 3)),
274 			    m->m_len);
275 			m->m_data -= (mtod(m,vm_offset_t) & 3);
276 		}
277 #endif
278 	}
279 
280 	/* Deliver to upper layer protocol */
281 	switch (af) {
282 #ifdef INET
283 	case AF_INET:
284 		isr = NETISR_IP;
285 		break;
286 #endif
287 #ifdef INET6
288 	case AF_INET6:
289 		m->m_flags |= M_LOOP;
290 		isr = NETISR_IPV6;
291 		break;
292 #endif
293 #ifdef IPX
294 	case AF_IPX:
295 		isr = NETISR_IPX;
296 		break;
297 #endif
298 #ifdef NETATALK
299 	case AF_APPLETALK:
300 		isr = NETISR_ATALK2;
301 		break;
302 #endif
303 	default:
304 		printf("if_simloop: can't handle af=%d\n", af);
305 		m_freem(m);
306 		return (EAFNOSUPPORT);
307 	}
308 	ifp->if_ipackets++;
309 	ifp->if_ibytes += m->m_pkthdr.len;
310 	netisr_queue(isr, m);	/* mbuf is free'd on failure. */
311 	return (0);
312 }
313 
314 /* ARGSUSED */
315 static void
316 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
317 {
318 
319 	RT_LOCK_ASSERT(rt);
320 	rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
321 }
322 
323 /*
324  * Process an ioctl request.
325  */
326 /* ARGSUSED */
327 int
328 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
329 {
330 	struct ifaddr *ifa;
331 	struct ifreq *ifr = (struct ifreq *)data;
332 	int error = 0;
333 
334 	switch (cmd) {
335 	case SIOCSIFADDR:
336 		ifp->if_flags |= IFF_UP;
337 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
338 		ifa = (struct ifaddr *)data;
339 		ifa->ifa_rtrequest = lortrequest;
340 		/*
341 		 * Everything else is done at a higher level.
342 		 */
343 		break;
344 
345 	case SIOCADDMULTI:
346 	case SIOCDELMULTI:
347 		if (ifr == 0) {
348 			error = EAFNOSUPPORT;		/* XXX */
349 			break;
350 		}
351 		switch (ifr->ifr_addr.sa_family) {
352 
353 #ifdef INET
354 		case AF_INET:
355 			break;
356 #endif
357 #ifdef INET6
358 		case AF_INET6:
359 			break;
360 #endif
361 
362 		default:
363 			error = EAFNOSUPPORT;
364 			break;
365 		}
366 		break;
367 
368 	case SIOCSIFMTU:
369 		ifp->if_mtu = ifr->ifr_mtu;
370 		break;
371 
372 	case SIOCSIFFLAGS:
373 		break;
374 
375 	default:
376 		error = EINVAL;
377 	}
378 	return (error);
379 }
380