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