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
32 /*
33 * Loopback interface driver for protocol testing and timing.
34 */
35
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 #include "opt_rss.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/mbuf.h>
44 #include <sys/module.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sysctl.h>
50
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_private.h>
54 #include <net/if_clone.h>
55 #include <net/if_types.h>
56 #include <net/netisr.h>
57 #include <net/route.h>
58 #include <net/bpf.h>
59 #include <net/vnet.h>
60
61 #ifdef INET
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #endif
65
66 #ifdef INET6
67 #ifndef INET
68 #include <netinet/in.h>
69 #endif
70 #include <netinet6/in6_var.h>
71 #include <netinet/ip6.h>
72 #endif
73
74 #include <security/mac/mac_framework.h>
75
76 #ifdef TINY_LOMTU
77 #define LOMTU (1024+512)
78 #elif defined(LARGE_LOMTU)
79 #define LOMTU 131072
80 #else
81 #define LOMTU 16384
82 #endif
83
84 #define LO_CSUM_FEATURES (CSUM_IP | CSUM_IP_TCP | CSUM_IP_UDP | \
85 CSUM_IP_SCTP)
86 #define LO_CSUM_FEATURES6 (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP)
87 #define LO_CSUM_SET (CSUM_L3_CALC | CSUM_L3_VALID | \
88 CSUM_L4_CALC | CSUM_L4_VALID)
89
90 static int loioctl(struct ifnet *, u_long, caddr_t);
91 static int looutput(struct ifnet *ifp, struct mbuf *m,
92 const struct sockaddr *dst, struct route *ro);
93
94 VNET_DEFINE(struct ifnet *, loif); /* Used externally */
95 VNET_DEFINE_STATIC(struct if_clone *, lo_cloner);
96 #define V_lo_cloner VNET(lo_cloner)
97
98 static const char loname[] = "lo";
99
100 static int
lo_clone_destroy(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)101 lo_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
102 {
103 if (ifp->if_dunit == 0 && (flags & IFC_F_FORCE) == 0)
104 return (EINVAL);
105
106 #ifndef VIMAGE
107 /* XXX: destroying lo0 will lead to panics. */
108 KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
109 #endif
110
111 bpfdetach(ifp);
112 if_detach(ifp);
113 if_free(ifp);
114
115 return (0);
116 }
117
118 static int
lo_clone_create(struct if_clone * ifc,char * name,size_t len,struct ifc_data * ifd,struct ifnet ** ifpp)119 lo_clone_create(struct if_clone *ifc, char *name, size_t len,
120 struct ifc_data *ifd, struct ifnet **ifpp)
121 {
122 struct ifnet *ifp;
123
124 ifp = if_alloc(IFT_LOOP);
125 if_initname(ifp, loname, ifd->unit);
126 ifp->if_mtu = LOMTU;
127 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
128 ifp->if_ioctl = loioctl;
129 ifp->if_output = looutput;
130 ifp->if_snd.ifq_maxlen = ifqmaxlen;
131 ifp->if_capabilities = ifp->if_capenable =
132 IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 | IFCAP_LINKSTATE;
133 ifp->if_hwassist = LO_CSUM_FEATURES | LO_CSUM_FEATURES6;
134 if_attach(ifp);
135 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
136 *ifpp = ifp;
137
138 return (0);
139 }
140
141 static void
vnet_loif_init(const void * unused __unused)142 vnet_loif_init(const void *unused __unused)
143 {
144 struct if_clone_addreq req = {
145 .create_f = lo_clone_create,
146 .destroy_f = lo_clone_destroy,
147 .flags = IFC_F_AUTOUNIT,
148 };
149 V_lo_cloner = ifc_attach_cloner(loname, &req);
150 struct ifc_data ifd = { .unit = 0 };
151 ifc_create_ifp(loname, &ifd, &V_loif);
152 }
153 VNET_SYSINIT(vnet_loif_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
154 vnet_loif_init, NULL);
155
156 #ifdef VIMAGE
157 static void
vnet_loif_uninit(const void * unused __unused)158 vnet_loif_uninit(const void *unused __unused)
159 {
160
161 ifc_detach_cloner(V_lo_cloner);
162 V_loif = NULL;
163 }
164 VNET_SYSUNINIT(vnet_loif_uninit, SI_SUB_INIT_IF, SI_ORDER_SECOND,
165 vnet_loif_uninit, NULL);
166 #endif
167
168 static int
loop_modevent(module_t mod,int type,void * data)169 loop_modevent(module_t mod, int type, void *data)
170 {
171
172 switch (type) {
173 case MOD_LOAD:
174 break;
175
176 case MOD_UNLOAD:
177 printf("loop module unload - not possible for this module type\n");
178 return (EINVAL);
179
180 default:
181 return (EOPNOTSUPP);
182 }
183 return (0);
184 }
185
186 static moduledata_t loop_mod = {
187 "if_lo",
188 loop_modevent,
189 0
190 };
191
192 DECLARE_MODULE(if_lo, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
193
194 static int
looutput(struct ifnet * ifp,struct mbuf * m,const struct sockaddr * dst,struct route * ro)195 looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
196 struct route *ro)
197 {
198 u_int32_t af;
199 #ifdef MAC
200 int error;
201 #endif
202
203 M_ASSERTPKTHDR(m); /* check if we have the packet header */
204
205 #ifdef MAC
206 error = mac_ifnet_check_transmit(ifp, m);
207 if (error) {
208 m_freem(m);
209 return (error);
210 }
211 #endif
212
213 if (ro != NULL && ro->ro_flags & (RT_REJECT|RT_BLACKHOLE)) {
214 m_freem(m);
215 return (ro->ro_flags & RT_BLACKHOLE ? 0 : EHOSTUNREACH);
216 }
217
218 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
219 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
220
221 #ifdef RSS
222 M_HASHTYPE_CLEAR(m);
223 #endif
224
225 /* BPF writes need to be handled specially. */
226 if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
227 bcopy(dst->sa_data, &af, sizeof(af));
228 else
229 af = RO_GET_FAMILY(ro, dst);
230
231 switch (af) {
232 case AF_INET:
233 if ((ifp->if_capenable & IFCAP_RXCSUM) == 0) {
234 m->m_pkthdr.csum_flags &= ~LO_CSUM_SET;
235 }
236 break;
237 case AF_INET6:
238 if ((ifp->if_capenable & IFCAP_RXCSUM_IPV6) == 0) {
239 m->m_pkthdr.csum_flags &= ~LO_CSUM_SET;
240 }
241 break;
242 default:
243 printf("looutput: af=%d unexpected\n", af);
244 m_freem(m);
245 return (EAFNOSUPPORT);
246 }
247 return (if_simloop(ifp, m, af, 0));
248 }
249
250 /*
251 * if_simloop()
252 *
253 * This function is to support software emulation of hardware loopback,
254 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
255 * hear their own broadcasts, we create a copy of the packet that we
256 * would normally receive via a hardware loopback.
257 *
258 * This function expects the packet to include the media header of length hlen.
259 */
260 int
if_simloop(struct ifnet * ifp,struct mbuf * m,int af,int hlen)261 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
262 {
263 int isr;
264 int32_t len;
265
266 M_ASSERTPKTHDR(m);
267 m_tag_delete_nonpersistent(m);
268 m->m_pkthdr.rcvif = ifp;
269
270 #ifdef MAC
271 mac_ifnet_create_mbuf(ifp, m);
272 #endif
273
274 /*
275 * Let BPF see incoming packet in the following manner:
276 * - Emulated packet loopback for a simplex interface
277 * (net/if_ethersubr.c)
278 * -> passes it to ifp's BPF
279 * - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
280 * -> not passes it to any BPF
281 * - Normal packet loopback from myself to myself (net/if_loop.c)
282 * -> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
283 */
284 if (hlen > 0) {
285 if (bpf_peers_present(ifp->if_bpf)) {
286 bpf_mtap(ifp->if_bpf, m);
287 }
288 } else {
289 if (bpf_peers_present(V_loif->if_bpf)) {
290 if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
291 /* XXX beware sizeof(af) != 4 */
292 u_int32_t af1 = af;
293
294 /*
295 * We need to prepend the address family.
296 */
297 bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
298 }
299 }
300 }
301
302 /* Strip away media header */
303 if (hlen > 0) {
304 m_adj(m, hlen);
305 #ifndef __NO_STRICT_ALIGNMENT
306 /*
307 * Some archs do not like unaligned data, so
308 * we move data down in the first mbuf.
309 */
310 if (mtod(m, vm_offset_t) & 3) {
311 KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
312 bcopy(m->m_data,
313 (char *)(mtod(m, vm_offset_t)
314 - (mtod(m, vm_offset_t) & 3)),
315 m->m_len);
316 m->m_data -= (mtod(m,vm_offset_t) & 3);
317 }
318 #endif
319 }
320
321 /* Deliver to upper layer protocol */
322 switch (af) {
323 #ifdef INET
324 case AF_INET:
325 isr = NETISR_IP;
326 break;
327 #endif
328 #ifdef INET6
329 case AF_INET6:
330 m->m_flags |= M_LOOP;
331 isr = NETISR_IPV6;
332 break;
333 #endif
334 default:
335 printf("if_simloop: can't handle af=%d\n", af);
336 m_freem(m);
337 return (EAFNOSUPPORT);
338 }
339 len = m->m_pkthdr.len;
340 if (netisr_queue(isr, m) == 0) {
341 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
342 if_inc_counter(ifp, IFCOUNTER_IBYTES, len);
343 } else {
344 /* mbuf is free'd on failure. */
345 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
346 }
347 return (0);
348 }
349
350 /*
351 * Process an ioctl request.
352 */
353 static int
loioctl(struct ifnet * ifp,u_long cmd,caddr_t data)354 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
355 {
356 struct ifreq *ifr = (struct ifreq *)data;
357 int error = 0, mask;
358
359 switch (cmd) {
360 case SIOCSIFADDR:
361 ifp->if_flags |= IFF_UP;
362 ifp->if_drv_flags |= IFF_DRV_RUNNING;
363 if_link_state_change(ifp, LINK_STATE_UP);
364 /*
365 * Everything else is done at a higher level.
366 */
367 break;
368
369 case SIOCADDMULTI:
370 case SIOCDELMULTI:
371 if (ifr == NULL) {
372 error = EAFNOSUPPORT; /* XXX */
373 break;
374 }
375 switch (ifr->ifr_addr.sa_family) {
376 #ifdef INET
377 case AF_INET:
378 break;
379 #endif
380 #ifdef INET6
381 case AF_INET6:
382 break;
383 #endif
384
385 default:
386 error = EAFNOSUPPORT;
387 break;
388 }
389 break;
390
391 case SIOCSIFMTU:
392 ifp->if_mtu = ifr->ifr_mtu;
393 break;
394
395 case SIOCSIFFLAGS:
396 if_link_state_change(ifp, (ifp->if_flags & IFF_UP) ?
397 LINK_STATE_UP: LINK_STATE_DOWN);
398 break;
399
400 case SIOCSIFCAP:
401 mask = ifp->if_capenable ^ ifr->ifr_reqcap;
402 if ((mask & IFCAP_RXCSUM) != 0)
403 ifp->if_capenable ^= IFCAP_RXCSUM;
404 if ((mask & IFCAP_TXCSUM) != 0)
405 ifp->if_capenable ^= IFCAP_TXCSUM;
406 if ((mask & IFCAP_RXCSUM_IPV6) != 0)
407 ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
408 if ((mask & IFCAP_TXCSUM_IPV6) != 0)
409 ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
410 ifp->if_hwassist = 0;
411 if (ifp->if_capenable & IFCAP_TXCSUM)
412 ifp->if_hwassist = LO_CSUM_FEATURES;
413 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
414 ifp->if_hwassist |= LO_CSUM_FEATURES6;
415 break;
416
417 default:
418 error = EINVAL;
419 }
420 return (error);
421 }
422