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/ikev2.h>
26 #include <vnet/ipsec/esp.h>
27 #include <vnet/ipsec/ah.h>
29 ipsec_main_t ipsec_main;
32 ipsec_rand_seed (void)
41 seed_data.time = time (NULL);
42 seed_data.pid = getpid ();
43 seed_data.p = (void *) &seed_data;
45 RAND_seed ((const void *) &seed_data, sizeof (seed_data));
49 ipsec_check_ah_support (ipsec_sa_t * sa)
51 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
52 return clib_error_return (0, "unsupported none integ-alg");
57 ipsec_check_esp_support (ipsec_sa_t * sa)
59 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
60 return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg");
61 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_192)
62 return clib_error_return (0, "unsupported aes-gcm-192 crypto-alg");
63 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_256)
64 return clib_error_return (0, "unsupported aes-gcm-256 crypto-alg");
70 ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
72 ipsec_ah_backend_t *ah =
73 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
74 if (ah->add_del_sa_sess_cb)
76 clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
80 ipsec_esp_backend_t *esp =
81 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
82 if (esp->add_del_sa_sess_cb)
84 clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
92 ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
94 clib_error_t *error = 0;
96 if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH))
98 ipsec_ah_backend_t *ah =
99 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
100 ASSERT (ah->check_support_cb);
101 error = ah->check_support_cb (sa);
105 ipsec_esp_backend_t *esp =
106 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
107 ASSERT (esp->check_support_cb);
108 error = esp->check_support_cb (sa);
115 ipsec_add_node (vlib_main_t * vm, const char *node_name,
116 const char *prev_node_name, u32 * out_node_index,
117 u32 * out_next_index)
119 vlib_node_t *prev_node, *node;
120 prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
122 node = vlib_get_node_by_name (vm, (u8 *) node_name);
124 *out_node_index = node->index;
125 *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
129 ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
131 const char *ah4_encrypt_node_name,
132 const char *ah4_decrypt_node_name,
133 const char *ah6_encrypt_node_name,
134 const char *ah6_decrypt_node_name,
135 check_support_cb_t ah_check_support_cb,
136 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
138 ipsec_ah_backend_t *b;
139 pool_get (im->ah_backends, b);
140 b->name = format (NULL, "%s", name);
142 ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature",
143 &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
144 ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature",
145 &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
146 ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature",
147 &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
148 ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature",
149 &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
151 b->check_support_cb = ah_check_support_cb;
152 b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
153 return b - im->ah_backends;
157 ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im,
159 const char *esp4_encrypt_node_name,
160 const char *esp4_decrypt_node_name,
161 const char *esp6_encrypt_node_name,
162 const char *esp6_decrypt_node_name,
163 check_support_cb_t esp_check_support_cb,
164 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb)
166 ipsec_esp_backend_t *b;
167 pool_get (im->esp_backends, b);
168 b->name = format (NULL, "%s", name);
170 ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature",
171 &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
172 ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature",
173 &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
174 ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature",
175 &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
176 ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature",
177 &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
179 b->check_support_cb = esp_check_support_cb;
180 b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
181 return b - im->esp_backends;
185 ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
187 if (pool_elts (im->sad) > 0
188 || pool_is_free_index (im->ah_backends, backend_idx))
192 ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
193 im->ah_current_backend = backend_idx;
194 im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
195 im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
196 im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
197 im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
198 im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
199 im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
200 im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
201 im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
206 ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
208 if (pool_elts (im->sad) > 0
209 || pool_is_free_index (im->esp_backends, backend_idx))
213 ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
214 im->esp_current_backend = backend_idx;
215 im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
216 im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
217 im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
218 im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
219 im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
220 im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
221 im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
222 im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
226 static clib_error_t *
227 ipsec_init (vlib_main_t * vm)
230 ipsec_main_t *im = &ipsec_main;
231 vlib_thread_main_t *tm = vlib_get_thread_main ();
235 clib_memset (im, 0, sizeof (im[0]));
237 im->vnet_main = vnet_get_main ();
240 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
241 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
242 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
244 vec_validate_aligned (im->empty_buffers, tm->n_vlib_mains - 1,
245 CLIB_CACHE_LINE_BYTES);
247 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
249 im->error_drop_node_index = node->index;
251 u32 idx = ipsec_register_ah_backend (vm, im, "default openssl backend",
256 ipsec_check_ah_support,
259 im->ah_default_backend = idx;
260 int rv = ipsec_select_ah_backend (im, idx);
262 (void) (rv); // avoid warning
264 idx = ipsec_register_esp_backend (vm, im, "default openssl backend",
269 ipsec_check_esp_support, NULL);
270 im->esp_default_backend = idx;
272 rv = ipsec_select_esp_backend (im, idx);
274 (void) (rv); // avoid warning
276 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
279 if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
284 if ((error = ikev2_init (vm)))
290 VLIB_INIT_FUNCTION (ipsec_init);
293 * fd.io coding-style-patch-verification: ON
296 * eval: (c-set-style "gnu")