blob: dd14812b5501971f38105e54995125df2bf19d06 [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
Damjan Marione936bbe2016-02-25 23:17:38 +010016#include <ctype.h>
17
Ed Warnickecb9cada2015-12-08 15:45:58 -070018#include <vnet/vnet.h>
19#include <vnet/api_errno.h>
20#include <vnet/ip/ip.h>
21#include <vnet/interface.h>
22
23#include <vnet/ipsec/ipsec.h>
24#include <vnet/ipsec/ikev2.h>
25#include <vnet/ipsec/ikev2_priv.h>
26
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070027/* *INDENT-OFF* */
28typedef CLIB_PACKED (struct
29 {
30 u8 nextpayload;
31 u8 flags;
32 u16 length;
33 u8 protocol_id;
34 u8 spi_size;
35 u16 msg_type;
36 u8 payload[0];}) ike_notify_payload_header_t;
37/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070038
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070039/* *INDENT-OFF* */
40typedef CLIB_PACKED (struct
41 {
42 u8 ts_type;
43 u8 protocol_id;
44 u16 selector_len;
45 u16 start_port;
46 u16 end_port;
47 ip4_address_t start_addr;
48 ip4_address_t end_addr;}) ikev2_ts_payload_entry_t;
49/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070051/* *INDENT-OFF* */
52typedef CLIB_PACKED (struct
53 {
54 u8 nextpayload;
55 u8 flags;
56 u16 length;
57 u8 num_ts;
58 u8 reserved[3];
59 ikev2_ts_payload_entry_t ts[0];})
60 ike_ts_payload_header_t;
61/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070062
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070063/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070064typedef CLIB_PACKED (struct {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070065 u8 last_or_more;
66 u8 reserved;
67 u16 proposal_len;
68 u8 proposal_num;
69 u8 protocol_id;
70 u8 spi_size;
71 u8 num_transforms; u32 spi[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -070072}) ike_sa_proposal_data_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070073/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070075/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070076typedef CLIB_PACKED (struct {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070077 u8 last_or_more;
78 u8 reserved;
79 u16 transform_len;
80 u8 transform_type;
81 u8 reserved2;
82 u16 transform_id;
83 u8 attributes[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -070084}) ike_sa_transform_data_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070085/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070087/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070088typedef CLIB_PACKED (struct {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070089 u8 nextpayload;
90 u8 flags;
91 u16 length;
92 u8 protocol_id;
93 u8 spi_size;
94 u16 num_of_spi;
95 u32 spi[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -070096}) ike_delete_payload_header_t;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070097/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
99static ike_payload_header_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700100ikev2_payload_add_hdr (ikev2_payload_chain_t * c, u8 payload_type, int len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700102 ike_payload_header_t *hdr =
103 (ike_payload_header_t *) & c->data[c->last_hdr_off];
104 u8 *tmp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105
106 if (c->data)
107 hdr->nextpayload = payload_type;
108 else
109 c->first_payload_type = payload_type;
110
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700111 c->last_hdr_off = vec_len (c->data);
112 vec_add2 (c->data, tmp, len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 hdr = (ike_payload_header_t *) tmp;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700114 memset (hdr, 0, len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700116 hdr->length = clib_host_to_net_u16 (len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117
118 return hdr;
119}
120
121static void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700122ikev2_payload_add_data (ikev2_payload_chain_t * c, u8 * data)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123{
124 u16 len;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700125 ike_payload_header_t *hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700127 vec_append (c->data, data);
128 hdr = (ike_payload_header_t *) & c->data[c->last_hdr_off];
129 len = clib_net_to_host_u16 (hdr->length);
130 hdr->length = clib_host_to_net_u16 (len + vec_len (data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131}
132
133void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700134ikev2_payload_add_notify (ikev2_payload_chain_t * c, u16 msg_type, u8 * data)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700136 ike_notify_payload_header_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700138 n =
139 (ike_notify_payload_header_t *) ikev2_payload_add_hdr (c,
140 IKEV2_PAYLOAD_NOTIFY,
141 sizeof (*n));
142 n->msg_type = clib_host_to_net_u16 (msg_type);
143 ikev2_payload_add_data (c, data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144}
145
146void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700147ikev2_payload_add_sa (ikev2_payload_chain_t * c,
148 ikev2_sa_proposal_t * proposals)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700150 ike_payload_header_t *ph;
151 ike_sa_proposal_data_t *prop;
152 ike_sa_transform_data_t *tr;
153 ikev2_sa_proposal_t *p;
154 ikev2_sa_transform_t *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700156 u8 *tmp;
157 u8 *pr_data = 0;
158 u8 *tr_data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700160 ikev2_payload_add_hdr (c, IKEV2_PAYLOAD_SA, sizeof (*ph));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700162 vec_foreach (p, proposals)
163 {
164 int spi_size = (p->protocol_id == IKEV2_PROTOCOL_ESP) ? 4 : 0;
165 pr_data = vec_new (u8, sizeof (ike_sa_proposal_data_t) + spi_size);
166 prop = (ike_sa_proposal_data_t *) pr_data;
167 prop->last_or_more = proposals - p + 1 < vec_len (proposals) ? 2 : 0;
168 prop->protocol_id = p->protocol_id;
169 prop->proposal_num = p->proposal_num;
170 prop->spi_size = spi_size;
171 prop->num_transforms = vec_len (p->transforms);
172
173 if (spi_size)
174 prop->spi[0] = clib_host_to_net_u32 (p->spi);
175
176 DBG_PLD ("proposal num %u protocol_id %u last_or_more %u spi_size %u%s%U",
177 prop->proposal_num, prop->protocol_id, prop->last_or_more,
178 prop->spi_size, prop->spi_size ? " spi_data " : "",
179 format_hex_bytes, prop->spi, prop->spi_size);
180
181 vec_foreach (t, p->transforms)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700183 vec_add2 (tr_data, tmp, sizeof (*tr) + vec_len (t->attrs));
184 tr = (ike_sa_transform_data_t *) tmp;
185 tr->last_or_more =
186 ((t - p->transforms) + 1 < vec_len (p->transforms)) ? 3 : 0;
187 tr->transform_type = t->type;
188 tr->transform_id = clib_host_to_net_u16 (t->transform_id);
189 tr->transform_len =
190 clib_host_to_net_u16 (sizeof (*tr) + vec_len (t->attrs));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700192 if (vec_len (t->attrs) > 0)
193 clib_memcpy (tr->attributes, t->attrs, vec_len (t->attrs));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700195 DBG_PLD
196 ("transform type %U transform_id %u last_or_more %u attr_size %u%s%U",
197 format_ikev2_transform_type, tr->transform_type, t->transform_id,
198 tr->last_or_more, vec_len (t->attrs),
199 vec_len (t->attrs) ? " attrs " : "", format_hex_bytes,
200 tr->attributes, vec_len (t->attrs));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700202
203 prop->proposal_len =
204 clib_host_to_net_u16 (vec_len (tr_data) + vec_len (pr_data));
205 ikev2_payload_add_data (c, pr_data);
206 ikev2_payload_add_data (c, tr_data);
207 vec_free (pr_data);
208 vec_free (tr_data);
209 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210}
211
212void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700213ikev2_payload_add_ke (ikev2_payload_chain_t * c, u16 dh_group, u8 * dh_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700215 ike_ke_payload_header_t *ke;
216 ke = (ike_ke_payload_header_t *) ikev2_payload_add_hdr (c, IKEV2_PAYLOAD_KE,
217 sizeof (*ke));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700219 ke->dh_group = clib_host_to_net_u16 (dh_group);
220 ikev2_payload_add_data (c, dh_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221}
222
223void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700224ikev2_payload_add_nonce (ikev2_payload_chain_t * c, u8 * nonce)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700226 ikev2_payload_add_hdr (c, IKEV2_PAYLOAD_NONCE,
227 sizeof (ike_payload_header_t));
228 ikev2_payload_add_data (c, nonce);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229}
230
231void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700232ikev2_payload_add_id (ikev2_payload_chain_t * c, ikev2_id_t * id, u8 type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700234 ike_id_payload_header_t *idp;
235 idp =
236 (ike_id_payload_header_t *) ikev2_payload_add_hdr (c, type,
237 sizeof (*idp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
239 idp->id_type = id->type;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700240 ikev2_payload_add_data (c, id->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241}
242
243void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700244ikev2_payload_add_delete (ikev2_payload_chain_t * c, ikev2_delete_t * d)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700246 ike_delete_payload_header_t *dp;
247 u16 num_of_spi = vec_len (d);
248 ikev2_delete_t *d2;
249 dp =
250 (ike_delete_payload_header_t *) ikev2_payload_add_hdr (c,
251 IKEV2_PAYLOAD_DELETE,
252 sizeof (*dp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253
254 if (d[0].protocol_id == IKEV2_PROTOCOL_IKE)
255 {
256 dp->protocol_id = 1;
257 }
258 else
259 {
260 dp->protocol_id = d[0].protocol_id;
261 dp->spi_size = 4;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700262 dp->num_of_spi = clib_host_to_net_u16 (num_of_spi);
263 vec_foreach (d2, d)
264 {
265 u8 *data = vec_new (u8, 4);
266 u32 spi = clib_host_to_net_u32 (d2->spi);
267 clib_memcpy (data, &spi, 4);
268 ikev2_payload_add_data (c, data);
269 vec_free (data);
270 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 }
272}
273
274void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700275ikev2_payload_add_auth (ikev2_payload_chain_t * c, ikev2_auth_t * auth)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700277 ike_auth_payload_header_t *ap;
278 ap =
279 (ike_auth_payload_header_t *) ikev2_payload_add_hdr (c,
280 IKEV2_PAYLOAD_AUTH,
281 sizeof (*ap));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
283 ap->auth_method = auth->method;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700284 ikev2_payload_add_data (c, auth->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285}
286
287void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700288ikev2_payload_add_ts (ikev2_payload_chain_t * c, ikev2_ts_t * ts, u8 type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700290 ike_ts_payload_header_t *tsh;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 ikev2_ts_t *ts2;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700292 u8 *data = 0, *tmp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700294 tsh =
295 (ike_ts_payload_header_t *) ikev2_payload_add_hdr (c, type,
296 sizeof (*tsh));
297 tsh->num_ts = vec_len (ts);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700299 vec_foreach (ts2, ts)
300 {
301 ASSERT (ts2->ts_type == 7); /*TS_IPV4_ADDR_RANGE */
302 ikev2_ts_payload_entry_t *entry;
303 vec_add2 (data, tmp, sizeof (*entry));
304 entry = (ikev2_ts_payload_entry_t *) tmp;
305 entry->ts_type = ts2->ts_type;
306 entry->protocol_id = ts2->protocol_id;
307 entry->selector_len = clib_host_to_net_u16 (16);
308 entry->start_port = clib_host_to_net_u16 (ts2->start_port);
309 entry->end_port = clib_host_to_net_u16 (ts2->end_port);
310 entry->start_addr.as_u32 = ts2->start_addr.as_u32;
311 entry->end_addr.as_u32 = ts2->end_addr.as_u32;
312 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700314 ikev2_payload_add_data (c, data);
315 vec_free (data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316}
317
318void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700319ikev2_payload_chain_add_padding (ikev2_payload_chain_t * c, int bs)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700321 u8 *tmp __attribute__ ((unused));
322 u8 pad_len = (vec_len (c->data) / bs + 1) * bs - vec_len (c->data);
323 vec_add2 (c->data, tmp, pad_len);
324 c->data[vec_len (c->data) - 1] = pad_len - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325}
326
327ikev2_sa_proposal_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700328ikev2_parse_sa_payload (ike_payload_header_t * ikep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700330 ikev2_sa_proposal_t *v = 0;
331 ikev2_sa_proposal_t *proposal;
332 ikev2_sa_transform_t *transform;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700334 u32 plen = clib_net_to_host_u16 (ikep->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700336 ike_sa_proposal_data_t *sap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337 int proposal_ptr = 0;
338
339 do
340 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700341 sap = (ike_sa_proposal_data_t *) & ikep->payload[proposal_ptr];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 int i;
343 int transform_ptr;
344
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700345 DBG_PLD ("proposal num %u len %u last_or_more %u id %u "
346 "spi_size %u num_transforms %u",
347 sap->proposal_num, clib_net_to_host_u16 (sap->proposal_len),
348 sap->last_or_more, sap->protocol_id, sap->spi_size,
349 sap->num_transforms);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350
351 /* IKE proposal should not have SPI */
352 if (sap->protocol_id == IKEV2_PROTOCOL_IKE && sap->spi_size != 0)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700353 goto data_corrupted;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
355 /* IKE proposal should not have SPI */
356 if (sap->protocol_id == IKEV2_PROTOCOL_ESP && sap->spi_size != 4)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700357 goto data_corrupted;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700359 transform_ptr = proposal_ptr + sizeof (*sap) + sap->spi_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700361 vec_add2 (v, proposal, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362 proposal->proposal_num = sap->proposal_num;
363 proposal->protocol_id = sap->protocol_id;
364
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700365 if (sap->spi_size == 4)
366 {
367 proposal->spi = clib_net_to_host_u32 (sap->spi[0]);
368 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700370 for (i = 0; i < sap->num_transforms; i++)
371 {
372 ike_sa_transform_data_t *tr =
373 (ike_sa_transform_data_t *) & ikep->payload[transform_ptr];
374 u16 tlen = clib_net_to_host_u16 (tr->transform_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700376 if (tlen < sizeof (*tr))
377 goto data_corrupted;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700379 vec_add2 (proposal->transforms, transform, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700381 transform->type = tr->transform_type;
382 transform->transform_id = clib_net_to_host_u16 (tr->transform_id);
383 if (tlen > sizeof (*tr))
384 vec_add (transform->attrs, tr->attributes, tlen - sizeof (*tr));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700386 DBG_PLD
387 ("transform num %u len %u last_or_more %u type %U id %u%s%U", i,
388 tlen, tr->last_or_more, format_ikev2_sa_transform, transform,
389 clib_net_to_host_u16 (tr->transform_id),
390 tlen > sizeof (*tr) ? " attrs " : "", format_hex_bytes,
391 tr->attributes, tlen - sizeof (*tr));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700393 transform_ptr += tlen;
394 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700396 proposal_ptr += clib_net_to_host_u16 (sap->proposal_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700398 while (proposal_ptr < (plen - sizeof (*ikep)) && sap->last_or_more == 2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399
400 /* data validation */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700401 if (proposal_ptr != (plen - sizeof (*ikep)) || sap->last_or_more)
402 goto data_corrupted;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403
404 return v;
405
406data_corrupted:
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700407 DBG_PLD ("SA payload data corrupted");
408 ikev2_sa_free_proposal_vector (&v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409 return 0;
410}
411
412ikev2_ts_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700413ikev2_parse_ts_payload (ike_payload_header_t * ikep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700415 ike_ts_payload_header_t *tsp = (ike_ts_payload_header_t *) ikep;
416 ikev2_ts_t *r = 0, *ts;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 u8 i;
418
419 for (i = 0; i < tsp->num_ts; i++)
420 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700421 if (tsp->ts[i].ts_type != 7) /* TS_IPV4_ADDR_RANGE */
422 {
423 DBG_PLD ("unsupported TS type received (%u)", tsp->ts[i].ts_type);
424 continue;
425 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700427 vec_add2 (r, ts, 1);
428 ts->ts_type = tsp->ts[i].ts_type;
429 ts->protocol_id = tsp->ts[i].protocol_id;
430 ts->start_port = tsp->ts[i].start_port;
431 ts->end_port = tsp->ts[i].end_port;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432 ts->start_addr.as_u32 = tsp->ts[i].start_addr.as_u32;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700433 ts->end_addr.as_u32 = tsp->ts[i].end_addr.as_u32;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434 }
435 return r;
436}
437
438ikev2_notify_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700439ikev2_parse_notify_payload (ike_payload_header_t * ikep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700441 ike_notify_payload_header_t *n = (ike_notify_payload_header_t *) ikep;
442 u32 plen = clib_net_to_host_u16 (ikep->length);
443 ikev2_notify_t *r = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444 u32 spi;
445
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700446 DBG_PLD ("msg_type %U len %u%s%U",
447 format_ikev2_notify_msg_type, clib_net_to_host_u16 (n->msg_type),
448 plen, plen > sizeof (*n) ? " data " : "",
449 format_hex_bytes, n->payload, plen - sizeof (*n));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700451 r = vec_new (ikev2_notify_t, 1);
452 r->msg_type = clib_net_to_host_u16 (n->msg_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 r->protocol_id = n->protocol_id;
454
455 if (n->spi_size == 4)
456 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700457 clib_memcpy (&spi, n->payload, n->spi_size);
458 r->spi = clib_net_to_host_u32 (spi);
459 DBG_PLD ("spi %lx", r->spi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460 }
461 else if (n->spi_size == 0)
462 {
463 r->spi = 0;
464 }
465 else
466 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700467 clib_warning ("invalid SPI Size %d", n->spi_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468 }
469
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700470 if (plen > (sizeof (*n) + n->spi_size))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700472 vec_add (r->data, n->payload + n->spi_size,
473 plen - sizeof (*n) - n->spi_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474 }
475
476 return r;
477}
478
479void
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700480ikev2_parse_vendor_payload (ike_payload_header_t * ikep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700482 u32 plen = clib_net_to_host_u16 (ikep->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483 int i;
484 int is_string = 1;
485
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700486 for (i = 0; i < plen - 4; i++)
487 if (!isprint (ikep->payload[i]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 is_string = 0;
489
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700490 DBG_PLD ("len %u data %s:%U",
491 plen,
492 is_string ? "string" : "hex",
493 is_string ? format_ascii_bytes : format_hex_bytes,
494 ikep->payload, plen - sizeof (*ikep));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495}
496
497ikev2_delete_t *
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700498ikev2_parse_delete_payload (ike_payload_header_t * ikep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700500 ike_delete_payload_header_t *d = (ike_delete_payload_header_t *) ikep;
501 u32 plen = clib_net_to_host_u16 (ikep->length);
502 ikev2_delete_t *r = 0, *del;
503 u16 num_of_spi = clib_net_to_host_u16 (d->num_of_spi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504 u16 i = 0;
505
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700506 DBG_PLD ("protocol_id %u spi_size %u num_of_spi %u len %u%s%U",
507 d->protocol_id, d->spi_size, num_of_spi,
508 plen, plen > sizeof (d) ? " data " : "",
509 format_hex_bytes, d->spi, plen - sizeof (*d));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510
511 if (d->protocol_id == IKEV2_PROTOCOL_IKE)
512 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700513 r = vec_new (ikev2_delete_t, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 r->protocol_id = 1;
515 }
516 else
517 {
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700518 r = vec_new (ikev2_delete_t, num_of_spi);
519 vec_foreach (del, r)
520 {
521 del->protocol_id = d->protocol_id;
522 del->spi = clib_net_to_host_u32 (d->spi[i++]);
523 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524 }
525
526 return r;
527}
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700528
529/*
530 * fd.io coding-style-patch-verification: ON
531 *
532 * Local Variables:
533 * eval: (c-set-style "gnu")
534 * End:
535 */