FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
ipsec_if.c
Go to the documentation of this file.
1 /*
2  * ipsec_if.c : IPSec interface support
3  *
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:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
21 
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24 
25 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
26 
27 static u8 *
28 format_ipsec_name (u8 * s, va_list * args)
29 {
30  u32 dev_instance = va_arg (*args, u32);
31  return format (s, "ipsec%d", dev_instance);
32 }
33 
34 static uword
36  vlib_node_runtime_t * node, vlib_frame_t * frame)
37 {
38  clib_warning ("you shouldn't be here, leaking buffers...");
39  return frame->n_vectors;
40 }
41 
42 static clib_error_t *
44 {
45  ipsec_main_t *im = &ipsec_main;
46  clib_error_t *err = 0;
49  ipsec_sa_t *sa;
50 
51  hi = vnet_get_hw_interface (vnm, hw_if_index);
53 
55  {
57 
58  sa = pool_elt_at_index (im->sad, t->input_sa_index);
59 
60  err = im->cb.check_support_cb (sa);
61  if (err)
62  return err;
63 
64  if (im->cb.add_del_sa_sess_cb)
65  {
66  err = im->cb.add_del_sa_sess_cb (t->input_sa_index, 1);
67  if (err)
68  return err;
69  }
70 
71  sa = pool_elt_at_index (im->sad, t->output_sa_index);
72 
73  err = im->cb.check_support_cb (sa);
74  if (err)
75  return err;
76 
77  if (im->cb.add_del_sa_sess_cb)
78  {
79  err = im->cb.add_del_sa_sess_cb (t->output_sa_index, 1);
80  if (err)
81  return err;
82  }
83 
84  vnet_hw_interface_set_flags (vnm, hw_if_index,
86  }
87  else
88  {
89  vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
90 
91  sa = pool_elt_at_index (im->sad, t->input_sa_index);
92 
93  if (im->cb.add_del_sa_sess_cb)
94  {
95  err = im->cb.add_del_sa_sess_cb (t->input_sa_index, 0);
96  if (err)
97  return err;
98  }
99 
100  sa = pool_elt_at_index (im->sad, t->output_sa_index);
101 
102  if (im->cb.add_del_sa_sess_cb)
103  {
104  err = im->cb.add_del_sa_sess_cb (t->output_sa_index, 0);
105  if (err)
106  return err;
107  }
108  }
109 
110  return /* no error */ 0;
111 }
112 
113 /* *INDENT-OFF* */
114 VNET_DEVICE_CLASS (ipsec_device_class, static) =
115 {
116  .name = "IPSec",
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,
121 };
122 /* *INDENT-ON* */
123 
124 /* *INDENT-OFF* */
125 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
126 {
127  .name = "IPSec",
128  .build_rewrite = default_build_rewrite,
130 };
131 /* *INDENT-ON* */
132 
133 static int
135 {
136  vnet_main_t *vnm = vnet_get_main ();
137  ASSERT (vlib_get_thread_index () == 0);
138 
139  return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
140 }
141 
142 int
144 {
146  (u8 *) args, sizeof (*args));
147  return 0;
148 }
149 
150 int
153  u32 * sw_if_index)
154 {
156  ipsec_main_t *im = &ipsec_main;
158  u32 hw_if_index = ~0;
159  uword *p;
160  ipsec_sa_t *sa;
161 
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);
164 
165  if (args->is_add)
166  {
167  /* check if same src/dst pair exists */
168  if (p)
169  return VNET_API_ERROR_INVALID_VALUE;
170 
172  memset (t, 0, sizeof (*t));
173 
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;
180  sa->is_tunnel = 1;
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))
185  {
188  args->remote_integ_key_len);
189  }
190  sa->crypto_alg = args->crypto_alg;
191  if (args->remote_crypto_key_len <= sizeof (args->remote_crypto_key))
192  {
195  args->remote_crypto_key_len);
196  }
197 
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;
204  sa->is_tunnel = 1;
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))
209  {
212  args->local_integ_key_len);
213  }
214  sa->crypto_alg = args->crypto_alg;
215  if (args->local_crypto_key_len <= sizeof (args->local_crypto_key))
216  {
219  args->local_crypto_key_len);
220  }
221 
223  t - im->tunnel_interfaces);
224 
225  if (vec_len (im->free_tunnel_if_indices) > 0)
226  {
227  hw_if_index =
229  1];
230  _vec_len (im->free_tunnel_if_indices) -= 1;
231  }
232  else
233  {
234  hw_if_index =
235  vnet_register_interface (vnm, ipsec_device_class.index,
236  t - im->tunnel_interfaces,
237  ipsec_hw_class.index,
238  t - im->tunnel_interfaces);
239  }
240 
241  hi = vnet_get_hw_interface (vnm, hw_if_index);
243  t->hw_if_index = hw_if_index;
244 
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);
249 
250  }
251  else
252  {
254 
255  /* check if exists */
256  if (!p)
257  return VNET_API_ERROR_INVALID_VALUE;
258 
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 */
263 
270 
271  /* delete input and output SA */
272  sa = pool_elt_at_index (im->sad, t->input_sa_index);
273 
274  pool_put (im->sad, sa);
275 
276  sa = pool_elt_at_index (im->sad, t->output_sa_index);
277 
278  pool_put (im->sad, sa);
279 
281  pool_put (im->tunnel_interfaces, t);
282  }
283 
284  if (sw_if_index)
285  *sw_if_index = hi->sw_if_index;
286 
287  return 0;
288 }
289 
290 int
293 {
294  ipsec_tunnel_if_t *t = 0;
295  ipsec_main_t *im = &ipsec_main;
296  uword *p;
297  ipsec_sa_t *sa;
298  u64 key;
299  u32 isa, osa;
300 
301  p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
302  if (!p)
303  return VNET_API_ERROR_INVALID_VALUE;
304  isa = p[0];
305 
306  p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
307  if (!p)
308  return VNET_API_ERROR_INVALID_VALUE;
309  osa = p[0];
310  sa = pool_elt_at_index (im->sad, p[0]);
311 
312  if (sa->is_tunnel)
313  key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi;
314  else
315  key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi;
316 
317  p = hash_get (im->ipsec_if_pool_index_by_key, key);
318 
319  if (args->is_add)
320  {
321  /* check if same src/dst pair exists */
322  if (p)
323  return VNET_API_ERROR_INVALID_VALUE;
324 
326  memset (t, 0, sizeof (*t));
327 
328  t->input_sa_index = isa;
329  t->output_sa_index = osa;
330  t->hw_if_index = ~0;
332  t - im->tunnel_interfaces);
333 
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);
338  }
339  else
340  {
341  /* check if exists */
342  if (!p)
343  return VNET_API_ERROR_INVALID_VALUE;
344 
345  t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
347  pool_put (im->tunnel_interfaces, t);
348  }
349  return 0;
350 }
351 
352 int
354  ipsec_if_set_key_type_t type, u8 alg, u8 * key)
355 {
356  ipsec_main_t *im = &ipsec_main;
359  ipsec_sa_t *sa;
360 
361  hi = vnet_get_hw_interface (vnm, hw_if_index);
363 
365  return VNET_API_ERROR_SYSCALL_ERROR_1;
366 
368  {
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));
373  }
374  else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
375  {
376  sa = pool_elt_at_index (im->sad, t->output_sa_index);
377  sa->integ_alg = alg;
378  sa->integ_key_len = vec_len (key);
379  clib_memcpy (sa->integ_key, key, vec_len (key));
380  }
381  else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
382  {
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));
387  }
388  else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
389  {
390  sa = pool_elt_at_index (im->sad, t->input_sa_index);
391  sa->integ_alg = alg;
392  sa->integ_key_len = vec_len (key);
393  clib_memcpy (sa->integ_key, key, vec_len (key));
394  }
395  else
396  return VNET_API_ERROR_INVALID_VALUE;
397 
398  return 0;
399 }
400 
401 
402 int
403 ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id,
404  u8 is_outbound)
405 {
406  ipsec_main_t *im = &ipsec_main;
409  ipsec_sa_t *sa, *old_sa;
410  u32 sa_index, old_sa_index;
411  uword *p;
412 
413  hi = vnet_get_hw_interface (vnm, hw_if_index);
415 
416  sa_index = ipsec_get_sa_index_by_sa_id (sa_id);
417  if (sa_index == ~0)
418  {
419  clib_warning ("SA with ID %u not found", sa_id);
420  return VNET_API_ERROR_INVALID_VALUE;
421  }
422 
423  if (ipsec_is_sa_used (sa_index))
424  {
425  clib_warning ("SA with ID %u is already in use", sa_id);
426  return VNET_API_ERROR_INVALID_VALUE;
427  }
428 
429  sa = pool_elt_at_index (im->sad, sa_index);
430  if (sa->is_tunnel_ip6)
431  {
432  clib_warning ("IPsec interface not supported with IPv6 endpoints");
433  return VNET_API_ERROR_UNIMPLEMENTED;
434  }
435 
436  if (!is_outbound)
437  {
438  u64 key;
439 
440  old_sa_index = t->input_sa_index;
441  old_sa = pool_elt_at_index (im->sad, old_sa_index);
442 
443  /* unset old inbound hash entry. packets should stop arriving */
444  key =
445  (u64) old_sa->tunnel_src_addr.ip4.as_u32 << 32 | (u64) old_sa->spi;
446  p = hash_get (im->ipsec_if_pool_index_by_key, key);
447  if (p)
449 
450  /* set new inbound SA, then set new hash entry */
451  t->input_sa_index = sa_index;
452  key = (u64) sa->tunnel_src_addr.ip4.as_u32 << 32 | (u64) sa->spi;
454  }
455  else
456  {
457  old_sa_index = t->output_sa_index;
458  old_sa = pool_elt_at_index (im->sad, old_sa_index);
459  t->output_sa_index = sa_index;
460  }
461 
462  /* remove sa_id to sa_index mapping on old SA */
463  if (ipsec_get_sa_index_by_sa_id (old_sa->id) == old_sa_index)
464  hash_unset (im->sa_index_by_sa_id, old_sa->id);
465 
466  if (im->cb.add_del_sa_sess_cb)
467  {
468  clib_error_t *err;
469 
470  err = im->cb.add_del_sa_sess_cb (old_sa_index, 0);
471  if (err)
472  return VNET_API_ERROR_SYSCALL_ERROR_1;
473  }
474 
475  pool_put (im->sad, old_sa);
476 
477  return 0;
478 }
479 
480 
481 clib_error_t *
483 {
484  ipsec_main_t *im = &ipsec_main;
485 
486  im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
487 
488  return 0;
489 }
490 
492 
493 
494 /*
495  * fd.io coding-style-patch-verification: ON
496  *
497  * Local Variables:
498  * eval: (c-set-style "gnu")
499  * End:
500  */
vmrglw vmrglh hi
VNET_DEVICE_CLASS(ipsec_device_class, static)
#define hash_set(h, key, value)
Definition: hash.h:254
ipsec_tunnel_if_t * tunnel_interfaces
Definition: ipsec.h:263
ip46_address_t tunnel_src_addr
Definition: ipsec.h:130
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:554
#define hash_unset(h, key)
Definition: hash.h:260
a
Definition: bitmap.h:516
u32 id
Definition: ipsec.h:113
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
vlib_node_registration_t ipsec_if_output_node
(constructor) VLIB_REGISTER_NODE (ipsec_if_output_node)
Definition: ipsec_if_out.c:151
vnet_interface_main_t interface_main
Definition: vnet.h:56
#define NULL
Definition: clib.h:55
int ipsec_set_interface_sa(vnet_main_t *vnm, u32 hw_if_index, u32 sa_id, u8 is_outbound)
Definition: ipsec_if.c:403
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:121
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:520
int ipsec_add_del_tunnel_if(ipsec_add_del_tunnel_args_t *args)
Definition: ipsec_if.c:143
u8 is_tunnel
Definition: ipsec.h:128
static uword dummy_interface_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ipsec_if.c:35
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
u32 ipsec_get_sa_index_by_sa_id(u32 sa_id)
Definition: ipsec.c:31
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:390
void ip4_register_protocol(u32 protocol, u32 node_index)
Definition: ip4_forward.c:1508
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:227
uword * ipsec_if_pool_index_by_key
Definition: ipsec.h:281
u8 crypto_key[128]
Definition: ipsec.h:119
u32 spi
Definition: ipsec.h:114
clib_error_t * ipsec_tunnel_if_init(vlib_main_t *vm)
Definition: ipsec_if.c:482
u8 integ_key[128]
Definition: ipsec.h:123
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
ipsec_main_t ipsec_main
Definition: ipsec.c:28
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:718
u8 use_esn
Definition: ipsec.h:125
ip4_address_t remote_ip
Definition: ipsec.h:161
ipsec_main_callbacks_t cb
Definition: ipsec.h:296
unsigned long u64
Definition: types.h:89
int ipsec_set_interface_key(vnet_main_t *vnm, u32 hw_if_index, ipsec_if_set_key_type_t type, u8 alg, u8 *key)
Definition: ipsec_if.c:353
u32 vnet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u32 hw_class_index, u32 hw_instance)
Definition: interface.c:705
static clib_error_t * ipsec_admin_up_down_function(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: ipsec_if.c:43
#define hash_get(h, key)
Definition: hash.h:248
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:461
static void vlib_zero_combined_counter(vlib_combined_counter_main_t *cm, u32 index)
Clear a combined counter Clears the set of per-thread counters.
Definition: counter.h:276
u8 is_tunnel_ip6
Definition: ipsec.h:129
clib_error_t *(* check_support_cb)(ipsec_sa_t *sa)
Definition: ipsec.h:253
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:273
ip46_address_t tunnel_dst_addr
Definition: ipsec.h:131
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:188
ipsec_if_set_key_type_t
Definition: ipsec.h:185
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:164
u16 n_vectors
Definition: node.h:344
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
vlib_main_t * vm
Definition: buffer.c:294
void vl_api_rpc_call_main_thread(void *fp, u8 *data, u32 data_length)
Definition: vlib_api.c:644
vlib_node_registration_t ipsec_if_input_node
(constructor) VLIB_REGISTER_NODE (ipsec_if_input_node)
Definition: ipsec_if_in.c:202
#define clib_warning(format, args...)
Definition: error.h:59
#define clib_memcpy(a, b, c)
Definition: string.h:75
uword * sa_index_by_sa_id
Definition: ipsec.h:280
static void vnet_interface_counter_unlock(vnet_interface_main_t *im)
Definition: interface.h:743
u32 output_sa_index
Definition: ipsec.h:246
u8 * default_build_rewrite(vnet_main_t *vnm, u32 sw_if_index, vnet_link_t link_type, const void *dst_address)
Return a complete, zero-length (aka dummy) rewrite.
Definition: interface.c:1500
int ipsec_add_del_ipsec_gre_tunnel(vnet_main_t *vnm, ipsec_add_del_ipsec_gre_tunnel_args_t *args)
Definition: ipsec_if.c:291
static void vnet_interface_counter_lock(vnet_interface_main_t *im)
Definition: interface.h:735
#define hash_create(elts, value_bytes)
Definition: hash.h:681
u8 ipsec_is_sa_used(u32 sa_index)
Definition: ipsec.c:384
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:588
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
static int ipsec_add_del_tunnel_if_rpc_callback(ipsec_add_del_tunnel_args_t *a)
Definition: ipsec_if.c:134
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:169
ip4_address_t local_ip
Definition: ipsec.h:161
ipsec_sa_t * sad
Definition: ipsec.h:260
u8 integ_key_len
Definition: ipsec.h:122
int ipsec_add_del_tunnel_if_internal(vnet_main_t *vnm, ipsec_add_del_tunnel_args_t *args, u32 *sw_if_index)
Definition: ipsec_if.c:151
u32 input_sa_index
Definition: ipsec.h:245
u64 uword
Definition: types.h:112
u8 crypto_key_len
Definition: ipsec.h:118
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
VNET_HW_INTERFACE_CLASS(ipsec_hw_class)
u32 * free_tunnel_if_indices
Definition: ipsec.h:264
a point 2 point interface
Definition: interface.h:285
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:117
static u8 * format_ipsec_name(u8 *s, va_list *args)
Definition: ipsec_if.c:28
clib_error_t *(* add_del_sa_sess_cb)(u32 sa_index, u8 is_add)
Definition: ipsec.h:252
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: interface.c:562
u32 flags
Definition: vhost-user.h:77
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
u8 use_anti_replay
Definition: ipsec.h:126
u8 * format_ipsec_if_output_trace(u8 *s, va_list *args)
Definition: ipsec_if_out.c:50
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128