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