if_loop.c revision 181118
1139825Simp/*-
2125184Sgrehan * Copyright (c) 1982, 1986, 1993
3125184Sgrehan *	The Regents of the University of California.  All rights reserved.
4125184Sgrehan *
5125184Sgrehan * Redistribution and use in source and binary forms, with or without
6125184Sgrehan * modification, are permitted provided that the following conditions
7125184Sgrehan * are met:
8125184Sgrehan * 1. Redistributions of source code must retain the above copyright
9125184Sgrehan *    notice, this list of conditions and the following disclaimer.
10125184Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
11125184Sgrehan *    notice, this list of conditions and the following disclaimer in the
12125184Sgrehan *    documentation and/or other materials provided with the distribution.
13125184Sgrehan * 4. Neither the name of the University nor the names of its contributors
14125184Sgrehan *    may be used to endorse or promote products derived from this software
15125184Sgrehan *    without specific prior written permission.
16125184Sgrehan *
17125184Sgrehan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18125184Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19125184Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20125184Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21125184Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22125184Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23125184Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24125184Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25125184Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26125184Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27125184Sgrehan * SUCH DAMAGE.
28125184Sgrehan *
29125184Sgrehan *	@(#)if_loop.c	8.2 (Berkeley) 1/9/95
30125184Sgrehan * $FreeBSD: head/sys/net/if_loop.c 181118 2008-08-01 09:41:45Z rwatson $
31125682Sgrehan */
32125184Sgrehan
33125184Sgrehan/*
34125184Sgrehan * Loopback interface driver for protocol testing and timing.
35125682Sgrehan */
36125184Sgrehan
37125184Sgrehan#include "opt_atalk.h"
38190681Snwhitehorn#include "opt_inet.h"
39125184Sgrehan#include "opt_inet6.h"
40190681Snwhitehorn#include "opt_ipx.h"
41125184Sgrehan
42190681Snwhitehorn#include <sys/param.h>
43125184Sgrehan#include <sys/systm.h>
44190681Snwhitehorn#include <sys/kernel.h>
45125184Sgrehan#include <sys/mbuf.h>
46125184Sgrehan#include <sys/module.h>
47125682Sgrehan#include <machine/bus.h>
48125682Sgrehan#include <sys/rman.h>
49125682Sgrehan#include <sys/socket.h>
50125682Sgrehan#include <sys/sockio.h>
51125184Sgrehan#include <sys/sysctl.h>
52125184Sgrehan
53125184Sgrehan#include <net/if.h>
54125184Sgrehan#include <net/if_clone.h>
55125184Sgrehan#include <net/if_types.h>
56125184Sgrehan#include <net/netisr.h>
57125184Sgrehan#include <net/route.h>
58190681Snwhitehorn#include <net/bpf.h>
59125184Sgrehan
60125184Sgrehan#ifdef	INET
61172189Salc#include <netinet/in.h>
62125184Sgrehan#include <netinet/in_var.h>
63172189Salc#endif
64125184Sgrehan
65125184Sgrehan#ifdef IPX
66125184Sgrehan#include <netipx/ipx.h>
67125184Sgrehan#include <netipx/ipx_if.h>
68125184Sgrehan#endif
69125184Sgrehan
70125184Sgrehan#ifdef INET6
71125184Sgrehan#ifndef INET
72125184Sgrehan#include <netinet/in.h>
73125184Sgrehan#endif
74125184Sgrehan#include <netinet6/in6_var.h>
75125184Sgrehan#include <netinet/ip6.h>
76125184Sgrehan#endif
77125184Sgrehan
78204128Snwhitehorn#ifdef NETATALK
79204128Snwhitehorn#include <netatalk/at.h>
80204128Snwhitehorn#include <netatalk/at_var.h>
81204128Snwhitehorn#endif
82125184Sgrehan
83125184Sgrehan#ifdef TINY_LOMTU
84125682Sgrehan#define	LOMTU	(1024+512)
85125682Sgrehan#elif defined(LARGE_LOMTU)
86125184Sgrehan#define LOMTU	131072
87125184Sgrehan#else
88125184Sgrehan#define LOMTU	16384
89125184Sgrehan#endif
90125184Sgrehan
91125184Sgrehanint		loioctl(struct ifnet *, u_long, caddr_t);
92125184Sgrehanstatic void	lortrequest(int, struct rtentry *, struct rt_addrinfo *);
93125184Sgrehanint		looutput(struct ifnet *ifp, struct mbuf *m,
94204128Snwhitehorn		    struct sockaddr *dst, struct rtentry *rt);
95204128Snwhitehornstatic int	lo_clone_create(struct if_clone *, int, caddr_t);
96204128Snwhitehornstatic void	lo_clone_destroy(struct ifnet *);
97190681Snwhitehorn
98204128Snwhitehornstruct ifnet *loif = NULL;			/* Used externally */
99172189Salc
100125184SgrehanIFC_SIMPLE_DECLARE(lo, 1);
101172189Salc
102125682Sgrehanstatic void
103125184Sgrehanlo_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
114static int
115lo_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
137static int
138loop_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
156static moduledata_t loop_mod = {
157	"loop",
158	loop_modevent,
159	0
160};
161
162DECLARE_MODULE(loop, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
163
164int
165looutput(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 */
213int
214if_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 */
304static void
305lortrequest(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 */
316int
317loioctl(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