1 /* $NetBSD: if_faith.c,v 1.49 2014/06/06 01:02:47 rmind Exp $ */ 2 /* $KAME: if_faith.c,v 1.21 2001/02/20 07:59:26 itojun Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1993 6 * The Regents of the University of California. 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 University 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 REGENTS 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 REGENTS 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 * derived from 34 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93 35 * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp 36 */ 37 38 /* 39 * IPv6-to-IPv4 TCP relay capturing interface 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: if_faith.c,v 1.49 2014/06/06 01:02:47 rmind Exp $"); 44 45 #include "opt_inet.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/mbuf.h> 51 #include <sys/socket.h> 52 #include <sys/errno.h> 53 #include <sys/ioctl.h> 54 #include <sys/time.h> 55 #include <sys/queue.h> 56 57 #include <sys/cpu.h> 58 59 #include <net/if.h> 60 #include <net/if_types.h> 61 #include <net/netisr.h> 62 #include <net/route.h> 63 #include <net/bpf.h> 64 #include <net/if_faith.h> 65 66 #ifdef INET 67 #include <netinet/in.h> 68 #include <netinet/in_systm.h> 69 #include <netinet/in_var.h> 70 #include <netinet/ip.h> 71 #endif 72 73 #ifdef INET6 74 #ifndef INET 75 #include <netinet/in.h> 76 #endif 77 #include <netinet6/in6_var.h> 78 #include <netinet/ip6.h> 79 #include <netinet6/ip6_var.h> 80 #endif 81 82 83 #include <net/net_osdep.h> 84 85 static int faithioctl(struct ifnet *, u_long, void *); 86 static int faithoutput(struct ifnet *, struct mbuf *, 87 const struct sockaddr *, struct rtentry *); 88 static void faithrtrequest(int, struct rtentry *, 89 const struct rt_addrinfo *); 90 91 void faithattach(int); 92 93 static int faith_clone_create(struct if_clone *, int); 94 static int faith_clone_destroy(struct ifnet *); 95 96 static struct if_clone faith_cloner = 97 IF_CLONE_INITIALIZER("faith", faith_clone_create, faith_clone_destroy); 98 99 #define FAITHMTU 1500 100 101 /* ARGSUSED */ 102 void 103 faithattach(int count) 104 { 105 106 if_clone_attach(&faith_cloner); 107 } 108 109 static int 110 faith_clone_create(struct if_clone *ifc, int unit) 111 { 112 struct ifnet *ifp; 113 114 ifp = if_alloc(IFT_FAITH); 115 116 if_initname(ifp, ifc->ifc_name, unit); 117 118 ifp->if_mtu = FAITHMTU; 119 /* Change to BROADCAST experimentaly to announce its prefix. */ 120 ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST; 121 ifp->if_ioctl = faithioctl; 122 ifp->if_output = faithoutput; 123 ifp->if_type = IFT_FAITH; 124 ifp->if_hdrlen = 0; 125 ifp->if_addrlen = 0; 126 ifp->if_dlt = DLT_NULL; 127 if_attach(ifp); 128 if_alloc_sadl(ifp); 129 bpf_attach(ifp, DLT_NULL, sizeof(u_int)); 130 return (0); 131 } 132 133 int 134 faith_clone_destroy(struct ifnet *ifp) 135 { 136 137 bpf_detach(ifp); 138 if_detach(ifp); 139 free(ifp, M_DEVBUF); 140 141 return (0); 142 } 143 144 static int 145 faithoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 146 struct rtentry *rt) 147 { 148 pktqueue_t *pktq; 149 size_t pktlen; 150 int s, error; 151 uint32_t af; 152 153 if ((m->m_flags & M_PKTHDR) == 0) 154 panic("faithoutput no HDR"); 155 af = dst->sa_family; 156 /* BPF write needs to be handled specially */ 157 if (af == AF_UNSPEC) { 158 af = *(mtod(m, int *)); 159 m_adj(m, sizeof(int)); 160 } 161 162 bpf_mtap_af(ifp, af, m); 163 164 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 165 m_freem(m); 166 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 167 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 168 } 169 pktlen = m->m_pkthdr.len; 170 ifp->if_opackets++; 171 ifp->if_obytes += pktlen; 172 switch (af) { 173 #ifdef INET 174 case AF_INET: 175 pktq = ip_pktq; 176 break; 177 #endif 178 #ifdef INET6 179 case AF_INET6: 180 pktq = ip6_pktq; 181 break; 182 #endif 183 default: 184 m_freem(m); 185 return EAFNOSUPPORT; 186 } 187 188 /* XXX do we need more sanity checks? */ 189 KASSERT(pktq != NULL); 190 m->m_pkthdr.rcvif = ifp; 191 192 s = splnet(); 193 if (__predict_true(pktq_enqueue(pktq, m, 0))) { 194 ifp->if_ipackets++; 195 ifp->if_ibytes += pktlen; 196 error = 0; 197 } else { 198 m_freem(m); 199 error = ENOBUFS; 200 } 201 splx(s); 202 203 return error; 204 } 205 206 /* ARGSUSED */ 207 static void 208 faithrtrequest(int cmd, struct rtentry *rt, 209 const struct rt_addrinfo *info) 210 { 211 if (rt) 212 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */ 213 } 214 215 /* 216 * Process an ioctl request. 217 */ 218 /* ARGSUSED */ 219 static int 220 faithioctl(struct ifnet *ifp, u_long cmd, void *data) 221 { 222 struct ifaddr *ifa; 223 struct ifreq *ifr = (struct ifreq *)data; 224 int error = 0; 225 226 switch (cmd) { 227 228 case SIOCINITIFADDR: 229 ifp->if_flags |= IFF_UP | IFF_RUNNING; 230 ifa = (struct ifaddr *)data; 231 ifa->ifa_rtrequest = faithrtrequest; 232 /* 233 * Everything else is done at a higher level. 234 */ 235 break; 236 237 case SIOCADDMULTI: 238 case SIOCDELMULTI: 239 if (ifr == 0) { 240 error = EAFNOSUPPORT; /* XXX */ 241 break; 242 } 243 switch (ifr->ifr_addr.sa_family) { 244 #ifdef INET 245 case AF_INET: 246 break; 247 #endif 248 #ifdef INET6 249 case AF_INET6: 250 break; 251 #endif 252 253 default: 254 error = EAFNOSUPPORT; 255 break; 256 } 257 break; 258 259 default: 260 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET) 261 error = 0; 262 break; 263 } 264 return (error); 265 } 266 267 #ifdef INET6 268 /* 269 * XXX could be slow 270 * XXX could be layer violation to call sys/net from sys/netinet6 271 */ 272 int 273 faithprefix(struct in6_addr *in6) 274 { 275 struct rtentry *rt; 276 struct sockaddr_in6 sin6; 277 int ret; 278 279 if (ip6_keepfaith == 0) 280 return 0; 281 282 memset(&sin6, 0, sizeof(sin6)); 283 sin6.sin6_family = AF_INET6; 284 sin6.sin6_len = sizeof(struct sockaddr_in6); 285 sin6.sin6_addr = *in6; 286 rt = rtalloc1((struct sockaddr *)&sin6, 0); 287 if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH && 288 (rt->rt_ifp->if_flags & IFF_UP) != 0) 289 ret = 1; 290 else 291 ret = 0; 292 if (rt) 293 rtfree(rt); 294 return ret; 295 } 296 #endif 297