2 * Copyright (c) 2006 The FreeBSD Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <machine/bus.h>
38 #include <sys/socket.h>
39 #include <sys/sockio.h>
40 #include <sys/sysctl.h>
43 #include <net/if_clone.h>
44 #include <net/if_types.h>
46 #include <net/route.h>
47 #include <net/netisr.h>
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/in_var.h>
56 #include "opt_inet6.h"
59 #include <netinet/ip6.h>
60 #include <netinet6/ip6_var.h>
64 #include <netipsec/ipsec.h>
65 #include <netipsec/xform.h>
67 #define ENCMTU (1024+512)
69 /* XXX this define must have the same value as in OpenBSD */
70 #define M_CONF 0x0400 /* payload was encrypted (ESP-transport) */
71 #define M_AUTH 0x0800 /* payload was authenticated (AH or ESP auth) */
72 #define M_AUTH_AH 0x2000 /* header was authenticated (AH) */
81 static struct mtx enc_mtx;
87 static int enc_ioctl(struct ifnet *, u_long, caddr_t);
88 static int enc_output(struct ifnet *ifp, struct mbuf *m,
89 struct sockaddr *dst, struct route *ro);
90 static int enc_clone_create(struct if_clone *, int, caddr_t);
91 static void enc_clone_destroy(struct ifnet *);
93 IFC_SIMPLE_DECLARE(enc, 1);
100 * Before and after are relative to when we are stripping the
103 SYSCTL_NODE(_net, OID_AUTO, enc, CTLFLAG_RW, 0, "enc sysctl");
105 SYSCTL_NODE(_net_enc, OID_AUTO, in, CTLFLAG_RW, 0, "enc input sysctl");
106 static int ipsec_filter_mask_in = ENC_BEFORE;
107 SYSCTL_XINT(_net_enc_in, OID_AUTO, ipsec_filter_mask, CTLFLAG_RW,
108 &ipsec_filter_mask_in, 0, "IPsec input firewall filter mask");
109 static int ipsec_bpf_mask_in = ENC_BEFORE;
110 SYSCTL_XINT(_net_enc_in, OID_AUTO, ipsec_bpf_mask, CTLFLAG_RW,
111 &ipsec_bpf_mask_in, 0, "IPsec input bpf mask");
113 SYSCTL_NODE(_net_enc, OID_AUTO, out, CTLFLAG_RW, 0, "enc output sysctl");
114 static int ipsec_filter_mask_out = ENC_BEFORE;
115 SYSCTL_XINT(_net_enc_out, OID_AUTO, ipsec_filter_mask, CTLFLAG_RW,
116 &ipsec_filter_mask_out, 0, "IPsec output firewall filter mask");
117 static int ipsec_bpf_mask_out = ENC_BEFORE|ENC_AFTER;
118 SYSCTL_XINT(_net_enc_out, OID_AUTO, ipsec_bpf_mask, CTLFLAG_RW,
119 &ipsec_bpf_mask_out, 0, "IPsec output bpf mask");
122 enc_clone_destroy(struct ifnet *ifp)
124 KASSERT(ifp != encif, ("%s: destroying encif", __func__));
132 enc_clone_create(struct if_clone *ifc, int unit, caddr_t params)
135 struct enc_softc *sc;
137 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
138 ifp = sc->sc_ifp = if_alloc(IFT_ENC);
144 if_initname(ifp, ifc->ifc_name, unit);
145 ifp->if_mtu = ENCMTU;
146 ifp->if_ioctl = enc_ioctl;
147 ifp->if_output = enc_output;
148 ifp->if_snd.ifq_maxlen = ifqmaxlen;
151 bpfattach(ifp, DLT_ENC, sizeof(struct enchdr));
154 /* grab a pointer to enc0, ignore the rest */
157 mtx_unlock(&enc_mtx);
163 enc_modevent(module_t mod, int type, void *data)
167 mtx_init(&enc_mtx, "enc mtx", NULL, MTX_DEF);
168 if_clone_attach(&enc_cloner);
171 printf("enc module unload - not possible for this module\n");
179 static moduledata_t enc_mod = {
185 DECLARE_MODULE(enc, enc_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
188 enc_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
196 * Process an ioctl request.
200 enc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
209 if (ifp->if_flags & IFF_UP)
210 ifp->if_drv_flags |= IFF_DRV_RUNNING;
212 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
220 mtx_unlock(&enc_mtx);
225 ipsec_filter(struct mbuf **mp, int dir, int flags)
230 KASSERT(encif != NULL, ("%s: encif is null", __func__));
231 KASSERT(flags & (ENC_IN|ENC_OUT),
232 ("%s: invalid flags: %04x", __func__, flags));
234 if ((encif->if_drv_flags & IFF_DRV_RUNNING) == 0)
237 if (flags & ENC_IN) {
238 if ((flags & ipsec_filter_mask_in) == 0)
241 if ((flags & ipsec_filter_mask_out) == 0)
245 /* Skip pfil(9) if no filters are loaded */
246 if (!(PFIL_HOOKED(&V_inet_pfil_hook)
248 || PFIL_HOOKED(&V_inet6_pfil_hook)
254 i = min((*mp)->m_pkthdr.len, max_protohdr);
255 if ((*mp)->m_len < i) {
256 *mp = m_pullup(*mp, i);
258 printf("%s: m_pullup failed\n", __func__);
264 ip = mtod(*mp, struct ip *);
268 * before calling the firewall, swap fields the same as
269 * IP does. here we assume the header is contiguous
271 ip->ip_len = ntohs(ip->ip_len);
272 ip->ip_off = ntohs(ip->ip_off);
274 error = pfil_run_hooks(&V_inet_pfil_hook, mp,
277 if (*mp == NULL || error != 0)
280 /* restore byte ordering */
281 ip = mtod(*mp, struct ip *);
282 ip->ip_len = htons(ip->ip_len);
283 ip->ip_off = htons(ip->ip_off);
288 error = pfil_run_hooks(&V_inet6_pfil_hook, mp,
293 printf("%s: unknown IP version\n", __func__);
297 * If the mbuf was consumed by the filter for requeueing (dummynet, etc)
298 * then error will be zero but we still want to return an error to our
299 * caller so the null mbuf isn't forwarded further.
301 if (*mp == NULL && error == 0)
302 return (-1); /* Consumed by the filter */
317 ipsec_bpf(struct mbuf *m, struct secasvar *sav, int af, int flags)
322 KASSERT(encif != NULL, ("%s: encif is null", __func__));
323 KASSERT(flags & (ENC_IN|ENC_OUT),
324 ("%s: invalid flags: %04x", __func__, flags));
326 if ((encif->if_drv_flags & IFF_DRV_RUNNING) == 0)
329 if (flags & ENC_IN) {
330 if ((flags & ipsec_bpf_mask_in) == 0)
333 if ((flags & ipsec_bpf_mask_out) == 0)
337 if (bpf_peers_present(encif->if_bpf)) {
342 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
344 struct tdb_ident *tdbi;
345 tdbi = (struct tdb_ident *) (mtag + 1);
346 if (tdbi->alg_enc != SADB_EALG_NONE)
348 if (tdbi->alg_auth != SADB_AALG_NONE)
353 if (sav->alg_enc != SADB_EALG_NONE)
355 if (sav->alg_auth != SADB_AALG_NONE)
361 * We need to prepend the address family as a four byte
362 * field. Cons up a dummy header to pacify bpf. This
363 * is safe because bpf will only read from the mbuf
364 * (i.e., it won't try to free it or keep a pointer a
368 /* hdr.spi already set above */
371 bpf_mtap2(encif->if_bpf, &hdr, sizeof(hdr), m);