ipsec: add fast path configuration parser
[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->fp_spd_is_enabled = 0;
487   im->fp_lookup_hash_buckets = IPSEC_FP_HASH_LOOKUP_HASH_BUCKETS;
488
489   vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
490
491   a = im->crypto_algs + IPSEC_CRYPTO_ALG_NONE;
492   a->enc_op_id = VNET_CRYPTO_OP_NONE;
493   a->dec_op_id = VNET_CRYPTO_OP_NONE;
494   a->alg = VNET_CRYPTO_ALG_NONE;
495   a->iv_size = 0;
496   a->block_align = 1;
497
498   a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
499   a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC;
500   a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC;
501   a->alg = VNET_CRYPTO_ALG_DES_CBC;
502   a->iv_size = a->block_align = 8;
503
504   a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
505   a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC;
506   a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC;
507   a->alg = VNET_CRYPTO_ALG_3DES_CBC;
508   a->iv_size = a->block_align = 8;
509
510   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
511   a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC;
512   a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC;
513   a->alg = VNET_CRYPTO_ALG_AES_128_CBC;
514   a->iv_size = a->block_align = 16;
515
516   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
517   a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC;
518   a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC;
519   a->alg = VNET_CRYPTO_ALG_AES_192_CBC;
520   a->iv_size = a->block_align = 16;
521
522   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
523   a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC;
524   a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC;
525   a->alg = VNET_CRYPTO_ALG_AES_256_CBC;
526   a->iv_size = a->block_align = 16;
527
528   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_128;
529   a->enc_op_id = VNET_CRYPTO_OP_AES_128_CTR_ENC;
530   a->dec_op_id = VNET_CRYPTO_OP_AES_128_CTR_DEC;
531   a->alg = VNET_CRYPTO_ALG_AES_128_CTR;
532   a->iv_size = 8;
533   a->block_align = 1;
534
535   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_192;
536   a->enc_op_id = VNET_CRYPTO_OP_AES_192_CTR_ENC;
537   a->dec_op_id = VNET_CRYPTO_OP_AES_192_CTR_DEC;
538   a->alg = VNET_CRYPTO_ALG_AES_192_CTR;
539   a->iv_size = 8;
540   a->block_align = 1;
541
542   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_256;
543   a->enc_op_id = VNET_CRYPTO_OP_AES_256_CTR_ENC;
544   a->dec_op_id = VNET_CRYPTO_OP_AES_256_CTR_DEC;
545   a->alg = VNET_CRYPTO_ALG_AES_256_CTR;
546   a->iv_size = 8;
547   a->block_align = 1;
548
549   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128;
550   a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
551   a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
552   a->alg = VNET_CRYPTO_ALG_AES_128_GCM;
553   a->iv_size = 8;
554   a->block_align = 1;
555   a->icv_size = 16;
556
557   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192;
558   a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC;
559   a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC;
560   a->alg = VNET_CRYPTO_ALG_AES_192_GCM;
561   a->iv_size = 8;
562   a->block_align = 1;
563   a->icv_size = 16;
564
565   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256;
566   a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
567   a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
568   a->alg = VNET_CRYPTO_ALG_AES_256_GCM;
569   a->iv_size = 8;
570   a->block_align = 1;
571   a->icv_size = 16;
572
573   vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
574   ipsec_main_integ_alg_t *i;
575
576   i = &im->integ_algs[IPSEC_INTEG_ALG_MD5_96];
577   i->op_id = VNET_CRYPTO_OP_MD5_HMAC;
578   i->alg = VNET_CRYPTO_ALG_HMAC_MD5;
579   i->icv_size = 12;
580
581   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
582   i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
583   i->alg = VNET_CRYPTO_ALG_HMAC_SHA1;
584   i->icv_size = 12;
585
586   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
587   i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
588   i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
589   i->icv_size = 12;
590
591   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
592   i->op_id = VNET_CRYPTO_OP_SHA256_HMAC;
593   i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
594   i->icv_size = 16;
595
596   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
597   i->op_id = VNET_CRYPTO_OP_SHA384_HMAC;
598   i->alg = VNET_CRYPTO_ALG_HMAC_SHA384;
599   i->icv_size = 24;
600
601   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
602   i->op_id = VNET_CRYPTO_OP_SHA512_HMAC;
603   i->alg = VNET_CRYPTO_ALG_HMAC_SHA512;
604   i->icv_size = 32;
605
606   vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES);
607
608   im->async_mode = 0;
609   crypto_engine_backend_register_post_node (vm);
610
611   im->ipsec4_out_spd_hash_tbl = NULL;
612   im->output_flow_cache_flag = 0;
613   im->ipsec4_out_spd_flow_cache_entries = 0;
614   im->epoch_count = 0;
615   im->ipsec4_out_spd_hash_num_buckets =
616     IPSEC4_OUT_SPD_DEFAULT_HASH_NUM_BUCKETS;
617
618   im->ipsec4_in_spd_hash_tbl = NULL;
619   im->input_flow_cache_flag = 0;
620   im->ipsec4_in_spd_flow_cache_entries = 0;
621   im->input_epoch_count = 0;
622   im->ipsec4_in_spd_hash_num_buckets = IPSEC4_SPD_DEFAULT_HASH_NUM_BUCKETS;
623
624   vec_validate_init_empty_aligned (im->next_header_registrations, 255, ~0,
625                                    CLIB_CACHE_LINE_BYTES);
626
627   return 0;
628 }
629
630 VLIB_INIT_FUNCTION (ipsec_init);
631
632 static clib_error_t *
633 ipsec_config (vlib_main_t *vm, unformat_input_t *input)
634 {
635   ipsec_main_t *im = &ipsec_main;
636   unformat_input_t sub_input;
637
638   u32 ipsec4_out_spd_hash_num_buckets;
639   u32 ipsec4_in_spd_hash_num_buckets;
640   u32 ipsec_spd_fp_num_buckets;
641
642   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
643     {
644       if (unformat (input, "ipv4-outbound-spd-fast-path on"))
645         {
646           im->fp_spd_is_enabled = 1;
647           im->output_flow_cache_flag = 0;
648         }
649       else if (unformat (input, "ipv4-outbound-spd-fast-path off"))
650         im->fp_spd_is_enabled = 0;
651       else if (unformat (input, "spd-fast-path-num-buckets %d",
652                          &ipsec_spd_fp_num_buckets))
653         {
654           /* Number of bihash buckets is power of 2 >= input */
655           im->fp_lookup_hash_buckets = 1ULL
656                                        << max_log2 (ipsec_spd_fp_num_buckets);
657         }
658       else if (unformat (input, "ipv4-outbound-spd-flow-cache on"))
659         im->output_flow_cache_flag = im->fp_spd_is_enabled ? 0 : 1;
660       else if (unformat (input, "ipv4-outbound-spd-flow-cache off"))
661         im->output_flow_cache_flag = 0;
662       else if (unformat (input, "ipv4-outbound-spd-hash-buckets %d",
663                          &ipsec4_out_spd_hash_num_buckets))
664         {
665           /* Size of hash is power of 2 >= number of buckets */
666           im->ipsec4_out_spd_hash_num_buckets =
667             1ULL << max_log2 (ipsec4_out_spd_hash_num_buckets);
668         }
669       else if (unformat (input, "ipv4-inbound-spd-flow-cache on"))
670         im->input_flow_cache_flag = 1;
671       else if (unformat (input, "ipv4-inbound-spd-flow-cache off"))
672         im->input_flow_cache_flag = 0;
673       else if (unformat (input, "ipv4-inbound-spd-hash-buckets %d",
674                          &ipsec4_in_spd_hash_num_buckets))
675         {
676           im->ipsec4_in_spd_hash_num_buckets =
677             1ULL << max_log2 (ipsec4_in_spd_hash_num_buckets);
678         }
679       else if (unformat (input, "ip4 %U", unformat_vlib_cli_sub_input,
680                          &sub_input))
681         {
682           uword table_size = ~0;
683           u32 n_buckets = ~0;
684
685           while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
686             {
687               if (unformat (&sub_input, "num-buckets %u", &n_buckets))
688                 ;
689               else
690                 return clib_error_return (0, "unknown input `%U'",
691                                           format_unformat_error, &sub_input);
692             }
693
694           ipsec_tun_table_init (AF_IP4, table_size, n_buckets);
695         }
696       else if (unformat (input, "ip6 %U", unformat_vlib_cli_sub_input,
697                          &sub_input))
698         {
699           uword table_size = ~0;
700           u32 n_buckets = ~0;
701
702           while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
703             {
704               if (unformat (&sub_input, "num-buckets %u", &n_buckets))
705                 ;
706               else
707                 return clib_error_return (0, "unknown input `%U'",
708                                           format_unformat_error, &sub_input);
709             }
710
711           ipsec_tun_table_init (AF_IP6, table_size, n_buckets);
712         }
713       else
714         return clib_error_return (0, "unknown input `%U'",
715                                   format_unformat_error, input);
716     }
717   if (im->output_flow_cache_flag)
718     {
719       vec_add2 (im->ipsec4_out_spd_hash_tbl, im->ipsec4_out_spd_hash_tbl,
720                 im->ipsec4_out_spd_hash_num_buckets);
721     }
722   if (im->input_flow_cache_flag)
723     {
724       vec_add2 (im->ipsec4_in_spd_hash_tbl, im->ipsec4_in_spd_hash_tbl,
725                 im->ipsec4_in_spd_hash_num_buckets);
726     }
727
728   return 0;
729 }
730
731 VLIB_CONFIG_FUNCTION (ipsec_config, "ipsec");
732
733 /*
734  * fd.io coding-style-patch-verification: ON
735  *
736  * Local Variables:
737  * eval: (c-set-style "gnu")
738  * End:
739  */