2 * ipsec.c : IPSEC module functions
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
24 #include <vnet/ipsec/ipsec.h>
25 #include <vnet/ipsec/esp.h>
26 #include <vnet/ipsec/ah.h>
28 ipsec_main_t ipsec_main;
31 ipsec_check_ah_support (ipsec_sa_t * sa)
33 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
34 return clib_error_return (0, "unsupported none integ-alg");
39 ipsec_check_esp_support (ipsec_sa_t * sa)
45 ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
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)
51 clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
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)
59 clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
67 ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
69 clib_error_t *error = 0;
71 if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH))
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);
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);
90 ipsec_add_node (vlib_main_t * vm, const char *node_name,
91 const char *prev_node_name, u32 * out_node_index,
94 vlib_node_t *prev_node, *node;
95 prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
97 node = vlib_get_node_by_name (vm, (u8 *) node_name);
99 *out_node_index = node->index;
100 *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
104 ipsec_add_feature (const char *arc_name,
105 const char *node_name, u32 * out_feature_index)
109 arc = vnet_get_feature_arc_index (arc_name);
110 *out_feature_index = vnet_get_feature_index (arc, node_name);
114 ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
116 const char *ah4_encrypt_node_name,
117 const char *ah4_decrypt_node_name,
118 const char *ah6_encrypt_node_name,
119 const char *ah6_decrypt_node_name,
120 check_support_cb_t ah_check_support_cb,
121 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
123 ipsec_ah_backend_t *b;
124 pool_get (im->ah_backends, b);
125 b->name = format (0, "%s%c", name, 0);
127 ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature",
128 &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
129 ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature",
130 &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
131 ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature",
132 &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
133 ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature",
134 &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
136 b->check_support_cb = ah_check_support_cb;
137 b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
138 return b - im->ah_backends;
142 ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im,
144 const char *esp4_encrypt_node_name,
145 const char *esp4_encrypt_node_tun_name,
146 const char *esp4_decrypt_node_name,
147 const char *esp6_encrypt_node_name,
148 const char *esp6_encrypt_node_tun_name,
149 const char *esp6_decrypt_node_name,
150 check_support_cb_t esp_check_support_cb,
151 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb)
153 ipsec_esp_backend_t *b;
155 pool_get (im->esp_backends, b);
156 b->name = format (0, "%s%c", name, 0);
158 ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature",
159 &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
160 ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature",
161 &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
162 ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature",
163 &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
164 ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature",
165 &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
167 ipsec_add_feature ("ip4-output", esp4_encrypt_node_tun_name,
168 &b->esp4_encrypt_tun_feature_index);
169 ipsec_add_feature ("ip6-output", esp6_encrypt_node_tun_name,
170 &b->esp6_encrypt_tun_feature_index);
172 b->check_support_cb = esp_check_support_cb;
173 b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
174 return b - im->esp_backends;
178 ipsec_rsc_in_use (ipsec_main_t * im)
180 /* return an error is crypto resource are in use */
181 if (pool_elts (im->sad) > 0)
182 return clib_error_return (0,
183 "%d SA entries configured",
184 pool_elts (im->sad));
186 if (pool_elts (im->tunnel_interfaces))
187 return clib_error_return (0,
188 "%d tunnel-interface entries configured",
189 pool_elts (im->tunnel_interfaces));
195 ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
197 if (ipsec_rsc_in_use (im))
198 return VNET_API_ERROR_RSRC_IN_USE;
200 if (pool_is_free_index (im->ah_backends, backend_idx))
201 return VNET_API_ERROR_INVALID_VALUE;
203 ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
204 im->ah_current_backend = backend_idx;
205 im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
206 im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
207 im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
208 im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
209 im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
210 im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
211 im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
212 im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
218 ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
220 if (ipsec_rsc_in_use (im))
221 return VNET_API_ERROR_RSRC_IN_USE;
223 if (pool_is_free_index (im->esp_backends, backend_idx))
224 return VNET_API_ERROR_INVALID_VALUE;
226 ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
227 im->esp_current_backend = backend_idx;
228 im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
229 im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
230 im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
231 im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
232 im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
233 im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
234 im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
235 im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
237 im->esp4_encrypt_tun_feature_index = b->esp4_encrypt_tun_feature_index;
238 im->esp6_encrypt_tun_feature_index = b->esp6_encrypt_tun_feature_index;
243 static clib_error_t *
244 ipsec_init (vlib_main_t * vm)
247 ipsec_main_t *im = &ipsec_main;
248 ipsec_main_crypto_alg_t *a;
250 im->vnet_main = vnet_get_main ();
253 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
254 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
255 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
257 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
259 im->error_drop_node_index = node->index;
261 u32 idx = ipsec_register_ah_backend (vm, im, "default openssl backend",
266 ipsec_check_ah_support,
269 im->ah_default_backend = idx;
270 int rv = ipsec_select_ah_backend (im, idx);
272 (void) (rv); // avoid warning
274 idx = ipsec_register_esp_backend (vm, im, "default openssl backend",
281 ipsec_check_esp_support, NULL);
282 im->esp_default_backend = idx;
284 rv = ipsec_select_esp_backend (im, idx);
286 (void) (rv); // avoid warning
288 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
291 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
294 vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
296 a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
297 a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC;
298 a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC;
299 a->alg = VNET_CRYPTO_ALG_DES_CBC;
300 a->iv_size = a->block_size = 8;
302 a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
303 a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC;
304 a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC;
305 a->alg = VNET_CRYPTO_ALG_3DES_CBC;
306 a->iv_size = a->block_size = 8;
308 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
309 a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC;
310 a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC;
311 a->alg = VNET_CRYPTO_ALG_AES_128_CBC;
312 a->iv_size = a->block_size = 16;
314 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
315 a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC;
316 a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC;
317 a->alg = VNET_CRYPTO_ALG_AES_192_CBC;
318 a->iv_size = a->block_size = 16;
320 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
321 a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC;
322 a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC;
323 a->alg = VNET_CRYPTO_ALG_AES_256_CBC;
324 a->iv_size = a->block_size = 16;
326 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128;
327 a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
328 a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
329 a->alg = VNET_CRYPTO_ALG_AES_128_GCM;
330 a->iv_size = a->block_size = 8;
333 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192;
334 a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC;
335 a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC;
336 a->alg = VNET_CRYPTO_ALG_AES_192_GCM;
337 a->iv_size = a->block_size = 8;
340 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256;
341 a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
342 a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
343 a->alg = VNET_CRYPTO_ALG_AES_256_GCM;
344 a->iv_size = a->block_size = 8;
347 vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
348 ipsec_main_integ_alg_t *i;
350 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
351 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
352 i->alg = VNET_CRYPTO_ALG_HMAC_SHA1;
355 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
356 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
357 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
360 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
361 i->op_id = VNET_CRYPTO_OP_SHA256_HMAC;
362 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
365 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
366 i->op_id = VNET_CRYPTO_OP_SHA384_HMAC;
367 i->alg = VNET_CRYPTO_ALG_HMAC_SHA384;
370 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
371 i->op_id = VNET_CRYPTO_OP_SHA512_HMAC;
372 i->alg = VNET_CRYPTO_ALG_HMAC_SHA512;
375 vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES);
380 VLIB_INIT_FUNCTION (ipsec_init);
383 * fd.io coding-style-patch-verification: ON
386 * eval: (c-set-style "gnu")