xref: /freebsd/sys/net/if_disc.c (revision d2387d42b8da231a5b95cbc313825fb2aadf26f6)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	From: @(#)if_loop.c	8.1 (Berkeley) 6/10/93
34  * $FreeBSD$
35  */
36 
37 /*
38  * Discard interface driver for protocol testing and timing.
39  * (Based on the loopback.)
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 
51 #include <net/if.h>
52 #include <net/if_types.h>
53 #include <net/route.h>
54 #include <net/bpf.h>
55 
56 #include "opt_inet.h"
57 #include "opt_inet6.h"
58 
59 #ifdef TINY_DSMTU
60 #define	DSMTU	(1024+512)
61 #else
62 #define DSMTU	65532
63 #endif
64 
65 #define DISCNAME	"disc"
66 
67 struct disc_softc {
68 	struct ifnet sc_if;	/* must be first */
69 	LIST_ENTRY(disc_softc) sc_list;
70 };
71 
72 static int	discoutput(struct ifnet *, struct mbuf *,
73 		    struct sockaddr *, struct rtentry *);
74 static void	discrtrequest(int, struct rtentry *, struct rt_addrinfo *);
75 static int	discioctl(struct ifnet *, u_long, caddr_t);
76 static int	disc_clone_create(struct if_clone *, int);
77 static void	disc_clone_destroy(struct ifnet *);
78 
79 static struct mtx disc_mtx;
80 static MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface");
81 static LIST_HEAD(, disc_softc) disc_softc_list;
82 static struct if_clone disc_cloner = IF_CLONE_INITIALIZER(DISCNAME,
83     disc_clone_create, disc_clone_destroy, 0, IF_MAXUNIT);
84 
85 static int
86 disc_clone_create(struct if_clone *ifc, int unit)
87 {
88 	struct ifnet		*ifp;
89 	struct disc_softc	*sc;
90 
91 	sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK);
92 	bzero(sc, sizeof(struct disc_softc));
93 
94 	ifp = &sc->sc_if;
95 
96 	ifp->if_softc = sc;
97 	if_initname(ifp, ifc->ifc_name, unit);
98 	ifp->if_mtu = DSMTU;
99 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
100 	ifp->if_ioctl = discioctl;
101 	ifp->if_output = discoutput;
102 	ifp->if_type = IFT_LOOP;
103 	ifp->if_hdrlen = 0;
104 	ifp->if_addrlen = 0;
105 	ifp->if_snd.ifq_maxlen = 20;
106 	if_attach(ifp);
107 	bpfattach(ifp, DLT_NULL, sizeof(u_int));
108 	mtx_lock(&disc_mtx);
109 	LIST_INSERT_HEAD(&disc_softc_list, sc, sc_list);
110 	mtx_unlock(&disc_mtx);
111 
112 	return (0);
113 }
114 
115 static void
116 disc_destroy(struct disc_softc *sc)
117 {
118 
119 	bpfdetach(&sc->sc_if);
120 	if_detach(&sc->sc_if);
121 
122 	free(sc, M_DISC);
123 }
124 
125 static void
126 disc_clone_destroy(struct ifnet *ifp)
127 {
128 	struct disc_softc	*sc;
129 
130 	sc = ifp->if_softc;
131 	mtx_lock(&disc_mtx);
132 	LIST_REMOVE(sc, sc_list);
133 	mtx_unlock(&disc_mtx);
134 
135 	disc_destroy(sc);
136 }
137 
138 static int
139 disc_modevent(module_t mod, int type, void *data)
140 {
141 	struct disc_softc *sc;
142 
143 	switch (type) {
144 	case MOD_LOAD:
145 		mtx_init(&disc_mtx, "disc_mtx", NULL, MTX_DEF);
146 		LIST_INIT(&disc_softc_list);
147 		if_clone_attach(&disc_cloner);
148 		break;
149 	case MOD_UNLOAD:
150 		if_clone_detach(&disc_cloner);
151 
152 		mtx_lock(&disc_mtx);
153 		while ((sc = LIST_FIRST(&disc_softc_list)) != NULL) {
154 			LIST_REMOVE(sc, sc_list);
155 			mtx_unlock(&disc_mtx);
156 			disc_destroy(sc);
157 			mtx_lock(&disc_mtx);
158 		}
159 		mtx_unlock(&disc_mtx);
160 		mtx_destroy(&disc_mtx);
161 		break;
162 	}
163 	return 0;
164 }
165 
166 static moduledata_t disc_mod = {
167 	"if_disc",
168 	disc_modevent,
169 	NULL
170 };
171 
172 DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
173 
174 static int
175 discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
176     struct rtentry *rt)
177 {
178 	M_ASSERTPKTHDR(m);
179 	/* BPF write needs to be handled specially */
180 	if (dst->sa_family == AF_UNSPEC) {
181 		dst->sa_family = *(mtod(m, int *));
182 		m->m_len -= sizeof(int);
183 		m->m_pkthdr.len -= sizeof(int);
184 		m->m_data += sizeof(int);
185 	}
186 
187 	if (ifp->if_bpf) {
188 		u_int af = dst->sa_family;
189 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
190 	}
191 	m->m_pkthdr.rcvif = ifp;
192 
193 	ifp->if_opackets++;
194 	ifp->if_obytes += m->m_pkthdr.len;
195 
196 	m_freem(m);
197 	return 0;
198 }
199 
200 /* ARGSUSED */
201 static void
202 discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
203 {
204 	RT_LOCK_ASSERT(rt);
205 
206 	if (rt)
207 		rt->rt_rmx.rmx_mtu = DSMTU;
208 }
209 
210 /*
211  * Process an ioctl request.
212  */
213 static int
214 discioctl(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