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