ipsec: infra for selecting backends
[vpp.git] / src / vnet / ipsec / esp.h
index d9ab1d8..50c4d39 100644 (file)
@@ -42,6 +42,14 @@ typedef CLIB_PACKED (struct {
 }) ip4_and_esp_header_t;
 /* *INDENT-ON* */
 
+/* *INDENT-OFF* */
+typedef CLIB_PACKED (struct {
+  ip4_header_t ip4;
+  udp_header_t udp;
+  esp_header_t esp;
+}) ip4_and_udp_and_esp_header_t;
+/* *INDENT-ON* */
+
 /* *INDENT-OFF* */
 typedef CLIB_PACKED (struct {
   ip6_header_t ip6;
@@ -52,13 +60,15 @@ typedef CLIB_PACKED (struct {
 typedef struct
 {
   const EVP_CIPHER *type;
-} esp_crypto_alg_t;
+  u8 iv_size;
+  u8 block_size;
+} ipsec_proto_main_crypto_alg_t;
 
 typedef struct
 {
   const EVP_MD *md;
   u8 trunc_size;
-} esp_integ_alg_t;
+} ipsec_proto_main_integ_alg_t;
 
 typedef struct
 {
@@ -83,16 +93,16 @@ typedef struct
   ipsec_crypto_alg_t last_encrypt_alg;
   ipsec_crypto_alg_t last_decrypt_alg;
   ipsec_integ_alg_t last_integ_alg;
-} esp_main_per_thread_data_t;
+} ipsec_proto_main_per_thread_data_t;
 
 typedef struct
 {
-  esp_crypto_alg_t *esp_crypto_algs;
-  esp_integ_alg_t *esp_integ_algs;
-  esp_main_per_thread_data_t *per_thread_data;
-} esp_main_t;
+  ipsec_proto_main_crypto_alg_t *ipsec_proto_main_crypto_algs;
+  ipsec_proto_main_integ_alg_t *ipsec_proto_main_integ_algs;
+  ipsec_proto_main_per_thread_data_t *per_thread_data;
+} ipsec_proto_main_t;
 
-extern esp_main_t esp_main;
+extern ipsec_proto_main_t ipsec_proto_main;
 
 #define ESP_WINDOW_SIZE                (64)
 #define ESP_SEQ_MAX            (4294967295UL)
@@ -244,38 +254,58 @@ esp_seq_advance (ipsec_sa_t * sa)
 }
 
 always_inline void
-esp_init ()
+ipsec_proto_init ()
 {
-  esp_main_t *em = &esp_main;
+  ipsec_proto_main_t *em = &ipsec_proto_main;
   vlib_thread_main_t *tm = vlib_get_thread_main ();
 
-  memset (em, 0, sizeof (em[0]));
-
-  vec_validate (em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
-  em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = EVP_aes_128_cbc ();
-  em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = EVP_aes_192_cbc ();
-  em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = EVP_aes_256_cbc ();
-
-  vec_validate (em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1);
-  esp_integ_alg_t *i;
-
-  i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
+  clib_memset (em, 0, sizeof (em[0]));
+
+  vec_validate (em->ipsec_proto_main_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type =
+    EVP_aes_128_cbc ();
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type =
+    EVP_aes_192_cbc ();
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type =
+    EVP_aes_256_cbc ();
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].iv_size = 16;
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].iv_size = 16;
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].iv_size = 16;
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].block_size =
+    16;
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].block_size =
+    16;
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].block_size =
+    16;
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].type =
+    EVP_des_cbc ();
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].type =
+    EVP_des_ede3_cbc ();
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].block_size = 8;
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].block_size = 8;
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].iv_size = 8;
+  em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].iv_size = 8;
+
+  vec_validate (em->ipsec_proto_main_integ_algs, IPSEC_INTEG_N_ALG - 1);
+  ipsec_proto_main_integ_alg_t *i;
+
+  i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
   i->md = EVP_sha1 ();
   i->trunc_size = 12;
 
-  i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
+  i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
   i->md = EVP_sha256 ();
   i->trunc_size = 12;
 
-  i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
+  i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
   i->md = EVP_sha256 ();
   i->trunc_size = 16;
 
-  i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
+  i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
   i->md = EVP_sha384 ();
   i->trunc_size = 24;
 
-  i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
+  i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
   i->md = EVP_sha512 ();
   i->trunc_size = 32;
 
@@ -283,7 +313,7 @@ esp_init ()
                        CLIB_CACHE_LINE_BYTES);
   int thread_id;
 
-  for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
+  for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++)
     {
 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
       em->per_thread_data[thread_id].encrypt_ctx = EVP_CIPHER_CTX_new ();
@@ -303,7 +333,7 @@ hmac_calc (ipsec_integ_alg_t alg,
           int key_len,
           u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
 {
-  esp_main_t *em = &esp_main;
+  ipsec_proto_main_t *em = &ipsec_proto_main;
   u32 thread_index = vlib_get_thread_index ();
 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
   HMAC_CTX *ctx = em->per_thread_data[thread_index].hmac_ctx;
@@ -315,12 +345,12 @@ hmac_calc (ipsec_integ_alg_t alg,
 
   ASSERT (alg < IPSEC_INTEG_N_ALG);
 
-  if (PREDICT_FALSE (em->esp_integ_algs[alg].md == 0))
+  if (PREDICT_FALSE (em->ipsec_proto_main_integ_algs[alg].md == 0))
     return 0;
 
   if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_integ_alg))
     {
-      md = em->esp_integ_algs[alg].md;
+      md = em->ipsec_proto_main_integ_algs[alg].md;
       em->per_thread_data[thread_index].last_integ_alg = alg;
     }
 
@@ -332,7 +362,7 @@ hmac_calc (ipsec_integ_alg_t alg,
     HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
   HMAC_Final (ctx, signature, &len);
 
-  return em->esp_integ_algs[alg].trunc_size;
+  return em->ipsec_proto_main_integ_algs[alg].trunc_size;
 }
 
 #endif /* __ESP_H__ */