blob: 95db17e7fb1e4e3b1235bf16178f15e8a5211111 [file] [log] [blame]
Neale Ranns999c8ee2019-02-01 03:31:24 -08001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Benoît Ganne5527a782022-01-18 15:56:41 +010016#include <sys/random.h>
Neale Ranns999c8ee2019-02-01 03:31:24 -080017#include <vnet/ipsec/ipsec.h>
Damjan Marionc59b9a22019-03-19 15:38:40 +010018#include <vnet/ipsec/esp.h>
Florin Corasb040f982020-10-20 14:59:43 -070019#include <vnet/udp/udp_local.h>
Neale Ranns8d7c5022019-02-06 01:41:05 -080020#include <vnet/fib/fib_table.h>
Neale Ranns1f50bf82019-07-16 15:28:52 +000021#include <vnet/fib/fib_entry_track.h>
Neale Rannsc87b66c2019-02-07 07:26:12 -080022#include <vnet/ipsec/ipsec_tun.h>
Arthur de Kerhorad95b062022-11-16 19:12:05 +010023#include <vnet/ipsec/ipsec.api_enum.h>
Neale Ranns999c8ee2019-02-01 03:31:24 -080024
Neale Rannseba31ec2019-02-17 18:04:27 +000025/**
26 * @brief
27 * SA packet & bytes counters
28 */
29vlib_combined_counter_main_t ipsec_sa_counters = {
30 .name = "SA",
31 .stat_segment_name = "/net/ipsec/sa",
32};
Arthur de Kerhorad95b062022-11-16 19:12:05 +010033/* Per-SA error counters */
34vlib_simple_counter_main_t ipsec_sa_err_counters[IPSEC_SA_N_ERRORS];
Neale Rannseba31ec2019-02-17 18:04:27 +000035
Neale Rannsc5fe57d2021-02-25 16:01:28 +000036ipsec_sa_t *ipsec_sa_pool;
Neale Rannseba31ec2019-02-17 18:04:27 +000037
Neale Ranns999c8ee2019-02-01 03:31:24 -080038static clib_error_t *
39ipsec_call_add_del_callbacks (ipsec_main_t * im, ipsec_sa_t * sa,
40 u32 sa_index, int is_add)
41{
42 ipsec_ah_backend_t *ab;
43 ipsec_esp_backend_t *eb;
44 switch (sa->protocol)
45 {
46 case IPSEC_PROTOCOL_AH:
47 ab = pool_elt_at_index (im->ah_backends, im->ah_current_backend);
48 if (ab->add_del_sa_sess_cb)
49 return ab->add_del_sa_sess_cb (sa_index, is_add);
50 break;
51 case IPSEC_PROTOCOL_ESP:
52 eb = pool_elt_at_index (im->esp_backends, im->esp_current_backend);
53 if (eb->add_del_sa_sess_cb)
54 return eb->add_del_sa_sess_cb (sa_index, is_add);
55 break;
56 }
57 return 0;
58}
59
Neale Ranns8d7c5022019-02-06 01:41:05 -080060void
61ipsec_mk_key (ipsec_key_t * key, const u8 * data, u8 len)
62{
63 memset (key, 0, sizeof (*key));
64
65 if (len > sizeof (key->data))
66 key->len = sizeof (key->data);
67 else
68 key->len = len;
69
70 memcpy (key->data, data, key->len);
71}
72
73/**
74 * 'stack' (resolve the recursion for) the SA tunnel destination
75 */
Neale Rannse8915fc2019-04-23 20:57:55 -040076static void
Neale Ranns8d7c5022019-02-06 01:41:05 -080077ipsec_sa_stack (ipsec_sa_t * sa)
78{
Neale Rannsb4cfd552019-02-13 02:08:06 -080079 ipsec_main_t *im = &ipsec_main;
Neale Ranns8d7c5022019-02-06 01:41:05 -080080 dpo_id_t tmp = DPO_INVALID;
Neale Ranns8d7c5022019-02-06 01:41:05 -080081
Neale Ranns9ec846c2021-02-09 14:04:02 +000082 tunnel_contribute_forwarding (&sa->tunnel, &tmp);
Neale Ranns8d7c5022019-02-06 01:41:05 -080083
Neale Ranns72f2a3a2019-06-17 15:43:38 +000084 if (IPSEC_PROTOCOL_AH == sa->protocol)
85 dpo_stack_from_node ((ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ?
86 im->ah6_encrypt_node_index :
87 im->ah4_encrypt_node_index), &sa->dpo, &tmp);
88 else
89 dpo_stack_from_node ((ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ?
90 im->esp6_encrypt_node_index :
91 im->esp4_encrypt_node_index), &sa->dpo, &tmp);
Neale Rannsb4cfd552019-02-13 02:08:06 -080092 dpo_reset (&tmp);
Neale Ranns8d7c5022019-02-06 01:41:05 -080093}
94
Damjan Marionb966e8b2019-03-20 16:07:09 +010095void
Benoît Ganne5527a782022-01-18 15:56:41 +010096ipsec_sa_set_async_mode (ipsec_sa_t *sa, int is_enabled)
97{
98 if (is_enabled)
99 {
100 sa->crypto_key_index = sa->crypto_async_key_index;
101 sa->crypto_enc_op_id = sa->crypto_async_enc_op_id;
102 sa->crypto_dec_op_id = sa->crypto_async_dec_op_id;
103 sa->integ_key_index = ~0;
104 sa->integ_op_id = ~0;
105 }
106 else
107 {
108 sa->crypto_key_index = sa->crypto_sync_key_index;
109 sa->crypto_enc_op_id = sa->crypto_sync_enc_op_id;
110 sa->crypto_dec_op_id = sa->crypto_sync_dec_op_id;
111 sa->integ_key_index = sa->integ_sync_key_index;
112 sa->integ_op_id = sa->integ_sync_op_id;
113 }
114}
115
116void
Damjan Marionb966e8b2019-03-20 16:07:09 +0100117ipsec_sa_set_crypto_alg (ipsec_sa_t * sa, ipsec_crypto_alg_t crypto_alg)
118{
119 ipsec_main_t *im = &ipsec_main;
120 sa->crypto_alg = crypto_alg;
121 sa->crypto_iv_size = im->crypto_algs[crypto_alg].iv_size;
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500122 sa->esp_block_align = clib_max (4, im->crypto_algs[crypto_alg].block_align);
Benoît Ganne5527a782022-01-18 15:56:41 +0100123 sa->crypto_sync_enc_op_id = im->crypto_algs[crypto_alg].enc_op_id;
124 sa->crypto_sync_dec_op_id = im->crypto_algs[crypto_alg].dec_op_id;
Damjan Mariond1bed682019-04-24 15:20:35 +0200125 sa->crypto_calg = im->crypto_algs[crypto_alg].alg;
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100126 ASSERT (sa->crypto_iv_size <= ESP_MAX_IV_SIZE);
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -0500127 ASSERT (sa->esp_block_align <= ESP_MAX_BLOCK_SIZE);
Vladimir Ratnikovd7c030d2022-09-13 13:09:53 +0000128 if (IPSEC_CRYPTO_ALG_IS_GCM (crypto_alg) ||
129 IPSEC_CRYPTO_ALG_CTR_AEAD_OTHERS (crypto_alg))
Neale Ranns47feb112019-04-11 15:14:07 +0000130 {
131 sa->integ_icv_size = im->crypto_algs[crypto_alg].icv_size;
Benoît Ganne490b9272021-01-22 18:03:09 +0100132 ipsec_sa_set_IS_CTR (sa);
Neale Ranns47feb112019-04-11 15:14:07 +0000133 ipsec_sa_set_IS_AEAD (sa);
134 }
Benoît Ganne490b9272021-01-22 18:03:09 +0100135 else if (IPSEC_CRYPTO_ALG_IS_CTR (crypto_alg))
136 {
137 ipsec_sa_set_IS_CTR (sa);
138 }
Damjan Marionb966e8b2019-03-20 16:07:09 +0100139}
140
141void
142ipsec_sa_set_integ_alg (ipsec_sa_t * sa, ipsec_integ_alg_t integ_alg)
143{
144 ipsec_main_t *im = &ipsec_main;
145 sa->integ_alg = integ_alg;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200146 sa->integ_icv_size = im->integ_algs[integ_alg].icv_size;
Benoît Ganne5527a782022-01-18 15:56:41 +0100147 sa->integ_sync_op_id = im->integ_algs[integ_alg].op_id;
Damjan Mariond1bed682019-04-24 15:20:35 +0200148 sa->integ_calg = im->integ_algs[integ_alg].alg;
Damjan Marion7c22ff72019-04-04 12:25:44 +0200149 ASSERT (sa->integ_icv_size <= ESP_MAX_ICV_SIZE);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100150}
151
Fan Zhangf5395782020-04-29 14:00:03 +0100152void
153ipsec_sa_set_async_op_ids (ipsec_sa_t * sa)
154{
155 /* *INDENT-OFF* */
156 if (ipsec_sa_is_set_USE_ESN (sa))
157 {
Benoît Ganne5527a782022-01-18 15:56:41 +0100158#define _(n, s, k) \
159 if (sa->crypto_sync_enc_op_id == VNET_CRYPTO_OP_##n##_ENC) \
160 sa->crypto_async_enc_op_id = VNET_CRYPTO_OP_##n##_TAG16_AAD12_ENC; \
161 if (sa->crypto_sync_dec_op_id == VNET_CRYPTO_OP_##n##_DEC) \
162 sa->crypto_async_dec_op_id = VNET_CRYPTO_OP_##n##_TAG16_AAD12_DEC;
163 foreach_crypto_aead_alg
Fan Zhangf5395782020-04-29 14:00:03 +0100164#undef _
165 }
166 else
167 {
Benoît Ganne5527a782022-01-18 15:56:41 +0100168#define _(n, s, k) \
169 if (sa->crypto_sync_enc_op_id == VNET_CRYPTO_OP_##n##_ENC) \
170 sa->crypto_async_enc_op_id = VNET_CRYPTO_OP_##n##_TAG16_AAD8_ENC; \
171 if (sa->crypto_sync_dec_op_id == VNET_CRYPTO_OP_##n##_DEC) \
172 sa->crypto_async_dec_op_id = VNET_CRYPTO_OP_##n##_TAG16_AAD8_DEC;
173 foreach_crypto_aead_alg
Fan Zhangf5395782020-04-29 14:00:03 +0100174#undef _
175 }
176
Benoît Ganne5527a782022-01-18 15:56:41 +0100177#define _(c, h, s, k, d) \
178 if (sa->crypto_sync_enc_op_id == VNET_CRYPTO_OP_##c##_ENC && \
179 sa->integ_sync_op_id == VNET_CRYPTO_OP_##h##_HMAC) \
180 sa->crypto_async_enc_op_id = VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC; \
181 if (sa->crypto_sync_dec_op_id == VNET_CRYPTO_OP_##c##_DEC && \
182 sa->integ_sync_op_id == VNET_CRYPTO_OP_##h##_HMAC) \
183 sa->crypto_async_dec_op_id = VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC;
Fan Zhangf5395782020-04-29 14:00:03 +0100184 foreach_crypto_link_async_alg
185#undef _
186 /* *INDENT-ON* */
187}
188
Neale Ranns999c8ee2019-02-01 03:31:24 -0800189int
Arthur de Kerhor4117b242022-08-31 19:13:03 +0200190ipsec_sa_update (u32 id, u16 src_port, u16 dst_port, const tunnel_t *tun,
191 bool is_tun)
192{
193 ipsec_main_t *im = &ipsec_main;
194 ipsec_sa_t *sa;
195 u32 sa_index;
196 uword *p;
197 int rv;
198
199 p = hash_get (im->sa_index_by_sa_id, id);
200 if (!p)
201 return VNET_API_ERROR_NO_SUCH_ENTRY;
202
203 sa = ipsec_sa_get (p[0]);
204 sa_index = sa - ipsec_sa_pool;
205
206 if (is_tun && ipsec_sa_is_set_IS_TUNNEL (sa) &&
207 (ip_address_cmp (&tun->t_src, &sa->tunnel.t_src) != 0 ||
208 ip_address_cmp (&tun->t_dst, &sa->tunnel.t_dst) != 0))
209 {
210 /* if the source IP is updated for an inbound SA under a tunnel protect,
211 we need to update the tun_protect DB with the new src IP */
212 if (ipsec_sa_is_set_IS_INBOUND (sa) &&
213 ip_address_cmp (&tun->t_src, &sa->tunnel.t_src) != 0 &&
214 !ip46_address_is_zero (&tun->t_src.ip))
215 {
216 if (ip46_address_is_ip4 (&sa->tunnel.t_src.ip))
217 {
218 ipsec4_tunnel_kv_t old_key, new_key;
219 clib_bihash_kv_8_16_t res,
220 *bkey = (clib_bihash_kv_8_16_t *) &old_key;
221
222 ipsec4_tunnel_mk_key (&old_key, &sa->tunnel.t_src.ip.ip4,
223 clib_host_to_net_u32 (sa->spi));
224 ipsec4_tunnel_mk_key (&new_key, &tun->t_src.ip.ip4,
225 clib_host_to_net_u32 (sa->spi));
226
227 if (!clib_bihash_search_8_16 (&im->tun4_protect_by_key, bkey,
228 &res))
229 {
230 clib_bihash_add_del_8_16 (&im->tun4_protect_by_key, &res, 0);
231 res.key = new_key.key;
232 clib_bihash_add_del_8_16 (&im->tun4_protect_by_key, &res, 1);
233 }
234 }
235 else
236 {
237 ipsec6_tunnel_kv_t old_key = {
238 .key = {
239 .remote_ip = sa->tunnel.t_src.ip.ip6,
240 .spi = clib_host_to_net_u32 (sa->spi),
241 },
242 }, new_key = {
243 .key = {
244 .remote_ip = tun->t_src.ip.ip6,
245 .spi = clib_host_to_net_u32 (sa->spi),
246 }};
247 clib_bihash_kv_24_16_t res,
248 *bkey = (clib_bihash_kv_24_16_t *) &old_key;
249
250 if (!clib_bihash_search_24_16 (&im->tun6_protect_by_key, bkey,
251 &res))
252 {
253 clib_bihash_add_del_24_16 (&im->tun6_protect_by_key, &res,
254 0);
255 clib_memcpy (&res.key, &new_key.key, 3);
256 clib_bihash_add_del_24_16 (&im->tun6_protect_by_key, &res,
257 1);
258 }
259 }
260 }
261 tunnel_unresolve (&sa->tunnel);
262 tunnel_copy (tun, &sa->tunnel);
263 if (!ipsec_sa_is_set_IS_INBOUND (sa))
264 {
265 dpo_reset (&sa->dpo);
266
267 sa->tunnel_flags = sa->tunnel.t_encap_decap_flags;
268
269 rv = tunnel_resolve (&sa->tunnel, FIB_NODE_TYPE_IPSEC_SA, sa_index);
270
271 if (rv)
272 {
273 hash_unset (im->sa_index_by_sa_id, sa->id);
274 pool_put (ipsec_sa_pool, sa);
275 return rv;
276 }
277 ipsec_sa_stack (sa);
278 /* generate header templates */
279 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
280 {
281 tunnel_build_v6_hdr (&sa->tunnel,
282 (ipsec_sa_is_set_UDP_ENCAP (sa) ?
283 IP_PROTOCOL_UDP :
284 IP_PROTOCOL_IPSEC_ESP),
285 &sa->ip6_hdr);
286 }
287 else
288 {
289 tunnel_build_v4_hdr (&sa->tunnel,
290 (ipsec_sa_is_set_UDP_ENCAP (sa) ?
291 IP_PROTOCOL_UDP :
292 IP_PROTOCOL_IPSEC_ESP),
293 &sa->ip4_hdr);
294 }
295 }
296 }
297
298 if (ipsec_sa_is_set_UDP_ENCAP (sa))
299 {
300 if (dst_port != IPSEC_UDP_PORT_NONE &&
301 dst_port != clib_net_to_host_u16 (sa->udp_hdr.dst_port))
302 {
303 if (ipsec_sa_is_set_IS_INBOUND (sa))
304 {
305 ipsec_unregister_udp_port (
306 clib_net_to_host_u16 (sa->udp_hdr.dst_port),
307 !ipsec_sa_is_set_IS_TUNNEL_V6 (sa));
308 ipsec_register_udp_port (dst_port,
309 !ipsec_sa_is_set_IS_TUNNEL_V6 (sa));
310 }
311 sa->udp_hdr.dst_port = clib_host_to_net_u16 (dst_port);
312 }
313 if (src_port != IPSEC_UDP_PORT_NONE &&
314 src_port != clib_net_to_host_u16 (sa->udp_hdr.src_port))
315 sa->udp_hdr.src_port = clib_host_to_net_u16 (src_port);
316 }
317 return (0);
318}
319
320int
Neale Ranns9ec846c2021-02-09 14:04:02 +0000321ipsec_sa_add_and_lock (u32 id, u32 spi, ipsec_protocol_t proto,
322 ipsec_crypto_alg_t crypto_alg, const ipsec_key_t *ck,
323 ipsec_integ_alg_t integ_alg, const ipsec_key_t *ik,
324 ipsec_sa_flags_t flags, u32 salt, u16 src_port,
325 u16 dst_port, const tunnel_t *tun, u32 *sa_out_index)
Neale Ranns8d7c5022019-02-06 01:41:05 -0800326{
Damjan Mariond1bed682019-04-24 15:20:35 +0200327 vlib_main_t *vm = vlib_get_main ();
Neale Ranns8d7c5022019-02-06 01:41:05 -0800328 ipsec_main_t *im = &ipsec_main;
329 clib_error_t *err;
330 ipsec_sa_t *sa;
331 u32 sa_index;
Benoît Ganne5527a782022-01-18 15:56:41 +0100332 u64 rand[2];
Neale Ranns8d7c5022019-02-06 01:41:05 -0800333 uword *p;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000334 int rv;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800335
336 p = hash_get (im->sa_index_by_sa_id, id);
337 if (p)
338 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
339
Benoît Ganne5527a782022-01-18 15:56:41 +0100340 if (getrandom (rand, sizeof (rand), 0) != sizeof (rand))
341 return VNET_API_ERROR_INIT_FAILED;
342
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000343 pool_get_aligned_zero (ipsec_sa_pool, sa, CLIB_CACHE_LINE_BYTES);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800344
Benoît Ganne5527a782022-01-18 15:56:41 +0100345 clib_pcg64i_srandom_r (&sa->iv_prng, rand[0], rand[1]);
346
Neale Ranns8d7c5022019-02-06 01:41:05 -0800347 fib_node_init (&sa->node, FIB_NODE_TYPE_IPSEC_SA);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000348 fib_node_lock (&sa->node);
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000349 sa_index = sa - ipsec_sa_pool;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800350
Neale Rannseba31ec2019-02-17 18:04:27 +0000351 vlib_validate_combined_counter (&ipsec_sa_counters, sa_index);
352 vlib_zero_combined_counter (&ipsec_sa_counters, sa_index);
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100353 for (int i = 0; i < IPSEC_SA_N_ERRORS; i++)
354 {
355 vlib_validate_simple_counter (&ipsec_sa_err_counters[i], sa_index);
356 vlib_zero_simple_counter (&ipsec_sa_err_counters[i], sa_index);
357 }
Neale Rannseba31ec2019-02-17 18:04:27 +0000358
Neale Ranns9ec846c2021-02-09 14:04:02 +0000359 tunnel_copy (tun, &sa->tunnel);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800360 sa->id = id;
361 sa->spi = spi;
Neale Rannseba31ec2019-02-17 18:04:27 +0000362 sa->stat_index = sa_index;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800363 sa->protocol = proto;
Neale Ranns2b5ba952019-04-02 10:15:40 +0000364 sa->flags = flags;
Neale Ranns47feb112019-04-11 15:14:07 +0000365 sa->salt = salt;
Neale Ranns1a52d372021-02-04 11:33:32 +0000366 sa->thread_index = (vlib_num_workers ()) ? ~0 : 0;
Filip Tehlarc41217a2019-10-18 17:51:06 +0000367 if (integ_alg != IPSEC_INTEG_ALG_NONE)
368 {
369 ipsec_sa_set_integ_alg (sa, integ_alg);
370 clib_memcpy (&sa->integ_key, ik, sizeof (sa->integ_key));
371 }
Neale Ranns47feb112019-04-11 15:14:07 +0000372 ipsec_sa_set_crypto_alg (sa, crypto_alg);
Fan Zhangf5395782020-04-29 14:00:03 +0100373 ipsec_sa_set_async_op_ids (sa);
374
Neale Ranns47feb112019-04-11 15:14:07 +0000375 clib_memcpy (&sa->crypto_key, ck, sizeof (sa->crypto_key));
Neale Ranns8d7c5022019-02-06 01:41:05 -0800376
Benoît Ganne5527a782022-01-18 15:56:41 +0100377 sa->crypto_sync_key_index = vnet_crypto_key_add (
378 vm, im->crypto_algs[crypto_alg].alg, (u8 *) ck->data, ck->len);
379 if (~0 == sa->crypto_sync_key_index)
Neale Rannse6be7022019-06-04 15:37:34 +0000380 {
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000381 pool_put (ipsec_sa_pool, sa);
Neale Rannse6be7022019-06-04 15:37:34 +0000382 return VNET_API_ERROR_KEY_LENGTH;
383 }
Benoît Gannebe954442019-04-29 16:05:46 +0200384
Filip Tehlarc41217a2019-10-18 17:51:06 +0000385 if (integ_alg != IPSEC_INTEG_ALG_NONE)
Neale Rannse6be7022019-06-04 15:37:34 +0000386 {
Benoît Ganne5527a782022-01-18 15:56:41 +0100387 sa->integ_sync_key_index = vnet_crypto_key_add (
388 vm, im->integ_algs[integ_alg].alg, (u8 *) ik->data, ik->len);
389 if (~0 == sa->integ_sync_key_index)
Filip Tehlarc41217a2019-10-18 17:51:06 +0000390 {
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000391 pool_put (ipsec_sa_pool, sa);
Filip Tehlarc41217a2019-10-18 17:51:06 +0000392 return VNET_API_ERROR_KEY_LENGTH;
393 }
Neale Rannse6be7022019-06-04 15:37:34 +0000394 }
Damjan Mariond1bed682019-04-24 15:20:35 +0200395
Benoît Ganne5527a782022-01-18 15:56:41 +0100396 if (sa->crypto_async_enc_op_id && !ipsec_sa_is_set_IS_AEAD (sa))
397 sa->crypto_async_key_index =
398 vnet_crypto_key_add_linked (vm, sa->crypto_sync_key_index,
399 sa->integ_sync_key_index); // AES-CBC & HMAC
400 else
401 sa->crypto_async_key_index = sa->crypto_sync_key_index;
Fan Zhangf5395782020-04-29 14:00:03 +0100402
403 if (im->async_mode)
Benoît Ganne5527a782022-01-18 15:56:41 +0100404 {
405 ipsec_sa_set_async_mode (sa, 1);
406 }
407 else if (ipsec_sa_is_set_IS_ASYNC (sa))
408 {
409 vnet_crypto_request_async_mode (1);
410 ipsec_sa_set_async_mode (sa, 1 /* is_enabled */);
411 }
Fan Zhangf5395782020-04-29 14:00:03 +0100412 else
Neale Rannsf16e9a52021-02-25 19:09:24 +0000413 {
Benoît Ganne5527a782022-01-18 15:56:41 +0100414 ipsec_sa_set_async_mode (sa, 0 /* is_enabled */);
Neale Rannsf16e9a52021-02-25 19:09:24 +0000415 }
Fan Zhangf5395782020-04-29 14:00:03 +0100416
Neale Ranns8d7c5022019-02-06 01:41:05 -0800417 err = ipsec_check_support_cb (im, sa);
418 if (err)
419 {
420 clib_warning ("%s", err->what);
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000421 pool_put (ipsec_sa_pool, sa);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800422 return VNET_API_ERROR_UNIMPLEMENTED;
423 }
424
425 err = ipsec_call_add_del_callbacks (im, sa, sa_index, 1);
426 if (err)
427 {
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000428 pool_put (ipsec_sa_pool, sa);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800429 return VNET_API_ERROR_SYSCALL_ERROR_1;
430 }
431
Neale Ranns53dd08c2021-06-24 15:41:03 +0000432 if (ipsec_sa_is_set_IS_TUNNEL (sa) &&
433 AF_IP6 == ip_addr_version (&tun->t_src))
434 ipsec_sa_set_IS_TUNNEL_V6 (sa);
435
Neale Ranns2b5ba952019-04-02 10:15:40 +0000436 if (ipsec_sa_is_set_IS_TUNNEL (sa) && !ipsec_sa_is_set_IS_INBOUND (sa))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800437 {
Neale Ranns9ec846c2021-02-09 14:04:02 +0000438 sa->tunnel_flags = sa->tunnel.t_encap_decap_flags;
439
440 rv = tunnel_resolve (&sa->tunnel, FIB_NODE_TYPE_IPSEC_SA, sa_index);
441
442 if (rv)
Neale Ranns8d7c5022019-02-06 01:41:05 -0800443 {
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000444 pool_put (ipsec_sa_pool, sa);
Neale Ranns9ec846c2021-02-09 14:04:02 +0000445 return rv;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800446 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800447 ipsec_sa_stack (sa);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100448
449 /* generate header templates */
Damjan Mariond709cbc2019-03-26 13:16:42 +0100450 if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100451 {
Neale Ranns9ec846c2021-02-09 14:04:02 +0000452 tunnel_build_v6_hdr (&sa->tunnel,
453 (ipsec_sa_is_set_UDP_ENCAP (sa) ?
454 IP_PROTOCOL_UDP :
455 IP_PROTOCOL_IPSEC_ESP),
456 &sa->ip6_hdr);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100457 }
458 else
459 {
Neale Ranns9ec846c2021-02-09 14:04:02 +0000460 tunnel_build_v4_hdr (&sa->tunnel,
461 (ipsec_sa_is_set_UDP_ENCAP (sa) ?
462 IP_PROTOCOL_UDP :
463 IP_PROTOCOL_IPSEC_ESP),
464 &sa->ip4_hdr);
Damjan Marionc59b9a22019-03-19 15:38:40 +0100465 }
Neale Ranns8d7c5022019-02-06 01:41:05 -0800466 }
Damjan Marionc59b9a22019-03-19 15:38:40 +0100467
Damjan Mariond709cbc2019-03-26 13:16:42 +0100468 if (ipsec_sa_is_set_UDP_ENCAP (sa))
Damjan Marionc59b9a22019-03-19 15:38:40 +0100469 {
Filip Tehlare5d34912020-03-02 15:17:37 +0000470 if (dst_port == IPSEC_UDP_PORT_NONE)
Neale Rannsabc56602020-04-01 09:45:23 +0000471 sa->udp_hdr.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
Filip Tehlare5d34912020-03-02 15:17:37 +0000472 else
Neale Rannsabc56602020-04-01 09:45:23 +0000473 sa->udp_hdr.dst_port = clib_host_to_net_u16 (dst_port);
474
475 if (src_port == IPSEC_UDP_PORT_NONE)
476 sa->udp_hdr.src_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
477 else
478 sa->udp_hdr.src_port = clib_host_to_net_u16 (src_port);
479
480 if (ipsec_sa_is_set_IS_INBOUND (sa))
Matthew Smith6f1eb482022-08-09 22:19:38 +0000481 ipsec_register_udp_port (clib_host_to_net_u16 (sa->udp_hdr.dst_port),
482 !ipsec_sa_is_set_IS_TUNNEL_V6 (sa));
Damjan Marionc59b9a22019-03-19 15:38:40 +0100483 }
484
Neale Ranns8d7c5022019-02-06 01:41:05 -0800485 hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
486
487 if (sa_out_index)
488 *sa_out_index = sa_index;
489
490 return (0);
491}
492
Neale Ranns495d7ff2019-07-12 09:15:26 +0000493static void
494ipsec_sa_del (ipsec_sa_t * sa)
Neale Ranns999c8ee2019-02-01 03:31:24 -0800495{
Damjan Mariond1bed682019-04-24 15:20:35 +0200496 vlib_main_t *vm = vlib_get_main ();
Neale Ranns999c8ee2019-02-01 03:31:24 -0800497 ipsec_main_t *im = &ipsec_main;
Neale Ranns999c8ee2019-02-01 03:31:24 -0800498 u32 sa_index;
Neale Ranns999c8ee2019-02-01 03:31:24 -0800499
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000500 sa_index = sa - ipsec_sa_pool;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800501 hash_unset (im->sa_index_by_sa_id, sa->id);
Neale Ranns9ec846c2021-02-09 14:04:02 +0000502 tunnel_unresolve (&sa->tunnel);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000503
504 /* no recovery possible when deleting an SA */
505 (void) ipsec_call_add_del_callbacks (im, sa, sa_index, 0);
Damjan Mariond709cbc2019-03-26 13:16:42 +0100506
Neale Rannsf16e9a52021-02-25 19:09:24 +0000507 if (ipsec_sa_is_set_IS_ASYNC (sa))
Benoît Ganneab412cd2023-01-03 18:35:04 +0100508 {
509 vnet_crypto_request_async_mode (0);
510 if (!ipsec_sa_is_set_IS_AEAD (sa))
Benoît Ganne5527a782022-01-18 15:56:41 +0100511 vnet_crypto_key_del (vm, sa->crypto_async_key_index);
Benoît Ganneab412cd2023-01-03 18:35:04 +0100512 }
513
Neale Rannsabc56602020-04-01 09:45:23 +0000514 if (ipsec_sa_is_set_UDP_ENCAP (sa) && ipsec_sa_is_set_IS_INBOUND (sa))
Matthew Smith6f1eb482022-08-09 22:19:38 +0000515 ipsec_unregister_udp_port (clib_net_to_host_u16 (sa->udp_hdr.dst_port),
516 !ipsec_sa_is_set_IS_TUNNEL_V6 (sa));
Neale Rannsabc56602020-04-01 09:45:23 +0000517
Neale Ranns2b5ba952019-04-02 10:15:40 +0000518 if (ipsec_sa_is_set_IS_TUNNEL (sa) && !ipsec_sa_is_set_IS_INBOUND (sa))
Neale Ranns9ec846c2021-02-09 14:04:02 +0000519 dpo_reset (&sa->dpo);
Benoît Ganne5527a782022-01-18 15:56:41 +0100520 vnet_crypto_key_del (vm, sa->crypto_sync_key_index);
Filip Tehlarc41217a2019-10-18 17:51:06 +0000521 if (sa->integ_alg != IPSEC_INTEG_ALG_NONE)
Benoît Ganne5527a782022-01-18 15:56:41 +0100522 vnet_crypto_key_del (vm, sa->integ_sync_key_index);
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000523 pool_put (ipsec_sa_pool, sa);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000524}
525
526void
527ipsec_sa_unlock (index_t sai)
528{
Neale Ranns495d7ff2019-07-12 09:15:26 +0000529 ipsec_sa_t *sa;
530
531 if (INDEX_INVALID == sai)
532 return;
533
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000534 sa = ipsec_sa_get (sai);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000535
536 fib_node_unlock (&sa->node);
537}
538
Neale Ranns12989b52019-09-26 16:20:19 +0000539void
540ipsec_sa_lock (index_t sai)
541{
Neale Ranns12989b52019-09-26 16:20:19 +0000542 ipsec_sa_t *sa;
543
544 if (INDEX_INVALID == sai)
545 return;
546
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000547 sa = ipsec_sa_get (sai);
Neale Ranns12989b52019-09-26 16:20:19 +0000548
549 fib_node_lock (&sa->node);
550}
551
Neale Ranns495d7ff2019-07-12 09:15:26 +0000552index_t
553ipsec_sa_find_and_lock (u32 id)
554{
555 ipsec_main_t *im = &ipsec_main;
556 ipsec_sa_t *sa;
557 uword *p;
558
559 p = hash_get (im->sa_index_by_sa_id, id);
560
561 if (!p)
562 return INDEX_INVALID;
563
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000564 sa = ipsec_sa_get (p[0]);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000565
566 fib_node_lock (&sa->node);
567
568 return (p[0]);
569}
570
571int
572ipsec_sa_unlock_id (u32 id)
573{
574 ipsec_main_t *im = &ipsec_main;
575 uword *p;
576
577 p = hash_get (im->sa_index_by_sa_id, id);
578
579 if (!p)
580 return VNET_API_ERROR_NO_SUCH_ENTRY;
581
582 ipsec_sa_unlock (p[0]);
583
584 return (0);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800585}
586
Neale Rannsc87b66c2019-02-07 07:26:12 -0800587void
588ipsec_sa_clear (index_t sai)
589{
590 vlib_zero_combined_counter (&ipsec_sa_counters, sai);
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100591 for (int i = 0; i < IPSEC_SA_N_ERRORS; i++)
592 vlib_zero_simple_counter (&ipsec_sa_err_counters[i], sai);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800593}
594
Neale Rannsb4cfd552019-02-13 02:08:06 -0800595void
596ipsec_sa_walk (ipsec_sa_walk_cb_t cb, void *ctx)
597{
Neale Rannsb4cfd552019-02-13 02:08:06 -0800598 ipsec_sa_t *sa;
599
600 /* *INDENT-OFF* */
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000601 pool_foreach (sa, ipsec_sa_pool)
602 {
603 if (WALK_CONTINUE != cb (sa, ctx))
604 break;
605 }
Neale Rannsb4cfd552019-02-13 02:08:06 -0800606 /* *INDENT-ON* */
607}
608
Neale Ranns8d7c5022019-02-06 01:41:05 -0800609/**
610 * Function definition to get a FIB node from its index
611 */
612static fib_node_t *
613ipsec_sa_fib_node_get (fib_node_index_t index)
614{
Neale Ranns8d7c5022019-02-06 01:41:05 -0800615 ipsec_sa_t *sa;
616
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000617 sa = ipsec_sa_get (index);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800618
619 return (&sa->node);
620}
621
Neale Ranns495d7ff2019-07-12 09:15:26 +0000622static ipsec_sa_t *
623ipsec_sa_from_fib_node (fib_node_t * node)
624{
625 ASSERT (FIB_NODE_TYPE_IPSEC_SA == node->fn_type);
626 return ((ipsec_sa_t *) (((char *) node) -
627 STRUCT_OFFSET_OF (ipsec_sa_t, node)));
628
629}
630
Neale Ranns8d7c5022019-02-06 01:41:05 -0800631/**
632 * Function definition to inform the FIB node that its last lock has gone.
633 */
634static void
635ipsec_sa_last_lock_gone (fib_node_t * node)
636{
637 /*
638 * The ipsec SA is a root of the graph. As such
639 * it never has children and thus is never locked.
640 */
Neale Ranns495d7ff2019-07-12 09:15:26 +0000641 ipsec_sa_del (ipsec_sa_from_fib_node (node));
Neale Ranns8d7c5022019-02-06 01:41:05 -0800642}
643
644/**
645 * Function definition to backwalk a FIB node
646 */
647static fib_node_back_walk_rc_t
648ipsec_sa_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
649{
650 ipsec_sa_stack (ipsec_sa_from_fib_node (node));
651
652 return (FIB_NODE_BACK_WALK_CONTINUE);
653}
654
655/*
Neale Rannsc87b66c2019-02-07 07:26:12 -0800656 * Virtual function table registered by SAs
Neale Ranns8d7c5022019-02-06 01:41:05 -0800657 * for participation in the FIB object graph.
658 */
659const static fib_node_vft_t ipsec_sa_vft = {
660 .fnv_get = ipsec_sa_fib_node_get,
661 .fnv_last_lock = ipsec_sa_last_lock_gone,
662 .fnv_back_walk = ipsec_sa_back_walk,
663};
664
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100665/* Init per-SA error counters and node type */
Neale Ranns8d7c5022019-02-06 01:41:05 -0800666clib_error_t *
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100667ipsec_sa_init (vlib_main_t *vm)
Neale Ranns8d7c5022019-02-06 01:41:05 -0800668{
669 fib_node_register_type (FIB_NODE_TYPE_IPSEC_SA, &ipsec_sa_vft);
670
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100671#define _(index, val, err, desc) \
672 ipsec_sa_err_counters[index].name = \
673 (char *) format (0, "SA-" #err "%c", 0); \
674 ipsec_sa_err_counters[index].stat_segment_name = \
675 (char *) format (0, "/net/ipsec/sa/err/" #err "%c", 0); \
676 ipsec_sa_err_counters[index].counters = 0;
677 foreach_ipsec_sa_err
678#undef _
679 return 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800680}
681
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100682VLIB_INIT_FUNCTION (ipsec_sa_init);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800683
Neale Ranns999c8ee2019-02-01 03:31:24 -0800684/*
685 * fd.io coding-style-patch-verification: ON
686 *
687 * Local Variables:
688 * eval: (c-set-style "gnu")
689 * End:
690 */