xref: /freebsd/sys/net/if_disc.c (revision f374ba41)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	From: @(#)if_loop.c	8.1 (Berkeley) 6/10/93
32  * $FreeBSD$
33  */
34 
35 /*
36  * Discard interface driver for protocol testing and timing.
37  * (Based on the loopback.)
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_private.h>
52 #include <net/if_clone.h>
53 #include <net/if_types.h>
54 #include <net/route.h>
55 #include <net/bpf.h>
56 #include <net/vnet.h>
57 
58 #include "opt_inet.h"
59 #include "opt_inet6.h"
60 
61 #ifdef TINY_DSMTU
62 #define	DSMTU	(1024+512)
63 #else
64 #define DSMTU	65532
65 #endif
66 
67 struct disc_softc {
68 	struct ifnet *sc_ifp;
69 };
70 
71 static int	discoutput(struct ifnet *, struct mbuf *,
72 		    const struct sockaddr *, struct route *);
73 static int	discioctl(struct ifnet *, u_long, caddr_t);
74 static int	disc_clone_create(struct if_clone *, int, caddr_t);
75 static void	disc_clone_destroy(struct ifnet *);
76 
77 static const char discname[] = "disc";
78 static MALLOC_DEFINE(M_DISC, discname, "Discard interface");
79 
80 VNET_DEFINE_STATIC(struct if_clone *, disc_cloner);
81 #define	V_disc_cloner	VNET(disc_cloner)
82 
83 static int
84 disc_clone_create(struct if_clone *ifc, int unit, caddr_t params)
85 {
86 	struct ifnet		*ifp;
87 	struct disc_softc	*sc;
88 
89 	sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK | M_ZERO);
90 	ifp = sc->sc_ifp = if_alloc(IFT_LOOP);
91 	if (ifp == NULL) {
92 		free(sc, M_DISC);
93 		return (ENOSPC);
94 	}
95 
96 	ifp->if_softc = sc;
97 	if_initname(ifp, discname, unit);
98 	ifp->if_mtu = DSMTU;
99 	/*
100 	 * IFF_LOOPBACK should not be removed from disc's flags because
101 	 * it controls what PF-specific routes are magically added when
102 	 * a network address is assigned to the interface.  Things just
103 	 * won't work as intended w/o such routes because the output
104 	 * interface selection for a packet is totally route-driven.
105 	 * A valid alternative to IFF_LOOPBACK can be IFF_BROADCAST or
106 	 * IFF_POINTOPOINT, but it would result in different properties
107 	 * of the interface.
108 	 */
109 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
110 	ifp->if_drv_flags = IFF_DRV_RUNNING;
111 	ifp->if_ioctl = discioctl;
112 	ifp->if_output = discoutput;
113 	ifp->if_hdrlen = 0;
114 	ifp->if_addrlen = 0;
115 	ifp->if_snd.ifq_maxlen = 20;
116 	if_attach(ifp);
117 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
118 
119 	return (0);
120 }
121 
122 static void
123 disc_clone_destroy(struct ifnet *ifp)
124 {
125 	struct disc_softc	*sc;
126 
127 	sc = ifp->if_softc;
128 
129 	bpfdetach(ifp);
130 	if_detach(ifp);
131 	if_free(ifp);
132 
133 	free(sc, M_DISC);
134 }
135 
136 static void
137 vnet_disc_init(const void *unused __unused)
138 {
139 
140 	V_disc_cloner = if_clone_simple(discname, disc_clone_create,
141 	    disc_clone_destroy, 0);
142 }
143 VNET_SYSINIT(vnet_disc_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
144     vnet_disc_init, NULL);
145 
146 static void
147 vnet_disc_uninit(const void *unused __unused)
148 {
149 
150 	if_clone_detach(V_disc_cloner);
151 }
152 VNET_SYSUNINIT(vnet_disc_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
153     vnet_disc_uninit, NULL);
154 
155 static int
156 disc_modevent(module_t mod, int type, void *data)
157 {
158 
159 	switch (type) {
160 	case MOD_LOAD:
161 	case MOD_UNLOAD:
162 		break;
163 	default:
164 		return (EOPNOTSUPP);
165 	}
166 	return (0);
167 }
168 
169 static moduledata_t disc_mod = {
170 	"if_disc",
171 	disc_modevent,
172 	NULL
173 };
174 
175 DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
176 
177 static int
178 discoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
179     struct route *ro)
180 {
181 	u_int32_t af;
182 
183 	M_ASSERTPKTHDR(m);
184 
185 	/* BPF writes need to be handled specially. */
186 	if (dst->sa_family == AF_UNSPEC)
187 		bcopy(dst->sa_data, &af, sizeof(af));
188 	else
189 		af = RO_GET_FAMILY(ro, dst);
190 
191 	if (bpf_peers_present(ifp->if_bpf))
192 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
193 
194 	m->m_pkthdr.rcvif = ifp;
195 
196 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
197 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
198 
199 	m_freem(m);
200 	return (0);
201 }
202 
203 /*
204  * Process an ioctl request.
205  */
206 static int
207 discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
208 {
209 	struct ifreq *ifr = (struct ifreq *)data;
210 	int error = 0;
211 
212 	switch (cmd) {
213 	case SIOCSIFADDR:
214 		ifp->if_flags |= IFF_UP;
215 
216 		/*
217 		 * Everything else is done at a higher level.
218 		 */
219 		break;
220 	case SIOCADDMULTI:
221 	case SIOCDELMULTI:
222 		if (ifr == NULL) {
223 			error = EAFNOSUPPORT;		/* XXX */
224 			break;
225 		}
226 		switch (ifr->ifr_addr.sa_family) {
227 #ifdef INET
228 		case AF_INET:
229 			break;
230 #endif
231 #ifdef INET6
232 		case AF_INET6:
233 			break;
234 #endif
235 		default:
236 			error = EAFNOSUPPORT;
237 			break;
238 		}
239 		break;
240 	case SIOCSIFMTU:
241 		ifp->if_mtu = ifr->ifr_mtu;
242 		break;
243 	default:
244 		error = EINVAL;
245 	}
246 	return (error);
247 }
248