blob: 34ab87c3e5640f389dbcfd433b3fdbb51becf0ef [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vlib/vlib.h>
17#include <vnet/vnet.h>
18#include <vnet/pg/pg.h>
19#include <vppinfra/error.h>
Chris Lukeab7b8d92017-09-07 07:40:13 -040020#include <vppinfra/random.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050021#include <vnet/udp/udp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022#include <vnet/ipsec/ipsec.h>
23#include <vnet/ipsec/ikev2.h>
24#include <vnet/ipsec/ikev2_priv.h>
Radu Nicolaucb33dc22017-02-16 16:49:46 +000025#include <openssl/sha.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
Dave Wallace71612d62017-10-24 01:32:41 -040027ikev2_main_t ikev2_main;
28
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070029static int ikev2_delete_tunnel_interface (vnet_main_t * vnm,
30 ikev2_sa_t * sa,
31 ikev2_child_sa_t * child);
Ed Warnickecb9cada2015-12-08 15:45:58 -070032
Ed Warnickecb9cada2015-12-08 15:45:58 -070033#define ikev2_set_state(sa, v) do { \
34 (sa)->state = v; \
35 clib_warning("sa state changed to " #v); \
36 } while(0);
37
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070038typedef struct
39{
Ed Warnickecb9cada2015-12-08 15:45:58 -070040 u32 next_index;
41 u32 sw_if_index;
42} ikev2_trace_t;
43
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070044static u8 *
45format_ikev2_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070046{
47 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070049 ikev2_trace_t *t = va_arg (*args, ikev2_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
51 s = format (s, "ikev2: sw_if_index %d, next index %d",
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070052 t->sw_if_index, t->next_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070053 return s;
54}
55
Jean-Mickael Guerin8941ec22016-03-04 14:14:21 +010056static vlib_node_registration_t ikev2_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
58#define foreach_ikev2_error \
59_(PROCESSED, "IKEv2 packets processed") \
60_(IKE_SA_INIT_RETRANSMIT, "IKE_SA_INIT retransmit ") \
61_(IKE_SA_INIT_IGNORE, "IKE_SA_INIT ignore (IKE SA already auth)") \
62_(IKE_REQ_RETRANSMIT, "IKE request retransmit") \
63_(IKE_REQ_IGNORE, "IKE request ignore (old msgid)") \
64_(NOT_IKEV2, "Non IKEv2 packets received")
65
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070066typedef enum
67{
Ed Warnickecb9cada2015-12-08 15:45:58 -070068#define _(sym,str) IKEV2_ERROR_##sym,
69 foreach_ikev2_error
70#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070071 IKEV2_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070072} ikev2_error_t;
73
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070074static char *ikev2_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070075#define _(sym,string) string,
76 foreach_ikev2_error
77#undef _
78};
79
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070080typedef enum
81{
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 IKEV2_NEXT_IP4_LOOKUP,
83 IKEV2_NEXT_ERROR_DROP,
84 IKEV2_N_NEXT,
85} ikev2_next_t;
86
87static ikev2_sa_transform_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070088ikev2_find_transform_data (ikev2_sa_transform_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -070089{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070090 ikev2_main_t *km = &ikev2_main;
91 ikev2_sa_transform_t *td;
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070093 vec_foreach (td, km->supported_transforms)
94 {
95 if (td->type != t->type)
96 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070098 if (td->transform_id != t->transform_id)
99 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700101 if (td->type == IKEV2_TRANSFORM_TYPE_ENCR)
102 {
103 if (vec_len (t->attrs) != 4 || t->attrs[0] != 0x80
104 || t->attrs[1] != 14)
105 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700107 if (((t->attrs[2] << 8 | t->attrs[3]) / 8) != td->key_len)
108 continue;
109 }
110 return td;
111 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 return 0;
113}
114
115static ikev2_sa_proposal_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700116ikev2_select_proposal (ikev2_sa_proposal_t * proposals,
117 ikev2_protocol_id_t prot_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700119 ikev2_sa_proposal_t *rv = 0;
120 ikev2_sa_proposal_t *proposal;
121 ikev2_sa_transform_t *transform, *new_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 u8 mandatory_bitmap, optional_bitmap;
123
124 if (prot_id == IKEV2_PROTOCOL_IKE)
125 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700126 mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
127 (1 << IKEV2_TRANSFORM_TYPE_PRF) |
128 (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
129 optional_bitmap = mandatory_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 }
131 else if (prot_id == IKEV2_PROTOCOL_ESP)
132 {
133 mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700134 (1 << IKEV2_TRANSFORM_TYPE_ESN);
135 optional_bitmap = mandatory_bitmap |
136 (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 }
138 else if (prot_id == IKEV2_PROTOCOL_AH)
139 {
140 mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_INTEG) |
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700141 (1 << IKEV2_TRANSFORM_TYPE_ESN);
142 optional_bitmap = mandatory_bitmap | (1 << IKEV2_TRANSFORM_TYPE_DH);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 }
144 else
145 return 0;
146
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700147 vec_add2 (rv, proposal, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700149 vec_foreach (proposal, proposals)
150 {
151 u8 bitmap = 0;
152 if (proposal->protocol_id != prot_id)
153 continue;
154
155 vec_foreach (transform, proposal->transforms)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700157 if ((1 << transform->type) & bitmap)
158 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700160 if (ikev2_find_transform_data (transform))
161 {
162 bitmap |= 1 << transform->type;
163 vec_add2 (rv->transforms, new_t, 1);
164 clib_memcpy (new_t, transform, sizeof (*new_t));
165 new_t->attrs = vec_dup (transform->attrs);
166 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 }
168
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700169 clib_warning ("bitmap is %x mandatory is %x optional is %x",
170 bitmap, mandatory_bitmap, optional_bitmap);
171
172 if ((bitmap & mandatory_bitmap) == mandatory_bitmap &&
173 (bitmap & ~optional_bitmap) == 0)
174 {
175 rv->proposal_num = proposal->proposal_num;
176 rv->protocol_id = proposal->protocol_id;
177 RAND_bytes ((u8 *) & rv->spi, sizeof (rv->spi));
178 goto done;
179 }
180 else
181 {
182 vec_free (rv->transforms);
183 }
184 }
185
186 vec_free (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187done:
188 return rv;
189}
190
191ikev2_sa_transform_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700192ikev2_sa_get_td_for_type (ikev2_sa_proposal_t * p,
193 ikev2_transform_type_t type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700195 ikev2_sa_transform_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
197 if (!p)
198 return 0;
199
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700200 vec_foreach (t, p->transforms)
201 {
202 if (t->type == type)
203 return ikev2_find_transform_data (t);
204 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205 return 0;
206}
207
208ikev2_child_sa_t *
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000209ikev2_sa_get_child (ikev2_sa_t * sa, u32 spi, ikev2_protocol_id_t prot_id,
210 int by_initiator)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700212 ikev2_child_sa_t *c;
213 vec_foreach (c, sa->childs)
214 {
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000215 ikev2_sa_proposal_t *proposal =
216 by_initiator ? &c->i_proposals[0] : &c->r_proposals[0];
217 if (proposal && proposal->spi == spi && proposal->protocol_id == prot_id)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700218 return c;
219 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
221 return 0;
222}
223
224void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700225ikev2_sa_free_proposal_vector (ikev2_sa_proposal_t ** v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700227 ikev2_sa_proposal_t *p;
228 ikev2_sa_transform_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229
230 if (!*v)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700231 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700233 vec_foreach (p, *v)
234 {
235 vec_foreach (t, p->transforms)
236 {
237 vec_free (t->attrs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700239 vec_free (p->transforms);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700241 vec_free (*v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242};
243
244static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700245ikev2_sa_free_all_child_sa (ikev2_child_sa_t ** childs)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700247 ikev2_child_sa_t *c;
248 vec_foreach (c, *childs)
249 {
250 ikev2_sa_free_proposal_vector (&c->r_proposals);
251 ikev2_sa_free_proposal_vector (&c->i_proposals);
252 vec_free (c->sk_ai);
253 vec_free (c->sk_ar);
254 vec_free (c->sk_ei);
255 vec_free (c->sk_er);
256 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700258 vec_free (*childs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259}
260
261static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700262ikev2_sa_del_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * child)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700264 ikev2_sa_free_proposal_vector (&child->r_proposals);
265 ikev2_sa_free_proposal_vector (&child->i_proposals);
266 vec_free (child->sk_ai);
267 vec_free (child->sk_ar);
268 vec_free (child->sk_ei);
269 vec_free (child->sk_er);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700271 vec_del1 (sa->childs, child - sa->childs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272}
273
274static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700275ikev2_sa_free_all_vec (ikev2_sa_t * sa)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700277 vec_free (sa->i_nonce);
278 vec_free (sa->i_dh_data);
279 vec_free (sa->dh_shared_key);
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000280 vec_free (sa->dh_private_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700282 ikev2_sa_free_proposal_vector (&sa->r_proposals);
283 ikev2_sa_free_proposal_vector (&sa->i_proposals);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700285 vec_free (sa->sk_d);
286 vec_free (sa->sk_ai);
287 vec_free (sa->sk_ar);
288 vec_free (sa->sk_ei);
289 vec_free (sa->sk_er);
290 vec_free (sa->sk_pi);
291 vec_free (sa->sk_pr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700293 vec_free (sa->i_id.data);
294 vec_free (sa->i_auth.data);
295 vec_free (sa->r_id.data);
296 vec_free (sa->r_auth.data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 if (sa->r_auth.key)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700298 EVP_PKEY_free (sa->r_auth.key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700300 vec_free (sa->del);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700302 ikev2_sa_free_all_child_sa (&sa->childs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303}
304
305static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700306ikev2_delete_sa (ikev2_sa_t * sa)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700308 ikev2_main_t *km = &ikev2_main;
Damjan Marion586afd72017-04-05 19:18:20 +0200309 u32 thread_index = vlib_get_thread_index ();
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700310 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700312 ikev2_sa_free_all_vec (sa);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
Damjan Marion586afd72017-04-05 19:18:20 +0200314 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi, sa->rspi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 if (p)
316 {
Damjan Marion586afd72017-04-05 19:18:20 +0200317 hash_unset (km->per_thread_data[thread_index].sa_by_rspi, sa->rspi);
318 pool_put (km->per_thread_data[thread_index].sas, sa);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319 }
320}
321
322static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700323ikev2_generate_sa_init_data (ikev2_sa_t * sa)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700325 ikev2_sa_transform_t *t = 0, *t2;
326 ikev2_main_t *km = &ikev2_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
328 if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
329 {
330 return;
331 }
332
333 /* check if received DH group is on our list of supported groups */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700334 vec_foreach (t2, km->supported_transforms)
335 {
336 if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
337 {
338 t = t2;
339 break;
340 }
341 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
343 if (!t)
344 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700345 clib_warning ("unknown dh data group %u (data len %u)", sa->dh_group,
346 vec_len (sa->i_dh_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347 sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
348 return;
349 }
350
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000351 if (sa->is_initiator)
352 {
353 /* generate rspi */
354 RAND_bytes ((u8 *) & sa->ispi, 8);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000356 /* generate nonce */
357 sa->i_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
358 RAND_bytes ((u8 *) sa->i_nonce, IKEV2_NONCE_SIZE);
359 }
360 else
361 {
362 /* generate rspi */
363 RAND_bytes ((u8 *) & sa->rspi, 8);
364
365 /* generate nonce */
366 sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
367 RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
368 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369
370 /* generate dh keys */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700371 ikev2_generate_dh (sa, t);
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000372
373}
374
375static void
376ikev2_complete_sa_data (ikev2_sa_t * sa, ikev2_sa_t * sai)
377{
378 ikev2_sa_transform_t *t = 0, *t2;
379 ikev2_main_t *km = &ikev2_main;
380
381
382 /*move some data to the new SA */
383#define _(A) ({void* __tmp__ = (A); (A) = 0; __tmp__;})
384 sa->i_nonce = _(sai->i_nonce);
385 sa->i_dh_data = _(sai->i_dh_data);
386 sa->dh_private_key = _(sai->dh_private_key);
387 sa->iaddr.as_u32 = sai->iaddr.as_u32;
388 sa->raddr.as_u32 = sai->raddr.as_u32;
389 sa->is_initiator = sai->is_initiator;
390 sa->profile = sai->profile;
391 sa->i_id.type = sai->i_id.type;
392 sa->i_id.data = _(sai->i_id.data);
393 sa->i_auth.method = sai->i_auth.method;
394 sa->i_auth.hex = sai->i_auth.hex;
395 sa->i_auth.data = _(sai->i_auth.data);
396 sa->i_auth.key = _(sai->i_auth.key);
397 sa->last_sa_init_req_packet_data = _(sai->last_sa_init_req_packet_data);
398 sa->childs = _(sai->childs);
399#undef _
400
401
402 if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
403 {
404 return;
405 }
406
407 /* check if received DH group is on our list of supported groups */
408 vec_foreach (t2, km->supported_transforms)
409 {
410 if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
411 {
412 t = t2;
413 break;
414 }
415 }
416
417 if (!t)
418 {
419 clib_warning ("unknown dh data group %u (data len %u)", sa->dh_group,
420 vec_len (sa->i_dh_data));
421 sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
422 return;
423 }
424
425
426 /* generate dh keys */
427 ikev2_complete_dh (sa, t);
428
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429}
430
431static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700432ikev2_calc_keys (ikev2_sa_t * sa)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700434 u8 *tmp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435 /* calculate SKEYSEED = prf(Ni | Nr, g^ir) */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700436 u8 *skeyseed = 0;
437 u8 *s = 0;
438 ikev2_sa_transform_t *tr_encr, *tr_prf, *tr_integ;
439 tr_encr =
440 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
441 tr_prf =
442 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
443 tr_integ =
444 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700446 vec_append (s, sa->i_nonce);
447 vec_append (s, sa->r_nonce);
448 skeyseed = ikev2_calc_prf (tr_prf, s, sa->dh_shared_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700450 /* Calculate S = Ni | Nr | SPIi | SPIr */
451 u64 *spi;
452 vec_add2 (s, tmp, 2 * sizeof (*spi));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 spi = (u64 *) tmp;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700454 spi[0] = clib_host_to_net_u64 (sa->ispi);
455 spi[1] = clib_host_to_net_u64 (sa->rspi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
457 /* calculate PRFplus */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700458 u8 *keymat;
459 int len = tr_prf->key_trunc + /* SK_d */
460 tr_integ->key_len * 2 + /* SK_ai, SK_ar */
461 tr_encr->key_len * 2 + /* SK_ei, SK_er */
462 tr_prf->key_len * 2; /* SK_pi, SK_pr */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700464 keymat = ikev2_calc_prfplus (tr_prf, skeyseed, s, len);
465 vec_free (skeyseed);
466 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467
468 int pos = 0;
469
470 /* SK_d */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700471 sa->sk_d = vec_new (u8, tr_prf->key_trunc);
472 clib_memcpy (sa->sk_d, keymat + pos, tr_prf->key_trunc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473 pos += tr_prf->key_trunc;
474
475 /* SK_ai */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700476 sa->sk_ai = vec_new (u8, tr_integ->key_len);
477 clib_memcpy (sa->sk_ai, keymat + pos, tr_integ->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 pos += tr_integ->key_len;
479
480 /* SK_ar */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700481 sa->sk_ar = vec_new (u8, tr_integ->key_len);
482 clib_memcpy (sa->sk_ar, keymat + pos, tr_integ->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483 pos += tr_integ->key_len;
484
485 /* SK_ei */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700486 sa->sk_ei = vec_new (u8, tr_encr->key_len);
487 clib_memcpy (sa->sk_ei, keymat + pos, tr_encr->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 pos += tr_encr->key_len;
489
490 /* SK_er */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700491 sa->sk_er = vec_new (u8, tr_encr->key_len);
492 clib_memcpy (sa->sk_er, keymat + pos, tr_encr->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 pos += tr_encr->key_len;
494
495 /* SK_pi */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700496 sa->sk_pi = vec_new (u8, tr_prf->key_len);
497 clib_memcpy (sa->sk_pi, keymat + pos, tr_prf->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498 pos += tr_prf->key_len;
499
500 /* SK_pr */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700501 sa->sk_pr = vec_new (u8, tr_prf->key_len);
502 clib_memcpy (sa->sk_pr, keymat + pos, tr_prf->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 pos += tr_prf->key_len;
504
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700505 vec_free (keymat);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506}
507
508static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700509ikev2_calc_child_keys (ikev2_sa_t * sa, ikev2_child_sa_t * child)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700511 u8 *s = 0;
512 ikev2_sa_transform_t *tr_prf, *ctr_encr, *ctr_integ;
513 tr_prf =
514 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
515 ctr_encr =
516 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
517 ctr_integ =
518 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700520 vec_append (s, sa->i_nonce);
521 vec_append (s, sa->r_nonce);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522 /* calculate PRFplus */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700523 u8 *keymat;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524 int len = ctr_encr->key_len * 2 + ctr_integ->key_len * 2;
525
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700526 keymat = ikev2_calc_prfplus (tr_prf, sa->sk_d, s, len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
528 int pos = 0;
529
530 /* SK_ei */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700531 child->sk_ei = vec_new (u8, ctr_encr->key_len);
532 clib_memcpy (child->sk_ei, keymat + pos, ctr_encr->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533 pos += ctr_encr->key_len;
534
535 /* SK_ai */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700536 child->sk_ai = vec_new (u8, ctr_integ->key_len);
537 clib_memcpy (child->sk_ai, keymat + pos, ctr_integ->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 pos += ctr_integ->key_len;
539
540 /* SK_er */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700541 child->sk_er = vec_new (u8, ctr_encr->key_len);
542 clib_memcpy (child->sk_er, keymat + pos, ctr_encr->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 pos += ctr_encr->key_len;
544
545 /* SK_ar */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700546 child->sk_ar = vec_new (u8, ctr_integ->key_len);
547 clib_memcpy (child->sk_ar, keymat + pos, ctr_integ->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548 pos += ctr_integ->key_len;
549
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700550 ASSERT (pos == len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700552 vec_free (keymat);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553}
554
555static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700556ikev2_process_sa_init_req (vlib_main_t * vm, ikev2_sa_t * sa,
557 ike_header_t * ike)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558{
559 int p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700560 u32 len = clib_net_to_host_u32 (ike->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561 u8 payload = ike->nextpayload;
562
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700563 clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
564 "exchange %x flags %x msgid %x length %u",
565 clib_net_to_host_u64 (ike->ispi),
566 clib_net_to_host_u64 (ike->rspi),
567 payload, ike->version,
568 ike->exchange, ike->flags,
569 clib_net_to_host_u32 (ike->msgid), len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700571 sa->ispi = clib_net_to_host_u64 (ike->ispi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572
573 /* store whole IKE payload - needed for PSK auth */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700574 vec_free (sa->last_sa_init_req_packet_data);
575 vec_add (sa->last_sa_init_req_packet_data, ike, len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700577 while (p < len && payload != IKEV2_PAYLOAD_NONE)
578 {
579 ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
580 u32 plen = clib_net_to_host_u16 (ikep->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700582 if (plen < sizeof (ike_payload_header_t))
583 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700585 if (payload == IKEV2_PAYLOAD_SA)
586 {
587 ikev2_sa_free_proposal_vector (&sa->i_proposals);
588 sa->i_proposals = ikev2_parse_sa_payload (ikep);
589 }
590 else if (payload == IKEV2_PAYLOAD_KE)
591 {
592 ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
593 sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
594 vec_free (sa->i_dh_data);
595 vec_add (sa->i_dh_data, ke->payload, plen - sizeof (*ke));
596 }
597 else if (payload == IKEV2_PAYLOAD_NONCE)
598 {
599 vec_free (sa->i_nonce);
600 vec_add (sa->i_nonce, ikep->payload, plen - sizeof (*ikep));
601 }
602 else if (payload == IKEV2_PAYLOAD_NOTIFY)
603 {
604 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
605 vec_free (n);
606 }
607 else if (payload == IKEV2_PAYLOAD_VENDOR)
608 {
609 ikev2_parse_vendor_payload (ikep);
610 }
611 else
612 {
613 clib_warning ("unknown payload %u flags %x length %u", payload,
614 ikep->flags, plen);
615 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
616 {
617 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
618 sa->unsupported_cp = payload;
619 return;
620 }
621 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700623 payload = ikep->nextpayload;
624 p += plen;
625 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700627 ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628}
629
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000630static void
631ikev2_process_sa_init_resp (vlib_main_t * vm, ikev2_sa_t * sa,
632 ike_header_t * ike)
633{
634 int p = 0;
635 u32 len = clib_net_to_host_u32 (ike->length);
636 u8 payload = ike->nextpayload;
637
638 clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
639 "exchange %x flags %x msgid %x length %u",
640 clib_net_to_host_u64 (ike->ispi),
641 clib_net_to_host_u64 (ike->rspi),
642 payload, ike->version,
643 ike->exchange, ike->flags,
644 clib_net_to_host_u32 (ike->msgid), len);
645
646 sa->ispi = clib_net_to_host_u64 (ike->ispi);
647 sa->rspi = clib_net_to_host_u64 (ike->rspi);
648
649 /* store whole IKE payload - needed for PSK auth */
650 vec_free (sa->last_sa_init_res_packet_data);
651 vec_add (sa->last_sa_init_res_packet_data, ike, len);
652
653 while (p < len && payload != IKEV2_PAYLOAD_NONE)
654 {
655 ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
656 u32 plen = clib_net_to_host_u16 (ikep->length);
657
658 if (plen < sizeof (ike_payload_header_t))
659 return;
660
661 if (payload == IKEV2_PAYLOAD_SA)
662 {
663 ikev2_sa_free_proposal_vector (&sa->r_proposals);
664 sa->r_proposals = ikev2_parse_sa_payload (ikep);
665 if (sa->r_proposals)
666 {
667 ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
668 ike->msgid =
669 clib_host_to_net_u32 (clib_net_to_host_u32 (ike->msgid) + 1);
670 }
671 }
672 else if (payload == IKEV2_PAYLOAD_KE)
673 {
674 ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
675 sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
676 vec_free (sa->r_dh_data);
677 vec_add (sa->r_dh_data, ke->payload, plen - sizeof (*ke));
678 }
679 else if (payload == IKEV2_PAYLOAD_NONCE)
680 {
681 vec_free (sa->r_nonce);
682 vec_add (sa->r_nonce, ikep->payload, plen - sizeof (*ikep));
683 }
684 else if (payload == IKEV2_PAYLOAD_NOTIFY)
685 {
686 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
687 vec_free (n);
688 }
689 else if (payload == IKEV2_PAYLOAD_VENDOR)
690 {
691 ikev2_parse_vendor_payload (ikep);
692 }
693 else
694 {
695 clib_warning ("unknown payload %u flags %x length %u", payload,
696 ikep->flags, plen);
697 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
698 {
699 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
700 sa->unsupported_cp = payload;
701 return;
702 }
703 }
704
705 payload = ikep->nextpayload;
706 p += plen;
707 }
708}
709
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710static u8 *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700711ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike, u8 * payload)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712{
713 int p = 0;
714 u8 last_payload = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700715 u8 *hmac = 0;
716 u32 len = clib_net_to_host_u32 (ike->length);
717 ike_payload_header_t *ikep = 0;
Benoît Ganned5304452016-04-08 22:25:05 -0700718 u32 plen = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700719 ikev2_sa_transform_t *tr_integ;
720 tr_integ =
721 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722
723 while (p < len &&
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700724 *payload != IKEV2_PAYLOAD_NONE && last_payload != IKEV2_PAYLOAD_SK)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700726 ikep = (ike_payload_header_t *) & ike->payload[p];
727 plen = clib_net_to_host_u16 (ikep->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700729 if (plen < sizeof (*ikep))
730 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731
732 if (*payload == IKEV2_PAYLOAD_SK)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700733 {
734 clib_warning ("received IKEv2 payload SK, len %u", plen - 4);
735 last_payload = *payload;
736 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700738 {
739 clib_warning ("unknown payload %u flags %x length %u", payload,
740 ikep->flags, plen);
741 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
742 {
743 sa->unsupported_cp = *payload;
744 return 0;
745 }
746 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700748 *payload = ikep->nextpayload;
749 p += plen;
750 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700752 if (last_payload != IKEV2_PAYLOAD_SK)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700754 clib_warning ("Last payload must be SK");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755 return 0;
756 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000758 hmac =
759 ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ar : sa->sk_ai,
760 (u8 *) ike, len - tr_integ->key_trunc);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700761
762 plen = plen - sizeof (*ikep) - tr_integ->key_trunc;
763
764 if (memcmp (hmac, &ikep->payload[plen], tr_integ->key_trunc))
765 {
766 clib_warning ("message integrity check failed");
767 vec_free (hmac);
768 return 0;
769 }
770 vec_free (hmac);
771
772 return ikev2_decrypt_data (sa, ikep->payload, plen);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773}
774
775static void
776ikev2_initial_contact_cleanup (ikev2_sa_t * sa)
777{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700778 ikev2_main_t *km = &ikev2_main;
779 ikev2_sa_t *tmp;
780 u32 i, *delete = 0;
781 ikev2_child_sa_t *c;
Damjan Marion586afd72017-04-05 19:18:20 +0200782 u32 thread_index = vlib_get_thread_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783
784 if (!sa->initial_contact)
785 return;
786
787 /* find old IKE SAs with the same authenticated identity */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700788 /* *INDENT-OFF* */
Damjan Marion586afd72017-04-05 19:18:20 +0200789 pool_foreach (tmp, km->per_thread_data[thread_index].sas, ({
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790 if (tmp->i_id.type != sa->i_id.type ||
791 vec_len(tmp->i_id.data) != vec_len(sa->i_id.data) ||
792 memcmp(sa->i_id.data, tmp->i_id.data, vec_len(sa->i_id.data)))
793 continue;
794
795 if (sa->rspi != tmp->rspi)
Damjan Marion586afd72017-04-05 19:18:20 +0200796 vec_add1(delete, tmp - km->per_thread_data[thread_index].sas);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700798 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700800 for (i = 0; i < vec_len (delete); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801 {
Damjan Marion586afd72017-04-05 19:18:20 +0200802 tmp =
803 pool_elt_at_index (km->per_thread_data[thread_index].sas, delete[i]);
804 vec_foreach (c,
805 tmp->childs) ikev2_delete_tunnel_interface (km->vnet_main,
806 tmp, c);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700807 ikev2_delete_sa (tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700808 }
809
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700810 vec_free (delete);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700811 sa->initial_contact = 0;
812}
813
814static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700815ikev2_process_auth_req (vlib_main_t * vm, ikev2_sa_t * sa, ike_header_t * ike)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700816{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700817 ikev2_child_sa_t *first_child_sa;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818 int p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700819 u32 len = clib_net_to_host_u32 (ike->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700820 u8 payload = ike->nextpayload;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700821 u8 *plaintext = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700822
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700823 ike_payload_header_t *ikep;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824 u32 plen;
825
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700826 clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
827 "exchange %x flags %x msgid %x length %u",
828 clib_net_to_host_u64 (ike->ispi),
829 clib_net_to_host_u64 (ike->rspi),
830 payload, ike->version,
831 ike->exchange, ike->flags,
832 clib_net_to_host_u32 (ike->msgid), len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700833
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700834 ikev2_calc_keys (sa);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700836 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837
838 if (!plaintext)
839 {
840 if (sa->unsupported_cp)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700841 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842 goto cleanup_and_exit;
843 }
844
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000845 /* select or create 1st child SA */
846 if (sa->is_initiator)
847 {
848 first_child_sa = &sa->childs[0];
849 }
850 else
851 {
852 ikev2_sa_free_all_child_sa (&sa->childs);
853 vec_add2 (sa->childs, first_child_sa, 1);
854 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700855
856
857 /* process encrypted payload */
858 p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700859 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700860 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700861 ikep = (ike_payload_header_t *) & plaintext[p];
862 plen = clib_net_to_host_u16 (ikep->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700864 if (plen < sizeof (ike_payload_header_t))
865 goto cleanup_and_exit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700866
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700867 if (payload == IKEV2_PAYLOAD_SA) /* 33 */
868 {
869 clib_warning ("received payload SA, len %u", plen - sizeof (*ikep));
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000870 if (sa->is_initiator)
871 {
872 ikev2_sa_free_proposal_vector (&first_child_sa->r_proposals);
873 first_child_sa->r_proposals = ikev2_parse_sa_payload (ikep);
874 }
875 else
876 {
877 ikev2_sa_free_proposal_vector (&first_child_sa->i_proposals);
878 first_child_sa->i_proposals = ikev2_parse_sa_payload (ikep);
879 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700880 }
Matthew Smith1e19ee42017-04-26 15:42:39 -0500881 else if (payload == IKEV2_PAYLOAD_IDI) /* 35 */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700882 {
883 ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884
Matthew Smith1e19ee42017-04-26 15:42:39 -0500885 sa->i_id.type = id->id_type;
886 vec_free (sa->i_id.data);
887 vec_add (sa->i_id.data, id->payload, plen - sizeof (*id));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888
Matthew Smith1e19ee42017-04-26 15:42:39 -0500889 clib_warning ("received payload IDi, len %u id_type %u",
890 plen - sizeof (*id), id->id_type);
891 }
892 else if (payload == IKEV2_PAYLOAD_IDR) /* 36 */
893 {
894 ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
895
896 sa->r_id.type = id->id_type;
897 vec_free (sa->r_id.data);
898 vec_add (sa->r_id.data, id->payload, plen - sizeof (*id));
899
900 clib_warning ("received payload IDr len %u id_type %u",
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700901 plen - sizeof (*id), id->id_type);
902 }
903 else if (payload == IKEV2_PAYLOAD_AUTH) /* 39 */
904 {
905 ike_auth_payload_header_t *a = (ike_auth_payload_header_t *) ikep;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906
Radu Nicolaucb33dc22017-02-16 16:49:46 +0000907 if (sa->is_initiator)
908 {
909 sa->r_auth.method = a->auth_method;
910 vec_free (sa->r_auth.data);
911 vec_add (sa->r_auth.data, a->payload, plen - sizeof (*a));
912 }
913 else
914 {
915 sa->i_auth.method = a->auth_method;
916 vec_free (sa->i_auth.data);
917 vec_add (sa->i_auth.data, a->payload, plen - sizeof (*a));
918 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700920 clib_warning ("received payload AUTH, len %u auth_type %u",
921 plen - sizeof (*a), a->auth_method);
922 }
923 else if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
924 {
925 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
926 if (n->msg_type == IKEV2_NOTIFY_MSG_INITIAL_CONTACT)
927 {
928 sa->initial_contact = 1;
929 }
930 vec_free (n);
931 }
932 else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
933 {
934 ikev2_parse_vendor_payload (ikep);
935 }
936 else if (payload == IKEV2_PAYLOAD_TSI) /* 44 */
937 {
938 clib_warning ("received payload TSi, len %u",
939 plen - sizeof (*ikep));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700941 vec_free (first_child_sa->tsi);
942 first_child_sa->tsi = ikev2_parse_ts_payload (ikep);
943 }
944 else if (payload == IKEV2_PAYLOAD_TSR) /* 45 */
945 {
946 clib_warning ("received payload TSr, len %u",
947 plen - sizeof (*ikep));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700949 vec_free (first_child_sa->tsr);
950 first_child_sa->tsr = ikev2_parse_ts_payload (ikep);
951 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700953 {
954 clib_warning ("unknown payload %u flags %x length %u data %u",
955 payload, ikep->flags, plen - 4,
956 format_hex_bytes, ikep->payload, plen - 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700958 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
959 {
960 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
961 sa->unsupported_cp = payload;
962 return;
963 }
964 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700965
966 payload = ikep->nextpayload;
967 p += plen;
968 }
969
970cleanup_and_exit:
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700971 vec_free (plaintext);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972}
973
974static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700975ikev2_process_informational_req (vlib_main_t * vm, ikev2_sa_t * sa,
976 ike_header_t * ike)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700977{
978 int p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700979 u32 len = clib_net_to_host_u32 (ike->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980 u8 payload = ike->nextpayload;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700981 u8 *plaintext = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700983 ike_payload_header_t *ikep;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984 u32 plen;
985
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700986 clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
987 "exchange %x flags %x msgid %x length %u",
988 clib_net_to_host_u64 (ike->ispi),
989 clib_net_to_host_u64 (ike->rspi),
990 payload, ike->version,
991 ike->exchange, ike->flags,
992 clib_net_to_host_u32 (ike->msgid), len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700993
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700994 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995
996 if (!plaintext)
997 goto cleanup_and_exit;
998
999 /* process encrypted payload */
1000 p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001001 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001003 ikep = (ike_payload_header_t *) & plaintext[p];
1004 plen = clib_net_to_host_u16 (ikep->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001006 if (plen < sizeof (ike_payload_header_t))
1007 goto cleanup_and_exit;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001009 if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
1010 {
1011 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
1012 if (n->msg_type == IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED)
1013 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1014 vec_free (n);
1015 }
1016 else if (payload == IKEV2_PAYLOAD_DELETE) /* 42 */
1017 {
1018 sa->del = ikev2_parse_delete_payload (ikep);
1019 }
1020 else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
1021 {
1022 ikev2_parse_vendor_payload (ikep);
1023 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001025 {
1026 clib_warning ("unknown payload %u flags %x length %u data %u",
1027 payload, ikep->flags, plen - 4,
1028 format_hex_bytes, ikep->payload, plen - 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001030 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1031 {
1032 sa->unsupported_cp = payload;
1033 return;
1034 }
1035 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036
1037 payload = ikep->nextpayload;
1038 p += plen;
1039 }
1040
1041cleanup_and_exit:
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001042 vec_free (plaintext);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001043}
1044
1045static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001046ikev2_process_create_child_sa_req (vlib_main_t * vm, ikev2_sa_t * sa,
1047 ike_header_t * ike)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048{
1049 int p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001050 u32 len = clib_net_to_host_u32 (ike->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051 u8 payload = ike->nextpayload;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001052 u8 *plaintext = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001053 u8 rekeying = 0;
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001054 u8 nonce[IKEV2_NONCE_SIZE];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001055
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001056 ike_payload_header_t *ikep;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001057 u32 plen;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001058 ikev2_notify_t *n = 0;
1059 ikev2_ts_t *tsi = 0;
1060 ikev2_ts_t *tsr = 0;
1061 ikev2_sa_proposal_t *proposal = 0;
1062 ikev2_child_sa_t *child_sa;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001063
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001064 clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
1065 "exchange %x flags %x msgid %x length %u",
1066 clib_net_to_host_u64 (ike->ispi),
1067 clib_net_to_host_u64 (ike->rspi),
1068 payload, ike->version,
1069 ike->exchange, ike->flags,
1070 clib_net_to_host_u32 (ike->msgid), len);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001071
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001072 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001073
1074 if (!plaintext)
1075 goto cleanup_and_exit;
1076
1077 /* process encrypted payload */
1078 p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001079 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001080 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001081 ikep = (ike_payload_header_t *) & plaintext[p];
1082 plen = clib_net_to_host_u16 (ikep->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001083
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001084 if (plen < sizeof (ike_payload_header_t))
1085 goto cleanup_and_exit;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001086
1087 else if (payload == IKEV2_PAYLOAD_SA)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001088 {
1089 proposal = ikev2_parse_sa_payload (ikep);
1090 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001091 else if (payload == IKEV2_PAYLOAD_NOTIFY)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001092 {
1093 n = ikev2_parse_notify_payload (ikep);
1094 if (n->msg_type == IKEV2_NOTIFY_MSG_REKEY_SA)
1095 {
1096 rekeying = 1;
1097 }
1098 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099 else if (payload == IKEV2_PAYLOAD_DELETE)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001100 {
1101 sa->del = ikev2_parse_delete_payload (ikep);
1102 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103 else if (payload == IKEV2_PAYLOAD_VENDOR)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001104 {
1105 ikev2_parse_vendor_payload (ikep);
1106 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001107 else if (payload == IKEV2_PAYLOAD_NONCE)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001108 {
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001109 clib_memcpy (nonce, ikep->payload, plen - sizeof (*ikep));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001110 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001111 else if (payload == IKEV2_PAYLOAD_TSI)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001112 {
1113 tsi = ikev2_parse_ts_payload (ikep);
1114 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115 else if (payload == IKEV2_PAYLOAD_TSR)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001116 {
1117 tsr = ikev2_parse_ts_payload (ikep);
1118 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001120 {
1121 clib_warning ("unknown payload %u flags %x length %u data %u",
1122 payload, ikep->flags, plen - 4,
1123 format_hex_bytes, ikep->payload, plen - 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001125 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
1126 {
1127 sa->unsupported_cp = payload;
1128 return;
1129 }
1130 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131
1132 payload = ikep->nextpayload;
1133 p += plen;
1134 }
1135
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001136 if (sa->is_initiator && proposal->protocol_id == IKEV2_PROTOCOL_ESP)
1137 {
1138 ikev2_rekey_t *rekey = &sa->rekey[0];
1139 rekey->protocol_id = proposal->protocol_id;
1140 rekey->i_proposal =
1141 ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
1142 rekey->i_proposal->spi = rekey->spi;
1143 rekey->r_proposal = proposal;
1144 rekey->tsi = tsi;
1145 rekey->tsr = tsr;
1146 /* update Nr */
1147 vec_free (sa->r_nonce);
1148 vec_add (sa->r_nonce, nonce, IKEV2_NONCE_SIZE);
1149 child_sa = ikev2_sa_get_child (sa, rekey->ispi, IKEV2_PROTOCOL_ESP, 1);
1150 if (child_sa)
1151 {
1152 child_sa->rekey_retries = 0;
1153 }
1154 }
1155 else if (rekeying)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001156 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001157 ikev2_rekey_t *rekey;
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001158 child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159 if (!child_sa)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001160 {
1161 clib_warning ("child SA spi %lx not found", n->spi);
1162 goto cleanup_and_exit;
1163 }
1164 vec_add2 (sa->rekey, rekey, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165 rekey->protocol_id = n->protocol_id;
1166 rekey->spi = n->spi;
1167 rekey->i_proposal = proposal;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001168 rekey->r_proposal =
1169 ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001170 rekey->tsi = tsi;
1171 rekey->tsr = tsr;
1172 /* update Ni */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001173 vec_free (sa->i_nonce);
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001174 vec_add (sa->i_nonce, nonce, IKEV2_NONCE_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175 /* generate new Nr */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001176 vec_free (sa->r_nonce);
1177 sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
1178 RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179 }
1180
1181cleanup_and_exit:
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001182 vec_free (plaintext);
1183 vec_free (n);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184}
1185
1186static u8 *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001187ikev2_sa_generate_authmsg (ikev2_sa_t * sa, int is_responder)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001189 u8 *authmsg = 0;
1190 u8 *data;
1191 u8 *nonce;
1192 ikev2_id_t *id;
1193 u8 *key;
1194 u8 *packet_data;
1195 ikev2_sa_transform_t *tr_prf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001196
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001197 tr_prf =
1198 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199
1200 if (is_responder)
1201 {
1202 id = &sa->r_id;
1203 key = sa->sk_pr;
1204 nonce = sa->i_nonce;
1205 packet_data = sa->last_sa_init_res_packet_data;
1206 }
1207 else
1208 {
1209 id = &sa->i_id;
1210 key = sa->sk_pi;
1211 nonce = sa->r_nonce;
1212 packet_data = sa->last_sa_init_req_packet_data;
1213 }
1214
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001215 data = vec_new (u8, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001216 data[0] = id->type;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001217 vec_append (data, id->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001218
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001219 u8 *id_hash = ikev2_calc_prf (tr_prf, key, data);
1220 vec_append (authmsg, packet_data);
1221 vec_append (authmsg, nonce);
1222 vec_append (authmsg, id_hash);
1223 vec_free (id_hash);
1224 vec_free (data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225
1226 return authmsg;
1227}
1228
1229static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001230ikev2_ts_cmp (ikev2_ts_t * ts1, ikev2_ts_t * ts2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001231{
1232 if (ts1->ts_type == ts2->ts_type && ts1->protocol_id == ts2->protocol_id &&
1233 ts1->start_port == ts2->start_port && ts1->end_port == ts2->end_port &&
1234 ts1->start_addr.as_u32 == ts2->start_addr.as_u32 &&
1235 ts1->end_addr.as_u32 == ts2->end_addr.as_u32)
1236 return 1;
1237
1238 return 0;
1239}
1240
1241static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001242ikev2_sa_match_ts (ikev2_sa_t * sa)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001243{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001244 ikev2_main_t *km = &ikev2_main;
1245 ikev2_profile_t *p;
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001246 ikev2_ts_t *ts, *p_tsi, *p_tsr, *tsi = 0, *tsr = 0;
1247 ikev2_id_t *id;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001249 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250 pool_foreach (p, km->profiles, ({
1251
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001252 if (sa->is_initiator)
1253 {
1254 p_tsi = &p->loc_ts;
1255 p_tsr = &p->rem_ts;
1256 id = &sa->r_id;
1257 }
1258 else
1259 {
1260 p_tsi = &p->rem_ts;
1261 p_tsr = &p->loc_ts;
1262 id = &sa->i_id;
1263 }
1264
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265 /* check id */
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001266 if (p->rem_id.type != id->type ||
1267 vec_len(p->rem_id.data) != vec_len(id->data) ||
1268 memcmp(p->rem_id.data, id->data, vec_len(p->rem_id.data)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269 continue;
1270
1271 vec_foreach(ts, sa->childs[0].tsi)
1272 {
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001273 if (ikev2_ts_cmp(p_tsi, ts))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001274 {
1275 tsi = vec_dup(ts);
1276 break;
1277 }
1278 }
1279
1280 vec_foreach(ts, sa->childs[0].tsr)
1281 {
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001282 if (ikev2_ts_cmp(p_tsr, ts))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283 {
1284 tsr = vec_dup(ts);
1285 break;
1286 }
1287 }
1288
1289 break;
1290 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001291 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001292
1293 if (tsi && tsr)
1294 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001295 vec_free (sa->childs[0].tsi);
1296 vec_free (sa->childs[0].tsr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001297 sa->childs[0].tsi = tsi;
1298 sa->childs[0].tsr = tsr;
1299 }
1300 else
1301 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001302 vec_free (tsi);
1303 vec_free (tsr);
1304 ikev2_set_state (sa, IKEV2_STATE_TS_UNACCEPTABLE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001305 }
1306}
1307
1308static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001309ikev2_sa_auth (ikev2_sa_t * sa)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001310{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001311 ikev2_main_t *km = &ikev2_main;
1312 ikev2_profile_t *p, *sel_p = 0;
1313 u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1314 ikev2_sa_transform_t *tr_prf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001315
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001316 tr_prf =
1317 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001318
1319 /* only shared key and rsa signature */
1320 if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001321 sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001322 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001323 clib_warning ("unsupported authentication method %u",
1324 sa->i_auth.method);
1325 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001326 return;
1327 }
1328
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001329 key_pad = format (0, "%s", IKEV2_KEY_PAD);
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001330 authmsg = ikev2_sa_generate_authmsg (sa, sa->is_initiator);
1331
1332 ikev2_id_t *sa_id;
1333 ikev2_auth_t *sa_auth;
1334
1335 if (sa->is_initiator)
1336 {
1337 sa_id = &sa->r_id;
1338 sa_auth = &sa->r_auth;
1339 }
1340 else
1341 {
1342 sa_id = &sa->i_id;
1343 sa_auth = &sa->i_auth;
1344 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001345
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001346 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001347 pool_foreach (p, km->profiles, ({
1348
1349 /* check id */
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001350 if (p->rem_id.type != sa_id->type ||
1351 vec_len(p->rem_id.data) != vec_len(sa_id->data) ||
1352 memcmp(p->rem_id.data, sa_id->data, vec_len(p->rem_id.data)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001353 continue;
1354
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001355 if (sa_auth->method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001356 {
1357 if (!p->auth.data ||
1358 p->auth.method != IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1359 continue;
1360
1361 psk = ikev2_calc_prf(tr_prf, p->auth.data, key_pad);
1362 auth = ikev2_calc_prf(tr_prf, psk, authmsg);
1363
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001364 if (!memcmp(auth, sa_auth->data, vec_len(sa_auth->data)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001365 {
1366 ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1367 vec_free(auth);
1368 sel_p = p;
1369 break;
1370 }
1371
1372 }
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001373 else if (sa_auth->method == IKEV2_AUTH_METHOD_RSA_SIG)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001374 {
1375 if (p->auth.method != IKEV2_AUTH_METHOD_RSA_SIG)
1376 continue;
1377
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001378 if (ikev2_verify_sign(p->auth.key, sa_auth->data, authmsg) == 1)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001379 {
1380 ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1381 sel_p = p;
1382 break;
1383 }
1384 }
1385
1386 vec_free(auth);
1387 vec_free(psk);
1388 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001389 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001390
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001391 vec_free (authmsg);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001392
1393 if (sa->state == IKEV2_STATE_AUTHENTICATED)
1394 {
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001395 if (!sa->is_initiator)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001396 {
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001397 vec_free (sa->r_id.data);
1398 sa->r_id.data = vec_dup (sel_p->loc_id.data);
1399 sa->r_id.type = sel_p->loc_id.type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001400
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001401 /* generate our auth data */
1402 authmsg = ikev2_sa_generate_authmsg (sa, 1);
1403 if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1404 {
1405 sa->r_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1406 sa->r_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1407 }
1408 else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1409 {
1410 sa->r_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1411 sa->r_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1412 }
1413 vec_free (authmsg);
1414
1415 /* select transforms for 1st child sa */
1416 ikev2_sa_free_proposal_vector (&sa->childs[0].r_proposals);
1417 sa->childs[0].r_proposals =
1418 ikev2_select_proposal (sa->childs[0].i_proposals,
1419 IKEV2_PROTOCOL_ESP);
1420 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001421 }
1422 else
1423 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001424 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001425 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001426 vec_free (psk);
1427 vec_free (key_pad);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001428}
1429
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001430
1431static void
1432ikev2_sa_auth_init (ikev2_sa_t * sa)
1433{
1434 ikev2_main_t *km = &ikev2_main;
1435 u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1436 ikev2_sa_transform_t *tr_prf;
1437
1438 tr_prf =
1439 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
1440
1441 /* only shared key and rsa signature */
1442 if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
1443 sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
1444 {
1445 clib_warning ("unsupported authentication method %u",
1446 sa->i_auth.method);
1447 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
1448 return;
1449 }
1450
1451 key_pad = format (0, "%s", IKEV2_KEY_PAD);
1452 authmsg = ikev2_sa_generate_authmsg (sa, 0);
1453 psk = ikev2_calc_prf (tr_prf, sa->i_auth.data, key_pad);
1454 auth = ikev2_calc_prf (tr_prf, psk, authmsg);
1455
1456
1457 if (sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1458 {
1459 sa->i_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1460 sa->i_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1461 }
1462 else if (sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1463 {
1464 sa->i_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1465 sa->i_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1466 }
1467
1468 vec_free (psk);
1469 vec_free (key_pad);
1470 vec_free (auth);
1471 vec_free (authmsg);
1472}
1473
1474
Ed Warnickecb9cada2015-12-08 15:45:58 -07001475static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001476ikev2_create_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
1477 ikev2_child_sa_t * child)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001478{
1479 ipsec_add_del_tunnel_args_t a;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001480 ikev2_sa_transform_t *tr;
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001481 ikev2_sa_proposal_t *proposals;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001482 u8 encr_type = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001483
1484 if (!child->r_proposals)
1485 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001486 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001487 return 1;
1488 }
1489
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001490 memset (&a, 0, sizeof (a));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001491 a.is_add = 1;
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001492 if (sa->is_initiator)
1493 {
1494 a.local_ip.as_u32 = sa->iaddr.as_u32;
1495 a.remote_ip.as_u32 = sa->raddr.as_u32;
1496 proposals = child->i_proposals;
1497 a.local_spi = child->r_proposals[0].spi;
1498 a.remote_spi = child->i_proposals[0].spi;
1499 }
1500 else
1501 {
1502 a.local_ip.as_u32 = sa->raddr.as_u32;
1503 a.remote_ip.as_u32 = sa->iaddr.as_u32;
1504 proposals = child->r_proposals;
1505 a.local_spi = child->i_proposals[0].spi;
1506 a.remote_spi = child->r_proposals[0].spi;
1507 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001508 a.anti_replay = 1;
1509
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001510 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ESN);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001511 if (tr)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001512 a.esn = tr->esn_type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001513 else
1514 a.esn = 0;
1515
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001516 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ENCR);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001517 if (tr)
1518 {
1519 if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001520 {
1521 switch (tr->key_len)
1522 {
1523 case 16:
1524 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_128;
1525 break;
1526 case 24:
1527 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_192;
1528 break;
1529 case 32:
1530 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_256;
1531 break;
1532 default:
1533 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1534 return 1;
1535 break;
1536 }
1537 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001538 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001539 {
1540 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1541 return 1;
1542 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001543 }
1544 else
1545 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001546 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001547 return 1;
1548 }
1549
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001550 tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_INTEG);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001551 if (tr)
1552 {
1553 if (tr->integ_type != IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001554 {
1555 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1556 return 1;
1557 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001558 }
1559 else
1560 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001561 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001562 return 1;
1563 }
1564
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001565 ikev2_calc_child_keys (sa, child);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001566
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001567 u8 *loc_ckey, *rem_ckey, *loc_ikey, *rem_ikey;
1568 if (sa->is_initiator)
1569 {
1570 loc_ikey = child->sk_ai;
1571 rem_ikey = child->sk_ar;
1572 loc_ckey = child->sk_ei;
1573 rem_ckey = child->sk_er;
1574 }
1575 else
1576 {
1577 loc_ikey = child->sk_ar;
1578 rem_ikey = child->sk_ai;
1579 loc_ckey = child->sk_er;
1580 rem_ckey = child->sk_ei;
1581 }
1582
Matthew Smith2838a232016-06-21 16:05:09 -05001583 a.integ_alg = IPSEC_INTEG_ALG_SHA1_96;
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001584 a.local_integ_key_len = vec_len (loc_ikey);
1585 clib_memcpy (a.local_integ_key, loc_ikey, a.local_integ_key_len);
1586 a.remote_integ_key_len = vec_len (rem_ikey);
1587 clib_memcpy (a.remote_integ_key, rem_ikey, a.remote_integ_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001588
Matthew Smith2838a232016-06-21 16:05:09 -05001589 a.crypto_alg = encr_type;
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001590 a.local_crypto_key_len = vec_len (loc_ckey);
1591 clib_memcpy (a.local_crypto_key, loc_ckey, a.local_crypto_key_len);
1592 a.remote_crypto_key_len = vec_len (rem_ckey);
1593 clib_memcpy (a.remote_crypto_key, rem_ckey, a.remote_crypto_key_len);
1594
1595 if (sa->profile && sa->profile->lifetime)
1596 {
1597 child->time_to_expiration = vlib_time_now (vnm->vlib_main)
1598 + sa->profile->lifetime;
1599 if (sa->profile->lifetime_jitter)
1600 {
Chris Lukeab7b8d92017-09-07 07:40:13 -04001601 // This is not much better than rand(3), which Coverity warns
1602 // is unsuitable for security applications; random_u32 is
1603 // however fast. If this perturbance to the expiration time
1604 // needs to use a better RNG then we may need to use something
1605 // like /dev/urandom which has significant overhead.
1606 u32 rnd = (u32) (vlib_time_now (vnm->vlib_main) * 1e6);
1607 rnd = random_u32 (&rnd);
1608
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001609 child->time_to_expiration +=
Chris Lukeab7b8d92017-09-07 07:40:13 -04001610 1 + (rnd % sa->profile->lifetime_jitter);
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001611 }
1612 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001613
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001614 ipsec_add_del_tunnel_if (&a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001615
1616 return 0;
1617}
1618
1619static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001620ikev2_delete_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
1621 ikev2_child_sa_t * child)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001622{
1623 ipsec_add_del_tunnel_args_t a;
1624
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001625 if (sa->is_initiator)
1626 {
1627 if (!vec_len (child->i_proposals))
1628 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001629
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001630 a.is_add = 0;
1631 a.local_ip.as_u32 = sa->iaddr.as_u32;
1632 a.remote_ip.as_u32 = sa->raddr.as_u32;
1633 a.local_spi = child->r_proposals[0].spi;
1634 a.remote_spi = child->i_proposals[0].spi;
1635 }
1636 else
1637 {
1638 if (!vec_len (child->r_proposals))
1639 return 0;
1640
1641 a.is_add = 0;
1642 a.local_ip.as_u32 = sa->raddr.as_u32;
1643 a.remote_ip.as_u32 = sa->iaddr.as_u32;
1644 a.local_spi = child->i_proposals[0].spi;
1645 a.remote_spi = child->r_proposals[0].spi;
1646 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001647
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001648 ipsec_add_del_tunnel_if (&a);
Matthew Smith2838a232016-06-21 16:05:09 -05001649 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001650}
1651
1652static u32
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001653ikev2_generate_message (ikev2_sa_t * sa, ike_header_t * ike, void *user)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001654{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001655 v8 *integ = 0;
1656 ike_payload_header_t *ph;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001657 u16 plen;
1658 u32 tlen = 0;
1659
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001660 ikev2_sa_transform_t *tr_encr, *tr_integ;
1661 tr_encr =
1662 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1663 tr_integ =
1664 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001665
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001666 ikev2_payload_chain_t *chain = 0;
1667 ikev2_payload_new_chain (chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001668
1669 if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
1670 {
1671 if (sa->r_proposals == 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001672 {
1673 ikev2_payload_add_notify (chain,
1674 IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1675 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1676 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001677 else if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001678 {
1679 u8 *data = vec_new (u8, 2);
1680 ikev2_sa_transform_t *tr_dh;
1681 tr_dh =
1682 ikev2_sa_get_td_for_type (sa->r_proposals,
1683 IKEV2_TRANSFORM_TYPE_DH);
1684 ASSERT (tr_dh && tr_dh->dh_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001685
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001686 data[0] = (tr_dh->dh_type >> 8) & 0xff;
1687 data[1] = (tr_dh->dh_type) & 0xff;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001688
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001689 ikev2_payload_add_notify (chain,
1690 IKEV2_NOTIFY_MSG_INVALID_KE_PAYLOAD,
1691 data);
1692 vec_free (data);
1693 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1694 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001695 else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001696 {
1697 u8 *data = vec_new (u8, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001698
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001699 data[0] = sa->unsupported_cp;
1700 ikev2_payload_add_notify (chain,
1701 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1702 data);
1703 vec_free (data);
1704 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001705 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001706 {
1707 ike->rspi = clib_host_to_net_u64 (sa->rspi);
1708 ikev2_payload_add_sa (chain, sa->r_proposals);
1709 ikev2_payload_add_ke (chain, sa->dh_group, sa->r_dh_data);
1710 ikev2_payload_add_nonce (chain, sa->r_nonce);
1711 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001712 }
1713 else if (ike->exchange == IKEV2_EXCHANGE_IKE_AUTH)
1714 {
1715 if (sa->state == IKEV2_STATE_AUTHENTICATED)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001716 {
1717 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1718 ikev2_payload_add_auth (chain, &sa->r_auth);
1719 ikev2_payload_add_sa (chain, sa->childs[0].r_proposals);
1720 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1721 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
1722 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001723 else if (sa->state == IKEV2_STATE_AUTH_FAILED)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001724 {
1725 ikev2_payload_add_notify (chain,
1726 IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED,
1727 0);
1728 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1729 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001730 else if (sa->state == IKEV2_STATE_TS_UNACCEPTABLE)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001731 {
1732 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_TS_UNACCEPTABLE,
1733 0);
1734 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1735 ikev2_payload_add_auth (chain, &sa->r_auth);
1736 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001737 else if (sa->state == IKEV2_STATE_NO_PROPOSAL_CHOSEN)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001738 {
1739 ikev2_payload_add_notify (chain,
1740 IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1741 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1742 ikev2_payload_add_auth (chain, &sa->r_auth);
1743 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1744 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
1745 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001746 else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001747 {
1748 u8 *data = vec_new (u8, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001749
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001750 data[0] = sa->unsupported_cp;
1751 ikev2_payload_add_notify (chain,
1752 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1753 data);
1754 vec_free (data);
1755 }
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001756 else if (sa->state == IKEV2_STATE_SA_INIT)
1757 {
1758 ikev2_payload_add_id (chain, &sa->i_id, IKEV2_PAYLOAD_IDI);
1759 ikev2_payload_add_auth (chain, &sa->i_auth);
1760 ikev2_payload_add_sa (chain, sa->childs[0].i_proposals);
1761 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1762 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
1763 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001764 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001765 {
1766 ikev2_set_state (sa, IKEV2_STATE_DELETED);
1767 goto done;
1768 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001769 }
1770 else if (ike->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
1771 {
1772 /* if pending delete */
1773 if (sa->del)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001774 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001775 if (sa->del[0].protocol_id == IKEV2_PROTOCOL_IKE)
1776 {
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001777 if (sa->is_initiator)
1778 ikev2_payload_add_delete (chain, sa->del);
1779
1780 /* The response to a request that deletes the IKE SA is an empty
1781 INFORMATIONAL response. */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001782 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1783 }
1784 /* The response to a request that deletes ESP or AH SAs will contain
1785 delete payloads for the paired SAs going in the other direction. */
1786 else
1787 {
1788 ikev2_payload_add_delete (chain, sa->del);
1789 }
1790 vec_free (sa->del);
1791 sa->del = 0;
1792 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001793 /* received N(AUTHENTICATION_FAILED) */
1794 else if (sa->state == IKEV2_STATE_AUTH_FAILED)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001795 {
1796 ikev2_set_state (sa, IKEV2_STATE_DELETED);
1797 goto done;
1798 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001799 /* received unsupported critical payload */
1800 else if (sa->unsupported_cp)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001801 {
1802 u8 *data = vec_new (u8, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001803
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001804 data[0] = sa->unsupported_cp;
1805 ikev2_payload_add_notify (chain,
1806 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1807 data);
1808 vec_free (data);
1809 sa->unsupported_cp = 0;
1810 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001811 /* else send empty response */
1812 }
1813 else if (ike->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
1814 {
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001815 if (sa->is_initiator)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001816 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001817
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001818 ikev2_sa_proposal_t *proposals = (ikev2_sa_proposal_t *) user;
1819 ikev2_notify_t notify;
1820 u8 *data = vec_new (u8, 4);
1821 memset (&notify, 0, sizeof (notify));
1822 notify.protocol_id = IKEV2_PROTOCOL_ESP;
1823 notify.spi = sa->childs[0].i_proposals->spi;
1824 *(u32 *) data = clib_host_to_net_u32 (notify.spi);
1825
1826 ikev2_payload_add_sa (chain, proposals);
1827 ikev2_payload_add_nonce (chain, sa->i_nonce);
1828 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1829 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
1830 ikev2_payload_add_notify_2 (chain, IKEV2_NOTIFY_MSG_REKEY_SA, data,
1831 &notify);
1832
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001833 vec_free (data);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001834 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001835 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001836 {
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001837 if (sa->rekey)
1838 {
1839 ikev2_payload_add_sa (chain, sa->rekey[0].r_proposal);
1840 ikev2_payload_add_nonce (chain, sa->r_nonce);
1841 ikev2_payload_add_ts (chain, sa->rekey[0].tsi,
1842 IKEV2_PAYLOAD_TSI);
1843 ikev2_payload_add_ts (chain, sa->rekey[0].tsr,
1844 IKEV2_PAYLOAD_TSR);
1845 vec_del1 (sa->rekey, 0);
1846 }
1847 else if (sa->unsupported_cp)
1848 {
1849 u8 *data = vec_new (u8, 1);
1850
1851 data[0] = sa->unsupported_cp;
1852 ikev2_payload_add_notify (chain,
1853 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1854 data);
1855 vec_free (data);
1856 sa->unsupported_cp = 0;
1857 }
1858 else
1859 {
1860 ikev2_payload_add_notify (chain,
1861 IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS,
1862 0);
1863 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001864 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001865 }
1866
1867 /* IKEv2 header */
1868 ike->version = IKE_VERSION_2;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001869 ike->nextpayload = IKEV2_PAYLOAD_SK;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001870 tlen = sizeof (*ike);
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001871 if (sa->is_initiator)
1872 {
1873 ike->flags = IKEV2_HDR_FLAG_INITIATOR;
1874 sa->last_init_msg_id = clib_net_to_host_u32 (ike->msgid);
1875 }
1876 else
1877 {
1878 ike->flags = IKEV2_HDR_FLAG_RESPONSE;
1879 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001880
1881
1882 if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
1883 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001884 tlen += vec_len (chain->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001885 ike->nextpayload = chain->first_payload_type;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001886 ike->length = clib_host_to_net_u32 (tlen);
1887 clib_memcpy (ike->payload, chain->data, vec_len (chain->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001888
1889 /* store whole IKE payload - needed for PSK auth */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001890 vec_free (sa->last_sa_init_res_packet_data);
1891 vec_add (sa->last_sa_init_res_packet_data, ike, tlen);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001892 }
1893 else
1894 {
1895
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001896 ikev2_payload_chain_add_padding (chain, tr_encr->block_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001897
1898 /* SK payload */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001899 plen = sizeof (*ph);
1900 ph = (ike_payload_header_t *) & ike->payload[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001901 ph->nextpayload = chain->first_payload_type;
1902 ph->flags = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001903 int enc_len = ikev2_encrypt_data (sa, chain->data, ph->payload);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001904 plen += enc_len;
1905
1906 /* add space for hmac */
1907 plen += tr_integ->key_trunc;
1908 tlen += plen;
1909
1910 /* payload and total length */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001911 ph->length = clib_host_to_net_u16 (plen);
1912 ike->length = clib_host_to_net_u32 (tlen);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001913
1914 /* calc integrity data for whole packet except hash itself */
Radu Nicolaucb33dc22017-02-16 16:49:46 +00001915 integ =
1916 ikev2_calc_integr (tr_integ, sa->is_initiator ? sa->sk_ai : sa->sk_ar,
1917 (u8 *) ike, tlen - tr_integ->key_trunc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001918
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001919 clib_memcpy (ike->payload + tlen - tr_integ->key_trunc - sizeof (*ike),
1920 integ, tr_integ->key_trunc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001921
1922 /* store whole IKE payload - needed for retransmit */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001923 vec_free (sa->last_res_packet_data);
1924 vec_add (sa->last_res_packet_data, ike, tlen);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001925 }
1926
1927done:
1928 ikev2_payload_destroy_chain (chain);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001929 vec_free (integ);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001930 return tlen;
1931}
1932
1933static int
1934ikev2_retransmit_sa_init (ike_header_t * ike,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001935 ip4_address_t iaddr, ip4_address_t raddr)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001936{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001937 ikev2_main_t *km = &ikev2_main;
1938 ikev2_sa_t *sa;
Damjan Marion586afd72017-04-05 19:18:20 +02001939 u32 thread_index = vlib_get_thread_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001940
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001941 /* *INDENT-OFF* */
Damjan Marion586afd72017-04-05 19:18:20 +02001942 pool_foreach (sa, km->per_thread_data[thread_index].sas, ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07001943 if (sa->ispi == clib_net_to_host_u64(ike->ispi) &&
1944 sa->iaddr.as_u32 == iaddr.as_u32 &&
1945 sa->raddr.as_u32 == raddr.as_u32)
1946 {
1947 int p = 0;
1948 u32 len = clib_net_to_host_u32(ike->length);
1949 u8 payload = ike->nextpayload;
1950
1951 while (p < len && payload!= IKEV2_PAYLOAD_NONE) {
1952 ike_payload_header_t * ikep = (ike_payload_header_t *) &ike->payload[p];
1953 u32 plen = clib_net_to_host_u16(ikep->length);
1954
1955 if (plen < sizeof(ike_payload_header_t))
1956 return -1;
1957
1958 if (payload == IKEV2_PAYLOAD_NONCE)
1959 {
1960 if (!memcmp(sa->i_nonce, ikep->payload, plen - sizeof(*ikep)))
1961 {
1962 /* req is retransmit */
1963 if (sa->state == IKEV2_STATE_SA_INIT)
1964 {
1965 ike_header_t * tmp;
1966 tmp = (ike_header_t*)sa->last_sa_init_res_packet_data;
1967 ike->ispi = tmp->ispi;
1968 ike->rspi = tmp->rspi;
1969 ike->nextpayload = tmp->nextpayload;
1970 ike->version = tmp->version;
1971 ike->exchange = tmp->exchange;
1972 ike->flags = tmp->flags;
1973 ike->msgid = tmp->msgid;
1974 ike->length = tmp->length;
Damjan Marionf1213b82016-03-13 02:22:06 +01001975 clib_memcpy(ike->payload, tmp->payload,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001976 clib_net_to_host_u32(tmp->length) - sizeof(*ike));
1977 clib_warning("IKE_SA_INIT retransmit from %U to %U",
1978 format_ip4_address, &raddr,
1979 format_ip4_address, &iaddr);
1980 return 1;
1981 }
1982 /* else ignore req */
1983 else
1984 {
1985 clib_warning("IKE_SA_INIT ignore from %U to %U",
1986 format_ip4_address, &raddr,
1987 format_ip4_address, &iaddr);
1988 return -1;
1989 }
1990 }
1991 }
1992 payload = ikep->nextpayload;
1993 p+=plen;
1994 }
1995 }
1996 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001997 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001998
1999 /* req is not retransmit */
2000 return 0;
2001}
2002
2003static int
2004ikev2_retransmit_resp (ikev2_sa_t * sa, ike_header_t * ike)
2005{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002006 u32 msg_id = clib_net_to_host_u32 (ike->msgid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002007
2008 /* new req */
2009 if (msg_id > sa->last_msg_id)
2010 {
2011 sa->last_msg_id = msg_id;
2012 return 0;
2013 }
2014 /* retransmitted req */
2015 else if (msg_id == sa->last_msg_id)
2016 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002017 ike_header_t *tmp;
2018 tmp = (ike_header_t *) sa->last_res_packet_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002019 ike->ispi = tmp->ispi;
2020 ike->rspi = tmp->rspi;
2021 ike->nextpayload = tmp->nextpayload;
2022 ike->version = tmp->version;
2023 ike->exchange = tmp->exchange;
2024 ike->flags = tmp->flags;
2025 ike->msgid = tmp->msgid;
2026 ike->length = tmp->length;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002027 clib_memcpy (ike->payload, tmp->payload,
2028 clib_net_to_host_u32 (tmp->length) - sizeof (*ike));
2029 clib_warning ("IKE msgid %u retransmit from %U to %U",
2030 msg_id,
2031 format_ip4_address, &sa->raddr,
2032 format_ip4_address, &sa->iaddr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002033 return 1;
2034 }
2035 /* old req ignore */
2036 else
2037 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002038 clib_warning ("IKE msgid %u req ignore from %U to %U",
2039 msg_id,
2040 format_ip4_address, &sa->raddr,
2041 format_ip4_address, &sa->iaddr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002042 return -1;
2043 }
2044}
2045
2046static uword
2047ikev2_node_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002048 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002049{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002050 u32 n_left_from, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002051 ikev2_next_t next_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002052 ikev2_main_t *km = &ikev2_main;
Damjan Marion586afd72017-04-05 19:18:20 +02002053 u32 thread_index = vlib_get_thread_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002054
2055 from = vlib_frame_vector_args (frame);
2056 n_left_from = frame->n_vectors;
2057 next_index = node->cached_next_index;
2058
2059 while (n_left_from > 0)
2060 {
2061 u32 n_left_to_next;
2062
2063 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2064
2065 while (n_left_from > 0 && n_left_to_next > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002066 {
2067 u32 bi0;
2068 vlib_buffer_t *b0;
2069 u32 next0 = IKEV2_NEXT_ERROR_DROP;
2070 u32 sw_if_index0;
2071 ip4_header_t *ip40;
2072 udp_header_t *udp0;
2073 ike_header_t *ike0;
2074 ikev2_sa_t *sa0 = 0;
Damjan Marion3f54b182016-08-16 11:27:02 +02002075 ikev2_sa_t sa; /* temporary store for SA */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002076 int len = 0;
2077 int r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002078
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002079 /* speculatively enqueue b0 to the current next frame */
2080 bi0 = from[0];
2081 to_next[0] = bi0;
2082 from += 1;
2083 to_next += 1;
2084 n_left_from -= 1;
2085 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002086
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002087 b0 = vlib_get_buffer (vm, bi0);
2088 ike0 = vlib_buffer_get_current (b0);
2089 vlib_buffer_advance (b0, -sizeof (*udp0));
2090 udp0 = vlib_buffer_get_current (b0);
2091 vlib_buffer_advance (b0, -sizeof (*ip40));
2092 ip40 = vlib_buffer_get_current (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002093
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002094 if (ike0->version != IKE_VERSION_2)
2095 {
2096 vlib_node_increment_counter (vm, ikev2_node.index,
2097 IKEV2_ERROR_NOT_IKEV2, 1);
2098 goto dispatch0;
2099 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002100
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002101 if (ike0->exchange == IKEV2_EXCHANGE_SA_INIT)
2102 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002103 sa0 = &sa;
2104 memset (sa0, 0, sizeof (*sa0));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002105
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002106 if (ike0->flags & IKEV2_HDR_FLAG_INITIATOR)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002107 {
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002108 if (ike0->rspi == 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002109 {
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002110 sa0->raddr.as_u32 = ip40->dst_address.as_u32;
2111 sa0->iaddr.as_u32 = ip40->src_address.as_u32;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002112
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002113 r = ikev2_retransmit_sa_init (ike0, sa0->iaddr,
2114 sa0->raddr);
2115 if (r == 1)
2116 {
2117 vlib_node_increment_counter (vm, ikev2_node.index,
2118 IKEV2_ERROR_IKE_SA_INIT_RETRANSMIT,
2119 1);
2120 len = clib_net_to_host_u32 (ike0->length);
2121 goto dispatch0;
2122 }
2123 else if (r == -1)
2124 {
2125 vlib_node_increment_counter (vm, ikev2_node.index,
2126 IKEV2_ERROR_IKE_SA_INIT_IGNORE,
2127 1);
2128 goto dispatch0;
2129 }
2130
2131 ikev2_process_sa_init_req (vm, sa0, ike0);
2132
2133 if (sa0->state == IKEV2_STATE_SA_INIT)
2134 {
2135 ikev2_sa_free_proposal_vector (&sa0->r_proposals);
2136 sa0->r_proposals =
2137 ikev2_select_proposal (sa0->i_proposals,
2138 IKEV2_PROTOCOL_IKE);
2139 ikev2_generate_sa_init_data (sa0);
2140 }
2141
2142 if (sa0->state == IKEV2_STATE_SA_INIT
2143 || sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE)
2144 {
2145 len = ikev2_generate_message (sa0, ike0, 0);
2146 }
2147
2148 if (sa0->state == IKEV2_STATE_SA_INIT)
2149 {
2150 /* add SA to the pool */
Damjan Marion586afd72017-04-05 19:18:20 +02002151 pool_get (km->per_thread_data[thread_index].sas,
2152 sa0);
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002153 clib_memcpy (sa0, &sa, sizeof (*sa0));
Damjan Marion586afd72017-04-05 19:18:20 +02002154 hash_set (km->
2155 per_thread_data[thread_index].sa_by_rspi,
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002156 sa0->rspi,
Damjan Marion586afd72017-04-05 19:18:20 +02002157 sa0 -
2158 km->per_thread_data[thread_index].sas);
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002159 }
2160 else
2161 {
2162 ikev2_sa_free_all_vec (sa0);
2163 }
2164 }
2165 }
2166 else
2167 {
2168 ikev2_process_sa_init_resp (vm, sa0, ike0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002169
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002170 if (sa0->state == IKEV2_STATE_SA_INIT)
2171 {
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002172 ike0->exchange = IKEV2_EXCHANGE_IKE_AUTH;
2173 uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
2174 if (p)
2175 {
2176 ikev2_sa_t *sai =
2177 pool_elt_at_index (km->sais, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002178
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002179 ikev2_complete_sa_data (sa0, sai);
2180 ikev2_calc_keys (sa0);
2181 ikev2_sa_auth_init (sa0);
2182 len = ikev2_generate_message (sa0, ike0, 0);
2183 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002184 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002185
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002186 if (sa0->state == IKEV2_STATE_SA_INIT)
2187 {
2188 /* add SA to the pool */
Damjan Marion586afd72017-04-05 19:18:20 +02002189 pool_get (km->per_thread_data[thread_index].sas, sa0);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002190 clib_memcpy (sa0, &sa, sizeof (*sa0));
Damjan Marion586afd72017-04-05 19:18:20 +02002191 hash_set (km->per_thread_data[thread_index].sa_by_rspi,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002192 sa0->rspi,
Damjan Marion586afd72017-04-05 19:18:20 +02002193 sa0 - km->per_thread_data[thread_index].sas);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002194 }
2195 else
2196 {
2197 ikev2_sa_free_all_vec (sa0);
2198 }
2199 }
2200 }
2201 else if (ike0->exchange == IKEV2_EXCHANGE_IKE_AUTH)
2202 {
2203 uword *p;
Damjan Marion586afd72017-04-05 19:18:20 +02002204 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002205 clib_net_to_host_u64 (ike0->rspi));
2206 if (p)
2207 {
Damjan Marion586afd72017-04-05 19:18:20 +02002208 sa0 =
2209 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2210 p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002211
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002212 r = ikev2_retransmit_resp (sa0, ike0);
2213 if (r == 1)
2214 {
2215 vlib_node_increment_counter (vm, ikev2_node.index,
2216 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2217 1);
2218 len = clib_net_to_host_u32 (ike0->length);
2219 goto dispatch0;
2220 }
2221 else if (r == -1)
2222 {
2223 vlib_node_increment_counter (vm, ikev2_node.index,
2224 IKEV2_ERROR_IKE_REQ_IGNORE,
2225 1);
2226 goto dispatch0;
2227 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002228
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002229 ikev2_process_auth_req (vm, sa0, ike0);
2230 ikev2_sa_auth (sa0);
2231 if (sa0->state == IKEV2_STATE_AUTHENTICATED)
2232 {
2233 ikev2_initial_contact_cleanup (sa0);
2234 ikev2_sa_match_ts (sa0);
2235 if (sa0->state != IKEV2_STATE_TS_UNACCEPTABLE)
2236 ikev2_create_tunnel_interface (km->vnet_main, sa0,
2237 &sa0->childs[0]);
2238 }
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002239
2240 if (sa0->is_initiator)
2241 {
2242 uword *p = hash_get (km->sa_by_ispi, ike0->ispi);
2243 if (p)
2244 {
2245 ikev2_sa_t *sai =
2246 pool_elt_at_index (km->sais, p[0]);
2247 hash_unset (km->sa_by_ispi, sai->ispi);
2248 ikev2_sa_free_all_vec (sai);
2249 pool_put (km->sais, sai);
2250 }
2251 }
2252 else
2253 {
2254 len = ikev2_generate_message (sa0, ike0, 0);
2255 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002256 }
2257 }
2258 else if (ike0->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
2259 {
2260 uword *p;
Damjan Marion586afd72017-04-05 19:18:20 +02002261 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002262 clib_net_to_host_u64 (ike0->rspi));
2263 if (p)
2264 {
Damjan Marion586afd72017-04-05 19:18:20 +02002265 sa0 =
2266 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2267 p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002268
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002269 r = ikev2_retransmit_resp (sa0, ike0);
2270 if (r == 1)
2271 {
2272 vlib_node_increment_counter (vm, ikev2_node.index,
2273 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2274 1);
2275 len = clib_net_to_host_u32 (ike0->length);
2276 goto dispatch0;
2277 }
2278 else if (r == -1)
2279 {
2280 vlib_node_increment_counter (vm, ikev2_node.index,
2281 IKEV2_ERROR_IKE_REQ_IGNORE,
2282 1);
2283 goto dispatch0;
2284 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002285
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002286 ikev2_process_informational_req (vm, sa0, ike0);
2287 if (sa0->del)
2288 {
2289 if (sa0->del[0].protocol_id != IKEV2_PROTOCOL_IKE)
2290 {
2291 ikev2_delete_t *d, *tmp, *resp = 0;
2292 vec_foreach (d, sa0->del)
2293 {
2294 ikev2_child_sa_t *ch_sa;
2295 ch_sa = ikev2_sa_get_child (sa0, d->spi,
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002296 d->protocol_id,
2297 !sa0->is_initiator);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002298 if (ch_sa)
2299 {
2300 ikev2_delete_tunnel_interface (km->vnet_main,
2301 sa0, ch_sa);
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002302 if (!sa0->is_initiator)
2303 {
2304 vec_add2 (resp, tmp, 1);
2305 tmp->protocol_id = d->protocol_id;
2306 tmp->spi = ch_sa->r_proposals[0].spi;
2307 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002308 ikev2_sa_del_child_sa (sa0, ch_sa);
2309 }
2310 }
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002311 if (!sa0->is_initiator)
2312 {
2313 vec_free (sa0->del);
2314 sa0->del = resp;
2315 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002316 }
2317 }
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002318 if (!sa0->is_initiator)
2319 {
2320 len = ikev2_generate_message (sa0, ike0, 0);
2321 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002322 }
2323 }
2324 else if (ike0->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
2325 {
2326 uword *p;
Damjan Marion586afd72017-04-05 19:18:20 +02002327 p = hash_get (km->per_thread_data[thread_index].sa_by_rspi,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002328 clib_net_to_host_u64 (ike0->rspi));
2329 if (p)
2330 {
Damjan Marion586afd72017-04-05 19:18:20 +02002331 sa0 =
2332 pool_elt_at_index (km->per_thread_data[thread_index].sas,
2333 p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002334
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002335 r = ikev2_retransmit_resp (sa0, ike0);
2336 if (r == 1)
2337 {
2338 vlib_node_increment_counter (vm, ikev2_node.index,
2339 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
2340 1);
2341 len = clib_net_to_host_u32 (ike0->length);
2342 goto dispatch0;
2343 }
2344 else if (r == -1)
2345 {
2346 vlib_node_increment_counter (vm, ikev2_node.index,
2347 IKEV2_ERROR_IKE_REQ_IGNORE,
2348 1);
2349 goto dispatch0;
2350 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002351
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002352 ikev2_process_create_child_sa_req (vm, sa0, ike0);
2353 if (sa0->rekey)
2354 {
2355 if (sa0->rekey[0].protocol_id != IKEV2_PROTOCOL_IKE)
2356 {
2357 ikev2_child_sa_t *child;
2358 vec_add2 (sa0->childs, child, 1);
2359 child->r_proposals = sa0->rekey[0].r_proposal;
2360 child->i_proposals = sa0->rekey[0].i_proposal;
2361 child->tsi = sa0->rekey[0].tsi;
2362 child->tsr = sa0->rekey[0].tsr;
2363 ikev2_create_tunnel_interface (km->vnet_main, sa0,
2364 child);
2365 }
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002366 if (sa0->is_initiator)
2367 {
2368 vec_del1 (sa0->rekey, 0);
2369 }
2370 else
2371 {
2372 len = ikev2_generate_message (sa0, ike0, 0);
2373 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002374 }
2375 }
2376 }
2377 else
2378 {
2379 clib_warning ("IKEv2 exchange %u packet received from %U to %U",
2380 ike0->exchange,
2381 format_ip4_address, ip40->src_address.as_u8,
2382 format_ip4_address, ip40->dst_address.as_u8);
2383 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002384
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002385 dispatch0:
2386 /* if we are sending packet back, rewrite headers */
2387 if (len)
2388 {
2389 next0 = IKEV2_NEXT_IP4_LOOKUP;
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002390 if (sa0->is_initiator)
2391 {
2392 ip40->dst_address.as_u32 = sa0->raddr.as_u32;
2393 ip40->src_address.as_u32 = sa0->iaddr.as_u32;
2394 }
2395 else
2396 {
2397 ip40->dst_address.as_u32 = sa0->iaddr.as_u32;
2398 ip40->src_address.as_u32 = sa0->raddr.as_u32;
2399 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002400 udp0->length =
2401 clib_host_to_net_u16 (len + sizeof (udp_header_t));
2402 udp0->checksum = 0;
2403 b0->current_length =
2404 len + sizeof (ip4_header_t) + sizeof (udp_header_t);
2405 ip40->length = clib_host_to_net_u16 (b0->current_length);
2406 ip40->checksum = ip4_header_checksum (ip40);
2407 }
2408 /* delete sa */
2409 if (sa0 && (sa0->state == IKEV2_STATE_DELETED ||
2410 sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE))
2411 {
2412 ikev2_child_sa_t *c;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002413
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002414 vec_foreach (c, sa0->childs)
2415 ikev2_delete_tunnel_interface (km->vnet_main, sa0, c);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002416
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002417 ikev2_delete_sa (sa0);
2418 }
2419 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07002420
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002421 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2422 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2423 {
2424 ikev2_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
2425 t->sw_if_index = sw_if_index0;
2426 t->next_index = next0;
2427 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002428
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002429 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2430 n_left_to_next, bi0, next0);
2431 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002432
2433 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2434 }
2435
2436 vlib_node_increment_counter (vm, ikev2_node.index,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002437 IKEV2_ERROR_PROCESSED, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002438 return frame->n_vectors;
2439}
2440
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002441/* *INDENT-OFF* */
Jean-Mickael Guerin8941ec22016-03-04 14:14:21 +01002442VLIB_REGISTER_NODE (ikev2_node,static) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002443 .function = ikev2_node_fn,
2444 .name = "ikev2",
2445 .vector_size = sizeof (u32),
2446 .format_trace = format_ikev2_trace,
2447 .type = VLIB_NODE_TYPE_INTERNAL,
2448
2449 .n_errors = ARRAY_LEN(ikev2_error_strings),
2450 .error_strings = ikev2_error_strings,
2451
2452 .n_next_nodes = IKEV2_N_NEXT,
2453
2454 .next_nodes = {
2455 [IKEV2_NEXT_IP4_LOOKUP] = "ip4-lookup",
2456 [IKEV2_NEXT_ERROR_DROP] = "error-drop",
2457 },
2458};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002459/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002460
2461
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002462static clib_error_t *
2463ikev2_set_initiator_proposals (vlib_main_t * vm, ikev2_sa_t * sa,
2464 ikev2_transforms_set * ts,
2465 ikev2_sa_proposal_t ** proposals, int is_ike)
2466{
2467 clib_error_t *r;
2468 ikev2_main_t *km = &ikev2_main;
2469 ikev2_sa_proposal_t *proposal;
2470 vec_add2 (*proposals, proposal, 1);
2471 ikev2_sa_transform_t *td;
2472 int error;
2473
2474 /* Encryption */
2475 error = 1;
2476 vec_foreach (td, km->supported_transforms)
2477 {
2478 if (td->type == IKEV2_TRANSFORM_TYPE_ENCR
2479 && td->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC
2480 && td->key_len == ts->crypto_key_size / 8)
2481 {
2482 u16 attr[2];
2483 attr[0] = clib_host_to_net_u16 (14 | (1 << 15));
2484 attr[1] = clib_host_to_net_u16 (td->key_len << 3);
2485 vec_add (td->attrs, (u8 *) attr, 4);
2486 vec_add1 (proposal->transforms, *td);
2487 td->attrs = 0;
2488
2489 error = 0;
2490 break;
2491 }
2492 }
2493 if (error)
2494 {
2495 r = clib_error_return (0, "Unsupported algorithm");
2496 return r;
2497 }
2498
2499 /* Integrity */
2500 error = 1;
2501 vec_foreach (td, km->supported_transforms)
2502 {
2503 if (td->type == IKEV2_TRANSFORM_TYPE_INTEG
2504 && td->integ_type == IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96)
2505 {
2506 vec_add1 (proposal->transforms, *td);
2507 error = 0;
2508 break;
2509 }
2510 }
2511 if (error)
2512 {
2513 r = clib_error_return (0, "Unsupported algorithm");
2514 return r;
2515 }
2516
2517 /* PRF */
2518 if (is_ike)
2519 {
2520 error = 1;
2521 vec_foreach (td, km->supported_transforms)
2522 {
2523 if (td->type == IKEV2_TRANSFORM_TYPE_PRF
2524 && td->prf_type == IKEV2_TRANSFORM_PRF_TYPE_PRF_HMAC_SHA1)
2525 {
2526 vec_add1 (proposal->transforms, *td);
2527 error = 0;
2528 break;
2529 }
2530 }
2531 if (error)
2532 {
2533 r = clib_error_return (0, "Unsupported algorithm");
2534 return r;
2535 }
2536 }
2537
2538 /* DH */
2539 error = 1;
2540 vec_foreach (td, km->supported_transforms)
2541 {
2542 if (td->type == IKEV2_TRANSFORM_TYPE_DH && td->dh_type == ts->dh_type)
2543 {
2544 vec_add1 (proposal->transforms, *td);
2545 if (is_ike)
2546 {
2547 sa->dh_group = td->dh_type;
2548 }
2549 error = 0;
2550 break;
2551 }
2552 }
2553 if (error)
2554 {
2555 r = clib_error_return (0, "Unsupported algorithm");
2556 return r;
2557 }
2558
2559 if (!is_ike)
2560 {
2561 error = 1;
2562 vec_foreach (td, km->supported_transforms)
2563 {
2564 if (td->type == IKEV2_TRANSFORM_TYPE_ESN)
2565 {
2566 vec_add1 (proposal->transforms, *td);
2567 error = 0;
2568 break;
2569 }
2570 }
2571 if (error)
2572 {
2573 r = clib_error_return (0, "Unsupported algorithm");
2574 return r;
2575 }
2576 }
2577
2578
2579 return 0;
2580}
2581
Ed Warnickecb9cada2015-12-08 15:45:58 -07002582static ikev2_profile_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002583ikev2_profile_index_by_name (u8 * name)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002584{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002585 ikev2_main_t *km = &ikev2_main;
2586 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002587
2588 p = mhash_get (&km->profile_index_by_name, name);
2589 if (!p)
2590 return 0;
2591
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002592 return pool_elt_at_index (km->profiles, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002593}
2594
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002595
2596static void
2597ikev2_send_ike (vlib_main_t * vm, ip4_address_t * src, ip4_address_t * dst,
2598 u32 bi0, u32 len)
2599{
2600 ip4_header_t *ip40;
2601 udp_header_t *udp0;
2602 vlib_buffer_t *b0;
2603 vlib_frame_t *f;
2604 u32 *to_next;
2605
2606 b0 = vlib_get_buffer (vm, bi0);
2607 vlib_buffer_advance (b0, -sizeof (udp_header_t));
2608 udp0 = vlib_buffer_get_current (b0);
2609 vlib_buffer_advance (b0, -sizeof (ip4_header_t));
2610 ip40 = vlib_buffer_get_current (b0);
2611
2612
2613 ip40->ip_version_and_header_length = 0x45;
2614 ip40->tos = 0;
2615 ip40->fragment_id = 0;
2616 ip40->flags_and_fragment_offset = 0;
2617 ip40->ttl = 0xff;
2618 ip40->protocol = IP_PROTOCOL_UDP;
2619 ip40->dst_address.as_u32 = dst->as_u32;
2620 ip40->src_address.as_u32 = src->as_u32;
2621 udp0->dst_port = clib_host_to_net_u16 (500);
2622 udp0->src_port = clib_host_to_net_u16 (500);
2623 udp0->length = clib_host_to_net_u16 (len + sizeof (udp_header_t));
2624 udp0->checksum = 0;
2625 b0->current_length = len + sizeof (ip4_header_t) + sizeof (udp_header_t);
2626 ip40->length = clib_host_to_net_u16 (b0->current_length);
2627 ip40->checksum = ip4_header_checksum (ip40);
2628
2629
2630 /* send the request */
2631 f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
2632 to_next = vlib_frame_vector_args (f);
2633 to_next[0] = bi0;
2634 f->n_vectors = 1;
2635 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
2636
2637}
2638
2639static u32
2640ikev2_get_new_ike_header_buff (vlib_main_t * vm, ike_header_t ** ike)
2641{
2642 u32 bi0;
2643 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2644 {
2645 *ike = 0;
2646 return 0;
2647 }
2648 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
2649 *ike = vlib_buffer_get_current (b0);
2650 return bi0;
2651}
2652
Ed Warnickecb9cada2015-12-08 15:45:58 -07002653clib_error_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002654ikev2_set_local_key (vlib_main_t * vm, u8 * file)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002655{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002656 ikev2_main_t *km = &ikev2_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002657
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002658 km->pkey = ikev2_load_key_file (file);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002659 if (km->pkey == NULL)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002660 return clib_error_return (0, "load key '%s' failed", file);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002661
2662 return 0;
2663}
2664
2665clib_error_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002666ikev2_add_del_profile (vlib_main_t * vm, u8 * name, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002667{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002668 ikev2_main_t *km = &ikev2_main;
2669 ikev2_profile_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002670
2671 if (is_add)
2672 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002673 if (ikev2_profile_index_by_name (name))
2674 return clib_error_return (0, "policy %v already exists", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002675
2676 pool_get (km->profiles, p);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002677 memset (p, 0, sizeof (*p));
2678 p->name = vec_dup (name);
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002679 p->responder.sw_if_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002680 uword index = p - km->profiles;
2681 mhash_set_mem (&km->profile_index_by_name, name, &index, 0);
2682 }
2683 else
2684 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002685 p = ikev2_profile_index_by_name (name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002686 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002687 return clib_error_return (0, "policy %v does not exists", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002688
2689 vec_free (p->name);
2690 pool_put (km->profiles, p);
2691 mhash_unset (&km->profile_index_by_name, name, 0);
2692 }
2693 return 0;
2694}
2695
2696clib_error_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002697ikev2_set_profile_auth (vlib_main_t * vm, u8 * name, u8 auth_method,
2698 u8 * auth_data, u8 data_hex_format)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002699{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002700 ikev2_profile_t *p;
2701 clib_error_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002702
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002703 p = ikev2_profile_index_by_name (name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002704
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002705 if (!p)
2706 {
2707 r = clib_error_return (0, "unknown profile %v", name);
2708 return r;
2709 }
2710 vec_free (p->auth.data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002711 p->auth.method = auth_method;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002712 p->auth.data = vec_dup (auth_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002713 p->auth.hex = data_hex_format;
2714
2715 if (auth_method == IKEV2_AUTH_METHOD_RSA_SIG)
2716 {
Matus Fabian698b9522016-09-13 23:15:56 -07002717 vec_add1 (p->auth.data, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002718 if (p->auth.key)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002719 EVP_PKEY_free (p->auth.key);
2720 p->auth.key = ikev2_load_cert_file (auth_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002721 if (p->auth.key == NULL)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002722 return clib_error_return (0, "load cert '%s' failed", auth_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002723 }
2724
2725 return 0;
2726}
2727
2728clib_error_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002729ikev2_set_profile_id (vlib_main_t * vm, u8 * name, u8 id_type, u8 * data,
2730 int is_local)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002731{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002732 ikev2_profile_t *p;
2733 clib_error_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002734
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002735 if (id_type > IKEV2_ID_TYPE_ID_RFC822_ADDR
2736 && id_type < IKEV2_ID_TYPE_ID_KEY_ID)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002737 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002738 r = clib_error_return (0, "unsupported identity type %U",
2739 format_ikev2_id_type, id_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002740 return r;
2741 }
2742
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002743 p = ikev2_profile_index_by_name (name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002744
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002745 if (!p)
2746 {
2747 r = clib_error_return (0, "unknown profile %v", name);
2748 return r;
2749 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002750
2751 if (is_local)
2752 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002753 vec_free (p->loc_id.data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002754 p->loc_id.type = id_type;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002755 p->loc_id.data = vec_dup (data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002756 }
2757 else
2758 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002759 vec_free (p->rem_id.data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002760 p->rem_id.type = id_type;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002761 p->rem_id.data = vec_dup (data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002762 }
2763
2764 return 0;
2765}
2766
2767clib_error_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002768ikev2_set_profile_ts (vlib_main_t * vm, u8 * name, u8 protocol_id,
2769 u16 start_port, u16 end_port, ip4_address_t start_addr,
2770 ip4_address_t end_addr, int is_local)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002771{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002772 ikev2_profile_t *p;
2773 clib_error_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002774
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002775 p = ikev2_profile_index_by_name (name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002776
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002777 if (!p)
2778 {
2779 r = clib_error_return (0, "unknown profile %v", name);
2780 return r;
2781 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002782
2783 if (is_local)
2784 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002785 p->loc_ts.start_addr.as_u32 = start_addr.as_u32;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002786 p->loc_ts.end_addr.as_u32 = end_addr.as_u32;
2787 p->loc_ts.start_port = start_port;
2788 p->loc_ts.end_port = end_port;
2789 p->loc_ts.protocol_id = protocol_id;
2790 p->loc_ts.ts_type = 7;
2791 }
2792 else
2793 {
2794 p->rem_ts.start_addr.as_u32 = start_addr.as_u32;
2795 p->rem_ts.end_addr.as_u32 = end_addr.as_u32;
2796 p->rem_ts.start_port = start_port;
2797 p->rem_ts.end_port = end_port;
2798 p->rem_ts.protocol_id = protocol_id;
2799 p->rem_ts.ts_type = 7;
2800 }
2801
2802 return 0;
2803}
2804
2805
2806clib_error_t *
Radu Nicolaucb33dc22017-02-16 16:49:46 +00002807ikev2_set_profile_responder (vlib_main_t * vm, u8 * name,
2808 u32 sw_if_index, ip4_address_t ip4)
2809{
2810 ikev2_profile_t *p;
2811 clib_error_t *r;
2812
2813 p = ikev2_profile_index_by_name (name);
2814
2815 if (!p)
2816 {
2817 r = clib_error_return (0, "unknown profile %v", name);
2818 return r;
2819 }
2820
2821 p->responder.sw_if_index = sw_if_index;
2822 p->responder.ip4 = ip4;
2823
2824 return 0;
2825}
2826
2827clib_error_t *
2828ikev2_set_profile_ike_transforms (vlib_main_t * vm, u8 * name,
2829 ikev2_transform_encr_type_t crypto_alg,
2830 ikev2_transform_integ_type_t integ_alg,
2831 ikev2_transform_dh_type_t dh_type,
2832 u32 crypto_key_size)
2833{
2834 ikev2_profile_t *p;
2835 clib_error_t *r;
2836
2837 p = ikev2_profile_index_by_name (name);
2838
2839 if (!p)
2840 {
2841 r = clib_error_return (0, "unknown profile %v", name);
2842 return r;
2843 }
2844
2845 p->ike_ts.crypto_alg = crypto_alg;
2846 p->ike_ts.integ_alg = integ_alg;
2847 p->ike_ts.dh_type = dh_type;
2848 p->ike_ts.crypto_key_size = crypto_key_size;
2849 return 0;
2850}
2851
2852clib_error_t *
2853ikev2_set_profile_esp_transforms (vlib_main_t * vm, u8 * name,
2854 ikev2_transform_encr_type_t crypto_alg,
2855 ikev2_transform_integ_type_t integ_alg,
2856 ikev2_transform_dh_type_t dh_type,
2857 u32 crypto_key_size)
2858{
2859 ikev2_profile_t *p;
2860 clib_error_t *r;
2861
2862 p = ikev2_profile_index_by_name (name);
2863
2864 if (!p)
2865 {
2866 r = clib_error_return (0, "unknown profile %v", name);
2867 return r;
2868 }
2869
2870 p->esp_ts.crypto_alg = crypto_alg;
2871 p->esp_ts.integ_alg = integ_alg;
2872 p->esp_ts.dh_type = dh_type;
2873 p->esp_ts.crypto_key_size = crypto_key_size;
2874 return 0;
2875}
2876
2877clib_error_t *
2878ikev2_set_profile_sa_lifetime (vlib_main_t * vm, u8 * name,
2879 u64 lifetime, u32 jitter, u32 handover,
2880 u64 maxdata)
2881{
2882 ikev2_profile_t *p;
2883 clib_error_t *r;
2884
2885 p = ikev2_profile_index_by_name (name);
2886
2887 if (!p)
2888 {
2889 r = clib_error_return (0, "unknown profile %v", name);
2890 return r;
2891 }
2892
2893 p->lifetime = lifetime;
2894 p->lifetime_jitter = jitter;
2895 p->handover = handover;
2896 p->lifetime_maxdata = maxdata;
2897 return 0;
2898}
2899
2900clib_error_t *
2901ikev2_initiate_sa_init (vlib_main_t * vm, u8 * name)
2902{
2903 ikev2_profile_t *p;
2904 clib_error_t *r;
2905 ip4_main_t *im = &ip4_main;
2906 ikev2_main_t *km = &ikev2_main;
2907
2908 p = ikev2_profile_index_by_name (name);
2909
2910 if (!p)
2911 {
2912 r = clib_error_return (0, "unknown profile %v", name);
2913 return r;
2914 }
2915
2916 if (p->responder.sw_if_index == ~0 || p->responder.ip4.data_u32 == 0)
2917 {
2918 r = clib_error_return (0, "responder not set for profile %v", name);
2919 return r;
2920 }
2921
2922
2923 /* Create the Initiator Request */
2924 {
2925 ike_header_t *ike0;
2926 u32 bi0 = 0;
2927 ip_lookup_main_t *lm = &im->lookup_main;
2928 u32 if_add_index0;
2929 int len = sizeof (ike_header_t);
2930
2931 /* Get own iface IP */
2932 if_add_index0 =
2933 lm->if_address_pool_index_by_sw_if_index[p->responder.sw_if_index];
2934 ip_interface_address_t *if_add =
2935 pool_elt_at_index (lm->if_address_pool, if_add_index0);
2936 ip4_address_t *if_ip = ip_interface_address_get_address (lm, if_add);
2937
2938 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
2939
2940 /* Prepare the SA and the IKE payload */
2941 ikev2_sa_t sa;
2942 memset (&sa, 0, sizeof (ikev2_sa_t));
2943 ikev2_payload_chain_t *chain = 0;
2944 ikev2_payload_new_chain (chain);
2945
2946 /* Build the IKE proposal payload */
2947 ikev2_sa_proposal_t *proposals = 0;
2948 ikev2_set_initiator_proposals (vm, &sa, &p->ike_ts, &proposals, 1);
2949 proposals[0].proposal_num = 1;
2950 proposals[0].protocol_id = IKEV2_PROTOCOL_IKE;
2951
2952 /* Add and then cleanup proposal data */
2953 ikev2_payload_add_sa (chain, proposals);
2954 ikev2_sa_free_proposal_vector (&proposals);
2955
2956 sa.is_initiator = 1;
2957 sa.profile = p;
2958 sa.state = IKEV2_STATE_SA_INIT;
2959 ikev2_generate_sa_init_data (&sa);
2960 ikev2_payload_add_ke (chain, sa.dh_group, sa.i_dh_data);
2961 ikev2_payload_add_nonce (chain, sa.i_nonce);
2962
2963 /* Build the child SA proposal */
2964 vec_resize (sa.childs, 1);
2965 ikev2_set_initiator_proposals (vm, &sa, &p->esp_ts,
2966 &sa.childs[0].i_proposals, 0);
2967 sa.childs[0].i_proposals[0].proposal_num = 1;
2968 sa.childs[0].i_proposals[0].protocol_id = IKEV2_PROTOCOL_ESP;
2969 RAND_bytes ((u8 *) & sa.childs[0].i_proposals[0].spi,
2970 sizeof (sa.childs[0].i_proposals[0].spi));
2971
2972
2973
2974 /* Add NAT detection notification messages (mandatory) */
2975 u8 nat_detection_source[8 + 8 + 4 + 2];
2976 u8 *nat_detection_sha1 = vec_new (u8, 20);
2977
2978 u64 tmpspi = clib_host_to_net_u64 (sa.ispi);
2979 clib_memcpy (&nat_detection_source[0], &tmpspi, sizeof (tmpspi));
2980 tmpspi = clib_host_to_net_u64 (sa.rspi);
2981 clib_memcpy (&nat_detection_source[8], &tmpspi, sizeof (tmpspi));
2982 u16 tmpport = clib_host_to_net_u16 (500);
2983 clib_memcpy (&nat_detection_source[8 + 8 + 4], &tmpport,
2984 sizeof (tmpport));
2985 u32 tmpip = clib_host_to_net_u32 (if_ip->as_u32);
2986 clib_memcpy (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
2987 SHA1 (nat_detection_source, sizeof (nat_detection_source),
2988 nat_detection_sha1);
2989 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP,
2990 nat_detection_sha1);
2991 tmpip = clib_host_to_net_u32 (p->responder.ip4.as_u32);
2992 clib_memcpy (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip));
2993 SHA1 (nat_detection_source, sizeof (nat_detection_source),
2994 nat_detection_sha1);
2995 ikev2_payload_add_notify (chain,
2996 IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP,
2997 nat_detection_sha1);
2998 vec_free (nat_detection_sha1);
2999
3000 u8 *sig_hash_algo = vec_new (u8, 8);
3001 u64 tmpsig = clib_host_to_net_u64 (0x0001000200030004);
3002 clib_memcpy (sig_hash_algo, &tmpsig, sizeof (tmpsig));
3003 ikev2_payload_add_notify (chain,
3004 IKEV2_NOTIFY_MSG_SIGNATURE_HASH_ALGORITHMS,
3005 sig_hash_algo);
3006 vec_free (sig_hash_algo);
3007
3008
3009 /* Buffer update and bolierplate */
3010 len += vec_len (chain->data);
3011 ike0->nextpayload = chain->first_payload_type;
3012 ike0->length = clib_host_to_net_u32 (len);
3013 clib_memcpy (ike0->payload, chain->data, vec_len (chain->data));
3014 ikev2_payload_destroy_chain (chain);
3015
3016 ike0->version = IKE_VERSION_2;
3017 ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3018 ike0->exchange = IKEV2_EXCHANGE_SA_INIT;
3019 ike0->ispi = sa.ispi;
3020
3021 /* store whole IKE payload - needed for PSK auth */
3022 vec_free (sa.last_sa_init_req_packet_data);
3023 vec_add (sa.last_sa_init_req_packet_data, ike0, len);
3024
3025 /* add data to the SA then add it to the pool */
3026 sa.iaddr.as_u32 = if_ip->as_u32;
3027 sa.raddr.as_u32 = p->responder.ip4.as_u32;
3028 sa.i_id.type = p->loc_id.type;
3029 sa.i_id.data = vec_dup (p->loc_id.data);
3030 sa.i_auth.method = p->auth.method;
3031 sa.i_auth.hex = p->auth.hex;
3032 sa.i_auth.data = vec_dup (p->auth.data);
Marco Varlesef616d102017-11-09 15:16:20 +01003033#if OPENSSL_VERSION_NUMBER >= 0x10100000L
3034 clib_memcpy (sa.i_auth.key, p->auth.key, EVP_PKEY_size (p->auth.key));
3035#else
Radu Nicolaucb33dc22017-02-16 16:49:46 +00003036 sa.i_auth.key = vec_dup (p->auth.key);
Marco Varlesef616d102017-11-09 15:16:20 +01003037#endif
Radu Nicolaucb33dc22017-02-16 16:49:46 +00003038 vec_add (sa.childs[0].tsi, &p->loc_ts, 1);
3039 vec_add (sa.childs[0].tsr, &p->rem_ts, 1);
3040
3041 /* add SA to the pool */
3042 ikev2_sa_t *sa0 = 0;
3043 pool_get (km->sais, sa0);
3044 clib_memcpy (sa0, &sa, sizeof (*sa0));
3045 hash_set (km->sa_by_ispi, sa0->ispi, sa0 - km->sais);
3046
3047 ikev2_send_ike (vm, if_ip, &p->responder.ip4, bi0, len);
3048
3049 }
3050
3051 return 0;
3052}
3053
3054static void
3055ikev2_delete_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
3056 ikev2_child_sa_t * csa)
3057{
3058 /* Create the Initiator notification for child SA removal */
3059 ikev2_main_t *km = &ikev2_main;
3060 ike_header_t *ike0;
3061 u32 bi0 = 0;
3062 int len;
3063
3064 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3065
3066
3067 ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3068 ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3069 ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3070 vec_resize (sa->del, 1);
3071 sa->del->protocol_id = IKEV2_PROTOCOL_ESP;
3072 sa->del->spi = csa->i_proposals->spi;
3073 ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
3074 sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3075 len = ikev2_generate_message (sa, ike0, 0);
3076
3077 ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
3078
3079 /* delete local child SA */
3080 ikev2_delete_tunnel_interface (km->vnet_main, sa, csa);
3081 ikev2_sa_del_child_sa (sa, csa);
3082}
3083
3084clib_error_t *
3085ikev2_initiate_delete_child_sa (vlib_main_t * vm, u32 ispi)
3086{
3087 clib_error_t *r;
3088 ikev2_main_t *km = &ikev2_main;
3089 ikev2_main_per_thread_data_t *tkm;
3090 ikev2_sa_t *fsa = 0;
3091 ikev2_child_sa_t *fchild = 0;
3092
3093 /* Search for the child SA */
3094 vec_foreach (tkm, km->per_thread_data)
3095 {
3096 ikev2_sa_t *sa;
3097 if (fchild)
3098 break;
3099 /* *INDENT-OFF* */
3100 pool_foreach (sa, tkm->sas, ({
3101 fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
3102 if (fchild)
3103 {
3104 fsa = sa;
3105 break;
3106 }
3107 }));
3108 /* *INDENT-ON* */
3109 }
3110
3111 if (!fchild || !fsa)
3112 {
3113 r = clib_error_return (0, "Child SA not found");
3114 return r;
3115 }
3116 else
3117 {
3118 ikev2_delete_child_sa_internal (vm, fsa, fchild);
3119 }
3120
3121 return 0;
3122}
3123
3124clib_error_t *
3125ikev2_initiate_delete_ike_sa (vlib_main_t * vm, u64 ispi)
3126{
3127 clib_error_t *r;
3128 ikev2_main_t *km = &ikev2_main;
3129 ikev2_main_per_thread_data_t *tkm;
3130 ikev2_sa_t *fsa = 0;
3131 ikev2_main_per_thread_data_t *ftkm = 0;
3132
3133 /* Search for the IKE SA */
3134 vec_foreach (tkm, km->per_thread_data)
3135 {
3136 ikev2_sa_t *sa;
3137 if (fsa)
3138 break;
3139 /* *INDENT-OFF* */
3140 pool_foreach (sa, tkm->sas, ({
3141 if (sa->ispi == ispi)
3142 {
3143 fsa = sa;
3144 ftkm = tkm;
3145 break;
3146 }
3147 }));
3148 /* *INDENT-ON* */
3149 }
3150
3151 if (!fsa)
3152 {
3153 r = clib_error_return (0, "IKE SA not found");
3154 return r;
3155 }
3156
3157
3158 /* Create the Initiator notification for IKE SA removal */
3159 {
3160 ike_header_t *ike0;
3161 u32 bi0 = 0;
3162 int len;
3163
3164 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3165
3166
3167 ike0->exchange = IKEV2_EXCHANGE_INFORMATIONAL;
3168 ike0->ispi = clib_host_to_net_u64 (fsa->ispi);
3169 ike0->rspi = clib_host_to_net_u64 (fsa->rspi);
3170 vec_resize (fsa->del, 1);
3171 fsa->del->protocol_id = IKEV2_PROTOCOL_IKE;
3172 fsa->del->spi = ispi;
3173 ike0->msgid = clib_host_to_net_u32 (fsa->last_init_msg_id + 1);
3174 fsa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3175 len = ikev2_generate_message (fsa, ike0, 0);
3176
3177 ikev2_send_ike (vm, &fsa->iaddr, &fsa->raddr, bi0, len);
3178 }
3179
3180
3181 /* delete local SA */
3182 ikev2_child_sa_t *c;
3183 vec_foreach (c, fsa->childs)
3184 {
3185 ikev2_delete_tunnel_interface (km->vnet_main, fsa, c);
3186 ikev2_sa_del_child_sa (fsa, c);
3187 }
3188 ikev2_sa_free_all_vec (fsa);
3189 uword *p = hash_get (ftkm->sa_by_rspi, fsa->rspi);
3190 if (p)
3191 {
3192 hash_unset (ftkm->sa_by_rspi, fsa->rspi);
3193 pool_put (ftkm->sas, fsa);
3194 }
3195
3196
3197 return 0;
3198}
3199
3200static void
3201ikev2_rekey_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa,
3202 ikev2_child_sa_t * csa)
3203{
3204 /* Create the Initiator request for create child SA */
3205 ike_header_t *ike0;
3206 u32 bi0 = 0;
3207 int len;
3208
3209
3210 bi0 = ikev2_get_new_ike_header_buff (vm, &ike0);
3211
3212
3213 ike0->version = IKE_VERSION_2;
3214 ike0->flags = IKEV2_HDR_FLAG_INITIATOR;
3215 ike0->exchange = IKEV2_EXCHANGE_CREATE_CHILD_SA;
3216 ike0->ispi = clib_host_to_net_u64 (sa->ispi);
3217 ike0->rspi = clib_host_to_net_u64 (sa->rspi);
3218 ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1);
3219 sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid);
3220
3221 ikev2_rekey_t *rekey;
3222 vec_add2 (sa->rekey, rekey, 1);
3223 ikev2_sa_proposal_t *proposals = vec_dup (csa->i_proposals);
3224
3225 /*need new ispi */
3226 RAND_bytes ((u8 *) & proposals[0].spi, sizeof (proposals[0].spi));
3227 rekey->spi = proposals[0].spi;
3228 rekey->ispi = csa->i_proposals->spi;
3229 len = ikev2_generate_message (sa, ike0, proposals);
3230 ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len);
3231 vec_free (proposals);
3232}
3233
3234clib_error_t *
3235ikev2_initiate_rekey_child_sa (vlib_main_t * vm, u32 ispi)
3236{
3237 clib_error_t *r;
3238 ikev2_main_t *km = &ikev2_main;
3239 ikev2_main_per_thread_data_t *tkm;
3240 ikev2_sa_t *fsa = 0;
3241 ikev2_child_sa_t *fchild = 0;
3242
3243 /* Search for the child SA */
3244 vec_foreach (tkm, km->per_thread_data)
3245 {
3246 ikev2_sa_t *sa;
3247 if (fchild)
3248 break;
3249 /* *INDENT-OFF* */
3250 pool_foreach (sa, tkm->sas, ({
3251 fchild = ikev2_sa_get_child(sa, ispi, IKEV2_PROTOCOL_ESP, 1);
3252 if (fchild)
3253 {
3254 fsa = sa;
3255 break;
3256 }
3257 }));
3258 /* *INDENT-ON* */
3259 }
3260
3261 if (!fchild || !fsa)
3262 {
3263 r = clib_error_return (0, "Child SA not found");
3264 return r;
3265 }
3266 else
3267 {
3268 ikev2_rekey_child_sa_internal (vm, fsa, fchild);
3269 }
3270
3271 return 0;
3272}
3273
3274clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07003275ikev2_init (vlib_main_t * vm)
3276{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07003277 ikev2_main_t *km = &ikev2_main;
3278 clib_error_t *error;
3279 vlib_thread_main_t *tm = vlib_get_thread_main ();
Matthew Smith2838a232016-06-21 16:05:09 -05003280 int thread_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003281
3282 memset (km, 0, sizeof (ikev2_main_t));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07003283 km->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07003284 km->vlib_main = vm;
3285
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07003286 ikev2_crypto_init (km);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003287
Ed Warnickecb9cada2015-12-08 15:45:58 -07003288 mhash_init_vec_string (&km->profile_index_by_name, sizeof (uword));
3289
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07003290 vec_validate (km->per_thread_data, tm->n_vlib_mains - 1);
Matthew Smith2838a232016-06-21 16:05:09 -05003291 for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
3292 {
3293 km->per_thread_data[thread_id].sa_by_rspi =
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07003294 hash_create (0, sizeof (uword));
Matthew Smith2838a232016-06-21 16:05:09 -05003295 }
3296
Radu Nicolaucb33dc22017-02-16 16:49:46 +00003297 km->sa_by_ispi = hash_create (0, sizeof (uword));
3298
3299
Ed Warnickecb9cada2015-12-08 15:45:58 -07003300 if ((error = vlib_call_init_function (vm, ikev2_cli_init)))
3301 return error;
3302
3303 udp_register_dst_port (vm, 500, ikev2_node.index, 1);
3304
3305 return 0;
3306}
3307
3308
Radu Nicolaucb33dc22017-02-16 16:49:46 +00003309static u8
3310ikev2_mngr_process_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * csa)
3311{
3312 ikev2_main_t *km = &ikev2_main;
3313 vlib_main_t *vm = km->vlib_main;
3314 f64 now = vlib_time_now (vm);
3315 u8 res = 0;
3316
3317 if (sa->is_initiator && sa->profile && csa->time_to_expiration
3318 && now > csa->time_to_expiration)
3319 {
3320 if (!csa->is_expired || csa->rekey_retries > 0)
3321 {
3322 ikev2_rekey_child_sa_internal (vm, sa, csa);
3323 csa->time_to_expiration = now + sa->profile->handover;
3324 csa->is_expired = 1;
3325 if (csa->rekey_retries == 0)
3326 {
3327 csa->rekey_retries = 5;
3328 }
3329 else if (csa->rekey_retries > 0)
3330 {
3331 csa->rekey_retries--;
3332 clib_warning ("Rekeing Child SA 0x%x, retries left %d",
3333 csa->i_proposals->spi, csa->rekey_retries);
3334 if (csa->rekey_retries == 0)
3335 {
3336 csa->rekey_retries = -1;
3337 }
3338 }
3339 res |= 1;
3340 }
3341 else
3342 {
3343 csa->time_to_expiration = 0;
3344 ikev2_delete_child_sa_internal (vm, sa, csa);
3345 res |= 1;
3346 }
3347 }
3348
3349 return res;
3350}
3351
3352static void
3353ikev2_mngr_process_ipsec_sa (ipsec_sa_t * ipsec_sa)
3354{
3355 ikev2_main_t *km = &ikev2_main;
3356 vlib_main_t *vm = km->vlib_main;
3357 ikev2_main_per_thread_data_t *tkm;
3358 ikev2_sa_t *fsa = 0;
3359 ikev2_child_sa_t *fchild = 0;
3360 f64 now = vlib_time_now (vm);
3361
3362 /* Search for the SA and child SA */
3363 vec_foreach (tkm, km->per_thread_data)
3364 {
3365 ikev2_sa_t *sa;
3366 if (fchild)
3367 break;
3368 /* *INDENT-OFF* */
3369 pool_foreach (sa, tkm->sas, ({
3370 fchild = ikev2_sa_get_child(sa, ipsec_sa->spi, IKEV2_PROTOCOL_ESP, 1);
3371 if (fchild)
3372 {
3373 fsa = sa;
3374 break;
3375 }
3376 }));
3377 /* *INDENT-ON* */
3378 }
3379
3380 if (fchild && fsa && fsa->profile && fsa->profile->lifetime_maxdata)
3381 {
3382 if (!fchild->is_expired
3383 && ipsec_sa->total_data_size > fsa->profile->lifetime_maxdata)
3384 {
3385 fchild->time_to_expiration = now;
3386 }
3387 }
3388}
3389
3390static vlib_node_registration_t ikev2_mngr_process_node;
3391
3392static uword
3393ikev2_mngr_process_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
3394 vlib_frame_t * f)
3395{
3396 ikev2_main_t *km = &ikev2_main;
3397 ipsec_main_t *im = &ipsec_main;
3398
3399 while (1)
3400 {
3401 u8 req_sent = 0;
3402 vlib_process_wait_for_event_or_clock (vm, 1);
3403 vlib_process_get_events (vm, NULL);
3404
3405 /* process ike child sas */
3406 ikev2_main_per_thread_data_t *tkm;
3407 vec_foreach (tkm, km->per_thread_data)
3408 {
3409 ikev2_sa_t *sa;
3410 /* *INDENT-OFF* */
3411 pool_foreach (sa, tkm->sas, ({
3412 ikev2_child_sa_t *c;
3413 vec_foreach (c, sa->childs)
3414 {
3415 req_sent |= ikev2_mngr_process_child_sa(sa, c);
3416 }
3417 }));
3418 /* *INDENT-ON* */
3419 }
3420
3421 /* process ipsec sas */
3422 ipsec_sa_t *sa;
3423 /* *INDENT-OFF* */
3424 pool_foreach (sa, im->sad, ({
3425 ikev2_mngr_process_ipsec_sa(sa);
3426 }));
3427 /* *INDENT-ON* */
3428
3429 if (req_sent)
3430 {
3431 vlib_process_wait_for_event_or_clock (vm, 5);
3432 vlib_process_get_events (vm, NULL);
3433 req_sent = 0;
3434 }
3435
3436 }
3437 return 0;
3438}
3439
3440/* *INDENT-OFF* */
3441VLIB_REGISTER_NODE (ikev2_mngr_process_node, static) = {
3442 .function = ikev2_mngr_process_fn,
3443 .type = VLIB_NODE_TYPE_PROCESS,
3444 .name =
3445 "ikev2-manager-process",
3446};
3447
3448/* *INDENT-ON* */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07003449
3450/*
3451 * fd.io coding-style-patch-verification: ON
3452 *
3453 * Local Variables:
3454 * eval: (c-set-style "gnu")
3455 * End:
3456 */