1 /* $KAME: if_faith.c,v 1.23 2001/12/17 13:55:29 sumikawa Exp $ */
4 * Copyright (c) 1982, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
36 * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
40 * Loopback interface driver for protocol testing and timing.
43 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/module.h>
50 #include <sys/socket.h>
51 #include <sys/errno.h>
52 #include <sys/sockio.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/malloc.h>
59 #include <net/if_clone.h>
60 #include <net/if_types.h>
61 #include <net/netisr.h>
62 #include <net/route.h>
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip.h>
74 #include <netinet/in.h>
76 #include <netinet6/in6_var.h>
77 #include <netinet/ip6.h>
78 #include <netinet6/ip6_var.h>
81 #include <net/net_osdep.h>
83 #define FAITHNAME "faith"
87 LIST_ENTRY(faith_softc) sc_list;
90 static int faithioctl(struct ifnet *, u_long, caddr_t);
91 int faithoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
93 static void faithrtrequest(int, struct rtentry *, struct rt_addrinfo *);
95 static int faithprefix(struct in6_addr *);
98 static int faithmodevent(module_t, int, void *);
100 static struct mtx faith_mtx;
101 static MALLOC_DEFINE(M_FAITH, FAITHNAME, "Firewall Assisted Tunnel Interface");
102 static LIST_HEAD(, faith_softc) faith_softc_list;
104 static int faith_clone_create(struct if_clone *, int);
105 static void faith_clone_destroy(struct ifnet *);
106 static void faith_destroy(struct faith_softc *);
108 IFC_SIMPLE_DECLARE(faith, 0);
110 #define FAITHMTU 1500
113 faithmodevent(mod, type, data)
118 struct faith_softc *sc;
122 mtx_init(&faith_mtx, "faith_mtx", NULL, MTX_DEF);
123 LIST_INIT(&faith_softc_list);
124 if_clone_attach(&faith_cloner);
127 faithprefix_p = faithprefix;
133 faithprefix_p = NULL;
136 if_clone_detach(&faith_cloner);
138 mtx_lock(&faith_mtx);
139 while ((sc = LIST_FIRST(&faith_softc_list)) != NULL) {
140 LIST_REMOVE(sc, sc_list);
141 mtx_unlock(&faith_mtx);
143 mtx_lock(&faith_mtx);
145 mtx_unlock(&faith_mtx);
146 mtx_destroy(&faith_mtx);
154 static moduledata_t faith_mod = {
160 DECLARE_MODULE(if_faith, faith_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
161 MODULE_VERSION(if_faith, 1);
164 faith_clone_create(ifc, unit)
165 struct if_clone *ifc;
169 struct faith_softc *sc;
171 sc = malloc(sizeof(struct faith_softc), M_FAITH, M_WAITOK | M_ZERO);
172 ifp = sc->sc_ifp = if_alloc(IFT_FAITH);
179 if_initname(sc->sc_ifp, ifc->ifc_name, unit);
181 ifp->if_mtu = FAITHMTU;
182 /* Change to BROADCAST experimentaly to announce its prefix. */
183 ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
184 ifp->if_ioctl = faithioctl;
185 ifp->if_output = faithoutput;
188 ifp->if_snd.ifq_maxlen = ifqmaxlen;
190 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
191 mtx_lock(&faith_mtx);
192 LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
193 mtx_unlock(&faith_mtx);
198 faith_destroy(struct faith_softc *sc)
201 bpfdetach(sc->sc_ifp);
202 if_detach(sc->sc_ifp);
208 faith_clone_destroy(ifp)
211 struct faith_softc *sc = ifp->if_softc;
213 mtx_lock(&faith_mtx);
214 LIST_REMOVE(sc, sc_list);
215 mtx_unlock(&faith_mtx);
221 faithoutput(ifp, m, dst, rt)
224 struct sockaddr *dst;
232 /* BPF writes need to be handled specially. */
233 if (dst->sa_family == AF_UNSPEC) {
234 bcopy(dst->sa_data, &af, sizeof(af));
238 if (bpf_peers_present(ifp->if_bpf)) {
240 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
243 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
245 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
246 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
249 ifp->if_obytes += m->m_pkthdr.len;
250 switch (dst->sa_family) {
266 /* XXX do we need more sanity checks? */
268 m->m_pkthdr.rcvif = ifp;
270 ifp->if_ibytes += m->m_pkthdr.len;
271 netisr_dispatch(isr, m);
277 faithrtrequest(cmd, rt, info)
280 struct rt_addrinfo *info;
283 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
287 * Process an ioctl request.
291 faithioctl(ifp, cmd, data)
297 struct ifreq *ifr = (struct ifreq *)data;
303 ifp->if_flags |= IFF_UP;
304 ifp->if_drv_flags |= IFF_DRV_RUNNING;
305 ifa = (struct ifaddr *)data;
306 ifa->ifa_rtrequest = faithrtrequest;
308 * Everything else is done at a higher level.
315 error = EAFNOSUPPORT; /* XXX */
318 switch (ifr->ifr_addr.sa_family) {
329 error = EAFNOSUPPORT;
336 ifp->if_mtu = ifr->ifr_mtu;
352 * XXX could be layer violation to call sys/net from sys/netinet6
356 struct in6_addr *in6;
359 struct sockaddr_in6 sin6;
362 if (ip6_keepfaith == 0)
365 bzero(&sin6, sizeof(sin6));
366 sin6.sin6_family = AF_INET6;
367 sin6.sin6_len = sizeof(struct sockaddr_in6);
368 sin6.sin6_addr = *in6;
369 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
370 if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
371 (rt->rt_ifp->if_flags & IFF_UP) != 0)