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