2 * ipsec_if.c : IPSec interface support
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>
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
25 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
28 format_ipsec_name (u8 * s, va_list * args)
30 u32 dev_instance = va_arg (*args, u32);
31 return format (s, "ipsec%d", dev_instance);
35 dummy_interface_tx (vlib_main_t * vm,
36 vlib_node_runtime_t * node, vlib_frame_t * frame)
38 clib_warning ("you shouldn't be here, leaking buffers...");
39 return frame->n_vectors;
43 ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
45 ipsec_main_t *im = &ipsec_main;
46 clib_error_t *err = 0;
48 vnet_hw_interface_t *hi;
51 hi = vnet_get_hw_interface (vnm, hw_if_index);
52 t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
54 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
56 ASSERT (im->cb.check_support_cb);
58 sa = pool_elt_at_index (im->sad, t->input_sa_index);
60 err = im->cb.check_support_cb (sa);
64 if (im->cb.add_del_sa_sess_cb)
66 err = im->cb.add_del_sa_sess_cb (t->input_sa_index, 1);
71 sa = pool_elt_at_index (im->sad, t->output_sa_index);
73 err = im->cb.check_support_cb (sa);
77 if (im->cb.add_del_sa_sess_cb)
79 err = im->cb.add_del_sa_sess_cb (t->output_sa_index, 1);
84 vnet_hw_interface_set_flags (vnm, hw_if_index,
85 VNET_HW_INTERFACE_FLAG_LINK_UP);
89 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
91 sa = pool_elt_at_index (im->sad, t->input_sa_index);
93 if (im->cb.add_del_sa_sess_cb)
95 err = im->cb.add_del_sa_sess_cb (t->input_sa_index, 0);
100 sa = pool_elt_at_index (im->sad, t->output_sa_index);
102 if (im->cb.add_del_sa_sess_cb)
104 err = im->cb.add_del_sa_sess_cb (t->output_sa_index, 0);
110 return /* no error */ 0;
114 VNET_DEVICE_CLASS (ipsec_device_class, static) =
117 .format_device_name = format_ipsec_name,
118 .format_tx_trace = format_ipsec_if_output_trace,
119 .tx_function = dummy_interface_tx,
120 .admin_up_down_function = ipsec_admin_up_down_function,
125 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
128 .build_rewrite = default_build_rewrite,
129 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
134 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
136 vnet_main_t *vnm = vnet_get_main ();
137 ASSERT (vlib_get_thread_index () == 0);
139 return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
143 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
145 vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
146 (u8 *) args, sizeof (*args));
151 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
152 ipsec_add_del_tunnel_args_t * args,
155 ipsec_tunnel_if_t *t;
156 ipsec_main_t *im = &ipsec_main;
157 vnet_hw_interface_t *hi = NULL;
158 u32 hw_if_index = ~0;
162 u64 key = (u64) args->remote_ip.as_u32 << 32 | (u64) args->remote_spi;
163 p = hash_get (im->ipsec_if_pool_index_by_key, key);
167 /* check if same src/dst pair exists */
169 return VNET_API_ERROR_INVALID_VALUE;
171 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
172 memset (t, 0, sizeof (*t));
174 pool_get (im->sad, sa);
175 memset (sa, 0, sizeof (*sa));
176 t->input_sa_index = sa - im->sad;
177 sa->spi = args->remote_spi;
178 sa->tunnel_src_addr.ip4.as_u32 = args->remote_ip.as_u32;
179 sa->tunnel_dst_addr.ip4.as_u32 = args->local_ip.as_u32;
181 sa->use_esn = args->esn;
182 sa->use_anti_replay = args->anti_replay;
183 sa->integ_alg = args->integ_alg;
184 if (args->remote_integ_key_len <= sizeof (args->remote_integ_key))
186 sa->integ_key_len = args->remote_integ_key_len;
187 clib_memcpy (sa->integ_key, args->remote_integ_key,
188 args->remote_integ_key_len);
190 sa->crypto_alg = args->crypto_alg;
191 if (args->remote_crypto_key_len <= sizeof (args->remote_crypto_key))
193 sa->crypto_key_len = args->remote_crypto_key_len;
194 clib_memcpy (sa->crypto_key, args->remote_crypto_key,
195 args->remote_crypto_key_len);
198 pool_get (im->sad, sa);
199 memset (sa, 0, sizeof (*sa));
200 t->output_sa_index = sa - im->sad;
201 sa->spi = args->local_spi;
202 sa->tunnel_src_addr.ip4.as_u32 = args->local_ip.as_u32;
203 sa->tunnel_dst_addr.ip4.as_u32 = args->remote_ip.as_u32;
205 sa->use_esn = args->esn;
206 sa->use_anti_replay = args->anti_replay;
207 sa->integ_alg = args->integ_alg;
208 if (args->local_integ_key_len <= sizeof (args->local_integ_key))
210 sa->integ_key_len = args->local_integ_key_len;
211 clib_memcpy (sa->integ_key, args->local_integ_key,
212 args->local_integ_key_len);
214 sa->crypto_alg = args->crypto_alg;
215 if (args->local_crypto_key_len <= sizeof (args->local_crypto_key))
217 sa->crypto_key_len = args->local_crypto_key_len;
218 clib_memcpy (sa->crypto_key, args->local_crypto_key,
219 args->local_crypto_key_len);
222 hash_set (im->ipsec_if_pool_index_by_key, key,
223 t - im->tunnel_interfaces);
225 if (vec_len (im->free_tunnel_if_indices) > 0)
228 im->free_tunnel_if_indices[vec_len (im->free_tunnel_if_indices) -
230 _vec_len (im->free_tunnel_if_indices) -= 1;
235 vnet_register_interface (vnm, ipsec_device_class.index,
236 t - im->tunnel_interfaces,
237 ipsec_hw_class.index,
238 t - im->tunnel_interfaces);
241 hi = vnet_get_hw_interface (vnm, hw_if_index);
242 hi->output_node_index = ipsec_if_output_node.index;
243 t->hw_if_index = hw_if_index;
245 /*1st interface, register protocol */
246 if (pool_elts (im->tunnel_interfaces) == 1)
247 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
248 ipsec_if_input_node.index);
253 vnet_interface_main_t *vim = &vnm->interface_main;
255 /* check if exists */
257 return VNET_API_ERROR_INVALID_VALUE;
259 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
260 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
261 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0); /* admin down */
262 vec_add1 (im->free_tunnel_if_indices, t->hw_if_index);
264 vnet_interface_counter_lock (vim);
265 vlib_zero_combined_counter (vim->combined_sw_if_counters +
266 VNET_INTERFACE_COUNTER_TX, hi->sw_if_index);
267 vlib_zero_combined_counter (vim->combined_sw_if_counters +
268 VNET_INTERFACE_COUNTER_RX, hi->sw_if_index);
269 vnet_interface_counter_unlock (vim);
271 /* delete input and output SA */
272 sa = pool_elt_at_index (im->sad, t->input_sa_index);
274 pool_put (im->sad, sa);
276 sa = pool_elt_at_index (im->sad, t->output_sa_index);
278 pool_put (im->sad, sa);
280 hash_unset (im->ipsec_if_pool_index_by_key, key);
281 pool_put (im->tunnel_interfaces, t);
285 *sw_if_index = hi->sw_if_index;
291 ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
292 ipsec_add_del_ipsec_gre_tunnel_args_t * args)
294 ipsec_tunnel_if_t *t = 0;
295 ipsec_main_t *im = &ipsec_main;
301 p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
303 return VNET_API_ERROR_INVALID_VALUE;
306 p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
308 return VNET_API_ERROR_INVALID_VALUE;
310 sa = pool_elt_at_index (im->sad, p[0]);
313 key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi;
315 key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi;
317 p = hash_get (im->ipsec_if_pool_index_by_key, key);
321 /* check if same src/dst pair exists */
323 return VNET_API_ERROR_INVALID_VALUE;
325 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
326 memset (t, 0, sizeof (*t));
328 t->input_sa_index = isa;
329 t->output_sa_index = osa;
331 hash_set (im->ipsec_if_pool_index_by_key, key,
332 t - im->tunnel_interfaces);
334 /*1st interface, register protocol */
335 if (pool_elts (im->tunnel_interfaces) == 1)
336 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
337 ipsec_if_input_node.index);
341 /* check if exists */
343 return VNET_API_ERROR_INVALID_VALUE;
345 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
346 hash_unset (im->ipsec_if_pool_index_by_key, key);
347 pool_put (im->tunnel_interfaces, t);
353 ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
354 ipsec_if_set_key_type_t type, u8 alg, u8 * key)
356 ipsec_main_t *im = &ipsec_main;
357 vnet_hw_interface_t *hi;
358 ipsec_tunnel_if_t *t;
361 hi = vnet_get_hw_interface (vnm, hw_if_index);
362 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
364 if (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
365 return VNET_API_ERROR_SYSCALL_ERROR_1;
367 if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
369 sa = pool_elt_at_index (im->sad, t->output_sa_index);
370 sa->crypto_alg = alg;
371 sa->crypto_key_len = vec_len (key);
372 clib_memcpy (sa->crypto_key, key, vec_len (key));
374 else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
376 sa = pool_elt_at_index (im->sad, t->output_sa_index);
378 sa->integ_key_len = vec_len (key);
379 clib_memcpy (sa->integ_key, key, vec_len (key));
381 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
383 sa = pool_elt_at_index (im->sad, t->input_sa_index);
384 sa->crypto_alg = alg;
385 sa->crypto_key_len = vec_len (key);
386 clib_memcpy (sa->crypto_key, key, vec_len (key));
388 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
390 sa = pool_elt_at_index (im->sad, t->input_sa_index);
392 sa->integ_key_len = vec_len (key);
393 clib_memcpy (sa->integ_key, key, vec_len (key));
396 return VNET_API_ERROR_INVALID_VALUE;
403 ipsec_tunnel_if_init (vlib_main_t * vm)
405 ipsec_main_t *im = &ipsec_main;
407 im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
412 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
416 * fd.io coding-style-patch-verification: ON
419 * eval: (c-set-style "gnu")