FD.io VPP  v17.04.2-2-ga8f93f8
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,
91 };
92 /* *INDENT-ON* */
93 
94 static int
97 
98 static int
100 {
101  vnet_main_t *vnm = vnet_get_main ();
102  ASSERT (os_get_cpu_number () == 0);
103 
104  return ipsec_add_del_tunnel_if_internal (vnm, a);
105 }
106 
107 int
109 {
111  (u8 *) args, sizeof (*args));
112  return 0;
113 }
114 
115 int
118 {
120  ipsec_main_t *im = &ipsec_main;
122  u32 hw_if_index = ~0;
123  uword *p;
124  ipsec_sa_t *sa;
125 
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);
128 
129  if (args->is_add)
130  {
131  /* check if same src/dst pair exists */
132  if (p)
133  return VNET_API_ERROR_INVALID_VALUE;
134 
136  memset (t, 0, sizeof (*t));
137 
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;
144  sa->is_tunnel = 1;
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))
149  {
152  args->remote_integ_key_len);
153  }
154  sa->crypto_alg = args->crypto_alg;
155  if (args->remote_crypto_key_len <= sizeof (args->remote_crypto_key))
156  {
159  args->remote_crypto_key_len);
160  }
161 
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;
165 
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;
172  sa->is_tunnel = 1;
173  sa->seq = 1;
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))
178  {
181  args->local_integ_key_len);
182  }
183  sa->crypto_alg = args->crypto_alg;
184  if (args->local_crypto_key_len <= sizeof (args->local_crypto_key))
185  {
188  args->local_crypto_key_len);
189  }
190 
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;
194 
196  t - im->tunnel_interfaces);
197 
198  if (vec_len (im->free_tunnel_if_indices) > 0)
199  {
200  hw_if_index =
202  1];
203  _vec_len (im->free_tunnel_if_indices) -= 1;
204  }
205  else
206  {
207  hw_if_index =
208  vnet_register_interface (vnm, ipsec_device_class.index,
209  t - im->tunnel_interfaces,
210  ipsec_hw_class.index,
211  t - im->tunnel_interfaces);
212 
213  hi = vnet_get_hw_interface (vnm, hw_if_index);
215  }
216  t->hw_if_index = hw_if_index;
217 
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);
222 
223  return hw_if_index;
224  }
225  else
226  {
227  /* check if exists */
228  if (!p)
229  return VNET_API_ERROR_INVALID_VALUE;
230 
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 */
235 
236  /* delete input and output SA */
237  sa = pool_elt_at_index (im->sad, t->input_sa_index);
238 
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;
242 
243  pool_put (im->sad, sa);
244 
245  sa = pool_elt_at_index (im->sad, t->output_sa_index);
246 
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;
250 
251  pool_put (im->sad, sa);
252 
254  pool_put (im->tunnel_interfaces, t);
255  }
256  return 0;
257 }
258 
259 int
262 {
263  ipsec_tunnel_if_t *t = 0;
264  ipsec_main_t *im = &ipsec_main;
265  uword *p;
266  ipsec_sa_t *sa;
267  u64 key;
268  u32 isa, osa;
269 
270  p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
271  if (!p)
272  return VNET_API_ERROR_INVALID_VALUE;
273  isa = p[0];
274 
275  p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
276  if (!p)
277  return VNET_API_ERROR_INVALID_VALUE;
278  osa = p[0];
279  sa = pool_elt_at_index (im->sad, p[0]);
280 
281  if (sa->is_tunnel)
282  key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi;
283  else
284  key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi;
285 
286  p = hash_get (im->ipsec_if_pool_index_by_key, key);
287 
288  if (args->is_add)
289  {
290  /* check if same src/dst pair exists */
291  if (p)
292  return VNET_API_ERROR_INVALID_VALUE;
293 
295  memset (t, 0, sizeof (*t));
296 
297  t->input_sa_index = isa;
298  t->output_sa_index = osa;
299  t->hw_if_index = ~0;
301  t - im->tunnel_interfaces);
302 
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);
307  }
308  else
309  {
310  /* check if exists */
311  if (!p)
312  return VNET_API_ERROR_INVALID_VALUE;
313 
314  t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
316  pool_put (im->tunnel_interfaces, t);
317  }
318  return 0;
319 }
320 
321 int
323  ipsec_if_set_key_type_t type, u8 alg, u8 * key)
324 {
325  ipsec_main_t *im = &ipsec_main;
328  ipsec_sa_t *sa;
329 
330  hi = vnet_get_hw_interface (vnm, hw_if_index);
332 
334  {
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));
339 
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;
343  }
344  else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
345  {
346  sa = pool_elt_at_index (im->sad, t->output_sa_index);
347  sa->integ_alg = alg;
348  sa->integ_key_len = vec_len (key);
349  clib_memcpy (sa->integ_key, key, vec_len (key));
350 
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;
354  }
355  else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
356  {
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));
361 
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;
365  }
366  else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
367  {
368  sa = pool_elt_at_index (im->sad, t->input_sa_index);
369  sa->integ_alg = alg;
370  sa->integ_key_len = vec_len (key);
371  clib_memcpy (sa->integ_key, key, vec_len (key));
372 
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;
376  }
377  else
378  return VNET_API_ERROR_INVALID_VALUE;
379 
380  return 0;
381 }
382 
383 
384 clib_error_t *
386 {
387  ipsec_main_t *im = &ipsec_main;
388 
389  im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
390 
391  return 0;
392 }
393 
395 
396 
397 /*
398  * fd.io coding-style-patch-verification: ON
399  *
400  * Local Variables:
401  * eval: (c-set-style "gnu")
402  * End:
403  */
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:530
#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:122
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:108
static int ipsec_add_del_tunnel_if_internal(vnet_main_t *vnm, ipsec_add_del_tunnel_args_t *args)
Definition: ipsec_if.c:116
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:418
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:379
void ip4_register_protocol(u32 protocol, u32 node_index)
Definition: ip4_forward.c:1932
#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:385
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
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:322
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:681
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
clib_error_t *(* check_support_cb)(ipsec_sa_t *sa)
Definition: ipsec.h:240
uword os_get_cpu_number(void)
Definition: unix-misc.c:224
#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:344
vlib_main_t * vm
Definition: buffer.c:276
void vl_api_rpc_call_main_thread(void *fp, u8 *data, u32 data_length)
Definition: memory_vlib.c:1327
vlib_node_registration_t ipsec_if_input_node
(constructor) VLIB_REGISTER_NODE (ipsec_if_input_node)
Definition: ipsec_if_in.c:136
#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
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:1386
int ipsec_add_del_ipsec_gre_tunnel(vnet_main_t *vnm, ipsec_add_del_ipsec_gre_tunnel_args_t *args)
Definition: ipsec_if.c:260
#define hash_create(elts, value_bytes)
Definition: hash.h:658
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:536
#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:99
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
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
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:538
u32 flags
Definition: vhost-user.h:78
#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