FD.io VPP  v21.06-3-gbb25fbf28
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 <plugins/dns/dns.h>
30 #include <openssl/sha.h>
31 #include <vnet/ipsec/ipsec_punt.h>
32 #include <plugins/ikev2/ikev2.api_enum.h>
33 
34 #define IKEV2_LIVENESS_RETRIES 3
35 #define IKEV2_LIVENESS_PERIOD_CHECK 30
36 
38 
40  ikev2_sa_t * sa,
41  ikev2_child_sa_t * child);
42 
43 #define ikev2_set_state(sa, v, ...) do { \
44  (sa)->state = v; \
45  ikev2_elog_sa_state("ispi %lx SA state changed to " #v __VA_ARGS__, sa->ispi); \
46  } while(0);
47 
48 typedef struct
49 {
53 
54 static u8 *
55 format_ikev2_trace (u8 * s, va_list * args)
56 {
57  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
58  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
59  ikev2_trace_t *t = va_arg (*args, ikev2_trace_t *);
60 
61  s = format (s, "ikev2: sw_if_index %d, next index %d",
62  t->sw_if_index, t->next_index);
63  return s;
64 }
65 
66 #define IKEV2_GENERATE_SA_INIT_OK_str ""
67 #define IKEV2_GENERATE_SA_INIT_OK_ERR_NO_DH_STR \
68  "no DH group configured for IKE proposals!"
69 #define IKEV2_GENERATE_SA_INIT_OK_ERR_UNSUPP_STR \
70  "DH group not supported!"
71 
72 typedef enum
73 {
78 
79 static u8 *
80 format_ikev2_gen_sa_error (u8 * s, va_list * args)
81 {
83  switch (e)
84  {
86  break;
89  break;
92  break;
93  }
94  return s;
95 }
96 
97 typedef enum
98 {
103 
104 typedef enum
105 {
110 
112 
115 {
117 }
118 
120 ikev2_insert_non_esp_marker (ike_header_t * ike, int len)
121 {
122  memmove ((u8 *) ike + sizeof (ikev2_non_esp_marker), ike, len);
123  clib_memset (ike, 0, sizeof (ikev2_non_esp_marker));
124  return len + sizeof (ikev2_non_esp_marker);
125 }
126 
127 static ikev2_sa_transform_t *
129 {
130  ikev2_main_t *km = &ikev2_main;
132 
134  {
135  if (td->type != t->type)
136  continue;
137 
138  if (td->transform_id != t->transform_id)
139  continue;
140 
141  if (td->type == IKEV2_TRANSFORM_TYPE_ENCR)
142  {
143  if (vec_len (t->attrs) != 4 || t->attrs[0] != 0x80
144  || t->attrs[1] != 14)
145  continue;
146 
147  if (((t->attrs[2] << 8 | t->attrs[3]) / 8) != td->key_len)
148  continue;
149  }
150  return td;
151  }
152  return 0;
153 }
154 
155 static ikev2_sa_proposal_t *
157  ikev2_protocol_id_t prot_id)
158 {
159  ikev2_sa_proposal_t *rv = 0;
160  ikev2_sa_proposal_t *proposal;
161  ikev2_sa_transform_t *transform, *new_t;
162  u8 mandatory_bitmap, optional_bitmap;
163 
164  if (prot_id == IKEV2_PROTOCOL_IKE)
165  {
166  mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
167  (1 << IKEV2_TRANSFORM_TYPE_PRF) | (1 << IKEV2_TRANSFORM_TYPE_DH);
168  optional_bitmap = mandatory_bitmap | (1 << IKEV2_TRANSFORM_TYPE_INTEG);
169  }
170  else if (prot_id == IKEV2_PROTOCOL_ESP)
171  {
172  mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
173  (1 << IKEV2_TRANSFORM_TYPE_ESN);
174  optional_bitmap = mandatory_bitmap |
175  (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
176  }
177  else if (prot_id == IKEV2_PROTOCOL_AH)
178  {
179  mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_INTEG) |
180  (1 << IKEV2_TRANSFORM_TYPE_ESN);
181  optional_bitmap = mandatory_bitmap | (1 << IKEV2_TRANSFORM_TYPE_DH);
182  }
183  else
184  return 0;
185 
186  vec_add2 (rv, proposal, 1);
187 
188  vec_foreach (proposal, proposals)
189  {
190  u8 bitmap = 0;
191  if (proposal->protocol_id != prot_id)
192  continue;
193 
194  vec_foreach (transform, proposal->transforms)
195  {
196  if ((1 << transform->type) & bitmap)
197  continue;
198 
199  if (ikev2_find_transform_data (transform))
200  {
201  bitmap |= 1 << transform->type;
202  vec_add2 (rv->transforms, new_t, 1);
203  clib_memcpy_fast (new_t, transform, sizeof (*new_t));
204  new_t->attrs = vec_dup (transform->attrs);
205  }
206  }
207 
208  if ((bitmap & mandatory_bitmap) == mandatory_bitmap &&
209  (bitmap & ~optional_bitmap) == 0)
210  {
211  rv->proposal_num = proposal->proposal_num;
212  rv->protocol_id = proposal->protocol_id;
213  RAND_bytes ((u8 *) & rv->spi, sizeof (rv->spi));
214  goto done;
215  }
216  else
217  {
218  vec_free (rv->transforms);
219  }
220  }
221 
222  vec_free (rv);
223 done:
224  return rv;
225 }
226 
230 {
232 
233  if (!p)
234  return 0;
235 
236  vec_foreach (t, p->transforms)
237  {
238  if (t->type == type)
239  return ikev2_find_transform_data (t);
240  }
241  return 0;
242 }
243 
246  int by_initiator)
247 {
249  vec_foreach (c, sa->childs)
250  {
251  ikev2_sa_proposal_t *proposal =
252  by_initiator ? &c->i_proposals[0] : &c->r_proposals[0];
253  if (proposal && proposal->spi == spi && proposal->protocol_id == prot_id)
254  return c;
255  }
256 
257  return 0;
258 }
259 
260 void
262 {
265 
266  if (!*v)
267  return;
268 
269  vec_foreach (p, *v)
270  {
271  vec_foreach (t, p->transforms)
272  {
273  vec_free (t->attrs);
274  }
275  vec_free (p->transforms);
276  }
277  vec_free (*v);
278 }
279 
280 static void
282 {
283  ikev2_sa_free_proposal_vector (&c->r_proposals);
284  ikev2_sa_free_proposal_vector (&c->i_proposals);
285  vec_free (c->sk_ai);
286  vec_free (c->sk_ar);
287  vec_free (c->sk_ei);
288  vec_free (c->sk_er);
289  vec_free (c->tsi);
290  vec_free (c->tsr);
291 }
292 
293 static void
295 {
297  vec_foreach (c, *childs) ikev2_sa_free_child_sa (c);
298 
299  vec_free (*childs);
300 }
301 
302 static void
304 {
305  ikev2_sa_free_child_sa (child);
306  vec_del1 (sa->childs, child - sa->childs);
307 }
308 
309 static void
311 {
312  vec_free (sa->i_nonce);
313  vec_free (sa->r_nonce);
314 
315  vec_free (sa->dh_shared_key);
316  vec_free (sa->dh_private_key);
317  vec_free (sa->i_dh_data);
318  vec_free (sa->r_dh_data);
319 
322 
323  vec_free (sa->sk_d);
324  vec_free (sa->sk_ai);
325  vec_free (sa->sk_ar);
326  vec_free (sa->sk_ei);
327  vec_free (sa->sk_er);
328  vec_free (sa->sk_pi);
329  vec_free (sa->sk_pr);
330 
331  vec_free (sa->i_id.data);
332  vec_free (sa->r_id.data);
333 
334  vec_free (sa->i_auth.data);
335  if (sa->i_auth.key)
336  EVP_PKEY_free (sa->i_auth.key);
337  vec_free (sa->r_auth.data);
338  if (sa->r_auth.key)
339  EVP_PKEY_free (sa->r_auth.key);
340 
341  vec_free (sa->del);
342 
343  vec_free (sa->rekey);
344 
347 
349 
351 }
352 
353 static void
355 {
356  uword *p;
357 
359 
360  p = hash_get (ptd->sa_by_rspi, sa->rspi);
361  if (p)
362  {
363  hash_unset (ptd->sa_by_rspi, sa->rspi);
364  pool_put (ptd->sas, sa);
365  }
366 }
367 
370 {
371  ikev2_sa_transform_t *t = 0, *t2;
372  ikev2_main_t *km = &ikev2_main;
373 
374  if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
376 
377  /* check if received DH group is on our list of supported groups */
379  {
380  if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
381  {
382  t = t2;
383  break;
384  }
385  }
386 
387  if (!t)
388  {
389  sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
391  }
392 
393  if (sa->is_initiator)
394  {
395  /* generate rspi */
396  RAND_bytes ((u8 *) & sa->ispi, 8);
397 
398  /* generate nonce */
400  RAND_bytes ((u8 *) sa->i_nonce, IKEV2_NONCE_SIZE);
401  }
402  else
403  {
404  /* generate rspi */
405  RAND_bytes ((u8 *) & sa->rspi, 8);
406 
407  /* generate nonce */
409  RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
410  }
411 
412  /* generate dh keys */
413  ikev2_generate_dh (sa, t);
414 
416 }
417 
418 static void
420 {
421  ikev2_sa_transform_t *t = 0, *t2;
422  ikev2_main_t *km = &ikev2_main;
423 
424  /*move some data to the new SA */
425 #define _(A) ({void* __tmp__ = (A); (A) = 0; __tmp__;})
426  sa->i_nonce = _(sai->i_nonce);
427  sa->i_dh_data = _(sai->i_dh_data);
428  sa->dh_private_key = _(sai->dh_private_key);
429  ip_address_copy (&sa->iaddr, &sai->iaddr);
430  ip_address_copy (&sa->raddr, &sai->raddr);
431  sa->is_initiator = sai->is_initiator;
432  sa->i_id.type = sai->i_id.type;
433  sa->r_id.type = sai->r_id.type;
434  sa->profile_index = sai->profile_index;
435  sa->tun_itf = sai->tun_itf;
436  sa->is_tun_itf_set = sai->is_tun_itf_set;
437  if (sai->natt_state == IKEV2_NATT_DISABLED)
439  sa->i_id.data = _(sai->i_id.data);
440  sa->r_id.data = _(sai->r_id.data);
441  sa->i_auth.method = sai->i_auth.method;
442  sa->i_auth.hex = sai->i_auth.hex;
443  sa->i_auth.data = _(sai->i_auth.data);
444  sa->i_auth.key = _(sai->i_auth.key);
447  sa->childs = _(sai->childs);
448  sa->udp_encap = sai->udp_encap;
450  sa->dst_port = sai->dst_port;
451  sa->sw_if_index = sai->sw_if_index;
452 #undef _
453 
454 
455  if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
456  {
457  return;
458  }
459 
460  /* check if received DH group is on our list of supported groups */
462  {
463  if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
464  {
465  t = t2;
466  break;
467  }
468  }
469 
470  if (!t)
471  {
472  sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
473  return;
474  }
475 
476 
477  /* generate dh keys */
478  ikev2_complete_dh (sa, t);
479 
480 }
481 
482 static void
484 {
485  u8 *tmp;
486  /* calculate SKEYSEED = prf(Ni | Nr, g^ir) */
487  u8 *skeyseed = 0;
488  u8 *s = 0;
489  u16 integ_key_len = 0, salt_len = 0;
490  ikev2_sa_transform_t *tr_encr, *tr_prf, *tr_integ;
491  tr_encr =
492  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
493  tr_prf =
494  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
495  tr_integ =
496  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
497 
498  if (tr_integ)
499  integ_key_len = tr_integ->key_len;
500  else
501  salt_len = sizeof (u32);
502 
503  vec_append (s, sa->i_nonce);
504  vec_append (s, sa->r_nonce);
505  skeyseed = ikev2_calc_prf (tr_prf, s, sa->dh_shared_key);
506 
507  /* Calculate S = Ni | Nr | SPIi | SPIr */
508  u64 *spi;
509  vec_add2 (s, tmp, 2 * sizeof (*spi));
510  spi = (u64 *) tmp;
511  spi[0] = clib_host_to_net_u64 (sa->ispi);
512  spi[1] = clib_host_to_net_u64 (sa->rspi);
513 
514  /* calculate PRFplus */
515  u8 *keymat;
516  int len = tr_prf->key_trunc + /* SK_d */
517  integ_key_len * 2 + /* SK_ai, SK_ar */
518  tr_encr->key_len * 2 + /* SK_ei, SK_er */
519  tr_prf->key_len * 2 + /* SK_pi, SK_pr */
520  salt_len * 2;
521 
522  keymat = ikev2_calc_prfplus (tr_prf, skeyseed, s, len);
523  vec_free (skeyseed);
524  vec_free (s);
525 
526  int pos = 0;
527 
528  /* SK_d */
529  sa->sk_d = vec_new (u8, tr_prf->key_trunc);
530  clib_memcpy_fast (sa->sk_d, keymat + pos, tr_prf->key_trunc);
531  pos += tr_prf->key_trunc;
532 
533  if (integ_key_len)
534  {
535  /* SK_ai */
536  sa->sk_ai = vec_new (u8, integ_key_len);
537  clib_memcpy_fast (sa->sk_ai, keymat + pos, integ_key_len);
538  pos += integ_key_len;
539 
540  /* SK_ar */
541  sa->sk_ar = vec_new (u8, integ_key_len);
542  clib_memcpy_fast (sa->sk_ar, keymat + pos, integ_key_len);
543  pos += integ_key_len;
544  }
545 
546  /* SK_ei */
547  sa->sk_ei = vec_new (u8, tr_encr->key_len + salt_len);
548  clib_memcpy_fast (sa->sk_ei, keymat + pos, tr_encr->key_len + salt_len);
549  pos += tr_encr->key_len + salt_len;
550 
551  /* SK_er */
552  sa->sk_er = vec_new (u8, tr_encr->key_len + salt_len);
553  clib_memcpy_fast (sa->sk_er, keymat + pos, tr_encr->key_len + salt_len);
554  pos += tr_encr->key_len + salt_len;
555 
556  /* SK_pi */
557  sa->sk_pi = vec_new (u8, tr_prf->key_len);
558  clib_memcpy_fast (sa->sk_pi, keymat + pos, tr_prf->key_len);
559  pos += tr_prf->key_len;
560 
561  /* SK_pr */
562  sa->sk_pr = vec_new (u8, tr_prf->key_len);
563  clib_memcpy_fast (sa->sk_pr, keymat + pos, tr_prf->key_len);
564  pos += tr_prf->key_len;
565 
566  vec_free (keymat);
567  sa->keys_generated = 1;
568 }
569 
570 static void
572 {
573  u8 *s = 0;
574  u16 integ_key_len = 0;
575  u8 salt_len = 0;
576 
577  ikev2_sa_transform_t *tr_prf, *ctr_encr, *ctr_integ;
578  tr_prf =
579  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
580  ctr_encr =
581  ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
582  ctr_integ =
583  ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
584 
585  if (ctr_integ)
586  integ_key_len = ctr_integ->key_len;
587  else
588  salt_len = sizeof (u32);
589 
590  vec_append (s, sa->i_nonce);
591  vec_append (s, sa->r_nonce);
592  /* calculate PRFplus */
593  u8 *keymat;
594  int len = ctr_encr->key_len * 2 + integ_key_len * 2 + salt_len * 2;
595 
596  keymat = ikev2_calc_prfplus (tr_prf, sa->sk_d, s, len);
597 
598  int pos = 0;
599 
600  /* SK_ei */
601  child->sk_ei = vec_new (u8, ctr_encr->key_len);
602  clib_memcpy_fast (child->sk_ei, keymat + pos, ctr_encr->key_len);
603  pos += ctr_encr->key_len;
604 
605  if (ctr_integ)
606  {
607  /* SK_ai */
608  child->sk_ai = vec_new (u8, ctr_integ->key_len);
609  clib_memcpy_fast (child->sk_ai, keymat + pos, ctr_integ->key_len);
610  pos += ctr_integ->key_len;
611  }
612  else
613  {
614  clib_memcpy (&child->salt_ei, keymat + pos, salt_len);
615  pos += salt_len;
616  }
617 
618  /* SK_er */
619  child->sk_er = vec_new (u8, ctr_encr->key_len);
620  clib_memcpy_fast (child->sk_er, keymat + pos, ctr_encr->key_len);
621  pos += ctr_encr->key_len;
622 
623  if (ctr_integ)
624  {
625  /* SK_ar */
626  child->sk_ar = vec_new (u8, integ_key_len);
627  clib_memcpy_fast (child->sk_ar, keymat + pos, integ_key_len);
628  pos += integ_key_len;
629  }
630  else
631  {
632  clib_memcpy (&child->salt_er, keymat + pos, salt_len);
633  pos += salt_len;
634  }
635 
636  ASSERT (pos == len);
637 
638  vec_free (keymat);
639 }
640 
643 {
644  const u32 max_buf_size =
645  sizeof (ispi) + sizeof (rspi) + sizeof (ip6_address_t) + sizeof (u16);
646  u8 buf[max_buf_size];
647  u8 *res = vec_new (u8, 20);
648 
649  clib_memcpy_fast (&buf[0], &ispi, sizeof (ispi));
650  clib_memcpy_fast (&buf[8], &rspi, sizeof (rspi));
651  clib_memcpy_fast (&buf[8 + 8], ip_addr_bytes (ia), ip_address_size (ia));
652  clib_memcpy_fast (&buf[8 + 8 + ip_address_size (ia)], &port, sizeof (port));
653  SHA1 (buf, 2 * sizeof (ispi) + sizeof (port) + ip_address_size (ia), res);
654  return res;
655 }
656 
657 static int
658 ikev2_parse_ke_payload (const void *p, u32 rlen, ikev2_sa_t * sa,
659  u8 ** ke_data)
660 {
661  const ike_ke_payload_header_t *ke = p;
662  u16 plen = clib_net_to_host_u16 (ke->length);
663  ASSERT (plen >= sizeof (*ke) && plen <= rlen);
664  if (sizeof (*ke) > rlen)
665  return 0;
666 
667  sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
668  vec_reset_length (ke_data[0]);
669  vec_add (ke_data[0], ke->payload, plen - sizeof (*ke));
670  return 1;
671 }
672 
673 static int
674 ikev2_parse_nonce_payload (const void *p, u32 rlen, u8 * nonce)
675 {
676  const ike_payload_header_t *ikep = p;
677  u16 plen = clib_net_to_host_u16 (ikep->length);
678  ASSERT (plen >= sizeof (*ikep) && plen <= rlen);
679  clib_memcpy_fast (nonce, ikep->payload, plen - sizeof (*ikep));
680  return 1;
681 }
682 
683 static int
684 ikev2_check_payload_length (const ike_payload_header_t * ikep, int rlen,
685  u16 * plen)
686 {
687  if (sizeof (*ikep) > rlen)
688  return 0;
689  *plen = clib_net_to_host_u16 (ikep->length);
690  if (*plen < sizeof (*ikep) || *plen > rlen)
691  return 0;
692  return 1;
693 }
694 
695 static int
698 {
699  u8 nonce[IKEV2_NONCE_SIZE];
700  int p = 0;
701  u8 payload = ike->nextpayload;
702  ike_payload_header_t *ikep;
703  u16 plen;
704 
705  ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT request received "
706  "from ", clib_net_to_host_u64 (ike->ispi),
707  clib_net_to_host_u64 (ike->rspi),
708  ip_addr_v4 (&sa->iaddr).as_u32,
709  ip_addr_version (&sa->iaddr) == AF_IP4);
710 
711  sa->ispi = clib_net_to_host_u64 (ike->ispi);
712  sa->sw_if_index = sw_if_index;
713 
714  /* store whole IKE payload - needed for PSK auth */
717 
718  if (len < sizeof (*ike))
719  return 0;
720 
721  len -= sizeof (*ike);
722  while (p < len && payload != IKEV2_PAYLOAD_NONE)
723  {
724  ikep = (ike_payload_header_t *) & ike->payload[p];
725  int current_length = len - p;
726  if (!ikev2_check_payload_length (ikep, current_length, &plen))
727  return 0;
728 
729  if (payload == IKEV2_PAYLOAD_SA)
730  {
732  sa->i_proposals = ikev2_parse_sa_payload (ikep, current_length);
733  }
734  else if (payload == IKEV2_PAYLOAD_KE)
735  {
736  if (!ikev2_parse_ke_payload (ikep, current_length, sa,
737  &sa->i_dh_data))
738  return 0;
739  }
740  else if (payload == IKEV2_PAYLOAD_NONCE)
741  {
743  if (ikev2_parse_nonce_payload (ikep, current_length, nonce))
744  vec_add (sa->i_nonce, nonce, plen - sizeof (*ikep));
745  }
746  else if (payload == IKEV2_PAYLOAD_NOTIFY)
747  {
748  ikev2_notify_t *n =
749  ikev2_parse_notify_payload (ikep, current_length);
750  if (n->msg_type == IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP)
751  {
752  u8 *src_sha = ikev2_compute_nat_sha1 (ike->ispi, 0, &sa->iaddr,
753  udp->src_port);
754  if (clib_memcmp (src_sha, n->data, vec_len (src_sha)))
755  {
756  if (sa->natt_state == IKEV2_NATT_ENABLED)
758  ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx initiator"
759  " behind NAT", sa->ispi);
760  }
761  vec_free (src_sha);
762  }
763  else if (n->msg_type ==
764  IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP)
765  {
766  u8 *dst_sha = ikev2_compute_nat_sha1 (ike->ispi, 0, &sa->raddr,
767  udp->dst_port);
768  if (clib_memcmp (dst_sha, n->data, vec_len (dst_sha)))
769  {
770  if (sa->natt_state == IKEV2_NATT_ENABLED)
772  ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx responder"
773  " (self) behind NAT", sa->ispi);
774  }
775  vec_free (dst_sha);
776  }
777  vec_free (n);
778  }
779  else if (payload == IKEV2_PAYLOAD_VENDOR)
780  {
782  }
783  else
784  {
785  ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
786  payload);
787  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
788  {
790  sa->unsupported_cp = payload;
791  return 0;
792  }
793  }
794 
795  payload = ikep->nextpayload;
796  p += plen;
797  }
798 
800  return 1;
801 }
802 
803 static void
805  ikev2_sa_t * sa, ike_header_t * ike,
806  udp_header_t * udp, u32 len)
807 {
808  u8 nonce[IKEV2_NONCE_SIZE];
809  int p = 0;
810  u8 payload = ike->nextpayload;
811  ike_payload_header_t *ikep;
812  u16 plen;
813 
814  sa->ispi = clib_net_to_host_u64 (ike->ispi);
815  sa->rspi = clib_net_to_host_u64 (ike->rspi);
816 
817  ikev2_elog_exchange ("ispi %lx rspi %lx IKE_INIT response received "
818  "from ", sa->ispi, sa->rspi,
819  ip_addr_v4 (&sa->raddr).as_u32,
820  ip_addr_version (&sa->raddr) == AF_IP4);
821 
822  /* store whole IKE payload - needed for PSK auth */
825 
826  if (sizeof (*ike) > len)
827  return;
828 
829  len -= sizeof (*ike);
830  while (p < len && payload != IKEV2_PAYLOAD_NONE)
831  {
832  int current_length = len - p;
833  ikep = (ike_payload_header_t *) & ike->payload[p];
834  if (!ikev2_check_payload_length (ikep, current_length, &plen))
835  return;
836 
837  if (payload == IKEV2_PAYLOAD_SA)
838  {
840  sa->r_proposals = ikev2_parse_sa_payload (ikep, current_length);
841  if (sa->r_proposals)
842  {
844  ike->msgid =
845  clib_host_to_net_u32 (clib_net_to_host_u32 (ike->msgid) + 1);
846  }
847  }
848  else if (payload == IKEV2_PAYLOAD_KE)
849  {
850  if (!ikev2_parse_ke_payload (ikep, current_length, sa,
851  &sa->r_dh_data))
852  return;
853  }
854  else if (payload == IKEV2_PAYLOAD_NONCE)
855  {
857  if (ikev2_parse_nonce_payload (ikep, current_length, nonce))
858  vec_add (sa->r_nonce, nonce, plen - sizeof (*ikep));
859  }
860  else if (payload == IKEV2_PAYLOAD_NOTIFY)
861  {
862  ikev2_notify_t *n =
863  ikev2_parse_notify_payload (ikep, current_length);
864  if (n->msg_type == IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP)
865  {
866  u8 *src_sha = ikev2_compute_nat_sha1 (ike->ispi, ike->rspi,
867  &sa->raddr,
868  udp->src_port);
869  if (clib_memcmp (src_sha, n->data, vec_len (src_sha)))
870  {
871  ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx responder"
872  " behind NAT, unsupported", sa->ispi);
873  }
874  vec_free (src_sha);
875  }
876  else if (n->msg_type ==
877  IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP)
878  {
879  u8 *dst_sha = ikev2_compute_nat_sha1 (ike->ispi, ike->rspi,
880  &sa->iaddr,
881  udp->dst_port);
882  if (clib_memcmp (dst_sha, n->data, vec_len (dst_sha)))
883  {
884  if (sa->natt_state == IKEV2_NATT_ENABLED)
886  ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx initiator"
887  " (self) behind NAT", sa->ispi);
888  }
889  vec_free (dst_sha);
890  }
891  vec_free (n);
892  }
893  else if (payload == IKEV2_PAYLOAD_VENDOR)
894  {
896  }
897  else
898  {
899  ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
900  payload);
901  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
902  {
904  sa->unsupported_cp = payload;
905  return;
906  }
907  }
908 
909  payload = ikep->nextpayload;
910  p += plen;
911  }
912 }
913 
914 static u8 *
916  u8 * payload, u32 rlen, u32 * out_len)
917 {
919  int p = 0;
920  u8 last_payload = 0, *hmac = 0, *plaintext = 0;
921  ike_payload_header_t *ikep = 0;
922  u16 plen = 0;
923  u32 dlen = 0;
924  ikev2_sa_transform_t *tr_integ;
925  ikev2_sa_transform_t *tr_encr;
926  tr_integ =
927  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
928  tr_encr =
929  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
930  int is_aead = tr_encr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16;
931 
932  if (((!sa->sk_ar || !sa->sk_ai) && !is_aead) || (!sa->sk_ei || !sa->sk_er))
933  return 0;
934 
935  if (rlen <= sizeof (*ike))
936  return 0;
937 
938  int len = rlen - sizeof (*ike);
939  while (p < len &&
940  *payload != IKEV2_PAYLOAD_NONE && last_payload != IKEV2_PAYLOAD_SK)
941  {
942  ikep = (ike_payload_header_t *) & ike->payload[p];
943  int current_length = len - p;
944  if (!ikev2_check_payload_length (ikep, current_length, &plen))
945  return 0;
946 
947  if (*payload == IKEV2_PAYLOAD_SK)
948  {
949  last_payload = *payload;
950  }
951  else
952  {
953  ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
954  *payload);
955  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
956  {
957  sa->unsupported_cp = *payload;
958  return 0;
959  }
960  }
961 
962  *payload = ikep->nextpayload;
963  p += plen;
964  }
965 
966  if (last_payload != IKEV2_PAYLOAD_SK)
967  {
968  ikev2_elog_error ("Last payload must be SK");
969  return 0;
970  }
971 
972  if (is_aead)
973  {
974  if (plen < sizeof (*ikep) + IKEV2_GCM_ICV_SIZE)
975  return 0;
976 
977  plen -= sizeof (*ikep) + IKEV2_GCM_ICV_SIZE;
978  u8 *aad = (u8 *) ike;
979  u32 aad_len = ikep->payload - aad;
980  u8 *tag = ikep->payload + plen;
981 
982  int rc = ikev2_decrypt_aead_data (ptd, sa, tr_encr, ikep->payload,
983  plen, aad, aad_len, tag, &dlen);
984  if (rc)
985  {
986  *out_len = dlen;
987  plaintext = ikep->payload + IKEV2_GCM_IV_SIZE;
988  }
989  }
990  else
991  {
992  if (rlen < tr_integ->key_trunc)
993  return 0;
994 
995  hmac =
996  ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ar : sa->sk_ai,
997  (u8 *) ike, rlen - tr_integ->key_trunc);
998 
999  if (plen < sizeof (*ikep) + tr_integ->key_trunc)
1000  return 0;
1001 
1002  plen = plen - sizeof (*ikep) - tr_integ->key_trunc;
1003 
1004  if (clib_memcmp (hmac, &ikep->payload[plen], tr_integ->key_trunc))
1005  {
1006  ikev2_elog_error ("message integrity check failed");
1007  vec_free (hmac);
1008  return 0;
1009  }
1010  vec_free (hmac);
1011 
1012  int rc = ikev2_decrypt_data (ptd, sa, tr_encr, ikep->payload, plen,
1013  &dlen);
1014  if (rc)
1015  {
1016  *out_len = dlen;
1017  plaintext = ikep->payload + tr_encr->block_size;
1018  }
1019  }
1020 
1021  return plaintext;
1022 }
1023 
1026 {
1027  if (i1->type != i2->type)
1028  return 0;
1029 
1030  if (vec_len (i1->data) != vec_len (i2->data))
1031  return 0;
1032 
1033  if (clib_memcmp (i1->data, i2->data, vec_len (i1->data)))
1034  return 0;
1035 
1036  return 1;
1037 }
1038 
1039 static void
1041  ikev2_sa_t * sa)
1042 {
1043  ikev2_main_t *km = &ikev2_main;
1044  ikev2_sa_t *tmp;
1045  u32 i, *delete = 0;
1047 
1048  /* find old IKE SAs with the same authenticated identity */
1049  /* *INDENT-OFF* */
1050  pool_foreach (tmp, ptd->sas) {
1051  if (!ikev2_is_id_equal (&tmp->i_id, &sa->i_id)
1052  || !ikev2_is_id_equal(&tmp->r_id, &sa->r_id))
1053  continue;
1054 
1055  if (sa->rspi != tmp->rspi)
1056  vec_add1(delete, tmp - ptd->sas);
1057  }
1058  /* *INDENT-ON* */
1059 
1060  for (i = 0; i < vec_len (delete); i++)
1061  {
1062  tmp = pool_elt_at_index (ptd->sas, delete[i]);
1063  vec_foreach (c, tmp->childs)
1064  {
1066  }
1067  ikev2_delete_sa (ptd, tmp);
1068  }
1069 
1070  vec_free (delete);
1071  sa->initial_contact = 0;
1072 }
1073 
1074 static void
1076  ikev2_sa_t * sa)
1077 {
1078  ikev2_main_t *km = &ikev2_main;
1079 
1080  if (!sa->initial_contact)
1081  return;
1082 
1083  if (ptd)
1084  {
1086  }
1087  else
1088  {
1089  vec_foreach (ptd, km->per_thread_data)
1091  }
1092  sa->initial_contact = 0;
1093 }
1094 
1095 static int
1096 ikev2_parse_id_payload (const void *p, u16 rlen, ikev2_id_t * sa_id)
1097 {
1098  const ike_id_payload_header_t *id = p;
1099  u16 plen = clib_net_to_host_u16 (id->length);
1100  if (plen < sizeof (*id) || plen > rlen)
1101  return 0;
1102 
1103  sa_id->type = id->id_type;
1104  vec_reset_length (sa_id->data);
1105  vec_add (sa_id->data, id->payload, plen - sizeof (*id));
1106 
1107  return 1;
1108 }
1109 
1110 static int
1111 ikev2_parse_auth_payload (const void *p, u32 rlen, ikev2_auth_t * a)
1112 {
1113  const ike_auth_payload_header_t *ah = p;
1114  u16 plen = clib_net_to_host_u16 (ah->length);
1115 
1116  a->method = ah->auth_method;
1117  vec_reset_length (a->data);
1118  vec_add (a->data, ah->payload, plen - sizeof (*ah));
1119  return 1;
1120 }
1121 
1122 static int
1124  ike_header_t * ike, u32 len)
1125 {
1126  int p = 0;
1127  ikev2_child_sa_t *first_child_sa;
1128  u8 payload = ike->nextpayload;
1129  u8 *plaintext = 0;
1130  ike_payload_header_t *ikep;
1131  u16 plen;
1132  u32 dlen = 0;
1133 
1134  ikev2_elog_exchange ("ispi %lx rspi %lx EXCHANGE_IKE_AUTH received "
1135  "from ", clib_host_to_net_u64 (ike->ispi),
1136  clib_host_to_net_u64 (ike->rspi),
1137  sa->is_initiator ?
1138  ip_addr_v4 (&sa->raddr).as_u32 :
1139  ip_addr_v4 (&sa->iaddr).as_u32,
1140  ip_addr_version (&sa->raddr) == AF_IP4);
1141 
1142  ikev2_calc_keys (sa);
1143 
1144  plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload, len, &dlen);
1145 
1146  if (!plaintext)
1147  {
1148  if (sa->unsupported_cp)
1149  {
1151  return 0;
1152  }
1153  goto malformed;
1154  }
1155 
1156  /* select or create 1st child SA */
1157  if (sa->is_initiator)
1158  {
1159  first_child_sa = &sa->childs[0];
1160  }
1161  else
1162  {
1164  vec_add2 (sa->childs, first_child_sa, 1);
1165  }
1166 
1167 
1168  /* process encrypted payload */
1169  while (p < dlen && payload != IKEV2_PAYLOAD_NONE)
1170  {
1171  ikep = (ike_payload_header_t *) & plaintext[p];
1172  int current_length = dlen - p;
1173  if (!ikev2_check_payload_length (ikep, current_length, &plen))
1174  goto malformed;
1175 
1176  if (payload == IKEV2_PAYLOAD_SA) /* 33 */
1177  {
1178  if (sa->is_initiator)
1179  {
1180  ikev2_sa_free_proposal_vector (&first_child_sa->r_proposals);
1181  first_child_sa->r_proposals = ikev2_parse_sa_payload (ikep,
1182  current_length);
1183  }
1184  else
1185  {
1186  ikev2_sa_free_proposal_vector (&first_child_sa->i_proposals);
1187  first_child_sa->i_proposals = ikev2_parse_sa_payload (ikep,
1188  current_length);
1189  }
1190  }
1191  else if (payload == IKEV2_PAYLOAD_IDI) /* 35 */
1192  {
1193  if (!ikev2_parse_id_payload (ikep, current_length, &sa->i_id))
1194  goto malformed;
1195  }
1196  else if (payload == IKEV2_PAYLOAD_IDR) /* 36 */
1197  {
1198  if (!ikev2_parse_id_payload (ikep, current_length, &sa->r_id))
1199  goto malformed;
1200  }
1201  else if (payload == IKEV2_PAYLOAD_AUTH) /* 39 */
1202  {
1203  if (sa->is_initiator)
1204  {
1205  if (!ikev2_parse_auth_payload (ikep, current_length,
1206  &sa->r_auth))
1207  goto malformed;
1208  }
1209  else
1210  {
1211  if (!ikev2_parse_auth_payload (ikep, current_length,
1212  &sa->i_auth))
1213  goto malformed;
1214  }
1215  }
1216  else if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
1217  {
1218  ikev2_notify_t *n =
1219  ikev2_parse_notify_payload (ikep, current_length);
1220  if (n->msg_type == IKEV2_NOTIFY_MSG_INITIAL_CONTACT)
1221  {
1222  sa->initial_contact = 1;
1223  }
1224  vec_free (n);
1225  }
1226  else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
1227  {
1229  }
1230  else if (payload == IKEV2_PAYLOAD_TSI) /* 44 */
1231  {
1232  vec_free (first_child_sa->tsi);
1233  first_child_sa->tsi = ikev2_parse_ts_payload (ikep, current_length);
1234  }
1235  else if (payload == IKEV2_PAYLOAD_TSR) /* 45 */
1236  {
1237  vec_free (first_child_sa->tsr);
1238  first_child_sa->tsr = ikev2_parse_ts_payload (ikep, current_length);
1239  }
1240  else
1241  {
1242  ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1243  payload);
1244 
1245  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1246  {
1248  sa->unsupported_cp = payload;
1249  return 0;
1250  }
1251  }
1252 
1253  payload = ikep->nextpayload;
1254  p += plen;
1255  }
1256 
1257  return 1;
1258 
1259 malformed:
1260  ikev2_set_state (sa, IKEV2_STATE_DELETED, ": malformed IKE_AUTH");
1261  return 0;
1262 }
1263 
1264 static int
1266  ikev2_sa_t * sa, ike_header_t * ike, u32 len)
1267 {
1268  int p = 0;
1269  u8 payload = ike->nextpayload;
1270  u8 *plaintext = 0;
1271  ike_payload_header_t *ikep;
1272  u32 dlen = 0;
1273  ikev2_notify_t *n = 0;
1274 
1275  sa->liveness_retries = 0;
1276  ikev2_elog_exchange ("ispi %lx rspi %lx INFORMATIONAL received "
1277  "from ", clib_host_to_net_u64 (ike->ispi),
1278  clib_host_to_net_u64 (ike->rspi),
1279  ip_addr_v4 (&sa->iaddr).as_u32,
1280  ip_addr_version (&sa->iaddr) == AF_IP4);
1281 
1282  plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload, len, &dlen);
1283 
1284  if (!plaintext)
1285  return 0;
1286 
1287  /* process encrypted payload */
1288  p = 0;
1289  while (p < dlen && payload != IKEV2_PAYLOAD_NONE)
1290  {
1291  u32 current_length = dlen - p;
1292  if (p + sizeof (*ikep) > dlen)
1293  return 0;
1294 
1295  ikep = (ike_payload_header_t *) & plaintext[p];
1296  u16 plen = clib_net_to_host_u16 (ikep->length);
1297 
1298  if (plen < sizeof (*ikep) || plen > current_length)
1299  return 0;
1300 
1301  if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
1302  {
1303  n = ikev2_parse_notify_payload (ikep, current_length);
1304  if (!n)
1305  return 0;
1306  if (n->msg_type == IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED)
1308  vec_free (n);
1309  }
1310  else if (payload == IKEV2_PAYLOAD_DELETE) /* 42 */
1311  {
1312  sa->del = ikev2_parse_delete_payload (ikep, current_length);
1313  }
1314  else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
1315  {
1317  }
1318  else
1319  {
1320  ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1321  payload);
1322  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1323  {
1324  sa->unsupported_cp = payload;
1325  return 0;
1326  }
1327  }
1328  payload = ikep->nextpayload;
1329  p += plen;
1330  }
1331  return 1;
1332 }
1333 
1334 static int
1336  ikev2_sa_t * sa, ike_header_t * ike,
1337  u32 len)
1338 {
1339  int p = 0;
1340  u8 payload = ike->nextpayload;
1341  u8 *plaintext = 0;
1342  u8 rekeying = 0;
1343  u8 nonce[IKEV2_NONCE_SIZE];
1344  ikev2_rekey_t *rekey;
1345  ike_payload_header_t *ikep;
1346  ikev2_notify_t *n = 0;
1347  ikev2_ts_t *tsi = 0;
1348  ikev2_ts_t *tsr = 0;
1349  ikev2_sa_proposal_t *proposal = 0;
1350  ikev2_child_sa_t *child_sa;
1351  u32 dlen = 0, src;
1352  u16 plen;
1353 
1354  if (sa->is_initiator)
1355  src = ip_addr_v4 (&sa->raddr).as_u32;
1356  else
1357  src = ip_addr_v4 (&sa->iaddr).as_u32;
1358 
1359  ikev2_elog_exchange ("ispi %lx rspi %lx CREATE_CHILD_SA received from",
1360  clib_host_to_net_u64 (ike->ispi),
1361  clib_host_to_net_u64 (ike->rspi), src,
1362  ip_addr_version (&sa->raddr) == AF_IP4);
1363 
1364  plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload, len, &dlen);
1365 
1366  if (!plaintext)
1367  goto cleanup_and_exit;
1368 
1369  /* process encrypted payload */
1370  p = 0;
1371  while (payload != IKEV2_PAYLOAD_NONE)
1372  {
1373  ikep = (ike_payload_header_t *) & plaintext[p];
1374  int current_length = dlen - p;
1375  if (!ikev2_check_payload_length (ikep, current_length, &plen))
1376  goto cleanup_and_exit;
1377 
1378  if (payload == IKEV2_PAYLOAD_SA)
1379  {
1380  proposal = ikev2_parse_sa_payload (ikep, current_length);
1381  }
1382  else if (payload == IKEV2_PAYLOAD_NOTIFY)
1383  {
1384  n = ikev2_parse_notify_payload (ikep, current_length);
1385  if (n->msg_type == IKEV2_NOTIFY_MSG_REKEY_SA)
1386  {
1387  rekeying = 1;
1388  }
1389  }
1390  else if (payload == IKEV2_PAYLOAD_DELETE)
1391  {
1392  sa->del = ikev2_parse_delete_payload (ikep, current_length);
1393  }
1394  else if (payload == IKEV2_PAYLOAD_VENDOR)
1395  {
1397  }
1398  else if (payload == IKEV2_PAYLOAD_NONCE)
1399  {
1400  ikev2_parse_nonce_payload (ikep, current_length, nonce);
1401  }
1402  else if (payload == IKEV2_PAYLOAD_TSI)
1403  {
1404  tsi = ikev2_parse_ts_payload (ikep, current_length);
1405  }
1406  else if (payload == IKEV2_PAYLOAD_TSR)
1407  {
1408  tsr = ikev2_parse_ts_payload (ikep, current_length);
1409  }
1410  else
1411  {
1412  ikev2_elog_uint (IKEV2_LOG_ERROR, "Unknown payload! type=%d",
1413  payload);
1414  if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1415  {
1416  sa->unsupported_cp = payload;
1417  goto cleanup_and_exit;
1418  }
1419  }
1420  payload = ikep->nextpayload;
1421  p += plen;
1422  }
1423 
1424  if (!proposal || proposal->protocol_id != IKEV2_PROTOCOL_ESP)
1425  goto cleanup_and_exit;
1426 
1427  if (sa->is_initiator)
1428  {
1429  rekey = sa->rekey;
1430  if (vec_len (rekey) == 0)
1431  goto cleanup_and_exit;
1432  rekey->protocol_id = proposal->protocol_id;
1433  rekey->i_proposal =
1435  rekey->i_proposal->spi = rekey->spi;
1436  rekey->r_proposal = proposal;
1437  rekey->tsi = tsi;
1438  rekey->tsr = tsr;
1439  /* update Nr */
1440  vec_reset_length (sa->r_nonce);
1441  vec_add (sa->r_nonce, nonce, IKEV2_NONCE_SIZE);
1442  child_sa = ikev2_sa_get_child (sa, rekey->ispi, IKEV2_PROTOCOL_ESP, 1);
1443  if (child_sa)
1444  {
1445  child_sa->rekey_retries = 0;
1446  }
1447  }
1448  else
1449  {
1450  if (rekeying)
1451  {
1452  child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id, 1);
1453  if (!child_sa)
1454  {
1455  ikev2_elog_uint (IKEV2_LOG_ERROR, "child SA spi %lx not found",
1456  n->spi);
1457  goto cleanup_and_exit;
1458  }
1459  vec_add2 (sa->rekey, rekey, 1);
1460  rekey->protocol_id = n->protocol_id;
1461  rekey->spi = n->spi;
1462  rekey->i_proposal = proposal;
1463  rekey->r_proposal =
1465  /* update Ni */
1466  vec_reset_length (sa->i_nonce);
1467  vec_add (sa->i_nonce, nonce, IKEV2_NONCE_SIZE);
1468  /* generate new Nr */
1470  RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
1471  }
1472  else
1473  {
1474  /* create new child SA */
1475  vec_add2 (sa->new_child, rekey, 1);
1476  rekey->i_proposal = proposal;
1477  rekey->r_proposal =
1479  /* update Ni */
1480  vec_reset_length (sa->i_nonce);
1481  vec_add (sa->i_nonce, nonce, IKEV2_NONCE_SIZE);
1482  /* generate new Nr */
1484  RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
1485  }
1486  rekey->tsi = tsi;
1487  rekey->tsr = tsr;
1488  }
1489  vec_free (n);
1490  return 1;
1491 
1492 cleanup_and_exit:
1493  vec_free (n);
1494  vec_free (proposal);
1495  vec_free (tsr);
1496  vec_free (tsi);
1497  return 0;
1498 }
1499 
1500 static u8 *
1501 ikev2_sa_generate_authmsg (ikev2_sa_t * sa, int is_responder)
1502 {
1503  u8 *authmsg = 0;
1504  u8 *data;
1505  u8 *nonce;
1506  ikev2_id_t *id;
1507  u8 *key;
1508  u8 *packet_data;
1509  ikev2_sa_transform_t *tr_prf;
1510 
1511  tr_prf =
1512  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1513 
1514  if (is_responder)
1515  {
1516  id = &sa->r_id;
1517  key = sa->sk_pr;
1518  nonce = sa->i_nonce;
1519  packet_data = sa->last_sa_init_res_packet_data;
1520  }
1521  else
1522  {
1523  id = &sa->i_id;
1524  key = sa->sk_pi;
1525  nonce = sa->r_nonce;
1526  packet_data = sa->last_sa_init_req_packet_data;
1527  }
1528 
1529  data = vec_new (u8, 4);
1530  data[0] = id->type;
1531  vec_append (data, id->data);
1532 
1533  u8 *id_hash = ikev2_calc_prf (tr_prf, key, data);
1534  vec_append (authmsg, packet_data);
1535  vec_append (authmsg, nonce);
1536  vec_append (authmsg, id_hash);
1537  vec_free (id_hash);
1538  vec_free (data);
1539 
1540  return authmsg;
1541 }
1542 
1543 static int
1545 {
1546  if (ts1->ts_type == ts2->ts_type && ts1->protocol_id == ts2->protocol_id &&
1547  ts1->start_port == ts2->start_port && ts1->end_port == ts2->end_port &&
1548  !ip_address_cmp (&ts1->start_addr, &ts2->start_addr) &&
1549  !ip_address_cmp (&ts1->end_addr, &ts2->end_addr))
1550  return 1;
1551 
1552  return 0;
1553 }
1554 
1555 static void
1557 {
1558  ikev2_main_t *km = &ikev2_main;
1559  ikev2_profile_t *p;
1560  ikev2_ts_t *ts, *p_tsi, *p_tsr, *tsi = 0, *tsr = 0;
1561  ikev2_id_t *id_rem, *id_loc;
1562 
1563  /* *INDENT-OFF* */
1564  pool_foreach (p, km->profiles) {
1565 
1566  if (sa->is_initiator)
1567  {
1568  p_tsi = &p->loc_ts;
1569  p_tsr = &p->rem_ts;
1570  id_rem = &sa->r_id;
1571  id_loc = &sa->i_id;
1572  }
1573  else
1574  {
1575  p_tsi = &p->rem_ts;
1576  p_tsr = &p->loc_ts;
1577  id_rem = &sa->i_id;
1578  id_loc = &sa->r_id;
1579  }
1580 
1581  /* check id */
1582  if (!ikev2_is_id_equal (&p->rem_id, id_rem)
1583  || !ikev2_is_id_equal (&p->loc_id, id_loc))
1584  continue;
1585 
1586  sa->profile_index = p - km->profiles;
1587 
1588  vec_foreach(ts, sa->childs[0].tsi)
1589  {
1590  if (ikev2_ts_cmp(p_tsi, ts))
1591  {
1592  vec_add1 (tsi, ts[0]);
1593  break;
1594  }
1595  }
1596 
1597  vec_foreach(ts, sa->childs[0].tsr)
1598  {
1599  if (ikev2_ts_cmp(p_tsr, ts))
1600  {
1601  vec_add1 (tsr, ts[0]);
1602  break;
1603  }
1604  }
1605 
1606  break;
1607  }
1608  /* *INDENT-ON* */
1609 
1610  if (tsi && tsr)
1611  {
1612  vec_free (sa->childs[0].tsi);
1613  vec_free (sa->childs[0].tsr);
1614  sa->childs[0].tsi = tsi;
1615  sa->childs[0].tsr = tsr;
1616  }
1617  else
1618  {
1619  vec_free (tsi);
1620  vec_free (tsr);
1622  }
1623 }
1624 
1625 static ikev2_profile_t *
1627  ikev2_sa_transform_t *tr_prf, u8 *key_pad)
1628 {
1629  ikev2_profile_t *ret = 0, *p;
1630  ikev2_id_t *id_rem, *id_loc;
1631  ikev2_auth_t *sa_auth;
1632  u8 *authmsg, *psk = 0, *auth = 0;
1633 
1634  authmsg = ikev2_sa_generate_authmsg (sa, sa->is_initiator);
1635 
1636  if (sa->is_initiator)
1637  {
1638  id_rem = &sa->r_id;
1639  id_loc = &sa->i_id;
1640  sa_auth = &sa->r_auth;
1641  }
1642  else
1643  {
1644  id_rem = &sa->i_id;
1645  id_loc = &sa->r_id;
1646  sa_auth = &sa->i_auth;
1647  }
1648 
1649  pool_foreach (p, km->profiles)
1650  {
1651  /* check id */
1652  if (!ikev2_is_id_equal (&p->rem_id, id_rem) ||
1653  !ikev2_is_id_equal (&p->loc_id, id_loc))
1654  continue;
1655 
1656  if (sa_auth->method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1657  {
1658  if (!p->auth.data ||
1659  p->auth.method != IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1660  continue;
1661 
1662  psk = ikev2_calc_prf (tr_prf, p->auth.data, key_pad);
1663  auth = ikev2_calc_prf (tr_prf, psk, authmsg);
1664 
1665  if (!clib_memcmp (auth, sa_auth->data, vec_len (sa_auth->data)))
1666  {
1668  vec_free (auth);
1669  ret = p;
1670  break;
1671  }
1672  else
1673  {
1674  ikev2_elog_uint (IKEV2_LOG_ERROR,
1675  "shared key mismatch! ispi %lx", sa->ispi);
1676  }
1677  }
1678  else if (sa_auth->method == IKEV2_AUTH_METHOD_RSA_SIG)
1679  {
1680  if (p->auth.method != IKEV2_AUTH_METHOD_RSA_SIG)
1681  continue;
1682 
1683  if (ikev2_verify_sign (p->auth.key, sa_auth->data, authmsg) == 1)
1684  {
1686  ret = p;
1687  break;
1688  }
1689  else
1690  {
1691  ikev2_elog_uint (IKEV2_LOG_ERROR,
1692  "cert verification failed! ispi %lx", sa->ispi);
1693  }
1694  }
1695  }
1696  vec_free (authmsg);
1697  return ret;
1698 }
1699 
1700 static void
1702 {
1703  ikev2_main_t *km = &ikev2_main;
1704  ikev2_profile_t *sel_p = 0;
1705  ikev2_sa_transform_t *tr_prf;
1706  u8 *psk, *authmsg, *key_pad;
1707 
1708  tr_prf =
1709  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1710 
1711  /* only shared key and rsa signature */
1712  if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1713  sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1714  {
1715  ikev2_elog_uint (IKEV2_LOG_ERROR, "unsupported authentication method %u",
1716  sa->i_auth.method);
1718  return;
1719  }
1720 
1721  key_pad = format (0, "%s", IKEV2_KEY_PAD);
1722  sel_p = ikev2_select_profile (km, sa, tr_prf, key_pad);
1723 
1724  if (sel_p)
1725  {
1727  sa->udp_encap = sel_p->udp_encap;
1729 
1730  if (!sa->is_initiator)
1731  {
1732  vec_free (sa->r_id.data);
1733  sa->r_id.data = vec_dup (sel_p->loc_id.data);
1734  sa->r_id.type = sel_p->loc_id.type;
1735  sa->i_id.data = vec_dup (sel_p->rem_id.data);
1736  sa->i_id.type = sel_p->rem_id.type;
1737 
1738  /* generate our auth data */
1739  authmsg = ikev2_sa_generate_authmsg (sa, 1);
1740  if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1741  {
1742  vec_free (sa->r_auth.data);
1743  psk = ikev2_calc_prf (tr_prf, sel_p->auth.data, key_pad);
1744  sa->r_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1745  sa->r_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1746  vec_free (psk);
1747  }
1748  else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1749  {
1750  vec_free (sa->r_auth.data);
1751  sa->r_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1752  sa->r_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1753  }
1754  vec_free (authmsg);
1755 
1756  /* select transforms for 1st child sa */
1758  sa->childs[0].r_proposals =
1761 
1762  if (~0 != sel_p->tun_itf)
1763  {
1764  sa->is_tun_itf_set = 1;
1765  sa->tun_itf = sel_p->tun_itf;
1766  }
1767  }
1768  }
1769  else
1770  {
1771  ikev2_elog_uint (IKEV2_LOG_ERROR, "authentication failed, no matching "
1772  "profile found! ispi %lx", sa->ispi);
1774  }
1775  vec_free (key_pad);
1776 }
1777 
1778 static void
1780 {
1781  ikev2_main_t *km = &ikev2_main;
1782  u8 *authmsg, *key_pad, *psk = 0;
1783  ikev2_sa_transform_t *tr_prf;
1784 
1785  tr_prf =
1786  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1787 
1788  /* only shared key and rsa signature */
1789  if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1790  sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1791  {
1792  ikev2_elog_uint (IKEV2_LOG_ERROR,
1793  "unsupported authentication method %u",
1794  sa->i_auth.method);
1796  return;
1797  }
1798 
1799  authmsg = ikev2_sa_generate_authmsg (sa, 0);
1800 
1801  if (sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1802  {
1803  key_pad = format (0, "%s", IKEV2_KEY_PAD);
1804  psk = ikev2_calc_prf (tr_prf, sa->i_auth.data, key_pad);
1805  vec_free (sa->i_auth.data);
1806  sa->i_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1807  sa->i_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1808  vec_free (psk);
1809  vec_free (key_pad);
1810  }
1811  else if (sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1812  {
1813  vec_free (sa->i_auth.data);
1814  sa->i_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1815  sa->i_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1816  }
1817  vec_free (authmsg);
1818 }
1819 
1820 static u32
1822 {
1823  return (0x80000000 | (ti << 24) | (sai << 12) | ci);
1824 }
1825 
1826 static u32
1828 {
1829  return (0xc0000000 | (ti << 24) | (sai << 12) | ci);
1830 }
1831 
1832 typedef struct
1833 {
1842  ipsec_crypto_alg_t encr_type;
1843  ipsec_integ_alg_t integ_type;
1846  ipsec_key_t loc_ckey, rem_ckey, loc_ikey, rem_ikey;
1853 
1854 static void
1856 {
1857  ikev2_main_t *km = &ikev2_main;
1858  u32 sw_if_index;
1859  int rv = 0;
1860  tunnel_t tun_in = {
1862  .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE,
1863  .t_dscp = 0,
1864  .t_mode = TUNNEL_MODE_P2P,
1865  .t_table_id = 0,
1866  .t_hop_limit = 255,
1867  .t_src = a->local_ip,
1868  .t_dst = a->remote_ip,
1869  };
1870  tunnel_t tun_out = {
1872  .t_encap_decap_flags = TUNNEL_ENCAP_DECAP_FLAG_NONE,
1873  .t_dscp = 0,
1874  .t_mode = TUNNEL_MODE_P2P,
1875  .t_table_id = 0,
1876  .t_hop_limit = 255,
1877  .t_src = a->remote_ip,
1878  .t_dst = a->local_ip,
1879  };
1880 
1881  if (~0 == a->sw_if_index)
1882  {
1883  /* no tunnel associated with the SA/profile - create a new one */
1884  rv = ipip_add_tunnel (IPIP_TRANSPORT_IP4, ~0, &ip_addr_46 (&a->local_ip),
1885  &ip_addr_46 (&a->remote_ip), 0,
1886  TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
1887  TUNNEL_MODE_P2P, &sw_if_index);
1888 
1889  if (rv == VNET_API_ERROR_IF_ALREADY_EXISTS)
1890  {
1891  if (hash_get (km->sw_if_indices, sw_if_index))
1892  /* interface is managed by IKE; proceed with updating SAs */
1893  rv = 0;
1894  }
1896  }
1897  else
1898  {
1899  sw_if_index = a->sw_if_index;
1901  }
1902 
1903  if (rv)
1904  {
1905  ikev2_elog_uint (IKEV2_LOG_ERROR,
1906  "installing ipip tunnel failed! local spi: %x",
1907  a->local_spi);
1908  return;
1909  }
1910 
1911  u32 *sas_in = NULL;
1912  vec_add1 (sas_in, a->remote_sa_id);
1913  if (a->is_rekey)
1914  {
1916 
1917  /* replace local SA immediately */
1918  ipsec_sa_unlock_id (a->local_sa_id);
1919 
1920  /* keep the old sa */
1921  vec_add1 (sas_in, a->old_remote_sa_id);
1922  }
1923 
1924  rv = ipsec_sa_add_and_lock (a->local_sa_id, a->local_spi, IPSEC_PROTOCOL_ESP,
1925  a->encr_type, &a->loc_ckey, a->integ_type,
1926  &a->loc_ikey, a->flags, a->salt_local,
1927  a->src_port, a->dst_port, &tun_out, NULL);
1928  if (rv)
1929  goto err0;
1930 
1932  a->remote_sa_id, a->remote_spi, IPSEC_PROTOCOL_ESP, a->encr_type,
1933  &a->rem_ckey, a->integ_type, &a->rem_ikey,
1934  (a->flags | IPSEC_SA_FLAG_IS_INBOUND), a->salt_remote,
1935  a->ipsec_over_udp_port, a->ipsec_over_udp_port, &tun_in, NULL);
1936  if (rv)
1937  goto err1;
1938 
1939  rv = ipsec_tun_protect_update (sw_if_index, NULL, a->local_sa_id, sas_in);
1940  if (rv)
1941  goto err2;
1942 
1943  return;
1944 
1945 err2:
1946  ipsec_sa_unlock_id (a->remote_sa_id);
1947 err1:
1948  ipsec_sa_unlock_id (a->local_sa_id);
1949 err0:
1950  vec_free (sas_in);
1951 }
1952 
1953 static int
1955  ikev2_sa_t * sa,
1956  ikev2_child_sa_t * child, u32 sa_index,
1957  u32 child_index, u8 is_rekey)
1958 {
1960  ikev2_main_t *km = &ikev2_main;
1961  ipsec_crypto_alg_t encr_type;
1962  ipsec_integ_alg_t integ_type;
1963  ikev2_profile_t *p = 0;
1965  ikev2_sa_proposal_t *proposals;
1966  u8 is_aead = 0;
1968 
1969  clib_memset (&a, 0, sizeof (a));
1970 
1971  if (!child->r_proposals)
1972  {
1974  return 1;
1975  }
1976 
1977  if (sa->is_initiator)
1978  {
1979  ip_address_copy (&a.local_ip, &sa->iaddr);
1980  ip_address_copy (&a.remote_ip, &sa->raddr);
1981  proposals = child->r_proposals;
1982  a.local_spi = child->r_proposals[0].spi;
1983  a.remote_spi = child->i_proposals[0].spi;
1984  }
1985  else
1986  {
1987  ip_address_copy (&a.local_ip, &sa->raddr);
1988  ip_address_copy (&a.remote_ip, &sa->iaddr);
1989  proposals = child->i_proposals;
1990  a.local_spi = child->i_proposals[0].spi;
1991  a.remote_spi = child->r_proposals[0].spi;
1992  }
1993 
1994  a.flags = IPSEC_SA_FLAG_USE_ANTI_REPLAY;
1995  if (sa->udp_encap)
1996  {
1997  a.flags |= IPSEC_SA_FLAG_IS_TUNNEL;
1998  a.flags |= IPSEC_SA_FLAG_UDP_ENCAP;
1999  }
2000  if (ikev2_natt_active (sa))
2001  a.flags |= IPSEC_SA_FLAG_UDP_ENCAP;
2002  a.is_rekey = is_rekey;
2003 
2004  tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ESN);
2005  if (tr && tr->esn_type)
2006  a.flags |= IPSEC_SA_FLAG_USE_ESN;
2007 
2008  tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ENCR);
2009  if (tr)
2010  {
2011  if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
2012  {
2013  switch (tr->key_len)
2014  {
2015  case 16:
2016  encr_type = IPSEC_CRYPTO_ALG_AES_CBC_128;
2017  break;
2018  case 24:
2019  encr_type = IPSEC_CRYPTO_ALG_AES_CBC_192;
2020  break;
2021  case 32:
2022  encr_type = IPSEC_CRYPTO_ALG_AES_CBC_256;
2023  break;
2024  default:
2026  return 1;
2027  break;
2028  }
2029  }
2030  else if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16
2031  && tr->key_len)
2032  {
2033  switch (tr->key_len)
2034  {
2035  case 16:
2036  encr_type = IPSEC_CRYPTO_ALG_AES_GCM_128;
2037  break;
2038  case 24:
2039  encr_type = IPSEC_CRYPTO_ALG_AES_GCM_192;
2040  break;
2041  case 32:
2042  encr_type = IPSEC_CRYPTO_ALG_AES_GCM_256;
2043  break;
2044  default:
2046  return 1;
2047  break;
2048  }
2049  is_aead = 1;
2050  }
2051  else
2052  {
2054  return 1;
2055  }
2056  }
2057  else
2058  {
2060  return 1;
2061  }
2062  a.encr_type = encr_type;
2063 
2064  if (!is_aead)
2065  {
2066  tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_INTEG);
2067  if (tr)
2068  {
2069  switch (tr->integ_type)
2070  {
2071  case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_256_128:
2072  integ_type = IPSEC_INTEG_ALG_SHA_256_128;
2073  break;
2074  case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_384_192:
2075  integ_type = IPSEC_INTEG_ALG_SHA_384_192;
2076  break;
2077  case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA2_512_256:
2078  integ_type = IPSEC_INTEG_ALG_SHA_512_256;
2079  break;
2080  case IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96:
2081  integ_type = IPSEC_INTEG_ALG_SHA1_96;
2082  break;
2083  default:
2085  return 1;
2086  }
2087  }
2088  else
2089  {
2091  return 1;
2092  }
2093  }
2094  else
2095  {
2096  integ_type = IPSEC_INTEG_ALG_NONE;
2097  }
2098 
2099  a.integ_type = integ_type;
2100  ikev2_calc_child_keys (sa, child);
2101 
2102  if (sa->is_initiator)
2103  {
2104  ipsec_mk_key (&a.loc_ikey, child->sk_ai, vec_len (child->sk_ai));
2105  ipsec_mk_key (&a.rem_ikey, child->sk_ar, vec_len (child->sk_ar));
2106  ipsec_mk_key (&a.loc_ckey, child->sk_ei, vec_len (child->sk_ei));
2107  ipsec_mk_key (&a.rem_ckey, child->sk_er, vec_len (child->sk_er));
2108  if (is_aead)
2109  {
2110  a.salt_remote = child->salt_er;
2111  a.salt_local = child->salt_ei;
2112  }
2113  a.dst_port = a.src_port = sa->ipsec_over_udp_port;
2114  }
2115  else
2116  {
2117  ipsec_mk_key (&a.loc_ikey, child->sk_ar, vec_len (child->sk_ar));
2118  ipsec_mk_key (&a.rem_ikey, child->sk_ai, vec_len (child->sk_ai));
2119  ipsec_mk_key (&a.loc_ckey, child->sk_er, vec_len (child->sk_er));
2120  ipsec_mk_key (&a.rem_ckey, child->sk_ei, vec_len (child->sk_ei));
2121  if (is_aead)
2122  {
2123  a.salt_remote = child->salt_ei;
2124  a.salt_local = child->salt_er;
2125  }
2126  a.dst_port =
2128  a.src_port = sa->ipsec_over_udp_port;
2129  }
2130 
2131  if (sa->is_initiator && sa->profile_index != ~0)
2132  p = pool_elt_at_index (km->profiles, sa->profile_index);
2133 
2134  if (p && p->lifetime)
2135  {
2136  child->time_to_expiration = vlib_time_now (vm) + p->lifetime;
2137  if (p->lifetime_jitter)
2138  {
2139  // This is not much better than rand(3), which Coverity warns
2140  // is unsuitable for security applications; random_u32 is
2141  // however fast. If this perturbance to the expiration time
2142  // needs to use a better RNG then we may need to use something
2143  // like /dev/urandom which has significant overhead.
2144  u32 rnd = (u32) (vlib_time_now (vm) * 1e6);
2145  rnd = random_u32 (&rnd);
2146 
2147  child->time_to_expiration += 1 + (rnd % p->lifetime_jitter);
2148  }
2149  }
2150 
2151  if (thread_index & 0xffffffc0)
2152  ikev2_elog_error ("error: thread index exceeds max range 0x3f!");
2153 
2154  if (child_index & 0xfffff000 || sa_index & 0xfffff000)
2155  ikev2_elog_error ("error: sa/child index exceeds max range 0xfff!");
2156 
2157  child->local_sa_id =
2158  a.local_sa_id =
2159  ikev2_mk_local_sa_id (sa_index, child_index, thread_index);
2160 
2161  u32 remote_sa_id = ikev2_mk_remote_sa_id (sa_index, child_index,
2162  thread_index);
2163 
2164  if (is_rekey)
2165  {
2166  /* create a new remote SA ID to keep the old SA for a bit longer
2167  * so the peer has some time to swap their SAs */
2168 
2169  /* use most significat bit of child index part in id */
2170  u32 mask = 0x800;
2171  if (sa->current_remote_id_mask)
2172  {
2173  sa->old_remote_id = a.old_remote_sa_id = remote_sa_id | mask;
2174  sa->current_remote_id_mask = 0;
2175  }
2176  else
2177  {
2178  sa->old_remote_id = a.old_remote_sa_id = remote_sa_id;
2180  remote_sa_id |= mask;
2181  }
2182  sa->old_id_expiration = 3.0;
2183  sa->old_remote_id_present = 1;
2184  }
2185 
2186  child->remote_sa_id = a.remote_sa_id = remote_sa_id;
2187 
2188  a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
2189  a.ipsec_over_udp_port = sa->ipsec_over_udp_port;
2190 
2192  (u8 *) & a, sizeof (a));
2193  return 0;
2194 }
2195 
2196 typedef struct
2197 {
2198  ip46_address_t local_ip;
2199  ip46_address_t remote_ip;
2204 
2207 {
2208  u32 mask = 0x800;
2209  if (mask & id)
2210  return id & ~mask;
2211  return id | mask;
2212 }
2213 
2214 static void
2216 {
2217  ikev2_main_t *km = &ikev2_main;
2218  ipip_tunnel_t *ipip = NULL;
2219  u32 sw_if_index;
2220 
2221  if (~0 == a->sw_if_index)
2222  {
2223  /* *INDENT-OFF* */
2224  ipip_tunnel_key_t key = {
2225  .src = a->local_ip,
2226  .dst = a->remote_ip,
2227  .transport = IPIP_TRANSPORT_IP4,
2228  .fib_index = 0,
2229  };
2230  /* *INDENT-ON* */
2231 
2232  ipip = ipip_tunnel_db_find (&key);
2233 
2234  if (ipip)
2235  {
2236  sw_if_index = ipip->sw_if_index;
2237  hash_unset (km->sw_if_indices, ipip->sw_if_index);
2238  }
2239  else
2240  sw_if_index = ~0;
2241  }
2242  else
2243  {
2244  sw_if_index = a->sw_if_index;
2246  }
2247 
2248  if (~0 != sw_if_index)
2250 
2251  ipsec_sa_unlock_id (a->remote_sa_id);
2252  ipsec_sa_unlock_id (a->local_sa_id);
2254 
2255  if (ipip)
2256  ipip_del_tunnel (ipip->sw_if_index);
2257 }
2258 
2259 static int
2261  ikev2_child_sa_t * child)
2262 {
2264 
2265  clib_memset (&a, 0, sizeof (a));
2266 
2267  if (sa->is_initiator)
2268  {
2269  ip_address_to_46 (&sa->iaddr, &a.local_ip);
2270  ip_address_to_46 (&sa->raddr, &a.remote_ip);
2271  }
2272  else
2273  {
2274  ip_address_to_46 (&sa->raddr, &a.local_ip);
2275  ip_address_to_46 (&sa->iaddr, &a.remote_ip);
2276  }
2277 
2278  a.remote_sa_id = child->remote_sa_id;
2279  a.local_sa_id = child->local_sa_id;
2280  a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0);
2281 
2283  sizeof (a));
2284  return 0;
2285 }
2286 
2287 static u32
2289  void *user, udp_header_t *udp, ikev2_stats_t *stats)
2290 {
2291  ikev2_main_t *km = &ikev2_main;
2292  u16 buffer_data_size = vlib_buffer_get_default_data_size (km->vlib_main);
2293  v8 *integ = 0;
2294  ike_payload_header_t *ph;
2295  u16 plen;
2296  u32 tlen = 0;
2297 
2298  ikev2_sa_transform_t *tr_encr, *tr_integ;
2299  tr_encr =
2300  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
2301  tr_integ =
2302  ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
2303 
2304  ikev2_payload_chain_t *chain = 0;
2305  ikev2_payload_new_chain (chain);
2306 
2307  if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
2308  {
2309  if (sa->r_proposals == 0)
2310  {
2311  ikev2_payload_add_notify (chain,
2312  IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
2314  }
2315  else if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
2316  {
2317  u8 *data = vec_new (u8, 2);
2318  ikev2_sa_transform_t *tr_dh;
2319  tr_dh =
2321  IKEV2_TRANSFORM_TYPE_DH);
2322  ASSERT (tr_dh && tr_dh->dh_type);
2323 
2324  data[0] = (tr_dh->dh_type >> 8) & 0xff;
2325  data[1] = (tr_dh->dh_type) & 0xff;
2326 
2327  ikev2_payload_add_notify (chain,
2328  IKEV2_NOTIFY_MSG_INVALID_KE_PAYLOAD,
2329  data);
2330  vec_free (data);
2332  }
2333  else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2334  {
2335  u8 *data = vec_new (u8, 1);
2336 
2337  data[0] = sa->unsupported_cp;
2338  ikev2_payload_add_notify (chain,
2339  IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2340  data);
2341  vec_free (data);
2342  }
2343  else
2344  {
2345  ASSERT (udp);
2346 
2347  ike->rspi = clib_host_to_net_u64 (sa->rspi);
2348  ikev2_payload_add_sa (chain, sa->r_proposals);
2349  ikev2_payload_add_ke (chain, sa->dh_group, sa->r_dh_data);
2350  ikev2_payload_add_nonce (chain, sa->r_nonce);
2351 
2352  u8 *nat_detection_sha1 =
2353  ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa->ispi),
2354  clib_host_to_net_u64 (sa->rspi),
2355  &sa->raddr, udp->dst_port);
2356  ikev2_payload_add_notify (chain,
2357  IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
2358  nat_detection_sha1);
2359  vec_free (nat_detection_sha1);
2360  nat_detection_sha1 =
2361  ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa->ispi),
2362  clib_host_to_net_u64 (sa->rspi),
2363  &sa->iaddr, udp->src_port);
2364  ikev2_payload_add_notify (chain,
2365  IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
2366  nat_detection_sha1);
2367  vec_free (nat_detection_sha1);
2368  }
2369  }
2370  else if (ike->exchange == IKEV2_EXCHANGE_IKE_AUTH)
2371  {
2372  if (sa->state == IKEV2_STATE_AUTHENTICATED)
2373  {
2376  ikev2_payload_add_auth (chain, &sa->r_auth);
2377  ikev2_payload_add_sa (chain, sa->childs[0].r_proposals);
2380  }
2381  else if (sa->state == IKEV2_STATE_AUTH_FAILED)
2382  {
2383  ikev2_payload_add_notify (chain,
2384  IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED,
2385  0);
2387  }
2388  else if (sa->state == IKEV2_STATE_TS_UNACCEPTABLE)
2389  {
2390  ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_TS_UNACCEPTABLE,
2391  0);
2393  ikev2_payload_add_auth (chain, &sa->r_auth);
2394  }
2395  else if (sa->state == IKEV2_STATE_NO_PROPOSAL_CHOSEN)
2396  {
2397  ikev2_payload_add_notify (chain,
2398  IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
2400  ikev2_payload_add_auth (chain, &sa->r_auth);
2403  }
2404  else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2405  {
2406  u8 *data = vec_new (u8, 1);
2407 
2408  data[0] = sa->unsupported_cp;
2409  ikev2_payload_add_notify (chain,
2410  IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2411  data);
2412  vec_free (data);
2413  }
2414  else if (sa->state == IKEV2_STATE_SA_INIT)
2415  {
2418  ikev2_payload_add_auth (chain, &sa->i_auth);
2419  ikev2_payload_add_sa (chain, sa->childs[0].i_proposals);
2422  ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_INITIAL_CONTACT,
2423  0);
2424  }
2425  else
2426  {
2427  ikev2_set_state (sa, IKEV2_STATE_DELETED, ": unexpected IKE_AUTH");
2428  goto done;
2429  }
2430  }
2431  else if (ike->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2432  {
2433  /* if pending delete */
2434  if (sa->del)
2435  {
2436  if (sa->del[0].protocol_id == IKEV2_PROTOCOL_IKE)
2437  {
2438  if (ike_hdr_is_request (ike))
2439  ikev2_payload_add_delete (chain, sa->del);
2440 
2441  /* The response to a request that deletes the IKE SA is an empty
2442  INFORMATIONAL response. */
2444  }
2445  /* The response to a request that deletes ESP or AH SAs will contain
2446  delete payloads for the paired SAs going in the other direction. */
2447  else
2448  {
2449  ikev2_payload_add_delete (chain, sa->del);
2450  }
2451  vec_free (sa->del);
2452  sa->del = 0;
2453  }
2454  /* received N(AUTHENTICATION_FAILED) */
2455  else if (sa->state == IKEV2_STATE_AUTH_FAILED)
2456  {
2457  ikev2_set_state (sa, IKEV2_STATE_DELETED, ": auth failed");
2458  goto done;
2459  }
2460  /* received unsupported critical payload */
2461  else if (sa->unsupported_cp)
2462  {
2463  u8 *data = vec_new (u8, 1);
2464 
2465  data[0] = sa->unsupported_cp;
2466  ikev2_payload_add_notify (chain,
2467  IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
2468  data);
2469  vec_free (data);
2470  sa->unsupported_cp = 0;
2471  }
2472  else
2473  /* else send empty response */
2474  {
2475  if (ike_hdr_is_response (ike))
2476  {
2477  ASSERT (stats != 0);
2478  stats->n_keepalives++;
2479  sa->stats.n_keepalives++;
2480  }
2481  }
2482  }
2483  else if (ike->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2484  {
2485  if (sa->is_initiator)
2486  {
2487 
2488  ikev2_sa_proposal_t *proposals = (ikev2_sa_proposal_t *) user;
2489  ikev2_notify_t notify;
2490  u8 *data = vec_new (u8, 4);
2491  clib_memset (&notify, 0, sizeof (notify));
2493  notify.spi = sa->childs[0].i_proposals->spi;
2494  *(u32 *) data = clib_host_to_net_u32 (notify.spi);
2495 
2496  ikev2_payload_add_sa (chain, proposals);
2497  ikev2_payload_add_nonce (chain, sa->i_nonce);
2500  ikev2_payload_add_notify_2 (chain, IKEV2_NOTIFY_MSG_REKEY_SA, data,
2501  &notify);
2502 
2503  vec_free (data);
2504  }
2505  else if (vec_len (sa->rekey) > 0)
2506  {
2507  ikev2_payload_add_sa (chain, sa->rekey[0].r_proposal);
2508  ikev2_payload_add_nonce (chain, sa->r_nonce);
2511  vec_del1 (sa->rekey, 0);
2512  }
2513  else if (vec_len (sa->new_child) > 0)
2514  {
2515  ikev2_payload_add_sa (chain, sa->new_child[0].r_proposal);
2516  ikev2_payload_add_nonce (chain, sa->r_nonce);
2517  ikev2_payload_add_ts (chain, sa->new_child[0].tsi,
2519  ikev2_payload_add_ts (chain, sa->new_child[0].tsr,
2521  vec_del1 (sa->new_child, 0);
2522  }
2523  else if (sa->unsupported_cp)
2524  {
2525  u8 *data = vec_new (u8, 1);
2526 
2527  data[0] = sa->unsupported_cp;
2529  chain, IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD, data);
2530  vec_free (data);
2531  sa->unsupported_cp = 0;
2532  }
2533  else
2534  {
2535  ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS,
2536  0);
2537  }
2538  }
2539 
2540  /* IKEv2 header */
2541  ike->version = IKE_VERSION_2;
2542  ike->nextpayload = IKEV2_PAYLOAD_SK;
2543  tlen = sizeof (*ike);
2544 
2545  if (sa->is_initiator)
2546  ike->flags |= IKEV2_HDR_FLAG_INITIATOR;
2547 
2548  if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
2549  {
2550  tlen += vec_len (chain->data);
2551  ike->nextpayload = chain->first_payload_type;
2552  ike->length = clib_host_to_net_u32 (tlen);
2553 
2554  if (tlen + b->current_length + b->current_data > buffer_data_size)
2555  {
2556  tlen = ~0;
2557  goto done;
2558  }
2559 
2560  clib_memcpy_fast (ike->payload, chain->data, vec_len (chain->data));
2561 
2562  /* store whole IKE payload - needed for PSK auth */
2565  }
2566  else
2567  {
2569  ikev2_payload_chain_add_padding (chain, tr_encr->block_size);
2570 
2571  /* SK payload */
2572  plen = sizeof (*ph);
2573  ph = (ike_payload_header_t *) & ike->payload[0];
2574  ph->nextpayload = chain->first_payload_type;
2575  ph->flags = 0;
2576  int is_aead =
2577  tr_encr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_GCM_16;
2578  int iv_len = is_aead ? IKEV2_GCM_IV_SIZE : tr_encr->block_size;
2579  plen += vec_len (chain->data) + iv_len;
2580 
2581  /* add space for hmac/tag */
2582  if (tr_integ)
2583  plen += tr_integ->key_trunc;
2584  else
2585  plen += IKEV2_GCM_ICV_SIZE;
2586  tlen += plen;
2587 
2588  if (tlen + b->current_length + b->current_data > buffer_data_size)
2589  {
2590  tlen = ~0;
2591  goto done;
2592  }
2593 
2594  /* payload and total length */
2595  ph->length = clib_host_to_net_u16 (plen);
2596  ike->length = clib_host_to_net_u32 (tlen);
2597 
2598  if (is_aead)
2599  {
2600  if (!ikev2_encrypt_aead_data (ptd, sa, tr_encr, chain->data,
2601  ph->payload, (u8 *) ike,
2602  sizeof (*ike) + sizeof (*ph),
2603  ph->payload + plen - sizeof (*ph) -
2605  {
2606  tlen = ~0;
2607  goto done;
2608  }
2609  }
2610  else
2611  {
2612  if (!ikev2_encrypt_data
2613  (ptd, sa, tr_encr, chain->data, ph->payload))
2614  {
2615  tlen = ~0;
2616  goto done;
2617  }
2618  integ =
2619  ikev2_calc_integr (tr_integ,
2620  sa->is_initiator ? sa->sk_ai : sa->sk_ar,
2621  (u8 *) ike, tlen - tr_integ->key_trunc);
2622  clib_memcpy_fast (ike->payload + tlen - tr_integ->key_trunc -
2623  sizeof (*ike), integ, tr_integ->key_trunc);
2624  }
2625 
2626  /* store whole IKE payload - needed for retransmit */
2628  vec_add (sa->last_res_packet_data, ike, tlen);
2629  }
2630 
2631 done:
2633  vec_free (integ);
2634  return tlen;
2635 }
2636 
2637 static u32
2640  u32 rlen)
2641 {
2642  int p = 0;
2643  ike_header_t *tmp;
2644  u8 payload = ike->nextpayload;
2645 
2646  if (sa->ispi != clib_net_to_host_u64 (ike->ispi) ||
2647  ip_address_cmp (&sa->iaddr, &iaddr) ||
2648  ip_address_cmp (&sa->raddr, &raddr))
2649  {
2650  return 0;
2651  }
2652 
2653  while (p < rlen && payload != IKEV2_PAYLOAD_NONE)
2654  {
2655  ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
2656  u32 plen = clib_net_to_host_u16 (ikep->length);
2657 
2658  if (plen < sizeof (ike_payload_header_t))
2659  return ~0;
2660 
2661  if (payload == IKEV2_PAYLOAD_NONCE &&
2662  !clib_memcmp (sa->i_nonce, ikep->payload, plen - sizeof (*ikep)))
2663  {
2664  /* req is retransmit */
2665  if (sa->state == IKEV2_STATE_SA_INIT)
2666  {
2667  sa->stats.n_init_retransmit++;
2668  tmp = (ike_header_t *) sa->last_sa_init_res_packet_data;
2669  u32 slen = clib_net_to_host_u32 (tmp->length);
2670  ike->ispi = tmp->ispi;
2671  ike->rspi = tmp->rspi;
2672  ike->nextpayload = tmp->nextpayload;
2673  ike->version = tmp->version;
2674  ike->exchange = tmp->exchange;
2675  ike->flags = tmp->flags;
2676  ike->msgid = tmp->msgid;
2677  ike->length = tmp->length;
2678  clib_memcpy_fast (ike->payload, tmp->payload,
2679  slen - sizeof (*ike));
2680  ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2681  "ispi %lx IKE_SA_INIT retransmit "
2682  "from %d.%d.%d.%d to %d.%d.%d.%d",
2683  ike->ispi,
2684  ip_addr_v4 (&raddr).as_u32,
2685  ip_addr_v4 (&iaddr).as_u32);
2686  return slen;
2687  }
2688  /* else ignore req */
2689  else
2690  {
2691  ikev2_elog_uint_peers (IKEV2_LOG_DEBUG,
2692  "ispi %lx IKE_SA_INIT ignore "
2693  "from %d.%d.%d.%d to %d.%d.%d.%d",
2694  ike->ispi,
2695  ip_addr_v4 (&raddr).as_u32,
2696  ip_addr_v4 (&iaddr).as_u32);
2697  return ~0;
2698  }
2699  }
2700  payload = ikep->nextpayload;
2701  p += plen;
2702  }
2703 
2704  return 0;
2705 }
2706 
2707 static u32
2709  ip_address_t raddr, u32 rlen)
2710 {
2711  ikev2_sa_t *sa;
2712  u32 res;
2714 
2715  /* *INDENT-OFF* */
2716  pool_foreach (sa, ptd->sas) {
2717  res = ikev2_retransmit_sa_init_one (sa, ike, iaddr, raddr, rlen);
2718  if (res)
2719  return res;
2720  }
2721  /* *INDENT-ON* */
2722 
2723  /* req is not retransmit */
2724  return 0;
2725 }
2726 
2727 static u32
2728 ikev2_retransmit_resp (ikev2_sa_t * sa, ike_header_t * ike)
2729 {
2730  if (ike_hdr_is_response (ike))
2731  return 0;
2732 
2733  u32 msg_id = clib_net_to_host_u32 (ike->msgid);
2734 
2735  /* new req */
2736  if (msg_id > sa->last_msg_id || sa->last_msg_id == ~0)
2737  {
2738  sa->last_msg_id = msg_id;
2739  return 0;
2740  }
2741 
2742  /* retransmitted req */
2743  if (msg_id == sa->last_msg_id)
2744  {
2745  sa->stats.n_retransmit++;
2746  ike_header_t *tmp = (ike_header_t *) sa->last_res_packet_data;
2747  u32 slen = clib_net_to_host_u32 (tmp->length);
2748  ike->ispi = tmp->ispi;
2749  ike->rspi = tmp->rspi;
2750  ike->nextpayload = tmp->nextpayload;
2751  ike->version = tmp->version;
2752  ike->exchange = tmp->exchange;
2753  ike->flags = tmp->flags;
2754  ike->msgid = tmp->msgid;
2755  ike->length = tmp->length;
2756  clib_memcpy_fast (ike->payload, tmp->payload, slen - sizeof (*ike));
2757  ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE retransmit msgid %d",
2758  msg_id, ip_addr_v4 (&sa->raddr).as_u32,
2759  ip_addr_v4 (&sa->iaddr).as_u32);
2760  return slen;
2761  }
2762 
2763  /* old req ignore */
2764  ikev2_elog_uint_peers (IKEV2_LOG_DEBUG, "IKE req ignore msgid %d",
2765  msg_id, ip_addr_v4 (&sa->raddr).as_u32,
2766  ip_addr_v4 (&sa->iaddr).as_u32);
2767  return ~0;
2768 }
2769 
2770 static void
2772 {
2773  ikev2_main_t *km = &ikev2_main;
2775  sa->profile_index = ~0;
2776 }
2777 
2778 static void
2780 {
2781  ikev2_main_t *km = &ikev2_main;
2782  uword *p = hash_get (km->sa_by_ispi, *ispi);
2783  if (p)
2784  {
2785  ikev2_sa_t *sai = pool_elt_at_index (km->sais, p[0]);
2786  hash_unset (km->sa_by_ispi, sai->ispi);
2787  ikev2_sa_free_all_vec (sai);
2788  pool_put (km->sais, sai);
2789  }
2790 }
2791 
2792 static void
2794 {
2796  sizeof (ispi));
2797 }
2798 
2801 {
2802  if (sa->is_initiator)
2803  {
2804  ip_address_copy_addr (&ih->dst_address, &sa->raddr);
2805  ip_address_copy_addr (&ih->src_address, &sa->iaddr);
2806  }
2807  else
2808  {
2809  ip_address_copy_addr (&ih->dst_address, &sa->iaddr);
2810  ip_address_copy_addr (&ih->src_address, &sa->raddr);
2811  }
2812 }
2813 
2816 {
2817  if (sa->is_initiator)
2818  {
2819  ip_address_copy_addr (&ih->dst_address, &sa->raddr);
2820  ip_address_copy_addr (&ih->src_address, &sa->iaddr);
2821  }
2822  else
2823  {
2824  ip_address_copy_addr (&ih->dst_address, &sa->iaddr);
2825  ip_address_copy_addr (&ih->src_address, &sa->raddr);
2826  }
2827 }
2828 
2830 ikev2_set_ip_address (ikev2_sa_t *sa, const void *iaddr, const void *raddr,
2831  const ip_address_family_t af)
2832 {
2833  ip_address_set (&sa->raddr, raddr, af);
2834  ip_address_set (&sa->iaddr, iaddr, af);
2835 }
2836 
2837 static void
2839  ip6_header_t * ip6, u8 is_ip4)
2840 {
2841  u32 src, dst;
2842  if (is_ip4)
2843  {
2844  src = ip4->src_address.as_u32;
2845  dst = ip4->dst_address.as_u32;
2846  }
2847  else
2848  {
2849  src = ip6->src_address.as_u32[3];
2850  dst = ip6->dst_address.as_u32[3];
2851  }
2852  ikev2_elog_uint_peers (IKEV2_LOG_WARNING, "IKEv2 exchange %d "
2853  "received from %d.%d.%d.%d to %d.%d.%d.%d",
2854  exchange, src, dst);
2855 }
2856 
2857 static void
2859 {
2861 
2863  return;
2864 
2869 }
2870 
2871 static void
2873 {
2874  vlib_node_increment_counter (vm, node_index, IKEV2_ERROR_KEEPALIVE,
2875  s->n_keepalives);
2876  vlib_node_increment_counter (vm, node_index, IKEV2_ERROR_REKEY_REQ,
2877  s->n_rekey_req);
2878  vlib_node_increment_counter (vm, node_index, IKEV2_ERROR_INIT_SA_REQ,
2879  s->n_sa_init_req);
2880  vlib_node_increment_counter (vm, node_index, IKEV2_ERROR_IKE_AUTH_REQ,
2881  s->n_sa_auth_req);
2882 }
2883 
2886  vlib_frame_t *frame, u8 is_ip4, u8 natt)
2887 {
2888  u32 n_left = frame->n_vectors, *from;
2889  ikev2_main_t *km = &ikev2_main;
2893  ikev2_stats_t _stats, *stats = &_stats;
2894  int res;
2895 
2896  /* no NAT traversal for ip6 */
2897  ASSERT (!natt || is_ip4);
2898 
2899  clib_memset_u16 (stats, 0, sizeof (stats[0]) / sizeof (u16));
2902  b = bufs;
2903 
2904  while (n_left > 0)
2905  {
2906  vlib_buffer_t *b0 = b[0];
2907  next[0] = is_ip4 ? IKEV2_NEXT_IP4_ERROR_DROP
2909  ip4_header_t *ip40 = 0;
2910  ip6_header_t *ip60 = 0;
2911  udp_header_t *udp0;
2912  ike_header_t *ike0;
2913  ikev2_sa_t *sa0 = 0;
2914  ikev2_sa_t sa; /* temporary store for SA */
2915  u32 rlen, slen = 0;
2916  int ip_hdr_sz = 0;
2917  int is_req = 0;
2918 
2919  if (natt)
2920  {
2921  u8 *ptr = vlib_buffer_get_current (b0);
2922  ip40 = (ip4_header_t *) ptr;
2923  ptr += sizeof (*ip40);
2924  udp0 = (udp_header_t *) ptr;
2925  ptr += sizeof (*udp0);
2926  ike0 = (ike_header_t *) ptr;
2927  ip_hdr_sz = sizeof (*ip40);
2928  }
2929  else
2930  {
2931  u8 *ipx_hdr = b0->data + vnet_buffer (b0)->l3_hdr_offset;
2932  ike0 = vlib_buffer_get_current (b0);
2933  vlib_buffer_advance (b0, -sizeof (*udp0));
2934  udp0 = vlib_buffer_get_current (b0);
2935 
2936  if (is_ip4)
2937  {
2938  ip40 = (ip4_header_t *) ipx_hdr;
2939  ip_hdr_sz = sizeof (*ip40);
2940  }
2941  else
2942  {
2943  ip60 = (ip6_header_t *) ipx_hdr;
2944  ip_hdr_sz = sizeof (*ip60);
2945  }
2946  vlib_buffer_advance (b0, -ip_hdr_sz);
2947  }
2948 
2949  rlen = b0->current_length - ip_hdr_sz - sizeof (*udp0);
2950 
2951  /* check for non-esp marker */
2952  if (natt)
2953  {
2954  ASSERT (*((u32 *) ike0) == 0);
2955  ike0 =
2956  (ike_header_t *) ((u8 *) ike0 + sizeof (ikev2_non_esp_marker));
2957  rlen -= sizeof (ikev2_non_esp_marker);
2958  }
2959 
2960  if (clib_net_to_host_u32 (ike0->length) != rlen)
2961  {
2962  vlib_node_increment_counter (vm, node->node_index,
2963  IKEV2_ERROR_BAD_LENGTH, 1);
2964  goto dispatch0;
2965  }
2966 
2967  if (ike0->version != IKE_VERSION_2)
2968  {
2969  vlib_node_increment_counter (vm, node->node_index,
2970  IKEV2_ERROR_NOT_IKEV2, 1);
2971  goto dispatch0;
2972  }
2973 
2974  if (ike0->exchange == IKEV2_EXCHANGE_SA_INIT)
2975  {
2976  sa0 = &sa;
2977  clib_memset (sa0, 0, sizeof (*sa0));
2978 
2979  if (ike_hdr_is_initiator (ike0))
2980  {
2981  sa0->stats.n_sa_init_req++;
2982  stats->n_sa_init_req++;
2983  if (ike0->rspi == 0)
2984  {
2985  if (is_ip4)
2986  ikev2_set_ip_address (sa0, &ip40->src_address,
2987  &ip40->dst_address, AF_IP4);
2988  else
2989  ikev2_set_ip_address (sa0, &ip60->src_address,
2990  &ip60->dst_address, AF_IP6);
2991 
2992  sa0->dst_port = clib_net_to_host_u16 (udp0->src_port);
2993 
2994  slen =
2995  ikev2_retransmit_sa_init (ike0, sa0->iaddr,
2996  sa0->raddr, rlen);
2997  if (slen)
2998  {
2999  vlib_node_increment_counter (vm, node->node_index,
3000  ~0 ==
3001  slen ?
3002  IKEV2_ERROR_IKE_SA_INIT_IGNORE
3003  :
3004  IKEV2_ERROR_IKE_SA_INIT_RETRANSMIT,
3005  1);
3006  goto dispatch0;
3007  }
3008 
3010  vm, sa0, ike0, udp0, rlen,
3012  if (!res)
3013  vlib_node_increment_counter (vm, node->node_index,
3014  IKEV2_ERROR_MALFORMED_PACKET,
3015  1);
3016 
3017  if (sa0->state == IKEV2_STATE_SA_INIT)
3018  {
3020  sa0->r_proposals =
3024  }
3025 
3026  if (sa0->state == IKEV2_STATE_SA_INIT
3028  {
3029  ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3030  slen =
3031  ikev2_generate_message (b0, sa0, ike0, 0, udp0, stats);
3032  if (~0 == slen)
3033  vlib_node_increment_counter (vm, node->node_index,
3034  IKEV2_ERROR_NO_BUFF_SPACE,
3035  1);
3036  }
3037 
3038  if (sa0->state == IKEV2_STATE_SA_INIT)
3039  {
3040  /* add SA to the pool */
3041  pool_get (ptd->sas, sa0);
3042  clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
3043  ikev2_init_sa (vm, sa0);
3044  hash_set (ptd->sa_by_rspi, sa0->rspi, sa0 - ptd->sas);
3045  }
3046  else
3047  {
3048  ikev2_sa_free_all_vec (sa0);
3049  }
3050  }
3051  }
3052  else //received sa_init without initiator flag
3053  {
3054  if (is_ip4)
3055  ikev2_set_ip_address (sa0, &ip40->dst_address,
3056  &ip40->src_address, AF_IP4);
3057  else
3058  ikev2_set_ip_address (sa0, &ip60->dst_address,
3059  &ip60->src_address, AF_IP6);
3060 
3061  ikev2_process_sa_init_resp (vm, sa0, ike0, udp0, rlen);
3062 
3063  if (sa0->state == IKEV2_STATE_SA_INIT)
3064  {
3065  is_req = 1;
3066  ike0->exchange = IKEV2_EXCHANGE_IKE_AUTH;
3067  uword *p = hash_get (km->sa_by_ispi, sa0->ispi);
3068  if (p)
3069  {
3070  ikev2_sa_t *sai = pool_elt_at_index (km->sais, p[0]);
3071 
3073  (&sai->init_response_received, 0, 1))
3074  {
3075  ikev2_complete_sa_data (sa0, sai);
3076  ikev2_calc_keys (sa0);
3077  ikev2_sa_auth_init (sa0);
3078  ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3079  ike0->msgid =
3080  clib_net_to_host_u32 (sai->last_init_msg_id);
3081  sa0->last_init_msg_id = sai->last_init_msg_id + 1;
3082  slen = ikev2_generate_message (b0, sa0, ike0, 0,
3083  udp0, stats);
3084  if (~0 == slen)
3086  node->node_index,
3087  IKEV2_ERROR_NO_BUFF_SPACE,
3088  1);
3089  }
3090  else
3091  {
3092  /* we've already processed sa-init response */
3093  sa0->state = IKEV2_STATE_UNKNOWN;
3094  }
3095  }
3096  }
3097 
3098  if (sa0->state == IKEV2_STATE_SA_INIT)
3099  {
3100  /* add SA to the pool */
3101  pool_get (ptd->sas, sa0);
3102  clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
3103  hash_set (ptd->sa_by_rspi, sa0->rspi, sa0 - ptd->sas);
3104  }
3105  else
3106  {
3107  ikev2_sa_free_all_vec (sa0);
3108  }
3109  }
3110  }
3111  else if (ike0->exchange == IKEV2_EXCHANGE_IKE_AUTH)
3112  {
3113  uword *p;
3114  p = hash_get (ptd->sa_by_rspi, clib_net_to_host_u64 (ike0->rspi));
3115  if (p)
3116  {
3117  sa0 = pool_elt_at_index (ptd->sas, p[0]);
3118  slen = ikev2_retransmit_resp (sa0, ike0);
3119  if (slen)
3120  {
3121  vlib_node_increment_counter (vm, node->node_index,
3122  ~0 ==
3123  slen ?
3124  IKEV2_ERROR_IKE_REQ_IGNORE
3125  :
3126  IKEV2_ERROR_IKE_REQ_RETRANSMIT,
3127  1);
3128  goto dispatch0;
3129  }
3130 
3131  sa0->dst_port = clib_net_to_host_u16 (udp0->src_port);
3132  res = ikev2_process_auth_req (vm, sa0, ike0, rlen);
3133  if (res)
3134  ikev2_sa_auth (sa0);
3135  else
3136  vlib_node_increment_counter (vm, node->node_index,
3137  IKEV2_ERROR_MALFORMED_PACKET, 1);
3138  if (sa0->state == IKEV2_STATE_AUTHENTICATED)
3139  {
3140  ikev2_initial_contact_cleanup (ptd, sa0);
3141  ikev2_sa_match_ts (sa0);
3142  if (sa0->state != IKEV2_STATE_TS_UNACCEPTABLE)
3144  &sa0->childs[0],
3145  p[0], 0, 0);
3146  }
3147 
3148  if (sa0->is_initiator)
3149  {
3150  sa0->last_msg_id = ~0;
3151  ikev2_del_sa_init (sa0->ispi);
3152  }
3153  else
3154  {
3155  sa0->stats.n_sa_auth_req++;
3156  stats->n_sa_auth_req++;
3157  ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3158  slen =
3159  ikev2_generate_message (b0, sa0, ike0, 0, udp0, stats);
3160  if (~0 == slen)
3161  vlib_node_increment_counter (vm, node->node_index,
3162  IKEV2_ERROR_NO_BUFF_SPACE,
3163  1);
3164  }
3165  }
3166  }
3167  else if (ike0->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
3168  {
3169  uword *p;
3170  p = hash_get (ptd->sa_by_rspi, clib_net_to_host_u64 (ike0->rspi));
3171  if (p)
3172  {
3173  sa0 = pool_elt_at_index (ptd->sas, p[0]);
3174  slen = ikev2_retransmit_resp (sa0, ike0);
3175  if (slen)
3176  {
3177  vlib_node_increment_counter (vm, node->node_index,
3178  ~0 ==
3179  slen ?
3180  IKEV2_ERROR_IKE_REQ_IGNORE
3181  :
3182  IKEV2_ERROR_IKE_REQ_RETRANSMIT,
3183  1);
3184  goto dispatch0;
3185  }
3186 
3187  res = ikev2_process_informational_req (vm, sa0, ike0, rlen);
3188  if (!res)
3189  {
3190  vlib_node_increment_counter (vm, node->node_index,
3191  IKEV2_ERROR_MALFORMED_PACKET,
3192  1);
3193  slen = ~0;
3194  goto dispatch0;
3195  }
3196 
3197  if (sa0->del)
3198  {
3199  if (sa0->del[0].protocol_id != IKEV2_PROTOCOL_IKE)
3200  {
3201  ikev2_delete_t *d, *tmp, *resp = 0;
3202  vec_foreach (d, sa0->del)
3203  {
3204  ikev2_child_sa_t *ch_sa;
3205  ch_sa = ikev2_sa_get_child (sa0, d->spi,
3206  d->protocol_id,
3207  !sa0->is_initiator);
3208  if (ch_sa)
3209  {
3211  sa0, ch_sa);
3212  if (!sa0->is_initiator)
3213  {
3214  vec_add2 (resp, tmp, 1);
3215  tmp->protocol_id = d->protocol_id;
3216  tmp->spi = ch_sa->r_proposals[0].spi;
3217  }
3218  ikev2_sa_del_child_sa (sa0, ch_sa);
3219  }
3220  }
3221  if (!sa0->is_initiator)
3222  {
3223  vec_free (sa0->del);
3224  sa0->del = resp;
3225  }
3226  }
3227  }
3228  if (ike_hdr_is_request (ike0))
3229  {
3230  ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3231  slen =
3232  ikev2_generate_message (b0, sa0, ike0, 0, udp0, stats);
3233  if (~0 == slen)
3234  vlib_node_increment_counter (vm, node->node_index,
3235  IKEV2_ERROR_NO_BUFF_SPACE,
3236  1);
3237  }
3238  }
3239  }
3240  else if (ike0->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
3241  {
3242  uword *p;
3243  p = hash_get (ptd->sa_by_rspi, clib_net_to_host_u64 (ike0->rspi));
3244  if (p)
3245  {
3246  sa0 = pool_elt_at_index (ptd->sas, p[0]);
3247  slen = ikev2_retransmit_resp (sa0, ike0);
3248  if (slen)
3249  {
3250  vlib_node_increment_counter (vm, node->node_index,
3251  ~0 ==
3252  slen ?
3253  IKEV2_ERROR_IKE_REQ_IGNORE
3254  :
3255  IKEV2_ERROR_IKE_REQ_RETRANSMIT,
3256  1);
3257  goto dispatch0;
3258  }
3259 
3260  res = ikev2_process_create_child_sa_req (vm, sa0, ike0, rlen);
3261  if (!res)
3262  {
3263  vlib_node_increment_counter (vm, node->node_index,
3264  IKEV2_ERROR_MALFORMED_PACKET,
3265  1);
3266  slen = ~0;
3267  goto dispatch0;
3268  }
3269 
3270  if (sa0->rekey)
3271  {
3272  if (sa0->rekey[0].protocol_id != IKEV2_PROTOCOL_IKE)
3273  {
3274  if (sa0->childs)
3276  ikev2_child_sa_t *child;
3277  vec_add2 (sa0->childs, child, 1);
3278  clib_memset (child, 0, sizeof (*child));
3279  child->r_proposals = sa0->rekey[0].r_proposal;
3280  child->i_proposals = sa0->rekey[0].i_proposal;
3281  child->tsi = sa0->rekey[0].tsi;
3282  child->tsr = sa0->rekey[0].tsr;
3283  ikev2_create_tunnel_interface (vm, sa0, child, p[0],
3284  child - sa0->childs, 1);
3285  }
3286  if (ike_hdr_is_response (ike0))
3287  {
3288  vec_free (sa0->rekey);
3289  }
3290  else
3291  {
3292  stats->n_rekey_req++;
3293  sa0->stats.n_rekey_req++;
3294  ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3295  slen =
3296  ikev2_generate_message (b0, sa0, ike0, 0, udp0, stats);
3297  if (~0 == slen)
3298  vlib_node_increment_counter (vm, node->node_index,
3299  IKEV2_ERROR_NO_BUFF_SPACE,
3300  1);
3301  }
3302  }
3303  else if (sa0->new_child)
3304  {
3306  vec_add2 (sa0->childs, c, 1);
3307  memset (c, 0, sizeof (*c));
3308  c->r_proposals = sa0->new_child[0].r_proposal;
3309  c->i_proposals = sa0->new_child[0].i_proposal;
3310  c->tsi = sa0->new_child[0].tsi;
3311  c->tsr = sa0->new_child[0].tsr;
3312  ikev2_create_tunnel_interface (vm, sa0, c, p[0],
3313  c - sa0->childs, 0);
3314  if (ike_hdr_is_request (ike0))
3315  {
3316  ike0->flags = IKEV2_HDR_FLAG_RESPONSE;
3317  slen =
3318  ikev2_generate_message (b0, sa0, ike0, 0, udp0, 0);
3319  if (~0 == slen)
3321  vm, node->node_index, IKEV2_ERROR_NO_BUFF_SPACE, 1);
3322  }
3323  }
3324  }
3325  }
3326  else
3327  {
3328  ikev2_elog_uint_peers_addr (ike0->exchange, ip40, ip60, is_ip4);
3329  }
3330 
3331  dispatch0:
3332  /* if we are sending packet back, rewrite headers */
3333  if (slen && ~0 != slen)
3334  {
3335  if (is_ip4)
3336  {
3338  ikev2_rewrite_v4_addrs (sa0, ip40);
3339  }
3340  else
3341  {
3343  ikev2_rewrite_v6_addrs (sa0, ip60);
3344  }
3345 
3346  if (is_req)
3347  {
3348  udp0->dst_port = udp0->src_port =
3349  clib_net_to_host_u16 (ikev2_get_port (sa0));
3350 
3351  if (udp0->dst_port == clib_net_to_host_u16 (IKEV2_PORT_NATT)
3352  && ikev2_natt_active (sa0))
3353  {
3354  if (!natt)
3355  slen = ikev2_insert_non_esp_marker (ike0, slen);
3356  }
3357  }
3358  else
3359  {
3360  if (natt)
3361  slen += sizeof (ikev2_non_esp_marker);
3362 
3363  u16 tp = udp0->dst_port;
3364  udp0->dst_port = udp0->src_port;
3365  udp0->src_port = tp;
3366  }
3367 
3368  udp0->length = clib_host_to_net_u16 (slen + sizeof (udp_header_t));
3369  udp0->checksum = 0;
3370  b0->current_length = slen + ip_hdr_sz + sizeof (udp_header_t);
3371  if (is_ip4)
3372  {
3373  ip40->length = clib_host_to_net_u16 (b0->current_length);
3374  ip40->checksum = ip4_header_checksum (ip40);
3375  }
3376  else
3377  {
3378  ip60->payload_length =
3379  clib_host_to_net_u16 (b0->current_length - sizeof (*ip60));
3380  }
3381  }
3382  /* delete sa */
3383  if (sa0 && (sa0->state == IKEV2_STATE_DELETED ||
3385  {
3387 
3388  vec_foreach (c, sa0->childs)
3390 
3391  ikev2_delete_sa (ptd, sa0);
3392  }
3393  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
3394  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
3395  {
3396 
3397  ikev2_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
3398  t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3399  t->next_index = next[0];
3400  }
3401  n_left -= 1;
3402  next += 1;
3403  b += 1;
3404  }
3405 
3406  ikev2_update_stats (vm, node->node_index, stats);
3407  vlib_node_increment_counter (vm, node->node_index,
3408  IKEV2_ERROR_PROCESSED, frame->n_vectors);
3410  return frame->n_vectors;
3411 }
3412 
3413 static uword
3415 {
3416  return ikev2_node_internal (vm, node, frame, 1 /* is_ip4 */, 0);
3417 }
3418 
3419 static uword
3422 {
3423  return ikev2_node_internal (vm, node, frame, 1 /* is_ip4 */, 1 /* natt */);
3424 }
3425 
3426 static uword
3428 {
3429  return ikev2_node_internal (vm, node, frame, 0 /* is_ip4 */, 0);
3430 }
3431 
3432 /* *INDENT-OFF* */
3434  .function = ikev2_ip4,
3435  .name = "ikev2-ip4",
3436  .vector_size = sizeof (u32),
3437  .format_trace = format_ikev2_trace,
3439 
3440  .n_errors = IKEV2_N_ERROR,
3441  .error_counters = ikev2_error_counters,
3442 
3443  .n_next_nodes = IKEV2_IP4_N_NEXT,
3444  .next_nodes = {
3445  [IKEV2_NEXT_IP4_LOOKUP] = "ip4-lookup",
3446  [IKEV2_NEXT_IP4_ERROR_DROP] = "error-drop",
3447  },
3448 };
3449 
3451  .function = ikev2_ip4_natt,
3452  .name = "ikev2-ip4-natt",
3453  .vector_size = sizeof (u32),
3454  .format_trace = format_ikev2_trace,
3456 
3457  .n_errors = IKEV2_N_ERROR,
3458  .error_counters = ikev2_error_counters,
3459 
3460  .n_next_nodes = IKEV2_IP4_N_NEXT,
3461  .next_nodes = {
3462  [IKEV2_NEXT_IP4_LOOKUP] = "ip4-lookup",
3463  [IKEV2_NEXT_IP4_ERROR_DROP] = "error-drop",
3464  },
3465 };
3466 
3468  .function = ikev2_ip6,
3469  .name = "ikev2-ip6",
3470  .vector_size = sizeof (u32),
3471  .format_trace = format_ikev2_trace,
3473 
3474  .n_errors = IKEV2_N_ERROR,
3475  .error_counters = ikev2_error_counters,
3476 
3477  .n_next_nodes = IKEV2_IP6_N_NEXT,
3478  .next_nodes = {
3479  [IKEV2_NEXT_IP6_LOOKUP] = "ip6-lookup",
3480  [IKEV2_NEXT_IP6_ERROR_DROP] = "error-drop",
3481  },
3482 };
3483 /* *INDENT-ON* */
3484 
3485 // set ikev2 proposals when vpp is used as initiator
3486 static clib_error_t *
3488  ikev2_transforms_set * ts,
3489  ikev2_sa_proposal_t ** proposals, int is_ike)
3490 {
3491  clib_error_t *r;
3492  ikev2_main_t *km = &ikev2_main;
3493  ikev2_sa_proposal_t *proposal;
3494  vec_add2 (*proposals, proposal, 1);
3496  int error;
3497 
3498  /* Encryption */
3499  error = 1;
3501  {
3502  if (td->type == IKEV2_TRANSFORM_TYPE_ENCR
3503  && td->encr_type == ts->crypto_alg
3504  && td->key_len == ts->crypto_key_size / 8)
3505  {
3506  u16 attr[2];
3507  attr[0] = clib_host_to_net_u16 (14 | (1 << 15));
3508  attr[1] = clib_host_to_net_u16 (td->key_len << 3);
3509  vec_add (td->attrs, (u8 *) attr, 4);
3510  vec_add1 (proposal->transforms, *td);
3511  td->attrs = 0;
3512 
3513  error = 0;
3514  break;
3515  }
3516  }
3517  if (error)
3518  {
3519  r = clib_error_return (0, "Unsupported algorithm");
3520  return r;
3521  }
3522 
3523  if (IKEV2_TRANSFORM_INTEG_TYPE_NONE != ts->integ_alg)
3524  {
3525  /* Integrity */
3526  error = 1;
3528  {
3529  if (td->type == IKEV2_TRANSFORM_TYPE_INTEG
3530  && td->integ_type == ts->integ_alg)
3531  {
3532  vec_add1 (proposal->transforms, *td);
3533  error = 0;
3534  break;
3535  }
3536  }
3537  if (error)
3538  {
3540  ("Didn't find any supported algorithm for IKEV2_TRANSFORM_TYPE_INTEG");
3541  r = clib_error_return (0, "Unsupported algorithm");
3542  return r;
3543  }
3544  }
3545 
3546  /* PRF */
3547  if (is_ike)
3548  {
3549  error = 1;
3551  {
3552  if (td->type == IKEV2_TRANSFORM_TYPE_PRF
3553  && td->prf_type == IKEV2_TRANSFORM_PRF_TYPE_PRF_HMAC_SHA2_256)
3554  {
3555  vec_add1 (proposal->transforms, *td);
3556  error = 0;
3557  break;
3558  }
3559  }
3560  if (error)
3561  {
3562  r = clib_error_return (0, "Unsupported algorithm");
3563  return r;
3564  }
3565  }
3566 
3567  /* DH */
3568  if (is_ike)
3569  {
3570  error = 1;
3572  {
3573  if (td->type == IKEV2_TRANSFORM_TYPE_DH && td->dh_type == ts->dh_type)
3574  {
3575  vec_add1 (proposal->transforms, *td);
3576  if (is_ike)
3577  {
3578  sa->dh_group = td->dh_type;
3579  }
3580  error = 0;
3581  break;
3582  }
3583  }
3584  if (error)
3585  {
3586  r = clib_error_return (0, "Unsupported algorithm");
3587  return r;
3588  }
3589  }
3590 
3591  if (!is_ike)
3592  {
3593  error = 1;
3595  {
3596  if (td->type == IKEV2_TRANSFORM_TYPE_ESN)
3597  {
3598  vec_add1 (proposal->transforms, *td);
3599  error = 0;
3600  }
3601  }
3602  if (error)
3603  {
3604  r = clib_error_return (0, "Unsupported algorithm");
3605  return r;
3606  }
3607  }
3608 
3609 
3610  return 0;
3611 }
3612 
3613 static ikev2_profile_t *
3615 {
3616  ikev2_main_t *km = &ikev2_main;
3617  uword *p;
3618 
3619  p = mhash_get (&km->profile_index_by_name, name);
3620  if (!p)
3621  return 0;
3622 
3623  return pool_elt_at_index (km->profiles, p[0]);
3624 }
3625 
3626 
3627 static void
3630 {
3631  ip4_header_t *ip40;
3632  ip6_header_t *ip60;
3633  udp_header_t *udp0;
3634  vlib_buffer_t *b0;
3635  vlib_frame_t *f;
3636  u32 *to_next;
3637 
3638  b0 = vlib_get_buffer (vm, bi0);
3639  vlib_buffer_advance (b0, -sizeof (udp_header_t));
3640  udp0 = vlib_buffer_get_current (b0);
3641  udp0->dst_port = clib_host_to_net_u16 (dst_port);
3642  udp0->src_port = clib_host_to_net_u16 (src_port);
3643  udp0->length = clib_host_to_net_u16 (len + sizeof (udp_header_t));
3644  udp0->checksum = 0;
3645 
3646  if (ip_addr_version (dst) == AF_IP4)
3647  {
3648  vlib_buffer_advance (b0, -sizeof (ip4_header_t));
3649  ip40 = vlib_buffer_get_current (b0);
3650  ip40->ip_version_and_header_length = 0x45;
3651  ip40->tos = 0;
3652  ip40->fragment_id = 0;
3653  ip40->flags_and_fragment_offset = 0;
3654  ip40->ttl = 0xff;
3655  ip40->protocol = IP_PROTOCOL_UDP;
3656  ip40->dst_address.as_u32 = ip_addr_v4 (dst).as_u32;
3657  ip40->src_address.as_u32 = ip_addr_v4 (src).as_u32;
3658  b0->current_length =
3659  len + sizeof (ip4_header_t) + sizeof (udp_header_t);
3660  ip40->length = clib_host_to_net_u16 (b0->current_length);
3661  ip40->checksum = ip4_header_checksum (ip40);
3662  }
3663  else
3664  {
3665  vlib_buffer_advance (b0, -sizeof (ip6_header_t));
3666  ip60 = vlib_buffer_get_current (b0);
3667 
3668  b0->current_length = len + sizeof (*ip60) + sizeof (udp_header_t);
3670  clib_host_to_net_u32 (0x6 << 28);
3671  ip60->payload_length =
3672  clib_host_to_net_u16 (b0->current_length - sizeof (*ip60));
3673  ip60->protocol = IP_PROTOCOL_UDP;
3674  ip60->hop_limit = 0xff;
3675  clib_memcpy_fast (ip60->src_address.as_u8, ip_addr_v6 (src).as_u8,
3676  sizeof (ip60->src_address));
3677  clib_memcpy_fast (ip60->dst_address.as_u8, ip_addr_v6 (dst).as_u8,
3678  sizeof (ip60->src_address));
3679  }
3680 
3681  b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3682  vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index;
3683  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
3684 
3686  ip4_lookup_node.index : ip6_lookup_node.index;
3687 
3688  /* send the request */
3690  to_next = vlib_frame_vector_args (f);
3691  to_next[0] = bi0;
3692  f->n_vectors = 1;
3694 
3695 }
3696 
3697 static u32
3699 {
3700  u32 bi0;
3701  if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
3702  {
3703  *b = 0;
3704  return 0;
3705  }
3706  *b = vlib_get_buffer (vm, bi0);
3707  return bi0;
3708 }
3709 
3710 clib_error_t *
3712 {
3713  ikev2_main_t *km = &ikev2_main;
3714 
3715  if (km->pkey)
3716  EVP_PKEY_free (km->pkey);
3717  km->pkey = ikev2_load_key_file (file);
3718  if (km->pkey == NULL)
3719  return clib_error_return (0, "load key '%s' failed", file);
3720 
3721  return 0;
3722 }
3723 
3726 {
3729  return 0;
3730 }
3731 
3734 {
3736  return;
3737 
3740 }
3741 
3742 static void
3745  ikev2_sa_t * sa, u8 send_notification)
3746 {
3747  ikev2_main_t *km = &ikev2_main;
3748  ip_address_t *src, *dst;
3749  vlib_buffer_t *b0;
3751 
3752  /* Create the Initiator notification for IKE SA removal */
3753  ike_header_t *ike0;
3754  u32 bi0 = 0;
3755  int len;
3756 
3757  vec_resize (sa->del, 1);
3759  sa->del->spi = sa->ispi;
3760 
3761  if (send_notification)
3762  {
3763  bi0 = ikev2_get_new_ike_header_buff (vm, &b0);
3764  if (!bi0)
3765  {
3766  ikev2_log_error ("buffer alloc failure");
3767  goto delete_sa;
3768  }
3769 
3770  ike0 = vlib_buffer_get_current (b0);
3771  ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3772  ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3773  ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3774  ike0->flags = 0;
3775  ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id);
3776  sa->last_init_msg_id += 1;
3777  len = ikev2_generate_message (b0, sa, ike0, 0, 0, 0);
3778  if (~0 == len)
3779  return;
3780 
3781  if (ikev2_natt_active (sa))
3783 
3784  if (sa->is_initiator)
3785  {
3786  src = &sa->iaddr;
3787  dst = &sa->raddr;
3788  }
3789  else
3790  {
3791  dst = &sa->iaddr;
3792  src = &sa->raddr;
3793  }
3794 
3795  ikev2_send_ike (vm, src, dst, bi0, len,
3796  ikev2_get_port (sa), sa->dst_port, 0);
3797  }
3798 
3799 delete_sa:
3800  /* delete local SA */
3801  vec_foreach (c, sa->childs)
3803 
3804  u64 rspi = sa->rspi;
3805  ikev2_sa_free_all_vec (sa);
3806  uword *p = hash_get (tkm->sa_by_rspi, rspi);
3807  if (p)
3808  {
3809  hash_unset (tkm->sa_by_rspi, rspi);
3810  pool_put (tkm->sas, sa);
3811  }
3812 }
3813 
3814 static void
3816 {
3818  ikev2_sa_t *sa;
3819  u32 pi = p - km->profiles;
3820  u32 *sai;
3821  u32 *del_sai = 0;
3822 
3823  /* *INDENT-OFF* */
3824  pool_foreach (sa, km->sais) {
3825  if (pi == sa->profile_index)
3826  vec_add1 (del_sai, sa - km->sais);
3827  }
3828  /* *INDENT-ON* */
3829 
3830  vec_foreach (sai, del_sai)
3831  {
3832  sa = pool_elt_at_index (km->sais, sai[0]);
3833  ikev2_sa_free_all_vec (sa);
3834  hash_unset (km->sa_by_ispi, sa->ispi);
3835  pool_put (km->sais, sa);
3836  }
3837  vec_reset_length (del_sai);
3838 
3839  vec_foreach (tkm, km->per_thread_data)
3840  {
3841  /* *INDENT-OFF* */
3842  pool_foreach (sa, tkm->sas) {
3843  if (sa->profile_index != ~0 && pi == sa->profile_index)
3844  vec_add1 (del_sai, sa - tkm->sas);
3845  }
3846  /* *INDENT-ON* */
3847 
3848  vec_foreach (sai, del_sai)
3849  {
3850  sa = pool_elt_at_index (tkm->sas, sai[0]);
3852  }
3853 
3854  vec_reset_length (del_sai);
3855  }
3856 
3857  vec_free (del_sai);
3858 }
3859 
3860 static void
3862 {
3863  vec_free (r->hostname);
3864 }
3865 
3866 static void
3868 {
3869  vec_free (p->name);
3870 
3871  vec_free (p->auth.data);
3872  if (p->auth.key)
3873  EVP_PKEY_free (p->auth.key);
3874 
3876 
3877  vec_free (p->loc_id.data);
3878  vec_free (p->rem_id.data);
3879 }
3880 
3881 clib_error_t *
3883 {
3884  ikev2_main_t *km = &ikev2_main;
3885  ikev2_profile_t *p;
3886 
3887  if (is_add)
3888  {
3890  return clib_error_return (0, "policy %v already exists", name);
3891 
3892  pool_get (km->profiles, p);
3893  clib_memset (p, 0, sizeof (*p));
3894  p->name = vec_dup (name);
3896  p->responder.sw_if_index = ~0;
3897  p->tun_itf = ~0;
3898  uword index = p - km->profiles;
3900  }
3901  else
3902  {
3904  if (!p)
3905  return clib_error_return (0, "policy %v does not exists", name);
3906 
3909 
3910  ikev2_profile_free (p);
3911  pool_put (km->profiles, p);
3913  }
3914  return 0;
3915 }
3916 
3917 clib_error_t *
3919  u8 * auth_data, u8 data_hex_format)
3920 {
3921  ikev2_profile_t *p;
3922  clib_error_t *r;
3923 
3925 
3926  if (!p)
3927  {
3928  r = clib_error_return (0, "unknown profile %v", name);
3929  return r;
3930  }
3931 
3932  if (p->auth.key)
3933  EVP_PKEY_free (p->auth.key);
3934  vec_free (p->auth.data);
3935 
3936  p->auth.method = auth_method;
3937  p->auth.data = vec_dup (auth_data);
3938  p->auth.hex = data_hex_format;
3939 
3940  if (auth_method == IKEV2_AUTH_METHOD_RSA_SIG)
3941  {
3942  vec_add1 (p->auth.data, 0);
3944  if (p->auth.key == NULL)
3945  return clib_error_return (0, "load cert '%s' failed", p->auth.data);
3946  }
3947 
3948  return 0;
3949 }
3950 
3951 static int
3953 {
3954  return (id_type == IKEV2_ID_TYPE_ID_IPV4_ADDR ||
3955  id_type == IKEV2_ID_TYPE_ID_IPV6_ADDR ||
3956  id_type == IKEV2_ID_TYPE_ID_RFC822_ADDR ||
3957  id_type == IKEV2_ID_TYPE_ID_FQDN);
3958 }
3959 
3960 clib_error_t *
3962  int is_local)
3963 {
3964  ikev2_profile_t *p;
3965  clib_error_t *r;
3966 
3967  if (!ikev2_is_id_supported (id_type))
3968  {
3969  r = clib_error_return (0, "unsupported identity type %U",
3970  format_ikev2_id_type, id_type);
3971  return r;
3972  }
3973 
3975 
3976  if (!p)
3977  {
3978  r = clib_error_return (0, "unknown profile %v", name);
3979  return r;
3980  }
3981 
3982  if (is_local)
3983  {
3984  vec_free (p->loc_id.data);
3985  p->loc_id.type = id_type;
3986  p->loc_id.data = vec_dup (data);
3987  }
3988  else
3989  {
3990  vec_free (p->rem_id.data);
3991  p->rem_id.type = id_type;
3992  p->rem_id.data = vec_dup (data);
3993  }
3994 
3995  return 0;
3996 }
3997 
4000 {
4001  if (ip_addr_version (addr) == AF_IP4)
4003  else
4005 }
4006 
4009  const ip_address_t * end)
4010 {
4011  ip_address_copy (&ts->start_addr, start);
4012  ip_address_copy (&ts->end_addr, end);
4013 }
4014 
4015 clib_error_t *
4019 {
4020  ikev2_profile_t *p;
4021  clib_error_t *r;
4022 
4024 
4025  if (!p)
4026  {
4027  r = clib_error_return (0, "unknown profile %v", name);
4028  return r;
4029  }
4030 
4032  return clib_error_return (0, "IP address version mismatch!");
4033 
4034  if (is_local)
4035  {
4038  p->loc_ts.end_port = end_port;
4041  }
4042  else
4043  {
4046  p->rem_ts.end_port = end_port;
4049  }
4050 
4051  return 0;
4052 }
4053 
4054 clib_error_t *
4056  u32 sw_if_index)
4057 {
4058  ikev2_profile_t *p;
4059  clib_error_t *r;
4060 
4062 
4063  if (!p)
4064  {
4065  r = clib_error_return (0, "unknown profile %v", name);
4066  return r;
4067  }
4068 
4069  p->responder.is_resolved = 0;
4072 
4073  return 0;
4074 }
4075 
4076 clib_error_t *
4079 {
4080  ikev2_profile_t *p;
4081  clib_error_t *r;
4082 
4084 
4085  if (!p)
4086  {
4087  r = clib_error_return (0, "unknown profile %v", name);
4088  return r;
4089  }
4090 
4091  p->responder.is_resolved = 1;
4094 
4095  return 0;
4096 }
4097 
4098 clib_error_t *
4100  ikev2_transform_encr_type_t crypto_alg,
4102  ikev2_transform_dh_type_t dh_type,
4104 {
4105  ikev2_profile_t *p;
4106  clib_error_t *r;
4107 
4109 
4110  if (!p)
4111  {
4112  r = clib_error_return (0, "unknown profile %v", name);
4113  return r;
4114  }
4115 
4116  p->ike_ts.crypto_alg = crypto_alg;
4117  p->ike_ts.integ_alg = integ_alg;
4118  p->ike_ts.dh_type = dh_type;
4120  return 0;
4121 }
4122 
4123 clib_error_t *
4125  ikev2_transform_encr_type_t crypto_alg,
4128 {
4129  ikev2_profile_t *p;
4130  clib_error_t *r;
4131 
4133 
4134  if (!p)
4135  {
4136  r = clib_error_return (0, "unknown profile %v", name);
4137  return r;
4138  }
4139 
4140  p->esp_ts.crypto_alg = crypto_alg;
4141  p->esp_ts.integ_alg = integ_alg;
4143  return 0;
4144 }
4145 
4146 clib_error_t *
4148  u8 * name, u32 sw_if_index)
4149 {
4150  ikev2_profile_t *p;
4151  clib_error_t *r;
4152 
4154 
4155  if (!p)
4156  {
4157  r = clib_error_return (0, "unknown profile %v", name);
4158  return r;
4159  }
4160 
4161  p->tun_itf = sw_if_index;
4162 
4163  return 0;
4164 }
4165 
4168  u8 is_set)
4169 {
4171  vnet_api_error_t rv = 0;
4172 
4173  if (!p)
4174  return VNET_API_ERROR_INVALID_VALUE;
4175 
4176  if (is_set)
4177  {
4179  return VNET_API_ERROR_VALUE_EXIST;
4180 
4182  }
4183  else
4184  {
4186  return VNET_API_ERROR_INVALID_VALUE;
4187 
4189  }
4190  return rv;
4191 }
4192 
4193 clib_error_t *
4195 {
4197  clib_error_t *r;
4198 
4199  if (!p)
4200  {
4201  r = clib_error_return (0, "unknown profile %v", name);
4202  return r;
4203  }
4204 
4205  p->udp_encap = 1;
4206  return 0;
4207 }
4208 
4209 clib_error_t *
4211  u64 lifetime, u32 jitter, u32 handover,
4212  u64 maxdata)
4213 {
4214  ikev2_profile_t *p;
4215  clib_error_t *r;
4216 
4218 
4219  if (!p)
4220  {
4221  r = clib_error_return (0, "unknown profile %v", name);
4222  return r;
4223  }
4224 
4225  p->lifetime = lifetime;
4226  p->lifetime_jitter = jitter;
4227  p->handover = handover;
4228  p->lifetime_maxdata = maxdata;
4229  return 0;
4230 }
4231 
4232 static int
4234  ip_address_t * out_addr)
4235 {
4236  ip4_address_t *if_ip4;
4237  ip6_address_t *if_ip6;
4238 
4239  if (af == AF_IP4)
4240  {
4242  if (if_ip4)
4243  {
4244  ip_address_set (out_addr, if_ip4, AF_IP4);
4245  return 1;
4246  }
4247  }
4248  else
4249  {
4251  if (if_ip6)
4252  {
4253  ip_address_set (out_addr, if_ip6, AF_IP6);
4254  return 1;
4255  }
4256  }
4257  return 0;
4258 }
4259 
4260 static clib_error_t *
4262 {
4263  ikev2_main_t *km = &ikev2_main;
4264  dns_cache_entry_t *ep = 0;
4265  dns_pending_request_t _t0, *t0 = &_t0;
4266  dns_resolve_name_t _rn, *rn = &_rn;
4267  int rv;
4268 
4269  if (!km->dns_resolve_name)
4270  return clib_error_return (0, "cannot load symbols from dns plugin");
4271 
4273  rv = km->dns_resolve_name (r->hostname, &ep, t0, rn);
4274  if (rv < 0)
4275  return clib_error_return (0, "dns lookup failure");
4276 
4277  if (ep == 0)
4278  return 0;
4279 
4280  ip_address_copy (&r->addr, &rn->address);
4281  r->is_resolved = 1;
4282  return 0;
4283 }
4284 
4285 clib_error_t *
4287 {
4288  ikev2_profile_t *p;
4289  clib_error_t *r;
4290  ikev2_main_t *km = &ikev2_main;
4291  vlib_buffer_t *b0;
4292  ike_header_t *ike0;
4293  u32 bi0 = 0;
4294  int len = sizeof (ike_header_t), valid_ip = 0;
4296 
4298 
4299  if (!p)
4300  {
4301  r = clib_error_return (0, "unknown profile %v", name);
4302  return r;
4303  }
4304 
4305  if (p->responder.sw_if_index == ~0 ||
4307  vec_len (p->responder.hostname) == 0))
4308  {
4309  r = clib_error_return (0, "responder not set for profile %v", name);
4310  return r;
4311  }
4312 
4313  if (!p->responder.is_resolved)
4314  {
4315  /* try to resolve using dns plugin
4316  * success does not mean we have resolved the name */
4318  if (r)
4319  return r;
4320  }
4321 
4322  if (p->responder.is_resolved &&
4324  ip_addr_version (&p->responder.addr), &src_if_ip))
4325  {
4326  valid_ip = 1;
4327  }
4328 
4329  /* Prepare the SA and the IKE payload */
4330  ikev2_sa_t sa;
4331  clib_memset (&sa, 0, sizeof (ikev2_sa_t));
4332  ikev2_payload_chain_t *chain = 0;
4333  ikev2_payload_new_chain (chain);
4334 
4335  /* Build the IKE proposal payload */
4336  ikev2_sa_proposal_t *proposals = 0;
4337  ikev2_set_initiator_proposals (vm, &sa, &p->ike_ts, &proposals, 1);
4338  proposals[0].proposal_num = 1;
4339  proposals[0].protocol_id = IKEV2_PROTOCOL_IKE;
4340 
4341  /* Add and then cleanup proposal data */
4342  ikev2_payload_add_sa (chain, proposals);
4343  ikev2_sa_free_proposal_vector (&proposals);
4344 
4345  sa.is_initiator = 1;
4346  sa.profile_index = p - km->profiles;
4348  sa.tun_itf = p->tun_itf;
4349  sa.udp_encap = p->udp_encap;
4350  if (p->natt_disabled)
4353  sa.is_tun_itf_set = 1;
4354  sa.initial_contact = 1;
4355  sa.dst_port = IKEV2_PORT;
4356 
4358  if (rc != IKEV2_GENERATE_SA_INIT_OK)
4359  {
4360  ikev2_sa_free_all_vec (&sa);
4362  return clib_error_return (0, "%U", format_ikev2_gen_sa_error, rc);
4363  }
4364 
4365  ikev2_payload_add_ke (chain, sa.dh_group, sa.i_dh_data);
4366  ikev2_payload_add_nonce (chain, sa.i_nonce);
4367 
4368  /* Build the child SA proposal */
4369  vec_resize (sa.childs, 1);
4371  &sa.childs[0].i_proposals, 0);
4372  sa.childs[0].i_proposals[0].proposal_num = 1;
4374  RAND_bytes ((u8 *) & sa.childs[0].i_proposals[0].spi,
4375  sizeof (sa.childs[0].i_proposals[0].spi));
4376 
4377  /* Add NAT detection notification messages (mandatory) */
4378  u8 *nat_detection_sha1 = ikev2_compute_nat_sha1 (
4379  clib_host_to_net_u64 (sa.ispi), clib_host_to_net_u64 (sa.rspi), &src_if_ip,
4380  clib_host_to_net_u16 (IKEV2_PORT));
4381 
4382  ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
4383  nat_detection_sha1);
4384  vec_free (nat_detection_sha1);
4385  nat_detection_sha1 =
4386  ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa.ispi),
4387  clib_host_to_net_u64 (sa.rspi),
4388  &p->responder.addr,
4389  clib_host_to_net_u16 (sa.dst_port));
4390  ikev2_payload_add_notify (chain,
4391  IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
4392  nat_detection_sha1);
4393  vec_free (nat_detection_sha1);
4394 
4395  u8 *sig_hash_algo = vec_new (u8, 8);
4396  u64 tmpsig = clib_host_to_net_u64 (0x0001000200030004);
4397  clib_memcpy_fast (sig_hash_algo, &tmpsig, sizeof (tmpsig));
4398  ikev2_payload_add_notify (chain,
4399  IKEV2_NOTIFY_MSG_SIGNATURE_HASH_ALGORITHMS,
4400  sig_hash_algo);
4401  vec_free (sig_hash_algo);
4402 
4403  bi0 = ikev2_get_new_ike_header_buff (vm, &b0);
4404  if (!bi0)
4405  {
4406  ikev2_sa_free_all_vec (&sa);
4408  char *errmsg = "buffer alloc failure";
4409  ikev2_log_error (errmsg);
4410  return clib_error_return (0, errmsg);
4411  }
4412  ike0 = vlib_buffer_get_current (b0);
4413 
4414  /* Buffer update and boilerplate */
4415  len += vec_len (chain->data);
4416  ike0->nextpayload = chain->first_payload_type;
4417  ike0->length = clib_host_to_net_u32 (len);
4418  clib_memcpy_fast (ike0->payload, chain->data, vec_len (chain->data));
4420 
4421  ike0->version = IKE_VERSION_2;
4422  ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
4423  ike0->exchange = IKEV2_EXCHANGE_SA_INIT;
4424  ike0->ispi = clib_host_to_net_u64 (sa.ispi);
4425  ike0->rspi = 0;
4426  ike0->msgid = 0;
4427  sa.last_init_msg_id += 1;
4428 
4429  /* store whole IKE payload - needed for PSK auth */
4432 
4433  /* add data to the SA then add it to the pool */
4434  ip_address_copy (&sa.iaddr, &src_if_ip);
4435  ip_address_copy (&sa.raddr, &p->responder.addr);
4436  sa.i_id.type = p->loc_id.type;
4437  sa.i_id.data = vec_dup (p->loc_id.data);
4438  sa.r_id.type = p->rem_id.type;
4439  sa.r_id.data = vec_dup (p->rem_id.data);
4440  sa.i_auth.method = p->auth.method;
4441  sa.i_auth.hex = p->auth.hex;
4442  sa.i_auth.data = vec_dup (p->auth.data);
4444  vec_add (sa.childs[0].tsi, &p->loc_ts, 1);
4445  vec_add (sa.childs[0].tsr, &p->rem_ts, 1);
4446 
4448 
4449  /* add SA to the pool */
4450  ikev2_sa_t *sa0 = 0;
4451  pool_get (km->sais, sa0);
4452  clib_memcpy_fast (sa0, &sa, sizeof (*sa0));
4453  hash_set (km->sa_by_ispi, sa0->ispi, sa0 - km->sais);
4454 
4455  if (valid_ip)
4456  {
4457  ikev2_send_ike (vm, &src_if_ip, &p->responder.addr, bi0, len, IKEV2_PORT,
4458  sa.dst_port, sa.sw_if_index);
4459 
4461  ("ispi %lx rspi %lx IKEV2_EXCHANGE_SA_INIT sent to ",
4462  clib_host_to_net_u64 (sa0->ispi), 0,
4463  ip_addr_v4 (&p->responder.addr).as_u32,
4465  }
4466 
4467  return 0;
4468 }
4469 
4470 static void
4472  ikev2_child_sa_t * csa)
4473 {
4474  /* Create the Initiator notification for child SA removal */
4475  ikev2_main_t *km = &ikev2_main;
4476  ike_header_t *ike0;
4477  u32 bi0 = 0;
4478  vlib_buffer_t *b0;
4479  int len;
4480 
4481  bi0 = ikev2_get_new_ike_header_buff (vm, &b0);
4482  if (!bi0)
4483  {
4484  ikev2_log_error ("buffer alloc failure");
4485  return;
4486  }
4487 
4488  ike0 = vlib_buffer_get_current (b0);
4489  ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
4490  ike0->ispi = clib_host_to_net_u64 (sa->ispi);
4491  ike0->rspi = clib_host_to_net_u64 (sa->rspi);
4492  ike0->flags = 0;
4493  vec_resize (sa->del, 1);
4495  sa->del->spi = csa->i_proposals->spi;
4496  ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id);
4497  sa->last_init_msg_id += 1;
4498  len = ikev2_generate_message (b0, sa, ike0, 0, 0, 0);
4499  if (~0 == len)
4500  return;
4501 
4502  if (ikev2_natt_active (sa))
4504  ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len,
4505  ikev2_get_port (sa), sa->dst_port, sa->sw_if_index);
4506 
4507  /* delete local child SA */
4509  ikev2_sa_del_child_sa (sa, csa);
4510 }
4511 
4512 clib_error_t *
4514 {
4515  clib_error_t *r;
4516  ikev2_main_t *km = &ikev2_main;
4518  ikev2_sa_t *fsa = 0;
4519  ikev2_child_sa_t *fchild = 0;
4520 
4521  /* Search for the child SA */
4522  vec_foreach (tkm, km->per_thread_data)
4523  {
4524  ikev2_sa_t *sa;
4525  if (fchild)
4526  break;
4527  /* *INDENT-OFF* */
4528  pool_foreach (sa, tkm->sas) {
4529  fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
4530  if (fchild)
4531  {
4532  fsa = sa;
4533  break;
4534  }
4535  }
4536  /* *INDENT-ON* */
4537  }
4538 
4539  if (!fchild || !fsa)
4540  {
4541  r = clib_error_return (0, "Child SA not found");
4542  return r;
4543  }
4544  else
4545  {
4546  ikev2_delete_child_sa_internal (vm, fsa, fchild);
4547  }
4548 
4549  return 0;
4550 }
4551 
4552 clib_error_t *
4554 {
4555  clib_error_t *r;
4556  ikev2_main_t *km = &ikev2_main;
4558  ikev2_sa_t *fsa = 0;
4559  ikev2_main_per_thread_data_t *ftkm = 0;
4560 
4561  /* Search for the IKE SA */
4562  vec_foreach (tkm, km->per_thread_data)
4563  {
4564  ikev2_sa_t *sa;
4565  if (fsa)
4566  break;
4567  /* *INDENT-OFF* */
4568  pool_foreach (sa, tkm->sas) {
4569  if (sa->ispi == ispi)
4570  {
4571  fsa = sa;
4572  ftkm = tkm;
4573  break;
4574  }
4575  }
4576  /* *INDENT-ON* */
4577  }
4578 
4579  if (!fsa)
4580  {
4581  r = clib_error_return (0, "IKE SA not found");
4582  return r;
4583  }
4584 
4585  ikev2_initiate_delete_ike_sa_internal (vm, ftkm, fsa, 1);
4586  return 0;
4587 }
4588 
4589 static void
4591  ikev2_child_sa_t * csa)
4592 {
4593  /* Create the Initiator request for create child SA */
4594  ike_header_t *ike0;
4595  vlib_buffer_t *b0;
4596  u32 bi0 = 0;
4597  int len;
4598 
4599  bi0 = ikev2_get_new_ike_header_buff (vm, &b0);
4600  if (!bi0)
4601  {
4602  ikev2_log_error ("buffer alloc failure");
4603  return;
4604  }
4605 
4606  ike0 = vlib_buffer_get_current (b0);
4607  ike0->version = IKE_VERSION_2;
4608  ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
4609  ike0->exchange = IKEV2_EXCHANGE_CREATE_CHILD_SA;
4610  ike0->ispi = clib_host_to_net_u64 (sa->ispi);
4611  ike0->rspi = clib_host_to_net_u64 (sa->rspi);
4612  ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id);
4613  sa->last_init_msg_id += 1;
4614 
4615  ikev2_rekey_t *rekey;
4616  vec_reset_length (sa->rekey);
4617  vec_add2 (sa->rekey, rekey, 1);
4618  ikev2_sa_proposal_t *proposals = vec_dup (csa->i_proposals);
4619 
4620  /*need new ispi */
4621  RAND_bytes ((u8 *) & proposals[0].spi, sizeof (proposals[0].spi));
4622  rekey->spi = proposals[0].spi;
4623  rekey->ispi = csa->i_proposals->spi;
4624  len = ikev2_generate_message (b0, sa, ike0, proposals, 0, 0);
4625  if (~0 == len)
4626  return;
4627 
4628  if (ikev2_natt_active (sa))
4630  ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len,
4631  ikev2_get_port (sa), ikev2_get_port (sa), sa->sw_if_index);
4632  vec_free (proposals);
4633 }
4634 
4635 clib_error_t *
4637 {
4638  clib_error_t *r;
4639  ikev2_main_t *km = &ikev2_main;
4641  ikev2_sa_t *fsa = 0;
4642  ikev2_child_sa_t *fchild = 0;
4643 
4644  /* Search for the child SA */
4645  vec_foreach (tkm, km->per_thread_data)
4646  {
4647  ikev2_sa_t *sa;
4648  if (fchild)
4649  break;
4650  /* *INDENT-OFF* */
4651  pool_foreach (sa, tkm->sas) {
4652  fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
4653  if (fchild)
4654  {
4655  fsa = sa;
4656  break;
4657  }
4658  }
4659  /* *INDENT-ON* */
4660  }
4661 
4662  if (!fchild || !fsa)
4663  {
4664  r = clib_error_return (0, "Child SA not found");
4665  return r;
4666  }
4667  else
4668  {
4669  ikev2_rekey_child_sa_internal (vm, fsa, fchild);
4670  }
4671 
4672  return 0;
4673 }
4674 
4675 static int
4677 {
4678  return (sa->sw_if_index == sw_if_index) && sa->is_initiator;
4679 }
4680 
4681 static void
4683 {
4684  u64 *ispi, *ispi_vec = 0;
4685  ikev2_sa_t *sa, **sap, **sa_vec = 0;
4686  ikev2_main_t *km = &ikev2_main;
4688  p->responder.sw_if_index = ~0;
4689 
4690  vec_foreach (tkm, km->per_thread_data)
4691  {
4692  /* *INDENT-OFF* */
4693  pool_foreach (sa, tkm->sas) {
4695  vec_add1 (sa_vec, sa);
4696  }
4697  /* *INDENT-ON* */
4698 
4699  vec_foreach (sap, sa_vec)
4700  {
4702  }
4703  vec_reset_length (sa_vec);
4704  }
4705  vec_free (sa_vec);
4706 
4707  /* *INDENT-OFF* */
4708  pool_foreach (sa, km->sais) {
4710  vec_add1 (ispi_vec, sa->ispi);
4711  }
4712  /* *INDENT-ON* */
4713 
4714  vec_foreach (ispi, ispi_vec)
4715  {
4717  }
4718 
4719  vec_free (ispi_vec);
4720 }
4721 
4722 static clib_error_t *
4724 {
4725  ikev2_main_t *km = &ikev2_main;
4726  ikev2_profile_t *p;
4727 
4728  if (is_add)
4729  return 0;
4730 
4731  /* *INDENT-OFF* */
4732  pool_foreach (p, km->profiles) {
4733  if (p->responder.sw_if_index == sw_if_index)
4735  }
4736  /* *INDENT-ON* */
4737 
4738  return 0;
4739 }
4740 
4742 
4743 clib_error_t *
4745 {
4746  ikev2_main_t *km = &ikev2_main;
4748  int thread_id;
4749 
4750  clib_memset (km, 0, sizeof (ikev2_main_t));
4751  km->vnet_main = vnet_get_main ();
4752  km->vlib_main = vm;
4753 
4756  ikev2_crypto_init (km);
4757 
4759 
4762  for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++)
4763  {
4765  vec_elt_at_index (km->per_thread_data, thread_id);
4766 
4767  ptd->sa_by_rspi = hash_create (0, sizeof (uword));
4768 
4769 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
4770  ptd->evp_ctx = EVP_CIPHER_CTX_new ();
4771  ptd->hmac_ctx = HMAC_CTX_new ();
4772 #else
4773  EVP_CIPHER_CTX_init (&ptd->_evp_ctx);
4774  ptd->evp_ctx = &ptd->_evp_ctx;
4775  HMAC_CTX_init (&(ptd->_hmac_ctx));
4776  ptd->hmac_ctx = &ptd->_hmac_ctx;
4777 #endif
4778  }
4779 
4780  km->sa_by_ispi = hash_create (0, sizeof (uword));
4781  km->sw_if_indices = hash_create (0, 0);
4782 
4787 
4788  vlib_punt_hdl_t punt_hdl = vlib_punt_client_register ("ikev2-ip4-natt");
4789  vlib_punt_register (punt_hdl, ipsec_punt_reason[IPSEC_PUNT_IP4_SPI_UDP_0],
4790  "ikev2-ip4-natt");
4792 
4793  km->dns_resolve_name =
4794  vlib_get_plugin_symbol ("dns_plugin.so", "dns_resolve_name");
4795  if (!km->dns_resolve_name)
4796  ikev2_log_error ("cannot load symbols from dns plugin");
4797 
4798  km->log_level = IKEV2_LOG_ERROR;
4799  km->log_class = vlib_log_register_class ("ikev2", 0);
4800  return 0;
4801 }
4802 
4803 /* *INDENT-OFF* */
4805  .runs_after = VLIB_INITS ("ipsec_init", "ipsec_punt_init", "dns_init"),
4806 };
4807 /* *INDENT-ON* */
4808 
4809 static u8
4811  u8 del_old_ids)
4812 {
4813  ikev2_main_t *km = &ikev2_main;
4814  ikev2_profile_t *p = 0;
4815  vlib_main_t *vm = km->vlib_main;
4816  f64 now = vlib_time_now (vm);
4817  u8 res = 0;
4818 
4819  if (sa->profile_index != ~0)
4820  p = pool_elt_at_index (km->profiles, sa->profile_index);
4821 
4822  if (sa->is_initiator && p && csa->time_to_expiration
4823  &&