xref: /freebsd/sys/net/if_disc.c (revision 6b129086dcee14496517fae085b448e3edc69bc7)
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$
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_var.h>
49 #include <net/if_clone.h>
50 #include <net/if_types.h>
51 #include <net/route.h>
52 #include <net/bpf.h>
53 #include <net/vnet.h>
54 
55 #include "opt_inet.h"
56 #include "opt_inet6.h"
57 
58 #ifdef TINY_DSMTU
59 #define	DSMTU	(1024+512)
60 #else
61 #define DSMTU	65532
62 #endif
63 
64 struct disc_softc {
65 	struct ifnet *sc_ifp;
66 };
67 
68 static int	discoutput(struct ifnet *, struct mbuf *,
69 		    const struct sockaddr *, struct route *);
70 static int	discioctl(struct ifnet *, u_long, caddr_t);
71 static int	disc_clone_create(struct if_clone *, int, caddr_t);
72 static void	disc_clone_destroy(struct ifnet *);
73 
74 static const char discname[] = "disc";
75 static MALLOC_DEFINE(M_DISC, discname, "Discard interface");
76 
77 static VNET_DEFINE(struct if_clone *, disc_cloner);
78 #define	V_disc_cloner	VNET(disc_cloner)
79 
80 static int
81 disc_clone_create(struct if_clone *ifc, int unit, caddr_t params)
82 {
83 	struct ifnet		*ifp;
84 	struct disc_softc	*sc;
85 
86 	sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK | M_ZERO);
87 	ifp = sc->sc_ifp = if_alloc(IFT_LOOP);
88 	if (ifp == NULL) {
89 		free(sc, M_DISC);
90 		return (ENOSPC);
91 	}
92 
93 	ifp->if_softc = sc;
94 	if_initname(ifp, discname, unit);
95 	ifp->if_mtu = DSMTU;
96 	/*
97 	 * IFF_LOOPBACK should not be removed from disc's flags because
98 	 * it controls what PF-specific routes are magically added when
99 	 * a network address is assigned to the interface.  Things just
100 	 * won't work as intended w/o such routes because the output
101 	 * interface selection for a packet is totally route-driven.
102 	 * A valid alternative to IFF_LOOPBACK can be IFF_BROADCAST or
103 	 * IFF_POINTOPOINT, but it would result in different properties
104 	 * of the interface.
105 	 */
106 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
107 	ifp->if_drv_flags = IFF_DRV_RUNNING;
108 	ifp->if_ioctl = discioctl;
109 	ifp->if_output = discoutput;
110 	ifp->if_hdrlen = 0;
111 	ifp->if_addrlen = 0;
112 	ifp->if_snd.ifq_maxlen = 20;
113 	if_attach(ifp);
114 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
115 
116 	return (0);
117 }
118 
119 static void
120 disc_clone_destroy(struct ifnet *ifp)
121 {
122 	struct disc_softc	*sc;
123 
124 	sc = ifp->if_softc;
125 
126 	bpfdetach(ifp);
127 	if_detach(ifp);
128 	if_free(ifp);
129 
130 	free(sc, M_DISC);
131 }
132 
133 static void
134 vnet_disc_init(const void *unused __unused)
135 {
136 
137 	V_disc_cloner = if_clone_simple(discname, disc_clone_create,
138 	    disc_clone_destroy, 0);
139 }
140 VNET_SYSINIT(vnet_disc_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
141     vnet_disc_init, NULL);
142 
143 static void
144 vnet_disc_uninit(const void *unused __unused)
145 {
146 
147 	if_clone_detach(V_disc_cloner);
148 }
149 VNET_SYSUNINIT(vnet_disc_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
150     vnet_disc_uninit, NULL);
151 
152 static int
153 disc_modevent(module_t mod, int type, void *data)
154 {
155 
156 	switch (type) {
157 	case MOD_LOAD:
158 	case MOD_UNLOAD:
159 		break;
160 	default:
161 		return (EOPNOTSUPP);
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, const struct sockaddr *dst,
176     struct route *ro)
177 {
178 	u_int32_t af;
179 
180 	M_ASSERTPKTHDR(m);
181 
182 	/* BPF writes need to be handled specially. */
183 	if (dst->sa_family == AF_UNSPEC)
184 		bcopy(dst->sa_data, &af, sizeof(af));
185 	else
186 		af = dst->sa_family;
187 
188 	if (bpf_peers_present(ifp->if_bpf))
189 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
190 
191 	m->m_pkthdr.rcvif = ifp;
192 
193 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
194 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
195 
196 	m_freem(m);
197 	return (0);
198 }
199 
200 /*
201  * Process an ioctl request.
202  */
203 static int
204 discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
205 {
206 	struct ifreq *ifr = (struct ifreq *)data;
207 	int error = 0;
208 
209 	switch (cmd) {
210 	case SIOCSIFADDR:
211 		ifp->if_flags |= IFF_UP;
212 
213 		/*
214 		 * Everything else is done at a higher level.
215 		 */
216 		break;
217 	case SIOCADDMULTI:
218 	case SIOCDELMULTI:
219 		if (ifr == 0) {
220 			error = EAFNOSUPPORT;		/* XXX */
221 			break;
222 		}
223 		switch (ifr->ifr_addr.sa_family) {
224 #ifdef INET
225 		case AF_INET:
226 			break;
227 #endif
228 #ifdef INET6
229 		case AF_INET6:
230 			break;
231 #endif
232 		default:
233 			error = EAFNOSUPPORT;
234 			break;
235 		}
236 		break;
237 	case SIOCSIFMTU:
238 		ifp->if_mtu = ifr->ifr_mtu;
239 		break;
240 	default:
241 		error = EINVAL;
242 	}
243 	return (error);
244 }
245