FreeBSD/Linux Kernel Cross Reference
sys/net/if_faith.c
1 /* $KAME: if_faith.c,v 1.23 2001/12/17 13:55:29 sumikawa 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 * 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.
18 *
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
29 * SUCH DAMAGE.
30 *
31 * $FreeBSD: src/sys/net/if_faith.c,v 1.32.2.1 2005/01/31 23:26:22 imp Exp $
32 */
33 /*
34 * derived from
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
37 */
38
39 /*
40 * Loopback interface driver for protocol testing and timing.
41 */
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/mbuf.h>
49 #include <sys/module.h>
50 #include <sys/socket.h>
51 #include <sys/errno.h>
52 #include <sys/sockio.h>
53 #include <sys/time.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/malloc.h>
57
58 #include <net/if.h>
59 #include <net/if_clone.h>
60 #include <net/if_types.h>
61 #include <net/netisr.h>
62 #include <net/route.h>
63 #include <net/bpf.h>
64
65 #ifdef INET
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip.h>
70 #endif
71
72 #ifdef INET6
73 #ifndef INET
74 #include <netinet/in.h>
75 #endif
76 #include <netinet6/in6_var.h>
77 #include <netinet/ip6.h>
78 #include <netinet6/ip6_var.h>
79 #endif
80
81 #include <net/net_osdep.h>
82
83 #define FAITHNAME "faith"
84
85 struct faith_softc {
86 struct ifnet sc_if; /* must be first */
87 LIST_ENTRY(faith_softc) sc_list;
88 };
89
90 static int faithioctl(struct ifnet *, u_long, caddr_t);
91 int faithoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
92 struct rtentry *);
93 static void faithrtrequest(int, struct rtentry *, struct rt_addrinfo *);
94 #ifdef INET6
95 static int faithprefix(struct in6_addr *);
96 #endif
97
98 static int faithmodevent(module_t, int, void *);
99
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;
103
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 *);
107
108 IFC_SIMPLE_DECLARE(faith, 0);
109
110 #define FAITHMTU 1500
111
112 static int
113 faithmodevent(mod, type, data)
114 module_t mod;
115 int type;
116 void *data;
117 {
118 struct faith_softc *sc;
119
120 switch (type) {
121 case MOD_LOAD:
122 mtx_init(&faith_mtx, "faith_mtx", NULL, MTX_DEF);
123 LIST_INIT(&faith_softc_list);
124 if_clone_attach(&faith_cloner);
125
126 #ifdef INET6
127 faithprefix_p = faithprefix;
128 #endif
129
130 break;
131 case MOD_UNLOAD:
132 #ifdef INET6
133 faithprefix_p = NULL;
134 #endif
135
136 if_clone_detach(&faith_cloner);
137
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);
142 faith_destroy(sc);
143 mtx_lock(&faith_mtx);
144 }
145 mtx_unlock(&faith_mtx);
146 mtx_destroy(&faith_mtx);
147 break;
148 default:
149 return EOPNOTSUPP;
150 }
151 return 0;
152 }
153
154 static moduledata_t faith_mod = {
155 "if_faith",
156 faithmodevent,
157 0
158 };
159
160 DECLARE_MODULE(if_faith, faith_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
161 MODULE_VERSION(if_faith, 1);
162
163 static int
164 faith_clone_create(ifc, unit)
165 struct if_clone *ifc;
166 int unit;
167 {
168 struct faith_softc *sc;
169
170 sc = malloc(sizeof(struct faith_softc), M_FAITH, M_WAITOK | M_ZERO);
171
172 sc->sc_if.if_softc = sc;
173 if_initname(&sc->sc_if, ifc->ifc_name, unit);
174
175 sc->sc_if.if_mtu = FAITHMTU;
176 /* Change to BROADCAST experimentaly to announce its prefix. */
177 sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
178 sc->sc_if.if_ioctl = faithioctl;
179 sc->sc_if.if_output = faithoutput;
180 sc->sc_if.if_type = IFT_FAITH;
181 sc->sc_if.if_hdrlen = 0;
182 sc->sc_if.if_addrlen = 0;
183 sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
184 if_attach(&sc->sc_if);
185 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
186 mtx_lock(&faith_mtx);
187 LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
188 mtx_unlock(&faith_mtx);
189 return (0);
190 }
191
192 static void
193 faith_destroy(struct faith_softc *sc)
194 {
195
196 bpfdetach(&sc->sc_if);
197 if_detach(&sc->sc_if);
198 free(sc, M_FAITH);
199 }
200
201 static void
202 faith_clone_destroy(ifp)
203 struct ifnet *ifp;
204 {
205 struct faith_softc *sc = (void *) ifp;
206
207 mtx_lock(&faith_mtx);
208 LIST_REMOVE(sc, sc_list);
209 mtx_unlock(&faith_mtx);
210
211 faith_destroy(sc);
212 }
213
214 int
215 faithoutput(ifp, m, dst, rt)
216 struct ifnet *ifp;
217 struct mbuf *m;
218 struct sockaddr *dst;
219 struct rtentry *rt;
220 {
221 int isr;
222
223 M_ASSERTPKTHDR(m);
224
225 /* BPF write needs to be handled specially */
226 if (dst->sa_family == AF_UNSPEC) {
227 dst->sa_family = *(mtod(m, int *));
228 m->m_len -= sizeof(int);
229 m->m_pkthdr.len -= sizeof(int);
230 m->m_data += sizeof(int);
231 }
232
233 if (ifp->if_bpf) {
234 u_int32_t af = dst->sa_family;
235 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
236 }
237
238 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
239 m_freem(m);
240 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
241 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
242 }
243 ifp->if_opackets++;
244 ifp->if_obytes += m->m_pkthdr.len;
245 switch (dst->sa_family) {
246 #ifdef INET
247 case AF_INET:
248 isr = NETISR_IP;
249 break;
250 #endif
251 #ifdef INET6
252 case AF_INET6:
253 isr = NETISR_IPV6;
254 break;
255 #endif
256 default:
257 m_freem(m);
258 return EAFNOSUPPORT;
259 }
260
261 /* XXX do we need more sanity checks? */
262
263 m->m_pkthdr.rcvif = ifp;
264 ifp->if_ipackets++;
265 ifp->if_ibytes += m->m_pkthdr.len;
266 netisr_dispatch(isr, m);
267 return (0);
268 }
269
270 /* ARGSUSED */
271 static void
272 faithrtrequest(cmd, rt, info)
273 int cmd;
274 struct rtentry *rt;
275 struct rt_addrinfo *info;
276 {
277 RT_LOCK_ASSERT(rt);
278 if (rt)
279 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
280 }
281
282 /*
283 * Process an ioctl request.
284 */
285 /* ARGSUSED */
286 static int
287 faithioctl(ifp, cmd, data)
288 struct ifnet *ifp;
289 u_long cmd;
290 caddr_t data;
291 {
292 struct ifaddr *ifa;
293 struct ifreq *ifr = (struct ifreq *)data;
294 int error = 0;
295
296 switch (cmd) {
297
298 case SIOCSIFADDR:
299 ifp->if_flags |= IFF_UP | IFF_RUNNING;
300 ifa = (struct ifaddr *)data;
301 ifa->ifa_rtrequest = faithrtrequest;
302 /*
303 * Everything else is done at a higher level.
304 */
305 break;
306
307 case SIOCADDMULTI:
308 case SIOCDELMULTI:
309 if (ifr == 0) {
310 error = EAFNOSUPPORT; /* XXX */
311 break;
312 }
313 switch (ifr->ifr_addr.sa_family) {
314 #ifdef INET
315 case AF_INET:
316 break;
317 #endif
318 #ifdef INET6
319 case AF_INET6:
320 break;
321 #endif
322
323 default:
324 error = EAFNOSUPPORT;
325 break;
326 }
327 break;
328
329 #ifdef SIOCSIFMTU
330 case SIOCSIFMTU:
331 ifp->if_mtu = ifr->ifr_mtu;
332 break;
333 #endif
334
335 case SIOCSIFFLAGS:
336 break;
337
338 default:
339 error = EINVAL;
340 }
341 return (error);
342 }
343
344 #ifdef INET6
345 /*
346 * XXX could be slow
347 * XXX could be layer violation to call sys/net from sys/netinet6
348 */
349 static int
350 faithprefix(in6)
351 struct in6_addr *in6;
352 {
353 struct rtentry *rt;
354 struct sockaddr_in6 sin6;
355 int ret;
356
357 if (ip6_keepfaith == 0)
358 return 0;
359
360 bzero(&sin6, sizeof(sin6));
361 sin6.sin6_family = AF_INET6;
362 sin6.sin6_len = sizeof(struct sockaddr_in6);
363 sin6.sin6_addr = *in6;
364 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
365 if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
366 (rt->rt_ifp->if_flags & IFF_UP) != 0)
367 ret = 1;
368 else
369 ret = 0;
370 if (rt)
371 RTFREE_LOCKED(rt);
372 return ret;
373 }
374 #endif
Cache object: 024162449ba051840ad64bfa6f14fc24
|