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