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/ip6-nd/ip6_ra.h>
18 #include <vnet/ip/ip6.h>
19 #include <vnet/ip/ip6_link.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/fib/fib_table.h>
29 ip6_address_t address;
36 ip6_address_t router_address;
43 u8 install_default_routes;
51 interface_config_t *config_by_sw_if_index;
52 slaac_address_t *slaac_address_pool;
53 default_route_t *default_route_pool;
55 /* binary API client */
60 vlib_log_class_t log_class;
63 vlib_main_t *vlib_main;
64 vnet_main_t *vnet_main;
68 rd_cp_main_t rd_cp_main;
72 RD_CP_EVENT_INTERRUPT,
75 #define vl_api_ip6_nd_address_autoconfig_t_print vl_noop_handler
78 router_solicitation_start_stop (u32 sw_if_index, u8 start)
80 rd_cp_main_t *rm = &rd_cp_main;
81 icmp6_send_router_solicitation_params_t params = { 0, };
89 icmp6_send_router_solicitation (rm->vlib_main, sw_if_index, !start,
93 static void interrupt_process (void);
96 add_slaac_address (vlib_main_t * vm, u32 sw_if_index, u8 address_length,
97 const ip6_address_t * address, f64 due_time)
99 rd_cp_main_t *rm = &rd_cp_main;
100 slaac_address_t *slaac_address;
101 clib_error_t *rv = 0;
103 pool_get_zero (rm->slaac_address_pool, slaac_address);
105 slaac_address->sw_if_index = sw_if_index;
106 slaac_address->address_length = address_length;
107 slaac_address->address = *address;
108 slaac_address->due_time = due_time;
111 ip6_add_del_interface_address (vm, sw_if_index, &slaac_address->address,
118 add_default_route (vlib_main_t * vm, u32 sw_if_index,
119 const ip6_address_t * next_hop_address, f64 due_time)
121 rd_cp_main_t *rm = &rd_cp_main;
122 default_route_t *default_route;
124 pool_get_zero (rm->default_route_pool, default_route);
126 default_route->sw_if_index = sw_if_index;
127 default_route->router_address = *next_hop_address;
128 default_route->due_time = due_time;
131 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
135 .fp_proto = FIB_PROTOCOL_IP6,
137 ip46_address_t nh = {
138 .ip6 = default_route->router_address,
140 fib_table_entry_update_one_path (fib_index, &pfx,
145 default_route->sw_if_index,
146 0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
151 remove_slaac_address (vlib_main_t * vm, slaac_address_t * slaac_address)
153 rd_cp_main_t *rm = &rd_cp_main;
154 clib_error_t *rv = 0;
156 rv = ip6_add_del_interface_address (vm, slaac_address->sw_if_index,
157 &slaac_address->address,
158 slaac_address->address_length, 1);
160 pool_put (rm->slaac_address_pool, slaac_address);
166 remove_default_route (vlib_main_t * vm, default_route_t * default_route)
168 rd_cp_main_t *rm = &rd_cp_main;
171 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
175 .fp_proto = FIB_PROTOCOL_IP6,
177 ip46_address_t nh = {
178 .ip6 = default_route->router_address,
180 fib_table_entry_path_remove (fib_index, &pfx,
184 default_route->sw_if_index,
185 0, 1, FIB_ROUTE_PATH_FLAG_NONE);
188 pool_put (rm->default_route_pool, default_route);
192 get_interface_mac_address (u32 sw_if_index, u8 mac[])
194 rd_cp_main_t *rm = &rd_cp_main;
195 vnet_sw_interface_t *si;
196 ethernet_interface_t *eth_if = 0;
198 if (!vnet_sw_interface_is_api_valid (rm->vnet_main, sw_if_index))
200 vlib_log_warn (rm->log_class, "Invalid sw_if_index");
204 si = vnet_get_sup_sw_interface (rm->vnet_main, sw_if_index);
205 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
206 eth_if = ethernet_get_interface (ðernet_main, si->hw_if_index);
210 vlib_log_warn (rm->log_class, "Failed to get hardware interface");
214 clib_memcpy_fast (mac, ð_if->address, 6);
220 ip6_prefixes_equal (ip6_address_t * prefix1, ip6_address_t * prefix2, u8 len)
224 if (prefix1->as_u64[0] != prefix2->as_u64[0])
228 return prefix1->as_u64[1] >> (128 - len) ==
229 prefix2->as_u64[1] >> (128 - len);
231 return prefix1->as_u64[0] >> (64 - len) == prefix2->as_u64[0] >> (64 - len);
234 #define PREFIX_FLAG_A (1 << 6)
235 #define PREFIX_FLAG_L (1 << 7)
238 ip6_ra_report_handler (const ip6_ra_report_t * r)
240 rd_cp_main_t *rm = &rd_cp_main;
241 vlib_main_t *vm = rm->vlib_main;
242 interface_config_t *if_config;
243 default_route_t *default_route;
244 slaac_address_t *slaac_address;
246 u16 router_lifetime_in_sec;
248 ra_report_prefix_info_t *prefix;
253 current_time = vlib_time_now (vm);
255 sw_if_index = r->sw_if_index;
257 if (sw_if_index >= vec_len (rm->config_by_sw_if_index))
259 if_config = &rm->config_by_sw_if_index[sw_if_index];
261 if (if_config->install_default_routes)
263 router_lifetime_in_sec = r->router_lifetime_in_sec;
264 u8 route_already_present = 0;
266 pool_foreach (default_route, rm->default_route_pool)
268 if (default_route->sw_if_index != sw_if_index)
270 else if (0 != memcmp (&default_route->router_address,
271 &r->router_address, 16))
275 route_already_present = 1;
276 goto default_route_pool_foreach_out;
280 default_route_pool_foreach_out:
282 if (!route_already_present)
284 if (router_lifetime_in_sec != 0)
285 add_default_route (vm, sw_if_index, &r->router_address,
286 current_time + router_lifetime_in_sec);
290 if (router_lifetime_in_sec != 0)
291 default_route->due_time = current_time + router_lifetime_in_sec;
293 remove_default_route (vm, default_route);
297 if (get_interface_mac_address (sw_if_index, mac) != 0)
299 vlib_log_warn (rm->log_class, "Error getting MAC address");
303 if (!if_config->enabled)
306 n_prefixes = vec_len (r->prefixes);
307 for (i = 0; i < n_prefixes; i++)
309 ip6_address_t *dst_address;
315 prefix = &r->prefixes[i];
317 if (!(prefix->flags & PREFIX_FLAG_A))
320 dst_address = &prefix->prefix.fp_addr.ip6;
321 prefix_length = prefix->prefix.fp_len;
323 if (ip6_address_is_link_local_unicast (dst_address))
326 valid_time = prefix->valid_time;
327 preferred_time = prefix->preferred_time;
329 if (preferred_time > valid_time)
332 if (prefix_length != 64)
335 u8 address_already_present = 0;
337 pool_foreach (slaac_address, rm->slaac_address_pool)
339 if (slaac_address->sw_if_index != sw_if_index)
341 else if (slaac_address->address_length != prefix_length)
343 else if (!ip6_prefixes_equal (&slaac_address->address, dst_address,
348 address_already_present = 1;
349 goto slaac_address_pool_foreach_out;
353 slaac_address_pool_foreach_out:
355 if (address_already_present)
357 f64 remaining_life_time = slaac_address->due_time - current_time;
358 if (valid_time > 2 * 60 * 60 || valid_time > remaining_life_time)
359 slaac_address->due_time = current_time + valid_time;
360 else if (remaining_life_time > 2 * 60 * 60)
361 slaac_address->due_time = current_time + 2 * 60 * 60;
368 due_time = current_time + valid_time;
371 addr.as_u64[0] = dst_address->as_u64[0];
372 /* Invert the "u" bit */
373 addr.as_u8[8] = mac[0] ^ (1 << 1);
374 addr.as_u8[9] = mac[1];
375 addr.as_u8[10] = mac[2];
376 addr.as_u8[11] = 0xFF;
377 addr.as_u8[12] = 0xFE;
378 addr.as_u8[13] = mac[3];
379 addr.as_u8[14] = mac[4];
380 addr.as_u8[15] = mac[5];
382 add_slaac_address (vm, sw_if_index, prefix_length, &addr, due_time);
385 interrupt_process ();
391 rd_cp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
393 uword *event_data = 0;
394 rd_cp_main_t *rm = &rd_cp_main;
395 slaac_address_t *slaac_address;
396 default_route_t *default_route;
397 f64 sleep_time = 1e9;
403 vlib_process_wait_for_event_or_clock (vm, sleep_time);
404 vlib_process_get_events (vm, &event_data);
406 vec_reset_length (event_data);
408 current_time = vlib_time_now (vm);
411 due_time = current_time + 1e9;
414 * we do not use pool_foreach() to iterate over pool elements here
415 * as we are removing elements inside the loop body
418 pool_foreach_index (index, rm->slaac_address_pool)
420 slaac_address = pool_elt_at_index(rm->slaac_address_pool, index);
421 if (slaac_address->due_time > current_time)
423 if (slaac_address->due_time < due_time)
424 due_time = slaac_address->due_time;
428 u32 sw_if_index = slaac_address->sw_if_index;
429 remove_slaac_address (vm, slaac_address);
430 /* make sure ip6 stays enabled */
431 ip6_link_enable (sw_if_index, NULL);
434 pool_foreach_index (index, rm->default_route_pool)
436 default_route = pool_elt_at_index(rm->default_route_pool, index);
437 if (default_route->due_time > current_time)
439 if (default_route->due_time < due_time)
440 due_time = default_route->due_time;
443 remove_default_route (vm, default_route);
446 current_time = vlib_time_now (vm);
448 while (due_time < current_time);
450 sleep_time = due_time - current_time;
457 VLIB_REGISTER_NODE (rd_cp_process_node) = {
458 .function = rd_cp_process,
459 .type = VLIB_NODE_TYPE_PROCESS,
460 .name = "rd-cp-process",
465 interrupt_process (void)
467 rd_cp_main_t *rm = &rd_cp_main;
468 vlib_main_t *vm = rm->vlib_main;
470 vlib_process_signal_event (vm, rd_cp_process_node.index,
471 RD_CP_EVENT_INTERRUPT, 0);
475 rd_cp_set_address_autoconfig (u32 sw_if_index,
476 u8 enable, u8 install_default_routes)
478 rd_cp_main_t *rm = &rd_cp_main;
479 vlib_main_t *vm = rm->vlib_main;
480 vnet_main_t *vnm = rm->vnet_main;
481 interface_config_t *if_config;
482 interface_config_t empty_config = { 0, 0 };
483 slaac_address_t *slaac_address;
484 default_route_t *default_route;
487 install_default_routes = 0;
489 if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index))
491 vlib_log_warn (rm->log_class, "Invalid sw_if_index");
497 /* process kickoff */
498 interrupt_process ();
502 vec_validate_init_empty (rm->config_by_sw_if_index, sw_if_index,
504 if_config = &rm->config_by_sw_if_index[sw_if_index];
506 if (!if_config->enabled && enable)
507 ip6_link_enable (sw_if_index, NULL);
509 if ((!if_config->enabled && enable)
510 || (!if_config->install_default_routes && install_default_routes))
511 router_solicitation_start_stop (sw_if_index, 1);
512 else if (if_config->enabled && !enable)
513 router_solicitation_start_stop (sw_if_index, 0);
515 if (if_config->enabled && !enable)
518 pool_foreach (slaac_address, rm->slaac_address_pool)
520 remove_slaac_address (vm, slaac_address);
524 if (if_config->install_default_routes && !install_default_routes)
527 pool_foreach (default_route, rm->default_route_pool)
529 remove_default_route (vm, default_route);
534 if_config->enabled = enable;
535 if_config->install_default_routes = install_default_routes;
540 static clib_error_t *
541 ip6_nd_address_autoconfig (vlib_main_t * vm,
542 unformat_input_t * input, vlib_cli_command_t * cmd)
544 rd_cp_main_t *rm = &rd_cp_main;
545 vnet_main_t *vnm = rm->vnet_main;
546 clib_error_t *error = 0;
547 u32 sw_if_index = ~0;
549 u8 default_route = 0;
551 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
554 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
556 if (unformat (input, "default-route"))
558 if (unformat (input, "disable"))
564 if (sw_if_index != ~0)
566 if (rd_cp_set_address_autoconfig (sw_if_index, enable, default_route) !=
568 error = clib_error_return (0, "Invalid sw_if_index");
571 error = clib_error_return (0, "Missing sw_if_index");
577 * This command is used to enable ND address autoconfiguration
578 * on particular interface including setting up default routes.
582 * Example of how to enable ND address autoconfiguration:
583 * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0}
584 * Example of how to enable ND address autoconfiguration
585 * with setting up default routes:
586 * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0 default-route}
587 * Example of how to disable ND address autoconfiguration:
588 * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0 disable}
592 VLIB_CLI_COMMAND (ip6_nd_address_autoconfig_command, static) = {
593 .path = "ip6 nd address autoconfig",
594 .short_help = "ip6 nd address autoconfig <interface> [default-route|disable]",
595 .function = ip6_nd_address_autoconfig,
599 static clib_error_t *
600 rd_cp_init (vlib_main_t * vm)
602 rd_cp_main_t *rm = &rd_cp_main;
605 rm->vnet_main = vnet_get_main ();
606 rm->node_index = rd_cp_process_node.index;
608 rm->log_class = vlib_log_register_class ("rd_cp", 0);
610 ip6_ra_report_register (ip6_ra_report_handler);
615 VLIB_INIT_FUNCTION (rd_cp_init);
618 * fd.io coding-style-patch-verification: ON
621 * eval: (c-set-style "gnu")