FD.io VPP  v17.07.01-10-g3be13f0
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  {
56  sa = pool_elt_at_index (im->sad, t->input_sa_index);
57  err = im->cb.check_support_cb (sa);
58  if (err)
59  return err;
60 
61  sa = pool_elt_at_index (im->sad, t->output_sa_index);
62  err = im->cb.check_support_cb (sa);
63  if (err)
64  return err;
65 
66  vnet_hw_interface_set_flags (vnm, hw_if_index,
68  }
69  else
70  vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
71 
72  return /* no error */ 0;
73 }
74 
75 /* *INDENT-OFF* */
76 VNET_DEVICE_CLASS (ipsec_device_class, static) =
77 {
78  .name = "IPSec",
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,
83 };
84 /* *INDENT-ON* */
85 
86 /* *INDENT-OFF* */
87 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
88 {
89  .name = "IPSec",
90  .build_rewrite = default_build_rewrite,
92 };
93 /* *INDENT-ON* */
94 
95 static int
97 {
98  vnet_main_t *vnm = vnet_get_main ();
99  ASSERT (vlib_get_thread_index () == 0);
100 
101  return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
102 }
103 
104 int
106 {
108  (u8 *) args, sizeof (*args));
109  return 0;
110 }
111 
112 int
115  u32 * sw_if_index)
116 {
118  ipsec_main_t *im = &ipsec_main;
120  u32 hw_if_index = ~0;
121  uword *p;
122  ipsec_sa_t *sa;
123 
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);
126 
127  if (args->is_add)
128  {
129  /* check if same src/dst pair exists */
130  if (p)
131  return VNET_API_ERROR_INVALID_VALUE;
132 
134  memset (t, 0, sizeof (*t));
135 
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;
142  sa->is_tunnel = 1;
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))
147  {
150  args->remote_integ_key_len);
151  }
152  sa->crypto_alg = args->crypto_alg;
153  if (args->remote_crypto_key_len <= sizeof (args->remote_crypto_key))
154  {
157  args->remote_crypto_key_len);
158  }
159 
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;
163 
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;
170  sa->is_tunnel = 1;
171  sa->seq = 1;
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))
176  {
179  args->local_integ_key_len);
180  }
181  sa->crypto_alg = args->crypto_alg;
182  if (args->local_crypto_key_len <= sizeof (args->local_crypto_key))
183  {
186  args->local_crypto_key_len);
187  }
188 
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;
192 
194  t - im->tunnel_interfaces);
195 
196  if (vec_len (im->free_tunnel_if_indices) > 0)
197  {
198  hw_if_index =
200  1];
201  _vec_len (im->free_tunnel_if_indices) -= 1;
202  }
203  else
204  {
205  hw_if_index =
206  vnet_register_interface (vnm, ipsec_device_class.index,
207  t - im->tunnel_interfaces,
208  ipsec_hw_class.index,
209  t - im->tunnel_interfaces);
210  }
211 
212  hi = vnet_get_hw_interface (vnm, hw_if_index);
214  t->hw_if_index = hw_if_index;
215 
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);
220 
221  }
222  else
223  {
225 
226  /* check if exists */
227  if (!p)
228  return VNET_API_ERROR_INVALID_VALUE;
229 
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 */
234 
241 
242  /* delete input and output SA */
243  sa = pool_elt_at_index (im->sad, t->input_sa_index);
244 
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;
248 
249  pool_put (im->sad, sa);
250 
251  sa = pool_elt_at_index (im->sad, t->output_sa_index);
252 
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;
256 
257  pool_put (im->sad, sa);
258 
260  pool_put (im->tunnel_interfaces, t);
261  }
262 
263  if (sw_if_index)
264  *sw_if_index = hi->sw_if_index;
265 
266  return 0;
267 }
268 
269 int
272 {
273  ipsec_tunnel_if_t *t = 0;
274  ipsec_main_t *im = &ipsec_main;
275  uword *p;
276  ipsec_sa_t *sa;
277  u64 key;
278  u32 isa, osa;
279 
280  p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
281  if (!p)
282  return VNET_API_ERROR_INVALID_VALUE;
283  isa = p[0];
284 
285  p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
286  if (!p)
287  return VNET_API_ERROR_INVALID_VALUE;
288  osa = p[0];
289  sa = pool_elt_at_index (im->sad, p[0]);
290 
291  if (sa->is_tunnel)
292  key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi;
293  else
294  key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi;
295 
296  p = hash_get (im->ipsec_if_pool_index_by_key, key);
297 
298  if (args->is_add)
299  {
300  /* check if same src/dst pair exists */
301  if (p)
302  return VNET_API_ERROR_INVALID_VALUE;
303 
305  memset (t, 0, sizeof (*t));
306 
307  t->input_sa_index = isa;
308  t->output_sa_index = osa;
309  t->hw_if_index = ~0;
311  t - im->tunnel_interfaces);
312 
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);
317  }
318  else
319  {
320  /* check if exists */
321  if (!p)
322  return VNET_API_ERROR_INVALID_VALUE;
323 
324  t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
326  pool_put (im->tunnel_interfaces, t);
327  }
328  return 0;
329 }
330 
331 int
333  ipsec_if_set_key_type_t type, u8 alg, u8 * key)
334 {
335  ipsec_main_t *im = &ipsec_main;
338  ipsec_sa_t *sa;
339 
340  hi = vnet_get_hw_interface (vnm, hw_if_index);
342 
344  {
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));
349 
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;
353  }
354  else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
355  {
356  sa = pool_elt_at_index (im->sad, t->output_sa_index);
357  sa->integ_alg = alg;
358  sa->integ_key_len = vec_len (key);
359  clib_memcpy (sa->integ_key, key, vec_len (key));
360 
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;
364  }
365  else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
366  {
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));
371 
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;
375  }
376  else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
377  {
378  sa = pool_elt_at_index (im->sad, t->input_sa_index);
379  sa->integ_alg = alg;
380  sa->integ_key_len = vec_len (key);
381  clib_memcpy (sa->integ_key, key, vec_len (key));
382 
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;
386  }
387  else
388  return VNET_API_ERROR_INVALID_VALUE;
389 
390  return 0;
391 }
392 
393 
394 clib_error_t *
396 {
397  ipsec_main_t *im = &ipsec_main;
398 
399  im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
400 
401  return 0;
402 }
403 
405 
406 
407 /*
408  * fd.io coding-style-patch-verification: ON
409  *
410  * Local Variables:
411  * eval: (c-set-style "gnu")
412  * End:
413  */
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:250
ip46_address_t tunnel_src_addr
Definition: ipsec.h:119
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:537
#define hash_unset(h, key)
Definition: hash.h:260
a
Definition: bitmap.h:516
i32(* add_del_sa_sess_cb)(u32 sa_index, u8 is_add)
Definition: ipsec.h:239
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
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
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:110
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:522
int ipsec_add_del_tunnel_if(ipsec_add_del_tunnel_args_t *args)
Definition: ipsec_if.c:105
u8 is_tunnel
Definition: ipsec.h:117
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
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:397
void ip4_register_protocol(u32 protocol, u32 node_index)
Definition: ip4_forward.c:1959
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
uword * ipsec_if_pool_index_by_key
Definition: ipsec.h:268
u8 crypto_key[128]
Definition: ipsec.h:108
u32 spi
Definition: ipsec.h:103
clib_error_t * ipsec_tunnel_if_init(vlib_main_t *vm)
Definition: ipsec_if.c:395
u8 integ_key[128]
Definition: ipsec.h:112
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
ipsec_main_t ipsec_main
Definition: ipsec.h:282
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:653
u8 use_esn
Definition: ipsec.h:114
ip4_address_t remote_ip
Definition: ipsec.h:150
ipsec_main_callbacks_t cb
Definition: ipsec.h:279
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:332
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:688
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:397
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
clib_error_t *(* check_support_cb)(ipsec_sa_t *sa)
Definition: ipsec.h:240
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:241
ip46_address_t tunnel_dst_addr
Definition: ipsec.h:120
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:169
ipsec_if_set_key_type_t
Definition: ipsec.h:174
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:153
u16 n_vectors
Definition: node.h:345
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:185
void vl_api_rpc_call_main_thread(void *fp, u8 *data, u32 data_length)
Definition: memory_vlib.c:1328
vlib_node_registration_t ipsec_if_input_node
(constructor) VLIB_REGISTER_NODE (ipsec_if_input_node)
Definition: ipsec_if_in.c:199
#define clib_warning(format, args...)
Definition: error.h:59
#define clib_memcpy(a, b, c)
Definition: string.h:69
uword * sa_index_by_sa_id
Definition: ipsec.h:267
static void vnet_interface_counter_unlock(vnet_interface_main_t *im)
Definition: interface.h:678
u32 output_sa_index
Definition: ipsec.h:233
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:1404
int ipsec_add_del_ipsec_gre_tunnel(vnet_main_t *vnm, ipsec_add_del_ipsec_gre_tunnel_args_t *args)
Definition: ipsec_if.c:270
static void vnet_interface_counter_lock(vnet_interface_main_t *im)
Definition: interface.h:670
#define hash_create(elts, value_bytes)
Definition: hash.h:658
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:560
#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:96
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:158
ip4_address_t local_ip
Definition: ipsec.h:150
ipsec_sa_t * sad
Definition: ipsec.h:247
u8 integ_key_len
Definition: ipsec.h:111
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:113
u32 input_sa_index
Definition: ipsec.h:232
u32 seq
Definition: ipsec.h:125
u64 uword
Definition: types.h:112
u8 crypto_key_len
Definition: ipsec.h:107
#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:251
a point 2 point interface
Definition: interface.h:292
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:106
static u8 * format_ipsec_name(u8 *s, va_list *args)
Definition: ipsec_if.c:28
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: interface.c:545
u32 flags
Definition: vhost-user.h:76
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u8 use_anti_replay
Definition: ipsec.h:115
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:109