crypto: add support for AEAD and AES-GCM
[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.h>
23
24 #include <vnet/ipsec/ipsec.h>
25 #include <vnet/ipsec/esp.h>
26 #include <vnet/ipsec/ah.h>
27
28 ipsec_main_t ipsec_main;
29
30 static clib_error_t *
31 ipsec_check_ah_support (ipsec_sa_t * sa)
32 {
33   if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
34     return clib_error_return (0, "unsupported none integ-alg");
35   return 0;
36 }
37
38 static clib_error_t *
39 ipsec_check_esp_support (ipsec_sa_t * sa)
40 {
41   if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
42     return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg");
43   if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_192)
44     return clib_error_return (0, "unsupported aes-gcm-192 crypto-alg");
45   if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_256)
46     return clib_error_return (0, "unsupported aes-gcm-256 crypto-alg");
47
48   return 0;
49 }
50
51 clib_error_t *
52 ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
53 {
54   ipsec_ah_backend_t *ah =
55     pool_elt_at_index (im->ah_backends, im->ah_current_backend);
56   if (ah->add_del_sa_sess_cb)
57     {
58       clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
59       if (err)
60         return err;
61     }
62   ipsec_esp_backend_t *esp =
63     pool_elt_at_index (im->esp_backends, im->esp_current_backend);
64   if (esp->add_del_sa_sess_cb)
65     {
66       clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
67       if (err)
68         return err;
69     }
70   return 0;
71 }
72
73 clib_error_t *
74 ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
75 {
76   clib_error_t *error = 0;
77
78   if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH))
79     {
80       ipsec_ah_backend_t *ah =
81         pool_elt_at_index (im->ah_backends, im->ah_current_backend);
82       ASSERT (ah->check_support_cb);
83       error = ah->check_support_cb (sa);
84     }
85   else
86     {
87       ipsec_esp_backend_t *esp =
88         pool_elt_at_index (im->esp_backends, im->esp_current_backend);
89       ASSERT (esp->check_support_cb);
90       error = esp->check_support_cb (sa);
91     }
92   return error;
93 }
94
95
96 static void
97 ipsec_add_node (vlib_main_t * vm, const char *node_name,
98                 const char *prev_node_name, u32 * out_node_index,
99                 u32 * out_next_index)
100 {
101   vlib_node_t *prev_node, *node;
102   prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
103   ASSERT (prev_node);
104   node = vlib_get_node_by_name (vm, (u8 *) node_name);
105   ASSERT (node);
106   *out_node_index = node->index;
107   *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
108 }
109
110 u32
111 ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
112                            const char *name,
113                            const char *ah4_encrypt_node_name,
114                            const char *ah4_decrypt_node_name,
115                            const char *ah6_encrypt_node_name,
116                            const char *ah6_decrypt_node_name,
117                            check_support_cb_t ah_check_support_cb,
118                            add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
119 {
120   ipsec_ah_backend_t *b;
121   pool_get (im->ah_backends, b);
122   b->name = format (0, "%s%c", name, 0);
123
124   ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature",
125                   &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
126   ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature",
127                   &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
128   ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature",
129                   &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
130   ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature",
131                   &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
132
133   b->check_support_cb = ah_check_support_cb;
134   b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
135   return b - im->ah_backends;
136 }
137
138 u32
139 ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im,
140                             const char *name,
141                             const char *esp4_encrypt_node_name,
142                             const char *esp4_decrypt_node_name,
143                             const char *esp6_encrypt_node_name,
144                             const char *esp6_decrypt_node_name,
145                             check_support_cb_t esp_check_support_cb,
146                             add_del_sa_sess_cb_t esp_add_del_sa_sess_cb)
147 {
148   ipsec_esp_backend_t *b;
149   pool_get (im->esp_backends, b);
150   b->name = format (0, "%s%c", name, 0);
151
152   ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature",
153                   &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
154   ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature",
155                   &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
156   ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature",
157                   &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
158   ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature",
159                   &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
160
161   b->check_support_cb = esp_check_support_cb;
162   b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
163   return b - im->esp_backends;
164 }
165
166 static walk_rc_t
167 ipsec_sa_restack (ipsec_sa_t * sa, void *ctx)
168 {
169   ipsec_sa_stack (sa);
170
171   return (WALK_CONTINUE);
172 }
173
174 int
175 ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
176 {
177   if (pool_elts (im->sad) > 0
178       || pool_is_free_index (im->ah_backends, backend_idx))
179     {
180       return -1;
181     }
182   ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
183   im->ah_current_backend = backend_idx;
184   im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
185   im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
186   im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
187   im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
188   im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
189   im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
190   im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
191   im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
192
193   ipsec_sa_walk (ipsec_sa_restack, NULL);
194   return 0;
195 }
196
197 int
198 ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
199 {
200   if (pool_elts (im->sad) > 0
201       || pool_is_free_index (im->esp_backends, backend_idx))
202     {
203       return -1;
204     }
205   ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
206   im->esp_current_backend = backend_idx;
207   im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
208   im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
209   im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
210   im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
211   im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
212   im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
213   im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
214   im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
215
216   ipsec_sa_walk (ipsec_sa_restack, NULL);
217   return 0;
218 }
219
220 static clib_error_t *
221 ipsec_init (vlib_main_t * vm)
222 {
223   clib_error_t *error;
224   ipsec_main_t *im = &ipsec_main;
225   ipsec_main_crypto_alg_t *a;
226
227   im->vnet_main = vnet_get_main ();
228   im->vlib_main = vm;
229
230   im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
231   im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
232   im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
233
234   vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
235   ASSERT (node);
236   im->error_drop_node_index = node->index;
237
238   u32 idx = ipsec_register_ah_backend (vm, im, "default openssl backend",
239                                        "ah4-encrypt",
240                                        "ah4-decrypt",
241                                        "ah6-encrypt",
242                                        "ah6-decrypt",
243                                        ipsec_check_ah_support,
244                                        NULL);
245
246   im->ah_default_backend = idx;
247   int rv = ipsec_select_ah_backend (im, idx);
248   ASSERT (0 == rv);
249   (void) (rv);                  // avoid warning
250
251   idx = ipsec_register_esp_backend (vm, im, "default openssl backend",
252                                     "esp4-encrypt",
253                                     "esp4-decrypt",
254                                     "esp6-encrypt",
255                                     "esp6-decrypt",
256                                     ipsec_check_esp_support, NULL);
257   im->esp_default_backend = idx;
258
259   rv = ipsec_select_esp_backend (im, idx);
260   ASSERT (0 == rv);
261   (void) (rv);                  // avoid warning
262
263   if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
264     return error;
265
266   if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
267     return error;
268
269   vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
270
271   a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
272   a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC;
273   a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC;
274   a->iv_size = a->block_size = 8;
275
276   a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
277   a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC;
278   a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC;
279   a->iv_size = a->block_size = 8;
280
281   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
282   a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC;
283   a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC;
284   a->iv_size = a->block_size = 16;
285
286   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
287   a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC;
288   a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC;
289   a->iv_size = a->block_size = 16;
290
291   a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
292   a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC;
293   a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC;
294   a->iv_size = a->block_size = 16;
295
296   vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
297   ipsec_main_integ_alg_t *i;
298
299   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
300   i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
301   i->icv_size = 12;
302
303   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
304   i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
305   i->icv_size = 12;
306
307   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
308   i->op_id = VNET_CRYPTO_OP_SHA256_HMAC;
309   i->icv_size = 16;
310
311   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
312   i->op_id = VNET_CRYPTO_OP_SHA384_HMAC;
313   i->icv_size = 24;
314
315   i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
316   i->op_id = VNET_CRYPTO_OP_SHA512_HMAC;
317   i->icv_size = 32;
318
319   vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES);
320
321   return 0;
322 }
323
324 VLIB_INIT_FUNCTION (ipsec_init);
325
326 /*
327  * fd.io coding-style-patch-verification: ON
328  *
329  * Local Variables:
330  * eval: (c-set-style "gnu")
331  * End:
332  */