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