]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_faith.c
MFV of 255902, tzdata2013f
[FreeBSD/FreeBSD.git] / sys / net / 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  * 4. 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$
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 #include "opt_inet.h"
43 #include "opt_inet6.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/mbuf.h>
49 #include <sys/module.h>
50 #include <sys/socket.h>
51 #include <sys/errno.h>
52 #include <sys/sockio.h>
53 #include <sys/time.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/malloc.h>
57
58 #include <net/if.h>
59 #include <net/if_var.h>
60 #include <net/if_clone.h>
61 #include <net/if_types.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
64 #include <net/bpf.h>
65 #include <net/vnet.h>
66
67 #ifdef  INET
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip.h>
72 #endif
73
74 #ifdef INET6
75 #ifndef INET
76 #include <netinet/in.h>
77 #endif
78 #include <netinet6/in6_var.h>
79 #include <netinet/ip6.h>
80 #include <netinet6/ip6_var.h>
81 #endif
82
83 struct faith_softc {
84         struct ifnet *sc_ifp;
85 };
86
87 static int faithioctl(struct ifnet *, u_long, caddr_t);
88 static int faithoutput(struct ifnet *, struct mbuf *, const struct sockaddr *,
89         struct route *);
90 static void faithrtrequest(int, struct rtentry *, struct rt_addrinfo *);
91 #ifdef INET6
92 static int faithprefix(struct in6_addr *);
93 #endif
94
95 static int faithmodevent(module_t, int, void *);
96
97 static const char faithname[] = "faith";
98 static MALLOC_DEFINE(M_FAITH, faithname, "Firewall Assisted Tunnel Interface");
99
100 static int      faith_clone_create(struct if_clone *, int, caddr_t);
101 static void     faith_clone_destroy(struct ifnet *);
102 static struct if_clone *faith_cloner;
103
104 #define FAITHMTU        1500
105
106 static int
107 faithmodevent(mod, type, data)
108         module_t mod;
109         int type;
110         void *data;
111 {
112
113         switch (type) {
114         case MOD_LOAD:
115                 faith_cloner = if_clone_simple(faithname, faith_clone_create,
116                     faith_clone_destroy, 0);
117 #ifdef INET6
118                 faithprefix_p = faithprefix;
119 #endif
120
121                 break;
122         case MOD_UNLOAD:
123 #ifdef INET6
124                 faithprefix_p = NULL;
125 #endif
126
127                 if_clone_detach(faith_cloner);
128                 break;
129         default:
130                 return EOPNOTSUPP;
131         }
132         return 0;
133 }
134
135 static moduledata_t faith_mod = {
136         "if_faith",
137         faithmodevent,
138         0
139 };
140
141 DECLARE_MODULE(if_faith, faith_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
142 MODULE_VERSION(if_faith, 1);
143
144 static int
145 faith_clone_create(ifc, unit, params)
146         struct if_clone *ifc;
147         int unit;
148         caddr_t params;
149 {
150         struct ifnet *ifp;
151         struct faith_softc *sc;
152
153         sc = malloc(sizeof(struct faith_softc), M_FAITH, M_WAITOK | M_ZERO);
154         ifp = sc->sc_ifp = if_alloc(IFT_FAITH);
155         if (ifp == NULL) {
156                 free(sc, M_FAITH);
157                 return (ENOSPC);
158         }
159
160         ifp->if_softc = sc;
161         if_initname(sc->sc_ifp, faithname, unit);
162
163         ifp->if_mtu = FAITHMTU;
164         /* Change to BROADCAST experimentaly to announce its prefix. */
165         ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
166         ifp->if_ioctl = faithioctl;
167         ifp->if_output = faithoutput;
168         ifp->if_hdrlen = 0;
169         ifp->if_addrlen = 0;
170         ifp->if_snd.ifq_maxlen = ifqmaxlen;
171         if_attach(ifp);
172         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
173         return (0);
174 }
175
176 static void
177 faith_clone_destroy(ifp)
178         struct ifnet *ifp;
179 {
180         struct faith_softc *sc = ifp->if_softc;
181
182         bpfdetach(ifp);
183         if_detach(ifp);
184         if_free(ifp);
185         free(sc, M_FAITH);
186 }
187
188 static int
189 faithoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
190         struct route *ro)
191 {
192         int isr;
193         u_int32_t af;
194         struct rtentry *rt = NULL;
195
196         M_ASSERTPKTHDR(m);
197
198         if (ro != NULL)
199                 rt = ro->ro_rt;
200         /* BPF writes need to be handled specially. */
201         if (dst->sa_family == AF_UNSPEC)
202                 bcopy(dst->sa_data, &af, sizeof(af));
203         else
204                 af = dst->sa_family;
205
206         if (bpf_peers_present(ifp->if_bpf))
207                 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
208
209         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
210                 m_freem(m);
211                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
212                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
213         }
214         ifp->if_opackets++;
215         ifp->if_obytes += m->m_pkthdr.len;
216         switch (af) {
217 #ifdef INET
218         case AF_INET:
219                 isr = NETISR_IP;
220                 break;
221 #endif
222 #ifdef INET6
223         case AF_INET6:
224                 isr = NETISR_IPV6;
225                 break;
226 #endif
227         default:
228                 m_freem(m);
229                 return EAFNOSUPPORT;
230         }
231
232         /* XXX do we need more sanity checks? */
233
234         m->m_pkthdr.rcvif = ifp;
235         ifp->if_ipackets++;
236         ifp->if_ibytes += m->m_pkthdr.len;
237         netisr_dispatch(isr, m);
238         return (0);
239 }
240
241 /* ARGSUSED */
242 static void
243 faithrtrequest(cmd, rt, info)
244         int cmd;
245         struct rtentry *rt;
246         struct rt_addrinfo *info;
247 {
248         RT_LOCK_ASSERT(rt);
249         rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
250 }
251
252 /*
253  * Process an ioctl request.
254  */
255 /* ARGSUSED */
256 static int
257 faithioctl(ifp, cmd, data)
258         struct ifnet *ifp;
259         u_long cmd;
260         caddr_t data;
261 {
262         struct ifaddr *ifa;
263         struct ifreq *ifr = (struct ifreq *)data;
264         int error = 0;
265
266         switch (cmd) {
267
268         case SIOCSIFADDR:
269                 ifp->if_flags |= IFF_UP;
270                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
271                 ifa = (struct ifaddr *)data;
272                 ifa->ifa_rtrequest = faithrtrequest;
273                 /*
274                  * Everything else is done at a higher level.
275                  */
276                 break;
277
278         case SIOCADDMULTI:
279         case SIOCDELMULTI:
280                 if (ifr == 0) {
281                         error = EAFNOSUPPORT;           /* XXX */
282                         break;
283                 }
284                 switch (ifr->ifr_addr.sa_family) {
285 #ifdef INET
286                 case AF_INET:
287                         break;
288 #endif
289 #ifdef INET6
290                 case AF_INET6:
291                         break;
292 #endif
293
294                 default:
295                         error = EAFNOSUPPORT;
296                         break;
297                 }
298                 break;
299
300 #ifdef SIOCSIFMTU
301         case SIOCSIFMTU:
302                 ifp->if_mtu = ifr->ifr_mtu;
303                 break;
304 #endif
305
306         case SIOCSIFFLAGS:
307                 break;
308
309         default:
310                 error = EINVAL;
311         }
312         return (error);
313 }
314
315 #ifdef INET6
316 /*
317  * XXX could be slow
318  * XXX could be layer violation to call sys/net from sys/netinet6
319  */
320 static int
321 faithprefix(in6)
322         struct in6_addr *in6;
323 {
324         struct rtentry *rt;
325         struct sockaddr_in6 sin6;
326         int ret;
327
328         if (V_ip6_keepfaith == 0)
329                 return 0;
330
331         bzero(&sin6, sizeof(sin6));
332         sin6.sin6_family = AF_INET6;
333         sin6.sin6_len = sizeof(struct sockaddr_in6);
334         sin6.sin6_addr = *in6;
335         rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL, RT_DEFAULT_FIB);
336         if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
337             (rt->rt_ifp->if_flags & IFF_UP) != 0)
338                 ret = 1;
339         else
340                 ret = 0;
341         if (rt)
342                 RTFREE_LOCKED(rt);
343         return ret;
344 }
345 #endif