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)
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");
52 ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
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)
58 clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
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)
66 clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
74 ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
76 clib_error_t *error = 0;
78 if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH))
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);
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);
97 ipsec_add_node (vlib_main_t * vm, const char *node_name,
98 const char *prev_node_name, u32 * out_node_index,
101 vlib_node_t *prev_node, *node;
102 prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
104 node = vlib_get_node_by_name (vm, (u8 *) node_name);
106 *out_node_index = node->index;
107 *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
111 ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
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)
120 ipsec_ah_backend_t *b;
121 pool_get (im->ah_backends, b);
122 b->name = format (0, "%s%c", name, 0);
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);
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;
139 ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im,
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)
148 ipsec_esp_backend_t *b;
149 pool_get (im->esp_backends, b);
150 b->name = format (0, "%s%c", name, 0);
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);
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;
167 ipsec_sa_restack (ipsec_sa_t * sa, void *ctx)
171 return (WALK_CONTINUE);
175 ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
177 if (pool_elts (im->sad) > 0
178 || pool_is_free_index (im->ah_backends, backend_idx))
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;
193 ipsec_sa_walk (ipsec_sa_restack, NULL);
198 ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
200 if (pool_elts (im->sad) > 0
201 || pool_is_free_index (im->esp_backends, backend_idx))
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;
216 ipsec_sa_walk (ipsec_sa_restack, NULL);
220 static clib_error_t *
221 ipsec_init (vlib_main_t * vm)
224 ipsec_main_t *im = &ipsec_main;
225 ipsec_main_crypto_alg_t *a;
227 im->vnet_main = vnet_get_main ();
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));
234 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
236 im->error_drop_node_index = node->index;
238 u32 idx = ipsec_register_ah_backend (vm, im, "default openssl backend",
243 ipsec_check_ah_support,
246 im->ah_default_backend = idx;
247 int rv = ipsec_select_ah_backend (im, idx);
249 (void) (rv); // avoid warning
251 idx = ipsec_register_esp_backend (vm, im, "default openssl backend",
256 ipsec_check_esp_support, NULL);
257 im->esp_default_backend = idx;
259 rv = ipsec_select_esp_backend (im, idx);
261 (void) (rv); // avoid warning
263 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
266 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
269 vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
271 a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
272 a->enc_op_type = VNET_CRYPTO_OP_DES_CBC_ENC;
273 a->dec_op_type = VNET_CRYPTO_OP_DES_CBC_DEC;
274 a->iv_size = a->block_size = 8;
276 a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
277 a->enc_op_type = VNET_CRYPTO_OP_3DES_CBC_ENC;
278 a->dec_op_type = VNET_CRYPTO_OP_3DES_CBC_DEC;
279 a->iv_size = a->block_size = 8;
281 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
282 a->enc_op_type = VNET_CRYPTO_OP_AES_128_CBC_ENC;
283 a->dec_op_type = VNET_CRYPTO_OP_AES_128_CBC_DEC;
284 a->iv_size = a->block_size = 16;
286 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
287 a->enc_op_type = VNET_CRYPTO_OP_AES_192_CBC_ENC;
288 a->dec_op_type = VNET_CRYPTO_OP_AES_192_CBC_DEC;
289 a->iv_size = a->block_size = 16;
291 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
292 a->enc_op_type = VNET_CRYPTO_OP_AES_256_CBC_ENC;
293 a->dec_op_type = VNET_CRYPTO_OP_AES_256_CBC_DEC;
294 a->iv_size = a->block_size = 16;
296 vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
297 ipsec_main_integ_alg_t *i;
299 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
300 i->op_type = VNET_CRYPTO_OP_SHA1_HMAC;
303 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
304 i->op_type = VNET_CRYPTO_OP_SHA1_HMAC;
307 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
308 i->op_type = VNET_CRYPTO_OP_SHA256_HMAC;
311 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
312 i->op_type = VNET_CRYPTO_OP_SHA384_HMAC;
315 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
316 i->op_type = VNET_CRYPTO_OP_SHA512_HMAC;
322 VLIB_INIT_FUNCTION (ipsec_init);
325 * fd.io coding-style-patch-verification: ON
328 * eval: (c-set-style "gnu")