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