Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
Damjan Marion | e936bbe | 2016-02-25 23:17:38 +0100 | [diff] [blame] | 16 | #include <ctype.h> |
| 17 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 18 | #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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 27 | /* *INDENT-OFF* */ |
| 28 | typedef 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 38 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 39 | /* *INDENT-OFF* */ |
| 40 | typedef 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 51 | /* *INDENT-OFF* */ |
| 52 | typedef 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 63 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | typedef CLIB_PACKED (struct { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 65 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | }) ike_sa_proposal_data_t; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 73 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 75 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | typedef CLIB_PACKED (struct { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 77 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | }) ike_sa_transform_data_t; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 85 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 87 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | typedef CLIB_PACKED (struct { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 89 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | }) ike_delete_payload_header_t; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 97 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | |
| 99 | static ike_payload_header_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 100 | ikev2_payload_add_hdr (ikev2_payload_chain_t * c, u8 payload_type, int len) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 102 | ike_payload_header_t *hdr = |
| 103 | (ike_payload_header_t *) & c->data[c->last_hdr_off]; |
| 104 | u8 *tmp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | |
| 106 | if (c->data) |
| 107 | hdr->nextpayload = payload_type; |
| 108 | else |
| 109 | c->first_payload_type = payload_type; |
| 110 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 111 | c->last_hdr_off = vec_len (c->data); |
| 112 | vec_add2 (c->data, tmp, len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | hdr = (ike_payload_header_t *) tmp; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 114 | memset (hdr, 0, len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 116 | hdr->length = clib_host_to_net_u16 (len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | |
| 118 | return hdr; |
| 119 | } |
| 120 | |
| 121 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 122 | ikev2_payload_add_data (ikev2_payload_chain_t * c, u8 * data) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | { |
| 124 | u16 len; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 125 | ike_payload_header_t *hdr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 127 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 134 | ikev2_payload_add_notify (ikev2_payload_chain_t * c, u16 msg_type, u8 * data) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 136 | ike_notify_payload_header_t *n; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 138 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 147 | ikev2_payload_add_sa (ikev2_payload_chain_t * c, |
| 148 | ikev2_sa_proposal_t * proposals) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 150 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 156 | u8 *tmp; |
| 157 | u8 *pr_data = 0; |
| 158 | u8 *tr_data = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 160 | ikev2_payload_add_hdr (c, IKEV2_PAYLOAD_SA, sizeof (*ph)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 162 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 183 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 192 | if (vec_len (t->attrs) > 0) |
| 193 | clib_memcpy (tr->attributes, t->attrs, vec_len (t->attrs)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 195 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 202 | |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 213 | ikev2_payload_add_ke (ikev2_payload_chain_t * c, u16 dh_group, u8 * dh_data) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 215 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 219 | ke->dh_group = clib_host_to_net_u16 (dh_group); |
| 220 | ikev2_payload_add_data (c, dh_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 224 | ikev2_payload_add_nonce (ikev2_payload_chain_t * c, u8 * nonce) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 226 | ikev2_payload_add_hdr (c, IKEV2_PAYLOAD_NONCE, |
| 227 | sizeof (ike_payload_header_t)); |
| 228 | ikev2_payload_add_data (c, nonce); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 232 | ikev2_payload_add_id (ikev2_payload_chain_t * c, ikev2_id_t * id, u8 type) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 234 | ike_id_payload_header_t *idp; |
| 235 | idp = |
| 236 | (ike_id_payload_header_t *) ikev2_payload_add_hdr (c, type, |
| 237 | sizeof (*idp)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | |
| 239 | idp->id_type = id->type; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 240 | ikev2_payload_add_data (c, id->data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 244 | ikev2_payload_add_delete (ikev2_payload_chain_t * c, ikev2_delete_t * d) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 246 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | |
| 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 262 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 275 | ikev2_payload_add_auth (ikev2_payload_chain_t * c, ikev2_auth_t * auth) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 276 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 277 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | |
| 283 | ap->auth_method = auth->method; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 284 | ikev2_payload_add_data (c, auth->data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 288 | ikev2_payload_add_ts (ikev2_payload_chain_t * c, ikev2_ts_t * ts, u8 type) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 290 | ike_ts_payload_header_t *tsh; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | ikev2_ts_t *ts2; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 292 | u8 *data = 0, *tmp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 293 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 294 | tsh = |
| 295 | (ike_ts_payload_header_t *) ikev2_payload_add_hdr (c, type, |
| 296 | sizeof (*tsh)); |
| 297 | tsh->num_ts = vec_len (ts); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 298 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 299 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 313 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 314 | ikev2_payload_add_data (c, data); |
| 315 | vec_free (data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 319 | ikev2_payload_chain_add_padding (ikev2_payload_chain_t * c, int bs) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 320 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 321 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | ikev2_sa_proposal_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 328 | ikev2_parse_sa_payload (ike_payload_header_t * ikep) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 330 | ikev2_sa_proposal_t *v = 0; |
| 331 | ikev2_sa_proposal_t *proposal; |
| 332 | ikev2_sa_transform_t *transform; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 333 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 334 | u32 plen = clib_net_to_host_u16 (ikep->length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 336 | ike_sa_proposal_data_t *sap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 337 | int proposal_ptr = 0; |
| 338 | |
| 339 | do |
| 340 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 341 | sap = (ike_sa_proposal_data_t *) & ikep->payload[proposal_ptr]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 342 | int i; |
| 343 | int transform_ptr; |
| 344 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 345 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | |
| 351 | /* IKE proposal should not have SPI */ |
| 352 | if (sap->protocol_id == IKEV2_PROTOCOL_IKE && sap->spi_size != 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 353 | goto data_corrupted; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | |
| 355 | /* IKE proposal should not have SPI */ |
| 356 | if (sap->protocol_id == IKEV2_PROTOCOL_ESP && sap->spi_size != 4) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 357 | goto data_corrupted; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 359 | transform_ptr = proposal_ptr + sizeof (*sap) + sap->spi_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 361 | vec_add2 (v, proposal, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | proposal->proposal_num = sap->proposal_num; |
| 363 | proposal->protocol_id = sap->protocol_id; |
| 364 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 365 | if (sap->spi_size == 4) |
| 366 | { |
| 367 | proposal->spi = clib_net_to_host_u32 (sap->spi[0]); |
| 368 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 369 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 370 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 376 | if (tlen < sizeof (*tr)) |
| 377 | goto data_corrupted; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 379 | vec_add2 (proposal->transforms, transform, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 381 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 386 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 392 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 393 | transform_ptr += tlen; |
| 394 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 396 | proposal_ptr += clib_net_to_host_u16 (sap->proposal_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 398 | while (proposal_ptr < (plen - sizeof (*ikep)) && sap->last_or_more == 2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | |
| 400 | /* data validation */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 401 | if (proposal_ptr != (plen - sizeof (*ikep)) || sap->last_or_more) |
| 402 | goto data_corrupted; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | |
| 404 | return v; |
| 405 | |
| 406 | data_corrupted: |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 407 | DBG_PLD ("SA payload data corrupted"); |
| 408 | ikev2_sa_free_proposal_vector (&v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | ikev2_ts_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 413 | ikev2_parse_ts_payload (ike_payload_header_t * ikep) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 415 | ike_ts_payload_header_t *tsp = (ike_ts_payload_header_t *) ikep; |
| 416 | ikev2_ts_t *r = 0, *ts; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 417 | u8 i; |
| 418 | |
| 419 | for (i = 0; i < tsp->num_ts; i++) |
| 420 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 421 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 426 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 427 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | ts->start_addr.as_u32 = tsp->ts[i].start_addr.as_u32; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 433 | ts->end_addr.as_u32 = tsp->ts[i].end_addr.as_u32; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 434 | } |
| 435 | return r; |
| 436 | } |
| 437 | |
| 438 | ikev2_notify_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 439 | ikev2_parse_notify_payload (ike_payload_header_t * ikep) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 440 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 441 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 444 | u32 spi; |
| 445 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 446 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 450 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 451 | r = vec_new (ikev2_notify_t, 1); |
| 452 | r->msg_type = clib_net_to_host_u16 (n->msg_type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 453 | r->protocol_id = n->protocol_id; |
| 454 | |
| 455 | if (n->spi_size == 4) |
| 456 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 457 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 460 | } |
| 461 | else if (n->spi_size == 0) |
| 462 | { |
| 463 | r->spi = 0; |
| 464 | } |
| 465 | else |
| 466 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 467 | clib_warning ("invalid SPI Size %d", n->spi_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 470 | if (plen > (sizeof (*n) + n->spi_size)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 472 | vec_add (r->data, n->payload + n->spi_size, |
| 473 | plen - sizeof (*n) - n->spi_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | return r; |
| 477 | } |
| 478 | |
| 479 | void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 480 | ikev2_parse_vendor_payload (ike_payload_header_t * ikep) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 481 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 482 | u32 plen = clib_net_to_host_u16 (ikep->length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | int i; |
| 484 | int is_string = 1; |
| 485 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 486 | for (i = 0; i < plen - 4; i++) |
| 487 | if (!isprint (ikep->payload[i])) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | is_string = 0; |
| 489 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 490 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | ikev2_delete_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 498 | ikev2_parse_delete_payload (ike_payload_header_t * ikep) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 499 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 500 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 504 | u16 i = 0; |
| 505 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 506 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 510 | |
| 511 | if (d->protocol_id == IKEV2_PROTOCOL_IKE) |
| 512 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 513 | r = vec_new (ikev2_delete_t, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | r->protocol_id = 1; |
| 515 | } |
| 516 | else |
| 517 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 518 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | return r; |
| 527 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 528 | |
| 529 | /* |
| 530 | * fd.io coding-style-patch-verification: ON |
| 531 | * |
| 532 | * Local Variables: |
| 533 | * eval: (c-set-style "gnu") |
| 534 | * End: |
| 535 | */ |