1*f7ebbf19Shenning /* $OpenBSD: if_loop.c,v 1.88 2018/09/09 10:11:41 henning 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 170809d3a3eSbluhm ifp = malloc(sizeof(*ifp), M_DEVBUF, M_WAITOK|M_ZERO); 171fb1d047fSmarkus snprintf(ifp->if_xname, sizeof ifp->if_xname, "lo%d", unit); 17209d901c0Sderaadt ifp->if_softc = NULL; 173df930be7Sderaadt ifp->if_mtu = LOMTU; 174df930be7Sderaadt ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 17599bf4b5fSmpi ifp->if_xflags = IFXF_CLONED; 176dcb17c31Smpi ifp->if_rtrequest = lortrequest; 177df930be7Sderaadt ifp->if_ioctl = loioctl; 178df930be7Sderaadt ifp->if_output = looutput; 179df930be7Sderaadt ifp->if_type = IFT_LOOP; 180643a56f3Sderaadt ifp->if_hdrlen = sizeof(u_int32_t); 181fb1d047fSmarkus if (unit == 0) { 182138560ceSderaadt if_attachhead(ifp); 1835e9a6f6cSmpi if_addgroup(ifp, ifc->ifc_name); 1843a63dfc0Smpi rtable_l2set(0, 0, ifp->if_index); 185fb1d047fSmarkus } else 186fb1d047fSmarkus if_attach(ifp); 187d6cd50e9Sitojun if_alloc_sadl(ifp); 188df930be7Sderaadt #if NBPFILTER > 0 189643a56f3Sderaadt bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t)); 190df930be7Sderaadt #endif 191c0a69b8aSbluhm if_ih_insert(ifp, loinput, NULL); 192fb1d047fSmarkus return (0); 193df930be7Sderaadt } 194fb1d047fSmarkus 195b9e739dfSmarkus int 19631eea693Sclaudio loop_clone_destroy(struct ifnet *ifp) 197fb1d047fSmarkus { 198*f7ebbf19Shenning struct ifnet *p; 199*f7ebbf19Shenning 200*f7ebbf19Shenning if (ifp->if_index == rtable_loindex(ifp->if_rdomain)) { 201*f7ebbf19Shenning /* rdomain 0 always needs a loopback */ 202*f7ebbf19Shenning if (ifp->if_rdomain == 0) 203b9e739dfSmarkus return (EPERM); 204fb1d047fSmarkus 205*f7ebbf19Shenning /* if there is any other interface in this rdomain, deny */ 206*f7ebbf19Shenning NET_LOCK(); 207*f7ebbf19Shenning TAILQ_FOREACH(p, &ifnet, if_list) { 208*f7ebbf19Shenning if (p->if_rdomain != ifp->if_rdomain) 209*f7ebbf19Shenning continue; 210*f7ebbf19Shenning if (p->if_index == ifp->if_index) 211*f7ebbf19Shenning continue; 212*f7ebbf19Shenning NET_UNLOCK(); 213*f7ebbf19Shenning return (EBUSY); 214*f7ebbf19Shenning } 215*f7ebbf19Shenning NET_UNLOCK(); 216*f7ebbf19Shenning 217*f7ebbf19Shenning rtable_l2set(ifp->if_rdomain, 0, 0); 218*f7ebbf19Shenning } 219*f7ebbf19Shenning 220c0a69b8aSbluhm if_ih_remove(ifp, loinput, NULL); 221fb1d047fSmarkus if_detach(ifp); 222fb1d047fSmarkus 223ac3ba07eSderaadt free(ifp, M_DEVBUF, sizeof(*ifp)); 224b9e739dfSmarkus return (0); 225df930be7Sderaadt } 226df930be7Sderaadt 227df930be7Sderaadt int 228c0a69b8aSbluhm loinput(struct ifnet *ifp, struct mbuf *m, void *cookie) 229c0a69b8aSbluhm { 230c0a69b8aSbluhm int error; 231c0a69b8aSbluhm 232c0a69b8aSbluhm if ((m->m_flags & M_PKTHDR) == 0) 233c0a69b8aSbluhm panic("%s: no header mbuf", __func__); 234c0a69b8aSbluhm 235c0a69b8aSbluhm error = if_input_local(ifp, m, m->m_pkthdr.ph_family); 236c0a69b8aSbluhm if (error) 237c0a69b8aSbluhm ifp->if_ierrors++; 238c0a69b8aSbluhm 239c0a69b8aSbluhm return (1); 240c0a69b8aSbluhm } 241c0a69b8aSbluhm 242c0a69b8aSbluhm int 24331eea693Sclaudio looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 24431eea693Sclaudio struct rtentry *rt) 245df930be7Sderaadt { 246df930be7Sderaadt if ((m->m_flags & M_PKTHDR) == 0) 247c0a69b8aSbluhm panic("%s: no header mbuf", __func__); 248df930be7Sderaadt 249df930be7Sderaadt if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 250df930be7Sderaadt m_freem(m); 251df930be7Sderaadt return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 252df930be7Sderaadt rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 253df930be7Sderaadt } 254287546eaSitojun 255c0a69b8aSbluhm /* Use the quick path only once to avoid stack overflow. */ 256c0a69b8aSbluhm if ((m->m_flags & M_LOOP) == 0) 2577f3eeafaSmpi return (if_input_local(ifp, m, dst->sa_family)); 258c0a69b8aSbluhm 259ea47e5e1Sdlg return (if_output_local(ifp, m, dst->sa_family)); 260df930be7Sderaadt } 261df930be7Sderaadt 262df930be7Sderaadt void 263dcb17c31Smpi lortrequest(struct ifnet *ifp, int cmd, struct rtentry *rt) 264df930be7Sderaadt { 2659b6a6412Sbluhm if (rt && rt->rt_mtu == 0) 2669b6a6412Sbluhm rt->rt_mtu = LOMTU; 267df930be7Sderaadt } 268df930be7Sderaadt 269df930be7Sderaadt /* 270df930be7Sderaadt * Process an ioctl request. 271df930be7Sderaadt */ 272df930be7Sderaadt int 27331eea693Sclaudio loioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 274df930be7Sderaadt { 27564aa4cc7Sitojun struct ifreq *ifr; 276fafd72b5Smpi int error = 0; 277df930be7Sderaadt 278df930be7Sderaadt switch (cmd) { 279acf06393Smpi case SIOCSIFFLAGS: 280acf06393Smpi break; 281df930be7Sderaadt 282df930be7Sderaadt case SIOCSIFADDR: 283bf673490Ssthen ifp->if_flags |= IFF_RUNNING; 284bf673490Ssthen if_up(ifp); /* send up RTM_IFINFO */ 285df930be7Sderaadt /* 286df930be7Sderaadt * Everything else is done at a higher level. 287df930be7Sderaadt */ 288df930be7Sderaadt break; 289df930be7Sderaadt 290df930be7Sderaadt case SIOCADDMULTI: 291df930be7Sderaadt case SIOCDELMULTI: 292df930be7Sderaadt break; 293df930be7Sderaadt 2946f606230Sdugsong case SIOCSIFMTU: 2956f606230Sdugsong ifr = (struct ifreq *)data; 2966f606230Sdugsong ifp->if_mtu = ifr->ifr_mtu; 2976f606230Sdugsong break; 2986f606230Sdugsong 299df930be7Sderaadt default: 300e7581027Sbrad error = ENOTTY; 301df930be7Sderaadt } 302df930be7Sderaadt return (error); 303df930be7Sderaadt } 304