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