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 | |
| 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 25 | static int ikev2_delete_tunnel_interface (vnet_main_t * vnm, |
| 26 | ikev2_sa_t * sa, |
| 27 | ikev2_child_sa_t * child); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | #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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 34 | typedef struct |
| 35 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | u32 next_index; |
| 37 | u32 sw_if_index; |
| 38 | } ikev2_trace_t; |
| 39 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 40 | static u8 * |
| 41 | format_ikev2_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | { |
| 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 45 | ikev2_trace_t *t = va_arg (*args, ikev2_trace_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | |
| 47 | s = format (s, "ikev2: sw_if_index %d, next index %d", |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 48 | t->sw_if_index, t->next_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | return s; |
| 50 | } |
| 51 | |
Jean-Mickael Guerin | 8941ec2 | 2016-03-04 14:14:21 +0100 | [diff] [blame] | 52 | static vlib_node_registration_t ikev2_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | |
| 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 62 | typedef enum |
| 63 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | #define _(sym,str) IKEV2_ERROR_##sym, |
| 65 | foreach_ikev2_error |
| 66 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 67 | IKEV2_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | } ikev2_error_t; |
| 69 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 70 | static char *ikev2_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | #define _(sym,string) string, |
| 72 | foreach_ikev2_error |
| 73 | #undef _ |
| 74 | }; |
| 75 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 76 | typedef enum |
| 77 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | IKEV2_NEXT_IP4_LOOKUP, |
| 79 | IKEV2_NEXT_ERROR_DROP, |
| 80 | IKEV2_N_NEXT, |
| 81 | } ikev2_next_t; |
| 82 | |
| 83 | static ikev2_sa_transform_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 84 | ikev2_find_transform_data (ikev2_sa_transform_t * t) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 86 | ikev2_main_t *km = &ikev2_main; |
| 87 | ikev2_sa_transform_t *td; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 89 | vec_foreach (td, km->supported_transforms) |
| 90 | { |
| 91 | if (td->type != t->type) |
| 92 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 94 | if (td->transform_id != t->transform_id) |
| 95 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 97 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 103 | if (((t->attrs[2] << 8 | t->attrs[3]) / 8) != td->key_len) |
| 104 | continue; |
| 105 | } |
| 106 | return td; |
| 107 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static ikev2_sa_proposal_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 112 | ikev2_select_proposal (ikev2_sa_proposal_t * proposals, |
| 113 | ikev2_protocol_id_t prot_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 115 | ikev2_sa_proposal_t *rv = 0; |
| 116 | ikev2_sa_proposal_t *proposal; |
| 117 | ikev2_sa_transform_t *transform, *new_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | u8 mandatory_bitmap, optional_bitmap; |
| 119 | |
| 120 | if (prot_id == IKEV2_PROTOCOL_IKE) |
| 121 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 122 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | } |
| 127 | else if (prot_id == IKEV2_PROTOCOL_ESP) |
| 128 | { |
| 129 | mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_ENCR) | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 130 | (1 << IKEV2_TRANSFORM_TYPE_ESN); |
| 131 | optional_bitmap = mandatory_bitmap | |
| 132 | (1 << IKEV2_TRANSFORM_TYPE_INTEG) | (1 << IKEV2_TRANSFORM_TYPE_DH); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | } |
| 134 | else if (prot_id == IKEV2_PROTOCOL_AH) |
| 135 | { |
| 136 | mandatory_bitmap = (1 << IKEV2_TRANSFORM_TYPE_INTEG) | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 137 | (1 << IKEV2_TRANSFORM_TYPE_ESN); |
| 138 | optional_bitmap = mandatory_bitmap | (1 << IKEV2_TRANSFORM_TYPE_DH); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | } |
| 140 | else |
| 141 | return 0; |
| 142 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 143 | vec_add2 (rv, proposal, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 145 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 153 | if ((1 << transform->type) & bitmap) |
| 154 | continue; |
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 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 165 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | done: |
| 184 | return rv; |
| 185 | } |
| 186 | |
| 187 | ikev2_sa_transform_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 188 | ikev2_sa_get_td_for_type (ikev2_sa_proposal_t * p, |
| 189 | ikev2_transform_type_t type) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 191 | ikev2_sa_transform_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | |
| 193 | if (!p) |
| 194 | return 0; |
| 195 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 196 | vec_foreach (t, p->transforms) |
| 197 | { |
| 198 | if (t->type == type) |
| 199 | return ikev2_find_transform_data (t); |
| 200 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | ikev2_child_sa_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 205 | ikev2_sa_get_child (ikev2_sa_t * sa, u32 spi, ikev2_protocol_id_t prot_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 207 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 219 | ikev2_sa_free_proposal_vector (ikev2_sa_proposal_t ** v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 221 | ikev2_sa_proposal_t *p; |
| 222 | ikev2_sa_transform_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | |
| 224 | if (!*v) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 225 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 227 | vec_foreach (p, *v) |
| 228 | { |
| 229 | vec_foreach (t, p->transforms) |
| 230 | { |
| 231 | vec_free (t->attrs); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 233 | vec_free (p->transforms); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 235 | vec_free (*v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 239 | ikev2_sa_free_all_child_sa (ikev2_child_sa_t ** childs) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 241 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 252 | vec_free (*childs); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 256 | ikev2_sa_del_child_sa (ikev2_sa_t * sa, ikev2_child_sa_t * child) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 258 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 265 | vec_del1 (sa->childs, child - sa->childs); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 269 | ikev2_sa_free_all_vec (ikev2_sa_t * sa) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 270 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 271 | vec_free (sa->i_nonce); |
| 272 | vec_free (sa->i_dh_data); |
| 273 | vec_free (sa->dh_shared_key); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 275 | ikev2_sa_free_proposal_vector (&sa->r_proposals); |
| 276 | ikev2_sa_free_proposal_vector (&sa->i_proposals); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 278 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 286 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 290 | if (sa->r_auth.key) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 291 | EVP_PKEY_free (sa->r_auth.key); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 293 | vec_free (sa->del); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 295 | ikev2_sa_free_all_child_sa (&sa->childs); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 299 | ikev2_delete_sa (ikev2_sa_t * sa) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 300 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 301 | ikev2_main_t *km = &ikev2_main; |
| 302 | u32 cpu_index = os_get_cpu_number (); |
| 303 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 304 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 305 | ikev2_sa_free_all_vec (sa); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 306 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 307 | p = hash_get (km->per_thread_data[cpu_index].sa_by_rspi, sa->rspi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | if (p) |
| 309 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 310 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 316 | ikev2_generate_sa_init_data (ikev2_sa_t * sa) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 318 | ikev2_sa_transform_t *t = 0, *t2; |
| 319 | ikev2_main_t *km = &ikev2_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 320 | |
| 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 327 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | |
| 336 | if (!t) |
| 337 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 338 | clib_warning ("unknown dh data group %u (data len %u)", sa->dh_group, |
| 339 | vec_len (sa->i_dh_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 340 | sa->dh_group = IKEV2_TRANSFORM_DH_TYPE_NONE; |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | /* generate rspi */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 345 | RAND_bytes ((u8 *) & sa->rspi, 8); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | |
| 347 | /* generate nonce */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 348 | sa->r_nonce = vec_new (u8, IKEV2_NONCE_SIZE); |
| 349 | RAND_bytes ((u8 *) sa->r_nonce, IKEV2_NONCE_SIZE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | |
| 351 | /* generate dh keys */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 352 | ikev2_generate_dh (sa, t); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 356 | ikev2_calc_keys (ikev2_sa_t * sa) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 357 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 358 | u8 *tmp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | /* calculate SKEYSEED = prf(Ni | Nr, g^ir) */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 360 | 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 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 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 373 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 374 | /* Calculate S = Ni | Nr | SPIi | SPIr */ |
| 375 | u64 *spi; |
| 376 | vec_add2 (s, tmp, 2 * sizeof (*spi)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 377 | spi = (u64 *) tmp; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 378 | spi[0] = clib_host_to_net_u64 (sa->ispi); |
| 379 | spi[1] = clib_host_to_net_u64 (sa->rspi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | |
| 381 | /* calculate PRFplus */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 382 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 388 | keymat = ikev2_calc_prfplus (tr_prf, skeyseed, s, len); |
| 389 | vec_free (skeyseed); |
| 390 | vec_free (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | |
| 392 | int pos = 0; |
| 393 | |
| 394 | /* SK_d */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 395 | sa->sk_d = vec_new (u8, tr_prf->key_trunc); |
| 396 | clib_memcpy (sa->sk_d, keymat + pos, tr_prf->key_trunc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | pos += tr_prf->key_trunc; |
| 398 | |
| 399 | /* SK_ai */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 400 | sa->sk_ai = vec_new (u8, tr_integ->key_len); |
| 401 | clib_memcpy (sa->sk_ai, keymat + pos, tr_integ->key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 402 | pos += tr_integ->key_len; |
| 403 | |
| 404 | /* SK_ar */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 405 | sa->sk_ar = vec_new (u8, tr_integ->key_len); |
| 406 | clib_memcpy (sa->sk_ar, keymat + pos, tr_integ->key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 407 | pos += tr_integ->key_len; |
| 408 | |
| 409 | /* SK_ei */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 410 | sa->sk_ei = vec_new (u8, tr_encr->key_len); |
| 411 | clib_memcpy (sa->sk_ei, keymat + pos, tr_encr->key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 412 | pos += tr_encr->key_len; |
| 413 | |
| 414 | /* SK_er */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 415 | sa->sk_er = vec_new (u8, tr_encr->key_len); |
| 416 | clib_memcpy (sa->sk_er, keymat + pos, tr_encr->key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 417 | pos += tr_encr->key_len; |
| 418 | |
| 419 | /* SK_pi */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 420 | sa->sk_pi = vec_new (u8, tr_prf->key_len); |
| 421 | clib_memcpy (sa->sk_pi, keymat + pos, tr_prf->key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 422 | pos += tr_prf->key_len; |
| 423 | |
| 424 | /* SK_pr */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 425 | sa->sk_pr = vec_new (u8, tr_prf->key_len); |
| 426 | clib_memcpy (sa->sk_pr, keymat + pos, tr_prf->key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 427 | pos += tr_prf->key_len; |
| 428 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 429 | vec_free (keymat); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 433 | ikev2_calc_child_keys (ikev2_sa_t * sa, ikev2_child_sa_t * child) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 434 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 435 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 444 | vec_append (s, sa->i_nonce); |
| 445 | vec_append (s, sa->r_nonce); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | /* calculate PRFplus */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 447 | u8 *keymat; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | int len = ctr_encr->key_len * 2 + ctr_integ->key_len * 2; |
| 449 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 450 | keymat = ikev2_calc_prfplus (tr_prf, sa->sk_d, s, len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 451 | |
| 452 | int pos = 0; |
| 453 | |
| 454 | /* SK_ei */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 455 | child->sk_ei = vec_new (u8, ctr_encr->key_len); |
| 456 | clib_memcpy (child->sk_ei, keymat + pos, ctr_encr->key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | pos += ctr_encr->key_len; |
| 458 | |
| 459 | /* SK_ai */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 460 | child->sk_ai = vec_new (u8, ctr_integ->key_len); |
| 461 | clib_memcpy (child->sk_ai, keymat + pos, ctr_integ->key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 462 | pos += ctr_integ->key_len; |
| 463 | |
| 464 | /* SK_er */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 465 | child->sk_er = vec_new (u8, ctr_encr->key_len); |
| 466 | clib_memcpy (child->sk_er, keymat + pos, ctr_encr->key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 467 | pos += ctr_encr->key_len; |
| 468 | |
| 469 | /* SK_ar */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 470 | child->sk_ar = vec_new (u8, ctr_integ->key_len); |
| 471 | clib_memcpy (child->sk_ar, keymat + pos, ctr_integ->key_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | pos += ctr_integ->key_len; |
| 473 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 474 | ASSERT (pos == len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 475 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 476 | vec_free (keymat); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 480 | ikev2_process_sa_init_req (vlib_main_t * vm, ikev2_sa_t * sa, |
| 481 | ike_header_t * ike) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 482 | { |
| 483 | int p = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 484 | u32 len = clib_net_to_host_u32 (ike->length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 485 | u8 payload = ike->nextpayload; |
| 486 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 487 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 494 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 495 | sa->ispi = clib_net_to_host_u64 (ike->ispi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 496 | |
| 497 | /* store whole IKE payload - needed for PSK auth */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 498 | vec_free (sa->last_sa_init_req_packet_data); |
| 499 | vec_add (sa->last_sa_init_req_packet_data, ike, len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 500 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 501 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 506 | if (plen < sizeof (ike_payload_header_t)) |
| 507 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 508 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 509 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 547 | payload = ikep->nextpayload; |
| 548 | p += plen; |
| 549 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 550 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 551 | ikev2_set_state (sa, IKEV2_STATE_SA_INIT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | static u8 * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 555 | ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike, u8 * payload) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 556 | { |
| 557 | int p = 0; |
| 558 | u8 last_payload = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 559 | u8 *hmac = 0; |
| 560 | u32 len = clib_net_to_host_u32 (ike->length); |
| 561 | ike_payload_header_t *ikep = 0; |
Benoît Ganne | d530445 | 2016-04-08 22:25:05 -0700 | [diff] [blame] | 562 | u32 plen = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 563 | ikev2_sa_transform_t *tr_integ; |
| 564 | tr_integ = |
| 565 | ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 566 | |
| 567 | while (p < len && |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 568 | *payload != IKEV2_PAYLOAD_NONE && last_payload != IKEV2_PAYLOAD_SK) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 569 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 570 | ikep = (ike_payload_header_t *) & ike->payload[p]; |
| 571 | plen = clib_net_to_host_u16 (ikep->length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 572 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 573 | if (plen < sizeof (*ikep)) |
| 574 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 575 | |
| 576 | if (*payload == IKEV2_PAYLOAD_SK) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 577 | { |
| 578 | clib_warning ("received IKEv2 payload SK, len %u", plen - 4); |
| 579 | last_payload = *payload; |
| 580 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 581 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 582 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 591 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 592 | *payload = ikep->nextpayload; |
| 593 | p += plen; |
| 594 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 595 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 596 | if (last_payload != IKEV2_PAYLOAD_SK) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 597 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 598 | clib_warning ("Last payload must be SK"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 599 | return 0; |
| 600 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 601 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 602 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static void |
| 619 | ikev2_initial_contact_cleanup (ikev2_sa_t * sa) |
| 620 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 621 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 626 | |
| 627 | if (!sa->initial_contact) |
| 628 | return; |
| 629 | |
| 630 | /* find old IKE SAs with the same authenticated identity */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 631 | /* *INDENT-OFF* */ |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 632 | pool_foreach (tmp, km->per_thread_data[cpu_index].sas, ({ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 633 | 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 Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 639 | vec_add1(delete, tmp - km->per_thread_data[cpu_index].sas); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 640 | })); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 641 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 642 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 643 | for (i = 0; i < vec_len (delete); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 644 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 645 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 651 | vec_free (delete); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 652 | sa->initial_contact = 0; |
| 653 | } |
| 654 | |
| 655 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 656 | ikev2_process_auth_req (vlib_main_t * vm, ikev2_sa_t * sa, ike_header_t * ike) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 657 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 658 | ikev2_child_sa_t *first_child_sa; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 659 | int p = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 660 | u32 len = clib_net_to_host_u32 (ike->length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 661 | u8 payload = ike->nextpayload; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 662 | u8 *plaintext = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 663 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 664 | ike_payload_header_t *ikep; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 665 | u32 plen; |
| 666 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 667 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 674 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 675 | ikev2_calc_keys (sa); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 676 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 677 | plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 678 | |
| 679 | if (!plaintext) |
| 680 | { |
| 681 | if (sa->unsupported_cp) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 682 | ikev2_set_state (sa, IKEV2_STATE_NOTIFY_AND_DELETE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 683 | goto cleanup_and_exit; |
| 684 | } |
| 685 | |
| 686 | /* create 1st child SA */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 687 | ikev2_sa_free_all_child_sa (&sa->childs); |
| 688 | vec_add2 (sa->childs, first_child_sa, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 689 | |
| 690 | |
| 691 | /* process encrypted payload */ |
| 692 | p = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 693 | while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 694 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 695 | ikep = (ike_payload_header_t *) & plaintext[p]; |
| 696 | plen = clib_net_to_host_u16 (ikep->length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 697 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 698 | if (plen < sizeof (ike_payload_header_t)) |
| 699 | goto cleanup_and_exit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 700 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 701 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 710 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 711 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 714 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 715 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 721 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 722 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 725 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 726 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 747 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 754 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 755 | vec_free (first_child_sa->tsr); |
| 756 | first_child_sa->tsr = ikev2_parse_ts_payload (ikep); |
| 757 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 758 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 759 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 763 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 764 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 771 | |
| 772 | payload = ikep->nextpayload; |
| 773 | p += plen; |
| 774 | } |
| 775 | |
| 776 | cleanup_and_exit: |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 777 | vec_free (plaintext); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 781 | ikev2_process_informational_req (vlib_main_t * vm, ikev2_sa_t * sa, |
| 782 | ike_header_t * ike) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 783 | { |
| 784 | int p = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 785 | u32 len = clib_net_to_host_u32 (ike->length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 786 | u8 payload = ike->nextpayload; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 787 | u8 *plaintext = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 788 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 789 | ike_payload_header_t *ikep; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 790 | u32 plen; |
| 791 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 792 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 799 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 800 | plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 801 | |
| 802 | if (!plaintext) |
| 803 | goto cleanup_and_exit; |
| 804 | |
| 805 | /* process encrypted payload */ |
| 806 | p = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 807 | while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 808 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 809 | ikep = (ike_payload_header_t *) & plaintext[p]; |
| 810 | plen = clib_net_to_host_u16 (ikep->length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 811 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 812 | if (plen < sizeof (ike_payload_header_t)) |
| 813 | goto cleanup_and_exit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 814 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 815 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 830 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 831 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 835 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 836 | if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL) |
| 837 | { |
| 838 | sa->unsupported_cp = payload; |
| 839 | return; |
| 840 | } |
| 841 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 842 | |
| 843 | payload = ikep->nextpayload; |
| 844 | p += plen; |
| 845 | } |
| 846 | |
| 847 | cleanup_and_exit: |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 848 | vec_free (plaintext); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 852 | ikev2_process_create_child_sa_req (vlib_main_t * vm, ikev2_sa_t * sa, |
| 853 | ike_header_t * ike) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 854 | { |
| 855 | int p = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 856 | u32 len = clib_net_to_host_u32 (ike->length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 857 | u8 payload = ike->nextpayload; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 858 | u8 *plaintext = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 859 | u8 rekeying = 0; |
| 860 | u8 i_nonce[IKEV2_NONCE_SIZE]; |
| 861 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 862 | ike_payload_header_t *ikep; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 863 | u32 plen; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 864 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 869 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 870 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 877 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 878 | plaintext = ikev2_decrypt_sk_payload (sa, ike, &payload); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 879 | |
| 880 | if (!plaintext) |
| 881 | goto cleanup_and_exit; |
| 882 | |
| 883 | /* process encrypted payload */ |
| 884 | p = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 885 | while (p < vec_len (plaintext) && payload != IKEV2_PAYLOAD_NONE) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 886 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 887 | ikep = (ike_payload_header_t *) & plaintext[p]; |
| 888 | plen = clib_net_to_host_u16 (ikep->length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 889 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 890 | if (plen < sizeof (ike_payload_header_t)) |
| 891 | goto cleanup_and_exit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 892 | |
| 893 | else if (payload == IKEV2_PAYLOAD_SA) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 894 | { |
| 895 | proposal = ikev2_parse_sa_payload (ikep); |
| 896 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 897 | else if (payload == IKEV2_PAYLOAD_NOTIFY) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 898 | { |
| 899 | n = ikev2_parse_notify_payload (ikep); |
| 900 | if (n->msg_type == IKEV2_NOTIFY_MSG_REKEY_SA) |
| 901 | { |
| 902 | rekeying = 1; |
| 903 | } |
| 904 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 905 | else if (payload == IKEV2_PAYLOAD_DELETE) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 906 | { |
| 907 | sa->del = ikev2_parse_delete_payload (ikep); |
| 908 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 909 | else if (payload == IKEV2_PAYLOAD_VENDOR) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 910 | { |
| 911 | ikev2_parse_vendor_payload (ikep); |
| 912 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 913 | else if (payload == IKEV2_PAYLOAD_NONCE) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 914 | { |
| 915 | clib_memcpy (i_nonce, ikep->payload, plen - sizeof (*ikep)); |
| 916 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 917 | else if (payload == IKEV2_PAYLOAD_TSI) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 918 | { |
| 919 | tsi = ikev2_parse_ts_payload (ikep); |
| 920 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 921 | else if (payload == IKEV2_PAYLOAD_TSR) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 922 | { |
| 923 | tsr = ikev2_parse_ts_payload (ikep); |
| 924 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 925 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 926 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 930 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 931 | if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL) |
| 932 | { |
| 933 | sa->unsupported_cp = payload; |
| 934 | return; |
| 935 | } |
| 936 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 937 | |
| 938 | payload = ikep->nextpayload; |
| 939 | p += plen; |
| 940 | } |
| 941 | |
| 942 | if (rekeying) |
| 943 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 944 | ikev2_rekey_t *rekey; |
| 945 | child_sa = ikev2_sa_get_child (sa, n->spi, n->protocol_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 946 | if (!child_sa) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 947 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 952 | rekey->protocol_id = n->protocol_id; |
| 953 | rekey->spi = n->spi; |
| 954 | rekey->i_proposal = proposal; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 955 | rekey->r_proposal = |
| 956 | ikev2_select_proposal (proposal, IKEV2_PROTOCOL_ESP); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 957 | rekey->tsi = tsi; |
| 958 | rekey->tsr = tsr; |
| 959 | /* update Ni */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 960 | vec_free (sa->i_nonce); |
| 961 | vec_add (sa->i_nonce, i_nonce, IKEV2_NONCE_SIZE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 962 | /* generate new Nr */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 963 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | cleanup_and_exit: |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 969 | vec_free (plaintext); |
| 970 | vec_free (n); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | static u8 * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 974 | ikev2_sa_generate_authmsg (ikev2_sa_t * sa, int is_responder) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 975 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 976 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 983 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 984 | tr_prf = |
| 985 | ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 986 | |
| 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1002 | data = vec_new (u8, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1003 | data[0] = id->type; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1004 | vec_append (data, id->data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1005 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1006 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1012 | |
| 1013 | return authmsg; |
| 1014 | } |
| 1015 | |
| 1016 | static int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1017 | ikev2_ts_cmp (ikev2_ts_t * ts1, ikev2_ts_t * ts2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1018 | { |
| 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 | |
| 1028 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1029 | ikev2_sa_match_ts (ikev2_sa_t * sa) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1030 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1031 | ikev2_main_t *km = &ikev2_main; |
| 1032 | ikev2_profile_t *p; |
| 1033 | ikev2_ts_t *ts, *tsi = 0, *tsr = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1034 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1035 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1036 | 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1064 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1065 | |
| 1066 | if (tsi && tsr) |
| 1067 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1068 | vec_free (sa->childs[0].tsi); |
| 1069 | vec_free (sa->childs[0].tsr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1070 | sa->childs[0].tsi = tsi; |
| 1071 | sa->childs[0].tsr = tsr; |
| 1072 | } |
| 1073 | else |
| 1074 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1075 | vec_free (tsi); |
| 1076 | vec_free (tsr); |
| 1077 | ikev2_set_state (sa, IKEV2_STATE_TS_UNACCEPTABLE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1082 | ikev2_sa_auth (ikev2_sa_t * sa) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1083 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1084 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1088 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1089 | tr_prf = |
| 1090 | ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_PRF); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1091 | |
| 1092 | /* only shared key and rsa signature */ |
| 1093 | if (!(sa->i_auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC || |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1094 | sa->i_auth.method == IKEV2_AUTH_METHOD_RSA_SIG)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1095 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1096 | clib_warning ("unsupported authentication method %u", |
| 1097 | sa->i_auth.method); |
| 1098 | ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1099 | return; |
| 1100 | } |
| 1101 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1102 | key_pad = format (0, "%s", IKEV2_KEY_PAD); |
| 1103 | authmsg = ikev2_sa_generate_authmsg (sa, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1104 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1105 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1106 | 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1148 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1149 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1150 | vec_free (authmsg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1151 | |
| 1152 | if (sa->state == IKEV2_STATE_AUTHENTICATED) |
| 1153 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1154 | vec_free (sa->r_id.data); |
| 1155 | sa->r_id.data = vec_dup (sel_p->loc_id.data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1156 | sa->r_id.type = sel_p->loc_id.type; |
| 1157 | |
| 1158 | /* generate our auth data */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1159 | authmsg = ikev2_sa_generate_authmsg (sa, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1160 | if (sel_p->auth.method == IKEV2_AUTH_METHOD_SHARED_KEY_MIC) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1161 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1165 | else if (sel_p->auth.method == IKEV2_AUTH_METHOD_RSA_SIG) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1166 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1171 | |
| 1172 | /* select transforms for 1st child sa */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1173 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1176 | } |
| 1177 | else |
| 1178 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1179 | ikev2_set_state (sa, IKEV2_STATE_AUTH_FAILED); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1180 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1181 | vec_free (psk); |
| 1182 | vec_free (key_pad); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | static int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1186 | ikev2_create_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa, |
| 1187 | ikev2_child_sa_t * child) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1188 | { |
| 1189 | ipsec_add_del_tunnel_args_t a; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1190 | ikev2_sa_transform_t *tr; |
| 1191 | u8 encr_type = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1192 | |
| 1193 | if (!child->r_proposals) |
| 1194 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1195 | ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1196 | return 1; |
| 1197 | } |
| 1198 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1199 | memset (&a, 0, sizeof (a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1200 | 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1207 | tr = |
| 1208 | ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ESN); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1209 | if (tr) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1210 | a.esn = tr->esn_type; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1211 | else |
| 1212 | a.esn = 0; |
| 1213 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1214 | tr = |
| 1215 | ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_ENCR); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1216 | if (tr) |
| 1217 | { |
| 1218 | if (tr->encr_type == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1219 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1237 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1238 | { |
| 1239 | ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN); |
| 1240 | return 1; |
| 1241 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1242 | } |
| 1243 | else |
| 1244 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1245 | ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1246 | return 1; |
| 1247 | } |
| 1248 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1249 | tr = |
| 1250 | ikev2_sa_get_td_for_type (child->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1251 | if (tr) |
| 1252 | { |
| 1253 | if (tr->integ_type != IKEV2_TRANSFORM_INTEG_TYPE_AUTH_HMAC_SHA1_96) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1254 | { |
| 1255 | ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN); |
| 1256 | return 1; |
| 1257 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1258 | } |
| 1259 | else |
| 1260 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1261 | ikev2_set_state (sa, IKEV2_STATE_NO_PROPOSAL_CHOSEN); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1262 | return 1; |
| 1263 | } |
| 1264 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1265 | ikev2_calc_child_keys (sa, child); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1266 | |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 1267 | a.integ_alg = IPSEC_INTEG_ALG_SHA1_96; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1268 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1272 | |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 1273 | a.crypto_alg = encr_type; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1274 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1278 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1279 | ipsec_add_del_tunnel_if (&a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1280 | |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | static int |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1285 | ikev2_delete_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa, |
| 1286 | ikev2_child_sa_t * child) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1287 | { |
| 1288 | ipsec_add_del_tunnel_args_t a; |
| 1289 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1290 | if (!vec_len (child->r_proposals)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1291 | 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1299 | ipsec_add_del_tunnel_if (&a); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 1300 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | static u32 |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1304 | ikev2_generate_resp (ikev2_sa_t * sa, ike_header_t * ike) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1305 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1306 | v8 *integ = 0; |
| 1307 | ike_payload_header_t *ph; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1308 | u16 plen; |
| 1309 | u32 tlen = 0; |
| 1310 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1311 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1316 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1317 | ikev2_payload_chain_t *chain = 0; |
| 1318 | ikev2_payload_new_chain (chain); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1319 | |
| 1320 | if (ike->exchange == IKEV2_EXCHANGE_SA_INIT) |
| 1321 | { |
| 1322 | if (sa->r_proposals == 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1323 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1328 | else if (sa->dh_group == IKEV2_TRANSFORM_DH_TYPE_NONE) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1329 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1336 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1337 | data[0] = (tr_dh->dh_type >> 8) & 0xff; |
| 1338 | data[1] = (tr_dh->dh_type) & 0xff; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1339 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1340 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1346 | else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1347 | { |
| 1348 | u8 *data = vec_new (u8, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1349 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1350 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1356 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1357 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1363 | } |
| 1364 | else if (ike->exchange == IKEV2_EXCHANGE_IKE_AUTH) |
| 1365 | { |
| 1366 | if (sa->state == IKEV2_STATE_AUTHENTICATED) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1367 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1374 | else if (sa->state == IKEV2_STATE_AUTH_FAILED) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1375 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1381 | else if (sa->state == IKEV2_STATE_TS_UNACCEPTABLE) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1382 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1388 | else if (sa->state == IKEV2_STATE_NO_PROPOSAL_CHOSEN) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1389 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1397 | else if (sa->state == IKEV2_STATE_NOTIFY_AND_DELETE) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1398 | { |
| 1399 | u8 *data = vec_new (u8, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1400 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1401 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1407 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1408 | { |
| 1409 | ikev2_set_state (sa, IKEV2_STATE_DELETED); |
| 1410 | goto done; |
| 1411 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1412 | } |
| 1413 | else if (ike->exchange == IKEV2_EXCHANGE_INFORMATIONAL) |
| 1414 | { |
| 1415 | /* if pending delete */ |
| 1416 | if (sa->del) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1417 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1433 | /* received N(AUTHENTICATION_FAILED) */ |
| 1434 | else if (sa->state == IKEV2_STATE_AUTH_FAILED) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1435 | { |
| 1436 | ikev2_set_state (sa, IKEV2_STATE_DELETED); |
| 1437 | goto done; |
| 1438 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1439 | /* received unsupported critical payload */ |
| 1440 | else if (sa->unsupported_cp) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1441 | { |
| 1442 | u8 *data = vec_new (u8, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1443 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1444 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1451 | /* else send empty response */ |
| 1452 | } |
| 1453 | else if (ike->exchange == IKEV2_EXCHANGE_CREATE_CHILD_SA) |
| 1454 | { |
| 1455 | if (sa->rekey) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1456 | { |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1463 | else if (sa->unsupported_cp) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1464 | { |
| 1465 | u8 *data = vec_new (u8, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1466 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1467 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1474 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1475 | { |
| 1476 | ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NO_ADDITIONAL_SAS, |
| 1477 | 0); |
| 1478 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1479 | } |
| 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1485 | tlen = sizeof (*ike); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1486 | |
| 1487 | |
| 1488 | if (ike->exchange == IKEV2_EXCHANGE_SA_INIT) |
| 1489 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1490 | tlen += vec_len (chain->data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1491 | ike->nextpayload = chain->first_payload_type; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1492 | ike->length = clib_host_to_net_u32 (tlen); |
| 1493 | clib_memcpy (ike->payload, chain->data, vec_len (chain->data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1494 | |
| 1495 | /* store whole IKE payload - needed for PSK auth */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1496 | vec_free (sa->last_sa_init_res_packet_data); |
| 1497 | vec_add (sa->last_sa_init_res_packet_data, ike, tlen); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1498 | } |
| 1499 | else |
| 1500 | { |
| 1501 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1502 | ikev2_payload_chain_add_padding (chain, tr_encr->block_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1503 | |
| 1504 | /* SK payload */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1505 | plen = sizeof (*ph); |
| 1506 | ph = (ike_payload_header_t *) & ike->payload[0]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1507 | ph->nextpayload = chain->first_payload_type; |
| 1508 | ph->flags = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1509 | int enc_len = ikev2_encrypt_data (sa, chain->data, ph->payload); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1510 | 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1517 | ph->length = clib_host_to_net_u16 (plen); |
| 1518 | ike->length = clib_host_to_net_u32 (tlen); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1519 | |
| 1520 | /* calc integrity data for whole packet except hash itself */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1521 | integ = ikev2_calc_integr (tr_integ, sa->sk_ar, (u8 *) ike, |
| 1522 | tlen - tr_integ->key_trunc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1523 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1524 | clib_memcpy (ike->payload + tlen - tr_integ->key_trunc - sizeof (*ike), |
| 1525 | integ, tr_integ->key_trunc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1526 | |
| 1527 | /* store whole IKE payload - needed for retransmit */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1528 | vec_free (sa->last_res_packet_data); |
| 1529 | vec_add (sa->last_res_packet_data, ike, tlen); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | done: |
| 1533 | ikev2_payload_destroy_chain (chain); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1534 | vec_free (integ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1535 | return tlen; |
| 1536 | } |
| 1537 | |
| 1538 | static int |
| 1539 | ikev2_retransmit_sa_init (ike_header_t * ike, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1540 | ip4_address_t iaddr, ip4_address_t raddr) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1541 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1542 | ikev2_main_t *km = &ikev2_main; |
| 1543 | ikev2_sa_t *sa; |
| 1544 | u32 cpu_index = os_get_cpu_number (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1545 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1546 | /* *INDENT-OFF* */ |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 1547 | pool_foreach (sa, km->per_thread_data[cpu_index].sas, ({ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1548 | 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 Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 1580 | clib_memcpy(ike->payload, tmp->payload, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1581 | 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1602 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1603 | |
| 1604 | /* req is not retransmit */ |
| 1605 | return 0; |
| 1606 | } |
| 1607 | |
| 1608 | static int |
| 1609 | ikev2_retransmit_resp (ikev2_sa_t * sa, ike_header_t * ike) |
| 1610 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1611 | u32 msg_id = clib_net_to_host_u32 (ike->msgid); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1612 | |
| 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1622 | ike_header_t *tmp; |
| 1623 | tmp = (ike_header_t *) sa->last_res_packet_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1624 | 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1632 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1638 | return 1; |
| 1639 | } |
| 1640 | /* old req ignore */ |
| 1641 | else |
| 1642 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1643 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1647 | return -1; |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | static uword |
| 1652 | ikev2_node_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1653 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1654 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1655 | u32 n_left_from, *from, *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1656 | ikev2_next_t next_index; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1657 | ikev2_main_t *km = &ikev2_main; |
| 1658 | u32 cpu_index = os_get_cpu_number (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1659 | |
| 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1671 | { |
| 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 Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 1680 | ikev2_sa_t sa; /* temporary store for SA */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1681 | int len = 0; |
| 1682 | int r; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1683 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1684 | /* 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1691 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1692 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1698 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1699 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1705 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1706 | if (ike0->exchange == IKEV2_EXCHANGE_SA_INIT) |
| 1707 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1708 | sa0 = &sa; |
| 1709 | memset (sa0, 0, sizeof (*sa0)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1710 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1711 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1715 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1716 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1732 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1733 | ikev2_process_sa_init_req (vm, sa0, ike0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1734 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1735 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1743 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1744 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1749 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1750 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1774 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1775 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1791 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1792 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1814 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1815 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1831 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1832 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1869 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1870 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1886 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1887 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1912 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1913 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1933 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1934 | vec_foreach (c, sa0->childs) |
| 1935 | ikev2_delete_tunnel_interface (km->vnet_main, sa0, c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1936 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1937 | ikev2_delete_sa (sa0); |
| 1938 | } |
| 1939 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1940 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1941 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1948 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1949 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 1950 | n_left_to_next, bi0, next0); |
| 1951 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1952 | |
| 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1957 | IKEV2_ERROR_PROCESSED, frame->n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1958 | return frame->n_vectors; |
| 1959 | } |
| 1960 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1961 | /* *INDENT-OFF* */ |
Jean-Mickael Guerin | 8941ec2 | 2016-03-04 14:14:21 +0100 | [diff] [blame] | 1962 | VLIB_REGISTER_NODE (ikev2_node,static) = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1963 | .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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1979 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1980 | |
| 1981 | |
| 1982 | static ikev2_profile_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1983 | ikev2_profile_index_by_name (u8 * name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1984 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1985 | ikev2_main_t *km = &ikev2_main; |
| 1986 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1987 | |
| 1988 | p = mhash_get (&km->profile_index_by_name, name); |
| 1989 | if (!p) |
| 1990 | return 0; |
| 1991 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1992 | return pool_elt_at_index (km->profiles, p[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | clib_error_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1996 | ikev2_set_local_key (vlib_main_t * vm, u8 * file) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1997 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1998 | ikev2_main_t *km = &ikev2_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1999 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2000 | km->pkey = ikev2_load_key_file (file); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2001 | if (km->pkey == NULL) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2002 | return clib_error_return (0, "load key '%s' failed", file); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2003 | |
| 2004 | return 0; |
| 2005 | } |
| 2006 | |
| 2007 | clib_error_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2008 | ikev2_add_del_profile (vlib_main_t * vm, u8 * name, int is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2009 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2010 | ikev2_main_t *km = &ikev2_main; |
| 2011 | ikev2_profile_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2012 | |
| 2013 | if (is_add) |
| 2014 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2015 | if (ikev2_profile_index_by_name (name)) |
| 2016 | return clib_error_return (0, "policy %v already exists", name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2017 | |
| 2018 | pool_get (km->profiles, p); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2019 | memset (p, 0, sizeof (*p)); |
| 2020 | p->name = vec_dup (name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2021 | uword index = p - km->profiles; |
| 2022 | mhash_set_mem (&km->profile_index_by_name, name, &index, 0); |
| 2023 | } |
| 2024 | else |
| 2025 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2026 | p = ikev2_profile_index_by_name (name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2027 | if (!p) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2028 | return clib_error_return (0, "policy %v does not exists", name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2029 | |
| 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 | |
| 2037 | clib_error_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2038 | ikev2_set_profile_auth (vlib_main_t * vm, u8 * name, u8 auth_method, |
| 2039 | u8 * auth_data, u8 data_hex_format) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2040 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2041 | ikev2_profile_t *p; |
| 2042 | clib_error_t *r; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2043 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2044 | p = ikev2_profile_index_by_name (name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2045 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2046 | if (!p) |
| 2047 | { |
| 2048 | r = clib_error_return (0, "unknown profile %v", name); |
| 2049 | return r; |
| 2050 | } |
| 2051 | vec_free (p->auth.data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2052 | p->auth.method = auth_method; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2053 | p->auth.data = vec_dup (auth_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2054 | p->auth.hex = data_hex_format; |
| 2055 | |
| 2056 | if (auth_method == IKEV2_AUTH_METHOD_RSA_SIG) |
| 2057 | { |
Matus Fabian | 698b952 | 2016-09-13 23:15:56 -0700 | [diff] [blame] | 2058 | vec_add1 (p->auth.data, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2059 | if (p->auth.key) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2060 | EVP_PKEY_free (p->auth.key); |
| 2061 | p->auth.key = ikev2_load_cert_file (auth_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2062 | if (p->auth.key == NULL) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2063 | return clib_error_return (0, "load cert '%s' failed", auth_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | return 0; |
| 2067 | } |
| 2068 | |
| 2069 | clib_error_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2070 | ikev2_set_profile_id (vlib_main_t * vm, u8 * name, u8 id_type, u8 * data, |
| 2071 | int is_local) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2072 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2073 | ikev2_profile_t *p; |
| 2074 | clib_error_t *r; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2075 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2076 | if (id_type > IKEV2_ID_TYPE_ID_RFC822_ADDR |
| 2077 | && id_type < IKEV2_ID_TYPE_ID_KEY_ID) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2078 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2079 | r = clib_error_return (0, "unsupported identity type %U", |
| 2080 | format_ikev2_id_type, id_type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2081 | return r; |
| 2082 | } |
| 2083 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2084 | p = ikev2_profile_index_by_name (name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2085 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2086 | if (!p) |
| 2087 | { |
| 2088 | r = clib_error_return (0, "unknown profile %v", name); |
| 2089 | return r; |
| 2090 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2091 | |
| 2092 | if (is_local) |
| 2093 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2094 | vec_free (p->loc_id.data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2095 | p->loc_id.type = id_type; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2096 | p->loc_id.data = vec_dup (data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2097 | } |
| 2098 | else |
| 2099 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2100 | vec_free (p->rem_id.data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2101 | p->rem_id.type = id_type; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2102 | p->rem_id.data = vec_dup (data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | return 0; |
| 2106 | } |
| 2107 | |
| 2108 | clib_error_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2109 | ikev2_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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2112 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2113 | ikev2_profile_t *p; |
| 2114 | clib_error_t *r; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2115 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2116 | p = ikev2_profile_index_by_name (name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2117 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2118 | if (!p) |
| 2119 | { |
| 2120 | r = clib_error_return (0, "unknown profile %v", name); |
| 2121 | return r; |
| 2122 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2123 | |
| 2124 | if (is_local) |
| 2125 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2126 | p->loc_ts.start_addr.as_u32 = start_addr.as_u32; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2127 | 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 | |
| 2147 | clib_error_t * |
| 2148 | ikev2_init (vlib_main_t * vm) |
| 2149 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2150 | ikev2_main_t *km = &ikev2_main; |
| 2151 | clib_error_t *error; |
| 2152 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 2153 | int thread_id; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2154 | |
| 2155 | memset (km, 0, sizeof (ikev2_main_t)); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2156 | km->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2157 | km->vlib_main = vm; |
| 2158 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2159 | ikev2_crypto_init (km); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2160 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2161 | mhash_init_vec_string (&km->profile_index_by_name, sizeof (uword)); |
| 2162 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2163 | vec_validate (km->per_thread_data, tm->n_vlib_mains - 1); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 2164 | 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2167 | hash_create (0, sizeof (uword)); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 2168 | } |
| 2169 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2170 | 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) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 2179 | |
| 2180 | /* |
| 2181 | * fd.io coding-style-patch-verification: ON |
| 2182 | * |
| 2183 | * Local Variables: |
| 2184 | * eval: (c-set-style "gnu") |
| 2185 | * End: |
| 2186 | */ |