2 #include <vlibmemory/api.h>
3 #include <vnet/vnet_msg_enum.h>
4 #include <vnet/ip/ip6.h>
8 #define vl_typedefs /* define message structures */
9 #include <vnet/vnet_all_api_h.h>
12 #define vl_endianfun /* define message structures */
13 #include <vnet/vnet_all_api_h.h>
16 #include <vlibapi/api_helper_macros.h>
18 #define foreach_rd_cp_msg \
19 _(IP6_ND_ADDRESS_AUTOCONFIG, ip6_nd_address_autoconfig)
21 #define foreach_client_rd_cp_msg \
22 _(IP6_RA_EVENT, ip6_ra_event) \
23 _(IP6ND_SEND_ROUTER_SOLICITATION_REPLY, ip6nd_send_router_solicitation_reply) \
24 _(WANT_IP6_RA_EVENTS_REPLY, want_ip6_ra_events_reply) \
25 _(SW_INTERFACE_ADD_DEL_ADDRESS_REPLY, sw_interface_add_del_address_reply) \
26 _(IP_ADD_DEL_ROUTE_REPLY, ip_add_del_route_reply) \
27 _(SW_INTERFACE_GET_MAC_ADDRESS_REPLY, sw_interface_get_mac_address_reply) \
28 _(SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY, sw_interface_ip6_enable_disable_reply)
34 ip6_address_t address;
41 ip6_address_t router_address;
48 u8 install_default_routes;
56 interface_config_t *config_by_sw_if_index;
57 slaac_address_t *slaac_address_pool;
58 default_route_t *default_route_pool;
60 /* binary API client */
62 svm_queue_t *vl_input_queue;
64 void (**msg_handlers) (void *);
76 vlib_main_t *vlib_main;
77 vnet_main_t *vnet_main;
82 rd_cp_main_t rd_cp_main;
86 RD_CP_EVENT_INTERRUPT,
89 #define vl_api_ip6_nd_address_autoconfig_t_print vl_noop_handler
92 msg_api_config (vl_msg_api_msg_config_t * c)
94 rd_cp_main_t *rm = &rd_cp_main;
96 vec_validate (rm->msg_handlers, c->id);
98 if (rm->msg_handlers[c->id] && rm->msg_handlers[c->id] != c->handler)
100 ("BUG: re-registering 'vl_api_%s_t_handler'."
101 "Handler was %llx, replaced by %llx",
102 c->name, rm->msg_handlers[c->id], c->handler);
104 rm->msg_handlers[c->id] = c->handler;
108 set_handler (int id, char *name, void *handler)
110 vl_msg_api_msg_config_t cfg;
111 vl_msg_api_msg_config_t *c = &cfg;
113 memset (c, 0, sizeof (*c));
117 c->handler = handler;
119 c->message_bounce = 0;
124 static_always_inline void
127 rd_cp_main_t *rm = &rd_cp_main;
130 if (!rm->api_connected)
133 if (svm_queue_sub2 (rm->vl_input_queue, (u8 *) & msgp))
136 u16 id = ntohs (*((u16 *) msgp));
137 u8 *(*handler) (void *);
139 if (id < vec_len (rm->msg_handlers) && rm->msg_handlers[id])
141 handler = (void *) rm->msg_handlers[id];
142 (*handler) ((void *) msgp);
146 if (id != VL_API_MEMCLNT_KEEPALIVE)
147 clib_warning ("no handler for msg id %d", id);
151 static_always_inline int
152 wait_for_reply (void)
154 rd_cp_main_t *rm = &rd_cp_main;
155 vlib_main_t *vm = rm->vlib_main;
158 timeout = vlib_time_now (vm) + 1.0;
159 while (vlib_time_now (vm) < timeout)
162 if (rm->api_reply.arrived)
164 vlib_process_suspend (vm, 1e-5);
167 if (!rm->api_reply.arrived)
170 return rm->api_reply.retval;
173 static_always_inline void
176 rd_cp_main_t *rm = &rd_cp_main;
178 vl_msg_api_send_shmem (rm->api_main->shmem_hdr->vl_input_queue,
182 static_always_inline int
183 send_msg_and_wait_for_reply (void *msg)
185 rd_cp_main_t *rm = &rd_cp_main;
187 rm->api_reply.arrived = 0;
189 return wait_for_reply ();
193 router_solicitation_start_stop (u32 sw_if_index, u8 start)
195 rd_cp_main_t *rm = &rd_cp_main;
196 vl_api_ip6nd_send_router_solicitation_t *mp;
199 mp = vl_msg_api_alloc (sizeof (*mp));
200 memset (mp, 0, sizeof (*mp));
201 mp->_vl_msg_id = htons (VL_API_IP6ND_SEND_ROUTER_SOLICITATION);
202 mp->client_index = rm->my_client_index;
203 mp->sw_if_index = htonl (sw_if_index);
207 mp->mrt = htonl (120);
212 rv = send_msg_and_wait_for_reply (mp);
218 vl_api_ip6nd_send_router_solicitation_reply_t_handler
219 (vl_api_ip6nd_send_router_solicitation_reply_t * mp)
221 rd_cp_main_t *rm = &rd_cp_main;
223 rm->api_reply.arrived = 1;
224 rm->api_reply.retval = ntohl (mp->retval);
228 ip6_ra_events_enable_disable (int enable)
230 rd_cp_main_t *rm = &rd_cp_main;
231 vl_api_want_ip6_ra_events_t *mp;
234 mp = vl_msg_api_alloc (sizeof (*mp));
235 memset (mp, 0, sizeof (*mp));
236 mp->_vl_msg_id = htons (VL_API_WANT_IP6_RA_EVENTS);
237 mp->client_index = rm->my_client_index;
238 mp->enable_disable = enable;
239 mp->pid = htonl (getpid ());
241 rv = send_msg_and_wait_for_reply (mp);
244 rm->events_on = enable;
250 vl_api_want_ip6_ra_events_reply_t_handler (vl_api_want_ip6_ra_events_reply_t *
253 rd_cp_main_t *rm = &rd_cp_main;
255 rm->api_reply.arrived = 1;
256 rm->api_reply.retval = ntohl (mp->retval);
259 static void interrupt_process (void);
262 add_slaac_address (vlib_main_t * vm, u32 sw_if_index, u8 address_length,
263 ip6_address_t * address, f64 due_time)
265 rd_cp_main_t *rm = &rd_cp_main;
266 slaac_address_t *slaac_address;
267 vl_api_sw_interface_add_del_address_t *mp;
270 pool_get (rm->slaac_address_pool, slaac_address);
272 slaac_address->sw_if_index = sw_if_index;
273 slaac_address->address_length = address_length;
274 slaac_address->address = *address;
275 slaac_address->due_time = due_time;
277 mp = vl_msg_api_alloc (sizeof (*mp));
278 memset (mp, 0, sizeof (*mp));
279 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS);
280 mp->client_index = rm->my_client_index;
283 mp->sw_if_index = htonl (sw_if_index);
284 mp->address_length = slaac_address->address_length;
285 clib_memcpy (mp->address, slaac_address->address.as_u8, 16);
287 rv = send_msg_and_wait_for_reply (mp);
293 vl_api_sw_interface_add_del_address_reply_t_handler
294 (vl_api_sw_interface_add_del_address_reply_t * mp)
296 rd_cp_main_t *rm = &rd_cp_main;
298 rm->api_reply.arrived = 1;
299 rm->api_reply.retval = ntohl (mp->retval);
303 add_default_route (vlib_main_t * vm, u32 sw_if_index,
304 ip6_address_t * next_hop_address, f64 due_time)
306 rd_cp_main_t *rm = &rd_cp_main;
307 default_route_t *default_route;
308 vl_api_ip_add_del_route_t *mp;
311 pool_get (rm->default_route_pool, default_route);
313 default_route->sw_if_index = sw_if_index;
314 default_route->router_address = *next_hop_address;
315 default_route->due_time = due_time;
317 mp = vl_msg_api_alloc (sizeof (*mp));
318 memset (mp, 0, sizeof (*mp));
319 mp->_vl_msg_id = htons (VL_API_IP_ADD_DEL_ROUTE);
320 mp->client_index = rm->my_client_index;
323 mp->dst_address_length = 0;
324 mp->next_hop_sw_if_index = htonl (default_route->sw_if_index);
325 clib_memcpy (mp->next_hop_address, default_route->router_address.as_u8, 16);
327 rv = send_msg_and_wait_for_reply (mp);
333 vl_api_ip_add_del_route_reply_t_handler (vl_api_ip_add_del_route_reply_t * mp)
335 rd_cp_main_t *rm = &rd_cp_main;
337 rm->api_reply.arrived = 1;
338 rm->api_reply.retval = ntohl (mp->retval);
342 remove_slaac_address (vlib_main_t * vm, slaac_address_t * slaac_address)
344 rd_cp_main_t *rm = &rd_cp_main;
345 vl_api_sw_interface_add_del_address_t *mp;
347 mp = vl_msg_api_alloc (sizeof (*mp));
348 memset (mp, 0, sizeof (*mp));
349 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS);
350 mp->client_index = rm->my_client_index;
353 mp->sw_if_index = htonl (slaac_address->sw_if_index);
354 mp->address_length = slaac_address->address_length;
355 clib_memcpy (mp->address, slaac_address->address.as_u8, 16);
357 send_msg_and_wait_for_reply (mp);
359 pool_put (rm->slaac_address_pool, slaac_address);
365 remove_default_route (vlib_main_t * vm, default_route_t * default_route)
367 rd_cp_main_t *rm = &rd_cp_main;
368 vl_api_ip_add_del_route_t *mp;
370 mp = vl_msg_api_alloc (sizeof (*mp));
371 memset (mp, 0, sizeof (*mp));
372 mp->_vl_msg_id = htons (VL_API_IP_ADD_DEL_ROUTE);
373 mp->client_index = rm->my_client_index;
376 mp->dst_address_length = 0;
377 mp->next_hop_sw_if_index = htonl (default_route->sw_if_index);
378 clib_memcpy (mp->next_hop_address, default_route->router_address.as_u8, 16);
380 send_msg_and_wait_for_reply (mp);
382 pool_put (rm->default_route_pool, default_route);
388 get_interface_mac_address (u32 sw_if_index, u8 mac[])
390 rd_cp_main_t *rm = &rd_cp_main;
391 vl_api_sw_interface_get_mac_address_t *mp;
394 mp = vl_msg_api_alloc (sizeof (*mp));
395 memset (mp, 0, sizeof (*mp));
396 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS);
397 mp->client_index = rm->my_client_index;
398 mp->sw_if_index = htonl (sw_if_index);
400 rv = send_msg_and_wait_for_reply (mp);
403 clib_memcpy (mac, rm->api_reply.mac_address, 6);
409 vl_api_sw_interface_get_mac_address_reply_t_handler
410 (vl_api_sw_interface_get_mac_address_reply_t * mp)
412 rd_cp_main_t *rm = &rd_cp_main;
414 rm->api_reply.arrived = 1;
415 rm->api_reply.retval = ntohl (mp->retval);
417 if (rm->api_reply.retval == 0)
418 clib_memcpy (rm->api_reply.mac_address, mp->mac_address, 6);
422 ip6_enable (u32 sw_if_index)
424 rd_cp_main_t *rm = &rd_cp_main;
425 vl_api_sw_interface_ip6_enable_disable_t *mp;
428 mp = vl_msg_api_alloc (sizeof (*mp));
429 memset (mp, 0, sizeof (*mp));
430 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE);
431 mp->client_index = rm->my_client_index;
432 mp->sw_if_index = htonl (sw_if_index);
435 rv = send_msg_and_wait_for_reply (mp);
441 vl_api_sw_interface_ip6_enable_disable_reply_t_handler
442 (vl_api_sw_interface_ip6_enable_disable_reply_t * mp)
444 rd_cp_main_t *rm = &rd_cp_main;
446 rm->api_reply.arrived = 1;
447 rm->api_reply.retval = ntohl (mp->retval);
451 ip6_prefixes_equal (ip6_address_t * prefix1, ip6_address_t * prefix2, u8 len)
455 if (prefix1->as_u64[0] != prefix2->as_u64[0])
459 return prefix1->as_u64[1] >> (128 - len) ==
460 prefix2->as_u64[1] >> (128 - len);
462 return prefix1->as_u64[0] >> (64 - len) == prefix2->as_u64[0] >> (64 - len);
465 #define PREFIX_FLAG_A (1 << 6)
466 #define PREFIX_FLAG_L (1 << 7)
469 vl_api_ip6_ra_event_t_handler (vl_api_ip6_ra_event_t * mp)
471 rd_cp_main_t *rm = &rd_cp_main;
472 vlib_main_t *vm = rm->vlib_main;
473 interface_config_t *if_config;
474 default_route_t *default_route;
475 slaac_address_t *slaac_address;
477 u16 router_lifetime_in_sec;
479 vl_api_ip6_ra_prefix_info_t *prefix;
484 current_time = vlib_time_now (vm);
486 sw_if_index = ntohl (mp->sw_if_index);
488 if_config = &rm->config_by_sw_if_index[sw_if_index];
490 if (if_config->install_default_routes)
492 router_lifetime_in_sec = ntohs (mp->router_lifetime_in_sec);
493 u8 route_already_present = 0;
495 pool_foreach (default_route, rm->default_route_pool,
497 if (default_route->sw_if_index != sw_if_index)
499 else if (0 != memcmp (&default_route->router_address,
500 mp->router_address, 16))
504 route_already_present = 1;
505 goto default_route_pool_foreach_out;
509 default_route_pool_foreach_out:
511 if (!route_already_present)
513 if (router_lifetime_in_sec != 0)
514 add_default_route (vm, sw_if_index, (void *) mp->router_address,
515 current_time + router_lifetime_in_sec);
519 if (router_lifetime_in_sec != 0)
520 default_route->due_time = current_time + router_lifetime_in_sec;
522 remove_default_route (vm, default_route);
526 if (get_interface_mac_address (sw_if_index, mac) != 0)
528 clib_warning ("Error getting MAC address");
532 if (!if_config->enabled)
535 n_prefixes = ntohl (mp->n_prefixes);
536 for (i = 0; i < n_prefixes; i++)
538 ip6_address_t *dst_address;
544 prefix = &mp->prefixes[i];
546 if (!(prefix->flags & PREFIX_FLAG_A))
549 dst_address = (ip6_address_t *) prefix->dst_address;
550 prefix_length = prefix->dst_address_length;
552 if (ip6_address_is_link_local_unicast (dst_address))
555 valid_time = ntohl (prefix->valid_time);
556 preferred_time = ntohl (prefix->preferred_time);
558 if (preferred_time > valid_time)
561 if (prefix_length != 64)
564 u8 address_already_present = 0;
566 pool_foreach (slaac_address, rm->slaac_address_pool,
568 if (slaac_address->sw_if_index != sw_if_index)
570 else if (slaac_address->address_length != prefix_length)
572 else if (!ip6_prefixes_equal (&slaac_address->address, dst_address,
577 address_already_present = 1;
578 goto slaac_address_pool_foreach_out;
582 slaac_address_pool_foreach_out:
584 if (address_already_present)
586 f64 remaining_life_time = slaac_address->due_time - current_time;
587 if (valid_time > 2 * 60 * 60 || valid_time > remaining_life_time)
588 slaac_address->due_time = current_time + valid_time;
589 else if (remaining_life_time > 2 * 60 * 60)
590 slaac_address->due_time = current_time + 2 * 60 * 60;
597 due_time = current_time + valid_time;
600 addr.as_u64[0] = dst_address->as_u64[0];
601 /* Invert the "u" bit */
602 addr.as_u8[8] = mac[0] ^ (1 << 1);
603 addr.as_u8[9] = mac[1];
604 addr.as_u8[10] = mac[2];
605 addr.as_u8[11] = 0xFF;
606 addr.as_u8[12] = 0xFE;
607 addr.as_u8[13] = mac[3];
608 addr.as_u8[14] = mac[4];
609 addr.as_u8[15] = mac[5];
611 add_slaac_address (vm, sw_if_index, prefix_length, &addr, due_time);
614 interrupt_process ();
618 rd_cp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
621 uword *event_data = 0;
622 rd_cp_main_t *rm = &rd_cp_main;
623 slaac_address_t *slaac_address;
624 default_route_t *default_route;
625 f64 sleep_time = 1e9;
626 const f64 micro_sleep_time = 1e-5;
633 start_time = vlib_time_now (vm);
638 vlib_process_wait_for_event_or_clock (vm, micro_sleep_time);
639 event_type = vlib_process_get_events (vm, &event_data);
641 || vlib_time_now (vm) > start_time + sleep_time)
645 vec_reset_length (event_data);
647 current_time = vlib_time_now (vm);
650 due_time = current_time + 1e9;
652 pool_foreach (slaac_address, rm->slaac_address_pool,
654 if (slaac_address->due_time > current_time)
656 if (slaac_address->due_time < due_time)
657 due_time = slaac_address->due_time;
661 remove_slaac_address (vm, slaac_address);
662 /* make sure ip6 stays enabled */
663 ip6_enable (slaac_address->sw_if_index);
666 pool_foreach (default_route, rm->default_route_pool,
668 if (default_route->due_time > current_time)
670 if (default_route->due_time < due_time)
671 due_time = default_route->due_time;
674 remove_default_route (vm, default_route);
677 current_time = vlib_time_now (vm);
679 while (due_time < current_time);
681 sleep_time = due_time - current_time;
688 VLIB_REGISTER_NODE (rd_cp_process_node) = {
689 .function = rd_cp_process,
690 .type = VLIB_NODE_TYPE_PROCESS,
691 .name = "rd-cp-process",
696 interrupt_process (void)
698 rd_cp_main_t *rm = &rd_cp_main;
699 vlib_main_t *vm = rm->vlib_main;
701 vlib_process_signal_event (vm, rd_cp_process_node.index,
702 RD_CP_EVENT_INTERRUPT, 0);
706 create_api_loopback (void)
708 rd_cp_main_t *rm = &rd_cp_main;
709 api_main_t *am = &api_main;
714 pthread_mutex_lock (&svm->mutex);
715 rd_cp_main.vl_input_queue =
716 svm_queue_init (1024, sizeof (uword), getpid (), 0);
717 pthread_mutex_unlock (&svm->mutex);
719 rd_cp_main.my_client_index =
720 vl_api_memclnt_create_internal ("ndp_rd_client",
721 rd_cp_main.vl_input_queue);
723 rm->api_connected = 1;
729 set_address_autoconfig (u32 sw_if_index, u8 enable, u8 install_default_routes)
731 rd_cp_main_t *rm = &rd_cp_main;
732 vlib_main_t *vm = rm->vlib_main;
733 vnet_main_t *vnm = rm->vnet_main;
734 interface_config_t *if_config;
735 interface_config_t empty_config = { 0, 0 };
736 slaac_address_t *slaac_address;
737 default_route_t *default_route;
740 install_default_routes = 0;
742 // TODO: makes direct interaction with data plane
743 if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index))
745 clib_warning ("Invalid sw_if_index");
751 create_api_loopback ();
752 /* process kickoff */
753 interrupt_process ();
754 ip6_ra_events_enable_disable (1);
758 vec_validate_init_empty (rm->config_by_sw_if_index, sw_if_index,
760 if_config = &rm->config_by_sw_if_index[sw_if_index];
762 if (!if_config->enabled && enable)
763 ip6_enable (sw_if_index);
765 if ((!if_config->enabled && enable)
766 || (!if_config->install_default_routes && install_default_routes))
767 router_solicitation_start_stop (sw_if_index, 1);
768 else if (if_config->enabled && !enable)
769 router_solicitation_start_stop (sw_if_index, 0);
771 if (if_config->enabled && !enable)
774 pool_foreach (slaac_address, rm->slaac_address_pool,
776 remove_slaac_address (vm, slaac_address);
780 if (if_config->install_default_routes && !install_default_routes)
783 pool_foreach (default_route, rm->default_route_pool,
785 remove_default_route (vm, default_route);
790 if_config->enabled = enable;
791 if_config->install_default_routes = install_default_routes;
796 static clib_error_t *
797 ip6_nd_address_autoconfig (vlib_main_t * vm,
798 unformat_input_t * input, vlib_cli_command_t * cmd)
800 rd_cp_main_t *rm = &rd_cp_main;
801 vnet_main_t *vnm = rm->vnet_main;
802 clib_error_t *error = 0;
803 u32 sw_if_index = ~0;
805 u8 default_route = 0;
807 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
810 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
812 if (unformat (input, "default-route"))
814 if (unformat (input, "disable"))
820 if (sw_if_index != ~0)
822 if (set_address_autoconfig (sw_if_index, enable, default_route) != 0)
823 error = clib_error_return (0, "Invalid sw_if_index");
826 error = clib_error_return (0, "Missing sw_if_index");
832 * This command is used to enable ND address autoconfiguration
833 * on particular interface including setting up default routes.
837 * Example of how to enable ND address autoconfiguration:
838 * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0}
839 * Example of how to enable ND address autoconfiguration
840 * with setting up default routes:
841 * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0 default-route}
842 * Example of how to disable ND address autoconfiguration:
843 * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0 disable}
847 VLIB_CLI_COMMAND (ip6_nd_address_autoconfig_command, static) = {
848 .path = "ip6 nd address autoconfig",
849 .short_help = "ip6 nd address autoconfig <interface> [default-route|disable]",
850 .function = ip6_nd_address_autoconfig,
855 vl_api_ip6_nd_address_autoconfig_t_handler (vl_api_ip6_nd_address_autoconfig_t
858 vl_api_ip6_nd_address_autoconfig_reply_t *rmp;
862 VALIDATE_SW_IF_INDEX (mp);
864 sw_if_index = ntohl (mp->sw_if_index);
867 set_address_autoconfig (sw_if_index, mp->enable,
868 mp->install_default_routes);
870 BAD_SW_IF_INDEX_LABEL;
872 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
875 #define vl_msg_name_crc_list
876 #include <vnet/ip/rd_cp.api.h>
877 #undef vl_msg_name_crc_list
880 setup_message_id_table (api_main_t * am)
882 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
883 foreach_vl_msg_name_crc_rd_cp;
887 static clib_error_t *
888 rd_cp_init (vlib_main_t * vm)
890 rd_cp_main_t *rm = &rd_cp_main;
891 api_main_t *am = &api_main;
894 rm->vnet_main = vnet_get_main ();
896 rm->node_index = rd_cp_process_node.index;
899 vl_msg_api_set_handlers(VL_API_##N, #n, \
900 vl_api_##n##_t_handler, \
902 vl_api_##n##_t_endian, \
903 vl_api_##n##_t_print, \
904 sizeof(vl_api_##n##_t), 0/* do NOT trace! */);
909 set_handler(VL_API_##N, #n, vl_api_##n##_t_handler);
910 foreach_client_rd_cp_msg;
914 * Set up the (msg_name, crc, message-id) table
916 setup_message_id_table (am);
921 VLIB_INIT_FUNCTION (rd_cp_init);
924 * fd.io coding-style-patch-verification: ON
927 * eval: (c-set-style "gnu")