blob: a512d0094a6741082c00da52a8de20dbd4f2feee [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Neale Ranns999c8ee2019-02-01 03:31:24 -08002 * ipsec.c : IPSEC module functions
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 *
4 * Copyright (c) 2015 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/api_errno.h>
20#include <vnet/ip/ip.h>
21#include <vnet/interface.h>
Klement Sekera4b089f22018-04-17 18:04:57 +020022#include <vnet/udp/udp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24#include <vnet/ipsec/ipsec.h>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000025#include <vnet/ipsec/esp.h>
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080026#include <vnet/ipsec/ah.h>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000027
Dave Wallace71612d62017-10-24 01:32:41 -040028ipsec_main_t ipsec_main;
29
Ed Warnickecb9cada2015-12-08 15:45:58 -070030static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070031ipsec_rand_seed (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -070032{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070033 struct
34 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070035 time_t time;
36 pid_t pid;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070037 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070038 } seed_data;
39
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070040 seed_data.time = time (NULL);
41 seed_data.pid = getpid ();
42 seed_data.p = (void *) &seed_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -070043
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070044 RAND_seed ((const void *) &seed_data, sizeof (seed_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -070045}
46
47static clib_error_t *
Klement Sekera4fd5a9d2019-01-30 11:11:23 +010048ipsec_check_ah_support (ipsec_sa_t * sa)
49{
50 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
51 return clib_error_return (0, "unsupported none integ-alg");
52 return 0;
53}
54
55static clib_error_t *
56ipsec_check_esp_support (ipsec_sa_t * sa)
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +000057{
58 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
59 return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg");
Klement Sekera4fd5a9d2019-01-30 11:11:23 +010060 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_192)
61 return clib_error_return (0, "unsupported aes-gcm-192 crypto-alg");
62 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_256)
63 return clib_error_return (0, "unsupported aes-gcm-256 crypto-alg");
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +000064
65 return 0;
66}
67
Klement Sekerab4d30532018-11-08 13:00:02 +010068clib_error_t *
69ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
70{
71 ipsec_ah_backend_t *ah =
72 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
73 if (ah->add_del_sa_sess_cb)
74 {
75 clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
76 if (err)
77 return err;
78 }
79 ipsec_esp_backend_t *esp =
80 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
81 if (esp->add_del_sa_sess_cb)
82 {
83 clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
84 if (err)
85 return err;
86 }
87 return 0;
88}
89
90clib_error_t *
91ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
92{
93 clib_error_t *error = 0;
Matthew Smith461caa52018-12-21 11:53:16 -060094
95 if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH))
96 {
97 ipsec_ah_backend_t *ah =
98 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
99 ASSERT (ah->check_support_cb);
100 error = ah->check_support_cb (sa);
101 }
102 else
103 {
104 ipsec_esp_backend_t *esp =
105 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
106 ASSERT (esp->check_support_cb);
107 error = esp->check_support_cb (sa);
108 }
Klement Sekerab4d30532018-11-08 13:00:02 +0100109 return error;
110}
111
112
113static void
114ipsec_add_node (vlib_main_t * vm, const char *node_name,
115 const char *prev_node_name, u32 * out_node_index,
116 u32 * out_next_index)
117{
118 vlib_node_t *prev_node, *node;
119 prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
120 ASSERT (prev_node);
121 node = vlib_get_node_by_name (vm, (u8 *) node_name);
122 ASSERT (node);
123 *out_node_index = node->index;
124 *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
125}
126
127u32
128ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
129 const char *name,
130 const char *ah4_encrypt_node_name,
131 const char *ah4_decrypt_node_name,
132 const char *ah6_encrypt_node_name,
133 const char *ah6_decrypt_node_name,
134 check_support_cb_t ah_check_support_cb,
135 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
136{
137 ipsec_ah_backend_t *b;
138 pool_get (im->ah_backends, b);
139 b->name = format (NULL, "%s", name);
140
Pierre Pfister057b3562018-12-10 17:01:01 +0100141 ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100142 &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100143 ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100144 &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100145 ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100146 &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100147 ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100148 &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
149
150 b->check_support_cb = ah_check_support_cb;
151 b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
152 return b - im->ah_backends;
153}
154
155u32
156ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im,
157 const char *name,
158 const char *esp4_encrypt_node_name,
159 const char *esp4_decrypt_node_name,
160 const char *esp6_encrypt_node_name,
161 const char *esp6_decrypt_node_name,
162 check_support_cb_t esp_check_support_cb,
163 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb)
164{
165 ipsec_esp_backend_t *b;
166 pool_get (im->esp_backends, b);
167 b->name = format (NULL, "%s", name);
168
Pierre Pfister057b3562018-12-10 17:01:01 +0100169 ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100170 &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100171 ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100172 &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100173 ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100174 &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100175 ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100176 &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
177
178 b->check_support_cb = esp_check_support_cb;
179 b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
180 return b - im->esp_backends;
181}
182
Neale Rannsb4cfd552019-02-13 02:08:06 -0800183static walk_rc_t
184ipsec_sa_restack (ipsec_sa_t * sa, void *ctx)
185{
186 ipsec_sa_stack (sa);
187
188 return (WALK_CONTINUE);
189}
190
Klement Sekerab4d30532018-11-08 13:00:02 +0100191int
192ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
193{
194 if (pool_elts (im->sad) > 0
195 || pool_is_free_index (im->ah_backends, backend_idx))
196 {
197 return -1;
198 }
199 ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
200 im->ah_current_backend = backend_idx;
201 im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
202 im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
203 im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
204 im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
205 im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
206 im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
207 im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
208 im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
Neale Rannsb4cfd552019-02-13 02:08:06 -0800209
210 ipsec_sa_walk (ipsec_sa_restack, NULL);
Klement Sekerab4d30532018-11-08 13:00:02 +0100211 return 0;
212}
213
214int
215ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
216{
217 if (pool_elts (im->sad) > 0
218 || pool_is_free_index (im->esp_backends, backend_idx))
219 {
220 return -1;
221 }
222 ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
223 im->esp_current_backend = backend_idx;
224 im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
225 im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
226 im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
227 im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
228 im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
229 im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
230 im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
231 im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
Neale Rannsb4cfd552019-02-13 02:08:06 -0800232
233 ipsec_sa_walk (ipsec_sa_restack, NULL);
Klement Sekerab4d30532018-11-08 13:00:02 +0100234 return 0;
235}
236
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000237static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238ipsec_init (vlib_main_t * vm)
239{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700240 clib_error_t *error;
241 ipsec_main_t *im = &ipsec_main;
242 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700244 ipsec_rand_seed ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
Dave Barachb7b92992018-10-17 10:38:51 -0400246 clib_memset (im, 0, sizeof (im[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700248 im->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249 im->vlib_main = vm;
250
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700251 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
252 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
254
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700255 vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1,
256 CLIB_CACHE_LINE_BYTES);
Matthew Smith29d85102016-05-01 14:52:08 -0500257
Klement Sekerab4d30532018-11-08 13:00:02 +0100258 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700259 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 im->error_drop_node_index = node->index;
261
Klement Sekerab4d30532018-11-08 13:00:02 +0100262 u32 idx = ipsec_register_ah_backend (vm, im, "default openssl backend",
263 "ah4-encrypt",
264 "ah4-decrypt",
265 "ah6-encrypt",
266 "ah6-decrypt",
Klement Sekera4fd5a9d2019-01-30 11:11:23 +0100267 ipsec_check_ah_support,
Klement Sekerab4d30532018-11-08 13:00:02 +0100268 NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
Klement Sekerab4d30532018-11-08 13:00:02 +0100270 im->ah_default_backend = idx;
271 int rv = ipsec_select_ah_backend (im, idx);
272 ASSERT (0 == rv);
273 (void) (rv); // avoid warning
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000274
Klement Sekerab4d30532018-11-08 13:00:02 +0100275 idx = ipsec_register_esp_backend (vm, im, "default openssl backend",
276 "esp4-encrypt",
277 "esp4-decrypt",
278 "esp6-encrypt",
279 "esp6-decrypt",
Klement Sekera4fd5a9d2019-01-30 11:11:23 +0100280 ipsec_check_esp_support, NULL);
Klement Sekerab4d30532018-11-08 13:00:02 +0100281 im->esp_default_backend = idx;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800282
Klement Sekerab4d30532018-11-08 13:00:02 +0100283 rv = ipsec_select_esp_backend (im, idx);
284 ASSERT (0 == rv);
285 (void) (rv); // avoid warning
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
288 return error;
289
290 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
291 return error;
292
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800293 ipsec_proto_init ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 return 0;
296}
297
298VLIB_INIT_FUNCTION (ipsec_init);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700299
300/*
301 * fd.io coding-style-patch-verification: ON
302 *
303 * Local Variables:
304 * eval: (c-set-style "gnu")
305 * End:
306 */