2 * sixrd.c - 6RD specific functions (RFC5969)
4 * Copyright (c) 2018 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * This code supports the following sixrd modes:
21 * 32 EA bits (Complete IPv4 address is embedded):
23 * IPv4 suffix is embedded:
25 * No embedded address bits (1:1 mode):
30 #include <vlibapi/api.h>
31 #include <vlibmemory/api.h>
32 #include <vnet/adj/adj.h>
33 #include <vnet/adj/adj_delegate.h>
34 #include <vnet/adj/adj_midchain.h>
35 #include <vnet/dpo/lookup_dpo.h>
36 #include <vnet/fib/fib_table.h>
37 #include <vnet/fib/ip6_fib.h>
38 #include <vnet/plugin/plugin.h>
40 extern vlib_node_registration_t ip4_sixrd_node;
45 typedef struct sixrd_adj_delegate_t_
48 fib_node_t sixrd_node;
49 fib_node_index_t sixrd_fib_entry_index;
51 } sixrd_adj_delegate_t;
54 * Pool of delegate structs
56 static sixrd_adj_delegate_t *sixrd_adj_delegate_pool;
59 * Adj delegate registered type
61 static adj_delegate_type_t sixrd_adj_delegate_type;
64 * FIB node registered type
66 static fib_node_type_t sixrd_fib_node_type;
68 static inline sixrd_adj_delegate_t *
69 sixrd_adj_from_base (adj_delegate_t * ad)
73 return (pool_elt_at_index (sixrd_adj_delegate_pool, ad->ad_index));
76 static inline const sixrd_adj_delegate_t *
77 sixrd_adj_from_const_base (const adj_delegate_t * ad)
83 return (pool_elt_at_index (sixrd_adj_delegate_pool, ad->ad_index));
87 sixrd_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b0,
90 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
91 ip6_header_t *ip6 = vlib_buffer_get_current (b0) + sizeof (ip4_header_t);
92 const ipip_tunnel_t *t = data;
94 ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
95 ip4->dst_address.as_u32 =
96 sixrd_get_addr_net (t, ip6->dst_address.as_u64[0]);
97 ip4->checksum = ip4_header_checksum (ip4);
101 ip6ip_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b0,
104 const ipip_tunnel_t *t = data;
105 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
106 ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
107 ip4->dst_address.as_u32 =
108 sixrd_get_addr_net (t, adj->sub_type.nbr.next_hop.as_u64[0]);
109 ip4->checksum = ip4_header_checksum (ip4);
113 sixrd_build_rewrite (vnet_main_t * vnm, u32 sw_if_index,
114 vnet_link_t link_type, const void *dst_address)
119 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
123 vec_validate (rewrite, sizeof (ip4_header_t) - 1);
124 ip4_header_t *ip4 = (ip4_header_t *) rewrite;
125 ip4->ip_version_and_header_length = 0x45;
127 ip4->protocol = IP_PROTOCOL_IPV6;
128 /* fixup ip4 header length and checksum after-the-fact */
129 ip4->src_address.as_u32 = t->tunnel_src.ip4.as_u32;
130 ip4->dst_address.as_u32 = 0;
131 ip4->checksum = ip4_header_checksum (ip4);
137 ip6ip_tunnel_stack (adj_index_t ai, u32 fib_entry_index)
139 ip_adjacency_t *adj = adj_get (ai);
141 u32 sw_if_index = adj->rewrite_header.sw_if_index;
143 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
148 * find the adjacency that is contributed by the FIB entry
149 * that this tunnel resolves via, and use it as the next adj
152 if (vnet_hw_interface_get_flags (vnet_get_main (), t->hw_if_index) &
153 VNET_HW_INTERFACE_FLAG_LINK_UP)
155 adj_nbr_midchain_stack (ai,
156 fib_entry_contribute_ip_forwarding
161 adj_nbr_midchain_unstack (ai);
166 sixrd_tunnel_stack (adj_index_t ai, u32 fib_index)
168 dpo_id_t dpo = DPO_INVALID;
169 ip_adjacency_t *adj = adj_get (ai);
170 u32 sw_if_index = adj->rewrite_header.sw_if_index;
172 ipip_tunnel_t *t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
176 lookup_dpo_add_or_lock_w_fib_index (fib_index, DPO_PROTO_IP4,
177 LOOKUP_UNICAST, LOOKUP_INPUT_DST_ADDR,
178 LOOKUP_TABLE_FROM_CONFIG, &dpo);
179 adj_nbr_midchain_stack (ai, &dpo);
184 sixrd_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
186 ip_adjacency_t *adj = adj_get (ai);
187 ipip_tunnel_t *t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
192 if (IP_LOOKUP_NEXT_BCAST == adj->lookup_next_index)
194 adj_nbr_midchain_update_rewrite (ai, sixrd_fixup, t, ADJ_FLAG_NONE,
195 sixrd_build_rewrite (vnm, sw_if_index,
198 sixrd_tunnel_stack (ai, t->fib_index);
202 sixrd_adj_delegate_t *sixrd_ad;
206 sixrd_get_addr_net (t, adj->sub_type.nbr.next_hop.as_u64[0]);
209 .fp_proto = FIB_PROTOCOL_IP4,
217 adj_nbr_midchain_update_rewrite (ai, ip6ip_fixup, t, ADJ_FLAG_NONE,
218 sixrd_build_rewrite (vnm, sw_if_index,
223 sixrd_adj_from_base (adj_delegate_get (adj, sixrd_adj_delegate_type));
224 if (sixrd_ad == NULL)
226 pool_get (sixrd_adj_delegate_pool, sixrd_ad);
227 fib_node_init (&sixrd_ad->sixrd_node, sixrd_fib_node_type);
228 sixrd_ad->adj_index = ai;
229 sixrd_ad->sixrd_fib_entry_index =
230 fib_table_entry_special_add (t->fib_index, &pfx, FIB_SOURCE_RR,
231 FIB_ENTRY_FLAG_NONE);
232 sixrd_ad->sixrd_sibling =
233 fib_entry_child_add (sixrd_ad->sixrd_fib_entry_index,
235 sixrd_ad - sixrd_adj_delegate_pool);
237 adj_delegate_add (adj, sixrd_adj_delegate_type,
238 sixrd_ad - sixrd_adj_delegate_pool);
240 ip6ip_tunnel_stack (ai, sixrd_ad->sixrd_fib_entry_index);
246 sixrd_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
249 vnet_hw_interface_set_flags (vnm, hw_if_index,
250 VNET_HW_INTERFACE_FLAG_LINK_UP);
251 return /* no error */ 0;
255 VNET_HW_INTERFACE_CLASS(sixrd_hw_interface_class) = {
257 .build_rewrite = sixrd_build_rewrite,
258 .update_adjacency = sixrd_update_adj,
261 VNET_DEVICE_CLASS(sixrd_device_class) = {
263 .admin_up_down_function = sixrd_interface_admin_up_down,
272 sixrd_add_tunnel (ip6_address_t * ip6_prefix, u8 ip6_prefix_len,
273 ip4_address_t * ip4_prefix, u8 ip4_prefix_len,
274 ip4_address_t * ip4_src, bool security_check,
275 u32 fib_index, u32 * sw_if_index)
277 ipip_main_t *gm = &ipip_main;
281 return VNET_API_ERROR_NO_SUCH_FIB;
283 if ((ip6_prefix_len + 32 - ip4_prefix_len) > 64)
284 return VNET_API_ERROR_INVALID_VALUE;
286 /* Tunnel already configured */
287 ip46_address_t src = ip46_address_initializer, dst =
288 ip46_address_initializer;
289 ip_set (&src, ip4_src, true);
290 ipip_tunnel_key_t key = {.transport = IPIP_TRANSPORT_IP4,
291 .fib_index = fib_index,
296 t = ipip_tunnel_db_find (&key);
298 return VNET_API_ERROR_IF_ALREADY_EXISTS;
300 /* Get tunnel index */
301 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
302 memset (t, 0, sizeof (*t));
303 u32 t_idx = t - gm->tunnels; /* tunnel index (or instance) */
305 /* Init tunnel struct */
306 t->mode = IPIP_MODE_6RD;
307 t->sixrd.ip4_prefix.as_u32 = ip4_prefix->as_u32;
308 t->sixrd.ip4_prefix_len = ip4_prefix_len;
309 t->sixrd.ip6_prefix = *ip6_prefix;
310 t->sixrd.ip6_prefix_len = ip6_prefix_len;
312 t->sixrd.security_check = security_check;
314 (ip4_prefix_len < 32) ? 64 - ip6_prefix_len - (32 - ip4_prefix_len) : 0;
316 /* Create interface */
318 vnet_register_interface (vnet_get_main (), sixrd_device_class.index,
320 sixrd_hw_interface_class.index, t_idx);
322 /* Default the interface to up and enable IPv6 (payload) */
323 vnet_hw_interface_t *hi =
324 vnet_get_hw_interface (vnet_get_main (), hw_if_index);
325 t->hw_if_index = hw_if_index;
326 t->fib_index = fib_index;
327 t->sw_if_index = hi->sw_if_index;
328 t->dev_instance = t_idx;
329 t->user_instance = t_idx;
331 vnet_sw_interface_set_mtu (vnet_get_main (), t->sw_if_index, 1480);
333 ipip_tunnel_db_add (t, &key);
335 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, hi->sw_if_index,
337 gm->tunnel_index_by_sw_if_index[hi->sw_if_index] = t_idx;
339 vnet_hw_interface_set_flags (vnet_get_main (), hw_if_index,
340 VNET_HW_INTERFACE_FLAG_LINK_UP);
341 vnet_sw_interface_set_flags (vnet_get_main (), hi->sw_if_index,
342 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
343 ip6_sw_interface_enable_disable (hi->sw_if_index, true);
345 /* Create IPv6 route/adjacency */
346 fib_prefix_t pfx6 = {
347 .fp_proto = FIB_PROTOCOL_IP6,
348 .fp_len = t->sixrd.ip6_prefix_len,
350 .ip6 = t->sixrd.ip6_prefix,
355 fib_table_entry_update_one_path (fib_index, &pfx6, FIB_SOURCE_CLI,
356 FIB_ENTRY_FLAG_ATTACHED, DPO_PROTO_IP6,
357 &ADJ_BCAST_ADDR, hi->sw_if_index, ~0, 1,
358 NULL, FIB_ROUTE_PATH_FLAG_NONE);
360 *sw_if_index = hi->sw_if_index;
362 if (!gm->ip4_protocol_registered)
364 vlib_node_t *ipip4_input =
365 vlib_get_node_by_name (gm->vlib_main, (u8 *) "ipip4-input");
366 ASSERT (ipip4_input);
367 ip4_register_protocol (IP_PROTOCOL_IPV6, ipip4_input->index);
376 sixrd_del_tunnel (u32 sw_if_index)
378 ipip_main_t *gm = &ipip_main;
379 ipip_tunnel_t *t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
383 clib_warning ("SIXRD tunnel delete: tunnel does not exist: %d",
388 fib_prefix_t pfx6 = {
389 .fp_proto = FIB_PROTOCOL_IP6,
390 .fp_len = t->sixrd.ip6_prefix_len,
392 .ip6 = t->sixrd.ip6_prefix,
396 fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_CLI);
397 vnet_sw_interface_set_flags (vnet_get_main (), t->sw_if_index,
399 ip6_sw_interface_enable_disable (t->sw_if_index, false);
400 gm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
402 vnet_delete_hw_interface (vnet_get_main (), t->hw_if_index);
403 ipip_tunnel_db_remove (t);
404 pool_put (gm->tunnels, t);
410 sixrd_adj_delegate_adj_deleted (adj_delegate_t * aed)
412 sixrd_adj_delegate_t *sixrd_ad;
414 sixrd_ad = sixrd_adj_from_base (aed);
415 fib_entry_child_remove (sixrd_ad->sixrd_fib_entry_index,
416 sixrd_ad->sixrd_sibling);
417 fib_table_entry_delete_index (sixrd_ad->sixrd_fib_entry_index,
419 pool_put (sixrd_adj_delegate_pool, sixrd_ad);
423 sixrd_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
425 const sixrd_adj_delegate_t *sixrd_ad;
427 sixrd_ad = sixrd_adj_from_const_base (aed);
428 s = format (s, "SIXRD:[fib-entry:%d]", sixrd_ad->sixrd_fib_entry_index);
434 sixrd_fib_node_last_lock_gone (fib_node_t * node)
436 /* top of the dependency tree, locks not managed here. */
439 static sixrd_adj_delegate_t *
440 sixrd_adj_delegate_from_fib_node (fib_node_t * node)
442 return ((sixrd_adj_delegate_t *) (((char *) node) -
443 STRUCT_OFFSET_OF (sixrd_adj_delegate_t,
447 static fib_node_back_walk_rc_t
448 sixrd_fib_node_back_walk_notify (fib_node_t * node,
449 fib_node_back_walk_ctx_t * ctx)
451 sixrd_adj_delegate_t *sixrd_ad;
453 sixrd_ad = sixrd_adj_delegate_from_fib_node (node);
454 ip6ip_tunnel_stack (sixrd_ad->adj_index, sixrd_ad->sixrd_fib_entry_index);
456 return (FIB_NODE_BACK_WALK_CONTINUE);
460 * Function definition to get a FIB node from its index
463 sixrd_fib_node_get (fib_node_index_t index)
465 sixrd_adj_delegate_t *sixrd_ad;
467 sixrd_ad = pool_elt_at_index (sixrd_adj_delegate_pool, index);
469 return (&sixrd_ad->sixrd_node);
473 * VFT registered with the adjacency delegate
475 const static adj_delegate_vft_t sixrd_adj_delegate_vft = {
476 .adv_adj_deleted = sixrd_adj_delegate_adj_deleted,
477 .adv_format = sixrd_adj_delegate_format,
481 * VFT registered with the FIB node for the adj delegate
483 const static fib_node_vft_t sixrd_fib_node_vft = {
484 .fnv_get = sixrd_fib_node_get,
485 .fnv_last_lock = sixrd_fib_node_last_lock_gone,
486 .fnv_back_walk = sixrd_fib_node_back_walk_notify,
489 static clib_error_t *
490 sixrd_init (vlib_main_t * vm)
492 clib_error_t *error = 0;
494 /* Make sure the IPIP tunnel subsystem is initialised */
495 error = vlib_call_init_function (vm, ipip_init);
497 sixrd_adj_delegate_type =
498 adj_delegate_register_new_type (&sixrd_adj_delegate_vft);
499 sixrd_fib_node_type = fib_node_register_new_type (&sixrd_fib_node_vft);
504 VLIB_INIT_FUNCTION (sixrd_init);
507 * fd.io coding-style-patch-verification: ON
510 * eval: (c-set-style "gnu")