diff options
author | Juraj Sloboda <jsloboda@cisco.com> | 2018-05-04 14:20:06 +0200 |
---|---|---|
committer | Ole Trøan <otroan@employees.org> | 2018-06-21 10:03:03 +0000 |
commit | dd3b8f7ab9f120b92c5fdf26016db47bbe18cb39 (patch) | |
tree | 6d43daf689e29638fdabb38c66c4357c1c5f491f | |
parent | 65ce94a1dcafbba684085e1136b3f12c7bdface3 (diff) |
Implement DHCPv6 IA NA client (VPP-1094)
Change-Id: I682a47d6cf9975aca6136188d28ee93eaadf4fe3
Signed-off-by: Juraj Sloboda <jsloboda@cisco.com>
-rw-r--r-- | src/vlibapi/api_helper_macros.h | 3 | ||||
-rw-r--r-- | src/vnet.am | 11 | ||||
-rw-r--r-- | src/vnet/dhcp/dhcp.api | 101 | ||||
-rw-r--r-- | src/vnet/dhcp/dhcp6_client_common_dp.c | 488 | ||||
-rw-r--r-- | src/vnet/dhcp/dhcp6_client_common_dp.h | 118 | ||||
-rw-r--r-- | src/vnet/dhcp/dhcp6_ia_na_client_cp.api | 36 | ||||
-rw-r--r-- | src/vnet/dhcp/dhcp6_ia_na_client_cp.c | 831 | ||||
-rw-r--r-- | src/vnet/dhcp/dhcp6_ia_na_client_dp.c | 656 | ||||
-rw-r--r-- | src/vnet/dhcp/dhcp6_ia_na_client_dp.h | 176 | ||||
-rw-r--r-- | src/vnet/dhcp/dhcp6_packet.h | 6 | ||||
-rw-r--r-- | src/vnet/dhcp/dhcp6_pd_client_cp.c | 7 | ||||
-rw-r--r-- | src/vnet/dhcp/dhcp6_pd_client_dp.c | 550 | ||||
-rw-r--r-- | src/vnet/dhcp/dhcp6_pd_client_dp.h | 62 | ||||
-rw-r--r-- | src/vnet/dhcp/dhcp_api.c | 8 | ||||
-rw-r--r-- | src/vnet/vnet_all_api_h.h | 1 | ||||
-rw-r--r-- | test/test_dhcp6.py | 391 | ||||
-rw-r--r-- | test/vpp_papi_provider.py | 29 |
17 files changed, 2902 insertions, 572 deletions
diff --git a/src/vlibapi/api_helper_macros.h b/src/vlibapi/api_helper_macros.h index 1245db82dd0..5746f071cdb 100644 --- a/src/vlibapi/api_helper_macros.h +++ b/src/vlibapi/api_helper_macros.h @@ -238,7 +238,8 @@ _(bfd_events) \ _(wc_ip6_nd_events) \ _(wc_ip4_arp_events) \ _(ip6_ra_events) \ -_(dhcp6_pd_reply_events) +_(dhcp6_pd_reply_events) \ +_(dhcp6_reply_events) typedef struct { diff --git a/src/vnet.am b/src/vnet.am index 2601f48ae4f..cecc2e2ff31 100644 --- a/src/vnet.am +++ b/src/vnet.am @@ -783,19 +783,26 @@ API_FILES += vnet/lisp-gpe/lisp_gpe.api libvnet_la_SOURCES += \ vnet/dhcp/client.c \ vnet/dhcp/dhcp_client_detect.c \ + vnet/dhcp/dhcp6_client_common_dp.c \ vnet/dhcp/dhcp6_pd_client_dp.c \ vnet/dhcp/dhcp6_pd_client_cp.c \ + vnet/dhcp/dhcp6_ia_na_client_dp.c \ + vnet/dhcp/dhcp6_ia_na_client_cp.c \ vnet/dhcp/dhcp_api.c nobase_include_HEADERS += \ vnet/dhcp/client.h \ + vnet/dhcp/dhcp6_client_common_dp.h \ vnet/dhcp/dhcp6_pd_client_dp.h \ + vnet/dhcp/dhcp6_ia_na_client_dp.h \ vnet/dhcp/dhcp.api.h \ - vnet/dhcp/dhcp6_pd_client_cp.api.h + vnet/dhcp/dhcp6_pd_client_cp.api.h \ + vnet/dhcp/dhcp6_ia_na_client_cp.api.h API_FILES += \ vnet/dhcp/dhcp.api \ - vnet/dhcp/dhcp6_pd_client_cp.api + vnet/dhcp/dhcp6_pd_client_cp.api \ + vnet/dhcp/dhcp6_ia_na_client_cp.api ######################################## # DHCP proxy diff --git a/src/vnet/dhcp/dhcp.api b/src/vnet/dhcp/dhcp.api index ab0f7b60980..ac1e68509e7 100644 --- a/src/vnet/dhcp/dhcp.api +++ b/src/vnet/dhcp/dhcp.api @@ -209,6 +209,18 @@ autoreply define dhcp6_clients_enable_disable u8 enable; }; +/** \brief Struct representing DHCPv6 address + @param address - address + @param valid_time - valid lifetime + @param preferred_time - preferred lifetime +*/ +typeonly define dhcp6_address_info +{ + u8 address[16]; + u32 valid_time; + u32 preferred_time; +}; + /** \brief Struct representing DHCPv6 PD prefix @param prefix - prefix @param prefix_length - prefix length @@ -223,6 +235,45 @@ typeonly define dhcp6_pd_prefix_info u32 preferred_time; }; +/** \brief Send DHCPv6 client message of specified type + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param sw_if_index - index of TX interface, also identifies IAID + @param server_index - used to dentify DHCPv6 server, + unique for each DHCPv6 server on the link, + value obrtained from dhcp6_reply_event API message, + use ~0 to send message to all DHCPv6 servers + @param irt - initial retransmission time + @param mrt - maximum retransmission time + @param mrc - maximum retransmission count + @param mrd - maximum retransmission duration + for sending the message + @param stop - if non-zero then stop resending the message, + otherwise start sending the message + @param msg_type - message type + @param T1 - value of T1 in IA_NA option + @param T2 - value of T2 in IA_NA option + @param n_addresses - number of addresses in IA_NA option + @param addresses - list of addresses in IA_NA option +*/ +autoreply define dhcp6_send_client_message +{ + u32 client_index; + u32 context; + u32 sw_if_index; + u32 server_index; + u32 irt; + u32 mrt; + u32 mrc; + u32 mrd; + u8 stop; + u8 msg_type; + u32 T1; + u32 T2; + u32 n_addresses; + vl_api_dhcp6_address_info_t addresses[n_addresses]; +}; + /** \brief Send DHCPv6 PD client message of specified type @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request @@ -263,10 +314,29 @@ autoreply define dhcp6_pd_send_client_message }; service { + rpc want_dhcp6_reply_events returns want_dhcp6_reply_events_reply + events dhcp6_reply_event; +}; + +service { rpc want_dhcp6_pd_reply_events returns want_dhcp6_pd_reply_events_reply events dhcp6_pd_reply_event; }; +/** \brief Register for DHCPv6 reply events + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param enable_disable - 1 => register for events, 0 => cancel registration + @param pid - sender's pid +*/ +autoreply define want_dhcp6_reply_events +{ + u32 client_index; + u32 context; + u8 enable_disable; + u32 pid; +}; + /** \brief Register for DHCPv6 PD reply events @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request @@ -281,6 +351,37 @@ autoreply define want_dhcp6_pd_reply_events u32 pid; }; +/** \brief Tell client about a DHCPv6 server reply event + @param client_index - opaque cookie to identify the sender + @param pid - client pid registered to receive notification + @param sw_if_index - index of RX interface, also identifies IAID + @param server_index - used to dentify DHCPv6 server, + unique for each DHCPv6 server on the link + @param msg_type - message type + @param T1 - value of T1 in IA_NA option + @param T2 - value of T2 in IA_NA option + @param inner_status_code - value of status code inside IA_NA option + @param status_code - value of status code + @param preference - value of preference option in reply message + @param n_addresses - number of addresses in IA_NA option + @param addresses - list of addresses in IA_NA option +*/ +define dhcp6_reply_event +{ + u32 client_index; + u32 pid; + u32 sw_if_index; + u32 server_index; + u8 msg_type; + u32 T1; + u32 T2; + u16 inner_status_code; + u16 status_code; + u8 preference; + u32 n_addresses; + vl_api_dhcp6_address_info_t addresses[n_addresses]; +}; + /** \brief Tell client about a DHCPv6 PD server reply event @param client_index - opaque cookie to identify the sender @param pid - client pid registered to receive notification diff --git a/src/vnet/dhcp/dhcp6_client_common_dp.c b/src/vnet/dhcp/dhcp6_client_common_dp.c new file mode 100644 index 00000000000..216bf5fa0e4 --- /dev/null +++ b/src/vnet/dhcp/dhcp6_client_common_dp.c @@ -0,0 +1,488 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vnet/dhcp/dhcp6_client_common_dp.h> +#include <vnet/dhcp/dhcp6_ia_na_client_dp.h> +#include <vnet/dhcp/dhcp6_pd_client_dp.h> +#include <vnet/dhcp/dhcp6_packet.h> +#include <vnet/udp/udp.h> + +dhcp6_client_common_main_t dhcp6_client_common_main; +dhcpv6_duid_ll_string_t client_duid; + +u32 +server_index_get_or_create (u8 * data, u16 len) +{ + dhcp6_client_common_main_t *ccm = &dhcp6_client_common_main; + u32 i; + server_id_t *se; + server_id_t new_se; + + for (i = 0; i < vec_len (ccm->server_ids); i++) + { + se = &ccm->server_ids[i]; + if (se->len == len && 0 == memcmp (se->data, data, len)) + return i; + } + + new_se.len = len; + new_se.data = 0; + vec_validate (new_se.data, len - 1); + memcpy (new_se.data, data, len); + + vec_add1 (ccm->server_ids, new_se); + + return vec_len (ccm->server_ids) - 1; +} + +void +vl_api_dhcp6_duid_ll_set_t_handler (vl_api_dhcp6_duid_ll_set_t * mp) +{ + vl_api_dhcp6_duid_ll_set_reply_t *rmp; + dhcpv6_duid_ll_string_t *duid; + int rv = 0; + + duid = (dhcpv6_duid_ll_string_t *) mp->duid_ll; + if (duid->duid_type != htonl (DHCPV6_DUID_LL)) + { + rv = VNET_API_ERROR_INVALID_VALUE; + goto reply; + } + clib_memcpy (&client_duid, &duid, sizeof (client_duid)); + +reply: + REPLY_MACRO (VL_API_DHCP6_DUID_LL_SET_REPLY); +} + +static void +generate_client_duid (void) +{ + client_duid.duid_type = htons (DHCPV6_DUID_LL); + client_duid.hardware_type = htons (1); + + vnet_main_t *vnm = vnet_get_main (); + vnet_interface_main_t *im = &vnm->interface_main; + vnet_hw_interface_t *hi; + ethernet_interface_t *eth_if = 0; + + /* *INDENT-OFF* */ + pool_foreach (hi, im->hw_interfaces, + ({ + eth_if = ethernet_get_interface (ðernet_main, hi->hw_if_index); + if (eth_if) + break; + })); + /* *INDENT-ON* */ + + if (eth_if) + clib_memcpy (client_duid.lla, eth_if->address, 6); + else + { + clib_warning ("Failed to find any Ethernet interface, " + "setting DHCPv6 DUID link-layer address to random value"); + u32 seed = random_default_seed (); + random_u32 (&seed); + client_duid.lla[0] = 0xc2; /* locally administered unicast */ + client_duid.lla[1] = 0x18; + client_duid.lla[2] = 0x44; + client_duid.lla[3] = random_u32 (&seed); + client_duid.lla[4] = random_u32 (&seed); + client_duid.lla[5] = random_u32 (&seed); + } +} + +#define foreach_dhcpv6_client \ + _(DROP, "error-drop") \ + _(LOOKUP, "ip6-lookup") + +typedef enum +{ +#define _(sym,str) DHCPV6_CLIENT_NEXT_##sym, + foreach_dhcpv6_client +#undef _ + DHCPV6_CLIENT_N_NEXT, +} dhcpv6_client_next_t; + +/** + * per-packet trace data + */ +typedef struct dhcpv6_client_trace_t_ +{ +} dhcpv6_client_trace_t; + +static u8 * +format_dhcpv6_client_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + //dhcpv6_client_trace_t *t = va_arg (*args, dhcpv6_client_trace_t *); + + s = format (s, "nothing"); + + return s; +} + +static uword +dhcpv6_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + dhcp6_ia_na_client_main_t *icm = &dhcp6_ia_na_client_main; + dhcp6_pd_client_main_t *pcm = &dhcp6_pd_client_main; + + dhcpv6_client_next_t next_index; + u32 n_left_from, *from, *to_next; + next_index = 0; + n_left_from = frame->n_vectors; + from = vlib_frame_vector_args (frame); + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + while (n_left_from > 0 && n_left_to_next > 0) + { + ip6_header_t *ip0; + u32 options_length; + dhcpv6_header_t *dhcpv60; + dhcpv6_option_t *option; + vlib_buffer_t *b0; + dhcp6_report_common_t report; + dhcp6_address_info_t *addresses = 0; + dhcp6_prefix_info_t *prefixes = 0; + u32 next0 = DHCPV6_CLIENT_NEXT_DROP; + u32 bi0; + u32 xid; + u32 sw_if_index; + u32 iaid; + u8 client_id_present = 0; + u8 discard = 0; + u8 is_pd_packet = 0; + + dhcp6_ia_na_client_state_t *ia_na_client_state = NULL; + dhcp6_pd_client_state_t *pd_client_state = NULL; + + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer (vm, bi0); + + dhcpv60 = vlib_buffer_get_current (b0); + ip0 = (void *) (b0->data + vnet_buffer (b0)->l3_hdr_offset); + u32 dhcpv6_ip6_palyoad_offset = + (u8 *) dhcpv60 - ((u8 *) ip0 + sizeof (*ip0)); + options_length = + ntohs (ip0->payload_length) - dhcpv6_ip6_palyoad_offset - + sizeof (*dhcpv60); + + memset (&report, 0, sizeof (report)); + + sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX]; + if (sw_if_index >= vec_len (icm->client_state_by_sw_if_index)) + ia_na_client_state = 0; + else + ia_na_client_state = + &icm->client_state_by_sw_if_index[sw_if_index]; + if (sw_if_index >= vec_len (pcm->client_state_by_sw_if_index)) + pd_client_state = 0; + else + pd_client_state = &pcm->client_state_by_sw_if_index[sw_if_index]; + + xid = + (dhcpv60->xid[0] << 16) + (dhcpv60->xid[1] << 8) + + dhcpv60->xid[2]; + if (ia_na_client_state && ia_na_client_state->transaction_id == xid) + is_pd_packet = 0; + else if (pd_client_state && pd_client_state->transaction_id == xid) + is_pd_packet = 1; + else + { + clib_warning + ("Received DHCPv6 message with wrong Transaction ID"); + discard = 1; + } + + report.sw_if_index = sw_if_index; + report.msg_type = dhcpv60->msg_type; + report.server_index = ~0; + + switch (dhcpv60->msg_type) + { + case DHCPV6_MSG_ADVERTISE: + case DHCPV6_MSG_REPLY: + option = (dhcpv6_option_t *) (dhcpv60 + 1); + while (options_length > 0) + { + if (options_length < + ntohs (option->length) + sizeof (*option)) + { + clib_warning + ("remaining payload length < option length (%d < %d)", + options_length, + ntohs (option->length) + sizeof (*option)); + break; + } + u16 oo = ntohs (option->option); + if (oo == DHCPV6_OPTION_IA_NA || oo == DHCPV6_OPTION_IA_PD) + { + u8 discard_option = 0; + dhcpv6_ia_header_t *ia_header = (void *) option; + iaid = ntohl (ia_header->iaid); + u32 T1 = ntohl (ia_header->t1); + u32 T2 = ntohl (ia_header->t2); + if (iaid != DHCPV6_CLIENT_IAID) + discard_option = 1; + if (T1 != 0 && T2 != 0 && T1 > T2) + discard_option = 1; + if (!discard_option) + { + report.T1 = T1; + report.T2 = T2; + } + dhcpv6_option_t *inner_option = + (void *) ia_header->data; + u16 inner_options_length = + ntohs (option->length) - (sizeof (*ia_header) - + sizeof (dhcpv6_option_t)); + while (inner_options_length > 0) + { + u16 inner_oo = ntohs (inner_option->option); + if (discard_option) + ; + else if (inner_oo == DHCPV6_OPTION_IAADDR) + { + dhcpv6_ia_opt_addr_t *iaaddr = + (void *) inner_option; + u32 n_addresses = vec_len (addresses); + vec_validate (addresses, n_addresses); + dhcp6_address_info_t *address_info = + &addresses[n_addresses]; + address_info->preferred_time = + ntohl (iaaddr->preferred); + address_info->valid_time = + ntohl (iaaddr->valid); + address_info->address = iaaddr->addr; + } + else if (inner_oo == DHCPV6_OPTION_IAPREFIX) + { + dhcpv6_ia_opt_pd_t *iaprefix = + (void *) inner_option; + u32 n_prefixes = vec_len (prefixes); + vec_validate (prefixes, n_prefixes); + dhcp6_prefix_info_t *prefix_info = + &prefixes[n_prefixes]; + prefix_info->preferred_time = + ntohl (iaprefix->preferred); + prefix_info->valid_time = + ntohl (iaprefix->valid); + prefix_info->prefix_length = iaprefix->prefix; + prefix_info->prefix = iaprefix->addr; + } + else if (inner_oo == DHCPV6_OPTION_STATUS_CODE) + { + dhcpv6_status_code_t *sc = + (void *) inner_option; + report.inner_status_code = + ntohs (sc->status_code); + } + inner_options_length -= + sizeof (*inner_option) + + ntohs (inner_option->length); + inner_option = + (void *) ((u8 *) inner_option + + sizeof (*inner_option) + + ntohs (inner_option->length)); + } + } + else if (oo == DHCPV6_OPTION_CLIENTID) + { + if (client_id_present) + { + clib_warning + ("Duplicate Client ID in received DHVPv6 message"); + discard = 1; + } + else + { + u16 len = ntohs (option->length); + client_id_present = 1; + if (len != CLIENT_DUID_LENGTH || + 0 != memcmp (option->data, + client_duid.bin_string, + CLIENT_DUID_LENGTH)) + { + clib_warning + ("Unrecognized client DUID inside received DHVPv6 message"); + discard = 1; + } + } + } + else if (oo == DHCPV6_OPTION_SERVERID) + { + if (report.server_index != ~0) + { + clib_warning + ("Duplicate Server ID in received DHVPv6 message"); + discard = 1; + } + else + { + u16 ol = ntohs (option->length); + if (ol - 2 /* 2 byte DUID type code */ > 128) + { + clib_warning + ("Server DUID (without type code) is longer than 128 octets"); + discard = 1; + } + else + { + report.server_index = + server_index_get_or_create (option->data, ol); + } + } + } + else if (oo == DHCPV6_OPTION_PREFERENCE) + { + report.preference = option->data[0]; + } + else if (oo == DHCPV6_OPTION_STATUS_CODE) + { + dhcpv6_status_code_t *sc = (void *) option; + report.status_code = ntohs (sc->status_code); + } + options_length -= sizeof (*option) + ntohs (option->length); + option = + (void *) ((u8 *) option + sizeof (*option) + + ntohs (option->length)); + } + + if (!client_id_present) + { + clib_warning + ("Missing Client ID in received DHVPv6 message"); + discard = 1; + } + if (report.server_index == ~0) + { + clib_warning + ("Missing Server ID in received DHVPv6 message"); + discard = 1; + } + + if (!discard) + { + if (!is_pd_packet) + { + address_report_t r; + r.body = report; + r.n_addresses = vec_len (addresses); + r.addresses = addresses; + dhcp6_publish_report (&r); + } + else + { + prefix_report_t r; + r.body = report; + r.n_prefixes = vec_len (prefixes); + r.prefixes = prefixes; + dhcp6_pd_publish_report (&r); + } + } + vec_free (addresses); + vec_free (prefixes); + + break; + default: + break; + } + + if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) + { + dhcpv6_client_trace_t *t = + vlib_add_trace (vm, node, b0, sizeof (*t)); + } + + /* verify speculative enqueue, maybe switch current next frame */ + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + return frame->n_vectors; +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (dhcpv6_client_node, static) = { + .function = dhcpv6_client_node_fn, + .name = "dhcpv6-client", + .vector_size = sizeof (u32), + + .n_errors = 0, + + .n_next_nodes = DHCPV6_CLIENT_N_NEXT, + .next_nodes = { + #define _(s,n) [DHCPV6_CLIENT_NEXT_##s] = n, + foreach_dhcpv6_client + #undef _ + }, + + .format_trace = format_dhcpv6_client_trace, +}; +/* *INDENT-ON* */ + +void +dhcp6_clients_enable_disable (u8 enable) +{ + vlib_main_t *vm = vlib_get_main (); + + if (enable) + { + if (client_duid.duid_type == 0) + generate_client_duid (); + udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client, + dhcpv6_client_node.index, 0 /* is_ip6 */ ); + } + else + udp_unregister_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client, + 0 /* is_ip6 */ ); +} + +void + vl_api_dhcp6_clients_enable_disable_t_handler + (vl_api_dhcp6_clients_enable_disable_t * mp) +{ + vl_api_dhcp6_clients_enable_disable_reply_t *rmp; + int rv = 0; + + dhcp6_clients_enable_disable (mp->enable); + + REPLY_MACRO (VL_API_DHCP6_CLIENTS_ENABLE_DISABLE_REPLY); +} + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/dhcp/dhcp6_client_common_dp.h b/src/vnet/dhcp/dhcp6_client_common_dp.h new file mode 100644 index 00000000000..0acef8408c6 --- /dev/null +++ b/src/vnet/dhcp/dhcp6_client_common_dp.h @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef included_vnet_dhcp6_client_common_dp_h +#define included_vnet_dhcp6_client_common_dp_h + +#include <vlib/vlib.h> +#include <vnet/dhcp/dhcp6_client_common_dp.h> +#include <vnet/dhcp/dhcp6_packet.h> +#include <vnet/vnet_msg_enum.h> +#include <vlibapi/api_common.h> +#include <vlibmemory/api.h> + +#define vl_typedefs /* define message structures */ +#include <vnet/vnet_all_api_h.h> +#undef vl_typedefs + +#define vl_endianfun /* define message structures */ +#include <vnet/vnet_all_api_h.h> +#undef vl_endianfun + +/* instantiate all the print functions we know about */ +#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) +#define vl_printfun +#include <vnet/vnet_all_api_h.h> +#undef vl_printfun + +#include <vlibapi/api_helper_macros.h> + +typedef struct +{ + u32 sw_if_index; + u32 server_index; + u8 msg_type; + u32 T1; + u32 T2; + u16 inner_status_code; + u16 status_code; + u8 preference; +} dhcp6_report_common_t; + +typedef struct +{ + u8 *data; + u16 len; +} server_id_t; + +typedef struct +{ + server_id_t *server_ids; +} dhcp6_client_common_main_t; + +extern dhcp6_client_common_main_t dhcp6_client_common_main; + +typedef union +{ + CLIB_PACKED (struct + { + u16 duid_type; + u16 hardware_type; + u8 lla[6]; + }); + char bin_string[10]; +} dhcpv6_duid_ll_string_t; + +extern dhcpv6_duid_ll_string_t client_duid; +#define CLIENT_DUID_LENGTH sizeof (client_duid) +#define DHCPV6_CLIENT_IAID 1 + +void dhcp6_clients_enable_disable (u8 enable); +u32 server_index_get_or_create (u8 * data, u16 len); + +#define vl_typedefs /* define message structures */ +#include <vnet/vnet_all_api_h.h> +#undef vl_typedefs + +void vl_api_dhcp6_duid_ll_set_t_handler (vl_api_dhcp6_duid_ll_set_t * mp); + +static_always_inline f64 +random_f64_from_to (f64 from, f64 to) +{ + static u32 seed = 0; + static u8 seed_set = 0; + if (!seed_set) + { + seed = random_default_seed (); + seed_set = 1; + } + return random_f64 (&seed) * (to - from) + from; +} + +static const ip6_address_t all_dhcp6_relay_agents_and_servers = { + .as_u8 = { + 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02} +}; + +#endif /* included_vnet_dhcp6_client_common_dp_h */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/dhcp/dhcp6_ia_na_client_cp.api b/src/vnet/dhcp/dhcp6_ia_na_client_cp.api new file mode 100644 index 00000000000..6dd67384aec --- /dev/null +++ b/src/vnet/dhcp/dhcp6_ia_na_client_cp.api @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +option version = "1.0.0"; + +/** \brief Enable/disable DHCPv6 client on interface + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param sw_if_index - interface to enable/disable client on + @param enable - 1 to enable, 0 to disable +*/ +autoreply define dhcp6_client_enable_disable +{ + u32 client_index; + u32 context; + u32 sw_if_index; + u8 enable; +}; + +/* + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/dhcp/dhcp6_ia_na_client_cp.c b/src/vnet/dhcp/dhcp6_ia_na_client_cp.c new file mode 100644 index 00000000000..e83eecc17c3 --- /dev/null +++ b/src/vnet/dhcp/dhcp6_ia_na_client_cp.c @@ -0,0 +1,831 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vnet/vnet.h> +#include <vlibmemory/api.h> +#include <vnet/vnet_msg_enum.h> +#include <vnet/dhcp/dhcp6_packet.h> +#include <vnet/dhcp/dhcp6_ia_na_client_dp.h> +#include <vnet/ip/ip.h> +#include <vnet/ip/ip6.h> +#include <float.h> +#include <math.h> + +#define vl_typedefs /* define message structures */ +#include <vnet/vnet_all_api_h.h> +#undef vl_typedefs + +#define vl_endianfun /* define message structures */ +#include <vnet/vnet_all_api_h.h> +#undef vl_endianfun + +#include <vlibapi/api_helper_macros.h> + +#define foreach_dhcp6_client_cp_msg \ +_(DHCP6_CLIENT_ENABLE_DISABLE, dhcp6_client_enable_disable) + +#define vl_api_dhcp6_client_enable_disable_t_print vl_noop_handler + +typedef struct +{ + u32 sw_if_index; + ip6_address_t address; + u32 preferred_lt; + u32 valid_lt; + f64 due_time; +} address_info_t; + +typedef struct +{ + u8 enabled; + u32 server_index; + u32 T1; + u32 T2; + f64 T1_due_time; + f64 T2_due_time; + u32 address_count; + u8 rebinding; +} client_state_t; + +typedef struct +{ + address_info_t *address_pool; + client_state_t *client_state_by_sw_if_index; + u32 n_clients; + f64 max_valid_due_time; + + /* convenience */ + vlib_main_t *vlib_main; + vnet_main_t *vnet_main; + api_main_t *api_main; + u32 node_index; +} dhcp6_client_cp_main_t; + +static dhcp6_client_cp_main_t dhcp6_client_cp_main; + +enum +{ + RD_CP_EVENT_INTERRUPT, + RD_CP_EVENT_DISABLE, +}; + +static void +send_client_message_start_stop (u32 sw_if_index, u32 server_index, + u8 msg_type, address_info_t * address_list, + u8 start) +{ + dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main; + dhcp6_send_client_message_params_t params = { 0, }; + dhcp6_send_client_message_params_address_t *addresses = 0, *addr; + u32 i; + + ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) && + rm->client_state_by_sw_if_index[sw_if_index].enabled); + client_state_t *client_state = + &rm->client_state_by_sw_if_index[sw_if_index]; + + params.sw_if_index = sw_if_index; + params.server_index = server_index; + params.msg_type = msg_type; + if (start) + { + if (msg_type == DHCPV6_MSG_SOLICIT) + { + params.irt = 1; + params.mrt = 120; + } + else if (msg_type == DHCPV6_MSG_REQUEST) + { + params.irt = 1; + params.mrt = 30; + params.mrc = 10; + } + else if (msg_type == DHCPV6_MSG_RENEW) + { + params.irt = 10; + params.mrt = 600; + f64 current_time = vlib_time_now (rm->vlib_main); + i32 diff_time = client_state->T2 - current_time; + if (diff_time < 0) + diff_time = 0; + params.mrd = diff_time; + } + else if (msg_type == DHCPV6_MSG_REBIND) + { + params.irt = 10; + params.mrt = 600; + f64 current_time = vlib_time_now (rm->vlib_main); + i32 diff_time = rm->max_valid_due_time - current_time; + if (diff_time < 0) + diff_time = 0; + params.mrd = diff_time; + } + else if (msg_type == DHCPV6_MSG_RELEASE) + { + params.mrc = 1; + } + } + + params.T1 = 0; + params.T2 = 0; + if (vec_len (address_list) != 0) + vec_validate (addresses, vec_len (address_list) - 1); + for (i = 0; i < vec_len (address_list); i++) + { + address_info_t *address = &address_list[i]; + addr = &addresses[i]; + addr->valid_lt = address->valid_lt; + addr->preferred_lt = address->preferred_lt; + addr->address = address->address; + } + params.addresses = addresses; + + dhcp6_send_client_message (rm->vlib_main, sw_if_index, !start, ¶ms); + + vec_free (params.addresses); +} + +static void interrupt_process (void); + +static u32 +ip6_enable (u32 sw_if_index) +{ + dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main; + clib_error_t *rv; + + rv = enable_ip6_interface (rm->vlib_main, sw_if_index); + + return rv != 0; +} + +static u8 +ip6_addresses_equal (ip6_address_t * address1, ip6_address_t * address2) +{ + if (address1->as_u64[0] != address2->as_u64[0]) + return 0; + return address1->as_u64[1] == address2->as_u64[1]; +} + +static clib_error_t * +dhcp6_reply_event_handler (vl_api_dhcp6_reply_event_t * mp) +{ + dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main; + vlib_main_t *vm = rm->vlib_main; + client_state_t *client_state; + ip6_address_t *address; + u32 sw_if_index; + u32 n_addresses; + vl_api_dhcp6_address_info_t *api_address; + u32 inner_status_code; + u32 status_code; + u32 server_index; + f64 current_time; + clib_error_t *error = 0; + u32 i; + + current_time = vlib_time_now (vm); + + sw_if_index = ntohl (mp->sw_if_index); + + if (sw_if_index >= vec_len (rm->client_state_by_sw_if_index)) + return 0; + + client_state = &rm->client_state_by_sw_if_index[sw_if_index]; + + if (!client_state->enabled) + return 0; + + server_index = ntohl (mp->server_index); + + n_addresses = ntohl (mp->n_addresses); + + inner_status_code = ntohs (mp->inner_status_code); + status_code = ntohs (mp->status_code); + + if (mp->msg_type == DHCPV6_MSG_ADVERTISE + && client_state->server_index == ~0) + { + address_info_t *address_list = 0, *address_info; + + if (inner_status_code == DHCPV6_STATUS_NOADDRS_AVAIL) + { + clib_warning + ("Advertise message arrived with NoAddrsAvail status code"); + return 0; + } + + if (n_addresses > 0) + vec_validate (address_list, n_addresses - 1); + for (i = 0; i < n_addresses; i++) + { + api_address = &mp->addresses[i]; + address = (ip6_address_t *) api_address->address; + + address_info = &address_list[i]; + address_info->address = *address; + address_info->preferred_lt = 0; + address_info->valid_lt = 0; + } + + client_state->server_index = server_index; + + send_client_message_start_stop (sw_if_index, server_index, + DHCPV6_MSG_REQUEST, address_list, 1); + vec_free (address_list); + } + + if (mp->msg_type != DHCPV6_MSG_REPLY) + return 0; + + if (!client_state->rebinding && client_state->server_index != server_index) + { + clib_warning ("Reply message arrived with Server ID different " + "from that in Request or Renew message"); + return 0; + } + + if (inner_status_code == DHCPV6_STATUS_NOADDRS_AVAIL) + { + clib_warning ("Reply message arrived with NoAddrsAvail status code"); + if (n_addresses > 0) + { + clib_warning + ("Invalid Reply message arrived: It contains NoAddrsAvail " + "status code but also contains addresses"); + return 0; + } + } + + if (status_code == DHCPV6_STATUS_UNSPEC_FAIL) + { + clib_warning ("Reply message arrived with UnspecFail status code"); + return 0; + } + + send_client_message_start_stop (sw_if_index, server_index, + mp->msg_type, 0, 0); + + for (i = 0; i < n_addresses; i++) + { + address_info_t *address_info = 0; + u32 valid_time; + u32 preferred_time; + + api_address = &mp->addresses[i]; + + address = (ip6_address_t *) api_address->address; + + if (ip6_address_is_link_local_unicast (address)) + continue; + + valid_time = ntohl (api_address->valid_time); + preferred_time = ntohl (api_address->preferred_time); + + if (preferred_time > valid_time) + continue; + + u8 address_already_present = 0; + /* *INDENT-OFF* */ + pool_foreach (address_info, rm->address_pool, + ({ + if (address_info->sw_if_index != sw_if_index) + ; + else if (!ip6_addresses_equal (&address_info->address, address)) + ; + else + { + address_already_present = 1; + goto address_pool_foreach_out; + } + })); + /* *INDENT-ON* */ + address_pool_foreach_out: + + if (address_already_present) + { + address_info->preferred_lt = preferred_time; + address_info->valid_lt = valid_time; + address_info->due_time = current_time + valid_time; + if (address_info->due_time > rm->max_valid_due_time) + rm->max_valid_due_time = address_info->due_time; + continue; + } + + if (valid_time == 0) + continue; + + pool_get (rm->address_pool, address_info); + address_info->sw_if_index = sw_if_index; + address_info->address = *address; + address_info->preferred_lt = preferred_time; + address_info->valid_lt = valid_time; + address_info->due_time = current_time + valid_time; + if (address_info->due_time > rm->max_valid_due_time) + rm->max_valid_due_time = address_info->due_time; + rm->client_state_by_sw_if_index[sw_if_index].address_count++; + + error = ip6_add_del_interface_address (vm, sw_if_index, + &address_info->address, 64, 0); + if (error) + clib_warning ("Failed to add interface address"); + } + + client_state->server_index = server_index; + client_state->T1 = ntohl (mp->T1); + client_state->T2 = ntohl (mp->T2); + if (client_state->T1 != 0) + client_state->T1_due_time = current_time + client_state->T1; + if (client_state->T2 != 0) + client_state->T2_due_time = current_time + client_state->T2; + client_state->rebinding = 0; + + interrupt_process (); + + return error; +} + +static address_info_t * +create_address_list (u32 sw_if_index) +{ + dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main; + address_info_t *address_info, *address_list = 0;; + + /* *INDENT-OFF* */ + pool_foreach (address_info, rm->address_pool, + ({ + if (address_info->sw_if_index == sw_if_index) + { + u32 pos = vec_len (address_list); + vec_validate (address_list, pos); + clib_memcpy (&address_list[pos], address_info, sizeof (*address_info)); + } + })); + /* *INDENT-ON* */ + + return address_list; +} + +VNET_DHCP6_REPLY_EVENT_FUNCTION (dhcp6_reply_event_handler); + +static uword +dhcp6_client_cp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, + vlib_frame_t * f) +{ + dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main; + address_info_t *address_info; + client_state_t *client_state; + f64 sleep_time = 1e9; + clib_error_t *error; + f64 current_time; + f64 due_time; + uword event_type; + uword *event_data = 0; + int i; + + while (1) + { + vlib_process_wait_for_event_or_clock (vm, sleep_time); + event_type = vlib_process_get_events (vm, &event_data); + vec_reset_length (event_data); + + if (event_type == RD_CP_EVENT_DISABLE) + { + vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_DISABLED); + sleep_time = 1e9; + continue; + } + + current_time = vlib_time_now (vm); + do + { + due_time = current_time + 1e9; + /* *INDENT-OFF* */ + pool_foreach (address_info, rm->address_pool, + ({ + if (address_info->due_time > current_time) + { + if (address_info->due_time < due_time) + due_time = address_info->due_time; + } + else + { + u32 sw_if_index = address_info->sw_if_index; + error = ip6_add_del_interface_address (vm, sw_if_index, + &address_info->address, + 64, 1); + if (error) + clib_warning ("Failed to delete interface address"); + pool_put (rm->address_pool, address_info); + /* make sure ip6 stays enabled */ + ip6_enable (sw_if_index); + client_state = &rm->client_state_by_sw_if_index[sw_if_index]; + if (--client_state->address_count == 0) + { + client_state->rebinding = 0; + client_state->server_index = ~0; + send_client_message_start_stop (sw_if_index, ~0, + DHCPV6_MSG_SOLICIT, + 0, 1); + } + } + })); + /* *INDENT-ON* */ + for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++) + { + client_state_t *cs = &rm->client_state_by_sw_if_index[i]; + if (cs->enabled && cs->server_index != ~0) + { + if (cs->T2_due_time > current_time) + { + if (cs->T2_due_time < due_time) + due_time = cs->T2_due_time; + if (cs->T1_due_time > current_time) + { + if (cs->T1_due_time < due_time) + due_time = cs->T1_due_time; + } + else + { + cs->T1_due_time = DBL_MAX; + address_info_t *address_list; + address_list = create_address_list (i); + cs->rebinding = 1; + send_client_message_start_stop (i, cs->server_index, + DHCPV6_MSG_RENEW, + address_list, 1); + vec_free (address_list); + } + } + else + { + cs->T2_due_time = DBL_MAX; + address_info_t *address_list; + address_list = create_address_list (i); + cs->rebinding = 1; + send_client_message_start_stop (i, ~0, + DHCPV6_MSG_REBIND, + address_list, 1); + vec_free (address_list); + } + } + } + current_time = vlib_time_now (vm); + } + while (due_time < current_time); + + sleep_time = due_time - current_time; + } + + return 0; +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (dhcp6_client_cp_process_node) = { + .function = dhcp6_client_cp_process, + .type = VLIB_NODE_TYPE_PROCESS, + .name = "dhcp6-client-cp-process", +}; +/* *INDENT-ON* */ + +static void +interrupt_process (void) +{ + dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main; + vlib_main_t *vm = rm->vlib_main; + + vlib_process_signal_event (vm, dhcp6_client_cp_process_node.index, + RD_CP_EVENT_INTERRUPT, 0); +} + +static void +disable_process (void) +{ + dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main; + vlib_main_t *vm = rm->vlib_main; + + vlib_process_signal_event (vm, dhcp6_client_cp_process_node.index, + RD_CP_EVENT_DISABLE, 0); +} + +static void +enable_process (void) +{ + dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main; + vlib_main_t *vm = rm->vlib_main; + vlib_node_t *node; + + node = vec_elt (vm->node_main.nodes, rm->node_index); + + vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_POLLING); + vlib_start_process (vm, node->runtime_index); +} + +static clib_error_t * +dhcp6_addresses_show_command_function (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + dhcp6_client_cp_main_t *dm = &dhcp6_client_cp_main; + clib_error_t *error = 0; + address_info_t *address_info; + f64 current_time = vlib_time_now (vm); + + /* *INDENT-OFF* */ + pool_foreach (address_info, dm->address_pool, + ({ + vlib_cli_output (vm, "address: %U, " + "preferred lifetime: %u, valid lifetime: %u " + "(%f remaining)", + format_ip6_address, &address_info->address, + address_info->preferred_lt, address_info->valid_lt, + address_info->due_time - current_time); + })); + /* *INDENT-ON* */ + + return error; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (dhcp6_addresses_show_command, static) = { + .path = "show dhcp6 addresses", + .short_help = "show dhcp6 addresses", + .function = dhcp6_addresses_show_command_function, +}; +/* *INDENT-ON* */ + +static clib_error_t * +dhcp6_clients_show_command_function (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main; + clib_error_t *error = 0; + client_state_t *cs; + f64 current_time = vlib_time_now (vm); + char buf1[256]; + char buf2[256]; + const char *rebinding; + u32 i; + + for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++) + { + cs = &rm->client_state_by_sw_if_index[i]; + if (cs->enabled) + { + if (cs->T1_due_time != DBL_MAX && cs->T1_due_time > current_time) + { + sprintf (buf1, "%u remaining", + (u32) round (cs->T1_due_time - current_time)); + } + else + sprintf (buf1, "timeout"); + if (cs->T2_due_time != DBL_MAX && cs->T2_due_time > current_time) + sprintf (buf2, "%u remaining", + (u32) round (cs->T2_due_time - current_time)); + else + sprintf (buf2, "timeout"); + if (cs->rebinding) + rebinding = ", REBINDING"; + else + rebinding = ""; + if (cs->T1) + vlib_cli_output (vm, + "sw_if_index: %u, T1: %u (%s), " + "T2: %u (%s), server index: %d%s", i, + cs->T1, buf1, cs->T2, buf2, + cs->server_index, rebinding); + else + vlib_cli_output (vm, "sw_if_index: %u%s", i, rebinding); + } + } + + return error; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (dhcp6_clients_show_command, static) = { + .path = "show dhcp6 clients", + .short_help = "show dhcp6 clients", + .function = dhcp6_clients_show_command_function, +}; +/* *INDENT-ON* */ + +static int +dhcp6_client_enable_disable (u32 sw_if_index, u8 enable) +{ + dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main; + vnet_main_t *vnm = rm->vnet_main; + vlib_main_t *vm = rm->vlib_main; + client_state_t *client_state; + client_state_t empty_config = { 0 }; + address_info_t *address_info; + clib_error_t *error; + + if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index)) + { + clib_warning ("Invalid sw_if_index"); + return 1; + } + + vec_validate_init_empty (rm->client_state_by_sw_if_index, sw_if_index, + empty_config); + client_state = &rm->client_state_by_sw_if_index[sw_if_index]; + + u8 old_enabled = client_state->enabled; + if (enable) + client_state->enabled = 1; + client_state->server_index = ~0; + + if (!old_enabled && enable) + { + rm->n_clients++; + if (rm->n_clients == 1) + { + enable_process (); + dhcp6_clients_enable_disable (1); + } + + ip6_enable (sw_if_index); + send_client_message_start_stop (sw_if_index, ~0, DHCPV6_MSG_SOLICIT, + 0, 1); + } + else if (old_enabled && !enable) + { + send_client_message_start_stop (sw_if_index, ~0, ~0, 0, 0); + + rm->n_clients--; + if (rm->n_clients == 0) + { + dhcp6_clients_enable_disable (0); + disable_process (); + } + + /* *INDENT-OFF* */ + pool_foreach (address_info, rm->address_pool, + ({ + if (address_info->sw_if_index == sw_if_index) + { + ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) && + rm->client_state_by_sw_if_index[sw_if_index].enabled); + client_state_t *client_state = + &rm->client_state_by_sw_if_index[sw_if_index]; + send_client_message_start_stop (sw_if_index, + client_state->server_index, + DHCPV6_MSG_RELEASE, address_info, + 1); + error = ip6_add_del_interface_address (vm, sw_if_index, + &address_info->address, + 64, 1); + if (error) + clib_warning ("Failed to delete interface address"); + pool_put (rm->address_pool, address_info); + } + })); + /* *INDENT-ON* */ + } + + if (!enable) + client_state->enabled = 0; + + return 0; +} + +static clib_error_t * +dhcp6_client_enable_disable_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main; + vnet_main_t *vnm = rm->vnet_main; + clib_error_t *error = 0; + u32 sw_if_index = ~0; + u8 enable = 1; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat + (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) + ; + else if (unformat (input, "disable")) + enable = 0; + else + { + error = clib_error_return (0, "unexpected input `%U'", + format_unformat_error, input); + goto done; + } + } + + if (sw_if_index != ~0) + { + if (dhcp6_client_enable_disable (sw_if_index, enable) != 0) + error = clib_error_return (0, "Invalid sw_if_index"); + } + else + error = clib_error_return (0, "Missing sw_if_index"); + +done: + return error; +} + +/*? + * This command is used to enable/disable DHCPv6 client + * on particular interface. + * + * @cliexpar + * @parblock + * Example of how to enable DHCPv6 client: + * @cliexcmd{dhcp6 client GigabitEthernet2/0/0} + * Example of how to disable DHCPv6 client: + * @cliexcmd{dhcp6 client GigabitEthernet2/0/0 disable} + * @endparblock +?*/ +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (dhcp6_client_enable_disable_command, static) = { + .path = "dhcp6 client", + .short_help = "dhcp6 client <interface> [disable]", + .function = dhcp6_client_enable_disable_command_fn, +}; +/* *INDENT-ON* */ + +static void + vl_api_dhcp6_client_enable_disable_t_handler + (vl_api_dhcp6_client_enable_disable_t * mp) +{ + vl_api_dhcp6_client_enable_disable_reply_t *rmp; + u32 sw_if_index; + int rv = 0; + + VALIDATE_SW_IF_INDEX (mp); + + sw_if_index = ntohl (mp->sw_if_index); + + rv = dhcp6_client_enable_disable (sw_if_index, mp->enable); + + BAD_SW_IF_INDEX_LABEL; + + REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY); +} + +#define vl_msg_name_crc_list +#include <vnet/dhcp/dhcp6_ia_na_client_cp.api.h> +#undef vl_msg_name_crc_list + +static void +setup_message_id_table (api_main_t * am) +{ +#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); + foreach_vl_msg_name_crc_dhcp6_ia_na_client_cp; +#undef _ +} + +static clib_error_t * +dhcp_client_cp_init (vlib_main_t * vm) +{ + dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main; + api_main_t *am = &api_main; + + rm->vlib_main = vm; + rm->vnet_main = vnet_get_main (); + rm->api_main = am; + rm->node_index = dhcp6_client_cp_process_node.index; + +#define _(N,n) \ + vl_msg_api_set_handlers(VL_API_##N, #n, \ + vl_api_##n##_t_handler, \ + vl_noop_handler, \ + vl_api_##n##_t_endian, \ + vl_api_##n##_t_print, \ + sizeof(vl_api_##n##_t), 0/* do NOT trace! */); + foreach_dhcp6_client_cp_msg; +#undef _ + + /* + * Set up the (msg_name, crc, message-id) table + */ + setup_message_id_table (am); + + return 0; +} + +VLIB_INIT_FUNCTION (dhcp_client_cp_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/dhcp/dhcp6_ia_na_client_dp.c b/src/vnet/dhcp/dhcp6_ia_na_client_dp.c new file mode 100644 index 00000000000..d5bd648502e --- /dev/null +++ b/src/vnet/dhcp/dhcp6_ia_na_client_dp.c @@ -0,0 +1,656 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vlib/vlib.h> +#include <vnet/dhcp/dhcp6_packet.h> +#include <vnet/dhcp/dhcp_proxy.h> +#include <vnet/mfib/mfib_table.h> +#include <vnet/mfib/ip6_mfib.h> +#include <vnet/fib/fib.h> +#include <vnet/adj/adj_mcast.h> +#include <vnet/ip/ip6_neighbor.h> +#include <vlibapi/api_common.h> +#include <vlibmemory/api.h> +#include <vnet/dhcp/dhcp6_ia_na_client_dp.h> +#include <vnet/dhcp/dhcp6_client_common_dp.h> + +#include <vnet/vnet_msg_enum.h> + +#define vl_typedefs /* define message structures */ +#include <vnet/vnet_all_api_h.h> +#undef vl_typedefs + +#define vl_endianfun /* define message structures */ +#include <vnet/vnet_all_api_h.h> +#undef vl_endianfun + +/* instantiate all the print functions we know about */ +#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) +#define vl_printfun +#include <vnet/vnet_all_api_h.h> +#undef vl_printfun + +#include <vlibapi/api_helper_macros.h> + +dhcp6_ia_na_client_main_t dhcp6_ia_na_client_main; +dhcp6_ia_na_client_public_main_t dhcp6_ia_na_client_public_main; + +static void +signal_report (address_report_t * r) +{ + vlib_main_t *vm = vlib_get_main (); + dhcp6_ia_na_client_main_t *cm = &dhcp6_ia_na_client_main; + uword ni = cm->publisher_node; + uword et = cm->publisher_et; + + if (ni == (uword) ~ 0) + return; + address_report_t *q = + vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q); + + *q = *r; +} + +int +dhcp6_publish_report (address_report_t * r) +{ + void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length); + vl_api_rpc_call_main_thread (signal_report, (u8 *) r, sizeof *r); + return 0; +} + +void +dhcp6_set_publisher_node (uword node_index, uword event_type) +{ + dhcp6_ia_na_client_main_t *cm = &dhcp6_ia_na_client_main; + cm->publisher_node = node_index; + cm->publisher_et = event_type; +} + +static void +stop_sending_client_message (vlib_main_t * vm, + dhcp6_ia_na_client_state_t * client_state) +{ + u32 bi0; + + client_state->keep_sending_client_message = 0; + vec_free (client_state->params.addresses); + if (client_state->buffer) + { + bi0 = vlib_get_buffer_index (vm, client_state->buffer); + vlib_buffer_free (vm, &bi0, 1); + client_state->buffer = 0; + adj_unlock (client_state->adj_index); + client_state->adj_index = ~0; + } +} + +static vlib_buffer_t * +create_buffer_for_client_message (vlib_main_t * vm, u32 sw_if_index, + dhcp6_ia_na_client_state_t * client_state, + u32 type) +{ + dhcp6_client_common_main_t *ccm = &dhcp6_client_common_main; + vnet_main_t *vnm = vnet_get_main (); + + vlib_buffer_t *b; + u32 bi; + ip6_header_t *ip; + udp_header_t *udp; + dhcpv6_header_t *dhcp; + ip6_address_t src_addr; + u32 dhcp_opt_len = 0; + client_state->transaction_start = vlib_time_now (vm); + u32 n_addresses; + u32 i; + + vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); + vnet_sw_interface_t *sup_sw = vnet_get_sup_sw_interface (vnm, sw_if_index); + vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, sw_if_index); + + /* Interface(s) down? */ + if ((hw->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) == 0) + return NULL; + if ((sup_sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0) + return NULL; + if ((sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0) + return NULL; + + /* Get a link-local address */ + src_addr = ip6_neighbor_get_link_local_address (sw_if_index); + + if (src_addr.as_u8[0] != 0xfe) + { + clib_warning ("Could not find source address to send DHCPv6 packet"); + return NULL; + } + + if (vlib_buffer_alloc (vm, &bi, 1) != 1) + { + clib_warning ("Buffer allocation failed"); + return NULL; + } + + b = vlib_get_buffer (vm, bi); + vnet_buffer (b)->sw_if_index[VLIB_RX] = sw_if_index; + vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index; + client_state->adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6, + VNET_LINK_IP6, + sw_if_index); + vnet_buffer (b)->ip.adj_index[VLIB_TX] = client_state->adj_index; + b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; + + ip = (ip6_header_t *) vlib_buffer_get_current (b); + udp = (udp_header_t *) (ip + 1); + dhcp = (dhcpv6_header_t *) (udp + 1); + + ip->src_address = src_addr; + ip->hop_limit = 255; + ip->ip_version_traffic_class_and_flow_label = + clib_host_to_net_u32 (0x6 << 28); + ip->payload_length = 0; + ip->protocol = IP_PROTOCOL_UDP; + + udp->src_port = clib_host_to_net_u16 (DHCPV6_CLIENT_PORT); + udp->dst_port = clib_host_to_net_u16 (DHCPV6_SERVER_PORT); + udp->checksum = 0; + udp->length = 0; + + dhcp->msg_type = type; + dhcp->xid[0] = (client_state->transaction_id & 0x00ff0000) >> 16; + dhcp->xid[1] = (client_state->transaction_id & 0x0000ff00) >> 8; + dhcp->xid[2] = (client_state->transaction_id & 0x000000ff) >> 0; + + void *d = (void *) dhcp->data; + dhcpv6_option_t *duid; + dhcpv6_elapsed_t *elapsed; + dhcpv6_ia_header_t *ia_hdr; + dhcpv6_ia_opt_addr_t *opt_addr; + if (type == DHCPV6_MSG_SOLICIT || type == DHCPV6_MSG_REQUEST || + type == DHCPV6_MSG_RENEW || type == DHCPV6_MSG_REBIND || + type == DHCPV6_MSG_RELEASE) + { + duid = (dhcpv6_option_t *) d; + duid->option = clib_host_to_net_u16 (DHCPV6_OPTION_CLIENTID); + duid->length = clib_host_to_net_u16 (CLIENT_DUID_LENGTH); + clib_memcpy (duid + 1, client_duid.bin_string, CLIENT_DUID_LENGTH); + d += sizeof (*duid) + CLIENT_DUID_LENGTH; + + if (client_state->params.server_index != ~0) + { + server_id_t *se = + &ccm->server_ids[client_state->params.server_index]; + + duid = (dhcpv6_option_t *) d; + duid->option = clib_host_to_net_u16 (DHCPV6_OPTION_SERVERID); + duid->length = clib_host_to_net_u16 (se->len); + clib_memcpy (duid + 1, se->data, se->len); + d += sizeof (*duid) + se->len; + } + + elapsed = (dhcpv6_elapsed_t *) d; + elapsed->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_ELAPSED_TIME); + elapsed->opt.length = + clib_host_to_net_u16 (sizeof (*elapsed) - sizeof (elapsed->opt)); + elapsed->elapsed_10ms = 0; + client_state->elapsed_pos = + (char *) &elapsed->elapsed_10ms - + (char *) vlib_buffer_get_current (b); + d += sizeof (*elapsed); + + ia_hdr = (dhcpv6_ia_header_t *) d; + ia_hdr->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_IA_NA); + ia_hdr->iaid = clib_host_to_net_u32 (DHCPV6_CLIENT_IAID); + ia_hdr->t1 = clib_host_to_net_u32 (client_state->params.T1); + ia_hdr->t2 = clib_host_to_net_u32 (client_state->params.T2); + d += sizeof (*ia_hdr); + + n_addresses = vec_len (client_state->params.addresses); + + ia_hdr->opt.length = + clib_host_to_net_u16 (sizeof (*ia_hdr) + + n_addresses * sizeof (*opt_addr) - + sizeof (ia_hdr->opt)); + + for (i = 0; i < n_addresses; i++) + { + dhcp6_send_client_message_params_address_t *addr = + &client_state->params.addresses[i]; + opt_addr = (dhcpv6_ia_opt_addr_t *) d; + opt_addr->opt.option = clib_host_to_net_u16 (DHCPV6_OPTION_IAADDR); + opt_addr->opt.length = + clib_host_to_net_u16 (sizeof (*opt_addr) - + sizeof (opt_addr->opt)); + opt_addr->addr = addr->address; + opt_addr->valid = clib_host_to_net_u32 (addr->valid_lt); + opt_addr->preferred = clib_host_to_net_u32 (addr->preferred_lt); + d += sizeof (*opt_addr); + } + } + else + { + clib_warning ("State not implemented"); + } + + dhcp_opt_len = ((u8 *) d) - dhcp->data; + udp->length = + clib_host_to_net_u16 (sizeof (*udp) + sizeof (*dhcp) + dhcp_opt_len); + ip->payload_length = udp->length; + b->current_length = + sizeof (*ip) + sizeof (*udp) + sizeof (*dhcp) + dhcp_opt_len; + + ip->dst_address = all_dhcp6_relay_agents_and_servers; + + return b; +} + +static inline u8 +check_send_client_message (vlib_main_t * vm, + dhcp6_ia_na_client_state_t * client_state, + f64 current_time, f64 * due_time) +{ + vlib_buffer_t *p0; + vlib_frame_t *f; + u32 *to_next; + u32 next_index; + vlib_buffer_t *c0; + ip6_header_t *ip; + udp_header_t *udp; + u32 ci0; + int bogus_length = 0; + + dhcp6_send_client_message_params_t *params; + + f64 now = vlib_time_now (vm); + + if (!client_state->keep_sending_client_message) + return false; + + params = &client_state->params; + + if (client_state->due_time > current_time) + { + *due_time = client_state->due_time; + return true; + } + + p0 = client_state->buffer; + + next_index = ip6_rewrite_mcast_node.index; + + c0 = vlib_buffer_copy (vm, p0); + ci0 = vlib_get_buffer_index (vm, c0); + + ip = (ip6_header_t *) vlib_buffer_get_current (c0); + udp = (udp_header_t *) (ip + 1); + + u16 *elapsed_field = (u16 *) ((void *) ip + client_state->elapsed_pos); + *elapsed_field = + clib_host_to_net_u16 ((u16) + ((now - client_state->transaction_start) * 100)); + + udp->checksum = 0; + udp->checksum = + ip6_tcp_udp_icmp_compute_checksum (vm, 0, ip, &bogus_length); + + f = vlib_get_frame_to_node (vm, next_index); + to_next = vlib_frame_vector_args (f); + to_next[0] = ci0; + f->n_vectors = 1; + vlib_put_frame_to_node (vm, next_index, f); + + if (params->mrc != 0 && --client_state->n_left == 0) + stop_sending_client_message (vm, client_state); + else + { + client_state->sleep_interval = + (2 + random_f64_from_to (-0.1, 0.1)) * client_state->sleep_interval; + if (client_state->sleep_interval > params->mrt) + client_state->sleep_interval = + (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt; + + client_state->due_time = current_time + client_state->sleep_interval; + + if (params->mrd != 0 + && current_time > client_state->start_time + params->mrd) + stop_sending_client_message (vm, client_state); + else + *due_time = client_state->due_time; + } + + return client_state->keep_sending_client_message; +} + +static uword +send_dhcp6_client_message_process (vlib_main_t * vm, + vlib_node_runtime_t * rt, + vlib_frame_t * f0) +{ + dhcp6_ia_na_client_main_t *cm = &dhcp6_ia_na_client_main; + dhcp6_ia_na_client_state_t *client_state; + uword *event_data = 0; + f64 sleep_time = 1e9; + f64 current_time; + f64 due_time; + f64 dt = 0; + int i; + + while (true) + { + vlib_process_wait_for_event_or_clock (vm, sleep_time); + vlib_process_get_events (vm, &event_data); + vec_reset_length (event_data); + + current_time = vlib_time_now (vm); + do + { + due_time = current_time + 1e9; + for (i = 0; i < vec_len (cm->client_state_by_sw_if_index); i++) + { + client_state = &cm->client_state_by_sw_if_index[i]; + if (!client_state->entry_valid) + continue; + if (check_send_client_message + (vm, client_state, current_time, &dt) && (dt < due_time)) + due_time = dt; + } + current_time = vlib_time_now (vm); + } + while (due_time < current_time); + + sleep_time = due_time - current_time; + } + + return 0; +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (send_dhcp6_client_message_process_node, static) = { + .function = send_dhcp6_client_message_process, + .type = VLIB_NODE_TYPE_PROCESS, + .name = "send-dhcp6-client-message-process", +}; +/* *INDENT-ON* */ + +void +dhcp6_send_client_message (vlib_main_t * vm, u32 sw_if_index, u8 stop, + dhcp6_send_client_message_params_t * params) +{ + dhcp6_ia_na_client_main_t *cm = &dhcp6_ia_na_client_main; + dhcp6_ia_na_client_state_t *client_state = 0; + dhcp6_ia_na_client_state_t empty_state = { 0, }; + + ASSERT (~0 != sw_if_index); + + vec_validate_init_empty (cm->client_state_by_sw_if_index, sw_if_index, + empty_state); + client_state = &cm->client_state_by_sw_if_index[sw_if_index]; + if (!client_state->entry_valid) + { + client_state->entry_valid = 1; + client_state->adj_index = ~0; + } + + stop_sending_client_message (vm, client_state); + + if (!stop) + { + client_state->keep_sending_client_message = 1; + vec_free (client_state->params.addresses); + client_state->params = *params; + client_state->params.addresses = vec_dup (params->addresses); + client_state->n_left = params->mrc; + client_state->start_time = vlib_time_now (vm); + client_state->sleep_interval = + (1 + random_f64_from_to (-0.1, 0.1)) * params->irt; + client_state->due_time = 0; /* send first packet ASAP */ + client_state->transaction_id = random_u32 (&cm->seed) & 0x00ffffff; + client_state->buffer = + create_buffer_for_client_message (vm, sw_if_index, client_state, + params->msg_type); + if (!client_state->buffer) + client_state->keep_sending_client_message = 0; + else + vlib_process_signal_event (vm, + send_dhcp6_client_message_process_node.index, + 1, 0); + } +} + +void + vl_api_dhcp6_send_client_message_t_handler + (vl_api_dhcp6_send_client_message_t * mp) +{ + vl_api_dhcp6_send_client_message_reply_t *rmp; + dhcp6_send_client_message_params_t params; + vlib_main_t *vm = vlib_get_main (); + u32 n_addresses; + u32 i; + int rv = 0; + + VALIDATE_SW_IF_INDEX (mp); + + BAD_SW_IF_INDEX_LABEL; + REPLY_MACRO (VL_API_DHCP6_SEND_CLIENT_MESSAGE_REPLY); + + if (rv != 0) + return; + + params.sw_if_index = ntohl (mp->sw_if_index); + params.server_index = ntohl (mp->server_index); + params.irt = ntohl (mp->irt); + params.mrt = ntohl (mp->mrt); + params.mrc = ntohl (mp->mrc); + params.mrd = ntohl (mp->mrd); + params.msg_type = mp->msg_type; + params.T1 = ntohl (mp->T1); + params.T2 = ntohl (mp->T2); + n_addresses = ntohl (mp->n_addresses); + params.addresses = 0; + if (n_addresses > 0) + vec_validate (params.addresses, n_addresses - 1); + for (i = 0; i < n_addresses; i++) + { + vl_api_dhcp6_address_info_t *ai = &mp->addresses[i]; + dhcp6_send_client_message_params_address_t *addr = ¶ms.addresses[i]; + addr->preferred_lt = ntohl (ai->preferred_time); + addr->valid_lt = ntohl (ai->valid_time); + memcpy (addr->address.as_u8, ai->address, 16); + } + + dhcp6_send_client_message (vm, ntohl (mp->sw_if_index), mp->stop, ¶ms); +} + +clib_error_t * +call_dhcp6_reply_event_callbacks (void *data, + _vnet_dhcp6_reply_event_function_list_elt_t + * elt) +{ + clib_error_t *error = 0; + + while (elt) + { + error = elt->fp (data); + if (error) + return error; + elt = elt->next_dhcp6_reply_event_function; + } + + return error; +} + +static uword +dhcp6_reply_process (vlib_main_t * vm, vlib_node_runtime_t * rt, + vlib_frame_t * f) +{ + /* These cross the longjmp boundry (vlib_process_wait_for_event) + * and need to be volatile - to prevent them from being optimized into + * a register - which could change during suspension */ + + while (1) + { + vlib_process_wait_for_event (vm); + uword event_type = DHCP6_DP_REPLY_REPORT; + void *event_data = vlib_process_get_event_data (vm, &event_type); + + int i; + if (event_type == DHCP6_DP_REPLY_REPORT) + { + address_report_t *events = event_data; + for (i = 0; i < vec_len (events); i++) + { + u32 event_size = + sizeof (vl_api_dhcp6_reply_event_t) + + vec_len (events[i].addresses) * + sizeof (vl_api_dhcp6_address_info_t); + vl_api_dhcp6_reply_event_t *event = clib_mem_alloc (event_size); + memset (event, 0, event_size); + + event->sw_if_index = htonl (events[i].body.sw_if_index); + event->server_index = htonl (events[i].body.server_index); + event->msg_type = events[i].body.msg_type; + event->T1 = htonl (events[i].body.T1); + event->T2 = htonl (events[i].body.T2); + event->inner_status_code = + htons (events[i].body.inner_status_code); + event->status_code = htons (events[i].body.status_code); + event->preference = events[i].body.preference; + + event->n_addresses = htonl (vec_len (events[i].addresses)); + vl_api_dhcp6_address_info_t *address = + (typeof (address)) event->addresses; + u32 j; + for (j = 0; j < vec_len (events[i].addresses); j++) + { + dhcp6_address_info_t *info = &events[i].addresses[j]; + memcpy (address->address, &info->address, 16); + address->valid_time = htonl (info->valid_time); + address->preferred_time = htonl (info->preferred_time); + address++; + } + + dhcp6_ia_na_client_public_main_t *dcpm = + &dhcp6_ia_na_client_public_main; + call_dhcp6_reply_event_callbacks (event, dcpm->functions); + + vpe_client_registration_t *reg; + /* *INDENT-OFF* */ + pool_foreach(reg, vpe_api_main.dhcp6_reply_events_registrations, + ({ + vl_api_registration_t *vl_reg; + vl_reg = + vl_api_client_index_to_registration (reg->client_index); + if (vl_reg && vl_api_can_send_msg (vl_reg)) + { + vl_api_dhcp6_reply_event_t *msg = + vl_msg_api_alloc (event_size); + clib_memcpy (msg, event, event_size); + msg->_vl_msg_id = htons (VL_API_DHCP6_REPLY_EVENT); + msg->client_index = reg->client_index; + msg->pid = reg->client_pid; + vl_api_send_msg (vl_reg, (u8 *) msg); + } + })); + /* *INDENT-ON* */ + + clib_mem_free (event); + } + } + vlib_process_put_event_data (vm, event_data); + } + + return 0; +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (dhcp6_reply_process_node) = { + .function = dhcp6_reply_process, + .type = VLIB_NODE_TYPE_PROCESS, + .name = "dhcp6-reply-publisher-process", +}; +/* *INDENT-ON* */ + +void + vl_api_want_dhcp6_reply_events_t_handler + (vl_api_want_dhcp6_reply_events_t * mp) +{ + vpe_api_main_t *am = &vpe_api_main; + vl_api_want_dhcp6_reply_events_reply_t *rmp; + int rv = 0; + + uword *p = + hash_get (am->dhcp6_reply_events_registration_hash, mp->client_index); + vpe_client_registration_t *rp; + if (p) + { + if (mp->enable_disable) + { + clib_warning ("pid %d: already enabled...", ntohl (mp->pid)); + rv = VNET_API_ERROR_INVALID_REGISTRATION; + goto reply; + } + else + { + rp = pool_elt_at_index (am->dhcp6_reply_events_registrations, p[0]); + pool_put (am->dhcp6_reply_events_registrations, rp); + hash_unset (am->dhcp6_reply_events_registration_hash, + mp->client_index); + if (pool_elts (am->dhcp6_reply_events_registrations) == 0) + dhcp6_set_publisher_node (~0, DHCP6_DP_REPORT_MAX); + goto reply; + } + } + if (mp->enable_disable == 0) + { + clib_warning ("pid %d: already disabled...", ntohl (mp->pid)); + rv = VNET_API_ERROR_INVALID_REGISTRATION; + goto reply; + } + pool_get (am->dhcp6_reply_events_registrations, rp); + rp->client_index = mp->client_index; + rp->client_pid = ntohl (mp->pid); + hash_set (am->dhcp6_reply_events_registration_hash, rp->client_index, + rp - am->dhcp6_reply_events_registrations); + dhcp6_set_publisher_node (dhcp6_reply_process_node.index, + DHCP6_DP_REPLY_REPORT); + +reply: + REPLY_MACRO (VL_API_WANT_DHCP6_REPLY_EVENTS_REPLY); +} + +static clib_error_t * +dhcp6_client_init (vlib_main_t * vm) +{ + dhcp6_ia_na_client_main_t *cm = &dhcp6_ia_na_client_main; + + cm->vlib_main = vm; + cm->vnet_main = vnet_get_main (); + + cm->publisher_node = ~0; + + cm->seed = 0xdeaccabe; + + return 0; +} + +VLIB_INIT_FUNCTION (dhcp6_client_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/dhcp/dhcp6_ia_na_client_dp.h b/src/vnet/dhcp/dhcp6_ia_na_client_dp.h new file mode 100644 index 00000000000..a866479fbaf --- /dev/null +++ b/src/vnet/dhcp/dhcp6_ia_na_client_dp.h @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef included_vnet_dhcp6_client_dp_h +#define included_vnet_dhcp6_client_dp_h + +#include <vlib/vlib.h> +#include <vnet/dhcp/dhcp6_client_common_dp.h> + +typedef struct +{ + u32 preferred_lt; + u32 valid_lt; + ip6_address_t address; +} dhcp6_send_client_message_params_address_t; + +typedef struct +{ + u32 sw_if_index; + u32 server_index; + u32 irt; + u32 mrt; + u32 mrc; + u32 mrd; + u8 msg_type; + u32 T1; + u32 T2; + dhcp6_send_client_message_params_address_t *addresses; +} dhcp6_send_client_message_params_t; + +typedef struct +{ + u8 entry_valid; + u8 keep_sending_client_message; /* when true then next fields are valid */ + dhcp6_send_client_message_params_t params; + f64 transaction_start; + f64 sleep_interval; + f64 due_time; + u32 n_left; + f64 start_time; + u32 transaction_id; + vlib_buffer_t *buffer; + u32 elapsed_pos; + u32 adj_index; +} dhcp6_ia_na_client_state_t; + +typedef struct +{ + dhcp6_ia_na_client_state_t *client_state_by_sw_if_index; + + uword publisher_node; + uword publisher_et; + + u32 seed; + + /* convenience */ + vlib_main_t *vlib_main; + vnet_main_t *vnet_main; +} dhcp6_ia_na_client_main_t; + +extern dhcp6_ia_na_client_main_t dhcp6_ia_na_client_main; + +typedef struct +{ + ip6_address_t address; + u32 valid_time; + u32 preferred_time; + u16 status_code; +} dhcp6_address_info_t; + +typedef struct +{ + dhcp6_report_common_t body; + u32 n_addresses; + dhcp6_address_info_t *addresses; +} address_report_t; + +#define vl_typedefs /* define message structures */ +#include <vnet/vnet_all_api_h.h> +#undef vl_typedefs + +void dhcp6_send_client_message (vlib_main_t * vm, u32 sw_if_index, u8 stop, + dhcp6_send_client_message_params_t * params); +void dhcp6_set_publisher_node (uword node_index, uword event_type); +int dhcp6_publish_report (address_report_t * r); + + +void + vl_api_want_dhcp6_reply_events_t_handler + (vl_api_want_dhcp6_reply_events_t * mp); +void + vl_api_dhcp6_send_client_message_t_handler + (vl_api_dhcp6_send_client_message_t * mp); +void + vl_api_dhcp6_clients_enable_disable_t_handler + (vl_api_dhcp6_clients_enable_disable_t * mp); + +extern vlib_node_registration_t dhcp6_reply_process_node; + +enum +{ DHCP6_DP_REPLY_REPORT, DHCP6_DP_REPORT_MAX }; + +typedef struct _vnet_dhcp6_reply_function_list_elt +{ + struct _vnet_dhcp6_reply_function_list_elt *next_dhcp6_reply_event_function; + clib_error_t *(*fp) (vl_api_dhcp6_reply_event_t * mp); +} _vnet_dhcp6_reply_event_function_list_elt_t; + +typedef struct +{ + _vnet_dhcp6_reply_event_function_list_elt_t *functions; +} dhcp6_ia_na_client_public_main_t; + +extern dhcp6_ia_na_client_public_main_t dhcp6_ia_na_client_public_main; + +#define VNET_DHCP6_REPLY_EVENT_FUNCTION(f) \ + \ +static void __vnet_dhcp6_reply_event_function_init_##f (void) \ + __attribute__((__constructor__)) ; \ + \ +static void __vnet_dhcp6_reply_event_function_init_##f (void) \ +{ \ + dhcp6_ia_na_client_public_main_t * nm = &dhcp6_ia_na_client_public_main; \ + static _vnet_dhcp6_reply_event_function_list_elt_t init_function; \ + init_function.next_dhcp6_reply_event_function = nm->functions; \ + nm->functions = &init_function; \ + init_function.fp = (void *) &f; \ +} \ + \ +static void __vnet_dhcp6_reply_event_function_deinit_##f (void) \ + __attribute__((__destructor__)) ; \ + \ +static void __vnet_dhcp6_reply_event_function_deinit_##f (void) \ +{ \ + dhcp6_ia_na_client_public_main_t * nm = &dhcp6_ia_na_client_public_main; \ + _vnet_dhcp6_reply_event_function_list_elt_t *next; \ + if (nm->functions->fp == (void *) &f) \ + { \ + nm->functions = \ + nm->functions->next_dhcp6_reply_event_function; \ + return; \ + } \ + next = nm->functions; \ + while (next->next_dhcp6_reply_event_function) \ + { \ + if (next->next_dhcp6_reply_event_function->fp == (void *) &f) \ + { \ + next->next_dhcp6_reply_event_function = \ + next->next_dhcp6_reply_event_function->next_dhcp6_reply_event_function; \ + return; \ + } \ + next = next->next_dhcp6_reply_event_function; \ + } \ +} + +#endif /* included_vnet_dhcp6_client_dp_h */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/dhcp/dhcp6_packet.h b/src/vnet/dhcp/dhcp6_packet.h index 1926154db27..d5467952a64 100644 --- a/src/vnet/dhcp/dhcp6_packet.h +++ b/src/vnet/dhcp/dhcp6_packet.h @@ -232,6 +232,12 @@ typedef CLIB_PACKED (struct typedef CLIB_PACKED (struct { + dhcpv6_option_t opt; ip6_address_t addr; u32 preferred; + u32 valid; + }) dhcpv6_ia_opt_addr_t; + +typedef CLIB_PACKED (struct + { dhcpv6_option_t opt; u16 options[0]; }) dhcpv6_oro_t; diff --git a/src/vnet/dhcp/dhcp6_pd_client_cp.c b/src/vnet/dhcp/dhcp6_pd_client_cp.c index a2a0618106c..33813789e70 100644 --- a/src/vnet/dhcp/dhcp6_pd_client_cp.c +++ b/src/vnet/dhcp/dhcp6_pd_client_cp.c @@ -45,7 +45,6 @@ typedef struct { u32 prefix_group_index; uword opaque_data; // used by prefix publisher - u32 TODO; ip6_address_t prefix; u8 prefix_length; u32 preferred_lt; @@ -365,7 +364,7 @@ dhcp6_pd_reply_event_handler (vl_api_dhcp6_pd_reply_event_t * mp) if (!client_state->rebinding && client_state->server_index != server_index) { clib_warning ("Reply message arrived with Server ID different " - "from that in Request of Renew message"); + "from that in Request or Renew message"); return 0; } @@ -433,6 +432,8 @@ dhcp6_pd_reply_event_handler (vl_api_dhcp6_pd_reply_event_t * mp) prefix_info->preferred_lt = preferred_time; prefix_info->valid_lt = valid_time; prefix_info->due_time = current_time + valid_time; + if (prefix_info->due_time > rm->max_valid_due_time) + rm->max_valid_due_time = prefix_info->due_time; continue; } @@ -448,6 +449,8 @@ dhcp6_pd_reply_event_handler (vl_api_dhcp6_pd_reply_event_t * mp) prefix_info->preferred_lt = preferred_time; prefix_info->valid_lt = valid_time; prefix_info->due_time = current_time + valid_time; + if (prefix_info->due_time > rm->max_valid_due_time) + rm->max_valid_due_time = prefix_info->due_time; rm->client_state_by_sw_if_index[sw_if_index].prefix_count++; u32 prefix_index = prefix_info - pm->prefix_pool; diff --git a/src/vnet/dhcp/dhcp6_pd_client_dp.c b/src/vnet/dhcp/dhcp6_pd_client_dp.c index b2b8f88f8fa..93311210944 100644 --- a/src/vnet/dhcp/dhcp6_pd_client_dp.c +++ b/src/vnet/dhcp/dhcp6_pd_client_dp.c @@ -24,6 +24,7 @@ #include <vlibapi/api_common.h> #include <vlibmemory/api.h> #include <vnet/dhcp/dhcp6_pd_client_dp.h> +#include <vnet/dhcp/dhcp6_client_common_dp.h> #include <vnet/vnet_msg_enum.h> @@ -43,86 +44,11 @@ #include <vlibapi/api_helper_macros.h> -typedef struct -{ - u8 entry_valid; - u8 keep_sending_client_message; /* when true then next fields are valid */ - dhcp6_pd_send_client_message_params_t params; - f64 transaction_start; - f64 sleep_interval; - f64 due_time; - u32 n_left; - f64 start_time; - u32 transaction_id; - vlib_buffer_t *buffer; - u32 elapsed_pos; - u32 adj_index; -} dhcp6_pd_client_state_t; - -typedef struct -{ - u8 *data; - u16 len; -} server_id_t; - -typedef struct -{ - dhcp6_pd_client_state_t *client_state_by_sw_if_index; - server_id_t *server_ids; - - uword publisher_node; - uword publisher_et; - - u32 seed; - - /* convenience */ - vlib_main_t *vlib_main; - vnet_main_t *vnet_main; -} dhcp6_pd_client_main_t; - -static dhcp6_pd_client_main_t dhcp6_pd_client_main; +dhcp6_pd_client_main_t dhcp6_pd_client_main; dhcp6_pd_client_public_main_t dhcp6_pd_client_public_main; -typedef struct -{ - ip6_address_t prefix; - u8 prefix_length; - u32 valid_time; - u32 preferred_time; - u16 status_code; -} prefix_info_t; - -typedef struct -{ - u32 sw_if_index; - u32 server_index; - u8 msg_type; - u32 T1; - u32 T2; - u16 inner_status_code; - u16 status_code; - u8 preference; - u32 n_prefixes; - prefix_info_t *prefixes; -} report_t; - -typedef union -{ - CLIB_PACKED (struct - { - u16 duid_type; - u16 hardware_type; - u8 lla[6]; - }); - char bin_string[10]; -} dhcpv6_duid_ll_string_t; - -static dhcpv6_duid_ll_string_t client_duid; -#define CLIENT_DUID_LENGTH sizeof (client_duid) -#define DHCPV6_CLIENT_IAID 1 - static void -signal_report (report_t * r) +signal_report (prefix_report_t * r) { vlib_main_t *vm = vlib_get_main (); dhcp6_pd_client_main_t *cm = &dhcp6_pd_client_main; @@ -131,13 +57,14 @@ signal_report (report_t * r) if (ni == (uword) ~ 0) return; - report_t *q = vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q); + prefix_report_t *q = + vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q); *q = *r; } -static int -publish_report (report_t * r) +int +dhcp6_pd_publish_report (prefix_report_t * r) { void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length); vl_api_rpc_call_main_thread (signal_report, (u8 *) r, sizeof *r); @@ -152,62 +79,6 @@ dhcp6_pd_set_publisher_node (uword node_index, uword event_type) cm->publisher_et = event_type; } -#define foreach_dhcpv6_pd_client \ - _(DROP, "error-drop") \ - _(LOOKUP, "ip6-lookup") - -typedef enum -{ -#define _(sym,str) DHCPV6_PD_CLIENT_NEXT_##sym, - foreach_dhcpv6_pd_client -#undef _ - DHCPV6_PD_CLIENT_N_NEXT, -} dhcpv6_pd_client_next_t; - -/** - * per-packet trace data - */ -typedef struct dhcpv6_pd_client_trace_t_ -{ -} dhcpv6_pd_client_trace_t; - -static u8 * -format_dhcpv6_pd_client_trace (u8 * s, va_list * args) -{ - CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); - CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); - //dhcpv6_pd_client_trace_t *t = va_arg (*args, dhcpv6_pd_client_trace_t *); - - s = format (s, "nothing"); - - return s; -} - -static u32 -server_index_get_or_create (u8 * data, u16 len) -{ - dhcp6_pd_client_main_t *cm = &dhcp6_pd_client_main; - u32 i; - server_id_t *se; - server_id_t new_se; - - for (i = 0; i < vec_len (cm->server_ids); i++) - { - se = &cm->server_ids[i]; - if (se->len == len && 0 == memcmp (se->data, data, len)) - return i; - } - - new_se.len = len; - new_se.data = 0; - vec_validate (new_se.data, len - 1); - memcpy (new_se.data, data, len); - - vec_add1 (cm->server_ids, new_se); - - return vec_len (cm->server_ids) - 1; -} - static void stop_sending_client_message (vlib_main_t * vm, dhcp6_pd_client_state_t * client_state) @@ -226,304 +97,13 @@ stop_sending_client_message (vlib_main_t * vm, } } -static uword -dhcpv6_pd_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, - vlib_frame_t * frame) -{ - dhcp6_pd_client_main_t *cm = &dhcp6_pd_client_main; - - dhcpv6_pd_client_next_t next_index; - u32 n_left_from, *from, *to_next; - next_index = 0; - n_left_from = frame->n_vectors; - from = vlib_frame_vector_args (frame); - - while (n_left_from > 0) - { - u32 n_left_to_next; - - vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); - - while (n_left_from > 0 && n_left_to_next > 0) - { - ip6_header_t *ip0; - u32 options_length; - dhcpv6_header_t *dhcpv60; - dhcpv6_option_t *option; - vlib_buffer_t *b0; - report_t report; - u32 next0 = DHCPV6_PD_CLIENT_NEXT_DROP; - u32 bi0; - u32 xid; - u32 sw_if_index; - u32 iaid; - u8 client_id_present = 0; - u8 discard = 0; - - dhcp6_pd_client_state_t *client_state = NULL; - - bi0 = from[0]; - to_next[0] = bi0; - from += 1; - to_next += 1; - n_left_from -= 1; - n_left_to_next -= 1; - - b0 = vlib_get_buffer (vm, bi0); - - dhcpv60 = vlib_buffer_get_current (b0); - ip0 = (void *) (b0->data + vnet_buffer (b0)->l3_hdr_offset); - u32 dhcpv6_ip6_palyoad_offset = - (u8 *) dhcpv60 - ((u8 *) ip0 + sizeof (*ip0)); - options_length = - ntohs (ip0->payload_length) - dhcpv6_ip6_palyoad_offset - - sizeof (*dhcpv60); - - memset (&report, 0, sizeof (report)); - - sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX]; - if (sw_if_index >= vec_len (cm->client_state_by_sw_if_index)) - client_state = 0; - else - client_state = &cm->client_state_by_sw_if_index[sw_if_index]; - - xid = - (dhcpv60->xid[0] << 16) + (dhcpv60->xid[1] << 8) + - dhcpv60->xid[2]; - if (!client_state || client_state->transaction_id != xid) - { - clib_warning - ("Received DHCPv6 message with wrong Transaction ID"); - discard = 1; - } - - report.sw_if_index = sw_if_index; - report.msg_type = dhcpv60->msg_type; - report.server_index = ~0; - - switch (dhcpv60->msg_type) - { - case DHCPV6_MSG_ADVERTISE: - case DHCPV6_MSG_REPLY: - option = (dhcpv6_option_t *) (dhcpv60 + 1); - while (options_length > 0) - { - if (options_length < - ntohs (option->length) + sizeof (*option)) - { - clib_warning - ("remaining payload length < option length (%d < %d)", - options_length, - ntohs (option->length) + sizeof (*option)); - break; - } - u16 oo = ntohs (option->option); - if (oo == DHCPV6_OPTION_IA_PD) - { - u8 discard_ia_pd = 0; - dhcpv6_ia_header_t *ia_header = (void *) option; - iaid = ntohl (ia_header->iaid); - u32 T1 = ntohl (ia_header->t1); - u32 T2 = ntohl (ia_header->t2); - if (iaid != DHCPV6_CLIENT_IAID) - discard_ia_pd = 1; - if (T1 != 0 && T2 != 0 && T1 > T2) - discard_ia_pd = 1; - if (!discard_ia_pd) - { - report.T1 = T1; - report.T2 = T2; - } - dhcpv6_option_t *inner_option = - (void *) ia_header->data; - u16 inner_options_length = - ntohs (option->length) - (sizeof (*ia_header) - - sizeof (dhcpv6_option_t)); - while (inner_options_length > 0) - { - u16 inner_oo = ntohs (inner_option->option); - if (discard_ia_pd) - ; - else if (inner_oo == DHCPV6_OPTION_IAPREFIX) - { - dhcpv6_ia_opt_pd_t *iaprefix = - (void *) inner_option; - vec_validate (report.prefixes, - report.n_prefixes); - prefix_info_t *prefix_info = - &report.prefixes[report.n_prefixes]; - report.n_prefixes++; - prefix_info->preferred_time = - ntohl (iaprefix->preferred); - prefix_info->valid_time = - ntohl (iaprefix->valid); - prefix_info->prefix_length = iaprefix->prefix; - prefix_info->prefix = iaprefix->addr; - } - else if (inner_oo == DHCPV6_OPTION_STATUS_CODE) - { - dhcpv6_status_code_t *sc = - (void *) inner_option; - report.inner_status_code = - ntohs (sc->status_code); - } - inner_options_length -= - sizeof (*inner_option) + - ntohs (inner_option->length); - inner_option = - (void *) ((u8 *) inner_option + - sizeof (*inner_option) + - ntohs (inner_option->length)); - } - } - else if (oo == DHCPV6_OPTION_CLIENTID) - { - if (client_id_present) - { - clib_warning - ("Duplicate Client ID in received DHVPv6 message"); - discard = 1; - } - else - { - u16 len = ntohs (option->length); - client_id_present = 1; - if (len != CLIENT_DUID_LENGTH || - 0 != memcmp (option->data, - client_duid.bin_string, - CLIENT_DUID_LENGTH)) - { - clib_warning - ("Unrecognized client DUID inside received DHVPv6 message"); - discard = 1; - } - } - } - else if (oo == DHCPV6_OPTION_SERVERID) - { - if (report.server_index != ~0) - { - clib_warning - ("Duplicate Server ID in received DHVPv6 message"); - discard = 1; - } - else - { - u16 ol = ntohs (option->length); - if (ol - 2 /* 2 byte DUID type code */ > 128) - { - clib_warning - ("Server DUID (without type code) is longer than 128 octets"); - discard = 1; - } - else - { - report.server_index = - server_index_get_or_create (option->data, ol); - } - } - } - else if (oo == DHCPV6_OPTION_PREFERENCE) - { - report.preference = option->data[0]; - } - else if (oo == DHCPV6_OPTION_STATUS_CODE) - { - dhcpv6_status_code_t *sc = (void *) option; - report.status_code = ntohs (sc->status_code); - } - options_length -= sizeof (*option) + ntohs (option->length); - option = - (void *) ((u8 *) option + sizeof (*option) + - ntohs (option->length)); - } - - if (!client_id_present) - { - clib_warning - ("Missing Client ID in received DHVPv6 message"); - discard = 1; - } - if (report.server_index == ~0) - { - clib_warning - ("Missing Server ID in received DHVPv6 message"); - discard = 1; - } - - if (!discard) - publish_report (&report); - else - vec_free (report.prefixes); - - break; - default: - break; - } - - if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) - { - dhcpv6_pd_client_trace_t *t = - vlib_add_trace (vm, node, b0, sizeof (*t)); - } - - /* verify speculative enqueue, maybe switch current next frame */ - vlib_validate_buffer_enqueue_x1 (vm, node, next_index, - to_next, n_left_to_next, - bi0, next0); - } - - vlib_put_next_frame (vm, node, next_index, n_left_to_next); - } - - return frame->n_vectors; -} - -/* *INDENT-OFF* */ -VLIB_REGISTER_NODE (dhcpv6_pd_client_node, static) = { - .function = dhcpv6_pd_client_node_fn, - .name = "dhcpv6-pd-client", - .vector_size = sizeof (u32), - - .n_errors = 0, - - .n_next_nodes = DHCPV6_PD_CLIENT_N_NEXT, - .next_nodes = { - #define _(s,n) [DHCPV6_PD_CLIENT_NEXT_##s] = n, - foreach_dhcpv6_pd_client - #undef _ - }, - - .format_trace = format_dhcpv6_pd_client_trace, -}; -/* *INDENT-ON* */ - -static_always_inline f64 -random_f64_from_to (f64 from, f64 to) -{ - static u32 seed = 0; - static u8 seed_set = 0; - if (!seed_set) - { - seed = random_default_seed (); - seed_set = 1; - } - return random_f64 (&seed) * (to - from) + from; -} - -static const ip6_address_t all_dhcp6_relay_agents_and_servers = { - .as_u8 = { - 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02} -}; - static vlib_buffer_t * create_buffer_for_client_message (vlib_main_t * vm, u32 sw_if_index, dhcp6_pd_client_state_t * client_state, u32 type) { - dhcp6_pd_client_main_t *dm = &dhcp6_pd_client_main; + dhcp6_client_common_main_t *ccm = &dhcp6_client_common_main; vnet_main_t *vnm = vnet_get_main (); vlib_buffer_t *b; @@ -612,7 +192,7 @@ create_buffer_for_client_message (vlib_main_t * vm, if (client_state->params.server_index != ~0) { server_id_t *se = - &dm->server_ids[client_state->params.server_index]; + &ccm->server_ids[client_state->params.server_index]; duid = (dhcpv6_option_t *) d; duid->option = clib_host_to_net_u16 (DHCPV6_OPTION_SERVERID); @@ -798,7 +378,7 @@ send_dhcp6_pd_client_message_process (vlib_main_t * vm, } /* *INDENT-OFF* */ -VLIB_REGISTER_NODE (send_dhcp6_pd_client_message_process_node) = { +VLIB_REGISTER_NODE (send_dhcp6_pd_client_message_process_node, static) = { .function = send_dhcp6_pd_client_message_process, .type = VLIB_NODE_TYPE_PROCESS, .name = "send-dhcp6-pd-client-message-process", @@ -934,7 +514,7 @@ dhcp6_pd_reply_process (vlib_main_t * vm, vlib_node_runtime_t * rt, int i; if (event_type == DHCP6_PD_DP_REPLY_REPORT) { - report_t *events = event_data; + prefix_report_t *events = event_data; for (i = 0; i < vec_len (events); i++) { u32 event_size = @@ -945,14 +525,15 @@ dhcp6_pd_reply_process (vlib_main_t * vm, vlib_node_runtime_t * rt, clib_mem_alloc (event_size); memset (event, 0, event_size); - event->sw_if_index = htonl (events[i].sw_if_index); - event->server_index = htonl (events[i].server_index); - event->msg_type = events[i].msg_type; - event->T1 = htonl (events[i].T1); - event->T2 = htonl (events[i].T2); - event->inner_status_code = htons (events[i].inner_status_code); - event->status_code = htons (events[i].status_code); - event->preference = events[i].preference; + event->sw_if_index = htonl (events[i].body.sw_if_index); + event->server_index = htonl (events[i].body.server_index); + event->msg_type = events[i].body.msg_type; + event->T1 = htonl (events[i].body.T1); + event->T2 = htonl (events[i].body.T2); + event->inner_status_code = + htons (events[i].body.inner_status_code); + event->status_code = htons (events[i].body.status_code); + event->preference = events[i].body.preference; event->n_prefixes = htonl (vec_len (events[i].prefixes)); vl_api_dhcp6_pd_prefix_info_t *prefix = @@ -960,7 +541,7 @@ dhcp6_pd_reply_process (vlib_main_t * vm, vlib_node_runtime_t * rt, u32 j; for (j = 0; j < vec_len (events[i].prefixes); j++) { - prefix_info_t *info = &events[i].prefixes[j]; + dhcp6_prefix_info_t *info = &events[i].prefixes[j]; memcpy (prefix->prefix, &info->prefix, 16); prefix->prefix_length = info->prefix_length; prefix->valid_time = htonl (info->valid_time); @@ -1002,7 +583,7 @@ dhcp6_pd_reply_process (vlib_main_t * vm, vlib_node_runtime_t * rt, } /* *INDENT-OFF* */ -VLIB_REGISTER_NODE (dhcp6_pd_reply_process_node, ) = { +VLIB_REGISTER_NODE (dhcp6_pd_reply_process_node) = { .function = dhcp6_pd_reply_process, .type = VLIB_NODE_TYPE_PROCESS, .name = "dhcp6-pd-reply-publisher-process", @@ -1036,7 +617,7 @@ void hash_unset (am->dhcp6_pd_reply_events_registration_hash, mp->client_index); if (pool_elts (am->dhcp6_pd_reply_events_registrations) == 0) - dhcp6_pd_set_publisher_node (~0, REPORT_MAX); + dhcp6_pd_set_publisher_node (~0, DHCP6_PD_DP_REPORT_MAX); goto reply; } } @@ -1058,91 +639,6 @@ reply: REPLY_MACRO (VL_API_WANT_DHCP6_PD_REPLY_EVENTS_REPLY); } -void -vl_api_dhcp6_duid_ll_set_t_handler (vl_api_dhcp6_duid_ll_set_t * mp) -{ - vl_api_dhcp6_duid_ll_set_reply_t *rmp; - dhcpv6_duid_ll_string_t *duid; - int rv = 0; - - duid = (dhcpv6_duid_ll_string_t *) mp->duid_ll; - if (duid->duid_type != htonl (DHCPV6_DUID_LL)) - { - rv = VNET_API_ERROR_INVALID_VALUE; - goto reply; - } - clib_memcpy (&client_duid, &duid, sizeof (client_duid)); - -reply: - REPLY_MACRO (VL_API_DHCP6_DUID_LL_SET_REPLY); -} - -static void -generate_client_duid (void) -{ - client_duid.duid_type = htons (DHCPV6_DUID_LL); - client_duid.hardware_type = htons (1); - - vnet_main_t *vnm = vnet_get_main (); - vnet_interface_main_t *im = &vnm->interface_main; - vnet_hw_interface_t *hi; - ethernet_interface_t *eth_if = 0; - - /* *INDENT-OFF* */ - pool_foreach (hi, im->hw_interfaces, - ({ - eth_if = ethernet_get_interface (ðernet_main, hi->hw_if_index); - if (eth_if) - break; - })); - /* *INDENT-ON* */ - - if (eth_if) - clib_memcpy (client_duid.lla, eth_if->address, 6); - else - { - clib_warning ("Failed to find any Ethernet interface, " - "setting DHCPv6 DUID link-layer address to random value"); - u32 seed = random_default_seed (); - random_u32 (&seed); - client_duid.lla[0] = 0xc2; /* locally administered unicast */ - client_duid.lla[1] = 0x18; - client_duid.lla[2] = 0x44; - client_duid.lla[3] = random_u32 (&seed); - client_duid.lla[4] = random_u32 (&seed); - client_duid.lla[5] = random_u32 (&seed); - } -} - -void -dhcp6_clients_enable_disable (u8 enable) -{ - vlib_main_t *vm = vlib_get_main (); - - if (enable) - { - if (client_duid.duid_type == 0) - generate_client_duid (); - udp_register_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client, - dhcpv6_pd_client_node.index, 0 /* is_ip6 */ ); - } - else - udp_unregister_dst_port (vm, UDP_DST_PORT_dhcpv6_to_client, - 0 /* is_ip6 */ ); -} - -void - vl_api_dhcp6_clients_enable_disable_t_handler - (vl_api_dhcp6_clients_enable_disable_t * mp) -{ - vl_api_dhcp6_clients_enable_disable_reply_t *rmp; - int rv = 0; - - dhcp6_clients_enable_disable (mp->enable); - - REPLY_MACRO (VL_API_WANT_DHCP6_PD_REPLY_EVENTS_REPLY); -} - static clib_error_t * dhcp6_pd_client_init (vlib_main_t * vm) { diff --git a/src/vnet/dhcp/dhcp6_pd_client_dp.h b/src/vnet/dhcp/dhcp6_pd_client_dp.h index ef69f19ff4d..88c731c2e67 100644 --- a/src/vnet/dhcp/dhcp6_pd_client_dp.h +++ b/src/vnet/dhcp/dhcp6_pd_client_dp.h @@ -17,10 +17,7 @@ #define included_vnet_dhcp6_pd_client_dp_h #include <vlib/vlib.h> - -#define vl_typedefs /* define message structures */ -#include <vnet/vnet_all_api_h.h> -#undef vl_typedefs +#include <vnet/dhcp/dhcp6_client_common_dp.h> typedef struct { @@ -44,11 +41,63 @@ typedef struct dhcp6_pd_send_client_message_params_prefix_t *prefixes; } dhcp6_pd_send_client_message_params_t; +typedef struct +{ + u8 entry_valid; + u8 keep_sending_client_message; /* when true then next fields are valid */ + dhcp6_pd_send_client_message_params_t params; + f64 transaction_start; + f64 sleep_interval; + f64 due_time; + u32 n_left; + f64 start_time; + u32 transaction_id; + vlib_buffer_t *buffer; + u32 elapsed_pos; + u32 adj_index; +} dhcp6_pd_client_state_t; + +typedef struct +{ + dhcp6_pd_client_state_t *client_state_by_sw_if_index; + + uword publisher_node; + uword publisher_et; + + u32 seed; + + /* convenience */ + vlib_main_t *vlib_main; + vnet_main_t *vnet_main; +} dhcp6_pd_client_main_t; + +extern dhcp6_pd_client_main_t dhcp6_pd_client_main; + +typedef struct +{ + ip6_address_t prefix; + u8 prefix_length; + u32 valid_time; + u32 preferred_time; + u16 status_code; +} dhcp6_prefix_info_t; + +typedef struct +{ + dhcp6_report_common_t body; + u32 n_prefixes; + dhcp6_prefix_info_t *prefixes; +} prefix_report_t; + +#define vl_typedefs /* define message structures */ +#include <vnet/vnet_all_api_h.h> +#undef vl_typedefs + void dhcp6_pd_send_client_message (vlib_main_t * vm, u32 sw_if_index, u8 stop, dhcp6_pd_send_client_message_params_t * params); void dhcp6_pd_set_publisher_node (uword node_index, uword event_type); -void dhcp6_clients_enable_disable (u8 enable); +int dhcp6_pd_publish_report (prefix_report_t * r); void vl_api_want_dhcp6_pd_reply_events_t_handler @@ -59,12 +108,11 @@ void void vl_api_dhcp6_clients_enable_disable_t_handler (vl_api_dhcp6_clients_enable_disable_t * mp); -void vl_api_dhcp6_duid_ll_set_t_handler (vl_api_dhcp6_duid_ll_set_t * mp); extern vlib_node_registration_t dhcp6_pd_reply_process_node; enum -{ DHCP6_PD_DP_REPLY_REPORT, REPORT_MAX }; +{ DHCP6_PD_DP_REPLY_REPORT, DHCP6_PD_DP_REPORT_MAX }; typedef struct _vnet_dhcp6_pd_reply_function_list_elt { diff --git a/src/vnet/dhcp/dhcp_api.c b/src/vnet/dhcp/dhcp_api.c index 4e2ed86deed..59ab6dcbba5 100644 --- a/src/vnet/dhcp/dhcp_api.c +++ b/src/vnet/dhcp/dhcp_api.c @@ -25,6 +25,8 @@ #include <vnet/dhcp/dhcp_proxy.h> #include <vnet/dhcp/client.h> #include <vnet/dhcp/dhcp6_pd_client_dp.h> +#include <vnet/dhcp/dhcp6_ia_na_client_dp.h> +#include <vnet/dhcp/dhcp6_client_common_dp.h> #include <vnet/fib/fib_table.h> #include <vnet/vnet_msg_enum.h> @@ -53,7 +55,9 @@ _(DHCP_CLIENT_CONFIG, dhcp_client_config) \ _(DHCP_CLIENT_DUMP, dhcp_client_dump) \ _(WANT_DHCP6_PD_REPLY_EVENTS, want_dhcp6_pd_reply_events) \ _(DHCP6_PD_SEND_CLIENT_MESSAGE, dhcp6_pd_send_client_message) \ -_(DHCP6_CLIENTS_ENABLE_DISABLE, dhcp6_clients_enable_disable) \ +_(WANT_DHCP6_REPLY_EVENTS, want_dhcp6_reply_events) \ +_(DHCP6_SEND_CLIENT_MESSAGE, dhcp6_send_client_message) \ +_(DHCP6_CLIENTS_ENABLE_DISABLE, dhcp6_clients_enable_disable) \ _(DHCP6_DUID_LL_SET, dhcp6_duid_ll_set) @@ -390,6 +394,8 @@ dhcp_api_hookup (vlib_main_t * vm) dhcp6_pd_set_publisher_node (dhcp6_pd_reply_process_node.index, DHCP6_PD_DP_REPLY_REPORT); + dhcp6_set_publisher_node (dhcp6_reply_process_node.index, + DHCP6_DP_REPLY_REPORT); return 0; } diff --git a/src/vnet/vnet_all_api_h.h b/src/vnet/vnet_all_api_h.h index d272fbc052d..c5c47e810b6 100644 --- a/src/vnet/vnet_all_api_h.h +++ b/src/vnet/vnet_all_api_h.h @@ -74,6 +74,7 @@ #include <vnet/sctp/sctp.api.h> #include <vnet/qos/qos.api.h> #include <vnet/dhcp/dhcp6_pd_client_cp.api.h> +#include <vnet/dhcp/dhcp6_ia_na_client_cp.api.h> /* * fd.io coding-style-patch-verification: ON diff --git a/test/test_dhcp6.py b/test/test_dhcp6.py index 3976f22c4f9..fe06f986bf5 100644 --- a/test/test_dhcp6.py +++ b/test/test_dhcp6.py @@ -1,13 +1,13 @@ from scapy.layers.dhcp6 import DHCP6_Advertise, DHCP6OptClientId, \ DHCP6OptStatusCode, DHCP6OptPref, DHCP6OptIA_PD, DHCP6OptIAPrefix, \ DHCP6OptServerId, DHCP6_Solicit, DHCP6_Reply, DHCP6_Request, DHCP6_Renew, \ - DHCP6_Rebind, DUID_LL, DHCP6_Release, DHCP6OptElapsedTime + DHCP6_Rebind, DUID_LL, DHCP6_Release, DHCP6OptElapsedTime, DHCP6OptIA_NA, \ + DHCP6OptIAAddress from scapy.layers.inet6 import IPv6, Ether, UDP from scapy.utils6 import in6_mactoifaceid from scapy.utils import inet_ntop, inet_pton from socket import AF_INET6 from framework import VppTestCase -from time import time def ip6_normalize(ip6): @@ -20,15 +20,15 @@ def mk_ll_addr(mac): return addr -class TestDHCPv6PD(VppTestCase): - """ DHCPv6 PD Data Plane Test Case """ +class TestDHCPv6DataPlane(VppTestCase): + """ DHCPv6 Data Plane Test Case """ @classmethod def setUpClass(cls): - super(TestDHCPv6PD, cls).setUpClass() + super(TestDHCPv6DataPlane, cls).setUpClass() def setUp(self): - super(TestDHCPv6PD, self).setUp() + super(TestDHCPv6DataPlane, self).setUp() self.create_pg_interfaces(range(1)) self.interfaces = list(self.pg_interfaces) @@ -42,10 +42,85 @@ class TestDHCPv6PD(VppTestCase): for i in self.interfaces: i.unconfig_ip6() i.admin_down() - super(TestDHCPv6PD, self).tearDown() + super(TestDHCPv6DataPlane, self).tearDown() - def test_dhcp_send_solicit_receive_advertise(self): - """ Verify DHCPv6 PD Solicit packet and received Advertise envent """ + def test_dhcp_ia_na_send_solicit_receive_advertise(self): + """ Verify DHCPv6 IA NA Solicit packet and Advertise envent """ + + self.vapi.dhcp6_clients_enable_disable() + + self.pg_enable_capture(self.pg_interfaces) + self.pg_start() + address_bin = '\00\01\00\02\00\03' + '\00' * 8 + '\00\05' + address = {'address': address_bin, + 'preferred_time': 60, + 'valid_time': 120} + self.vapi.dhcp6_send_client_message(1, self.pg0.sw_if_index, + T1=20, T2=40, addresses=[address]) + rx_list = self.pg0.get_capture(1) + self.assertEqual(len(rx_list), 1) + packet = rx_list[0] + + self.assertTrue(packet.haslayer(IPv6)) + self.assertTrue(packet[IPv6].haslayer(DHCP6_Solicit)) + + client_duid = packet[DHCP6OptClientId].duid + trid = packet[DHCP6_Solicit].trid + + dst = ip6_normalize(packet[IPv6].dst) + dst2 = ip6_normalize("ff02::1:2") + self.assert_equal(dst, dst2) + src = ip6_normalize(packet[IPv6].src) + src2 = ip6_normalize(self.pg0.local_ip6_ll) + self.assert_equal(src, src2) + ia_na = packet[DHCP6OptIA_NA] + self.assert_equal(ia_na.T1, 20) + self.assert_equal(ia_na.T2, 40) + self.assert_equal(len(ia_na.ianaopts), 1) + address = ia_na.ianaopts[0] + self.assert_equal(address.addr, '1:2:3::5') + self.assert_equal(address.preflft, 60) + self.assert_equal(address.validlft, 120) + + self.vapi.want_dhcp6_reply_events() + + try: + ia_na_opts = DHCP6OptIAAddress(addr='7:8::2', preflft=60, + validlft=120) + p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / + IPv6(src=mk_ll_addr(self.pg0.remote_mac), + dst=self.pg0.local_ip6_ll) / + UDP(sport=547, dport=546) / + DHCP6_Advertise(trid=trid) / + DHCP6OptServerId(duid=self.server_duid) / + DHCP6OptClientId(duid=client_duid) / + DHCP6OptPref(prefval=7) / + DHCP6OptStatusCode(statuscode=1) / + DHCP6OptIA_NA(iaid=1, T1=20, T2=40, ianaopts=ia_na_opts) + ) + self.pg0.add_stream([p]) + self.pg_start() + + ev = self.vapi.wait_for_event(1, "dhcp6_reply_event") + + self.assert_equal(ev.preference, 7) + self.assert_equal(ev.status_code, 1) + self.assert_equal(ev.T1, 20) + self.assert_equal(ev.T2, 40) + + reported_address = ev.addresses[0] + address = inet_pton(AF_INET6, ia_na_opts.getfieldval("addr")) + self.assert_equal(reported_address.address, address) + self.assert_equal(reported_address.preferred_time, + ia_na_opts.getfieldval("preflft")) + self.assert_equal(reported_address.valid_time, + ia_na_opts.getfieldval("validlft")) + + finally: + self.vapi.want_dhcp6_reply_events(enable_disable=0) + + def test_dhcp_pd_send_solicit_receive_advertise(self): + """ Verify DHCPv6 PD Solicit packet and Advertise envent """ self.vapi.dhcp6_clients_enable_disable() @@ -86,38 +161,289 @@ class TestDHCPv6PD(VppTestCase): self.vapi.want_dhcp6_pd_reply_events() - ia_pd_opts = DHCP6OptIAPrefix(prefix='7:8::', plen=56, preflft=60, - validlft=120) + try: + ia_pd_opts = DHCP6OptIAPrefix(prefix='7:8::', plen=56, preflft=60, + validlft=120) + p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / + IPv6(src=mk_ll_addr(self.pg0.remote_mac), + dst=self.pg0.local_ip6_ll) / + UDP(sport=547, dport=546) / + DHCP6_Advertise(trid=trid) / + DHCP6OptServerId(duid=self.server_duid) / + DHCP6OptClientId(duid=client_duid) / + DHCP6OptPref(prefval=7) / + DHCP6OptStatusCode(statuscode=1) / + DHCP6OptIA_PD(iaid=1, T1=20, T2=40, iapdopt=ia_pd_opts) + ) + self.pg0.add_stream([p]) + self.pg_start() + + ev = self.vapi.wait_for_event(1, "dhcp6_pd_reply_event") + + self.assert_equal(ev.preference, 7) + self.assert_equal(ev.status_code, 1) + self.assert_equal(ev.T1, 20) + self.assert_equal(ev.T2, 40) + + reported_prefix = ev.prefixes[0] + prefix = inet_pton(AF_INET6, ia_pd_opts.getfieldval("prefix")) + self.assert_equal(reported_prefix.prefix, prefix) + self.assert_equal(reported_prefix.prefix_length, + ia_pd_opts.getfieldval("plen")) + self.assert_equal(reported_prefix.preferred_time, + ia_pd_opts.getfieldval("preflft")) + self.assert_equal(reported_prefix.valid_time, + ia_pd_opts.getfieldval("validlft")) + + finally: + self.vapi.want_dhcp6_pd_reply_events(enable_disable=0) + + +class TestDHCPv6IANAControlPlane(VppTestCase): + """ DHCPv6 IA NA Control Plane Test Case """ + + @classmethod + def setUpClass(cls): + super(TestDHCPv6IANAControlPlane, cls).setUpClass() + + def setUp(self): + super(TestDHCPv6IANAControlPlane, self).setUp() + + self.create_pg_interfaces(range(1)) + self.interfaces = list(self.pg_interfaces) + for i in self.interfaces: + i.admin_up() + + self.server_duid = DUID_LL(lladdr=self.pg0.remote_mac) + self.client_duid = None + self.T1 = 1 + self.T2 = 2 + + fib = self.vapi.ip6_fib_dump() + self.initial_addresses = set(self.get_interface_addresses(fib, + self.pg0)) + + self.pg_enable_capture(self.pg_interfaces) + self.pg_start() + + self.vapi.dhcp6_client_enable_disable(self.pg0.sw_if_index) + + def tearDown(self): + self.vapi.dhcp6_client_enable_disable(self.pg0.sw_if_index, enable=0) + + for i in self.interfaces: + i.admin_down() + + super(TestDHCPv6IANAControlPlane, self).tearDown() + + @staticmethod + def get_interface_addresses(fib, pg): + lst = [] + for entry in fib: + if entry.address_length == 128: + path = entry.path[0] + if path.sw_if_index == pg.sw_if_index: + lst.append(entry.address) + return lst + + def get_addresses(self): + fib = self.vapi.ip6_fib_dump() + addresses = set(self.get_interface_addresses(fib, self.pg0)) + return addresses.difference(self.initial_addresses) + + def validate_duid_ll(self, duid): + DUID_LL(duid) + + def validate_packet(self, packet, msg_type, is_resend=False): + try: + self.assertTrue(packet.haslayer(msg_type)) + client_duid = packet[DHCP6OptClientId].duid + if self.client_duid is None: + self.client_duid = client_duid + self.validate_duid_ll(client_duid) + else: + self.assertEqual(self.client_duid, client_duid) + if msg_type != DHCP6_Solicit and msg_type != DHCP6_Rebind: + server_duid = packet[DHCP6OptServerId].duid + self.assertEqual(server_duid, self.server_duid) + if is_resend: + self.assertEqual(self.trid, packet[msg_type].trid) + else: + self.trid = packet[msg_type].trid + ip = packet[IPv6] + udp = packet[UDP] + self.assertEqual(ip.dst, 'ff02::1:2') + self.assertEqual(udp.sport, 546) + self.assertEqual(udp.dport, 547) + dhcpv6 = packet[msg_type] + elapsed_time = dhcpv6[DHCP6OptElapsedTime] + if (is_resend): + self.assertNotEqual(elapsed_time.elapsedtime, 0) + else: + self.assertEqual(elapsed_time.elapsedtime, 0) + except: + packet.show() + raise + + def wait_for_packet(self, msg_type, timeout=None, is_resend=False): + if timeout is None: + timeout = 3 + rx_list = self.pg0.get_capture(1, timeout=timeout) + packet = rx_list[0] + self.validate_packet(packet, msg_type, is_resend=is_resend) + + def wait_for_solicit(self, timeout=None, is_resend=False): + self.wait_for_packet(DHCP6_Solicit, timeout, is_resend=is_resend) + + def wait_for_request(self, timeout=None, is_resend=False): + self.wait_for_packet(DHCP6_Request, timeout, is_resend=is_resend) + + def wait_for_renew(self, timeout=None, is_resend=False): + self.wait_for_packet(DHCP6_Renew, timeout, is_resend=is_resend) + + def wait_for_rebind(self, timeout=None, is_resend=False): + self.wait_for_packet(DHCP6_Rebind, timeout, is_resend=is_resend) + + def wait_for_release(self, timeout=None, is_resend=False): + self.wait_for_packet(DHCP6_Release, timeout, is_resend=is_resend) + + def send_packet(self, msg_type, t1=None, t2=None, ianaopts=None): + if t1 is None: + t1 = self.T1 + if t2 is None: + t2 = self.T2 + if ianaopts is None: + opt_ia_na = DHCP6OptIA_NA(iaid=1, T1=t1, T2=t2) + else: + opt_ia_na = DHCP6OptIA_NA(iaid=1, T1=t1, T2=t2, ianaopts=ianaopts) p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(src=mk_ll_addr(self.pg0.remote_mac), dst=self.pg0.local_ip6_ll) / UDP(sport=547, dport=546) / - DHCP6_Advertise(trid=trid) / + msg_type(trid=self.trid) / DHCP6OptServerId(duid=self.server_duid) / - DHCP6OptClientId(duid=client_duid) / - DHCP6OptPref(prefval=7) / - DHCP6OptStatusCode(statuscode=1) / - DHCP6OptIA_PD(iaid=1, T1=20, T2=40, iapdopt=ia_pd_opts) + DHCP6OptClientId(duid=self.client_duid) / + opt_ia_na ) self.pg0.add_stream([p]) + self.pg_enable_capture(self.pg_interfaces) self.pg_start() - ev = self.vapi.wait_for_event(1, "dhcp6_pd_reply_event") + def send_advertise(self, t1=None, t2=None, ianaopts=None): + self.send_packet(DHCP6_Advertise, t1, t2, ianaopts) + + def send_reply(self, t1=None, t2=None, ianaopts=None): + self.send_packet(DHCP6_Reply, t1, t2, ianaopts) + + def test_T1_and_T2_timeouts(self): + """ Test T1 and T2 timeouts """ + + self.wait_for_solicit() + self.send_advertise() + self.wait_for_request() + self.send_reply() + + self.sleep(1) + + self.wait_for_renew() + + self.pg_enable_capture(self.pg_interfaces) + + self.sleep(1) + + self.wait_for_rebind() + + def test_addresses(self): + """ Test handling of addresses """ - self.assert_equal(ev.preference, 7) - self.assert_equal(ev.status_code, 1) - self.assert_equal(ev.T1, 20) - self.assert_equal(ev.T2, 40) + ia_na_opts = DHCP6OptIAAddress(addr='7:8::2', preflft=1, + validlft=2) - reported_prefix = ev.prefixes[0] - prefix = inet_pton(AF_INET6, ia_pd_opts.getfieldval("prefix")) - self.assert_equal(reported_prefix.prefix, prefix) - self.assert_equal(reported_prefix.prefix_length, - ia_pd_opts.getfieldval("plen")) - self.assert_equal(reported_prefix.preferred_time, - ia_pd_opts.getfieldval("preflft")) - self.assert_equal(reported_prefix.valid_time, - ia_pd_opts.getfieldval("validlft")) + self.wait_for_solicit() + self.send_advertise(t1=20, t2=40, ianaopts=ia_na_opts) + self.wait_for_request() + self.send_reply(t1=20, t2=40, ianaopts=ia_na_opts) + self.sleep(0.1) + + # check FIB for new address + new_addresses = self.get_addresses() + self.assertEqual(len(new_addresses), 1) + addr = list(new_addresses)[0] + self.assertEqual(inet_ntop(AF_INET6, addr), '7:8::2') + + self.sleep(2) + + # check that the address is deleted + fib = self.vapi.ip6_fib_dump() + addresses = set(self.get_interface_addresses(fib, self.pg0)) + new_addresses = addresses.difference(self.initial_addresses) + self.assertEqual(len(new_addresses), 0) + + def test_sending_client_messages_solicit(self): + """ VPP receives messages from DHCPv6 client """ + + self.wait_for_solicit() + self.send_packet(DHCP6_Solicit) + self.send_packet(DHCP6_Request) + self.send_packet(DHCP6_Renew) + self.send_packet(DHCP6_Rebind) + self.sleep(1) + self.wait_for_solicit(is_resend=True) + + def test_sending_inappropriate_packets(self): + """ Server sends messages with inappropriate message types """ + + self.wait_for_solicit() + self.send_reply() + self.wait_for_solicit(is_resend=True) + self.send_advertise() + self.wait_for_request() + self.send_advertise() + self.wait_for_request(is_resend=True) + self.send_reply() + self.wait_for_renew() + + def test_no_address_available_in_advertise(self): + """ Advertise message contains NoAddrsAvail status code """ + + self.wait_for_solicit() + noavail = DHCP6OptStatusCode(statuscode=2) # NoAddrsAvail + self.send_advertise(ianaopts=noavail) + self.wait_for_solicit(is_resend=True) + + def test_preferred_greater_than_valit_lifetime(self): + """ Preferred lifetime is greater than valid lifetime """ + + self.wait_for_solicit() + self.send_advertise() + self.wait_for_request() + ia_na_opts = DHCP6OptIAAddress(addr='7:8::2', preflft=4, validlft=3) + self.send_reply(ianaopts=ia_na_opts) + + self.sleep(0.5) + + # check FIB contains no addresses + fib = self.vapi.ip6_fib_dump() + addresses = set(self.get_interface_addresses(fib, self.pg0)) + new_addresses = addresses.difference(self.initial_addresses) + self.assertEqual(len(new_addresses), 0) + + def test_T1_greater_than_T2(self): + """ T1 is greater than T2 """ + + self.wait_for_solicit() + self.send_advertise() + self.wait_for_request() + ia_na_opts = DHCP6OptIAAddress(addr='7:8::2', preflft=4, validlft=8) + self.send_reply(t1=80, t2=40, ianaopts=ia_na_opts) + + self.sleep(0.5) + + # check FIB contains no addresses + fib = self.vapi.ip6_fib_dump() + addresses = set(self.get_interface_addresses(fib, self.pg0)) + new_addresses = addresses.difference(self.initial_addresses) + self.assertEqual(len(new_addresses), 0) class TestDHCPv6PDControlPlane(VppTestCase): @@ -281,6 +607,7 @@ class TestDHCPv6PDControlPlane(VppTestCase): def test_prefixes(self): """ Test handling of prefixes """ + address_bin_1 = None address_bin_2 = None try: @@ -358,8 +685,8 @@ class TestDHCPv6PDControlPlane(VppTestCase): self.sleep(1) self.wait_for_solicit(is_resend=True) - def test_sending_inapropriate_packets(self): - """ Server sends messages with inapropriate message types """ + def test_sending_inappropriate_packets(self): + """ Server sends messages with inappropriate message types """ self.wait_for_solicit() self.send_reply() diff --git a/test/vpp_papi_provider.py b/test/vpp_papi_provider.py index b0b452e8c9c..7869afa32cc 100644 --- a/test/vpp_papi_provider.py +++ b/test/vpp_papi_provider.py @@ -503,6 +503,11 @@ class VppPapiProvider(object): 'learn_limit': learn_limit, 'pid': os.getpid(), }) + def want_dhcp6_reply_events(self, enable_disable=1): + return self.api(self.papi.want_dhcp6_reply_events, + {'enable_disable': enable_disable, + 'pid': os.getpid()}) + def want_dhcp6_pd_reply_events(self, enable_disable=1): return self.api(self.papi.want_dhcp6_pd_reply_events, {'enable_disable': enable_disable, @@ -512,6 +517,24 @@ class VppPapiProvider(object): return self.api(self.papi.dhcp6_clients_enable_disable, {'enable': enable}) + def dhcp6_send_client_message(self, msg_type, sw_if_index, T1, T2, + addresses, server_index=0xFFFFFFFF, + irt=0, mrt=0, mrc=1, mrd=0, stop=0, + ): + return self.api(self.papi.dhcp6_send_client_message, + {'sw_if_index': sw_if_index, + 'server_index': server_index, + 'irt': irt, + 'mrt': mrt, + 'mrc': mrc, + 'mrd': mrd, + 'stop': stop, + 'msg_type': msg_type, + 'T1': T1, + 'T2': T2, + 'n_addresses': len(addresses), + 'addresses': addresses}) + def dhcp6_pd_send_client_message(self, msg_type, sw_if_index, T1, T2, prefixes, server_index=0xFFFFFFFF, irt=0, mrt=0, mrc=1, mrd=0, stop=0, @@ -530,6 +553,12 @@ class VppPapiProvider(object): 'n_prefixes': len(prefixes), 'prefixes': prefixes}) + def dhcp6_client_enable_disable(self, sw_if_index, prefix_group='', + enable=1): + return self.api(self.papi.dhcp6_client_enable_disable, + {'sw_if_index': sw_if_index, + 'enable': enable}) + def dhcp6_pd_client_enable_disable(self, sw_if_index, prefix_group='', enable=1): return self.api(self.papi.dhcp6_pd_client_enable_disable, |