1 /* $OpenBSD: if_loop.c,v 1.56 2014/07/12 18:44:22 tedu 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/kernel.h> 113 #include <sys/mbuf.h> 114 #include <sys/socket.h> 115 #include <sys/errno.h> 116 #include <sys/ioctl.h> 117 #include <sys/time.h> 118 119 120 #include <net/if.h> 121 #include <net/if_types.h> 122 #include <net/netisr.h> 123 #include <net/route.h> 124 125 #ifdef INET 126 #include <netinet/in.h> 127 #include <netinet/in_systm.h> 128 #include <netinet/ip.h> 129 #endif 130 131 #ifdef INET6 132 #ifndef INET 133 #include <netinet/in.h> 134 #endif 135 #include <netinet/ip6.h> 136 #endif 137 138 #ifdef MPLS 139 #include <netmpls/mpls.h> 140 #endif 141 142 #if NBPFILTER > 0 143 #include <net/bpf.h> 144 #endif 145 146 #define LOMTU 32768 147 148 int loop_clone_create(struct if_clone *, int); 149 int loop_clone_destroy(struct ifnet *); 150 151 struct if_clone loop_cloner = 152 IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy); 153 154 /* ARGSUSED */ 155 void 156 loopattach(int n) 157 { 158 (void) loop_clone_create(&loop_cloner, 0); 159 if_clone_attach(&loop_cloner); 160 } 161 162 int 163 loop_clone_create(struct if_clone *ifc, int unit) 164 { 165 struct ifnet *ifp; 166 167 ifp = malloc(sizeof(*ifp), M_DEVBUF, M_NOWAIT|M_ZERO); 168 if (ifp == NULL) 169 return (ENOMEM); 170 171 snprintf(ifp->if_xname, sizeof ifp->if_xname, "lo%d", unit); 172 ifp->if_softc = NULL; 173 ifp->if_mtu = LOMTU; 174 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 175 ifp->if_ioctl = loioctl; 176 ifp->if_output = looutput; 177 ifp->if_type = IFT_LOOP; 178 ifp->if_hdrlen = sizeof(u_int32_t); 179 ifp->if_addrlen = 0; 180 IFQ_SET_READY(&ifp->if_snd); 181 if (unit == 0) { 182 lo0ifp = ifp; 183 if_attachhead(ifp); 184 if_addgroup(lo0ifp, ifc->ifc_name); 185 } else 186 if_attach(ifp); 187 if_alloc_sadl(ifp); 188 #if NBPFILTER > 0 189 bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t)); 190 #endif 191 return (0); 192 } 193 194 int 195 loop_clone_destroy(struct ifnet *ifp) 196 { 197 if (ifp == lo0ifp) 198 return (EPERM); 199 200 if_detach(ifp); 201 202 free(ifp, M_DEVBUF, 0); 203 return (0); 204 } 205 206 int 207 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 208 struct rtentry *rt) 209 { 210 int s, isr; 211 struct ifqueue *ifq = 0; 212 213 if ((m->m_flags & M_PKTHDR) == 0) 214 panic("looutput: no header mbuf"); 215 #if NBPFILTER > 0 216 /* 217 * only send packets to bpf if they are real loopback packets; 218 * looutput() is also called for SIMPLEX interfaces to duplicate 219 * packets for local use. But don't dup them to bpf. 220 */ 221 if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK)) 222 bpf_mtap_af(ifp->if_bpf, dst->sa_family, m, BPF_DIRECTION_OUT); 223 #endif 224 m->m_pkthdr.rcvif = ifp; 225 226 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 227 m_freem(m); 228 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 229 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 230 } 231 232 ifp->if_opackets++; 233 ifp->if_obytes += m->m_pkthdr.len; 234 switch (dst->sa_family) { 235 236 #ifdef INET 237 case AF_INET: 238 ifq = &ipintrq; 239 isr = NETISR_IP; 240 break; 241 #endif 242 #ifdef INET6 243 case AF_INET6: 244 ifq = &ip6intrq; 245 isr = NETISR_IPV6; 246 break; 247 #endif /* INET6 */ 248 #ifdef MPLS 249 case AF_MPLS: 250 ifq = &mplsintrq; 251 isr = NETISR_MPLS; 252 break; 253 #endif /* MPLS */ 254 default: 255 printf("%s: can't handle af%d\n", ifp->if_xname, 256 dst->sa_family); 257 m_freem(m); 258 return (EAFNOSUPPORT); 259 } 260 s = splnet(); 261 if (IF_QFULL(ifq)) { 262 IF_DROP(ifq); 263 m_freem(m); 264 splx(s); 265 return (ENOBUFS); 266 } 267 IF_ENQUEUE(ifq, m); 268 schednetisr(isr); 269 ifp->if_ipackets++; 270 ifp->if_ibytes += m->m_pkthdr.len; 271 splx(s); 272 return (0); 273 } 274 275 /* ARGSUSED */ 276 void 277 lortrequest(int cmd, struct rtentry *rt) 278 { 279 if (rt) 280 rt->rt_rmx.rmx_mtu = LOMTU; 281 } 282 283 /* 284 * Process an ioctl request. 285 */ 286 /* ARGSUSED */ 287 int 288 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 289 { 290 struct ifaddr *ifa; 291 struct ifreq *ifr; 292 int s, error = 0; 293 294 switch (cmd) { 295 296 case SIOCSIFADDR: 297 s = splnet(); 298 ifp->if_flags |= IFF_RUNNING; 299 if_up(ifp); /* send up RTM_IFINFO */ 300 splx(s); 301 302 ifa = (struct ifaddr *)data; 303 if (ifa != 0) 304 ifa->ifa_rtrequest = lortrequest; 305 /* 306 * Everything else is done at a higher level. 307 */ 308 break; 309 310 case SIOCADDMULTI: 311 case SIOCDELMULTI: 312 break; 313 314 case SIOCSIFMTU: 315 ifr = (struct ifreq *)data; 316 ifp->if_mtu = ifr->ifr_mtu; 317 break; 318 319 default: 320 error = ENOTTY; 321 } 322 return (error); 323 } 324