blob: c44c8345e302544d353007453cdca1f9d3a73ebb [file] [log] [blame]
Ole Troan298c6952018-03-08 12:30:43 +01001/*
2 * sixrd.c - 6RD specific functions (RFC5969)
3 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18/**
19 * This code supports the following sixrd modes:
20 *
21 * 32 EA bits (Complete IPv4 address is embedded):
22 * ea_bits_len = 32
23 * IPv4 suffix is embedded:
24 * ea_bits_len = < 32
25 * No embedded address bits (1:1 mode):
26 * ea_bits_len = 0
27 */
28
29#include "ipip.h"
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>
39#include <vpp/app/version.h> // Really needed?
40
41extern vlib_node_registration_t ip4_sixrd_node;
42
43/**
44 * Adj delegate data
45 */
46typedef struct sixrd_adj_delegate_t_
47{
48 u32 adj_index;
49 fib_node_t sixrd_node;
50 fib_node_index_t sixrd_fib_entry_index;
51 u32 sixrd_sibling;
52} sixrd_adj_delegate_t;
53
54/**
55 * Pool of delegate structs
56 */
57static sixrd_adj_delegate_t *sixrd_adj_delegate_pool;
58
59/**
60 * Adj delegate registered type
61 */
62static adj_delegate_type_t sixrd_adj_delegate_type;
63
64/**
65 * FIB node registered type
66 */
67static fib_node_type_t sixrd_fib_node_type;
68
69static inline sixrd_adj_delegate_t *
70sixrd_adj_from_base (adj_delegate_t * ad)
71{
72 if (ad == NULL)
73 return (NULL);
74 return (pool_elt_at_index (sixrd_adj_delegate_pool, ad->ad_index));
75}
76
77static inline const sixrd_adj_delegate_t *
78sixrd_adj_from_const_base (const adj_delegate_t * ad)
79{
80 if (ad == NULL)
81 {
82 return (NULL);
83 }
84 return (pool_elt_at_index (sixrd_adj_delegate_pool, ad->ad_index));
85}
86
87static void
88sixrd_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b0,
89 const void *data)
90{
91 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
92 ip6_header_t *ip6 = vlib_buffer_get_current (b0) + sizeof (ip4_header_t);
93 const ipip_tunnel_t *t = data;
94
95 ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
96 ip4->dst_address.as_u32 =
97 sixrd_get_addr_net (t, ip6->dst_address.as_u64[0]);
98 ip4->checksum = ip4_header_checksum (ip4);
99}
100
101static void
102ip6ip_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b0,
103 const void *data)
104{
105 const ipip_tunnel_t *t = data;
106 ip4_header_t *ip4 = vlib_buffer_get_current (b0);
107 ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
108 ip4->dst_address.as_u32 =
109 sixrd_get_addr_net (t, adj->sub_type.nbr.next_hop.as_u64[0]);
110 ip4->checksum = ip4_header_checksum (ip4);
111}
112
113static u8 *
114sixrd_build_rewrite (vnet_main_t * vnm, u32 sw_if_index,
115 vnet_link_t link_type, const void *dst_address)
116{
117 u8 *rewrite = NULL;
118 ipip_tunnel_t *t;
119
120 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
121 if (!t)
122 return 0;
123
124 vec_validate (rewrite, sizeof (ip4_header_t) - 1);
125 ip4_header_t *ip4 = (ip4_header_t *) rewrite;
126 ip4->ip_version_and_header_length = 0x45;
127 ip4->ttl = 64;
128 ip4->protocol = IP_PROTOCOL_IPV6;
129 /* fixup ip4 header length and checksum after-the-fact */
130 ip4->src_address.as_u32 = t->tunnel_src.ip4.as_u32;
131 ip4->dst_address.as_u32 = 0;
132 ip4->checksum = ip4_header_checksum (ip4);
133
134 return rewrite;
135}
136
137static void
138ip6ip_tunnel_stack (adj_index_t ai, u32 fib_entry_index)
139{
140 ip_adjacency_t *adj = adj_get (ai);
141 ipip_tunnel_t *t;
142 u32 sw_if_index = adj->rewrite_header.sw_if_index;
143
144 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
145 if (!t)
146 return;
147
148 /*
149 * find the adjacency that is contributed by the FIB entry
150 * that this tunnel resolves via, and use it as the next adj
151 * in the midchain
152 */
153 if (vnet_hw_interface_get_flags (vnet_get_main (), t->hw_if_index) &
154 VNET_HW_INTERFACE_FLAG_LINK_UP)
155 {
156 adj_nbr_midchain_stack (ai,
157 fib_entry_contribute_ip_forwarding
158 (fib_entry_index));
159 }
160 else
161 {
162 adj_nbr_midchain_unstack (ai);
163 }
164}
165
166static void
167sixrd_tunnel_stack (adj_index_t ai, u32 fib_index)
168{
169 dpo_id_t dpo = DPO_INVALID;
170 ip_adjacency_t *adj = adj_get (ai);
171 u32 sw_if_index = adj->rewrite_header.sw_if_index;
172
173 ipip_tunnel_t *t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
174 if (!t)
175 return;
176
177 lookup_dpo_add_or_lock_w_fib_index (fib_index, DPO_PROTO_IP4,
178 LOOKUP_UNICAST, LOOKUP_INPUT_DST_ADDR,
179 LOOKUP_TABLE_FROM_CONFIG, &dpo);
180 adj_nbr_midchain_stack (ai, &dpo);
181}
182
183const static ip46_address_t sixrd_special_nh = {
184 .ip6 = {
185 .as_u64 = {
186 [0] = 0xffffffffffffffff,
187 [1] = 0xffffffffffffffff,
188 },
189 },
190};
191
192static void
193sixrd_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
194{
195 ip_adjacency_t *adj = adj_get (ai);
196 ipip_tunnel_t *t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
197
198 if (!memcmp (&sixrd_special_nh, &adj->sub_type.nbr.next_hop,
199 sizeof (sixrd_special_nh)))
200 {
201 adj_nbr_midchain_update_rewrite (ai, sixrd_fixup, t, ADJ_FLAG_NONE,
202 sixrd_build_rewrite (vnm, sw_if_index,
203 adj_get_link_type
204 (ai), NULL));
205 sixrd_tunnel_stack (ai, t->fib_index);
206 }
207 else
208 {
209 sixrd_adj_delegate_t *sixrd_ad;
210 ip4_address_t da4;
211
212 da4.as_u32 =
213 sixrd_get_addr_net (t, adj->sub_type.nbr.next_hop.as_u64[0]);
214
215 fib_prefix_t pfx = {
216 .fp_proto = FIB_PROTOCOL_IP4,
217 .fp_len = 32,
218 .fp_addr = {
219 .ip4 = da4,
220 }
221 ,
222 };
223
224 adj_nbr_midchain_update_rewrite (ai, ip6ip_fixup, t, ADJ_FLAG_NONE,
225 sixrd_build_rewrite (vnm, sw_if_index,
226 adj_get_link_type
227 (ai), NULL));
228
229 sixrd_ad =
230 sixrd_adj_from_base (adj_delegate_get (adj, sixrd_adj_delegate_type));
231 if (sixrd_ad == NULL)
232 {
233 pool_get (sixrd_adj_delegate_pool, sixrd_ad);
234 fib_node_init (&sixrd_ad->sixrd_node, sixrd_fib_node_type);
235 sixrd_ad->adj_index = ai;
236 sixrd_ad->sixrd_fib_entry_index =
237 fib_table_entry_special_add (t->fib_index, &pfx, FIB_SOURCE_RR,
238 FIB_ENTRY_FLAG_NONE);
239 sixrd_ad->sixrd_sibling =
240 fib_entry_child_add (sixrd_ad->sixrd_fib_entry_index,
241 sixrd_fib_node_type,
242 sixrd_ad - sixrd_adj_delegate_pool);
243
244 adj_delegate_add (adj, sixrd_adj_delegate_type,
245 sixrd_ad - sixrd_adj_delegate_pool);
246
247 ip6ip_tunnel_stack (ai, sixrd_ad->sixrd_fib_entry_index);
248 }
249 }
250}
251
252clib_error_t *
253sixrd_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
254{
255 /* Always up */
256 vnet_hw_interface_set_flags (vnm, hw_if_index,
257 VNET_HW_INTERFACE_FLAG_LINK_UP);
258 return /* no error */ 0;
259}
260
261/* *INDENT-OFF* */
262VNET_HW_INTERFACE_CLASS(sixrd_hw_interface_class) = {
263 .name = "ip6ip-6rd",
264 .build_rewrite = sixrd_build_rewrite,
265 .update_adjacency = sixrd_update_adj,
266};
267
268VNET_DEVICE_CLASS(sixrd_device_class) = {
269 .name = "ip6ip-6rd",
270 .admin_up_down_function = sixrd_interface_admin_up_down,
271#ifdef SOON
272 .clear counter = 0;
273#endif
274}
275;
276/* *INDENT-ON* */
277
278int
279sixrd_add_tunnel (ip6_address_t * ip6_prefix, u8 ip6_prefix_len,
280 ip4_address_t * ip4_prefix, u8 ip4_prefix_len,
281 ip4_address_t * ip4_src, bool security_check,
282 u32 fib_index, u32 * sw_if_index)
283{
284 ipip_main_t *gm = &ipip_main;
285 ipip_tunnel_t *t;
286
287 if (fib_index == ~0)
288 return VNET_API_ERROR_NO_SUCH_FIB;
289
290 if ((ip6_prefix_len + 32 - ip4_prefix_len) > 64)
291 return VNET_API_ERROR_INVALID_VALUE;
292
293 /* Tunnel already configured */
294 ip46_address_t src = ip46_address_initializer, dst =
295 ip46_address_initializer;
296 ip_set (&src, ip4_src, true);
297 ipip_tunnel_key_t key = {.transport = IPIP_TRANSPORT_IP4,
298 .fib_index = fib_index,
299 .src = src,
300 .dst = dst
301 };
302
303 t = ipip_tunnel_db_find (&key);
304 if (t)
305 return VNET_API_ERROR_IF_ALREADY_EXISTS;
306
307 /* Get tunnel index */
308 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
309 memset (t, 0, sizeof (*t));
310 u32 t_idx = t - gm->tunnels; /* tunnel index (or instance) */
311
312 /* Init tunnel struct */
313 t->mode = IPIP_MODE_6RD;
314 t->sixrd.ip4_prefix.as_u32 = ip4_prefix->as_u32;
315 t->sixrd.ip4_prefix_len = ip4_prefix_len;
316 t->sixrd.ip6_prefix = *ip6_prefix;
317 t->sixrd.ip6_prefix_len = ip6_prefix_len;
318 t->tunnel_src = src;
319 t->sixrd.security_check = security_check;
320 t->sixrd.shift =
321 (ip4_prefix_len < 32) ? 64 - ip6_prefix_len - (32 - ip4_prefix_len) : 0;
322
323 /* Create interface */
324 u32 hw_if_index =
325 vnet_register_interface (vnet_get_main (), sixrd_device_class.index,
326 t_idx,
327 sixrd_hw_interface_class.index, t_idx);
328
329 /* Default the interface to up and enable IPv6 (payload) */
330 vnet_hw_interface_t *hi =
331 vnet_get_hw_interface (vnet_get_main (), hw_if_index);
332 t->hw_if_index = hw_if_index;
333 t->fib_index = fib_index;
334 t->sw_if_index = hi->sw_if_index;
335 t->dev_instance = t_idx;
336 t->user_instance = t_idx;
337
338 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 1480;
339
340 ipip_tunnel_db_add (t, &key);
341
342 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, hi->sw_if_index,
343 ~0);
344 gm->tunnel_index_by_sw_if_index[hi->sw_if_index] = t_idx;
345
346 vnet_hw_interface_set_flags (vnet_get_main (), hw_if_index,
347 VNET_HW_INTERFACE_FLAG_LINK_UP);
348 vnet_sw_interface_set_flags (vnet_get_main (), hi->sw_if_index,
349 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
350 ip6_sw_interface_enable_disable (hi->sw_if_index, true);
351
352 /* Create IPv6 route/adjacency */
353 fib_prefix_t pfx6 = {
354 .fp_proto = FIB_PROTOCOL_IP6,
355 .fp_len = t->sixrd.ip6_prefix_len,
356 .fp_addr = {
357 .ip6 = t->sixrd.ip6_prefix,
358 }
359 ,
360 };
361
362 fib_table_entry_update_one_path (fib_index, &pfx6, FIB_SOURCE_CLI,
363 FIB_ENTRY_FLAG_ATTACHED, DPO_PROTO_IP6,
364 &sixrd_special_nh, hi->sw_if_index, ~0, 1,
365 NULL, FIB_ROUTE_PATH_FLAG_NONE);
366
367 *sw_if_index = hi->sw_if_index;
368
369 if (!gm->ip4_protocol_registered)
370 {
371 vlib_node_t *ipip4_input =
372 vlib_get_node_by_name (gm->vlib_main, (u8 *) "ipip4-input");
373 ASSERT (ipip4_input);
374 ip4_register_protocol (IP_PROTOCOL_IPV6, ipip4_input->index);
375 }
376 return 0;
377}
378
379/*
380 * sixrd_del_tunnel
381 */
382int
383sixrd_del_tunnel (u32 sw_if_index)
384{
385 ipip_main_t *gm = &ipip_main;
386 ipip_tunnel_t *t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
387
388 if (!t)
389 {
390 clib_warning ("SIXRD tunnel delete: tunnel does not exist: %d",
391 sw_if_index);
392 return -1;
393 }
394
395 fib_prefix_t pfx6 = {
396 .fp_proto = FIB_PROTOCOL_IP6,
397 .fp_len = t->sixrd.ip6_prefix_len,
398 .fp_addr = {
399 .ip6 = t->sixrd.ip6_prefix,
400 }
401 ,
402 };
403 fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_CLI);
404 vnet_sw_interface_set_flags (vnet_get_main (), t->sw_if_index,
405 0 /* down */ );
406 ip6_sw_interface_enable_disable (t->sw_if_index, false);
407 gm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
408
409 vnet_delete_hw_interface (vnet_get_main (), t->hw_if_index);
410 ipip_tunnel_db_remove (t);
411 pool_put (gm->tunnels, t);
412
413 return 0;
414}
415
416static void
417sixrd_adj_delegate_adj_deleted (adj_delegate_t * aed)
418{
419 sixrd_adj_delegate_t *sixrd_ad;
420
421 sixrd_ad = sixrd_adj_from_base (aed);
422 fib_entry_child_remove (sixrd_ad->sixrd_fib_entry_index,
423 sixrd_ad->sixrd_sibling);
424 fib_table_entry_delete_index (sixrd_ad->sixrd_fib_entry_index,
425 FIB_SOURCE_RR);
426 pool_put (sixrd_adj_delegate_pool, sixrd_ad);
427}
428
429static u8 *
430sixrd_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
431{
432 const sixrd_adj_delegate_t *sixrd_ad;
433
434 sixrd_ad = sixrd_adj_from_const_base (aed);
435 s = format (s, "SIXRD:[fib-entry:%d]", sixrd_ad->sixrd_fib_entry_index);
436
437 return (s);
438}
439
440static void
441sixrd_fib_node_last_lock_gone (fib_node_t * node)
442{
443 /* top of the dependency tree, locks not managed here. */
444}
445
446static sixrd_adj_delegate_t *
447sixrd_adj_delegate_from_fib_node (fib_node_t * node)
448{
449 return ((sixrd_adj_delegate_t *) (((char *) node) -
450 STRUCT_OFFSET_OF (sixrd_adj_delegate_t,
451 sixrd_node)));
452}
453
454static fib_node_back_walk_rc_t
455sixrd_fib_node_back_walk_notify (fib_node_t * node,
456 fib_node_back_walk_ctx_t * ctx)
457{
458 sixrd_adj_delegate_t *sixrd_ad;
459
460 sixrd_ad = sixrd_adj_delegate_from_fib_node (node);
461 ip6ip_tunnel_stack (sixrd_ad->adj_index, sixrd_ad->sixrd_fib_entry_index);
462
463 return (FIB_NODE_BACK_WALK_CONTINUE);
464}
465
466/**
467 * Function definition to get a FIB node from its index
468 */
469static fib_node_t *
470sixrd_fib_node_get (fib_node_index_t index)
471{
472 sixrd_adj_delegate_t *sixrd_ad;
473
474 sixrd_ad = pool_elt_at_index (sixrd_adj_delegate_pool, index);
475
476 return (&sixrd_ad->sixrd_node);
477}
478
479/**
480 * VFT registered with the adjacency delegate
481 */
482const static adj_delegate_vft_t sixrd_adj_delegate_vft = {
483 .adv_adj_deleted = sixrd_adj_delegate_adj_deleted,
484 .adv_format = sixrd_adj_delegate_format,
485};
486
487/**
488 * VFT registered with the FIB node for the adj delegate
489 */
490const static fib_node_vft_t sixrd_fib_node_vft = {
491 .fnv_get = sixrd_fib_node_get,
492 .fnv_last_lock = sixrd_fib_node_last_lock_gone,
493 .fnv_back_walk = sixrd_fib_node_back_walk_notify,
494};
495
496static clib_error_t *
497sixrd_init (vlib_main_t * vm)
498{
499 clib_error_t *error = 0;
500
501 /* Make sure the IPIP tunnel subsystem is initialised */
502 vlib_call_init_function (vm, ipip_init);
503
504 sixrd_adj_delegate_type =
505 adj_delegate_register_new_type (&sixrd_adj_delegate_vft);
506 sixrd_fib_node_type = fib_node_register_new_type (&sixrd_fib_node_vft);
507
508 return error;
509}
510
511VLIB_INIT_FUNCTION (sixrd_init);
512
513/*
514 * fd.io coding-style-patch-verification: ON
515 *
516 * Local Variables:
517 * eval: (c-set-style "gnu")
518 * End:
519 */