blob: 5a6c3674477afe14fe0aa10fa5504d2ca9a7756c [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>
20#include <vnet/ip/udp.h>
21#include <vnet/ipsec/ipsec.h>
22#include <vnet/ipsec/ikev2.h>
23#include <vnet/ipsec/ikev2_priv.h>
24
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070025static int ikev2_delete_tunnel_interface (vnet_main_t * vnm,
26 ikev2_sa_t * sa,
27 ikev2_child_sa_t * child);
Ed Warnickecb9cada2015-12-08 15:45:58 -070028
Ed Warnickecb9cada2015-12-08 15:45:58 -070029#define ikev2_set_state(sa, v) do { \
30 (sa)->state = v; \
31 clib_warning("sa state changed to " #v); \
32 } while(0);
33
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070034typedef struct
35{
Ed Warnickecb9cada2015-12-08 15:45:58 -070036 u32 next_index;
37 u32 sw_if_index;
38} ikev2_trace_t;
39
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070040static u8 *
41format_ikev2_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070042{
43 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
44 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070045 ikev2_trace_t *t = va_arg (*args, ikev2_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47 s = format (s, "ikev2: sw_if_index %d, next index %d",
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070048 t->sw_if_index, t->next_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070049 return s;
50}
51
Jean-Mickael Guerin8941ec22016-03-04 14:14:21 +010052static vlib_node_registration_t ikev2_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
54#define foreach_ikev2_error \
55_(PROCESSED, "IKEv2 packets processed") \
56_(IKE_SA_INIT_RETRANSMIT, "IKE_SA_INIT retransmit ") \
57_(IKE_SA_INIT_IGNORE, "IKE_SA_INIT ignore (IKE SA already auth)") \
58_(IKE_REQ_RETRANSMIT, "IKE request retransmit") \
59_(IKE_REQ_IGNORE, "IKE request ignore (old msgid)") \
60_(NOT_IKEV2, "Non IKEv2 packets received")
61
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070062typedef enum
63{
Ed Warnickecb9cada2015-12-08 15:45:58 -070064#define _(sym,str) IKEV2_ERROR_##sym,
65 foreach_ikev2_error
66#undef _
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070067 IKEV2_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070068} ikev2_error_t;
69
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070070static char *ikev2_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070071#define _(sym,string) string,
72 foreach_ikev2_error
73#undef _
74};
75
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070076typedef enum
77{
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 IKEV2_NEXT_IP4_LOOKUP,
79 IKEV2_NEXT_ERROR_DROP,
80 IKEV2_N_NEXT,
81} ikev2_next_t;
82
83static ikev2_sa_transform_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070084ikev2_find_transform_data (ikev2_sa_transform_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -070085{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070086 ikev2_main_t *km = &ikev2_main;
87 ikev2_sa_transform_t *td;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070089 vec_foreach (td, km->supported_transforms)
90 {
91 if (td->type != t->type)
92 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070094 if (td->transform_id != t->transform_id)
95 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070097 if (td->type == IKEV2_TRANSFORM_TYPE_ENCR)
98 {
99 if (vec_len (t->attrs) != 4 || t->attrs[0] != 0x80
100 || t->attrs[1] != 14)
101 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700103 if (((t->attrs[2] << 8 | t->attrs[3]) / 8) != td->key_len)
104 continue;
105 }
106 return td;
107 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 return 0;
109}
110
111static ikev2_sa_proposal_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700112ikev2_select_proposal (ikev2_sa_proposal_t * proposals,
113 ikev2_protocol_id_t prot_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700115 ikev2_sa_proposal_t *rv = 0;
116 ikev2_sa_proposal_t *proposal;
117 ikev2_sa_transform_t *transform, *new_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118 u8 mandatory_bitmap, optional_bitmap;
119
120 if (prot_id == IKEV2_PROTOCOL_IKE)
121 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700122 mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
123 (1 << IKEV2_TRANSFORM_TYPE_PRF) |
124 (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
125 optional_bitmap = mandatory_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 }
127 else if (prot_id == IKEV2_PROTOCOL_ESP)
128 {
129 mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) |
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700130 (1 << IKEV2_TRANSFORM_TYPE_ESN);
131 optional_bitmap = mandatory_bitmap |
132 (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 }
134 else if (prot_id == IKEV2_PROTOCOL_AH)
135 {
136 mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_INTEG) |
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700137 (1 << IKEV2_TRANSFORM_TYPE_ESN);
138 optional_bitmap = mandatory_bitmap | (1 << IKEV2_TRANSFORM_TYPE_DH);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 }
140 else
141 return 0;
142
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700143 vec_add2 (rv, proposal, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700145 vec_foreach (proposal, proposals)
146 {
147 u8 bitmap = 0;
148 if (proposal->protocol_id != prot_id)
149 continue;
150
151 vec_foreach (transform, proposal->transforms)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700153 if ((1 << transform->type) & bitmap)
154 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700156 if (ikev2_find_transform_data (transform))
157 {
158 bitmap |= 1 << transform->type;
159 vec_add2 (rv->transforms, new_t, 1);
160 clib_memcpy (new_t, transform, sizeof (*new_t));
161 new_t->attrs = vec_dup (transform->attrs);
162 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 }
164
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700165 clib_warning ("bitmap is %x mandatory is %x optional is %x",
166 bitmap, mandatory_bitmap, optional_bitmap);
167
168 if ((bitmap & mandatory_bitmap) == mandatory_bitmap &&
169 (bitmap & ~optional_bitmap) == 0)
170 {
171 rv->proposal_num = proposal->proposal_num;
172 rv->protocol_id = proposal->protocol_id;
173 RAND_bytes ((u8 *) & rv->spi, sizeof (rv->spi));
174 goto done;
175 }
176 else
177 {
178 vec_free (rv->transforms);
179 }
180 }
181
182 vec_free (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183done:
184 return rv;
185}
186
187ikev2_sa_transform_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700188ikev2_sa_get_td_for_type (ikev2_sa_proposal_t * p,
189 ikev2_transform_type_t type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700191 ikev2_sa_transform_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
193 if (!p)
194 return 0;
195
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700196 vec_foreach (t, p->transforms)
197 {
198 if (t->type == type)
199 return ikev2_find_transform_data (t);
200 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201 return 0;
202}
203
204ikev2_child_sa_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700205ikev2_sa_get_child (ikev2_sa_t * sa, u32 spi, ikev2_protocol_id_t prot_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700207 ikev2_child_sa_t *c;
208 vec_foreach (c, sa->childs)
209 {
210 if (c->i_proposals[0].spi == spi
211 && c->i_proposals[0].protocol_id == prot_id)
212 return c;
213 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214
215 return 0;
216}
217
218void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700219ikev2_sa_free_proposal_vector (ikev2_sa_proposal_t ** v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700221 ikev2_sa_proposal_t *p;
222 ikev2_sa_transform_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223
224 if (!*v)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700225 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700227 vec_foreach (p, *v)
228 {
229 vec_foreach (t, p->transforms)
230 {
231 vec_free (t->attrs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700233 vec_free (p->transforms);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700235 vec_free (*v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236};
237
238static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700239ikev2_sa_free_all_child_sa (ikev2_child_sa_t ** childs)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700241 ikev2_child_sa_t *c;
242 vec_foreach (c, *childs)
243 {
244 ikev2_sa_free_proposal_vector (&c->r_proposals);
245 ikev2_sa_free_proposal_vector (&c->i_proposals);
246 vec_free (c->sk_ai);
247 vec_free (c->sk_ar);
248 vec_free (c->sk_ei);
249 vec_free (c->sk_er);
250 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700252 vec_free (*childs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253}
254
255static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700256ikev2_sa_del_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * child)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700258 ikev2_sa_free_proposal_vector (&child->r_proposals);
259 ikev2_sa_free_proposal_vector (&child->i_proposals);
260 vec_free (child->sk_ai);
261 vec_free (child->sk_ar);
262 vec_free (child->sk_ei);
263 vec_free (child->sk_er);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700265 vec_del1 (sa->childs, child - sa->childs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266}
267
268static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700269ikev2_sa_free_all_vec (ikev2_sa_t * sa)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700271 vec_free (sa->i_nonce);
272 vec_free (sa->i_dh_data);
273 vec_free (sa->dh_shared_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700275 ikev2_sa_free_proposal_vector (&sa->r_proposals);
276 ikev2_sa_free_proposal_vector (&sa->i_proposals);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700278 vec_free (sa->sk_d);
279 vec_free (sa->sk_ai);
280 vec_free (sa->sk_ar);
281 vec_free (sa->sk_ei);
282 vec_free (sa->sk_er);
283 vec_free (sa->sk_pi);
284 vec_free (sa->sk_pr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700286 vec_free (sa->i_id.data);
287 vec_free (sa->i_auth.data);
288 vec_free (sa->r_id.data);
289 vec_free (sa->r_auth.data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 if (sa->r_auth.key)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700291 EVP_PKEY_free (sa->r_auth.key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700293 vec_free (sa->del);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700295 ikev2_sa_free_all_child_sa (&sa->childs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296}
297
298static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700299ikev2_delete_sa (ikev2_sa_t * sa)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700301 ikev2_main_t *km = &ikev2_main;
302 u32 cpu_index = os_get_cpu_number ();
303 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700305 ikev2_sa_free_all_vec (sa);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700307 p = hash_get (km->per_thread_data[cpu_index].sa_by_rspi, sa->rspi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308 if (p)
309 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700310 hash_unset (km->per_thread_data[cpu_index].sa_by_rspi, sa->rspi);
311 pool_put (km->per_thread_data[cpu_index].sas, sa);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312 }
313}
314
315static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700316ikev2_generate_sa_init_data (ikev2_sa_t * sa)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700318 ikev2_sa_transform_t *t = 0, *t2;
319 ikev2_main_t *km = &ikev2_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
321 if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
322 {
323 return;
324 }
325
326 /* check if received DH group is on our list of supported groups */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700327 vec_foreach (t2, km->supported_transforms)
328 {
329 if (t2->type == IKEV2_TRANSFORM_TYPE_DH && sa->dh_group == t2->dh_type)
330 {
331 t = t2;
332 break;
333 }
334 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335
336 if (!t)
337 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700338 clib_warning ("unknown dh data group %u (data len %u)", sa->dh_group,
339 vec_len (sa->i_dh_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE;
341 return;
342 }
343
344 /* generate rspi */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700345 RAND_bytes ((u8 *) & sa->rspi, 8);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
347 /* generate nonce */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700348 sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
349 RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350
351 /* generate dh keys */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700352 ikev2_generate_dh (sa, t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353}
354
355static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700356ikev2_calc_keys (ikev2_sa_t * sa)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700358 u8 *tmp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359 /* calculate SKEYSEED = prf(Ni | Nr, g^ir) */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700360 u8 *skeyseed = 0;
361 u8 *s = 0;
362 ikev2_sa_transform_t *tr_encr, *tr_prf, *tr_integ;
363 tr_encr =
364 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
365 tr_prf =
366 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
367 tr_integ =
368 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700370 vec_append (s, sa->i_nonce);
371 vec_append (s, sa->r_nonce);
372 skeyseed = ikev2_calc_prf (tr_prf, s, sa->dh_shared_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700374 /* Calculate S = Ni | Nr | SPIi | SPIr */
375 u64 *spi;
376 vec_add2 (s, tmp, 2 * sizeof (*spi));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377 spi = (u64 *) tmp;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700378 spi[0] = clib_host_to_net_u64 (sa->ispi);
379 spi[1] = clib_host_to_net_u64 (sa->rspi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380
381 /* calculate PRFplus */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700382 u8 *keymat;
383 int len = tr_prf->key_trunc + /* SK_d */
384 tr_integ->key_len * 2 + /* SK_ai, SK_ar */
385 tr_encr->key_len * 2 + /* SK_ei, SK_er */
386 tr_prf->key_len * 2; /* SK_pi, SK_pr */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700388 keymat = ikev2_calc_prfplus (tr_prf, skeyseed, s, len);
389 vec_free (skeyseed);
390 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
392 int pos = 0;
393
394 /* SK_d */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700395 sa->sk_d = vec_new (u8, tr_prf->key_trunc);
396 clib_memcpy (sa->sk_d, keymat + pos, tr_prf->key_trunc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397 pos += tr_prf->key_trunc;
398
399 /* SK_ai */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700400 sa->sk_ai = vec_new (u8, tr_integ->key_len);
401 clib_memcpy (sa->sk_ai, keymat + pos, tr_integ->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 pos += tr_integ->key_len;
403
404 /* SK_ar */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700405 sa->sk_ar = vec_new (u8, tr_integ->key_len);
406 clib_memcpy (sa->sk_ar, keymat + pos, tr_integ->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407 pos += tr_integ->key_len;
408
409 /* SK_ei */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700410 sa->sk_ei = vec_new (u8, tr_encr->key_len);
411 clib_memcpy (sa->sk_ei, keymat + pos, tr_encr->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412 pos += tr_encr->key_len;
413
414 /* SK_er */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700415 sa->sk_er = vec_new (u8, tr_encr->key_len);
416 clib_memcpy (sa->sk_er, keymat + pos, tr_encr->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 pos += tr_encr->key_len;
418
419 /* SK_pi */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700420 sa->sk_pi = vec_new (u8, tr_prf->key_len);
421 clib_memcpy (sa->sk_pi, keymat + pos, tr_prf->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 pos += tr_prf->key_len;
423
424 /* SK_pr */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700425 sa->sk_pr = vec_new (u8, tr_prf->key_len);
426 clib_memcpy (sa->sk_pr, keymat + pos, tr_prf->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 pos += tr_prf->key_len;
428
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700429 vec_free (keymat);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430}
431
432static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700433ikev2_calc_child_keys (ikev2_sa_t * sa, ikev2_child_sa_t * child)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700435 u8 *s = 0;
436 ikev2_sa_transform_t *tr_prf, *ctr_encr, *ctr_integ;
437 tr_prf =
438 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
439 ctr_encr =
440 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
441 ctr_integ =
442 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700444 vec_append (s, sa->i_nonce);
445 vec_append (s, sa->r_nonce);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 /* calculate PRFplus */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700447 u8 *keymat;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 int len = ctr_encr->key_len * 2 + ctr_integ->key_len * 2;
449
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700450 keymat = ikev2_calc_prfplus (tr_prf, sa->sk_d, s, len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451
452 int pos = 0;
453
454 /* SK_ei */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700455 child->sk_ei = vec_new (u8, ctr_encr->key_len);
456 clib_memcpy (child->sk_ei, keymat + pos, ctr_encr->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 pos += ctr_encr->key_len;
458
459 /* SK_ai */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700460 child->sk_ai = vec_new (u8, ctr_integ->key_len);
461 clib_memcpy (child->sk_ai, keymat + pos, ctr_integ->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462 pos += ctr_integ->key_len;
463
464 /* SK_er */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700465 child->sk_er = vec_new (u8, ctr_encr->key_len);
466 clib_memcpy (child->sk_er, keymat + pos, ctr_encr->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 pos += ctr_encr->key_len;
468
469 /* SK_ar */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700470 child->sk_ar = vec_new (u8, ctr_integ->key_len);
471 clib_memcpy (child->sk_ar, keymat + pos, ctr_integ->key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472 pos += ctr_integ->key_len;
473
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700474 ASSERT (pos == len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700476 vec_free (keymat);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477}
478
479static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700480ikev2_process_sa_init_req (vlib_main_t * vm, ikev2_sa_t * sa,
481 ike_header_t * ike)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482{
483 int p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700484 u32 len = clib_net_to_host_u32 (ike->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 u8 payload = ike->nextpayload;
486
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700487 clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
488 "exchange %x flags %x msgid %x length %u",
489 clib_net_to_host_u64 (ike->ispi),
490 clib_net_to_host_u64 (ike->rspi),
491 payload, ike->version,
492 ike->exchange, ike->flags,
493 clib_net_to_host_u32 (ike->msgid), len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700495 sa->ispi = clib_net_to_host_u64 (ike->ispi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496
497 /* store whole IKE payload - needed for PSK auth */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700498 vec_free (sa->last_sa_init_req_packet_data);
499 vec_add (sa->last_sa_init_req_packet_data, ike, len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700501 while (p < len && payload != IKEV2_PAYLOAD_NONE)
502 {
503 ike_payload_header_t *ikep = (ike_payload_header_t *) & ike->payload[p];
504 u32 plen = clib_net_to_host_u16 (ikep->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700506 if (plen < sizeof (ike_payload_header_t))
507 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700509 if (payload == IKEV2_PAYLOAD_SA)
510 {
511 ikev2_sa_free_proposal_vector (&sa->i_proposals);
512 sa->i_proposals = ikev2_parse_sa_payload (ikep);
513 }
514 else if (payload == IKEV2_PAYLOAD_KE)
515 {
516 ike_ke_payload_header_t *ke = (ike_ke_payload_header_t *) ikep;
517 sa->dh_group = clib_net_to_host_u16 (ke->dh_group);
518 vec_free (sa->i_dh_data);
519 vec_add (sa->i_dh_data, ke->payload, plen - sizeof (*ke));
520 }
521 else if (payload == IKEV2_PAYLOAD_NONCE)
522 {
523 vec_free (sa->i_nonce);
524 vec_add (sa->i_nonce, ikep->payload, plen - sizeof (*ikep));
525 }
526 else if (payload == IKEV2_PAYLOAD_NOTIFY)
527 {
528 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
529 vec_free (n);
530 }
531 else if (payload == IKEV2_PAYLOAD_VENDOR)
532 {
533 ikev2_parse_vendor_payload (ikep);
534 }
535 else
536 {
537 clib_warning ("unknown payload %u flags %x length %u", payload,
538 ikep->flags, plen);
539 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
540 {
541 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
542 sa->unsupported_cp = payload;
543 return;
544 }
545 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700547 payload = ikep->nextpayload;
548 p += plen;
549 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700551 ikev2_set_state (sa, IKEV2_STATE_SA_INIT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552}
553
554static u8 *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700555ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike, u8 * payload)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556{
557 int p = 0;
558 u8 last_payload = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700559 u8 *hmac = 0;
560 u32 len = clib_net_to_host_u32 (ike->length);
561 ike_payload_header_t *ikep = 0;
Benoît Ganned5304452016-04-08 22:25:05 -0700562 u32 plen = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700563 ikev2_sa_transform_t *tr_integ;
564 tr_integ =
565 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566
567 while (p < len &&
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700568 *payload != IKEV2_PAYLOAD_NONE && last_payload != IKEV2_PAYLOAD_SK)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700570 ikep = (ike_payload_header_t *) & ike->payload[p];
571 plen = clib_net_to_host_u16 (ikep->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700573 if (plen < sizeof (*ikep))
574 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575
576 if (*payload == IKEV2_PAYLOAD_SK)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700577 {
578 clib_warning ("received IKEv2 payload SK, len %u", plen - 4);
579 last_payload = *payload;
580 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700582 {
583 clib_warning ("unknown payload %u flags %x length %u", payload,
584 ikep->flags, plen);
585 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
586 {
587 sa->unsupported_cp = *payload;
588 return 0;
589 }
590 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700592 *payload = ikep->nextpayload;
593 p += plen;
594 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700596 if (last_payload != IKEV2_PAYLOAD_SK)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700598 clib_warning ("Last payload must be SK");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 return 0;
600 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700602 hmac = ikev2_calc_integr (tr_integ, sa->sk_ai, (u8 *) ike,
603 len - tr_integ->key_trunc);
604
605 plen = plen - sizeof (*ikep) - tr_integ->key_trunc;
606
607 if (memcmp (hmac, &ikep->payload[plen], tr_integ->key_trunc))
608 {
609 clib_warning ("message integrity check failed");
610 vec_free (hmac);
611 return 0;
612 }
613 vec_free (hmac);
614
615 return ikev2_decrypt_data (sa, ikep->payload, plen);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616}
617
618static void
619ikev2_initial_contact_cleanup (ikev2_sa_t * sa)
620{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700621 ikev2_main_t *km = &ikev2_main;
622 ikev2_sa_t *tmp;
623 u32 i, *delete = 0;
624 ikev2_child_sa_t *c;
625 u32 cpu_index = os_get_cpu_number ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
627 if (!sa->initial_contact)
628 return;
629
630 /* find old IKE SAs with the same authenticated identity */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700631 /* *INDENT-OFF* */
Matthew Smith2838a232016-06-21 16:05:09 -0500632 pool_foreach (tmp, km->per_thread_data[cpu_index].sas, ({
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633 if (tmp->i_id.type != sa->i_id.type ||
634 vec_len(tmp->i_id.data) != vec_len(sa->i_id.data) ||
635 memcmp(sa->i_id.data, tmp->i_id.data, vec_len(sa->i_id.data)))
636 continue;
637
638 if (sa->rspi != tmp->rspi)
Matthew Smith2838a232016-06-21 16:05:09 -0500639 vec_add1(delete, tmp - km->per_thread_data[cpu_index].sas);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700641 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700642
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700643 for (i = 0; i < vec_len (delete); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700645 tmp = pool_elt_at_index (km->per_thread_data[cpu_index].sas, delete[i]);
646 vec_foreach (c, tmp->childs)
647 ikev2_delete_tunnel_interface (km->vnet_main, tmp, c);
648 ikev2_delete_sa (tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649 }
650
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700651 vec_free (delete);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652 sa->initial_contact = 0;
653}
654
655static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700656ikev2_process_auth_req (vlib_main_t * vm, ikev2_sa_t * sa, ike_header_t * ike)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700658 ikev2_child_sa_t *first_child_sa;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659 int p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700660 u32 len = clib_net_to_host_u32 (ike->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661 u8 payload = ike->nextpayload;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700662 u8 *plaintext = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700664 ike_payload_header_t *ikep;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665 u32 plen;
666
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700667 clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
668 "exchange %x flags %x msgid %x length %u",
669 clib_net_to_host_u64 (ike->ispi),
670 clib_net_to_host_u64 (ike->rspi),
671 payload, ike->version,
672 ike->exchange, ike->flags,
673 clib_net_to_host_u32 (ike->msgid), len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700675 ikev2_calc_keys (sa);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700677 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678
679 if (!plaintext)
680 {
681 if (sa->unsupported_cp)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700682 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 goto cleanup_and_exit;
684 }
685
686 /* create 1st child SA */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700687 ikev2_sa_free_all_child_sa (&sa->childs);
688 vec_add2 (sa->childs, first_child_sa, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689
690
691 /* process encrypted payload */
692 p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700693 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700695 ikep = (ike_payload_header_t *) & plaintext[p];
696 plen = clib_net_to_host_u16 (ikep->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700698 if (plen < sizeof (ike_payload_header_t))
699 goto cleanup_and_exit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700701 if (payload == IKEV2_PAYLOAD_SA) /* 33 */
702 {
703 clib_warning ("received payload SA, len %u", plen - sizeof (*ikep));
704 ikev2_sa_free_proposal_vector (&first_child_sa->i_proposals);
705 first_child_sa->i_proposals = ikev2_parse_sa_payload (ikep);
706 }
707 else if (payload == IKEV2_PAYLOAD_IDI) /* 35 */
708 {
709 ike_id_payload_header_t *id = (ike_id_payload_header_t *) ikep;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700711 sa->i_id.type = id->id_type;
712 vec_free (sa->i_id.data);
713 vec_add (sa->i_id.data, id->payload, plen - sizeof (*id));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700715 clib_warning ("received payload IDi, len %u id_type %u",
716 plen - sizeof (*id), id->id_type);
717 }
718 else if (payload == IKEV2_PAYLOAD_AUTH) /* 39 */
719 {
720 ike_auth_payload_header_t *a = (ike_auth_payload_header_t *) ikep;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700722 sa->i_auth.method = a->auth_method;
723 vec_free (sa->i_auth.data);
724 vec_add (sa->i_auth.data, a->payload, plen - sizeof (*a));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700726 clib_warning ("received payload AUTH, len %u auth_type %u",
727 plen - sizeof (*a), a->auth_method);
728 }
729 else if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
730 {
731 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
732 if (n->msg_type == IKEV2_NOTIFY_MSG_INITIAL_CONTACT)
733 {
734 sa->initial_contact = 1;
735 }
736 vec_free (n);
737 }
738 else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
739 {
740 ikev2_parse_vendor_payload (ikep);
741 }
742 else if (payload == IKEV2_PAYLOAD_TSI) /* 44 */
743 {
744 clib_warning ("received payload TSi, len %u",
745 plen - sizeof (*ikep));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700747 vec_free (first_child_sa->tsi);
748 first_child_sa->tsi = ikev2_parse_ts_payload (ikep);
749 }
750 else if (payload == IKEV2_PAYLOAD_TSR) /* 45 */
751 {
752 clib_warning ("received payload TSr, len %u",
753 plen - sizeof (*ikep));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700755 vec_free (first_child_sa->tsr);
756 first_child_sa->tsr = ikev2_parse_ts_payload (ikep);
757 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700759 {
760 clib_warning ("unknown payload %u flags %x length %u data %u",
761 payload, ikep->flags, plen - 4,
762 format_hex_bytes, ikep->payload, plen - 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700764 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
765 {
766 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
767 sa->unsupported_cp = payload;
768 return;
769 }
770 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771
772 payload = ikep->nextpayload;
773 p += plen;
774 }
775
776cleanup_and_exit:
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700777 vec_free (plaintext);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778}
779
780static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700781ikev2_process_informational_req (vlib_main_t * vm, ikev2_sa_t * sa,
782 ike_header_t * ike)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783{
784 int p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700785 u32 len = clib_net_to_host_u32 (ike->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786 u8 payload = ike->nextpayload;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700787 u8 *plaintext = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700789 ike_payload_header_t *ikep;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790 u32 plen;
791
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700792 clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
793 "exchange %x flags %x msgid %x length %u",
794 clib_net_to_host_u64 (ike->ispi),
795 clib_net_to_host_u64 (ike->rspi),
796 payload, ike->version,
797 ike->exchange, ike->flags,
798 clib_net_to_host_u32 (ike->msgid), len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700800 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801
802 if (!plaintext)
803 goto cleanup_and_exit;
804
805 /* process encrypted payload */
806 p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700807 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700808 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700809 ikep = (ike_payload_header_t *) & plaintext[p];
810 plen = clib_net_to_host_u16 (ikep->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700811
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700812 if (plen < sizeof (ike_payload_header_t))
813 goto cleanup_and_exit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700815 if (payload == IKEV2_PAYLOAD_NOTIFY) /* 41 */
816 {
817 ikev2_notify_t *n = ikev2_parse_notify_payload (ikep);
818 if (n->msg_type == IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED)
819 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
820 vec_free (n);
821 }
822 else if (payload == IKEV2_PAYLOAD_DELETE) /* 42 */
823 {
824 sa->del = ikev2_parse_delete_payload (ikep);
825 }
826 else if (payload == IKEV2_PAYLOAD_VENDOR) /* 43 */
827 {
828 ikev2_parse_vendor_payload (ikep);
829 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700830 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700831 {
832 clib_warning ("unknown payload %u flags %x length %u data %u",
833 payload, ikep->flags, plen - 4,
834 format_hex_bytes, ikep->payload, plen - 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700836 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
837 {
838 sa->unsupported_cp = payload;
839 return;
840 }
841 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842
843 payload = ikep->nextpayload;
844 p += plen;
845 }
846
847cleanup_and_exit:
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700848 vec_free (plaintext);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849}
850
851static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700852ikev2_process_create_child_sa_req (vlib_main_t * vm, ikev2_sa_t * sa,
853 ike_header_t * ike)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854{
855 int p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700856 u32 len = clib_net_to_host_u32 (ike->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857 u8 payload = ike->nextpayload;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700858 u8 *plaintext = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700859 u8 rekeying = 0;
860 u8 i_nonce[IKEV2_NONCE_SIZE];
861
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700862 ike_payload_header_t *ikep;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863 u32 plen;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700864 ikev2_notify_t *n = 0;
865 ikev2_ts_t *tsi = 0;
866 ikev2_ts_t *tsr = 0;
867 ikev2_sa_proposal_t *proposal = 0;
868 ikev2_child_sa_t *child_sa;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700869
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700870 clib_warning ("ispi %lx rspi %lx nextpayload %x version %x "
871 "exchange %x flags %x msgid %x length %u",
872 clib_net_to_host_u64 (ike->ispi),
873 clib_net_to_host_u64 (ike->rspi),
874 payload, ike->version,
875 ike->exchange, ike->flags,
876 clib_net_to_host_u32 (ike->msgid), len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700877
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700878 plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879
880 if (!plaintext)
881 goto cleanup_and_exit;
882
883 /* process encrypted payload */
884 p = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700885 while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700887 ikep = (ike_payload_header_t *) & plaintext[p];
888 plen = clib_net_to_host_u16 (ikep->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700890 if (plen < sizeof (ike_payload_header_t))
891 goto cleanup_and_exit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892
893 else if (payload == IKEV2_PAYLOAD_SA)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700894 {
895 proposal = ikev2_parse_sa_payload (ikep);
896 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897 else if (payload == IKEV2_PAYLOAD_NOTIFY)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700898 {
899 n = ikev2_parse_notify_payload (ikep);
900 if (n->msg_type == IKEV2_NOTIFY_MSG_REKEY_SA)
901 {
902 rekeying = 1;
903 }
904 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905 else if (payload == IKEV2_PAYLOAD_DELETE)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700906 {
907 sa->del = ikev2_parse_delete_payload (ikep);
908 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909 else if (payload == IKEV2_PAYLOAD_VENDOR)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700910 {
911 ikev2_parse_vendor_payload (ikep);
912 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913 else if (payload == IKEV2_PAYLOAD_NONCE)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700914 {
915 clib_memcpy (i_nonce, ikep->payload, plen - sizeof (*ikep));
916 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917 else if (payload == IKEV2_PAYLOAD_TSI)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700918 {
919 tsi = ikev2_parse_ts_payload (ikep);
920 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921 else if (payload == IKEV2_PAYLOAD_TSR)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700922 {
923 tsr = ikev2_parse_ts_payload (ikep);
924 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700926 {
927 clib_warning ("unknown payload %u flags %x length %u data %u",
928 payload, ikep->flags, plen - 4,
929 format_hex_bytes, ikep->payload, plen - 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700931 if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL)
932 {
933 sa->unsupported_cp = payload;
934 return;
935 }
936 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937
938 payload = ikep->nextpayload;
939 p += plen;
940 }
941
942 if (rekeying)
943 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700944 ikev2_rekey_t *rekey;
945 child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946 if (!child_sa)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700947 {
948 clib_warning ("child SA spi %lx not found", n->spi);
949 goto cleanup_and_exit;
950 }
951 vec_add2 (sa->rekey, rekey, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952 rekey->protocol_id = n->protocol_id;
953 rekey->spi = n->spi;
954 rekey->i_proposal = proposal;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700955 rekey->r_proposal =
956 ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957 rekey->tsi = tsi;
958 rekey->tsr = tsr;
959 /* update Ni */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700960 vec_free (sa->i_nonce);
961 vec_add (sa->i_nonce, i_nonce, IKEV2_NONCE_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962 /* generate new Nr */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700963 vec_free (sa->r_nonce);
964 sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE);
965 RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700966 }
967
968cleanup_and_exit:
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700969 vec_free (plaintext);
970 vec_free (n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971}
972
973static u8 *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700974ikev2_sa_generate_authmsg (ikev2_sa_t * sa, int is_responder)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700976 u8 *authmsg = 0;
977 u8 *data;
978 u8 *nonce;
979 ikev2_id_t *id;
980 u8 *key;
981 u8 *packet_data;
982 ikev2_sa_transform_t *tr_prf;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700984 tr_prf =
985 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986
987 if (is_responder)
988 {
989 id = &sa->r_id;
990 key = sa->sk_pr;
991 nonce = sa->i_nonce;
992 packet_data = sa->last_sa_init_res_packet_data;
993 }
994 else
995 {
996 id = &sa->i_id;
997 key = sa->sk_pi;
998 nonce = sa->r_nonce;
999 packet_data = sa->last_sa_init_req_packet_data;
1000 }
1001
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001002 data = vec_new (u8, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003 data[0] = id->type;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001004 vec_append (data, id->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001006 u8 *id_hash = ikev2_calc_prf (tr_prf, key, data);
1007 vec_append (authmsg, packet_data);
1008 vec_append (authmsg, nonce);
1009 vec_append (authmsg, id_hash);
1010 vec_free (id_hash);
1011 vec_free (data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001012
1013 return authmsg;
1014}
1015
1016static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001017ikev2_ts_cmp (ikev2_ts_t * ts1, ikev2_ts_t * ts2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018{
1019 if (ts1->ts_type == ts2->ts_type && ts1->protocol_id == ts2->protocol_id &&
1020 ts1->start_port == ts2->start_port && ts1->end_port == ts2->end_port &&
1021 ts1->start_addr.as_u32 == ts2->start_addr.as_u32 &&
1022 ts1->end_addr.as_u32 == ts2->end_addr.as_u32)
1023 return 1;
1024
1025 return 0;
1026}
1027
1028static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001029ikev2_sa_match_ts (ikev2_sa_t * sa)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001031 ikev2_main_t *km = &ikev2_main;
1032 ikev2_profile_t *p;
1033 ikev2_ts_t *ts, *tsi = 0, *tsr = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001035 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036 pool_foreach (p, km->profiles, ({
1037
1038 /* check id */
1039 if (p->rem_id.type != sa->i_id.type ||
1040 vec_len(p->rem_id.data) != vec_len(sa->i_id.data) ||
1041 memcmp(p->rem_id.data, sa->i_id.data, vec_len(p->rem_id.data)))
1042 continue;
1043
1044 vec_foreach(ts, sa->childs[0].tsi)
1045 {
1046 if (ikev2_ts_cmp(&p->rem_ts, ts))
1047 {
1048 tsi = vec_dup(ts);
1049 break;
1050 }
1051 }
1052
1053 vec_foreach(ts, sa->childs[0].tsr)
1054 {
1055 if (ikev2_ts_cmp(&p->loc_ts, ts))
1056 {
1057 tsr = vec_dup(ts);
1058 break;
1059 }
1060 }
1061
1062 break;
1063 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001064 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001065
1066 if (tsi && tsr)
1067 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001068 vec_free (sa->childs[0].tsi);
1069 vec_free (sa->childs[0].tsr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070 sa->childs[0].tsi = tsi;
1071 sa->childs[0].tsr = tsr;
1072 }
1073 else
1074 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001075 vec_free (tsi);
1076 vec_free (tsr);
1077 ikev2_set_state (sa, IKEV2_STATE_TS_UNACCEPTABLE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001078 }
1079}
1080
1081static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001082ikev2_sa_auth (ikev2_sa_t * sa)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001083{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001084 ikev2_main_t *km = &ikev2_main;
1085 ikev2_profile_t *p, *sel_p = 0;
1086 u8 *authmsg, *key_pad, *psk = 0, *auth = 0;
1087 ikev2_sa_transform_t *tr_prf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001089 tr_prf =
1090 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001091
1092 /* only shared key and rsa signature */
1093 if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC ||
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001094 sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001096 clib_warning ("unsupported authentication method %u",
1097 sa->i_auth.method);
1098 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099 return;
1100 }
1101
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001102 key_pad = format (0, "%s", IKEV2_KEY_PAD);
1103 authmsg = ikev2_sa_generate_authmsg (sa, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001105 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106 pool_foreach (p, km->profiles, ({
1107
1108 /* check id */
1109 if (p->rem_id.type != sa->i_id.type ||
1110 vec_len(p->rem_id.data) != vec_len(sa->i_id.data) ||
1111 memcmp(p->rem_id.data, sa->i_id.data, vec_len(p->rem_id.data)))
1112 continue;
1113
1114 if (sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1115 {
1116 if (!p->auth.data ||
1117 p->auth.method != IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
1118 continue;
1119
1120 psk = ikev2_calc_prf(tr_prf, p->auth.data, key_pad);
1121 auth = ikev2_calc_prf(tr_prf, psk, authmsg);
1122
1123 if (!memcmp(auth, sa->i_auth.data, vec_len(sa->i_auth.data)))
1124 {
1125 ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1126 vec_free(auth);
1127 sel_p = p;
1128 break;
1129 }
1130
1131 }
1132 else if (sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
1133 {
1134 if (p->auth.method != IKEV2_AUTH_METHOD_RSA_SIG)
1135 continue;
1136
1137 if (ikev2_verify_sign(p->auth.key, sa->i_auth.data, authmsg) == 1)
1138 {
1139 ikev2_set_state(sa, IKEV2_STATE_AUTHENTICATED);
1140 sel_p = p;
1141 break;
1142 }
1143 }
1144
1145 vec_free(auth);
1146 vec_free(psk);
1147 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001148 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001149
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001150 vec_free (authmsg);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151
1152 if (sa->state == IKEV2_STATE_AUTHENTICATED)
1153 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001154 vec_free (sa->r_id.data);
1155 sa->r_id.data = vec_dup (sel_p->loc_id.data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001156 sa->r_id.type = sel_p->loc_id.type;
1157
1158 /* generate our auth data */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001159 authmsg = ikev2_sa_generate_authmsg (sa, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160 if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001161 {
1162 sa->r_auth.data = ikev2_calc_prf (tr_prf, psk, authmsg);
1163 sa->r_auth.method = IKEV2_AUTH_METHOD_SHARED_KEY_MIC;
1164 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165 else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001166 {
1167 sa->r_auth.data = ikev2_calc_sign (km->pkey, authmsg);
1168 sa->r_auth.method = IKEV2_AUTH_METHOD_RSA_SIG;
1169 }
1170 vec_free (authmsg);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171
1172 /* select transforms for 1st child sa */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001173 ikev2_sa_free_proposal_vector (&sa->childs[0].r_proposals);
1174 sa->childs[0].r_proposals =
1175 ikev2_select_proposal (sa->childs[0].i_proposals, IKEV2_PROTOCOL_ESP);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001176 }
1177 else
1178 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001179 ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001180 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001181 vec_free (psk);
1182 vec_free (key_pad);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001183}
1184
1185static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001186ikev2_create_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
1187 ikev2_child_sa_t * child)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188{
1189 ipsec_add_del_tunnel_args_t a;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001190 ikev2_sa_transform_t *tr;
1191 u8 encr_type = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192
1193 if (!child->r_proposals)
1194 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001195 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001196 return 1;
1197 }
1198
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001199 memset (&a, 0, sizeof (a));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001200 a.is_add = 1;
1201 a.local_ip.as_u32 = sa->raddr.as_u32;
1202 a.remote_ip.as_u32 = sa->iaddr.as_u32;
1203 a.local_spi = child->i_proposals[0].spi;
1204 a.remote_spi = child->r_proposals[0].spi;
1205 a.anti_replay = 1;
1206
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001207 tr =
1208 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ESN);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209 if (tr)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001210 a.esn = tr->esn_type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211 else
1212 a.esn = 0;
1213
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001214 tr =
1215 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001216 if (tr)
1217 {
1218 if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001219 {
1220 switch (tr->key_len)
1221 {
1222 case 16:
1223 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_128;
1224 break;
1225 case 24:
1226 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_192;
1227 break;
1228 case 32:
1229 encr_type = IPSEC_CRYPTO_ALG_AES_CBC_256;
1230 break;
1231 default:
1232 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1233 return 1;
1234 break;
1235 }
1236 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001237 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001238 {
1239 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1240 return 1;
1241 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242 }
1243 else
1244 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001245 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001246 return 1;
1247 }
1248
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001249 tr =
1250 ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001251 if (tr)
1252 {
1253 if (tr->integ_type != IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001254 {
1255 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
1256 return 1;
1257 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001258 }
1259 else
1260 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001261 ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262 return 1;
1263 }
1264
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001265 ikev2_calc_child_keys (sa, child);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001266
Matthew Smith2838a232016-06-21 16:05:09 -05001267 a.integ_alg = IPSEC_INTEG_ALG_SHA1_96;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001268 a.local_integ_key_len = vec_len (child->sk_ar);
1269 clib_memcpy (a.local_integ_key, child->sk_ar, a.local_integ_key_len);
1270 a.remote_integ_key_len = vec_len (child->sk_ai);
1271 clib_memcpy (a.remote_integ_key, child->sk_ai, a.remote_integ_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001272
Matthew Smith2838a232016-06-21 16:05:09 -05001273 a.crypto_alg = encr_type;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001274 a.local_crypto_key_len = vec_len (child->sk_er);
1275 clib_memcpy (a.local_crypto_key, child->sk_er, a.local_crypto_key_len);
1276 a.remote_crypto_key_len = vec_len (child->sk_ei);
1277 clib_memcpy (a.remote_crypto_key, child->sk_ei, a.remote_crypto_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001278
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001279 ipsec_add_del_tunnel_if (&a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001280
1281 return 0;
1282}
1283
1284static int
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001285ikev2_delete_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa,
1286 ikev2_child_sa_t * child)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001287{
1288 ipsec_add_del_tunnel_args_t a;
1289
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001290 if (!vec_len (child->r_proposals))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001291 return 0;
1292
1293 a.is_add = 0;
1294 a.local_ip.as_u32 = sa->raddr.as_u32;
1295 a.remote_ip.as_u32 = sa->iaddr.as_u32;
1296 a.local_spi = child->i_proposals[0].spi;
1297 a.remote_spi = child->r_proposals[0].spi;
1298
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001299 ipsec_add_del_tunnel_if (&a);
Matthew Smith2838a232016-06-21 16:05:09 -05001300 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001301}
1302
1303static u32
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001304ikev2_generate_resp (ikev2_sa_t * sa, ike_header_t * ike)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001305{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001306 v8 *integ = 0;
1307 ike_payload_header_t *ph;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001308 u16 plen;
1309 u32 tlen = 0;
1310
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001311 ikev2_sa_transform_t *tr_encr, *tr_integ;
1312 tr_encr =
1313 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR);
1314 tr_integ =
1315 ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001316
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001317 ikev2_payload_chain_t *chain = 0;
1318 ikev2_payload_new_chain (chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001319
1320 if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
1321 {
1322 if (sa->r_proposals == 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001323 {
1324 ikev2_payload_add_notify (chain,
1325 IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1326 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1327 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001328 else if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001329 {
1330 u8 *data = vec_new (u8, 2);
1331 ikev2_sa_transform_t *tr_dh;
1332 tr_dh =
1333 ikev2_sa_get_td_for_type (sa->r_proposals,
1334 IKEV2_TRANSFORM_TYPE_DH);
1335 ASSERT (tr_dh && tr_dh->dh_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001336
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001337 data[0] = (tr_dh->dh_type >> 8) & 0xff;
1338 data[1] = (tr_dh->dh_type) & 0xff;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001339
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001340 ikev2_payload_add_notify (chain,
1341 IKEV2_NOTIFY_MSG_INVALID_KE_PAYLOAD,
1342 data);
1343 vec_free (data);
1344 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1345 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001346 else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001347 {
1348 u8 *data = vec_new (u8, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001349
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001350 data[0] = sa->unsupported_cp;
1351 ikev2_payload_add_notify (chain,
1352 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1353 data);
1354 vec_free (data);
1355 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001356 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001357 {
1358 ike->rspi = clib_host_to_net_u64 (sa->rspi);
1359 ikev2_payload_add_sa (chain, sa->r_proposals);
1360 ikev2_payload_add_ke (chain, sa->dh_group, sa->r_dh_data);
1361 ikev2_payload_add_nonce (chain, sa->r_nonce);
1362 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001363 }
1364 else if (ike->exchange == IKEV2_EXCHANGE_IKE_AUTH)
1365 {
1366 if (sa->state == IKEV2_STATE_AUTHENTICATED)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001367 {
1368 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1369 ikev2_payload_add_auth (chain, &sa->r_auth);
1370 ikev2_payload_add_sa (chain, sa->childs[0].r_proposals);
1371 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1372 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
1373 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001374 else if (sa->state == IKEV2_STATE_AUTH_FAILED)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001375 {
1376 ikev2_payload_add_notify (chain,
1377 IKEV2_NOTIFY_MSG_AUTHENTICATION_FAILED,
1378 0);
1379 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1380 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001381 else if (sa->state == IKEV2_STATE_TS_UNACCEPTABLE)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001382 {
1383 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_TS_UNACCEPTABLE,
1384 0);
1385 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1386 ikev2_payload_add_auth (chain, &sa->r_auth);
1387 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001388 else if (sa->state == IKEV2_STATE_NO_PROPOSAL_CHOSEN)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001389 {
1390 ikev2_payload_add_notify (chain,
1391 IKEV2_NOTIFY_MSG_NO_PROPOSAL_CHOSEN, 0);
1392 ikev2_payload_add_id (chain, &sa->r_id, IKEV2_PAYLOAD_IDR);
1393 ikev2_payload_add_auth (chain, &sa->r_auth);
1394 ikev2_payload_add_ts (chain, sa->childs[0].tsi, IKEV2_PAYLOAD_TSI);
1395 ikev2_payload_add_ts (chain, sa->childs[0].tsr, IKEV2_PAYLOAD_TSR);
1396 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001397 else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001398 {
1399 u8 *data = vec_new (u8, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001400
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001401 data[0] = sa->unsupported_cp;
1402 ikev2_payload_add_notify (chain,
1403 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1404 data);
1405 vec_free (data);
1406 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001407 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001408 {
1409 ikev2_set_state (sa, IKEV2_STATE_DELETED);
1410 goto done;
1411 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001412 }
1413 else if (ike->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
1414 {
1415 /* if pending delete */
1416 if (sa->del)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001417 {
1418 /* The response to a request that deletes the IKE SA is an empty
1419 INFORMATIONAL response. */
1420 if (sa->del[0].protocol_id == IKEV2_PROTOCOL_IKE)
1421 {
1422 ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE);
1423 }
1424 /* The response to a request that deletes ESP or AH SAs will contain
1425 delete payloads for the paired SAs going in the other direction. */
1426 else
1427 {
1428 ikev2_payload_add_delete (chain, sa->del);
1429 }
1430 vec_free (sa->del);
1431 sa->del = 0;
1432 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001433 /* received N(AUTHENTICATION_FAILED) */
1434 else if (sa->state == IKEV2_STATE_AUTH_FAILED)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001435 {
1436 ikev2_set_state (sa, IKEV2_STATE_DELETED);
1437 goto done;
1438 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439 /* received unsupported critical payload */
1440 else if (sa->unsupported_cp)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001441 {
1442 u8 *data = vec_new (u8, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001443
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001444 data[0] = sa->unsupported_cp;
1445 ikev2_payload_add_notify (chain,
1446 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1447 data);
1448 vec_free (data);
1449 sa->unsupported_cp = 0;
1450 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001451 /* else send empty response */
1452 }
1453 else if (ike->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
1454 {
1455 if (sa->rekey)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001456 {
1457 ikev2_payload_add_sa (chain, sa->rekey[0].r_proposal);
1458 ikev2_payload_add_nonce (chain, sa->r_nonce);
1459 ikev2_payload_add_ts (chain, sa->rekey[0].tsi, IKEV2_PAYLOAD_TSI);
1460 ikev2_payload_add_ts (chain, sa->rekey[0].tsr, IKEV2_PAYLOAD_TSR);
1461 vec_del1 (sa->rekey, 0);
1462 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001463 else if (sa->unsupported_cp)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001464 {
1465 u8 *data = vec_new (u8, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001466
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001467 data[0] = sa->unsupported_cp;
1468 ikev2_payload_add_notify (chain,
1469 IKEV2_NOTIFY_MSG_UNSUPPORTED_CRITICAL_PAYLOAD,
1470 data);
1471 vec_free (data);
1472 sa->unsupported_cp = 0;
1473 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001474 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001475 {
1476 ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS,
1477 0);
1478 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001479 }
1480
1481 /* IKEv2 header */
1482 ike->version = IKE_VERSION_2;
1483 ike->flags = IKEV2_HDR_FLAG_RESPONSE;
1484 ike->nextpayload = IKEV2_PAYLOAD_SK;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001485 tlen = sizeof (*ike);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001486
1487
1488 if (ike->exchange == IKEV2_EXCHANGE_SA_INIT)
1489 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001490 tlen += vec_len (chain->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001491 ike->nextpayload = chain->first_payload_type;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001492 ike->length = clib_host_to_net_u32 (tlen);
1493 clib_memcpy (ike->payload, chain->data, vec_len (chain->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001494
1495 /* store whole IKE payload - needed for PSK auth */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001496 vec_free (sa->last_sa_init_res_packet_data);
1497 vec_add (sa->last_sa_init_res_packet_data, ike, tlen);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001498 }
1499 else
1500 {
1501
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001502 ikev2_payload_chain_add_padding (chain, tr_encr->block_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001503
1504 /* SK payload */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001505 plen = sizeof (*ph);
1506 ph = (ike_payload_header_t *) & ike->payload[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001507 ph->nextpayload = chain->first_payload_type;
1508 ph->flags = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001509 int enc_len = ikev2_encrypt_data (sa, chain->data, ph->payload);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001510 plen += enc_len;
1511
1512 /* add space for hmac */
1513 plen += tr_integ->key_trunc;
1514 tlen += plen;
1515
1516 /* payload and total length */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001517 ph->length = clib_host_to_net_u16 (plen);
1518 ike->length = clib_host_to_net_u32 (tlen);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001519
1520 /* calc integrity data for whole packet except hash itself */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001521 integ = ikev2_calc_integr (tr_integ, sa->sk_ar, (u8 *) ike,
1522 tlen - tr_integ->key_trunc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001523
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001524 clib_memcpy (ike->payload + tlen - tr_integ->key_trunc - sizeof (*ike),
1525 integ, tr_integ->key_trunc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001526
1527 /* store whole IKE payload - needed for retransmit */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001528 vec_free (sa->last_res_packet_data);
1529 vec_add (sa->last_res_packet_data, ike, tlen);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001530 }
1531
1532done:
1533 ikev2_payload_destroy_chain (chain);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001534 vec_free (integ);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001535 return tlen;
1536}
1537
1538static int
1539ikev2_retransmit_sa_init (ike_header_t * ike,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001540 ip4_address_t iaddr, ip4_address_t raddr)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001541{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001542 ikev2_main_t *km = &ikev2_main;
1543 ikev2_sa_t *sa;
1544 u32 cpu_index = os_get_cpu_number ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001545
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001546 /* *INDENT-OFF* */
Matthew Smith2838a232016-06-21 16:05:09 -05001547 pool_foreach (sa, km->per_thread_data[cpu_index].sas, ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07001548 if (sa->ispi == clib_net_to_host_u64(ike->ispi) &&
1549 sa->iaddr.as_u32 == iaddr.as_u32 &&
1550 sa->raddr.as_u32 == raddr.as_u32)
1551 {
1552 int p = 0;
1553 u32 len = clib_net_to_host_u32(ike->length);
1554 u8 payload = ike->nextpayload;
1555
1556 while (p < len && payload!= IKEV2_PAYLOAD_NONE) {
1557 ike_payload_header_t * ikep = (ike_payload_header_t *) &ike->payload[p];
1558 u32 plen = clib_net_to_host_u16(ikep->length);
1559
1560 if (plen < sizeof(ike_payload_header_t))
1561 return -1;
1562
1563 if (payload == IKEV2_PAYLOAD_NONCE)
1564 {
1565 if (!memcmp(sa->i_nonce, ikep->payload, plen - sizeof(*ikep)))
1566 {
1567 /* req is retransmit */
1568 if (sa->state == IKEV2_STATE_SA_INIT)
1569 {
1570 ike_header_t * tmp;
1571 tmp = (ike_header_t*)sa->last_sa_init_res_packet_data;
1572 ike->ispi = tmp->ispi;
1573 ike->rspi = tmp->rspi;
1574 ike->nextpayload = tmp->nextpayload;
1575 ike->version = tmp->version;
1576 ike->exchange = tmp->exchange;
1577 ike->flags = tmp->flags;
1578 ike->msgid = tmp->msgid;
1579 ike->length = tmp->length;
Damjan Marionf1213b82016-03-13 02:22:06 +01001580 clib_memcpy(ike->payload, tmp->payload,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001581 clib_net_to_host_u32(tmp->length) - sizeof(*ike));
1582 clib_warning("IKE_SA_INIT retransmit from %U to %U",
1583 format_ip4_address, &raddr,
1584 format_ip4_address, &iaddr);
1585 return 1;
1586 }
1587 /* else ignore req */
1588 else
1589 {
1590 clib_warning("IKE_SA_INIT ignore from %U to %U",
1591 format_ip4_address, &raddr,
1592 format_ip4_address, &iaddr);
1593 return -1;
1594 }
1595 }
1596 }
1597 payload = ikep->nextpayload;
1598 p+=plen;
1599 }
1600 }
1601 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001602 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001603
1604 /* req is not retransmit */
1605 return 0;
1606}
1607
1608static int
1609ikev2_retransmit_resp (ikev2_sa_t * sa, ike_header_t * ike)
1610{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001611 u32 msg_id = clib_net_to_host_u32 (ike->msgid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001612
1613 /* new req */
1614 if (msg_id > sa->last_msg_id)
1615 {
1616 sa->last_msg_id = msg_id;
1617 return 0;
1618 }
1619 /* retransmitted req */
1620 else if (msg_id == sa->last_msg_id)
1621 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001622 ike_header_t *tmp;
1623 tmp = (ike_header_t *) sa->last_res_packet_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001624 ike->ispi = tmp->ispi;
1625 ike->rspi = tmp->rspi;
1626 ike->nextpayload = tmp->nextpayload;
1627 ike->version = tmp->version;
1628 ike->exchange = tmp->exchange;
1629 ike->flags = tmp->flags;
1630 ike->msgid = tmp->msgid;
1631 ike->length = tmp->length;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001632 clib_memcpy (ike->payload, tmp->payload,
1633 clib_net_to_host_u32 (tmp->length) - sizeof (*ike));
1634 clib_warning ("IKE msgid %u retransmit from %U to %U",
1635 msg_id,
1636 format_ip4_address, &sa->raddr,
1637 format_ip4_address, &sa->iaddr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001638 return 1;
1639 }
1640 /* old req ignore */
1641 else
1642 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001643 clib_warning ("IKE msgid %u req ignore from %U to %U",
1644 msg_id,
1645 format_ip4_address, &sa->raddr,
1646 format_ip4_address, &sa->iaddr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001647 return -1;
1648 }
1649}
1650
1651static uword
1652ikev2_node_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001653 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001654{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001655 u32 n_left_from, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001656 ikev2_next_t next_index;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001657 ikev2_main_t *km = &ikev2_main;
1658 u32 cpu_index = os_get_cpu_number ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001659
1660 from = vlib_frame_vector_args (frame);
1661 n_left_from = frame->n_vectors;
1662 next_index = node->cached_next_index;
1663
1664 while (n_left_from > 0)
1665 {
1666 u32 n_left_to_next;
1667
1668 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1669
1670 while (n_left_from > 0 && n_left_to_next > 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001671 {
1672 u32 bi0;
1673 vlib_buffer_t *b0;
1674 u32 next0 = IKEV2_NEXT_ERROR_DROP;
1675 u32 sw_if_index0;
1676 ip4_header_t *ip40;
1677 udp_header_t *udp0;
1678 ike_header_t *ike0;
1679 ikev2_sa_t *sa0 = 0;
Damjan Marion3f54b182016-08-16 11:27:02 +02001680 ikev2_sa_t sa; /* temporary store for SA */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001681 int len = 0;
1682 int r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001683
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001684 /* speculatively enqueue b0 to the current next frame */
1685 bi0 = from[0];
1686 to_next[0] = bi0;
1687 from += 1;
1688 to_next += 1;
1689 n_left_from -= 1;
1690 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001691
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001692 b0 = vlib_get_buffer (vm, bi0);
1693 ike0 = vlib_buffer_get_current (b0);
1694 vlib_buffer_advance (b0, -sizeof (*udp0));
1695 udp0 = vlib_buffer_get_current (b0);
1696 vlib_buffer_advance (b0, -sizeof (*ip40));
1697 ip40 = vlib_buffer_get_current (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001698
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001699 if (ike0->version != IKE_VERSION_2)
1700 {
1701 vlib_node_increment_counter (vm, ikev2_node.index,
1702 IKEV2_ERROR_NOT_IKEV2, 1);
1703 goto dispatch0;
1704 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001705
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001706 if (ike0->exchange == IKEV2_EXCHANGE_SA_INIT)
1707 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001708 sa0 = &sa;
1709 memset (sa0, 0, sizeof (*sa0));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001710
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001711 if (ike0->rspi == 0)
1712 {
1713 sa0->raddr.as_u32 = ip40->dst_address.as_u32;
1714 sa0->iaddr.as_u32 = ip40->src_address.as_u32;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001715
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001716 r = ikev2_retransmit_sa_init (ike0, sa0->iaddr, sa0->raddr);
1717 if (r == 1)
1718 {
1719 vlib_node_increment_counter (vm, ikev2_node.index,
1720 IKEV2_ERROR_IKE_SA_INIT_RETRANSMIT,
1721 1);
1722 len = clib_net_to_host_u32 (ike0->length);
1723 goto dispatch0;
1724 }
1725 else if (r == -1)
1726 {
1727 vlib_node_increment_counter (vm, ikev2_node.index,
1728 IKEV2_ERROR_IKE_SA_INIT_IGNORE,
1729 1);
1730 goto dispatch0;
1731 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001732
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001733 ikev2_process_sa_init_req (vm, sa0, ike0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001734
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001735 if (sa0->state == IKEV2_STATE_SA_INIT)
1736 {
1737 ikev2_sa_free_proposal_vector (&sa0->r_proposals);
1738 sa0->r_proposals =
1739 ikev2_select_proposal (sa0->i_proposals,
1740 IKEV2_PROTOCOL_IKE);
1741 ikev2_generate_sa_init_data (sa0);
1742 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001743
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001744 if (sa0->state == IKEV2_STATE_SA_INIT ||
1745 sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE)
1746 {
1747 len = ikev2_generate_resp (sa0, ike0);
1748 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001749
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001750 if (sa0->state == IKEV2_STATE_SA_INIT)
1751 {
1752 /* add SA to the pool */
1753 pool_get (km->per_thread_data[cpu_index].sas, sa0);
1754 clib_memcpy (sa0, &sa, sizeof (*sa0));
1755 hash_set (km->per_thread_data[cpu_index].sa_by_rspi,
1756 sa0->rspi,
1757 sa0 - km->per_thread_data[cpu_index].sas);
1758 }
1759 else
1760 {
1761 ikev2_sa_free_all_vec (sa0);
1762 }
1763 }
1764 }
1765 else if (ike0->exchange == IKEV2_EXCHANGE_IKE_AUTH)
1766 {
1767 uword *p;
1768 p = hash_get (km->per_thread_data[cpu_index].sa_by_rspi,
1769 clib_net_to_host_u64 (ike0->rspi));
1770 if (p)
1771 {
1772 sa0 = pool_elt_at_index (km->per_thread_data[cpu_index].sas,
1773 p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001774
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001775 r = ikev2_retransmit_resp (sa0, ike0);
1776 if (r == 1)
1777 {
1778 vlib_node_increment_counter (vm, ikev2_node.index,
1779 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
1780 1);
1781 len = clib_net_to_host_u32 (ike0->length);
1782 goto dispatch0;
1783 }
1784 else if (r == -1)
1785 {
1786 vlib_node_increment_counter (vm, ikev2_node.index,
1787 IKEV2_ERROR_IKE_REQ_IGNORE,
1788 1);
1789 goto dispatch0;
1790 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001791
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001792 ikev2_process_auth_req (vm, sa0, ike0);
1793 ikev2_sa_auth (sa0);
1794 if (sa0->state == IKEV2_STATE_AUTHENTICATED)
1795 {
1796 ikev2_initial_contact_cleanup (sa0);
1797 ikev2_sa_match_ts (sa0);
1798 if (sa0->state != IKEV2_STATE_TS_UNACCEPTABLE)
1799 ikev2_create_tunnel_interface (km->vnet_main, sa0,
1800 &sa0->childs[0]);
1801 }
1802 len = ikev2_generate_resp (sa0, ike0);
1803 }
1804 }
1805 else if (ike0->exchange == IKEV2_EXCHANGE_INFORMATIONAL)
1806 {
1807 uword *p;
1808 p = hash_get (km->per_thread_data[cpu_index].sa_by_rspi,
1809 clib_net_to_host_u64 (ike0->rspi));
1810 if (p)
1811 {
1812 sa0 = pool_elt_at_index (km->per_thread_data[cpu_index].sas,
1813 p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001814
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001815 r = ikev2_retransmit_resp (sa0, ike0);
1816 if (r == 1)
1817 {
1818 vlib_node_increment_counter (vm, ikev2_node.index,
1819 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
1820 1);
1821 len = clib_net_to_host_u32 (ike0->length);
1822 goto dispatch0;
1823 }
1824 else if (r == -1)
1825 {
1826 vlib_node_increment_counter (vm, ikev2_node.index,
1827 IKEV2_ERROR_IKE_REQ_IGNORE,
1828 1);
1829 goto dispatch0;
1830 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001831
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001832 ikev2_process_informational_req (vm, sa0, ike0);
1833 if (sa0->del)
1834 {
1835 if (sa0->del[0].protocol_id != IKEV2_PROTOCOL_IKE)
1836 {
1837 ikev2_delete_t *d, *tmp, *resp = 0;
1838 vec_foreach (d, sa0->del)
1839 {
1840 ikev2_child_sa_t *ch_sa;
1841 ch_sa = ikev2_sa_get_child (sa0, d->spi,
1842 d->protocol_id);
1843 if (ch_sa)
1844 {
1845 ikev2_delete_tunnel_interface (km->vnet_main,
1846 sa0, ch_sa);
1847 vec_add2 (resp, tmp, 1);
1848 tmp->protocol_id = d->protocol_id;
1849 tmp->spi = ch_sa->r_proposals[0].spi;
1850 ikev2_sa_del_child_sa (sa0, ch_sa);
1851 }
1852 }
1853 vec_free (sa0->del);
1854 sa0->del = resp;
1855 }
1856 }
1857 len = ikev2_generate_resp (sa0, ike0);
1858 }
1859 }
1860 else if (ike0->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA)
1861 {
1862 uword *p;
1863 p = hash_get (km->per_thread_data[cpu_index].sa_by_rspi,
1864 clib_net_to_host_u64 (ike0->rspi));
1865 if (p)
1866 {
1867 sa0 = pool_elt_at_index (km->per_thread_data[cpu_index].sas,
1868 p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001869
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001870 r = ikev2_retransmit_resp (sa0, ike0);
1871 if (r == 1)
1872 {
1873 vlib_node_increment_counter (vm, ikev2_node.index,
1874 IKEV2_ERROR_IKE_REQ_RETRANSMIT,
1875 1);
1876 len = clib_net_to_host_u32 (ike0->length);
1877 goto dispatch0;
1878 }
1879 else if (r == -1)
1880 {
1881 vlib_node_increment_counter (vm, ikev2_node.index,
1882 IKEV2_ERROR_IKE_REQ_IGNORE,
1883 1);
1884 goto dispatch0;
1885 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001887 ikev2_process_create_child_sa_req (vm, sa0, ike0);
1888 if (sa0->rekey)
1889 {
1890 if (sa0->rekey[0].protocol_id != IKEV2_PROTOCOL_IKE)
1891 {
1892 ikev2_child_sa_t *child;
1893 vec_add2 (sa0->childs, child, 1);
1894 child->r_proposals = sa0->rekey[0].r_proposal;
1895 child->i_proposals = sa0->rekey[0].i_proposal;
1896 child->tsi = sa0->rekey[0].tsi;
1897 child->tsr = sa0->rekey[0].tsr;
1898 ikev2_create_tunnel_interface (km->vnet_main, sa0,
1899 child);
1900 }
1901 len = ikev2_generate_resp (sa0, ike0);
1902 }
1903 }
1904 }
1905 else
1906 {
1907 clib_warning ("IKEv2 exchange %u packet received from %U to %U",
1908 ike0->exchange,
1909 format_ip4_address, ip40->src_address.as_u8,
1910 format_ip4_address, ip40->dst_address.as_u8);
1911 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001912
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001913 dispatch0:
1914 /* if we are sending packet back, rewrite headers */
1915 if (len)
1916 {
1917 next0 = IKEV2_NEXT_IP4_LOOKUP;
1918 ip40->dst_address.as_u32 = sa0->iaddr.as_u32;
1919 ip40->src_address.as_u32 = sa0->raddr.as_u32;
1920 udp0->length =
1921 clib_host_to_net_u16 (len + sizeof (udp_header_t));
1922 udp0->checksum = 0;
1923 b0->current_length =
1924 len + sizeof (ip4_header_t) + sizeof (udp_header_t);
1925 ip40->length = clib_host_to_net_u16 (b0->current_length);
1926 ip40->checksum = ip4_header_checksum (ip40);
1927 }
1928 /* delete sa */
1929 if (sa0 && (sa0->state == IKEV2_STATE_DELETED ||
1930 sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE))
1931 {
1932 ikev2_child_sa_t *c;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001933
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001934 vec_foreach (c, sa0->childs)
1935 ikev2_delete_tunnel_interface (km->vnet_main, sa0, c);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001936
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001937 ikev2_delete_sa (sa0);
1938 }
1939 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001940
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001941 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1942 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1943 {
1944 ikev2_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1945 t->sw_if_index = sw_if_index0;
1946 t->next_index = next0;
1947 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001948
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001949 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1950 n_left_to_next, bi0, next0);
1951 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001952
1953 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1954 }
1955
1956 vlib_node_increment_counter (vm, ikev2_node.index,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001957 IKEV2_ERROR_PROCESSED, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001958 return frame->n_vectors;
1959}
1960
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001961/* *INDENT-OFF* */
Jean-Mickael Guerin8941ec22016-03-04 14:14:21 +01001962VLIB_REGISTER_NODE (ikev2_node,static) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001963 .function = ikev2_node_fn,
1964 .name = "ikev2",
1965 .vector_size = sizeof (u32),
1966 .format_trace = format_ikev2_trace,
1967 .type = VLIB_NODE_TYPE_INTERNAL,
1968
1969 .n_errors = ARRAY_LEN(ikev2_error_strings),
1970 .error_strings = ikev2_error_strings,
1971
1972 .n_next_nodes = IKEV2_N_NEXT,
1973
1974 .next_nodes = {
1975 [IKEV2_NEXT_IP4_LOOKUP] = "ip4-lookup",
1976 [IKEV2_NEXT_ERROR_DROP] = "error-drop",
1977 },
1978};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001979/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001980
1981
1982static ikev2_profile_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001983ikev2_profile_index_by_name (u8 * name)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001984{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001985 ikev2_main_t *km = &ikev2_main;
1986 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001987
1988 p = mhash_get (&km->profile_index_by_name, name);
1989 if (!p)
1990 return 0;
1991
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001992 return pool_elt_at_index (km->profiles, p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001993}
1994
1995clib_error_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001996ikev2_set_local_key (vlib_main_t * vm, u8 * file)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001997{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001998 ikev2_main_t *km = &ikev2_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001999
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002000 km->pkey = ikev2_load_key_file (file);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002001 if (km->pkey == NULL)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002002 return clib_error_return (0, "load key '%s' failed", file);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002003
2004 return 0;
2005}
2006
2007clib_error_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002008ikev2_add_del_profile (vlib_main_t * vm, u8 * name, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002009{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002010 ikev2_main_t *km = &ikev2_main;
2011 ikev2_profile_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002012
2013 if (is_add)
2014 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002015 if (ikev2_profile_index_by_name (name))
2016 return clib_error_return (0, "policy %v already exists", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002017
2018 pool_get (km->profiles, p);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002019 memset (p, 0, sizeof (*p));
2020 p->name = vec_dup (name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002021 uword index = p - km->profiles;
2022 mhash_set_mem (&km->profile_index_by_name, name, &index, 0);
2023 }
2024 else
2025 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002026 p = ikev2_profile_index_by_name (name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002027 if (!p)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002028 return clib_error_return (0, "policy %v does not exists", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002029
2030 vec_free (p->name);
2031 pool_put (km->profiles, p);
2032 mhash_unset (&km->profile_index_by_name, name, 0);
2033 }
2034 return 0;
2035}
2036
2037clib_error_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002038ikev2_set_profile_auth (vlib_main_t * vm, u8 * name, u8 auth_method,
2039 u8 * auth_data, u8 data_hex_format)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002040{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002041 ikev2_profile_t *p;
2042 clib_error_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002043
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002044 p = ikev2_profile_index_by_name (name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002045
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002046 if (!p)
2047 {
2048 r = clib_error_return (0, "unknown profile %v", name);
2049 return r;
2050 }
2051 vec_free (p->auth.data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002052 p->auth.method = auth_method;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002053 p->auth.data = vec_dup (auth_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002054 p->auth.hex = data_hex_format;
2055
2056 if (auth_method == IKEV2_AUTH_METHOD_RSA_SIG)
2057 {
Matus Fabian698b9522016-09-13 23:15:56 -07002058 vec_add1 (p->auth.data, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002059 if (p->auth.key)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002060 EVP_PKEY_free (p->auth.key);
2061 p->auth.key = ikev2_load_cert_file (auth_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002062 if (p->auth.key == NULL)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002063 return clib_error_return (0, "load cert '%s' failed", auth_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002064 }
2065
2066 return 0;
2067}
2068
2069clib_error_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002070ikev2_set_profile_id (vlib_main_t * vm, u8 * name, u8 id_type, u8 * data,
2071 int is_local)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002072{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002073 ikev2_profile_t *p;
2074 clib_error_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002075
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002076 if (id_type > IKEV2_ID_TYPE_ID_RFC822_ADDR
2077 && id_type < IKEV2_ID_TYPE_ID_KEY_ID)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002078 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002079 r = clib_error_return (0, "unsupported identity type %U",
2080 format_ikev2_id_type, id_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002081 return r;
2082 }
2083
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002084 p = ikev2_profile_index_by_name (name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002085
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002086 if (!p)
2087 {
2088 r = clib_error_return (0, "unknown profile %v", name);
2089 return r;
2090 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002091
2092 if (is_local)
2093 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002094 vec_free (p->loc_id.data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002095 p->loc_id.type = id_type;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002096 p->loc_id.data = vec_dup (data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002097 }
2098 else
2099 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002100 vec_free (p->rem_id.data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002101 p->rem_id.type = id_type;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002102 p->rem_id.data = vec_dup (data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002103 }
2104
2105 return 0;
2106}
2107
2108clib_error_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002109ikev2_set_profile_ts (vlib_main_t * vm, u8 * name, u8 protocol_id,
2110 u16 start_port, u16 end_port, ip4_address_t start_addr,
2111 ip4_address_t end_addr, int is_local)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002112{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002113 ikev2_profile_t *p;
2114 clib_error_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002115
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002116 p = ikev2_profile_index_by_name (name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002117
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002118 if (!p)
2119 {
2120 r = clib_error_return (0, "unknown profile %v", name);
2121 return r;
2122 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002123
2124 if (is_local)
2125 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002126 p->loc_ts.start_addr.as_u32 = start_addr.as_u32;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002127 p->loc_ts.end_addr.as_u32 = end_addr.as_u32;
2128 p->loc_ts.start_port = start_port;
2129 p->loc_ts.end_port = end_port;
2130 p->loc_ts.protocol_id = protocol_id;
2131 p->loc_ts.ts_type = 7;
2132 }
2133 else
2134 {
2135 p->rem_ts.start_addr.as_u32 = start_addr.as_u32;
2136 p->rem_ts.end_addr.as_u32 = end_addr.as_u32;
2137 p->rem_ts.start_port = start_port;
2138 p->rem_ts.end_port = end_port;
2139 p->rem_ts.protocol_id = protocol_id;
2140 p->rem_ts.ts_type = 7;
2141 }
2142
2143 return 0;
2144}
2145
2146
2147clib_error_t *
2148ikev2_init (vlib_main_t * vm)
2149{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002150 ikev2_main_t *km = &ikev2_main;
2151 clib_error_t *error;
2152 vlib_thread_main_t *tm = vlib_get_thread_main ();
Matthew Smith2838a232016-06-21 16:05:09 -05002153 int thread_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002154
2155 memset (km, 0, sizeof (ikev2_main_t));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002156 km->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002157 km->vlib_main = vm;
2158
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002159 ikev2_crypto_init (km);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002160
Ed Warnickecb9cada2015-12-08 15:45:58 -07002161 mhash_init_vec_string (&km->profile_index_by_name, sizeof (uword));
2162
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002163 vec_validate (km->per_thread_data, tm->n_vlib_mains - 1);
Matthew Smith2838a232016-06-21 16:05:09 -05002164 for (thread_id = 0; thread_id < tm->n_vlib_mains - 1; thread_id++)
2165 {
2166 km->per_thread_data[thread_id].sa_by_rspi =
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002167 hash_create (0, sizeof (uword));
Matthew Smith2838a232016-06-21 16:05:09 -05002168 }
2169
Ed Warnickecb9cada2015-12-08 15:45:58 -07002170 if ((error = vlib_call_init_function (vm, ikev2_cli_init)))
2171 return error;
2172
2173 udp_register_dst_port (vm, 500, ikev2_node.index, 1);
2174
2175 return 0;
2176}
2177
2178
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07002179
2180/*
2181 * fd.io coding-style-patch-verification: ON
2182 *
2183 * Local Variables:
2184 * eval: (c-set-style "gnu")
2185 * End:
2186 */