1/* $KAME: if_faith.c,v 1.21 2001/02/20 07:59:26 itojun Exp $ */ 2 3/* 4 * Copyright (c) 1982, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 *
|
35 * $FreeBSD: head/sys/net/if_faith.c 83934 2001-09-25 18:40:52Z brooks $
|
35 * $FreeBSD: head/sys/net/if_faith.c 85074 2001-10-17 18:07:05Z ru $ |
36 */ 37/* 38 * derived from 39 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93 40 * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp 41 */ 42 43/* 44 * Loopback interface driver for protocol testing and timing. 45 */ 46#include "opt_inet.h" 47#include "opt_inet6.h" 48 49#include <sys/param.h> 50#include <sys/systm.h> 51#include <sys/kernel.h> 52#include <sys/mbuf.h> 53#include <sys/socket.h> 54#include <sys/errno.h> 55#include <sys/sockio.h> 56#include <sys/time.h> 57#include <sys/queue.h> 58#include <sys/types.h> 59#include <sys/malloc.h> 60#include <machine/bus.h> /* XXX: Shouldn't really be required! */ 61#include <sys/rman.h> 62 63#include <net/if.h> 64#include <net/if_types.h> 65#include <net/netisr.h> 66#include <net/route.h> 67#include <net/bpf.h> 68 69#ifdef INET 70#include <netinet/in.h> 71#include <netinet/in_systm.h> 72#include <netinet/in_var.h> 73#include <netinet/ip.h> 74#endif 75 76#ifdef INET6 77#ifndef INET 78#include <netinet/in.h> 79#endif 80#include <netinet6/in6_var.h> 81#include <netinet/ip6.h> 82#include <netinet6/ip6_var.h> 83#endif 84 85#include <net/net_osdep.h> 86 87#define FAITHNAME "faith" 88#define FAITH_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */ 89 90struct faith_softc { 91 struct ifnet sc_if; /* must be first */ 92 struct resource *r_unit; 93 LIST_ENTRY(faith_softc) sc_list; 94}; 95 96static int faithioctl __P((struct ifnet *, u_long, caddr_t)); 97int faithoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *, 98 struct rtentry *));
|
99static void faithrtrequest __P((int, struct rtentry *, struct sockaddr *));
|
99static void faithrtrequest __P((int, struct rtentry *, struct rt_addrinfo *)); |
100static int faithprefix __P((struct in6_addr *)); 101 102static int faithmodevent __P((module_t, int, void *)); 103 104static MALLOC_DEFINE(M_FAITH, FAITHNAME, "Firewall Assisted Tunnel Interface"); 105static struct rman faithunits[1]; 106LIST_HEAD(, faith_softc) faith_softc_list; 107 108int faith_clone_create __P((struct if_clone *, int *)); 109void faith_clone_destroy __P((struct ifnet *)); 110 111struct if_clone faith_cloner = 112 IF_CLONE_INITIALIZER(FAITHNAME, faith_clone_create, faith_clone_destroy); 113 114#define FAITHMTU 1500 115 116static int 117faithmodevent(mod, type, data) 118 module_t mod; 119 int type; 120 void *data; 121{ 122 int err; 123 124 switch (type) { 125 case MOD_LOAD: 126 faithunits->rm_type = RMAN_ARRAY; 127 faithunits->rm_descr = "configurable if_faith units"; 128 err = rman_init(faithunits); 129 if (err != 0) 130 return (err); 131 err = rman_manage_region(faithunits, 0, FAITH_MAXUNIT); 132 if (err != 0) { 133 printf("%s: faithunits: rman_manage_region: " 134 "Failed %d\n", FAITHNAME, err); 135 rman_fini(faithunits); 136 return (err); 137 } 138 LIST_INIT(&faith_softc_list); 139 if_clone_attach(&faith_cloner); 140 141#ifdef INET6 142 faithprefix_p = faithprefix; 143#endif 144 145 break; 146 case MOD_UNLOAD: 147#ifdef INET6 148 faithprefix_p = NULL; 149#endif 150 151 if_clone_detach(&faith_cloner); 152 153 while (!LIST_EMPTY(&faith_softc_list)) 154 faith_clone_destroy( 155 &LIST_FIRST(&faith_softc_list)->sc_if); 156 157 err = rman_fini(faithunits); 158 if (err != 0) 159 return (err); 160 161 break; 162 } 163 return 0; 164} 165 166static moduledata_t faith_mod = { 167 "if_faith", 168 faithmodevent, 169 0 170}; 171 172DECLARE_MODULE(if_faith, faith_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 173MODULE_VERSION(if_faith, 1); 174 175int 176faith_clone_create(ifc, unit) 177 struct if_clone *ifc; 178 int *unit; 179{ 180 struct resource *r; 181 struct faith_softc *sc; 182 183 if (*unit > FAITH_MAXUNIT) 184 return (ENXIO); 185 186 if (*unit < 0) { 187 r = rman_reserve_resource(faithunits, 0, FAITH_MAXUNIT, 1, 188 RF_ALLOCATED | RF_ACTIVE, NULL); 189 if (r == NULL) 190 return (ENOSPC); 191 *unit = rman_get_start(r); 192 } else { 193 r = rman_reserve_resource(faithunits, *unit, *unit, 1, 194 RF_ALLOCATED | RF_ACTIVE, NULL); 195 if (r == NULL) 196 return (ENOSPC); 197 } 198 199 sc = malloc(sizeof(struct faith_softc), M_FAITH, M_WAITOK); 200 bzero(sc, sizeof(struct faith_softc)); 201 202 sc->sc_if.if_softc = sc; 203 sc->sc_if.if_name = FAITHNAME; 204 sc->sc_if.if_unit = *unit; 205 sc->r_unit = r; 206 207 sc->sc_if.if_mtu = FAITHMTU; 208 /* Change to BROADCAST experimentaly to announce its prefix. */ 209 sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST; 210 sc->sc_if.if_ioctl = faithioctl; 211 sc->sc_if.if_output = faithoutput; 212 sc->sc_if.if_type = IFT_FAITH; 213 sc->sc_if.if_hdrlen = 0; 214 sc->sc_if.if_addrlen = 0; 215 if_attach(&sc->sc_if); 216 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int)); 217 LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list); 218 return (0); 219} 220 221void 222faith_clone_destroy(ifp) 223 struct ifnet *ifp; 224{ 225 int err; 226 struct faith_softc *sc = (void *) ifp; 227 228 LIST_REMOVE(sc, sc_list); 229 bpfdetach(ifp); 230 if_detach(ifp); 231 232 err = rman_release_resource(sc->r_unit); 233 KASSERT(err == 0, ("Unexpected error freeing resource")); 234 235 free(sc, M_FAITH); 236} 237 238int 239faithoutput(ifp, m, dst, rt) 240 struct ifnet *ifp; 241 struct mbuf *m; 242 struct sockaddr *dst; 243 struct rtentry *rt; 244{ 245 int isr; 246 struct ifqueue *ifq = 0; 247 248 if ((m->m_flags & M_PKTHDR) == 0) 249 panic("faithoutput no HDR"); 250 251 /* BPF write needs to be handled specially */ 252 if (dst->sa_family == AF_UNSPEC) { 253 dst->sa_family = *(mtod(m, int *)); 254 m->m_len -= sizeof(int); 255 m->m_pkthdr.len -= sizeof(int); 256 m->m_data += sizeof(int); 257 } 258 259 if (ifp->if_bpf) { 260 /* 261 * We need to prepend the address family as 262 * a four byte field. Cons up a faith header 263 * to pacify bpf. This is safe because bpf 264 * will only read from the mbuf (i.e., it won't 265 * try to free it or keep a pointer a to it). 266 */ 267 struct mbuf m0; 268 u_int32_t af = dst->sa_family; 269 270 m0.m_next = m; 271 m0.m_len = 4; 272 m0.m_data = (char *)⁡ 273 274 bpf_mtap(ifp, &m0); 275 } 276 277 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 278 m_freem(m); 279 return (rt->rt_flags & RTF_BLACKHOLE ? 0 : 280 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 281 } 282 ifp->if_opackets++; 283 ifp->if_obytes += m->m_pkthdr.len; 284 switch (dst->sa_family) { 285#ifdef INET 286 case AF_INET: 287 ifq = &ipintrq; 288 isr = NETISR_IP; 289 break; 290#endif 291#ifdef INET6 292 case AF_INET6: 293 ifq = &ip6intrq; 294 isr = NETISR_IPV6; 295 break; 296#endif 297 default: 298 m_freem(m); 299 return EAFNOSUPPORT; 300 } 301 302 /* XXX do we need more sanity checks? */ 303 304 m->m_pkthdr.rcvif = ifp; 305 ifp->if_ipackets++; 306 ifp->if_ibytes += m->m_pkthdr.len; 307 (void) IF_HANDOFF(ifq, m, NULL); 308 schednetisr(isr); 309 return (0); 310} 311 312/* ARGSUSED */ 313static void
|
314faithrtrequest(cmd, rt, sa)
|
314faithrtrequest(cmd, rt, info) |
315 int cmd; 316 struct rtentry *rt;
|
317 struct sockaddr *sa;
|
317 struct rt_addrinfo *info; |
318{ 319 if (rt) { 320 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */ 321 /* 322 * For optimal performance, the send and receive buffers 323 * should be at least twice the MTU plus a little more for 324 * overhead. 325 */ 326 rt->rt_rmx.rmx_recvpipe = 327 rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU; 328 } 329} 330 331/* 332 * Process an ioctl request. 333 */ 334/* ARGSUSED */ 335static int 336faithioctl(ifp, cmd, data) 337 struct ifnet *ifp; 338 u_long cmd; 339 caddr_t data; 340{ 341 struct ifaddr *ifa; 342 struct ifreq *ifr = (struct ifreq *)data; 343 int error = 0; 344 345 switch (cmd) { 346 347 case SIOCSIFADDR: 348 ifp->if_flags |= IFF_UP | IFF_RUNNING; 349 ifa = (struct ifaddr *)data; 350 ifa->ifa_rtrequest = faithrtrequest; 351 /* 352 * Everything else is done at a higher level. 353 */ 354 break; 355 356 case SIOCADDMULTI: 357 case SIOCDELMULTI: 358 if (ifr == 0) { 359 error = EAFNOSUPPORT; /* XXX */ 360 break; 361 } 362 switch (ifr->ifr_addr.sa_family) { 363#ifdef INET 364 case AF_INET: 365 break; 366#endif 367#ifdef INET6 368 case AF_INET6: 369 break; 370#endif 371 372 default: 373 error = EAFNOSUPPORT; 374 break; 375 } 376 break; 377 378#ifdef SIOCSIFMTU 379 case SIOCSIFMTU: 380 ifp->if_mtu = ifr->ifr_mtu; 381 break; 382#endif 383 384 case SIOCSIFFLAGS: 385 break; 386 387 default: 388 error = EINVAL; 389 } 390 return (error); 391} 392 393#ifdef INET6 394/* 395 * XXX could be slow 396 * XXX could be layer violation to call sys/net from sys/netinet6 397 */ 398static int 399faithprefix(in6) 400 struct in6_addr *in6; 401{ 402 struct rtentry *rt; 403 struct sockaddr_in6 sin6; 404 int ret; 405 406 if (ip6_keepfaith == 0) 407 return 0; 408 409 bzero(&sin6, sizeof(sin6)); 410 sin6.sin6_family = AF_INET6; 411 sin6.sin6_len = sizeof(struct sockaddr_in6); 412 sin6.sin6_addr = *in6; 413 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 414 if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH && 415 (rt->rt_ifp->if_flags & IFF_UP) != 0) 416 ret = 1; 417 else 418 ret = 0; 419 if (rt) 420 RTFREE(rt); 421 return ret; 422} 423#endif
|