FD.io VPP  v16.06
Vector Packet Processing
ipsec.c
Go to the documentation of this file.
1 /*
2  * decap.c : IPSec tunnel 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 #include <vnet/interface.h>
22 
23 #include <vnet/ipsec/ipsec.h>
24 #include <vnet/ipsec/esp.h>
25 #include <vnet/ipsec/ikev2.h>
26 
27 int
28 ipsec_set_interface_spd(vlib_main_t * vm, u32 sw_if_index, u32 spd_id, int is_add)
29 {
30  ipsec_main_t *im = &ipsec_main;
31  ip_lookup_main_t * lm;
32  ip_config_main_t * rx_cm;
33  ip4_ipsec_config_t config;
34 
35  u32 spd_index, ci;
36  uword *p;
37 
38  p = hash_get (im->spd_index_by_spd_id, spd_id);
39  if (!p)
40  return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such spd-id */
41 
42  spd_index = p[0];
43 
44  p = hash_get (im->spd_index_by_sw_if_index, sw_if_index);
45  if (p && is_add)
46  return VNET_API_ERROR_SYSCALL_ERROR_1; /* spd already assigned */
47 
48  if (is_add)
49  {
50  hash_set (im->spd_index_by_sw_if_index, sw_if_index, spd_index);
51  }
52  else
53  {
54  hash_unset (im->spd_index_by_sw_if_index, sw_if_index);
55  }
56 
57  clib_warning("sw_if_index %u spd_id %u spd_index %u",
58  sw_if_index, spd_id, spd_index);
59 
60  /* enable IPsec on TX */
61  vnet_interface_add_del_feature(im->vnet_main, vm, sw_if_index,
62  INTF_OUTPUT_FEAT_IPSEC, is_add);
63 
64  /* enable IPsec on RX */
65  config.spd_index = spd_index;
66 
67  /* IPv4 */
68  lm = &ip4_main.lookup_main;
69  rx_cm = &lm->rx_config_mains[VNET_UNICAST];
70 
71  ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
72 
74  (vm, &rx_cm->config_main,
75  ci,
77  &config,
78  sizeof (config));
79  rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
80 
81  /* IPv6 */
82  lm = &ip6_main.lookup_main;
83  rx_cm = &lm->rx_config_mains[VNET_UNICAST];
84 
85  ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
86 
88  (vm, &rx_cm->config_main,
89  ci,
91  &config,
92  sizeof (config));
93  rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
94 
95  return 0;
96 }
97 
98 int
99 ipsec_add_del_spd(vlib_main_t * vm, u32 spd_id, int is_add)
100 {
101  ipsec_main_t *im = &ipsec_main;
102  ipsec_spd_t * spd = 0;
103  uword *p;
104  u32 spd_index, k, v;
105 
106  p = hash_get (im->spd_index_by_spd_id, spd_id);
107  if (p && is_add)
108  return VNET_API_ERROR_INVALID_VALUE;
109  if (!p && !is_add)
110  return VNET_API_ERROR_INVALID_VALUE;
111 
112  if (!is_add) /* delete */
113  {
114  spd_index = p[0];
115  spd = pool_elt_at_index(im->spds, spd_index);
116  if (!spd)
117  return VNET_API_ERROR_INVALID_VALUE;
119  if (v == spd_index)
120  ipsec_set_interface_spd(vm, k, spd_id, 0);
121  }));
122  hash_unset (im->spd_index_by_spd_id, spd_id);
123  pool_free (spd->policies);
128  pool_put (im->spds, spd);
129  }
130  else /* create new SPD */
131  {
132  pool_get (im->spds, spd);
133  memset (spd, 0, sizeof (*spd));
134  spd_index = spd - im->spds;
135  spd->id = spd_id;
136  hash_set (im->spd_index_by_spd_id, spd_id, spd_index);
137  }
138  return 0;
139 }
140 
141 static int
142 ipsec_spd_entry_sort(void * a1, void * a2)
143 {
144  ipsec_main_t *im = &ipsec_main;
145  u32 * id1 = a1;
146  u32 * id2 = a2;
147  ipsec_spd_t * spd;
148  ipsec_policy_t * p1, * p2;
149 
150  pool_foreach (spd, im->spds, ({
151  p1 = pool_elt_at_index(spd->policies, *id1);
152  p2 = pool_elt_at_index(spd->policies, *id2);
153  if (p1 && p2)
154  return p2->priority - p1->priority;
155  }));
156 
157  return 0;
158 }
159 
160 int
162 {
163  ipsec_main_t *im = &ipsec_main;
164  ipsec_spd_t * spd = 0;
165  ipsec_policy_t * vp;
166  uword *p;
167  u32 spd_index;
168 
169  clib_warning("policy-id %u priority %d is_outbound %u",policy->id, policy->priority, policy->is_outbound);
170 
171  if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
172  {
173  p = hash_get(im->sa_index_by_sa_id, policy->sa_id);
174  if (!p)
175  return VNET_API_ERROR_SYSCALL_ERROR_1;
176  policy->sa_index = p[0];
177  }
178 
179  p = hash_get (im->spd_index_by_spd_id, policy->id);
180 
181  if (!p)
182  return VNET_API_ERROR_SYSCALL_ERROR_1;
183 
184  spd_index = p[0];
185  spd = pool_elt_at_index(im->spds, spd_index);
186  if (!spd)
187  return VNET_API_ERROR_SYSCALL_ERROR_1;
188 
189  if (is_add)
190  {
191  u32 policy_index;
192 
193  pool_get (spd->policies, vp);
194  clib_memcpy (vp, policy, sizeof (*vp));
195  policy_index = vp - spd->policies;
196 
197  if (policy->is_outbound)
198  {
199  if (policy->is_ipv6)
200  {
201  vec_add1 (spd->ipv6_outbound_policies, policy_index);
202  clib_memcpy(vp, policy, sizeof(ipsec_policy_t));
205  }
206  else
207  {
208  vec_add1 (spd->ipv4_outbound_policies, policy_index);
209  clib_memcpy(vp, policy, sizeof(ipsec_policy_t));
212  }
213  }
214  else
215  {
216  if (policy->is_ipv6)
217  {
218  if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
219  {
221  policy_index);
222  clib_memcpy(vp, policy, sizeof(ipsec_policy_t));
226  }
227  else
228  {
230  policy_index);
231  clib_memcpy(vp, policy, sizeof(ipsec_policy_t));
235  }
236  }
237  else
238  {
239  if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
240  {
242  policy_index);
243  clib_memcpy(vp, policy, sizeof(ipsec_policy_t));
247  }
248  else
249  {
251  policy_index);
252  clib_memcpy(vp, policy, sizeof(ipsec_policy_t));
256  }
257  }
258  }
259 
260  }
261  else
262  {
263  u32 i, j;
264  pool_foreach_index(i, spd->policies, ({
265  vp = pool_elt_at_index(spd->policies, i);
266  if (vp->priority != policy->priority)
267  continue;
268  if (vp->is_outbound != policy->is_outbound)
269  continue;
270  if (vp->policy != policy->policy)
271  continue;
272  if (vp->sa_id != policy->sa_id)
273  continue;
274  if (vp->protocol != policy->protocol)
275  continue;
276  if (vp->lport.start != policy->lport.start)
277  continue;
278  if (vp->lport.stop != policy->lport.stop)
279  continue;
280  if (vp->rport.start != policy->rport.start)
281  continue;
282  if (vp->rport.stop != policy->rport.stop)
283  continue;
284  if (vp->is_ipv6 != policy->is_ipv6)
285  continue;
286  if (policy->is_ipv6)
287  {
288  if (vp->laddr.start.ip6.as_u64[0] != policy->laddr.start.ip6.as_u64[0])
289  continue;
290  if (vp->laddr.start.ip6.as_u64[1] != policy->laddr.start.ip6.as_u64[1])
291  continue;
292  if (vp->laddr.stop.ip6.as_u64[0] != policy->laddr.stop.ip6.as_u64[0])
293  continue;
294  if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
295  continue;
296  if (vp->raddr.start.ip6.as_u64[0] != policy->raddr.start.ip6.as_u64[0])
297  continue;
298  if (vp->raddr.start.ip6.as_u64[1] != policy->raddr.start.ip6.as_u64[1])
299  continue;
300  if (vp->raddr.stop.ip6.as_u64[0] != policy->raddr.stop.ip6.as_u64[0])
301  continue;
302  if (vp->laddr.stop.ip6.as_u64[1] != policy->laddr.stop.ip6.as_u64[1])
303  continue;
304  if (policy->is_outbound)
305  {
306  vec_foreach_index(j, spd->ipv6_outbound_policies) {
307  if (vec_elt(spd->ipv6_outbound_policies, j) == i) {
308  vec_del1 (spd->ipv6_outbound_policies, j);
309  break;
310  }
311  }
312  }
313  else
314  {
315  if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
316  {
317  vec_foreach_index(j, spd->ipv6_inbound_protect_policy_indices) {
318  if (vec_elt(spd->ipv6_inbound_protect_policy_indices, j) == i) {
319  vec_del1 (spd->ipv6_inbound_protect_policy_indices, j);
320  break;
321  }
322  }
323  }
324  else
325  {
326  vec_foreach_index(j, spd->ipv6_inbound_policy_discard_and_bypass_indices) {
327  if (vec_elt(spd->ipv6_inbound_policy_discard_and_bypass_indices, j) == i) {
328  vec_del1 (spd->ipv6_inbound_policy_discard_and_bypass_indices, j);
329  break;
330  }
331  }
332  }
333  }
334  }
335  else
336  {
337  if (vp->laddr.start.ip4.as_u32 != policy->laddr.start.ip4.as_u32)
338  continue;
339  if (vp->laddr.stop.ip4.as_u32 != policy->laddr.stop.ip4.as_u32)
340  continue;
341  if (vp->raddr.start.ip4.as_u32 != policy->raddr.start.ip4.as_u32)
342  continue;
343  if (vp->raddr.stop.ip4.as_u32 != policy->raddr.stop.ip4.as_u32)
344  continue;
345  if (policy->is_outbound)
346  {
347  vec_foreach_index(j, spd->ipv4_outbound_policies) {
348  if (vec_elt(spd->ipv4_outbound_policies, j) == i) {
349  vec_del1 (spd->ipv4_outbound_policies, j);
350  break;
351  }
352  }
353  }
354  else
355  {
356  if (policy->policy == IPSEC_POLICY_ACTION_PROTECT)
357  {
358  vec_foreach_index(j, spd->ipv4_inbound_protect_policy_indices) {
359  if (vec_elt(spd->ipv4_inbound_protect_policy_indices, j) == i) {
360  vec_del1 (spd->ipv4_inbound_protect_policy_indices, j);
361  break;
362  }
363  }
364  }
365  else
366  {
367  vec_foreach_index(j, spd->ipv4_inbound_policy_discard_and_bypass_indices) {
368  if (vec_elt(spd->ipv4_inbound_policy_discard_and_bypass_indices, j) == i) {
369  vec_del1 (spd->ipv4_inbound_policy_discard_and_bypass_indices, j);
370  break;
371  }
372  }
373  }
374  }
375  pool_put (spd->policies, vp);
376  break;
377  }
378  }));
379  }
380 
381  return 0;
382 }
383 
384 static u8
386 {
387  ipsec_main_t * im = &ipsec_main;
388  ipsec_spd_t * spd;
389  ipsec_policy_t * p;
390 
391  pool_foreach(spd, im->spds, ({
392  pool_foreach(p, spd->policies, ({
393  if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
394  {
395  if (p->sa_index == sa_index)
396  return 1;
397  }
398  }));
399  }));
400 
401  return 0;
402 }
403 
404 int
405 ipsec_add_del_sa(vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add)
406 {
407  ipsec_main_t *im = &ipsec_main;
408  ipsec_sa_t * sa = 0;
409  uword *p;
410  u32 sa_index;
411 
412  clib_warning("id %u spi %u", new_sa->id, new_sa->spi);
413 
414  p = hash_get (im->sa_index_by_sa_id, new_sa->id);
415  if (p && is_add)
416  return VNET_API_ERROR_SYSCALL_ERROR_1; /* already exists */
417  if (!p && !is_add)
418  return VNET_API_ERROR_SYSCALL_ERROR_1;
419 
420  if (!is_add) /* delete */
421  {
422  sa_index = p[0];
423  sa = pool_elt_at_index(im->sad, sa_index);
424  if (ipsec_is_sa_used(sa_index))
425  {
426  clib_warning("sa_id %u used in policy", sa->id);
427  return VNET_API_ERROR_SYSCALL_ERROR_1; /* sa used in policy */
428  }
429  hash_unset (im->sa_index_by_sa_id, sa->id);
430  pool_put (im->sad, sa);
431  }
432  else /* create new SA */
433  {
434  pool_get (im->sad, sa);
435  clib_memcpy (sa, new_sa, sizeof (*sa));
436  sa_index = sa - im->sad;
437  hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
438  }
439  return 0;
440 }
441 
442 int
444 {
445  ipsec_main_t *im = &ipsec_main;
446  uword *p;
447  u32 sa_index;
448  ipsec_sa_t * sa = 0;
449 
450  p = hash_get (im->sa_index_by_sa_id, sa_update->id);
451  if (!p)
452  return VNET_API_ERROR_SYSCALL_ERROR_1; /* no such sa-id */
453 
454  sa_index = p[0];
455  sa = pool_elt_at_index(im->sad, sa_index);
456 
457  /* new crypto key */
458  if (0 < sa_update->crypto_key_len)
459  {
460  clib_memcpy(sa->crypto_key, sa_update->crypto_key, sa_update->crypto_key_len);
461  sa->crypto_key_len = sa_update->crypto_key_len;
462  }
463 
464  /* new integ key */
465  if (0 < sa_update->integ_key_len)
466  {
467  clib_memcpy(sa->integ_key, sa_update->integ_key, sa_update->integ_key_len);
468  sa->integ_key_len = sa_update->integ_key_len;
469  }
470 
471  return 0;
472 }
473 
474 static void
476 {
477  struct {
478  time_t time;
479  pid_t pid;
480  void * p;
481  } seed_data;
482 
483  seed_data.time = time(NULL);
484  seed_data.pid = getpid();
485  seed_data.p = (void *)&seed_data;
486 
487  RAND_seed((const void *)&seed_data, sizeof(seed_data));
488 }
489 
490 static clib_error_t *
492 {
493  clib_error_t * error;
494  ipsec_main_t * im = &ipsec_main;
496  vlib_node_t * node;
497 
498  ipsec_rand_seed();
499 
500  memset (im, 0, sizeof (im[0]));
501 
502  im->vnet_main = vnet_get_main();
503  im->vlib_main = vm;
504 
505  im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
506  im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
507  im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
508 
510 
511  node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
512  ASSERT(node);
513  im->error_drop_node_index = node->index;
514 
515  node = vlib_get_node_by_name (vm, (u8 *) "esp-encrypt");
516  ASSERT(node);
517  im->esp_encrypt_node_index = node->index;
518 
519  node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
520  ASSERT(node);
521  im->ip4_lookup_node_index = node->index;
522 
523 
524  if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
525  return error;
526 
527  if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
528  return error;
529 
530  esp_init();
531 
532  if ((error = ikev2_init (vm)))
533  return error;
534 
535  return 0;
536 }
537 
u32 * ipv6_inbound_protect_policy_indices
Definition: ipsec.h:151
ipsec_spd_t * spds
Definition: ipsec.h:171
#define hash_set(h, key, value)
Definition: hash.h:237
always_inline vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
u32 * ipv4_inbound_protect_policy_indices
Definition: ipsec.h:149
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
u32 * config_index_by_sw_if_index
Definition: lookup.h:345
#define hash_unset(h, key)
Definition: hash.h:243
u32 id
Definition: ipsec.h:67
i32 priority
Definition: ipsec.h:122
int vnet_interface_add_del_feature(vnet_main_t *vnm, vlib_main_t *vm, u32 sw_if_index, intf_output_feat_t feature, int is_add)
Definition: interface.c:1056
#define NULL
Definition: clib.h:55
u32 index
Definition: node.h:203
u32 vnet_config_del_feature(vlib_main_t *vm, vnet_config_main_t *cm, u32 config_string_heap_index, u32 feature_index, void *feature_config, u32 n_feature_config_bytes)
Definition: config.c:284
ip_config_main_t rx_config_mains[VNET_N_CAST]
Definition: lookup.h:401
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
ipsec_main_t ipsec_main
Definition: ipsec.h:202
u32 * ipv4_outbound_policies
Definition: ipsec.h:147
ip_lookup_main_t lookup_main
Definition: ip4.h:129
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:405
#define pool_get(P, E)
Definition: pool.h:186
u8 crypto_key[128]
Definition: ipsec.h:73
u32 spi
Definition: ipsec.h:68
uword * spd_index_by_sw_if_index
Definition: ipsec.h:191
clib_error_t * ipsec_tunnel_if_init(vlib_main_t *vm)
Definition: ipsec_if.c:189
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
u8 integ_key[128]
Definition: ipsec.h:77
#define pool_foreach(VAR, POOL, BODY)
Definition: pool.h:328
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
#define hash_foreach(key_var, value_var, h, body)
Definition: hash.h:380
int ipsec_add_del_policy(vlib_main_t *vm, ipsec_policy_t *policy, int is_add)
Definition: ipsec.c:161
#define clib_warning(format, args...)
Definition: error.h:59
#define vlib_call_init_function(vm, x)
Definition: init.h:159
ipsec_policy_t * policies
Definition: ipsec.h:145
u32 ip4_lookup_node_index
Definition: ipsec.h:197
u32 error_drop_node_index
Definition: ipsec.h:196
#define hash_get(h, key)
Definition: hash.h:231
#define pool_elt_at_index(p, i)
Definition: pool.h:346
vnet_main_t * vnet_main
Definition: ipsec.h:184
int ipsec_add_del_sa(vlib_main_t *vm, ipsec_sa_t *new_sa, int is_add)
Definition: ipsec.c:405
#define pool_put(P, E)
Definition: pool.h:200
uword * spd_index_by_spd_id
Definition: ipsec.h:190
#define pool_free(p)
Definition: pool.h:248
int ipsec_set_sa_key(vlib_main_t *vm, ipsec_sa_t *sa_update)
Definition: ipsec.c:443
u32 esp_encrypt_node_index
Definition: ipsec.h:198
clib_error_t * ipsec_cli_init(vlib_main_t *vm)
Definition: ipsec_cli.c:704
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
u32 sa_index
Definition: ipsec.h:136
#define clib_memcpy(a, b, c)
Definition: string.h:63
uword * sa_index_by_sa_id
Definition: ipsec.h:192
static void ipsec_rand_seed(void)
Definition: ipsec.c:475
int ipsec_add_del_spd(vlib_main_t *vm, u32 spd_id, int is_add)
Definition: ipsec.c:99
static clib_error_t * ipsec_init(vlib_main_t *vm)
Definition: ipsec.c:491
static u8 ipsec_is_sa_used(u32 sa_index)
Definition: ipsec.c:385
vlib_main_t * vlib_main
Definition: ipsec.h:183
#define hash_create(elts, value_bytes)
Definition: hash.h:615
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
ip6_main_t ip6_main
Definition: ip6_forward.c:2490
ip_lookup_main_t lookup_main
Definition: ip6.h:135
ipsec_sa_t * sad
Definition: ipsec.h:172
u8 integ_key_len
Definition: ipsec.h:76
u32 vnet_config_add_feature(vlib_main_t *vm, vnet_config_main_t *cm, u32 config_string_heap_index, u32 feature_index, void *feature_config, u32 n_feature_config_bytes)
Definition: config.c:233
u64 uword
Definition: types.h:112
u8 crypto_key_len
Definition: ipsec.h:72
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:44
static int ipsec_spd_entry_sort(void *a1, void *a2)
Definition: ipsec.c:142
u32 * ipv4_inbound_policy_discard_and_bypass_indices
Definition: ipsec.h:150
unsigned char u8
Definition: types.h:56
u8 is_outbound
Definition: ipsec.h:123
u32 * ipv6_inbound_policy_discard_and_bypass_indices
Definition: ipsec.h:152
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:898
always_inline void esp_init()
Definition: esp.h:74
u32 * ipv6_outbound_policies
Definition: ipsec.h:148
u32 id
Definition: ipsec.h:143
clib_error_t * ikev2_init(vlib_main_t *vm)
Definition: ikev2.c:2091
ip4_main_t ip4_main
Definition: ip4_forward.c:1394
#define pool_foreach_index(i, v, body)
Definition: pool.h:366
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 ** empty_buffers
Definition: ipsec.h:178
int ipsec_set_interface_spd(vlib_main_t *vm, u32 sw_if_index, u32 spd_id, int is_add)
Definition: ipsec.c:28
vnet_config_main_t config_main
Definition: lookup.h:343