1fcf59617SAndrey V. Elsukov /*-
2fcf59617SAndrey V. Elsukov * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
3fcf59617SAndrey V. Elsukov * All rights reserved.
4fcf59617SAndrey V. Elsukov *
5fcf59617SAndrey V. Elsukov * Redistribution and use in source and binary forms, with or without
6fcf59617SAndrey V. Elsukov * modification, are permitted provided that the following conditions
7fcf59617SAndrey V. Elsukov * are met:
8fcf59617SAndrey V. Elsukov *
9fcf59617SAndrey V. Elsukov * 1. Redistributions of source code must retain the above copyright
10fcf59617SAndrey V. Elsukov * notice, this list of conditions and the following disclaimer.
11fcf59617SAndrey V. Elsukov * 2. Redistributions in binary form must reproduce the above copyright
12fcf59617SAndrey V. Elsukov * notice, this list of conditions and the following disclaimer in the
13fcf59617SAndrey V. Elsukov * documentation and/or other materials provided with the distribution.
14fcf59617SAndrey V. Elsukov *
15fcf59617SAndrey V. Elsukov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16fcf59617SAndrey V. Elsukov * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17fcf59617SAndrey V. Elsukov * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18fcf59617SAndrey V. Elsukov * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19fcf59617SAndrey V. Elsukov * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20fcf59617SAndrey V. Elsukov * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21fcf59617SAndrey V. Elsukov * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22fcf59617SAndrey V. Elsukov * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23fcf59617SAndrey V. Elsukov * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24fcf59617SAndrey V. Elsukov * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25fcf59617SAndrey V. Elsukov */
26fcf59617SAndrey V. Elsukov
27fcf59617SAndrey V. Elsukov #include "opt_inet.h"
28fcf59617SAndrey V. Elsukov #include "opt_inet6.h"
29fcf59617SAndrey V. Elsukov #include "opt_ipsec.h"
30fcf59617SAndrey V. Elsukov
31fcf59617SAndrey V. Elsukov #include <sys/param.h>
32fcf59617SAndrey V. Elsukov #include <sys/systm.h>
33fcf59617SAndrey V. Elsukov #include <sys/kernel.h>
34fcf59617SAndrey V. Elsukov #include <sys/lock.h>
35fcf59617SAndrey V. Elsukov #include <sys/malloc.h>
36fcf59617SAndrey V. Elsukov #include <sys/mbuf.h>
37fcf59617SAndrey V. Elsukov #include <sys/module.h>
38fcf59617SAndrey V. Elsukov #include <sys/priv.h>
39fcf59617SAndrey V. Elsukov #include <sys/rmlock.h>
40fcf59617SAndrey V. Elsukov #include <sys/socket.h>
41fcf59617SAndrey V. Elsukov #include <sys/sockopt.h>
42fcf59617SAndrey V. Elsukov #include <sys/syslog.h>
43fcf59617SAndrey V. Elsukov #include <sys/proc.h>
44fcf59617SAndrey V. Elsukov
45fcf59617SAndrey V. Elsukov #include <netinet/in.h>
46fcf59617SAndrey V. Elsukov #include <netinet/in_pcb.h>
47fcf59617SAndrey V. Elsukov
48fcf59617SAndrey V. Elsukov #include <netipsec/ipsec.h>
49fcf59617SAndrey V. Elsukov #include <netipsec/ipsec6.h>
50fcf59617SAndrey V. Elsukov #include <netipsec/key.h>
51fcf59617SAndrey V. Elsukov #include <netipsec/key_debug.h>
52fcf59617SAndrey V. Elsukov
53fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
54fcf59617SAndrey V. Elsukov
55fcf59617SAndrey V. Elsukov #ifdef INET
56fcf59617SAndrey V. Elsukov static const struct ipsec_methods ipv4_methods = {
57fcf59617SAndrey V. Elsukov .input = ipsec4_input,
58fcf59617SAndrey V. Elsukov .forward = ipsec4_forward,
59fcf59617SAndrey V. Elsukov .output = ipsec4_output,
60fcf59617SAndrey V. Elsukov .pcbctl = ipsec4_pcbctl,
61fcf59617SAndrey V. Elsukov .capability = ipsec4_capability,
62fcf59617SAndrey V. Elsukov .check_policy = ipsec4_in_reject,
63d9d59bb1SWojciech Macek .ctlinput = ipsec4_ctlinput,
64fcf59617SAndrey V. Elsukov .hdrsize = ipsec_hdrsiz_inpcb,
65fcf59617SAndrey V. Elsukov .udp_input = udp_ipsec_input,
66fcf59617SAndrey V. Elsukov .udp_pcbctl = udp_ipsec_pcbctl,
67fcf59617SAndrey V. Elsukov };
68fcf59617SAndrey V. Elsukov #ifndef KLD_MODULE
69fcf59617SAndrey V. Elsukov static const struct ipsec_support ipv4_ipsec = {
70fcf59617SAndrey V. Elsukov .enabled = IPSEC_MODULE_ENABLED,
71fcf59617SAndrey V. Elsukov .methods = &ipv4_methods
72fcf59617SAndrey V. Elsukov };
73fcf59617SAndrey V. Elsukov const struct ipsec_support * const ipv4_ipsec_support = &ipv4_ipsec;
74fcf59617SAndrey V. Elsukov #endif /* !KLD_MODULE */
75fcf59617SAndrey V. Elsukov #endif /* INET */
76fcf59617SAndrey V. Elsukov
77fcf59617SAndrey V. Elsukov #ifdef INET6
78fcf59617SAndrey V. Elsukov static const struct ipsec_methods ipv6_methods = {
79fcf59617SAndrey V. Elsukov .input = ipsec6_input,
80fcf59617SAndrey V. Elsukov .forward = ipsec6_forward,
81fcf59617SAndrey V. Elsukov .output = ipsec6_output,
82fcf59617SAndrey V. Elsukov .pcbctl = ipsec6_pcbctl,
83fcf59617SAndrey V. Elsukov .capability = ipsec6_capability,
84fcf59617SAndrey V. Elsukov .check_policy = ipsec6_in_reject,
85d9d59bb1SWojciech Macek .ctlinput = ipsec6_ctlinput,
86fcf59617SAndrey V. Elsukov .hdrsize = ipsec_hdrsiz_inpcb,
87*80044c78SXavier Beaudouin .udp_input = udp_ipsec_input,
88*80044c78SXavier Beaudouin .udp_pcbctl = udp_ipsec_pcbctl,
89fcf59617SAndrey V. Elsukov };
90fcf59617SAndrey V. Elsukov #ifndef KLD_MODULE
91fcf59617SAndrey V. Elsukov static const struct ipsec_support ipv6_ipsec = {
92fcf59617SAndrey V. Elsukov .enabled = IPSEC_MODULE_ENABLED,
93fcf59617SAndrey V. Elsukov .methods = &ipv6_methods
94fcf59617SAndrey V. Elsukov };
95fcf59617SAndrey V. Elsukov const struct ipsec_support * const ipv6_ipsec_support = &ipv6_ipsec;
96fcf59617SAndrey V. Elsukov #endif /* !KLD_MODULE */
97fcf59617SAndrey V. Elsukov #endif /* INET6 */
98fcf59617SAndrey V. Elsukov
99fcf59617SAndrey V. Elsukov /*
100fcf59617SAndrey V. Elsukov * Always register ipsec module.
101fcf59617SAndrey V. Elsukov * Even when IPsec is build in the kernel, we need to have
102fcf59617SAndrey V. Elsukov * module registered. This will prevent to load ipsec.ko.
103fcf59617SAndrey V. Elsukov */
104fcf59617SAndrey V. Elsukov static int
ipsec_modevent(module_t mod,int type,void * data)105fcf59617SAndrey V. Elsukov ipsec_modevent(module_t mod, int type, void *data)
106fcf59617SAndrey V. Elsukov {
107fcf59617SAndrey V. Elsukov
108fcf59617SAndrey V. Elsukov switch (type) {
109fcf59617SAndrey V. Elsukov case MOD_LOAD:
110fcf59617SAndrey V. Elsukov /* All xforms are registered via SYSINIT */
111fcf59617SAndrey V. Elsukov if (!ipsec_initialized())
112fcf59617SAndrey V. Elsukov return (ENOMEM);
113fcf59617SAndrey V. Elsukov #ifdef KLD_MODULE
114fcf59617SAndrey V. Elsukov #ifdef INET
115fcf59617SAndrey V. Elsukov ipsec_support_enable(ipv4_ipsec_support, &ipv4_methods);
116fcf59617SAndrey V. Elsukov #endif
117fcf59617SAndrey V. Elsukov #ifdef INET6
118fcf59617SAndrey V. Elsukov ipsec_support_enable(ipv6_ipsec_support, &ipv6_methods);
119fcf59617SAndrey V. Elsukov #endif
120fcf59617SAndrey V. Elsukov #endif /* KLD_MODULE */
121fcf59617SAndrey V. Elsukov break;
122fcf59617SAndrey V. Elsukov case MOD_UNLOAD:
123fcf59617SAndrey V. Elsukov /* All xforms are unregistered via SYSUNINIT */
124fcf59617SAndrey V. Elsukov #ifdef KLD_MODULE
125fcf59617SAndrey V. Elsukov #ifdef INET
126fcf59617SAndrey V. Elsukov ipsec_support_disable(ipv4_ipsec_support);
127fcf59617SAndrey V. Elsukov #endif
128fcf59617SAndrey V. Elsukov #ifdef INET6
129fcf59617SAndrey V. Elsukov ipsec_support_disable(ipv6_ipsec_support);
130fcf59617SAndrey V. Elsukov #endif
131fcf59617SAndrey V. Elsukov #endif /* KLD_MODULE */
132fcf59617SAndrey V. Elsukov break;
133fcf59617SAndrey V. Elsukov default:
134fcf59617SAndrey V. Elsukov return (EOPNOTSUPP);
135fcf59617SAndrey V. Elsukov }
136fcf59617SAndrey V. Elsukov return (0);
137fcf59617SAndrey V. Elsukov }
138fcf59617SAndrey V. Elsukov
139fcf59617SAndrey V. Elsukov static moduledata_t ipsec_mod = {
140fcf59617SAndrey V. Elsukov "ipsec",
141fcf59617SAndrey V. Elsukov ipsec_modevent,
142fcf59617SAndrey V. Elsukov 0
143fcf59617SAndrey V. Elsukov };
144fcf59617SAndrey V. Elsukov
145fcf59617SAndrey V. Elsukov DECLARE_MODULE(ipsec, ipsec_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
146fcf59617SAndrey V. Elsukov MODULE_VERSION(ipsec, 1);
147fcf59617SAndrey V. Elsukov #ifdef KLD_MODULE
148fcf59617SAndrey V. Elsukov MODULE_DEPEND(ipsec, ipsec_support, 1, 1, 1);
149fcf59617SAndrey V. Elsukov #endif
150