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 */ 35 36 /* 37 * Loopback interface driver for protocol testing and timing. 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/mbuf.h> 44 #include <sys/socket.h> 45 #include <sys/errno.h> 46 #include <sys/ioctl.h> 47 #include <sys/time.h> 48 #include <machine/cpu.h> 49 50 #include <net/if.h> 51 #include <net/if_types.h> 52 #include <net/netisr.h> 53 #include <net/route.h> 54 #include <net/bpf.h> 55 56 #ifdef INET 57 #include <netinet/in.h> 58 #include <netinet/in_systm.h> 59 #include <netinet/in_var.h> 60 #include <netinet/ip.h> 61 #endif 62 63 #ifdef NS 64 #include <netns/ns.h> 65 #include <netns/ns_if.h> 66 #endif 67 68 #ifdef ISO 69 #include <netiso/iso.h> 70 #include <netiso/iso_var.h> 71 #endif 72 73 #include "bpfilter.h" 74 75 #define LOMTU (1024+512) 76 77 struct ifnet loif; 78 79 /* ARGSUSED */ 80 void 81 loopattach(n) 82 int n; 83 { 84 register struct ifnet *ifp = &loif; 85 86 #ifdef lint 87 n = n; /* Highlander: there can only be one... */ 88 #endif 89 ifp->if_name = "lo"; 90 ifp->if_mtu = LOMTU; 91 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST; 92 ifp->if_ioctl = loioctl; 93 ifp->if_output = looutput; 94 ifp->if_type = IFT_LOOP; 95 ifp->if_hdrlen = 0; 96 ifp->if_addrlen = 0; 97 if_attach(ifp); 98 #if NBPFILTER > 0 99 bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int)); 100 #endif 101 } 102 103 int 104 looutput(ifp, m, dst, rt) 105 struct ifnet *ifp; 106 register struct mbuf *m; 107 struct sockaddr *dst; 108 register struct rtentry *rt; 109 { 110 int s, isr; 111 register struct ifqueue *ifq = 0; 112 113 if ((m->m_flags & M_PKTHDR) == 0) 114 panic("looutput no HDR"); 115 ifp->if_lastchange = time; 116 #if NBPFILTER > 0 117 if (loif.if_bpf) { 118 /* 119 * We need to prepend the address family as 120 * a four byte field. Cons up a dummy header 121 * to pacify bpf. This is safe because bpf 122 * will only read from the mbuf (i.e., it won't 123 * try to free it or keep a pointer a to it). 124 */ 125 struct mbuf m0; 126 u_int af = dst->sa_family; 127 128 m0.m_next = m; 129 m0.m_len = 4; 130 m0.m_data = (char *)⁡ 131 132 bpf_mtap(loif.if_bpf, &m0); 133 } 134 #endif 135 m->m_pkthdr.rcvif = ifp; 136 137 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 138 m_freem(m); 139 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 140 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 141 } 142 ifp->if_opackets++; 143 ifp->if_obytes += m->m_pkthdr.len; 144 switch (dst->sa_family) { 145 146 #ifdef INET 147 case AF_INET: 148 ifq = &ipintrq; 149 isr = NETISR_IP; 150 break; 151 #endif 152 #ifdef NS 153 case AF_NS: 154 ifq = &nsintrq; 155 isr = NETISR_NS; 156 break; 157 #endif 158 #ifdef ISO 159 case AF_ISO: 160 ifq = &clnlintrq; 161 isr = NETISR_ISO; 162 break; 163 #endif 164 default: 165 printf("lo%d: can't handle af%d\n", ifp->if_unit, 166 dst->sa_family); 167 m_freem(m); 168 return (EAFNOSUPPORT); 169 } 170 s = splimp(); 171 if (IF_QFULL(ifq)) { 172 IF_DROP(ifq); 173 m_freem(m); 174 splx(s); 175 return (ENOBUFS); 176 } 177 IF_ENQUEUE(ifq, m); 178 schednetisr(isr); 179 ifp->if_ipackets++; 180 ifp->if_ibytes += m->m_pkthdr.len; 181 splx(s); 182 return (0); 183 } 184 185 /* ARGSUSED */ 186 void 187 lortrequest(cmd, rt, sa) 188 int cmd; 189 struct rtentry *rt; 190 struct sockaddr *sa; 191 { 192 193 if (rt) 194 rt->rt_rmx.rmx_mtu = LOMTU; 195 } 196 197 /* 198 * Process an ioctl request. 199 */ 200 /* ARGSUSED */ 201 int 202 loioctl(ifp, cmd, data) 203 register struct ifnet *ifp; 204 int cmd; 205 caddr_t data; 206 { 207 register struct ifaddr *ifa; 208 register struct ifreq *ifr; 209 register int error = 0; 210 211 switch (cmd) { 212 213 case SIOCSIFADDR: 214 ifp->if_flags |= IFF_UP; 215 ifa = (struct ifaddr *)data; 216 if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO) 217 ifa->ifa_rtrequest = lortrequest; 218 /* 219 * Everything else is done at a higher level. 220 */ 221 break; 222 223 case SIOCADDMULTI: 224 case SIOCDELMULTI: 225 ifr = (struct ifreq *)data; 226 if (ifr == 0) { 227 error = EAFNOSUPPORT; /* XXX */ 228 break; 229 } 230 switch (ifr->ifr_addr.sa_family) { 231 232 #ifdef INET 233 case AF_INET: 234 break; 235 #endif 236 237 default: 238 error = EAFNOSUPPORT; 239 break; 240 } 241 break; 242 243 default: 244 error = EINVAL; 245 } 246 return (error); 247 } 248