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,
91 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
96 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
98 vnet_main_t *vnm = vnet_get_main ();
99 ASSERT (vlib_get_thread_index () == 0);
101 return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
105 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
107 vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
108 (u8 *) args, sizeof (*args));
113 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
114 ipsec_add_del_tunnel_args_t * args,
117 ipsec_tunnel_if_t *t;
118 ipsec_main_t *im = &ipsec_main;
119 vnet_hw_interface_t *hi = NULL;
120 u32 hw_if_index = ~0;
124 u64 key = (u64) args->remote_ip.as_u32 << 32 | (u64) args->remote_spi;
125 p = hash_get (im->ipsec_if_pool_index_by_key, key);
129 /* check if same src/dst pair exists */
131 return VNET_API_ERROR_INVALID_VALUE;
133 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
134 memset (t, 0, sizeof (*t));
136 pool_get (im->sad, sa);
137 memset (sa, 0, sizeof (*sa));
138 t->input_sa_index = sa - im->sad;
139 sa->spi = args->remote_spi;
140 sa->tunnel_src_addr.ip4.as_u32 = args->remote_ip.as_u32;
141 sa->tunnel_dst_addr.ip4.as_u32 = args->local_ip.as_u32;
143 sa->use_esn = args->esn;
144 sa->use_anti_replay = args->anti_replay;
145 sa->integ_alg = args->integ_alg;
146 if (args->remote_integ_key_len <= sizeof (args->remote_integ_key))
148 sa->integ_key_len = args->remote_integ_key_len;
149 clib_memcpy (sa->integ_key, args->remote_integ_key,
150 args->remote_integ_key_len);
152 sa->crypto_alg = args->crypto_alg;
153 if (args->remote_crypto_key_len <= sizeof (args->remote_crypto_key))
155 sa->crypto_key_len = args->remote_crypto_key_len;
156 clib_memcpy (sa->crypto_key, args->remote_crypto_key,
157 args->remote_crypto_key_len);
160 if (im->cb.add_del_sa_sess_cb &&
161 im->cb.add_del_sa_sess_cb (t->input_sa_index, args->is_add) < 0)
162 return VNET_API_ERROR_SYSCALL_ERROR_1;
164 pool_get (im->sad, sa);
165 memset (sa, 0, sizeof (*sa));
166 t->output_sa_index = sa - im->sad;
167 sa->spi = args->local_spi;
168 sa->tunnel_src_addr.ip4.as_u32 = args->local_ip.as_u32;
169 sa->tunnel_dst_addr.ip4.as_u32 = args->remote_ip.as_u32;
172 sa->use_esn = args->esn;
173 sa->use_anti_replay = args->anti_replay;
174 sa->integ_alg = args->integ_alg;
175 if (args->local_integ_key_len <= sizeof (args->local_integ_key))
177 sa->integ_key_len = args->local_integ_key_len;
178 clib_memcpy (sa->integ_key, args->local_integ_key,
179 args->local_integ_key_len);
181 sa->crypto_alg = args->crypto_alg;
182 if (args->local_crypto_key_len <= sizeof (args->local_crypto_key))
184 sa->crypto_key_len = args->local_crypto_key_len;
185 clib_memcpy (sa->crypto_key, args->local_crypto_key,
186 args->local_crypto_key_len);
189 if (im->cb.add_del_sa_sess_cb &&
190 im->cb.add_del_sa_sess_cb (t->output_sa_index, args->is_add) < 0)
191 return VNET_API_ERROR_SYSCALL_ERROR_1;
193 hash_set (im->ipsec_if_pool_index_by_key, key,
194 t - im->tunnel_interfaces);
196 if (vec_len (im->free_tunnel_if_indices) > 0)
199 im->free_tunnel_if_indices[vec_len (im->free_tunnel_if_indices) -
201 _vec_len (im->free_tunnel_if_indices) -= 1;
206 vnet_register_interface (vnm, ipsec_device_class.index,
207 t - im->tunnel_interfaces,
208 ipsec_hw_class.index,
209 t - im->tunnel_interfaces);
212 hi = vnet_get_hw_interface (vnm, hw_if_index);
213 hi->output_node_index = ipsec_if_output_node.index;
214 t->hw_if_index = hw_if_index;
216 /*1st interface, register protocol */
217 if (pool_elts (im->tunnel_interfaces) == 1)
218 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
219 ipsec_if_input_node.index);
224 vnet_interface_main_t *vim = &vnm->interface_main;
226 /* check if exists */
228 return VNET_API_ERROR_INVALID_VALUE;
230 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
231 hi = vnet_get_hw_interface (vnm, t->hw_if_index);
232 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0); /* admin down */
233 vec_add1 (im->free_tunnel_if_indices, t->hw_if_index);
235 vnet_interface_counter_lock (vim);
236 vlib_zero_combined_counter (vim->combined_sw_if_counters +
237 VNET_INTERFACE_COUNTER_TX, hi->sw_if_index);
238 vlib_zero_combined_counter (vim->combined_sw_if_counters +
239 VNET_INTERFACE_COUNTER_RX, hi->sw_if_index);
240 vnet_interface_counter_unlock (vim);
242 /* delete input and output SA */
243 sa = pool_elt_at_index (im->sad, t->input_sa_index);
245 if (im->cb.add_del_sa_sess_cb &&
246 im->cb.add_del_sa_sess_cb (t->input_sa_index, args->is_add) < 0)
247 return VNET_API_ERROR_SYSCALL_ERROR_1;
249 pool_put (im->sad, sa);
251 sa = pool_elt_at_index (im->sad, t->output_sa_index);
253 if (im->cb.add_del_sa_sess_cb &&
254 im->cb.add_del_sa_sess_cb (t->output_sa_index, args->is_add) < 0)
255 return VNET_API_ERROR_SYSCALL_ERROR_1;
257 pool_put (im->sad, sa);
259 hash_unset (im->ipsec_if_pool_index_by_key, key);
260 pool_put (im->tunnel_interfaces, t);
264 *sw_if_index = hi->sw_if_index;
270 ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
271 ipsec_add_del_ipsec_gre_tunnel_args_t * args)
273 ipsec_tunnel_if_t *t = 0;
274 ipsec_main_t *im = &ipsec_main;
280 p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
282 return VNET_API_ERROR_INVALID_VALUE;
285 p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
287 return VNET_API_ERROR_INVALID_VALUE;
289 sa = pool_elt_at_index (im->sad, p[0]);
292 key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi;
294 key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi;
296 p = hash_get (im->ipsec_if_pool_index_by_key, key);
300 /* check if same src/dst pair exists */
302 return VNET_API_ERROR_INVALID_VALUE;
304 pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
305 memset (t, 0, sizeof (*t));
307 t->input_sa_index = isa;
308 t->output_sa_index = osa;
310 hash_set (im->ipsec_if_pool_index_by_key, key,
311 t - im->tunnel_interfaces);
313 /*1st interface, register protocol */
314 if (pool_elts (im->tunnel_interfaces) == 1)
315 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
316 ipsec_if_input_node.index);
320 /* check if exists */
322 return VNET_API_ERROR_INVALID_VALUE;
324 t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
325 hash_unset (im->ipsec_if_pool_index_by_key, key);
326 pool_put (im->tunnel_interfaces, t);
332 ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
333 ipsec_if_set_key_type_t type, u8 alg, u8 * key)
335 ipsec_main_t *im = &ipsec_main;
336 vnet_hw_interface_t *hi;
337 ipsec_tunnel_if_t *t;
340 hi = vnet_get_hw_interface (vnm, hw_if_index);
341 t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
343 if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
345 sa = pool_elt_at_index (im->sad, t->output_sa_index);
346 sa->crypto_alg = alg;
347 sa->crypto_key_len = vec_len (key);
348 clib_memcpy (sa->crypto_key, key, vec_len (key));
350 if (im->cb.add_del_sa_sess_cb &&
351 im->cb.add_del_sa_sess_cb (t->output_sa_index, 0) < 0)
352 return VNET_API_ERROR_SYSCALL_ERROR_1;
354 else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
356 sa = pool_elt_at_index (im->sad, t->output_sa_index);
358 sa->integ_key_len = vec_len (key);
359 clib_memcpy (sa->integ_key, key, vec_len (key));
361 if (im->cb.add_del_sa_sess_cb &&
362 im->cb.add_del_sa_sess_cb (t->output_sa_index, 0) < 0)
363 return VNET_API_ERROR_SYSCALL_ERROR_1;
365 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
367 sa = pool_elt_at_index (im->sad, t->input_sa_index);
368 sa->crypto_alg = alg;
369 sa->crypto_key_len = vec_len (key);
370 clib_memcpy (sa->crypto_key, key, vec_len (key));
372 if (im->cb.add_del_sa_sess_cb &&
373 im->cb.add_del_sa_sess_cb (t->input_sa_index, 0) < 0)
374 return VNET_API_ERROR_SYSCALL_ERROR_1;
376 else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
378 sa = pool_elt_at_index (im->sad, t->input_sa_index);
380 sa->integ_key_len = vec_len (key);
381 clib_memcpy (sa->integ_key, key, vec_len (key));
383 if (im->cb.add_del_sa_sess_cb &&
384 im->cb.add_del_sa_sess_cb (t->input_sa_index, 0) < 0)
385 return VNET_API_ERROR_SYSCALL_ERROR_1;
388 return VNET_API_ERROR_INVALID_VALUE;
395 ipsec_tunnel_if_init (vlib_main_t * vm)
397 ipsec_main_t *im = &ipsec_main;
399 im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
404 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
408 * fd.io coding-style-patch-verification: ON
411 * eval: (c-set-style "gnu")