if_faith.c revision 1.43 1 /* $NetBSD: if_faith.c,v 1.43 2008/06/15 18:40:55 christos Exp $ */
2 /* $KAME: if_faith.c,v 1.21 2001/02/20 07:59:26 itojun Exp $ */
3
4 /*
5 * Copyright (c) 1982, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32 /*
33 * derived from
34 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
35 * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
36 */
37
38 /*
39 * IPv6-to-IPv4 TCP relay capturing interface
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: if_faith.c,v 1.43 2008/06/15 18:40:55 christos Exp $");
44
45 #include "opt_inet.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52 #include <sys/errno.h>
53 #include <sys/ioctl.h>
54 #include <sys/time.h>
55 #include <sys/queue.h>
56
57 #include <sys/cpu.h>
58
59 #include <net/if.h>
60 #include <net/if_types.h>
61 #include <net/netisr.h>
62 #include <net/route.h>
63 #include <net/bpf.h>
64 #include <net/if_faith.h>
65
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip.h>
71 #endif
72
73 #ifdef INET6
74 #ifndef INET
75 #include <netinet/in.h>
76 #endif
77 #include <netinet6/in6_var.h>
78 #include <netinet/ip6.h>
79 #include <netinet6/ip6_var.h>
80 #endif
81
82 #include "bpfilter.h"
83
84 #include <net/net_osdep.h>
85
86 static int faithioctl(struct ifnet *, u_long, void *);
87 static int faithoutput(struct ifnet *, struct mbuf *,
88 const struct sockaddr *, struct rtentry *);
89 static void faithrtrequest(int, struct rtentry *, struct rt_addrinfo *);
90
91 void faithattach(int);
92
93 static int faith_clone_create(struct if_clone *, int);
94 static int faith_clone_destroy(struct ifnet *);
95
96 static struct if_clone faith_cloner =
97 IF_CLONE_INITIALIZER("faith", faith_clone_create, faith_clone_destroy);
98
99 #define FAITHMTU 1500
100
101 /* ARGSUSED */
102 void
103 faithattach(int count)
104 {
105
106 if_clone_attach(&faith_cloner);
107 }
108
109 static int
110 faith_clone_create(struct if_clone *ifc, int unit)
111 {
112 struct ifnet *ifp;
113
114 ifp = if_alloc(IFT_FAITH);
115
116 if_initname(ifp, ifc->ifc_name, unit);
117
118 ifp->if_mtu = FAITHMTU;
119 /* Change to BROADCAST experimentaly to announce its prefix. */
120 ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
121 ifp->if_ioctl = faithioctl;
122 ifp->if_output = faithoutput;
123 ifp->if_type = IFT_FAITH;
124 ifp->if_hdrlen = 0;
125 ifp->if_addrlen = 0;
126 ifp->if_dlt = DLT_NULL;
127 if_attach(ifp);
128 if_alloc_sadl(ifp);
129 #if NBPFILTER > 0
130 bpfattach(ifp, DLT_NULL, sizeof(u_int));
131 #endif
132 return (0);
133 }
134
135 int
136 faith_clone_destroy(struct ifnet *ifp)
137 {
138
139 #if NBPFILTER > 0
140 bpfdetach(ifp);
141 #endif
142 if_detach(ifp);
143 free(ifp, M_DEVBUF);
144
145 return (0);
146 }
147
148 static int
149 faithoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
150 struct rtentry *rt)
151 {
152 int s, isr;
153 uint32_t af;
154 struct ifqueue *ifq = 0;
155
156 if ((m->m_flags & M_PKTHDR) == 0)
157 panic("faithoutput no HDR");
158 af = dst->sa_family;
159 #if NBPFILTER > 0
160 /* BPF write needs to be handled specially */
161 if (af == AF_UNSPEC) {
162 af = *(mtod(m, int *));
163 m_adj(m, sizeof(int));
164 }
165
166 if (ifp->if_bpf)
167 bpf_mtap_af(ifp->if_bpf, af, m);
168 #endif
169
170 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
171 m_freem(m);
172 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
173 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
174 }
175 ifp->if_opackets++;
176 ifp->if_obytes += m->m_pkthdr.len;
177 switch (af) {
178 #ifdef INET
179 case AF_INET:
180 ifq = &ipintrq;
181 isr = NETISR_IP;
182 break;
183 #endif
184 #ifdef INET6
185 case AF_INET6:
186 ifq = &ip6intrq;
187 isr = NETISR_IPV6;
188 break;
189 #endif
190 default:
191 m_freem(m);
192 return EAFNOSUPPORT;
193 }
194
195 /* XXX do we need more sanity checks? */
196
197 m->m_pkthdr.rcvif = ifp;
198 s = splnet();
199 if (IF_QFULL(ifq)) {
200 IF_DROP(ifq);
201 m_freem(m);
202 splx(s);
203 return (ENOBUFS);
204 }
205 IF_ENQUEUE(ifq, m);
206 schednetisr(isr);
207 ifp->if_ipackets++;
208 ifp->if_ibytes += m->m_pkthdr.len;
209 splx(s);
210 return (0);
211 }
212
213 /* ARGSUSED */
214 static void
215 faithrtrequest(int cmd, struct rtentry *rt,
216 struct rt_addrinfo *info)
217 {
218 if (rt)
219 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
220 }
221
222 /*
223 * Process an ioctl request.
224 */
225 /* ARGSUSED */
226 static int
227 faithioctl(struct ifnet *ifp, u_long cmd, void *data)
228 {
229 struct ifaddr *ifa;
230 struct ifreq *ifr = (struct ifreq *)data;
231 int error = 0;
232
233 switch (cmd) {
234
235 case SIOCSIFADDR:
236 ifp->if_flags |= IFF_UP | IFF_RUNNING;
237 ifa = (struct ifaddr *)data;
238 ifa->ifa_rtrequest = faithrtrequest;
239 /*
240 * Everything else is done at a higher level.
241 */
242 break;
243
244 case SIOCADDMULTI:
245 case SIOCDELMULTI:
246 if (ifr == 0) {
247 error = EAFNOSUPPORT; /* XXX */
248 break;
249 }
250 switch (ifr->ifr_addr.sa_family) {
251 #ifdef INET
252 case AF_INET:
253 break;
254 #endif
255 #ifdef INET6
256 case AF_INET6:
257 break;
258 #endif
259
260 default:
261 error = EAFNOSUPPORT;
262 break;
263 }
264 break;
265
266 #ifdef SIOCSIFMTU
267 case SIOCSIFMTU:
268 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
269 error = 0;
270 break;
271 #endif
272
273 case SIOCSIFFLAGS:
274 break;
275
276 default:
277 error = EINVAL;
278 }
279 return (error);
280 }
281
282 #ifdef INET6
283 /*
284 * XXX could be slow
285 * XXX could be layer violation to call sys/net from sys/netinet6
286 */
287 int
288 faithprefix(struct in6_addr *in6)
289 {
290 struct rtentry *rt;
291 struct sockaddr_in6 sin6;
292 int ret;
293
294 if (ip6_keepfaith == 0)
295 return 0;
296
297 memset(&sin6, 0, sizeof(sin6));
298 sin6.sin6_family = AF_INET6;
299 sin6.sin6_len = sizeof(struct sockaddr_in6);
300 sin6.sin6_addr = *in6;
301 rt = rtalloc1((struct sockaddr *)&sin6, 0);
302 if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
303 (rt->rt_ifp->if_flags & IFF_UP) != 0)
304 ret = 1;
305 else
306 ret = 0;
307 if (rt)
308 RTFREE(rt);
309 return ret;
310 }
311 #endif
312