blob: dc2f4cdbb607a2d6f0e61f0798f5dfa1ae33c6c5 [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;
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
30static clib_error_t *
Klement Sekera4fd5a9d2019-01-30 11:11:23 +010031ipsec_check_ah_support (ipsec_sa_t * sa)
32{
33 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
34 return clib_error_return (0, "unsupported none integ-alg");
35 return 0;
36}
37
38static clib_error_t *
39ipsec_check_esp_support (ipsec_sa_t * sa)
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +000040{
41 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
42 return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg");
Klement Sekera4fd5a9d2019-01-30 11:11:23 +010043 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_192)
44 return clib_error_return (0, "unsupported aes-gcm-192 crypto-alg");
45 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_256)
46 return clib_error_return (0, "unsupported aes-gcm-256 crypto-alg");
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +000047
48 return 0;
49}
50
Klement Sekerab4d30532018-11-08 13:00:02 +010051clib_error_t *
52ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
53{
54 ipsec_ah_backend_t *ah =
55 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
56 if (ah->add_del_sa_sess_cb)
57 {
58 clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
59 if (err)
60 return err;
61 }
62 ipsec_esp_backend_t *esp =
63 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
64 if (esp->add_del_sa_sess_cb)
65 {
66 clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
67 if (err)
68 return err;
69 }
70 return 0;
71}
72
73clib_error_t *
74ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
75{
76 clib_error_t *error = 0;
Matthew Smith461caa52018-12-21 11:53:16 -060077
78 if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH))
79 {
80 ipsec_ah_backend_t *ah =
81 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
82 ASSERT (ah->check_support_cb);
83 error = ah->check_support_cb (sa);
84 }
85 else
86 {
87 ipsec_esp_backend_t *esp =
88 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
89 ASSERT (esp->check_support_cb);
90 error = esp->check_support_cb (sa);
91 }
Klement Sekerab4d30532018-11-08 13:00:02 +010092 return error;
93}
94
95
96static void
97ipsec_add_node (vlib_main_t * vm, const char *node_name,
98 const char *prev_node_name, u32 * out_node_index,
99 u32 * out_next_index)
100{
101 vlib_node_t *prev_node, *node;
102 prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
103 ASSERT (prev_node);
104 node = vlib_get_node_by_name (vm, (u8 *) node_name);
105 ASSERT (node);
106 *out_node_index = node->index;
107 *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
108}
109
110u32
111ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
112 const char *name,
113 const char *ah4_encrypt_node_name,
114 const char *ah4_decrypt_node_name,
115 const char *ah6_encrypt_node_name,
116 const char *ah6_decrypt_node_name,
117 check_support_cb_t ah_check_support_cb,
118 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
119{
120 ipsec_ah_backend_t *b;
121 pool_get (im->ah_backends, b);
Kingwel Xie561d1ca2019-03-05 22:56:17 -0500122 b->name = format (0, "%s%c", name, 0);
Klement Sekerab4d30532018-11-08 13:00:02 +0100123
Pierre Pfister057b3562018-12-10 17:01:01 +0100124 ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100125 &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100126 ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100127 &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100128 ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100129 &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100130 ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100131 &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
132
133 b->check_support_cb = ah_check_support_cb;
134 b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
135 return b - im->ah_backends;
136}
137
138u32
139ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im,
140 const char *name,
141 const char *esp4_encrypt_node_name,
142 const char *esp4_decrypt_node_name,
143 const char *esp6_encrypt_node_name,
144 const char *esp6_decrypt_node_name,
145 check_support_cb_t esp_check_support_cb,
146 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb)
147{
148 ipsec_esp_backend_t *b;
149 pool_get (im->esp_backends, b);
Kingwel Xie561d1ca2019-03-05 22:56:17 -0500150 b->name = format (0, "%s%c", name, 0);
Klement Sekerab4d30532018-11-08 13:00:02 +0100151
Pierre Pfister057b3562018-12-10 17:01:01 +0100152 ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100153 &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100154 ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100155 &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100156 ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100157 &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
Pierre Pfister057b3562018-12-10 17:01:01 +0100158 ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature",
Klement Sekerab4d30532018-11-08 13:00:02 +0100159 &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
160
161 b->check_support_cb = esp_check_support_cb;
162 b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
163 return b - im->esp_backends;
164}
165
Neale Rannsb4cfd552019-02-13 02:08:06 -0800166static walk_rc_t
167ipsec_sa_restack (ipsec_sa_t * sa, void *ctx)
168{
169 ipsec_sa_stack (sa);
170
171 return (WALK_CONTINUE);
172}
173
Klement Sekerab4d30532018-11-08 13:00:02 +0100174int
175ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
176{
177 if (pool_elts (im->sad) > 0
178 || pool_is_free_index (im->ah_backends, backend_idx))
179 {
180 return -1;
181 }
182 ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
183 im->ah_current_backend = backend_idx;
184 im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
185 im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
186 im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
187 im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
188 im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
189 im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
190 im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
191 im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
Neale Rannsb4cfd552019-02-13 02:08:06 -0800192
193 ipsec_sa_walk (ipsec_sa_restack, NULL);
Klement Sekerab4d30532018-11-08 13:00:02 +0100194 return 0;
195}
196
197int
198ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
199{
200 if (pool_elts (im->sad) > 0
201 || pool_is_free_index (im->esp_backends, backend_idx))
202 {
203 return -1;
204 }
205 ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
206 im->esp_current_backend = backend_idx;
207 im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
208 im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
209 im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
210 im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
211 im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
212 im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
213 im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
214 im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
Neale Rannsb4cfd552019-02-13 02:08:06 -0800215
216 ipsec_sa_walk (ipsec_sa_restack, NULL);
Klement Sekerab4d30532018-11-08 13:00:02 +0100217 return 0;
218}
219
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000220static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221ipsec_init (vlib_main_t * vm)
222{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700223 clib_error_t *error;
224 ipsec_main_t *im = &ipsec_main;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100225 ipsec_main_crypto_alg_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700227 im->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228 im->vlib_main = vm;
229
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700230 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
231 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
233
Klement Sekerab4d30532018-11-08 13:00:02 +0100234 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700235 ASSERT (node);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236 im->error_drop_node_index = node->index;
237
Klement Sekerab4d30532018-11-08 13:00:02 +0100238 u32 idx = ipsec_register_ah_backend (vm, im, "default openssl backend",
239 "ah4-encrypt",
240 "ah4-decrypt",
241 "ah6-encrypt",
242 "ah6-decrypt",
Klement Sekera4fd5a9d2019-01-30 11:11:23 +0100243 ipsec_check_ah_support,
Klement Sekerab4d30532018-11-08 13:00:02 +0100244 NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
Klement Sekerab4d30532018-11-08 13:00:02 +0100246 im->ah_default_backend = idx;
247 int rv = ipsec_select_ah_backend (im, idx);
248 ASSERT (0 == rv);
249 (void) (rv); // avoid warning
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000250
Klement Sekerab4d30532018-11-08 13:00:02 +0100251 idx = ipsec_register_esp_backend (vm, im, "default openssl backend",
252 "esp4-encrypt",
253 "esp4-decrypt",
254 "esp6-encrypt",
255 "esp6-decrypt",
Klement Sekera4fd5a9d2019-01-30 11:11:23 +0100256 ipsec_check_esp_support, NULL);
Klement Sekerab4d30532018-11-08 13:00:02 +0100257 im->esp_default_backend = idx;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800258
Klement Sekerab4d30532018-11-08 13:00:02 +0100259 rv = ipsec_select_esp_backend (im, idx);
260 ASSERT (0 == rv);
261 (void) (rv); // avoid warning
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
264 return error;
265
266 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
267 return error;
268
Damjan Marion91f17dc2019-03-18 18:59:25 +0100269 vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
270
271 a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
Damjan Marion060bfb92019-03-29 13:47:54 +0100272 a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC;
273 a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100274 a->iv_size = a->block_size = 8;
275
276 a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
Damjan Marion060bfb92019-03-29 13:47:54 +0100277 a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC;
278 a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100279 a->iv_size = a->block_size = 8;
280
281 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
Damjan Marion060bfb92019-03-29 13:47:54 +0100282 a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC;
283 a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100284 a->iv_size = a->block_size = 16;
285
286 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
Damjan Marion060bfb92019-03-29 13:47:54 +0100287 a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC;
288 a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100289 a->iv_size = a->block_size = 16;
290
291 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
Damjan Marion060bfb92019-03-29 13:47:54 +0100292 a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC;
293 a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100294 a->iv_size = a->block_size = 16;
295
296 vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
297 ipsec_main_integ_alg_t *i;
298
299 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
Damjan Marion060bfb92019-03-29 13:47:54 +0100300 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200301 i->icv_size = 12;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100302
303 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
Damjan Marion060bfb92019-03-29 13:47:54 +0100304 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200305 i->icv_size = 12;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100306
307 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
Damjan Marion060bfb92019-03-29 13:47:54 +0100308 i->op_id = VNET_CRYPTO_OP_SHA256_HMAC;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200309 i->icv_size = 16;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100310
311 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
Damjan Marion060bfb92019-03-29 13:47:54 +0100312 i->op_id = VNET_CRYPTO_OP_SHA384_HMAC;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200313 i->icv_size = 24;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100314
315 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
Damjan Marion060bfb92019-03-29 13:47:54 +0100316 i->op_id = VNET_CRYPTO_OP_SHA512_HMAC;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200317 i->icv_size = 32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318
Neale Ranns5ae793a2019-04-03 13:36:56 +0000319 vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100320
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321 return 0;
322}
323
324VLIB_INIT_FUNCTION (ipsec_init);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700325
326/*
327 * fd.io coding-style-patch-verification: ON
328 *
329 * Local Variables:
330 * eval: (c-set-style "gnu")
331 * End:
332 */