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