5cc8044e3d4d99cf0d184a310ce17e04c2a19669
[vpp.git] / src / vnet / ipsec / ipsec.c
1 /*
2  * ipsec.c : IPSEC module functions
3  *
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>
22 #include <vnet/udp/udp_local.h>
23
24 #include <vnet/ipsec/ipsec.h>
25 #include <vnet/ipsec/esp.h>
26 #include <vnet/ipsec/ah.h>
27 #include <vnet/ipsec/ipsec_tun.h>
28 #include <vnet/ipsec/ipsec_itf.h>
29
30 /* Flow cache is sized for 1 million flows with a load factor of .25.
31  */
32 #define IPSEC4_OUT_SPD_DEFAULT_HASH_NUM_BUCKETS (1 << 22)
33
34 ipsec_main_t ipsec_main;
35 esp_async_post_next_t esp_encrypt_async_next;
36 esp_async_post_next_t esp_decrypt_async_next;
37
38 static clib_error_t *
39 ipsec_check_ah_support (ipsec_sa_t * sa)
40 {
41   ipsec_main_t *im = &ipsec_main;
42
43   if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
44     return clib_error_return (0, "unsupported none integ-alg");
45
46   if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg))
47     return clib_error_return (0, "No crypto engine support for %U",
48                               format_ipsec_integ_alg, sa->integ_alg);
49
50   return 0;
51 }
52
53 static clib_error_t *
54 ipsec_check_esp_support (ipsec_sa_t * sa)
55 {
56   ipsec_main_t *im = &ipsec_main;
57
58   if (IPSEC_INTEG_ALG_NONE != sa->integ_alg)
59     {
60       if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg))
61         return clib_error_return (0, "No crypto engine support for %U",
62                                   format_ipsec_integ_alg, sa->integ_alg);
63     }
64   if (IPSEC_CRYPTO_ALG_NONE != sa->crypto_alg)
65     {
66       if (!vnet_crypto_is_set_handler (im->crypto_algs[sa->crypto_alg].alg))
67         return clib_error_return (0, "No crypto engine support for %U",
68                                   format_ipsec_crypto_alg, sa->crypto_alg);
69     }
70
71   return (0);
72 }
73
74 clib_error_t *
75 ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
76 {
77   ipsec_ah_backend_t *ah =
78     pool_elt_at_index (im->ah_backends, im->ah_current_backend);
79   if (ah->add_del_sa_sess_cb)
80     {
81       clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
82       if (err)
83         return err;
84     }
85   ipsec_esp_backend_t *esp =
86     pool_elt_at_index (im->esp_backends, im->esp_current_backend);
87   if (esp->add_del_sa_sess_cb)
88     {
89       clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
90       if (err)
91         return err;
92     }
93   return 0;
94 }
95
96 clib_error_t *
97 ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
98 {
99   clib_error_t *error = 0;
100
101   if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH))
102     {
103       ipsec_ah_backend_t *ah =
104         pool_elt_at_index (im->ah_backends, im->ah_current_backend);
105       ASSERT (ah->check_support_cb);
106       error = ah->check_support_cb (sa);
107     }
108   else
109     {
110       ipsec_esp_backend_t *esp =
111         pool_elt_at_index (im->esp_backends, im->esp_current_backend);
112       ASSERT (esp->check_support_cb);
113       error = esp->check_support_cb (sa);
114     }
115   return error;
116 }
117
118
119 static void
120 ipsec_add_node (vlib_main_t * vm, const char *node_name,
121                 const char *prev_node_name, u32 * out_node_index,
122                 u32 * out_next_index)
123 {
124   vlib_node_t *prev_node, *node;
125   prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
126   ASSERT (prev_node);
127   node = vlib_get_node_by_name (vm, (u8 *) node_name);
128   ASSERT (node);
129   *out_node_index = node->index;
130   *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
131 }
132
133 void
134 ipsec_unregister_udp_port (u16 port)
135 {
136   ipsec_main_t *im = &ipsec_main;
137   u32 n_regs;
138   uword *p;
139
140   p = hash_get (im->udp_port_registrations, port);
141
142   ASSERT (p);
143
144   n_regs = p[0];
145
146   if (0 == --n_regs)
147     {
148       udp_unregister_dst_port (vlib_get_main (), port, 1);
149       hash_unset (im->udp_port_registrations, port);
150     }
151   else
152     {
153       hash_unset (im->udp_port_registrations, port);
154       hash_set (im->udp_port_registrations, port, n_regs);
155     }
156 }
157
158 void
159 ipsec_register_udp_port (u16 port)
160 {
161   ipsec_main_t *im = &ipsec_main;
162   u32 n_regs;
163   uword *p;
164
165   p = hash_get (im->udp_port_registrations, port);
166
167   n_regs = (p ? p[0] : 0);
168
169   if (0 == n_regs++)
170     udp_register_dst_port (vlib_get_main (), port,
171                            ipsec4_tun_input_node.index, 1);
172
173   hash_unset (im->udp_port_registrations, port);
174   hash_set (im->udp_port_registrations, port, n_regs);
175 }
176
177 u32
178 ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
179                            const char *name,
180                            const char *ah4_encrypt_node_name,
181                            const char *ah4_decrypt_node_name,
182                            const char *ah6_encrypt_node_name,
183                            const char *ah6_decrypt_node_name,
184                            check_support_cb_t ah_check_support_cb,
185                            add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
186 {
187   ipsec_ah_backend_t *b;
188   pool_get (im->ah_backends, b);
189   b->name = format (0, "%s%c", name, 0);
190
191   ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature",
192                   &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
193   ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature",
194                   &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
195   ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature",
196                   &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
197   ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature",
198                   &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
199
200   b->check_support_cb = ah_check_support_cb;
201   b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
202   return b - im->ah_backends;
203 }
204
205 u32
206 ipsec_register_esp_backend (
207   vlib_main_t *vm, ipsec_main_t *im, const char *name,
208   const char *esp4_encrypt_node_name, const char *esp4_encrypt_node_tun_name,
209   const char *esp4_decrypt_node_name, const char *esp4_decrypt_tun_node_name,
210   const char *esp6_encrypt_node_name, const char *esp6_encrypt_node_tun_name,
211   const char *esp6_decrypt_node_name, const char *esp6_decrypt_tun_node_name,
212   const char *esp_mpls_encrypt_node_tun_name,
213   check_support_cb_t esp_check_support_cb,
214   add_del_sa_sess_cb_t esp_add_del_sa_sess_cb,
215   enable_disable_cb_t enable_disable_cb)
216 {
217   ipsec_esp_backend_t *b;
218
219   pool_get (im->esp_backends, b);
220   b->name = format (0, "%s%c", name, 0);
221
222   ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature",
223                   &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
224   ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature",
225                   &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
226   ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature",
227                   &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
228   ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature",
229                   &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
230   ipsec_add_node (vm, esp4_decrypt_tun_node_name, "ipsec4-tun-input",
231                   &b->esp4_decrypt_tun_node_index,
232                   &b->esp4_decrypt_tun_next_index);
233   ipsec_add_node (vm, esp6_decrypt_tun_node_name, "ipsec6-tun-input",
234                   &b->esp6_decrypt_tun_node_index,
235                   &b->esp6_decrypt_tun_next_index);
236
237   b->esp6_encrypt_tun_node_index =
238     vlib_get_node_by_name (vm, (u8 *) esp6_encrypt_node_tun_name)->index;
239   b->esp_mpls_encrypt_tun_node_index =
240     vlib_get_node_by_name (vm, (u8 *) esp_mpls_encrypt_node_tun_name)->index;
241   b->esp4_encrypt_tun_node_index =
242     vlib_get_node_by_name (vm, (u8 *) esp4_encrypt_node_tun_name)->index;
243
244   b->check_support_cb = esp_check_support_cb;
245   b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
246   b->enable_disable_cb = enable_disable_cb;
247
248   return b - im->esp_backends;
249 }
250
251 clib_error_t *
252 ipsec_rsc_in_use (ipsec_main_t * im)
253 {
254   /* return an error is crypto resource are in use */
255   if (pool_elts (ipsec_sa_pool) > 0)
256     return clib_error_return (0, "%d SA entries configured",
257                               pool_elts (ipsec_sa_pool));
258   if (ipsec_itf_count () > 0)
259     return clib_error_return (0, "%d IPSec interface configured",
260                               ipsec_itf_count ());
261
262   return (NULL);
263 }
264
265 int
266 ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
267 {
268   if (ipsec_rsc_in_use (im))
269     return VNET_API_ERROR_RSRC_IN_USE;
270
271   if (pool_is_free_index (im->ah_backends, backend_idx))
272     return VNET_API_ERROR_INVALID_VALUE;
273
274   ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
275   im->ah_current_backend = backend_idx;
276   im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
277   im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
278   im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
279   im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
280   im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
281   im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
282   im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
283   im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
284
285   return 0;
286 }
287
288 int
289 ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
290 {
291   if (ipsec_rsc_in_use (im))
292     return VNET_API_ERROR_RSRC_IN_USE;
293
294   if (pool_is_free_index (im->esp_backends, backend_idx))
295     return VNET_API_ERROR_INVALID_VALUE;
296
297   /* disable current backend */
298   if (im->esp_current_backend != ~0)
299     {
300       ipsec_esp_backend_t *cb = pool_elt_at_index (im->esp_backends,
301                                                    im->esp_current_backend);
302       if (cb->enable_disable_cb)
303         {
304           if ((cb->enable_disable_cb) (0) != 0)
305             return -1;
306         }
307     }
308
309   ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
310   im->esp_current_backend = backend_idx;
311   im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
312   im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
313   im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
314   im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
315   im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
316   im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
317   im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
318   im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
319   im->esp4_decrypt_tun_node_index = b->esp4_decrypt_tun_node_index;
320   im->esp4_decrypt_tun_next_index = b->esp4_decrypt_tun_next_index;
321   im->esp6_decrypt_tun_node_index = b->esp6_decrypt_tun_node_index;
322   im->esp6_decrypt_tun_next_index = b->esp6_decrypt_tun_next_index;
323   im->esp4_encrypt_tun_node_index = b->esp4_encrypt_tun_node_index;
324   im->esp6_encrypt_tun_node_index = b->esp6_encrypt_tun_node_index;
325   im->esp_mpls_encrypt_tun_node_index = b->esp_mpls_encrypt_tun_node_index;
326
327   if (b->enable_disable_cb)
328     {
329       if ((b->enable_disable_cb) (1) != 0)
330         return -1;
331     }
332   return 0;
333 }
334
335 void
336 ipsec_set_async_mode (u32 is_enabled)
337 {
338   ipsec_main_t *im = &ipsec_main;
339   ipsec_sa_t *sa;
340
341   vnet_crypto_request_async_mode (is_enabled);
342
343   im->async_mode = is_enabled;
344
345   /* change SA crypto op data */
346   pool_foreach (sa, ipsec_sa_pool)
347     {
348       sa->crypto_op_data =
349         (is_enabled ? sa->async_op_data.data : sa->sync_op_data.data);
350     }
351 }
352
353 static void
354 crypto_engine_backend_register_post_node (vlib_main_t * vm)
355 {
356   esp_async_post_next_t *eit;
357   esp_async_post_next_t *dit;
358
359   eit = &esp_encrypt_async_next;
360   eit->esp4_post_next =
361     vnet_crypto_register_post_node (vm, "esp4-encrypt-post");
362   eit->esp6_post_next =
363     vnet_crypto_register_post_node (vm, "esp6-encrypt-post");
364   eit->esp4_tun_post_next =
365     vnet_crypto_register_post_node (vm, "esp4-encrypt-tun-post");
366   eit->esp6_tun_post_next =
367     vnet_crypto_register_post_node (vm, "esp6-encrypt-tun-post");
368   eit->esp_mpls_tun_post_next =
369     vnet_crypto_register_post_node (vm, "esp-mpls-encrypt-tun-post");
370
371   dit = &esp_decrypt_async_next;
372   dit->esp4_post_next =
373     vnet_crypto_register_post_node (vm, "esp4-decrypt-post");
374   dit->esp6_post_next =
375     vnet_crypto_register_post_node (vm, "esp6-decrypt-post");
376   dit->esp4_tun_post_next =
377     vnet_crypto_register_post_node (vm, "esp4-decrypt-tun-post");
378   dit->esp6_tun_post_next =
379     vnet_crypto_register_post_node (vm, "esp6-decrypt-tun-post");
380 }
381
382 static clib_error_t *
383 ipsec_init (vlib_main_t * vm)
384 {
385   clib_error_t *error;
386   ipsec_main_t *im = &ipsec_main;
387   ipsec_main_crypto_alg_t *a;
388
389   /* Backend registration requires the feature arcs to be set up */
390   if ((error = vlib_call_init_function (vm, vnet_feature_init)))
391     return (error);
392
393   im->vnet_main = vnet_get_main ();
394   im->vlib_main = vm;
395
396   im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
397   im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
398   im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
399
400   vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
401   ASSERT (node);
402   im->error_drop_node_index = node->index;
403
404   im->ah_current_backend = ~0;
405   im->esp_current_backend = ~0;
406
407   u32 idx = ipsec_register_ah_backend (vm, im, "crypto engine backend",
408                                        "ah4-encrypt",
409                                        "ah4-decrypt",
410                                        "ah6-encrypt",
411                                        "ah6-decrypt",
412                                        ipsec_check_ah_support,
413                                        NULL);
414
415   im->ah_default_backend = idx;
416   int rv = ipsec_select_ah_backend (im, idx);
417   ASSERT (0 == rv);
418   (void) (rv);                  // avoid warning
419
420   idx = ipsec_register_esp_backend (
421     vm, im, "crypto engine backend", "esp4-encrypt", "esp4-encrypt-tun",
422     "esp4-decrypt", "esp4-decrypt-tun", "esp6-encrypt", "esp6-encrypt-tun",
423     "esp6-decrypt", "esp6-decrypt-tun", "esp-mpls-encrypt-tun",
424     ipsec_check_esp_support, NULL, crypto_dispatch_enable_disable);
425   im->esp_default_backend = idx;
426
427   rv = ipsec_select_esp_backend (im, idx);
428   ASSERT (0 == rv);
429   (void) (rv);                  // avoid warning
430
431   if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
432     return error;
433
434   vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
435
436   a = im->crypto_algs + IPSEC_CRYPTO_ALG_NONE;
437   a->enc_op_id = VNET_CRYPTO_OP_NONE;
438   a->dec_op_id = VNET_CRYPTO_OP_NONE;
439   a->alg = VNET_CRYPTO_ALG_NONE;
440   a->iv_size = 0;
441   a->block_align = 1;
442
443   a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
444   a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC;
445   a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC;
446   a->alg = VNET_CRYPTO_ALG_DES_CBC;
447   a->iv_size = a->block_align = 8;
448
449   a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
450   a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC;
451   a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC;
452   a->alg = VNET_CRYPTO_ALG_3DES_CBC;
453   a->iv_size = a->block_align = 8;
454
455   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
456   a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC;
457   a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC;
458   a->alg = VNET_CRYPTO_ALG_AES_128_CBC;
459   a->iv_size = a->block_align = 16;
460
461   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
462   a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC;
463   a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC;
464   a->alg = VNET_CRYPTO_ALG_AES_192_CBC;
465   a->iv_size = a->block_align = 16;
466
467   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
468   a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC;
469   a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC;
470   a->alg = VNET_CRYPTO_ALG_AES_256_CBC;
471   a->iv_size = a->block_align = 16;
472
473   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_128;
474   a->enc_op_id = VNET_CRYPTO_OP_AES_128_CTR_ENC;
475   a->dec_op_id = VNET_CRYPTO_OP_AES_128_CTR_DEC;
476   a->alg = VNET_CRYPTO_ALG_AES_128_CTR;
477   a->iv_size = 8;
478   a->block_align = 1;
479
480   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_192;
481   a->enc_op_id = VNET_CRYPTO_OP_AES_192_CTR_ENC;
482   a->dec_op_id = VNET_CRYPTO_OP_AES_192_CTR_DEC;
483   a->alg = VNET_CRYPTO_ALG_AES_192_CTR;
484   a->iv_size = 8;
485   a->block_align = 1;
486
487   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_256;
488   a->enc_op_id = VNET_CRYPTO_OP_AES_256_CTR_ENC;
489   a->dec_op_id = VNET_CRYPTO_OP_AES_256_CTR_DEC;
490   a->alg = VNET_CRYPTO_ALG_AES_256_CTR;
491   a->iv_size = 8;
492   a->block_align = 1;
493
494   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128;
495   a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
496   a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
497   a->alg = VNET_CRYPTO_ALG_AES_128_GCM;
498   a->iv_size = 8;
499   a->block_align = 1;
500   a->icv_size = 16;
501
502   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192;
503   a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC;
504   a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC;
505   a->alg = VNET_CRYPTO_ALG_AES_192_GCM;
506   a->iv_size = 8;
507   a->block_align = 1;
508   a->icv_size = 16;
509
510   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256;
511   a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
512   a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
513   a->alg = VNET_CRYPTO_ALG_AES_256_GCM;
514   a->iv_size = 8;
515   a->block_align = 1;
516   a->icv_size = 16;
517
518   vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
519   ipsec_main_integ_alg_t *i;
520
521   i = &im->integ_algs[IPSEC_INTEG_ALG_MD5_96];
522   i->op_id = VNET_CRYPTO_OP_MD5_HMAC;
523   i->alg = VNET_CRYPTO_ALG_HMAC_MD5;
524   i->icv_size = 12;
525
526   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
527   i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
528   i->alg = VNET_CRYPTO_ALG_HMAC_SHA1;
529   i->icv_size = 12;
530
531   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
532   i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
533   i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
534   i->icv_size = 12;
535
536   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
537   i->op_id = VNET_CRYPTO_OP_SHA256_HMAC;
538   i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
539   i->icv_size = 16;
540
541   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
542   i->op_id = VNET_CRYPTO_OP_SHA384_HMAC;
543   i->alg = VNET_CRYPTO_ALG_HMAC_SHA384;
544   i->icv_size = 24;
545
546   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
547   i->op_id = VNET_CRYPTO_OP_SHA512_HMAC;
548   i->alg = VNET_CRYPTO_ALG_HMAC_SHA512;
549   i->icv_size = 32;
550
551   vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES);
552
553   im->async_mode = 0;
554   crypto_engine_backend_register_post_node (vm);
555
556   im->ipsec4_out_spd_hash_tbl = NULL;
557   im->flow_cache_flag = 0;
558   im->ipsec4_out_spd_flow_cache_entries = 0;
559   im->epoch_count = 0;
560   im->ipsec4_out_spd_hash_num_buckets =
561     IPSEC4_OUT_SPD_DEFAULT_HASH_NUM_BUCKETS;
562
563   return 0;
564 }
565
566 VLIB_INIT_FUNCTION (ipsec_init);
567
568 static clib_error_t *
569 ipsec_config (vlib_main_t *vm, unformat_input_t *input)
570 {
571   ipsec_main_t *im = &ipsec_main;
572   unformat_input_t sub_input;
573   u32 ipsec4_out_spd_hash_num_buckets;
574
575   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
576     {
577       if (unformat (input, "ipv4-outbound-spd-flow-cache on"))
578         im->flow_cache_flag = 1;
579       else if (unformat (input, "ipv4-outbound-spd-flow-cache off"))
580         im->flow_cache_flag = 0;
581       else if (unformat (input, "ipv4-outbound-spd-hash-buckets %d",
582                          &ipsec4_out_spd_hash_num_buckets))
583         {
584           /* Size of hash is power of 2 >= number of buckets */
585           im->ipsec4_out_spd_hash_num_buckets =
586             1ULL << max_log2 (ipsec4_out_spd_hash_num_buckets);
587         }
588       else if (unformat (input, "ip4 %U", unformat_vlib_cli_sub_input,
589                          &sub_input))
590         {
591           uword table_size = ~0;
592           u32 n_buckets = ~0;
593
594           while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
595             {
596               if (unformat (&sub_input, "num-buckets %u", &n_buckets))
597                 ;
598               else
599                 return clib_error_return (0, "unknown input `%U'",
600                                           format_unformat_error, &sub_input);
601             }
602
603           ipsec_tun_table_init (AF_IP4, table_size, n_buckets);
604         }
605       else if (unformat (input, "ip6 %U", unformat_vlib_cli_sub_input,
606                          &sub_input))
607         {
608           uword table_size = ~0;
609           u32 n_buckets = ~0;
610
611           while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
612             {
613               if (unformat (&sub_input, "num-buckets %u", &n_buckets))
614                 ;
615               else
616                 return clib_error_return (0, "unknown input `%U'",
617                                           format_unformat_error, &sub_input);
618             }
619
620           ipsec_tun_table_init (AF_IP6, table_size, n_buckets);
621         }
622       else
623         return clib_error_return (0, "unknown input `%U'",
624                                   format_unformat_error, input);
625     }
626   if (im->flow_cache_flag)
627     {
628       vec_add2 (im->ipsec4_out_spd_hash_tbl, im->ipsec4_out_spd_hash_tbl,
629                 im->ipsec4_out_spd_hash_num_buckets);
630     }
631
632   return 0;
633 }
634
635 VLIB_CONFIG_FUNCTION (ipsec_config, "ipsec");
636
637 /*
638  * fd.io coding-style-patch-verification: ON
639  *
640  * Local Variables:
641  * eval: (c-set-style "gnu")
642  * End:
643  */