diff options
author | Filip Tehlar <ftehlar@cisco.com> | 2020-04-02 13:13:39 +0000 |
---|---|---|
committer | Neale Ranns <nranns@cisco.com> | 2020-05-15 11:41:14 +0000 |
commit | 4362baa33c00b93d07f6648c87c064c96900f4df (patch) | |
tree | f7ffb67f3b6a98348ae4f88ae8d06f8d0ecbeffd /src/plugins/ikev2 | |
parent | 17b5c3d6ab83a597e7c79613041f2fd7470825bd (diff) |
ikev2: add support for NAT traversal
Type: feature
* initiator behind NAT supported
* tested with static NAT mappings
* works only with pre-configured tunnels
The pre-configured tunnel has to be defined as follows:
initiator (i) side: src=ip(i) dst=ip(r)
responder (r) side: src=ip(r) dst=ip(nat)
Change-Id: Ia9f79ddbbcc3f7dc8fde6bbeca2a433e3b784e94
Signed-off-by: Filip Tehlar <ftehlar@cisco.com>
Diffstat (limited to 'src/plugins/ikev2')
-rw-r--r-- | src/plugins/ikev2/ikev2.c | 349 | ||||
-rw-r--r-- | src/plugins/ikev2/ikev2.h | 5 | ||||
-rw-r--r-- | src/plugins/ikev2/ikev2_cli.c | 4 | ||||
-rw-r--r-- | src/plugins/ikev2/ikev2_priv.h | 10 |
4 files changed, 286 insertions, 82 deletions
diff --git a/src/plugins/ikev2/ikev2.c b/src/plugins/ikev2/ikev2.c index 068ae7cee83..b49832d0080 100644 --- a/src/plugins/ikev2/ikev2.c +++ b/src/plugins/ikev2/ikev2.c @@ -28,6 +28,7 @@ #include <plugins/ikev2/ikev2.h> #include <plugins/ikev2/ikev2_priv.h> #include <openssl/sha.h> +#include <vnet/ipsec/ipsec_punt.h> #define IKEV2_LIVENESS_RETRIES 3 #define IKEV2_LIVENESS_PERIOD_CHECK 30 @@ -92,6 +93,22 @@ typedef enum IKEV2_N_NEXT, } ikev2_next_t; +typedef u32 ikev2_non_esp_marker; + +static_always_inline u16 +ikev2_get_port (ikev2_sa_t * sa) +{ + return sa->natt ? IKEV2_PORT_NATT : IKEV2_PORT; +} + +static_always_inline int +ikev2_insert_non_esp_marker (ike_header_t * ike, int len) +{ + memmove ((u8 *) ike + sizeof (ikev2_non_esp_marker), ike, len); + clib_memset (ike, 0, sizeof (ikev2_non_esp_marker)); + return len + sizeof (ikev2_non_esp_marker); +} + static ikev2_sa_transform_t * ikev2_find_transform_data (ikev2_sa_transform_t * t) { @@ -401,7 +418,9 @@ ikev2_complete_sa_data (ikev2_sa_t * sa, ikev2_sa_t * sai) sa->last_sa_init_req_packet_data = _(sai->last_sa_init_req_packet_data); sa->childs = _(sai->childs); sa->udp_encap = sai->udp_encap; + sa->ipsec_over_udp_port = sai->ipsec_over_udp_port; sa->dst_port = sai->dst_port; + sa->sw_if_index = sai->sw_if_index; #undef _ @@ -587,9 +606,24 @@ ikev2_calc_child_keys (ikev2_sa_t * sa, ikev2_child_sa_t * child) vec_free (keymat); } +static_always_inline u8 * +ikev2_compute_nat_sha1 (u64 ispi, u64 rspi, u32 ip, u16 port) +{ + /* ispi, rspi, ip, port */ + u8 buf[8 + 8 + 4 + 2]; + u8 *res = vec_new (u8, 20); + + clib_memcpy_fast (&buf[0], &ispi, sizeof (ispi)); + clib_memcpy_fast (&buf[8], &rspi, sizeof (rspi)); + clib_memcpy_fast (&buf[8 + 8], &ip, sizeof (ip)); + clib_memcpy_fast (&buf[8 + 8 + 4], &port, sizeof (port)); + SHA1 (buf, sizeof (buf), res); + return res; +} + static void ikev2_process_sa_init_req (vlib_main_t * vm, ikev2_sa_t * sa, - ike_header_t * ike) + ike_header_t * ike, udp_header_t * udp) { int p = 0; u32 len = clib_net_to_host_u32 (ike->length); @@ -634,6 +668,37 @@ ikev2_process_sa_init_req (vlib_main_t * vm, ikev2_sa_t * sa, else if (payload == IKEV2_PAYLOAD_NOTIFY) { ikev2_notify_t *n = ikev2_parse_notify_payload (ikep); + if (n->msg_type == IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP) + { + u8 *src_sha = + ikev2_compute_nat_sha1 (clib_net_to_host_u64 (ike->ispi), 0, + clib_net_to_host_u32 (sa-> + iaddr.as_u32), + udp->src_port); + if (clib_memcmp (src_sha, n->data, vec_len (src_sha))) + { + sa->natt = 1; + ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx initiator" + " behind NAT", sa->ispi); + } + vec_free (src_sha); + } + else if (n->msg_type == + IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP) + { + u8 *dst_sha = + ikev2_compute_nat_sha1 (clib_net_to_host_u64 (ike->ispi), 0, + clib_net_to_host_u32 (sa-> + raddr.as_u32), + udp->dst_port); + if (clib_memcmp (dst_sha, n->data, vec_len (dst_sha))) + { + sa->natt = 1; + ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx responder" + " (self) behind NAT", sa->ispi); + } + vec_free (dst_sha); + } vec_free (n); } else if (payload == IKEV2_PAYLOAD_VENDOR) @@ -661,7 +726,7 @@ ikev2_process_sa_init_req (vlib_main_t * vm, ikev2_sa_t * sa, static void ikev2_process_sa_init_resp (vlib_main_t * vm, ikev2_sa_t * sa, - ike_header_t * ike) + ike_header_t * ike, udp_header_t * udp) { int p = 0; u32 len = clib_net_to_host_u32 (ike->length); @@ -712,6 +777,35 @@ ikev2_process_sa_init_resp (vlib_main_t * vm, ikev2_sa_t * sa, else if (payload == IKEV2_PAYLOAD_NOTIFY) { ikev2_notify_t *n = ikev2_parse_notify_payload (ikep); + if (n->msg_type == IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP) + { + u8 *src_sha = ikev2_compute_nat_sha1 (ike->ispi, + ike->rspi, + clib_net_to_host_u32 + (sa->raddr.as_u32), + udp->src_port); + if (clib_memcmp (src_sha, n->data, vec_len (src_sha))) + { + ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx responder" + " behind NAT, unsupported", sa->ispi); + } + vec_free (src_sha); + } + else if (n->msg_type == + IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP) + { + u8 *dst_sha = ikev2_compute_nat_sha1 (ike->ispi, + ike->rspi, + sa->iaddr.as_u32, + udp->dst_port); + if (clib_memcmp (dst_sha, n->data, vec_len (dst_sha))) + { + sa->natt = 1; + ikev2_elog_uint (IKEV2_LOG_DEBUG, "ispi %lx initiator" + " (self) behind NAT", sa->ispi); + } + vec_free (dst_sha); + } vec_free (n); } else if (payload == IKEV2_PAYLOAD_VENDOR) @@ -748,6 +842,9 @@ ikev2_decrypt_sk_payload (ikev2_sa_t * sa, ike_header_t * ike, u8 * payload) tr_integ = ikev2_sa_get_td_for_type (sa->r_proposals, IKEV2_TRANSFORM_TYPE_INTEG); + if (!sa->sk_ar || !sa->sk_ai) + return 0; + while (p < len && *payload != IKEV2_PAYLOAD_NONE && last_payload != IKEV2_PAYLOAD_SK) { @@ -1117,7 +1214,7 @@ ikev2_process_create_child_sa_req (vlib_main_t * vm, ikev2_sa_t * sa, if (ikep->flags & IKEV2_PAYLOAD_FLAG_CRITICAL) { sa->unsupported_cp = payload; - return; + goto cleanup_and_exit; } } @@ -1385,7 +1482,7 @@ ikev2_sa_auth (ikev2_sa_t * sa) if (sel_p) { sa->udp_encap = sel_p->udp_encap; - sa->dst_port = sel_p->dst_port; + sa->ipsec_over_udp_port = sel_p->ipsec_over_udp_port; } vec_free (authmsg); @@ -1508,6 +1605,8 @@ typedef struct ipsec_key_t loc_ckey, rem_ckey, loc_ikey, rem_ikey; u8 is_rekey; u32 old_remote_sa_id; + u16 ipsec_over_udp_port; + u16 src_port; u16 dst_port; } ikev2_add_ipsec_tunnel_args_t; @@ -1566,13 +1665,14 @@ ikev2_add_tunnel_from_main (ikev2_add_ipsec_tunnel_args_t * a) IPSEC_PROTOCOL_ESP, a->encr_type, &a->loc_ckey, a->integ_type, &a->loc_ikey, a->flags, 0, a->salt_local, &a->local_ip, - &a->remote_ip, NULL, a->dst_port, a->dst_port); + &a->remote_ip, NULL, a->src_port, a->dst_port); rv |= ipsec_sa_add_and_lock (a->remote_sa_id, a->remote_spi, IPSEC_PROTOCOL_ESP, a->encr_type, &a->rem_ckey, a->integ_type, &a->rem_ikey, (a->flags | IPSEC_SA_FLAG_IS_INBOUND), 0, a->salt_remote, &a->remote_ip, - &a->local_ip, NULL, a->dst_port, a->dst_port); + &a->local_ip, NULL, a->ipsec_over_udp_port, + a->ipsec_over_udp_port); rv |= ipsec_tun_protect_update (sw_if_index, NULL, a->local_sa_id, sas_in); } @@ -1624,6 +1724,8 @@ ikev2_create_tunnel_interface (vlib_main_t * vm, a.flags |= IPSEC_SA_FLAG_IS_TUNNEL; a.flags |= IPSEC_SA_FLAG_UDP_ENCAP; } + if (sa->natt) + a.flags |= IPSEC_SA_FLAG_UDP_ENCAP; a.is_rekey = is_rekey; tr = ikev2_sa_get_td_for_type (proposals, IKEV2_TRANSFORM_TYPE_ESN); @@ -1735,6 +1837,7 @@ ikev2_create_tunnel_interface (vlib_main_t * vm, a.salt_remote = child->salt_er; a.salt_local = child->salt_ei; } + a.dst_port = a.src_port = sa->ipsec_over_udp_port; } else { @@ -1747,6 +1850,8 @@ ikev2_create_tunnel_interface (vlib_main_t * vm, a.salt_remote = child->salt_ei; a.salt_local = child->salt_er; } + a.dst_port = sa->natt ? sa->dst_port : sa->ipsec_over_udp_port; + a.src_port = sa->ipsec_over_udp_port; } if (sa->is_profile_index_set) @@ -1807,7 +1912,7 @@ ikev2_create_tunnel_interface (vlib_main_t * vm, child->remote_sa_id = a.remote_sa_id = remote_sa_id; a.sw_if_index = (sa->is_tun_itf_set ? sa->tun_itf : ~0); - a.dst_port = sa->dst_port; + a.ipsec_over_udp_port = sa->ipsec_over_udp_port; vl_api_rpc_call_main_thread (ikev2_add_tunnel_from_main, (u8 *) & a, sizeof (a)); @@ -1906,7 +2011,8 @@ ikev2_delete_tunnel_interface (vnet_main_t * vnm, ikev2_sa_t * sa, } static u32 -ikev2_generate_message (ikev2_sa_t * sa, ike_header_t * ike, void *user) +ikev2_generate_message (ikev2_sa_t * sa, ike_header_t * ike, void *user, + udp_header_t * udp) { v8 *integ = 0; ike_payload_header_t *ph; @@ -1960,10 +2066,30 @@ ikev2_generate_message (ikev2_sa_t * sa, ike_header_t * ike, void *user) } else { + ASSERT (udp); + ike->rspi = clib_host_to_net_u64 (sa->rspi); ikev2_payload_add_sa (chain, sa->r_proposals); ikev2_payload_add_ke (chain, sa->dh_group, sa->r_dh_data); ikev2_payload_add_nonce (chain, sa->r_nonce); + + u8 *nat_detection_sha1 = + ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa->ispi), + clib_host_to_net_u64 (sa->rspi), + clib_host_to_net_u32 (sa->raddr.as_u32), + udp->dst_port); + ikev2_payload_add_notify (chain, + IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP, + nat_detection_sha1); + vec_free (nat_detection_sha1); + nat_detection_sha1 = + ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa->ispi), + clib_host_to_net_u64 (sa->rspi), + sa->iaddr.as_u32, udp->src_port); + ikev2_payload_add_notify (chain, + IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP, + nat_detection_sha1); + vec_free (nat_detection_sha1); } } else if (ike->exchange == IKEV2_EXCHANGE_IKE_AUTH) @@ -2092,7 +2218,7 @@ ikev2_generate_message (ikev2_sa_t * sa, ike_header_t * ike, void *user) } else { - if (sa->rekey) + if (vec_len (sa->rekey) > 0) { ikev2_payload_add_sa (chain, sa->rekey[0].r_proposal); ikev2_payload_add_nonce (chain, sa->r_nonce); @@ -2360,7 +2486,7 @@ ikev2_node_fn (vlib_main_t * vm, ikev2_sa_t *sa0 = 0; ikev2_sa_t sa; /* temporary store for SA */ int len = 0; - int r; + int r, is_req = 0, has_non_esp_marker = 0; /* speculatively enqueue b0 to the current next frame */ bi0 = from[0]; @@ -2371,11 +2497,31 @@ ikev2_node_fn (vlib_main_t * vm, n_left_to_next -= 1; b0 = vlib_get_buffer (vm, bi0); - ike0 = vlib_buffer_get_current (b0); - vlib_buffer_advance (b0, -sizeof (*udp0)); - udp0 = vlib_buffer_get_current (b0); - vlib_buffer_advance (b0, -sizeof (*ip40)); - ip40 = vlib_buffer_get_current (b0); + + if (b0->punt_reason == ipsec_punt_reason[IPSEC_PUNT_IP4_SPI_UDP_0]) + { + u8 *ptr = vlib_buffer_get_current (b0); + ip40 = (ip4_header_t *) ptr; + ptr += sizeof (*ip40); + udp0 = (udp_header_t *) ptr; + ptr += sizeof (*udp0); + ike0 = (ike_header_t *) ptr; + } + else + { + ike0 = vlib_buffer_get_current (b0); + vlib_buffer_advance (b0, -sizeof (*udp0)); + udp0 = vlib_buffer_get_current (b0); + vlib_buffer_advance (b0, -sizeof (*ip40)); + ip40 = vlib_buffer_get_current (b0); + } + + /* check for non-esp marker */ + if (*((u32 *) ike0) == 0) + { + ike0 = (ike_header_t *) ((u8 *) ike0 + sizeof (u32)); + has_non_esp_marker = 1; + } if (ike0->version != IKE_VERSION_2) { @@ -2395,6 +2541,7 @@ ikev2_node_fn (vlib_main_t * vm, { sa0->raddr.as_u32 = ip40->dst_address.as_u32; sa0->iaddr.as_u32 = ip40->src_address.as_u32; + sa0->dst_port = clib_net_to_host_u16 (udp0->src_port); r = ikev2_retransmit_sa_init (ike0, sa0->iaddr, sa0->raddr); @@ -2414,7 +2561,7 @@ ikev2_node_fn (vlib_main_t * vm, goto dispatch0; } - ikev2_process_sa_init_req (vm, sa0, ike0); + ikev2_process_sa_init_req (vm, sa0, ike0, udp0); if (sa0->state == IKEV2_STATE_SA_INIT) { @@ -2428,7 +2575,7 @@ ikev2_node_fn (vlib_main_t * vm, if (sa0->state == IKEV2_STATE_SA_INIT || sa0->state == IKEV2_STATE_NOTIFY_AND_DELETE) { - len = ikev2_generate_message (sa0, ike0, 0); + len = ikev2_generate_message (sa0, ike0, 0, udp0); } if (sa0->state == IKEV2_STATE_SA_INIT) @@ -2452,10 +2599,13 @@ ikev2_node_fn (vlib_main_t * vm, } else //received sa_init without initiator flag { - ikev2_process_sa_init_resp (vm, sa0, ike0); + sa0->raddr.as_u32 = ip40->src_address.as_u32; + sa0->iaddr.as_u32 = ip40->dst_address.as_u32; + ikev2_process_sa_init_resp (vm, sa0, ike0, udp0); if (sa0->state == IKEV2_STATE_SA_INIT) { + is_req = 1; ike0->exchange = IKEV2_EXCHANGE_IKE_AUTH; uword *p = hash_get (km->sa_by_ispi, ike0->ispi); if (p) @@ -2469,7 +2619,8 @@ ikev2_node_fn (vlib_main_t * vm, ikev2_complete_sa_data (sa0, sai); ikev2_calc_keys (sa0); ikev2_sa_auth_init (sa0); - len = ikev2_generate_message (sa0, ike0, 0); + len = + ikev2_generate_message (sa0, ike0, 0, udp0); } else { @@ -2522,6 +2673,7 @@ ikev2_node_fn (vlib_main_t * vm, goto dispatch0; } + sa0->dst_port = clib_net_to_host_u16 (udp0->src_port); ikev2_process_auth_req (vm, sa0, ike0); ikev2_sa_auth (sa0); if (sa0->state == IKEV2_STATE_AUTHENTICATED) @@ -2540,7 +2692,7 @@ ikev2_node_fn (vlib_main_t * vm, } else { - len = ikev2_generate_message (sa0, ike0, 0); + len = ikev2_generate_message (sa0, ike0, 0, udp0); } } } @@ -2607,7 +2759,7 @@ ikev2_node_fn (vlib_main_t * vm, if (!(ike0->flags & IKEV2_HDR_FLAG_RESPONSE)) { ike0->flags |= IKEV2_HDR_FLAG_RESPONSE; - len = ikev2_generate_message (sa0, ike0, 0); + len = ikev2_generate_message (sa0, ike0, 0, udp0); } } } @@ -2645,9 +2797,10 @@ ikev2_node_fn (vlib_main_t * vm, if (sa0->rekey[0].protocol_id != IKEV2_PROTOCOL_IKE) { if (sa0->childs) - vec_free (sa0->childs); + ikev2_sa_free_all_child_sa (&sa0->childs); ikev2_child_sa_t *child; vec_add2 (sa0->childs, child, 1); + clib_memset (child, 0, sizeof (*child)); child->r_proposals = sa0->rekey[0].r_proposal; child->i_proposals = sa0->rekey[0].i_proposal; child->tsi = sa0->rekey[0].tsi; @@ -2659,11 +2812,11 @@ ikev2_node_fn (vlib_main_t * vm, } if (sa0->is_initiator) { - vec_del1 (sa0->rekey, 0); + vec_free (sa0->rekey); } else { - len = ikev2_generate_message (sa0, ike0, 0); + len = ikev2_generate_message (sa0, ike0, 0, udp0); } } } @@ -2692,6 +2845,29 @@ ikev2_node_fn (vlib_main_t * vm, ip40->dst_address.as_u32 = sa0->iaddr.as_u32; ip40->src_address.as_u32 = sa0->raddr.as_u32; } + + if (is_req) + { + udp0->dst_port = udp0->src_port = + clib_net_to_host_u16 (ikev2_get_port (sa0)); + + if (udp0->dst_port == clib_net_to_host_u16 (IKEV2_PORT_NATT) + && sa0->natt) + { + if (!has_non_esp_marker) + len = ikev2_insert_non_esp_marker (ike0, len); + } + } + else + { + if (has_non_esp_marker) + len += sizeof (ikev2_non_esp_marker); + + u16 tp = udp0->dst_port; + udp0->dst_port = udp0->src_port; + udp0->src_port = tp; + } + udp0->length = clib_host_to_net_u16 (len + sizeof (udp_header_t)); udp0->checksum = 0; @@ -2898,7 +3074,7 @@ ikev2_profile_index_by_name (u8 * name) static void ikev2_send_ike (vlib_main_t * vm, ip4_address_t * src, ip4_address_t * dst, - u32 bi0, u32 len) + u32 bi0, u32 len, u16 src_port, u16 dst_port, u32 sw_if_index) { ip4_header_t *ip40; udp_header_t *udp0; @@ -2921,14 +3097,16 @@ ikev2_send_ike (vlib_main_t * vm, ip4_address_t * src, ip4_address_t * dst, ip40->protocol = IP_PROTOCOL_UDP; ip40->dst_address.as_u32 = dst->as_u32; ip40->src_address.as_u32 = src->as_u32; - udp0->dst_port = clib_host_to_net_u16 (500); - udp0->src_port = clib_host_to_net_u16 (500); + udp0->dst_port = clib_host_to_net_u16 (dst_port); + udp0->src_port = clib_host_to_net_u16 (src_port); udp0->length = clib_host_to_net_u16 (len + sizeof (udp_header_t)); udp0->checksum = 0; b0->current_length = len + sizeof (ip4_header_t) + sizeof (udp_header_t); ip40->length = clib_host_to_net_u16 (b0->current_length); ip40->checksum = ip4_header_checksum (ip40); + vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index; + vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0; /* send the request */ f = vlib_get_frame_to_node (vm, ip4_lookup_node.index); @@ -2989,7 +3167,7 @@ ikev2_register_udp_port (ikev2_profile_t * p, u16 port) ipsec4_tun_input_node.index, 1); hash_set (km->udp_ports, port, 1); } - p->dst_port = port; + p->ipsec_over_udp_port = port; return 0; } @@ -2999,10 +3177,10 @@ ikev2_unregister_udp_port (ikev2_profile_t * p) ikev2_main_t *km = &ikev2_main; uword *v; - if (p->dst_port == IPSEC_UDP_PORT_NONE) + if (p->ipsec_over_udp_port == IPSEC_UDP_PORT_NONE) return; - v = hash_get (km->udp_ports, p->dst_port); + v = hash_get (km->udp_ports, p->ipsec_over_udp_port); if (!v) return; @@ -3010,11 +3188,11 @@ ikev2_unregister_udp_port (ikev2_profile_t * p) if (v[0] == 0) { - udp_unregister_dst_port (km->vlib_main, p->dst_port, 1); - hash_unset (km->udp_ports, p->dst_port); + udp_unregister_dst_port (km->vlib_main, p->ipsec_over_udp_port, 1); + hash_unset (km->udp_ports, p->ipsec_over_udp_port); } - p->dst_port = IPSEC_UDP_PORT_NONE; + p->ipsec_over_udp_port = IPSEC_UDP_PORT_NONE; } clib_error_t * @@ -3031,7 +3209,7 @@ ikev2_add_del_profile (vlib_main_t * vm, u8 * name, int is_add) pool_get (km->profiles, p); clib_memset (p, 0, sizeof (*p)); p->name = vec_dup (name); - p->dst_port = IPSEC_UDP_PORT_NONE; + p->ipsec_over_udp_port = IPSEC_UDP_PORT_NONE; p->responder.sw_if_index = ~0; p->tun_itf = ~0; uword index = p - km->profiles; @@ -3267,7 +3445,7 @@ ikev2_set_profile_ipsec_udp_port (vlib_main_t * vm, u8 * name, u16 port, if (is_set) { - if (p->dst_port != IPSEC_UDP_PORT_NONE) + if (p->ipsec_over_udp_port != IPSEC_UDP_PORT_NONE) return VNET_API_ERROR_VALUE_EXIST; rv = ikev2_register_udp_port (p, port); @@ -3278,7 +3456,7 @@ ikev2_set_profile_ipsec_udp_port (vlib_main_t * vm, u8 * name, u16 port, if (!v) return VNET_API_ERROR_IKE_NO_PORT; - if (p->dst_port == IPSEC_UDP_PORT_NONE) + if (p->ipsec_over_udp_port == IPSEC_UDP_PORT_NONE) return VNET_API_ERROR_INVALID_VALUE; ikev2_unregister_udp_port (p); @@ -3387,9 +3565,10 @@ ikev2_initiate_sa_init (vlib_main_t * vm, u8 * name) sa.state = IKEV2_STATE_SA_INIT; sa.tun_itf = p->tun_itf; sa.udp_encap = p->udp_encap; - sa.dst_port = p->dst_port; + sa.ipsec_over_udp_port = p->ipsec_over_udp_port; sa.is_tun_itf_set = 1; sa.initial_contact = 1; + sa.dst_port = IKEV2_PORT; ikev2_generate_sa_init_data (&sa); ikev2_payload_add_ke (chain, sa.dh_group, sa.i_dh_data); ikev2_payload_add_nonce (chain, sa.i_nonce); @@ -3403,29 +3582,21 @@ ikev2_initiate_sa_init (vlib_main_t * vm, u8 * name) RAND_bytes ((u8 *) & sa.childs[0].i_proposals[0].spi, sizeof (sa.childs[0].i_proposals[0].spi)); - - /* Add NAT detection notification messages (mandatory) */ - u8 nat_detection_source[8 + 8 + 4 + 2]; - u8 *nat_detection_sha1 = vec_new (u8, 20); - - u64 tmpspi = clib_host_to_net_u64 (sa.ispi); - clib_memcpy_fast (&nat_detection_source[0], &tmpspi, sizeof (tmpspi)); - tmpspi = clib_host_to_net_u64 (sa.rspi); - clib_memcpy_fast (&nat_detection_source[8], &tmpspi, sizeof (tmpspi)); - u16 tmpport = clib_host_to_net_u16 (500); - clib_memcpy_fast (&nat_detection_source[8 + 8 + 4], &tmpport, - sizeof (tmpport)); - u32 tmpip = clib_host_to_net_u32 (if_ip->as_u32); - clib_memcpy_fast (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip)); - SHA1 (nat_detection_source, sizeof (nat_detection_source), - nat_detection_sha1); + u8 *nat_detection_sha1 = + ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa.ispi), + clib_host_to_net_u64 (sa.rspi), + clib_host_to_net_u32 (if_ip->as_u32), + clib_host_to_net_u16 (IKEV2_PORT)); + ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NAT_DETECTION_SOURCE_IP, nat_detection_sha1); - tmpip = clib_host_to_net_u32 (p->responder.ip4.as_u32); - clib_memcpy_fast (&nat_detection_source[8 + 8], &tmpip, sizeof (tmpip)); - SHA1 (nat_detection_source, sizeof (nat_detection_source), - nat_detection_sha1); + vec_free (nat_detection_sha1); + nat_detection_sha1 = + ikev2_compute_nat_sha1 (clib_host_to_net_u64 (sa.ispi), + clib_host_to_net_u64 (sa.rspi), + clib_host_to_net_u32 (p->responder.ip4.as_u32), + clib_host_to_net_u16 (sa.dst_port)); ikev2_payload_add_notify (chain, IKEV2_NOTIFY_MSG_NAT_DETECTION_DESTINATION_IP, nat_detection_sha1); @@ -3466,6 +3637,7 @@ ikev2_initiate_sa_init (vlib_main_t * vm, u8 * name) sa.i_auth.method = p->auth.method; sa.i_auth.hex = p->auth.hex; sa.i_auth.data = vec_dup (p->auth.data); + sa.sw_if_index = p->responder.sw_if_index; vec_add (sa.childs[0].tsi, &p->loc_ts, 1); vec_add (sa.childs[0].tsr, &p->rem_ts, 1); @@ -3477,7 +3649,8 @@ ikev2_initiate_sa_init (vlib_main_t * vm, u8 * name) clib_memcpy_fast (sa0, &sa, sizeof (*sa0)); hash_set (km->sa_by_ispi, sa0->ispi, sa0 - km->sais); - ikev2_send_ike (vm, if_ip, &p->responder.ip4, bi0, len); + ikev2_send_ike (vm, if_ip, &p->responder.ip4, bi0, len, + IKEV2_PORT, sa.dst_port, sa.sw_if_index); ikev2_elog_exchange ("ispi %lx rspi %lx IKEV2_EXCHANGE_SA_INIT sent to " "%d.%d.%d.%d", clib_host_to_net_u64 (sa0->ispi), 0, @@ -3508,9 +3681,11 @@ ikev2_delete_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa, sa->del->spi = csa->i_proposals->spi; ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1); sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid); - len = ikev2_generate_message (sa, ike0, 0); - - ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len); + len = ikev2_generate_message (sa, ike0, 0, 0); + if (sa->natt) + len = ikev2_insert_non_esp_marker (ike0, len); + ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len, + ikev2_get_port (sa), sa->dst_port, sa->sw_if_index); /* delete local child SA */ ikev2_delete_tunnel_interface (km->vnet_main, sa, csa); @@ -3608,9 +3783,11 @@ ikev2_initiate_delete_ike_sa (vlib_main_t * vm, u64 ispi) fsa->del->spi = ispi; ike0->msgid = clib_host_to_net_u32 (fsa->last_init_msg_id + 1); fsa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid); - len = ikev2_generate_message (fsa, ike0, 0); - - ikev2_send_ike (vm, &fsa->iaddr, &fsa->raddr, bi0, len); + len = ikev2_generate_message (fsa, ike0, 0, 0); + if (fsa->natt) + len = ikev2_insert_non_esp_marker (ike0, len); + ikev2_send_ike (vm, &fsa->iaddr, &fsa->raddr, bi0, len, + ikev2_get_port (fsa), fsa->dst_port, fsa->sw_if_index); } @@ -3662,8 +3839,11 @@ ikev2_rekey_child_sa_internal (vlib_main_t * vm, ikev2_sa_t * sa, RAND_bytes ((u8 *) & proposals[0].spi, sizeof (proposals[0].spi)); rekey->spi = proposals[0].spi; rekey->ispi = csa->i_proposals->spi; - len = ikev2_generate_message (sa, ike0, proposals); - ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len); + len = ikev2_generate_message (sa, ike0, proposals, 0); + if (sa->natt) + len = ikev2_insert_non_esp_marker (ike0, len); + ikev2_send_ike (vm, &sa->iaddr, &sa->raddr, bi0, len, + ikev2_get_port (sa), ikev2_get_port (sa), sa->sw_if_index); vec_free (proposals); } @@ -3736,8 +3916,12 @@ ikev2_init (vlib_main_t * vm) km->sw_if_indices = hash_create (0, 0); km->udp_ports = hash_create (0, sizeof (uword)); - udp_register_dst_port (vm, 500, ikev2_node.index, 1); + udp_register_dst_port (vm, IKEV2_PORT, ikev2_node.index, 1); + udp_register_dst_port (vm, IKEV2_PORT_NATT, ikev2_node.index, 1); + vlib_punt_hdl_t punt_hdl = vlib_punt_client_register ("ikev2"); + vlib_punt_register (punt_hdl, ipsec_punt_reason[IPSEC_PUNT_IP4_SPI_UDP_0], + "ikev2"); ikev2_cli_reference (); km->log_level = IKEV2_LOG_ERROR; @@ -3748,7 +3932,7 @@ ikev2_init (vlib_main_t * vm) /* *INDENT-OFF* */ VLIB_INIT_FUNCTION (ikev2_init) = { - .runs_after = VLIB_INITS("ipsec_init"), + .runs_after = VLIB_INITS("ipsec_init", "ipsec_punt_init"), }; /* *INDENT-ON* */ @@ -3937,7 +4121,8 @@ ikev2_process_pending_sa_init (ikev2_main_t * km) sa->last_sa_init_req_packet_data, vec_len (sa->last_sa_init_req_packet_data)); ikev2_send_ike (km->vlib_main, &sa->iaddr, &sa->raddr, bi0, - vec_len (sa->last_sa_init_req_packet_data)); + vec_len (sa->last_sa_init_req_packet_data), + ikev2_get_port (sa), IKEV2_PORT, sa->sw_if_index); })); /* *INDENT-ON* */ } @@ -3951,6 +4136,7 @@ ikev2_send_informational_request (ikev2_sa_t * sa) ip4_address_t *src, *dst; ike_header_t *ike0; u32 bi0 = 0; + u16 dp; int len; bi0 = ikev2_get_new_ike_header_buff (km->vlib_main, &ike0); @@ -3960,7 +4146,9 @@ ikev2_send_informational_request (ikev2_sa_t * sa) ike0->rspi = clib_host_to_net_u64 (sa->rspi); ike0->msgid = clib_host_to_net_u32 (sa->last_init_msg_id + 1); sa->last_init_msg_id = clib_net_to_host_u32 (ike0->msgid); - len = ikev2_generate_message (sa, ike0, 0); + len = ikev2_generate_message (sa, ike0, 0, 0); + if (sa->natt) + len = ikev2_insert_non_esp_marker (ike0, len); if (sa->is_initiator) { @@ -3969,12 +4157,13 @@ ikev2_send_informational_request (ikev2_sa_t * sa) } else { - dst = &sa->iaddr; src = &sa->raddr; } - ikev2_send_ike (km->vlib_main, src, dst, bi0, len); + dp = sa->dst_port ? sa->dst_port : ikev2_get_port (sa); + ikev2_send_ike (km->vlib_main, src, dst, bi0, len, ikev2_get_port (sa), dp, + sa->sw_if_index); } static_always_inline int @@ -4048,7 +4237,17 @@ ikev2_mngr_process_fn (vlib_main_t * vm, vlib_node_runtime_t * rt, vec_foreach (sai, to_be_deleted) { sa = pool_elt_at_index (tkm->sas, sai[0]); - if (sa->is_initiator && sa->is_profile_index_set) + u8 reinitiate = (sa->is_initiator && sa->is_profile_index_set); + vec_foreach (c, sa->childs) + { + ikev2_delete_tunnel_interface (km->vnet_main, sa, c); + ikev2_sa_del_child_sa (sa, c); + } + ikev2_sa_free_all_vec (sa); + hash_unset (tkm->sa_by_rspi, sa->rspi); + pool_put (tkm->sas, sa); + + if (reinitiate) { p = pool_elt_at_index (km->profiles, sa->profile_index); if (p) @@ -4056,10 +4255,6 @@ ikev2_mngr_process_fn (vlib_main_t * vm, vlib_node_runtime_t * rt, ikev2_initiate_sa_init (vm, p->name); } } - vec_foreach (c, sa->childs) - ikev2_delete_tunnel_interface (km->vnet_main, sa, c); - hash_unset (tkm->sa_by_rspi, sa->rspi); - pool_put (tkm->sas, sa); } vec_free (to_be_deleted); } diff --git a/src/plugins/ikev2/ikev2.h b/src/plugins/ikev2/ikev2.h index c701e301ce1..26df41d8595 100644 --- a/src/plugins/ikev2/ikev2.h +++ b/src/plugins/ikev2/ikev2.h @@ -21,7 +21,8 @@ #include <vppinfra/error.h> #define IKEV2_NONCE_SIZE 32 - +#define IKEV2_PORT 500 +#define IKEV2_PORT_NATT 4500 #define IKEV2_KEY_PAD "Key Pad for IKEv2" typedef u8 v8; @@ -91,6 +92,8 @@ typedef CLIB_PACKED (struct { #define IKEV2_PAYLOAD_FLAG_CRITICAL (1<<7) #define IKEV2_PAYLOAD_NONE 0 +#define IKEV2_PAYLOAD_NAT_D 20 +#define IKEV2_PAYLOAD_NAT_OA 21 #define IKEV2_PAYLOAD_SA 33 #define IKEV2_PAYLOAD_KE 34 #define IKEV2_PAYLOAD_IDI 35 diff --git a/src/plugins/ikev2/ikev2_cli.c b/src/plugins/ikev2/ikev2_cli.c index b88d257554c..2a5ebb3b6a2 100644 --- a/src/plugins/ikev2/ikev2_cli.c +++ b/src/plugins/ikev2/ikev2_cli.c @@ -512,8 +512,8 @@ show_ikev2_profile_command_fn (vlib_main_t * vm, if (p->udp_encap) vlib_cli_output(vm, " udp-encap"); - if (p->dst_port != IPSEC_UDP_PORT_NONE) - vlib_cli_output(vm, " ipsec-over-udp port %d", p->dst_port); + if (p->ipsec_over_udp_port != IPSEC_UDP_PORT_NONE) + vlib_cli_output(vm, " ipsec-over-udp port %d", p->ipsec_over_udp_port); })); /* *INDENT-ON* */ diff --git a/src/plugins/ikev2/ikev2_priv.h b/src/plugins/ikev2/ikev2_priv.h index a344e716a89..7f4cb03c373 100644 --- a/src/plugins/ikev2/ikev2_priv.h +++ b/src/plugins/ikev2/ikev2_priv.h @@ -358,7 +358,7 @@ typedef struct u64 lifetime_maxdata; u32 lifetime_jitter; u32 handover; - u16 dst_port; + u16 ipsec_over_udp_port; u32 tun_itf; u8 udp_encap; @@ -425,7 +425,7 @@ typedef struct u8 is_tun_itf_set; u32 tun_itf; u8 udp_encap; - u16 dst_port; + u16 ipsec_over_udp_port; f64 old_id_expiration; u32 current_remote_id_mask; @@ -437,6 +437,12 @@ typedef struct u8 liveness_retries; f64 liveness_period_check; + + u16 dst_port; + u32 sw_if_index; + + /* is NAT traversal mode */ + u8 natt; } ikev2_sa_t; |