FD.io VPP  v21.01.1
Vector Packet Processing
ikev2.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <vlib/vlib.h>
17 #include <vlib/unix/plugin.h>
18 #include <vlibmemory/api.h>
19 #include <vpp/app/version.h>
20 #include <vnet/vnet.h>
21 #include <vppinfra/error.h>
22 #include <vppinfra/random.h>
23 #include <vnet/udp/udp.h>
24 #include <vnet/ipsec/ipsec.h>
25 #include <vnet/ipsec/ipsec_tun.h>
26 #include <vnet/ipip/ipip.h>
27 #include <plugins/ikev2/ikev2.h>
29 #include <openssl/sha.h>
30 #include <vnet/ipsec/ipsec_punt.h>
31 
32 #define IKEV2_LIVENESS_RETRIES 3
33 #define IKEV2_LIVENESS_PERIOD_CHECK 30
34 
36 
38  ikev2_sa_t * sa,
39  ikev2_child_sa_t * child);
40 
41 #define ikev2_set_state(sa, v, ...) do { \
42  (sa)->state = v; \
43  ikev2_elog_sa_state("ispi %lx SA state changed to " #v __VA_ARGS__, sa->ispi); \
44  } while(0);
45 
46 typedef struct
47 {
51 
52 static u8 *
53 format_ikev2_trace (u8 * s, va_list * args)
54 {
55  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
56  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
57  ikev2_trace_t *t = va_arg (*args, ikev2_trace_t *);
58 
59  s = format (s, "ikev2: sw_if_index %d, next index %d",
60  t->sw_if_index, t->next_index);
61  return s;
62 }
63 
64 #define IKEV2_GENERATE_SA_INIT_OK_str ""
65 #define IKEV2_GENERATE_SA_INIT_OK_ERR_NO_DH_STR \
66  "no DH group configured for IKE proposals!"
67 #define IKEV2_GENERATE_SA_INIT_OK_ERR_UNSUPP_STR \
68  "DH group not supported!"
69 
70 typedef enum
71 {
76 
77 static u8 *
78 format_ikev2_gen_sa_error (u8 * s, va_list * args)
79 {
81  switch (e)
82  {
84  break;
87  break;
90  break;
91  }
92  return s;
93 }
94 
95 #define foreach_ikev2_error \
96 _(PROCESSED, "IKEv2 packets processed") \
97 _(IKE_SA_INIT_RETRANSMIT, "IKE_SA_INIT retransmit ") \
98 _(IKE_SA_INIT_IGNORE, "IKE_SA_INIT ignore (IKE SA already auth)") \
99 _(IKE_REQ_RETRANSMIT, "IKE request retransmit") \
100 _(IKE_REQ_IGNORE, "IKE request ignore (old msgid)") \
101 _(NOT_IKEV2, "Non IKEv2 packets received") \
102 _(BAD_LENGTH, "Bad packet length") \
103 _(MALFORMED_PACKET, "Malformed packet") \
104 _(NO_BUFF_SPACE, "No buffer space")
105 
106 typedef enum
107 {
108 #define _(sym,str) IKEV2_ERROR_##sym,
110 #undef _
112 } ikev2_error_t;
113 
114 static char *ikev2_error_strings[] = {
115 #define _(sym,string) string,
117 #undef _
118 };
119 
120 typedef enum
121 {
126 
127 typedef enum
128 {
133 
135 
138 {
140 }
141 
143 ikev2_insert_non_esp_marker (ike_header_t * ike, int len)
144 {
145  memmove ((u8 *) ike + sizeof (ikev2_non_esp_marker), ike, len);
146  clib_memset (ike, 0, sizeof (ikev2_non_esp_marker));
147  return len + sizeof (ikev2_non_esp_marker);
148 }
149 
150 static ikev2_sa_transform_t *
152 {
153  ikev2_main_t *km = &ikev2_main;
155 
157  {
158  if (td->type != t->type)
159  continue;
160 
161  if (td->transform_id != t->transform_id)
162  continue;
163 
164  if (td->type == IKEV2_TRANSFORM_TYPE_ENCR)
165  {
166  if (vec_len (t->attrs) != 4 || t->attrs[0] != 0x80
167  || t->attrs[1] != 14)
168  continue;
169 
170  if (((t->attrs[2] << 8 | t->attrs[3]) / 8) != td->key_len)
171  continue;
172  }
173  return td;
174  }
175  return 0;
176 }
177 
178 static ikev2_sa_proposal_t *
180  ikev2_protocol_id_t prot_id)
181 {
182  ikev2_sa_proposal_t *rv = 0;
183  ikev2_sa_proposal_t *proposal;
184  ikev2_sa_transform_t *transform, *new_t;
185  u8 mandatory_bitmap, optional_bitmap;
186 
187  if (prot_id == IKEV2_PROTOCOL_IKE)
188  {
189  mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
190  (1 << IKEV2_TRANSFORM_TYPE_PRF) | (1 << IKEV2_TRANSFORM_TYPE_DH);
191  optional_bitmap = mandatory_bitmap | (1 << IKEV2_TRANSFORM_TYPE_INTEG);
192  }
193  else if (prot_id == IKEV2_PROTOCOL_ESP)
194  {
195  mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
196  (1 << IKEV2_TRANSFORM_TYPE_ESN);
197  optional_bitmap = mandatory_bitmap |
198  (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
199  }
200  else if (prot_id == IKEV2_PROTOCOL_AH)
201  {
202  mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_INTEG) |
203  (1 << IKEV2_TRANSFORM_TYPE_ESN);
204  optional_bitmap = mandatory_bitmap | (1 << IKEV2_TRANSFORM_TYPE_DH);
205  }
206  else
207  return 0;
208 
209  vec_add2 (rv, proposal, 1);
210 
211  vec_foreach (proposal, proposals)
212  {
213  u8 bitmap = 0;
214  if (proposal->protocol_id != prot_id)
215  continue;
216 
217  vec_foreach (transform, proposal->transforms)
218  {
219  if ((1 << transform->type) & bitmap)
220  continue;
221 
222  if (ikev2_find_transform_data (transform))
223  {
224  bitmap |= 1 << transform->type;
225  vec_add2 (rv->transforms, new_t, 1);
226  clib_memcpy_fast (new_t, transform, sizeof (*new_t));
227  new_t->attrs = vec_dup (transform->attrs);
228  }
229  }
230 
231  if ((bitmap & mandatory_bitmap) == mandatory_bitmap &&
232  (bitmap & ~optional_bitmap) == 0)
233  {
234  rv->proposal_num = proposal->proposal_num;
235  rv->protocol_id = proposal->protocol_id;
236  RAND_bytes ((u8 *) & rv->spi, sizeof (rv->spi));
237  goto done;
238  }
239  else
240  {
241  vec_free (rv->transforms);
242  }
243  }
244 
245  vec_free (rv);
246 done:
247  return rv;
248 }
249 
253 {
255 
256  if (!p)
257  return 0;
258 
259  vec_foreach (t, p->transforms)
260  {
261  if (t->type == type)
262  return ikev2_find_transform_data (t);
263  }
264  return 0;
265 }
266 
269  int by_initiator)
270 {
272  vec_foreach (c, sa->childs)
273  {
274  ikev2_sa_proposal_t *proposal =
275  by_initiator ? &c->i_proposals[0] : &c->r_proposals[0];
276  if (proposal && proposal->spi == spi && proposal->protocol_id == prot_id)
277  return c;
278  }
279 
280  return 0;
281 }
282 
283 void
285 {
288 
289  if (!*v)
290  return;
291 
292  vec_foreach (p, *v)
293  {
294  vec_foreach (t, p->transforms)
295  {
296  vec_free (t->attrs);
297  }
298  vec_free (p->transforms);
299  }
300  vec_free (*v);
301 }
302 
303 static void
305 {
308  vec_free (c->sk_ai);
309  vec_free (c->sk_ar);
310  vec_free (c->sk_ei);
311  vec_free (c->sk_er);
312  vec_free (c->tsi);
313  vec_free (c->tsr);
314 }
315 
316 static void
318 {
320  vec_foreach (c, *childs) ikev2_sa_free_child_sa (c);
321 
322  vec_free (*childs);
323 }
324 
325 static void
327 {
328  ikev2_sa_free_child_sa (child);
329  vec_del1 (sa->childs, child - sa->childs);
330 }
331 
332 static void
334 {
335  vec_free (sa->i_nonce);
336  vec_free (sa->r_nonce);
337 
338  vec_free (sa->dh_shared_key);
339  vec_free (sa->dh_private_key);
340  vec_free (sa->i_dh_data);
341  vec_free (sa->r_dh_data);
342 
345 
346  vec_free (sa->sk_d);
347  vec_free (sa->sk_ai);
348  vec_free (sa->sk_ar);
349  vec_free (sa->sk_ei);
350  vec_free (sa->sk_er);
351  vec_free (sa->sk_pi);
352  vec_free (sa->sk_pr);
353 
354  vec_free (sa->i_id.data);
355  vec_free (sa->r_id.data);
356 
357  vec_free (sa->i_auth.data);
358  if (sa->i_auth.key)
359  EVP_PKEY_free (sa->i_auth.key);
360  vec_free (sa->r_auth.data);
361  if (sa->r_auth.key)
362  EVP_PKEY_free (sa->r_auth.key);
363 
364  vec_free (sa->del);
365 
366  vec_free (sa->rekey);
367 
370 
372 
374 }
375 
376 static void
378 {
379  uword *p;
380 
382 
383  p = hash_get (ptd->sa_by_rspi, sa->rspi);
384  if (p)
385  {
386  hash_unset (ptd->sa_by_rspi, sa->rspi);
387  pool_put (ptd->sas, sa);
388  }
389 }
390 
393 {
394  ikev2_sa_transform_t *t = 0, *t2;
395  ikev2_main_t *km = &ikev2_main;
396 
397  if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
399 
400  /* check if received DH group is on our list of supported groups */
402  {
403  if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
404  {
405  t = t2;
406  break;
407  }
408  }
409 
410  if (!t)
411  {
412  sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
414  }
415 
416  if (sa->is_initiator)
417  {
418  /* generate rspi */
419  RAND_bytes ((u8 *) & sa->ispi, 8);
420 
421  /* generate nonce */
423  RAND_bytes ((u8 *) sa->i_nonce, IKEV2_NONCE_SIZE);
424  }
425  else
426  {
427  /* generate rspi */
428  RAND_bytes ((u8 *) & sa->rspi, 8);
429 
430  /* generate nonce */
432  RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
433  }
434 
435  /* generate dh keys */
436  ikev2_generate_dh (sa, t);
437 
439 }
440 
441 static void
443 {
444  ikev2_sa_transform_t *t = 0, *t2;
445  ikev2_main_t *km = &ikev2_main;
446 
447  /*move some data to the new SA */
448 #define _(A) ({void* __tmp__ = (A); (A) = 0; __tmp__;})
449  sa->i_nonce = _(sai->i_nonce);
450  sa->i_dh_data = _(sai->i_dh_data);
451  sa->dh_private_key = _(sai->dh_private_key);
452  ip_address_copy (&sa->iaddr, &sai->iaddr);
453  ip_address_copy (&sa->raddr, &sai->raddr);
454  sa->is_initiator = sai->is_initiator;
455  sa->i_id.type = sai->i_id.type;
456  sa->r_id.type = sai->r_id.type;
457  sa->profile_index = sai->profile_index;
458  sa->tun_itf = sai->tun_itf;
459  sa->is_tun_itf_set = sai->is_tun_itf_set;
460  if (sai->natt_state == IKEV2_NATT_DISABLED)
462  sa->i_id.data = _(sai->i_id.data);
463  sa->r_id.data = _(sai->r_id.data);
464  sa->i_auth.method = sai->i_auth.method;
465  sa->i_auth.hex = sai->i_auth.hex;
466  sa->i_auth.data = _(sai->i_auth.data);
467  sa->i_auth.key = _(sai->i_auth.key);
470  sa->childs = _(sai->childs);
471  sa->udp_encap = sai->udp_encap;
473  sa->dst_port = sai->dst_port;
474  sa->sw_if_index = sai->sw_if_index;
475 #undef _
476 
477 
478  if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
479  {
480  return;
481  }
482 
483  /* check if received DH group is on our list of supported groups */
485  {
486  if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
487  {
488  t = t2;
489  break;
490  }
491  }
492 
493  if (!t)
494  {
495  sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
496  return;
497  }
498 
499 
500  /* generate dh keys */
501  ikev2_complete_dh (sa, t);
502 
503 }
504 
505 static void
507 {
508  u8 *tmp;
509  /* calculate SKEYSEED = prf(Ni | Nr, g^ir) */
510  u8 *skeyseed = 0;
511  u8 *s = 0;
512  u16 integ_key_len = 0, salt_len = 0;
513  ikev2_sa_transform_t *tr_encr, *tr_prf, *tr_integ;
514  tr_encr =
515  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
516  tr_prf =
517  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
518  tr_integ =
519  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
520 
521  if (tr_integ)
522  integ_key_len = tr_integ->key_len;
523  else
524  salt_len = sizeof (u32);
525 
526  vec_append (s, sa->i_nonce);
527  vec_append (s, sa->r_nonce);
528  skeyseed = ikev2_calc_prf (tr_prf, s, sa->dh_shared_key);
529 
530  /* Calculate S = Ni | Nr | SPIi | SPIr */
531  u64 *spi;
532  vec_add2 (s, tmp, 2 * sizeof (*spi));
533  spi = (u64 *) tmp;
534  spi[0] = clib_host_to_net_u64 (sa->ispi);
535  spi[1] = clib_host_to_net_u64 (sa->rspi);
536 
537  /* calculate PRFplus */
538  u8 *keymat;
539  int len = tr_prf->key_trunc + /* SK_d */
540  integ_key_len * 2 + /* SK_ai, SK_ar */
541  tr_encr->key_len * 2 + /* SK_ei, SK_er */
542  tr_prf->key_len * 2 + /* SK_pi, SK_pr */
543  salt_len * 2;
544 
545  keymat = ikev2_calc_prfplus (tr_prf, skeyseed, s, len);
546  vec_free (skeyseed);
547  vec_free (s);
548 
549  int pos = 0;
550 
551  /* SK_d */
552  sa->sk_d = vec_new (u8, tr_prf->key_trunc);
553  clib_memcpy_fast (sa->sk_d, keymat + pos, tr_prf->key_trunc);
554  pos += tr_prf->key_trunc;
555 
556  if (integ_key_len)
557  {
558  /* SK_ai */
559  sa->sk_ai = vec_new (u8, integ_key_len);
560  clib_memcpy_fast (sa->sk_ai, keymat + pos, integ_key_len);
561  pos += integ_key_len;
562 
563  /* SK_ar */
564  sa->sk_ar = vec_new (u8, integ_key_len);
565  clib_memcpy_fast (sa->sk_ar, keymat + pos, integ_key_len);
566  pos += integ_key_len;
567  }
568 
569  /* SK_ei */
570  sa->sk_ei = vec_new (u8, tr_encr->key_len + salt_len);
571  clib_memcpy_fast (sa->sk_ei, keymat + pos, tr_encr->key_len + salt_len);
572  pos += tr_encr->key_len + salt_len;
573 
574  /* SK_er */
575  sa->sk_er = vec_new (u8, tr_encr->key_len + salt_len);
576  clib_memcpy_fast (sa->sk_er, keymat + pos, tr_encr->key_len + salt_len);
577  pos += tr_encr->key_len + salt_len;
578 
579  /* SK_pi */
580  sa->sk_pi = vec_new (u8, tr_prf->key_len);
581  clib_memcpy_fast (sa->sk_pi, keymat + pos, tr_prf->key_len);
582  pos += tr_prf->key_len;
583 
584  /* SK_pr */
585  sa->sk_pr = vec_new (u8, tr_prf->key_len);
586  clib_memcpy_fast (sa->sk_pr, keymat + pos, tr_prf->key_len);
587  pos += tr_prf->key_len;
588 
589  vec_free (keymat);
590  sa->keys_generated = 1;
591 }
592 
593 static void
595 {
596  u8 *s = 0;
597  u16 integ_key_len = 0;
598  u8 salt_len = 0;
599 
600  ikev2_sa_transform_t *tr_prf, *ctr_encr, *ctr_integ;
601  tr_prf =
602  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
603  ctr_encr =
604  ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
605  ctr_integ =
606  ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
607 
608  if (ctr_integ)
609  integ_key_len = ctr_integ->key_len;
610  else
611  salt_len = sizeof (u32);
612 
613  vec_append (s, sa->i_nonce);
614  vec_append (s, sa->r_nonce);
615  /* calculate PRFplus */
616  u8 *keymat;
617  int len = ctr_encr->key_len * 2 + integ_key_len * 2 + salt_len * 2;
618 
619  keymat = ikev2_calc_prfplus (tr_prf, sa->sk_d, s, len);
620 
621  int pos = 0;
622 
623  /* SK_ei */
624  child->sk_ei = vec_new (u8, ctr_encr->key_len);
625  clib_memcpy_fast (child->sk_ei, keymat + pos, ctr_encr->key_len);
626  pos += ctr_encr->key_len;
627 
628  if (ctr_integ)
629  {
630  /* SK_ai */
631  child->sk_ai = vec_new (u8, ctr_integ->key_len);
632  clib_memcpy_fast (child->sk_ai, keymat + pos, ctr_integ->key_len);
633  pos += ctr_integ->key_len;
634  }
635  else
636  {
637  clib_memcpy (&child->salt_ei, keymat + pos, salt_len);
638  pos += salt_len;
639  }
640 
641  /* SK_er */
642  child->sk_er = vec_new (u8, ctr_encr->key_len);
643  clib_memcpy_fast (child->sk_er, keymat + pos, ctr_encr->key_len);
644  pos += ctr_encr->key_len;
645 
646  if (ctr_integ)
647  {
648  /* SK_ar */
649  child->sk_ar = vec_new (u8, integ_key_len);
650  clib_memcpy_fast (child->sk_ar, keymat + pos, integ_key_len);
651  pos += integ_key_len;
652  }
653  else
654  {
655  clib_memcpy (&child->salt_er, keymat + pos, salt_len);
656  pos += salt_len;
657  }
658 
659  ASSERT (pos == len);
660 
661  vec_free (keymat);
662 }
663 
666 {
667  const u32 max_buf_size =
668  sizeof (ispi) + sizeof (rspi) + sizeof (ip6_address_t) + sizeof (u16);
669  u8 buf[max_buf_size];
670  u8 *res = vec_new (u8, 20);
671 
672  clib_memcpy_fast (&buf[0], &ispi, sizeof (ispi));
673  clib_memcpy_fast (&buf[8], &rspi, sizeof (rspi));
674  clib_memcpy_fast (&buf[8 + 8], ip_addr_bytes (ia), ip_address_size (ia));
675  clib_memcpy_fast (&buf[8 + 8 + ip_address_size (ia)], &port, sizeof (port));
676  SHA1 (buf, 2 * sizeof (ispi) + sizeof (port) + ip_address_size (ia), res);
677  return res;
678 }
679 
680 static int
681 ikev2_parse_ke_payload (const void *p, u32 rlen, ikev2_sa_t * sa,
682  u8 ** ke_data)
683 {
684  const ike_ke_payload_header_t *ke = p;
685  u16 plen = clib_net_to_host_u16 (ke->length);
686  ASSERT (plen >= sizeof (*ke) && plen <= rlen);
687  if (sizeof (*ke) > rlen)
688  return 0;
689 
690  sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
691  vec_reset_length (ke_data[0]);
692  vec_add (ke_data[0], ke->payload, plen - sizeof (*ke));
693  return 1;
694 }
695 
696 static int
697 ikev2_parse_nonce_payload (const void *p, u32 rlen, u8 * nonce)
698 {
699  const ike_payload_header_t *ikep = p;
700  u16 plen = clib_net_to_host_u16 (ikep->length);
701  ASSERT (plen >= sizeof (*ikep) && plen <= rlen);
702  clib_memcpy_fast (nonce, ikep->payload, plen - sizeof (*ikep));
703  return 1;
704 }
705 
706 static int
707 ikev2_check_payload_length (const ike_payload_header_t * ikep, int rlen,
708  u16 * plen)
709 {
710  if (sizeof (*ikep) > rlen)
711  return 0;
712  *plen = clib_net_to_host_u16 (ikep->length);
713  if (*plen < sizeof (*ikep) || *plen > rlen)
714  return 0;
715  return 1;
716 }
717 
718 static int
720  ikev2_sa_t * sa, ike_header_t * ike,
721  udp_header_t * udp, u32 len)
722 {
723  u8 nonce[IKEV2_NONCE_SIZE];
724  int p = 0;
725  u8 payload = ike->nextpayload;
726  ike_payload_header_t *ikep;
727  u16 plen;
728 
729  ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT request received "
730  "from ", clib_net_to_host_u64 (ike->ispi),
731  clib_net_to_host_u64 (ike->rspi),
732  ip_addr_v4 (&sa->iaddr).as_u32,
733  ip_addr_version (&sa->iaddr) == AF_IP4);
734 
735  sa->ispi = clib_net_to_host_u64 (ike->ispi);
736 
737  /* store whole IKE payload - needed for PSK auth */
739  vec_add (sa->last_sa_init_req_packet_data, ike, len);
740 
741  if (len < sizeof (*ike))
742  return 0;
743 
744  len -= sizeof (*ike);
745  while (p < len && payload != IKEV2_PAYLOAD_NONE)
746  {
747  ikep = (ike_payload_header_t *) & ike->payload[p];
748  int current_length = len - p;
749  if (!ikev2_check_payload_length (ikep, current_length, &plen))
750  return 0;
751 
752  if (payload == IKEV2_PAYLOAD_SA)
753  {
755  sa->i_proposals = ikev2_parse_sa_payload (ikep, current_length);
756  }
757  else if (payload == IKEV2_PAYLOAD_KE)
758  {
759  if (!ikev2_parse_ke_payload (ikep, current_length, sa,
760  &sa->i_dh_data))
761  return 0;
762  }
763  else if (payload == IKEV2_PAYLOAD_NONCE)
764  {
766  if (ikev2_parse_nonce_payload (ikep, current_length, nonce))
767  vec_add (sa->i_nonce, nonce, plen - sizeof (*ikep));
768  }
769  else if (payload == IKEV2_PAYLOAD_NOTIFY)
770  {
771  ikev2_notify_t *n =
772  ikev2_parse_notify_payload (ikep, current_length);
773  if (n->msg_type == IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP)
774  {
775  u8 *src_sha = ikev2_compute_nat_sha1 (ike->ispi, 0, &sa->iaddr,
776  udp->src_port);
777  if (clib_memcmp (src_sha, n->data, vec_len (src_sha)))
778  {
779  if (sa->natt_state == IKEV2_NATT_ENABLED)
781  ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx initiator"
782  " behind NAT", sa->ispi);
783  }
784  vec_free (src_sha);
785  }
786  else if (n->msg_type ==
787  IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP)
788  {
789  u8 *dst_sha = ikev2_compute_nat_sha1 (ike->ispi, 0, &sa->raddr,
790  udp->dst_port);
791  if (clib_memcmp (dst_sha, n->data, vec_len (dst_sha)))
792  {
793  if (sa->natt_state == IKEV2_NATT_ENABLED)
795  ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx responder"
796  " (self) behind NAT", sa->ispi);
797  }
798  vec_free (dst_sha);
799  }
800  vec_free (n);
801  }
802  else if (payload == IKEV2_PAYLOAD_VENDOR)
803  {
805  }
806  else
807  {
808  ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
809  payload);
810  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
811  {
813  sa->unsupported_cp = payload;
814  return 0;
815  }
816  }
817 
818  payload = ikep->nextpayload;
819  p += plen;
820  }
821 
823  return 1;
824 }
825 
826 static void
828  ikev2_sa_t * sa, ike_header_t * ike,
829  udp_header_t * udp, u32 len)
830 {
831  u8 nonce[IKEV2_NONCE_SIZE];
832  int p = 0;
833  u8 payload = ike->nextpayload;
834  ike_payload_header_t *ikep;
835  u16 plen;
836 
837  sa->ispi = clib_net_to_host_u64 (ike->ispi);
838  sa->rspi = clib_net_to_host_u64 (ike->rspi);
839 
840  ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT response received "
841  "from ", sa->ispi, sa->rspi,
842  ip_addr_v4 (&sa->raddr).as_u32,
843  ip_addr_version (&sa->raddr) == AF_IP4);
844 
845  /* store whole IKE payload - needed for PSK auth */
847  vec_add (sa->last_sa_init_res_packet_data, ike, len);
848 
849  if (sizeof (*ike) > len)
850  return;
851 
852  len -= sizeof (*ike);
853  while (p < len && payload != IKEV2_PAYLOAD_NONE)
854  {
855  int current_length = len - p;
856  ikep = (ike_payload_header_t *) & ike->payload[p];
857  if (!ikev2_check_payload_length (ikep, current_length, &plen))
858  return;
859 
860  if (payload == IKEV2_PAYLOAD_SA)
861  {
863  sa->r_proposals = ikev2_parse_sa_payload (ikep, current_length);
864  if (sa->r_proposals)
865  {
867  ike->msgid =
868  clib_host_to_net_u32 (clib_net_to_host_u32 (ike->msgid) + 1);
869  }
870  }
871  else if (payload == IKEV2_PAYLOAD_KE)
872  {
873  if (!ikev2_parse_ke_payload (ikep, current_length, sa,
874  &sa->r_dh_data))
875  return;
876  }
877  else if (payload == IKEV2_PAYLOAD_NONCE)
878  {
880  if (ikev2_parse_nonce_payload (ikep, current_length, nonce))
881  vec_add (sa->r_nonce, nonce, plen - sizeof (*ikep));
882  }
883  else if (payload == IKEV2_PAYLOAD_NOTIFY)
884  {
885  ikev2_notify_t *n =
886  ikev2_parse_notify_payload (ikep, current_length);
887  if (n->msg_type == IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP)
888  {
889  u8 *src_sha = ikev2_compute_nat_sha1 (ike->ispi, ike->rspi,
890  &sa->raddr,
891  udp->src_port);
892  if (clib_memcmp (src_sha, n->data, vec_len (src_sha)))
893  {
894  ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx responder"
895  " behind NAT, unsupported", sa->ispi);
896  }
897  vec_free (src_sha);
898  }
899  else if (n->msg_type ==
900  IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP)
901  {
902  u8 *dst_sha = ikev2_compute_nat_sha1 (ike->ispi, ike->rspi,
903  &sa->iaddr,
904  udp->dst_port);
905  if (clib_memcmp (dst_sha, n->data, vec_len (dst_sha)))
906  {
907  if (sa->natt_state == IKEV2_NATT_ENABLED)
909  ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx initiator"
910  " (self) behind NAT", sa->ispi);
911  }
912  vec_free (dst_sha);
913  }
914  vec_free (n);
915  }
916  else if (payload == IKEV2_PAYLOAD_VENDOR)
917  {
919  }
920  else
921  {
922  ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
923  payload);
924  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
925  {
927  sa->unsupported_cp = payload;
928  return;
929  }
930  }
931 
932  payload = ikep->nextpayload;
933  p += plen;
934  }
935 }
936 
937 static u8 *
938 ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike,
939  u8 * payload, u32 rlen, u32 * out_len)
940 {
942  int p = 0;
943  u8 last_payload = 0, *hmac = 0, *plaintext = 0;
944  ike_payload_header_t *ikep = 0;
945  u16 plen = 0;
946  u32 dlen = 0;
947  ikev2_sa_transform_t *tr_integ;
948  ikev2_sa_transform_t *tr_encr;
949  tr_integ =
950  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
951  tr_encr =
952  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
953  int is_aead = tr_encr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16;
954 
955  if (((!sa->sk_ar || !sa->sk_ai) && !is_aead) || (!sa->sk_ei || !sa->sk_er))
956  return 0;
957 
958  if (rlen <= sizeof (*ike))
959  return 0;
960 
961  int len = rlen - sizeof (*ike);
962  while (p < len &&
963  *payload != IKEV2_PAYLOAD_NONE && last_payload != IKEV2_PAYLOAD_SK)
964  {
965  ikep = (ike_payload_header_t *) & ike->payload[p];
966  int current_length = len - p;
967  if (!ikev2_check_payload_length (ikep, current_length, &plen))
968  return 0;
969 
970  if (*payload == IKEV2_PAYLOAD_SK)
971  {
972  last_payload = *payload;
973  }
974  else
975  {
976  ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
977  *payload);
978  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
979  {
980  sa->unsupported_cp = *payload;
981  return 0;
982  }
983  }
984 
985  *payload = ikep->nextpayload;
986  p += plen;
987  }
988 
989  if (last_payload != IKEV2_PAYLOAD_SK)
990  {
991  ikev2_elog_error ("Last payload must be SK");
992  return 0;
993  }
994 
995  if (is_aead)
996  {
997  if (plen < sizeof (*ikep) + IKEV2_GCM_ICV_SIZE)
998  return 0;
999 
1000  plen -= sizeof (*ikep) + IKEV2_GCM_ICV_SIZE;
1001  u8 *aad = (u8 *) ike;
1002  u32 aad_len = ikep->payload - aad;
1003  u8 *tag = ikep->payload + plen;
1004 
1005  int rc = ikev2_decrypt_aead_data (ptd, sa, tr_encr, ikep->payload,
1006  plen, aad, aad_len, tag, &dlen);
1007  if (rc)
1008  {
1009  *out_len = dlen;
1010  plaintext = ikep->payload + IKEV2_GCM_IV_SIZE;
1011  }
1012  }
1013  else
1014  {
1015  if (rlen < tr_integ->key_trunc)
1016  return 0;
1017 
1018  hmac =
1019  ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ar : sa->sk_ai,
1020  (u8 *) ike, rlen - tr_integ->key_trunc);
1021 
1022  if (plen < sizeof (*ikep) + tr_integ->key_trunc)
1023  return 0;
1024 
1025  plen = plen - sizeof (*ikep) - tr_integ->key_trunc;
1026 
1027  if (clib_memcmp (hmac, &ikep->payload[plen], tr_integ->key_trunc))
1028  {
1029  ikev2_elog_error ("message integrity check failed");
1030  vec_free (hmac);
1031  return 0;
1032  }
1033  vec_free (hmac);
1034 
1035  int rc = ikev2_decrypt_data (ptd, sa, tr_encr, ikep->payload, plen,
1036  &dlen);
1037  if (rc)
1038  {
1039  *out_len = dlen;
1040  plaintext = ikep->payload + tr_encr->block_size;
1041  }
1042  }
1043 
1044  return plaintext;
1045 }
1046 
1049 {
1050  if (i1->type != i2->type)
1051  return 0;
1052 
1053  if (vec_len (i1->data) != vec_len (i2->data))
1054  return 0;
1055 
1056  if (clib_memcmp (i1->data, i2->data, vec_len (i1->data)))
1057  return 0;
1058 
1059  return 1;
1060 }
1061 
1062 static void
1064  ikev2_sa_t * sa)
1065 {
1066  ikev2_main_t *km = &ikev2_main;
1067  ikev2_sa_t *tmp;
1068  u32 i, *delete = 0;
1070 
1071  /* find old IKE SAs with the same authenticated identity */
1072  /* *INDENT-OFF* */
1073  pool_foreach (tmp, ptd->sas) {
1074  if (!ikev2_is_id_equal (&tmp->i_id, &sa->i_id)
1075  || !ikev2_is_id_equal(&tmp->r_id, &sa->r_id))
1076  continue;
1077 
1078  if (sa->rspi != tmp->rspi)
1079  vec_add1(delete, tmp - ptd->sas);
1080  }
1081  /* *INDENT-ON* */
1082 
1083  for (i = 0; i < vec_len (delete); i++)
1084  {
1085  tmp = pool_elt_at_index (ptd->sas, delete[i]);
1086  vec_foreach (c, tmp->childs)
1087  {
1089  }
1090  ikev2_delete_sa (ptd, tmp);
1091  }
1092 
1093  vec_free (delete);
1094  sa->initial_contact = 0;
1095 }
1096 
1097 static void
1099  ikev2_sa_t * sa)
1100 {
1101  ikev2_main_t *km = &ikev2_main;
1102 
1103  if (!sa->initial_contact)
1104  return;
1105 
1106  if (ptd)
1107  {
1109  }
1110  else
1111  {
1112  vec_foreach (ptd, km->per_thread_data)
1114  }
1115  sa->initial_contact = 0;
1116 }
1117 
1118 static int
1119 ikev2_parse_id_payload (const void *p, u16 rlen, ikev2_id_t * sa_id)
1120 {
1121  const ike_id_payload_header_t *id = p;
1122  u16 plen = clib_net_to_host_u16 (id->length);
1123  if (plen < sizeof (*id) || plen > rlen)
1124  return 0;
1125 
1126  sa_id->type = id->id_type;
1127  vec_reset_length (sa_id->data);
1128  vec_add (sa_id->data, id->payload, plen - sizeof (*id));
1129 
1130  return 1;
1131 }
1132 
1133 static int
1134 ikev2_parse_auth_payload (const void *p, u32 rlen, ikev2_auth_t * a)
1135 {
1136  const ike_auth_payload_header_t *ah = p;
1137  u16 plen = clib_net_to_host_u16 (ah->length);
1138 
1139  a->method = ah->auth_method;
1140  vec_reset_length (a->data);
1141  vec_add (a->data, ah->payload, plen - sizeof (*ah));
1142  return 1;
1143 }
1144 
1145 static int
1147  ike_header_t * ike, u32 len)
1148 {
1149  int p = 0;
1150  ikev2_child_sa_t *first_child_sa;
1151  u8 payload = ike->nextpayload;
1152  u8 *plaintext = 0;
1153  ike_payload_header_t *ikep;
1154  u16 plen;
1155  u32 dlen = 0;
1156 
1157  ikev2_elog_exchange ("ispi %lx rspi %lx EXCHANGE_IKE_AUTH received "
1158  "from ", clib_host_to_net_u64 (ike->ispi),
1159  clib_host_to_net_u64 (ike->rspi),
1160  sa->is_initiator ?
1161  ip_addr_v4 (&sa->raddr).as_u32 :
1162  ip_addr_v4 (&sa->iaddr).as_u32,
1163  ip_addr_version (&sa->raddr) == AF_IP4);
1164 
1165  ikev2_calc_keys (sa);
1166 
1167  plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload, len, &dlen);
1168 
1169  if (!plaintext)
1170  {
1171  if (sa->unsupported_cp)
1172  {
1174  return 0;
1175  }
1176  goto malformed;
1177  }
1178 
1179  /* select or create 1st child SA */
1180  if (sa->is_initiator)
1181  {
1182  first_child_sa = &sa->childs[0];
1183  }
1184  else
1185  {
1187  vec_add2 (sa->childs, first_child_sa, 1);
1188  }
1189 
1190 
1191  /* process encrypted payload */
1192  while (p < dlen && payload != IKEV2_PAYLOAD_NONE)
1193  {
1194  ikep = (ike_payload_header_t *) & plaintext[p];
1195  int current_length = dlen - p;
1196  if (!ikev2_check_payload_length (ikep, current_length, &plen))
1197  goto malformed;
1198 
1199  if (payload == IKEV2_PAYLOAD_SA) /* 33 */
1200  {
1201  if (sa->is_initiator)
1202  {
1203  ikev2_sa_free_proposal_vector (&first_child_sa->r_proposals);
1204  first_child_sa->r_proposals = ikev2_parse_sa_payload (ikep,
1205  current_length);
1206  }
1207  else
1208  {
1209  ikev2_sa_free_proposal_vector (&first_child_sa->i_proposals);
1210  first_child_sa->i_proposals = ikev2_parse_sa_payload (ikep,
1211  current_length);
1212  }
1213  }
1214  else if (payload == IKEV2_PAYLOAD_IDI) /* 35 */
1215  {
1216  if (!ikev2_parse_id_payload (ikep, current_length, &sa->i_id))
1217  goto malformed;
1218  }
1219  else if (payload == IKEV2_PAYLOAD_IDR) /* 36 */
1220  {
1221  if (!ikev2_parse_id_payload (ikep, current_length, &sa->r_id))
1222  goto malformed;
1223  }
1224  else if (payload == IKEV2_PAYLOAD_AUTH) /* 39 */
1225  {
1226  if (sa->is_initiator)
1227  {
1228  if (!ikev2_parse_auth_payload (ikep, current_length,
1229  &sa->r_auth))
1230  goto malformed;
1231  }
1232  else
1233  {
1234  if (!ikev2_parse_auth_payload (ikep, current_length,
1235  &sa->i_auth))
1236  goto malformed;
1237  }
1238  }
1239  else if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
1240  {
1241  ikev2_notify_t *n =
1242  ikev2_parse_notify_payload (ikep, current_length);
1243  if (n->msg_type == IKEV2_NOTIFY_MSG_INITIAL_CONTACT)
1244  {
1245  sa->initial_contact = 1;
1246  }
1247  vec_free (n);
1248  }
1249  else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
1250  {
1252  }
1253  else if (payload == IKEV2_PAYLOAD_TSI) /* 44 */
1254  {
1255  vec_free (first_child_sa->tsi);
1256  first_child_sa->tsi = ikev2_parse_ts_payload (ikep, current_length);
1257  }
1258  else if (payload == IKEV2_PAYLOAD_TSR) /* 45 */
1259  {
1260  vec_free (first_child_sa->tsr);
1261  first_child_sa->tsr = ikev2_parse_ts_payload (ikep, current_length);
1262  }
1263  else
1264  {
1265  ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1266  payload);
1267 
1268  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1269  {
1271  sa->unsupported_cp = payload;
1272  return 0;
1273  }
1274  }
1275 
1276  payload = ikep->nextpayload;
1277  p += plen;
1278  }
1279 
1280  return 1;
1281 
1282 malformed:
1283  ikev2_set_state (sa, IKEV2_STATE_DELETED, ": malformed IKE_AUTH");
1284  return 0;
1285 }
1286 
1287 static int
1289  ikev2_sa_t * sa, ike_header_t * ike, u32 len)
1290 {
1291  int p = 0;
1292  u8 payload = ike->nextpayload;
1293  u8 *plaintext = 0;
1294  ike_payload_header_t *ikep;
1295  u32 dlen = 0;
1296  ikev2_notify_t *n = 0;
1297 
1298  sa->liveness_retries = 0;
1299  ikev2_elog_exchange ("ispi %lx rspi %lx INFORMATIONAL received "
1300  "from ", clib_host_to_net_u64 (ike->ispi),
1301  clib_host_to_net_u64 (ike->rspi),
1302  ip_addr_v4 (&sa->iaddr).as_u32,
1303  ip_addr_version (&sa->iaddr) == AF_IP4);
1304 
1305  plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload, len, &dlen);
1306 
1307  if (!plaintext)
1308  return 0;
1309 
1310  /* process encrypted payload */
1311  p = 0;
1312  while (p < dlen && payload != IKEV2_PAYLOAD_NONE)
1313  {
1314  u32 current_length = dlen - p;
1315  if (p + sizeof (*ikep) > dlen)
1316  return 0;
1317 
1318  ikep = (ike_payload_header_t *) & plaintext[p];
1319  u16 plen = clib_net_to_host_u16 (ikep->length);
1320 
1321  if (plen < sizeof (*ikep) || plen > current_length)
1322  return 0;
1323 
1324  if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
1325  {
1326  n = ikev2_parse_notify_payload (ikep, current_length);
1327  if (!n)
1328  return 0;
1329  if (n->msg_type == IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED)
1331  vec_free (n);
1332  }
1333  else if (payload == IKEV2_PAYLOAD_DELETE) /* 42 */
1334  {
1335  sa->del = ikev2_parse_delete_payload (ikep, current_length);
1336  }
1337  else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
1338  {
1340  }
1341  else
1342  {
1343  ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1344  payload);
1345  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1346  {
1347  sa->unsupported_cp = payload;
1348  return 0;
1349  }
1350  }
1351  payload = ikep->nextpayload;
1352  p += plen;
1353  }
1354  return 1;
1355 }
1356 
1357 static int
1359  ikev2_sa_t * sa, ike_header_t * ike,
1360  u32 len)
1361 {
1362  int p = 0;
1363  u8 payload = ike->nextpayload;
1364  u8 *plaintext = 0;
1365  u8 rekeying = 0;
1366  u8 nonce[IKEV2_NONCE_SIZE];
1367 
1368  ike_payload_header_t *ikep;
1369  ikev2_notify_t *n = 0;
1370  ikev2_ts_t *tsi = 0;
1371  ikev2_ts_t *tsr = 0;
1372  ikev2_sa_proposal_t *proposal = 0;
1373  ikev2_child_sa_t *child_sa;
1374  u32 dlen = 0;
1375  u16 plen;
1376 
1377  ikev2_elog_exchange ("ispi %lx rspi %lx CREATE_CHILD_SA received "
1378  "from ", clib_host_to_net_u64 (ike->ispi),
1379  clib_host_to_net_u64 (ike->rspi),
1380  ip_addr_v4 (&sa->raddr).as_u32,
1381  ip_addr_version (&sa->raddr) == AF_IP4);
1382 
1383  plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload, len, &dlen);
1384 
1385  if (!plaintext)
1386  goto cleanup_and_exit;
1387 
1388  /* process encrypted payload */
1389  p = 0;
1390  while (payload != IKEV2_PAYLOAD_NONE)
1391  {
1392  ikep = (ike_payload_header_t *) & plaintext[p];
1393  int current_length = dlen - p;
1394  if (!ikev2_check_payload_length (ikep, current_length, &plen))
1395  goto cleanup_and_exit;
1396 
1397  if (payload == IKEV2_PAYLOAD_SA)
1398  {
1399  proposal = ikev2_parse_sa_payload (ikep, current_length);
1400  }
1401  else if (payload == IKEV2_PAYLOAD_NOTIFY)
1402  {
1403  n = ikev2_parse_notify_payload (ikep, current_length);
1404  if (n->msg_type == IKEV2_NOTIFY_MSG_REKEY_SA)
1405  {
1406  rekeying = 1;
1407  }
1408  }
1409  else if (payload == IKEV2_PAYLOAD_DELETE)
1410  {
1411  sa->del = ikev2_parse_delete_payload (ikep, current_length);
1412  }
1413  else if (payload == IKEV2_PAYLOAD_VENDOR)
1414  {
1416  }
1417  else if (payload == IKEV2_PAYLOAD_NONCE)
1418  {
1419  ikev2_parse_nonce_payload (ikep, current_length, nonce);
1420  }
1421  else if (payload == IKEV2_PAYLOAD_TSI)
1422  {
1423  tsi = ikev2_parse_ts_payload (ikep, current_length);
1424  }
1425  else if (payload == IKEV2_PAYLOAD_TSR)
1426  {
1427  tsr = ikev2_parse_ts_payload (ikep, current_length);
1428  }
1429  else
1430  {
1431  ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1432  payload);
1433  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1434  {
1435  sa->unsupported_cp = payload;
1436  goto cleanup_and_exit;
1437  }
1438  }
1439  payload = ikep->nextpayload;
1440  p += plen;
1441  }
1442 
1443  if (sa->is_initiator && proposal
1444  && proposal->protocol_id == IKEV2_PROTOCOL_ESP)
1445  {
1446  ikev2_rekey_t *rekey = sa->rekey;
1447  if (vec_len (rekey) == 0)
1448  goto cleanup_and_exit;
1449  rekey->protocol_id = proposal->protocol_id;
1450  rekey->i_proposal =
1452  rekey->i_proposal->spi = rekey->spi;
1453  rekey->r_proposal = proposal;
1454  rekey->tsi = tsi;
1455  rekey->tsr = tsr;
1456  /* update Nr */
1457  vec_reset_length (sa->r_nonce);
1458  vec_add (sa->r_nonce, nonce, IKEV2_NONCE_SIZE);
1459  child_sa = ikev2_sa_get_child (sa, rekey->ispi, IKEV2_PROTOCOL_ESP, 1);
1460  if (child_sa)
1461  {
1462  child_sa->rekey_retries = 0;
1463  }
1464  }
1465  else if (rekeying)
1466  {
1467  ikev2_rekey_t *rekey;
1468  child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id, 1);
1469  if (!child_sa)
1470  {
1471  ikev2_elog_uint (IKEV2_LOG_ERROR, "child SA spi %lx not found",
1472  n->spi);
1473  goto cleanup_and_exit;
1474  }
1475  vec_add2 (sa->rekey, rekey, 1);
1476  rekey->protocol_id = n->protocol_id;
1477  rekey->spi = n->spi;
1478  rekey->i_proposal = proposal;
1479  rekey->r_proposal =
1481  rekey->tsi = tsi;
1482  rekey->tsr = tsr;
1483  /* update Ni */
1484  vec_reset_length (sa->i_nonce);
1485  vec_add (sa->i_nonce, nonce, IKEV2_NONCE_SIZE);
1486  /* generate new Nr */
1488  RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
1489  }
1490  else
1491  goto cleanup_and_exit;
1492  vec_free (n);
1493  return 1;
1494 
1495 cleanup_and_exit:
1496  vec_free (n);
1497  vec_free (proposal);
1498  vec_free (tsr);
1499  vec_free (tsi);
1500  return 0;
1501 }
1502 
1503 static u8 *
1504 ikev2_sa_generate_authmsg (ikev2_sa_t * sa, int is_responder)
1505 {
1506  u8 *authmsg = 0;
1507  u8 *data;
1508  u8 *nonce;
1509  ikev2_id_t *id;
1510  u8 *key;
1511  u8 *packet_data;
1512  ikev2_sa_transform_t *tr_prf;
1513 
1514  tr_prf =
1515  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1516 
1517  if (is_responder)
1518  {
1519  id = &sa->r_id;
1520  key = sa->sk_pr;
1521  nonce = sa->i_nonce;
1522  packet_data = sa->last_sa_init_res_packet_data;
1523  }
1524  else
1525  {
1526  id = &sa->i_id;
1527  key = sa->sk_pi;
1528  nonce = sa->r_nonce;
1529  packet_data = sa->last_sa_init_req_packet_data;
1530  }
1531 
1532  data = vec_new (u8, 4);
1533  data[0] = id->type;
1534  vec_append (data, id->data);
1535 
1536  u8 *id_hash = ikev2_calc_prf (tr_prf, key, data);
1537  vec_append (authmsg, packet_data);
1538  vec_append (authmsg, nonce);
1539  vec_append (authmsg, id_hash);
1540  vec_free (id_hash);
1541  vec_free (data);
1542 
1543  return authmsg;
1544 }
1545 
1546 static int
1548 {
1549  if (ts1->ts_type == ts2->ts_type && ts1->protocol_id == ts2->protocol_id &&
1550  ts1->start_port == ts2->start_port && ts1->end_port == ts2->end_port &&
1551  !ip_address_cmp (&ts1->start_addr, &ts2->start_addr) &&
1552  !ip_address_cmp (&ts1->end_addr, &ts2->end_addr))
1553  return 1;
1554 
1555  return 0;
1556 }
1557 
1558 static void
1560 {
1561  ikev2_main_t *km = &ikev2_main;
1562  ikev2_profile_t *p;
1563  ikev2_ts_t *ts, *p_tsi, *p_tsr, *tsi = 0, *tsr = 0;
1564  ikev2_id_t *id_rem, *id_loc;
1565 
1566  /* *INDENT-OFF* */
1567  pool_foreach (p, km->profiles) {
1568 
1569  if (sa->is_initiator)
1570  {
1571  p_tsi = &p->loc_ts;
1572  p_tsr = &p->rem_ts;
1573  id_rem = &sa->r_id;
1574  id_loc = &sa->i_id;
1575  }
1576  else
1577  {
1578  p_tsi = &p->rem_ts;
1579  p_tsr = &p->loc_ts;
1580  id_rem = &sa->i_id;
1581  id_loc = &sa->r_id;
1582  }
1583 
1584  /* check id */
1585  if (!ikev2_is_id_equal (&p->rem_id, id_rem)
1586  || !ikev2_is_id_equal (&p->loc_id, id_loc))
1587  continue;
1588 
1589  sa->profile_index = p - km->profiles;
1590 
1591  vec_foreach(ts, sa->childs[0].tsi)
1592  {
1593  if (ikev2_ts_cmp(p_tsi, ts))
1594  {
1595  vec_add1 (tsi, ts[0]);
1596  break;
1597  }
1598  }
1599 
1600  vec_foreach(ts, sa->childs[0].tsr)
1601  {
1602  if (ikev2_ts_cmp(p_tsr, ts))
1603  {
1604  vec_add1 (tsr, ts[0]);
1605  break;
1606  }
1607  }
1608 
1609  break;
1610  }
1611  /* *INDENT-ON* */
1612 
1613  if (tsi && tsr)
1614  {
1615  vec_free (sa->childs[0].tsi);
1616  vec_free (sa->childs[0].tsr);
1617  sa->childs[0].tsi = tsi;
1618  sa->childs[0].tsr = tsr;
1619  }
1620  else
1621  {
1622  vec_free (tsi);
1623  vec_free (tsr);
1625  }
1626 }
1627 
1628 static void
1630 {
1631  ikev2_main_t *km = &ikev2_main;
1632  ikev2_profile_t *p, *sel_p = 0;
1633  u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1634  ikev2_sa_transform_t *tr_prf;
1635 
1636  tr_prf =
1637  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1638 
1639  /* only shared key and rsa signature */
1640  if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1641  sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1642  {
1643  ikev2_elog_uint (IKEV2_LOG_ERROR,
1644  "unsupported authentication method %u",
1645  sa->i_auth.method);
1647  return;
1648  }
1649 
1650  key_pad = format (0, "%s", IKEV2_KEY_PAD);
1651  authmsg = ikev2_sa_generate_authmsg (sa, sa->is_initiator);
1652 
1653  ikev2_id_t *id_rem, *id_loc;
1654  ikev2_auth_t *sa_auth;
1655 
1656  if (sa->is_initiator)
1657  {
1658  id_rem = &sa->r_id;
1659  id_loc = &sa->i_id;
1660  sa_auth = &sa->r_auth;
1661  }
1662  else
1663  {
1664  id_rem = &sa->i_id;
1665  id_loc = &sa->r_id;
1666  sa_auth = &sa->i_auth;
1667  }
1668 
1669  /* *INDENT-OFF* */
1670  pool_foreach (p, km->profiles) {
1671 
1672  /* check id */
1673  if (!ikev2_is_id_equal (&p->rem_id, id_rem)
1674  || !ikev2_is_id_equal (&p->loc_id, id_loc))
1675  continue;
1676 
1677  if (sa_auth->method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1678  {
1679  if (!p->auth.data ||
1680  p->auth.method != IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1681  continue;
1682 
1683  psk = ikev2_calc_prf(tr_prf, p->auth.data, key_pad);
1684  auth = ikev2_calc_prf(tr_prf, psk, authmsg);
1685 
1686  if (!clib_memcmp(auth, sa_auth->data, vec_len(sa_auth->data)))
1687  {
1689  vec_free(auth);
1690  sel_p = p;
1691  break;
1692  }
1693 
1694  }
1695  else if (sa_auth->method == IKEV2_AUTH_METHOD_RSA_SIG)
1696  {
1697  if (p->auth.method != IKEV2_AUTH_METHOD_RSA_SIG)
1698  continue;
1699 
1700  if (ikev2_verify_sign(p->auth.key, sa_auth->data, authmsg) == 1)
1701  {
1703  sel_p = p;
1704  break;
1705  }
1706  }
1707 
1708  vec_free(auth);
1709  vec_free(psk);
1710  }
1711  /* *INDENT-ON* */
1712 
1713  if (sel_p)
1714  {
1715  sa->udp_encap = sel_p->udp_encap;
1717  }
1718  vec_free (authmsg);
1719 
1720  if (sa->state == IKEV2_STATE_AUTHENTICATED)
1721  {
1722  if (!sa->is_initiator)
1723  {
1724  vec_free (sa->r_id.data);
1725  sa->r_id.data = vec_dup (sel_p->loc_id.data);
1726  sa->r_id.type = sel_p->loc_id.type;
1727  sa->i_id.data = vec_dup (sel_p->rem_id.data);
1728  sa->i_id.type = sel_p->rem_id.type;
1729 
1730  /* generate our auth data */
1731  authmsg = ikev2_sa_generate_authmsg (sa, 1);
1732  if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1733  {
1734  vec_free (sa->r_auth.data);
1735  sa->r_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1736  sa->r_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1737  }
1738  else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1739  {
1740  vec_free (sa->r_auth.data);
1741  sa->r_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1742  sa->r_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1743  }
1744  vec_free (authmsg);
1745 
1746  /* select transforms for 1st child sa */
1748  sa->childs[0].r_proposals =
1751 
1752  if (~0 != sel_p->tun_itf)
1753  {
1754  sa->is_tun_itf_set = 1;
1755  sa->tun_itf = sel_p->tun_itf;
1756  }
1757  }
1758  }
1759  else
1760  {
1761  ikev2_elog_uint (IKEV2_LOG_ERROR, "authentication failed, no matching "
1762  "profile found! ispi %lx", sa->ispi);
1764  }
1765  vec_free (psk);
1766  vec_free (key_pad);
1767 }
1768 
1769 
1770 static void
1772 {
1773  ikev2_main_t *km = &ikev2_main;
1774  u8 *authmsg, *key_pad, *psk = 0;
1775  ikev2_sa_transform_t *tr_prf;
1776 
1777  tr_prf =
1778  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1779 
1780  /* only shared key and rsa signature */
1781  if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1782  sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1783  {
1784  ikev2_elog_uint (IKEV2_LOG_ERROR,
1785  "unsupported authentication method %u",
1786  sa->i_auth.method);
1788  return;
1789  }
1790 
1791  key_pad = format (0, "%s", IKEV2_KEY_PAD);
1792  authmsg = ikev2_sa_generate_authmsg (sa, 0);
1793  psk = ikev2_calc_prf (tr_prf, sa->i_auth.data, key_pad);
1794 
1795  if (sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1796  {
1797  vec_free (sa->i_auth.data);
1798  sa->i_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1799  sa->i_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1800  }
1801  else if (sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1802  {
1803  vec_free (sa->i_auth.data);
1804  sa->i_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1805  sa->i_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1806  }
1807 
1808  vec_free (psk);
1809  vec_free (key_pad);
1810  vec_free (authmsg);
1811 }
1812 
1813 static u32
1815 {
1816  return (0x80000000 | (ti << 24) | (sai << 12) | ci);
1817 }
1818 
1819 static u32
1821 {
1822  return (0xc0000000 | (ti << 24) | (sai << 12) | ci);
1823 }
1824 
1825 typedef struct
1826 {
1835  ipsec_crypto_alg_t encr_type;
1836  ipsec_integ_alg_t integ_type;
1837  ip46_address_t local_ip;
1838  ip46_address_t remote_ip;
1839  ipsec_key_t loc_ckey, rem_ckey, loc_ikey, rem_ikey;
1846 
1847 static void
1849 {
1850  ikev2_main_t *km = &ikev2_main;
1851  u32 sw_if_index;
1852  int rv = 0;
1853 
1854  if (~0 == a->sw_if_index)
1855  {
1856  /* no tunnel associated with the SA/profile - create a new one */
1858  &a->local_ip, &a->remote_ip, 0,
1859  TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
1860  TUNNEL_MODE_P2P, &sw_if_index);
1861 
1862  if (rv == VNET_API_ERROR_IF_ALREADY_EXISTS)
1863  {
1864  if (hash_get (km->sw_if_indices, sw_if_index))
1865  /* interface is managed by IKE; proceed with updating SAs */
1866  rv = 0;
1867  }
1868  hash_set1 (km->sw_if_indices, sw_if_index);
1869  }
1870  else
1871  {
1872  sw_if_index = a->sw_if_index;
1873  vnet_sw_interface_admin_up (vnet_get_main (), sw_if_index);
1874  }
1875 
1876  if (rv)
1877  {
1878  ikev2_elog_uint (IKEV2_LOG_ERROR,
1879  "installing ipip tunnel failed! local spi: %x",
1880  a->local_spi);
1881  return;
1882  }
1883 
1884  u32 *sas_in = NULL;
1885  vec_add1 (sas_in, a->remote_sa_id);
1886  if (a->is_rekey)
1887  {
1888  ipsec_tun_protect_del (sw_if_index, NULL);
1889 
1890  /* replace local SA immediately */
1892 
1893  /* keep the old sa */
1894  vec_add1 (sas_in, a->old_remote_sa_id);
1895  }
1896 
1898  a->local_spi,
1900  &a->loc_ckey, a->integ_type, &a->loc_ikey,
1901  a->flags, 0, a->salt_local, &a->local_ip,
1902  &a->remote_ip, TUNNEL_ENCAP_DECAP_FLAG_NONE,
1903  IP_DSCP_CS0, NULL, a->src_port, a->dst_port);
1904  if (rv)
1905  goto err0;
1906 
1909  a->integ_type, &a->rem_ikey,
1910  (a->flags | IPSEC_SA_FLAG_IS_INBOUND), 0,
1911  a->salt_remote, &a->remote_ip,
1912  &a->local_ip, TUNNEL_ENCAP_DECAP_FLAG_NONE,
1913  IP_DSCP_CS0, NULL,
1915  if (rv)
1916  goto err1;
1917 
1918  rv = ipsec_tun_protect_update (sw_if_index, NULL, a->local_sa_id, sas_in);
1919  if (rv)
1920  goto err2;
1921 
1922  return;
1923 
1924 err2:
1926 err1:
1928 err0:
1929  vec_free (sas_in);
1930 }
1931 
1932 static int
1934  ikev2_sa_t * sa,
1935  ikev2_child_sa_t * child, u32 sa_index,
1936  u32 child_index, u8 is_rekey)
1937 {
1938  u32 thread_index = vlib_get_thread_index ();
1939  ikev2_main_t *km = &ikev2_main;
1940  ipsec_crypto_alg_t encr_type;
1941  ipsec_integ_alg_t integ_type;
1942  ikev2_profile_t *p = 0;
1944  ikev2_sa_proposal_t *proposals;
1945  u8 is_aead = 0;
1947 
1948  clib_memset (&a, 0, sizeof (a));
1949 
1950  if (!child->r_proposals)
1951  {
1953  return 1;
1954  }
1955 
1956  if (sa->is_initiator)
1957  {
1958  ip_address_to_46 (&sa->iaddr, &a.local_ip);
1959  ip_address_to_46 (&sa->raddr, &a.remote_ip);
1960  proposals = child->r_proposals;
1961  a.local_spi = child->r_proposals[0].spi;
1962  a.remote_spi = child->i_proposals[0].spi;
1963  }
1964  else
1965  {
1966  ip_address_to_46 (&sa->raddr, &a.local_ip);
1967  ip_address_to_46 (&sa->iaddr, &a.remote_ip);
1968  proposals = child->i_proposals;
1969  a.local_spi = child->i_proposals[0].spi;
1970  a.remote_spi = child->r_proposals[0].spi;
1971  }
1972 
1973  a.flags = IPSEC_SA_FLAG_USE_ANTI_REPLAY;
1974  if (sa->udp_encap)
1975  {
1976  a.flags |= IPSEC_SA_FLAG_IS_TUNNEL;
1977  a.flags |= IPSEC_SA_FLAG_UDP_ENCAP;
1978  }
1979  if (ikev2_natt_active (sa))
1980  a.flags |= IPSEC_SA_FLAG_UDP_ENCAP;
1981  a.is_rekey = is_rekey;
1982 
1983  tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ESN);
1984  if (tr && tr->esn_type)
1985  a.flags |= IPSEC_SA_FLAG_USE_ESN;
1986 
1987  tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1988  if (tr)
1989  {
1990  if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
1991  {
1992  switch (tr->key_len)
1993  {
1994  case 16:
1995  encr_type = IPSEC_CRYPTO_ALG_AES_CBC_128;
1996  break;
1997  case 24:
1998  encr_type = IPSEC_CRYPTO_ALG_AES_CBC_192;
1999  break;
2000  case 32:
2001  encr_type = IPSEC_CRYPTO_ALG_AES_CBC_256;
2002  break;
2003  default:
2005  return 1;
2006  break;
2007  }
2008  }
2009  else if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16
2010  && tr->key_len)
2011  {
2012  switch (tr->key_len)
2013  {
2014  case 16:
2015  encr_type = IPSEC_CRYPTO_ALG_AES_GCM_128;
2016  break;
2017  case 24:
2018  encr_type = IPSEC_CRYPTO_ALG_AES_GCM_192;
2019  break;
2020  case 32:
2021  encr_type = IPSEC_CRYPTO_ALG_AES_GCM_256;
2022  break;
2023  default:
2025  return 1;
2026  break;
2027  }
2028  is_aead = 1;
2029  }
2030  else
2031  {
2033  return 1;
2034  }
2035  }
2036  else
2037  {
2039  return 1;
2040  }
2041  a.encr_type = encr_type;
2042 
2043  if (!is_aead)
2044  {
2045  tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_INTEG);
2046  if (tr)
2047  {
2048  switch (tr->integ_type)
2049  {
2050  case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_256_128:
2051  integ_type = IPSEC_INTEG_ALG_SHA_256_128;
2052  break;
2053  case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_384_192:
2054  integ_type = IPSEC_INTEG_ALG_SHA_384_192;
2055  break;
2056  case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_512_256:
2057  integ_type = IPSEC_INTEG_ALG_SHA_512_256;
2058  break;
2059  case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96:
2060  integ_type = IPSEC_INTEG_ALG_SHA1_96;
2061  break;
2062  default:
2064  return 1;
2065  }
2066  }
2067  else
2068  {
2070  return 1;
2071  }
2072  }
2073  else
2074  {
2075  integ_type = IPSEC_INTEG_ALG_NONE;
2076  }
2077 
2078  a.integ_type = integ_type;
2079  ikev2_calc_child_keys (sa, child);
2080 
2081  if (sa->is_initiator)
2082  {
2083  ipsec_mk_key (&a.loc_ikey, child->sk_ai, vec_len (child->sk_ai));
2084  ipsec_mk_key (&a.rem_ikey, child->sk_ar, vec_len (child->sk_ar));
2085  ipsec_mk_key (&a.loc_ckey, child->sk_ei, vec_len (child->sk_ei));
2086  ipsec_mk_key (&a.rem_ckey, child->sk_er, vec_len (child->sk_er));
2087  if (is_aead)
2088  {
2089  a.salt_remote = child->salt_er;
2090  a.salt_local = child->salt_ei;
2091  }
2093  }
2094  else
2095  {
2096  ipsec_mk_key (&a.loc_ikey, child->sk_ar, vec_len (child->sk_ar));
2097  ipsec_mk_key (&a.rem_ikey, child->sk_ai, vec_len (child->sk_ai));
2098  ipsec_mk_key (&a.loc_ckey, child->sk_er, vec_len (child->sk_er));
2099  ipsec_mk_key (&a.rem_ckey, child->sk_ei, vec_len (child->sk_ei));
2100  if (is_aead)
2101  {
2102  a.salt_remote = child->salt_ei;
2103  a.salt_local = child->salt_er;
2104  }
2105  a.dst_port =
2107  a.src_port = sa->ipsec_over_udp_port;
2108  }
2109 
2110  if (sa->is_initiator && sa->profile_index != ~0)
2111  p = pool_elt_at_index (km->profiles, sa->profile_index);
2112 
2113  if (p && p->lifetime)
2114  {
2115  child->time_to_expiration = vlib_time_now (vm) + p->lifetime;
2116  if (p->lifetime_jitter)
2117  {
2118  // This is not much better than rand(3), which Coverity warns
2119  // is unsuitable for security applications; random_u32 is
2120  // however fast. If this perturbance to the expiration time
2121  // needs to use a better RNG then we may need to use something
2122  // like /dev/urandom which has significant overhead.
2123  u32 rnd = (u32) (vlib_time_now (vm) * 1e6);
2124  rnd = random_u32 (&rnd);
2125 
2126  child->time_to_expiration += 1 + (rnd % p->lifetime_jitter);
2127  }
2128  }
2129 
2130  if (thread_index & 0xffffffc0)
2131  ikev2_elog_error ("error: thread index exceeds max range 0x3f!");
2132 
2133  if (child_index & 0xfffff000 || sa_index & 0xfffff000)
2134  ikev2_elog_error ("error: sa/child index exceeds max range 0xfff!");
2135 
2136  child->local_sa_id =
2137  a.local_sa_id =
2138  ikev2_mk_local_sa_id (sa_index, child_index, thread_index);
2139 
2140  u32 remote_sa_id = ikev2_mk_remote_sa_id (sa_index, child_index,
2141  thread_index);
2142 
2143  if (is_rekey)
2144  {
2145  /* create a new remote SA ID to keep the old SA for a bit longer
2146  * so the peer has some time to swap their SAs */
2147 
2148  /* use most significat bit of child index part in id */
2149  u32 mask = 0x800;
2150  if (sa->current_remote_id_mask)
2151  {
2152  sa->old_remote_id = a.old_remote_sa_id = remote_sa_id | mask;
2153  sa->current_remote_id_mask = 0;
2154  }
2155  else
2156  {
2157  sa->old_remote_id = a.old_remote_sa_id = remote_sa_id;
2159  remote_sa_id |= mask;
2160  }
2161  sa->old_id_expiration = 3.0;
2162  sa->old_remote_id_present = 1;
2163  }
2164 
2165  child->remote_sa_id = a.remote_sa_id = remote_sa_id;
2166 
2167  a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
2169 
2171  (u8 *) & a, sizeof (a));
2172  return 0;
2173 }
2174 
2175 typedef struct
2176 {
2177  ip46_address_t local_ip;
2178  ip46_address_t remote_ip;
2183 
2186 {
2187  u32 mask = 0x800;
2188  if (mask & id)
2189  return id & ~mask;
2190  return id | mask;
2191 }
2192 
2193 static void
2195 {
2196  ikev2_main_t *km = &ikev2_main;
2197  ipip_tunnel_t *ipip = NULL;
2198  u32 sw_if_index;
2199 
2200  if (~0 == a->sw_if_index)
2201  {
2202  /* *INDENT-OFF* */
2203  ipip_tunnel_key_t key = {
2204  .src = a->local_ip,
2205  .dst = a->remote_ip,
2206  .transport = IPIP_TRANSPORT_IP4,
2207  .fib_index = 0,
2208  };
2209  /* *INDENT-ON* */
2210 
2211  ipip = ipip_tunnel_db_find (&key);
2212 
2213  if (ipip)
2214  {
2215  sw_if_index = ipip->sw_if_index;
2216  hash_unset (km->sw_if_indices, ipip->sw_if_index);
2217  }
2218  else
2219  sw_if_index = ~0;
2220  }
2221  else
2222  {
2223  sw_if_index = a->sw_if_index;
2224  vnet_sw_interface_admin_down (vnet_get_main (), sw_if_index);
2225  }
2226 
2227  if (~0 != sw_if_index)
2228  ipsec_tun_protect_del (sw_if_index, NULL);
2229 
2233 
2234  if (ipip)
2235  ipip_del_tunnel (ipip->sw_if_index);
2236 }
2237 
2238 static int
2240  ikev2_child_sa_t * child)
2241 {
2243 
2244  clib_memset (&a, 0, sizeof (a));
2245 
2246  if (sa->is_initiator)
2247  {
2248  ip_address_to_46 (&sa->iaddr, &a.local_ip);
2249  ip_address_to_46 (&sa->raddr, &a.remote_ip);
2250  }
2251  else
2252  {
2253  ip_address_to_46 (&sa->raddr, &a.local_ip);
2254  ip_address_to_46 (&sa->iaddr, &a.remote_ip);
2255  }
2256 
2257  a.remote_sa_id = child->remote_sa_id;
2258  a.local_sa_id = child->local_sa_id;
2259  a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
2260 
2262  sizeof (a));
2263  return 0;
2264 }
2265 
2266 static u32
2268  ike_header_t * ike, void *user, udp_header_t * udp)
2269 {
2270  ikev2_main_t *km = &ikev2_main;
2271  u16 buffer_data_size = vlib_buffer_get_default_data_size (km->vlib_main);
2272  v8 *integ = 0;
2273  ike_payload_header_t *ph;
2274  u16 plen;
2275  u32 tlen = 0;
2276 
2277  ikev2_sa_transform_t *tr_encr, *tr_integ;
2278  tr_encr =
2279  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
2280  tr_integ =
2281  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
2282 
2283  ikev2_payload_chain_t *chain = 0;
2284  ikev2_payload_new_chain (chain);
2285 
2286  if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
2287  {
2288  if (sa->r_proposals == 0)
2289  {
2290  ikev2_payload_add_notify (chain,
2291  IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
2293  }
2294  else if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
2295  {
2296  u8 *data = vec_new (u8, 2);
2297  ikev2_sa_transform_t *tr_dh;
2298  tr_dh =
2300  IKEV2_TRANSFORM_TYPE_DH);
2301  ASSERT (tr_dh && tr_dh->dh_type);
2302 
2303  data[0] = (tr_dh->dh_type >> 8) & 0xff;
2304  data[1] = (tr_dh->dh_type) & 0xff;
2305 
2306  ikev2_payload_add_notify (chain,
2307  IKEV2_NOTIFY_MSG_INVALID_KE_PAYLOAD,
2308  data);
2309  vec_free (data);
2311  }
2312  else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2313  {
2314  u8 *data = vec_new (u8, 1);
2315 
2316  data[0] = sa->unsupported_cp;
2317  ikev2_payload_add_notify (chain,
2318  IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2319  data);
2320  vec_free (data);
2321  }
2322  else
2323  {
2324  ASSERT (udp);
2325 
2326  ike->rspi = clib_host_to_net_u64 (sa->rspi);
2327  ikev2_payload_add_sa (chain, sa->r_proposals);
2328  ikev2_payload_add_ke (chain, sa->dh_group, sa->r_dh_data);
2329  ikev2_payload_add_nonce (chain, sa->r_nonce);
2330 
2331  u8 *nat_detection_sha1 =
2332  ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa->ispi),
2333  clib_host_to_net_u64 (sa->rspi),
2334  &sa->raddr, udp->dst_port);
2335  ikev2_payload_add_notify (chain,
2336  IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
2337  nat_detection_sha1);
2338  vec_free (nat_detection_sha1);
2339  nat_detection_sha1 =
2340  ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa->ispi),
2341  clib_host_to_net_u64 (sa->rspi),
2342  &sa->iaddr, udp->src_port);
2343  ikev2_payload_add_notify (chain,
2344  IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
2345  nat_detection_sha1);
2346  vec_free (nat_detection_sha1);
2347  }
2348  }
2349  else if (ike->exchange == IKEV2_EXCHANGE_IKE_AUTH)
2350  {
2351  if (sa->state == IKEV2_STATE_AUTHENTICATED)
2352  {
2355  ikev2_payload_add_auth (chain, &sa->r_auth);
2356  ikev2_payload_add_sa (chain, sa->childs[0].r_proposals);
2359  }
2360  else if (sa->state == IKEV2_STATE_AUTH_FAILED)
2361  {
2362  ikev2_payload_add_notify (chain,
2363  IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED,
2364  0);
2366  }
2367  else if (sa->state == IKEV2_STATE_TS_UNACCEPTABLE)
2368  {
2369  ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_TS_UNACCEPTABLE,
2370  0);
2372  ikev2_payload_add_auth (chain, &sa->r_auth);
2373  }
2374  else if (sa->state == IKEV2_STATE_NO_PROPOSAL_CHOSEN)
2375  {
2376  ikev2_payload_add_notify (chain,
2377  IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
2379  ikev2_payload_add_auth (chain, &sa->r_auth);
2382  }
2383  else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2384  {
2385  u8 *data = vec_new (u8, 1);
2386 
2387  data[0] = sa->unsupported_cp;
2388  ikev2_payload_add_notify (chain,
2389  IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2390  data);
2391  vec_free (data);
2392  }
2393  else if (sa->state == IKEV2_STATE_SA_INIT)
2394  {
2397  ikev2_payload_add_auth (chain, &sa->i_auth);
2398  ikev2_payload_add_sa (chain, sa->childs[0].i_proposals);
2401  ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_INITIAL_CONTACT,
2402  0);
2403  }
2404  else
2405  {
2406  ikev2_set_state (sa, IKEV2_STATE_DELETED, ": unexpected IKE_AUTH");
2407  goto done;
2408  }
2409  }
2410  else if (ike->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2411  {
2412  /* if pending delete */
2413  if (sa->del)
2414  {
2415  if (sa->del[0].protocol_id == IKEV2_PROTOCOL_IKE)
2416  {
2417  if (ike_hdr_is_request (ike))
2418  ikev2_payload_add_delete (chain, sa->del);
2419 
2420  /* The response to a request that deletes the IKE SA is an empty
2421  INFORMATIONAL response. */
2423  }
2424  /* The response to a request that deletes ESP or AH SAs will contain
2425  delete payloads for the paired SAs going in the other direction. */
2426  else
2427  {
2428  ikev2_payload_add_delete (chain, sa->del);
2429  }
2430  vec_free (sa->del);
2431  sa->del = 0;
2432  }
2433  /* received N(AUTHENTICATION_FAILED) */
2434  else if (sa->state == IKEV2_STATE_AUTH_FAILED)
2435  {
2436  ikev2_set_state (sa, IKEV2_STATE_DELETED, ": auth failed");
2437  goto done;
2438  }
2439  /* received unsupported critical payload */
2440  else if (sa->unsupported_cp)
2441  {
2442  u8 *data = vec_new (u8, 1);
2443 
2444  data[0] = sa->unsupported_cp;
2445  ikev2_payload_add_notify (chain,
2446  IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2447  data);
2448  vec_free (data);
2449  sa->unsupported_cp = 0;
2450  }
2451  /* else send empty response */
2452  }
2453  else if (ike->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2454  {
2455  if (sa->is_initiator)
2456  {
2457 
2458  ikev2_sa_proposal_t *proposals = (ikev2_sa_proposal_t *) user;
2459  ikev2_notify_t notify;
2460  u8 *data = vec_new (u8, 4);
2461  clib_memset (&notify, 0, sizeof (notify));
2463  notify.spi = sa->childs[0].i_proposals->spi;
2464  *(u32 *) data = clib_host_to_net_u32 (notify.spi);
2465 
2466  ikev2_payload_add_sa (chain, proposals);
2467  ikev2_payload_add_nonce (chain, sa->i_nonce);
2470  ikev2_payload_add_notify_2 (chain, IKEV2_NOTIFY_MSG_REKEY_SA, data,
2471  &notify);
2472 
2473  vec_free (data);
2474  }
2475  else
2476  {
2477  if (vec_len (sa->rekey) > 0)
2478  {
2479  ikev2_payload_add_sa (chain, sa->rekey[0].r_proposal);
2480  ikev2_payload_add_nonce (chain, sa->r_nonce);
2481  ikev2_payload_add_ts (chain, sa->rekey[0].tsi,
2483  ikev2_payload_add_ts (chain, sa->rekey[0].tsr,
2485  vec_del1 (sa->rekey, 0);
2486  }
2487  else if (sa->unsupported_cp)
2488  {
2489  u8 *data = vec_new (u8, 1);
2490 
2491  data[0] = sa->unsupported_cp;
2492  ikev2_payload_add_notify (chain,
2493  IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2494  data);
2495  vec_free (data);
2496  sa->unsupported_cp = 0;
2497  }
2498  else
2499  {
2500  ikev2_payload_add_notify (chain,
2501  IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS,
2502  0);
2503  }
2504  }
2505  }
2506 
2507  /* IKEv2 header */
2508  ike->version = IKE_VERSION_2;
2509  ike->nextpayload = IKEV2_PAYLOAD_SK;
2510  tlen = sizeof (*ike);
2511 
2512  if (sa->is_initiator)
2513  ike->flags |= IKEV2_HDR_FLAG_INITIATOR;
2514 
2515  if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
2516  {
2517  tlen += vec_len (chain->data);
2518  ike->nextpayload = chain->first_payload_type;
2519  ike->length = clib_host_to_net_u32 (tlen);
2520 
2521  if (tlen + b->current_length + b->current_data > buffer_data_size)
2522  {
2523  tlen = ~0;
2524  goto done;
2525  }
2526 
2527  clib_memcpy_fast (ike->payload, chain->data, vec_len (chain->data));
2528 
2529  /* store whole IKE payload - needed for PSK auth */
2531  vec_add (sa->last_sa_init_res_packet_data, ike, tlen);
2532  }
2533  else
2534  {
2536  ikev2_payload_chain_add_padding (chain, tr_encr->block_size);
2537 
2538  /* SK payload */
2539  plen = sizeof (*ph);
2540  ph = (ike_payload_header_t *) & ike->payload[0];
2541  ph->nextpayload = chain->first_payload_type;
2542  ph->flags = 0;
2543  int is_aead =
2544  tr_encr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16;
2545  int iv_len = is_aead ? IKEV2_GCM_IV_SIZE : tr_encr->block_size;
2546  plen += vec_len (chain->data) + iv_len;
2547 
2548  /* add space for hmac/tag */
2549  if (tr_integ)
2550  plen += tr_integ->key_trunc;
2551  else
2552  plen += IKEV2_GCM_ICV_SIZE;
2553  tlen += plen;
2554 
2555  if (tlen + b->current_length + b->current_data > buffer_data_size)
2556  {
2557  tlen = ~0;
2558  goto done;
2559  }
2560 
2561  /* payload and total length */
2562  ph->length = clib_host_to_net_u16 (plen);
2563  ike->length = clib_host_to_net_u32 (tlen);
2564 
2565  if (is_aead)
2566  {
2567  if (!ikev2_encrypt_aead_data (ptd, sa, tr_encr, chain->data,
2568  ph->payload, (u8 *) ike,
2569  sizeof (*ike) + sizeof (*ph),
2570  ph->payload + plen - sizeof (*ph) -
2572  {
2573  tlen = ~0;
2574  goto done;
2575  }
2576  }
2577  else
2578  {
2579  if (!ikev2_encrypt_data
2580  (ptd, sa, tr_encr, chain->data, ph->payload))
2581  {
2582  tlen = ~0;
2583  goto done;
2584  }
2585  integ =
2586  ikev2_calc_integr (tr_integ,
2587  sa->is_initiator ? sa->sk_ai : sa->sk_ar,
2588  (u8 *) ike, tlen - tr_integ->key_trunc);
2589  clib_memcpy_fast (ike->payload + tlen - tr_integ->key_trunc -
2590  sizeof (*ike), integ, tr_integ->key_trunc);
2591  }
2592 
2593  /* store whole IKE payload - needed for retransmit */
2595  vec_add (sa->last_res_packet_data, ike, tlen);
2596  }
2597 
2598 done:
2600  vec_free (integ);
2601  return tlen;
2602 }
2603 
2604 static u32
2605 ikev2_retransmit_sa_init_one (ikev2_sa_t * sa, ike_header_t * ike,
2607  u32 rlen)
2608 {
2609  int p = 0;
2610  ike_header_t *tmp;
2611  u8 payload = ike->nextpayload;
2612 
2613  if (sa->ispi != clib_net_to_host_u64 (ike->ispi) ||
2614  ip_address_cmp (&sa->iaddr, &iaddr) ||
2615  ip_address_cmp (&sa->raddr, &raddr))
2616  {
2617  return 0;
2618  }
2619 
2620  while (p < rlen && payload != IKEV2_PAYLOAD_NONE)
2621  {
2622  ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
2623  u32 plen = clib_net_to_host_u16 (ikep->length);
2624 
2625  if (plen < sizeof (ike_payload_header_t))
2626  return ~0;
2627 
2628  if (payload == IKEV2_PAYLOAD_NONCE &&
2629  !clib_memcmp (sa->i_nonce, ikep->payload, plen - sizeof (*ikep)))
2630  {
2631  /* req is retransmit */
2632  if (sa->state == IKEV2_STATE_SA_INIT)
2633  {
2634  tmp = (ike_header_t *) sa->last_sa_init_res_packet_data;
2635  u32 slen = clib_net_to_host_u32 (tmp->length);
2636  ike->ispi = tmp->ispi;
2637  ike->rspi = tmp->rspi;
2638  ike->nextpayload = tmp->nextpayload;
2639  ike->version = tmp->version;
2640  ike->exchange = tmp->exchange;
2641  ike->flags = tmp->flags;
2642  ike->msgid = tmp->msgid;
2643  ike->length = tmp->length;
2644  clib_memcpy_fast (ike->payload, tmp->payload,
2645  slen - sizeof (*ike));
2646  ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2647  "ispi %lx IKE_SA_INIT retransmit "
2648  "from %d.%d.%d.%d to %d.%d.%d.%d",
2649  ike->ispi,
2650  ip_addr_v4 (&raddr).as_u32,
2651  ip_addr_v4 (&iaddr).as_u32);
2652  return slen;
2653  }
2654  /* else ignore req */
2655  else
2656  {
2657  ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2658  "ispi %lx IKE_SA_INIT ignore "
2659  "from %d.%d.%d.%d to %d.%d.%d.%d",
2660  ike->ispi,
2661  ip_addr_v4 (&raddr).as_u32,
2662  ip_addr_v4 (&iaddr).as_u32);
2663  return ~0;
2664  }
2665  }
2666  payload = ikep->nextpayload;
2667  p += plen;
2668  }
2669 
2670  return 0;
2671 }
2672 
2673 static u32
2675  ip_address_t raddr, u32 rlen)
2676 {
2677  ikev2_sa_t *sa;
2678  u32 res;
2680 
2681  /* *INDENT-OFF* */
2682  pool_foreach (sa, ptd->sas) {
2683  res = ikev2_retransmit_sa_init_one (sa, ike, iaddr, raddr, rlen);
2684  if (res)
2685  return res;
2686  }
2687  /* *INDENT-ON* */
2688 
2689  /* req is not retransmit */
2690  return 0;
2691 }
2692 
2693 static u32
2694 ikev2_retransmit_resp (ikev2_sa_t * sa, ike_header_t * ike)
2695 {
2696  if (ike_hdr_is_response (ike))
2697  return 0;
2698 
2699  u32 msg_id = clib_net_to_host_u32 (ike->msgid);
2700 
2701  /* new req */
2702  if (msg_id > sa->last_msg_id)
2703  {
2704  sa->last_msg_id = msg_id;
2705  return 0;
2706  }
2707 
2708  /* retransmitted req */
2709  if (msg_id == sa->last_msg_id)
2710  {
2711  ike_header_t *tmp = (ike_header_t *) sa->last_res_packet_data;
2712  u32 slen = clib_net_to_host_u32 (tmp->length);
2713  ike->ispi = tmp->ispi;
2714  ike->rspi = tmp->rspi;
2715  ike->nextpayload = tmp->nextpayload;
2716  ike->version = tmp->version;
2717  ike->exchange = tmp->exchange;
2718  ike->flags = tmp->flags;
2719  ike->msgid = tmp->msgid;
2720  ike->length = tmp->length;
2721  clib_memcpy_fast (ike->payload, tmp->payload, slen - sizeof (*ike));
2722  ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE retransmit msgid %d",
2723  msg_id, ip_addr_v4 (&sa->raddr).as_u32,
2724  ip_addr_v4 (&sa->iaddr).as_u32);
2725  return slen;
2726  }
2727 
2728  /* old req ignore */
2729  ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE req ignore msgid %d",
2730  msg_id, ip_addr_v4 (&sa->raddr).as_u32,
2731  ip_addr_v4 (&sa->iaddr).as_u32);
2732  return ~0;
2733 }
2734 
2735 static void
2737 {
2738  ikev2_main_t *km = &ikev2_main;
2740  sa->profile_index = ~0;
2741 }
2742 
2743 static void
2745 {
2746  ikev2_main_t *km = &ikev2_main;
2747  uword *p = hash_get (km->sa_by_ispi, *ispi);
2748  if (p)
2749  {
2750  ikev2_sa_t *sai = pool_elt_at_index (km->sais, p[0]);
2751  hash_unset (km->sa_by_ispi, sai->ispi);
2752  ikev2_sa_free_all_vec (sai);
2753  pool_put (km->sais, sai);
2754  }
2755 }
2756 
2757 static void
2759 {
2761  sizeof (ispi));
2762 }
2763 
2766 {
2767  if (sa->is_initiator)
2768  {
2769  ip_address_copy_addr (&ih->dst_address, &sa->raddr);
2770  ip_address_copy_addr (&ih->src_address, &sa->iaddr);
2771  }
2772  else
2773  {
2774  ip_address_copy_addr (&ih->dst_address, &sa->iaddr);
2775  ip_address_copy_addr (&ih->src_address, &sa->raddr);
2776  }
2777 }
2778 
2781 {
2782  if (sa->is_initiator)
2783  {
2784  ip_address_copy_addr (&ih->dst_address, &sa->raddr);
2785  ip_address_copy_addr (&ih->src_address, &sa->iaddr);
2786  }
2787  else
2788  {
2789  ip_address_copy_addr (&ih->dst_address, &sa->iaddr);
2790  ip_address_copy_addr (&ih->src_address, &sa->raddr);
2791  }
2792 }
2793 
2796  const void *raddr, const int af)
2797 {
2798  ip_address_set (&sa->raddr, raddr, af);
2799  ip_address_set (&sa->iaddr, iaddr, af);
2800 }
2801 
2802 static void
2804  ip6_header_t * ip6, u8 is_ip4)
2805 {
2806  u32 src, dst;
2807  if (is_ip4)
2808  {
2809  src = ip4->src_address.as_u32;
2810  dst = ip4->dst_address.as_u32;
2811  }
2812  else
2813  {
2814  src = ip6->src_address.as_u32[3];
2815  dst = ip6->dst_address.as_u32[3];
2816  }
2817  ikev2_elog_uint_peers (IKEV2_LOG_WARNING, "IKEv2 exchange %d "
2818  "received from %d.%d.%d.%d to %d.%d.%d.%d",
2819  exchange, src, dst);
2820 }
2821 
2822 static void
2824 {
2826 
2828  return;
2829 
2834 }
2835 
2839  u8 is_ip4)
2840 {
2841  u32 n_left = frame->n_vectors, *from;
2842  ikev2_main_t *km = &ikev2_main;
2843  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
2844  u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
2846  int res;
2847 
2848  from = vlib_frame_vector_args (frame);
2849  vlib_get_buffers (vm, from, bufs, n_left);
2850  b = bufs;
2851 
2852  while (n_left > 0)
2853  {
2854  vlib_buffer_t *b0 = b[0];
2855  next[0] = is_ip4 ? IKEV2_NEXT_IP4_ERROR_DROP
2857  ip4_header_t *ip40 = 0;
2858  ip6_header_t *ip60 = 0;
2859  udp_header_t *udp0;
2860  ike_header_t *ike0;
2861  ikev2_sa_t *sa0 = 0;
2862  ikev2_sa_t sa; /* temporary store for SA */
2863  u32 rlen, slen = 0;
2864  int ip_hdr_sz = 0;
2865  int is_req = 0, has_non_esp_marker = 0;
2866 
2867  ASSERT (0 == b0->punt_reason
2868  || (is_ip4
2869  && b0->punt_reason ==
2870  ipsec_punt_reason[IPSEC_PUNT_IP4_SPI_UDP_0]));
2871 
2872  if (is_ip4
2873  && b0->punt_reason == ipsec_punt_reason[IPSEC_PUNT_IP4_SPI_UDP_0])
2874  {
2875  u8 *ptr = vlib_buffer_get_current (b0);
2876  ip40 = (ip4_header_t *) ptr;
2877  ptr += sizeof (*ip40);
2878  udp0 = (udp_header_t *) ptr;
2879  ptr += sizeof (*udp0);
2880  ike0 = (ike_header_t *) ptr;
2881  ip_hdr_sz = sizeof (*ip40);
2882  }
2883  else
2884  {
2885  u8 *ipx_hdr = b0->data + vnet_buffer (b0)->l3_hdr_offset;
2886  ike0 = vlib_buffer_get_current (b0);
2887  vlib_buffer_advance (b0, -sizeof (*udp0));
2888  udp0 = vlib_buffer_get_current (b0);
2889 
2890  if (is_ip4)
2891  {
2892  ip40 = (ip4_header_t *) ipx_hdr;
2893  ip_hdr_sz = sizeof (*ip40);
2894  }
2895  else
2896  {
2897  ip60 = (ip6_header_t *) ipx_hdr;
2898  ip_hdr_sz = sizeof (*ip60);
2899  }
2900  vlib_buffer_advance (b0, -ip_hdr_sz);
2901  }
2902 
2903  rlen = b0->current_length - ip_hdr_sz - sizeof (*udp0);
2904 
2905  /* check for non-esp marker */
2906  if (*((u32 *) ike0) == 0)
2907  {
2908  ike0 =
2909  (ike_header_t *) ((u8 *) ike0 + sizeof (ikev2_non_esp_marker));
2910  rlen -= sizeof (ikev2_non_esp_marker);
2911  has_non_esp_marker = 1;
2912  }
2913 
2914  if (clib_net_to_host_u32 (ike0->length) != rlen)
2915  {
2917  IKEV2_ERROR_BAD_LENGTH, 1);
2918  goto dispatch0;
2919  }
2920 
2921  if (ike0->version != IKE_VERSION_2)
2922  {
2924  IKEV2_ERROR_NOT_IKEV2, 1);
2925  goto dispatch0;
2926  }
2927 
2928  if (ike0->exchange == IKEV2_EXCHANGE_SA_INIT)
2929  {
2930  sa0 = &sa;
2931  clib_memset (sa0, 0, sizeof (*sa0));
2932 
2933  if (ike_hdr_is_initiator (ike0))
2934  {
2935  if (ike0->rspi == 0)
2936  {
2937  if (is_ip4)
2938  ikev2_set_ip_address (sa0, &ip40->src_address,
2939  &ip40->dst_address, AF_IP4);
2940  else
2941  ikev2_set_ip_address (sa0, &ip60->src_address,
2942  &ip60->dst_address, AF_IP6);
2943 
2944  sa0->dst_port = clib_net_to_host_u16 (udp0->src_port);
2945 
2946  slen =
2947  ikev2_retransmit_sa_init (ike0, sa0->iaddr,
2948  sa0->raddr, rlen);
2949  if (slen)
2950  {
2952  ~0 ==
2953  slen ?
2954  IKEV2_ERROR_IKE_SA_INIT_IGNORE
2955  :
2956  IKEV2_ERROR_IKE_SA_INIT_RETRANSMIT,
2957  1);
2958  goto dispatch0;
2959  }
2960 
2961  res = ikev2_process_sa_init_req (vm, sa0, ike0, udp0, rlen);
2962  if (!res)
2964  IKEV2_ERROR_MALFORMED_PACKET,
2965  1);
2966 
2967  if (sa0->state == IKEV2_STATE_SA_INIT)
2968  {
2970  sa0->r_proposals =
2974  }
2975 
2976  if (sa0->state == IKEV2_STATE_SA_INIT
2978  {
2979  ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
2980  slen = ikev2_generate_message (b0, sa0, ike0, 0, udp0);
2981  if (~0 == slen)
2983  IKEV2_ERROR_NO_BUFF_SPACE,
2984  1);
2985  }
2986 
2987  if (sa0->state == IKEV2_STATE_SA_INIT)
2988  {
2989  /* add SA to the pool */
2990  pool_get (ptd->sas, sa0);
2991  clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
2992  ikev2_init_sa (vm, sa0);
2993  hash_set (ptd->sa_by_rspi, sa0->rspi, sa0 - ptd->sas);
2994  }
2995  else
2996  {
2997  ikev2_sa_free_all_vec (sa0);
2998  }
2999  }
3000  }
3001  else //received sa_init without initiator flag
3002  {
3003  if (is_ip4)
3004  ikev2_set_ip_address (sa0, &ip40->dst_address,
3005  &ip40->src_address, AF_IP4);
3006  else
3007  ikev2_set_ip_address (sa0, &ip60->dst_address,
3008  &ip60->src_address, AF_IP6);
3009 
3010  ikev2_process_sa_init_resp (vm, sa0, ike0, udp0, rlen);
3011 
3012  if (sa0->state == IKEV2_STATE_SA_INIT)
3013  {
3014  is_req = 1;
3015  ike0->exchange = IKEV2_EXCHANGE_IKE_AUTH;
3016  uword *p = hash_get (km->sa_by_ispi, sa0->ispi);
3017  if (p)
3018  {
3019  ikev2_sa_t *sai = pool_elt_at_index (km->sais, p[0]);
3020 
3022  (&sai->init_response_received, 0, 1))
3023  {
3024  ikev2_complete_sa_data (sa0, sai);
3025  ikev2_calc_keys (sa0);
3026  ikev2_sa_auth_init (sa0);
3027  ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3028  ike0->msgid =
3029  clib_net_to_host_u32 (sai->last_init_msg_id);
3030  sa0->last_init_msg_id = sai->last_init_msg_id + 1;
3031  slen =
3032  ikev2_generate_message (b0, sa0, ike0, 0, udp0);
3033  if (~0 == slen)
3035  node->node_index,
3036  IKEV2_ERROR_NO_BUFF_SPACE,
3037  1);
3038  }
3039  else
3040  {
3041  /* we've already processed sa-init response */
3042  sa0->state = IKEV2_STATE_UNKNOWN;
3043  }
3044  }
3045  }
3046 
3047  if (sa0->state == IKEV2_STATE_SA_INIT)
3048  {
3049  /* add SA to the pool */
3050  pool_get (ptd->sas, sa0);
3051  clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
3052  hash_set (ptd->sa_by_rspi, sa0->rspi, sa0 - ptd->sas);
3053  }
3054  else
3055  {
3056  ikev2_sa_free_all_vec (sa0);
3057  }
3058  }
3059  }
3060  else if (ike0->exchange == IKEV2_EXCHANGE_IKE_AUTH)
3061  {
3062  uword *p;
3063  p = hash_get (ptd->sa_by_rspi, clib_net_to_host_u64 (ike0->rspi));
3064  if (p)
3065  {
3066  sa0 = pool_elt_at_index (ptd->sas, p[0]);
3067  slen = ikev2_retransmit_resp (sa0, ike0);
3068  if (slen)
3069  {
3071  ~0 ==
3072  slen ?
3073  IKEV2_ERROR_IKE_REQ_IGNORE
3074  :
3075  IKEV2_ERROR_IKE_REQ_RETRANSMIT,
3076  1);
3077  goto dispatch0;
3078  }
3079 
3080  sa0->dst_port = clib_net_to_host_u16 (udp0->src_port);
3081  res = ikev2_process_auth_req (vm, sa0, ike0, rlen);
3082  if (res)
3083  ikev2_sa_auth (sa0);
3084  else
3086  IKEV2_ERROR_MALFORMED_PACKET, 1);
3087  if (sa0->state == IKEV2_STATE_AUTHENTICATED)
3088  {
3089  ikev2_initial_contact_cleanup (ptd, sa0);
3090  ikev2_sa_match_ts (sa0);
3091  if (sa0->state != IKEV2_STATE_TS_UNACCEPTABLE)
3093  &sa0->childs[0],
3094  p[0], 0, 0);
3095  }
3096 
3097  if (sa0->is_initiator)
3098  {
3099  ikev2_del_sa_init (sa0->ispi);
3100  }
3101  else
3102  {
3103  ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3104  slen = ikev2_generate_message (b0, sa0, ike0, 0, udp0);
3105  if (~0 == slen)
3107  IKEV2_ERROR_NO_BUFF_SPACE,
3108  1);
3109  }
3110  }
3111  }
3112  else if (ike0->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
3113  {
3114  uword *p;
3115  p = hash_get (ptd->sa_by_rspi, clib_net_to_host_u64 (ike0->rspi));
3116  if (p)
3117  {
3118  sa0 = pool_elt_at_index (ptd->sas, p[0]);
3119  slen = ikev2_retransmit_resp (sa0, ike0);
3120  if (slen)
3121  {
3123  ~0 ==
3124  slen ?
3125  IKEV2_ERROR_IKE_REQ_IGNORE
3126  :
3127  IKEV2_ERROR_IKE_REQ_RETRANSMIT,
3128  1);
3129  goto dispatch0;
3130  }
3131 
3132  res = ikev2_process_informational_req (vm, sa0, ike0, rlen);
3133  if (!res)
3134  {
3136  IKEV2_ERROR_MALFORMED_PACKET,
3137  1);
3138  slen = ~0;
3139  goto dispatch0;
3140  }
3141 
3142  if (sa0->del)
3143  {
3144  if (sa0->del[0].protocol_id != IKEV2_PROTOCOL_IKE)
3145  {
3146  ikev2_delete_t *d, *tmp, *resp = 0;
3147  vec_foreach (d, sa0->del)
3148  {
3149  ikev2_child_sa_t *ch_sa;
3150  ch_sa = ikev2_sa_get_child (sa0, d->spi,
3151  d->protocol_id,
3152  !sa0->is_initiator);
3153  if (ch_sa)
3154  {
3156  sa0, ch_sa);
3157  if (!sa0->is_initiator)
3158  {
3159  vec_add2 (resp, tmp, 1);
3160  tmp->protocol_id = d->protocol_id;
3161  tmp->spi = ch_sa->r_proposals[0].spi;
3162  }
3163  ikev2_sa_del_child_sa (sa0, ch_sa);
3164  }
3165  }
3166  if (!sa0->is_initiator)
3167  {
3168  vec_free (sa0->del);
3169  sa0->del = resp;
3170  }
3171  }
3172  }
3173  if (ike_hdr_is_request (ike0))
3174  {
3175  ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3176  slen = ikev2_generate_message (b0, sa0, ike0, 0, udp0);
3177  if (~0 == slen)
3179  IKEV2_ERROR_NO_BUFF_SPACE,
3180  1);
3181  }
3182  }
3183  }
3184  else if (ike0->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
3185  {
3186  uword *p;
3187  p = hash_get (ptd->sa_by_rspi, clib_net_to_host_u64 (ike0->rspi));
3188  if (p)
3189  {
3190  sa0 = pool_elt_at_index (ptd->sas, p[0]);
3191  slen = ikev2_retransmit_resp (sa0, ike0);
3192  if (slen)
3193  {
3195  ~0 ==
3196  slen ?
3197  IKEV2_ERROR_IKE_REQ_IGNORE
3198  :
3199  IKEV2_ERROR_IKE_REQ_RETRANSMIT,
3200  1);
3201  goto dispatch0;
3202  }
3203 
3204  res = ikev2_process_create_child_sa_req (vm, sa0, ike0, rlen);
3205  if (!res)
3206  {
3208  IKEV2_ERROR_MALFORMED_PACKET,
3209  1);
3210  slen = ~0;
3211  goto dispatch0;
3212  }
3213 
3214  if (sa0->rekey)
3215  {
3216  if (sa0->rekey[0].protocol_id != IKEV2_PROTOCOL_IKE)
3217  {
3218  if (sa0->childs)
3220  ikev2_child_sa_t *child;
3221  vec_add2 (sa0->childs, child, 1);
3222  clib_memset (child, 0, sizeof (*child));
3223  child->r_proposals = sa0->rekey[0].r_proposal;
3224  child->i_proposals = sa0->rekey[0].i_proposal;
3225  child->tsi = sa0->rekey[0].tsi;
3226  child->tsr = sa0->rekey[0].tsr;
3227  ikev2_create_tunnel_interface (vm, sa0, child, p[0],
3228  child - sa0->childs, 1);
3229  }
3230  if (ike_hdr_is_response (ike0))
3231  {
3232  vec_free (sa0->rekey);
3233  }
3234  else
3235  {
3236  ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3237  slen = ikev2_generate_message (b0, sa0, ike0, 0, udp0);
3238  if (~0 == slen)
3240  IKEV2_ERROR_NO_BUFF_SPACE,
3241  1);
3242  }
3243  }
3244  }
3245  }
3246  else
3247  {
3248  ikev2_elog_uint_peers_addr (ike0->exchange, ip40, ip60, is_ip4);
3249  }
3250 
3251  dispatch0:
3252  /* if we are sending packet back, rewrite headers */
3253  if (slen && ~0 != slen)
3254  {
3255  if (is_ip4)
3256  {
3257  next[0] = IKEV2_NEXT_IP4_LOOKUP;
3258  ikev2_rewrite_v4_addrs (sa0, ip40);
3259  }
3260  else
3261  {
3262  next[0] = IKEV2_NEXT_IP6_LOOKUP;
3263  ikev2_rewrite_v6_addrs (sa0, ip60);
3264  }
3265 
3266  if (is_req)
3267  {
3268  udp0->dst_port = udp0->src_port =
3269  clib_net_to_host_u16 (ikev2_get_port (sa0));
3270 
3271  if (udp0->dst_port == clib_net_to_host_u16 (IKEV2_PORT_NATT)
3272  && ikev2_natt_active (sa0))
3273  {
3274  if (!has_non_esp_marker)
3275  slen = ikev2_insert_non_esp_marker (ike0, slen);
3276  }
3277  }
3278  else
3279  {
3280  if (has_non_esp_marker)
3281  slen += sizeof (ikev2_non_esp_marker);
3282 
3283  u16 tp = udp0->dst_port;
3284  udp0->dst_port = udp0->src_port;
3285  udp0->src_port = tp;
3286  }
3287 
3288  udp0->length = clib_host_to_net_u16 (slen + sizeof (udp_header_t));
3289  udp0->checksum = 0;
3290  b0->current_length = slen + ip_hdr_sz + sizeof (udp_header_t);
3291  if (is_ip4)
3292  {
3293  ip40->length = clib_host_to_net_u16 (b0->current_length);
3294  ip40->checksum = ip4_header_checksum (ip40);
3295  }
3296  else
3297  {
3298  ip60->payload_length =
3299  clib_host_to_net_u16 (b0->current_length - sizeof (*ip60));
3300  }
3301  }
3302  /* delete sa */
3303  if (sa0 && (sa0->state == IKEV2_STATE_DELETED ||
3305  {
3307 
3308  vec_foreach (c, sa0->childs)
3310 
3311  ikev2_delete_sa (ptd, sa0);
3312  }
3314  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
3315  {
3316 
3317  ikev2_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
3318  t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3319  t->next_index = next[0];
3320  }
3321  n_left -= 1;
3322  next += 1;
3323  b += 1;
3324  }
3325 
3327  IKEV2_ERROR_PROCESSED, frame->n_vectors);
3328  vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
3329  return frame->n_vectors;
3330 }
3331 
3332 static uword
3334 {
3335  return ikev2_node_internal (vm, node, frame, 1 /* is_ip4 */ );
3336 }
3337 
3338 static uword
3340 {
3341  return ikev2_node_internal (vm, node, frame, 0 /* is_ip4 */ );
3342 }
3343 
3344 /* *INDENT-OFF* */
3346  .function = ikev2_ip4,
3347  .name = "ikev2-ip4",
3348  .vector_size = sizeof (u32),
3349  .format_trace = format_ikev2_trace,
3351 
3352  .n_errors = ARRAY_LEN(ikev2_error_strings),
3353  .error_strings = ikev2_error_strings,
3354 
3355  .n_next_nodes = IKEV2_IP4_N_NEXT,
3356  .next_nodes = {
3357  [IKEV2_NEXT_IP4_LOOKUP] = "ip4-lookup",
3358  [IKEV2_NEXT_IP4_ERROR_DROP] = "error-drop",
3359  },
3360 };
3361 
3363  .function = ikev2_ip6,
3364  .name = "ikev2-ip6",
3365  .vector_size = sizeof (u32),
3366  .format_trace = format_ikev2_trace,
3368 
3369  .n_errors = ARRAY_LEN(ikev2_error_strings),
3370  .error_strings = ikev2_error_strings,
3371 
3372  .n_next_nodes = IKEV2_IP6_N_NEXT,
3373  .next_nodes = {
3374  [IKEV2_NEXT_IP6_LOOKUP] = "ip6-lookup",
3375  [IKEV2_NEXT_IP6_ERROR_DROP] = "error-drop",
3376  },
3377 };
3378 /* *INDENT-ON* */
3379 
3380 // set ikev2 proposals when vpp is used as initiator
3381 static clib_error_t *
3383  ikev2_transforms_set * ts,
3384  ikev2_sa_proposal_t ** proposals, int is_ike)
3385 {
3386  clib_error_t *r;
3387  ikev2_main_t *km = &ikev2_main;
3388  ikev2_sa_proposal_t *proposal;
3389  vec_add2 (*proposals, proposal, 1);
3391  int error;
3392 
3393  /* Encryption */
3394  error = 1;
3396  {
3397  if (td->type == IKEV2_TRANSFORM_TYPE_ENCR
3398  && td->encr_type == ts->crypto_alg
3399  && td->key_len == ts->crypto_key_size / 8)
3400  {
3401  u16 attr[2];
3402  attr[0] = clib_host_to_net_u16 (14 | (1 << 15));
3403  attr[1] = clib_host_to_net_u16 (td->key_len << 3);
3404  vec_add (td->attrs, (u8 *) attr, 4);
3405  vec_add1 (proposal->transforms, *td);
3406  td->attrs = 0;
3407 
3408  error = 0;
3409  break;
3410  }
3411  }
3412  if (error)
3413  {
3414  r = clib_error_return (0, "Unsupported algorithm");
3415  return r;
3416  }
3417 
3418  if (IKEV2_TRANSFORM_INTEG_TYPE_NONE != ts->integ_alg)
3419  {
3420  /* Integrity */
3421  error = 1;
3423  {
3424  if (td->type == IKEV2_TRANSFORM_TYPE_INTEG
3425  && td->integ_type == ts->integ_alg)
3426  {
3427  vec_add1 (proposal->transforms, *td);
3428  error = 0;
3429  break;
3430  }
3431  }
3432  if (error)
3433  {
3435  ("Didn't find any supported algorithm for IKEV2_TRANSFORM_TYPE_INTEG");
3436  r = clib_error_return (0, "Unsupported algorithm");
3437  return r;
3438  }
3439  }
3440 
3441  /* PRF */
3442  if (is_ike)
3443  {
3444  error = 1;
3446  {
3447  if (td->type == IKEV2_TRANSFORM_TYPE_PRF
3448  && td->prf_type == IKEV2_TRANSFORM_PRF_TYPE_PRF_HMAC_SHA2_256)
3449  {
3450  vec_add1 (proposal->transforms, *td);
3451  error = 0;
3452  break;
3453  }
3454  }
3455  if (error)
3456  {
3457  r = clib_error_return (0, "Unsupported algorithm");
3458  return r;
3459  }
3460  }
3461 
3462  /* DH */
3463  if (is_ike)
3464  {
3465  error = 1;
3467  {
3468  if (td->type == IKEV2_TRANSFORM_TYPE_DH && td->dh_type == ts->dh_type)
3469  {
3470  vec_add1 (proposal->transforms, *td);
3471  if (is_ike)
3472  {
3473  sa->dh_group = td->dh_type;
3474  }
3475  error = 0;
3476  break;
3477  }
3478  }
3479  if (error)
3480  {
3481  r = clib_error_return (0, "Unsupported algorithm");
3482  return r;
3483  }
3484  }
3485 
3486  if (!is_ike)
3487  {
3488  error = 1;
3490  {
3491  if (td->type == IKEV2_TRANSFORM_TYPE_ESN)
3492  {
3493  vec_add1 (proposal->transforms, *td);
3494  error = 0;
3495  }
3496  }
3497  if (error)
3498  {
3499  r = clib_error_return (0, "Unsupported algorithm");
3500  return r;
3501  }
3502  }
3503 
3504 
3505  return 0;
3506 }
3507 
3508 static ikev2_profile_t *
3510 {
3511  ikev2_main_t *km = &ikev2_main;
3512  uword *p;
3513 
3514  p = mhash_get (&km->profile_index_by_name, name);
3515  if (!p)
3516  return 0;
3517 
3518  return pool_elt_at_index (km->profiles, p[0]);
3519 }
3520 
3521 
3522 static void
3525 {
3526  ip4_header_t *ip40;
3527  ip6_header_t *ip60;
3528  udp_header_t *udp0;
3529  vlib_buffer_t *b0;
3530  vlib_frame_t *f;
3531  u32 *to_next;
3532 
3533  b0 = vlib_get_buffer (vm, bi0);
3534  vlib_buffer_advance (b0, -sizeof (udp_header_t));
3535  udp0 = vlib_buffer_get_current (b0);
3536  udp0->dst_port = clib_host_to_net_u16 (dst_port);
3537  udp0->src_port = clib_host_to_net_u16 (src_port);
3538  udp0->length = clib_host_to_net_u16 (len + sizeof (udp_header_t));
3539  udp0->checksum = 0;
3540 
3541  if (ip_addr_version (dst) == AF_IP4)
3542  {
3543  vlib_buffer_advance (b0, -sizeof (ip4_header_t));
3544  ip40 = vlib_buffer_get_current (b0);
3545  ip40->ip_version_and_header_length = 0x45;
3546  ip40->tos = 0;
3547  ip40->fragment_id = 0;
3548  ip40->flags_and_fragment_offset = 0;
3549  ip40->ttl = 0xff;
3550  ip40->protocol = IP_PROTOCOL_UDP;
3551  ip40->dst_address.as_u32 = ip_addr_v4 (dst).as_u32;
3552  ip40->src_address.as_u32 = ip_addr_v4 (src).as_u32;
3553  b0->current_length =
3554  len + sizeof (ip4_header_t) + sizeof (udp_header_t);
3555  ip40->length = clib_host_to_net_u16 (b0->current_length);
3556  ip40->checksum = ip4_header_checksum (ip40);
3557  }
3558  else
3559  {
3560  vlib_buffer_advance (b0, -sizeof (ip6_header_t));
3561  ip60 = vlib_buffer_get_current (b0);
3562 
3563  b0->current_length = len + sizeof (*ip60) + sizeof (udp_header_t);
3565  clib_host_to_net_u32 (0x6 << 28);
3566  ip60->payload_length =
3567  clib_host_to_net_u16 (b0->current_length - sizeof (*ip60));
3568  ip60->protocol = IP_PROTOCOL_UDP;
3569  ip60->hop_limit = 0xff;
3570  clib_memcpy_fast (ip60->src_address.as_u8, ip_addr_v6 (src).as_u8,
3571  sizeof (ip60->src_address));
3572  clib_memcpy_fast (ip60->dst_address.as_u8, ip_addr_v6 (dst).as_u8,
3573  sizeof (ip60->src_address));
3574  }
3575 
3576  b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3577  vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index;
3578  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
3579 
3580  u32 next_index = (ip_addr_version (dst) == AF_IP4) ?
3581  ip4_lookup_node.index : ip6_lookup_node.index;
3582 
3583  /* send the request */
3584  f = vlib_get_frame_to_node (vm, next_index);
3585  to_next = vlib_frame_vector_args (f);
3586  to_next[0] = bi0;
3587  f->n_vectors = 1;
3588  vlib_put_frame_to_node (vm, next_index, f);
3589 
3590 }
3591 
3592 static u32
3594 {
3595  u32 bi0;
3596  if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
3597  {
3598  *b = 0;
3599  return 0;
3600  }
3601  *b = vlib_get_buffer (vm, bi0);
3602  return bi0;
3603 }
3604 
3605 clib_error_t *
3607 {
3608  ikev2_main_t *km = &ikev2_main;
3609 
3610  if (km->pkey)
3611  EVP_PKEY_free (km->pkey);
3612  km->pkey = ikev2_load_key_file (file);
3613  if (km->pkey == NULL)
3614  return clib_error_return (0, "load key '%s' failed", file);
3615 
3616  return 0;
3617 }
3618 
3621 {
3622  ikev2_main_t *km = &ikev2_main;
3623  udp_dst_port_info_t *pi;
3624 
3625  uword *v = hash_get (km->udp_ports, port);
3626  pi = udp_get_dst_port_info (&udp_main, port, UDP_IP4);
3627 
3628  if (v)
3629  {
3630  /* IKE already uses this port, only increment reference counter */
3631  ASSERT (pi);
3632  v[0]++;
3633  }
3634  else
3635  {
3636  if (pi)
3637  return VNET_API_ERROR_UDP_PORT_TAKEN;
3638 
3639  udp_register_dst_port (km->vlib_main, port,
3640  ipsec4_tun_input_node.index, 1);
3641  hash_set (km->udp_ports, port, 1);
3642  }
3644  return 0;
3645 }
3646 
3649 {
3650  ikev2_main_t *km = &ikev2_main;
3651  uword *v;
3652 
3654  return;
3655 
3656  v = hash_get (km->udp_ports, p->ipsec_over_udp_port);
3657  if (!v)
3658  return;
3659 
3660  v[0]--;
3661 
3662  if (v[0] == 0)
3663  {
3666  }
3667 
3669 }
3670 
3671 static void
3674  ikev2_sa_t * sa, u8 send_notification)
3675 {
3676  ikev2_main_t *km = &ikev2_main;
3677  ip_address_t *src, *dst;
3678  vlib_buffer_t *b0;
3680 
3681  /* Create the Initiator notification for IKE SA removal */
3682  ike_header_t *ike0;
3683  u32 bi0 = 0;
3684  int len;
3685 
3686  vec_resize (sa->del, 1);
3688  sa->del->spi = sa->ispi;
3689 
3690  if (send_notification)
3691  {
3692  bi0 = ikev2_get_new_ike_header_buff (vm, &b0);
3693  if (!bi0)
3694  {
3695  ikev2_log_error ("buffer alloc failure");
3696  goto delete_sa;
3697  }
3698 
3699  ike0 = vlib_buffer_get_current (b0);
3700  ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3701  ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3702  ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3703  ike0->flags = 0;
3704  ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id);
3705  sa->last_init_msg_id += 1;
3706  len = ikev2_generate_message (b0, sa, ike0, 0, 0);
3707  if (~0 == len)
3708  return;
3709 
3710  if (ikev2_natt_active (sa))
3711  len = ikev2_insert_non_esp_marker (ike0, len);
3712 
3713  if (sa->is_initiator)
3714  {
3715  src = &sa->iaddr;
3716  dst = &sa->raddr;
3717  }
3718  else
3719  {
3720  dst = &sa->iaddr;
3721  src = &sa->raddr;
3722  }
3723 
3724  ikev2_send_ike (vm, src, dst, bi0, len,
3725  ikev2_get_port (sa), sa->dst_port, 0);
3726  }
3727 
3728 delete_sa:
3729  /* delete local SA */
3730  vec_foreach (c, sa->childs)
3732 
3733  u64 rspi = sa->rspi;
3734  ikev2_sa_free_all_vec (sa);
3735  uword *p = hash_get (tkm->sa_by_rspi, rspi);
3736  if (p)
3737  {
3738  hash_unset (tkm->sa_by_rspi, rspi);
3739  pool_put (tkm->sas, sa);
3740  }
3741 }
3742 
3743 static void
3745 {
3747  ikev2_sa_t *sa;
3748  u32 pi = p - km->profiles;
3749  u32 *sai;
3750  u32 *del_sai = 0;
3751 
3752  /* *INDENT-OFF* */
3753  pool_foreach (sa, km->sais) {
3754  if (pi == sa->profile_index)
3755  vec_add1 (del_sai, sa - km->sais);
3756  }
3757  /* *INDENT-ON* */
3758 
3759  vec_foreach (sai, del_sai)
3760  {
3761  sa = pool_elt_at_index (km->sais, sai[0]);
3762  ikev2_sa_free_all_vec (sa);
3763  hash_unset (km->sa_by_ispi, sa->ispi);
3764  pool_put (km->sais, sa);
3765  }
3766  vec_reset_length (del_sai);
3767 
3768  vec_foreach (tkm, km->per_thread_data)
3769  {
3770  /* *INDENT-OFF* */
3771  pool_foreach (sa, tkm->sas) {
3772  if (sa->profile_index != ~0 && pi == sa->profile_index)
3773  vec_add1 (del_sai, sa - tkm->sas);
3774  }
3775  /* *INDENT-ON* */
3776 
3777  vec_foreach (sai, del_sai)
3778  {
3779  sa = pool_elt_at_index (tkm->sas, sai[0]);
3781  }
3782 
3783  vec_reset_length (del_sai);
3784  }
3785 
3786  vec_free (del_sai);
3787 }
3788 
3789 static void
3791 {
3792  vec_free (p->name);
3793 
3794  vec_free (p->auth.data);
3795  if (p->auth.key)
3796  EVP_PKEY_free (p->auth.key);
3797 
3798  vec_free (p->loc_id.data);
3799  vec_free (p->rem_id.data);
3800 }
3801 
3802 clib_error_t *
3804 {
3805  ikev2_main_t *km = &ikev2_main;
3806  ikev2_profile_t *p;
3807 
3808  if (is_add)
3809  {
3810  if (ikev2_profile_index_by_name (name))
3811  return clib_error_return (0, "policy %v already exists", name);
3812 
3813  pool_get (km->profiles, p);
3814  clib_memset (p, 0, sizeof (*p));
3815  p->name = vec_dup (name);
3817  p->responder.sw_if_index = ~0;
3818  p->tun_itf = ~0;
3819  uword index = p - km->profiles;
3820  mhash_set_mem (&km->profile_index_by_name, name, &index, 0);
3821  }
3822  else
3823  {
3824  p = ikev2_profile_index_by_name (name);
3825  if (!p)
3826  return clib_error_return (0, "policy %v does not exists", name);
3827 
3830 
3831  ikev2_profile_free (p);
3832  pool_put (km->profiles, p);
3833  mhash_unset (&km->profile_index_by_name, name, 0);
3834  }
3835  return 0;
3836 }
3837 
3838 clib_error_t *
3840  u8 * auth_data, u8 data_hex_format)
3841 {
3842  ikev2_profile_t *p;
3843  clib_error_t *r;
3844 
3845  p = ikev2_profile_index_by_name (name);
3846 
3847  if (!p)
3848  {
3849  r = clib_error_return (0, "unknown profile %v", name);
3850  return r;
3851  }
3852 
3853  if (p->auth.key)
3854  EVP_PKEY_free (p->auth.key);
3855  vec_free (p->auth.data);
3856 
3857  p->auth.method = auth_method;
3858  p->auth.data = vec_dup (auth_data);
3859  p->auth.hex = data_hex_format;
3860 
3861  if (auth_method == IKEV2_AUTH_METHOD_RSA_SIG)
3862  {
3863  vec_add1 (p->auth.data, 0);
3865  if (p->auth.key == NULL)
3866  return clib_error_return (0, "load cert '%s' failed", p->auth.data);
3867  }
3868 
3869  return 0;
3870 }
3871 
3872 static int
3874 {
3875  return (id_type == IKEV2_ID_TYPE_ID_IPV4_ADDR ||
3876  id_type == IKEV2_ID_TYPE_ID_IPV6_ADDR ||
3877  id_type == IKEV2_ID_TYPE_ID_RFC822_ADDR ||
3878  id_type == IKEV2_ID_TYPE_ID_FQDN);
3879 }
3880 
3881 clib_error_t *
3883  int is_local)
3884 {
3885  ikev2_profile_t *p;
3886  clib_error_t *r;
3887 
3888  if (!ikev2_is_id_supported (id_type))
3889  {
3890  r = clib_error_return (0, "unsupported identity type %U",
3891  format_ikev2_id_type, id_type);
3892  return r;
3893  }
3894 
3895  p = ikev2_profile_index_by_name (name);
3896 
3897  if (!p)
3898  {
3899  r = clib_error_return (0, "unknown profile %v", name);
3900  return r;
3901  }
3902 
3903  if (is_local)
3904  {
3905  vec_free (p->loc_id.data);
3906  p->loc_id.type = id_type;
3907  p->loc_id.data = vec_dup (data);
3908  }
3909  else
3910  {
3911  vec_free (p->rem_id.data);
3912  p->rem_id.type = id_type;
3913  p->rem_id.data = vec_dup (data);
3914  }
3915 
3916  return 0;
3917 }
3918 
3921 {
3922  if (ip_addr_version (addr) == AF_IP4)
3924  else
3926 }
3927 
3930  const ip_address_t * end)
3931 {
3932  ip_address_copy (&ts->start_addr, start);
3933  ip_address_copy (&ts->end_addr, end);
3934 }
3935 
3936 clib_error_t *
3940 {
3941  ikev2_profile_t *p;
3942  clib_error_t *r;
3943 
3944  p = ikev2_profile_index_by_name (name);
3945 
3946  if (!p)
3947  {
3948  r = clib_error_return (0, "unknown profile %v", name);
3949  return r;
3950  }
3951 
3952  if (ip_addr_version (&start_addr) != ip_addr_version (&end_addr))
3953  return clib_error_return (0, "IP address version mismatch!");
3954 
3955  if (is_local)
3956  {
3957  ikev2_set_ts_addrs (&p->loc_ts, &start_addr, &end_addr);
3959  p->loc_ts.end_port = end_port;
3961  ikev2_set_ts_type (&p->loc_ts, &start_addr);
3962  }
3963  else
3964  {
3965  ikev2_set_ts_addrs (&p->rem_ts, &start_addr, &end_addr);
3967  p->rem_ts.end_port = end_port;
3969  ikev2_set_ts_type (&p->rem_ts, &start_addr);
3970  }
3971 
3972  return 0;
3973 }
3974 
3975 
3976 clib_error_t *
3979 {
3980  ikev2_profile_t *p;
3981  clib_error_t *r;
3982 
3983  p = ikev2_profile_index_by_name (name);
3984 
3985  if (!p)
3986  {
3987  r = clib_error_return (0, "unknown profile %v", name);
3988  return r;
3989  }
3990 
3992  ip_address_copy (&p->responder.addr, &addr);
3993 
3994  return 0;
3995 }
3996 
3997 clib_error_t *
3999  ikev2_transform_encr_type_t crypto_alg,
4001  ikev2_transform_dh_type_t dh_type,
4003 {
4004  ikev2_profile_t *p;
4005  clib_error_t *r;
4006 
4007  p = ikev2_profile_index_by_name (name);
4008 
4009  if (!p)
4010  {
4011  r = clib_error_return (0, "unknown profile %v", name);
4012  return r;
4013  }
4014 
4015  p->ike_ts.crypto_alg = crypto_alg;
4016  p->ike_ts.integ_alg = integ_alg;
4017  p->ike_ts.dh_type = dh_type;
4019  return 0;
4020 }
4021 
4022 clib_error_t *
4024  ikev2_transform_encr_type_t crypto_alg,
4027 {
4028  ikev2_profile_t *p;
4029  clib_error_t *r;
4030 
4031  p = ikev2_profile_index_by_name (name);
4032 
4033  if (!p)
4034  {
4035  r = clib_error_return (0, "unknown profile %v", name);
4036  return r;
4037  }
4038 
4039  p->esp_ts.crypto_alg = crypto_alg;
4040  p->esp_ts.integ_alg = integ_alg;
4042  return 0;
4043 }
4044 
4045 clib_error_t *
4047  u8 * name, u32 sw_if_index)
4048 {
4049  ikev2_profile_t *p;
4050  clib_error_t *r;
4051 
4052  p = ikev2_profile_index_by_name (name);
4053 
4054  if (!p)
4055  {
4056  r = clib_error_return (0, "unknown profile %v", name);
4057  return r;
4058  }
4059 
4060  p->tun_itf = sw_if_index;
4061 
4062  return 0;
4063 }
4064 
4067  u8 is_set)
4068 {
4070  ikev2_main_t *km = &ikev2_main;
4071  vnet_api_error_t rv = 0;
4072  uword *v;
4073 
4074  if (!p)
4075  return VNET_API_ERROR_INVALID_VALUE;
4076 
4077  if (is_set)
4078  {
4080  return VNET_API_ERROR_VALUE_EXIST;
4081 
4082  rv = ikev2_register_udp_port (p, port);
4083  }
4084  else
4085  {
4086  v = hash_get (km->udp_ports, port);
4087  if (!v)
4088  return VNET_API_ERROR_IKE_NO_PORT;
4089 
4091  return VNET_API_ERROR_INVALID_VALUE;
4092 
4094  }
4095  return rv;
4096 }
4097 
4098 clib_error_t *
4100 {
4102  clib_error_t *r;
4103 
4104  if (!p)
4105  {
4106  r = clib_error_return (0, "unknown profile %v", name);
4107  return r;
4108  }
4109 
4110  p->udp_encap = 1;
4111  return 0;
4112 }
4113 
4114 clib_error_t *
4116  u64 lifetime, u32 jitter, u32 handover,
4117  u64 maxdata)
4118 {
4119  ikev2_profile_t *p;
4120  clib_error_t *r;
4121 
4122  p = ikev2_profile_index_by_name (name);
4123 
4124  if (!p)
4125  {
4126  r = clib_error_return (0, "unknown profile %v", name);
4127  return r;
4128  }
4129 
4130  p->lifetime = lifetime;
4131  p->lifetime_jitter = jitter;
4132  p->handover = handover;
4133  p->lifetime_maxdata = maxdata;
4134  return 0;
4135 }
4136 
4137 static int
4139  ip_address_t * out_addr)
4140 {
4141  ip4_address_t *if_ip4;
4142  ip6_address_t *if_ip6;
4143 
4144  if (af == AF_IP4)
4145  {
4146  if_ip4 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
4147  if (if_ip4)
4148  {
4149  ip_address_set (out_addr, if_ip4, AF_IP4);
4150  return 1;
4151  }
4152  }
4153  else
4154  {
4155  if_ip6 = ip6_interface_first_address (&ip6_main, sw_if_index);
4156  if (if_ip6)
4157  {
4158  ip_address_set (out_addr, if_ip6, AF_IP6);
4159  return 1;
4160  }
4161  }
4162  return 0;
4163 }
4164 
4165 clib_error_t *
4167 {
4168  ikev2_profile_t *p;
4169  clib_error_t *r;
4170  ikev2_main_t *km = &ikev2_main;
4171  vlib_buffer_t *b0;
4172  ike_header_t *ike0;
4173  u32 bi0 = 0;
4174  int len = sizeof (ike_header_t), valid_ip = 0;
4176 
4177  p = ikev2_profile_index_by_name (name);
4178 
4179  if (!p)
4180  {
4181  r = clib_error_return (0, "unknown profile %v", name);
4182  return r;
4183  }
4184 
4185  if (p->responder.sw_if_index == ~0
4187  {
4188  r = clib_error_return (0, "responder not set for profile %v", name);
4189  return r;
4190  }
4191 
4193  ip_addr_version (&p->responder.addr), &if_ip))
4194  {
4195  valid_ip = 1;
4196  }
4197 
4198  /* Prepare the SA and the IKE payload */
4199  ikev2_sa_t sa;
4200  clib_memset (&sa, 0, sizeof (ikev2_sa_t));
4201  ikev2_payload_chain_t *chain = 0;
4202  ikev2_payload_new_chain (chain);
4203 
4204  /* Build the IKE proposal payload */
4205  ikev2_sa_proposal_t *proposals = 0;
4206  ikev2_set_initiator_proposals (vm, &sa, &p->ike_ts, &proposals, 1);
4207  proposals[0].proposal_num = 1;
4208  proposals[0].protocol_id = IKEV2_PROTOCOL_IKE;
4209 
4210  /* Add and then cleanup proposal data */
4211  ikev2_payload_add_sa (chain, proposals);
4212  ikev2_sa_free_proposal_vector (&proposals);
4213 
4214  sa.is_initiator = 1;
4215  sa.profile_index = p - km->profiles;
4217  sa.tun_itf = p->tun_itf;
4218  sa.udp_encap = p->udp_encap;
4219  if (p->natt_disabled)
4222  sa.is_tun_itf_set = 1;
4223  sa.initial_contact = 1;
4224  sa.dst_port = IKEV2_PORT;
4225 
4227  if (rc != IKEV2_GENERATE_SA_INIT_OK)
4228  {
4229  ikev2_sa_free_all_vec (&sa);
4231  return clib_error_return (0, "%U", format_ikev2_gen_sa_error, rc);
4232  }
4233 
4234  ikev2_payload_add_ke (chain, sa.dh_group, sa.i_dh_data);
4235  ikev2_payload_add_nonce (chain, sa.i_nonce);
4236 
4237  /* Build the child SA proposal */
4238  vec_resize (sa.childs, 1);
4239  ikev2_set_initiator_proposals (vm, &sa, &p->esp_ts,
4240  &sa.childs[0].i_proposals, 0);
4241  sa.childs[0].i_proposals[0].proposal_num = 1;
4243  RAND_bytes ((u8 *) & sa.childs[0].i_proposals[0].spi,
4244  sizeof (sa.childs[0].i_proposals[0].spi));
4245 
4246  /* Add NAT detection notification messages (mandatory) */
4247  u8 *nat_detection_sha1 =
4248  ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa.ispi),
4249  clib_host_to_net_u64 (sa.rspi),
4250  &if_ip, clib_host_to_net_u16 (IKEV2_PORT));
4251 
4252  ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
4253  nat_detection_sha1);
4254  vec_free (nat_detection_sha1);
4255  nat_detection_sha1 =
4256  ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa.ispi),
4257  clib_host_to_net_u64 (sa.rspi),
4258  &p->responder.addr,
4259  clib_host_to_net_u16 (sa.dst_port));
4260  ikev2_payload_add_notify (chain,
4261  IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
4262  nat_detection_sha1);
4263  vec_free (nat_detection_sha1);
4264 
4265  u8 *sig_hash_algo = vec_new (u8, 8);
4266  u64 tmpsig = clib_host_to_net_u64 (0x0001000200030004);
4267  clib_memcpy_fast (sig_hash_algo, &tmpsig, sizeof (tmpsig));
4268  ikev2_payload_add_notify (chain,
4269  IKEV2_NOTIFY_MSG_SIGNATURE_HASH_ALGORITHMS,
4270  sig_hash_algo);
4271  vec_free (sig_hash_algo);
4272 
4273  bi0 = ikev2_get_new_ike_header_buff (vm, &b0);
4274  if (!bi0)
4275  {
4276  ikev2_sa_free_all_vec (&sa);
4278  char *errmsg = "buffer alloc failure";
4279  ikev2_log_error (errmsg);
4280  return clib_error_return (0, errmsg);
4281  }
4282  ike0 = vlib_buffer_get_current (b0);
4283 
4284  /* Buffer update and boilerplate */
4285  len += vec_len (chain->data);
4286  ike0->nextpayload = chain->first_payload_type;
4287  ike0->length = clib_host_to_net_u32 (len);
4288  clib_memcpy_fast (ike0->payload, chain->data, vec_len (chain->data));
4290 
4291  ike0->version = IKE_VERSION_2;
4292  ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
4293  ike0->exchange = IKEV2_EXCHANGE_SA_INIT;
4294  ike0->ispi = clib_host_to_net_u64 (sa.ispi);
4295  ike0->rspi = 0;
4296  ike0->msgid = 0;
4297  sa.last_init_msg_id += 1;
4298 
4299  /* store whole IKE payload - needed for PSK auth */
4301  vec_add (sa.last_sa_init_req_packet_data, ike0, len);
4302 
4303  /* add data to the SA then add it to the pool */
4304  ip_address_copy (&sa.iaddr, &if_ip);
4305  ip_address_copy (&sa.raddr, &p->responder.addr);
4306  sa.i_id.type = p->loc_id.type;
4307  sa.i_id.data = vec_dup (p->loc_id.data);
4308  sa.r_id.type = p->rem_id.type;
4309  sa.r_id.data = vec_dup (p->rem_id.data);
4310  sa.i_auth.method = p->auth.method;
4311  sa.i_auth.hex = p->auth.hex;
4312  sa.i_auth.data = vec_dup (p->auth.data);
4314  vec_add (sa.childs[0].tsi, &p->loc_ts, 1);
4315  vec_add (sa.childs[0].tsr, &p->rem_ts, 1);
4316 
4318 
4319  /* add SA to the pool */
4320  ikev2_sa_t *sa0 = 0;
4321  pool_get (km->sais, sa0);
4322  clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
4323  hash_set (km->sa_by_ispi, sa0->ispi, sa0 - km->sais);
4324 
4325  if (valid_ip)
4326  {
4327  ikev2_send_ike (vm, &if_ip, &p->responder.addr, bi0, len,
4328  IKEV2_PORT, sa.dst_port, sa.sw_if_index);
4329 
4331  ("ispi %lx rspi %lx IKEV2_EXCHANGE_SA_INIT sent to ",
4332  clib_host_to_net_u64 (sa0->ispi), 0,
4333  ip_addr_v4 (&p->responder.addr).as_u32,
4335  }
4336  else
4337  {
4338  r =
4339  clib_error_return (0, "interface %U does not have any IP address!",
4341  p->responder.sw_if_index);
4342  return r;
4343  }
4344 
4345  return 0;
4346 }
4347 
4348 static void
4350  ikev2_child_sa_t * csa)
4351 {
4352  /* Create the Initiator notification for child SA removal */
4353  ikev2_main_t *km = &ikev2_main;
4354  ike_header_t *ike0;
4355  u32 bi0 = 0;
4356  vlib_buffer_t *b0;
4357  int len;
4358 
4359  bi0 = ikev2_get_new_ike_header_buff (vm, &b0);
4360  if (!bi0)
4361  {
4362  ikev2_log_error ("buffer alloc failure");
4363  return;
4364  }
4365 
4366  ike0 = vlib_buffer_get_current (b0);
4367  ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
4368  ike0->ispi = clib_host_to_net_u64 (sa->ispi);
4369  ike0->rspi = clib_host_to_net_u64 (sa->rspi);
4370  ike0->flags = 0;
4371  vec_resize (sa->del, 1);
4373  sa->del->spi = csa->i_proposals->spi;
4374  ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id);
4375  sa->last_init_msg_id += 1;
4376  len = ikev2_generate_message (b0, sa, ike0, 0, 0);
4377  if (~0 == len)
4378  return;
4379 
4380  if (ikev2_natt_active (sa))
4381  len = ikev2_insert_non_esp_marker (ike0, len);
4382  ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len,
4383  ikev2_get_port (sa), sa->dst_port, sa->sw_if_index);
4384 
4385  /* delete local child SA */
4387  ikev2_sa_del_child_sa (sa, csa);
4388 }
4389 
4390 clib_error_t *
4392 {
4393  clib_error_t *r;
4394  ikev2_main_t *km = &ikev2_main;
4396  ikev2_sa_t *fsa = 0;
4397  ikev2_child_sa_t *fchild = 0;
4398 
4399  /* Search for the child SA */
4400  vec_foreach (tkm, km->per_thread_data)
4401  {
4402  ikev2_sa_t *sa;
4403  if (fchild)
4404  break;
4405  /* *INDENT-OFF* */
4406  pool_foreach (sa, tkm->sas) {
4407  fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
4408  if (fchild)
4409  {
4410  fsa = sa;
4411  break;
4412  }
4413  }
4414  /* *INDENT-ON* */
4415  }
4416 
4417  if (!fchild || !fsa)
4418  {
4419  r = clib_error_return (0, "Child SA not found");
4420  return r;
4421  }
4422  else
4423  {
4424  ikev2_delete_child_sa_internal (vm, fsa, fchild);
4425  }
4426 
4427  return 0;
4428 }
4429 
4430 clib_error_t *
4432 {
4433  clib_error_t *r;
4434  ikev2_main_t *km = &ikev2_main;
4436  ikev2_sa_t *fsa = 0;
4437  ikev2_main_per_thread_data_t *ftkm = 0;
4438 
4439  /* Search for the IKE SA */
4440  vec_foreach (tkm, km->per_thread_data)
4441  {
4442  ikev2_sa_t *sa;
4443  if (fsa)
4444  break;
4445  /* *INDENT-OFF* */
4446  pool_foreach (sa, tkm->sas) {
4447  if (sa->ispi == ispi)
4448  {
4449  fsa = sa;
4450  ftkm = tkm;
4451  break;
4452  }
4453  }
4454  /* *INDENT-ON* */
4455  }
4456 
4457  if (!fsa)
4458  {
4459  r = clib_error_return (0, "IKE SA not found");
4460  return r;
4461  }
4462 
4463  ikev2_initiate_delete_ike_sa_internal (vm, ftkm, fsa, 1);
4464  return 0;
4465 }
4466 
4467 static void
4469  ikev2_child_sa_t * csa)
4470 {
4471  /* Create the Initiator request for create child SA */
4472  ike_header_t *ike0;
4473  vlib_buffer_t *b0;
4474  u32 bi0 = 0;
4475  int len;
4476 
4477  bi0 = ikev2_get_new_ike_header_buff (vm, &b0);
4478  if (!bi0)
4479  {
4480  ikev2_log_error ("buffer alloc failure");
4481  return;
4482  }
4483 
4484  ike0 = vlib_buffer_get_current (b0);
4485  ike0->version = IKE_VERSION_2;
4486  ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
4487  ike0->exchange = IKEV2_EXCHANGE_CREATE_CHILD_SA;
4488  ike0->ispi = clib_host_to_net_u64 (sa->ispi);
4489  ike0->rspi = clib_host_to_net_u64 (sa->rspi);
4490  ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id);
4491  sa->last_init_msg_id += 1;
4492 
4493  ikev2_rekey_t *rekey;
4494  vec_reset_length (sa->rekey);
4495  vec_add2 (sa->rekey, rekey, 1);
4496  ikev2_sa_proposal_t *proposals = vec_dup (csa->i_proposals);
4497 
4498  /*need new ispi */
4499  RAND_bytes ((u8 *) & proposals[0].spi, sizeof (proposals[0].spi));
4500  rekey->spi = proposals[0].spi;
4501  rekey->ispi = csa->i_proposals->spi;
4502  len = ikev2_generate_message (b0, sa, ike0, proposals, 0);
4503  if (~0 == len)
4504  return;
4505 
4506  if (ikev2_natt_active (sa))
4507  len = ikev2_insert_non_esp_marker (ike0, len);
4508  ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len,
4509  ikev2_get_port (sa), ikev2_get_port (sa), sa->sw_if_index);
4510  vec_free (proposals);
4511 }
4512 
4513 clib_error_t *
4515 {
4516  clib_error_t *r;
4517  ikev2_main_t *km = &ikev2_main;
4519  ikev2_sa_t *fsa = 0;
4520  ikev2_child_sa_t *fchild = 0;
4521 
4522  /* Search for the child SA */
4523  vec_foreach (tkm, km->per_thread_data)
4524  {
4525  ikev2_sa_t *sa;
4526  if (fchild)
4527  break;
4528  /* *INDENT-OFF* */
4529  pool_foreach (sa, tkm->sas) {
4530  fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
4531  if (fchild)
4532  {
4533  fsa = sa;
4534  break;
4535  }
4536  }
4537  /* *INDENT-ON* */
4538  }
4539 
4540  if (!fchild || !fsa)
4541  {
4542  r = clib_error_return (0, "Child SA not found");
4543  return r;
4544  }
4545  else
4546  {
4547  ikev2_rekey_child_sa_internal (vm, fsa, fchild);
4548  }
4549 
4550  return 0;
4551 }
4552 
4553 static int
4555 {
4556  return (sa->sw_if_index == sw_if_index) && sa->is_initiator;
4557 }
4558 
4559 static void
4561 {
4562  u64 *ispi, *ispi_vec = 0;
4563  ikev2_sa_t *sa, **sap, **sa_vec = 0;
4564  ikev2_main_t *km = &ikev2_main;
4566  p->responder.sw_if_index = ~0;
4567 
4568  vec_foreach (tkm, km->per_thread_data)
4569  {
4570  /* *INDENT-OFF* */
4571  pool_foreach (sa, tkm->sas) {
4572  if (ikev2_sa_sw_if_match (sa, sw_if_index))
4573  vec_add1 (sa_vec, sa);
4574  }
4575  /* *INDENT-ON* */
4576 
4577  vec_foreach (sap, sa_vec)
4578  {
4580  }
4581  vec_reset_length (sa_vec);
4582  }
4583  vec_free (sa_vec);
4584 
4585  /* *INDENT-OFF* */
4586  pool_foreach (sa, km->sais) {
4587  if (ikev2_sa_sw_if_match (sa, sw_if_index))
4588  vec_add1 (ispi_vec, sa->ispi);
4589  }
4590  /* *INDENT-ON* */
4591 
4592  vec_foreach (ispi, ispi_vec)
4593  {
4595  }
4596 
4597  vec_free (ispi_vec);
4598 }
4599 
4600 static clib_error_t *
4602 {
4603  ikev2_main_t *km = &ikev2_main;
4604  ikev2_profile_t *p;
4605 
4606  if (is_add)
4607  return 0;
4608 
4609  /* *INDENT-OFF* */
4610  pool_foreach (p, km->profiles) {
4611  if (p->responder.sw_if_index == sw_if_index)
4612  ikev2_sa_del (p, sw_if_index);
4613  }
4614  /* *INDENT-ON* */
4615 
4616  return 0;
4617 }
4618 
4620 
4621 clib_error_t *
4623 {
4624  ikev2_main_t *km = &ikev2_main;
4626  int thread_id;
4627 
4628  clib_memset (km, 0, sizeof (ikev2_main_t));
4629  km->vnet_main = vnet_get_main ();
4630  km->vlib_main = vm;
4631 
4634  ikev2_crypto_init (km);
4635 
4637 
4640  for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++)
4641  {
4643  vec_elt_at_index (km->per_thread_data, thread_id);
4644 
4645  ptd->sa_by_rspi = hash_create (0, sizeof (uword));
4646 
4647 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
4648  ptd->evp_ctx = EVP_CIPHER_CTX_new ();
4649  ptd->hmac_ctx = HMAC_CTX_new ();
4650 #else
4651  EVP_CIPHER_CTX_init (&ptd->_evp_ctx);
4652  ptd->evp_ctx = &ptd->_evp_ctx;
4653  HMAC_CTX_init (&(ptd->_hmac_ctx));
4654  ptd->hmac_ctx = &ptd->_hmac_ctx;
4655 #endif
4656  }
4657 
4658  km->sa_by_ispi = hash_create (0, sizeof (uword));
4659  km->sw_if_indices = hash_create (0, 0);
4660  km->udp_ports = hash_create (0, sizeof (uword));
4661 
4666 
4668  vlib_punt_register (punt_hdl, ipsec_punt_reason[IPSEC_PUNT_IP4_SPI_UDP_0],
4669  "ikev2-ip4");
4671 
4672  km->log_level = IKEV2_LOG_ERROR;
4673  km->log_class = vlib_log_register_class ("ikev2", 0);
4674  return 0;
4675 }
4676 
4677 /* *INDENT-OFF* */
4679 {
4680  .runs_after = VLIB_INITS("ipsec_init", "ipsec_punt_init"),
4681 };
4682 /* *INDENT-ON* */
4683 
4684 static u8
4686  u8 del_old_ids)
4687 {
4688  ikev2_main_t *km = &ikev2_main;
4689  ikev2_profile_t *p = 0;
4690  vlib_main_t *vm = km->vlib_main;
4691  f64 now = vlib_time_now (vm);
4692  u8 res = 0;
4693 
4694  if (sa->profile_index != ~0)
4695  p = pool_elt_at_index (km->profiles, sa->profile_index);
4696 
4697  if (sa->is_initiator && p && csa->time_to_expiration
4698  && now > csa->time_to_expiration)
4699  {
4700  if (!csa->is_expired || csa->rekey_retries > 0)
4701  {
4702  ikev2_rekey_child_sa_internal (vm, sa, csa);
4703  csa->time_to_expiration = now + p->handover;
4704  csa->is_expired = 1;
4705  if (csa->rekey_retries == 0)
4706  {
4707  csa->rekey_retries = 5;
4708  }
4709  else if (csa->rekey_retries > 0)
4710  {
4711  csa->rekey_retries--;
4712  ikev2_log_debug ("Rekeying Child SA 0x%x, retries left %d",
4713  csa->i_proposals->spi, csa->rekey_retries);
4714  if (csa->rekey_retries == 0)
4715  {
4716  csa->rekey_retries = -1;
4717  }
4718  }
4719  res |= 1;
4720  }
4721  else
4722  {
4723  csa->time_to_expiration = 0;
4724  ikev2_delete_child_sa_internal (vm, sa, csa);
4725  res |= 1;
4726  return res;
4727  }
4728  }
4729 
4730  if (del_old_ids)
4731  {
4732  ipip_tunnel_t *ipip = NULL;
4733  u32 sw_if_index = sa->is_tun_itf_set ? sa->tun_itf : ~0;
4734  if (~0 == sw_if_index)
4735  {
4736  ip46_address_t local_ip;
4737  ip46_address_t remote_ip;
4738  if (sa->is_initiator)
4739  {
4740  local_ip = to_ip46 (ip_addr_version (&sa->iaddr),
4741  ip_addr_bytes (&sa->iaddr));
4742  remote_ip = to_ip46 (ip_addr_version (&sa->raddr),
4743  ip_addr_bytes (&sa->raddr));
4744  }
4745  else
4746  {
4747  local_ip = to_ip46 (ip_addr_version (&sa->raddr),
4748  ip_addr_bytes (&sa->raddr));
4749  remote_ip = to_ip46 (ip_addr_version (&sa->iaddr),
4750  ip_addr_bytes (&sa->iaddr));
4751  }
4752 
4753  /* *INDENT-OFF* */
4754  ipip_tunnel_key_t key = {
4755  .src = local_ip,
4756  .dst = remote_ip,
4757  .transport = IPIP_TRANSPORT_IP4,
4758  .fib_index = 0,
4759  };
4760  /* *INDENT-ON* */
4761 
4762  ipip = ipip_tunnel_db_find (&key);
4763 
4764  if (ipip)
4765  sw_if_index = ipip->sw_if_index;
4766  else
4767  return res;
4768  }
4769 
4770  u32 *sas_in = NULL;
4771  vec_add1 (sas_in, csa->remote_sa_id);
4773  int rv = ipsec_tun_protect_update (sw_if_index, NULL,
4774  csa->local_sa_id, sas_in);
4775  if (rv)
4776  vec_free (sas_in);
4779  }
4780 
4781  return res;
4782 }
4783 
4784 int
4786 {
4787  ikev2_main_t *km = &ikev2_main;
4788 
4789  if (log_level >= IKEV2_LOG_MAX)
4790  {
4791  ikev2_log_error ("unknown logging level %d", log_level);
4792  return -1;
4793  }
4794 
4795  km->log_level = log_level;
4796  return 0;
4797 }
4798 
4799 clib_error_t *
4800 ikev2_set_liveness_params (u32 period, u32 max_retries)
4801 {
4802  ikev2_main_t *km = &ikev2_main;
4803 
4804  if (period == 0 || max_retries == 0)
4805  return clib_error_return (0, "invalid args");
4806 
4807  km->liveness_period = period;
4808  km->liveness_max_retries = max_retries;
4809  return 0;
4810 }
4811 
4812 clib_error_t *
4814 {
4816  if (!p)
4817  return clib_error_return (0, "unknown profile %v", name);
4818 
4819  p->natt_disabled = 1;
4820  return 0;
4821 }
4822 
4823 static void
4825 {
4826  ikev2_main_t *km = &ikev2_main;
4827  vlib_main_t *vm = km->vlib_main;
4829  ikev2_sa_t *fsa = 0;
4830  ikev2_profile_t *p = 0;
4831  ikev2_child_sa_t *fchild = 0;
4832  f64 now = vlib_time_now (vm);
4833  vlib_counter_t counts;
4834 
4835  /* Search for the SA and child SA */
4836  vec_foreach (tkm, km->per_thread_data)
4837  {
4838  ikev2_sa_t *sa;
4839  if (fchild)
4840  break;
4841  /* *INDENT-OFF* */
4842  pool_foreach (sa, tkm->sas) {
4843  fchild = ikev2_sa_get_child(sa, ipsec_sa->spi, IKEV2_PROTOCOL_ESP, 1);
4844  if (fchild)
4845  {
4846  fsa = sa;
4847  break;
4848  }
4849  }
4850  /* *INDENT-ON* */
4851  }
4853  ipsec_sa->stat_index, &counts);
4854 
4855  if (fsa && fsa->profile_index != ~0 && fsa->is_initiator)
4856  p = pool_elt_at_index (km->profiles, fsa->profile_index);
4857 
4858  if (fchild && p && p->lifetime_maxdata)
4859  {
4860  if (!fchild->is_expired && counts.bytes > p->lifetime_maxdata)
4861  {
4862  fchild->time_to_expiration = now;
4863  }
4864  }
4865 }
4866 
4867 static void
4869 {
4870  ikev2_profile_t *p;
4871  u32 bi0;
4872  u8 *nat_sha, *np;
4873 
4874  if (ip_address_is_zero (&sa->iaddr))
4875  {
4876  p = pool_elt_at_index (km->profiles, sa->profile_index);
4879  &sa->iaddr))
4880  return;
4881 
4882  /* update NAT detection payload */
4883  np =
4885  ((ike_header_t *) sa->last_sa_init_req_packet_data,
4886  IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP);
4887  if (np)
4888  {
4889  nat_sha =
4890  ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa->ispi),
4891  clib_host_to_net_u64 (sa->rspi),
4892  &sa->iaddr,
4893  clib_host_to_net_u16 (IKEV2_PORT));
4894  clib_memcpy_fast (np, nat_sha, vec_len (nat_sha));
4895  vec_free (nat_sha);
4896  }
4897  }
4898 
4899  if (vlib_buffer_alloc (km->vlib_main, &bi0, 1) != 1)
4900  return;
4901 
4902  vlib_buffer_t *b = vlib_get_buffer (km->vlib_main, bi0);
4906 
4907  ikev2_send_ike (km->vlib_main, &sa->iaddr, &sa->raddr, bi0,
4910 }
4911 
4912 static void
4914 {
4915  u32 sai;
4916  u64 ispi;
4917  ikev2_sa_t *sa;
4918 
4919  /* *INDENT-OFF* */
4920  hash_foreach (ispi, sai, km->sa_by_ispi,
4921  ({
4922  sa = pool_elt_at_index (km->sais, sai);
4923  if (sa->init_response_received)
4924  continue;
4925 
4926  ikev2_process_pending_sa_init_one (km, sa);
4927  }));
4928  /* *INDENT-ON* */
4929 }
4930 
4931 static void
4933 {
4934  ikev2_main_t *km = &ikev2_main;
4935  ip_address_t *src, *dst;
4936  ike_header_t *ike0;
4937  vlib_buffer_t *b0;
4938  u32 bi0 = 0;
4939  u16 dp;
4940  int len;
4941 
4942  bi0 = ikev2_get_new_ike_header_buff (km->vlib_main, &b0);
4943  if (!bi0)
4944  {
4945  ikev2_log_error ("buffer alloc failure");
4946  return;
4947  }
4948 
4949  ike0 = vlib_buffer_get_current (b0);
4950  ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
4951  ike0->ispi = clib_host_to_net_u64 (sa->ispi);
4952  ike0->rspi = clib_host_to_net_u64 (sa->rspi);
4953  ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id);
4954  ike0->flags = 0;
4955  sa->last_init_msg_id += 1;
4956  len = ikev2_generate_message (b0, sa, ike0, 0, 0);
4957  if (~0 == len)
4958  return;
4959 
4960  if (ikev2_natt_active (sa))
4961  len = ikev2_insert_non_esp_marker (ike0, len);
4962 
4963  if (sa->is_initiator)
4964  {
4965  src = &sa->iaddr;
4966  dst = &sa->raddr;
4967  }
4968  else
4969  {
4970  dst = &sa->iaddr;
4971  src = &sa->raddr;
4972  }
4973 
4974  dp = sa->dst_port ? sa->dst_port : ikev2_get_port (sa);
4975  ikev2_send_ike (km->vlib_main, src, dst, bi0, len, ikev2_get_port (sa), dp,
4976  sa->sw_if_index);
4977 }
4978 
4979 void
4981 {
4982  ikev2_main_t *km = &ikev2_main;
4983  km->dpd_disabled = 1;
4984 }
4985 
4988 {
4989  ikev2_main_t *km = &ikev2_main;
4990  vlib_main_t *vm = km->vlib_main;
4991 
4992  if (!sa->keys_generated)
4993  return 0;
4994 
4995  if (sa->liveness_retries >= km->liveness_max_retries)
4996  return 1;
4997 
4998  f64 now = vlib_time_now (vm);
4999 
5000  if (sa->liveness_period_check < now)
5001  {
5002  sa->liveness_retries++;
5003  sa->liveness_period_check = now + km->liveness_period;
5005  }
5006  return 0;
5007 }
5008 
5009 static uword
5011  vlib_frame_t * f)
5012 {
5013  ikev2_main_t *km = &ikev2_main;
5014  ipsec_main_t *im = &ipsec_main;
5015  ikev2_profile_t *p;
5017  u32 *sai;
5018 
5019  while (1)
5020  {
5022  vlib_process_get_events (vm, NULL);
5023 
5024  /* process ike child sas */
5026  vec_foreach (tkm, km->per_thread_data)
5027  {
5028  ikev2_sa_t *sa;
5029  u32 *to_be_deleted = 0;
5030 
5031  /* *INDENT-OFF* */
5032  pool_foreach (sa, tkm->sas) {
5034  u8 del_old_ids = 0;
5035 
5036  if (sa->state != IKEV2_STATE_AUTHENTICATED)
5037  continue;
5038 
5039  if (sa->old_remote_id_present && 0 > sa->old_id_expiration)
5040  {
5041  sa->old_remote_id_present = 0;
5042  del_old_ids = 1;
5043  }
5044  else
5045  sa->old_id_expiration -= 1;
5046 
5047  vec_foreach (c, sa->childs)
5048  ikev2_mngr_process_child_sa(sa, c, del_old_ids);
5049 
5051  vec_add1 (to_be_deleted, sa - tkm->sas);
5052  }
5053  /* *INDENT-ON* */
5054 
5055  vec_foreach (sai, to_be_deleted)
5056  {
5057  sa = pool_elt_at_index (tkm->sas, sai[0]);
5058  u8 reinitiate = (sa->is_initiator && sa->profile_index != ~0);
5059  vec_foreach (c, sa->childs)
5060  {
5062  ikev2_sa_del_child_sa (sa, c);
5063  }
5064  ikev2_sa_free_all_vec (sa);
5065  hash_unset (tkm->sa_by_rspi, sa->rspi);
5066  pool_put (tkm->sas, sa);
5067 
5068  if (reinitiate)
5069  {
5070  p = pool_elt_at_index (km->profiles, sa->profile_index);
5071  if (p)
5072  {
5074  if (e)
5075  {
5077  clib_error_free (e);
5078  }
5079  }
5080  }
5081  }
5082  vec_free (to_be_deleted);
5083  }
5084 
5085  /* process ipsec sas */
5086  ipsec_sa_t *sa;
5087  /* *INDENT-OFF* */
5088  pool_foreach (sa, im->sad) {
5090  }
5091  /* *INDENT-ON* */
5092 
5094  }
5095  return 0;
5096 }
5097 
5098 /* *INDENT-OFF* */
5100  .function = ikev2_mngr_process_fn,
5101  .type = VLIB_NODE_TYPE_PROCESS,
5102  .name =
5103  "ikev2-manager-process",
5104 };
5105 
5106 VLIB_PLUGIN_REGISTER () = {
5107  .version = VPP_BUILD_VER,
5108  .description = "Internet Key Exchange (IKEv2) Protocol",
5109 };
5110 /* *INDENT-ON* */
5111 
5112 /*
5113  * fd.io coding-style-patch-verification: ON
5114  *
5115  * Local Variables:
5116  * eval: (c-set-style "gnu")
5117  * End:
5118  */
ikev2_main_per_thread_data_t * per_thread_data
Definition: ikev2_priv.h:493
ipip_tunnel_t * ipip_tunnel_db_find(const ipip_tunnel_key_t *key)
Definition: ipip.c:493
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:338
void udp_unregister_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u8 is_ip4)
Definition: udp_local.c:506
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
udp_main_t udp_main
Definition: udp.c:23
u32 liveness_period
Definition: ikev2_priv.h:511
clib_error_t * ikev2_profile_natt_disable(u8 *name)
Definition: ikev2.c:4813
u8 * dh_shared_key
Definition: ikev2_priv.h:382
ikev2_sa_t * sais
Definition: ikev2_priv.h:489
vl_api_address_t end_addr
Definition: ikev2_types.api:38
void ikev2_payload_add_nonce(ikev2_payload_chain_t *c, u8 *nonce)
static ikev2_generate_sa_error_t ikev2_generate_sa_init_data(ikev2_sa_t *sa)
Definition: ikev2.c:392
static u8 * format_ikev2_trace(u8 *s, va_list *args)
Definition: ikev2.c:53
u8 * dh_private_key
Definition: ikev2_priv.h:383
#define ip_addr_v6(_a)
Definition: ip_types.h:92
ikev2_transform_type_t type
Definition: ikev2_priv.h:215
vnet_api_error_t
Definition: api_errno.h:162
#define hash_set(h, key, value)
Definition: hash.h:255
int ipsec_sa_add_and_lock(u32 id, u32 spi, ipsec_protocol_t proto, ipsec_crypto_alg_t crypto_alg, const ipsec_key_t *ck, ipsec_integ_alg_t integ_alg, const ipsec_key_t *ik, ipsec_sa_flags_t flags, u32 tx_table_id, u32 salt, const ip46_address_t *tun_src, const ip46_address_t *tun_dst, tunnel_encap_decap_flags_t tunnel_flags, ip_dscp_t dscp, u32 *sa_out_index, u16 src_port, u16 dst_port)
Definition: ipsec_sa.c:170
#define IKEV2_PAYLOAD_NONCE
Definition: ikev2.h:113
ikev2_id_t r_id
Definition: ikev2_priv.h:406
ikev2_id_type_t type
Definition: ikev2_priv.h:273
#define CLIB_UNUSED(x)
Definition: clib.h:87
void ikev2_payload_add_notify(ikev2_payload_chain_t *c, u16 msg_type, u8 *data)
ikev2_transforms_set ike_ts
Definition: ikev2_priv.h:340
ip_address_t end_addr
Definition: ikev2_priv.h:253
u32 old_remote_id
Definition: ikev2_priv.h:434
static int ikev2_delete_tunnel_interface(vnet_main_t *vnm, ikev2_sa_t *sa, ikev2_child_sa_t *child)
Definition: ikev2.c:2239
static uword ikev2_ip4(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ikev2.c:3333
static u8 ikev2_mngr_process_child_sa(ikev2_sa_t *sa, ikev2_child_sa_t *csa, u8 del_old_ids)
Definition: ikev2.c:4685
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:751
ikev2_transform_integ_type_t
Definition: ikev2.h:282
#define hash_unset(h, key)
Definition: hash.h:261
a
Definition: bitmap.h:544
u16 dst_port
Definition: ikev2_priv.h:443
#define ike_hdr_is_response(_h)
Definition: ikev2.h:47
static void ikev2_del_sa_init(u64 ispi)
Definition: ikev2.c:2758
ikev2_natt_state_t natt_state
Definition: ikev2_priv.h:447
void ip_address_set(ip_address_t *dst, const void *src, u8 version)
Definition: ip_types.c:208
ip4_address_t src_address
Definition: ip4_packet.h:125
clib_error_t * ikev2_add_del_profile(vlib_main_t *vm, u8 *name, int is_add)
Definition: ikev2.c:3803
int ikev2_encrypt_data(ikev2_main_per_thread_data_t *ptd, ikev2_sa_t *sa, ikev2_sa_transform_t *tr_encr, v8 *src, u8 *dst)
Definition: ikev2_crypto.c:453
EVP_PKEY * pkey
Definition: ikev2_priv.h:482
A representation of a IPIP tunnel.
Definition: ipip.h:75
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_elog_uint_peers_addr(u32 exchange, ip4_header_t *ip4, ip6_header_t *ip6, u8 is_ip4)
Definition: ikev2.c:2803
ikev2_traffic_selector_type_t ts_type
Definition: ikev2_priv.h:247
clib_error_t * ikev2_set_profile_udp_encap(vlib_main_t *vm, u8 *name)
Definition: ikev2.c:4099
static void ikev2_profile_free(ikev2_profile_t *p)
Definition: ikev2.c:3790
static void ikev2_calc_keys(ikev2_sa_t *sa)
Definition: ikev2.c:506
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:527
u32 last_init_msg_id
Definition: ikev2_priv.h:425
vl_api_address_t raddr
ikev2_transform_dh_type_t dh_type
Definition: ikev2_priv.h:266
#define PREDICT_TRUE(x)
Definition: clib.h:122
#define IKEV2_PAYLOAD_NONE
Definition: ikev2.h:105
vl_api_ikev2_auth_t auth
Definition: ikev2_types.api:88
ikev2_profile_t * profiles
Definition: ikev2_priv.h:473
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
unsigned long u64
Definition: types.h:89
vl_api_ip_port_and_mask_t dst_port
Definition: flow_types.api:92
u8 v8
Definition: ikev2.h:33
clib_error_t * ikev2_initiate_delete_ike_sa(vlib_main_t *vm, u64 ispi)
Definition: ikev2.c:4431
vl_api_address_t start_addr
Definition: ikev2_types.api:37
static void ikev2_initial_contact_cleanup(ikev2_main_per_thread_data_t *ptd, ikev2_sa_t *sa)
Definition: ikev2.c:1098
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
u32 current_remote_id_mask
Definition: ikev2_priv.h:433
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void ikev2_sa_free_child_sa(ikev2_child_sa_t *c)
Definition: ikev2.c:304
ip4_address_t * ip4_interface_first_address(ip4_main_t *im, u32 sw_if_index, ip_interface_address_t **result_ia)
Definition: ip4_forward.c:281
v8 * ikev2_calc_prf(ikev2_sa_transform_t *tr, v8 *key, v8 *data)
Definition: ikev2_crypto.c:255
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:334
static void ikev2_complete_sa_data(ikev2_sa_t *sa, ikev2_sa_t *sai)
Definition: ikev2.c:442
#define ikev2_elog_error(_msg)
Definition: ikev2_priv.h:170
static int ikev2_get_if_address(u32 sw_if_index, ip_address_family_t af, ip_address_t *out_addr)
Definition: ikev2.c:4138
#define IKEV2_GENERATE_SA_INIT_OK_ERR_NO_DH_STR
Definition: ikev2.c:65
ip_address_t addr
Definition: ikev2_priv.h:259
#define IKEV2_EXCHANGE_SA_INIT
Definition: ikev2.h:94
static void ikev2_delete_child_sa_internal(vlib_main_t *vm, ikev2_sa_t *sa, ikev2_child_sa_t *csa)
Definition: ikev2.c:4349
static u32 ikev2_get_new_ike_header_buff(vlib_main_t *vm, vlib_buffer_t **b)
Definition: ikev2.c:3593
ikev2_transform_esn_type_t esn_type
Definition: ikev2_priv.h:223
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
VLIB_PLUGIN_REGISTER()
#define IKEV2_PAYLOAD_VENDOR
Definition: ikev2.h:116
ikev2_state_t state
Definition: ikev2_priv.h:370
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
ip6_address_t * ip6_interface_first_address(ip6_main_t *im, u32 sw_if_index)
get first IPv6 interface address
Definition: ip6_forward.c:279
void vnet_sw_interface_admin_down(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface.c:542
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
vl_api_address_t src
Definition: gre.api:54
ikev2_transform_encr_type_t crypto_alg
Definition: ikev2_priv.h:264
u16 ip_address_size(const ip_address_t *a)
Definition: ip_types.c:87
clib_error_t * ikev2_set_profile_tunnel_interface(vlib_main_t *vm, u8 *name, u32 sw_if_index)
Definition: ikev2.c:4046
#define IKEV2_PORT_NATT
Definition: ikev2.h:25
int vlib_punt_hdl_t
Typedef for a client handle.
Definition: punt.h:47
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:630
static int ikev2_process_sa_init_req(vlib_main_t *vm, ikev2_sa_t *sa, ike_header_t *ike, udp_header_t *udp, u32 len)
Definition: ikev2.c:719
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:468
u8 * sk_pi
Definition: ikev2_priv.h:397
static clib_error_t * ikev2_sw_interface_add_del(vnet_main_t *vnm, u32 sw_if_index, u32 is_add)
Definition: ikev2.c:4601
static void mhash_init_vec_string(mhash_t *h, uword n_value_bytes)
Definition: mhash.h:84
static udp_dst_port_info_t * udp_get_dst_port_info(udp_main_t *um, udp_dst_port_t dst_port, u8 is_ip4)
Definition: udp.h:226
vlib_main_t * vm
Definition: in2out_ed.c:1580
#define IKEV2_GCM_ICV_SIZE
Definition: ikev2.h:28
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:4115
u16 flags_and_fragment_offset
Definition: ip4_packet.h:106
void ikev2_payload_add_ke(ikev2_payload_chain_t *c, u16 dh_group, u8 *dh_data)
#define IKEV2_NONCE_SIZE
Definition: ikev2.h:23
u8 initial_contact
Definition: ikev2_priv.h:372
ikev2_ts_t * tsi
Definition: ikev2_priv.h:318
clib_error_t * ikev2_set_liveness_params(u32 period, u32 max_retries)
Definition: ikev2.c:4800
ip_address_t iaddr
Definition: ikev2_priv.h:373
#define IKEV2_PAYLOAD_TSR
Definition: ikev2.h:118
static int ikev2_parse_id_payload(const void *p, u16 rlen, ikev2_id_t *sa_id)
Definition: ikev2.c:1119
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:520
int ipip_add_tunnel(ipip_transport_t transport, u32 instance, ip46_address_t *src, ip46_address_t *dst, u32 fib_index, tunnel_encap_decap_flags_t flags, ip_dscp_t dscp, tunnel_mode_t tmode, u32 *sw_if_indexp)
Definition: ipip.c:664
ikev2_auth_t r_auth
Definition: ikev2_priv.h:402
u16 mask
Definition: flow_types.api:52
static_always_inline int ikev2_mngr_process_responder_sas(ikev2_sa_t *sa)
Definition: ikev2.c:4987
u16 start_port
Definition: ikev2_types.api:35
static u8 * format_ikev2_gen_sa_error(u8 *s, va_list *args)
Definition: ikev2.c:78
#define ip_addr_version(_a)
Definition: ip_types.h:93
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:251
vhost_vring_addr_t addr
Definition: vhost_user.h:111
u16 ipsec_over_udp_port
Definition: ikev2_priv.h:346
void ipsec_mk_key(ipsec_key_t *key, const u8 *data, u8 len)
Definition: ipsec_sa.c:56
ip6_address_t src_address
Definition: ip6_packet.h:310
u8 * last_sa_init_res_packet_data
Definition: ikev2_priv.h:416
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
vnet_api_error_t ikev2_set_profile_ipsec_udp_port(vlib_main_t *vm, u8 *name, u16 port, u8 is_set)
Definition: ikev2.c:4066
u8 init_response_received
Definition: ikev2_priv.h:436
u8 data[128]
Definition: ipsec_types.api:90
static void ikev2_cleanup_profile_sessions(ikev2_main_t *km, ikev2_profile_t *p)
Definition: ikev2.c:3744
ikev2_sa_transform_t * ikev2_sa_get_td_for_type(ikev2_sa_proposal_t *p, ikev2_transform_type_t type)
Definition: ikev2.c:251
ikev2_auth_t auth
Definition: ikev2_priv.h:334
u8 id[64]
Definition: dhcp.api:160
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
ikev2_ts_t * tsr
Definition: ikev2_priv.h:319
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:205
u16 end_port
Definition: ikev2_types.api:36
vlib_node_registration_t ip4_lookup_node
(constructor) VLIB_REGISTER_NODE (ip4_lookup_node)
Definition: ip4_forward.c:104
u16 key_trunc
Definition: ikev2_types.api:96
#define clib_memcpy(d, s, n)
Definition: string.h:180
static void ikev2_mngr_process_ipsec_sa(ipsec_sa_t *ipsec_sa)
Definition: ikev2.c:4824
ikev2_id_t rem_id
Definition: ikev2_priv.h:336
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:668
ikev2_transform_dh_type_t
Definition: ikev2.h:332
log_level
Definition: vpe_types.api:33
static int ikev2_sa_sw_if_match(ikev2_sa_t *sa, u32 sw_if_index)
Definition: ikev2.c:4554
u64 lifetime
Definition: ikev2_types.api:80
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:268
u32 next_index
Definition: ikev2.c:48
static u32 ikev2_retransmit_resp(ikev2_sa_t *sa, ike_header_t *ike)
Definition: ikev2.c:2694
static_always_inline void ikev2_rewrite_v6_addrs(ikev2_sa_t *sa, ip6_header_t *ih)
Definition: ikev2.c:2765
u32 last_msg_id
Definition: ikev2_priv.h:420
#define static_always_inline
Definition: clib.h:109
#define IKEV2_PAYLOAD_DELETE
Definition: ikev2.h:115
ikev2_sa_proposal_t * r_proposals
Definition: ikev2_priv.h:281
#define ip_address_initializer
Definition: ip_types.h:88
void ikev2_generate_dh(ikev2_sa_t *sa, ikev2_sa_transform_t *t)
Definition: ikev2_crypto.c:498
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:579
ipsec_main_t ipsec_main
Definition: ipsec.c:28
EVP_PKEY * ikev2_load_cert_file(u8 *file)
Definition: ikev2_crypto.c:816
clib_error_t * ikev2_initiate_sa_init(vlib_main_t *vm, u8 *name)
Definition: ikev2.c:4166
#define IKEV2_LIVENESS_PERIOD_CHECK
Definition: ikev2.c:33
vl_api_ip6_address_t ip6
Definition: one.api:424
int ipsec_tun_protect_del(u32 sw_if_index, const ip_address_t *nh)
Definition: ipsec_tun.c:804
static ikev2_sa_transform_t * ikev2_find_transform_data(ikev2_sa_transform_t *t)
Definition: ikev2.c:151
ip4_address_t dst_address
Definition: ip4_packet.h:125
u32 liveness_max_retries
Definition: ikev2_priv.h:514
#define vec_new(T, N)
Create new vector of given type and length (unspecified alignment, no header).
Definition: vec.h:350
EVP_PKEY * ikev2_load_key_file(u8 *file)
Definition: ikev2_crypto.c:847
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:3839
ipsec_crypto_alg_t encr_type
Definition: ikev2.c:1835
void ikev2_parse_vendor_payload(ike_payload_header_t *ikep)
description fragment has unexpected format
Definition: map.api:433
#define hash_foreach(key_var, value_var, h, body)
Definition: hash.h:442
static vlib_node_registration_t ikev2_mngr_process_node
(constructor) VLIB_REGISTER_NODE (ikev2_mngr_process_node)
Definition: ikev2.c:5099
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
ipsec_integ_alg_t integ_type
Definition: ikev2.c:1836
void vnet_sw_interface_admin_up(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface.c:529
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:182
#define clib_error_return(e, args...)
Definition: error.h:99
int ipsec_sa_unlock_id(u32 id)
Definition: ipsec_sa.c:435
void ikev2_payload_add_id(ikev2_payload_chain_t *c, ikev2_id_t *id, u8 type)
#define IKEV2_PAYLOAD_NOTIFY
Definition: ikev2.h:114
void vl_api_rpc_call_main_thread(void *fp, u8 *data, u32 data_length)
Definition: vlib_api.c:619
static void ikev2_sa_match_ts(ikev2_sa_t *sa)
Definition: ikev2.c:1559
static u32 ikev2_retransmit_sa_init_one(ikev2_sa_t *sa, ike_header_t *ike, ip_address_t iaddr, ip_address_t raddr, u32 rlen)
Definition: ikev2.c:2605
const cJSON *const b
Definition: cJSON.h:255
static void ikev2_initiate_delete_ike_sa_internal(vlib_main_t *vm, ikev2_main_per_thread_data_t *tkm, ikev2_sa_t *sa, u8 send_notification)
Definition: ikev2.c:3672
static void ikev2_send_ike(vlib_main_t *vm, ip_address_t *src, ip_address_t *dst, u32 bi0, u32 len, u16 src_port, u16 dst_port, u32 sw_if_index)
Definition: ikev2.c:3523
ikev2_sa_proposal_t * i_proposals
Definition: ikev2_priv.h:388
uword * sw_if_indices
Definition: ikev2_priv.h:496
static void ikev2_sa_auth(ikev2_sa_t *sa)
Definition: ikev2.c:1629
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:281
ikev2_transform_integ_type_t integ_alg
Definition: ikev2_priv.h:265
unsigned int u32
Definition: types.h:88
ikev2_auth_t i_auth
Definition: ikev2_priv.h:401
static u8 * ikev2_decrypt_sk_payload(ikev2_sa_t *sa, ike_header_t *ike, u8 *payload, u32 rlen, u32 *out_len)
Definition: ikev2.c:938
#define ikev2_payload_destroy_chain(V)
Definition: ikev2_priv.h:562
#define VLIB_FRAME_SIZE
Definition: node.h:378
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, u32 crypto_key_size)
Definition: ikev2.c:4023
static void ikev2_del_sa_init_from_main(u64 *ispi)
Definition: ikev2.c:2744
ikev2_id_t loc_id
Definition: ikev2_priv.h:335
ikev2_sa_transform_t * transforms
Definition: ikev2_priv.h:242
vlib_punt_hdl_t vlib_punt_client_register(const char *who)
Register a new clinet.
Definition: punt.c:156
static void ikev2_process_pending_sa_init_one(ikev2_main_t *km, ikev2_sa_t *sa)
Definition: ikev2.c:4868
#define IKEV2_EXCHANGE_CREATE_CHILD_SA
Definition: ikev2.h:96
u32 stat_index
Definition: ipsec_sa.h:204
u8 * sk_ar
Definition: ikev2_priv.h:394
pool_header_t * ph(void *p)
GDB callable function: ph - call pool_header - get pool header.
Definition: gdb_funcs.c:78
u8 * r_dh_data
Definition: ikev2_priv.h:385
void ip_address_copy_addr(void *dst, const ip_address_t *src)
Definition: ip_types.c:164
ip46_address_t remote_ip
Definition: ikev2.c:1838
ikev2_responder_t responder
Definition: ikev2_priv.h:339
vl_api_fib_path_type_t type
Definition: fib_types.api:123
int ikev2_set_log_level(ikev2_log_level_t log_level)
Definition: ikev2.c:4785
#define ikev2_set_state(sa, v,...)
Definition: ikev2.c:41
static ikev2_profile_t * ikev2_profile_index_by_name(u8 *name)
Definition: ikev2.c:3509
static vlib_node_registration_t ikev2_node_ip6
(constructor) VLIB_REGISTER_NODE (ikev2_node_ip6)
Definition: ikev2.c:3362
Definition: cJSON.c:84
#define hash_get(h, key)
Definition: hash.h:249
clib_error_t * ikev2_set_profile_id(vlib_main_t *vm, u8 *name, u8 id_type, u8 *data, int is_local)
Definition: ikev2.c:3882
u8 * ikev2_calc_prfplus(ikev2_sa_transform_t *tr, u8 *key, u8 *seed, int len)
Definition: ikev2_crypto.c:272
static u32 ikev2_mk_local_sa_id(u32 sai, u32 ci, u32 ti)
Definition: ikev2.c:1814
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:3382
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:546
#define ikev2_elog_exchange(_fmt, _ispi, _rspi, _addr, _v4)
Definition: ikev2_priv.h:116
u32 sa_id
Definition: ipsec.api:98
ikev2_main_t ikev2_main
Definition: ikev2.c:35
void ikev2_cli_reference(void)
Definition: ikev2_cli.c:791
u8 * last_sa_init_req_packet_data
Definition: ikev2_priv.h:415
u8 integ_alg
Definition: ikev2_types.api:59
#define IKEV2_PAYLOAD_IDR
Definition: ikev2.h:111
static_always_inline u32 ikev2_flip_alternate_sa_bit(u32 id)
Definition: ikev2.c:2185
#define IKEV2_PAYLOAD_SA
Definition: ikev2.h:108
ikev2_ts_t rem_ts
Definition: ikev2_priv.h:338
#define ikev2_log_debug(...)
Definition: ikev2_priv.h:184
VNET_SW_INTERFACE_ADD_DEL_FUNCTION(ikev2_sw_interface_add_del)
u8 * i_dh_data
Definition: ikev2_priv.h:384
static __clib_warn_unused_result u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:677
unsigned short u16
Definition: types.h:57
ikev2_sa_proposal_t * i_proposals
Definition: ikev2_priv.h:280
EVP_CIPHER_CTX * evp_ctx
Definition: ikev2_priv.h:462
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:216
u8 * r_nonce
Definition: ikev2_priv.h:378
#define IKEV2_HDR_FLAG_RESPONSE
Definition: ikev2.h:101
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:233
mhash_t profile_index_by_name
Definition: ikev2_priv.h:479
u16 end_port
Definition: ikev2_priv.h:251
ikev2_sa_transform_t * supported_transforms
Definition: ikev2_priv.h:476
static uword ikev2_ip6(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ikev2.c:3339
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:301
ikev2_rekey_t * rekey
Definition: ikev2_priv.h:412
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:429
clib_error_t * ikev2_initiate_delete_child_sa(vlib_main_t *vm, u32 ispi)
Definition: ikev2.c:4391
static_always_inline int ikev2_insert_non_esp_marker(ike_header_t *ike, int len)
Definition: ikev2.c:143
static void ikev2_rekey_child_sa_internal(vlib_main_t *vm, ikev2_sa_t *sa, ikev2_child_sa_t *csa)
Definition: ikev2.c:4468
bool ip_address_is_zero(const ip_address_t *ip)
Definition: ip_types.c:102
int ikev2_decrypt_data(ikev2_main_per_thread_data_t *ptd, ikev2_sa_t *sa, ikev2_sa_transform_t *tr_encr, u8 *data, int len, u32 *out_len)
Definition: ikev2_crypto.c:389
#define PREDICT_FALSE(x)
Definition: clib.h:121
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:875
vl_api_ip4_address_t ip4
Definition: one.api:376
ip6_main_t ip6_main
Definition: ip6_forward.c:2785
static_always_inline void ikev2_set_ts_type(ikev2_ts_t *ts, const ip_address_t *addr)
Definition: ikev2.c:3920
#define IKEV2_PAYLOAD_FLAG_CRITICAL
Definition: ikev2.h:103
int ikev2_verify_sign(EVP_PKEY *pkey, u8 *sigbuf, u8 *data)
Definition: ikev2_crypto.c:753
static void ikev2_sa_auth_init(ikev2_sa_t *sa)
Definition: ikev2.c:1771
static_always_inline ikev2_main_per_thread_data_t * ikev2_get_per_thread_data()
Definition: ikev2_priv.h:597
ikev2_protocol_id_t
Definition: ikev2.h:121
static int ikev2_parse_nonce_payload(const void *p, u32 rlen, u8 *nonce)
Definition: ikev2.c:697
fib_protocol_t ip_address_to_46(const ip_address_t *addr, ip46_address_t *a)
Definition: ip_types.c:253
u32 node_index
Node index.
Definition: node.h:488
ipsec_sa_flags_t flags
Definition: ikev2.c:1832
vl_api_address_t dst
Definition: gre.api:55
#define ikev2_natt_active(_sa)
Definition: ikev2_priv.h:366
static void ikev2_sa_del(ikev2_profile_t *p, u32 sw_if_index)
Definition: ikev2.c:4560
static int ikev2_process_informational_req(vlib_main_t *vm, ikev2_sa_t *sa, ike_header_t *ike, u32 len)
Definition: ikev2.c:1288
u8 * i_nonce
Definition: ikev2_priv.h:377
clib_error_t * ikev2_set_local_key(vlib_main_t *vm, u8 *file)
Definition: ikev2.c:3606
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1231
u32 punt_reason
Definition: buffer.h:149
static void ikev2_process_pending_sa_init(ikev2_main_t *km)
Definition: ikev2.c:4913
u8 len
Definition: ip_types.api:103
u8 * sk_ei
Definition: ikev2_priv.h:395
u8 old_remote_id_present
Definition: ikev2_priv.h:435
bool is_local
Definition: ikev2_types.api:33
#define IKEV2_EXCHANGE_INFORMATIONAL
Definition: ikev2.h:97
void ikev2_payload_add_delete(ikev2_payload_chain_t *c, ikev2_delete_t *d)
static_always_inline u32 vlib_buffer_get_default_data_size(vlib_main_t *vm)
Definition: buffer_funcs.h:96
static u32 ikev2_mk_remote_sa_id(u32 sai, u32 ci, u32 ti)
Definition: ikev2.c:1820
vl_api_ip_port_and_mask_t src_port
Definition: flow_types.api:91
#define IKEV2_HDR_FLAG_INITIATOR
Definition: ikev2.h:99
#define IKEV2_KEY_PAD
Definition: ikev2.h:26
u32 sw_if_index
Definition: ikev2_priv.h:444
ip_address_t raddr
Definition: ikev2_priv.h:374
static void ikev2_add_tunnel_from_main(ikev2_add_ipsec_tunnel_args_t *a)
Definition: ikev2.c:1848
static uword ikev2_mngr_process_fn(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: ikev2.c:5010
clib_error_t * ikev2_initiate_rekey_child_sa(vlib_main_t *vm, u32 ispi)
Definition: ikev2.c:4514
vlib_punt_reason_t ipsec_punt_reason[IPSEC_PUNT_N_REASONS]
Definition: ipsec_punt.c:24
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:170
ikev2_transform_dh_type_t dh_type
Definition: ikev2_priv.h:222
#define ikev2_elog_uint(_level, _format, _val)
Definition: ikev2_priv.h:124
svmdb_client_t * c
u16 n_vectors
Definition: node.h:397
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:219
static void vlib_get_combined_counter(const vlib_combined_counter_main_t *cm, u32 index, vlib_counter_t *result)
Get the value of a combined counter, never called in the speed path Scrapes the entire set of per-thr...
Definition: counter.h:278
static_always_inline void vlib_buffer_enqueue_to_next(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 *nexts, uword count)
Definition: buffer_node.h:339
static void ikev2_sa_del_child_sa(ikev2_sa_t *sa, ikev2_child_sa_t *child)
Definition: ikev2.c:326
ikev2_auth_method_t method
Definition: ikev2_priv.h:201
ikev2_transform_encr_type_t
Definition: ikev2.h:241
ikev2_delete_t * del
Definition: ikev2_priv.h:409
ikev2_ts_t * tsi
Definition: ikev2_priv.h:284
#define clib_memcmp(s1, s2, m1)
Definition: string.h:720
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
ikev2_sa_proposal_t * ikev2_parse_sa_payload(ike_payload_header_t *ikep, u32 rlen)
static vlib_punt_hdl_t punt_hdl
Definition: ipsec_punt.c:22
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
static_always_inline vnet_api_error_t ikev2_register_udp_port(ikev2_profile_t *p, u16 port)
Definition: ikev2.c:3620
#define IKEV2_LIVENESS_RETRIES
Definition: ikev2.c:32
ip46_address_t remote_ip
Definition: ikev2.c:2178
u8 data[]
Packet data.
Definition: buffer.h:181
static void ikev2_generate_sa_init_data_and_log(ikev2_sa_t *sa)
Definition: ikev2.c:2823
enum ipsec_sad_flags_t_ ipsec_sa_flags_t
static void ikev2_initial_contact_cleanup_internal(ikev2_main_per_thread_data_t *ptd, ikev2_sa_t *sa)
Definition: ikev2.c:1063
static_always_inline u16 ikev2_get_port(ikev2_sa_t *sa)
Definition: ikev2.c:137
uword * udp_ports
Definition: ikev2_priv.h:508
u32 spi
Definition: flow_types.api:140
u8 * sk_er
Definition: ikev2_priv.h:396
u8 is_initiator
Definition: ikev2_priv.h:423
static_always_inline void ikev2_unregister_udp_port(ikev2_profile_t *p)
Definition: ikev2.c:3648
static int ikev2_create_tunnel_interface(vlib_main_t *vm, ikev2_sa_t *sa, ikev2_child_sa_t *child, u32 sa_index, u32 child_index, u8 is_rekey)
Definition: ikev2.c:1933
#define ARRAY_LEN(x)
Definition: clib.h:67
#define IKEV2_PAYLOAD_KE
Definition: ikev2.h:109
static_always_inline uword ikev2_node_internal(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, u8 is_ip4)
Definition: ikev2.c:2837
string name[64]
Definition: ip.api:44
static_always_inline void ikev2_set_ip_address(ikev2_sa_t *sa, const void *iaddr, const void *raddr, const int af)
Definition: ikev2.c:2795
#define ikev2_log_error(...)
Definition: ikev2_priv.h:180
ikev2_ts_t loc_ts
Definition: ikev2_priv.h:337
ikev2_sa_proposal_t * r_proposals
Definition: ikev2_priv.h:389
static u32 ikev2_generate_message(vlib_buffer_t *b, ikev2_sa_t *sa, ike_header_t *ike, void *user, udp_header_t *udp)
Definition: ikev2.c:2267
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1580
static u8 * ikev2_sa_generate_authmsg(ikev2_sa_t *sa, int is_responder)
Definition: ikev2.c:1504
u64 rspi
static void ikev2_calc_child_keys(ikev2_sa_t *sa, ikev2_child_sa_t *child)
Definition: ikev2.c:594
f64 old_id_expiration
Definition: ikev2_priv.h:432
static int ikev2_parse_auth_payload(const void *p, u32 rlen, ikev2_auth_t *a)
Definition: ikev2.c:1134
#define hash_set1(h, key)
Definition: hash.h:258
enum ikev2_log_level_t_ ikev2_log_level_t
vlib_node_registration_t ip6_lookup_node
(constructor) VLIB_REGISTER_NODE (ip6_lookup_node)
Definition: ip6_forward.c:742
#define hash_create(elts, value_bytes)
Definition: hash.h:696
void ikev2_payload_add_auth(ikev2_payload_chain_t *c, ikev2_auth_t *auth)
ikev2_protocol_id_t protocol_id
Definition: ikev2_priv.h:240
vlib_combined_counter_main_t ipsec_sa_counters
SA packet & bytes counters.
Definition: ipsec_sa.c:27
u8 protocol_id
Definition: ikev2_priv.h:248
#define ASSERT(truth)
ip46_address_t local_ip
Definition: ikev2.c:2177
ikev2_notify_t * ikev2_parse_notify_payload(ike_payload_header_t *ikep, u32 rlen)
static_always_inline int ikev2_is_id_equal(ikev2_id_t *i1, ikev2_id_t *i2)
Definition: ikev2.c:1048
static_always_inline u8 * ikev2_compute_nat_sha1(u64 ispi, u64 rspi, ip_address_t *ia, u16 port)
Definition: ikev2.c:665
static_always_inline void ikev2_set_ts_addrs(ikev2_ts_t *ts, const ip_address_t *start, const ip_address_t *end)
Definition: ikev2.c:3929
#define ip_addr_v4(_a)
Definition: ip_types.h:91
static_always_inline void ikev2_rewrite_v4_addrs(ikev2_sa_t *sa, ip4_header_t *ih)
Definition: ikev2.c:2780
int ikev2_decrypt_aead_data(ikev2_main_per_thread_data_t *ptd, ikev2_sa_t *sa, ikev2_sa_transform_t *tr_encr, u8 *data, int data_len, u8 *aad, u32 aad_len, u8 *tag, u32 *out_len)
Definition: ikev2_crypto.c:351
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
#define ike_hdr_is_request(_h)
Definition: ikev2.h:48
vnet_main_t * vnet_main
Definition: ikev2_priv.h:486
f64 liveness_period_check
Definition: ikev2_priv.h:441
ip_dscp_t tos
Definition: ip4_packet.h:96
void ikev2_sa_free_proposal_vector(ikev2_sa_proposal_t **v)
Definition: ikev2.c:284
ipsec_sa_t * sad
Definition: ipsec.h:111
#define IKEV2_PAYLOAD_AUTH
Definition: ikev2.h:112
#define ikev2_payload_new_chain(V)
Definition: ikev2_priv.h:561
#define IKEV2_PAYLOAD_SK
Definition: ikev2.h:119
static vlib_node_registration_t ikev2_node_ip4
(constructor) VLIB_REGISTER_NODE (ikev2_node_ip4)
Definition: ikev2.c:3345
ikev2_sa_proposal_t * r_proposal
Definition: ikev2_priv.h:317
malformed
Definition: map.api:441
static void ikev2_process_sa_init_resp(vlib_main_t *vm, ikev2_sa_t *sa, ike_header_t *ike, udp_header_t *udp, u32 len)
Definition: ikev2.c:827
u8 * sk_ai
Definition: ikev2_priv.h:393
#define IKE_VERSION_2
Definition: ikev2.h:92
void ikev2_complete_dh(ikev2_sa_t *sa, ikev2_sa_transform_t *t)
Definition: ikev2_crypto.c:646
#define vec_append(v1, v2)
Append v2 after v1.
Definition: vec.h:890
int ikev2_encrypt_aead_data(ikev2_main_per_thread_data_t *ptd, ikev2_sa_t *sa, ikev2_sa_transform_t *tr_encr, v8 *src, u8 *dst, u8 *aad, u32 aad_len, u8 *tag)
Definition: ikev2_crypto.c:422
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:252
ikev2_ip6_next_t
Definition: ikev2.c:127
int ip_address_cmp(const ip_address_t *ip1, const ip_address_t *ip2)
Definition: ip_types.c:117
u16 dh_group
Definition: ikev2_priv.h:381
u8 is_tun_itf_set
Definition: ikev2_priv.h:427
static u8 plaintext[]
Definition: aes_cbc.c:29
ikev2_log_level_t log_level
Definition: ikev2_priv.h:505
ikev2_sa_proposal_t * i_proposal
Definition: ikev2_priv.h:316
u32 sw_if_index
Definition: ipip.h:86
vlib_node_registration_t ipsec4_tun_input_node
(constructor) VLIB_REGISTER_NODE (ipsec4_tun_input_node)
Definition: ipsec_tun_in.c:373
#define IKEV2_PAYLOAD_TSI
Definition: ikev2.h:117
EVP_PKEY * key
Definition: ikev2_priv.h:204
u8 liveness_retries
Definition: ikev2_priv.h:440
enum ip_address_family_t_ ip_address_family_t
typedef key
Definition: ipsec_types.api:86
ikev2_transform_encr_type_t encr_type
Definition: ikev2_priv.h:219
__clib_export uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:346
u32 crypto_key_size
Definition: ikev2_types.api:58
u8 * last_res_packet_data
Definition: ikev2_priv.h:421
counter_t bytes
byte counter
Definition: counter_types.h:29
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:297
ikev2_delete_t * ikev2_parse_delete_payload(ike_payload_header_t *ikep, u32 rlen)
ikev2_transform_integ_type_t integ_type
Definition: ikev2_priv.h:221
static char * ikev2_error_strings[]
Definition: ikev2.c:114
Definition: defs.h:47
#define clib_atomic_bool_cmp_and_swap(addr, old, new)
Definition: atomics.h:38
int vlib_punt_register(vlib_punt_hdl_t client, vlib_punt_reason_t reason, const char *node_name)
Register a node to receive particular punted buffers.
Definition: punt.c:268
u16 payload_length
Definition: ip6_packet.h:301
u8 protocol_id
Definition: ikev2_types.api:34
u32 ikev2_non_esp_marker
Definition: ikev2.c:134
static ip46_address_t to_ip46(u32 is_ipv6, u8 *buf)
Definition: ip46_address.h:137
#define IKEV2_PORT
Definition: ikev2.h:24
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
clib_error_t * ikev2_set_profile_responder(vlib_main_t *vm, u8 *name, u32 sw_if_index, ip_address_t addr)
Definition: ikev2.c:3977
u16 start_port
Definition: ikev2_priv.h:250
void ikev2_payload_chain_add_padding(ikev2_payload_chain_t *c, int bs)
u8 * sk_pr
Definition: ikev2_priv.h:398
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1581
static int ikev2_is_id_supported(u8 id_type)
Definition: ikev2.c:3873
VLIB buffer representation.
Definition: buffer.h:102
static int ikev2_parse_ke_payload(const void *p, u32 rlen, ikev2_sa_t *sa, u8 **ke_data)
Definition: ikev2.c:681
u64 uword
Definition: types.h:112
#define IKEV2_PAYLOAD_IDI
Definition: ikev2.h:110
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:3998
ikev2_id_t i_id
Definition: ikev2_priv.h:405
#define ikev2_elog_uint_peers(_level, _format, _val, _ip1, _ip2)
Definition: ikev2_priv.h:143
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:297
static u32 ikev2_retransmit_sa_init(ike_header_t *ike, ip_address_t iaddr, ip_address_t raddr, u32 rlen)
Definition: ikev2.c:2674
#define IKEV2_GENERATE_SA_INIT_OK_ERR_UNSUPP_STR
Definition: ikev2.c:67
u32 index
Definition: flow_types.api:221
static int ikev2_check_payload_length(const ike_payload_header_t *ikep, int rlen, u16 *plen)
Definition: ikev2.c:707
ikev2_ts_t * tsr
Definition: ikev2_priv.h:285
ikev2_child_sa_t * childs
Definition: ikev2_priv.h:438
vlib_log_class_t log_class
Definition: ikev2_priv.h:502
#define clib_error_free(e)
Definition: error.h:86
ikev2_transform_prf_type_t prf_type
Definition: ikev2_priv.h:220
int ipip_del_tunnel(u32 sw_if_index)
Definition: ipip.c:772
u16 port
Definition: lb_types.api:73
Linear Congruential Random Number Generator.
#define IKEV2_EXCHANGE_IKE_AUTH
Definition: ikev2.h:95
static int ikev2_process_create_child_sa_req(vlib_main_t *vm, ikev2_sa_t *sa, ike_header_t *ike, u32 len)
Definition: ikev2.c:1358
static int ikev2_ts_cmp(ikev2_ts_t *ts1, ikev2_ts_t *ts2)
Definition: ikev2.c:1547
#define vnet_buffer(b)
Definition: buffer.h:417
clib_error_t * ikev2_init(vlib_main_t *vm)
Definition: ikev2.c:4622
ip46_address_t local_ip
Definition: ikev2.c:1837
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1561
void ip_address_copy(ip_address_t *dst, const ip_address_t *src)
Definition: ip_types.c:133
#define ike_hdr_is_initiator(_h)
Definition: ikev2.h:49
ikev2_error_t
Definition: ikev2.c:106
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1105
ikev2_ts_t * ikev2_parse_ts_payload(ike_payload_header_t *ikep, u32 rlen)
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
u32 handover
Definition: ikev2_types.api:83
uword * sa_by_ispi
Definition: ikev2_priv.h:491
vl_api_address_t iaddr
#define vec_foreach(var, vec)
Vector iterator.
ip_address_t start_addr
Definition: ikev2_priv.h:252
u8 unsupported_cp
Definition: ikev2_priv.h:371
f64 end
end of the time range
Definition: mactime.api:44
u64 ispi
static void ikev2_sa_free_all_child_sa(ikev2_child_sa_t **childs)
Definition: ikev2.c:317
u16 flags
Copy of main node flags.
Definition: node.h:501
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:634
static ikev2_sa_proposal_t * ikev2_select_proposal(ikev2_sa_proposal_t *proposals, ikev2_protocol_id_t prot_id)
Definition: ikev2.c:179
u32 profile_index
Definition: ikev2_priv.h:426
ikev2_generate_sa_error_t
Definition: ikev2.c:70
u8 ip_version_and_header_length
Definition: ip4_packet.h:93
ikev2_transform_type_t
Definition: ikev2.h:217
static_always_inline void vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, int count)
Translate array of buffer indices into buffer pointers.
Definition: buffer_funcs.h:280
ikev2_transforms_set esp_ts
Definition: ikev2_priv.h:341
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:302
void ikev2_crypto_init(ikev2_main_t *km)
Definition: ikev2_crypto.c:869
static void ikev2_del_tunnel_from_main(ikev2_del_ipsec_tunnel_args_t *a)
Definition: ikev2.c:2194
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
u8 * ip_addr_bytes(ip_address_t *ip)
Definition: ip_types.c:149
void ikev2_payload_add_ts(ikev2_payload_chain_t *c, ikev2_ts_t *ts, u8 type)
ikev2_ip4_next_t
Definition: ikev2.c:120
vlib_main_t * vlib_main
Definition: ikev2_priv.h:485
clib_error_t * ikev2_set_profile_ts(vlib_main_t *vm, u8 *name, u8 protocol_id, u16 start_port, u16 end_port, ip_address_t start_addr, ip_address_t end_addr, int is_local)
Definition: ikev2.c:3937
static void ikev2_sa_free_all_vec(ikev2_sa_t *sa)
Definition: ikev2.c:333
u8 keys_generated
Definition: ikev2_priv.h:448
__clib_export u8 * format_clib_error(u8 *s, va_list *va)
Definition: error.c:191
#define VLIB_INITS(...)
Definition: init.h:357
static void ikev2_send_informational_request(ikev2_sa_t *sa)
Definition: ikev2.c:4932
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
#define IPSEC_UDP_PORT_NONE
Definition: ipsec_sa.h:290
static void ikev2_delete_sa(ikev2_main_per_thread_data_t *ptd, ikev2_sa_t *sa)
Definition: ikev2.c:377
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:314
__clib_export uword mhash_set_mem(mhash_t *h, void *key, uword *new_value, uword *old_value)
Definition: mhash.c:264
Definition: udp.h:93
u8 * ikev2_calc_sign(EVP_PKEY *pkey, u8 *data)
Definition: ikev2_crypto.c:782
v8 * ikev2_calc_integr(ikev2_sa_transform_t *tr, v8 *key, u8 *data, int len)
Definition: ikev2_crypto.c:314
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
u8 * ikev2_find_ike_notify_payload(ike_header_t *ike, u32 msg_type)
u8 * format_ikev2_id_type(u8 *s, va_list *args)
Definition: defs.h:46
#define foreach_ikev2_error
Definition: ikev2.c:95
void ikev2_disable_dpd(void)
Definition: ikev2.c:4980
ip6_address_t dst_address
Definition: ip6_packet.h:310
static void ikev2_init_sa(vlib_main_t *vm, ikev2_sa_t *sa)
Definition: ikev2.c:2736
u32 sw_if_index
Definition: ikev2.c:49
#define IKEV2_GCM_IV_SIZE
Definition: ikev2.h:31
u16 ipsec_over_udp_port
Definition: ikev2_priv.h:430
static int ikev2_process_auth_req(vlib_main_t *vm, ikev2_sa_t *sa, ike_header_t *ike, u32 len)
Definition: ikev2.c:1146
int ipsec_tun_protect_update(u32 sw_if_index, const ip_address_t *nh, u32 sa_out, u32 *sas_in)
Definition: ipsec_tun.c:654
void ikev2_payload_add_notify_2(ikev2_payload_chain_t *c, u16 msg_type, u8 *data, ikev2_notify_t *notify)