xref: /openbsd/sys/net/if_loop.c (revision 84d9c64a)
1*84d9c64aSbluhm /*	$OpenBSD: if_loop.c,v 1.99 2025/01/03 21:27:40 bluhm Exp $	*/
209d901c0Sderaadt /*	$NetBSD: if_loop.c,v 1.15 1996/05/07 02:40:33 thorpej Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*
5287546eaSitojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6287546eaSitojun  * All rights reserved.
7287546eaSitojun  *
8287546eaSitojun  * Redistribution and use in source and binary forms, with or without
9287546eaSitojun  * modification, are permitted provided that the following conditions
10287546eaSitojun  * are met:
11287546eaSitojun  * 1. Redistributions of source code must retain the above copyright
12287546eaSitojun  *    notice, this list of conditions and the following disclaimer.
13287546eaSitojun  * 2. Redistributions in binary form must reproduce the above copyright
14287546eaSitojun  *    notice, this list of conditions and the following disclaimer in the
15287546eaSitojun  *    documentation and/or other materials provided with the distribution.
16287546eaSitojun  * 3. Neither the name of the project nor the names of its contributors
17287546eaSitojun  *    may be used to endorse or promote products derived from this software
18287546eaSitojun  *    without specific prior written permission.
19287546eaSitojun  *
20287546eaSitojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21287546eaSitojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22287546eaSitojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23287546eaSitojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24287546eaSitojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25287546eaSitojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26287546eaSitojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27287546eaSitojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28287546eaSitojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29287546eaSitojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30287546eaSitojun  * SUCH DAMAGE.
31287546eaSitojun  */
32287546eaSitojun 
33287546eaSitojun /*
34df930be7Sderaadt  * Copyright (c) 1982, 1986, 1993
35df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
36df930be7Sderaadt  *
37df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
38df930be7Sderaadt  * modification, are permitted provided that the following conditions
39df930be7Sderaadt  * are met:
40df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
41df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
42df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
43df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
44df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
4529295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
46df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
47df930be7Sderaadt  *    without specific prior written permission.
48df930be7Sderaadt  *
49df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59df930be7Sderaadt  * SUCH DAMAGE.
60df930be7Sderaadt  *
61df930be7Sderaadt  *	@(#)if_loop.c	8.1 (Berkeley) 6/10/93
62df930be7Sderaadt  */
63df930be7Sderaadt 
64df930be7Sderaadt /*
65d3931dd6Sderaadt  *	@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
66d3931dd6Sderaadt  *
67d3931dd6Sderaadt  * NRL grants permission for redistribution and use in source and binary
68d3931dd6Sderaadt  * forms, with or without modification, of the software and documentation
69d3931dd6Sderaadt  * created at NRL provided that the following conditions are met:
70d3931dd6Sderaadt  *
71d3931dd6Sderaadt  * 1. Redistributions of source code must retain the above copyright
72d3931dd6Sderaadt  *    notice, this list of conditions and the following disclaimer.
73d3931dd6Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
74d3931dd6Sderaadt  *    notice, this list of conditions and the following disclaimer in the
75d3931dd6Sderaadt  *    documentation and/or other materials provided with the distribution.
76d3931dd6Sderaadt  * 3. All advertising materials mentioning features or use of this software
77d3931dd6Sderaadt  *    must display the following acknowledgements:
78d3931dd6Sderaadt  *	This product includes software developed by the University of
79d3931dd6Sderaadt  *	California, Berkeley and its contributors.
80d3931dd6Sderaadt  *	This product includes software developed at the Information
81d3931dd6Sderaadt  *	Technology Division, US Naval Research Laboratory.
82d3931dd6Sderaadt  * 4. Neither the name of the NRL nor the names of its contributors
83d3931dd6Sderaadt  *    may be used to endorse or promote products derived from this software
84d3931dd6Sderaadt  *    without specific prior written permission.
85d3931dd6Sderaadt  *
86d3931dd6Sderaadt  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
87d3931dd6Sderaadt  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
88d3931dd6Sderaadt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
89d3931dd6Sderaadt  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
90d3931dd6Sderaadt  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
91d3931dd6Sderaadt  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
92d3931dd6Sderaadt  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
93d3931dd6Sderaadt  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
94d3931dd6Sderaadt  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
95d3931dd6Sderaadt  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
96d3931dd6Sderaadt  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97d3931dd6Sderaadt  *
98d3931dd6Sderaadt  * The views and conclusions contained in the software and documentation
99d3931dd6Sderaadt  * are those of the authors and should not be interpreted as representing
100d3931dd6Sderaadt  * official policies, either expressed or implied, of the US Naval
101d3931dd6Sderaadt  * Research Laboratory (NRL).
10285d81336Sderaadt  */
10385d81336Sderaadt 
10485d81336Sderaadt /*
105df930be7Sderaadt  * Loopback interface driver for protocol testing and timing.
106df930be7Sderaadt  */
107df930be7Sderaadt 
108df930be7Sderaadt #include "bpfilter.h"
109df930be7Sderaadt 
110df930be7Sderaadt #include <sys/param.h>
111df930be7Sderaadt #include <sys/systm.h>
112df930be7Sderaadt #include <sys/kernel.h>
113df930be7Sderaadt #include <sys/mbuf.h>
114df930be7Sderaadt #include <sys/socket.h>
115df930be7Sderaadt #include <sys/errno.h>
116df930be7Sderaadt #include <sys/ioctl.h>
117df930be7Sderaadt #include <sys/time.h>
118df930be7Sderaadt 
119df930be7Sderaadt 
120df930be7Sderaadt #include <net/if.h>
1210deb6685Smpi #include <net/if_var.h>
122df930be7Sderaadt #include <net/if_types.h>
123df930be7Sderaadt #include <net/netisr.h>
1243a63dfc0Smpi #include <net/rtable.h>
125df930be7Sderaadt #include <net/route.h>
126df930be7Sderaadt 
127df930be7Sderaadt #include <netinet/in.h>
128df930be7Sderaadt 
129287546eaSitojun #ifdef INET6
130fa86ee14Sitojun #include <netinet/ip6.h>
131287546eaSitojun #endif
132287546eaSitojun 
133d46f459dSnorby #ifdef MPLS
134d46f459dSnorby #include <netmpls/mpls.h>
135d46f459dSnorby #endif
136d46f459dSnorby 
137df930be7Sderaadt #if NBPFILTER > 0
138df930be7Sderaadt #include <net/bpf.h>
139df930be7Sderaadt #endif
140df930be7Sderaadt 
141147aab6dSclaudio #define	LOMTU	32768
142df930be7Sderaadt 
143d5ee4d17Smpi int	loioctl(struct ifnet *, u_long, caddr_t);
144d5ee4d17Smpi void	loopattach(int);
145d5ee4d17Smpi void	lortrequest(struct ifnet *, int, struct rtentry *);
14623293512Sdlg void	loinput(struct ifnet *, struct mbuf *);
147d5ee4d17Smpi int	looutput(struct ifnet *,
148d5ee4d17Smpi 	    struct mbuf *, struct sockaddr *, struct rtentry *);
149c4dc8e60Sbluhm int	lo_bpf_mtap(caddr_t, const struct mbuf *, u_int);
150d5ee4d17Smpi 
151fb1d047fSmarkus int	loop_clone_create(struct if_clone *, int);
152b9e739dfSmarkus int	loop_clone_destroy(struct ifnet *);
153fb1d047fSmarkus 
154fb1d047fSmarkus struct if_clone loop_cloner =
155fb1d047fSmarkus     IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
156fb1d047fSmarkus 
157df930be7Sderaadt void
loopattach(int n)15831eea693Sclaudio loopattach(int n)
159df930be7Sderaadt {
160daf2ccbcSmpi 	if (loop_clone_create(&loop_cloner, 0))
161daf2ccbcSmpi 		panic("unable to create lo0");
162daf2ccbcSmpi 
163fb1d047fSmarkus 	if_clone_attach(&loop_cloner);
164fb1d047fSmarkus }
165df930be7Sderaadt 
166fb1d047fSmarkus int
loop_clone_create(struct if_clone * ifc,int unit)16731eea693Sclaudio loop_clone_create(struct if_clone *ifc, int unit)
168fb1d047fSmarkus {
169fb1d047fSmarkus 	struct ifnet *ifp;
170fb1d047fSmarkus 
171809d3a3eSbluhm 	ifp = malloc(sizeof(*ifp), M_DEVBUF, M_WAITOK|M_ZERO);
172fb1d047fSmarkus 	snprintf(ifp->if_xname, sizeof ifp->if_xname, "lo%d", unit);
17309d901c0Sderaadt 	ifp->if_softc = NULL;
174df930be7Sderaadt 	ifp->if_mtu = LOMTU;
175df930be7Sderaadt 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
1763e1926f8Sbluhm 	ifp->if_xflags = IFXF_CLONED | IFXF_LRO;
177c778c0d7Sbluhm 	ifp->if_capabilities = IFCAP_CSUM_IPv4 |
178c778c0d7Sbluhm 	    IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4 |
179a3c0391fSbluhm 	    IFCAP_CSUM_TCPv6 | IFCAP_CSUM_UDPv6 |
1803e1926f8Sbluhm 	    IFCAP_LRO | IFCAP_TSOv4 | IFCAP_TSOv6;
181c4dc8e60Sbluhm 	ifp->if_bpf_mtap = lo_bpf_mtap;
182dcb17c31Smpi 	ifp->if_rtrequest = lortrequest;
183df930be7Sderaadt 	ifp->if_ioctl = loioctl;
18423293512Sdlg 	ifp->if_input = loinput;
185df930be7Sderaadt 	ifp->if_output = looutput;
186df930be7Sderaadt 	ifp->if_type = IFT_LOOP;
187643a56f3Sderaadt 	ifp->if_hdrlen = sizeof(u_int32_t);
188f7f9047fSbluhm 	if_counters_alloc(ifp);
189fb1d047fSmarkus 	if (unit == 0) {
190138560ceSderaadt 		if_attachhead(ifp);
1915e9a6f6cSmpi 		if_addgroup(ifp, ifc->ifc_name);
1923a63dfc0Smpi 		rtable_l2set(0, 0, ifp->if_index);
193fb1d047fSmarkus 	} else
194fb1d047fSmarkus 		if_attach(ifp);
195d6cd50e9Sitojun 	if_alloc_sadl(ifp);
196df930be7Sderaadt #if NBPFILTER > 0
197643a56f3Sderaadt 	bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t));
198df930be7Sderaadt #endif
199fb1d047fSmarkus 	return (0);
200df930be7Sderaadt }
201fb1d047fSmarkus 
202b9e739dfSmarkus int
loop_clone_destroy(struct ifnet * ifp)20331eea693Sclaudio loop_clone_destroy(struct ifnet *ifp)
204fb1d047fSmarkus {
205f7ebbf19Shenning 	struct ifnet	*p;
2065a1e6e9cSclaudio 	unsigned int	 rdomain = 0;
207f7ebbf19Shenning 
208f7ebbf19Shenning 	if (ifp->if_index == rtable_loindex(ifp->if_rdomain)) {
209f7ebbf19Shenning 		/* rdomain 0 always needs a loopback */
210f7ebbf19Shenning 		if (ifp->if_rdomain == 0)
211b9e739dfSmarkus 			return (EPERM);
212fb1d047fSmarkus 
213f7ebbf19Shenning 		/* if there is any other interface in this rdomain, deny */
2147e119dafSkn 		NET_LOCK_SHARED();
215ccb45f8eSkn 		TAILQ_FOREACH(p, &ifnetlist, if_list) {
216f7ebbf19Shenning 			if (p->if_rdomain != ifp->if_rdomain)
217f7ebbf19Shenning 				continue;
218f7ebbf19Shenning 			if (p->if_index == ifp->if_index)
219f7ebbf19Shenning 				continue;
2207e119dafSkn 			NET_UNLOCK_SHARED();
221f7ebbf19Shenning 			return (EBUSY);
222f7ebbf19Shenning 		}
2237e119dafSkn 		NET_UNLOCK_SHARED();
224f7ebbf19Shenning 
2255a1e6e9cSclaudio 		rdomain = ifp->if_rdomain;
226f7ebbf19Shenning 	}
227f7ebbf19Shenning 
228fb1d047fSmarkus 	if_detach(ifp);
229fb1d047fSmarkus 
230ac3ba07eSderaadt 	free(ifp, M_DEVBUF, sizeof(*ifp));
2315a1e6e9cSclaudio 
2325a1e6e9cSclaudio 	if (rdomain)
2335a1e6e9cSclaudio 		rtable_l2set(rdomain, 0, 0);
234b9e739dfSmarkus 	return (0);
235df930be7Sderaadt }
236df930be7Sderaadt 
237c4dc8e60Sbluhm int
lo_bpf_mtap(caddr_t if_bpf,const struct mbuf * m,u_int dir)238c4dc8e60Sbluhm lo_bpf_mtap(caddr_t if_bpf, const struct mbuf *m, u_int dir)
239c4dc8e60Sbluhm {
240c4dc8e60Sbluhm 	/* loopback dumps on output, disable input bpf */
241c4dc8e60Sbluhm 	return (0);
242c4dc8e60Sbluhm }
243c4dc8e60Sbluhm 
24423293512Sdlg void
loinput(struct ifnet * ifp,struct mbuf * m)24523293512Sdlg loinput(struct ifnet *ifp, struct mbuf *m)
246c0a69b8aSbluhm {
247c0a69b8aSbluhm 	int error;
248c0a69b8aSbluhm 
249c0a69b8aSbluhm 	if ((m->m_flags & M_PKTHDR) == 0)
250c0a69b8aSbluhm 		panic("%s: no header mbuf", __func__);
251c0a69b8aSbluhm 
252c0a69b8aSbluhm 	error = if_input_local(ifp, m, m->m_pkthdr.ph_family);
253c0a69b8aSbluhm 	if (error)
254f7f9047fSbluhm 		counters_inc(ifp->if_counters, ifc_ierrors);
255c0a69b8aSbluhm }
256c0a69b8aSbluhm 
257c0a69b8aSbluhm int
looutput(struct ifnet * ifp,struct mbuf * m,struct sockaddr * dst,struct rtentry * rt)25831eea693Sclaudio looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
25931eea693Sclaudio     struct rtentry *rt)
260df930be7Sderaadt {
261df930be7Sderaadt 	if ((m->m_flags & M_PKTHDR) == 0)
262c0a69b8aSbluhm 		panic("%s: no header mbuf", __func__);
263df930be7Sderaadt 
264df930be7Sderaadt 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
265df930be7Sderaadt 		m_freem(m);
266df930be7Sderaadt 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
267df930be7Sderaadt 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
268df930be7Sderaadt 	}
269287546eaSitojun 
2708f6d2e8bSbluhm 	/*
2718f6d2e8bSbluhm 	 * Do not call if_input_local() directly.  Queue the packet to avoid
2728f6d2e8bSbluhm 	 * stack overflow and make TCP handshake over loopback work.
2738f6d2e8bSbluhm 	 */
274ea47e5e1Sdlg 	return (if_output_local(ifp, m, dst->sa_family));
275df930be7Sderaadt }
276df930be7Sderaadt 
277df930be7Sderaadt void
lortrequest(struct ifnet * ifp,int cmd,struct rtentry * rt)278dcb17c31Smpi lortrequest(struct ifnet *ifp, int cmd, struct rtentry *rt)
279df930be7Sderaadt {
280*84d9c64aSbluhm 	if (rt != NULL)
281*84d9c64aSbluhm 		atomic_cas_uint(&rt->rt_mtu, 0, LOMTU);
282df930be7Sderaadt }
283df930be7Sderaadt 
284df930be7Sderaadt /*
285df930be7Sderaadt  * Process an ioctl request.
286df930be7Sderaadt  */
287df930be7Sderaadt int
loioctl(struct ifnet * ifp,u_long cmd,caddr_t data)28831eea693Sclaudio loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
289df930be7Sderaadt {
29064aa4cc7Sitojun 	struct ifreq *ifr;
291fafd72b5Smpi 	int error = 0;
292df930be7Sderaadt 
293df930be7Sderaadt 	switch (cmd) {
294acf06393Smpi 	case SIOCSIFFLAGS:
295a3c0391fSbluhm 		if (ISSET(ifp->if_xflags, IFXF_LRO))
296a3c0391fSbluhm 			SET(ifp->if_capabilities, IFCAP_TSOv4 | IFCAP_TSOv6);
297a3c0391fSbluhm 		else
298a3c0391fSbluhm 			CLR(ifp->if_capabilities, IFCAP_TSOv4 | IFCAP_TSOv6);
299acf06393Smpi 		break;
300df930be7Sderaadt 
301df930be7Sderaadt 	case SIOCSIFADDR:
302bf673490Ssthen 		ifp->if_flags |= IFF_RUNNING;
303bf673490Ssthen 		if_up(ifp);		/* send up RTM_IFINFO */
304df930be7Sderaadt 		/*
305df930be7Sderaadt 		 * Everything else is done at a higher level.
306df930be7Sderaadt 		 */
307df930be7Sderaadt 		break;
308df930be7Sderaadt 
309df930be7Sderaadt 	case SIOCADDMULTI:
310df930be7Sderaadt 	case SIOCDELMULTI:
311df930be7Sderaadt 		break;
312df930be7Sderaadt 
3136f606230Sdugsong 	case SIOCSIFMTU:
3146f606230Sdugsong 		ifr = (struct ifreq *)data;
3156f606230Sdugsong 		ifp->if_mtu = ifr->ifr_mtu;
3166f606230Sdugsong 		break;
3176f606230Sdugsong 
318df930be7Sderaadt 	default:
319e7581027Sbrad 		error = ENOTTY;
320df930be7Sderaadt 	}
321df930be7Sderaadt 	return (error);
322df930be7Sderaadt }
323