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