2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * From: @(#)if_loop.c 8.1 (Berkeley) 6/10/93
34 * Discard interface driver for protocol testing and timing.
35 * (Based on the loopback.)
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>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
48 #include <net/if_clone.h>
49 #include <net/if_types.h>
50 #include <net/route.h>
54 #include "opt_inet6.h"
57 #define DSMTU (1024+512)
62 #define DISCNAME "disc"
65 struct ifnet sc_if; /* must be first */
66 LIST_ENTRY(disc_softc) sc_list;
69 static int discoutput(struct ifnet *, struct mbuf *,
70 struct sockaddr *, struct rtentry *);
71 static void discrtrequest(int, struct rtentry *, struct rt_addrinfo *);
72 static int discioctl(struct ifnet *, u_long, caddr_t);
73 static int disc_clone_create(struct if_clone *, int);
74 static void disc_clone_destroy(struct ifnet *);
76 static struct mtx disc_mtx;
77 static MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface");
78 static LIST_HEAD(, disc_softc) disc_softc_list;
80 IFC_SIMPLE_DECLARE(disc, 0);
83 disc_clone_create(struct if_clone *ifc, int unit)
86 struct disc_softc *sc;
88 sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK | M_ZERO);
93 if_initname(ifp, ifc->ifc_name, unit);
95 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
96 ifp->if_ioctl = discioctl;
97 ifp->if_output = discoutput;
98 ifp->if_type = IFT_LOOP;
101 ifp->if_snd.ifq_maxlen = 20;
103 bpfattach(ifp, DLT_NULL, sizeof(u_int));
105 LIST_INSERT_HEAD(&disc_softc_list, sc, sc_list);
106 mtx_unlock(&disc_mtx);
112 disc_destroy(struct disc_softc *sc)
115 bpfdetach(&sc->sc_if);
116 if_detach(&sc->sc_if);
122 disc_clone_destroy(struct ifnet *ifp)
124 struct disc_softc *sc;
128 LIST_REMOVE(sc, sc_list);
129 mtx_unlock(&disc_mtx);
135 disc_modevent(module_t mod, int type, void *data)
137 struct disc_softc *sc;
141 mtx_init(&disc_mtx, "disc_mtx", NULL, MTX_DEF);
142 LIST_INIT(&disc_softc_list);
143 if_clone_attach(&disc_cloner);
146 if_clone_detach(&disc_cloner);
149 while ((sc = LIST_FIRST(&disc_softc_list)) != NULL) {
150 LIST_REMOVE(sc, sc_list);
151 mtx_unlock(&disc_mtx);
155 mtx_unlock(&disc_mtx);
156 mtx_destroy(&disc_mtx);
164 static moduledata_t disc_mod = {
170 DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
173 discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
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);
188 u_int af = dst->sa_family;
189 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
191 m->m_pkthdr.rcvif = ifp;
194 ifp->if_obytes += m->m_pkthdr.len;
202 discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
205 rt->rt_rmx.rmx_mtu = DSMTU;
209 * Process an ioctl request.
212 discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
215 struct ifreq *ifr = (struct ifreq *)data;
221 ifp->if_flags |= IFF_UP;
222 ifa = (struct ifaddr *)data;
224 ifa->ifa_rtrequest = discrtrequest;
226 * Everything else is done at a higher level.
233 error = EAFNOSUPPORT; /* XXX */
236 switch (ifr->ifr_addr.sa_family) {
248 error = EAFNOSUPPORT;
254 ifp->if_mtu = ifr->ifr_mtu;