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/ip/ip6.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ip/ip6_neighbor.h>
22 #include <vnet/fib/fib_table.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_rd_cp_msg \
37 _(IP6_ND_ADDRESS_AUTOCONFIG, ip6_nd_address_autoconfig)
43 ip6_address_t address;
50 ip6_address_t router_address;
57 u8 install_default_routes;
65 interface_config_t *config_by_sw_if_index;
66 slaac_address_t *slaac_address_pool;
67 default_route_t *default_route_pool;
69 /* binary API client */
71 svm_queue_t *vl_input_queue;
75 vlib_main_t *vlib_main;
76 vnet_main_t *vnet_main;
81 rd_cp_main_t rd_cp_main;
85 RD_CP_EVENT_INTERRUPT,
88 #define vl_api_ip6_nd_address_autoconfig_t_print vl_noop_handler
91 router_solicitation_start_stop (u32 sw_if_index, u8 start)
93 rd_cp_main_t *rm = &rd_cp_main;
94 icmp6_send_router_solicitation_params_t params = { 0, };
102 icmp6_send_router_solicitation (rm->vlib_main, sw_if_index, !start,
106 static void interrupt_process (void);
109 add_slaac_address (vlib_main_t * vm, u32 sw_if_index, u8 address_length,
110 ip6_address_t * address, f64 due_time)
112 rd_cp_main_t *rm = &rd_cp_main;
113 slaac_address_t *slaac_address;
114 clib_error_t *rv = 0;
116 pool_get (rm->slaac_address_pool, slaac_address);
118 slaac_address->sw_if_index = sw_if_index;
119 slaac_address->address_length = address_length;
120 slaac_address->address = *address;
121 slaac_address->due_time = due_time;
124 ip6_add_del_interface_address (vm, sw_if_index, &slaac_address->address,
131 add_default_route (vlib_main_t * vm, u32 sw_if_index,
132 ip6_address_t * next_hop_address, f64 due_time)
134 rd_cp_main_t *rm = &rd_cp_main;
135 default_route_t *default_route;
137 pool_get (rm->default_route_pool, default_route);
139 default_route->sw_if_index = sw_if_index;
140 default_route->router_address = *next_hop_address;
141 default_route->due_time = due_time;
144 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
148 .fp_proto = FIB_PROTOCOL_IP6,
150 ip46_address_t nh = {
151 .ip6 = default_route->router_address,
153 fib_table_entry_update_one_path (fib_index, &pfx,
158 default_route->sw_if_index,
159 0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
164 remove_slaac_address (vlib_main_t * vm, slaac_address_t * slaac_address)
166 rd_cp_main_t *rm = &rd_cp_main;
167 clib_error_t *rv = 0;
169 rv = ip6_add_del_interface_address (vm, slaac_address->sw_if_index,
170 &slaac_address->address,
171 slaac_address->address_length, 1);
173 pool_put (rm->slaac_address_pool, slaac_address);
179 remove_default_route (vlib_main_t * vm, default_route_t * default_route)
181 rd_cp_main_t *rm = &rd_cp_main;
184 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
188 .fp_proto = FIB_PROTOCOL_IP6,
190 ip46_address_t nh = {
191 .ip6 = default_route->router_address,
193 fib_table_entry_path_remove (fib_index, &pfx,
197 default_route->sw_if_index,
198 0, 1, FIB_ROUTE_PATH_FLAG_NONE);
201 pool_put (rm->default_route_pool, default_route);
205 get_interface_mac_address (u32 sw_if_index, u8 mac[])
207 rd_cp_main_t *rm = &rd_cp_main;
208 vnet_sw_interface_t *si;
209 ethernet_interface_t *eth_if = 0;
211 if (!vnet_sw_interface_is_api_valid (rm->vnet_main, sw_if_index))
213 clib_warning ("Invalid sw_if_index");
217 si = vnet_get_sup_sw_interface (rm->vnet_main, sw_if_index);
218 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
219 eth_if = ethernet_get_interface (ðernet_main, si->hw_if_index);
221 clib_memcpy (mac, eth_if->address, 6);
227 ip6_enable (u32 sw_if_index)
229 rd_cp_main_t *rm = &rd_cp_main;
232 rv = enable_ip6_interface (rm->vlib_main, sw_if_index);
238 ip6_prefixes_equal (ip6_address_t * prefix1, ip6_address_t * prefix2, u8 len)
242 if (prefix1->as_u64[0] != prefix2->as_u64[0])
246 return prefix1->as_u64[1] >> (128 - len) ==
247 prefix2->as_u64[1] >> (128 - len);
249 return prefix1->as_u64[0] >> (64 - len) == prefix2->as_u64[0] >> (64 - len);
252 #define PREFIX_FLAG_A (1 << 6)
253 #define PREFIX_FLAG_L (1 << 7)
255 static clib_error_t *
256 ip6_ra_report_handler (void *data)
258 rd_cp_main_t *rm = &rd_cp_main;
259 vlib_main_t *vm = rm->vlib_main;
260 clib_error_t *error = 0;
261 ra_report_t *r = data;
262 interface_config_t *if_config;
263 default_route_t *default_route;
264 slaac_address_t *slaac_address;
266 u16 router_lifetime_in_sec;
268 ra_report_prefix_info_t *prefix;
273 current_time = vlib_time_now (vm);
275 sw_if_index = r->sw_if_index;
277 if (sw_if_index >= vec_len (rm->config_by_sw_if_index))
279 if_config = &rm->config_by_sw_if_index[sw_if_index];
281 if (if_config->install_default_routes)
283 router_lifetime_in_sec = r->router_lifetime_in_sec;
284 u8 route_already_present = 0;
286 pool_foreach (default_route, rm->default_route_pool,
288 if (default_route->sw_if_index != sw_if_index)
290 else if (0 != memcmp (&default_route->router_address,
291 r->router_address, 16))
295 route_already_present = 1;
296 goto default_route_pool_foreach_out;
300 default_route_pool_foreach_out:
302 if (!route_already_present)
304 if (router_lifetime_in_sec != 0)
305 add_default_route (vm, sw_if_index, (void *) r->router_address,
306 current_time + router_lifetime_in_sec);
310 if (router_lifetime_in_sec != 0)
311 default_route->due_time = current_time + router_lifetime_in_sec;
313 remove_default_route (vm, default_route);
317 if (get_interface_mac_address (sw_if_index, mac) != 0)
319 clib_warning ("Error getting MAC address");
320 return clib_error_return (0, "Error getting MAC address");
323 if (!if_config->enabled)
326 n_prefixes = vec_len (r->prefixes);
327 for (i = 0; i < n_prefixes; i++)
329 ip6_address_t *dst_address;
335 prefix = &r->prefixes[i];
337 if (!(prefix->flags & PREFIX_FLAG_A))
340 dst_address = &prefix->dst_address;
341 prefix_length = prefix->dst_address_length;
343 if (ip6_address_is_link_local_unicast (dst_address))
346 valid_time = prefix->valid_time;
347 preferred_time = prefix->preferred_time;
349 if (preferred_time > valid_time)
352 if (prefix_length != 64)
355 u8 address_already_present = 0;
357 pool_foreach (slaac_address, rm->slaac_address_pool,
359 if (slaac_address->sw_if_index != sw_if_index)
361 else if (slaac_address->address_length != prefix_length)
363 else if (!ip6_prefixes_equal (&slaac_address->address, dst_address,
368 address_already_present = 1;
369 goto slaac_address_pool_foreach_out;
373 slaac_address_pool_foreach_out:
375 if (address_already_present)
377 f64 remaining_life_time = slaac_address->due_time - current_time;
378 if (valid_time > 2 * 60 * 60 || valid_time > remaining_life_time)
379 slaac_address->due_time = current_time + valid_time;
380 else if (remaining_life_time > 2 * 60 * 60)
381 slaac_address->due_time = current_time + 2 * 60 * 60;
388 due_time = current_time + valid_time;
391 addr.as_u64[0] = dst_address->as_u64[0];
392 /* Invert the "u" bit */
393 addr.as_u8[8] = mac[0] ^ (1 << 1);
394 addr.as_u8[9] = mac[1];
395 addr.as_u8[10] = mac[2];
396 addr.as_u8[11] = 0xFF;
397 addr.as_u8[12] = 0xFE;
398 addr.as_u8[13] = mac[3];
399 addr.as_u8[14] = mac[4];
400 addr.as_u8[15] = mac[5];
402 add_slaac_address (vm, sw_if_index, prefix_length, &addr, due_time);
405 interrupt_process ();
410 VNET_IP6_NEIGHBOR_RA_FUNCTION (ip6_ra_report_handler);
413 rd_cp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
415 uword *event_data = 0;
416 rd_cp_main_t *rm = &rd_cp_main;
417 slaac_address_t *slaac_address;
418 default_route_t *default_route;
419 f64 sleep_time = 1e9;
425 vlib_process_wait_for_event_or_clock (vm, sleep_time);
426 vlib_process_get_events (vm, &event_data);
428 vec_reset_length (event_data);
430 current_time = vlib_time_now (vm);
433 due_time = current_time + 1e9;
435 pool_foreach (slaac_address, rm->slaac_address_pool,
437 if (slaac_address->due_time > current_time)
439 if (slaac_address->due_time < due_time)
440 due_time = slaac_address->due_time;
444 remove_slaac_address (vm, slaac_address);
445 /* make sure ip6 stays enabled */
446 ip6_enable (slaac_address->sw_if_index);
449 pool_foreach (default_route, rm->default_route_pool,
451 if (default_route->due_time > current_time)
453 if (default_route->due_time < due_time)
454 due_time = default_route->due_time;
457 remove_default_route (vm, default_route);
460 current_time = vlib_time_now (vm);
462 while (due_time < current_time);
464 sleep_time = due_time - current_time;
471 VLIB_REGISTER_NODE (rd_cp_process_node) = {
472 .function = rd_cp_process,
473 .type = VLIB_NODE_TYPE_PROCESS,
474 .name = "rd-cp-process",
479 interrupt_process (void)
481 rd_cp_main_t *rm = &rd_cp_main;
482 vlib_main_t *vm = rm->vlib_main;
484 vlib_process_signal_event (vm, rd_cp_process_node.index,
485 RD_CP_EVENT_INTERRUPT, 0);
489 set_address_autoconfig (u32 sw_if_index, u8 enable, u8 install_default_routes)
491 rd_cp_main_t *rm = &rd_cp_main;
492 vlib_main_t *vm = rm->vlib_main;
493 vnet_main_t *vnm = rm->vnet_main;
494 interface_config_t *if_config;
495 interface_config_t empty_config = { 0, 0 };
496 slaac_address_t *slaac_address;
497 default_route_t *default_route;
500 install_default_routes = 0;
502 if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index))
504 clib_warning ("Invalid sw_if_index");
510 /* process kickoff */
511 interrupt_process ();
515 vec_validate_init_empty (rm->config_by_sw_if_index, sw_if_index,
517 if_config = &rm->config_by_sw_if_index[sw_if_index];
519 if (!if_config->enabled && enable)
520 ip6_enable (sw_if_index);
522 if ((!if_config->enabled && enable)
523 || (!if_config->install_default_routes && install_default_routes))
524 router_solicitation_start_stop (sw_if_index, 1);
525 else if (if_config->enabled && !enable)
526 router_solicitation_start_stop (sw_if_index, 0);
528 if (if_config->enabled && !enable)
531 pool_foreach (slaac_address, rm->slaac_address_pool,
533 remove_slaac_address (vm, slaac_address);
537 if (if_config->install_default_routes && !install_default_routes)
540 pool_foreach (default_route, rm->default_route_pool,
542 remove_default_route (vm, default_route);
547 if_config->enabled = enable;
548 if_config->install_default_routes = install_default_routes;
553 static clib_error_t *
554 ip6_nd_address_autoconfig (vlib_main_t * vm,
555 unformat_input_t * input, vlib_cli_command_t * cmd)
557 rd_cp_main_t *rm = &rd_cp_main;
558 vnet_main_t *vnm = rm->vnet_main;
559 clib_error_t *error = 0;
560 u32 sw_if_index = ~0;
562 u8 default_route = 0;
564 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
567 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
569 if (unformat (input, "default-route"))
571 if (unformat (input, "disable"))
577 if (sw_if_index != ~0)
579 if (set_address_autoconfig (sw_if_index, enable, default_route) != 0)
580 error = clib_error_return (0, "Invalid sw_if_index");
583 error = clib_error_return (0, "Missing sw_if_index");
589 * This command is used to enable ND address autoconfiguration
590 * on particular interface including setting up default routes.
594 * Example of how to enable ND address autoconfiguration:
595 * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0}
596 * Example of how to enable ND address autoconfiguration
597 * with setting up default routes:
598 * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0 default-route}
599 * Example of how to disable ND address autoconfiguration:
600 * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0 disable}
604 VLIB_CLI_COMMAND (ip6_nd_address_autoconfig_command, static) = {
605 .path = "ip6 nd address autoconfig",
606 .short_help = "ip6 nd address autoconfig <interface> [default-route|disable]",
607 .function = ip6_nd_address_autoconfig,
612 vl_api_ip6_nd_address_autoconfig_t_handler (vl_api_ip6_nd_address_autoconfig_t
615 vl_api_ip6_nd_address_autoconfig_reply_t *rmp;
619 VALIDATE_SW_IF_INDEX (mp);
621 sw_if_index = ntohl (mp->sw_if_index);
624 set_address_autoconfig (sw_if_index, mp->enable,
625 mp->install_default_routes);
627 BAD_SW_IF_INDEX_LABEL;
629 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
632 #define vl_msg_name_crc_list
633 #include <vnet/ip/rd_cp.api.h>
634 #undef vl_msg_name_crc_list
637 setup_message_id_table (api_main_t * am)
639 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
640 foreach_vl_msg_name_crc_rd_cp;
644 static clib_error_t *
645 rd_cp_init (vlib_main_t * vm)
647 rd_cp_main_t *rm = &rd_cp_main;
648 api_main_t *am = &api_main;
651 rm->vnet_main = vnet_get_main ();
653 rm->node_index = rd_cp_process_node.index;
656 vl_msg_api_set_handlers(VL_API_##N, #n, \
657 vl_api_##n##_t_handler, \
659 vl_api_##n##_t_endian, \
660 vl_api_##n##_t_print, \
661 sizeof(vl_api_##n##_t), 0/* do NOT trace! */);
666 * Set up the (msg_name, crc, message-id) table
668 setup_message_id_table (am);
673 VLIB_INIT_FUNCTION (rd_cp_init);
676 * fd.io coding-style-patch-verification: ON
679 * eval: (c-set-style "gnu")