2 * Copyright (c) 2019 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/ip/ip6_link.h>
17 #include <vnet/ip/ip6_ll_table.h>
19 #include <vnet/ethernet/ethernet.h>
20 #include <vnet/mfib/ip6_mfib.h>
21 #include <vnet/adj/adj_mcast.h>
23 typedef struct ip6_link_delegate_t_
26 ip6_link_delegate_id_t ild_type;
28 } ip6_link_delegate_t;
30 const static ip6_link_delegate_t ip6_link_delegate_uninit = {
31 .ild_sw_if_index = ~0,
34 typedef struct ip6_link_t_
36 /** interface ip6 is enabled on */
39 /** link-local address - if unset that IP6 is disabled*/
40 ip6_address_t il_ll_addr;
42 /** list of delegates */
43 ip6_link_delegate_t *il_delegates;
45 /** multicast adjacency for this link */
46 adj_index_t il_mcast_adj;
48 /** number of references to IP6 enabled on this link */
52 #define FOREACH_IP6_LINK_DELEGATE(_ild, _il, body) \
55 vec_foreach (_ild, _il->il_delegates) { \
56 if (ip6_link_delegate_is_init(_ild)) \
62 #define FOREACH_IP6_LINK_DELEGATE_ID(_id) \
63 for (_id = 0; _id < il_delegate_id; _id++)
65 /** last used delegate ID */
66 static ip6_link_delegate_id_t il_delegate_id;
68 /** VFT registered per-delegate type */
69 static ip6_link_delegate_vft_t *il_delegate_vfts;
71 /** Per interface configs */
72 static ip6_link_t *ip6_links;
75 static u64 il_randomizer;
78 static vlib_log_class_t ip6_link_logger;
80 #define IP6_LINK_DBG(...) \
81 vlib_log_debug (ip6_link_logger, __VA_ARGS__);
83 #define IP6_LINK_INFO(...) \
84 vlib_log_notice (ip6_link_logger, __VA_ARGS__);
87 ip6_link_delegate_is_init (const ip6_link_delegate_t * ild)
89 return (~0 != ild->ild_sw_if_index);
93 ip6_link_is_enabled_i (const ip6_link_t * il)
95 return (!ip6_address_is_zero (&il->il_ll_addr));
99 ip6_link_local_address_from_mac (ip6_address_t * ip, const u8 * mac)
101 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
102 /* Invert the "u" bit */
103 ip->as_u8[8] = mac[0] ^ (1 << 1);
104 ip->as_u8[9] = mac[1];
105 ip->as_u8[10] = mac[2];
106 ip->as_u8[11] = 0xFF;
107 ip->as_u8[12] = 0xFE;
108 ip->as_u8[13] = mac[3];
109 ip->as_u8[14] = mac[4];
110 ip->as_u8[15] = mac[5];
114 ip6_mac_address_from_link_local (u8 * mac, const ip6_address_t * ip)
116 /* Invert the previously inverted "u" bit */
117 mac[0] = ip->as_u8[8] ^ (1 << 1);
118 mac[1] = ip->as_u8[9];
119 mac[2] = ip->as_u8[10];
120 mac[3] = ip->as_u8[13];
121 mac[4] = ip->as_u8[14];
122 mac[5] = ip->as_u8[15];
126 ip6_link_get (u32 sw_if_index)
130 if (sw_if_index >= vec_len (ip6_links))
133 il = &ip6_links[sw_if_index];
135 if (!ip6_link_is_enabled_i (il))
142 ip6_link_is_enabled (u32 sw_if_index)
144 return (NULL != ip6_link_get (sw_if_index));
149 ip6_link_enable (u32 sw_if_index, const ip6_address_t * link_local_addr)
154 il = ip6_link_get (sw_if_index);
158 const vnet_sw_interface_t *sw, *sw_sup;
159 const ethernet_interface_t *eth;
162 vnm = vnet_get_main ();
164 IP6_LINK_INFO ("enable: %U",
165 format_vnet_sw_if_index_name, vnm, sw_if_index);
167 sw_sup = vnet_get_sup_sw_interface (vnm, sw_if_index);
168 if (sw_sup->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
170 rv = VNET_API_ERROR_UNSUPPORTED;
174 eth = ethernet_get_interface (ðernet_main, sw_sup->hw_if_index);
178 rv = VNET_API_ERROR_UNSUPPORTED;
182 vec_validate (ip6_links, sw_if_index);
184 il = &ip6_links[sw_if_index];
186 il->il_sw_if_index = sw_if_index;
188 sw = vnet_get_sup_sw_interface (vnm, sw_if_index);
190 if (NULL != link_local_addr)
191 ip6_address_copy (&il->il_ll_addr, link_local_addr);
192 else if (sw->type == VNET_SW_INTERFACE_TYPE_SUB ||
193 sw->type == VNET_SW_INTERFACE_TYPE_PIPE ||
194 sw->type == VNET_SW_INTERFACE_TYPE_P2P)
196 il->il_ll_addr.as_u64[0] =
197 clib_host_to_net_u64 (0xFE80000000000000ULL);
199 /* make up an interface id */
200 il->il_ll_addr.as_u64[1] = random_u64 (&il_randomizer);
203 il->il_ll_addr.as_u8[8] &= 0xfd;
207 ip6_link_local_address_from_mac (&il->il_ll_addr, eth->address);
211 ip6_ll_prefix_t ilp = {
212 .ilp_addr = il->il_ll_addr,
213 .ilp_sw_if_index = sw_if_index,
216 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
219 /* essentially "enables" ipv6 on this interface */
220 ip6_mfib_interface_enable_disable (sw_if_index, 1);
221 ip6_sw_interface_enable_disable (sw_if_index, 1);
223 il->il_mcast_adj = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
224 VNET_LINK_IP6, sw_if_index);
226 /* inform all register clients */
227 ip6_link_delegate_id_t id;
228 FOREACH_IP6_LINK_DELEGATE_ID (id)
230 if (NULL != il_delegate_vfts[id].ildv_enable)
231 il_delegate_vfts[id].ildv_enable (il->il_sw_if_index);
238 rv = VNET_API_ERROR_VALUE_EXIST;
246 ip6_link_delegate_flush (ip6_link_t * il)
248 ip6_link_delegate_t *ild;
251 FOREACH_IP6_LINK_DELEGATE (ild, il,
253 il_delegate_vfts[ild->ild_type].ildv_disable(ild->ild_index);
257 vec_free (il->il_delegates);
258 il->il_delegates = NULL;
262 ip6_link_last_lock_gone (ip6_link_t * il)
264 ip6_ll_prefix_t ilp = {
265 .ilp_addr = il->il_ll_addr,
266 .ilp_sw_if_index = il->il_sw_if_index,
269 IP6_LINK_INFO ("last-lock: %U",
270 format_vnet_sw_if_index_name,
271 vnet_get_main (), il->il_sw_if_index);
273 ip6_link_delegate_flush (il);
274 ip6_ll_table_entry_delete (&ilp);
276 ip6_mfib_interface_enable_disable (il->il_sw_if_index, 0);
277 ip6_sw_interface_enable_disable (il->il_sw_if_index, 0);
279 ip6_address_set_zero (&il->il_ll_addr);
280 adj_unlock (il->il_mcast_adj);
281 il->il_mcast_adj = ADJ_INDEX_INVALID;
285 ip6_link_unlock (ip6_link_t * il)
292 if (0 == il->il_locks)
293 ip6_link_last_lock_gone (il);
297 ip6_link_disable (u32 sw_if_index)
301 il = ip6_link_get (sw_if_index);
304 return (VNET_API_ERROR_IP6_NOT_ENABLED);
306 IP6_LINK_INFO ("disable: %U",
307 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index);
309 ip6_link_unlock (il);
314 const ip6_address_t *
315 ip6_get_link_local_address (u32 sw_if_index)
317 const ip6_link_t *il;
319 vec_validate (ip6_links, sw_if_index);
321 il = &ip6_links[sw_if_index];
323 return (&il->il_ll_addr);
327 ip6_link_get_mcast_adj (u32 sw_if_index)
329 const ip6_link_t *il;
331 il = &ip6_links[sw_if_index];
333 return (il->il_mcast_adj);
337 ip6_src_address_for_packet (u32 sw_if_index,
338 const ip6_address_t * dst, ip6_address_t * src)
340 ip_lookup_main_t *lm;
342 lm = &ip6_main.lookup_main;
344 if (ip6_address_is_link_local_unicast (dst))
346 ip6_address_copy (src, ip6_get_link_local_address (sw_if_index));
353 lm->if_address_pool_index_by_sw_if_index[sw_if_index];
354 if (PREDICT_TRUE (if_add_index != ~0))
356 ip_interface_address_t *if_add =
357 pool_elt_at_index (lm->if_address_pool, if_add_index);
358 ip6_address_t *if_ip =
359 ip_interface_address_get_address (lm, if_add);
372 ip6_link_set_local_address (u32 sw_if_index, const ip6_address_t * address)
374 ip6_link_delegate_t *ild;
377 il = ip6_link_get (sw_if_index);
380 return ip6_link_enable (sw_if_index, address);
382 ip6_ll_prefix_t ilp = {
383 .ilp_addr = il->il_ll_addr,
384 .ilp_sw_if_index = sw_if_index,
387 IP6_LINK_INFO ("set-ll: %U -> %U",
388 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
389 format_ip6_address, address);
391 ip6_ll_table_entry_delete (&ilp);
392 ip6_address_copy (&il->il_ll_addr, address);
393 ip6_address_copy (&ilp.ilp_addr, address);
394 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
397 FOREACH_IP6_LINK_DELEGATE (ild, il,
399 if (NULL != il_delegate_vfts[ild->ild_type].ildv_ll_change)
400 il_delegate_vfts[ild->ild_type].ildv_ll_change(ild->ild_index,
408 ip6_link_delegate_id_t
409 ip6_link_delegate_register (const ip6_link_delegate_vft_t * vft)
411 ip6_link_delegate_id_t rc = il_delegate_id++;
413 ASSERT (vft->ildv_disable);
415 vec_validate (il_delegate_vfts, rc);
417 il_delegate_vfts[rc] = *vft;
423 ip6_link_delegate_get (u32 sw_if_index, ip6_link_delegate_id_t id)
427 il = ip6_link_get (sw_if_index);
430 return (INDEX_INVALID);
432 vec_validate_init_empty (il->il_delegates, id, ip6_link_delegate_uninit);
434 if (!ip6_link_delegate_is_init (&il->il_delegates[id]))
435 return (INDEX_INVALID);
437 return (il->il_delegates[id].ild_index);
441 ip6_link_delegate_update (u32 sw_if_index,
442 ip6_link_delegate_id_t id, index_t ii)
446 il = ip6_link_get (sw_if_index);
451 vec_validate_init_empty (il->il_delegates, id, ip6_link_delegate_uninit);
453 il->il_delegates[id].ild_sw_if_index = sw_if_index;
454 il->il_delegates[id].ild_type = id;
455 il->il_delegates[id].ild_index = ii;
461 ip6_link_delegate_remove (u32 sw_if_index,
462 ip6_link_delegate_id_t id, index_t ii)
466 il = ip6_link_get (sw_if_index);
470 if (vec_len (il->il_delegates) > id)
472 clib_memcpy (&il->il_delegates[id],
473 &ip6_link_delegate_uninit,
474 sizeof (il->il_delegates[0]));
480 ip6_link_add_del_address (ip6_main_t * im,
483 ip6_address_t * address,
485 u32 if_address_index, u32 is_delete)
487 const ip6_link_delegate_t *ild;
490 if (ip6_address_is_link_local_unicast (address))
491 // only interested in global addresses here
494 IP6_LINK_INFO ("addr-%s: %U -> %U",
495 (is_delete ? "del" : "add"),
496 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
497 format_ip6_address, address);
499 il = ip6_link_get (sw_if_index);
505 FOREACH_IP6_LINK_DELEGATE (ild, il,
509 if (NULL != il_delegate_vfts[ild->ild_type].ildv_addr_del)
510 il_delegate_vfts[ild->ild_type].ildv_addr_del(ild->ild_index,
511 address, address_length);
515 if (NULL != il_delegate_vfts[ild->ild_type].ildv_addr_add)
516 il_delegate_vfts[ild->ild_type].ildv_addr_add(ild->ild_index,
517 address, address_length);
523 static clib_error_t *
524 ip6_link_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
530 il = ip6_link_get (sw_if_index);
532 IP6_LINK_DBG ("link-del: %U",
533 format_vnet_sw_if_index_name, vnet_get_main (),
538 ip6_link_last_lock_gone (il);
544 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_link_interface_add_del);
546 static clib_error_t *
547 ip6_link_init (vlib_main_t * vm)
549 il_randomizer = clib_cpu_time_now ();
550 ip6_link_logger = vlib_log_register_class ("ip6", "link");
553 ip6_add_del_interface_address_callback_t cb = {
554 .function = ip6_link_add_del_address,
556 vec_add1 (ip6_main.add_del_interface_address_callbacks, cb);
561 VLIB_INIT_FUNCTION (ip6_link_init);
564 static clib_error_t *
565 test_ip6_link_command_fn (vlib_main_t * vm,
566 unformat_input_t * input, vlib_cli_command_t * cmd)
569 ip6_address_t _a, *a = &_a;
571 if (unformat (input, "%U", unformat_ethernet_address, mac))
573 ip6_link_local_address_from_mac (a, mac);
574 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
575 ip6_mac_address_from_link_local (mac, a);
576 vlib_cli_output (vm, "Original MAC address: %U",
577 format_ethernet_address, mac);
584 * This command converts the given MAC Address into an IPv6 link-local
588 * Example of how to create an IPv6 link-local address:
589 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
590 * Link local address: fe80::14d9:e0ff:fe91:7986
591 * Original MAC address: 16:d9:e0:91:79:86
595 VLIB_CLI_COMMAND (test_link_command, static) =
597 .path = "test ip6 link",
598 .function = test_ip6_link_command_fn,
599 .short_help = "test ip6 link <mac-address>",
604 ip6_print_addrs (u8 * s, u32 * addrs)
606 ip_lookup_main_t *lm = &ip6_main.lookup_main;
609 for (i = 0; i < vec_len (addrs); i++)
611 ip_interface_address_t *a =
612 pool_elt_at_index (lm->if_address_pool, addrs[i]);
613 ip6_address_t *address = ip_interface_address_get_address (lm, a);
615 s = format (s, "%U%U/%d\n",
616 format_white_space, 4,
617 format_ip6_address, address, a->address_length);
624 format_ip6_link (u8 * s, va_list * arg)
626 const ip6_link_t *il = va_arg (*arg, ip6_link_t *);
627 ip_lookup_main_t *lm = &ip6_main.lookup_main;
628 vnet_main_t *vnm = vnet_get_main ();
630 if (!ip6_link_is_enabled_i (il))
633 s = format (s, "%U is admin %s\n",
634 format_vnet_sw_interface_name, vnm,
635 vnet_get_sw_interface (vnm, il->il_sw_if_index),
636 (vnet_sw_interface_is_admin_up (vnm, il->il_sw_if_index) ?
640 u32 *link_scope = 0, *global_scope = 0;
641 u32 *local_scope = 0, *unknown_scope = 0;
642 ip_interface_address_t *a;
643 const ip6_link_delegate_t *ild;
645 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
646 il->il_sw_if_index, ~0);
647 ai = lm->if_address_pool_index_by_sw_if_index[il->il_sw_if_index];
649 while (ai != (u32) ~ 0)
651 a = pool_elt_at_index (lm->if_address_pool, ai);
652 ip6_address_t *address = ip_interface_address_get_address (lm, a);
654 if (ip6_address_is_link_local_unicast (address))
655 vec_add1 (link_scope, ai);
656 else if (ip6_address_is_global_unicast (address))
657 vec_add1 (global_scope, ai);
658 else if (ip6_address_is_local_unicast (address))
659 vec_add1 (local_scope, ai);
661 vec_add1 (unknown_scope, ai);
663 ai = a->next_this_sw_interface;
666 if (vec_len (link_scope))
668 s = format (s, "%ULink-local address(es):\n", format_white_space, 2);
669 s = ip6_print_addrs (s, link_scope);
670 vec_free (link_scope);
673 if (vec_len (local_scope))
675 s = format (s, "%ULocal unicast address(es):\n", format_white_space, 2);
676 s = ip6_print_addrs (s, local_scope);
677 vec_free (local_scope);
680 if (vec_len (global_scope))
682 s = format (s, "%UGlobal unicast address(es):\n",
683 format_white_space, 2);
684 s = ip6_print_addrs (s, global_scope);
685 vec_free (global_scope);
688 if (vec_len (unknown_scope))
690 s = format (s, "%UOther-scope address(es):\n", format_white_space, 2);
691 s = ip6_print_addrs (s, unknown_scope);
692 vec_free (unknown_scope);
695 s = format (s, "%ULink-local address(es):\n", format_white_space, 2);
696 s = format (s, "%U%U\n",
697 format_white_space, 4, format_ip6_address, &il->il_ll_addr);
700 FOREACH_IP6_LINK_DELEGATE(ild, il,
702 s = format (s, "%U", il_delegate_vfts[ild->ild_type].ildv_format,
710 static clib_error_t *
711 ip6_link_show (vlib_main_t * vm,
712 unformat_input_t * input, vlib_cli_command_t * cmd)
714 const ip6_link_t *il;
718 vnm = vnet_get_main ();
721 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
723 il = ip6_link_get (sw_if_index);
727 vlib_cli_output (vm, "IP6 disabled");
731 vlib_cli_output (vm, "%U", format_ip6_link, il);
735 vec_foreach (il, ip6_links)
736 vlib_cli_output (vm, "%U", format_ip6_link, il);
743 * This command is used to display various IPv6 attributes on a given
747 * Example of how to display IPv6 settings:
748 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
749 * GigabitEthernet2/0/0 is admin up
750 * Link-local address(es):
752 * Joined group address(es):
757 * Advertised Prefixes:
758 * prefix fe80::fe:28ff:fe9c:75b3, length 64
760 * ICMP error messages are unlimited
761 * ICMP redirects are disabled
762 * ICMP unreachables are not sent
764 * ND advertised reachable time is 0
765 * ND advertised retransmit interval is 0 (msec)
766 * ND router advertisements are sent every 200 seconds (min interval is 150)
767 * ND router advertisements live for 600 seconds
768 * Hosts use stateless autoconfig for addresses
769 * ND router advertisements sent 19336
770 * ND router solicitations received 0
771 * ND router solicitations dropped 0
773 * Example of output if IPv6 is not enabled on the interface:
774 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
775 * show ip6 interface: IPv6 not enabled on interface
779 VLIB_CLI_COMMAND (ip6_link_show_command, static) =
781 .path = "show ip6 interface",
782 .function = ip6_link_show,
783 .short_help = "show ip6 interface <interface>",
787 static clib_error_t *
788 enable_ip6_interface_cmd (vlib_main_t * vm,
789 unformat_input_t * input, vlib_cli_command_t * cmd)
791 vnet_main_t *vnm = vnet_get_main ();
792 clib_error_t *error = NULL;
797 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
799 if (ip6_link_enable (sw_if_index, NULL))
800 error = clib_error_return (0, "Failed\n");
804 error = clib_error_return (0, "unknown interface\n'",
805 format_unformat_error, input);
812 * This command is used to enable IPv6 on a given interface.
815 * Example of how enable IPv6 on a given interface:
816 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
819 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
821 .path = "enable ip6 interface",
822 .function = enable_ip6_interface_cmd,
823 .short_help = "enable ip6 interface <interface>",
827 static clib_error_t *
828 disable_ip6_interface_cmd (vlib_main_t * vm,
829 unformat_input_t * input, vlib_cli_command_t * cmd)
831 vnet_main_t *vnm = vnet_get_main ();
832 clib_error_t *error = NULL;
837 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
839 if (ip6_link_disable (sw_if_index))
840 error = clib_error_return (0, "Failed\n");
844 error = clib_error_return (0, "unknown interface\n'",
845 format_unformat_error, input);
852 * This command is used to disable IPv6 on a given interface.
855 * Example of how disable IPv6 on a given interface:
856 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
859 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
861 .path = "disable ip6 interface",
862 .function = disable_ip6_interface_cmd,
863 .short_help = "disable ip6 interface <interface>",
868 * fd.io coding-style-patch-verification: ON
871 * eval: (c-set-style "gnu")