cd41304aad923b7d0b90a1cd3a3c47dbaeb4c9a1
[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  * $DragonFly: src/sys/net/faith/if_faith.c,v 1.7 2004/01/06 01:40:50 dillon Exp $
37  */
38 /*
39  * derived from
40  *      @(#)if_loop.c   8.1 (Berkeley) 6/10/93
41  * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
42  */
43
44 /*
45  * Loopback interface driver for protocol testing and timing.
46  */
47 #ifndef NFAITH
48 #include "use_faith.h"
49 #endif
50 #include "opt_inet.h"
51 #include "opt_inet6.h"
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/mbuf.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 #include <machine/bus.h>        /* XXX: Shouldn't really be required! */
65
66 #include <net/if.h>
67 #include <net/if_types.h>
68 #include <net/netisr.h>
69 #include <net/route.h>
70 #include <net/bpf.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);
98 int faithoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
99         struct rtentry *);
100 static void faithrtrequest (int, struct rtentry *, struct rt_addrinfo *);
101 static int faithprefix (struct in6_addr *);
102
103 static int faithmodevent (module_t, int, void *);
104
105 static MALLOC_DEFINE(M_FAITH, FAITHNAME, "Firewall Assisted Tunnel Interface");
106 LIST_HEAD(, faith_softc) faith_softc_list;
107
108 int     faith_clone_create (struct if_clone *, int);
109 void    faith_clone_destroy (struct ifnet *);
110
111 struct if_clone faith_cloner = IF_CLONE_INITIALIZER(FAITHNAME,
112     faith_clone_create, faith_clone_destroy, NFAITH, IF_MAXUNIT);
113
114 #define FAITHMTU        1500
115
116 static int
117 faithmodevent(mod, type, data)
118         module_t mod;
119         int type;
120         void *data;
121 {
122
123         switch (type) {
124         case MOD_LOAD:
125                 LIST_INIT(&faith_softc_list);
126                 if_clone_attach(&faith_cloner);
127
128 #ifdef INET6
129                 faithprefix_p = faithprefix;
130 #endif
131
132                 break;
133         case MOD_UNLOAD:
134 #ifdef INET6
135                 faithprefix_p = NULL;
136 #endif
137
138                 if_clone_detach(&faith_cloner);
139
140                 while (!LIST_EMPTY(&faith_softc_list))
141                         faith_clone_destroy(
142                             &LIST_FIRST(&faith_softc_list)->sc_if);
143
144                 break;
145         }
146         return 0;
147 }
148
149 static moduledata_t faith_mod = {
150         "if_faith",
151         faithmodevent,
152         0
153 };
154
155 DECLARE_MODULE(if_faith, faith_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
156 MODULE_VERSION(if_faith, 1);
157
158 int
159 faith_clone_create(ifc, unit)
160         struct if_clone *ifc;
161         int unit;
162 {
163         struct faith_softc *sc;
164
165         sc = malloc(sizeof(struct faith_softc), M_FAITH, M_WAITOK);
166         bzero(sc, sizeof(struct faith_softc));
167
168         sc->sc_if.if_softc = sc;
169         if_initname(&(sc->sc_if), FAITHNAME, unit);
170
171         sc->sc_if.if_mtu = FAITHMTU;
172         /* Change to BROADCAST experimentaly to announce its prefix. */
173         sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
174         sc->sc_if.if_ioctl = faithioctl;
175         sc->sc_if.if_output = faithoutput;
176         sc->sc_if.if_type = IFT_FAITH;
177         sc->sc_if.if_hdrlen = 0;
178         sc->sc_if.if_addrlen = 0;
179         sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
180         if_attach(&sc->sc_if);
181         bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
182         LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
183         return (0);
184 }
185
186 void
187 faith_clone_destroy(ifp)
188         struct ifnet *ifp;
189 {
190         struct faith_softc *sc = (void *) ifp;
191
192         LIST_REMOVE(sc, sc_list);
193         bpfdetach(ifp);
194         if_detach(ifp);
195
196         free(sc, M_FAITH);
197 }
198
199 int
200 faithoutput(ifp, m, dst, rt)
201         struct ifnet *ifp;
202         struct mbuf *m;
203         struct sockaddr *dst;
204         struct rtentry *rt;
205 {
206         int isr;
207
208         if ((m->m_flags & M_PKTHDR) == 0)
209                 panic("faithoutput no HDR");
210
211         /* BPF write needs to be handled specially */
212         if (dst->sa_family == AF_UNSPEC) {
213                 dst->sa_family = *(mtod(m, int *));
214                 m->m_len -= sizeof(int);
215                 m->m_pkthdr.len -= sizeof(int);
216                 m->m_data += sizeof(int);
217         }
218
219         if (ifp->if_bpf) {
220                 /*
221                  * We need to prepend the address family as
222                  * a four byte field.  Cons up a faith header
223                  * to pacify bpf.  This is safe because bpf
224                  * will only read from the mbuf (i.e., it won't
225                  * try to free it or keep a pointer a to it).
226                  */
227                 struct mbuf m0;
228                 u_int32_t af = dst->sa_family;
229
230                 m0.m_next = m;
231                 m0.m_len = 4;
232                 m0.m_data = (char *)&af;
233
234                 bpf_mtap(ifp, &m0);
235         }
236
237         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
238                 m_freem(m);
239                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
240                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
241         }
242         ifp->if_opackets++;
243         ifp->if_obytes += m->m_pkthdr.len;
244         switch (dst->sa_family) {
245 #ifdef INET
246         case AF_INET:
247                 isr = NETISR_IP;
248                 break;
249 #endif
250 #ifdef INET6
251         case AF_INET6:
252                 isr = NETISR_IPV6;
253                 break;
254 #endif
255         default:
256                 m_freem(m);
257                 return EAFNOSUPPORT;
258         }
259
260         /* XXX do we need more sanity checks? */
261
262         m->m_pkthdr.rcvif = ifp;
263         ifp->if_ipackets++;
264         ifp->if_ibytes += m->m_pkthdr.len;
265         netisr_dispatch(isr, m);
266         return (0);
267 }
268
269 /* ARGSUSED */
270 static void
271 faithrtrequest(cmd, rt, info)
272         int cmd;
273         struct rtentry *rt;
274         struct rt_addrinfo *info;
275 {
276         if (rt) {
277                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
278                 /*
279                  * For optimal performance, the send and receive buffers
280                  * should be at least twice the MTU plus a little more for
281                  * overhead.
282                  */
283                 rt->rt_rmx.rmx_recvpipe =
284                         rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
285         }
286 }
287
288 /*
289  * Process an ioctl request.
290  */
291 /* ARGSUSED */
292 static int
293 faithioctl(ifp, cmd, data)
294         struct ifnet *ifp;
295         u_long cmd;
296         caddr_t data;
297 {
298         struct ifaddr *ifa;
299         struct ifreq *ifr = (struct ifreq *)data;
300         int error = 0;
301
302         switch (cmd) {
303
304         case SIOCSIFADDR:
305                 ifp->if_flags |= IFF_UP | IFF_RUNNING;
306                 ifa = (struct ifaddr *)data;
307                 ifa->ifa_rtrequest = faithrtrequest;
308                 /*
309                  * Everything else is done at a higher level.
310                  */
311                 break;
312
313         case SIOCADDMULTI:
314         case SIOCDELMULTI:
315                 if (ifr == 0) {
316                         error = EAFNOSUPPORT;           /* XXX */
317                         break;
318                 }
319                 switch (ifr->ifr_addr.sa_family) {
320 #ifdef INET
321                 case AF_INET:
322                         break;
323 #endif
324 #ifdef INET6
325                 case AF_INET6:
326                         break;
327 #endif
328
329                 default:
330                         error = EAFNOSUPPORT;
331                         break;
332                 }
333                 break;
334
335 #ifdef SIOCSIFMTU
336         case SIOCSIFMTU:
337                 ifp->if_mtu = ifr->ifr_mtu;
338                 break;
339 #endif
340
341         case SIOCSIFFLAGS:
342                 break;
343
344         default:
345                 error = EINVAL;
346         }
347         return (error);
348 }
349
350 #ifdef INET6
351 /*
352  * XXX could be slow
353  * XXX could be layer violation to call sys/net from sys/netinet6
354  */
355 static int
356 faithprefix(in6)
357         struct in6_addr *in6;
358 {
359         struct rtentry *rt;
360         struct sockaddr_in6 sin6;
361         int ret;
362
363         if (ip6_keepfaith == 0)
364                 return 0;
365
366         bzero(&sin6, sizeof(sin6));
367         sin6.sin6_family = AF_INET6;
368         sin6.sin6_len = sizeof(struct sockaddr_in6);
369         sin6.sin6_addr = *in6;
370         rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
371         if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
372             (rt->rt_ifp->if_flags & IFF_UP) != 0)
373                 ret = 1;
374         else
375                 ret = 0;
376         if (rt)
377                 RTFREE(rt);
378         return ret;
379 }
380 #endif