Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:28:47 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 "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 #include <sys/rman.h>
66
67 #include <net/if.h>
68 #include <net/if_types.h>
69 #include <net/netisr.h>
70 #include <net/route.h>
71 #include <net/bpf.h>
72
73 #ifdef  INET
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip.h>
78 #endif
79
80 #ifdef INET6
81 #ifndef INET
82 #include <netinet/in.h>
83 #endif
84 #include <netinet6/in6_var.h>
85 #include <netinet/ip6.h>
86 #include <netinet6/ip6_var.h>
87 #endif
88
89 #include <net/net_osdep.h>
90
91 #define FAITHNAME       "faith"
92 #define FAITH_MAXUNIT   0x7fff  /* ifp->if_unit is only 15 bits */
93
94 struct faith_softc {
95         struct ifnet sc_if;     /* must be first */
96         struct resource *r_unit;
97         LIST_ENTRY(faith_softc) sc_list;
98 };
99
100 static int faithioctl __P((struct ifnet *, u_long, caddr_t));
101 int faithoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
102         struct rtentry *));
103 static void faithrtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
104 static int faithprefix __P((struct in6_addr *));
105
106 static int faithmodevent __P((module_t, int, void *));
107
108 static MALLOC_DEFINE(M_FAITH, FAITHNAME, "Firewall Assisted Tunnel Interface");
109 static struct rman faithunits[1];
110 LIST_HEAD(, faith_softc) faith_softc_list;
111
112 int     faith_clone_create __P((struct if_clone *, int *));
113 void    faith_clone_destroy __P((struct ifnet *));
114
115 struct if_clone faith_cloner =
116     IF_CLONE_INITIALIZER(FAITHNAME, faith_clone_create, faith_clone_destroy);
117
118 #define FAITHMTU        1500
119
120 static int
121 faithmodevent(mod, type, data)
122         module_t mod;
123         int type;
124         void *data;
125 {
126         int i;
127         int err;
128
129         switch (type) {
130         case MOD_LOAD:
131                 faithunits->rm_type = RMAN_ARRAY;
132                 faithunits->rm_descr = "configurable if_faith units";
133                 err = rman_init(faithunits);
134                 if (err != 0)
135                         return (err);
136                 err = rman_manage_region(faithunits, 0, FAITH_MAXUNIT);
137                 if (err != 0) {
138                         printf("%s: faithunits: rman_manage_region: "
139                             "Failed %d\n", FAITHNAME, err);
140                         rman_fini(faithunits);
141                         return (err);
142                 }
143                 LIST_INIT(&faith_softc_list);
144                 if_clone_attach(&faith_cloner);
145                 for(i = 0; i < NFAITH; i ++) {
146                         err = faith_clone_create(&faith_cloner, &i);
147                         KASSERT(err == 0,
148                             ("Error creating initial faith interfaces"));
149                 }
150
151 #ifdef INET6
152                 faithprefix_p = faithprefix;
153 #endif
154
155                 break;
156         case MOD_UNLOAD:
157 #ifdef INET6
158                 faithprefix_p = NULL;
159 #endif
160
161                 if_clone_detach(&faith_cloner);
162
163                 while (!LIST_EMPTY(&faith_softc_list))
164                         faith_clone_destroy(
165                             &LIST_FIRST(&faith_softc_list)->sc_if);
166
167                 err = rman_fini(faithunits);
168                 if (err != 0)
169                         return (err);
170
171                 break;
172         }
173         return 0;
174 }
175
176 static moduledata_t faith_mod = {
177         "if_faith",
178         faithmodevent,
179         0
180 };
181
182 DECLARE_MODULE(if_faith, faith_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
183 MODULE_VERSION(if_faith, 1);
184
185 int
186 faith_clone_create(ifc, unit)
187         struct if_clone *ifc;
188         int *unit;
189 {
190         struct resource *r;
191         struct faith_softc *sc;
192
193         if (*unit > FAITH_MAXUNIT)
194                 return (ENXIO);
195
196         if (*unit < 0) {
197                 r = rman_reserve_resource(faithunits, 0, FAITH_MAXUNIT, 1,
198                     RF_ALLOCATED | RF_ACTIVE, NULL);
199                 if (r == NULL)
200                         return (ENOSPC);
201                 *unit = rman_get_start(r);
202         } else {
203                 r = rman_reserve_resource(faithunits, *unit, *unit, 1,
204                     RF_ALLOCATED | RF_ACTIVE, NULL);
205                 if (r == NULL)
206                         return (ENOSPC);
207         }
208
209         sc = malloc(sizeof(struct faith_softc), M_FAITH, M_WAITOK);
210         bzero(sc, sizeof(struct faith_softc));
211
212         sc->sc_if.if_softc = sc;
213         sc->sc_if.if_name = FAITHNAME;
214         sc->sc_if.if_unit = *unit;
215         sc->r_unit = r;
216
217         sc->sc_if.if_mtu = FAITHMTU;
218         /* Change to BROADCAST experimentaly to announce its prefix. */
219         sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
220         sc->sc_if.if_ioctl = faithioctl;
221         sc->sc_if.if_output = faithoutput;
222         sc->sc_if.if_type = IFT_FAITH;
223         sc->sc_if.if_hdrlen = 0;
224         sc->sc_if.if_addrlen = 0;
225         sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
226         if_attach(&sc->sc_if);
227         bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
228         LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
229         return (0);
230 }
231
232 void
233 faith_clone_destroy(ifp)
234         struct ifnet *ifp;
235 {
236         int err;
237         struct faith_softc *sc = (void *) ifp;
238
239         LIST_REMOVE(sc, sc_list);
240         bpfdetach(ifp);
241         if_detach(ifp);
242
243         err = rman_release_resource(sc->r_unit);
244         KASSERT(err == 0, ("Unexpected error freeing resource"));
245
246         free(sc, M_FAITH);
247 }
248
249 int
250 faithoutput(ifp, m, dst, rt)
251         struct ifnet *ifp;
252         struct mbuf *m;
253         struct sockaddr *dst;
254         struct rtentry *rt;
255 {
256         int s, isr;
257         struct ifqueue *ifq = 0;
258
259         if ((m->m_flags & M_PKTHDR) == 0)
260                 panic("faithoutput no HDR");
261
262         /* BPF write needs to be handled specially */
263         if (dst->sa_family == AF_UNSPEC) {
264                 dst->sa_family = *(mtod(m, int *));
265                 m->m_len -= sizeof(int);
266                 m->m_pkthdr.len -= sizeof(int);
267                 m->m_data += sizeof(int);
268         }
269
270         if (ifp->if_bpf) {
271                 /*
272                  * We need to prepend the address family as
273                  * a four byte field.  Cons up a faith header
274                  * to pacify bpf.  This is safe because bpf
275                  * will only read from the mbuf (i.e., it won't
276                  * try to free it or keep a pointer a to it).
277                  */
278                 struct mbuf m0;
279                 u_int32_t af = dst->sa_family;
280
281                 m0.m_next = m;
282                 m0.m_len = 4;
283                 m0.m_data = (char *)&af;
284
285                 bpf_mtap(ifp, &m0);
286         }
287
288         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
289                 m_freem(m);
290                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
291                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
292         }
293         ifp->if_opackets++;
294         ifp->if_obytes += m->m_pkthdr.len;
295         switch (dst->sa_family) {
296 #ifdef INET
297         case AF_INET:
298                 ifq = &ipintrq;
299                 isr = NETISR_IP;
300                 break;
301 #endif
302 #ifdef INET6
303         case AF_INET6:
304                 ifq = &ip6intrq;
305                 isr = NETISR_IPV6;
306                 break;
307 #endif
308         default:
309                 m_freem(m);
310                 return EAFNOSUPPORT;
311         }
312
313         /* XXX do we need more sanity checks? */
314
315         m->m_pkthdr.rcvif = ifp;
316         s = splimp();
317         if (IF_QFULL(ifq)) {
318                 IF_DROP(ifq);
319                 m_freem(m);
320                 splx(s);
321                 return (ENOBUFS);
322         }
323         IF_ENQUEUE(ifq, m);
324         schednetisr(isr);
325         ifp->if_ipackets++;
326         ifp->if_ibytes += m->m_pkthdr.len;
327         splx(s);
328         return (0);
329 }
330
331 /* ARGSUSED */
332 static void
333 faithrtrequest(cmd, rt, info)
334         int cmd;
335         struct rtentry *rt;
336         struct rt_addrinfo *info;
337 {
338         if (rt) {
339                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
340                 /*
341                  * For optimal performance, the send and receive buffers
342                  * should be at least twice the MTU plus a little more for
343                  * overhead.
344                  */
345                 rt->rt_rmx.rmx_recvpipe =
346                         rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
347         }
348 }
349
350 /*
351  * Process an ioctl request.
352  */
353 /* ARGSUSED */
354 static int
355 faithioctl(ifp, cmd, data)
356         struct ifnet *ifp;
357         u_long cmd;
358         caddr_t data;
359 {
360         struct ifaddr *ifa;
361         struct ifreq *ifr = (struct ifreq *)data;
362         int error = 0;
363
364         switch (cmd) {
365
366         case SIOCSIFADDR:
367                 ifp->if_flags |= IFF_UP | IFF_RUNNING;
368                 ifa = (struct ifaddr *)data;
369                 ifa->ifa_rtrequest = faithrtrequest;
370                 /*
371                  * Everything else is done at a higher level.
372                  */
373                 break;
374
375         case SIOCADDMULTI:
376         case SIOCDELMULTI:
377                 if (ifr == 0) {
378                         error = EAFNOSUPPORT;           /* XXX */
379                         break;
380                 }
381                 switch (ifr->ifr_addr.sa_family) {
382 #ifdef INET
383                 case AF_INET:
384                         break;
385 #endif
386 #ifdef INET6
387                 case AF_INET6:
388                         break;
389 #endif
390
391                 default:
392                         error = EAFNOSUPPORT;
393                         break;
394                 }
395                 break;
396
397 #ifdef SIOCSIFMTU
398         case SIOCSIFMTU:
399                 ifp->if_mtu = ifr->ifr_mtu;
400                 break;
401 #endif
402
403         case SIOCSIFFLAGS:
404                 break;
405
406         default:
407                 error = EINVAL;
408         }
409         return (error);
410 }
411
412 #ifdef INET6
413 /*
414  * XXX could be slow
415  * XXX could be layer violation to call sys/net from sys/netinet6
416  */
417 static int
418 faithprefix(in6)
419         struct in6_addr *in6;
420 {
421         struct rtentry *rt;
422         struct sockaddr_in6 sin6;
423         int ret;
424
425         if (ip6_keepfaith == 0)
426                 return 0;
427
428         bzero(&sin6, sizeof(sin6));
429         sin6.sin6_family = AF_INET6;
430         sin6.sin6_len = sizeof(struct sockaddr_in6);
431         sin6.sin6_addr = *in6;
432         rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
433         if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
434             (rt->rt_ifp->if_flags & IFF_UP) != 0)
435                 ret = 1;
436         else
437                 ret = 0;
438         if (rt)
439                 RTFREE(rt);
440         return ret;
441 }
442 #endif