xref: /openbsd/sys/net/if_loop.c (revision ea47e5e1)
1*ea47e5e1Sdlg /*	$OpenBSD: if_loop.c,v 1.84 2017/12/15 01:37:30 dlg 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 *);
146c0a69b8aSbluhm int	loinput(struct ifnet *, struct mbuf *, void *);
147d5ee4d17Smpi int	looutput(struct ifnet *,
148d5ee4d17Smpi 	    struct mbuf *, struct sockaddr *, struct rtentry *);
149d5ee4d17Smpi 
150fb1d047fSmarkus int	loop_clone_create(struct if_clone *, int);
151b9e739dfSmarkus int	loop_clone_destroy(struct ifnet *);
152fb1d047fSmarkus 
153fb1d047fSmarkus struct if_clone loop_cloner =
154fb1d047fSmarkus     IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
155fb1d047fSmarkus 
156df930be7Sderaadt void
15731eea693Sclaudio loopattach(int n)
158df930be7Sderaadt {
159daf2ccbcSmpi 	if (loop_clone_create(&loop_cloner, 0))
160daf2ccbcSmpi 		panic("unable to create lo0");
161daf2ccbcSmpi 
162fb1d047fSmarkus 	if_clone_attach(&loop_cloner);
163fb1d047fSmarkus }
164df930be7Sderaadt 
165fb1d047fSmarkus int
16631eea693Sclaudio loop_clone_create(struct if_clone *ifc, int unit)
167fb1d047fSmarkus {
168fb1d047fSmarkus 	struct ifnet *ifp;
169fb1d047fSmarkus 
1708b28ab37Shenning 	ifp = malloc(sizeof(*ifp), M_DEVBUF, M_NOWAIT|M_ZERO);
171fcc641efSmickey 	if (ifp == NULL)
172fb1d047fSmarkus 		return (ENOMEM);
173fb1d047fSmarkus 
174fb1d047fSmarkus 	snprintf(ifp->if_xname, sizeof ifp->if_xname, "lo%d", unit);
17509d901c0Sderaadt 	ifp->if_softc = NULL;
176df930be7Sderaadt 	ifp->if_mtu = LOMTU;
177df930be7Sderaadt 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
17899bf4b5fSmpi 	ifp->if_xflags = IFXF_CLONED;
179dcb17c31Smpi 	ifp->if_rtrequest = lortrequest;
180df930be7Sderaadt 	ifp->if_ioctl = loioctl;
181df930be7Sderaadt 	ifp->if_output = looutput;
182df930be7Sderaadt 	ifp->if_type = IFT_LOOP;
183643a56f3Sderaadt 	ifp->if_hdrlen = sizeof(u_int32_t);
184df930be7Sderaadt 	ifp->if_addrlen = 0;
185fb1d047fSmarkus 	if (unit == 0) {
186138560ceSderaadt 		if_attachhead(ifp);
1875e9a6f6cSmpi 		if_addgroup(ifp, ifc->ifc_name);
1883a63dfc0Smpi 		rtable_l2set(0, 0, ifp->if_index);
189fb1d047fSmarkus 	} else
190fb1d047fSmarkus 		if_attach(ifp);
191d6cd50e9Sitojun 	if_alloc_sadl(ifp);
192df930be7Sderaadt #if NBPFILTER > 0
193643a56f3Sderaadt 	bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t));
194df930be7Sderaadt #endif
195c0a69b8aSbluhm 	if_ih_insert(ifp, loinput, NULL);
196fb1d047fSmarkus 	return (0);
197df930be7Sderaadt }
198fb1d047fSmarkus 
199b9e739dfSmarkus int
20031eea693Sclaudio loop_clone_destroy(struct ifnet *ifp)
201fb1d047fSmarkus {
2023a63dfc0Smpi 	if (ifp->if_index == rtable_loindex(ifp->if_rdomain))
203b9e739dfSmarkus 		return (EPERM);
204fb1d047fSmarkus 
205c0a69b8aSbluhm 	if_ih_remove(ifp, loinput, NULL);
206fb1d047fSmarkus 	if_detach(ifp);
207fb1d047fSmarkus 
208ac3ba07eSderaadt 	free(ifp, M_DEVBUF, sizeof(*ifp));
209b9e739dfSmarkus 	return (0);
210df930be7Sderaadt }
211df930be7Sderaadt 
212df930be7Sderaadt int
213c0a69b8aSbluhm loinput(struct ifnet *ifp, struct mbuf *m, void *cookie)
214c0a69b8aSbluhm {
215c0a69b8aSbluhm 	int error;
216c0a69b8aSbluhm 
217c0a69b8aSbluhm 	if ((m->m_flags & M_PKTHDR) == 0)
218c0a69b8aSbluhm 		panic("%s: no header mbuf", __func__);
219c0a69b8aSbluhm 
220c0a69b8aSbluhm 	error = if_input_local(ifp, m, m->m_pkthdr.ph_family);
221c0a69b8aSbluhm 	if (error)
222c0a69b8aSbluhm 		ifp->if_ierrors++;
223c0a69b8aSbluhm 
224c0a69b8aSbluhm 	return (1);
225c0a69b8aSbluhm }
226c0a69b8aSbluhm 
227c0a69b8aSbluhm int
22831eea693Sclaudio looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
22931eea693Sclaudio     struct rtentry *rt)
230df930be7Sderaadt {
231df930be7Sderaadt 	if ((m->m_flags & M_PKTHDR) == 0)
232c0a69b8aSbluhm 		panic("%s: no header mbuf", __func__);
233df930be7Sderaadt 
234df930be7Sderaadt 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
235df930be7Sderaadt 		m_freem(m);
236df930be7Sderaadt 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
237df930be7Sderaadt 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
238df930be7Sderaadt 	}
239287546eaSitojun 
240c0a69b8aSbluhm 	/* Use the quick path only once to avoid stack overflow. */
241c0a69b8aSbluhm 	if ((m->m_flags & M_LOOP) == 0)
2427f3eeafaSmpi 		return (if_input_local(ifp, m, dst->sa_family));
243c0a69b8aSbluhm 
244*ea47e5e1Sdlg 	return (if_output_local(ifp, m, dst->sa_family));
245df930be7Sderaadt }
246df930be7Sderaadt 
247df930be7Sderaadt void
248dcb17c31Smpi lortrequest(struct ifnet *ifp, int cmd, struct rtentry *rt)
249df930be7Sderaadt {
2509b6a6412Sbluhm 	if (rt && rt->rt_mtu == 0)
2519b6a6412Sbluhm 		rt->rt_mtu = LOMTU;
252df930be7Sderaadt }
253df930be7Sderaadt 
254df930be7Sderaadt /*
255df930be7Sderaadt  * Process an ioctl request.
256df930be7Sderaadt  */
257df930be7Sderaadt int
25831eea693Sclaudio loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
259df930be7Sderaadt {
26064aa4cc7Sitojun 	struct ifreq *ifr;
261fafd72b5Smpi 	int error = 0;
262df930be7Sderaadt 
263df930be7Sderaadt 	switch (cmd) {
264acf06393Smpi 	case SIOCSIFFLAGS:
265acf06393Smpi 		break;
266df930be7Sderaadt 
267df930be7Sderaadt 	case SIOCSIFADDR:
268bf673490Ssthen 		ifp->if_flags |= IFF_RUNNING;
269bf673490Ssthen 		if_up(ifp);		/* send up RTM_IFINFO */
270df930be7Sderaadt 		/*
271df930be7Sderaadt 		 * Everything else is done at a higher level.
272df930be7Sderaadt 		 */
273df930be7Sderaadt 		break;
274df930be7Sderaadt 
275df930be7Sderaadt 	case SIOCADDMULTI:
276df930be7Sderaadt 	case SIOCDELMULTI:
277df930be7Sderaadt 		break;
278df930be7Sderaadt 
2796f606230Sdugsong 	case SIOCSIFMTU:
2806f606230Sdugsong 		ifr = (struct ifreq *)data;
2816f606230Sdugsong 		ifp->if_mtu = ifr->ifr_mtu;
2826f606230Sdugsong 		break;
2836f606230Sdugsong 
284df930be7Sderaadt 	default:
285e7581027Sbrad 		error = ENOTTY;
286df930be7Sderaadt 	}
287df930be7Sderaadt 	return (error);
288df930be7Sderaadt }
289