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