if_disc.c revision 147611
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 *	From: @(#)if_loop.c	8.1 (Berkeley) 6/10/93
30 * $FreeBSD: head/sys/net/if_disc.c 147611 2005-06-26 18:11:11Z dwmalone $
31 */
32
33/*
34 * Discard interface driver for protocol testing and timing.
35 * (Based on the loopback.)
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/malloc.h>
42#include <sys/module.h>
43#include <sys/mbuf.h>
44#include <sys/socket.h>
45#include <sys/sockio.h>
46
47#include <net/if.h>
48#include <net/if_clone.h>
49#include <net/if_types.h>
50#include <net/route.h>
51#include <net/bpf.h>
52
53#include "opt_inet.h"
54#include "opt_inet6.h"
55
56#ifdef TINY_DSMTU
57#define	DSMTU	(1024+512)
58#else
59#define DSMTU	65532
60#endif
61
62#define DISCNAME	"disc"
63
64struct disc_softc {
65	struct ifnet *sc_ifp;	/* must be first */
66	LIST_ENTRY(disc_softc) sc_list;
67};
68
69static int	discoutput(struct ifnet *, struct mbuf *,
70		    struct sockaddr *, struct rtentry *);
71static void	discrtrequest(int, struct rtentry *, struct rt_addrinfo *);
72static int	discioctl(struct ifnet *, u_long, caddr_t);
73static int	disc_clone_create(struct if_clone *, int);
74static void	disc_clone_destroy(struct ifnet *);
75
76static struct mtx disc_mtx;
77static MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface");
78static LIST_HEAD(, disc_softc) disc_softc_list;
79
80IFC_SIMPLE_DECLARE(disc, 0);
81
82static int
83disc_clone_create(struct if_clone *ifc, int unit)
84{
85	struct ifnet		*ifp;
86	struct disc_softc	*sc;
87
88	sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK | M_ZERO);
89	ifp = sc->sc_ifp = if_alloc(IFT_LOOP);
90	if (ifp == NULL) {
91		free(sc, M_DISC);
92		return (ENOSPC);
93	}
94
95	ifp->if_softc = sc;
96	if_initname(ifp, ifc->ifc_name, unit);
97	ifp->if_mtu = DSMTU;
98	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
99	ifp->if_ioctl = discioctl;
100	ifp->if_output = discoutput;
101	ifp->if_hdrlen = 0;
102	ifp->if_addrlen = 0;
103	ifp->if_snd.ifq_maxlen = 20;
104	if_attach(ifp);
105	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
106	mtx_lock(&disc_mtx);
107	LIST_INSERT_HEAD(&disc_softc_list, sc, sc_list);
108	mtx_unlock(&disc_mtx);
109
110	return (0);
111}
112
113static void
114disc_destroy(struct disc_softc *sc)
115{
116
117	bpfdetach(sc->sc_ifp);
118	if_detach(sc->sc_ifp);
119	if_free(sc->sc_ifp);
120
121	free(sc, M_DISC);
122}
123
124static void
125disc_clone_destroy(struct ifnet *ifp)
126{
127	struct disc_softc	*sc;
128
129	sc = ifp->if_softc;
130	mtx_lock(&disc_mtx);
131	LIST_REMOVE(sc, sc_list);
132	mtx_unlock(&disc_mtx);
133
134	disc_destroy(sc);
135}
136
137static int
138disc_modevent(module_t mod, int type, void *data)
139{
140	struct disc_softc *sc;
141
142	switch (type) {
143	case MOD_LOAD:
144		mtx_init(&disc_mtx, "disc_mtx", NULL, MTX_DEF);
145		LIST_INIT(&disc_softc_list);
146		if_clone_attach(&disc_cloner);
147		break;
148	case MOD_UNLOAD:
149		if_clone_detach(&disc_cloner);
150
151		mtx_lock(&disc_mtx);
152		while ((sc = LIST_FIRST(&disc_softc_list)) != NULL) {
153			LIST_REMOVE(sc, sc_list);
154			mtx_unlock(&disc_mtx);
155			disc_destroy(sc);
156			mtx_lock(&disc_mtx);
157		}
158		mtx_unlock(&disc_mtx);
159		mtx_destroy(&disc_mtx);
160		break;
161	default:
162		return (EOPNOTSUPP);
163	}
164	return (0);
165}
166
167static moduledata_t disc_mod = {
168	"if_disc",
169	disc_modevent,
170	NULL
171};
172
173DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
174
175static int
176discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
177    struct rtentry *rt)
178{
179	u_int32_t af;
180
181	M_ASSERTPKTHDR(m);
182
183	/* BPF writes need to be handled specially. */
184	if (dst->sa_family == AF_UNSPEC) {
185		bcopy(dst->sa_data, &af, sizeof(af));
186		dst->sa_family = af;
187	}
188
189	if (ifp->if_bpf) {
190		u_int af = dst->sa_family;
191		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
192	}
193	m->m_pkthdr.rcvif = ifp;
194
195	ifp->if_opackets++;
196	ifp->if_obytes += m->m_pkthdr.len;
197
198	m_freem(m);
199	return (0);
200}
201
202/* ARGSUSED */
203static void
204discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
205{
206	RT_LOCK_ASSERT(rt);
207	rt->rt_rmx.rmx_mtu = DSMTU;
208}
209
210/*
211 * Process an ioctl request.
212 */
213static int
214discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
215{
216	struct ifaddr *ifa;
217	struct ifreq *ifr = (struct ifreq *)data;
218	int error = 0;
219
220	switch (cmd) {
221
222	case SIOCSIFADDR:
223		ifp->if_flags |= IFF_UP;
224		ifa = (struct ifaddr *)data;
225		if (ifa != 0)
226			ifa->ifa_rtrequest = discrtrequest;
227		/*
228		 * Everything else is done at a higher level.
229		 */
230		break;
231
232	case SIOCADDMULTI:
233	case SIOCDELMULTI:
234		if (ifr == 0) {
235			error = EAFNOSUPPORT;		/* XXX */
236			break;
237		}
238		switch (ifr->ifr_addr.sa_family) {
239
240#ifdef INET
241		case AF_INET:
242			break;
243#endif
244#ifdef INET6
245		case AF_INET6:
246			break;
247#endif
248
249		default:
250			error = EAFNOSUPPORT;
251			break;
252		}
253		break;
254
255	case SIOCSIFMTU:
256		ifp->if_mtu = ifr->ifr_mtu;
257		break;
258
259	default:
260		error = EINVAL;
261	}
262	return (error);
263}
264