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