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