FD.io VPP  v19.04.4-rc0-5-ge88582fac
Vector Packet Processing
ikev2.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <vlib/vlib.h>
17 #include <vlib/unix/plugin.h>
18 #include <vpp/app/version.h>
19 #include <vnet/vnet.h>
20 #include <vnet/pg/pg.h>
21 #include <vppinfra/error.h>
22 #include <vppinfra/random.h>
23 #include <vnet/udp/udp.h>
24 #include <vnet/ipsec/ipsec.h>
25 #include <plugins/ikev2/ikev2.h>
27 #include <openssl/sha.h>
28 
30 
32  ikev2_sa_t * sa,
33  ikev2_child_sa_t * child);
34 
35 #define ikev2_set_state(sa, v) do { \
36  (sa)->state = v; \
37  clib_warning("sa state changed to " #v); \
38  } while(0);
39 
40 typedef struct
41 {
45 
46 static u8 *
47 format_ikev2_trace (u8 * s, va_list * args)
48 {
49  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
50  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
51  ikev2_trace_t *t = va_arg (*args, ikev2_trace_t *);
52 
53  s = format (s, "ikev2: sw_if_index %d, next index %d",
54  t->sw_if_index, t->next_index);
55  return s;
56 }
57 
59 
60 #define foreach_ikev2_error \
61 _(PROCESSED, "IKEv2 packets processed") \
62 _(IKE_SA_INIT_RETRANSMIT, "IKE_SA_INIT retransmit ") \
63 _(IKE_SA_INIT_IGNORE, "IKE_SA_INIT ignore (IKE SA already auth)") \
64 _(IKE_REQ_RETRANSMIT, "IKE request retransmit") \
65 _(IKE_REQ_IGNORE, "IKE request ignore (old msgid)") \
66 _(NOT_IKEV2, "Non IKEv2 packets received")
67 
68 typedef enum
69 {
70 #define _(sym,str) IKEV2_ERROR_##sym,
72 #undef _
75 
76 static char *ikev2_error_strings[] = {
77 #define _(sym,string) string,
79 #undef _
80 };
81 
82 typedef enum
83 {
87 } ikev2_next_t;
88 
89 static ikev2_sa_transform_t *
91 {
92  ikev2_main_t *km = &ikev2_main;
94 
96  {
97  if (td->type != t->type)
98  continue;
99 
100  if (td->transform_id != t->transform_id)
101  continue;
102 
103  if (td->type == IKEV2_TRANSFORM_TYPE_ENCR)
104  {
105  if (vec_len (t->attrs) != 4 || t->attrs[0] != 0x80
106  || t->attrs[1] != 14)
107  continue;
108 
109  if (((t->attrs[2] << 8 | t->attrs[3]) / 8) != td->key_len)
110  continue;
111  }
112  return td;
113  }
114  return 0;
115 }
116 
117 static ikev2_sa_proposal_t *
119  ikev2_protocol_id_t prot_id)
120 {
121  ikev2_sa_proposal_t *rv = 0;
122  ikev2_sa_proposal_t *proposal;
123  ikev2_sa_transform_t *transform, *new_t;
124  u8 mandatory_bitmap, optional_bitmap;
125 
126  if (prot_id == IKEV2_PROTOCOL_IKE)
127  {
128  mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
129  (1 << IKEV2_TRANSFORM_TYPE_PRF) |
130  (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
131  optional_bitmap = mandatory_bitmap;
132  }
133  else if (prot_id == IKEV2_PROTOCOL_ESP)
134  {
135  mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
136  (1 << IKEV2_TRANSFORM_TYPE_ESN);
137  optional_bitmap = mandatory_bitmap |
138  (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
139  }
140  else if (prot_id == IKEV2_PROTOCOL_AH)
141  {
142  mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_INTEG) |
143  (1 << IKEV2_TRANSFORM_TYPE_ESN);
144  optional_bitmap = mandatory_bitmap | (1 << IKEV2_TRANSFORM_TYPE_DH);
145  }
146  else
147  return 0;
148 
149  vec_add2 (rv, proposal, 1);
150 
151  vec_foreach (proposal, proposals)
152  {
153  u8 bitmap = 0;
154  if (proposal->protocol_id != prot_id)
155  continue;
156 
157  vec_foreach (transform, proposal->transforms)
158  {
159  if ((1 << transform->type) & bitmap)
160  continue;
161 
162  if (ikev2_find_transform_data (transform))
163  {
164  bitmap |= 1 << transform->type;
165  vec_add2 (rv->transforms, new_t, 1);
166  clib_memcpy_fast (new_t, transform, sizeof (*new_t));
167  new_t->attrs = vec_dup (transform->attrs);
168  }
169  }
170 
171  clib_warning ("bitmap is %x mandatory is %x optional is %x",
172  bitmap, mandatory_bitmap, optional_bitmap);
173 
174  if ((bitmap & mandatory_bitmap) == mandatory_bitmap &&
175  (bitmap & ~optional_bitmap) == 0)
176  {
177  rv->proposal_num = proposal->proposal_num;
178  rv->protocol_id = proposal->protocol_id;
179  RAND_bytes ((u8 *) & rv->spi, sizeof (rv->spi));
180  goto done;
181  }
182  else
183  {
184  vec_free (rv->transforms);
185  }
186  }
187 
188  vec_free (rv);
189 done:
190  return rv;
191 }
192 
196 {
198 
199  if (!p)
200  return 0;
201 
202  vec_foreach (t, p->transforms)
203  {
204  if (t->type == type)
205  return ikev2_find_transform_data (t);
206  }
207  return 0;
208 }
209 
212  int by_initiator)
213 {
215  vec_foreach (c, sa->childs)
216  {
217  ikev2_sa_proposal_t *proposal =
218  by_initiator ? &c->i_proposals[0] : &c->r_proposals[0];
219  if (proposal && proposal->spi == spi && proposal->protocol_id == prot_id)
220  return c;
221  }
222 
223  return 0;
224 }
225 
226 void
228 {
231 
232  if (!*v)
233  return;
234 
235  vec_foreach (p, *v)
236  {
237  vec_foreach (t, p->transforms)
238  {
239  vec_free (t->attrs);
240  }
241  vec_free (p->transforms);
242  }
243  vec_free (*v);
244 };
245 
246 static void
248 {
250  vec_foreach (c, *childs)
251  {
254  vec_free (c->sk_ai);
255  vec_free (c->sk_ar);
256  vec_free (c->sk_ei);
257  vec_free (c->sk_er);
258  }
259 
260  vec_free (*childs);
261 }
262 
263 static void
265 {
268  vec_free (child->sk_ai);
269  vec_free (child->sk_ar);
270  vec_free (child->sk_ei);
271  vec_free (child->sk_er);
272 
273  vec_del1 (sa->childs, child - sa->childs);
274 }
275 
276 static void
278 {
279  vec_free (sa->i_nonce);
280  vec_free (sa->i_dh_data);
281  vec_free (sa->dh_shared_key);
282  vec_free (sa->dh_private_key);
283 
286 
287  vec_free (sa->sk_d);
288  vec_free (sa->sk_ai);
289  vec_free (sa->sk_ar);
290  vec_free (sa->sk_ei);
291  vec_free (sa->sk_er);
292  vec_free (sa->sk_pi);
293  vec_free (sa->sk_pr);
294 
295  vec_free (sa->i_id.data);
296  vec_free (sa->i_auth.data);
297  vec_free (sa->r_id.data);
298  vec_free (sa->r_auth.data);
299  if (sa->r_auth.key)
300  EVP_PKEY_free (sa->r_auth.key);
301 
302  vec_free (sa->del);
303 
305 }
306 
307 static void
309 {
310  ikev2_main_t *km = &ikev2_main;
311  u32 thread_index = vlib_get_thread_index ();
312  uword *p;
313 
315 
316  p = hash_get (km->per_thread_data[thread_index].sa_by_rspi, sa->rspi);
317  if (p)
318  {
319  hash_unset (km->per_thread_data[thread_index].sa_by_rspi, sa->rspi);
320  pool_put (km->per_thread_data[thread_index].sas, sa);
321  }
322 }
323 
324 static void
326 {
327  ikev2_sa_transform_t *t = 0, *t2;
328  ikev2_main_t *km = &ikev2_main;
329 
330  if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
331  {
332  return;
333  }
334 
335  /* check if received DH group is on our list of supported groups */
337  {
338  if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
339  {
340  t = t2;
341  break;
342  }
343  }
344 
345  if (!t)
346  {
347  clib_warning ("unknown dh data group %u (data len %u)", sa->dh_group,
348  vec_len (sa->i_dh_data));
349  sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
350  return;
351  }
352 
353  if (sa->is_initiator)
354  {
355  /* generate rspi */
356  RAND_bytes ((u8 *) & sa->ispi, 8);
357 
358  /* generate nonce */
360  RAND_bytes ((u8 *) sa->i_nonce, IKEV2_NONCE_SIZE);
361  }
362  else
363  {
364  /* generate rspi */
365  RAND_bytes ((u8 *) & sa->rspi, 8);
366 
367  /* generate nonce */
369  RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
370  }
371 
372  /* generate dh keys */
373  ikev2_generate_dh (sa, t);
374 
375 }
376 
377 static void
379 {
380  ikev2_sa_transform_t *t = 0, *t2;
381  ikev2_main_t *km = &ikev2_main;
382 
383 
384  /*move some data to the new SA */
385 #define _(A) ({void* __tmp__ = (A); (A) = 0; __tmp__;})
386  sa->i_nonce = _(sai->i_nonce);
387  sa->i_dh_data = _(sai->i_dh_data);
388  sa->dh_private_key = _(sai->dh_private_key);
389  sa->iaddr.as_u32 = sai->iaddr.as_u32;
390  sa->raddr.as_u32 = sai->raddr.as_u32;
391  sa->is_initiator = sai->is_initiator;
392  sa->profile = sai->profile;
393  sa->i_id.type = sai->i_id.type;
394  sa->i_id.data = _(sai->i_id.data);
395  sa->i_auth.method = sai->i_auth.method;
396  sa->i_auth.hex = sai->i_auth.hex;
397  sa->i_auth.data = _(sai->i_auth.data);
398  sa->i_auth.key = _(sai->i_auth.key);
400  sa->childs = _(sai->childs);
401 #undef _
402 
403 
404  if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
405  {
406  return;
407  }
408 
409  /* check if received DH group is on our list of supported groups */
411  {
412  if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
413  {
414  t = t2;
415  break;
416  }
417  }
418 
419  if (!t)
420  {
421  clib_warning ("unknown dh data group %u (data len %u)", sa->dh_group,
422  vec_len (sa->i_dh_data));
423  sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
424  return;
425  }
426 
427 
428  /* generate dh keys */
429  ikev2_complete_dh (sa, t);
430 
431 }
432 
433 static void
435 {
436  u8 *tmp;
437  /* calculate SKEYSEED = prf(Ni | Nr, g^ir) */
438  u8 *skeyseed = 0;
439  u8 *s = 0;
440  ikev2_sa_transform_t *tr_encr, *tr_prf, *tr_integ;
441  tr_encr =
442  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
443  tr_prf =
444  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
445  tr_integ =
446  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
447 
448  vec_append (s, sa->i_nonce);
449  vec_append (s, sa->r_nonce);
450  skeyseed = ikev2_calc_prf (tr_prf, s, sa->dh_shared_key);
451 
452  /* Calculate S = Ni | Nr | SPIi | SPIr */
453  u64 *spi;
454  vec_add2 (s, tmp, 2 * sizeof (*spi));
455  spi = (u64 *) tmp;
456  spi[0] = clib_host_to_net_u64 (sa->ispi);
457  spi[1] = clib_host_to_net_u64 (sa->rspi);
458 
459  /* calculate PRFplus */
460  u8 *keymat;
461  int len = tr_prf->key_trunc + /* SK_d */
462  tr_integ->key_len * 2 + /* SK_ai, SK_ar */
463  tr_encr->key_len * 2 + /* SK_ei, SK_er */
464  tr_prf->key_len * 2; /* SK_pi, SK_pr */
465 
466  keymat = ikev2_calc_prfplus (tr_prf, skeyseed, s, len);
467  vec_free (skeyseed);
468  vec_free (s);
469 
470  int pos = 0;
471 
472  /* SK_d */
473  sa->sk_d = vec_new (u8, tr_prf->key_trunc);
474  clib_memcpy_fast (sa->sk_d, keymat + pos, tr_prf->key_trunc);
475  pos += tr_prf->key_trunc;
476 
477  /* SK_ai */
478  sa->sk_ai = vec_new (u8, tr_integ->key_len);
479  clib_memcpy_fast (sa->sk_ai, keymat + pos, tr_integ->key_len);
480  pos += tr_integ->key_len;
481 
482  /* SK_ar */
483  sa->sk_ar = vec_new (u8, tr_integ->key_len);
484  clib_memcpy_fast (sa->sk_ar, keymat + pos, tr_integ->key_len);
485  pos += tr_integ->key_len;
486 
487  /* SK_ei */
488  sa->sk_ei = vec_new (u8, tr_encr->key_len);
489  clib_memcpy_fast (sa->sk_ei, keymat + pos, tr_encr->key_len);
490  pos += tr_encr->key_len;
491 
492  /* SK_er */
493  sa->sk_er = vec_new (u8, tr_encr->key_len);
494  clib_memcpy_fast (sa->sk_er, keymat + pos, tr_encr->key_len);
495  pos += tr_encr->key_len;
496 
497  /* SK_pi */
498  sa->sk_pi = vec_new (u8, tr_prf->key_len);
499  clib_memcpy_fast (sa->sk_pi, keymat + pos, tr_prf->key_len);
500  pos += tr_prf->key_len;
501 
502  /* SK_pr */
503  sa->sk_pr = vec_new (u8, tr_prf->key_len);
504  clib_memcpy_fast (sa->sk_pr, keymat + pos, tr_prf->key_len);
505  pos += tr_prf->key_len;
506 
507  vec_free (keymat);
508 }
509 
510 static void
512 {
513  u8 *s = 0;
514  ikev2_sa_transform_t *tr_prf, *ctr_encr, *ctr_integ;
515  tr_prf =
516  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
517  ctr_encr =
518  ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
519  ctr_integ =
520  ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
521 
522  vec_append (s, sa->i_nonce);
523  vec_append (s, sa->r_nonce);
524  /* calculate PRFplus */
525  u8 *keymat;
526  int len = ctr_encr->key_len * 2 + ctr_integ->key_len * 2;
527 
528  keymat = ikev2_calc_prfplus (tr_prf, sa->sk_d, s, len);
529 
530  int pos = 0;
531 
532  /* SK_ei */
533  child->sk_ei = vec_new (u8, ctr_encr->key_len);
534  clib_memcpy_fast (child->sk_ei, keymat + pos, ctr_encr->key_len);
535  pos += ctr_encr->key_len;
536 
537  /* SK_ai */
538  child->sk_ai = vec_new (u8, ctr_integ->key_len);
539  clib_memcpy_fast (child->sk_ai, keymat + pos, ctr_integ->key_len);
540  pos += ctr_integ->key_len;
541 
542  /* SK_er */
543  child->sk_er = vec_new (u8, ctr_encr->key_len);
544  clib_memcpy_fast (child->sk_er, keymat + pos, ctr_encr->key_len);
545  pos += ctr_encr->key_len;
546 
547  /* SK_ar */
548  child->sk_ar = vec_new (u8, ctr_integ->key_len);
549  clib_memcpy_fast (child->sk_ar, keymat + pos, ctr_integ->key_len);
550  pos += ctr_integ->key_len;
551 
552  ASSERT (pos == len);
553 
554  vec_free (keymat);
555 }
556 
557 static void
559  ike_header_t * ike)
560 {
561  int p = 0;
562  u32 len = clib_net_to_host_u32 (ike->length);
563  u8 payload = ike->nextpayload;
564 
565  clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
566  "exchange %x flags %x msgid %x length %u",
567  clib_net_to_host_u64 (ike->ispi),
568  clib_net_to_host_u64 (ike->rspi),
569  payload, ike->version,
570  ike->exchange, ike->flags,
571  clib_net_to_host_u32 (ike->msgid), len);
572 
573  sa->ispi = clib_net_to_host_u64 (ike->ispi);
574 
575  /* store whole IKE payload - needed for PSK auth */
577  vec_add (sa->last_sa_init_req_packet_data, ike, len);
578 
579  while (p < len && payload != IKEV2_PAYLOAD_NONE)
580  {
581  ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
582  u32 plen = clib_net_to_host_u16 (ikep->length);
583 
584  if (plen < sizeof (ike_payload_header_t))
585  return;
586 
587  if (payload == IKEV2_PAYLOAD_SA)
588  {
590  sa->i_proposals = ikev2_parse_sa_payload (ikep);
591  }
592  else if (payload == IKEV2_PAYLOAD_KE)
593  {
594  ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
595  sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
596  vec_free (sa->i_dh_data);
597  vec_add (sa->i_dh_data, ke->payload, plen - sizeof (*ke));
598  }
599  else if (payload == IKEV2_PAYLOAD_NONCE)
600  {
601  vec_free (sa->i_nonce);
602  vec_add (sa->i_nonce, ikep->payload, plen - sizeof (*ikep));
603  }
604  else if (payload == IKEV2_PAYLOAD_NOTIFY)
605  {
607  vec_free (n);
608  }
609  else if (payload == IKEV2_PAYLOAD_VENDOR)
610  {
612  }
613  else
614  {
615  clib_warning ("unknown payload %u flags %x length %u", payload,
616  ikep->flags, plen);
617  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
618  {
620  sa->unsupported_cp = payload;
621  return;
622  }
623  }
624 
625  payload = ikep->nextpayload;
626  p += plen;
627  }
628 
630 }
631 
632 static void
634  ike_header_t * ike)
635 {
636  int p = 0;
637  u32 len = clib_net_to_host_u32 (ike->length);
638  u8 payload = ike->nextpayload;
639 
640  clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
641  "exchange %x flags %x msgid %x length %u",
642  clib_net_to_host_u64 (ike->ispi),
643  clib_net_to_host_u64 (ike->rspi),
644  payload, ike->version,
645  ike->exchange, ike->flags,
646  clib_net_to_host_u32 (ike->msgid), len);
647 
648  sa->ispi = clib_net_to_host_u64 (ike->ispi);
649  sa->rspi = clib_net_to_host_u64 (ike->rspi);
650 
651  /* store whole IKE payload - needed for PSK auth */
653  vec_add (sa->last_sa_init_res_packet_data, ike, len);
654 
655  while (p < len && payload != IKEV2_PAYLOAD_NONE)
656  {
657  ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
658  u32 plen = clib_net_to_host_u16 (ikep->length);
659 
660  if (plen < sizeof (ike_payload_header_t))
661  return;
662 
663  if (payload == IKEV2_PAYLOAD_SA)
664  {
666  sa->r_proposals = ikev2_parse_sa_payload (ikep);
667  if (sa->r_proposals)
668  {
670  ike->msgid =
671  clib_host_to_net_u32 (clib_net_to_host_u32 (ike->msgid) + 1);
672  }
673  }
674  else if (payload == IKEV2_PAYLOAD_KE)
675  {
676  ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
677  sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
678  vec_free (sa->r_dh_data);
679  vec_add (sa->r_dh_data, ke->payload, plen - sizeof (*ke));
680  }
681  else if (payload == IKEV2_PAYLOAD_NONCE)
682  {
683  vec_free (sa->r_nonce);
684  vec_add (sa->r_nonce, ikep->payload, plen - sizeof (*ikep));
685  }
686  else if (payload == IKEV2_PAYLOAD_NOTIFY)
687  {
689  vec_free (n);
690  }
691  else if (payload == IKEV2_PAYLOAD_VENDOR)
692  {
694  }
695  else
696  {
697  clib_warning ("unknown payload %u flags %x length %u", payload,
698  ikep->flags, plen);
699  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
700  {
702  sa->unsupported_cp = payload;
703  return;
704  }
705  }
706 
707  payload = ikep->nextpayload;
708  p += plen;
709  }
710 }
711 
712 static u8 *
713 ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike, u8 * payload)
714 {
715  int p = 0;
716  u8 last_payload = 0;
717  u8 *hmac = 0;
718  u32 len = clib_net_to_host_u32 (ike->length);
719  ike_payload_header_t *ikep = 0;
720  u32 plen = 0;
721  ikev2_sa_transform_t *tr_integ;
722  tr_integ =
723  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
724 
725  while (p < len &&
726  *payload != IKEV2_PAYLOAD_NONE && last_payload != IKEV2_PAYLOAD_SK)
727  {
728  ikep = (ike_payload_header_t *) & ike->payload[p];
729  plen = clib_net_to_host_u16 (ikep->length);
730 
731  if (plen < sizeof (*ikep))
732  return 0;
733 
734  if (*payload == IKEV2_PAYLOAD_SK)
735  {
736  clib_warning ("received IKEv2 payload SK, len %u", plen - 4);
737  last_payload = *payload;
738  }
739  else
740  {
741  clib_warning ("unknown payload %u flags %x length %u", payload,
742  ikep->flags, plen);
743  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
744  {
745  sa->unsupported_cp = *payload;
746  return 0;
747  }
748  }
749 
750  *payload = ikep->nextpayload;
751  p += plen;
752  }
753 
754  if (last_payload != IKEV2_PAYLOAD_SK)
755  {
756  clib_warning ("Last payload must be SK");
757  return 0;
758  }
759 
760  hmac =
761  ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ar : sa->sk_ai,
762  (u8 *) ike, len - tr_integ->key_trunc);
763 
764  plen = plen - sizeof (*ikep) - tr_integ->key_trunc;
765 
766  if (memcmp (hmac, &ikep->payload[plen], tr_integ->key_trunc))
767  {
768  clib_warning ("message integrity check failed");
769  vec_free (hmac);
770  return 0;
771  }
772  vec_free (hmac);
773 
774  return ikev2_decrypt_data (sa, ikep->payload, plen);
775 }
776 
777 static void
779 {
780  ikev2_main_t *km = &ikev2_main;
781  ikev2_sa_t *tmp;
782  u32 i, *delete = 0;
784  u32 thread_index = vlib_get_thread_index ();
785 
786  if (!sa->initial_contact)
787  return;
788 
789  /* find old IKE SAs with the same authenticated identity */
790  /* *INDENT-OFF* */
791  pool_foreach (tmp, km->per_thread_data[thread_index].sas, ({
792  if (tmp->i_id.type != sa->i_id.type ||
793  vec_len(tmp->i_id.data) != vec_len(sa->i_id.data) ||
794  memcmp(sa->i_id.data, tmp->i_id.data, vec_len(sa->i_id.data)))
795  continue;
796 
797  if (sa->rspi != tmp->rspi)
798  vec_add1(delete, tmp - km->per_thread_data[thread_index].sas);
799  }));
800  /* *INDENT-ON* */
801 
802  for (i = 0; i < vec_len (delete); i++)
803  {
804  tmp =
805  pool_elt_at_index (km->per_thread_data[thread_index].sas, delete[i]);
806  vec_foreach (c,
808  tmp, c);
809  ikev2_delete_sa (tmp);
810  }
811 
812  vec_free (delete);
813  sa->initial_contact = 0;
814 }
815 
816 static void
817 ikev2_process_auth_req (vlib_main_t * vm, ikev2_sa_t * sa, ike_header_t * ike)
818 {
819  ikev2_child_sa_t *first_child_sa;
820  int p = 0;
821  u32 len = clib_net_to_host_u32 (ike->length);
822  u8 payload = ike->nextpayload;
823  u8 *plaintext = 0;
824 
825  ike_payload_header_t *ikep;
826  u32 plen;
827 
828  clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
829  "exchange %x flags %x msgid %x length %u",
830  clib_net_to_host_u64 (ike->ispi),
831  clib_net_to_host_u64 (ike->rspi),
832  payload, ike->version,
833  ike->exchange, ike->flags,
834  clib_net_to_host_u32 (ike->msgid), len);
835 
836  ikev2_calc_keys (sa);
837 
838  plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
839 
840  if (!plaintext)
841  {
842  if (sa->unsupported_cp)
844  goto cleanup_and_exit;
845  }
846 
847  /* select or create 1st child SA */
848  if (sa->is_initiator)
849  {
850  first_child_sa = &sa->childs[0];
851  }
852  else
853  {
855  vec_add2 (sa->childs, first_child_sa, 1);
856  }
857 
858 
859  /* process encrypted payload */
860  p = 0;
861  while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
862  {
863  ikep = (ike_payload_header_t *) & plaintext[p];
864  plen = clib_net_to_host_u16 (ikep->length);
865 
866  if (plen < sizeof (ike_payload_header_t))
867  goto cleanup_and_exit;
868 
869  if (payload == IKEV2_PAYLOAD_SA) /* 33 */
870  {
871  clib_warning ("received payload SA, len %u", plen - sizeof (*ikep));
872  if (sa->is_initiator)
873  {
874  ikev2_sa_free_proposal_vector (&first_child_sa->r_proposals);
875  first_child_sa->r_proposals = ikev2_parse_sa_payload (ikep);
876  }
877  else
878  {
879  ikev2_sa_free_proposal_vector (&first_child_sa->i_proposals);
880  first_child_sa->i_proposals = ikev2_parse_sa_payload (ikep);
881  }
882  }
883  else if (payload == IKEV2_PAYLOAD_IDI) /* 35 */
884  {
885  ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
886 
887  sa->i_id.type = id->id_type;
888  vec_free (sa->i_id.data);
889  vec_add (sa->i_id.data, id->payload, plen - sizeof (*id));
890 
891  clib_warning ("received payload IDi, len %u id_type %u",
892  plen - sizeof (*id), id->id_type);
893  }
894  else if (payload == IKEV2_PAYLOAD_IDR) /* 36 */
895  {
896  ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
897 
898  sa->r_id.type = id->id_type;
899  vec_free (sa->r_id.data);
900  vec_add (sa->r_id.data, id->payload, plen - sizeof (*id));
901 
902  clib_warning ("received payload IDr len %u id_type %u",
903  plen - sizeof (*id), id->id_type);
904  }
905  else if (payload == IKEV2_PAYLOAD_AUTH) /* 39 */
906  {
907  ike_auth_payload_header_t *a = (ike_auth_payload_header_t *) ikep;
908 
909  if (sa->is_initiator)
910  {
911  sa->r_auth.method = a->auth_method;
912  vec_free (sa->r_auth.data);
913  vec_add (sa->r_auth.data, a->payload, plen - sizeof (*a));
914  }
915  else
916  {
917  sa->i_auth.method = a->auth_method;
918  vec_free (sa->i_auth.data);
919  vec_add (sa->i_auth.data, a->payload, plen - sizeof (*a));
920  }
921 
922  clib_warning ("received payload AUTH, len %u auth_type %u",
923  plen - sizeof (*a), a->auth_method);
924  }
925  else if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
926  {
928  if (n->msg_type == IKEV2_NOTIFY_MSG_INITIAL_CONTACT)
929  {
930  sa->initial_contact = 1;
931  }
932  vec_free (n);
933  }
934  else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
935  {
937  }
938  else if (payload == IKEV2_PAYLOAD_TSI) /* 44 */
939  {
940  clib_warning ("received payload TSi, len %u",
941  plen - sizeof (*ikep));
942 
943  vec_free (first_child_sa->tsi);
944  first_child_sa->tsi = ikev2_parse_ts_payload (ikep);
945  }
946  else if (payload == IKEV2_PAYLOAD_TSR) /* 45 */
947  {
948  clib_warning ("received payload TSr, len %u",
949  plen - sizeof (*ikep));
950 
951  vec_free (first_child_sa->tsr);
952  first_child_sa->tsr = ikev2_parse_ts_payload (ikep);
953  }
954  else
955  {
956  clib_warning ("unknown payload %u flags %x length %u data %u",
957  payload, ikep->flags, plen - 4,
958  format_hex_bytes, ikep->payload, plen - 4);
959 
960  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
961  {
963  sa->unsupported_cp = payload;
964  return;
965  }
966  }
967 
968  payload = ikep->nextpayload;
969  p += plen;
970  }
971 
972 cleanup_and_exit:
973  vec_free (plaintext);
974 }
975 
976 static void
978  ike_header_t * ike)
979 {
980  int p = 0;
981  u32 len = clib_net_to_host_u32 (ike->length);
982  u8 payload = ike->nextpayload;
983  u8 *plaintext = 0;
984 
985  ike_payload_header_t *ikep;
986  u32 plen;
987 
988  clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
989  "exchange %x flags %x msgid %x length %u",
990  clib_net_to_host_u64 (ike->ispi),
991  clib_net_to_host_u64 (ike->rspi),
992  payload, ike->version,
993  ike->exchange, ike->flags,
994  clib_net_to_host_u32 (ike->msgid), len);
995 
996  plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
997 
998  if (!plaintext)
999  goto cleanup_and_exit;
1000 
1001  /* process encrypted payload */
1002  p = 0;
1003  while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
1004  {
1005  ikep = (ike_payload_header_t *) & plaintext[p];
1006  plen = clib_net_to_host_u16 (ikep->length);
1007 
1008  if (plen < sizeof (ike_payload_header_t))
1009  goto cleanup_and_exit;
1010 
1011  if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
1012  {
1014  if (n->msg_type == IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED)
1016  vec_free (n);
1017  }
1018  else if (payload == IKEV2_PAYLOAD_DELETE) /* 42 */
1019  {
1020  sa->del = ikev2_parse_delete_payload (ikep);
1021  }
1022  else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
1023  {
1025  }
1026  else
1027  {
1028  clib_warning ("unknown payload %u flags %x length %u data %u",
1029  payload, ikep->flags, plen - 4,
1030  format_hex_bytes, ikep->payload, plen - 4);
1031 
1032  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1033  {
1034  sa->unsupported_cp = payload;
1035  return;
1036  }
1037  }
1038 
1039  payload = ikep->nextpayload;
1040  p += plen;
1041  }
1042 
1043 cleanup_and_exit:
1044  vec_free (plaintext);
1045 }
1046 
1047 static void
1049  ike_header_t * ike)
1050 {
1051  int p = 0;
1052  u32 len = clib_net_to_host_u32 (ike->length);
1053  u8 payload = ike->nextpayload;
1054  u8 *plaintext = 0;
1055  u8 rekeying = 0;
1056  u8 nonce[IKEV2_NONCE_SIZE];
1057 
1058  ike_payload_header_t *ikep;
1059  u32 plen;
1060  ikev2_notify_t *n = 0;
1061  ikev2_ts_t *tsi = 0;
1062  ikev2_ts_t *tsr = 0;
1063  ikev2_sa_proposal_t *proposal = 0;
1064  ikev2_child_sa_t *child_sa;
1065 
1066  clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
1067  "exchange %x flags %x msgid %x length %u",
1068  clib_net_to_host_u64 (ike->ispi),
1069  clib_net_to_host_u64 (ike->rspi),
1070  payload, ike->version,
1071  ike->exchange, ike->flags,
1072  clib_net_to_host_u32 (ike->msgid), len);
1073 
1074  plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
1075 
1076  if (!plaintext)
1077  goto cleanup_and_exit;
1078 
1079  /* process encrypted payload */
1080  p = 0;
1081  while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
1082  {
1083  ikep = (ike_payload_header_t *) & plaintext[p];
1084  plen = clib_net_to_host_u16 (ikep->length);
1085 
1086  if (plen < sizeof (ike_payload_header_t))
1087  goto cleanup_and_exit;
1088 
1089  else if (payload == IKEV2_PAYLOAD_SA)
1090  {
1091  proposal = ikev2_parse_sa_payload (ikep);
1092  }
1093  else if (payload == IKEV2_PAYLOAD_NOTIFY)
1094  {
1095  n = ikev2_parse_notify_payload (ikep);
1096  if (n->msg_type == IKEV2_NOTIFY_MSG_REKEY_SA)
1097  {
1098  rekeying = 1;
1099  }
1100  }
1101  else if (payload == IKEV2_PAYLOAD_DELETE)
1102  {
1103  sa->del = ikev2_parse_delete_payload (ikep);
1104  }
1105  else if (payload == IKEV2_PAYLOAD_VENDOR)
1106  {
1108  }
1109  else if (payload == IKEV2_PAYLOAD_NONCE)
1110  {
1111  clib_memcpy_fast (nonce, ikep->payload, plen - sizeof (*ikep));
1112  }
1113  else if (payload == IKEV2_PAYLOAD_TSI)
1114  {
1115  tsi = ikev2_parse_ts_payload (ikep);
1116  }
1117  else if (payload == IKEV2_PAYLOAD_TSR)
1118  {
1119  tsr = ikev2_parse_ts_payload (ikep);
1120  }
1121  else
1122  {
1123  clib_warning ("unknown payload %u flags %x length %u data %u",
1124  payload, ikep->flags, plen - 4,
1125  format_hex_bytes, ikep->payload, plen - 4);
1126 
1127  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1128  {
1129  sa->unsupported_cp = payload;
1130  return;
1131  }
1132  }
1133 
1134  payload = ikep->nextpayload;
1135  p += plen;
1136  }
1137 
1138  if (sa->is_initiator && proposal->protocol_id == IKEV2_PROTOCOL_ESP)
1139  {
1140  ikev2_rekey_t *rekey = &sa->rekey[0];
1141  rekey->protocol_id = proposal->protocol_id;
1142  rekey->i_proposal =
1144  rekey->i_proposal->spi = rekey->spi;
1145  rekey->r_proposal = proposal;
1146  rekey->tsi = tsi;
1147  rekey->tsr = tsr;
1148  /* update Nr */
1149  vec_free (sa->r_nonce);
1150  vec_add (sa->r_nonce, nonce, IKEV2_NONCE_SIZE);
1151  child_sa = ikev2_sa_get_child (sa, rekey->ispi, IKEV2_PROTOCOL_ESP, 1);
1152  if (child_sa)
1153  {
1154  child_sa->rekey_retries = 0;
1155  }
1156  }
1157  else if (rekeying)
1158  {
1159  ikev2_rekey_t *rekey;
1160  child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id, 1);
1161  if (!child_sa)
1162  {
1163  clib_warning ("child SA spi %lx not found", n->spi);
1164  goto cleanup_and_exit;
1165  }
1166  vec_add2 (sa->rekey, rekey, 1);
1167  rekey->protocol_id = n->protocol_id;
1168  rekey->spi = n->spi;
1169  rekey->i_proposal = proposal;
1170  rekey->r_proposal =
1172  rekey->tsi = tsi;
1173  rekey->tsr = tsr;
1174  /* update Ni */
1175  vec_free (sa->i_nonce);
1176  vec_add (sa->i_nonce, nonce, IKEV2_NONCE_SIZE);
1177  /* generate new Nr */
1178  vec_free (sa->r_nonce);
1180  RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
1181  }
1182 
1183 cleanup_and_exit:
1184  vec_free (plaintext);
1185  vec_free (n);
1186 }
1187 
1188 static u8 *
1189 ikev2_sa_generate_authmsg (ikev2_sa_t * sa, int is_responder)
1190 {
1191  u8 *authmsg = 0;
1192  u8 *data;
1193  u8 *nonce;
1194  ikev2_id_t *id;
1195  u8 *key;
1196  u8 *packet_data;
1197  ikev2_sa_transform_t *tr_prf;
1198 
1199  tr_prf =
1200  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1201 
1202  if (is_responder)
1203  {
1204  id = &sa->r_id;
1205  key = sa->sk_pr;
1206  nonce = sa->i_nonce;
1207  packet_data = sa->last_sa_init_res_packet_data;
1208  }
1209  else
1210  {
1211  id = &sa->i_id;
1212  key = sa->sk_pi;
1213  nonce = sa->r_nonce;
1214  packet_data = sa->last_sa_init_req_packet_data;
1215  }
1216 
1217  data = vec_new (u8, 4);
1218  data[0] = id->type;
1219  vec_append (data, id->data);
1220 
1221  u8 *id_hash = ikev2_calc_prf (tr_prf, key, data);
1222  vec_append (authmsg, packet_data);
1223  vec_append (authmsg, nonce);
1224  vec_append (authmsg, id_hash);
1225  vec_free (id_hash);
1226  vec_free (data);
1227 
1228  return authmsg;
1229 }
1230 
1231 static int
1233 {
1234  if (ts1->ts_type == ts2->ts_type && ts1->protocol_id == ts2->protocol_id &&
1235  ts1->start_port == ts2->start_port && ts1->end_port == ts2->end_port &&
1236  ts1->start_addr.as_u32 == ts2->start_addr.as_u32 &&
1237  ts1->end_addr.as_u32 == ts2->end_addr.as_u32)
1238  return 1;
1239 
1240  return 0;
1241 }
1242 
1243 static void
1245 {
1246  ikev2_main_t *km = &ikev2_main;
1247  ikev2_profile_t *p;
1248  ikev2_ts_t *ts, *p_tsi, *p_tsr, *tsi = 0, *tsr = 0;
1249  ikev2_id_t *id;
1250 
1251  /* *INDENT-OFF* */
1252  pool_foreach (p, km->profiles, ({
1253 
1254  if (sa->is_initiator)
1255  {
1256  p_tsi = &p->loc_ts;
1257  p_tsr = &p->rem_ts;
1258  id = &sa->r_id;
1259  }
1260  else
1261  {
1262  p_tsi = &p->rem_ts;
1263  p_tsr = &p->loc_ts;
1264  id = &sa->i_id;
1265  }
1266 
1267  /* check id */
1268  if (p->rem_id.type != id->type ||
1269  vec_len(p->rem_id.data) != vec_len(id->data) ||
1270  memcmp(p->rem_id.data, id->data, vec_len(p->rem_id.data)))
1271  continue;
1272 
1273  vec_foreach(ts, sa->childs[0].tsi)
1274  {
1275  if (ikev2_ts_cmp(p_tsi, ts))
1276  {
1277  tsi = vec_dup(ts);
1278  break;
1279  }
1280  }
1281 
1282  vec_foreach(ts, sa->childs[0].tsr)
1283  {
1284  if (ikev2_ts_cmp(p_tsr, ts))
1285  {
1286  tsr = vec_dup(ts);
1287  break;
1288  }
1289  }
1290 
1291  break;
1292  }));
1293  /* *INDENT-ON* */
1294 
1295  if (tsi && tsr)
1296  {
1297  vec_free (sa->childs[0].tsi);
1298  vec_free (sa->childs[0].tsr);
1299  sa->childs[0].tsi = tsi;
1300  sa->childs[0].tsr = tsr;
1301  }
1302  else
1303  {
1304  vec_free (tsi);
1305  vec_free (tsr);
1307  }
1308 }
1309 
1310 static void
1312 {
1313  ikev2_main_t *km = &ikev2_main;
1314  ikev2_profile_t *p, *sel_p = 0;
1315  u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1316  ikev2_sa_transform_t *tr_prf;
1317 
1318  tr_prf =
1319  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1320 
1321  /* only shared key and rsa signature */
1322  if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1323  sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1324  {
1325  clib_warning ("unsupported authentication method %u",
1326  sa->i_auth.method);
1328  return;
1329  }
1330 
1331  key_pad = format (0, "%s", IKEV2_KEY_PAD);
1332  authmsg = ikev2_sa_generate_authmsg (sa, sa->is_initiator);
1333 
1334  ikev2_id_t *sa_id;
1335  ikev2_auth_t *sa_auth;
1336 
1337  if (sa->is_initiator)
1338  {
1339  sa_id = &sa->r_id;
1340  sa_auth = &sa->r_auth;
1341  }
1342  else
1343  {
1344  sa_id = &sa->i_id;
1345  sa_auth = &sa->i_auth;
1346  }
1347 
1348  /* *INDENT-OFF* */
1349  pool_foreach (p, km->profiles, ({
1350 
1351  /* check id */
1352  if (p->rem_id.type != sa_id->type ||
1353  vec_len(p->rem_id.data) != vec_len(sa_id->data) ||
1354  memcmp(p->rem_id.data, sa_id->data, vec_len(p->rem_id.data)))
1355  continue;
1356 
1357  if (sa_auth->method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1358  {
1359  if (!p->auth.data ||
1360  p->auth.method != IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1361  continue;
1362 
1363  psk = ikev2_calc_prf(tr_prf, p->auth.data, key_pad);
1364  auth = ikev2_calc_prf(tr_prf, psk, authmsg);
1365 
1366  if (!memcmp(auth, sa_auth->data, vec_len(sa_auth->data)))
1367  {
1368  ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1369  vec_free(auth);
1370  sel_p = p;
1371  break;
1372  }
1373 
1374  }
1375  else if (sa_auth->method == IKEV2_AUTH_METHOD_RSA_SIG)
1376  {
1377  if (p->auth.method != IKEV2_AUTH_METHOD_RSA_SIG)
1378  continue;
1379 
1380  if (ikev2_verify_sign(p->auth.key, sa_auth->data, authmsg) == 1)
1381  {
1382  ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1383  sel_p = p;
1384  break;
1385  }
1386  }
1387 
1388  vec_free(auth);
1389  vec_free(psk);
1390  }));
1391  /* *INDENT-ON* */
1392 
1393  vec_free (authmsg);
1394 
1395  if (sa->state == IKEV2_STATE_AUTHENTICATED)
1396  {
1397  if (!sa->is_initiator)
1398  {
1399  vec_free (sa->r_id.data);
1400  sa->r_id.data = vec_dup (sel_p->loc_id.data);
1401  sa->r_id.type = sel_p->loc_id.type;
1402 
1403  /* generate our auth data */
1404  authmsg = ikev2_sa_generate_authmsg (sa, 1);
1405  if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1406  {
1407  sa->r_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1408  sa->r_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1409  }
1410  else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1411  {
1412  sa->r_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1413  sa->r_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1414  }
1415  vec_free (authmsg);
1416 
1417  /* select transforms for 1st child sa */
1418  ikev2_sa_free_proposal_vector (&sa->childs[0].r_proposals);
1419  sa->childs[0].r_proposals =
1420  ikev2_select_proposal (sa->childs[0].i_proposals,
1422  }
1423  }
1424  else
1425  {
1427  }
1428  vec_free (psk);
1429  vec_free (key_pad);
1430 }
1431 
1432 
1433 static void
1435 {
1436  ikev2_main_t *km = &ikev2_main;
1437  u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1438  ikev2_sa_transform_t *tr_prf;
1439 
1440  tr_prf =
1441  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1442 
1443  /* only shared key and rsa signature */
1444  if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1445  sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1446  {
1447  clib_warning ("unsupported authentication method %u",
1448  sa->i_auth.method);
1450  return;
1451  }
1452 
1453  key_pad = format (0, "%s", IKEV2_KEY_PAD);
1454  authmsg = ikev2_sa_generate_authmsg (sa, 0);
1455  psk = ikev2_calc_prf (tr_prf, sa->i_auth.data, key_pad);
1456  auth = ikev2_calc_prf (tr_prf, psk, authmsg);
1457 
1458 
1459  if (sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1460  {
1461  sa->i_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1462  sa->i_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1463  }
1464  else if (sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1465  {
1466  sa->i_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1467  sa->i_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1468  }
1469 
1470  vec_free (psk);
1471  vec_free (key_pad);
1472  vec_free (auth);
1473  vec_free (authmsg);
1474 }
1475 
1476 
1477 static int
1479  ikev2_child_sa_t * child)
1480 {
1483  ikev2_sa_proposal_t *proposals;
1484  u8 encr_type = 0;
1485  u8 integ_type = 0;
1486 
1487  if (!child->r_proposals)
1488  {
1490  return 1;
1491  }
1492 
1493  clib_memset (&a, 0, sizeof (a));
1494  a.is_add = 1;
1495  if (sa->is_initiator)
1496  {
1497  a.local_ip.ip4.as_u32 = sa->iaddr.as_u32;
1498  a.remote_ip.ip4.as_u32 = sa->raddr.as_u32;
1499  proposals = child->i_proposals;
1500  a.local_spi = child->r_proposals[0].spi;
1501  a.remote_spi = child->i_proposals[0].spi;
1502  }
1503  else
1504  {
1505  a.local_ip.ip4.as_u32 = sa->raddr.as_u32;
1506  a.remote_ip.ip4.as_u32 = sa->iaddr.as_u32;
1507  proposals = child->r_proposals;
1508  a.local_spi = child->i_proposals[0].spi;
1509  a.remote_spi = child->r_proposals[0].spi;
1510  }
1511  a.anti_replay = 1;
1512 
1513  tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ESN);
1514  if (tr)
1515  a.esn = tr->esn_type;
1516  else
1517  a.esn = 0;
1518 
1519  tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1520  if (tr)
1521  {
1522  if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
1523  {
1524  switch (tr->key_len)
1525  {
1526  case 16:
1527  encr_type = IPSEC_CRYPTO_ALG_AES_CBC_128;
1528  break;
1529  case 24:
1530  encr_type = IPSEC_CRYPTO_ALG_AES_CBC_192;
1531  break;
1532  case 32:
1533  encr_type = IPSEC_CRYPTO_ALG_AES_CBC_256;
1534  break;
1535  default:
1537  return 1;
1538  break;
1539  }
1540  }
1541  else
1542  {
1544  return 1;
1545  }
1546  }
1547  else
1548  {
1550  return 1;
1551  }
1552 
1553  tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_INTEG);
1554  if (tr)
1555  {
1556  switch (tr->integ_type)
1557  {
1558  case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_256_128:
1559  integ_type = IPSEC_INTEG_ALG_SHA_256_128;
1560  break;
1561  case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_384_192:
1562  integ_type = IPSEC_INTEG_ALG_SHA_384_192;
1563  break;
1564  case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_512_256:
1565  integ_type = IPSEC_INTEG_ALG_SHA_512_256;
1566  break;
1567  case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96:
1568  integ_type = IPSEC_INTEG_ALG_SHA1_96;
1569  break;
1570  default:
1572  return 1;
1573  }
1574  }
1575  else
1576  {
1578  return 1;
1579  }
1580 
1581  ikev2_calc_child_keys (sa, child);
1582 
1583  u8 *loc_ckey, *rem_ckey, *loc_ikey, *rem_ikey;
1584  if (sa->is_initiator)
1585  {
1586  loc_ikey = child->sk_ai;
1587  rem_ikey = child->sk_ar;
1588  loc_ckey = child->sk_ei;
1589  rem_ckey = child->sk_er;
1590  }
1591  else
1592  {
1593  loc_ikey = child->sk_ar;
1594  rem_ikey = child->sk_ai;
1595  loc_ckey = child->sk_er;
1596  rem_ckey = child->sk_ei;
1597  }
1598 
1599  a.integ_alg = integ_type;
1600  a.local_integ_key_len = vec_len (loc_ikey);
1602  a.remote_integ_key_len = vec_len (rem_ikey);
1604 
1605  a.crypto_alg = encr_type;
1606  a.local_crypto_key_len = vec_len (loc_ckey);
1608  a.remote_crypto_key_len = vec_len (rem_ckey);
1610 
1611  if (sa->profile && sa->profile->lifetime)
1612  {
1614  + sa->profile->lifetime;
1615  if (sa->profile->lifetime_jitter)
1616  {
1617  // This is not much better than rand(3), which Coverity warns
1618  // is unsuitable for security applications; random_u32 is
1619  // however fast. If this perturbance to the expiration time
1620  // needs to use a better RNG then we may need to use something
1621  // like /dev/urandom which has significant overhead.
1622  u32 rnd = (u32) (vlib_time_now (vnm->vlib_main) * 1e6);
1623  rnd = random_u32 (&rnd);
1624 
1625  child->time_to_expiration +=
1626  1 + (rnd % sa->profile->lifetime_jitter);
1627  }
1628  }
1629 
1631 
1632  return 0;
1633 }
1634 
1635 static int
1637  ikev2_child_sa_t * child)
1638 {
1640 
1641  if (sa->is_initiator)
1642  {
1643  if (!vec_len (child->i_proposals))
1644  return 0;
1645 
1646  a.is_add = 0;
1647  a.local_ip.ip4.as_u32 = sa->iaddr.as_u32;
1648  a.remote_ip.ip4.as_u32 = sa->raddr.as_u32;
1649  a.local_spi = child->r_proposals[0].spi;
1650  a.remote_spi = child->i_proposals[0].spi;
1651  }
1652  else
1653  {
1654  if (!vec_len (child->r_proposals))
1655  return 0;
1656 
1657  a.is_add = 0;
1658  a.local_ip.ip4.as_u32 = sa->raddr.as_u32;
1659  a.remote_ip.ip4.as_u32 = sa->iaddr.as_u32;
1660  a.local_spi = child->i_proposals[0].spi;
1661  a.remote_spi = child->r_proposals[0].spi;
1662  }
1663 
1665  return 0;
1666 }
1667 
1668 static u32
1669 ikev2_generate_message (ikev2_sa_t * sa, ike_header_t * ike, void *user)
1670 {
1671  v8 *integ = 0;
1672  ike_payload_header_t *ph;
1673  u16 plen;
1674  u32 tlen = 0;
1675 
1676  ikev2_sa_transform_t *tr_encr, *tr_integ;
1677  tr_encr =
1678  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1679  tr_integ =
1680  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
1681 
1682  ikev2_payload_chain_t *chain = 0;
1683  ikev2_payload_new_chain (chain);
1684 
1685  if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
1686  {
1687  if (sa->r_proposals == 0)
1688  {
1689  ikev2_payload_add_notify (chain,
1690  IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1692  }
1693  else if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
1694  {
1695  u8 *data = vec_new (u8, 2);
1696  ikev2_sa_transform_t *tr_dh;
1697  tr_dh =
1699  IKEV2_TRANSFORM_TYPE_DH);
1700  ASSERT (tr_dh && tr_dh->dh_type);
1701 
1702  data[0] = (tr_dh->dh_type >> 8) & 0xff;
1703  data[1] = (tr_dh->dh_type) & 0xff;
1704 
1705  ikev2_payload_add_notify (chain,
1706  IKEV2_NOTIFY_MSG_INVALID_KE_PAYLOAD,
1707  data);
1708  vec_free (data);
1710  }
1711  else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
1712  {
1713  u8 *data = vec_new (u8, 1);
1714 
1715  data[0] = sa->unsupported_cp;
1716  ikev2_payload_add_notify (chain,
1717  IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1718  data);
1719  vec_free (data);
1720  }
1721  else
1722  {
1723  ike->rspi = clib_host_to_net_u64 (sa->rspi);
1724  ikev2_payload_add_sa (chain, sa->r_proposals);
1725  ikev2_payload_add_ke (chain, sa->dh_group, sa->r_dh_data);
1726  ikev2_payload_add_nonce (chain, sa->r_nonce);
1727  }
1728  }
1729  else if (ike->exchange == IKEV2_EXCHANGE_IKE_AUTH)
1730  {
1731  if (sa->state == IKEV2_STATE_AUTHENTICATED)
1732  {
1734  ikev2_payload_add_auth (chain, &sa->r_auth);
1735  ikev2_payload_add_sa (chain, sa->childs[0].r_proposals);
1738  }
1739  else if (sa->state == IKEV2_STATE_AUTH_FAILED)
1740  {
1741  ikev2_payload_add_notify (chain,
1742  IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED,
1743  0);
1745  }
1746  else if (sa->state == IKEV2_STATE_TS_UNACCEPTABLE)
1747  {
1748  ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_TS_UNACCEPTABLE,
1749  0);
1751  ikev2_payload_add_auth (chain, &sa->r_auth);
1752  }
1753  else if (sa->state == IKEV2_STATE_NO_PROPOSAL_CHOSEN)
1754  {
1755  ikev2_payload_add_notify (chain,
1756  IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1758  ikev2_payload_add_auth (chain, &sa->r_auth);
1761  }
1762  else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
1763  {
1764  u8 *data = vec_new (u8, 1);
1765 
1766  data[0] = sa->unsupported_cp;
1767  ikev2_payload_add_notify (chain,
1768  IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1769  data);
1770  vec_free (data);
1771  }
1772  else if (sa->state == IKEV2_STATE_SA_INIT)
1773  {
1775  ikev2_payload_add_auth (chain, &sa->i_auth);
1776  ikev2_payload_add_sa (chain, sa->childs[0].i_proposals);
1779  }
1780  else
1781  {
1783  goto done;
1784  }
1785  }
1786  else if (ike->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
1787  {
1788  /* if pending delete */
1789  if (sa->del)
1790  {
1791  if (sa->del[0].protocol_id == IKEV2_PROTOCOL_IKE)
1792  {
1793  if (sa->is_initiator)
1794  ikev2_payload_add_delete (chain, sa->del);
1795 
1796  /* The response to a request that deletes the IKE SA is an empty
1797  INFORMATIONAL response. */
1799  }
1800  /* The response to a request that deletes ESP or AH SAs will contain
1801  delete payloads for the paired SAs going in the other direction. */
1802  else
1803  {
1804  ikev2_payload_add_delete (chain, sa->del);
1805  }
1806  vec_free (sa->del);
1807  sa->del = 0;
1808  }
1809  /* received N(AUTHENTICATION_FAILED) */
1810  else if (sa->state == IKEV2_STATE_AUTH_FAILED)
1811  {
1813  goto done;
1814  }
1815  /* received unsupported critical payload */
1816  else if (sa->unsupported_cp)
1817  {
1818  u8 *data = vec_new (u8, 1);
1819 
1820  data[0] = sa->unsupported_cp;
1821  ikev2_payload_add_notify (chain,
1822  IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1823  data);
1824  vec_free (data);
1825  sa->unsupported_cp = 0;
1826  }
1827  /* else send empty response */
1828  }
1829  else if (ike->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
1830  {
1831  if (sa->is_initiator)
1832  {
1833 
1834  ikev2_sa_proposal_t *proposals = (ikev2_sa_proposal_t *) user;
1835  ikev2_notify_t notify;
1836  u8 *data = vec_new (u8, 4);
1837  clib_memset (&notify, 0, sizeof (notify));
1839  notify.spi = sa->childs[0].i_proposals->spi;
1840  *(u32 *) data = clib_host_to_net_u32 (notify.spi);
1841 
1842  ikev2_payload_add_sa (chain, proposals);
1843  ikev2_payload_add_nonce (chain, sa->i_nonce);
1846  ikev2_payload_add_notify_2 (chain, IKEV2_NOTIFY_MSG_REKEY_SA, data,
1847  &notify);
1848 
1849  vec_free (data);
1850  }
1851  else
1852  {
1853  if (sa->rekey)
1854  {
1855  ikev2_payload_add_sa (chain, sa->rekey[0].r_proposal);
1856  ikev2_payload_add_nonce (chain, sa->r_nonce);
1857  ikev2_payload_add_ts (chain, sa->rekey[0].tsi,
1859  ikev2_payload_add_ts (chain, sa->rekey[0].tsr,
1861  vec_del1 (sa->rekey, 0);
1862  }
1863  else if (sa->unsupported_cp)
1864  {
1865  u8 *data = vec_new (u8, 1);
1866 
1867  data[0] = sa->unsupported_cp;
1868  ikev2_payload_add_notify (chain,
1869  IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1870  data);
1871  vec_free (data);
1872  sa->unsupported_cp = 0;
1873  }
1874  else
1875  {
1876  ikev2_payload_add_notify (chain,
1877  IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS,
1878  0);
1879  }
1880  }
1881  }
1882 
1883  /* IKEv2 header */
1884  ike->version = IKE_VERSION_2;
1885  ike->nextpayload = IKEV2_PAYLOAD_SK;
1886  tlen = sizeof (*ike);
1887  if (sa->is_initiator)
1888  {
1889  ike->flags = IKEV2_HDR_FLAG_INITIATOR;
1890  sa->last_init_msg_id = clib_net_to_host_u32 (ike->msgid);
1891  }
1892  else
1893  {
1894  ike->flags = IKEV2_HDR_FLAG_RESPONSE;
1895  }
1896 
1897 
1898  if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
1899  {
1900  tlen += vec_len (chain->data);
1901  ike->nextpayload = chain->first_payload_type;
1902  ike->length = clib_host_to_net_u32 (tlen);
1903  clib_memcpy_fast (ike->payload, chain->data, vec_len (chain->data));
1904 
1905  /* store whole IKE payload - needed for PSK auth */
1907  vec_add (sa->last_sa_init_res_packet_data, ike, tlen);
1908  }
1909  else
1910  {
1911 
1912  ikev2_payload_chain_add_padding (chain, tr_encr->block_size);
1913 
1914  /* SK payload */
1915  plen = sizeof (*ph);
1916  ph = (ike_payload_header_t *) & ike->payload[0];
1917  ph->nextpayload = chain->first_payload_type;
1918  ph->flags = 0;
1919  int enc_len = ikev2_encrypt_data (sa, chain->data, ph->payload);
1920  plen += enc_len;
1921 
1922  /* add space for hmac */
1923  plen += tr_integ->key_trunc;
1924  tlen += plen;
1925 
1926  /* payload and total length */
1927  ph->length = clib_host_to_net_u16 (plen);
1928  ike->length = clib_host_to_net_u32 (tlen);
1929 
1930  /* calc integrity data for whole packet except hash itself */
1931  integ =
1932  ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ai : sa->sk_ar,
1933  (u8 *) ike, tlen - tr_integ->key_trunc);
1934 
1935  clib_memcpy_fast (ike->payload + tlen - tr_integ->key_trunc -
1936  sizeof (*ike), integ, tr_integ->key_trunc);
1937 
1938  /* store whole IKE payload - needed for retransmit */
1940  vec_add (sa->last_res_packet_data, ike, tlen);
1941  }
1942 
1943 done:
1945  vec_free (integ);
1946  return tlen;
1947 }
1948 
1949 static int
1950 ikev2_retransmit_sa_init (ike_header_t * ike,
1951  ip4_address_t iaddr, ip4_address_t raddr)
1952 {
1953  ikev2_main_t *km = &ikev2_main;
1954  ikev2_sa_t *sa;
1955  u32 thread_index = vlib_get_thread_index ();
1956 
1957  /* *INDENT-OFF* */
1958  pool_foreach (sa, km->per_thread_data[thread_index].sas, ({
1959  if (sa->ispi == clib_net_to_host_u64(ike->ispi) &&
1960  sa->iaddr.as_u32 == iaddr.as_u32 &&
1961  sa->raddr.as_u32 == raddr.as_u32)
1962  {
1963  int p = 0;
1964  u32 len = clib_net_to_host_u32(ike->length);
1965  u8 payload = ike->nextpayload;
1966 
1967  while (p < len && payload!= IKEV2_PAYLOAD_NONE) {
1968  ike_payload_header_t * ikep = (ike_payload_header_t *) &ike->payload[p];
1969  u32 plen = clib_net_to_host_u16(ikep->length);
1970 
1971  if (plen < sizeof(ike_payload_header_t))
1972  return -1;
1973 
1974  if (payload == IKEV2_PAYLOAD_NONCE)
1975  {
1976  if (!memcmp(sa->i_nonce, ikep->payload, plen - sizeof(*ikep)))
1977  {
1978  /* req is retransmit */
1979  if (sa->state == IKEV2_STATE_SA_INIT)
1980  {
1981  ike_header_t * tmp;
1982  tmp = (ike_header_t*)sa->last_sa_init_res_packet_data;
1983  ike->ispi = tmp->ispi;
1984  ike->rspi = tmp->rspi;
1985  ike->nextpayload = tmp->nextpayload;
1986  ike->version = tmp->version;
1987  ike->exchange = tmp->exchange;
1988  ike->flags = tmp->flags;
1989  ike->msgid = tmp->msgid;
1990  ike->length = tmp->length;
1991  clib_memcpy_fast(ike->payload, tmp->payload,
1992  clib_net_to_host_u32(tmp->length) - sizeof(*ike));
1993  clib_warning("IKE_SA_INIT retransmit from %U to %U",
1994  format_ip4_address, &raddr,
1995  format_ip4_address, &iaddr);
1996  return 1;
1997  }
1998  /* else ignore req */
1999  else
2000  {
2001  clib_warning("IKE_SA_INIT ignore from %U to %U",
2002  format_ip4_address, &raddr,
2003  format_ip4_address, &iaddr);
2004  return -1;
2005  }
2006  }
2007  }
2008  payload = ikep->nextpayload;
2009  p+=plen;
2010  }
2011  }
2012  }));
2013  /* *INDENT-ON* */
2014 
2015  /* req is not retransmit */
2016  return 0;
2017 }
2018 
2019 static int
2020 ikev2_retransmit_resp (ikev2_sa_t * sa, ike_header_t * ike)
2021 {
2022  u32 msg_id = clib_net_to_host_u32 (ike->msgid);
2023 
2024  /* new req */
2025  if (msg_id > sa->last_msg_id)
2026  {
2027  sa->last_msg_id = msg_id;
2028  return 0;
2029  }
2030  /* retransmitted req */
2031  else if (msg_id == sa->last_msg_id)
2032  {
2033  ike_header_t *tmp;
2034  tmp = (ike_header_t *) sa->last_res_packet_data;
2035  ike->ispi = tmp->ispi;
2036  ike->rspi = tmp->rspi;
2037  ike->nextpayload = tmp->nextpayload;
2038  ike->version = tmp->version;
2039  ike->exchange = tmp->exchange;
2040  ike->flags = tmp->flags;
2041  ike->msgid = tmp->msgid;
2042  ike->length = tmp->length;
2043  clib_memcpy_fast (ike->payload, tmp->payload,
2044  clib_net_to_host_u32 (tmp->length) - sizeof (*ike));
2045  clib_warning ("IKE msgid %u retransmit from %U to %U",
2046  msg_id,
2047  format_ip4_address, &sa->raddr,
2048  format_ip4_address, &sa->iaddr);
2049  return 1;
2050  }
2051  /* old req ignore */
2052  else
2053  {
2054  clib_warning ("IKE msgid %u req ignore from %U to %U",
2055  msg_id,
2056  format_ip4_address, &sa->raddr,
2057  format_ip4_address, &sa->iaddr);
2058  return -1;
2059  }
2060 }
2061 
2062 
2063 static uword
2065  vlib_node_runtime_t * node, vlib_frame_t * frame)
2066 {
2067  u32 n_left_from, *from, *to_next;
2068  ikev2_next_t next_index;
2069  ikev2_main_t *km = &ikev2_main;
2070  u32 thread_index = vlib_get_thread_index ();
2071 
2072  from = vlib_frame_vector_args (frame);
2073  n_left_from = frame->n_vectors;
2074  next_index = node->cached_next_index;
2075 
2076  while (n_left_from > 0)
2077  {
2078  u32 n_left_to_next;
2079 
2080  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2081 
2082  while (n_left_from > 0 && n_left_to_next > 0)
2083  {
2084  u32 bi0;
2085  vlib_buffer_t *b0;
2086  u32 next0 = IKEV2_NEXT_ERROR_DROP;
2087  u32 sw_if_index0;
2088  ip4_header_t *ip40;
2089  udp_header_t *udp0;
2090  ike_header_t *ike0;
2091  ikev2_sa_t *sa0 = 0;
2092  ikev2_sa_t sa; /* temporary store for SA */
2093  int len = 0;
2094  int r;
2095 
2096  /* speculatively enqueue b0 to the current next frame */
2097  bi0 = from[0];
2098  to_next[0] = bi0;
2099  from += 1;
2100  to_next += 1;
2101  n_left_from -= 1;
2102  n_left_to_next -= 1;
2103 
2104  b0 = vlib_get_buffer (vm, bi0);
2105  ike0 = vlib_buffer_get_current (b0);
2106  vlib_buffer_advance (b0, -sizeof (*udp0));
2107  udp0 = vlib_buffer_get_current (b0);
2108  vlib_buffer_advance (b0, -sizeof (*ip40));
2109  ip40 = vlib_buffer_get_current (b0);
2110 
2111  if (ike0->version != IKE_VERSION_2)
2112  {
2114  IKEV2_ERROR_NOT_IKEV2, 1);
2115  goto dispatch0;
2116  }
2117 
2118  if (ike0->exchange == IKEV2_EXCHANGE_SA_INIT)
2119  {
2120  sa0 = &sa;
2121  clib_memset (sa0, 0, sizeof (*sa0));
2122 
2123  if (ike0->flags & IKEV2_HDR_FLAG_INITIATOR)
2124  {
2125  if (ike0->rspi == 0)
2126  {
2127  sa0->raddr.as_u32 = ip40->dst_address.as_u32;
2128  sa0->iaddr.as_u32 = ip40->src_address.as_u32;
2129 
2130  r = ikev2_retransmit_sa_init (ike0, sa0->iaddr,
2131  sa0->raddr);
2132  if (r == 1)
2133  {
2135  IKEV2_ERROR_IKE_SA_INIT_RETRANSMIT,
2136  1);
2137  len = clib_net_to_host_u32 (ike0->length);
2138  goto dispatch0;
2139  }
2140  else if (r == -1)
2141  {
2143  IKEV2_ERROR_IKE_SA_INIT_IGNORE,
2144  1);
2145  goto dispatch0;
2146  }
2147 
2148  ikev2_process_sa_init_req (vm, sa0, ike0);
2149 
2150  if (sa0->state == IKEV2_STATE_SA_INIT)
2151  {
2153  sa0->r_proposals =
2157  }
2158 
2159  if (sa0->state == IKEV2_STATE_SA_INIT
2161  {
2162  len = ikev2_generate_message (sa0, ike0, 0);
2163  }
2164 
2165  if (sa0->state == IKEV2_STATE_SA_INIT)
2166  {
2167  /* add SA to the pool */
2168  pool_get (km->per_thread_data[thread_index].sas,
2169  sa0);
2170  clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
2171  hash_set (km->
2172  per_thread_data[thread_index].sa_by_rspi,
2173  sa0->rspi,
2174  sa0 -
2175  km->per_thread_data[thread_index].sas);
2176  }
2177  else
2178  {
2179  ikev2_sa_free_all_vec (sa0);
2180  }
2181  }
2182  }
2183  else //received sa_init without initiator flag
2184  {
2185  ikev2_process_sa_init_resp (vm, sa0, ike0);
2186 
2187  if (sa0->state == IKEV2_STATE_SA_INIT)
2188  {
2189  ike0->exchange = IKEV2_EXCHANGE_IKE_AUTH;
2190  uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
2191  if (p)
2192  {
2193  ikev2_sa_t *sai =
2194  pool_elt_at_index (km->sais, p[0]);
2195 
2196  ikev2_complete_sa_data (sa0, sai);
2197  ikev2_calc_keys (sa0);
2198  ikev2_sa_auth_init (sa0);
2199  len = ikev2_generate_message (sa0, ike0, 0);
2200  }
2201  }
2202 
2203  if (sa0->state == IKEV2_STATE_SA_INIT)
2204  {
2205  /* add SA to the pool */
2206  pool_get (km->per_thread_data[thread_index].sas, sa0);
2207  clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
2208  hash_set (km->per_thread_data[thread_index].sa_by_rspi,
2209  sa0->rspi,
2210  sa0 - km->per_thread_data[thread_index].sas);
2211  }
2212  else
2213  {
2214  ikev2_sa_free_all_vec (sa0);
2215  }
2216  }
2217  }
2218  else if (ike0->exchange == IKEV2_EXCHANGE_IKE_AUTH)
2219  {
2220  uword *p;
2221  p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2222  clib_net_to_host_u64 (ike0->rspi));
2223  if (p)
2224  {
2225  sa0 =
2226  pool_elt_at_index (km->per_thread_data[thread_index].sas,
2227  p[0]);
2228 
2229  r = ikev2_retransmit_resp (sa0, ike0);
2230  if (r == 1)
2231  {
2233  IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2234  1);
2235  len = clib_net_to_host_u32 (ike0->length);
2236  goto dispatch0;
2237  }
2238  else if (r == -1)
2239  {
2241  IKEV2_ERROR_IKE_REQ_IGNORE,
2242  1);
2243  goto dispatch0;
2244  }
2245 
2246  ikev2_process_auth_req (vm, sa0, ike0);
2247  ikev2_sa_auth (sa0);
2248  if (sa0->state == IKEV2_STATE_AUTHENTICATED)
2249  {
2251  ikev2_sa_match_ts (sa0);
2252  if (sa0->state != IKEV2_STATE_TS_UNACCEPTABLE)
2254  &sa0->childs[0]);
2255  }
2256 
2257  if (sa0->is_initiator)
2258  {
2259  uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
2260  if (p)
2261  {
2262  ikev2_sa_t *sai =
2263  pool_elt_at_index (km->sais, p[0]);
2264  hash_unset (km->sa_by_ispi, sai->ispi);
2265  ikev2_sa_free_all_vec (sai);
2266  pool_put (km->sais, sai);
2267  }
2268  }
2269  else
2270  {
2271  len = ikev2_generate_message (sa0, ike0, 0);
2272  }
2273  }
2274  }
2275  else if (ike0->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2276  {
2277  uword *p;
2278  p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2279  clib_net_to_host_u64 (ike0->rspi));
2280  if (p)
2281  {
2282  sa0 =
2283  pool_elt_at_index (km->per_thread_data[thread_index].sas,
2284  p[0]);
2285 
2286  r = ikev2_retransmit_resp (sa0, ike0);
2287  if (r == 1)
2288  {
2290  IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2291  1);
2292  len = clib_net_to_host_u32 (ike0->length);
2293  goto dispatch0;
2294  }
2295  else if (r == -1)
2296  {
2298  IKEV2_ERROR_IKE_REQ_IGNORE,
2299  1);
2300  goto dispatch0;
2301  }
2302 
2303  ikev2_process_informational_req (vm, sa0, ike0);
2304  if (sa0->del)
2305  {
2306  if (sa0->del[0].protocol_id != IKEV2_PROTOCOL_IKE)
2307  {
2308  ikev2_delete_t *d, *tmp, *resp = 0;
2309  vec_foreach (d, sa0->del)
2310  {
2311  ikev2_child_sa_t *ch_sa;
2312  ch_sa = ikev2_sa_get_child (sa0, d->spi,
2313  d->protocol_id,
2314  !sa0->is_initiator);
2315  if (ch_sa)
2316  {
2318  sa0, ch_sa);
2319  if (!sa0->is_initiator)
2320  {
2321  vec_add2 (resp, tmp, 1);
2322  tmp->protocol_id = d->protocol_id;
2323  tmp->spi = ch_sa->r_proposals[0].spi;
2324  }
2325  ikev2_sa_del_child_sa (sa0, ch_sa);
2326  }
2327  }
2328  if (!sa0->is_initiator)
2329  {
2330  vec_free (sa0->del);
2331  sa0->del = resp;
2332  }
2333  }
2334  }
2335  if (!sa0->is_initiator)
2336  {
2337  len = ikev2_generate_message (sa0, ike0, 0);
2338  }
2339  }
2340  }
2341  else if (ike0->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2342  {
2343  uword *p;
2344  p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
2345  clib_net_to_host_u64 (ike0->rspi));
2346  if (p)
2347  {
2348  sa0 =
2349  pool_elt_at_index (km->per_thread_data[thread_index].sas,
2350  p[0]);
2351 
2352  r = ikev2_retransmit_resp (sa0, ike0);
2353  if (r == 1)
2354  {
2356  IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2357  1);
2358  len = clib_net_to_host_u32 (ike0->length);
2359  goto dispatch0;
2360  }
2361  else if (r == -1)
2362  {
2364  IKEV2_ERROR_IKE_REQ_IGNORE,
2365  1);
2366  goto dispatch0;
2367  }
2368 
2369  ikev2_process_create_child_sa_req (vm, sa0, ike0);
2370  if (sa0->rekey)
2371  {
2372  if (sa0->rekey[0].protocol_id != IKEV2_PROTOCOL_IKE)
2373  {
2374  ikev2_child_sa_t *child;
2375  vec_add2 (sa0->childs, child, 1);
2376  child->r_proposals = sa0->rekey[0].r_proposal;
2377  child->i_proposals = sa0->rekey[0].i_proposal;
2378  child->tsi = sa0->rekey[0].tsi;
2379  child->tsr = sa0->rekey[0].tsr;
2381  child);
2382  }
2383  if (sa0->is_initiator)
2384  {
2385  vec_del1 (sa0->rekey, 0);
2386  }
2387  else
2388  {
2389  len = ikev2_generate_message (sa0, ike0, 0);
2390  }
2391  }
2392  }
2393  }
2394  else
2395  {
2396  clib_warning ("IKEv2 exchange %u packet received from %U to %U",
2397  ike0->exchange,
2400  }
2401 
2402  dispatch0:
2403  /* if we are sending packet back, rewrite headers */
2404  if (len)
2405  {
2406  next0 = IKEV2_NEXT_IP4_LOOKUP;
2407  if (sa0->is_initiator)
2408  {
2409  ip40->dst_address.as_u32 = sa0->raddr.as_u32;
2410  ip40->src_address.as_u32 = sa0->iaddr.as_u32;
2411  }
2412  else
2413  {
2414  ip40->dst_address.as_u32 = sa0->iaddr.as_u32;
2415  ip40->src_address.as_u32 = sa0->raddr.as_u32;
2416  }
2417  udp0->length =
2418  clib_host_to_net_u16 (len + sizeof (udp_header_t));
2419  udp0->checksum = 0;
2420  b0->current_length =
2421  len + sizeof (ip4_header_t) + sizeof (udp_header_t);
2422  ip40->length = clib_host_to_net_u16 (b0->current_length);
2423  ip40->checksum = ip4_header_checksum (ip40);
2424  }
2425  /* delete sa */
2426  if (sa0 && (sa0->state == IKEV2_STATE_DELETED ||
2428  {
2430 
2431  vec_foreach (c, sa0->childs)
2433 
2434  ikev2_delete_sa (sa0);
2435  }
2436  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2437 
2439  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2440  {
2441  ikev2_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
2442  t->sw_if_index = sw_if_index0;
2443  t->next_index = next0;
2444  }
2445 
2446  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2447  n_left_to_next, bi0, next0);
2448  }
2449 
2450  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2451  }
2452 
2454  IKEV2_ERROR_PROCESSED, frame->n_vectors);
2455  return frame->n_vectors;
2456 }
2457 
2458 /* *INDENT-OFF* */
2459 VLIB_REGISTER_NODE (ikev2_node,static) = {
2460  .function = ikev2_node_fn,
2461  .name = "ikev2",
2462  .vector_size = sizeof (u32),
2463  .format_trace = format_ikev2_trace,
2464  .type = VLIB_NODE_TYPE_INTERNAL,
2465 
2466  .n_errors = ARRAY_LEN(ikev2_error_strings),
2467  .error_strings = ikev2_error_strings,
2468 
2469  .n_next_nodes = IKEV2_N_NEXT,
2470 
2471  .next_nodes = {
2472  [IKEV2_NEXT_IP4_LOOKUP] = "ip4-lookup",
2473  [IKEV2_NEXT_ERROR_DROP] = "error-drop",
2474  },
2475 };
2476 /* *INDENT-ON* */
2477 
2478 // set ikev2 proposals when vpp is used as initiator
2479 static clib_error_t *
2481  ikev2_transforms_set * ts,
2482  ikev2_sa_proposal_t ** proposals, int is_ike)
2483 {
2484  clib_error_t *r;
2485  ikev2_main_t *km = &ikev2_main;
2486  ikev2_sa_proposal_t *proposal;
2487  vec_add2 (*proposals, proposal, 1);
2489  int error;
2490 
2491  /* Encryption */
2492  error = 1;
2494  {
2495  if (td->type == IKEV2_TRANSFORM_TYPE_ENCR
2496  && td->encr_type == ts->crypto_alg
2497  && td->key_len == ts->crypto_key_size / 8)
2498  {
2499  u16 attr[2];
2500  attr[0] = clib_host_to_net_u16 (14 | (1 << 15));
2501  attr[1] = clib_host_to_net_u16 (td->key_len << 3);
2502  vec_add (td->attrs, (u8 *) attr, 4);
2503  vec_add1 (proposal->transforms, *td);
2504  td->attrs = 0;
2505 
2506  error = 0;
2507  break;
2508  }
2509  }
2510  if (error)
2511  {
2512  r = clib_error_return (0, "Unsupported algorithm");
2513  return r;
2514  }
2515 
2516  /* Integrity */
2517  error = 1;
2519  {
2520  if (td->type == IKEV2_TRANSFORM_TYPE_INTEG
2521  && td->integ_type == ts->integ_alg)
2522  {
2523  vec_add1 (proposal->transforms, *td);
2524  error = 0;
2525  break;
2526  }
2527  }
2528  if (error)
2529  {
2530  clib_warning
2531  ("Didn't find any supported algorithm for IKEV2_TRANSFORM_TYPE_INTEG");
2532  r = clib_error_return (0, "Unsupported algorithm");
2533  return r;
2534  }
2535 
2536  /* PRF */
2537  if (is_ike)
2538  {
2539  error = 1;
2541  {
2542  if (td->type == IKEV2_TRANSFORM_TYPE_PRF
2543  && td->prf_type == IKEV2_TRANSFORM_PRF_TYPE_PRF_HMAC_SHA2_256)
2544  {
2545  vec_add1 (proposal->transforms, *td);
2546  error = 0;
2547  break;
2548  }
2549  }
2550  if (error)
2551  {
2552  r = clib_error_return (0, "Unsupported algorithm");
2553  return r;
2554  }
2555  }
2556 
2557  /* DH */
2558  error = 1;
2560  {
2561  if (td->type == IKEV2_TRANSFORM_TYPE_DH && td->dh_type == ts->dh_type)
2562  {
2563  vec_add1 (proposal->transforms, *td);
2564  if (is_ike)
2565  {
2566  sa->dh_group = td->dh_type;
2567  }
2568  error = 0;
2569  break;
2570  }
2571  }
2572  if (error)
2573  {
2574  r = clib_error_return (0, "Unsupported algorithm");
2575  return r;
2576  }
2577 
2578  if (!is_ike)
2579  {
2580  error = 1;
2582  {
2583  if (td->type == IKEV2_TRANSFORM_TYPE_ESN)
2584  {
2585  vec_add1 (proposal->transforms, *td);
2586  error = 0;
2587  break;
2588  }
2589  }
2590  if (error)
2591  {
2592  r = clib_error_return (0, "Unsupported algorithm");
2593  return r;
2594  }
2595  }
2596 
2597 
2598  return 0;
2599 }
2600 
2601 static ikev2_profile_t *
2603 {
2604  ikev2_main_t *km = &ikev2_main;
2605  uword *p;
2606 
2607  p = mhash_get (&km->profile_index_by_name, name);
2608  if (!p)
2609  return 0;
2610 
2611  return pool_elt_at_index (km->profiles, p[0]);
2612 }
2613 
2614 
2615 static void
2617  u32 bi0, u32 len)
2618 {
2619  ip4_header_t *ip40;
2620  udp_header_t *udp0;
2621  vlib_buffer_t *b0;
2622  vlib_frame_t *f;
2623  u32 *to_next;
2624 
2625  b0 = vlib_get_buffer (vm, bi0);
2626  vlib_buffer_advance (b0, -sizeof (udp_header_t));
2627  udp0 = vlib_buffer_get_current (b0);
2628  vlib_buffer_advance (b0, -sizeof (ip4_header_t));
2629  ip40 = vlib_buffer_get_current (b0);
2630 
2631 
2632  ip40->ip_version_and_header_length = 0x45;
2633  ip40->tos = 0;
2634  ip40->fragment_id = 0;
2635  ip40->flags_and_fragment_offset = 0;
2636  ip40->ttl = 0xff;
2637  ip40->protocol = IP_PROTOCOL_UDP;
2638  ip40->dst_address.as_u32 = dst->as_u32;
2639  ip40->src_address.as_u32 = src->as_u32;
2640  udp0->dst_port = clib_host_to_net_u16 (500);
2641  udp0->src_port = clib_host_to_net_u16 (500);
2642  udp0->length = clib_host_to_net_u16 (len + sizeof (udp_header_t));
2643  udp0->checksum = 0;
2644  b0->current_length = len + sizeof (ip4_header_t) + sizeof (udp_header_t);
2645  ip40->length = clib_host_to_net_u16 (b0->current_length);
2646  ip40->checksum = ip4_header_checksum (ip40);
2647 
2648 
2649  /* send the request */
2650  f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
2651  to_next = vlib_frame_vector_args (f);
2652  to_next[0] = bi0;
2653  f->n_vectors = 1;
2654  vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
2655 
2656 }
2657 
2658 static u32
2660 {
2661  u32 bi0;
2662  if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2663  {
2664  *ike = 0;
2665  return 0;
2666  }
2667  vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
2668  *ike = vlib_buffer_get_current (b0);
2669  return bi0;
2670 }
2671 
2672 clib_error_t *
2674 {
2675  ikev2_main_t *km = &ikev2_main;
2676 
2677  km->pkey = ikev2_load_key_file (file);
2678  if (km->pkey == NULL)
2679  return clib_error_return (0, "load key '%s' failed", file);
2680 
2681  return 0;
2682 }
2683 
2684 clib_error_t *
2686 {
2687  ikev2_main_t *km = &ikev2_main;
2688  ikev2_profile_t *p;
2689 
2690  if (is_add)
2691  {
2692  if (ikev2_profile_index_by_name (name))
2693  return clib_error_return (0, "policy %v already exists", name);
2694 
2695  pool_get (km->profiles, p);
2696  clib_memset (p, 0, sizeof (*p));
2697  p->name = vec_dup (name);
2698  p->responder.sw_if_index = ~0;
2699  uword index = p - km->profiles;
2700  mhash_set_mem (&km->profile_index_by_name, name, &index, 0);
2701  }
2702  else
2703  {
2704  p = ikev2_profile_index_by_name (name);
2705  if (!p)
2706  return clib_error_return (0, "policy %v does not exists", name);
2707 
2708  vec_free (p->name);
2709  pool_put (km->profiles, p);
2710  mhash_unset (&km->profile_index_by_name, name, 0);
2711  }
2712  return 0;
2713 }
2714 
2715 clib_error_t *
2717  u8 * auth_data, u8 data_hex_format)
2718 {
2719  ikev2_profile_t *p;
2720  clib_error_t *r;
2721 
2722  p = ikev2_profile_index_by_name (name);
2723 
2724  if (!p)
2725  {
2726  r = clib_error_return (0, "unknown profile %v", name);
2727  return r;
2728  }
2729  vec_free (p->auth.data);
2730  p->auth.method = auth_method;
2731  p->auth.data = vec_dup (auth_data);
2732  p->auth.hex = data_hex_format;
2733 
2734  if (auth_method == IKEV2_AUTH_METHOD_RSA_SIG)
2735  {
2736  vec_add1 (p->auth.data, 0);
2737  if (p->auth.key)
2738  EVP_PKEY_free (p->auth.key);
2739  p->auth.key = ikev2_load_cert_file (auth_data);
2740  if (p->auth.key == NULL)
2741  return clib_error_return (0, "load cert '%s' failed", auth_data);
2742  }
2743 
2744  return 0;
2745 }
2746 
2747 clib_error_t *
2749  int is_local)
2750 {
2751  ikev2_profile_t *p;
2752  clib_error_t *r;
2753 
2754  if (id_type > IKEV2_ID_TYPE_ID_RFC822_ADDR
2755  && id_type < IKEV2_ID_TYPE_ID_KEY_ID)
2756  {
2757  r = clib_error_return (0, "unsupported identity type %U",
2758  format_ikev2_id_type, id_type);
2759  return r;
2760  }
2761 
2762  p = ikev2_profile_index_by_name (name);
2763 
2764  if (!p)
2765  {
2766  r = clib_error_return (0, "unknown profile %v", name);
2767  return r;
2768  }
2769 
2770  if (is_local)
2771  {
2772  vec_free (p->loc_id.data);
2773  p->loc_id.type = id_type;
2774  p->loc_id.data = vec_dup (data);
2775  }
2776  else
2777  {
2778  vec_free (p->rem_id.data);
2779  p->rem_id.type = id_type;
2780  p->rem_id.data = vec_dup (data);
2781  }
2782 
2783  return 0;
2784 }
2785 
2786 clib_error_t *
2788  u16 start_port, u16 end_port, ip4_address_t start_addr,
2789  ip4_address_t end_addr, int is_local)
2790 {
2791  ikev2_profile_t *p;
2792  clib_error_t *r;
2793 
2794  p = ikev2_profile_index_by_name (name);
2795 
2796  if (!p)
2797  {
2798  r = clib_error_return (0, "unknown profile %v", name);
2799  return r;
2800  }
2801 
2802  if (is_local)
2803  {
2804  p->loc_ts.start_addr.as_u32 = start_addr.as_u32;
2805  p->loc_ts.end_addr.as_u32 = end_addr.as_u32;
2806  p->loc_ts.start_port = start_port;
2807  p->loc_ts.end_port = end_port;
2808  p->loc_ts.protocol_id = protocol_id;
2809  p->loc_ts.ts_type = 7;
2810  }
2811  else
2812  {
2813  p->rem_ts.start_addr.as_u32 = start_addr.as_u32;
2814  p->rem_ts.end_addr.as_u32 = end_addr.as_u32;
2815  p->rem_ts.start_port = start_port;
2816  p->rem_ts.end_port = end_port;
2817  p->rem_ts.protocol_id = protocol_id;
2818  p->rem_ts.ts_type = 7;
2819  }
2820 
2821  return 0;
2822 }
2823 
2824 
2825 clib_error_t *
2828 {
2829  ikev2_profile_t *p;
2830  clib_error_t *r;
2831 
2832  p = ikev2_profile_index_by_name (name);
2833 
2834  if (!p)
2835  {
2836  r = clib_error_return (0, "unknown profile %v", name);
2837  return r;
2838  }
2839 
2841  p->responder.ip4 = ip4;
2842 
2843  return 0;
2844 }
2845 
2846 clib_error_t *
2848  ikev2_transform_encr_type_t crypto_alg,
2849  ikev2_transform_integ_type_t integ_alg,
2850  ikev2_transform_dh_type_t dh_type,
2851  u32 crypto_key_size)
2852 {
2853  ikev2_profile_t *p;
2854  clib_error_t *r;
2855 
2856  p = ikev2_profile_index_by_name (name);
2857 
2858  if (!p)
2859  {
2860  r = clib_error_return (0, "unknown profile %v", name);
2861  return r;
2862  }
2863 
2864  p->ike_ts.crypto_alg = crypto_alg;
2865  p->ike_ts.integ_alg = integ_alg;
2866  p->ike_ts.dh_type = dh_type;
2867  p->ike_ts.crypto_key_size = crypto_key_size;
2868  return 0;
2869 }
2870 
2871 clib_error_t *
2873  ikev2_transform_encr_type_t crypto_alg,
2874  ikev2_transform_integ_type_t integ_alg,
2875  ikev2_transform_dh_type_t dh_type,
2876  u32 crypto_key_size)
2877 {
2878  ikev2_profile_t *p;
2879  clib_error_t *r;
2880 
2881  p = ikev2_profile_index_by_name (name);
2882 
2883  if (!p)
2884  {
2885  r = clib_error_return (0, "unknown profile %v", name);
2886  return r;
2887  }
2888 
2889  p->esp_ts.crypto_alg = crypto_alg;
2890  p->esp_ts.integ_alg = integ_alg;
2891  p->esp_ts.dh_type = dh_type;
2892  p->esp_ts.crypto_key_size = crypto_key_size;
2893  return 0;
2894 }
2895 
2896 clib_error_t *
2898  u64 lifetime, u32 jitter, u32 handover,
2899  u64 maxdata)
2900 {
2901  ikev2_profile_t *p;
2902  clib_error_t *r;
2903 
2904  p = ikev2_profile_index_by_name (name);
2905 
2906  if (!p)
2907  {
2908  r = clib_error_return (0, "unknown profile %v", name);
2909  return r;
2910  }
2911 
2912  p->lifetime = lifetime;
2913  p->lifetime_jitter = jitter;
2914  p->handover = handover;
2915  p->lifetime_maxdata = maxdata;
2916  return 0;
2917 }
2918 
2919 clib_error_t *
2921 {
2922  ikev2_profile_t *p;
2923  clib_error_t *r;
2924  ip4_main_t *im = &ip4_main;
2925  ikev2_main_t *km = &ikev2_main;
2926 
2927  p = ikev2_profile_index_by_name (name);
2928 
2929  if (!p)
2930  {
2931  r = clib_error_return (0, "unknown profile %v", name);
2932  return r;
2933  }
2934 
2935  if (p->responder.sw_if_index == ~0 || p->responder.ip4.data_u32 == 0)
2936  {
2937  r = clib_error_return (0, "responder not set for profile %v", name);
2938  return r;
2939  }
2940 
2941 
2942  /* Create the Initiator Request */
2943  {
2944  ike_header_t *ike0;
2945  u32 bi0 = 0;
2946  ip_lookup_main_t *lm = &im->lookup_main;
2947  u32 if_add_index0;
2948  int len = sizeof (ike_header_t);
2949 
2950  /* Get own iface IP */
2951  if_add_index0 =
2953  ip_interface_address_t *if_add =
2954  pool_elt_at_index (lm->if_address_pool, if_add_index0);
2955  ip4_address_t *if_ip = ip_interface_address_get_address (lm, if_add);
2956 
2957  bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
2958 
2959  /* Prepare the SA and the IKE payload */
2960  ikev2_sa_t sa;
2961  clib_memset (&sa, 0, sizeof (ikev2_sa_t));
2962  ikev2_payload_chain_t *chain = 0;
2963  ikev2_payload_new_chain (chain);
2964 
2965  /* Build the IKE proposal payload */
2966  ikev2_sa_proposal_t *proposals = 0;
2967  ikev2_set_initiator_proposals (vm, &sa, &p->ike_ts, &proposals, 1);
2968  proposals[0].proposal_num = 1;
2969  proposals[0].protocol_id = IKEV2_PROTOCOL_IKE;
2970 
2971  /* Add and then cleanup proposal data */
2972  ikev2_payload_add_sa (chain, proposals);
2973  ikev2_sa_free_proposal_vector (&proposals);
2974 
2975  sa.is_initiator = 1;
2976  sa.profile = p;
2979  ikev2_payload_add_ke (chain, sa.dh_group, sa.i_dh_data);
2980  ikev2_payload_add_nonce (chain, sa.i_nonce);
2981 
2982  /* Build the child SA proposal */
2983  vec_resize (sa.childs, 1);
2984  ikev2_set_initiator_proposals (vm, &sa, &p->esp_ts,
2985  &sa.childs[0].i_proposals, 0);
2986  sa.childs[0].i_proposals[0].proposal_num = 1;
2988  RAND_bytes ((u8 *) & sa.childs[0].i_proposals[0].spi,
2989  sizeof (sa.childs[0].i_proposals[0].spi));
2990 
2991 
2992 
2993  /* Add NAT detection notification messages (mandatory) */
2994  u8 nat_detection_source[8 + 8 + 4 + 2];
2995  u8 *nat_detection_sha1 = vec_new (u8, 20);
2996 
2997  u64 tmpspi = clib_host_to_net_u64 (sa.ispi);
2998  clib_memcpy_fast (&nat_detection_source[0], &tmpspi, sizeof (tmpspi));
2999  tmpspi = clib_host_to_net_u64 (sa.rspi);
3000  clib_memcpy_fast (&nat_detection_source[8], &tmpspi, sizeof (tmpspi));
3001  u16 tmpport = clib_host_to_net_u16 (500);
3002  clib_memcpy_fast (&nat_detection_source[8 + 8 + 4], &tmpport,
3003  sizeof (tmpport));
3004  u32 tmpip = clib_host_to_net_u32 (if_ip->as_u32);
3005  clib_memcpy_fast (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
3006  SHA1 (nat_detection_source, sizeof (nat_detection_source),
3007  nat_detection_sha1);
3008  ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
3009  nat_detection_sha1);
3010  tmpip = clib_host_to_net_u32 (p->responder.ip4.as_u32);
3011  clib_memcpy_fast (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
3012  SHA1 (nat_detection_source, sizeof (nat_detection_source),
3013  nat_detection_sha1);
3014  ikev2_payload_add_notify (chain,
3015  IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
3016  nat_detection_sha1);
3017  vec_free (nat_detection_sha1);
3018 
3019  u8 *sig_hash_algo = vec_new (u8, 8);
3020  u64 tmpsig = clib_host_to_net_u64 (0x0001000200030004);
3021  clib_memcpy_fast (sig_hash_algo, &tmpsig, sizeof (tmpsig));
3022  ikev2_payload_add_notify (chain,
3023  IKEV2_NOTIFY_MSG_SIGNATURE_HASH_ALGORITHMS,
3024  sig_hash_algo);
3025  vec_free (sig_hash_algo);
3026 
3027 
3028  /* Buffer update and boilerplate */
3029  len += vec_len (chain->data);
3030  ike0->nextpayload = chain->first_payload_type;
3031  ike0->length = clib_host_to_net_u32 (len);
3032  clib_memcpy_fast (ike0->payload, chain->data, vec_len (chain->data));
3034 
3035  ike0->version = IKE_VERSION_2;
3036  ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3037  ike0->exchange = IKEV2_EXCHANGE_SA_INIT;
3038  ike0->ispi = sa.ispi;
3039 
3040  /* store whole IKE payload - needed for PSK auth */
3042  vec_add (sa.last_sa_init_req_packet_data, ike0, len);
3043 
3044  /* add data to the SA then add it to the pool */
3045  sa.iaddr.as_u32 = if_ip->as_u32;
3046  sa.raddr.as_u32 = p->responder.ip4.as_u32;
3047  sa.i_id.type = p->loc_id.type;
3048  sa.i_id.data = vec_dup (p->loc_id.data);
3049  sa.i_auth.method = p->auth.method;
3050  sa.i_auth.hex = p->auth.hex;
3051  sa.i_auth.data = vec_dup (p->auth.data);
3052 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
3054  EVP_PKEY_size (p->auth.key));
3055 #else
3056  sa.i_auth.key = vec_dup (p->auth.key);
3057 #endif
3058  vec_add (sa.childs[0].tsi, &p->loc_ts, 1);
3059  vec_add (sa.childs[0].tsr, &p->rem_ts, 1);
3060 
3061  /* add SA to the pool */
3062  ikev2_sa_t *sa0 = 0;
3063  pool_get (km->sais, sa0);
3064  clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
3065  hash_set (km->sa_by_ispi, sa0->ispi, sa0 - km->sais);
3066 
3067  ikev2_send_ike (vm, if_ip, &p->responder.ip4, bi0, len);
3068 
3069  }
3070 
3071  return 0;
3072 }
3073 
3074 static void
3076  ikev2_child_sa_t * csa)
3077 {
3078  /* Create the Initiator notification for child SA removal */
3079  ikev2_main_t *km = &ikev2_main;
3080  ike_header_t *ike0;
3081  u32 bi0 = 0;
3082  int len;
3083 
3084  bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3085 
3086 
3087  ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3088  ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3089  ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3090  vec_resize (sa->del, 1);
3092  sa->del->spi = csa->i_proposals->spi;
3093  ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
3094  sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3095  len = ikev2_generate_message (sa, ike0, 0);
3096 
3097  ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
3098 
3099  /* delete local child SA */
3101  ikev2_sa_del_child_sa (sa, csa);
3102 }
3103 
3104 clib_error_t *
3106 {
3107  clib_error_t *r;
3108  ikev2_main_t *km = &ikev2_main;
3110  ikev2_sa_t *fsa = 0;
3111  ikev2_child_sa_t *fchild = 0;
3112 
3113  /* Search for the child SA */
3114  vec_foreach (tkm, km->per_thread_data)
3115  {
3116  ikev2_sa_t *sa;
3117  if (fchild)
3118  break;
3119  /* *INDENT-OFF* */
3120  pool_foreach (sa, tkm->sas, ({
3121  fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
3122  if (fchild)
3123  {
3124  fsa = sa;
3125  break;
3126  }
3127  }));
3128  /* *INDENT-ON* */
3129  }
3130 
3131  if (!fchild || !fsa)
3132  {
3133  r = clib_error_return (0, "Child SA not found");
3134  return r;
3135  }
3136  else
3137  {
3138  ikev2_delete_child_sa_internal (vm, fsa, fchild);
3139  }
3140 
3141  return 0;
3142 }
3143 
3144 clib_error_t *
3146 {
3147  clib_error_t *r;
3148  ikev2_main_t *km = &ikev2_main;
3150  ikev2_sa_t *fsa = 0;
3151  ikev2_main_per_thread_data_t *ftkm = 0;
3152 
3153  /* Search for the IKE SA */
3154  vec_foreach (tkm, km->per_thread_data)
3155  {
3156  ikev2_sa_t *sa;
3157  if (fsa)
3158  break;
3159  /* *INDENT-OFF* */
3160  pool_foreach (sa, tkm->sas, ({
3161  if (sa->ispi == ispi)
3162  {
3163  fsa = sa;
3164  ftkm = tkm;
3165  break;
3166  }
3167  }));
3168  /* *INDENT-ON* */
3169  }
3170 
3171  if (!fsa)
3172  {
3173  r = clib_error_return (0, "IKE SA not found");
3174  return r;
3175  }
3176 
3177 
3178  /* Create the Initiator notification for IKE SA removal */
3179  {
3180  ike_header_t *ike0;
3181  u32 bi0 = 0;
3182  int len;
3183 
3184  bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3185 
3186 
3187  ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3188  ike0->ispi = clib_host_to_net_u64 (fsa->ispi);
3189  ike0->rspi = clib_host_to_net_u64 (fsa->rspi);
3190  vec_resize (fsa->del, 1);
3191  fsa->del->protocol_id = IKEV2_PROTOCOL_IKE;
3192  fsa->del->spi = ispi;
3193  ike0->msgid = clib_host_to_net_u32 (fsa->last_init_msg_id + 1);
3194  fsa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3195  len = ikev2_generate_message (fsa, ike0, 0);
3196 
3197  ikev2_send_ike (vm, &fsa->iaddr, &fsa->raddr, bi0, len);
3198  }
3199 
3200 
3201  /* delete local SA */
3203  vec_foreach (c, fsa->childs)
3204  {
3205  ikev2_delete_tunnel_interface (km->vnet_main, fsa, c);
3206  ikev2_sa_del_child_sa (fsa, c);
3207  }
3208  ikev2_sa_free_all_vec (fsa);
3209  uword *p = hash_get (ftkm->sa_by_rspi, fsa->rspi);
3210  if (p)
3211  {
3212  hash_unset (ftkm->sa_by_rspi, fsa->rspi);
3213  pool_put (ftkm->sas, fsa);
3214  }
3215 
3216 
3217  return 0;
3218 }
3219 
3220 static void
3222  ikev2_child_sa_t * csa)
3223 {
3224  /* Create the Initiator request for create child SA */
3225  ike_header_t *ike0;
3226  u32 bi0 = 0;
3227  int len;
3228 
3229 
3230  bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3231 
3232 
3233  ike0->version = IKE_VERSION_2;
3234  ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3235  ike0->exchange = IKEV2_EXCHANGE_CREATE_CHILD_SA;
3236  ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3237  ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3238  ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
3239  sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3240 
3241  ikev2_rekey_t *rekey;
3242  vec_add2 (sa->rekey, rekey, 1);
3243  ikev2_sa_proposal_t *proposals = vec_dup (csa->i_proposals);
3244 
3245  /*need new ispi */
3246  RAND_bytes ((u8 *) & proposals[0].spi, sizeof (proposals[0].spi));
3247  rekey->spi = proposals[0].spi;
3248  rekey->ispi = csa->i_proposals->spi;
3249  len = ikev2_generate_message (sa, ike0, proposals);
3250  ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
3251  vec_free (proposals);
3252 }
3253 
3254 clib_error_t *
3256 {
3257  clib_error_t *r;
3258  ikev2_main_t *km = &ikev2_main;
3260  ikev2_sa_t *fsa = 0;
3261  ikev2_child_sa_t *fchild = 0;
3262 
3263  /* Search for the child SA */
3264  vec_foreach (tkm, km->per_thread_data)
3265  {
3266  ikev2_sa_t *sa;
3267  if (fchild)
3268  break;
3269  /* *INDENT-OFF* */
3270  pool_foreach (sa, tkm->sas, ({
3271  fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
3272  if (fchild)
3273  {
3274  fsa = sa;
3275  break;
3276  }
3277  }));
3278  /* *INDENT-ON* */
3279  }
3280 
3281  if (!fchild || !fsa)
3282  {
3283  r = clib_error_return (0, "Child SA not found");
3284  return r;
3285  }
3286  else
3287  {
3288  ikev2_rekey_child_sa_internal (vm, fsa, fchild);
3289  }
3290 
3291  return 0;
3292 }
3293 
3294 clib_error_t *
3296 {
3297  ikev2_main_t *km = &ikev2_main;
3298  clib_error_t *error;
3300  int thread_id;
3301 
3302  if ((error = vlib_call_init_function (vm, ipsec_init)))
3303  return error;
3304 
3305  clib_memset (km, 0, sizeof (ikev2_main_t));
3306  km->vnet_main = vnet_get_main ();
3307  km->vlib_main = vm;
3308 
3309  ikev2_crypto_init (km);
3310 
3312 
3313  vec_validate (km->per_thread_data, tm->n_vlib_mains - 1);
3314  for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
3315  {
3316  km->per_thread_data[thread_id].sa_by_rspi =
3317  hash_create (0, sizeof (uword));
3318  }
3319 
3320  km->sa_by_ispi = hash_create (0, sizeof (uword));
3321 
3322 
3323  if ((error = vlib_call_init_function (vm, ikev2_cli_init)))
3324  return error;
3325 
3326  udp_register_dst_port (vm, 500, ikev2_node.index, 1);
3327 
3328  return 0;
3329 }
3330 
3332 
3333 static u8
3335 {
3336  ikev2_main_t *km = &ikev2_main;
3337  vlib_main_t *vm = km->vlib_main;
3338  f64 now = vlib_time_now (vm);
3339  u8 res = 0;
3340 
3341  if (sa->is_initiator && sa->profile && csa->time_to_expiration
3342  && now > csa->time_to_expiration)
3343  {
3344  if (!csa->is_expired || csa->rekey_retries > 0)
3345  {
3346  ikev2_rekey_child_sa_internal (vm, sa, csa);
3347  csa->time_to_expiration = now + sa->profile->handover;
3348  csa->is_expired = 1;
3349  if (csa->rekey_retries == 0)
3350  {
3351  csa->rekey_retries = 5;
3352  }
3353  else if (csa->rekey_retries > 0)
3354  {
3355  csa->rekey_retries--;
3356  clib_warning ("Rekeying Child SA 0x%x, retries left %d",
3357  csa->i_proposals->spi, csa->rekey_retries);
3358  if (csa->rekey_retries == 0)
3359  {
3360  csa->rekey_retries = -1;
3361  }
3362  }
3363  res |= 1;
3364  }
3365  else
3366  {
3367  csa->time_to_expiration = 0;
3368  ikev2_delete_child_sa_internal (vm, sa, csa);
3369  res |= 1;
3370  }
3371  }
3372 
3373  return res;
3374 }
3375 
3376 static void
3378 {
3379  ikev2_main_t *km = &ikev2_main;
3380  vlib_main_t *vm = km->vlib_main;
3382  ikev2_sa_t *fsa = 0;
3383  ikev2_child_sa_t *fchild = 0;
3384  f64 now = vlib_time_now (vm);
3385  vlib_counter_t counts;
3386 
3387  /* Search for the SA and child SA */
3388  vec_foreach (tkm, km->per_thread_data)
3389  {
3390  ikev2_sa_t *sa;
3391  if (fchild)
3392  break;
3393  /* *INDENT-OFF* */
3394  pool_foreach (sa, tkm->sas, ({
3395  fchild = ikev2_sa_get_child(sa, ipsec_sa->spi, IKEV2_PROTOCOL_ESP, 1);
3396  if (fchild)
3397  {
3398  fsa = sa;
3399  break;
3400  }
3401  }));
3402  /* *INDENT-ON* */
3403  }
3405  ipsec_sa->stat_index, &counts);
3406 
3407  if (fchild && fsa && fsa->profile && fsa->profile->lifetime_maxdata)
3408  {
3409  if (!fchild->is_expired
3410  && counts.bytes > fsa->profile->lifetime_maxdata)
3411  {
3412  fchild->time_to_expiration = now;
3413  }
3414  }
3415 }
3416 
3418 
3419 static uword
3421  vlib_frame_t * f)
3422 {
3423  ikev2_main_t *km = &ikev2_main;
3424  ipsec_main_t *im = &ipsec_main;
3425 
3426  while (1)
3427  {
3428  u8 req_sent = 0;
3431 
3432  /* process ike child sas */
3434  vec_foreach (tkm, km->per_thread_data)
3435  {
3436  ikev2_sa_t *sa;
3437  /* *INDENT-OFF* */
3438  pool_foreach (sa, tkm->sas, ({
3439  ikev2_child_sa_t *c;
3440  vec_foreach (c, sa->childs)
3441  {
3442  req_sent |= ikev2_mngr_process_child_sa(sa, c);
3443  }
3444  }));
3445  /* *INDENT-ON* */
3446  }
3447 
3448  /* process ipsec sas */
3449  ipsec_sa_t *sa;
3450  /* *INDENT-OFF* */
3451  pool_foreach (sa, im->sad, ({
3452  ikev2_mngr_process_ipsec_sa(sa);
3453  }));
3454  /* *INDENT-ON* */
3455 
3456  if (req_sent)
3457  {
3460  req_sent = 0;
3461  }
3462 
3463  }
3464  return 0;
3465 }
3466 
3467 /* *INDENT-OFF* */
3469  .function = ikev2_mngr_process_fn,
3470  .type = VLIB_NODE_TYPE_PROCESS,
3471  .name =
3472  "ikev2-manager-process",
3473 };
3474 
3475 VLIB_PLUGIN_REGISTER () = {
3476  .version = VPP_BUILD_VER,
3477  .description = "Internet Key Exchange (IKEv2) Protocol",
3478 };
3479 /* *INDENT-ON* */
3480 
3481 /*
3482  * fd.io coding-style-patch-verification: ON
3483  *
3484  * Local Variables:
3485  * eval: (c-set-style "gnu")
3486  * End:
3487  */
ikev2_main_per_thread_data_t * per_thread_data
Definition: ikev2_priv.h:291
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
static int ikev2_retransmit_sa_init(ike_header_t *ike, ip4_address_t iaddr, ip4_address_t raddr)
Definition: ikev2.c:1950
u32 sw_if_index
Definition: ipsec_gre.api:37
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
u8 * dh_shared_key
Definition: ikev2_priv.h:211
ikev2_sa_t * sais
Definition: ikev2_priv.h:287
void ikev2_payload_add_nonce(ikev2_payload_chain_t *c, u8 *nonce)
static u8 * format_ikev2_trace(u8 *s, va_list *args)
Definition: ikev2.c:47
u8 * dh_private_key
Definition: ikev2_priv.h:212
ikev2_transform_type_t type
Definition: ikev2_priv.h:69
#define hash_set(h, key, value)
Definition: hash.h:255
#define IKEV2_PAYLOAD_NONCE
Definition: ikev2.h:99
clib_error_t * ikev2_set_profile_responder(vlib_main_t *vm, u8 *name, u32 sw_if_index, ip4_address_t ip4)
Definition: ikev2.c:2826
ikev2_id_t r_id
Definition: ikev2_priv.h:235
ikev2_id_type_t type
Definition: ikev2_priv.h:127
#define CLIB_UNUSED(x)
Definition: clib.h:82
void ikev2_payload_add_notify(ikev2_payload_chain_t *c, u16 msg_type, u8 *data)
ikev2_transforms_set ike_ts
Definition: ikev2_priv.h:189
static int ikev2_delete_tunnel_interface(vnet_main_t *vnm, ikev2_sa_t *sa, ikev2_child_sa_t *child)
Definition: ikev2.c:1636
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:673
ikev2_transform_integ_type_t
Definition: ikev2.h:267
#define hash_unset(h, key)
Definition: hash.h:261
a
Definition: bitmap.h:538
static void ikev2_send_ike(vlib_main_t *vm, ip4_address_t *src, ip4_address_t *dst, u32 bi0, u32 len)
Definition: ikev2.c:2616
ip4_address_t src_address
Definition: ip4_packet.h:170
clib_error_t * ikev2_add_del_profile(vlib_main_t *vm, u8 *name, int is_add)
Definition: ikev2.c:2685
EVP_PKEY * pkey
Definition: ikev2_priv.h:280
ip_interface_address_t * if_address_pool
Pool of addresses that are assigned to interfaces.
Definition: lookup.h:125
void ikev2_payload_add_sa(ikev2_payload_chain_t *c, ikev2_sa_proposal_t *proposals)
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
static void ikev2_calc_keys(ikev2_sa_t *sa)
Definition: ikev2.c:434
ip46_address_t local_ip
Definition: ipsec_if.h:47
ikev2_sa_proposal_t * ikev2_parse_sa_payload(ike_payload_header_t *ikep)
u32 last_init_msg_id
Definition: ikev2_priv.h:252
static u32 ikev2_generate_message(ikev2_sa_t *sa, ike_header_t *ike, void *user)
Definition: ikev2.c:1669
ikev2_transform_dh_type_t dh_type
Definition: ikev2_priv.h:120
#define IKEV2_PAYLOAD_NONE
Definition: ikev2.h:93
ikev2_profile_t * profiles
Definition: ikev2_priv.h:271
unsigned long u64
Definition: types.h:89
u8 v8
Definition: ikev2.h:27
clib_error_t * ikev2_initiate_delete_ike_sa(vlib_main_t *vm, u64 ispi)
Definition: ikev2.c:3145
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
ikev2_ts_t * ikev2_parse_ts_payload(ike_payload_header_t *ikep)
#define NULL
Definition: clib.h:58
uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:353
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
v8 * ikev2_calc_prf(ikev2_sa_transform_t *tr, v8 *key, v8 *data)
Definition: ikev2_crypto.c:257
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:255
static void ikev2_complete_sa_data(ikev2_sa_t *sa, ikev2_sa_t *sai)
Definition: ikev2.c:378
static vlib_node_registration_t ikev2_mngr_process_node
(constructor) VLIB_REGISTER_NODE (ikev2_mngr_process_node)
Definition: ikev2.c:3417
#define IKEV2_EXCHANGE_SA_INIT
Definition: ikev2.h:82
static void ikev2_delete_child_sa_internal(vlib_main_t *vm, ikev2_sa_t *sa, ikev2_child_sa_t *csa)
Definition: ikev2.c:3075
ikev2_transform_esn_type_t esn_type
Definition: ikev2_priv.h:77
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
VLIB_PLUGIN_REGISTER()
#define IKEV2_PAYLOAD_VENDOR
Definition: ikev2.h:102
ikev2_state_t state
Definition: ikev2_priv.h:199
#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:214
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
ikev2_transform_encr_type_t crypto_alg
Definition: ikev2_priv.h:118
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:560
int i
u8 * sk_pi
Definition: ikev2_priv.h:226
static void mhash_init_vec_string(mhash_t *h, uword n_value_bytes)
Definition: mhash.h:84
ip_lookup_main_t lookup_main
Definition: ip4.h:98
ip4_address_t ip4
Definition: ikev2_priv.h:113
clib_error_t * ikev2_set_profile_sa_lifetime(vlib_main_t *vm, u8 *name, u64 lifetime, u32 jitter, u32 handover, u64 maxdata)
Definition: ikev2.c:2897
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
u16 flags_and_fragment_offset
Definition: ip4_packet.h:151
void ikev2_payload_add_ke(ikev2_payload_chain_t *c, u16 dh_group, u8 *dh_data)
ikev2_next_t
Definition: ikev2.c:82
u8 data[128]
Definition: ipsec.api:248
static void ikev2_generate_sa_init_data(ikev2_sa_t *sa)
Definition: ikev2.c:325
#define IKEV2_NONCE_SIZE
Definition: ikev2.h:23
u8 initial_contact
Definition: ikev2_priv.h:201
ikev2_ts_t * tsi
Definition: ikev2_priv.h:166
vl_api_ip4_address_t dst
Definition: ipsec_gre.api:39
#define IKEV2_PAYLOAD_TSR
Definition: ikev2.h:104
ikev2_auth_t r_auth
Definition: ikev2_priv.h:231
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
static void ikev2_delete_sa(ikev2_sa_t *sa)
Definition: ikev2.c:308
u8 * last_sa_init_res_packet_data
Definition: ikev2_priv.h:245
unsigned char u8
Definition: types.h:56
ikev2_notify_t * ikev2_parse_notify_payload(ike_payload_header_t *ikep)
ikev2_profile_t * profile
Definition: ikev2_priv.h:253
ikev2_sa_transform_t * ikev2_sa_get_td_for_type(ikev2_sa_proposal_t *p, ikev2_transform_type_t type)
Definition: ikev2.c:194
ikev2_auth_t auth
Definition: ikev2_priv.h:183
double f64
Definition: types.h:142
ikev2_ts_t * tsr
Definition: ikev2_priv.h:167
vlib_node_registration_t ip4_lookup_node
(constructor) VLIB_REGISTER_NODE (ip4_lookup_node)
Definition: ip4_forward.c:104
static void ikev2_mngr_process_ipsec_sa(ipsec_sa_t *ipsec_sa)
Definition: ikev2.c:3377
ikev2_id_t rem_id
Definition: ikev2_priv.h:185
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:598
ikev2_transform_dh_type_t
Definition: ikev2.h:317
int ikev2_encrypt_data(ikev2_sa_t *sa, v8 *src, u8 *dst)
Definition: ikev2_crypto.c:421
ikev2_child_sa_t * ikev2_sa_get_child(ikev2_sa_t *sa, u32 spi, ikev2_protocol_id_t prot_id, int by_initiator)
Definition: ikev2.c:211
format_function_t format_ip4_address
Definition: format.h:75
u32 next_index
Definition: ikev2.c:42
u32 last_msg_id
Definition: ikev2_priv.h:248
#define IKEV2_PAYLOAD_DELETE
Definition: ikev2.h:101
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
ikev2_sa_proposal_t * r_proposals
Definition: ikev2_priv.h:135
void ikev2_generate_dh(ikev2_sa_t *sa, ikev2_sa_transform_t *t)
Definition: ikev2_crypto.c:457
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:516
ipsec_main_t ipsec_main
Definition: ipsec.c:28
EVP_PKEY * ikev2_load_cert_file(u8 *file)
Definition: ikev2_crypto.c:759
clib_error_t * ikev2_initiate_sa_init(vlib_main_t *vm, u8 *name)
Definition: ikev2.c:2920
static ikev2_sa_transform_t * ikev2_find_transform_data(ikev2_sa_transform_t *t)
Definition: ikev2.c:90
ip4_address_t dst_address
Definition: ip4_packet.h:170
#define vec_new(T, N)
Create new vector of given type and length (unspecified alignment, no header).
Definition: vec.h:311
EVP_PKEY * ikev2_load_key_file(u8 *file)
Definition: ikev2_crypto.c:789
clib_error_t * ikev2_set_profile_auth(vlib_main_t *vm, u8 *name, u8 auth_method, u8 *auth_data, u8 data_hex_format)
Definition: ikev2.c:2716
void ikev2_parse_vendor_payload(ike_payload_header_t *ikep)
ip4_address_t start_addr
Definition: ikev2_priv.h:106
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:84
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:187
#define clib_error_return(e, args...)
Definition: error.h:99
static u32 ikev2_get_new_ike_header_buff(vlib_main_t *vm, ike_header_t **ike)
Definition: ikev2.c:2659
void ikev2_payload_add_id(ikev2_payload_chain_t *c, ikev2_id_t *id, u8 type)
#define IKEV2_PAYLOAD_NOTIFY
Definition: ikev2.h:100
static void ikev2_sa_match_ts(ikev2_sa_t *sa)
Definition: ikev2.c:1244
ikev2_sa_proposal_t * i_proposals
Definition: ikev2_priv.h:217
clib_error_t * ikev2_set_profile_esp_transforms(vlib_main_t *vm, u8 *name, ikev2_transform_encr_type_t crypto_alg, ikev2_transform_integ_type_t integ_alg, ikev2_transform_dh_type_t dh_type, u32 crypto_key_size)
Definition: ikev2.c:2872
static void ikev2_sa_auth(ikev2_sa_t *sa)
Definition: ikev2.c:1311
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:242
ikev2_transform_integ_type_t integ_alg
Definition: ikev2_priv.h:119
unsigned int u32
Definition: types.h:88
ikev2_auth_t i_auth
Definition: ikev2_priv.h:230
#define vlib_call_init_function(vm, x)
Definition: init.h:260
#define ikev2_set_state(sa, v)
Definition: ikev2.c:35
#define ikev2_payload_destroy_chain(V)
Definition: ikev2_priv.h:328
ikev2_id_t loc_id
Definition: ikev2_priv.h:184
ikev2_sa_transform_t * transforms
Definition: ikev2_priv.h:96
#define IKEV2_EXCHANGE_CREATE_CHILD_SA
Definition: ikev2.h:84
u8 * sk_ar
Definition: ikev2_priv.h:223
u8 * r_dh_data
Definition: ikev2_priv.h:214
static void ikev2_initial_contact_cleanup(ikev2_sa_t *sa)
Definition: ikev2.c:778
ikev2_responder_t responder
Definition: ikev2_priv.h:188
static ikev2_profile_t * ikev2_profile_index_by_name(u8 *name)
Definition: ikev2.c:2602
#define hash_get(h, key)
Definition: hash.h:249
clib_error_t * ikev2_set_profile_id(vlib_main_t *vm, u8 *name, u8 id_type, u8 *data, int is_local)
Definition: ikev2.c:2748
u8 * ikev2_calc_prfplus(ikev2_sa_transform_t *tr, u8 *key, u8 *seed, int len)
Definition: ikev2_crypto.c:286
static clib_error_t * ikev2_set_initiator_proposals(vlib_main_t *vm, ikev2_sa_t *sa, ikev2_transforms_set *ts, ikev2_sa_proposal_t **proposals, int is_ike)
Definition: ikev2.c:2480
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
u32 sa_id
Definition: ipsec.api:94
ikev2_main_t ikev2_main
Definition: ikev2.c:29
vlib_main_t * vlib_main
Definition: vnet.h:80
u8 * last_sa_init_req_packet_data
Definition: ikev2_priv.h:244
#define IKEV2_PAYLOAD_IDR
Definition: ikev2.h:97
static int ikev2_create_tunnel_interface(vnet_main_t *vnm, ikev2_sa_t *sa, ikev2_child_sa_t *child)
Definition: ikev2.c:1478
#define IKEV2_PAYLOAD_SA
Definition: ikev2.h:94
ikev2_ts_t rem_ts
Definition: ikev2_priv.h:187
u8 * i_dh_data
Definition: ikev2_priv.h:213
uword mhash_set_mem(mhash_t *h, void *key, uword *new_value, uword *old_value)
Definition: mhash.c:271
unsigned short u16
Definition: types.h:57
ikev2_sa_proposal_t * i_proposals
Definition: ikev2_priv.h:134
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:196
u8 * r_nonce
Definition: ikev2_priv.h:207
#define IKEV2_HDR_FLAG_RESPONSE
Definition: ikev2.h:89
ip46_address_t remote_ip
Definition: ipsec_if.h:47
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
mhash_t profile_index_by_name
Definition: ikev2_priv.h:277
u16 end_port
Definition: ikev2_priv.h:105
ikev2_sa_transform_t * supported_transforms
Definition: ikev2_priv.h:274
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
ikev2_rekey_t * rekey
Definition: ikev2_priv.h:241
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:375
clib_error_t * ikev2_initiate_delete_child_sa(vlib_main_t *vm, u32 ispi)
Definition: ikev2.c:3105
static void ikev2_rekey_child_sa_internal(vlib_main_t *vm, ikev2_sa_t *sa, ikev2_child_sa_t *csa)
Definition: ikev2.c:3221
#define PREDICT_FALSE(x)
Definition: clib.h:111
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:804
#define IKEV2_PAYLOAD_FLAG_CRITICAL
Definition: ikev2.h:91
static void ikev2_sa_auth_init(ikev2_sa_t *sa)
Definition: ikev2.c:1434
ikev2_protocol_id_t
Definition: ikev2.h:107
vl_api_ip4_address_t src
Definition: ipsec_gre.api:38
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:338
ip4_address_t end_addr
Definition: ikev2_priv.h:107
u8 name[64]
Definition: memclnt.api:152
ip4_address_t iaddr
Definition: ikev2_priv.h:202
u8 * i_nonce
Definition: ikev2_priv.h:206
clib_error_t * ikev2_set_local_key(vlib_main_t *vm, u8 *file)
Definition: ikev2.c:2673
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
clib_error_t * ikev2_set_profile_ts(vlib_main_t *vm, u8 *name, u8 protocol_id, u16 start_port, u16 end_port, ip4_address_t start_addr, ip4_address_t end_addr, int is_local)
Definition: ikev2.c:2787
u8 len
Definition: ip_types.api:49
u8 * sk_ei
Definition: ikev2_priv.h:224
#define IKEV2_EXCHANGE_INFORMATIONAL
Definition: ikev2.h:85
void ikev2_payload_add_delete(ikev2_payload_chain_t *c, ikev2_delete_t *d)
#define IKEV2_HDR_FLAG_INITIATOR
Definition: ikev2.h:87
#define IKEV2_KEY_PAD
Definition: ikev2.h:25
static uword ikev2_mngr_process_fn(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: ikev2.c:3420
clib_error_t * ikev2_initiate_rekey_child_sa(vlib_main_t *vm, u32 ispi)
Definition: ikev2.c:3255
ipsec_crypto_alg_t crypto_alg
Definition: ipsec_if.h:50
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
ikev2_transform_dh_type_t dh_type
Definition: ikev2_priv.h:76
svmdb_client_t * c
u16 n_vectors
Definition: node.h:395
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:212
static void vlib_get_combined_counter(const vlib_combined_counter_main_t *cm, u32 index, vlib_counter_t *result)
Get the value of a combined counter, never called in the speed path Scrapes the entire set of per-thr...
Definition: counter.h:259
vlib_main_t * vm
Definition: buffer.c:312
static u8 * ikev2_decrypt_sk_payload(ikev2_sa_t *sa, ike_header_t *ike, u8 *payload)
Definition: ikev2.c:713
static void ikev2_sa_del_child_sa(ikev2_sa_t *sa, ikev2_child_sa_t *child)
Definition: ikev2.c:264
ikev2_auth_method_t method
Definition: ikev2_priv.h:55
ikev2_transform_encr_type_t
Definition: ikev2.h:226
ikev2_delete_t * del
Definition: ikev2_priv.h:238
ikev2_ts_t * tsi
Definition: ikev2_priv.h:138
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static void ikev2_process_auth_req(vlib_main_t *vm, ikev2_sa_t *sa, ike_header_t *ike)
Definition: ikev2.c:817
static uword ikev2_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ikev2.c:2064
ip4_address_t raddr
Definition: ikev2_priv.h:203
#define clib_warning(format, args...)
Definition: error.h:59
u8 * sk_er
Definition: ikev2_priv.h:225
static void ikev2_process_sa_init_resp(vlib_main_t *vm, ikev2_sa_t *sa, ike_header_t *ike)
Definition: ikev2.c:633
u8 is_initiator
Definition: ikev2_priv.h:251
#define ARRAY_LEN(x)
Definition: clib.h:62
#define IKEV2_PAYLOAD_KE
Definition: ikev2.h:95
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:458
ikev2_ts_t loc_ts
Definition: ikev2_priv.h:186
ikev2_sa_proposal_t * r_proposals
Definition: ikev2_priv.h:218
static u8 * ikev2_sa_generate_authmsg(ikev2_sa_t *sa, int is_responder)
Definition: ikev2.c:1189
static void ikev2_calc_child_keys(ikev2_sa_t *sa, ikev2_child_sa_t *child)
Definition: ikev2.c:511
static clib_error_t * ipsec_init(vlib_main_t *vm)
Definition: ipsec.c:244
u32 * if_address_pool_index_by_sw_if_index
Head of doubly linked list of interface addresses for each software interface.
Definition: lookup.h:132
#define hash_create(elts, value_bytes)
Definition: hash.h:696
void ikev2_payload_add_auth(ikev2_payload_chain_t *c, ikev2_auth_t *auth)
ikev2_protocol_id_t protocol_id
Definition: ikev2_priv.h:94
vlib_combined_counter_main_t ipsec_sa_counters
SA packet & bytes counters.
Definition: ipsec_sa.c:25
u8 protocol_id
Definition: ikev2_priv.h:102
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:513
#define ASSERT(truth)
u32 spi
Definition: ipsec.api:270
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
vnet_main_t * vnet_main
Definition: ikev2_priv.h:284
u8 is_add
Definition: ipsec_gre.api:36
static void ikev2_process_informational_req(vlib_main_t *vm, ikev2_sa_t *sa, ike_header_t *ike)
Definition: ikev2.c:977
ipsec_integ_alg_t integ_alg
Definition: ipsec_if.h:55
void ikev2_sa_free_proposal_vector(ikev2_sa_proposal_t **v)
Definition: ikev2.c:227
ipsec_sa_t * sad
Definition: ipsec.h:95
#define IKEV2_PAYLOAD_AUTH
Definition: ikev2.h:98
IPv4 main type.
Definition: ip4.h:96
#define ikev2_payload_new_chain(V)
Definition: ikev2_priv.h:327
#define IKEV2_PAYLOAD_SK
Definition: ikev2.h:105
ikev2_sa_proposal_t * r_proposal
Definition: ikev2_priv.h:165
u8 * sk_ai
Definition: ikev2_priv.h:222
#define IKE_VERSION_2
Definition: ikev2.h:80
void ikev2_complete_dh(ikev2_sa_t *sa, ikev2_sa_transform_t *t)
Definition: ikev2_crypto.c:601
#define vec_append(v1, v2)
Append v2 after v1.
Definition: vec.h:818
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:248
static vlib_node_registration_t ikev2_node
(constructor) VLIB_REGISTER_NODE (ikev2_node)
Definition: ikev2.c:58
u16 dh_group
Definition: ikev2_priv.h:210
static u8 plaintext[]
Definition: aes_cbc.c:29
ikev2_sa_proposal_t * i_proposal
Definition: ikev2_priv.h:164
#define IKEV2_PAYLOAD_TSI
Definition: ikev2.h:103
EVP_PKEY * key
Definition: ikev2_priv.h:58
static int ikev2_retransmit_resp(ikev2_sa_t *sa, ike_header_t *ike)
Definition: ikev2.c:2020
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:57
ikev2_transform_encr_type_t encr_type
Definition: ikev2_priv.h:73
struct _vlib_node_registration vlib_node_registration_t
u8 * last_res_packet_data
Definition: ikev2_priv.h:249
ikev2_transform_integ_type_t integ_type
Definition: ikev2_priv.h:75
static char * ikev2_error_strings[]
Definition: ikev2.c:76
static void ikev2_process_sa_init_req(vlib_main_t *vm, ikev2_sa_t *sa, ike_header_t *ike)
Definition: ikev2.c:558
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u16 start_port
Definition: ikev2_priv.h:104
void ikev2_payload_chain_add_padding(ikev2_payload_chain_t *c, int bs)
u8 * sk_pr
Definition: ikev2_priv.h:227
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
#define IKEV2_PAYLOAD_IDI
Definition: ikev2.h:96
clib_error_t * ikev2_set_profile_ike_transforms(vlib_main_t *vm, u8 *name, ikev2_transform_encr_type_t crypto_alg, ikev2_transform_integ_type_t integ_alg, ikev2_transform_dh_type_t dh_type, u32 crypto_key_size)
Definition: ikev2.c:2847
ikev2_id_t i_id
Definition: ikev2_priv.h:234
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
typedef key
Definition: ipsec.api:244
static void ikev2_process_create_child_sa_req(vlib_main_t *vm, ikev2_sa_t *sa, ike_header_t *ike)
Definition: ikev2.c:1048
ikev2_ts_t * tsr
Definition: ikev2_priv.h:139
ikev2_child_sa_t * childs
Definition: ikev2_priv.h:255
ikev2_transform_prf_type_t prf_type
Definition: ikev2_priv.h:74
Linear Congruential Random Number Generator.
#define IKEV2_EXCHANGE_IKE_AUTH
Definition: ikev2.h:83
static int ikev2_ts_cmp(ikev2_ts_t *ts1, ikev2_ts_t *ts2)
Definition: ikev2.c:1232
#define vnet_buffer(b)
Definition: buffer.h:369
clib_error_t * ikev2_init(vlib_main_t *vm)
Definition: ikev2.c:3295
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
ikev2_error_t
Definition: ikev2.c:68
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:905
clib_error_t * ikev2_cli_init(vlib_main_t *vm)
Definition: ikev2_cli.c:589
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
static u8 ikev2_mngr_process_child_sa(ikev2_sa_t *sa, ikev2_child_sa_t *csa)
Definition: ikev2.c:3334
uword * sa_by_ispi
Definition: ikev2_priv.h:289
#define vec_foreach(var, vec)
Vector iterator.
u8 unsupported_cp
Definition: ikev2_priv.h:200
static void ikev2_sa_free_all_child_sa(ikev2_child_sa_t **childs)
Definition: ikev2.c:247
u16 flags
Copy of main node flags.
Definition: node.h:507
static ikev2_sa_proposal_t * ikev2_select_proposal(ikev2_sa_proposal_t *proposals, ikev2_protocol_id_t prot_id)
Definition: ikev2.c:118
u32 id
Definition: udp.api:45
void udp_register_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u32 node_index, u8 is_ip4)
Definition: udp_local.c:484
u8 ip_version_and_header_length
Definition: ip4_packet.h:138
static void * ip_interface_address_get_address(ip_lookup_main_t *lm, ip_interface_address_t *a)
Definition: lookup.h:175
ikev2_transform_type_t
Definition: ikev2.h:203
ikev2_transforms_set esp_ts
Definition: ikev2_priv.h:190
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:301
void ikev2_crypto_init(ikev2_main_t *km)
Definition: ikev2_crypto.c:811
void ikev2_payload_add_ts(ikev2_payload_chain_t *c, ikev2_ts_t *ts, u8 type)
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:612
vlib_main_t * vlib_main
Definition: ikev2_priv.h:283
v8 * ikev2_decrypt_data(ikev2_sa_t *sa, u8 *data, int len)
Definition: ikev2_crypto.c:371
static void ikev2_sa_free_all_vec(ikev2_sa_t *sa)
Definition: ikev2.c:277
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:247
u8 * ikev2_calc_sign(EVP_PKEY *pkey, u8 *data)
Definition: ikev2_crypto.c:728
v8 * ikev2_calc_integr(ikev2_sa_transform_t *tr, v8 *key, u8 *data, int len)
Definition: ikev2_crypto.c:328
u8 * format_ikev2_id_type(u8 *s, va_list *args)
Definition: defs.h:46
#define foreach_ikev2_error
Definition: ikev2.c:60
static openssl_per_thread_data_t * per_thread_data
Definition: main.c:37
u32 sw_if_index
Definition: ikev2.c:43
void ikev2_payload_add_notify_2(ikev2_payload_chain_t *c, u16 msg_type, u8 *data, ikev2_notify_t *notify)
ikev2_delete_t * ikev2_parse_delete_payload(ike_payload_header_t *ikep)