bpf: Use bpf global token instead mplock to protect bpf stuffs
[dragonfly.git] / sys / net / faith / 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: src/sys/net/if_faith.c,v 1.3.2.6 2002/04/28 05:40:25 suz Exp $
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 #ifndef NFAITH
47 #include "use_faith.h"
48 #endif
49 #include "opt_inet.h"
50 #include "opt_inet6.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/mbuf.h>
56 #include <sys/bus.h>
57 #include <sys/socket.h>
58 #include <sys/errno.h>
59 #include <sys/sockio.h>
60 #include <sys/time.h>
61 #include <sys/queue.h>
62 #include <sys/types.h>
63 #include <sys/malloc.h>
64
65 #include <net/if.h>
66 #include <net/if_types.h>
67 #include <net/netisr.h>
68 #include <net/route.h>
69 #include <net/bpf.h>
70 #include <net/if_clone.h>
71
72 #ifdef  INET
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in_var.h>
76 #include <netinet/ip.h>
77 #endif
78
79 #ifdef INET6
80 #ifndef INET
81 #include <netinet/in.h>
82 #endif
83 #include <netinet6/in6_var.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/ip6_var.h>
86 #endif
87
88 #include <net/net_osdep.h>
89
90 #define FAITHNAME       "faith"
91
92 struct faith_softc {
93         struct ifnet sc_if;     /* must be first */
94         LIST_ENTRY(faith_softc) sc_list;
95 };
96
97 static int faithioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
98 int faithoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
99         struct rtentry *);
100 static void faithrtrequest (int, struct rtentry *, struct rt_addrinfo *);
101 #ifdef INET6
102 static int faithprefix (struct in6_addr *);
103 #endif
104
105 static int faithmodevent (module_t, int, void *);
106
107 static MALLOC_DEFINE(M_FAITH, FAITHNAME, "Firewall Assisted Tunnel Interface");
108 LIST_HEAD(, faith_softc) faith_softc_list;
109
110 int     faith_clone_create (struct if_clone *, int, caddr_t);
111 int     faith_clone_destroy (struct ifnet *);
112
113 struct if_clone faith_cloner = IF_CLONE_INITIALIZER(FAITHNAME,
114     faith_clone_create, faith_clone_destroy, NFAITH, IF_MAXUNIT);
115
116 #define FAITHMTU        1500
117
118 static int
119 faithmodevent(module_t mod, int type, void *data)
120 {
121
122         switch (type) {
123         case MOD_LOAD:
124                 LIST_INIT(&faith_softc_list);
125                 if_clone_attach(&faith_cloner);
126
127 #ifdef INET6
128                 faithprefix_p = faithprefix;
129 #endif
130
131                 break;
132         case MOD_UNLOAD:
133 #ifdef INET6
134                 faithprefix_p = NULL;
135 #endif
136
137                 if_clone_detach(&faith_cloner);
138
139                 while (!LIST_EMPTY(&faith_softc_list))
140                         faith_clone_destroy(
141                             &LIST_FIRST(&faith_softc_list)->sc_if);
142
143                 break;
144         }
145         return 0;
146 }
147
148 static moduledata_t faith_mod = {
149         "if_faith",
150         faithmodevent,
151         0
152 };
153
154 DECLARE_MODULE(if_faith, faith_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
155 MODULE_VERSION(if_faith, 1);
156
157 int
158 faith_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
159 {
160         struct faith_softc *sc;
161
162         sc = kmalloc(sizeof(struct faith_softc), M_FAITH, M_WAITOK | M_ZERO);
163
164         sc->sc_if.if_softc = sc;
165         if_initname(&(sc->sc_if), FAITHNAME, unit);
166
167         sc->sc_if.if_mtu = FAITHMTU;
168         /* Change to BROADCAST experimentaly to announce its prefix. */
169         sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
170         sc->sc_if.if_ioctl = faithioctl;
171         sc->sc_if.if_output = faithoutput;
172         sc->sc_if.if_type = IFT_FAITH;
173         sc->sc_if.if_hdrlen = 0;
174         sc->sc_if.if_addrlen = 0;
175         sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
176         if_attach(&sc->sc_if, NULL);
177         bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
178         LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
179         return (0);
180 }
181
182 int
183 faith_clone_destroy(struct ifnet *ifp)
184 {
185         struct faith_softc *sc = (struct faith_softc *) ifp;
186
187         LIST_REMOVE(sc, sc_list);
188         bpfdetach(ifp);
189         if_detach(ifp);
190
191         kfree(sc, M_FAITH);
192
193         return 0;
194 }
195
196 int
197 faithoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
198             struct rtentry *rt)
199 {
200         int isr;
201
202         if ((m->m_flags & M_PKTHDR) == 0)
203                 panic("faithoutput no HDR");
204
205         /* BPF write needs to be handled specially */
206         if (dst->sa_family == AF_UNSPEC) {
207                 dst->sa_family = *(mtod(m, int *));
208                 m->m_len -= sizeof(int);
209                 m->m_pkthdr.len -= sizeof(int);
210                 m->m_data += sizeof(int);
211         }
212
213         if (ifp->if_bpf != NULL) {
214                 bpf_gettoken();
215                 if (ifp->if_bpf != NULL) {
216                         /*
217                          * We need to prepend the address family as
218                          * a four byte field.
219                          */
220                         uint32_t af = dst->sa_family;
221
222                         bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
223                 }
224                 bpf_reltoken();
225         }
226
227         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
228                 m_freem(m);
229                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
230                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
231         }
232         ifp->if_opackets++;
233         ifp->if_obytes += m->m_pkthdr.len;
234         switch (dst->sa_family) {
235 #ifdef INET
236         case AF_INET:
237                 isr = NETISR_IP;
238                 break;
239 #endif
240 #ifdef INET6
241         case AF_INET6:
242                 isr = NETISR_IPV6;
243                 break;
244 #endif
245         default:
246                 m_freem(m);
247                 return EAFNOSUPPORT;
248         }
249
250         /* XXX do we need more sanity checks? */
251
252         m->m_pkthdr.rcvif = ifp;
253         m->m_flags &= ~M_HASH;
254         ifp->if_ipackets++;
255         ifp->if_ibytes += m->m_pkthdr.len;
256         netisr_queue(isr, m);
257         return (0);
258 }
259
260 /* ARGSUSED */
261 static void
262 faithrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
263 {
264         if (rt) {
265                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
266                 /*
267                  * For optimal performance, the send and receive buffers
268                  * should be at least twice the MTU plus a little more for
269                  * overhead.
270                  */
271                 rt->rt_rmx.rmx_recvpipe =
272                         rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
273         }
274 }
275
276 /*
277  * Process an ioctl request.
278  */
279 /* ARGSUSED */
280 static int
281 faithioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
282 {
283         struct ifaddr *ifa;
284         struct ifreq *ifr = (struct ifreq *)data;
285         int error = 0;
286
287         switch (cmd) {
288
289         case SIOCSIFADDR:
290                 ifp->if_flags |= IFF_UP | IFF_RUNNING;
291                 ifa = (struct ifaddr *)data;
292                 ifa->ifa_rtrequest = faithrtrequest;
293                 /*
294                  * Everything else is done at a higher level.
295                  */
296                 break;
297
298         case SIOCADDMULTI:
299         case SIOCDELMULTI:
300                 if (ifr == NULL) {
301                         error = EAFNOSUPPORT;           /* XXX */
302                         break;
303                 }
304                 switch (ifr->ifr_addr.sa_family) {
305 #ifdef INET
306                 case AF_INET:
307                         break;
308 #endif
309 #ifdef INET6
310                 case AF_INET6:
311                         break;
312 #endif
313
314                 default:
315                         error = EAFNOSUPPORT;
316                         break;
317                 }
318                 break;
319
320 #ifdef SIOCSIFMTU
321         case SIOCSIFMTU:
322                 ifp->if_mtu = ifr->ifr_mtu;
323                 break;
324 #endif
325
326         case SIOCSIFFLAGS:
327                 break;
328
329         default:
330                 error = EINVAL;
331         }
332         return (error);
333 }
334
335 #ifdef INET6
336 /*
337  * XXX could be slow
338  * XXX could be layer violation to call sys/net from sys/netinet6
339  */
340 static int
341 faithprefix(struct in6_addr *in6)
342 {
343         struct rtentry *rt;
344         struct sockaddr_in6 sin6;
345         int ret;
346
347         if (ip6_keepfaith == 0)
348                 return 0;
349
350         bzero(&sin6, sizeof sin6);
351         sin6.sin6_family = AF_INET6;
352         sin6.sin6_len = sizeof(struct sockaddr_in6);
353         sin6.sin6_addr = *in6;
354         rt = rtpurelookup((struct sockaddr *)&sin6);
355         if (rt != NULL && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
356             (rt->rt_ifp->if_flags & IFF_UP))
357                 ret = 1;
358         else
359                 ret = 0;
360         if (rt != NULL)
361                 RTFREE(rt);
362         return ret;
363 }
364 #endif