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