1 /* 2 * Copyright (c) 1982, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93 34 * $Id: if_loop.c,v 1.5 1994/08/02 07:46:17 davidg Exp $ 35 */ 36 37 /* 38 * Loopback interface driver for protocol testing and timing. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/mbuf.h> 45 #include <sys/socket.h> 46 #include <sys/errno.h> 47 #include <sys/ioctl.h> 48 #include <sys/time.h> 49 #include <machine/cpu.h> 50 51 #include <net/if.h> 52 #include <net/if_types.h> 53 #include <net/netisr.h> 54 #include <net/route.h> 55 #include <net/bpf.h> 56 57 #ifdef INET 58 #include <netinet/in.h> 59 #include <netinet/in_systm.h> 60 #include <netinet/in_var.h> 61 #include <netinet/ip.h> 62 #endif 63 64 #ifdef NS 65 #include <netns/ns.h> 66 #include <netns/ns_if.h> 67 #endif 68 69 #ifdef ISO 70 #include <netiso/iso.h> 71 #include <netiso/iso_var.h> 72 #endif 73 74 #include "bpfilter.h" 75 76 #ifdef TINY_LOMTU 77 #define LOMTU (1024+512) 78 #else 79 #define LOMTU 65532 80 #endif 81 82 struct ifnet loif; 83 84 /* ARGSUSED */ 85 void 86 loopattach(void) 87 { 88 register struct ifnet *ifp = &loif; 89 90 ifp->if_name = "lo"; 91 ifp->if_mtu = LOMTU; 92 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 93 ifp->if_ioctl = loioctl; 94 ifp->if_output = looutput; 95 ifp->if_type = IFT_LOOP; 96 ifp->if_hdrlen = 0; 97 ifp->if_addrlen = 0; 98 if_attach(ifp); 99 #if NBPFILTER > 0 100 bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int)); 101 #endif 102 } 103 104 TEXT_SET(pseudo_set, loopattach); 105 106 int 107 looutput(ifp, m, dst, rt) 108 struct ifnet *ifp; 109 register struct mbuf *m; 110 struct sockaddr *dst; 111 register struct rtentry *rt; 112 { 113 int s, isr; 114 register struct ifqueue *ifq = 0; 115 116 if ((m->m_flags & M_PKTHDR) == 0) 117 panic("looutput no HDR"); 118 ifp->if_lastchange = time; 119 #if NBPFILTER > 0 120 if (loif.if_bpf) { 121 /* 122 * We need to prepend the address family as 123 * a four byte field. Cons up a dummy header 124 * to pacify bpf. This is safe because bpf 125 * will only read from the mbuf (i.e., it won't 126 * try to free it or keep a pointer a to it). 127 */ 128 struct mbuf m0; 129 u_int af = dst->sa_family; 130 131 m0.m_next = m; 132 m0.m_len = 4; 133 m0.m_data = (char *)⁡ 134 135 bpf_mtap(loif.if_bpf, &m0); 136 } 137 #endif 138 m->m_pkthdr.rcvif = ifp; 139 140 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 141 m_freem(m); 142 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 143 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 144 } 145 ifp->if_opackets++; 146 ifp->if_obytes += m->m_pkthdr.len; 147 switch (dst->sa_family) { 148 149 #ifdef INET 150 case AF_INET: 151 ifq = &ipintrq; 152 isr = NETISR_IP; 153 break; 154 #endif 155 #ifdef NS 156 case AF_NS: 157 ifq = &nsintrq; 158 isr = NETISR_NS; 159 break; 160 #endif 161 #ifdef ISO 162 case AF_ISO: 163 ifq = &clnlintrq; 164 isr = NETISR_ISO; 165 break; 166 #endif 167 default: 168 printf("lo%d: can't handle af%d\n", ifp->if_unit, 169 dst->sa_family); 170 m_freem(m); 171 return (EAFNOSUPPORT); 172 } 173 s = splimp(); 174 if (IF_QFULL(ifq)) { 175 IF_DROP(ifq); 176 m_freem(m); 177 splx(s); 178 return (ENOBUFS); 179 } 180 IF_ENQUEUE(ifq, m); 181 schednetisr(isr); 182 ifp->if_ipackets++; 183 ifp->if_ibytes += m->m_pkthdr.len; 184 splx(s); 185 return (0); 186 } 187 188 /* ARGSUSED */ 189 void 190 lortrequest(cmd, rt, sa) 191 int cmd; 192 struct rtentry *rt; 193 struct sockaddr *sa; 194 { 195 196 if (rt) 197 rt->rt_rmx.rmx_mtu = LOMTU; 198 } 199 200 /* 201 * Process an ioctl request. 202 */ 203 /* ARGSUSED */ 204 int 205 loioctl(ifp, cmd, data) 206 register struct ifnet *ifp; 207 int cmd; 208 caddr_t data; 209 { 210 register struct ifaddr *ifa; 211 register struct ifreq *ifr = (struct ifreq *)data; 212 register int error = 0; 213 214 switch (cmd) { 215 216 case SIOCSIFADDR: 217 ifp->if_flags |= IFF_UP; 218 ifa = (struct ifaddr *)data; 219 if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO) 220 ifa->ifa_rtrequest = lortrequest; 221 /* 222 * Everything else is done at a higher level. 223 */ 224 break; 225 226 case SIOCADDMULTI: 227 case SIOCDELMULTI: 228 if (ifr == 0) { 229 error = EAFNOSUPPORT; /* XXX */ 230 break; 231 } 232 switch (ifr->ifr_addr.sa_family) { 233 234 #ifdef INET 235 case AF_INET: 236 break; 237 #endif 238 239 default: 240 error = EAFNOSUPPORT; 241 break; 242 } 243 break; 244 245 case SIOCSIFMTU: 246 ifp->if_mtu = ifr->ifr_mtu; 247 break; 248 249 default: 250 error = EINVAL; 251 } 252 return (error); 253 } 254