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