if_faith.c revision 1.46 1 /* $NetBSD: if_faith.c,v 1.46 2010/01/19 22:08:00 pooka 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.46 2010/01/19 22:08:00 pooka 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
83 #include <net/net_osdep.h>
84
85 static int faithioctl(struct ifnet *, u_long, void *);
86 static int faithoutput(struct ifnet *, struct mbuf *,
87 const struct sockaddr *, struct rtentry *);
88 static void faithrtrequest(int, struct rtentry *,
89 const 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 bpf_ops->bpf_attach(ifp, DLT_NULL, sizeof(u_int), &ifp->if_bpf);
130 return (0);
131 }
132
133 int
134 faith_clone_destroy(struct ifnet *ifp)
135 {
136
137 bpf_ops->bpf_detach(ifp);
138 if_detach(ifp);
139 free(ifp, M_DEVBUF);
140
141 return (0);
142 }
143
144 static int
145 faithoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
146 struct rtentry *rt)
147 {
148 int s, isr;
149 uint32_t af;
150 struct ifqueue *ifq = 0;
151
152 if ((m->m_flags & M_PKTHDR) == 0)
153 panic("faithoutput no HDR");
154 af = dst->sa_family;
155 /* BPF write needs to be handled specially */
156 if (af == AF_UNSPEC) {
157 af = *(mtod(m, int *));
158 m_adj(m, sizeof(int));
159 }
160
161 if (ifp->if_bpf)
162 bpf_ops->bpf_mtap_af(ifp->if_bpf, af, m);
163
164 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
165 m_freem(m);
166 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
167 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
168 }
169 ifp->if_opackets++;
170 ifp->if_obytes += m->m_pkthdr.len;
171 switch (af) {
172 #ifdef INET
173 case AF_INET:
174 ifq = &ipintrq;
175 isr = NETISR_IP;
176 break;
177 #endif
178 #ifdef INET6
179 case AF_INET6:
180 ifq = &ip6intrq;
181 isr = NETISR_IPV6;
182 break;
183 #endif
184 default:
185 m_freem(m);
186 return EAFNOSUPPORT;
187 }
188
189 /* XXX do we need more sanity checks? */
190
191 m->m_pkthdr.rcvif = ifp;
192 s = splnet();
193 if (IF_QFULL(ifq)) {
194 IF_DROP(ifq);
195 m_freem(m);
196 splx(s);
197 return (ENOBUFS);
198 }
199 IF_ENQUEUE(ifq, m);
200 schednetisr(isr);
201 ifp->if_ipackets++;
202 ifp->if_ibytes += m->m_pkthdr.len;
203 splx(s);
204 return (0);
205 }
206
207 /* ARGSUSED */
208 static void
209 faithrtrequest(int cmd, struct rtentry *rt,
210 const struct rt_addrinfo *info)
211 {
212 if (rt)
213 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
214 }
215
216 /*
217 * Process an ioctl request.
218 */
219 /* ARGSUSED */
220 static int
221 faithioctl(struct ifnet *ifp, u_long cmd, void *data)
222 {
223 struct ifaddr *ifa;
224 struct ifreq *ifr = (struct ifreq *)data;
225 int error = 0;
226
227 switch (cmd) {
228
229 case SIOCINITIFADDR:
230 ifp->if_flags |= IFF_UP | IFF_RUNNING;
231 ifa = (struct ifaddr *)data;
232 ifa->ifa_rtrequest = faithrtrequest;
233 /*
234 * Everything else is done at a higher level.
235 */
236 break;
237
238 case SIOCADDMULTI:
239 case SIOCDELMULTI:
240 if (ifr == 0) {
241 error = EAFNOSUPPORT; /* XXX */
242 break;
243 }
244 switch (ifr->ifr_addr.sa_family) {
245 #ifdef INET
246 case AF_INET:
247 break;
248 #endif
249 #ifdef INET6
250 case AF_INET6:
251 break;
252 #endif
253
254 default:
255 error = EAFNOSUPPORT;
256 break;
257 }
258 break;
259
260 default:
261 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
262 error = 0;
263 break;
264 }
265 return (error);
266 }
267
268 #ifdef INET6
269 /*
270 * XXX could be slow
271 * XXX could be layer violation to call sys/net from sys/netinet6
272 */
273 int
274 faithprefix(struct in6_addr *in6)
275 {
276 struct rtentry *rt;
277 struct sockaddr_in6 sin6;
278 int ret;
279
280 if (ip6_keepfaith == 0)
281 return 0;
282
283 memset(&sin6, 0, sizeof(sin6));
284 sin6.sin6_family = AF_INET6;
285 sin6.sin6_len = sizeof(struct sockaddr_in6);
286 sin6.sin6_addr = *in6;
287 rt = rtalloc1((struct sockaddr *)&sin6, 0);
288 if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
289 (rt->rt_ifp->if_flags & IFF_UP) != 0)
290 ret = 1;
291 else
292 ret = 0;
293 if (rt)
294 RTFREE(rt);
295 return ret;
296 }
297 #endif
298