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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/ip/ip.h>
19 #include <vnet/feature/feature.h>
21 #include <openssl/hmac.h>
22 #include <openssl/rand.h>
23 #include <openssl/evp.h>
25 #include <vppinfra/types.h>
26 #include <vppinfra/cache.h>
28 #define IPSEC_FLAG_IPSEC_GRE_TUNNEL (1 << 0)
30 #define foreach_ipsec_output_next \
31 _ (DROP, "error-drop") \
32 _ (ESP4_ENCRYPT, "esp4-encrypt") \
33 _ (AH4_ENCRYPT, "ah4-encrypt") \
34 _ (ESP6_ENCRYPT, "esp6-encrypt") \
35 _ (AH6_ENCRYPT, "ah6-encrypt")
37 #define _(v, s) IPSEC_OUTPUT_NEXT_##v,
40 foreach_ipsec_output_next
43 } ipsec_output_next_t;
45 #define foreach_ipsec_input_next \
46 _ (DROP, "error-drop") \
47 _ (ESP4_DECRYPT, "esp4-decrypt") \
48 _ (AH4_DECRYPT, "ah4-decrypt") \
49 _ (ESP6_DECRYPT, "esp6-decrypt") \
50 _ (AH6_DECRYPT, "ah6-decrypt")
52 #define _(v, s) IPSEC_INPUT_NEXT_##v,
55 foreach_ipsec_input_next
60 #define foreach_ipsec_policy_action \
61 _ (0, BYPASS, "bypass") \
62 _ (1, DISCARD, "discard") \
63 _ (2, RESOLVE, "resolve") \
64 _ (3, PROTECT, "protect")
68 #define _(v, f, s) IPSEC_POLICY_ACTION_##f = v,
69 foreach_ipsec_policy_action
71 IPSEC_POLICY_N_ACTION,
72 } ipsec_policy_action_t;
74 #define foreach_ipsec_crypto_alg \
76 _ (1, AES_CBC_128, "aes-cbc-128") \
77 _ (2, AES_CBC_192, "aes-cbc-192") \
78 _ (3, AES_CBC_256, "aes-cbc-256") \
79 _ (4, AES_CTR_128, "aes-ctr-128") \
80 _ (5, AES_CTR_192, "aes-ctr-192") \
81 _ (6, AES_CTR_256, "aes-ctr-256") \
82 _ (7, AES_GCM_128, "aes-gcm-128") \
83 _ (8, AES_GCM_192, "aes-gcm-192") \
84 _ (9, AES_GCM_256, "aes-gcm-256") \
85 _ (10, DES_CBC, "des-cbc") \
86 _ (11, 3DES_CBC, "3des-cbc")
90 #define _(v, f, s) IPSEC_CRYPTO_ALG_##f = v,
91 foreach_ipsec_crypto_alg
96 #define foreach_ipsec_integ_alg \
98 _ (1, MD5_96, "md5-96") /* RFC2403 */ \
99 _ (2, SHA1_96, "sha1-96") /* RFC2404 */ \
100 _ (3, SHA_256_96, "sha-256-96") /* draft-ietf-ipsec-ciph-sha-256-00 */ \
101 _ (4, SHA_256_128, "sha-256-128") /* RFC4868 */ \
102 _ (5, SHA_384_192, "sha-384-192") /* RFC4868 */ \
103 _ (6, SHA_512_256, "sha-512-256") /* RFC4868 */
107 #define _(v, f, s) IPSEC_INTEG_ALG_##f = v,
108 foreach_ipsec_integ_alg
115 IPSEC_PROTOCOL_AH = 0,
116 IPSEC_PROTOCOL_ESP = 1
123 ipsec_protocol_t protocol;
125 ipsec_crypto_alg_t crypto_alg;
129 ipsec_integ_alg_t integ_alg;
139 ip46_address_t tunnel_src_addr;
140 ip46_address_t tunnel_dst_addr;
157 ip46_address_t start, stop;
158 } ip46_address_range_t;
170 ip4_address_t local_ip, remote_ip;
173 ipsec_crypto_alg_t crypto_alg;
174 u8 local_crypto_key_len;
175 u8 local_crypto_key[128];
176 u8 remote_crypto_key_len;
177 u8 remote_crypto_key[128];
178 ipsec_integ_alg_t integ_alg;
179 u8 local_integ_key_len;
180 u8 local_integ_key[128];
181 u8 remote_integ_key_len;
182 u8 remote_integ_key[128];
186 } ipsec_add_del_tunnel_args_t;
193 ip4_address_t local_ip;
194 ip4_address_t remote_ip;
195 } ipsec_add_del_ipsec_gre_tunnel_args_t;
199 IPSEC_IF_SET_KEY_TYPE_NONE,
200 IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO,
201 IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO,
202 IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG,
203 IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG,
204 } ipsec_if_set_key_type_t;
214 ip46_address_range_t laddr;
215 ip46_address_range_t raddr;
226 vlib_counter_t counter;
232 /* pool of policies */
233 ipsec_policy_t *policies;
234 /* vectors of policy indices */
235 u32 *ipv4_outbound_policies;
236 u32 *ipv6_outbound_policies;
237 u32 *ipv4_inbound_protect_policy_indices;
238 u32 *ipv4_inbound_policy_discard_and_bypass_indices;
239 u32 *ipv6_inbound_protect_policy_indices;
240 u32 *ipv6_inbound_policy_discard_and_bypass_indices;
246 } ip4_ipsec_config_t;
251 } ip6_ipsec_config_t;
255 /* Required for pool_get_aligned */
256 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
263 typedef clib_error_t *(*add_del_sa_sess_cb_t) (u32 sa_index, u8 is_add);
264 typedef clib_error_t *(*check_support_cb_t) (ipsec_sa_t * sa);
269 /* add/del callback */
270 add_del_sa_sess_cb_t add_del_sa_sess_cb;
271 /* check support function */
272 check_support_cb_t check_support_cb;
273 u32 ah4_encrypt_node_index;
274 u32 ah4_decrypt_node_index;
275 u32 ah4_encrypt_next_index;
276 u32 ah4_decrypt_next_index;
277 u32 ah6_encrypt_node_index;
278 u32 ah6_decrypt_node_index;
279 u32 ah6_encrypt_next_index;
280 u32 ah6_decrypt_next_index;
281 } ipsec_ah_backend_t;
286 /* add/del callback */
287 add_del_sa_sess_cb_t add_del_sa_sess_cb;
288 /* check support function */
289 check_support_cb_t check_support_cb;
290 u32 esp4_encrypt_node_index;
291 u32 esp4_decrypt_node_index;
292 u32 esp4_encrypt_next_index;
293 u32 esp4_decrypt_next_index;
294 u32 esp6_encrypt_node_index;
295 u32 esp6_decrypt_node_index;
296 u32 esp6_encrypt_next_index;
297 u32 esp6_decrypt_next_index;
298 } ipsec_esp_backend_t;
302 const EVP_CIPHER *type;
305 } ipsec_proto_main_crypto_alg_t;
311 } ipsec_proto_main_integ_alg_t;
315 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
316 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
317 EVP_CIPHER_CTX *encrypt_ctx;
319 EVP_CIPHER_CTX encrypt_ctx;
321 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
322 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
323 EVP_CIPHER_CTX *decrypt_ctx;
325 EVP_CIPHER_CTX decrypt_ctx;
327 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
328 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
333 ipsec_crypto_alg_t last_encrypt_alg;
334 ipsec_crypto_alg_t last_decrypt_alg;
335 ipsec_integ_alg_t last_integ_alg;
336 } ipsec_proto_main_per_thread_data_t;
340 ipsec_proto_main_crypto_alg_t *ipsec_proto_main_crypto_algs;
341 ipsec_proto_main_integ_alg_t *ipsec_proto_main_integ_algs;
342 ipsec_proto_main_per_thread_data_t *per_thread_data;
343 } ipsec_proto_main_t;
345 extern ipsec_proto_main_t ipsec_proto_main;
349 /* pool of tunnel instances */
353 /* pool of tunnel interfaces */
354 ipsec_tunnel_if_t *tunnel_interfaces;
355 u32 *free_tunnel_if_indices;
359 uword *tunnel_index_by_key;
362 vlib_main_t *vlib_main;
363 vnet_main_t *vnet_main;
365 /* next node indices */
366 u32 feature_next_node_index[32];
369 uword *spd_index_by_spd_id;
370 uword *spd_index_by_sw_if_index;
371 uword *sa_index_by_sa_id;
372 uword *ipsec_if_pool_index_by_key;
373 uword *ipsec_if_real_dev_by_show_dev;
376 u32 error_drop_node_index;
377 u32 esp4_encrypt_node_index;
378 u32 esp4_decrypt_node_index;
379 u32 ah4_encrypt_node_index;
380 u32 ah4_decrypt_node_index;
381 u32 esp6_encrypt_node_index;
382 u32 esp6_decrypt_node_index;
383 u32 ah6_encrypt_node_index;
384 u32 ah6_decrypt_node_index;
385 /* next node indices */
386 u32 esp4_encrypt_next_index;
387 u32 esp4_decrypt_next_index;
388 u32 ah4_encrypt_next_index;
389 u32 ah4_decrypt_next_index;
390 u32 esp6_encrypt_next_index;
391 u32 esp6_decrypt_next_index;
392 u32 ah6_encrypt_next_index;
393 u32 ah6_decrypt_next_index;
395 /* pool of ah backends */
396 ipsec_ah_backend_t *ah_backends;
397 /* pool of esp backends */
398 ipsec_esp_backend_t *esp_backends;
399 /* index of current ah backend */
400 u32 ah_current_backend;
401 /* index of current esp backend */
402 u32 esp_current_backend;
403 /* index of default ah backend */
404 u32 ah_default_backend;
405 /* index of default esp backend */
406 u32 esp_default_backend;
408 /* helper for sort function */
409 ipsec_spd_t *spd_to_sort;
412 extern ipsec_main_t ipsec_main;
414 clib_error_t *ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index,
417 clib_error_t *ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa);
419 extern vlib_node_registration_t esp4_encrypt_node;
420 extern vlib_node_registration_t esp4_decrypt_node;
421 extern vlib_node_registration_t ah4_encrypt_node;
422 extern vlib_node_registration_t ah4_decrypt_node;
423 extern vlib_node_registration_t esp6_encrypt_node;
424 extern vlib_node_registration_t esp6_decrypt_node;
425 extern vlib_node_registration_t ah6_encrypt_node;
426 extern vlib_node_registration_t ah6_decrypt_node;
427 extern vlib_node_registration_t ipsec_if_input_node;
432 int ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id,
434 int ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add);
435 int ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy,
437 int ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add);
438 int ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update);
440 u32 ipsec_get_sa_index_by_sa_id (u32 sa_id);
441 u8 ipsec_is_sa_used (u32 sa_index);
442 u8 *format_ipsec_policy_action (u8 * s, va_list * args);
443 u8 *format_ipsec_crypto_alg (u8 * s, va_list * args);
444 u8 *format_ipsec_integ_alg (u8 * s, va_list * args);
445 u8 *format_ipsec_replay_window (u8 * s, va_list * args);
446 uword unformat_ipsec_policy_action (unformat_input_t * input, va_list * args);
447 uword unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args);
448 uword unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args);
450 int ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
451 ipsec_add_del_tunnel_args_t * args,
453 int ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args);
454 int ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
455 ipsec_add_del_ipsec_gre_tunnel_args_t *
457 int ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
458 ipsec_if_set_key_type_t type, u8 alg, u8 * key);
459 int ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
467 ipsec_alloc_empty_buffers (vlib_main_t * vm, ipsec_main_t * im)
469 u32 thread_index = vm->thread_index;
470 uword l = vec_len (im->empty_buffers[thread_index]);
473 if (PREDICT_FALSE (l < VLIB_FRAME_SIZE))
475 if (!im->empty_buffers[thread_index])
477 vec_alloc (im->empty_buffers[thread_index], 2 * VLIB_FRAME_SIZE);
480 n_alloc = vlib_buffer_alloc (vm, im->empty_buffers[thread_index] + l,
481 2 * VLIB_FRAME_SIZE - l);
483 _vec_len (im->empty_buffers[thread_index]) = l + n_alloc;
487 static_always_inline u32
488 get_next_output_feature_node_index (vlib_buffer_t * b,
489 vlib_node_runtime_t * nr)
492 vlib_main_t *vm = vlib_get_main ();
493 vlib_node_t *node = vlib_get_node (vm, nr->node_index);
495 vnet_feature_next (&next, b);
496 return node->next_nodes[next];
499 u32 ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
501 const char *ah4_encrypt_node_name,
502 const char *ah4_decrypt_node_name,
503 const char *ah6_encrypt_node_name,
504 const char *ah6_decrypt_node_name,
505 check_support_cb_t ah_check_support_cb,
506 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb);
508 u32 ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im,
510 const char *esp4_encrypt_node_name,
511 const char *esp4_decrypt_node_name,
512 const char *esp6_encrypt_node_name,
513 const char *esp6_decrypt_node_name,
514 check_support_cb_t esp_check_support_cb,
515 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb);
517 int ipsec_select_ah_backend (ipsec_main_t * im, u32 ah_backend_idx);
518 int ipsec_select_esp_backend (ipsec_main_t * im, u32 esp_backend_idx);
519 #endif /* __IPSEC_H__ */
522 * fd.io coding-style-patch-verification: ON
525 * eval: (c-set-style "gnu")