xref: /freebsd/sys/net/if_enc.c (revision 277fbb92d5e4cd0938c67f77b08d9ba4ac9d54a6)
1 /*-
2  * Copyright (c) 2006 The FreeBSD Project.
3  * Copyright (c) 2015 Andrey V. Elsukov <ae@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/module.h>
40 #include <machine/bus.h>
41 #include <sys/rman.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/sysctl.h>
45 
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_clone.h>
49 #include <net/if_types.h>
50 #include <net/pfil.h>
51 #include <net/route.h>
52 #include <net/netisr.h>
53 #include <net/bpf.h>
54 #include <net/vnet.h>
55 
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/in_var.h>
61 
62 #ifdef INET6
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #endif
66 
67 #include <netipsec/ipsec.h>
68 #include <netipsec/xform.h>
69 
70 #define ENCMTU		(1024+512)
71 
72 /* XXX this define must have the same value as in OpenBSD */
73 #define M_CONF		0x0400	/* payload was encrypted (ESP-transport) */
74 #define M_AUTH		0x0800	/* payload was authenticated (AH or ESP auth) */
75 #define M_AUTH_AH	0x2000	/* header was authenticated (AH) */
76 
77 struct enchdr {
78 	u_int32_t af;
79 	u_int32_t spi;
80 	u_int32_t flags;
81 };
82 struct enc_softc {
83 	struct	ifnet *sc_ifp;
84 };
85 static VNET_DEFINE(struct enc_softc *, enc_sc);
86 #define	V_enc_sc	VNET(enc_sc)
87 static VNET_DEFINE(struct if_clone *, enc_cloner);
88 #define	V_enc_cloner	VNET(enc_cloner)
89 
90 static int	enc_ioctl(struct ifnet *, u_long, caddr_t);
91 static int	enc_output(struct ifnet *, struct mbuf *,
92     const struct sockaddr *, struct route *);
93 static int	enc_clone_create(struct if_clone *, int, caddr_t);
94 static void	enc_clone_destroy(struct ifnet *);
95 static int	enc_add_hhooks(struct enc_softc *);
96 static void	enc_remove_hhooks(struct enc_softc *);
97 
98 static const char encname[] = "enc";
99 
100 /*
101  * Before and after are relative to when we are stripping the
102  * outer IP header.
103  */
104 static VNET_DEFINE(int, filter_mask_in) = IPSEC_ENC_BEFORE;
105 static VNET_DEFINE(int, bpf_mask_in) = IPSEC_ENC_BEFORE;
106 static VNET_DEFINE(int, filter_mask_out) = IPSEC_ENC_BEFORE;
107 static VNET_DEFINE(int, bpf_mask_out) = IPSEC_ENC_BEFORE | IPSEC_ENC_AFTER;
108 #define	V_filter_mask_in	VNET(filter_mask_in)
109 #define	V_bpf_mask_in		VNET(bpf_mask_in)
110 #define	V_filter_mask_out	VNET(filter_mask_out)
111 #define	V_bpf_mask_out		VNET(bpf_mask_out)
112 
113 static SYSCTL_NODE(_net, OID_AUTO, enc, CTLFLAG_RW, 0, "enc sysctl");
114 static SYSCTL_NODE(_net_enc, OID_AUTO, in, CTLFLAG_RW, 0, "enc input sysctl");
115 static SYSCTL_NODE(_net_enc, OID_AUTO, out, CTLFLAG_RW, 0, "enc output sysctl");
116 SYSCTL_INT(_net_enc_in, OID_AUTO, ipsec_filter_mask,
117     CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(filter_mask_in), 0,
118     "IPsec input firewall filter mask");
119 SYSCTL_INT(_net_enc_in, OID_AUTO, ipsec_bpf_mask,
120     CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(bpf_mask_in), 0,
121     "IPsec input bpf mask");
122 SYSCTL_INT(_net_enc_out, OID_AUTO, ipsec_filter_mask,
123     CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(filter_mask_out), 0,
124     "IPsec output firewall filter mask");
125 SYSCTL_INT(_net_enc_out, OID_AUTO, ipsec_bpf_mask,
126     CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(bpf_mask_out), 0,
127     "IPsec output bpf mask");
128 
129 static void
130 enc_clone_destroy(struct ifnet *ifp)
131 {
132 	struct enc_softc *sc;
133 
134 	sc = ifp->if_softc;
135 	KASSERT(sc == V_enc_sc, ("sc != ifp->if_softc"));
136 
137 	enc_remove_hhooks(sc);
138 	bpfdetach(ifp);
139 	if_detach(ifp);
140 	if_free(ifp);
141 	free(sc, M_DEVBUF);
142 	V_enc_sc = NULL;
143 }
144 
145 static int
146 enc_clone_create(struct if_clone *ifc, int unit, caddr_t params)
147 {
148 	struct ifnet *ifp;
149 	struct enc_softc *sc;
150 
151 	sc = malloc(sizeof(struct enc_softc), M_DEVBUF,
152 	    M_WAITOK | M_ZERO);
153 	ifp = sc->sc_ifp = if_alloc(IFT_ENC);
154 	if (ifp == NULL) {
155 		free(sc, M_DEVBUF);
156 		return (ENOSPC);
157 	}
158 	if (V_enc_sc != NULL) {
159 		if_free(ifp);
160 		free(sc, M_DEVBUF);
161 		return (EEXIST);
162 	}
163 	V_enc_sc = sc;
164 	if_initname(ifp, encname, unit);
165 	ifp->if_mtu = ENCMTU;
166 	ifp->if_ioctl = enc_ioctl;
167 	ifp->if_output = enc_output;
168 	ifp->if_softc = sc;
169 	if_attach(ifp);
170 	bpfattach(ifp, DLT_ENC, sizeof(struct enchdr));
171 	if (enc_add_hhooks(sc) != 0) {
172 		enc_clone_destroy(ifp);
173 		return (ENXIO);
174 	}
175 	return (0);
176 }
177 
178 static int
179 enc_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
180     struct route *ro)
181 {
182 
183 	m_freem(m);
184 	return (0);
185 }
186 
187 static int
188 enc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
189 {
190 
191 	if (cmd != SIOCSIFFLAGS)
192 		return (EINVAL);
193 	if (ifp->if_flags & IFF_UP)
194 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
195 	else
196 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
197 	return (0);
198 }
199 
200 /*
201  * One helper hook function is used by any hook points.
202  * + from hhook_type we can determine the packet direction:
203  *   HHOOK_TYPE_IPSEC_IN or HHOOK_TYPE_IPSEC_OUT;
204  * + from hhook_id we can determine address family: AF_INET or AF_INET6;
205  * + udata contains pointer to enc_softc;
206  * + ctx_data contains pointer to struct ipsec_ctx_data.
207  */
208 static int
209 enc_hhook(int32_t hhook_type, int32_t hhook_id, void *udata, void *ctx_data,
210     void *hdata, struct osd *hosd)
211 {
212 	struct enchdr hdr;
213 	struct ipsec_ctx_data *ctx;
214 	struct enc_softc *sc;
215 	struct ifnet *ifp, *rcvif;
216 	struct pfil_head *ph;
217 	int pdir;
218 
219 	sc = (struct enc_softc *)udata;
220 	ifp = sc->sc_ifp;
221 	if ((ifp->if_flags & IFF_UP) == 0)
222 		return (0);
223 
224 	ctx = (struct ipsec_ctx_data *)ctx_data;
225 	/* XXX: wrong hook point was used by caller? */
226 	if (ctx->af != hhook_id)
227 		return (EPFNOSUPPORT);
228 
229 	if (((hhook_type == HHOOK_TYPE_IPSEC_IN &&
230 	    (ctx->enc & V_bpf_mask_in) != 0) ||
231 	    (hhook_type == HHOOK_TYPE_IPSEC_OUT &&
232 	    (ctx->enc & V_bpf_mask_out) != 0)) &&
233 	    bpf_peers_present(ifp->if_bpf) != 0) {
234 		hdr.af = ctx->af;
235 		hdr.spi = ctx->sav->spi;
236 		hdr.flags = 0;
237 		if (ctx->sav->alg_enc != SADB_EALG_NONE)
238 			hdr.flags |= M_CONF;
239 		if (ctx->sav->alg_auth != SADB_AALG_NONE)
240 			hdr.flags |= M_AUTH;
241 		bpf_mtap2(ifp->if_bpf, &hdr, sizeof(hdr), *ctx->mp);
242 	}
243 
244 	switch (hhook_type) {
245 	case HHOOK_TYPE_IPSEC_IN:
246 		if (ctx->enc == IPSEC_ENC_BEFORE) {
247 			/* Do accounting only once */
248 			if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
249 			if_inc_counter(ifp, IFCOUNTER_IBYTES,
250 			    (*ctx->mp)->m_pkthdr.len);
251 		}
252 		if ((ctx->enc & V_filter_mask_in) == 0)
253 			return (0); /* skip pfil processing */
254 		pdir = PFIL_IN;
255 		break;
256 	case HHOOK_TYPE_IPSEC_OUT:
257 		if (ctx->enc == IPSEC_ENC_BEFORE) {
258 			/* Do accounting only once */
259 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
260 			if_inc_counter(ifp, IFCOUNTER_OBYTES,
261 			    (*ctx->mp)->m_pkthdr.len);
262 		}
263 		if ((ctx->enc & V_filter_mask_out) == 0)
264 			return (0); /* skip pfil processing */
265 		pdir = PFIL_OUT;
266 		break;
267 	default:
268 		return (EINVAL);
269 	}
270 
271 	switch (hhook_id) {
272 #ifdef INET
273 	case AF_INET:
274 		ph = &V_inet_pfil_hook;
275 		break;
276 #endif
277 #ifdef INET6
278 	case AF_INET6:
279 		ph = &V_inet6_pfil_hook;
280 		break;
281 #endif
282 	default:
283 		ph = NULL;
284 	}
285 	if (ph == NULL || !PFIL_HOOKED(ph))
286 		return (0);
287 	/* Make a packet looks like it was received on enc(4) */
288 	rcvif = (*ctx->mp)->m_pkthdr.rcvif;
289 	(*ctx->mp)->m_pkthdr.rcvif = ifp;
290 	if (pfil_run_hooks(ph, ctx->mp, ifp, pdir, NULL) != 0 ||
291 	    *ctx->mp == NULL) {
292 		*ctx->mp = NULL; /* consumed by filter */
293 		return (EACCES);
294 	}
295 	(*ctx->mp)->m_pkthdr.rcvif = rcvif;
296 	return (0);
297 }
298 
299 static int
300 enc_add_hhooks(struct enc_softc *sc)
301 {
302 	struct hookinfo hki;
303 	int error;
304 
305 	error = EPFNOSUPPORT;
306 	hki.hook_func = enc_hhook;
307 	hki.hook_helper = NULL;
308 	hki.hook_udata = sc;
309 #ifdef INET
310 	hki.hook_id = AF_INET;
311 	hki.hook_type = HHOOK_TYPE_IPSEC_IN;
312 	error = hhook_add_hook(V_ipsec_hhh_in[HHOOK_IPSEC_INET],
313 	    &hki, HHOOK_WAITOK);
314 	if (error != 0)
315 		return (error);
316 	hki.hook_type = HHOOK_TYPE_IPSEC_OUT;
317 	error = hhook_add_hook(V_ipsec_hhh_out[HHOOK_IPSEC_INET],
318 	    &hki, HHOOK_WAITOK);
319 	if (error != 0)
320 		return (error);
321 #endif
322 #ifdef INET6
323 	hki.hook_id = AF_INET6;
324 	hki.hook_type = HHOOK_TYPE_IPSEC_IN;
325 	error = hhook_add_hook(V_ipsec_hhh_in[HHOOK_IPSEC_INET6],
326 	    &hki, HHOOK_WAITOK);
327 	if (error != 0)
328 		return (error);
329 	hki.hook_type = HHOOK_TYPE_IPSEC_OUT;
330 	error = hhook_add_hook(V_ipsec_hhh_out[HHOOK_IPSEC_INET6],
331 	    &hki, HHOOK_WAITOK);
332 	if (error != 0)
333 		return (error);
334 #endif
335 	return (error);
336 }
337 
338 static void
339 enc_remove_hhooks(struct enc_softc *sc)
340 {
341 	struct hookinfo hki;
342 
343 	hki.hook_func = enc_hhook;
344 	hki.hook_helper = NULL;
345 	hki.hook_udata = sc;
346 #ifdef INET
347 	hki.hook_id = AF_INET;
348 	hki.hook_type = HHOOK_TYPE_IPSEC_IN;
349 	hhook_remove_hook(V_ipsec_hhh_in[HHOOK_IPSEC_INET], &hki);
350 	hki.hook_type = HHOOK_TYPE_IPSEC_OUT;
351 	hhook_remove_hook(V_ipsec_hhh_out[HHOOK_IPSEC_INET], &hki);
352 #endif
353 #ifdef INET6
354 	hki.hook_id = AF_INET6;
355 	hki.hook_type = HHOOK_TYPE_IPSEC_IN;
356 	hhook_remove_hook(V_ipsec_hhh_in[HHOOK_IPSEC_INET6], &hki);
357 	hki.hook_type = HHOOK_TYPE_IPSEC_OUT;
358 	hhook_remove_hook(V_ipsec_hhh_out[HHOOK_IPSEC_INET6], &hki);
359 #endif
360 }
361 
362 static void
363 vnet_enc_init(const void *unused __unused)
364 {
365 
366 	V_enc_sc = NULL;
367 	V_enc_cloner = if_clone_simple(encname, enc_clone_create,
368 	    enc_clone_destroy, 1);
369 }
370 VNET_SYSINIT(vnet_enc_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
371     vnet_enc_init, NULL);
372 
373 static void
374 vnet_enc_uninit(const void *unused __unused)
375 {
376 
377 	if_clone_detach(V_enc_cloner);
378 }
379 VNET_SYSUNINIT(vnet_enc_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
380     vnet_enc_uninit, NULL);
381 
382 static int
383 enc_modevent(module_t mod, int type, void *data)
384 {
385 
386 	switch (type) {
387 	case MOD_LOAD:
388 	case MOD_UNLOAD:
389 		break;
390 	default:
391 		return (EOPNOTSUPP);
392 	}
393 	return (0);
394 }
395 
396 static moduledata_t enc_mod = {
397 	"if_enc",
398 	enc_modevent,
399 	0
400 };
401 
402 DECLARE_MODULE(if_enc, enc_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
403