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 <dhcp/dhcp6_packet.h>
20 #include <dhcp/dhcp6_ia_na_client_dp.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ip/ip6.h>
23 #include <vnet/ip/ip6_link.h>
30 ip6_address_t address;
50 address_info_t *address_pool;
51 client_state_t *client_state_by_sw_if_index;
53 f64 max_valid_due_time;
56 vlib_main_t *vlib_main;
57 vnet_main_t *vnet_main;
60 } dhcp6_client_cp_main_t;
62 static dhcp6_client_cp_main_t dhcp6_client_cp_main;
66 RD_CP_EVENT_INTERRUPT,
71 send_client_message_start_stop (u32 sw_if_index, u32 server_index,
72 u8 msg_type, address_info_t * address_list,
75 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
76 dhcp6_send_client_message_params_t params = { 0, };
77 dhcp6_send_client_message_params_address_t *addresses = 0, *addr;
80 ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) &&
81 rm->client_state_by_sw_if_index[sw_if_index].enabled);
82 client_state_t *client_state =
83 &rm->client_state_by_sw_if_index[sw_if_index];
85 params.sw_if_index = sw_if_index;
86 params.server_index = server_index;
87 params.msg_type = msg_type;
90 if (msg_type == DHCPV6_MSG_SOLICIT)
95 else if (msg_type == DHCPV6_MSG_REQUEST)
101 else if (msg_type == DHCPV6_MSG_RENEW)
105 f64 current_time = vlib_time_now (rm->vlib_main);
106 i32 diff_time = client_state->T2 - current_time;
109 params.mrd = diff_time;
111 else if (msg_type == DHCPV6_MSG_REBIND)
115 f64 current_time = vlib_time_now (rm->vlib_main);
116 i32 diff_time = rm->max_valid_due_time - current_time;
119 params.mrd = diff_time;
121 else if (msg_type == DHCPV6_MSG_RELEASE)
129 if (vec_len (address_list) != 0)
130 vec_validate (addresses, vec_len (address_list) - 1);
131 for (i = 0; i < vec_len (address_list); i++)
133 address_info_t *address = &address_list[i];
134 addr = &addresses[i];
135 addr->valid_lt = address->valid_lt;
136 addr->preferred_lt = address->preferred_lt;
137 addr->address = address->address;
139 params.addresses = addresses;
141 dhcp6_send_client_message (rm->vlib_main, sw_if_index, !start, ¶ms);
143 vec_free (params.addresses);
146 static void interrupt_process (void);
149 ip6_addresses_equal (ip6_address_t * address1, ip6_address_t * address2)
151 if (address1->as_u64[0] != address2->as_u64[0])
153 return address1->as_u64[1] == address2->as_u64[1];
156 static clib_error_t *
157 dhcp6_reply_event_handler (vl_api_dhcp6_reply_event_t * mp)
159 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
160 vlib_main_t *vm = rm->vlib_main;
161 client_state_t *client_state;
162 ip6_address_t *address;
165 vl_api_dhcp6_address_info_t *api_address;
166 u32 inner_status_code;
170 clib_error_t *error = 0;
173 current_time = vlib_time_now (vm);
175 sw_if_index = ntohl (mp->sw_if_index);
177 if (sw_if_index >= vec_len (rm->client_state_by_sw_if_index))
180 client_state = &rm->client_state_by_sw_if_index[sw_if_index];
182 if (!client_state->enabled)
185 server_index = ntohl (mp->server_index);
187 n_addresses = ntohl (mp->n_addresses);
189 inner_status_code = ntohs (mp->inner_status_code);
190 status_code = ntohs (mp->status_code);
192 if (mp->msg_type == DHCPV6_MSG_API_ADVERTISE
193 && client_state->server_index == ~0)
195 address_info_t *address_list = 0, *address_info;
197 if (inner_status_code == DHCPV6_STATUS_NOADDRS_AVAIL)
200 ("Advertise message arrived with NoAddrsAvail status code");
205 vec_validate (address_list, n_addresses - 1);
206 for (i = 0; i < n_addresses; i++)
208 api_address = &mp->addresses[i];
209 address = (ip6_address_t *) api_address->address;
211 address_info = &address_list[i];
212 address_info->address = *address;
213 address_info->preferred_lt = 0;
214 address_info->valid_lt = 0;
217 client_state->server_index = server_index;
219 send_client_message_start_stop (sw_if_index, server_index,
220 DHCPV6_MSG_REQUEST, address_list, 1);
221 vec_free (address_list);
224 if (mp->msg_type != DHCPV6_MSG_API_REPLY)
227 if (!client_state->rebinding && client_state->server_index != server_index)
229 clib_warning ("Reply message arrived with Server ID different "
230 "from that in Request or Renew message");
234 if (inner_status_code == DHCPV6_STATUS_NOADDRS_AVAIL)
236 clib_warning ("Reply message arrived with NoAddrsAvail status code");
240 ("Invalid Reply message arrived: It contains NoAddrsAvail "
241 "status code but also contains addresses");
246 if (status_code == DHCPV6_STATUS_UNSPEC_FAIL)
248 clib_warning ("Reply message arrived with UnspecFail status code");
252 send_client_message_start_stop (sw_if_index, server_index,
255 for (i = 0; i < n_addresses; i++)
257 address_info_t *address_info = 0;
261 api_address = &mp->addresses[i];
263 address = (ip6_address_t *) api_address->address;
265 if (ip6_address_is_link_local_unicast (address))
268 valid_time = ntohl (api_address->valid_time);
269 preferred_time = ntohl (api_address->preferred_time);
271 if (preferred_time > valid_time)
274 u8 address_already_present = 0;
276 pool_foreach (address_info, rm->address_pool,
278 if (address_info->sw_if_index != sw_if_index)
280 else if (!ip6_addresses_equal (&address_info->address, address))
284 address_already_present = 1;
285 goto address_pool_foreach_out;
289 address_pool_foreach_out:
291 if (address_already_present)
293 address_info->preferred_lt = preferred_time;
294 address_info->valid_lt = valid_time;
295 address_info->due_time = current_time;
296 /* Renew the lease at the preferred time, if non-zero */
297 address_info->due_time += (preferred_time > 0) ?
298 preferred_time : valid_time;
300 if (address_info->due_time > rm->max_valid_due_time)
301 rm->max_valid_due_time = address_info->due_time;
308 pool_get (rm->address_pool, address_info);
309 address_info->sw_if_index = sw_if_index;
310 address_info->address = *address;
311 address_info->preferred_lt = preferred_time;
312 address_info->valid_lt = valid_time;
313 address_info->due_time = current_time;
314 /* Renew the lease at the preferred time, if non-zero */
315 address_info->due_time += (preferred_time > 0) ?
316 preferred_time : valid_time;
318 if (address_info->due_time > rm->max_valid_due_time)
319 rm->max_valid_due_time = address_info->due_time;
320 rm->client_state_by_sw_if_index[sw_if_index].address_count++;
322 error = ip6_add_del_interface_address (vm, sw_if_index,
323 &address_info->address, 64, 0);
325 clib_warning ("Failed to add interface address");
328 client_state->server_index = server_index;
329 client_state->T1 = ntohl (mp->T1);
330 client_state->T2 = ntohl (mp->T2);
331 if (client_state->T1 != 0)
332 client_state->T1_due_time = current_time + client_state->T1;
333 if (client_state->T2 != 0)
334 client_state->T2_due_time = current_time + client_state->T2;
335 client_state->rebinding = 0;
337 interrupt_process ();
342 static address_info_t *
343 create_address_list (u32 sw_if_index)
345 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
346 address_info_t *address_info, *address_list = 0;;
349 pool_foreach (address_info, rm->address_pool,
351 if (address_info->sw_if_index == sw_if_index)
353 u32 pos = vec_len (address_list);
354 vec_validate (address_list, pos);
355 clib_memcpy (&address_list[pos], address_info, sizeof (*address_info));
363 VNET_DHCP6_REPLY_EVENT_FUNCTION (dhcp6_reply_event_handler);
366 dhcp6_client_cp_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
369 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
370 address_info_t *address_info;
371 client_state_t *client_state;
372 f64 sleep_time = 1e9;
377 uword *event_data = 0;
382 vlib_process_wait_for_event_or_clock (vm, sleep_time);
383 event_type = vlib_process_get_events (vm, &event_data);
384 vec_reset_length (event_data);
386 if (event_type == RD_CP_EVENT_DISABLE)
388 vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_DISABLED);
393 current_time = vlib_time_now (vm);
396 due_time = current_time + 1e9;
398 pool_foreach (address_info, rm->address_pool,
400 if (address_info->due_time > current_time)
402 if (address_info->due_time < due_time)
403 due_time = address_info->due_time;
407 u32 sw_if_index = address_info->sw_if_index;
408 error = ip6_add_del_interface_address (vm, sw_if_index,
409 &address_info->address,
412 clib_warning ("Failed to delete interface address");
413 pool_put (rm->address_pool, address_info);
414 /* make sure ip6 stays enabled */
415 ip6_link_enable (sw_if_index, NULL);
416 client_state = &rm->client_state_by_sw_if_index[sw_if_index];
417 if (--client_state->address_count == 0)
419 client_state->rebinding = 0;
420 client_state->server_index = ~0;
421 send_client_message_start_stop (sw_if_index, ~0,
428 for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++)
430 client_state_t *cs = &rm->client_state_by_sw_if_index[i];
431 if (cs->enabled && cs->server_index != ~0)
433 if (cs->T2_due_time > current_time)
435 if (cs->T2_due_time < due_time)
436 due_time = cs->T2_due_time;
437 if (cs->T1_due_time > current_time)
439 if (cs->T1_due_time < due_time)
440 due_time = cs->T1_due_time;
444 cs->T1_due_time = DBL_MAX;
445 address_info_t *address_list;
446 address_list = create_address_list (i);
448 send_client_message_start_stop (i, cs->server_index,
451 vec_free (address_list);
456 cs->T2_due_time = DBL_MAX;
457 address_info_t *address_list;
458 address_list = create_address_list (i);
460 send_client_message_start_stop (i, ~0,
463 vec_free (address_list);
467 current_time = vlib_time_now (vm);
469 while (due_time < current_time);
471 sleep_time = due_time - current_time;
478 VLIB_REGISTER_NODE (dhcp6_client_cp_process_node) = {
479 .function = dhcp6_client_cp_process,
480 .type = VLIB_NODE_TYPE_PROCESS,
481 .name = "dhcp6-client-cp-process",
486 interrupt_process (void)
488 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
489 vlib_main_t *vm = rm->vlib_main;
491 vlib_process_signal_event (vm, dhcp6_client_cp_process_node.index,
492 RD_CP_EVENT_INTERRUPT, 0);
496 disable_process (void)
498 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
499 vlib_main_t *vm = rm->vlib_main;
501 vlib_process_signal_event (vm, dhcp6_client_cp_process_node.index,
502 RD_CP_EVENT_DISABLE, 0);
506 enable_process (void)
508 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
509 vlib_main_t *vm = rm->vlib_main;
512 node = vec_elt (vm->node_main.nodes, rm->node_index);
514 vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_POLLING);
515 vlib_start_process (vm, node->runtime_index);
518 static clib_error_t *
519 dhcp6_addresses_show_command_function (vlib_main_t * vm,
520 unformat_input_t * input,
521 vlib_cli_command_t * cmd)
523 dhcp6_client_cp_main_t *dm = &dhcp6_client_cp_main;
524 clib_error_t *error = 0;
525 address_info_t *address_info;
526 f64 current_time = vlib_time_now (vm);
529 pool_foreach (address_info, dm->address_pool,
531 vlib_cli_output (vm, "address: %U, "
532 "preferred lifetime: %u, valid lifetime: %u "
534 format_ip6_address, &address_info->address,
535 address_info->preferred_lt, address_info->valid_lt,
536 address_info->due_time - current_time);
544 VLIB_CLI_COMMAND (dhcp6_addresses_show_command, static) = {
545 .path = "show dhcp6 addresses",
546 .short_help = "show dhcp6 addresses",
547 .function = dhcp6_addresses_show_command_function,
551 static clib_error_t *
552 dhcp6_clients_show_command_function (vlib_main_t * vm,
553 unformat_input_t * input,
554 vlib_cli_command_t * cmd)
556 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
557 clib_error_t *error = 0;
559 f64 current_time = vlib_time_now (vm);
562 const char *rebinding;
565 for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++)
567 cs = &rm->client_state_by_sw_if_index[i];
570 vec_reset_length (buf1);
571 vec_reset_length (buf2);
572 if (cs->T1_due_time != DBL_MAX && cs->T1_due_time > current_time)
574 buf1 = format (buf1, "%u remaining",
575 (u32) round (cs->T1_due_time - current_time));
578 buf1 = format (buf1, "timeout");
579 if (cs->T2_due_time != DBL_MAX && cs->T2_due_time > current_time)
580 buf2 = format (buf2, "%u remaining",
581 (u32) round (cs->T2_due_time - current_time));
583 buf2 = format (buf2, "timeout");
585 rebinding = ", REBINDING";
590 "sw_if_index: %u, T1: %u (%v), "
591 "T2: %u (%v), server index: %d%s", i,
592 cs->T1, buf1, cs->T2, buf2,
593 cs->server_index, rebinding);
595 vlib_cli_output (vm, "sw_if_index: %u%s", i, rebinding);
606 VLIB_CLI_COMMAND (dhcp6_clients_show_command, static) = {
607 .path = "show dhcp6 clients",
608 .short_help = "show dhcp6 clients",
609 .function = dhcp6_clients_show_command_function,
614 dhcp6_client_enable_disable (u32 sw_if_index, u8 enable)
616 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
617 vnet_main_t *vnm = rm->vnet_main;
618 vlib_main_t *vm = rm->vlib_main;
619 client_state_t *client_state;
620 client_state_t empty_config = { 0 };
621 address_info_t *address_info;
624 if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index))
626 clib_warning ("Invalid sw_if_index");
630 vec_validate_init_empty (rm->client_state_by_sw_if_index, sw_if_index,
632 client_state = &rm->client_state_by_sw_if_index[sw_if_index];
634 u8 old_enabled = client_state->enabled;
636 client_state->enabled = 1;
637 client_state->server_index = ~0;
639 if (!old_enabled && enable)
642 if (rm->n_clients == 1)
645 dhcp6_clients_enable_disable (1);
648 ip6_link_enable (sw_if_index, NULL);
649 send_client_message_start_stop (sw_if_index, ~0, DHCPV6_MSG_SOLICIT,
652 else if (old_enabled && !enable)
654 send_client_message_start_stop (sw_if_index, ~0, ~0, 0, 0);
657 if (rm->n_clients == 0)
659 dhcp6_clients_enable_disable (0);
664 pool_foreach (address_info, rm->address_pool,
666 if (address_info->sw_if_index == sw_if_index)
668 ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) &&
669 rm->client_state_by_sw_if_index[sw_if_index].enabled);
670 client_state_t *client_state =
671 &rm->client_state_by_sw_if_index[sw_if_index];
672 send_client_message_start_stop (sw_if_index,
673 client_state->server_index,
674 DHCPV6_MSG_RELEASE, address_info,
676 error = ip6_add_del_interface_address (vm, sw_if_index,
677 &address_info->address,
680 clib_warning ("Failed to delete interface address");
681 pool_put (rm->address_pool, address_info);
688 client_state->enabled = 0;
693 static clib_error_t *
694 dhcp6_client_enable_disable_command_fn (vlib_main_t * vm,
695 unformat_input_t * input,
696 vlib_cli_command_t * cmd)
698 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
699 vnet_main_t *vnm = rm->vnet_main;
700 clib_error_t *error = 0;
701 u32 sw_if_index = ~0;
703 unformat_input_t _line_input, *line_input = &_line_input;
705 if (!unformat_user (input, unformat_line_input, line_input))
708 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
711 (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
713 else if (unformat (line_input, "disable"))
717 error = clib_error_return (0, "unexpected input `%U'",
718 format_unformat_error, line_input);
723 unformat_free (line_input);
725 if (sw_if_index != ~0)
727 if (dhcp6_client_enable_disable (sw_if_index, enable) != 0)
728 error = clib_error_return (0, "Invalid sw_if_index");
731 error = clib_error_return (0, "Missing sw_if_index");
738 * This command is used to enable/disable DHCPv6 client
739 * on particular interface.
743 * Example of how to enable DHCPv6 client:
744 * @cliexcmd{dhcp6 client GigabitEthernet2/0/0}
745 * Example of how to disable DHCPv6 client:
746 * @cliexcmd{dhcp6 client GigabitEthernet2/0/0 disable}
750 VLIB_CLI_COMMAND (dhcp6_client_enable_disable_command, static) = {
751 .path = "dhcp6 client",
752 .short_help = "dhcp6 client <interface> [disable]",
753 .function = dhcp6_client_enable_disable_command_fn,
757 static clib_error_t *
758 dhcp_ia_na_client_cp_init (vlib_main_t * vm)
760 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
763 rm->vnet_main = vnet_get_main ();
764 rm->api_main = vlibapi_get_main ();
765 rm->node_index = dhcp6_client_cp_process_node.index;
770 VLIB_INIT_FUNCTION (dhcp_ia_na_client_cp_init);
773 * fd.io coding-style-patch-verification: ON
776 * eval: (c-set-style "gnu")