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