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 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
54 t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
55 ASSERT (im->cb.check_support_cb);
56 sa = pool_elt_at_index (im->sad, t->input_sa_index);
57 err = im->cb.check_support_cb (sa);
61 sa = pool_elt_at_index (im->sad, t->output_sa_index);
62 err = im->cb.check_support_cb (sa);
66 vnet_hw_interface_set_flags (vnm, hw_if_index,
67 VNET_HW_INTERFACE_FLAG_LINK_UP);
70 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
72 return /* no error */ 0;
76 VNET_DEVICE_CLASS (ipsec_device_class, static) =
79 .format_device_name = format_ipsec_name,
80 .format_tx_trace = format_ipsec_if_output_trace,
81 .tx_function = dummy_interface_tx,
82 .admin_up_down_function = ipsec_admin_up_down_function,
87 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
90 .build_rewrite = default_build_rewrite,
95 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
96 ipsec_add_del_tunnel_args_t * args);
99 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
101 vnet_main_t *vnm = vnet_get_main ();
102 ASSERT (vlib_get_thread_index () == 0);
104 return ipsec_add_del_tunnel_if_internal (vnm, a);
108 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
110 vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
111 (u8 *) args, sizeof (*args));
116 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
117 ipsec_add_del_tunnel_args_t * args)
119 ipsec_tunnel_if_t *t;
120 ipsec_main_t *im = &ipsec_main;
121 vnet_hw_interface_t *hi;
122 u32 hw_if_index = ~0;
126 u64 key = (u64) args->remote_ip.as_u32 << 32 | (u64) args->remote_spi;
127 p = hash_get (im->ipsec_if_pool_index_by_key, key);
131 /* check if same src/dst pair exists */
133 return VNET_API_ERROR_INVALID_VALUE;
135 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
136 memset (t, 0, sizeof (*t));
138 pool_get (im->sad, sa);
139 memset (sa, 0, sizeof (*sa));
140 t->input_sa_index = sa - im->sad;
141 sa->spi = args->remote_spi;
142 sa->tunnel_src_addr.ip4.as_u32 = args->remote_ip.as_u32;
143 sa->tunnel_dst_addr.ip4.as_u32 = args->local_ip.as_u32;
145 sa->use_esn = args->esn;
146 sa->use_anti_replay = args->anti_replay;
147 sa->integ_alg = args->integ_alg;
148 if (args->remote_integ_key_len <= sizeof (args->remote_integ_key))
150 sa->integ_key_len = args->remote_integ_key_len;
151 clib_memcpy (sa->integ_key, args->remote_integ_key,
152 args->remote_integ_key_len);
154 sa->crypto_alg = args->crypto_alg;
155 if (args->remote_crypto_key_len <= sizeof (args->remote_crypto_key))
157 sa->crypto_key_len = args->remote_crypto_key_len;
158 clib_memcpy (sa->crypto_key, args->remote_crypto_key,
159 args->remote_crypto_key_len);
162 if (im->cb.add_del_sa_sess_cb &&
163 im->cb.add_del_sa_sess_cb (t->input_sa_index, args->is_add) < 0)
164 return VNET_API_ERROR_SYSCALL_ERROR_1;
166 pool_get (im->sad, sa);
167 memset (sa, 0, sizeof (*sa));
168 t->output_sa_index = sa - im->sad;
169 sa->spi = args->local_spi;
170 sa->tunnel_src_addr.ip4.as_u32 = args->local_ip.as_u32;
171 sa->tunnel_dst_addr.ip4.as_u32 = args->remote_ip.as_u32;
174 sa->use_esn = args->esn;
175 sa->use_anti_replay = args->anti_replay;
176 sa->integ_alg = args->integ_alg;
177 if (args->local_integ_key_len <= sizeof (args->local_integ_key))
179 sa->integ_key_len = args->local_integ_key_len;
180 clib_memcpy (sa->integ_key, args->local_integ_key,
181 args->local_integ_key_len);
183 sa->crypto_alg = args->crypto_alg;
184 if (args->local_crypto_key_len <= sizeof (args->local_crypto_key))
186 sa->crypto_key_len = args->local_crypto_key_len;
187 clib_memcpy (sa->crypto_key, args->local_crypto_key,
188 args->local_crypto_key_len);
191 if (im->cb.add_del_sa_sess_cb &&
192 im->cb.add_del_sa_sess_cb (t->output_sa_index, args->is_add) < 0)
193 return VNET_API_ERROR_SYSCALL_ERROR_1;
195 hash_set (im->ipsec_if_pool_index_by_key, key,
196 t - im->tunnel_interfaces);
198 if (vec_len (im->free_tunnel_if_indices) > 0)
201 im->free_tunnel_if_indices[vec_len (im->free_tunnel_if_indices) -
203 _vec_len (im->free_tunnel_if_indices) -= 1;
208 vnet_register_interface (vnm, ipsec_device_class.index,
209 t - im->tunnel_interfaces,
210 ipsec_hw_class.index,
211 t - im->tunnel_interfaces);
213 hi = vnet_get_hw_interface (vnm, hw_if_index);
214 hi->output_node_index = ipsec_if_output_node.index;
216 t->hw_if_index = hw_if_index;
218 /*1st interface, register protocol */
219 if (pool_elts (im->tunnel_interfaces) == 1)
220 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
221 ipsec_if_input_node.index);
227 /* check if exists */
229 return VNET_API_ERROR_INVALID_VALUE;
231 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
232 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
233 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0); /* admin down */
234 vec_add1 (im->free_tunnel_if_indices, t->hw_if_index);
236 /* delete input and output SA */
237 sa = pool_elt_at_index (im->sad, t->input_sa_index);
239 if (im->cb.add_del_sa_sess_cb &&
240 im->cb.add_del_sa_sess_cb (t->input_sa_index, args->is_add) < 0)
241 return VNET_API_ERROR_SYSCALL_ERROR_1;
243 pool_put (im->sad, sa);
245 sa = pool_elt_at_index (im->sad, t->output_sa_index);
247 if (im->cb.add_del_sa_sess_cb &&
248 im->cb.add_del_sa_sess_cb (t->output_sa_index, args->is_add) < 0)
249 return VNET_API_ERROR_SYSCALL_ERROR_1;
251 pool_put (im->sad, sa);
253 hash_unset (im->ipsec_if_pool_index_by_key, key);
254 pool_put (im->tunnel_interfaces, t);
260 ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
261 ipsec_add_del_ipsec_gre_tunnel_args_t * args)
263 ipsec_tunnel_if_t *t = 0;
264 ipsec_main_t *im = &ipsec_main;
270 p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
272 return VNET_API_ERROR_INVALID_VALUE;
275 p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
277 return VNET_API_ERROR_INVALID_VALUE;
279 sa = pool_elt_at_index (im->sad, p[0]);
282 key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi;
284 key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi;
286 p = hash_get (im->ipsec_if_pool_index_by_key, key);
290 /* check if same src/dst pair exists */
292 return VNET_API_ERROR_INVALID_VALUE;
294 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
295 memset (t, 0, sizeof (*t));
297 t->input_sa_index = isa;
298 t->output_sa_index = osa;
300 hash_set (im->ipsec_if_pool_index_by_key, key,
301 t - im->tunnel_interfaces);
303 /*1st interface, register protocol */
304 if (pool_elts (im->tunnel_interfaces) == 1)
305 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
306 ipsec_if_input_node.index);
310 /* check if exists */
312 return VNET_API_ERROR_INVALID_VALUE;
314 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
315 hash_unset (im->ipsec_if_pool_index_by_key, key);
316 pool_put (im->tunnel_interfaces, t);
322 ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
323 ipsec_if_set_key_type_t type, u8 alg, u8 * key)
325 ipsec_main_t *im = &ipsec_main;
326 vnet_hw_interface_t *hi;
327 ipsec_tunnel_if_t *t;
330 hi = vnet_get_hw_interface (vnm, hw_if_index);
331 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
333 if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
335 sa = pool_elt_at_index (im->sad, t->output_sa_index);
336 sa->crypto_alg = alg;
337 sa->crypto_key_len = vec_len (key);
338 clib_memcpy (sa->crypto_key, key, vec_len (key));
340 if (im->cb.add_del_sa_sess_cb &&
341 im->cb.add_del_sa_sess_cb (t->output_sa_index, 0) < 0)
342 return VNET_API_ERROR_SYSCALL_ERROR_1;
344 else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
346 sa = pool_elt_at_index (im->sad, t->output_sa_index);
348 sa->integ_key_len = vec_len (key);
349 clib_memcpy (sa->integ_key, key, vec_len (key));
351 if (im->cb.add_del_sa_sess_cb &&
352 im->cb.add_del_sa_sess_cb (t->output_sa_index, 0) < 0)
353 return VNET_API_ERROR_SYSCALL_ERROR_1;
355 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
357 sa = pool_elt_at_index (im->sad, t->input_sa_index);
358 sa->crypto_alg = alg;
359 sa->crypto_key_len = vec_len (key);
360 clib_memcpy (sa->crypto_key, key, vec_len (key));
362 if (im->cb.add_del_sa_sess_cb &&
363 im->cb.add_del_sa_sess_cb (t->input_sa_index, 0) < 0)
364 return VNET_API_ERROR_SYSCALL_ERROR_1;
366 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
368 sa = pool_elt_at_index (im->sad, t->input_sa_index);
370 sa->integ_key_len = vec_len (key);
371 clib_memcpy (sa->integ_key, key, vec_len (key));
373 if (im->cb.add_del_sa_sess_cb &&
374 im->cb.add_del_sa_sess_cb (t->input_sa_index, 0) < 0)
375 return VNET_API_ERROR_SYSCALL_ERROR_1;
378 return VNET_API_ERROR_INVALID_VALUE;
385 ipsec_tunnel_if_init (vlib_main_t * vm)
387 ipsec_main_t *im = &ipsec_main;
389 im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
394 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
398 * fd.io coding-style-patch-verification: ON
401 * eval: (c-set-style "gnu")