Do a major clean-up of the BUSDMA architecture. A large number of
[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.16 2006/10/25 20:56:02 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/bus.h>
58 #include <sys/socket.h>
59 #include <sys/errno.h>
60 #include <sys/sockio.h>
61 #include <sys/time.h>
62 #include <sys/queue.h>
63 #include <sys/types.h>
64 #include <sys/malloc.h>
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, 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 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(module_t mod, int type, 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(struct if_clone *ifc, int unit)
157 {
158         struct faith_softc *sc;
159
160         sc = kmalloc(sizeof(struct faith_softc), M_FAITH, M_WAITOK);
161         bzero(sc, sizeof(struct faith_softc));
162
163         sc->sc_if.if_softc = sc;
164         if_initname(&(sc->sc_if), FAITHNAME, unit);
165
166         sc->sc_if.if_mtu = FAITHMTU;
167         /* Change to BROADCAST experimentaly to announce its prefix. */
168         sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
169         sc->sc_if.if_ioctl = faithioctl;
170         sc->sc_if.if_output = faithoutput;
171         sc->sc_if.if_type = IFT_FAITH;
172         sc->sc_if.if_hdrlen = 0;
173         sc->sc_if.if_addrlen = 0;
174         sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
175         if_attach(&sc->sc_if, NULL);
176         bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
177         LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
178         return (0);
179 }
180
181 void
182 faith_clone_destroy(struct ifnet *ifp)
183 {
184         struct faith_softc *sc = (struct faith_softc *) ifp;
185
186         LIST_REMOVE(sc, sc_list);
187         bpfdetach(ifp);
188         if_detach(ifp);
189
190         kfree(sc, M_FAITH);
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                 /*
212                  * We need to prepend the address family as
213                  * a four byte field.
214                  */
215                 uint32_t af = dst->sa_family;
216
217                 bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
218         }
219
220         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
221                 m_freem(m);
222                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
223                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
224         }
225         ifp->if_opackets++;
226         ifp->if_obytes += m->m_pkthdr.len;
227         switch (dst->sa_family) {
228 #ifdef INET
229         case AF_INET:
230                 isr = NETISR_IP;
231                 break;
232 #endif
233 #ifdef INET6
234         case AF_INET6:
235                 isr = NETISR_IPV6;
236                 break;
237 #endif
238         default:
239                 m_freem(m);
240                 return EAFNOSUPPORT;
241         }
242
243         /* XXX do we need more sanity checks? */
244
245         m->m_pkthdr.rcvif = ifp;
246         ifp->if_ipackets++;
247         ifp->if_ibytes += m->m_pkthdr.len;
248         netisr_dispatch(isr, m);
249         return (0);
250 }
251
252 /* ARGSUSED */
253 static void
254 faithrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
255 {
256         if (rt) {
257                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
258                 /*
259                  * For optimal performance, the send and receive buffers
260                  * should be at least twice the MTU plus a little more for
261                  * overhead.
262                  */
263                 rt->rt_rmx.rmx_recvpipe =
264                         rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
265         }
266 }
267
268 /*
269  * Process an ioctl request.
270  */
271 /* ARGSUSED */
272 static int
273 faithioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
274 {
275         struct ifaddr *ifa;
276         struct ifreq *ifr = (struct ifreq *)data;
277         int error = 0;
278
279         switch (cmd) {
280
281         case SIOCSIFADDR:
282                 ifp->if_flags |= IFF_UP | IFF_RUNNING;
283                 ifa = (struct ifaddr *)data;
284                 ifa->ifa_rtrequest = faithrtrequest;
285                 /*
286                  * Everything else is done at a higher level.
287                  */
288                 break;
289
290         case SIOCADDMULTI:
291         case SIOCDELMULTI:
292                 if (ifr == 0) {
293                         error = EAFNOSUPPORT;           /* XXX */
294                         break;
295                 }
296                 switch (ifr->ifr_addr.sa_family) {
297 #ifdef INET
298                 case AF_INET:
299                         break;
300 #endif
301 #ifdef INET6
302                 case AF_INET6:
303                         break;
304 #endif
305
306                 default:
307                         error = EAFNOSUPPORT;
308                         break;
309                 }
310                 break;
311
312 #ifdef SIOCSIFMTU
313         case SIOCSIFMTU:
314                 ifp->if_mtu = ifr->ifr_mtu;
315                 break;
316 #endif
317
318         case SIOCSIFFLAGS:
319                 break;
320
321         default:
322                 error = EINVAL;
323         }
324         return (error);
325 }
326
327 #ifdef INET6
328 /*
329  * XXX could be slow
330  * XXX could be layer violation to call sys/net from sys/netinet6
331  */
332 static int
333 faithprefix(struct in6_addr *in6)
334 {
335         struct rtentry *rt;
336         struct sockaddr_in6 sin6;
337         int ret;
338
339         if (ip6_keepfaith == 0)
340                 return 0;
341
342         bzero(&sin6, sizeof sin6);
343         sin6.sin6_family = AF_INET6;
344         sin6.sin6_len = sizeof(struct sockaddr_in6);
345         sin6.sin6_addr = *in6;
346         rt = rtpurelookup((struct sockaddr *)&sin6);
347         if (rt != NULL && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
348             (rt->rt_ifp->if_flags & IFF_UP))
349                 ret = 1;
350         else
351                 ret = 0;
352         if (rt != NULL)
353                 RTFREE(rt);
354         return ret;
355 }
356 #endif