1159965Sthompsa/*-
2159965Sthompsa * Copyright (c) 2006 The FreeBSD Project.
3291292Sae * Copyright (c) 2015 Andrey V. Elsukov <ae@FreeBSD.org>
4159965Sthompsa * All rights reserved.
5159965Sthompsa *
6159965Sthompsa * Redistribution and use in source and binary forms, with or without
7159965Sthompsa * modification, are permitted provided that the following conditions
8159965Sthompsa * are met:
9159965Sthompsa *
10159965Sthompsa * 1. Redistributions of source code must retain the above copyright
11159965Sthompsa *    notice, this list of conditions and the following disclaimer.
12159965Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
13159965Sthompsa *    notice, this list of conditions and the following disclaimer in the
14159965Sthompsa *    documentation and/or other materials provided with the distribution.
15159965Sthompsa *
16159965Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17159965Sthompsa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18159965Sthompsa * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19159965Sthompsa * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20159965Sthompsa * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21159965Sthompsa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22159965Sthompsa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23159965Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24159965Sthompsa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25159965Sthompsa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26159965Sthompsa * SUCH DAMAGE.
27159965Sthompsa *
28159965Sthompsa * $FreeBSD: releng/11.0/sys/net/if_enc.c 302054 2016-06-21 13:48:49Z bz $
29159965Sthompsa */
30159965Sthompsa
31221130Sbz#include "opt_inet.h"
32221130Sbz#include "opt_inet6.h"
33221130Sbz
34159965Sthompsa#include <sys/param.h>
35159965Sthompsa#include <sys/systm.h>
36291335Sngie#include <sys/hhook.h>
37159965Sthompsa#include <sys/kernel.h>
38159965Sthompsa#include <sys/malloc.h>
39159965Sthompsa#include <sys/mbuf.h>
40159965Sthompsa#include <sys/module.h>
41159965Sthompsa#include <machine/bus.h>
42159965Sthompsa#include <sys/rman.h>
43159965Sthompsa#include <sys/socket.h>
44159965Sthompsa#include <sys/sockio.h>
45159965Sthompsa#include <sys/sysctl.h>
46159965Sthompsa
47159965Sthompsa#include <net/if.h>
48291335Sngie#include <net/if_enc.h>
49257176Sglebius#include <net/if_var.h>
50159965Sthompsa#include <net/if_clone.h>
51159965Sthompsa#include <net/if_types.h>
52159965Sthompsa#include <net/pfil.h>
53159965Sthompsa#include <net/route.h>
54159965Sthompsa#include <net/netisr.h>
55159965Sthompsa#include <net/bpf.h>
56195699Srwatson#include <net/vnet.h>
57159965Sthompsa
58159965Sthompsa#include <netinet/in.h>
59159965Sthompsa#include <netinet/in_systm.h>
60159965Sthompsa#include <netinet/ip.h>
61159965Sthompsa#include <netinet/ip_var.h>
62159965Sthompsa#include <netinet/in_var.h>
63159965Sthompsa
64159965Sthompsa#ifdef INET6
65159965Sthompsa#include <netinet/ip6.h>
66159965Sthompsa#include <netinet6/ip6_var.h>
67159965Sthompsa#endif
68159965Sthompsa
69159965Sthompsa#include <netipsec/ipsec.h>
70174054Sbz#include <netipsec/xform.h>
71159965Sthompsa
72159965Sthompsa#define ENCMTU		(1024+512)
73159965Sthompsa
74159965Sthompsa/* XXX this define must have the same value as in OpenBSD */
75159965Sthompsa#define M_CONF		0x0400	/* payload was encrypted (ESP-transport) */
76159965Sthompsa#define M_AUTH		0x0800	/* payload was authenticated (AH or ESP auth) */
77159965Sthompsa#define M_AUTH_AH	0x2000	/* header was authenticated (AH) */
78159965Sthompsa
79159965Sthompsastruct enchdr {
80159965Sthompsa	u_int32_t af;
81159965Sthompsa	u_int32_t spi;
82159965Sthompsa	u_int32_t flags;
83159965Sthompsa};
84159965Sthompsastruct enc_softc {
85159965Sthompsa	struct	ifnet *sc_ifp;
86159965Sthompsa};
87291292Saestatic VNET_DEFINE(struct enc_softc *, enc_sc);
88291292Sae#define	V_enc_sc	VNET(enc_sc)
89291292Saestatic VNET_DEFINE(struct if_clone *, enc_cloner);
90291292Sae#define	V_enc_cloner	VNET(enc_cloner)
91159965Sthompsa
92159965Sthompsastatic int	enc_ioctl(struct ifnet *, u_long, caddr_t);
93291292Saestatic int	enc_output(struct ifnet *, struct mbuf *,
94291292Sae    const struct sockaddr *, struct route *);
95160233Sthompsastatic int	enc_clone_create(struct if_clone *, int, caddr_t);
96160099Sthompsastatic void	enc_clone_destroy(struct ifnet *);
97291292Saestatic int	enc_add_hhooks(struct enc_softc *);
98291292Saestatic void	enc_remove_hhooks(struct enc_softc *);
99291292Sae
100241610Sglebiusstatic const char encname[] = "enc";
101159965Sthompsa
102174054Sbz/*
103174054Sbz * Before and after are relative to when we are stripping the
104174054Sbz * outer IP header.
105174054Sbz */
106291292Saestatic VNET_DEFINE(int, filter_mask_in) = IPSEC_ENC_BEFORE;
107291292Saestatic VNET_DEFINE(int, bpf_mask_in) = IPSEC_ENC_BEFORE;
108291292Saestatic VNET_DEFINE(int, filter_mask_out) = IPSEC_ENC_BEFORE;
109291292Saestatic VNET_DEFINE(int, bpf_mask_out) = IPSEC_ENC_BEFORE | IPSEC_ENC_AFTER;
110291292Sae#define	V_filter_mask_in	VNET(filter_mask_in)
111291292Sae#define	V_bpf_mask_in		VNET(bpf_mask_in)
112291292Sae#define	V_filter_mask_out	VNET(filter_mask_out)
113291292Sae#define	V_bpf_mask_out		VNET(bpf_mask_out)
114291292Sae
115227309Sedstatic SYSCTL_NODE(_net, OID_AUTO, enc, CTLFLAG_RW, 0, "enc sysctl");
116227309Sedstatic SYSCTL_NODE(_net_enc, OID_AUTO, in, CTLFLAG_RW, 0, "enc input sysctl");
117227309Sedstatic SYSCTL_NODE(_net_enc, OID_AUTO, out, CTLFLAG_RW, 0, "enc output sysctl");
118291292SaeSYSCTL_INT(_net_enc_in, OID_AUTO, ipsec_filter_mask,
119291292Sae    CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(filter_mask_in), 0,
120291292Sae    "IPsec input firewall filter mask");
121291292SaeSYSCTL_INT(_net_enc_in, OID_AUTO, ipsec_bpf_mask,
122291292Sae    CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(bpf_mask_in), 0,
123291292Sae    "IPsec input bpf mask");
124291292SaeSYSCTL_INT(_net_enc_out, OID_AUTO, ipsec_filter_mask,
125291292Sae    CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(filter_mask_out), 0,
126291292Sae    "IPsec output firewall filter mask");
127291292SaeSYSCTL_INT(_net_enc_out, OID_AUTO, ipsec_bpf_mask,
128291292Sae    CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(bpf_mask_out), 0,
129291292Sae    "IPsec output bpf mask");
130174054Sbz
131160099Sthompsastatic void
132159965Sthompsaenc_clone_destroy(struct ifnet *ifp)
133159965Sthompsa{
134291292Sae	struct enc_softc *sc;
135159965Sthompsa
136291292Sae	sc = ifp->if_softc;
137291292Sae	KASSERT(sc == V_enc_sc, ("sc != ifp->if_softc"));
138291292Sae
139159965Sthompsa	bpfdetach(ifp);
140159965Sthompsa	if_detach(ifp);
141159965Sthompsa	if_free(ifp);
142291292Sae	free(sc, M_DEVBUF);
143291292Sae	V_enc_sc = NULL;
144159965Sthompsa}
145159965Sthompsa
146159965Sthompsastatic int
147160233Sthompsaenc_clone_create(struct if_clone *ifc, int unit, caddr_t params)
148159965Sthompsa{
149159965Sthompsa	struct ifnet *ifp;
150159965Sthompsa	struct enc_softc *sc;
151159965Sthompsa
152291292Sae	sc = malloc(sizeof(struct enc_softc), M_DEVBUF,
153291292Sae	    M_WAITOK | M_ZERO);
154159965Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_ENC);
155159965Sthompsa	if (ifp == NULL) {
156159965Sthompsa		free(sc, M_DEVBUF);
157159965Sthompsa		return (ENOSPC);
158159965Sthompsa	}
159291292Sae	if (V_enc_sc != NULL) {
160291292Sae		if_free(ifp);
161291292Sae		free(sc, M_DEVBUF);
162291292Sae		return (EEXIST);
163291292Sae	}
164291292Sae	V_enc_sc = sc;
165241610Sglebius	if_initname(ifp, encname, unit);
166159965Sthompsa	ifp->if_mtu = ENCMTU;
167159965Sthompsa	ifp->if_ioctl = enc_ioctl;
168159965Sthompsa	ifp->if_output = enc_output;
169159965Sthompsa	ifp->if_softc = sc;
170159965Sthompsa	if_attach(ifp);
171159969Sthompsa	bpfattach(ifp, DLT_ENC, sizeof(struct enchdr));
172159965Sthompsa	return (0);
173159965Sthompsa}
174159965Sthompsa
175159965Sthompsastatic int
176291292Saeenc_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
177291292Sae    struct route *ro)
178159965Sthompsa{
179291292Sae
180291292Sae	m_freem(m);
181159965Sthompsa	return (0);
182159965Sthompsa}
183159965Sthompsa
184159965Sthompsastatic int
185291292Saeenc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
186159965Sthompsa{
187291292Sae
188291292Sae	if (cmd != SIOCSIFFLAGS)
189291292Sae		return (EINVAL);
190291292Sae	if (ifp->if_flags & IFF_UP)
191291292Sae		ifp->if_drv_flags |= IFF_DRV_RUNNING;
192291292Sae	else
193291292Sae		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
194159965Sthompsa	return (0);
195159965Sthompsa}
196159965Sthompsa
197159965Sthompsa/*
198291292Sae * One helper hook function is used by any hook points.
199291292Sae * + from hhook_type we can determine the packet direction:
200291292Sae *   HHOOK_TYPE_IPSEC_IN or HHOOK_TYPE_IPSEC_OUT;
201291292Sae * + from hhook_id we can determine address family: AF_INET or AF_INET6;
202291292Sae * + udata contains pointer to enc_softc;
203291292Sae * + ctx_data contains pointer to struct ipsec_ctx_data.
204159965Sthompsa */
205159965Sthompsastatic int
206291292Saeenc_hhook(int32_t hhook_type, int32_t hhook_id, void *udata, void *ctx_data,
207291292Sae    void *hdata, struct osd *hosd)
208159965Sthompsa{
209291292Sae	struct enchdr hdr;
210291292Sae	struct ipsec_ctx_data *ctx;
211291292Sae	struct enc_softc *sc;
212291292Sae	struct ifnet *ifp, *rcvif;
213291292Sae	struct pfil_head *ph;
214291292Sae	int pdir;
215159965Sthompsa
216291292Sae	sc = (struct enc_softc *)udata;
217291292Sae	ifp = sc->sc_ifp;
218291292Sae	if ((ifp->if_flags & IFF_UP) == 0)
219291292Sae		return (0);
220160011Sthompsa
221291292Sae	ctx = (struct ipsec_ctx_data *)ctx_data;
222291292Sae	/* XXX: wrong hook point was used by caller? */
223291292Sae	if (ctx->af != hhook_id)
224291292Sae		return (EPFNOSUPPORT);
225159965Sthompsa
226291292Sae	if (((hhook_type == HHOOK_TYPE_IPSEC_IN &&
227291292Sae	    (ctx->enc & V_bpf_mask_in) != 0) ||
228291292Sae	    (hhook_type == HHOOK_TYPE_IPSEC_OUT &&
229291292Sae	    (ctx->enc & V_bpf_mask_out) != 0)) &&
230291292Sae	    bpf_peers_present(ifp->if_bpf) != 0) {
231291292Sae		hdr.af = ctx->af;
232291292Sae		hdr.spi = ctx->sav->spi;
233291292Sae		hdr.flags = 0;
234291292Sae		if (ctx->sav->alg_enc != SADB_EALG_NONE)
235291292Sae			hdr.flags |= M_CONF;
236291292Sae		if (ctx->sav->alg_auth != SADB_AALG_NONE)
237291292Sae			hdr.flags |= M_AUTH;
238291292Sae		bpf_mtap2(ifp->if_bpf, &hdr, sizeof(hdr), *ctx->mp);
239291292Sae	}
240159965Sthompsa
241291292Sae	switch (hhook_type) {
242291292Sae	case HHOOK_TYPE_IPSEC_IN:
243291292Sae		if (ctx->enc == IPSEC_ENC_BEFORE) {
244291292Sae			/* Do accounting only once */
245291292Sae			if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
246291292Sae			if_inc_counter(ifp, IFCOUNTER_IBYTES,
247291292Sae			    (*ctx->mp)->m_pkthdr.len);
248291292Sae		}
249291292Sae		if ((ctx->enc & V_filter_mask_in) == 0)
250291292Sae			return (0); /* skip pfil processing */
251291292Sae		pdir = PFIL_IN;
252159965Sthompsa		break;
253291292Sae	case HHOOK_TYPE_IPSEC_OUT:
254291292Sae		if (ctx->enc == IPSEC_ENC_BEFORE) {
255291292Sae			/* Do accounting only once */
256291292Sae			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
257291292Sae			if_inc_counter(ifp, IFCOUNTER_OBYTES,
258291292Sae			    (*ctx->mp)->m_pkthdr.len);
259291292Sae		}
260291292Sae		if ((ctx->enc & V_filter_mask_out) == 0)
261291292Sae			return (0); /* skip pfil processing */
262291292Sae		pdir = PFIL_OUT;
263291292Sae		break;
264291292Sae	default:
265291292Sae		return (EINVAL);
266291292Sae	}
267159965Sthompsa
268291292Sae	switch (hhook_id) {
269291292Sae#ifdef INET
270291292Sae	case AF_INET:
271291292Sae		ph = &V_inet_pfil_hook;
272291292Sae		break;
273291292Sae#endif
274291292Sae#ifdef INET6
275291292Sae	case AF_INET6:
276291292Sae		ph = &V_inet6_pfil_hook;
277291292Sae		break;
278291292Sae#endif
279159965Sthompsa	default:
280291292Sae		ph = NULL;
281159965Sthompsa	}
282291292Sae	if (ph == NULL || !PFIL_HOOKED(ph))
283291292Sae		return (0);
284291292Sae	/* Make a packet looks like it was received on enc(4) */
285291292Sae	rcvif = (*ctx->mp)->m_pkthdr.rcvif;
286291292Sae	(*ctx->mp)->m_pkthdr.rcvif = ifp;
287291292Sae	if (pfil_run_hooks(ph, ctx->mp, ifp, pdir, NULL) != 0 ||
288291292Sae	    *ctx->mp == NULL) {
289291292Sae		*ctx->mp = NULL; /* consumed by filter */
290291292Sae		return (EACCES);
291291292Sae	}
292291292Sae	(*ctx->mp)->m_pkthdr.rcvif = rcvif;
293291292Sae	return (0);
294159965Sthompsa}
295159965Sthompsa
296291292Saestatic int
297291292Saeenc_add_hhooks(struct enc_softc *sc)
298159965Sthompsa{
299291292Sae	struct hookinfo hki;
300291292Sae	int error;
301159965Sthompsa
302291292Sae	error = EPFNOSUPPORT;
303291292Sae	hki.hook_func = enc_hhook;
304291292Sae	hki.hook_helper = NULL;
305291292Sae	hki.hook_udata = sc;
306221130Sbz#ifdef INET
307291292Sae	hki.hook_id = AF_INET;
308291292Sae	hki.hook_type = HHOOK_TYPE_IPSEC_IN;
309291292Sae	error = hhook_add_hook(V_ipsec_hhh_in[HHOOK_IPSEC_INET],
310291292Sae	    &hki, HHOOK_WAITOK);
311291292Sae	if (error != 0)
312291292Sae		return (error);
313291292Sae	hki.hook_type = HHOOK_TYPE_IPSEC_OUT;
314291292Sae	error = hhook_add_hook(V_ipsec_hhh_out[HHOOK_IPSEC_INET],
315291292Sae	    &hki, HHOOK_WAITOK);
316291292Sae	if (error != 0)
317291292Sae		return (error);
318221130Sbz#endif
319159965Sthompsa#ifdef INET6
320291292Sae	hki.hook_id = AF_INET6;
321291292Sae	hki.hook_type = HHOOK_TYPE_IPSEC_IN;
322291292Sae	error = hhook_add_hook(V_ipsec_hhh_in[HHOOK_IPSEC_INET6],
323291292Sae	    &hki, HHOOK_WAITOK);
324291292Sae	if (error != 0)
325291292Sae		return (error);
326291292Sae	hki.hook_type = HHOOK_TYPE_IPSEC_OUT;
327291292Sae	error = hhook_add_hook(V_ipsec_hhh_out[HHOOK_IPSEC_INET6],
328291292Sae	    &hki, HHOOK_WAITOK);
329291292Sae	if (error != 0)
330291292Sae		return (error);
331159965Sthompsa#endif
332291292Sae	return (error);
333291292Sae}
334159965Sthompsa
335291292Saestatic void
336291292Saeenc_remove_hhooks(struct enc_softc *sc)
337291292Sae{
338291292Sae	struct hookinfo hki;
339159965Sthompsa
340291292Sae	hki.hook_func = enc_hhook;
341291292Sae	hki.hook_helper = NULL;
342291292Sae	hki.hook_udata = sc;
343221130Sbz#ifdef INET
344291292Sae	hki.hook_id = AF_INET;
345291292Sae	hki.hook_type = HHOOK_TYPE_IPSEC_IN;
346291292Sae	hhook_remove_hook(V_ipsec_hhh_in[HHOOK_IPSEC_INET], &hki);
347291292Sae	hki.hook_type = HHOOK_TYPE_IPSEC_OUT;
348291292Sae	hhook_remove_hook(V_ipsec_hhh_out[HHOOK_IPSEC_INET], &hki);
349221130Sbz#endif
350159965Sthompsa#ifdef INET6
351291292Sae	hki.hook_id = AF_INET6;
352291292Sae	hki.hook_type = HHOOK_TYPE_IPSEC_IN;
353291292Sae	hhook_remove_hook(V_ipsec_hhh_in[HHOOK_IPSEC_INET6], &hki);
354291292Sae	hki.hook_type = HHOOK_TYPE_IPSEC_OUT;
355291292Sae	hhook_remove_hook(V_ipsec_hhh_out[HHOOK_IPSEC_INET6], &hki);
356159965Sthompsa#endif
357291292Sae}
358159965Sthompsa
359291292Saestatic void
360291292Saevnet_enc_init(const void *unused __unused)
361291292Sae{
362159965Sthompsa
363291292Sae	V_enc_sc = NULL;
364291292Sae	V_enc_cloner = if_clone_simple(encname, enc_clone_create,
365291292Sae	    enc_clone_destroy, 1);
366159965Sthompsa}
367302054SbzVNET_SYSINIT(vnet_enc_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
368291292Sae    vnet_enc_init, NULL);
369159965Sthompsa
370291292Saestatic void
371302054Sbzvnet_enc_init_proto(void *unused __unused)
372302054Sbz{
373302054Sbz	KASSERT(V_enc_sc != NULL, ("%s: V_enc_sc is %p\n", __func__, V_enc_sc));
374302054Sbz
375302054Sbz	if (enc_add_hhooks(V_enc_sc) != 0)
376302054Sbz		enc_clone_destroy(V_enc_sc->sc_ifp);
377302054Sbz}
378302054SbzVNET_SYSINIT(vnet_enc_init_proto, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
379302054Sbz    vnet_enc_init_proto, NULL);
380302054Sbz
381302054Sbzstatic void
382291292Saevnet_enc_uninit(const void *unused __unused)
383159965Sthompsa{
384302054Sbz	KASSERT(V_enc_sc != NULL, ("%s: V_enc_sc is %p\n", __func__, V_enc_sc));
385159965Sthompsa
386291292Sae	if_clone_detach(V_enc_cloner);
387291292Sae}
388302054SbzVNET_SYSUNINIT(vnet_enc_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
389291292Sae    vnet_enc_uninit, NULL);
390159965Sthompsa
391302054Sbz/*
392302054Sbz * The hhook consumer needs to go before ip[6]_destroy are called on
393302054Sbz * SI_ORDER_THIRD.
394302054Sbz */
395302054Sbzstatic void
396302054Sbzvnet_enc_uninit_hhook(const void *unused __unused)
397302054Sbz{
398302054Sbz	KASSERT(V_enc_sc != NULL, ("%s: V_enc_sc is %p\n", __func__, V_enc_sc));
399302054Sbz
400302054Sbz	enc_remove_hhooks(V_enc_sc);
401302054Sbz}
402302054SbzVNET_SYSUNINIT(vnet_enc_uninit_hhook, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
403302054Sbz    vnet_enc_uninit_hhook, NULL);
404302054Sbz
405291292Saestatic int
406291292Saeenc_modevent(module_t mod, int type, void *data)
407291292Sae{
408159965Sthompsa
409291292Sae	switch (type) {
410291292Sae	case MOD_LOAD:
411291292Sae	case MOD_UNLOAD:
412291292Sae		break;
413291292Sae	default:
414291292Sae		return (EOPNOTSUPP);
415174054Sbz	}
416291292Sae	return (0);
417291292Sae}
418174054Sbz
419291292Saestatic moduledata_t enc_mod = {
420291292Sae	"if_enc",
421291292Sae	enc_modevent,
422291292Sae	0
423291292Sae};
424159965Sthompsa
425302054SbzDECLARE_MODULE(if_enc, enc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
426