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 <dhcp/dhcp6_packet.h>
19 #include <dhcp/dhcp6_ia_na_client_dp.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/ip/ip6.h>
22 #include <vnet/ip/ip6_link.h>
29 ip6_address_t address;
49 address_info_t *address_pool;
50 client_state_t *client_state_by_sw_if_index;
52 f64 max_valid_due_time;
55 vlib_main_t *vlib_main;
56 vnet_main_t *vnet_main;
59 } dhcp6_client_cp_main_t;
61 static dhcp6_client_cp_main_t dhcp6_client_cp_main;
65 RD_CP_EVENT_INTERRUPT,
70 send_client_message_start_stop (u32 sw_if_index, u32 server_index,
71 u8 msg_type, address_info_t * address_list,
74 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
75 dhcp6_send_client_message_params_t params = { 0, };
76 dhcp6_send_client_message_params_address_t *addresses = 0, *addr;
79 ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) &&
80 rm->client_state_by_sw_if_index[sw_if_index].enabled);
81 client_state_t *client_state =
82 &rm->client_state_by_sw_if_index[sw_if_index];
84 params.sw_if_index = sw_if_index;
85 params.server_index = server_index;
86 params.msg_type = msg_type;
89 if (msg_type == DHCPV6_MSG_SOLICIT)
94 else if (msg_type == DHCPV6_MSG_REQUEST)
100 else if (msg_type == DHCPV6_MSG_RENEW)
104 f64 current_time = vlib_time_now (rm->vlib_main);
105 i32 diff_time = client_state->T2 - current_time;
108 params.mrd = diff_time;
110 else if (msg_type == DHCPV6_MSG_REBIND)
114 f64 current_time = vlib_time_now (rm->vlib_main);
115 i32 diff_time = rm->max_valid_due_time - current_time;
118 params.mrd = diff_time;
120 else if (msg_type == DHCPV6_MSG_RELEASE)
128 if (vec_len (address_list) != 0)
129 vec_validate (addresses, vec_len (address_list) - 1);
130 for (i = 0; i < vec_len (address_list); i++)
132 address_info_t *address = &address_list[i];
133 addr = &addresses[i];
134 addr->valid_lt = address->valid_lt;
135 addr->preferred_lt = address->preferred_lt;
136 addr->address = address->address;
138 params.addresses = addresses;
140 dhcp6_send_client_message (rm->vlib_main, sw_if_index, !start, ¶ms);
142 vec_free (params.addresses);
145 static void interrupt_process (void);
148 ip6_addresses_equal (ip6_address_t * address1, ip6_address_t * address2)
150 if (address1->as_u64[0] != address2->as_u64[0])
152 return address1->as_u64[1] == address2->as_u64[1];
155 static clib_error_t *
156 dhcp6_reply_event_handler (vl_api_dhcp6_reply_event_t * mp)
158 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
159 vlib_main_t *vm = rm->vlib_main;
160 client_state_t *client_state;
161 ip6_address_t *address;
164 vl_api_dhcp6_address_info_t *api_address;
165 u32 inner_status_code;
169 clib_error_t *error = 0;
172 current_time = vlib_time_now (vm);
174 sw_if_index = ntohl (mp->sw_if_index);
176 if (sw_if_index >= vec_len (rm->client_state_by_sw_if_index))
179 client_state = &rm->client_state_by_sw_if_index[sw_if_index];
181 if (!client_state->enabled)
184 server_index = ntohl (mp->server_index);
186 n_addresses = ntohl (mp->n_addresses);
188 inner_status_code = ntohs (mp->inner_status_code);
189 status_code = ntohs (mp->status_code);
191 if (mp->msg_type == DHCPV6_MSG_API_ADVERTISE
192 && client_state->server_index == ~0)
194 address_info_t *address_list = 0, *address_info;
196 if (inner_status_code == DHCPV6_STATUS_NOADDRS_AVAIL)
199 ("Advertise message arrived with NoAddrsAvail status code");
204 vec_validate (address_list, n_addresses - 1);
205 for (i = 0; i < n_addresses; i++)
207 api_address = &mp->addresses[i];
208 address = (ip6_address_t *) api_address->address;
210 address_info = &address_list[i];
211 address_info->address = *address;
212 address_info->preferred_lt = 0;
213 address_info->valid_lt = 0;
216 client_state->server_index = server_index;
218 send_client_message_start_stop (sw_if_index, server_index,
219 DHCPV6_MSG_REQUEST, address_list, 1);
220 vec_free (address_list);
223 if (mp->msg_type != DHCPV6_MSG_API_REPLY)
226 if (!client_state->rebinding && client_state->server_index != server_index)
228 clib_warning ("Reply message arrived with Server ID different "
229 "from that in Request or Renew message");
233 if (inner_status_code == DHCPV6_STATUS_NOADDRS_AVAIL)
235 clib_warning ("Reply message arrived with NoAddrsAvail status code");
239 ("Invalid Reply message arrived: It contains NoAddrsAvail "
240 "status code but also contains addresses");
245 if (status_code == DHCPV6_STATUS_UNSPEC_FAIL)
247 clib_warning ("Reply message arrived with UnspecFail status code");
251 send_client_message_start_stop (sw_if_index, server_index,
254 for (i = 0; i < n_addresses; i++)
256 address_info_t *address_info = 0;
260 api_address = &mp->addresses[i];
262 address = (ip6_address_t *) api_address->address;
264 if (ip6_address_is_link_local_unicast (address))
267 valid_time = ntohl (api_address->valid_time);
268 preferred_time = ntohl (api_address->preferred_time);
270 if (preferred_time > valid_time)
273 u8 address_already_present = 0;
274 pool_foreach (address_info, rm->address_pool)
276 if (address_info->sw_if_index != sw_if_index)
278 else if (!ip6_addresses_equal (&address_info->address, address))
282 address_already_present = 1;
283 goto address_pool_foreach_out;
286 address_pool_foreach_out:
288 if (address_already_present)
290 address_info->preferred_lt = preferred_time;
291 address_info->valid_lt = valid_time;
292 address_info->due_time = current_time;
293 /* Renew the lease at the preferred time, if non-zero */
294 address_info->due_time += (preferred_time > 0) ?
295 preferred_time : valid_time;
297 if (address_info->due_time > rm->max_valid_due_time)
298 rm->max_valid_due_time = address_info->due_time;
305 pool_get (rm->address_pool, address_info);
306 address_info->sw_if_index = sw_if_index;
307 address_info->address = *address;
308 address_info->preferred_lt = preferred_time;
309 address_info->valid_lt = valid_time;
310 address_info->due_time = current_time;
311 /* Renew the lease at the preferred time, if non-zero */
312 address_info->due_time += (preferred_time > 0) ?
313 preferred_time : valid_time;
315 if (address_info->due_time > rm->max_valid_due_time)
316 rm->max_valid_due_time = address_info->due_time;
317 rm->client_state_by_sw_if_index[sw_if_index].address_count++;
319 error = ip6_add_del_interface_address (vm, sw_if_index,
320 &address_info->address, 64, 0);
322 clib_warning ("Failed to add interface address");
325 client_state->server_index = server_index;
326 client_state->T1 = ntohl (mp->T1);
327 client_state->T2 = ntohl (mp->T2);
328 if (client_state->T1 != 0)
329 client_state->T1_due_time = current_time + client_state->T1;
330 if (client_state->T2 != 0)
331 client_state->T2_due_time = current_time + client_state->T2;
332 client_state->rebinding = 0;
334 interrupt_process ();
339 static address_info_t *
340 create_address_list (u32 sw_if_index)
342 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
343 address_info_t *address_info, *address_list = 0;;
345 pool_foreach (address_info, rm->address_pool)
347 if (address_info->sw_if_index == sw_if_index)
349 u32 pos = vec_len (address_list);
350 vec_validate (address_list, pos);
351 clib_memcpy (&address_list[pos], address_info, sizeof (*address_info));
358 VNET_DHCP6_REPLY_EVENT_FUNCTION (dhcp6_reply_event_handler);
361 dhcp6_client_cp_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
364 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
365 address_info_t *address_info;
366 client_state_t *client_state;
367 f64 sleep_time = 1e9;
372 uword *event_data = 0;
377 vlib_process_wait_for_event_or_clock (vm, sleep_time);
378 event_type = vlib_process_get_events (vm, &event_data);
379 vec_reset_length (event_data);
381 if (event_type == RD_CP_EVENT_DISABLE)
383 vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_DISABLED);
388 current_time = vlib_time_now (vm);
391 due_time = current_time + 1e9;
392 pool_foreach (address_info, rm->address_pool)
394 if (address_info->due_time > current_time)
396 if (address_info->due_time < due_time)
397 due_time = address_info->due_time;
401 u32 sw_if_index = address_info->sw_if_index;
402 error = ip6_add_del_interface_address (vm, sw_if_index,
403 &address_info->address,
406 clib_warning ("Failed to delete interface address");
407 pool_put (rm->address_pool, address_info);
408 /* make sure ip6 stays enabled */
409 ip6_link_enable (sw_if_index, NULL);
410 client_state = &rm->client_state_by_sw_if_index[sw_if_index];
411 if (--client_state->address_count == 0)
413 client_state->rebinding = 0;
414 client_state->server_index = ~0;
415 send_client_message_start_stop (sw_if_index, ~0,
421 for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++)
423 client_state_t *cs = &rm->client_state_by_sw_if_index[i];
424 if (cs->enabled && cs->server_index != ~0)
426 if (cs->T2_due_time > current_time)
428 if (cs->T2_due_time < due_time)
429 due_time = cs->T2_due_time;
430 if (cs->T1_due_time > current_time)
432 if (cs->T1_due_time < due_time)
433 due_time = cs->T1_due_time;
437 cs->T1_due_time = DBL_MAX;
438 address_info_t *address_list;
439 address_list = create_address_list (i);
441 send_client_message_start_stop (i, cs->server_index,
444 vec_free (address_list);
449 cs->T2_due_time = DBL_MAX;
450 address_info_t *address_list;
451 address_list = create_address_list (i);
453 send_client_message_start_stop (i, ~0,
456 vec_free (address_list);
460 current_time = vlib_time_now (vm);
462 while (due_time < current_time);
464 sleep_time = due_time - current_time;
470 VLIB_REGISTER_NODE (dhcp6_client_cp_process_node) = {
471 .function = dhcp6_client_cp_process,
472 .type = VLIB_NODE_TYPE_PROCESS,
473 .name = "dhcp6-client-cp-process",
477 interrupt_process (void)
479 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
480 vlib_main_t *vm = rm->vlib_main;
482 vlib_process_signal_event (vm, dhcp6_client_cp_process_node.index,
483 RD_CP_EVENT_INTERRUPT, 0);
487 disable_process (void)
489 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
490 vlib_main_t *vm = rm->vlib_main;
492 vlib_process_signal_event (vm, dhcp6_client_cp_process_node.index,
493 RD_CP_EVENT_DISABLE, 0);
497 enable_process (void)
499 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
500 vlib_main_t *vm = rm->vlib_main;
503 node = vec_elt (vm->node_main.nodes, rm->node_index);
505 vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_POLLING);
506 vlib_start_process (vm, node->runtime_index);
509 static clib_error_t *
510 dhcp6_addresses_show_command_function (vlib_main_t * vm,
511 unformat_input_t * input,
512 vlib_cli_command_t * cmd)
514 dhcp6_client_cp_main_t *dm = &dhcp6_client_cp_main;
515 clib_error_t *error = 0;
516 address_info_t *address_info;
517 f64 current_time = vlib_time_now (vm);
519 pool_foreach (address_info, dm->address_pool)
521 vlib_cli_output (vm, "address: %U, "
522 "preferred lifetime: %u, valid lifetime: %u "
524 format_ip6_address, &address_info->address,
525 address_info->preferred_lt, address_info->valid_lt,
526 address_info->due_time - current_time);
532 VLIB_CLI_COMMAND (dhcp6_addresses_show_command, static) = {
533 .path = "show dhcp6 addresses",
534 .short_help = "show dhcp6 addresses",
535 .function = dhcp6_addresses_show_command_function,
538 static clib_error_t *
539 dhcp6_clients_show_command_function (vlib_main_t * vm,
540 unformat_input_t * input,
541 vlib_cli_command_t * cmd)
543 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
544 clib_error_t *error = 0;
546 f64 current_time = vlib_time_now (vm);
549 const char *rebinding;
552 for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++)
554 cs = &rm->client_state_by_sw_if_index[i];
557 vec_reset_length (buf1);
558 vec_reset_length (buf2);
559 if (cs->T1_due_time != DBL_MAX && cs->T1_due_time > current_time)
561 buf1 = format (buf1, "%u remaining",
562 (u32) round (cs->T1_due_time - current_time));
565 buf1 = format (buf1, "timeout");
566 if (cs->T2_due_time != DBL_MAX && cs->T2_due_time > current_time)
567 buf2 = format (buf2, "%u remaining",
568 (u32) round (cs->T2_due_time - current_time));
570 buf2 = format (buf2, "timeout");
572 rebinding = ", REBINDING";
577 "sw_if_index: %u, T1: %u (%v), "
578 "T2: %u (%v), server index: %d%s", i,
579 cs->T1, buf1, cs->T2, buf2,
580 cs->server_index, rebinding);
582 vlib_cli_output (vm, "sw_if_index: %u%s", i, rebinding);
592 VLIB_CLI_COMMAND (dhcp6_clients_show_command, static) = {
593 .path = "show dhcp6 clients",
594 .short_help = "show dhcp6 clients",
595 .function = dhcp6_clients_show_command_function,
599 dhcp6_client_enable_disable (u32 sw_if_index, u8 enable)
601 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
602 vnet_main_t *vnm = rm->vnet_main;
603 vlib_main_t *vm = rm->vlib_main;
604 client_state_t *client_state;
605 client_state_t empty_config = { 0 };
606 address_info_t *address_info;
609 if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index))
611 clib_warning ("Invalid sw_if_index");
615 vec_validate_init_empty (rm->client_state_by_sw_if_index, sw_if_index,
617 client_state = &rm->client_state_by_sw_if_index[sw_if_index];
619 u8 old_enabled = client_state->enabled;
621 client_state->enabled = 1;
622 client_state->server_index = ~0;
624 if (!old_enabled && enable)
627 if (rm->n_clients == 1)
630 dhcp6_clients_enable_disable (1);
633 ip6_link_enable (sw_if_index, NULL);
634 send_client_message_start_stop (sw_if_index, ~0, DHCPV6_MSG_SOLICIT,
637 else if (old_enabled && !enable)
639 send_client_message_start_stop (sw_if_index, ~0, ~0, 0, 0);
642 if (rm->n_clients == 0)
644 dhcp6_clients_enable_disable (0);
648 pool_foreach (address_info, rm->address_pool)
650 if (address_info->sw_if_index == sw_if_index)
652 ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) &&
653 rm->client_state_by_sw_if_index[sw_if_index].enabled);
654 client_state_t *client_state =
655 &rm->client_state_by_sw_if_index[sw_if_index];
656 send_client_message_start_stop (sw_if_index,
657 client_state->server_index,
658 DHCPV6_MSG_RELEASE, address_info,
660 error = ip6_add_del_interface_address (vm, sw_if_index,
661 &address_info->address,
664 clib_warning ("Failed to delete interface address");
665 pool_put (rm->address_pool, address_info);
671 client_state->enabled = 0;
676 static clib_error_t *
677 dhcp6_client_enable_disable_command_fn (vlib_main_t * vm,
678 unformat_input_t * input,
679 vlib_cli_command_t * cmd)
681 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
682 vnet_main_t *vnm = rm->vnet_main;
683 clib_error_t *error = 0;
684 u32 sw_if_index = ~0;
686 unformat_input_t _line_input, *line_input = &_line_input;
688 if (!unformat_user (input, unformat_line_input, line_input))
691 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
694 (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
696 else if (unformat (line_input, "disable"))
700 error = clib_error_return (0, "unexpected input `%U'",
701 format_unformat_error, line_input);
706 unformat_free (line_input);
708 if (sw_if_index != ~0)
710 if (dhcp6_client_enable_disable (sw_if_index, enable) != 0)
711 error = clib_error_return (0, "Invalid sw_if_index");
714 error = clib_error_return (0, "Missing sw_if_index");
721 * This command is used to enable/disable DHCPv6 client
722 * on particular interface.
726 * Example of how to enable DHCPv6 client:
727 * @cliexcmd{dhcp6 client GigabitEthernet2/0/0}
728 * Example of how to disable DHCPv6 client:
729 * @cliexcmd{dhcp6 client GigabitEthernet2/0/0 disable}
732 VLIB_CLI_COMMAND (dhcp6_client_enable_disable_command, static) = {
733 .path = "dhcp6 client",
734 .short_help = "dhcp6 client <interface> [disable]",
735 .function = dhcp6_client_enable_disable_command_fn,
738 static clib_error_t *
739 dhcp_ia_na_client_cp_init (vlib_main_t * vm)
741 dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
744 rm->vnet_main = vnet_get_main ();
745 rm->api_main = vlibapi_get_main ();
746 rm->node_index = dhcp6_client_cp_process_node.index;
751 VLIB_INIT_FUNCTION (dhcp_ia_na_client_cp_init);
754 * fd.io coding-style-patch-verification: ON
757 * eval: (c-set-style "gnu")