jcs's openbsd hax
openbsd
1/* $OpenBSD: if_loop.c,v 1.103 2025/09/09 09:16:18 bluhm Exp $ */
2/* $NetBSD: if_loop.c,v 1.15 1996/05/07 02:40:33 thorpej Exp $ */
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1982, 1986, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
62 */
63
64/*
65 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
66 *
67 * NRL grants permission for redistribution and use in source and binary
68 * forms, with or without modification, of the software and documentation
69 * created at NRL provided that the following conditions are met:
70 *
71 * 1. Redistributions of source code must retain the above copyright
72 * notice, this list of conditions and the following disclaimer.
73 * 2. Redistributions in binary form must reproduce the above copyright
74 * notice, this list of conditions and the following disclaimer in the
75 * documentation and/or other materials provided with the distribution.
76 * 3. All advertising materials mentioning features or use of this software
77 * must display the following acknowledgements:
78 * This product includes software developed by the University of
79 * California, Berkeley and its contributors.
80 * This product includes software developed at the Information
81 * Technology Division, US Naval Research Laboratory.
82 * 4. Neither the name of the NRL nor the names of its contributors
83 * may be used to endorse or promote products derived from this software
84 * without specific prior written permission.
85 *
86 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
87 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
88 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
89 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
90 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
91 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
92 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
93 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
94 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
95 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
96 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97 *
98 * The views and conclusions contained in the software and documentation
99 * are those of the authors and should not be interpreted as representing
100 * official policies, either expressed or implied, of the US Naval
101 * Research Laboratory (NRL).
102 */
103
104/*
105 * Loopback interface driver for protocol testing and timing.
106 */
107
108#include "bpfilter.h"
109
110#include <sys/param.h>
111#include <sys/systm.h>
112#include <sys/mbuf.h>
113#include <sys/socket.h>
114#include <sys/errno.h>
115#include <sys/ioctl.h>
116
117#include <net/if.h>
118#include <net/if_var.h>
119#include <net/if_types.h>
120#include <net/rtable.h>
121#include <net/route.h>
122
123#if NBPFILTER > 0
124#include <net/bpf.h>
125#endif
126
127#define LOMTU 32768
128
129int loioctl(struct ifnet *, u_long, caddr_t);
130void loopattach(int);
131void lortrequest(struct ifnet *, int, struct rtentry *);
132void loinput(struct ifnet *, struct mbuf *, struct netstack *);
133int looutput(struct ifnet *,
134 struct mbuf *, struct sockaddr *, struct rtentry *);
135int lo_bpf_mtap(caddr_t, const struct mbuf *, u_int);
136
137int loop_clone_create(struct if_clone *, int);
138int loop_clone_destroy(struct ifnet *);
139
140struct if_clone loop_cloner =
141 IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
142
143void
144loopattach(int n)
145{
146 if (loop_clone_create(&loop_cloner, 0))
147 panic("unable to create lo0");
148
149 if_clone_attach(&loop_cloner);
150}
151
152int
153loop_clone_create(struct if_clone *ifc, int unit)
154{
155 struct ifnet *ifp;
156
157 ifp = malloc(sizeof(*ifp), M_DEVBUF, M_WAITOK|M_ZERO);
158 snprintf(ifp->if_xname, sizeof ifp->if_xname, "lo%d", unit);
159 ifp->if_softc = NULL;
160 ifp->if_mtu = LOMTU;
161 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
162 ifp->if_xflags = IFXF_CLONED | IFXF_LRO;
163 ifp->if_capabilities = IFCAP_CSUM_IPv4 |
164 IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4 |
165 IFCAP_CSUM_TCPv6 | IFCAP_CSUM_UDPv6 |
166 IFCAP_LRO | IFCAP_TSOv4 | IFCAP_TSOv6;
167 ifp->if_bpf_mtap = lo_bpf_mtap;
168 ifp->if_rtrequest = lortrequest;
169 ifp->if_ioctl = loioctl;
170 ifp->if_input = loinput;
171 ifp->if_output = looutput;
172 ifp->if_type = IFT_LOOP;
173 ifp->if_hdrlen = sizeof(u_int32_t);
174 if_counters_alloc(ifp);
175 if (unit == 0) {
176 if_attachhead(ifp);
177 if_addgroup(ifp, ifc->ifc_name);
178 rtable_l2set(0, 0, ifp->if_index);
179 } else
180 if_attach(ifp);
181 if_attach_queues(ifp, softnet_count());
182 if_attach_iqueues(ifp, softnet_count());
183 if_alloc_sadl(ifp);
184#if NBPFILTER > 0
185 bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t));
186#endif
187 return (0);
188}
189
190int
191loop_clone_destroy(struct ifnet *ifp)
192{
193 struct ifnet *p;
194 unsigned int rdomain = 0;
195
196 if (ifp->if_index == rtable_loindex(ifp->if_rdomain)) {
197 /* rdomain 0 always needs a loopback */
198 if (ifp->if_rdomain == 0)
199 return (EPERM);
200
201 /* if there is any other interface in this rdomain, deny */
202 NET_LOCK_SHARED();
203 TAILQ_FOREACH(p, &ifnetlist, if_list) {
204 if (p->if_rdomain != ifp->if_rdomain)
205 continue;
206 if (p->if_index == ifp->if_index)
207 continue;
208 NET_UNLOCK_SHARED();
209 return (EBUSY);
210 }
211 NET_UNLOCK_SHARED();
212
213 rdomain = ifp->if_rdomain;
214 }
215
216 if_detach(ifp);
217
218 free(ifp, M_DEVBUF, sizeof(*ifp));
219
220 if (rdomain)
221 rtable_l2set(rdomain, 0, 0);
222 return (0);
223}
224
225int
226lo_bpf_mtap(caddr_t if_bpf, const struct mbuf *m, u_int dir)
227{
228 /* loopback dumps on output, disable input bpf */
229 return (0);
230}
231
232void
233loinput(struct ifnet *ifp, struct mbuf *m, struct netstack *ns)
234{
235 int error;
236
237 if ((m->m_flags & M_PKTHDR) == 0)
238 panic("%s: no header mbuf", __func__);
239
240 error = if_input_local(ifp, m, m->m_pkthdr.ph_family, ns);
241 if (error)
242 counters_inc(ifp->if_counters, ifc_ierrors);
243}
244
245int
246looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
247 struct rtentry *rt)
248{
249 if ((m->m_flags & M_PKTHDR) == 0)
250 panic("%s: no header mbuf", __func__);
251
252 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
253 m_freem(m);
254 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
255 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
256 }
257
258 /*
259 * Do not call if_input_local() directly. Queue the packet to avoid
260 * stack overflow and make TCP handshake over loopback work.
261 */
262 return (if_output_local(ifp, m, dst->sa_family));
263}
264
265void
266lortrequest(struct ifnet *ifp, int cmd, struct rtentry *rt)
267{
268 if (rt != NULL)
269 atomic_cas_uint(&rt->rt_mtu, 0, LOMTU);
270}
271
272/*
273 * Process an ioctl request.
274 */
275int
276loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
277{
278 struct ifreq *ifr;
279 int error = 0;
280
281 switch (cmd) {
282 case SIOCSIFFLAGS:
283 if (ISSET(ifp->if_xflags, IFXF_LRO))
284 SET(ifp->if_capabilities, IFCAP_TSOv4 | IFCAP_TSOv6);
285 else
286 CLR(ifp->if_capabilities, IFCAP_TSOv4 | IFCAP_TSOv6);
287 break;
288
289 case SIOCSIFADDR:
290 ifp->if_flags |= IFF_RUNNING;
291 if_up(ifp); /* send up RTM_IFINFO */
292 /*
293 * Everything else is done at a higher level.
294 */
295 break;
296
297 case SIOCADDMULTI:
298 case SIOCDELMULTI:
299 break;
300
301 case SIOCSIFMTU:
302 ifr = (struct ifreq *)data;
303 ifp->if_mtu = ifr->ifr_mtu;
304 break;
305
306 default:
307 error = ENOTTY;
308 }
309 return (error);
310}