if_faith.c revision 1.59 1 /* $NetBSD: if_faith.c,v 1.59 2018/06/26 06:48:02 msaitoh 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.59 2018/06/26 06:48:02 msaitoh Exp $");
44
45 #ifdef _KERNEL_OPT
46 #include "opt_inet.h"
47 #endif
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/mbuf.h>
53 #include <sys/socket.h>
54 #include <sys/errno.h>
55 #include <sys/ioctl.h>
56 #include <sys/time.h>
57 #include <sys/queue.h>
58 #include <sys/device.h>
59 #include <sys/module.h>
60 #include <sys/atomic.h>
61
62 #include <sys/cpu.h>
63
64 #include <net/if.h>
65 #include <net/if_types.h>
66 #include <net/netisr.h>
67 #include <net/route.h>
68 #include <net/bpf.h>
69 #include <net/if_faith.h>
70
71 #ifdef INET
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip.h>
76 #endif
77
78 #ifdef INET6
79 #ifndef INET
80 #include <netinet/in.h>
81 #endif
82 #include <netinet6/in6_var.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_var.h>
85 #endif
86
87 #include "ioconf.h"
88
89 static int faithioctl(struct ifnet *, u_long, void *);
90 static int faithoutput(struct ifnet *, struct mbuf *,
91 const struct sockaddr *, const struct rtentry *);
92 static void faithrtrequest(int, struct rtentry *,
93 const struct rt_addrinfo *);
94
95 static int faith_clone_create(struct if_clone *, int);
96 static int faith_clone_destroy(struct ifnet *);
97
98 static struct if_clone faith_cloner =
99 IF_CLONE_INITIALIZER("faith", faith_clone_create, faith_clone_destroy);
100
101 #define FAITHMTU 1500
102
103 static u_int faith_count;
104
105 /* ARGSUSED */
106 void
107 faithattach(int count)
108 {
109
110 /*
111 * Nothing to do here, initialization is handled by the
112 * module initialization code in faithinit() below).
113 */
114 }
115
116 static void
117 faithinit(void)
118 {
119 if_clone_attach(&faith_cloner);
120 }
121
122 static int
123 faithdetach(void)
124 {
125 int error = 0;
126
127 if (faith_count != 0)
128 error = EBUSY;
129
130 if (error == 0)
131 if_clone_detach(&faith_cloner);
132
133 return error;
134 }
135
136 static int
137 faith_clone_create(struct if_clone *ifc, int unit)
138 {
139 struct ifnet *ifp;
140 int rv;
141
142 ifp = if_alloc(IFT_FAITH);
143
144 if_initname(ifp, ifc->ifc_name, unit);
145
146 ifp->if_mtu = FAITHMTU;
147 /* Change to BROADCAST experimentaly to announce its prefix. */
148 ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
149 ifp->if_ioctl = faithioctl;
150 ifp->if_output = faithoutput;
151 ifp->if_type = IFT_FAITH;
152 ifp->if_hdrlen = 0;
153 ifp->if_addrlen = 0;
154 ifp->if_dlt = DLT_NULL;
155 rv = if_attach(ifp);
156 if (rv != 0) {
157 if_free(ifp);
158 return rv;
159 }
160 if_alloc_sadl(ifp);
161 bpf_attach(ifp, DLT_NULL, sizeof(u_int));
162 atomic_inc_uint(&faith_count);
163 return (0);
164 }
165
166 int
167 faith_clone_destroy(struct ifnet *ifp)
168 {
169
170 bpf_detach(ifp);
171 if_detach(ifp);
172 if_free(ifp);
173
174 atomic_dec_uint(&faith_count);
175 return (0);
176 }
177
178 static int
179 faithoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
180 const struct rtentry *rt)
181 {
182 pktqueue_t *pktq;
183 size_t pktlen;
184 int s, error;
185 uint32_t af;
186
187 if ((m->m_flags & M_PKTHDR) == 0)
188 panic("faithoutput no HDR");
189 af = dst->sa_family;
190 /* BPF write needs to be handled specially */
191 if (af == AF_UNSPEC) {
192 af = *(mtod(m, int *));
193 m_adj(m, sizeof(int));
194 }
195
196 bpf_mtap_af(ifp, af, m, BPF_D_OUT);
197
198 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
199 m_freem(m);
200 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
201 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
202 }
203 pktlen = m->m_pkthdr.len;
204 ifp->if_opackets++;
205 ifp->if_obytes += pktlen;
206 switch (af) {
207 #ifdef INET
208 case AF_INET:
209 pktq = ip_pktq;
210 break;
211 #endif
212 #ifdef INET6
213 case AF_INET6:
214 pktq = ip6_pktq;
215 break;
216 #endif
217 default:
218 m_freem(m);
219 return EAFNOSUPPORT;
220 }
221
222 /* XXX do we need more sanity checks? */
223 KASSERT(pktq != NULL);
224 m_set_rcvif(m, ifp);
225
226 s = splnet();
227 if (__predict_true(pktq_enqueue(pktq, m, 0))) {
228 ifp->if_ipackets++;
229 ifp->if_ibytes += pktlen;
230 error = 0;
231 } else {
232 m_freem(m);
233 error = ENOBUFS;
234 }
235 splx(s);
236
237 return error;
238 }
239
240 /* ARGSUSED */
241 static void
242 faithrtrequest(int cmd, struct rtentry *rt,
243 const struct rt_addrinfo *info)
244 {
245 if (rt)
246 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
247 }
248
249 /*
250 * Process an ioctl request.
251 */
252 /* ARGSUSED */
253 static int
254 faithioctl(struct ifnet *ifp, u_long cmd, void *data)
255 {
256 struct ifaddr *ifa;
257 struct ifreq *ifr = (struct ifreq *)data;
258 int error = 0;
259
260 switch (cmd) {
261
262 case SIOCINITIFADDR:
263 ifa = (struct ifaddr *)data;
264 ifa->ifa_rtrequest = faithrtrequest;
265 ifp->if_flags |= IFF_UP | IFF_RUNNING;
266 /*
267 * Everything else is done at a higher level.
268 */
269 break;
270
271 case SIOCADDMULTI:
272 case SIOCDELMULTI:
273 if (ifr == 0) {
274 error = EAFNOSUPPORT; /* XXX */
275 break;
276 }
277 switch (ifr->ifr_addr.sa_family) {
278 #ifdef INET
279 case AF_INET:
280 break;
281 #endif
282 #ifdef INET6
283 case AF_INET6:
284 break;
285 #endif
286
287 default:
288 error = EAFNOSUPPORT;
289 break;
290 }
291 break;
292
293 default:
294 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
295 error = 0;
296 break;
297 }
298 return (error);
299 }
300
301 #ifdef INET6
302 /*
303 * XXX could be slow
304 * XXX could be layer violation to call sys/net from sys/netinet6
305 */
306 int
307 faithprefix(struct in6_addr *in6)
308 {
309 struct rtentry *rt;
310 struct sockaddr_in6 sin6;
311 int ret;
312
313 if (ip6_keepfaith == 0)
314 return 0;
315
316 memset(&sin6, 0, sizeof(sin6));
317 sin6.sin6_family = AF_INET6;
318 sin6.sin6_len = sizeof(struct sockaddr_in6);
319 sin6.sin6_addr = *in6;
320 rt = rtalloc1((struct sockaddr *)&sin6, 0);
321 if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
322 (rt->rt_ifp->if_flags & IFF_UP) != 0)
323 ret = 1;
324 else
325 ret = 0;
326 if (rt)
327 rt_unref(rt);
328 return ret;
329 }
330 #endif
331
332 /*
333 * Module infrastructure
334 */
335 #include "if_module.h"
336
337 IF_MODULE(MODULE_CLASS_DRIVER, faith, "")
338