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 * 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: releng/5.0/sys/net/if_faith.c 106939 2002-11-15 00:00:15Z sam $
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
61 #include <net/if.h>
62 #include <net/if_types.h>
63 #include <net/netisr.h>
64 #include <net/route.h>
65 #include <net/bpf.h>
66
67 #ifdef INET
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip.h>
72 #endif
73
74 #ifdef INET6
75 #ifndef INET
76 #include <netinet/in.h>
77 #endif
78 #include <netinet6/in6_var.h>
79 #include <netinet/ip6.h>
80 #include <netinet6/ip6_var.h>
81 #endif
82
83 #include <net/net_osdep.h>
84
85 #define FAITHNAME "faith"
86
87 struct faith_softc {
88 struct ifnet sc_if; /* must be first */
89 LIST_ENTRY(faith_softc) sc_list;
90 };
91
92 static int faithioctl(struct ifnet *, u_long, caddr_t);
93 int faithoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
94 struct rtentry *);
95 static void faithrtrequest(int, struct rtentry *, struct rt_addrinfo *);
96 #ifdef INET6
97 static int faithprefix(struct in6_addr *);
98 #endif
99
100 static int faithmodevent(module_t, int, void *);
101
102 static MALLOC_DEFINE(M_FAITH, FAITHNAME, "Firewall Assisted Tunnel Interface");
103 static LIST_HEAD(, faith_softc) faith_softc_list;
104
105 int faith_clone_create(struct if_clone *, int);
106 void faith_clone_destroy(struct ifnet *);
107
108 struct if_clone faith_cloner = IF_CLONE_INITIALIZER(FAITHNAME,
109 faith_clone_create, faith_clone_destroy, 0, IF_MAXUNIT);
110
111 #define FAITHMTU 1500
112
113 static int
114 faithmodevent(mod, type, data)
115 module_t mod;
116 int type;
117 void *data;
118 {
119
120 switch (type) {
121 case MOD_LOAD:
122 LIST_INIT(&faith_softc_list);
123 if_clone_attach(&faith_cloner);
124
125 #ifdef INET6
126 faithprefix_p = faithprefix;
127 #endif
128
129 break;
130 case MOD_UNLOAD:
131 #ifdef INET6
132 faithprefix_p = NULL;
133 #endif
134
135 if_clone_detach(&faith_cloner);
136
137 while (!LIST_EMPTY(&faith_softc_list))
138 faith_clone_destroy(
139 &LIST_FIRST(&faith_softc_list)->sc_if);
140
141 break;
142 }
143 return 0;
144 }
145
146 static moduledata_t faith_mod = {
147 "if_faith",
148 faithmodevent,
149 0
150 };
151
152 DECLARE_MODULE(if_faith, faith_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
153 MODULE_VERSION(if_faith, 1);
154
155 int
156 faith_clone_create(ifc, unit)
157 struct if_clone *ifc;
158 int unit;
159 {
160 struct faith_softc *sc;
161
162 sc = malloc(sizeof(struct faith_softc), M_FAITH, M_WAITOK);
163 bzero(sc, sizeof(struct faith_softc));
164
165 sc->sc_if.if_softc = sc;
166 sc->sc_if.if_name = FAITHNAME;
167 sc->sc_if.if_unit = unit;
168
169 sc->sc_if.if_mtu = FAITHMTU;
170 /* Change to BROADCAST experimentaly to announce its prefix. */
171 sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
172 sc->sc_if.if_ioctl = faithioctl;
173 sc->sc_if.if_output = faithoutput;
174 sc->sc_if.if_type = IFT_FAITH;
175 sc->sc_if.if_hdrlen = 0;
176 sc->sc_if.if_addrlen = 0;
177 sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
178 if_attach(&sc->sc_if);
179 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
180 LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
181 return (0);
182 }
183
184 void
185 faith_clone_destroy(ifp)
186 struct ifnet *ifp;
187 {
188 struct faith_softc *sc = (void *) ifp;
189
190 LIST_REMOVE(sc, sc_list);
191 bpfdetach(ifp);
192 if_detach(ifp);
193
194 free(sc, M_FAITH);
195 }
196
197 int
198 faithoutput(ifp, m, dst, rt)
199 struct ifnet *ifp;
200 struct mbuf *m;
201 struct sockaddr *dst;
202 struct rtentry *rt;
203 {
204 int isr;
205 struct ifqueue *ifq = 0;
206
207 if ((m->m_flags & M_PKTHDR) == 0)
208 panic("faithoutput no HDR");
209
210 /* BPF write needs to be handled specially */
211 if (dst->sa_family == AF_UNSPEC) {
212 dst->sa_family = *(mtod(m, int *));
213 m->m_len -= sizeof(int);
214 m->m_pkthdr.len -= sizeof(int);
215 m->m_data += sizeof(int);
216 }
217
218 if (ifp->if_bpf) {
219 /*
220 * We need to prepend the address family as
221 * a four byte field. Cons up a faith header
222 * to pacify bpf. This is safe because bpf
223 * will only read from the mbuf (i.e., it won't
224 * try to free it or keep a pointer a to it).
225 */
226 struct mbuf m0;
227 u_int32_t af = dst->sa_family;
228
229 m0.m_next = m;
230 m0.m_len = 4;
231 m0.m_data = (char *)⁡
232
233 BPF_MTAP(ifp, &m0);
234 }
235
236 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
237 m_freem(m);
238 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
239 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
240 }
241 ifp->if_opackets++;
242 ifp->if_obytes += m->m_pkthdr.len;
243 switch (dst->sa_family) {
244 #ifdef INET
245 case AF_INET:
246 ifq = &ipintrq;
247 isr = NETISR_IP;
248 break;
249 #endif
250 #ifdef INET6
251 case AF_INET6:
252 ifq = &ip6intrq;
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 (void) IF_HANDOFF(ifq, m, NULL);
267 schednetisr(isr);
268 return (0);
269 }
270
271 /* ARGSUSED */
272 static void
273 faithrtrequest(cmd, rt, info)
274 int cmd;
275 struct rtentry *rt;
276 struct rt_addrinfo *info;
277 {
278 if (rt) {
279 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
280 /*
281 * For optimal performance, the send and receive buffers
282 * should be at least twice the MTU plus a little more for
283 * overhead.
284 */
285 rt->rt_rmx.rmx_recvpipe =
286 rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
287 }
288 }
289
290 /*
291 * Process an ioctl request.
292 */
293 /* ARGSUSED */
294 static int
295 faithioctl(ifp, cmd, data)
296 struct ifnet *ifp;
297 u_long cmd;
298 caddr_t data;
299 {
300 struct ifaddr *ifa;
301 struct ifreq *ifr = (struct ifreq *)data;
302 int error = 0;
303
304 switch (cmd) {
305
306 case SIOCSIFADDR:
307 ifp->if_flags |= IFF_UP | IFF_RUNNING;
308 ifa = (struct ifaddr *)data;
309 ifa->ifa_rtrequest = faithrtrequest;
310 /*
311 * Everything else is done at a higher level.
312 */
313 break;
314
315 case SIOCADDMULTI:
316 case SIOCDELMULTI:
317 if (ifr == 0) {
318 error = EAFNOSUPPORT; /* XXX */
319 break;
320 }
321 switch (ifr->ifr_addr.sa_family) {
322 #ifdef INET
323 case AF_INET:
324 break;
325 #endif
326 #ifdef INET6
327 case AF_INET6:
328 break;
329 #endif
330
331 default:
332 error = EAFNOSUPPORT;
333 break;
334 }
335 break;
336
337 #ifdef SIOCSIFMTU
338 case SIOCSIFMTU:
339 ifp->if_mtu = ifr->ifr_mtu;
340 break;
341 #endif
342
343 case SIOCSIFFLAGS:
344 break;
345
346 default:
347 error = EINVAL;
348 }
349 return (error);
350 }
351
352 #ifdef INET6
353 /*
354 * XXX could be slow
355 * XXX could be layer violation to call sys/net from sys/netinet6
356 */
357 static int
358 faithprefix(in6)
359 struct in6_addr *in6;
360 {
361 struct rtentry *rt;
362 struct sockaddr_in6 sin6;
363 int ret;
364
365 if (ip6_keepfaith == 0)
366 return 0;
367
368 bzero(&sin6, sizeof(sin6));
369 sin6.sin6_family = AF_INET6;
370 sin6.sin6_len = sizeof(struct sockaddr_in6);
371 sin6.sin6_addr = *in6;
372 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
373 if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
374 (rt->rt_ifp->if_flags & IFF_UP) != 0)
375 ret = 1;
376 else
377 ret = 0;
378 if (rt)
379 RTFREE(rt);
380 return ret;
381 }
382 #endif
Cache object: ddc28e49fa307c255e5cfd0375b9f366
|