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 * This code supports the following sixrd modes:
18 * 32 EA bits (Complete IPv4 address is embedded):
20 * IPv4 suffix is embedded:
22 * No embedded address bits (1:1 mode):
27 #include <vnet/plugin/plugin.h>
28 #include <vlibapi/api.h>
29 #include <vlibmemory/api.h>
30 #include <vnet/adj/adj.h>
31 #include <vnet/fib/fib_table.h>
32 #include <vnet/fib/ip6_fib.h>
33 #include <vnet/adj/adj_midchain.h>
34 #include <vnet/adj/adj_delegate.h>
35 #include <vnet/dpo/lookup_dpo.h>
36 #include <vpp/app/version.h> // Really needed?
38 /* define message IDs */
39 #include "sixrd_msg_enum.h"
41 /* define message structures */
43 #include "sixrd_all_api_h.h"
46 /* define generated endian-swappers */
48 #include "sixrd_all_api_h.h"
51 /* instantiate all the print functions we know about */
52 #define vl_print(handle, ...) vlib_cli_output(handle, __VA_ARGS__)
54 #include "sixrd_all_api_h.h"
57 /* Get the API version number */
58 #define vl_api_version(n, v) static u32 api_version = (v);
59 #include "sixrd_all_api_h.h"
62 #define REPLY_MSG_ID_BASE sm->msg_id_base
63 #include <vlibapi/api_helper_macros.h>
65 extern vlib_node_registration_t ip4_sixrd_node;
70 typedef struct sixrd_adj_delegate_t_
78 * Linnkage to the FIB node graph
80 fib_node_t sixrd_node;
83 * tracking of the IPv4 next-hop
85 fib_node_index_t sixrd_fib_entry_index;
88 * sibling on the fib-entry
91 } sixrd_adj_delegate_t;
94 * Pool of delegate structs
96 static sixrd_adj_delegate_t *sixrd_adj_delegate_pool;
99 * Adj delegate registered type
101 static adj_delegate_type_t sixrd_adj_delegate_type;
104 * FIB node registered type
106 static fib_node_type_t sixrd_fib_node_type;
108 static inline sixrd_adj_delegate_t *
109 sixrd_adj_from_base (adj_delegate_t * ad)
115 return (pool_elt_at_index (sixrd_adj_delegate_pool, ad->ad_index));
118 static inline const sixrd_adj_delegate_t *
119 sixrd_adj_from_const_base (const adj_delegate_t * ad)
125 return (pool_elt_at_index (sixrd_adj_delegate_pool, ad->ad_index));
128 sixrd_main_t sixrd_main;
131 sixrd_fixup (vlib_main_t * vm,
132 ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
134 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
135 ip6_header_t *ip6 = vlib_buffer_get_current (b0) + sizeof (ip4_header_t);
136 const sixrd_tunnel_t *t = data;
138 ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
139 ip4->dst_address.as_u32 =
140 sixrd_get_addr_net (t, ip6->dst_address.as_u64[0]);
141 ip4->checksum = ip4_header_checksum (ip4);
145 ip6ip_fixup (vlib_main_t * vm,
146 ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
148 const sixrd_tunnel_t *t = data;
149 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
150 ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
151 ip4->dst_address.as_u32 =
152 sixrd_get_addr_net (t, adj->sub_type.nbr.next_hop.as_u64[0]);
153 ip4->checksum = ip4_header_checksum (ip4);
156 static sixrd_tunnel_t *
157 find_tunnel_by_sw_if_index (u32 sw_if_index)
159 sixrd_main_t *sm = &sixrd_main;
160 u32 ti = sm->tunnel_index_by_sw_if_index[sw_if_index];
163 clib_warning ("Not our tunnel\n");
166 return pool_elt_at_index (sm->tunnels, ti);
169 static sixrd_tunnel_t *
170 find_tunnel (ip6_address_t * ip6_prefix)
172 sixrd_main_t *sm = &sixrd_main;
176 pool_foreach (d, sm->tunnels,
178 if (!memcmp (&d->ip6_prefix, ip6_prefix, 16))
187 sixrd_build_rewrite (vnet_main_t * vnm,
189 vnet_link_t link_type, const void *dst_address)
194 t = find_tunnel_by_sw_if_index (sw_if_index);
200 vec_validate (rewrite, sizeof (ip4_header_t) - 1);
201 ip4_header_t *ip4 = (ip4_header_t *) rewrite;
202 ip4->ip_version_and_header_length = 0x45;
204 ip4->protocol = IP_PROTOCOL_IPV6;
205 /* fixup ip4 header length and checksum after-the-fact */
206 ip4->src_address.as_u32 = t->ip4_src.as_u32;
207 ip4->dst_address.as_u32 = 0;
208 ip4->checksum = ip4_header_checksum (ip4);
214 ip6ip_tunnel_stack (adj_index_t ai, u32 fib_entry_index)
216 sixrd_main_t *sm = &sixrd_main;
217 ip_adjacency_t *adj = adj_get (ai);
219 u32 sw_if_index = adj->rewrite_header.sw_if_index;
221 if ((vec_len (sm->tunnel_index_by_sw_if_index) < sw_if_index) ||
222 (~0 == sm->tunnel_index_by_sw_if_index[sw_if_index]))
226 pool_elt_at_index (sm->tunnels,
227 sm->tunnel_index_by_sw_if_index[sw_if_index]);
230 * find the adjacency that is contributed by the FIB entry
231 * that this tunnel resolves via, and use it as the next adj
234 if (vnet_hw_interface_get_flags (vnet_get_main (), t->hw_if_index) &
235 VNET_HW_INTERFACE_FLAG_LINK_UP)
237 adj_nbr_midchain_stack (ai,
238 fib_entry_contribute_ip_forwarding
243 adj_nbr_midchain_unstack (ai);
248 sixrd_tunnel_stack (adj_index_t ai, u32 fib_index)
250 dpo_id_t dpo = DPO_INVALID;
251 sixrd_main_t *sm = &sixrd_main;
252 ip_adjacency_t *adj = adj_get (ai);
253 u32 sw_if_index = adj->rewrite_header.sw_if_index;
255 if ((vec_len (sm->tunnel_index_by_sw_if_index) < sw_if_index) ||
256 (~0 == sm->tunnel_index_by_sw_if_index[sw_if_index]))
259 if ((vec_len (sm->tunnel_index_by_sw_if_index) < sw_if_index) ||
260 (sm->tunnel_index_by_sw_if_index[sw_if_index] == ~0))
263 lookup_dpo_add_or_lock_w_fib_index (fib_index, DPO_PROTO_IP4,
264 LOOKUP_UNICAST, LOOKUP_INPUT_DST_ADDR,
265 LOOKUP_TABLE_FROM_CONFIG, &dpo);
266 adj_nbr_midchain_stack (ai, &dpo);
269 const static ip46_address_t sixrd_special_nh = {
272 [0] = 0xffffffffffffffff,
273 [1] = 0xffffffffffffffff,
279 sixrd_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
281 ip_adjacency_t *adj = adj_get (ai);
282 sixrd_tunnel_t *t = find_tunnel_by_sw_if_index (sw_if_index);
284 if (!memcmp (&sixrd_special_nh,
285 &adj->sub_type.nbr.next_hop, sizeof (sixrd_special_nh)))
287 adj_nbr_midchain_update_rewrite (ai, sixrd_fixup, t, ADJ_FLAG_NONE,
288 sixrd_build_rewrite (vnm, sw_if_index,
291 sixrd_tunnel_stack (ai, t->fib_index);
295 sixrd_adj_delegate_t *sixrd_ad;
299 sixrd_get_addr_net (t, adj->sub_type.nbr.next_hop.as_u64[0]);
302 .fp_proto = FIB_PROTOCOL_IP4,
304 .fp_addr = {.ip4 = da4,}
308 adj_nbr_midchain_update_rewrite
311 sixrd_build_rewrite (vnm, sw_if_index,
312 adj_get_link_type (ai), NULL));
315 sixrd_adj_from_base (adj_delegate_get (adj, sixrd_adj_delegate_type));
316 if (NULL == sixrd_ad)
318 pool_get (sixrd_adj_delegate_pool, sixrd_ad);
319 fib_node_init (&sixrd_ad->sixrd_node, sixrd_fib_node_type);
320 sixrd_ad->adj_index = ai;
321 sixrd_ad->sixrd_fib_entry_index =
322 fib_table_entry_special_add (t->fib_index, &pfx,
323 FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
324 sixrd_ad->sixrd_sibling =
325 fib_entry_child_add (sixrd_ad->sixrd_fib_entry_index,
327 sixrd_ad - sixrd_adj_delegate_pool);
329 adj_delegate_add (adj,
330 sixrd_adj_delegate_type,
331 sixrd_ad - sixrd_adj_delegate_pool);
333 ip6ip_tunnel_stack (ai, sixrd_ad->sixrd_fib_entry_index);
340 sixrd_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
343 vnet_hw_interface_set_flags (vnm, hw_if_index,
344 VNET_HW_INTERFACE_FLAG_LINK_UP);
345 return /* no error */ 0;
349 VNET_HW_INTERFACE_CLASS (sixrd_hw_interface_class) =
352 .build_rewrite = sixrd_build_rewrite,
353 .update_adjacency = sixrd_update_adj,
356 VNET_DEVICE_CLASS (sixrd_device_class) =
359 .admin_up_down_function = sixrd_interface_admin_up_down,
367 sixrd_create_tunnel (ip6_address_t * ip6_prefix, u8 ip6_prefix_len,
368 ip4_address_t * ip4_prefix, u8 ip4_prefix_len,
369 ip4_address_t * ip4_src, u16 mtu, bool security_check,
370 u32 fib_index, u32 * sixrd_tunnel_index)
372 sixrd_main_t *sm = &sixrd_main;
376 return VNET_API_ERROR_NO_SUCH_FIB;
378 if ((ip6_prefix_len + 32 - ip4_prefix_len) > 64)
379 return VNET_API_ERROR_INVALID_VALUE;
381 /* Tunnel already configured */
382 if (find_tunnel (ip6_prefix))
383 return VNET_API_ERROR_INVALID_VALUE;
384 /* Tunnel already configured for this source */
385 if (find_tunnel_by_ip4_address (ip4_src))
386 return VNET_API_ERROR_INVALID_VALUE;
388 /* Get tunnel index */
389 pool_get_aligned (sm->tunnels, t, CLIB_CACHE_LINE_BYTES);
390 memset (t, 0, sizeof (*t));
391 t->tunnel_index = t - sm->tunnels;
393 /* Init tunnel struct */
394 t->ip4_prefix.as_u32 = ip4_prefix->as_u32;
395 t->ip4_prefix_len = ip4_prefix_len;
396 t->ip6_prefix = *ip6_prefix;
397 t->ip6_prefix_len = ip6_prefix_len;
398 t->ip4_src = *ip4_src;
399 t->mtu = mtu ? mtu : SIXRD_DEFAULT_MTU;
400 t->security_check = security_check;
402 t->shift = (ip4_prefix_len < 32) ?
403 64 - ip6_prefix_len - (32 - ip4_prefix_len) : 0;
405 /* Create interface */
406 u32 hw_if_index = vnet_register_interface (vnet_get_main (),
407 sixrd_device_class.index,
409 sixrd_hw_interface_class.index,
412 /* Default the interface to up and enable IPv6 (payload) */
413 vnet_hw_interface_t *hi =
414 vnet_get_hw_interface (vnet_get_main (), hw_if_index);
415 t->hw_if_index = hw_if_index;
416 t->fib_index = fib_index;
417 t->sw_if_index = hi->sw_if_index;
419 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
422 vec_validate_init_empty (sm->tunnel_index_by_sw_if_index, hi->sw_if_index,
424 sm->tunnel_index_by_sw_if_index[hi->sw_if_index] = t->tunnel_index;
426 hash_set (sm->tunnel_by_ip, ip4_src->as_u32, t->tunnel_index);
428 vnet_hw_interface_set_flags (vnet_get_main (), hw_if_index,
429 VNET_HW_INTERFACE_FLAG_LINK_UP);
430 vnet_sw_interface_set_flags (vnet_get_main (), hi->sw_if_index,
431 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
433 /* Create IPv6 route/adjacency */
434 fib_prefix_t pfx6 = {
435 .fp_proto = FIB_PROTOCOL_IP6,
436 .fp_len = t->ip6_prefix_len,
437 .fp_addr = {.ip6 = t->ip6_prefix,}
441 fib_table_entry_update_one_path (fib_index, &pfx6,
442 FIB_SOURCE_CLI, FIB_ENTRY_FLAG_ATTACHED,
443 DPO_PROTO_IP6, &sixrd_special_nh,
445 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
447 *sixrd_tunnel_index = hi->sw_if_index;
449 ip4_register_protocol (IP_PROTOCOL_IPV6, ip4_sixrd_node.index);
455 * sixrd_delete_tunnel
458 sixrd_delete_tunnel (u32 sw_if_index)
460 sixrd_main_t *sm = &sixrd_main;
461 sixrd_tunnel_t *t = find_tunnel_by_sw_if_index (sw_if_index);
465 clib_warning ("SIXRD tunnel delete: tunnel does not exist: %d",
470 fib_prefix_t pfx6 = {
471 .fp_proto = FIB_PROTOCOL_IP6,
472 .fp_len = t->ip6_prefix_len,
473 .fp_addr = {.ip6 = t->ip6_prefix,}
476 fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_CLI);
477 vnet_sw_interface_set_flags (vnet_get_main (), t->sw_if_index,
480 sm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
482 hash_unset (sm->tunnel_by_ip, t->ip4_src.as_u32);
484 vnet_delete_hw_interface (vnet_get_main (), t->hw_if_index);
486 pool_put (sm->tunnels, t);
491 static clib_error_t *
492 sixrd_add_del_tunnel_command_fn (vlib_main_t * vm,
493 unformat_input_t * input,
494 vlib_cli_command_t * cmd)
496 unformat_input_t _line_input, *line_input = &_line_input;
497 ip4_address_t ip4_prefix;
498 ip6_address_t ip6_prefix;
499 ip4_address_t ip4_src;
500 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, sixrd_tunnel_index;
502 /* Optional arguments */
505 clib_error_t *error = 0;
506 bool is_add = true, security_check = false;
508 /* Get a line of input. */
509 if (!unformat_user (input, unformat_line_input, line_input))
511 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
513 if (unformat (line_input, "del"))
515 if (unformat (line_input, "security-check"))
516 security_check = true;
519 (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
522 else if (unformat (line_input, "ip4-pfx %U/%d", unformat_ip4_address,
523 &ip4_prefix, &ip4_prefix_len))
527 (line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
529 else if (unformat (line_input, "mtu %d", &mtu))
531 else if (unformat (line_input, "fib-id %d", &fib_index))
536 clib_error_return (0, "unknown input `%U'", format_unformat_error,
544 error = clib_error_return (0, "mandatory argument(s) missing");
549 int rv = sixrd_create_tunnel (&ip6_prefix, ip6_prefix_len, &ip4_prefix,
550 ip4_prefix_len, &ip4_src, mtu,
552 fib_index, &sixrd_tunnel_index);
554 error = clib_error_return (0, "adding tunnel failed");
558 sixrd_tunnel_t *t = find_tunnel (&ip6_prefix);
561 sixrd_delete_tunnel (t->sw_if_index);
566 unformat_free (line_input);
572 VLIB_CLI_COMMAND (sixrd_add_del_tunnel_command, static) =
574 .path = "create 6rd tunnel",
575 .short_help = "create 6rd tunnel ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> "
576 "ip4-src <ip4-addr> [del]",
577 .function = sixrd_add_del_tunnel_command_fn,
582 vl_api_sixrd_add_tunnel_t_handler (vl_api_sixrd_add_tunnel_t * mp)
584 sixrd_main_t *sm = &sixrd_main;
585 vl_api_sixrd_add_tunnel_reply_t *rmp;
586 u32 sixrd_tunnel_index;
588 int rv = sixrd_create_tunnel ((ip6_address_t *) & mp->ip6_prefix,
590 (ip4_address_t *) & mp->ip4_prefix,
592 (ip4_address_t *) & mp->ip4_src,
594 mp->security_check, ntohl (mp->fib_index),
595 &sixrd_tunnel_index);
597 REPLY_MACRO2 (VL_API_SIXRD_ADD_TUNNEL_REPLY, (
600 htonl (sixrd_tunnel_index);
605 vl_api_sixrd_del_tunnel_t_handler (vl_api_sixrd_del_tunnel_t * mp)
607 sixrd_main_t *sm = &sixrd_main;
608 vl_api_sixrd_del_tunnel_reply_t *rmp;
610 int rv = sixrd_delete_tunnel (ntohl (mp->sw_if_index));
612 REPLY_MACRO (VL_API_SIXRD_DEL_TUNNEL_REPLY);
615 /* List of message types that this plugin understands */
617 #define foreach_sixrd_plugin_api_msg \
618 _(SIXRD_ADD_TUNNEL, sixrd_add_tunnel) \
619 _(SIXRD_DEL_TUNNEL, sixrd_del_tunnel)
622 VLIB_PLUGIN_REGISTER() = {
623 .version = VPP_BUILD_VER,
624 .description = "IPv6 Rapid Deployment on IPv4 Infrastructure (RFC5969)",
629 * @brief Set up the API message handling tables.
631 static clib_error_t *
632 sixrd_plugin_api_hookup (vlib_main_t * vm)
634 sixrd_main_t *sm = &sixrd_main;
637 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), #n, \
638 vl_api_##n##_t_handler, vl_noop_handler, \
639 vl_api_##n##_t_endian, vl_api_##n##_t_print, \
640 sizeof(vl_api_##n##_t), 1);
641 foreach_sixrd_plugin_api_msg;
647 #define vl_msg_name_crc_list
648 #include "sixrd_all_api_h.h"
649 #undef vl_msg_name_crc_list
652 setup_message_id_table (sixrd_main_t * sm, api_main_t * am)
654 #define _(id, n, crc) \
655 vl_msg_api_add_msg_name_crc(am, #n "_" #crc, id + sm->msg_id_base);
656 foreach_vl_msg_name_crc_sixrd;
661 sixrd_adj_delegate_adj_deleted (adj_delegate_t * aed)
663 sixrd_adj_delegate_t *sixrd_ad;
665 sixrd_ad = sixrd_adj_from_base (aed);
667 fib_entry_child_remove (sixrd_ad->sixrd_fib_entry_index,
668 sixrd_ad->sixrd_sibling);
669 fib_table_entry_delete_index (sixrd_ad->sixrd_fib_entry_index,
672 pool_put (sixrd_adj_delegate_pool, sixrd_ad);
676 sixrd_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
678 const sixrd_adj_delegate_t *sixrd_ad;
680 sixrd_ad = sixrd_adj_from_const_base (aed);
682 s = format (s, "SIXRD:[fib-entry:%d]", sixrd_ad->sixrd_fib_entry_index);
688 sixrd_fib_node_last_lock_gone (fib_node_t * node)
690 // top of the dependency tree, locks not managed here.
693 static sixrd_adj_delegate_t *
694 sixrd_adj_delegate_from_fib_node (fib_node_t * node)
696 return ((sixrd_adj_delegate_t *) (((char *) node) -
697 STRUCT_OFFSET_OF (sixrd_adj_delegate_t,
701 static fib_node_back_walk_rc_t
702 sixrd_fib_node_back_walk_notify (fib_node_t * node,
703 fib_node_back_walk_ctx_t * ctx)
705 sixrd_adj_delegate_t *sixrd_ad;
707 sixrd_ad = sixrd_adj_delegate_from_fib_node (node);
709 ip6ip_tunnel_stack (sixrd_ad->adj_index, sixrd_ad->sixrd_fib_entry_index);
711 return (FIB_NODE_BACK_WALK_CONTINUE);
715 * Function definition to get a FIB node from its index
718 sixrd_fib_node_get (fib_node_index_t index)
720 sixrd_adj_delegate_t *sixrd_ad;
722 sixrd_ad = pool_elt_at_index (sixrd_adj_delegate_pool, index);
724 return (&sixrd_ad->sixrd_node);
728 * VFT registered with the adjacency delegate
730 const static adj_delegate_vft_t sixrd_adj_delegate_vft = {
731 .adv_adj_deleted = sixrd_adj_delegate_adj_deleted,
732 .adv_format = sixrd_adj_delegate_format,
736 * VFT registered with the FIB node for the adj delegate
738 const static fib_node_vft_t sixrd_fib_node_vft = {
739 .fnv_get = sixrd_fib_node_get,
740 .fnv_last_lock = sixrd_fib_node_last_lock_gone,
741 .fnv_back_walk = sixrd_fib_node_back_walk_notify,
744 static clib_error_t *
745 sixrd_init (vlib_main_t * vm)
747 sixrd_main_t *sm = &sixrd_main;
748 clib_error_t *error = 0;
751 name = format (0, "sixrd_%08x%c", api_version, 0);
754 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
756 error = sixrd_plugin_api_hookup (vm);
758 setup_message_id_table (sm, &api_main);
760 sixrd_adj_delegate_type =
761 adj_delegate_register_new_type (&sixrd_adj_delegate_vft);
762 sixrd_fib_node_type = fib_node_register_new_type (&sixrd_fib_node_vft);
767 VLIB_INIT_FUNCTION (sixrd_init);
770 * fd.io coding-style-patch-verification: ON
773 * eval: (c-set-style "gnu")