if_loop.c revision 241394
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: head/sys/net/if_loop.c 241394 2012-10-10 08:36:38Z kevlo $
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#include <net/vnet.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#include <security/mac/mac_framework.h>
85
86#ifdef TINY_LOMTU
87#define	LOMTU	(1024+512)
88#elif defined(LARGE_LOMTU)
89#define LOMTU	131072
90#else
91#define LOMTU	16384
92#endif
93
94#define	LO_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP)
95#define	LO_CSUM_FEATURES6	(CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_SCTP_IPV6)
96#define	LO_CSUM_SET		(CSUM_DATA_VALID | CSUM_DATA_VALID_IPV6 | \
97				    CSUM_PSEUDO_HDR | \
98				    CSUM_IP_CHECKED | CSUM_IP_VALID | \
99				    CSUM_SCTP_VALID)
100
101int		loioctl(struct ifnet *, u_long, caddr_t);
102static void	lortrequest(int, struct rtentry *, struct rt_addrinfo *);
103int		looutput(struct ifnet *ifp, struct mbuf *m,
104		    struct sockaddr *dst, struct route *ro);
105static int	lo_clone_create(struct if_clone *, int, caddr_t);
106static void	lo_clone_destroy(struct ifnet *);
107
108VNET_DEFINE(struct ifnet *, loif);	/* Used externally */
109
110#ifdef VIMAGE
111static VNET_DEFINE(struct ifc_simple_data, lo_cloner_data);
112static VNET_DEFINE(struct if_clone, lo_cloner);
113#define	V_lo_cloner_data	VNET(lo_cloner_data)
114#define	V_lo_cloner		VNET(lo_cloner)
115#endif
116
117IFC_SIMPLE_DECLARE(lo, 1);
118
119static void
120lo_clone_destroy(struct ifnet *ifp)
121{
122
123#ifndef VIMAGE
124	/* XXX: destroying lo0 will lead to panics. */
125	KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
126#endif
127
128	bpfdetach(ifp);
129	if_detach(ifp);
130	if_free(ifp);
131}
132
133static int
134lo_clone_create(struct if_clone *ifc, int unit, caddr_t params)
135{
136	struct ifnet *ifp;
137
138	ifp = if_alloc(IFT_LOOP);
139	if (ifp == NULL)
140		return (ENOSPC);
141
142	if_initname(ifp, ifc->ifc_name, unit);
143	ifp->if_mtu = LOMTU;
144	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
145	ifp->if_ioctl = loioctl;
146	ifp->if_output = looutput;
147	ifp->if_snd.ifq_maxlen = ifqmaxlen;
148	ifp->if_capabilities = ifp->if_capenable =
149	    IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
150	ifp->if_hwassist = LO_CSUM_FEATURES | LO_CSUM_FEATURES6;
151	if_attach(ifp);
152	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
153	if (V_loif == NULL)
154		V_loif = ifp;
155
156	return (0);
157}
158
159static void
160vnet_loif_init(const void *unused __unused)
161{
162
163#ifdef VIMAGE
164	V_lo_cloner = lo_cloner;
165	V_lo_cloner_data = lo_cloner_data;
166	V_lo_cloner.ifc_data = &V_lo_cloner_data;
167	if_clone_attach(&V_lo_cloner);
168#else
169	if_clone_attach(&lo_cloner);
170#endif
171}
172VNET_SYSINIT(vnet_loif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
173    vnet_loif_init, NULL);
174
175#ifdef VIMAGE
176static void
177vnet_loif_uninit(const void *unused __unused)
178{
179
180	if_clone_detach(&V_lo_cloner);
181	V_loif = NULL;
182}
183VNET_SYSUNINIT(vnet_loif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
184    vnet_loif_uninit, NULL);
185#endif
186
187static int
188loop_modevent(module_t mod, int type, void *data)
189{
190
191	switch (type) {
192	case MOD_LOAD:
193		break;
194
195	case MOD_UNLOAD:
196		printf("loop module unload - not possible for this module type\n");
197		return (EINVAL);
198
199	default:
200		return (EOPNOTSUPP);
201	}
202	return (0);
203}
204
205static moduledata_t loop_mod = {
206	"if_lo",
207	loop_modevent,
208	0
209};
210
211DECLARE_MODULE(if_lo, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
212
213int
214looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
215    struct route *ro)
216{
217	u_int32_t af;
218	struct rtentry *rt = NULL;
219#ifdef MAC
220	int error;
221#endif
222
223	M_ASSERTPKTHDR(m); /* check if we have the packet header */
224
225	if (ro != NULL)
226		rt = ro->ro_rt;
227#ifdef MAC
228	error = mac_ifnet_check_transmit(ifp, m);
229	if (error) {
230		m_freem(m);
231		return (error);
232	}
233#endif
234
235	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
236		m_freem(m);
237		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
238		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
239	}
240
241	ifp->if_opackets++;
242	ifp->if_obytes += m->m_pkthdr.len;
243
244	/* BPF writes need to be handled specially. */
245	if (dst->sa_family == AF_UNSPEC) {
246		bcopy(dst->sa_data, &af, sizeof(af));
247		dst->sa_family = af;
248	}
249
250#if 1	/* XXX */
251	switch (dst->sa_family) {
252	case AF_INET:
253		if (ifp->if_capenable & IFCAP_RXCSUM) {
254			m->m_pkthdr.csum_data = 0xffff;
255			m->m_pkthdr.csum_flags = LO_CSUM_SET;
256		}
257		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES;
258		break;
259	case AF_INET6:
260#if 0
261		/*
262		 * XXX-BZ for now always claim the checksum is good despite
263		 * any interface flags.   This is a workaround for 9.1-R and
264		 * a proper solution ought to be sought later.
265		 */
266		if (ifp->if_capenable & IFCAP_RXCSUM_IPV6) {
267			m->m_pkthdr.csum_data = 0xffff;
268			m->m_pkthdr.csum_flags = LO_CSUM_SET;
269		}
270#else
271		m->m_pkthdr.csum_data = 0xffff;
272		m->m_pkthdr.csum_flags = LO_CSUM_SET;
273#endif
274		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES6;
275		break;
276	case AF_IPX:
277	case AF_APPLETALK:
278		break;
279	default:
280		printf("looutput: af=%d unexpected\n", dst->sa_family);
281		m_freem(m);
282		return (EAFNOSUPPORT);
283	}
284#endif
285	return (if_simloop(ifp, m, dst->sa_family, 0));
286}
287
288/*
289 * if_simloop()
290 *
291 * This function is to support software emulation of hardware loopback,
292 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
293 * hear their own broadcasts, we create a copy of the packet that we
294 * would normally receive via a hardware loopback.
295 *
296 * This function expects the packet to include the media header of length hlen.
297 */
298int
299if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
300{
301	int isr;
302
303	M_ASSERTPKTHDR(m);
304	m_tag_delete_nonpersistent(m);
305	m->m_pkthdr.rcvif = ifp;
306
307#ifdef MAC
308	mac_ifnet_create_mbuf(ifp, m);
309#endif
310
311	/*
312	 * Let BPF see incoming packet in the following manner:
313	 *  - Emulated packet loopback for a simplex interface
314	 *    (net/if_ethersubr.c)
315	 *	-> passes it to ifp's BPF
316	 *  - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
317	 *	-> not passes it to any BPF
318	 *  - Normal packet loopback from myself to myself (net/if_loop.c)
319	 *	-> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
320	 */
321	if (hlen > 0) {
322		if (bpf_peers_present(ifp->if_bpf)) {
323			bpf_mtap(ifp->if_bpf, m);
324		}
325	} else {
326		if (bpf_peers_present(V_loif->if_bpf)) {
327			if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
328				/* XXX beware sizeof(af) != 4 */
329				u_int32_t af1 = af;
330
331				/*
332				 * We need to prepend the address family.
333				 */
334				bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
335			}
336		}
337	}
338
339	/* Strip away media header */
340	if (hlen > 0) {
341		m_adj(m, hlen);
342#ifndef __NO_STRICT_ALIGNMENT
343		/*
344		 * Some archs do not like unaligned data, so
345		 * we move data down in the first mbuf.
346		 */
347		if (mtod(m, vm_offset_t) & 3) {
348			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
349			bcopy(m->m_data,
350			    (char *)(mtod(m, vm_offset_t)
351				- (mtod(m, vm_offset_t) & 3)),
352			    m->m_len);
353			m->m_data -= (mtod(m,vm_offset_t) & 3);
354		}
355#endif
356	}
357
358	/* Deliver to upper layer protocol */
359	switch (af) {
360#ifdef INET
361	case AF_INET:
362		isr = NETISR_IP;
363		break;
364#endif
365#ifdef INET6
366	case AF_INET6:
367		m->m_flags |= M_LOOP;
368		isr = NETISR_IPV6;
369		break;
370#endif
371#ifdef IPX
372	case AF_IPX:
373		isr = NETISR_IPX;
374		break;
375#endif
376#ifdef NETATALK
377	case AF_APPLETALK:
378		isr = NETISR_ATALK2;
379		break;
380#endif
381	default:
382		printf("if_simloop: can't handle af=%d\n", af);
383		m_freem(m);
384		return (EAFNOSUPPORT);
385	}
386	ifp->if_ipackets++;
387	ifp->if_ibytes += m->m_pkthdr.len;
388	netisr_queue(isr, m);	/* mbuf is free'd on failure. */
389	return (0);
390}
391
392/* ARGSUSED */
393static void
394lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
395{
396
397	RT_LOCK_ASSERT(rt);
398	rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
399}
400
401/*
402 * Process an ioctl request.
403 */
404/* ARGSUSED */
405int
406loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
407{
408	struct ifaddr *ifa;
409	struct ifreq *ifr = (struct ifreq *)data;
410	int error = 0, mask;
411
412	switch (cmd) {
413	case SIOCSIFADDR:
414		ifp->if_flags |= IFF_UP;
415		ifp->if_drv_flags |= IFF_DRV_RUNNING;
416		ifa = (struct ifaddr *)data;
417		ifa->ifa_rtrequest = lortrequest;
418		/*
419		 * Everything else is done at a higher level.
420		 */
421		break;
422
423	case SIOCADDMULTI:
424	case SIOCDELMULTI:
425		if (ifr == 0) {
426			error = EAFNOSUPPORT;		/* XXX */
427			break;
428		}
429		switch (ifr->ifr_addr.sa_family) {
430
431#ifdef INET
432		case AF_INET:
433			break;
434#endif
435#ifdef INET6
436		case AF_INET6:
437			break;
438#endif
439
440		default:
441			error = EAFNOSUPPORT;
442			break;
443		}
444		break;
445
446	case SIOCSIFMTU:
447		ifp->if_mtu = ifr->ifr_mtu;
448		break;
449
450	case SIOCSIFFLAGS:
451		break;
452
453	case SIOCSIFCAP:
454		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
455		if ((mask & IFCAP_RXCSUM) != 0)
456			ifp->if_capenable ^= IFCAP_RXCSUM;
457		if ((mask & IFCAP_TXCSUM) != 0)
458			ifp->if_capenable ^= IFCAP_TXCSUM;
459		if ((mask & IFCAP_RXCSUM_IPV6) != 0) {
460#if 0
461			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
462#else
463			error = EOPNOTSUPP;
464			break;
465#endif
466		}
467		if ((mask & IFCAP_TXCSUM_IPV6) != 0) {
468#if 0
469			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
470#else
471			error = EOPNOTSUPP;
472			break;
473#endif
474		}
475		ifp->if_hwassist = 0;
476		if (ifp->if_capenable & IFCAP_TXCSUM)
477			ifp->if_hwassist = LO_CSUM_FEATURES;
478#if 0
479		if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
480			ifp->if_hwassist |= LO_CSUM_FEATURES6;
481#endif
482		break;
483
484	default:
485		error = EINVAL;
486	}
487	return (error);
488}
489