2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/vnet.h>
17 #include <vlibmemory/api.h>
18 #include <vnet/vnet_msg_enum.h>
19 #include <vnet/dhcp/dhcp6_packet.h>
20 #include <vnet/dhcp/dhcp6_ia_na_client_dp.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ip/ip6.h>
26 #define vl_typedefs /* define message structures */
27 #include <vnet/vnet_all_api_h.h>
30 #define vl_endianfun /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
34 #include <vlibapi/api_helper_macros.h>
36 #define foreach_dhcp6_client_cp_msg \
37 _(DHCP6_CLIENT_ENABLE_DISABLE, dhcp6_client_enable_disable)
39 #define vl_api_dhcp6_client_enable_disable_t_print vl_noop_handler
44 ip6_address_t address;
64 address_info_t *address_pool;
65 client_state_t *client_state_by_sw_if_index;
67 f64 max_valid_due_time;
70 vlib_main_t *vlib_main;
71 vnet_main_t *vnet_main;
74 } dhcp6_client_cp_main_t;
76 static dhcp6_client_cp_main_t dhcp6_client_cp_main;
80 RD_CP_EVENT_INTERRUPT,
85 send_client_message_start_stop (u32 sw_if_index, u32 server_index,
86 u8 msg_type, address_info_t * address_list,
89 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
90 dhcp6_send_client_message_params_t params = { 0, };
91 dhcp6_send_client_message_params_address_t *addresses = 0, *addr;
94 ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) &&
95 rm->client_state_by_sw_if_index[sw_if_index].enabled);
96 client_state_t *client_state =
97 &rm->client_state_by_sw_if_index[sw_if_index];
99 params.sw_if_index = sw_if_index;
100 params.server_index = server_index;
101 params.msg_type = msg_type;
104 if (msg_type == DHCPV6_MSG_SOLICIT)
109 else if (msg_type == DHCPV6_MSG_REQUEST)
115 else if (msg_type == DHCPV6_MSG_RENEW)
119 f64 current_time = vlib_time_now (rm->vlib_main);
120 i32 diff_time = client_state->T2 - current_time;
123 params.mrd = diff_time;
125 else if (msg_type == DHCPV6_MSG_REBIND)
129 f64 current_time = vlib_time_now (rm->vlib_main);
130 i32 diff_time = rm->max_valid_due_time - current_time;
133 params.mrd = diff_time;
135 else if (msg_type == DHCPV6_MSG_RELEASE)
143 if (vec_len (address_list) != 0)
144 vec_validate (addresses, vec_len (address_list) - 1);
145 for (i = 0; i < vec_len (address_list); i++)
147 address_info_t *address = &address_list[i];
148 addr = &addresses[i];
149 addr->valid_lt = address->valid_lt;
150 addr->preferred_lt = address->preferred_lt;
151 addr->address = address->address;
153 params.addresses = addresses;
155 dhcp6_send_client_message (rm->vlib_main, sw_if_index, !start, ¶ms);
157 vec_free (params.addresses);
160 static void interrupt_process (void);
163 ip6_enable (u32 sw_if_index)
165 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
168 rv = enable_ip6_interface (rm->vlib_main, sw_if_index);
174 ip6_addresses_equal (ip6_address_t * address1, ip6_address_t * address2)
176 if (address1->as_u64[0] != address2->as_u64[0])
178 return address1->as_u64[1] == address2->as_u64[1];
181 static clib_error_t *
182 dhcp6_reply_event_handler (vl_api_dhcp6_reply_event_t * mp)
184 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
185 vlib_main_t *vm = rm->vlib_main;
186 client_state_t *client_state;
187 ip6_address_t *address;
190 vl_api_dhcp6_address_info_t *api_address;
191 u32 inner_status_code;
195 clib_error_t *error = 0;
198 current_time = vlib_time_now (vm);
200 sw_if_index = ntohl (mp->sw_if_index);
202 if (sw_if_index >= vec_len (rm->client_state_by_sw_if_index))
205 client_state = &rm->client_state_by_sw_if_index[sw_if_index];
207 if (!client_state->enabled)
210 server_index = ntohl (mp->server_index);
212 n_addresses = ntohl (mp->n_addresses);
214 inner_status_code = ntohs (mp->inner_status_code);
215 status_code = ntohs (mp->status_code);
217 if (mp->msg_type == DHCPV6_MSG_ADVERTISE
218 && client_state->server_index == ~0)
220 address_info_t *address_list = 0, *address_info;
222 if (inner_status_code == DHCPV6_STATUS_NOADDRS_AVAIL)
225 ("Advertise message arrived with NoAddrsAvail status code");
230 vec_validate (address_list, n_addresses - 1);
231 for (i = 0; i < n_addresses; i++)
233 api_address = &mp->addresses[i];
234 address = (ip6_address_t *) api_address->address;
236 address_info = &address_list[i];
237 address_info->address = *address;
238 address_info->preferred_lt = 0;
239 address_info->valid_lt = 0;
242 client_state->server_index = server_index;
244 send_client_message_start_stop (sw_if_index, server_index,
245 DHCPV6_MSG_REQUEST, address_list, 1);
246 vec_free (address_list);
249 if (mp->msg_type != DHCPV6_MSG_REPLY)
252 if (!client_state->rebinding && client_state->server_index != server_index)
254 clib_warning ("Reply message arrived with Server ID different "
255 "from that in Request or Renew message");
259 if (inner_status_code == DHCPV6_STATUS_NOADDRS_AVAIL)
261 clib_warning ("Reply message arrived with NoAddrsAvail status code");
265 ("Invalid Reply message arrived: It contains NoAddrsAvail "
266 "status code but also contains addresses");
271 if (status_code == DHCPV6_STATUS_UNSPEC_FAIL)
273 clib_warning ("Reply message arrived with UnspecFail status code");
277 send_client_message_start_stop (sw_if_index, server_index,
280 for (i = 0; i < n_addresses; i++)
282 address_info_t *address_info = 0;
286 api_address = &mp->addresses[i];
288 address = (ip6_address_t *) api_address->address;
290 if (ip6_address_is_link_local_unicast (address))
293 valid_time = ntohl (api_address->valid_time);
294 preferred_time = ntohl (api_address->preferred_time);
296 if (preferred_time > valid_time)
299 u8 address_already_present = 0;
301 pool_foreach (address_info, rm->address_pool,
303 if (address_info->sw_if_index != sw_if_index)
305 else if (!ip6_addresses_equal (&address_info->address, address))
309 address_already_present = 1;
310 goto address_pool_foreach_out;
314 address_pool_foreach_out:
316 if (address_already_present)
318 address_info->preferred_lt = preferred_time;
319 address_info->valid_lt = valid_time;
320 address_info->due_time = current_time + valid_time;
321 if (address_info->due_time > rm->max_valid_due_time)
322 rm->max_valid_due_time = address_info->due_time;
329 pool_get (rm->address_pool, address_info);
330 address_info->sw_if_index = sw_if_index;
331 address_info->address = *address;
332 address_info->preferred_lt = preferred_time;
333 address_info->valid_lt = valid_time;
334 address_info->due_time = current_time + valid_time;
335 if (address_info->due_time > rm->max_valid_due_time)
336 rm->max_valid_due_time = address_info->due_time;
337 rm->client_state_by_sw_if_index[sw_if_index].address_count++;
339 error = ip6_add_del_interface_address (vm, sw_if_index,
340 &address_info->address, 64, 0);
342 clib_warning ("Failed to add interface address");
345 client_state->server_index = server_index;
346 client_state->T1 = ntohl (mp->T1);
347 client_state->T2 = ntohl (mp->T2);
348 if (client_state->T1 != 0)
349 client_state->T1_due_time = current_time + client_state->T1;
350 if (client_state->T2 != 0)
351 client_state->T2_due_time = current_time + client_state->T2;
352 client_state->rebinding = 0;
354 interrupt_process ();
359 static address_info_t *
360 create_address_list (u32 sw_if_index)
362 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
363 address_info_t *address_info, *address_list = 0;;
366 pool_foreach (address_info, rm->address_pool,
368 if (address_info->sw_if_index == sw_if_index)
370 u32 pos = vec_len (address_list);
371 vec_validate (address_list, pos);
372 clib_memcpy (&address_list[pos], address_info, sizeof (*address_info));
380 VNET_DHCP6_REPLY_EVENT_FUNCTION (dhcp6_reply_event_handler);
383 dhcp6_client_cp_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
386 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
387 address_info_t *address_info;
388 client_state_t *client_state;
389 f64 sleep_time = 1e9;
394 uword *event_data = 0;
399 vlib_process_wait_for_event_or_clock (vm, sleep_time);
400 event_type = vlib_process_get_events (vm, &event_data);
401 vec_reset_length (event_data);
403 if (event_type == RD_CP_EVENT_DISABLE)
405 vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_DISABLED);
410 current_time = vlib_time_now (vm);
413 due_time = current_time + 1e9;
415 pool_foreach (address_info, rm->address_pool,
417 if (address_info->due_time > current_time)
419 if (address_info->due_time < due_time)
420 due_time = address_info->due_time;
424 u32 sw_if_index = address_info->sw_if_index;
425 error = ip6_add_del_interface_address (vm, sw_if_index,
426 &address_info->address,
429 clib_warning ("Failed to delete interface address");
430 pool_put (rm->address_pool, address_info);
431 /* make sure ip6 stays enabled */
432 ip6_enable (sw_if_index);
433 client_state = &rm->client_state_by_sw_if_index[sw_if_index];
434 if (--client_state->address_count == 0)
436 client_state->rebinding = 0;
437 client_state->server_index = ~0;
438 send_client_message_start_stop (sw_if_index, ~0,
445 for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++)
447 client_state_t *cs = &rm->client_state_by_sw_if_index[i];
448 if (cs->enabled && cs->server_index != ~0)
450 if (cs->T2_due_time > current_time)
452 if (cs->T2_due_time < due_time)
453 due_time = cs->T2_due_time;
454 if (cs->T1_due_time > current_time)
456 if (cs->T1_due_time < due_time)
457 due_time = cs->T1_due_time;
461 cs->T1_due_time = DBL_MAX;
462 address_info_t *address_list;
463 address_list = create_address_list (i);
465 send_client_message_start_stop (i, cs->server_index,
468 vec_free (address_list);
473 cs->T2_due_time = DBL_MAX;
474 address_info_t *address_list;
475 address_list = create_address_list (i);
477 send_client_message_start_stop (i, ~0,
480 vec_free (address_list);
484 current_time = vlib_time_now (vm);
486 while (due_time < current_time);
488 sleep_time = due_time - current_time;
495 VLIB_REGISTER_NODE (dhcp6_client_cp_process_node) = {
496 .function = dhcp6_client_cp_process,
497 .type = VLIB_NODE_TYPE_PROCESS,
498 .name = "dhcp6-client-cp-process",
503 interrupt_process (void)
505 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
506 vlib_main_t *vm = rm->vlib_main;
508 vlib_process_signal_event (vm, dhcp6_client_cp_process_node.index,
509 RD_CP_EVENT_INTERRUPT, 0);
513 disable_process (void)
515 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
516 vlib_main_t *vm = rm->vlib_main;
518 vlib_process_signal_event (vm, dhcp6_client_cp_process_node.index,
519 RD_CP_EVENT_DISABLE, 0);
523 enable_process (void)
525 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
526 vlib_main_t *vm = rm->vlib_main;
529 node = vec_elt (vm->node_main.nodes, rm->node_index);
531 vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_POLLING);
532 vlib_start_process (vm, node->runtime_index);
535 static clib_error_t *
536 dhcp6_addresses_show_command_function (vlib_main_t * vm,
537 unformat_input_t * input,
538 vlib_cli_command_t * cmd)
540 dhcp6_client_cp_main_t *dm = &dhcp6_client_cp_main;
541 clib_error_t *error = 0;
542 address_info_t *address_info;
543 f64 current_time = vlib_time_now (vm);
546 pool_foreach (address_info, dm->address_pool,
548 vlib_cli_output (vm, "address: %U, "
549 "preferred lifetime: %u, valid lifetime: %u "
551 format_ip6_address, &address_info->address,
552 address_info->preferred_lt, address_info->valid_lt,
553 address_info->due_time - current_time);
561 VLIB_CLI_COMMAND (dhcp6_addresses_show_command, static) = {
562 .path = "show dhcp6 addresses",
563 .short_help = "show dhcp6 addresses",
564 .function = dhcp6_addresses_show_command_function,
568 static clib_error_t *
569 dhcp6_clients_show_command_function (vlib_main_t * vm,
570 unformat_input_t * input,
571 vlib_cli_command_t * cmd)
573 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
574 clib_error_t *error = 0;
576 f64 current_time = vlib_time_now (vm);
579 const char *rebinding;
582 for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++)
584 cs = &rm->client_state_by_sw_if_index[i];
587 if (cs->T1_due_time != DBL_MAX && cs->T1_due_time > current_time)
589 sprintf (buf1, "%u remaining",
590 (u32) round (cs->T1_due_time - current_time));
593 sprintf (buf1, "timeout");
594 if (cs->T2_due_time != DBL_MAX && cs->T2_due_time > current_time)
595 sprintf (buf2, "%u remaining",
596 (u32) round (cs->T2_due_time - current_time));
598 sprintf (buf2, "timeout");
600 rebinding = ", REBINDING";
605 "sw_if_index: %u, T1: %u (%s), "
606 "T2: %u (%s), server index: %d%s", i,
607 cs->T1, buf1, cs->T2, buf2,
608 cs->server_index, rebinding);
610 vlib_cli_output (vm, "sw_if_index: %u%s", i, rebinding);
618 VLIB_CLI_COMMAND (dhcp6_clients_show_command, static) = {
619 .path = "show dhcp6 clients",
620 .short_help = "show dhcp6 clients",
621 .function = dhcp6_clients_show_command_function,
626 dhcp6_client_enable_disable (u32 sw_if_index, u8 enable)
628 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
629 vnet_main_t *vnm = rm->vnet_main;
630 vlib_main_t *vm = rm->vlib_main;
631 client_state_t *client_state;
632 client_state_t empty_config = { 0 };
633 address_info_t *address_info;
636 if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index))
638 clib_warning ("Invalid sw_if_index");
642 vec_validate_init_empty (rm->client_state_by_sw_if_index, sw_if_index,
644 client_state = &rm->client_state_by_sw_if_index[sw_if_index];
646 u8 old_enabled = client_state->enabled;
648 client_state->enabled = 1;
649 client_state->server_index = ~0;
651 if (!old_enabled && enable)
654 if (rm->n_clients == 1)
657 dhcp6_clients_enable_disable (1);
660 ip6_enable (sw_if_index);
661 send_client_message_start_stop (sw_if_index, ~0, DHCPV6_MSG_SOLICIT,
664 else if (old_enabled && !enable)
666 send_client_message_start_stop (sw_if_index, ~0, ~0, 0, 0);
669 if (rm->n_clients == 0)
671 dhcp6_clients_enable_disable (0);
676 pool_foreach (address_info, rm->address_pool,
678 if (address_info->sw_if_index == sw_if_index)
680 ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) &&
681 rm->client_state_by_sw_if_index[sw_if_index].enabled);
682 client_state_t *client_state =
683 &rm->client_state_by_sw_if_index[sw_if_index];
684 send_client_message_start_stop (sw_if_index,
685 client_state->server_index,
686 DHCPV6_MSG_RELEASE, address_info,
688 error = ip6_add_del_interface_address (vm, sw_if_index,
689 &address_info->address,
692 clib_warning ("Failed to delete interface address");
693 pool_put (rm->address_pool, address_info);
700 client_state->enabled = 0;
705 static clib_error_t *
706 dhcp6_client_enable_disable_command_fn (vlib_main_t * vm,
707 unformat_input_t * input,
708 vlib_cli_command_t * cmd)
710 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
711 vnet_main_t *vnm = rm->vnet_main;
712 clib_error_t *error = 0;
713 u32 sw_if_index = ~0;
716 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
719 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
721 else if (unformat (input, "disable"))
725 error = clib_error_return (0, "unexpected input `%U'",
726 format_unformat_error, input);
731 if (sw_if_index != ~0)
733 if (dhcp6_client_enable_disable (sw_if_index, enable) != 0)
734 error = clib_error_return (0, "Invalid sw_if_index");
737 error = clib_error_return (0, "Missing sw_if_index");
744 * This command is used to enable/disable DHCPv6 client
745 * on particular interface.
749 * Example of how to enable DHCPv6 client:
750 * @cliexcmd{dhcp6 client GigabitEthernet2/0/0}
751 * Example of how to disable DHCPv6 client:
752 * @cliexcmd{dhcp6 client GigabitEthernet2/0/0 disable}
756 VLIB_CLI_COMMAND (dhcp6_client_enable_disable_command, static) = {
757 .path = "dhcp6 client",
758 .short_help = "dhcp6 client <interface> [disable]",
759 .function = dhcp6_client_enable_disable_command_fn,
764 vl_api_dhcp6_client_enable_disable_t_handler
765 (vl_api_dhcp6_client_enable_disable_t * mp)
767 vl_api_dhcp6_client_enable_disable_reply_t *rmp;
771 VALIDATE_SW_IF_INDEX (mp);
773 sw_if_index = ntohl (mp->sw_if_index);
775 rv = dhcp6_client_enable_disable (sw_if_index, mp->enable);
777 BAD_SW_IF_INDEX_LABEL;
779 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
782 #define vl_msg_name_crc_list
783 #include <vnet/dhcp/dhcp6_ia_na_client_cp.api.h>
784 #undef vl_msg_name_crc_list
787 setup_message_id_table (api_main_t * am)
789 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
790 foreach_vl_msg_name_crc_dhcp6_ia_na_client_cp;
794 static clib_error_t *
795 dhcp_client_cp_init (vlib_main_t * vm)
797 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
798 api_main_t *am = &api_main;
801 rm->vnet_main = vnet_get_main ();
803 rm->node_index = dhcp6_client_cp_process_node.index;
806 vl_msg_api_set_handlers(VL_API_##N, #n, \
807 vl_api_##n##_t_handler, \
809 vl_api_##n##_t_endian, \
810 vl_api_##n##_t_print, \
811 sizeof(vl_api_##n##_t), 0/* do NOT trace! */);
812 foreach_dhcp6_client_cp_msg;
816 * Set up the (msg_name, crc, message-id) table
818 setup_message_id_table (am);
823 VLIB_INIT_FUNCTION (dhcp_client_cp_init);
826 * fd.io coding-style-patch-verification: ON
829 * eval: (c-set-style "gnu")