2 * sr.c: ipv6 segment routing
4 * Copyright (c) 2013 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.
20 * @brief Segment Routing main functions
23 #include <vnet/vnet.h>
24 #include <vnet/sr/sr.h>
25 #include <vnet/fib/ip6_fib.h>
26 #include <vnet/dpo/dpo.h>
28 #include <openssl/hmac.h>
30 ip6_sr_main_t sr_main;
31 static vlib_node_registration_t sr_local_node;
34 * @brief Dynamically added SR DPO type
36 static dpo_type_t sr_dpo_type;
39 * @brief Use passed HMAC key in ip6_sr_header_t in OpenSSL HMAC routines
41 * @param sm ip6_sr_main_t *
42 * @param ip ip6_header_t *
43 * @param sr ip6_sr_header_t *
46 sr_fix_hmac (ip6_sr_main_t * sm, ip6_header_t * ip, ip6_sr_header_t * sr)
54 ip6_sr_hmac_key_t *hmac_key;
57 key_index = sr->hmac_key;
59 /* No signature? Pass... */
63 /* We don't know about this key? Fail... */
64 if (key_index >= vec_len (sm->hmac_keys))
67 hmac_key = sm->hmac_keys + key_index;
69 vec_reset_length (keybuf);
71 /* pkt ip6 src address */
72 vec_add2 (keybuf, copy_target, sizeof (ip6_address_t));
73 clib_memcpy (copy_target, ip->src_address.as_u8, sizeof (ip6_address_t));
76 vec_add2 (keybuf, copy_target, 1);
77 copy_target[0] = sr->first_segment;
79 /* octet w/ bit 0 = "clean" flag */
80 vec_add2 (keybuf, copy_target, 1);
82 = (sr->flags & clib_host_to_net_u16 (IP6_SR_HEADER_FLAG_CLEANUP))
86 vec_add2 (keybuf, copy_target, 1);
87 copy_target[0] = sr->hmac_key;
89 first_segment = sr->first_segment;
94 for (i = 0; i <= first_segment; i++)
96 vec_add2 (keybuf, copy_target, sizeof (ip6_address_t));
97 clib_memcpy (copy_target, addrp->as_u8, sizeof (ip6_address_t));
103 HMAC_CTX_init (sm->hmac_ctx);
104 if (!HMAC_Init (sm->hmac_ctx, hmac_key->shared_secret,
105 vec_len (hmac_key->shared_secret), sm->md))
106 clib_warning ("barf1");
107 if (!HMAC_Update (sm->hmac_ctx, keybuf, vec_len (keybuf)))
108 clib_warning ("barf2");
109 if (!HMAC_Final (sm->hmac_ctx, (unsigned char *) addrp, &sig_len))
110 clib_warning ("barf3");
111 HMAC_CTX_cleanup (sm->hmac_ctx);
115 * @brief Format function for decoding various SR flags
117 * @param s u8 * - formatted string
118 * @param args va_list * - u16 flags
120 * @return formatted output string u8 *
123 format_ip6_sr_header_flags (u8 * s, va_list * args)
125 u16 flags = (u16) va_arg (*args, int);
127 int bswap_needed = va_arg (*args, int);
131 flags = clib_host_to_net_u16 (flags);
133 if (flags & IP6_SR_HEADER_FLAG_CLEANUP)
134 s = format (s, "cleanup ");
136 if (flags & IP6_SR_HEADER_FLAG_PROTECTED)
137 s = format (s, "reroute ");
139 s = format (s, "pl: ");
140 for (i = 1; i <= 4; i++)
142 pl_flag = ip6_sr_policy_list_flags (flags, i);
143 s = format (s, "[%d] ", i);
147 case IP6_SR_HEADER_FLAG_PL_ELT_NOT_PRESENT:
148 s = format (s, "NotPr ");
150 case IP6_SR_HEADER_FLAG_PL_ELT_INGRESS_PE:
151 s = format (s, "InPE ");
153 case IP6_SR_HEADER_FLAG_PL_ELT_EGRESS_PE:
154 s = format (s, "EgPE ");
157 case IP6_SR_HEADER_FLAG_PL_ELT_ORIG_SRC_ADDR:
158 s = format (s, "OrgSrc ");
166 * @brief Format function for decoding ip6_sr_header_t
168 * @param s u8 * - formatted string
169 * @param args va_list * - ip6_sr_header_t
171 * @return formatted output string u8 *
174 format_ip6_sr_header (u8 * s, va_list * args)
176 ip6_sr_header_t *h = va_arg (*args, ip6_sr_header_t *);
177 ip6_address_t placeholder_addr =
178 { {254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254,
181 int print_hmac = va_arg (*args, int);
182 int i, pl_index, max_segs;
183 int flags_host_byte_order = clib_net_to_host_u16 (h->flags);
185 s = format (s, "next proto %d, len %d, type %d",
186 h->protocol, (h->length << 3) + 8, h->type);
187 s = format (s, "\n segs left %d, first_segment %d, hmac key %d",
188 h->segments_left, h->first_segment, h->hmac_key);
189 s = format (s, "\n flags %U", format_ip6_sr_header_flags,
190 flags_host_byte_order, 0 /* bswap needed */ );
193 * Header length is in 8-byte units (minus one), so
194 * divide by 2 to ascertain the number of ip6 addresses in the
197 max_segs = (h->length >> 1);
199 if (!print_hmac && h->hmac_key)
202 s = format (s, "\n Segments (in processing order):");
204 for (i = h->first_segment; i >= 1; i--)
205 s = format (s, "\n %U", format_ip6_address, h->segments + i);
206 if (ip6_address_is_equal (&placeholder_addr, h->segments))
207 s = format (s, "\n (empty placeholder)");
209 s = format (s, "\n %U", format_ip6_address, h->segments);
211 s = format (s, "\n Policy List:");
213 pl_index = 1; /* to match the RFC text */
214 for (i = (h->first_segment + 1); i < max_segs; i++, pl_index++)
217 char *tags[] = { " ", "InPE: ", "EgPE: ", "OrgSrc: " };
220 if (pl_index >= 1 && pl_index <= 4)
222 int this_pl_flag = ip6_sr_policy_list_flags
223 (flags_host_byte_order, pl_index);
224 tag = tags[this_pl_flag];
227 s = format (s, "\n %s%U", tag, format_ip6_address, h->segments + i);
234 * @brief Format function for decoding ip6_sr_header_t with length
236 * @param s u8 * - formatted string
237 * @param args va_list * - ip6_header_t + ip6_sr_header_t
239 * @return formatted output string u8 *
242 format_ip6_sr_header_with_length (u8 * s, va_list * args)
244 ip6_header_t *h = va_arg (*args, ip6_header_t *);
245 u32 max_header_bytes = va_arg (*args, u32);
248 header_bytes = sizeof (h[0]) + sizeof (ip6_sr_header_t);
249 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
250 return format (s, "ip6_sr header truncated");
252 s = format (s, "IP6: %U\n", format_ip6_header, h, max_header_bytes);
254 format (s, "SR: %U\n", format_ip6_sr_header, (ip6_sr_header_t *) (h + 1),
255 0 /* print_hmac */ , max_header_bytes);
260 * @brief Defined valid next nodes
261 * @note Cannot call replicate yet without DPDK
264 #define foreach_sr_rewrite_next \
265 _(ERROR, "error-drop") \
266 _(IP6_LOOKUP, "ip6-lookup") \
267 _(SR_LOCAL, "sr-local") \
268 _(SR_REPLICATE,"sr-replicate")
270 #define foreach_sr_rewrite_next \
271 _(ERROR, "error-drop") \
272 _(IP6_LOOKUP, "ip6-lookup") \
273 _(SR_LOCAL, "sr-local")
277 * @brief Struct for defined valid next nodes
281 #define _(s,n) SR_REWRITE_NEXT_##s,
282 foreach_sr_rewrite_next
288 * @brief Struct for data for SR rewrite packet trace
292 ip6_address_t src, dst;
297 } sr_rewrite_trace_t;
300 * @brief Error strings for SR rewrite
302 static char *sr_rewrite_error_strings[] = {
303 #define sr_error(n,s) s,
304 #include "sr_error.def"
309 * @brief Struct for SR rewrite error strings
313 #define sr_error(n,s) SR_REWRITE_ERROR_##n,
314 #include "sr_error.def"
317 } sr_rewrite_error_t;
321 * @brief Format function for SR rewrite trace.
324 format_sr_rewrite_trace (u8 * s, va_list * args)
326 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
327 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
328 sr_rewrite_trace_t *t = va_arg (*args, sr_rewrite_trace_t *);
329 ip6_sr_main_t *sm = &sr_main;
330 ip6_sr_tunnel_t *tun = pool_elt_at_index (sm->tunnels, t->tunnel_index);
331 ip6_fib_t *rx_fib, *tx_fib;
333 rx_fib = ip6_fib_get (tun->rx_fib_index);
334 tx_fib = ip6_fib_get (tun->tx_fib_index);
337 (s, "SR-REWRITE: next %s ip6 src %U dst %U len %u\n"
338 " rx-fib-id %d tx-fib-id %d\n%U",
339 (t->next_index == SR_REWRITE_NEXT_SR_LOCAL)
340 ? "sr-local" : "ip6-lookup",
341 format_ip6_address, &t->src,
342 format_ip6_address, &t->dst, t->length,
343 rx_fib->table_id, tx_fib->table_id,
344 format_ip6_sr_header, t->sr, 0 /* print_hmac */ );
349 * @brief Main processing dual-loop for Segment Routing Rewrite
352 * @param vm vlib_main_t *
353 * @param node vlib_node_runtime_t *
354 * @param from_frame vlib_frame_t *
356 * @return from_frame->n_vectors uword
359 sr_rewrite (vlib_main_t * vm,
360 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
362 u32 n_left_from, next_index, *from, *to_next;
363 ip6_main_t *im = &ip6_main;
364 ip_lookup_main_t *lm = &im->lookup_main;
365 ip6_sr_main_t *sm = &sr_main;
366 u32 (*sr_local_cb) (vlib_main_t *, vlib_node_runtime_t *,
367 vlib_buffer_t *, ip6_header_t *, ip6_sr_header_t *);
368 sr_local_cb = sm->sr_local_cb;
370 from = vlib_frame_vector_args (from_frame);
371 n_left_from = from_frame->n_vectors;
373 next_index = node->cached_next_index;
375 while (n_left_from > 0)
379 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
381 /* Note 2x loop disabled */
382 while (0 && n_left_from >= 4 && n_left_to_next >= 2)
385 vlib_buffer_t *b0, *b1;
386 ip6_header_t *ip0, *ip1;
387 ip_adjacency_t *adj0, *adj1;
388 ip6_sr_header_t *sr0, *sr1;
389 ip6_sr_tunnel_t *t0, *t1;
390 u32 next0 = SR_REWRITE_NEXT_IP6_LOOKUP;
391 u32 next1 = SR_REWRITE_NEXT_IP6_LOOKUP;
395 /* Prefetch next iteration. */
397 vlib_buffer_t *p2, *p3;
399 p2 = vlib_get_buffer (vm, from[2]);
400 p3 = vlib_get_buffer (vm, from[3]);
402 vlib_prefetch_buffer_header (p2, LOAD);
403 vlib_prefetch_buffer_header (p3, LOAD);
415 b0 = vlib_get_buffer (vm, bi0);
416 b1 = vlib_get_buffer (vm, bi1);
419 * $$$ parse through header(s) to pick the point
420 * where we punch in the SR extention header
424 ip_get_adjacency (lm, vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
426 ip_get_adjacency (lm, vnet_buffer (b1)->ip.adj_index[VLIB_TX]);
428 pool_elt_at_index (sm->tunnels, adj0->rewrite_header.sw_if_index);
430 pool_elt_at_index (sm->tunnels, adj1->rewrite_header.sw_if_index);
432 ASSERT (VLIB_BUFFER_PRE_DATA_SIZE
433 >= ((word) vec_len (t0->rewrite)) + b0->current_data);
434 ASSERT (VLIB_BUFFER_PRE_DATA_SIZE
435 >= ((word) vec_len (t1->rewrite)) + b1->current_data);
437 vnet_buffer (b0)->sw_if_index[VLIB_TX] = t0->tx_fib_index;
438 vnet_buffer (b1)->sw_if_index[VLIB_TX] = t1->tx_fib_index;
440 ip0 = vlib_buffer_get_current (b0);
441 ip1 = vlib_buffer_get_current (b1);
444 * SR-unaware service chaining case: pkt coming back from
445 * service has the original dst address, and will already
446 * have an SR header. If so, send it to sr-local
448 if (PREDICT_FALSE (ip0->protocol == IPPROTO_IPV6_ROUTE))
450 vlib_buffer_advance (b0, sizeof (ip0));
451 sr0 = (ip6_sr_header_t *) (ip0 + 1);
452 new_l0 = clib_net_to_host_u16 (ip0->payload_length);
453 next0 = SR_REWRITE_NEXT_SR_LOCAL;
458 * Copy data before the punch-in point left by the
459 * required amount. Assume (for the moment) that only
460 * the main packet header needs to be copied.
462 clib_memcpy (((u8 *) ip0) - vec_len (t0->rewrite),
463 ip0, sizeof (ip6_header_t));
464 vlib_buffer_advance (b0, -(word) vec_len (t0->rewrite));
465 ip0 = vlib_buffer_get_current (b0);
466 sr0 = (ip6_sr_header_t *) (ip0 + 1);
468 clib_memcpy (sr0, t0->rewrite, vec_len (t0->rewrite));
470 /* Fix the next header chain */
471 sr0->protocol = ip0->protocol;
472 ip0->protocol = IPPROTO_IPV6_ROUTE; /* routing extension header */
473 new_l0 = clib_net_to_host_u16 (ip0->payload_length) +
474 vec_len (t0->rewrite);
475 ip0->payload_length = clib_host_to_net_u16 (new_l0);
477 /* Copy dst address into the DA slot in the segment list */
478 clib_memcpy (sr0->segments, ip0->dst_address.as_u64,
479 sizeof (ip6_address_t));
480 /* Rewrite the ip6 dst address with the first hop */
481 clib_memcpy (ip0->dst_address.as_u64, t0->first_hop.as_u64,
482 sizeof (ip6_address_t));
484 sr_fix_hmac (sm, ip0, sr0);
486 next0 = sr_local_cb ? sr_local_cb (vm, node, b0, ip0, sr0) :
490 * Ignore "do not rewrite" shtik in this path
492 if (PREDICT_FALSE (next0 & 0x80000000))
495 if (PREDICT_FALSE (next0 == SR_REWRITE_NEXT_ERROR))
496 b0->error = node->errors[SR_REWRITE_ERROR_APP_CALLBACK];
500 if (PREDICT_FALSE (ip1->protocol == IPPROTO_IPV6_ROUTE))
502 vlib_buffer_advance (b1, sizeof (ip1));
503 sr1 = (ip6_sr_header_t *) (ip1 + 1);
504 new_l1 = clib_net_to_host_u16 (ip1->payload_length);
505 next1 = SR_REWRITE_NEXT_SR_LOCAL;
509 clib_memcpy (((u8 *) ip0) - vec_len (t0->rewrite),
510 ip0, sizeof (ip6_header_t));
511 vlib_buffer_advance (b1, -(word) vec_len (t1->rewrite));
512 ip1 = vlib_buffer_get_current (b1);
513 sr1 = (ip6_sr_header_t *) (ip1 + 1);
514 clib_memcpy (sr1, t1->rewrite, vec_len (t1->rewrite));
516 sr1->protocol = ip1->protocol;
517 ip1->protocol = IPPROTO_IPV6_ROUTE;
518 new_l1 = clib_net_to_host_u16 (ip1->payload_length) +
519 vec_len (t1->rewrite);
520 ip1->payload_length = clib_host_to_net_u16 (new_l1);
522 /* Copy dst address into the DA slot in the segment list */
523 clib_memcpy (sr1->segments, ip1->dst_address.as_u64,
524 sizeof (ip6_address_t));
525 /* Rewrite the ip6 dst address with the first hop */
526 clib_memcpy (ip1->dst_address.as_u64, t1->first_hop.as_u64,
527 sizeof (ip6_address_t));
529 sr_fix_hmac (sm, ip1, sr1);
531 next1 = sr_local_cb ? sr_local_cb (vm, node, b1, ip1, sr1) :
535 * Ignore "do not rewrite" shtik in this path
537 if (PREDICT_FALSE (next1 & 0x80000000))
540 if (PREDICT_FALSE (next1 == SR_REWRITE_NEXT_ERROR))
541 b1->error = node->errors[SR_REWRITE_ERROR_APP_CALLBACK];
545 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
547 sr_rewrite_trace_t *tr = vlib_add_trace (vm, node,
549 tr->tunnel_index = t0 - sm->tunnels;
550 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
551 sizeof (tr->src.as_u8));
552 clib_memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
553 sizeof (tr->dst.as_u8));
555 tr->next_index = next0;
556 clib_memcpy (tr->sr, sr0, sizeof (tr->sr));
558 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
560 sr_rewrite_trace_t *tr = vlib_add_trace (vm, node,
562 tr->tunnel_index = t1 - sm->tunnels;
563 clib_memcpy (tr->src.as_u8, ip1->src_address.as_u8,
564 sizeof (tr->src.as_u8));
565 clib_memcpy (tr->dst.as_u8, ip1->dst_address.as_u8,
566 sizeof (tr->dst.as_u8));
568 tr->next_index = next1;
569 clib_memcpy (tr->sr, sr1, sizeof (tr->sr));
572 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
573 to_next, n_left_to_next,
574 bi0, bi1, next0, next1);
577 while (n_left_from > 0 && n_left_to_next > 0)
581 ip6_header_t *ip0 = 0;
582 ip_adjacency_t *adj0;
583 ip6_sr_header_t *sr0 = 0;
585 u32 next0 = SR_REWRITE_NEXT_IP6_LOOKUP;
595 b0 = vlib_get_buffer (vm, bi0);
598 * $$$ parse through header(s) to pick the point
599 * where we punch in the SR extention header
603 ip_get_adjacency (lm, vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
605 pool_elt_at_index (sm->tunnels, adj0->rewrite_header.sw_if_index);
607 #if DPDK > 0 /* Cannot call replication node yet without DPDK */
608 /* add a replication node */
609 if (PREDICT_FALSE (t0->policy_index != ~0))
611 vnet_buffer (b0)->ip.save_protocol = t0->policy_index;
612 next0 = SR_REWRITE_NEXT_SR_REPLICATE;
617 ASSERT (VLIB_BUFFER_PRE_DATA_SIZE
618 >= ((word) vec_len (t0->rewrite)) + b0->current_data);
620 vnet_buffer (b0)->sw_if_index[VLIB_TX] = t0->tx_fib_index;
622 ip0 = vlib_buffer_get_current (b0);
625 * SR-unaware service chaining case: pkt coming back from
626 * service has the original dst address, and will already
627 * have an SR header. If so, send it to sr-local
629 if (PREDICT_FALSE (ip0->protocol == IPPROTO_IPV6_ROUTE))
631 vlib_buffer_advance (b0, sizeof (ip0));
632 sr0 = (ip6_sr_header_t *) (ip0 + 1);
633 new_l0 = clib_net_to_host_u16 (ip0->payload_length);
634 next0 = SR_REWRITE_NEXT_SR_LOCAL;
639 * Copy data before the punch-in point left by the
640 * required amount. Assume (for the moment) that only
641 * the main packet header needs to be copied.
643 clib_memcpy (((u8 *) ip0) - vec_len (t0->rewrite),
644 ip0, sizeof (ip6_header_t));
645 vlib_buffer_advance (b0, -(word) vec_len (t0->rewrite));
646 ip0 = vlib_buffer_get_current (b0);
647 sr0 = (ip6_sr_header_t *) (ip0 + 1);
649 clib_memcpy (sr0, t0->rewrite, vec_len (t0->rewrite));
651 /* Fix the next header chain */
652 sr0->protocol = ip0->protocol;
653 ip0->protocol = IPPROTO_IPV6_ROUTE; /* routing extension header */
654 new_l0 = clib_net_to_host_u16 (ip0->payload_length) +
655 vec_len (t0->rewrite);
656 ip0->payload_length = clib_host_to_net_u16 (new_l0);
658 /* Copy dst address into the DA slot in the segment list */
659 clib_memcpy (sr0->segments, ip0->dst_address.as_u64,
660 sizeof (ip6_address_t));
661 /* Rewrite the ip6 dst address with the first hop */
662 clib_memcpy (ip0->dst_address.as_u64, t0->first_hop.as_u64,
663 sizeof (ip6_address_t));
665 sr_fix_hmac (sm, ip0, sr0);
667 next0 = sr_local_cb ? sr_local_cb (vm, node, b0, ip0, sr0) :
671 * Ignore "do not rewrite" shtik in this path
673 if (PREDICT_FALSE (next0 & 0x80000000))
676 if (PREDICT_FALSE (next0 == SR_REWRITE_NEXT_ERROR))
677 b0->error = node->errors[SR_REWRITE_ERROR_APP_CALLBACK];
681 #if DPDK > 0 /* Cannot run replicate without DPDK and only replicate uses this label */
684 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
686 sr_rewrite_trace_t *tr = vlib_add_trace (vm, node,
688 tr->tunnel_index = t0 - sm->tunnels;
691 memcpy (tr->src.as_u8, ip0->src_address.as_u8,
692 sizeof (tr->src.as_u8));
693 memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
694 sizeof (tr->dst.as_u8));
697 tr->next_index = next0;
698 clib_memcpy (tr->sr, sr0, sizeof (tr->sr));
701 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
702 to_next, n_left_to_next,
706 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
708 return from_frame->n_vectors;
712 VLIB_REGISTER_NODE (sr_rewrite_node) = {
713 .function = sr_rewrite,
714 .name = "sr-rewrite",
715 /* Takes a vector of packets. */
716 .vector_size = sizeof (u32),
717 .format_trace = format_sr_rewrite_trace,
718 .format_buffer = format_ip6_sr_header_with_length,
720 .n_errors = SR_REWRITE_N_ERROR,
721 .error_strings = sr_rewrite_error_strings,
723 .runtime_data_bytes = 0,
725 .n_next_nodes = SR_REWRITE_N_NEXT,
727 #define _(s,n) [SR_REWRITE_NEXT_##s] = n,
728 foreach_sr_rewrite_next
734 VLIB_NODE_FUNCTION_MULTIARCH (sr_rewrite_node, sr_rewrite)
735 static int ip6_delete_route_no_next_hop (ip6_address_t * dst_address_arg,
736 u32 dst_address_length,
740 .fp_len = dst_address_length,
741 .fp_proto = FIB_PROTOCOL_IP6,
743 .ip6 = *dst_address_arg,
747 fib_table_entry_delete (fib_table_id_find_fib_index (FIB_PROTOCOL_IP6,
749 &pfx, FIB_SOURCE_SR);
755 * @brief Find or add if not found - HMAC shared secret
757 * @param sm ip6_sr_main_t *
759 * @param indexp u32 *
761 * @return ip6_sr_hmac_key_t *
763 static ip6_sr_hmac_key_t *
764 find_or_add_shared_secret (ip6_sr_main_t * sm, u8 * secret, u32 * indexp)
767 ip6_sr_hmac_key_t *key = 0;
770 p = hash_get_mem (sm->hmac_key_by_shared_secret, secret);
774 key = vec_elt_at_index (sm->hmac_keys, p[0]);
780 /* Specific key ID? */
781 if (indexp && *indexp)
783 vec_validate (sm->hmac_keys, *indexp);
784 key = sm->hmac_keys + *indexp;
788 for (i = 0; i < vec_len (sm->hmac_keys); i++)
790 if (sm->hmac_keys[i].shared_secret == 0)
792 key = sm->hmac_keys + i;
796 vec_validate (sm->hmac_keys, i);
797 key = sm->hmac_keys + i;
802 key->shared_secret = vec_dup (secret);
804 hash_set_mem (sm->hmac_key_by_shared_secret, key->shared_secret,
805 key - sm->hmac_keys);
808 *indexp = key - sm->hmac_keys;
813 * @brief Add or Delete a Segment Routing tunnel.
815 * @param a ip6_sr_add_del_tunnel_args_t *
820 ip6_sr_add_del_tunnel (ip6_sr_add_del_tunnel_args_t * a)
822 ip6_main_t *im = &ip6_main;
823 ip6_sr_tunnel_key_t key;
826 ip6_sr_header_t *h = 0;
828 ip6_address_t *addrp, *this_address;
829 ip6_sr_main_t *sm = &sr_main;
831 u32 rx_fib_index, tx_fib_index;
832 u32 hmac_key_index_u32;
833 u8 hmac_key_index = 0;
836 dpo_id_t dpo = DPO_NULL;
838 /* Make sure that the rx FIB exists */
839 p = hash_get (im->fib_index_by_table_id, a->rx_table_id);
844 /* remember the FIB index */
847 /* Make sure that the supplied FIB exists */
848 p = hash_get (im->fib_index_by_table_id, a->tx_table_id);
853 /* remember the FIB index */
856 clib_memcpy (key.src.as_u8, a->src_address->as_u8, sizeof (key.src));
857 clib_memcpy (key.dst.as_u8, a->dst_address->as_u8, sizeof (key.dst));
859 /* When adding a tunnel:
860 * - If a "name" is given, it must not exist.
861 * - The "key" is always checked, and must not exist.
862 * When deleting a tunnel:
863 * - If the "name" is given, and it exists, then use it.
864 * - If the "name" is not given, use the "key".
865 * - If the "name" and the "key" are given, then both must point to the same
870 p = hash_get_mem (sm->tunnel_index_by_key, &key);
872 /* If the name is given, look it up */
874 n = hash_get_mem (sm->tunnel_index_by_name, a->name);
878 /* validate key/name parameters */
879 if (!a->is_del) /* adding a tunnel */
881 if (a->name && n) /* name given & exists already */
883 if (p) /* key exists already */
886 else /* deleting a tunnel */
888 if (!p) /* key doesn't exist */
890 if (a->name && !n) /* name given & it doesn't exist */
893 if (n) /* name given & found */
895 if (n[0] != p[0]) /* name and key do not point to the same thing */
901 if (a->is_del) /* delete the tunnel */
905 /* Delete existing tunnel */
906 t = pool_elt_at_index (sm->tunnels, p[0]);
908 ip6_delete_route_no_next_hop (&t->key.dst, t->dst_mask_width,
910 vec_free (t->rewrite);
911 /* Remove tunnel from any policy if associated */
912 if (t->policy_index != ~0)
914 pt = pool_elt_at_index (sm->policies, t->policy_index);
915 for (i = 0; i < vec_len (pt->tunnel_indices); i++)
917 if (pt->tunnel_indices[i] == t - sm->tunnels)
919 vec_delete (pt->tunnel_indices, 1, i);
923 clib_warning ("Tunnel index %d not found in policy_index %d",
924 t - sm->tunnels, pt - sm->policies);
926 /* If this is last tunnel in the policy, clean up the policy too */
927 if (vec_len (pt->tunnel_indices) == 0)
929 hash_unset_mem (sm->policy_index_by_policy_name, pt->name);
931 pool_put (sm->policies, pt);
935 /* Clean up the tunnel by name */
938 hash_unset_mem (sm->tunnel_index_by_name, t->name);
941 pool_put (sm->tunnels, t);
942 hp = hash_get_pair (sm->tunnel_index_by_key, &key);
943 key_copy = (void *) (hp->key);
944 hash_unset_mem (sm->tunnel_index_by_key, &key);
949 /* create a new tunnel */
950 pool_get (sm->tunnels, t);
951 memset (t, 0, sizeof (*t));
952 t->policy_index = ~0;
954 clib_memcpy (&t->key, &key, sizeof (t->key));
955 t->dst_mask_width = a->dst_mask_width;
956 t->rx_fib_index = rx_fib_index;
957 t->tx_fib_index = tx_fib_index;
959 if (!vec_len (a->segments))
960 /* there must be at least one segment... */
963 /* The first specified hop goes right into the dst address */
964 clib_memcpy (&t->first_hop, &a->segments[0], sizeof (ip6_address_t));
967 * Create the sr header rewrite string
968 * The list of segments needs an extra slot for the ultimate destination
969 * which is taken from the packet we add the SRH to.
971 header_length = sizeof (*h) +
972 sizeof (ip6_address_t) * (vec_len (a->segments) + 1 + vec_len (a->tags));
974 if (a->shared_secret)
976 /* Allocate a new key slot if we don't find the secret key */
977 hmac_key_index_u32 = 0;
978 (void) find_or_add_shared_secret (sm, a->shared_secret,
979 &hmac_key_index_u32);
981 /* Hey Vinz Clortho: Gozzer is pissed.. you're out of keys! */
982 if (hmac_key_index_u32 >= 256)
984 hmac_key_index = hmac_key_index_u32;
985 header_length += SHA256_DIGEST_LENGTH;
988 vec_validate (t->rewrite, header_length - 1);
990 h = (ip6_sr_header_t *) t->rewrite;
992 h->protocol = 0xFF; /* we don't know yet */
994 h->length = (header_length / 8) - 1;
995 h->type = ROUTING_HEADER_TYPE_SR;
997 /* first_segment and segments_left need to have the index of the last
998 * element in the list; a->segments has one element less than ends up
999 * in the header (it does not have the DA in it), so vec_len(a->segments)
1000 * is the value we want.
1002 h->first_segment = h->segments_left = vec_len (a->segments);
1004 if (a->shared_secret)
1005 h->hmac_key = hmac_key_index & 0xFF;
1007 h->flags = a->flags_net_byte_order;
1009 /* Paint on the segment list, in reverse.
1010 * This is offset by one to leave room at the start for the ultimate
1013 addrp = h->segments + vec_len (a->segments);
1015 vec_foreach (this_address, a->segments)
1017 clib_memcpy (addrp->as_u8, this_address->as_u8, sizeof (ip6_address_t));
1022 * Since the ultimate destination address is not yet known, set that slot
1023 * to a value we will instantly recognize as bogus.
1025 memset (h->segments, 0xfe, sizeof (ip6_address_t));
1027 /* Paint on the tag list, not reversed */
1028 addrp = h->segments + vec_len (a->segments);
1030 vec_foreach (this_address, a->tags)
1032 clib_memcpy (addrp->as_u8, this_address->as_u8, sizeof (ip6_address_t));
1036 key_copy = vec_new (ip6_sr_tunnel_key_t, 1);
1037 clib_memcpy (key_copy, &key, sizeof (ip6_sr_tunnel_key_t));
1038 hash_set_mem (sm->tunnel_index_by_key, key_copy, t - sm->tunnels);
1041 * Stick the tunnel index into the rewrite header.
1043 * Unfortunately, inserting an SR header according to the various
1044 * RFC's requires parsing through the ip6 header, perhaps consing a
1045 * buffer onto the head of the vlib_buffer_t, etc. We don't use the
1046 * normal reverse bcopy rewrite code.
1048 * We don't handle ugly RFC-related cases yet, but I'm sure PL will complain
1051 dpo_set (&dpo, sr_dpo_type, DPO_PROTO_IP6, t - sm->tunnels);
1053 fib_prefix_t pfx = {
1054 .fp_proto = FIB_PROTOCOL_IP6,
1055 .fp_len = a->dst_mask_width,
1057 .ip6 = *a->dst_address,
1060 fib_table_entry_special_dpo_add (rx_fib_index,
1063 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1068 p = hash_get_mem (sm->policy_index_by_policy_name, a->policy_name);
1071 pt = pool_elt_at_index (sm->policies, p[0]);
1073 else /* no policy, lets create one */
1075 pool_get (sm->policies, pt);
1076 memset (pt, 0, sizeof (*pt));
1077 pt->name = format (0, "%s%c", a->policy_name, 0);
1078 hash_set_mem (sm->policy_index_by_policy_name, pt->name,
1080 p = hash_get_mem (sm->policy_index_by_policy_name, a->policy_name);
1082 vec_add1 (pt->tunnel_indices, t - sm->tunnels);
1084 clib_warning ("p is NULL!");
1085 t->policy_index = p ? p[0] : ~0; /* equiv. to (pt - sm->policies) */
1090 t->name = format (0, "%s%c", a->name, 0);
1091 hash_set_mem (sm->tunnel_index_by_name, t->name, t - sm->tunnels);
1098 * @brief no-op lock function.
1099 * The lifetime of the SR entry is managed by the control plane
1102 sr_dpo_lock (dpo_id_t * dpo)
1107 * @brief no-op unlock function.
1108 * The lifetime of the SR entry is managed by the control plane
1111 sr_dpo_unlock (dpo_id_t * dpo)
1116 format_sr_dpo (u8 * s, va_list * args)
1118 index_t index = va_arg (*args, index_t);
1119 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
1121 return (format (s, "SR: tunnel:[%d]", index));
1124 const static dpo_vft_t sr_vft = {
1125 .dv_lock = sr_dpo_lock,
1126 .dv_unlock = sr_dpo_unlock,
1127 .dv_format = format_sr_dpo,
1130 const static char *const sr_ip6_nodes[] = {
1135 const static char *const *const sr_nodes[DPO_PROTO_NUM] = {
1136 [DPO_PROTO_IP6] = sr_ip6_nodes,
1140 * @brief CLI parser for Add or Delete a Segment Routing tunnel.
1142 * @param vm vlib_main_t *
1143 * @param input unformat_input_t *
1144 * @param cmd vlib_cli_command_t *
1146 * @return error clib_error_t *
1148 static clib_error_t *
1149 sr_add_del_tunnel_command_fn (vlib_main_t * vm,
1150 unformat_input_t * input,
1151 vlib_cli_command_t * cmd)
1154 ip6_address_t src_address;
1155 int src_address_set = 0;
1156 ip6_address_t dst_address;
1158 int dst_address_set = 0;
1160 u8 *shared_secret = 0;
1162 u8 *policy_name = 0;
1163 u32 rx_table_id = 0;
1164 u32 tx_table_id = 0;
1165 ip6_address_t *segments = 0;
1166 ip6_address_t *this_seg;
1167 ip6_address_t *tags = 0;
1168 ip6_address_t *this_tag;
1169 ip6_sr_add_del_tunnel_args_t _a, *a = &_a;
1170 ip6_address_t next_address, tag;
1174 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1176 if (unformat (input, "del"))
1178 else if (unformat (input, "rx-fib-id %d", &rx_table_id))
1180 else if (unformat (input, "tx-fib-id %d", &tx_table_id))
1182 else if (unformat (input, "src %U", unformat_ip6_address, &src_address))
1183 src_address_set = 1;
1184 else if (unformat (input, "name %s", &name))
1186 else if (unformat (input, "policy %s", &policy_name))
1188 else if (unformat (input, "dst %U/%d",
1189 unformat_ip6_address, &dst_address, &dst_mask_width))
1190 dst_address_set = 1;
1191 else if (unformat (input, "next %U", unformat_ip6_address,
1194 vec_add2 (segments, this_seg, 1);
1195 clib_memcpy (this_seg->as_u8, next_address.as_u8,
1196 sizeof (*this_seg));
1198 else if (unformat (input, "tag %U", unformat_ip6_address, &tag))
1200 vec_add2 (tags, this_tag, 1);
1201 clib_memcpy (this_tag->as_u8, tag.as_u8, sizeof (*this_tag));
1203 else if (unformat (input, "clean"))
1204 flags |= IP6_SR_HEADER_FLAG_CLEANUP;
1205 else if (unformat (input, "protected"))
1206 flags |= IP6_SR_HEADER_FLAG_PROTECTED;
1207 else if (unformat (input, "key %s", &shared_secret))
1208 /* Do not include the trailing NULL byte. Guaranteed interop issue */
1209 _vec_len (shared_secret) -= 1;
1210 else if (unformat (input, "InPE %d", &pl_index))
1212 if (pl_index <= 0 || pl_index > 4)
1214 pl_index_range_error:
1215 return clib_error_return
1216 (0, "Policy List Element Index %d out of range (1-4)",
1220 flags |= IP6_SR_HEADER_FLAG_PL_ELT_INGRESS_PE
1221 << ip6_sr_policy_list_shift_from_index (pl_index);
1223 else if (unformat (input, "EgPE %d", &pl_index))
1225 if (pl_index <= 0 || pl_index > 4)
1226 goto pl_index_range_error;
1227 flags |= IP6_SR_HEADER_FLAG_PL_ELT_EGRESS_PE
1228 << ip6_sr_policy_list_shift_from_index (pl_index);
1230 else if (unformat (input, "OrgSrc %d", &pl_index))
1232 if (pl_index <= 0 || pl_index > 4)
1233 goto pl_index_range_error;
1234 flags |= IP6_SR_HEADER_FLAG_PL_ELT_ORIG_SRC_ADDR
1235 << ip6_sr_policy_list_shift_from_index (pl_index);
1241 if (!src_address_set)
1242 return clib_error_return (0, "src address required");
1244 if (!dst_address_set)
1245 return clib_error_return (0, "dst address required");
1248 return clib_error_return (0, "at least one sr segment required");
1250 memset (a, 0, sizeof (*a));
1251 a->src_address = &src_address;
1252 a->dst_address = &dst_address;
1253 a->dst_mask_width = dst_mask_width;
1254 a->segments = segments;
1256 a->flags_net_byte_order = clib_host_to_net_u16 (flags);
1258 a->rx_table_id = rx_table_id;
1259 a->tx_table_id = tx_table_id;
1260 a->shared_secret = shared_secret;
1267 if (vec_len (policy_name))
1268 a->policy_name = policy_name;
1272 rv = ip6_sr_add_del_tunnel (a);
1274 vec_free (segments);
1276 vec_free (shared_secret);
1284 return clib_error_return (0, "SR tunnel src %U dst %U already exists",
1285 format_ip6_address, &src_address,
1286 format_ip6_address, &dst_address);
1289 return clib_error_return (0, "SR tunnel src %U dst %U does not exist",
1290 format_ip6_address, &src_address,
1291 format_ip6_address, &dst_address);
1294 return clib_error_return (0, "FIB table %d does not exist",
1298 return clib_error_return (0, "At least one segment is required");
1301 return clib_error_return (0, "BUG: ip6_sr_add_del_tunnel returns %d",
1309 VLIB_CLI_COMMAND (sr_tunnel_command, static) = {
1310 .path = "sr tunnel",
1312 "sr tunnel [del] [name <name>] src <addr> dst <addr> [next <addr>] "
1313 "[clean] [reroute] [key <secret>] [policy <policy_name>]"
1314 "[rx-fib-id <fib_id>] [tx-fib-id <fib_id>]",
1315 .function = sr_add_del_tunnel_command_fn,
1320 * @brief Display Segment Routing tunnel
1322 * @param vm vlib_main_t *
1323 * @param t ip6_sr_tunnel_t *
1327 ip6_sr_tunnel_display (vlib_main_t * vm, ip6_sr_tunnel_t * t)
1329 ip6_sr_main_t *sm = &sr_main;
1330 ip6_fib_t *rx_fib, *tx_fib;
1331 ip6_sr_policy_t *pt;
1333 rx_fib = ip6_fib_get (t->rx_fib_index);
1334 tx_fib = ip6_fib_get (t->tx_fib_index);
1337 vlib_cli_output (vm, "sr tunnel name: %s", (char *) t->name);
1339 vlib_cli_output (vm, "src %U dst %U first hop %U",
1340 format_ip6_address, &t->key.src,
1341 format_ip6_address, &t->key.dst,
1342 format_ip6_address, &t->first_hop);
1343 vlib_cli_output (vm, " rx-fib-id %d tx-fib-id %d",
1344 rx_fib->table_id, tx_fib->table_id);
1345 vlib_cli_output (vm, " sr: %U", format_ip6_sr_header, t->rewrite,
1346 0 /* print_hmac */ );
1348 if (t->policy_index != ~0)
1350 pt = pool_elt_at_index (sm->policies, t->policy_index);
1351 vlib_cli_output (vm, "sr policy: %s", (char *) pt->name);
1353 vlib_cli_output (vm, "-------");
1359 * @brief CLI Parser for Display Segment Routing tunnel
1361 * @param vm vlib_main_t *
1362 * @param input unformat_input_t *
1363 * @param cmd vlib_cli_command_t *
1365 * @return error clib_error_t *
1367 static clib_error_t *
1368 show_sr_tunnel_fn (vlib_main_t * vm,
1369 unformat_input_t * input, vlib_cli_command_t * cmd)
1371 static ip6_sr_tunnel_t **tunnels;
1373 ip6_sr_main_t *sm = &sr_main;
1378 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1380 if (unformat (input, "name %s", &name))
1382 p = hash_get_mem (sm->tunnel_index_by_name, name);
1384 vlib_cli_output (vm, "No SR tunnel with name: %s. Showing all.",
1391 vec_reset_length (tunnels);
1393 if (!p) /* Either name parm not passed or no tunnel with that name found, show all */
1396 pool_foreach (t, sm->tunnels,
1398 vec_add1 (tunnels, t);
1402 else /* Just show the one tunnel by name */
1403 vec_add1 (tunnels, &sm->tunnels[p[0]]);
1405 if (vec_len (tunnels) == 0)
1406 vlib_cli_output (vm, "No SR tunnels configured");
1408 for (i = 0; i < vec_len (tunnels); i++)
1411 ip6_sr_tunnel_display (vm, t);
1418 VLIB_CLI_COMMAND (show_sr_tunnel_command, static) = {
1419 .path = "show sr tunnel",
1420 .short_help = "show sr tunnel [name <sr-tunnel-name>]",
1421 .function = show_sr_tunnel_fn,
1426 * @brief Add or Delete a Segment Routing policy
1428 * @param a ip6_sr_add_del_policy_args_t *
1430 * @return retval int
1433 ip6_sr_add_del_policy (ip6_sr_add_del_policy_args_t * a)
1435 ip6_sr_main_t *sm = &sr_main;
1437 ip6_sr_tunnel_t *t = 0;
1438 ip6_sr_policy_t *policy;
1439 u32 *tunnel_indices = 0;
1446 p = hash_get_mem (sm->policy_index_by_policy_name, a->name);
1448 return -6; /* policy name not found */
1450 policy = pool_elt_at_index (sm->policies, p[0]);
1452 vec_foreach_index (i, policy->tunnel_indices)
1454 t = pool_elt_at_index (sm->tunnels, policy->tunnel_indices[i]);
1455 t->policy_index = ~0;
1457 hash_unset_mem (sm->policy_index_by_policy_name, a->name);
1458 pool_put (sm->policies, policy);
1463 if (!vec_len (a->tunnel_names))
1464 return -3; /*tunnel name is required case */
1466 vec_reset_length (tunnel_indices);
1467 /* Check tunnel names, add tunnel_index to policy */
1468 for (i = 0; i < vec_len (a->tunnel_names); i++)
1470 p = hash_get_mem (sm->tunnel_index_by_name, a->tunnel_names[i]);
1472 return -4; /* tunnel name not found case */
1474 t = pool_elt_at_index (sm->tunnels, p[0]);
1476 No need to check t==0. -3 condition above ensures name
1478 if (t->policy_index != ~0)
1479 return -5; /* tunnel name already associated with a policy */
1481 /* Add to tunnel indicies */
1482 vec_add1 (tunnel_indices, p[0]);
1485 /* Add policy to ip6_sr_main_t */
1486 pool_get (sm->policies, policy);
1487 policy->name = a->name;
1488 policy->tunnel_indices = tunnel_indices;
1489 hash_set_mem (sm->policy_index_by_policy_name, policy->name,
1490 policy - sm->policies);
1492 /* Yes, this could be construed as overkill but the last thing you should do is set
1493 the policy_index on the tunnel after everything is set in ip6_sr_main_t.
1494 If this is deemed overly cautious, could set this in the vec_len(tunnel_names) loop.
1496 for (i = 0; i < vec_len (policy->tunnel_indices); i++)
1498 t = pool_elt_at_index (sm->tunnels, policy->tunnel_indices[i]);
1499 t->policy_index = policy - sm->policies;
1506 * @brief CLI Parser for Add or Delete a Segment Routing policy
1508 * @param vm vlib_main_t *
1509 * @param input unformat_input_t *
1510 * @param cmd vlib_cli_command_t *
1512 * @return error clib_error_t *
1514 static clib_error_t *
1515 sr_add_del_policy_command_fn (vlib_main_t * vm,
1516 unformat_input_t * input,
1517 vlib_cli_command_t * cmd)
1520 u8 **tunnel_names = 0;
1521 u8 *tunnel_name = 0;
1523 ip6_sr_add_del_policy_args_t _a, *a = &_a;
1526 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1528 if (unformat (input, "del"))
1530 else if (unformat (input, "name %s", &name))
1532 else if (unformat (input, "tunnel %s", &tunnel_name))
1536 vec_add1 (tunnel_names, tunnel_name);
1545 return clib_error_return (0, "name of SR policy required");
1548 memset (a, 0, sizeof (*a));
1552 a->tunnel_names = tunnel_names;
1554 rv = ip6_sr_add_del_policy (a);
1556 vec_free (tunnel_names);
1564 return clib_error_return (0,
1565 "tunnel name to associate to SR policy is required");
1568 return clib_error_return (0, "tunnel name not found");
1571 return clib_error_return (0, "tunnel already associated with policy");
1574 return clib_error_return (0, "policy name %s not found", name);
1577 return clib_error_return (0, "TODO: deleting policy name %s", name);
1580 return clib_error_return (0, "BUG: ip6_sr_add_del_policy returns %d",
1588 VLIB_CLI_COMMAND (sr_policy_command, static) = {
1589 .path = "sr policy",
1591 "sr policy [del] name <policy-name> tunnel <sr-tunnel-name> [tunnel <sr-tunnel-name>]*",
1592 .function = sr_add_del_policy_command_fn,
1597 * @brief CLI Parser for Displaying Segment Routing policy
1599 * @param vm vlib_main_t *
1600 * @param input unformat_input_t *
1601 * @param cmd vlib_cli_command_t *
1603 * @return error clib_error_t *
1605 static clib_error_t *
1606 show_sr_policy_fn (vlib_main_t * vm,
1607 unformat_input_t * input, vlib_cli_command_t * cmd)
1609 static ip6_sr_policy_t **policies;
1610 ip6_sr_policy_t *policy;
1612 ip6_sr_main_t *sm = &sr_main;
1617 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1619 if (unformat (input, "name %s", &name))
1621 p = hash_get_mem (sm->policy_index_by_policy_name, name);
1623 vlib_cli_output (vm,
1624 "policy with name %s not found. Showing all.",
1631 vec_reset_length (policies);
1633 if (!p) /* Either name parm not passed or no policy with that name found, show all */
1636 pool_foreach (policy, sm->policies,
1638 vec_add1 (policies, policy);
1642 else /* Just show the one policy by name and a summary of tunnel names */
1644 policy = pool_elt_at_index (sm->policies, p[0]);
1645 vec_add1 (policies, policy);
1648 if (vec_len (policies) == 0)
1649 vlib_cli_output (vm, "No SR policies configured");
1651 for (i = 0; i < vec_len (policies); i++)
1653 policy = policies[i];
1656 vlib_cli_output (vm, "SR policy name: %s", (char *) policy->name);
1657 for (j = 0; j < vec_len (policy->tunnel_indices); j++)
1659 t = pool_elt_at_index (sm->tunnels, policy->tunnel_indices[j]);
1660 ip6_sr_tunnel_display (vm, t);
1669 VLIB_CLI_COMMAND (show_sr_policy_command, static) = {
1670 .path = "show sr policy",
1671 .short_help = "show sr policy [name <sr-policy-name>]",
1672 .function = show_sr_policy_fn,
1677 * @brief Add or Delete a mapping of IP6 multicast address
1678 * to Segment Routing policy.
1680 * @param a ip6_sr_add_del_multicastmap_args_t *
1682 * @return retval int
1685 ip6_sr_add_del_multicastmap (ip6_sr_add_del_multicastmap_args_t * a)
1689 ip6_sr_main_t *sm = &sr_main;
1690 ip6_sr_policy_t *pt;
1694 /* clean up the adjacency */
1696 hash_get_mem (sm->policy_index_by_multicast_address,
1697 a->multicast_address);
1701 /* Get our policy by policy_name */
1702 p = hash_get_mem (sm->policy_index_by_policy_name, a->policy_name);
1708 pt = pool_elt_at_index (sm->policies, p[0]);
1711 Get the first tunnel associated with policy populate the fib adjacency.
1712 From there, since this tunnel will have it's policy_index != ~0 it will
1713 be the trigger in the dual_loop to pull up the policy and make a copy-rewrite
1714 for each tunnel in the policy
1717 t = pool_elt_at_index (sm->tunnels, pt->tunnel_indices[0]);
1720 * Stick the tunnel index into the rewrite header.
1722 * Unfortunately, inserting an SR header according to the various
1723 * RFC's requires parsing through the ip6 header, perhaps consing a
1724 * buffer onto the head of the vlib_buffer_t, etc. We don't use the
1725 * normal reverse bcopy rewrite code.
1727 * We don't handle ugly RFC-related cases yet, but I'm sure PL will complain
1730 dpo_id_t dpo = DPO_NULL;
1732 dpo_set (&dpo, sr_dpo_type, DPO_PROTO_IP6, t - sm->tunnels);
1734 /* Construct a FIB entry for multicast using the rx/tx fib from the first tunnel */
1735 fib_prefix_t pfx = {
1736 .fp_proto = FIB_PROTOCOL_IP6,
1739 .ip6 = *a->multicast_address,
1742 fib_table_entry_special_dpo_add (t->rx_fib_index,
1745 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1749 mcast_copy = vec_new (ip6_address_t, 1);
1750 memcpy (mcast_copy, a->multicast_address, sizeof (ip6_address_t));
1754 hash_unset_mem (sm->policy_index_by_multicast_address, mcast_copy);
1755 vec_free (mcast_copy);
1760 hash_set_mem (sm->policy_index_by_multicast_address, mcast_copy,
1768 * @brief CLI Parser for Adding or Delete a mapping of IP6 multicast address
1769 * to Segment Routing policy.
1771 * @param vm vlib_main_t *
1772 * @param input unformat_input_t *
1773 * @param cmd vlib_cli_command_t *
1775 * @return error clib_error_t *
1777 static clib_error_t *
1778 sr_add_del_multicast_map_command_fn (vlib_main_t * vm,
1779 unformat_input_t * input,
1780 vlib_cli_command_t * cmd)
1783 ip6_address_t multicast_address;
1784 u8 *policy_name = 0;
1785 int multicast_address_set = 0;
1786 ip6_sr_add_del_multicastmap_args_t _a, *a = &_a;
1789 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1791 if (unformat (input, "del"))
1795 (input, "address %U", unformat_ip6_address, &multicast_address))
1796 multicast_address_set = 1;
1797 else if (unformat (input, "sr-policy %s", &policy_name))
1803 if (!is_del && !policy_name)
1804 return clib_error_return (0, "name of sr policy required");
1806 if (!multicast_address_set)
1807 return clib_error_return (0, "multicast address required");
1809 memset (a, 0, sizeof (*a));
1812 a->multicast_address = &multicast_address;
1813 a->policy_name = policy_name;
1815 #if DPDK > 0 /*Cannot call replicate or configure multicast map yet without DPDK */
1816 rv = ip6_sr_add_del_multicastmap (a);
1818 return clib_error_return (0,
1819 "cannot use multicast replicate spray case without DPDK installed");
1827 return clib_error_return (0, "no policy with name: %s", policy_name);
1830 return clib_error_return (0, "multicast map someting ");
1833 return clib_error_return (0,
1834 "tunnel name to associate to SR policy is required");
1837 return clib_error_return (0, "TODO: deleting policy name %s",
1841 return clib_error_return (0, "BUG: ip6_sr_add_del_policy returns %d",
1851 VLIB_CLI_COMMAND (sr_multicast_map_command, static) = {
1852 .path = "sr multicast-map",
1854 "sr multicast-map address <multicast-ip6-address> sr-policy <sr-policy-name> [del]",
1855 .function = sr_add_del_multicast_map_command_fn,
1860 * @brief CLI Parser for Displaying a mapping of IP6 multicast address
1861 * to Segment Routing policy.
1863 * @param vm vlib_main_t *
1864 * @param input unformat_input_t *
1865 * @param cmd vlib_cli_command_t *
1867 * @return error clib_error_t *
1869 static clib_error_t *
1870 show_sr_multicast_map_fn (vlib_main_t * vm,
1871 unformat_input_t * input, vlib_cli_command_t * cmd)
1873 ip6_sr_main_t *sm = &sr_main;
1876 ip6_address_t multicast_address;
1877 ip6_sr_policy_t *pt;
1879 /* pull all entries from the hash table into vector for display */
1882 hash_foreach_mem (key, value, sm->policy_index_by_multicast_address,
1885 vlib_cli_output (vm, "no multicast maps configured");
1888 multicast_address = *((ip6_address_t *)key);
1889 pt = pool_elt_at_index (sm->policies, value);
1892 vlib_cli_output (vm, "address: %U policy: %s",
1893 format_ip6_address, &multicast_address,
1897 vlib_cli_output (vm, "BUG: policy not found for address: %U with policy index %d",
1898 format_ip6_address, &multicast_address,
1910 VLIB_CLI_COMMAND (show_sr_multicast_map_command, static) = {
1911 .path = "show sr multicast-map",
1912 .short_help = "show sr multicast-map",
1913 .function = show_sr_multicast_map_fn,
1918 #define foreach_sr_fix_dst_addr_next \
1919 _(DROP, "error-drop")
1922 * @brief Struct for valid next-nodes for SR fix destination address node
1926 #define _(s,n) SR_FIX_DST_ADDR_NEXT_##s,
1927 foreach_sr_fix_dst_addr_next
1929 SR_FIX_DST_ADDR_N_NEXT,
1930 } sr_fix_dst_addr_next_t;
1933 * @brief Error strings for SR Fix Destination rewrite
1935 static char *sr_fix_dst_error_strings[] = {
1936 #define sr_fix_dst_error(n,s) s,
1937 #include "sr_fix_dst_error.def"
1938 #undef sr_fix_dst_error
1942 * @brief Struct for errors for SR Fix Destination rewrite
1946 #define sr_fix_dst_error(n,s) SR_FIX_DST_ERROR_##n,
1947 #include "sr_fix_dst_error.def"
1948 #undef sr_fix_dst_error
1950 } sr_fix_dst_error_t;
1953 * @brief Information for fix address trace
1957 ip6_address_t src, dst;
1961 } sr_fix_addr_trace_t;
1964 * @brief Formatter for fix address trace
1967 format_sr_fix_addr_trace (u8 * s, va_list * args)
1969 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1970 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1971 sr_fix_addr_trace_t *t = va_arg (*args, sr_fix_addr_trace_t *);
1972 vnet_hw_interface_t *hi = 0;
1973 ip_adjacency_t *adj;
1974 ip6_main_t *im = &ip6_main;
1975 ip_lookup_main_t *lm = &im->lookup_main;
1976 vnet_main_t *vnm = vnet_get_main ();
1978 if (t->adj_index != ~0)
1980 adj = ip_get_adjacency (lm, t->adj_index);
1981 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
1984 s = format (s, "SR-FIX_ADDR: next %s ip6 src %U dst %U\n",
1985 (t->next_index == SR_FIX_DST_ADDR_NEXT_DROP)
1986 ? "drop" : "output",
1987 format_ip6_address, &t->src, format_ip6_address, &t->dst);
1988 if (t->next_index != SR_FIX_DST_ADDR_NEXT_DROP)
1991 format (s, "%U\n", format_ip6_sr_header, t->sr, 1 /* print_hmac */ );
1993 format (s, " output via %s",
1994 hi ? (char *) (hi->name) : "Invalid adj");
2000 * @brief Fix SR destination address - dual-loop
2002 * @node sr-fix-dst-addr
2003 * @param vm vlib_main_t *
2004 * @param node vlib_node_runtime_t *
2005 * @param from_frame vlib_frame_t *
2007 * @return from_frame->n_vectors uword
2010 sr_fix_dst_addr (vlib_main_t * vm,
2011 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2013 u32 n_left_from, next_index, *from, *to_next;
2014 ip6_main_t *im = &ip6_main;
2015 ip_lookup_main_t *lm = &im->lookup_main;
2017 from = vlib_frame_vector_args (from_frame);
2018 n_left_from = from_frame->n_vectors;
2020 next_index = node->cached_next_index;
2022 while (n_left_from > 0)
2026 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2029 while (0 && n_left_from >= 4 && n_left_to_next >= 2)
2032 __attribute__ ((unused)) vlib_buffer_t *b0, *b1;
2033 u32 next0 = SR_FIX_DST_ADDR_NEXT_DROP;
2034 u32 next1 = SR_FIX_DST_ADDR_NEXT_DROP;
2036 /* Prefetch next iteration. */
2038 vlib_buffer_t *p2, *p3;
2040 p2 = vlib_get_buffer (vm, from[2]);
2041 p3 = vlib_get_buffer (vm, from[3]);
2043 vlib_prefetch_buffer_header (p2, LOAD);
2044 vlib_prefetch_buffer_header (p3, LOAD);
2053 n_left_to_next -= 2;
2056 b0 = vlib_get_buffer (vm, bi0);
2057 b1 = vlib_get_buffer (vm, bi1);
2060 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2061 to_next, n_left_to_next,
2062 bi0, bi1, next0, next1);
2066 while (n_left_from > 0 && n_left_to_next > 0)
2071 ip_adjacency_t *adj0;
2072 ip6_sr_header_t *sr0;
2073 u32 next0 = SR_FIX_DST_ADDR_NEXT_DROP;
2074 ip6_address_t *new_dst0;
2075 ethernet_header_t *eh0;
2082 n_left_to_next -= 1;
2084 b0 = vlib_get_buffer (vm, bi0);
2087 ip_get_adjacency (lm, vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
2088 next0 = adj0->mcast_group_index;
2090 /* We should be pointing at an Ethernet header... */
2091 eh0 = vlib_buffer_get_current (b0);
2092 ip0 = (ip6_header_t *) (eh0 + 1);
2093 sr0 = (ip6_sr_header_t *) (ip0 + 1);
2095 /* We'd better find an SR header... */
2096 if (PREDICT_FALSE (ip0->protocol != IPPROTO_IPV6_ROUTE))
2098 b0->error = node->errors[SR_FIX_DST_ERROR_NO_SR_HEADER];
2104 * We get here from sr_rewrite or sr_local, with
2105 * sr->segments_left pointing at the (copy of the original) dst
2106 * address. Use it, then increment sr0->segments_left.
2109 /* Out of segments? Turf the packet */
2110 if (PREDICT_FALSE (sr0->segments_left == 0))
2112 b0->error = node->errors[SR_FIX_DST_ERROR_NO_MORE_SEGMENTS];
2117 * Rewrite the packet with the original dst address
2118 * We assume that the last segment (in processing order) contains
2119 * the original dst address. The list is reversed, so sr0->segments
2120 * contains the original dst address.
2122 new_dst0 = sr0->segments;
2123 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
2124 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
2129 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2131 sr_fix_addr_trace_t *t = vlib_add_trace (vm, node,
2133 t->next_index = next0;
2136 if (next0 != SR_FIX_DST_ADDR_NEXT_DROP)
2138 t->adj_index = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2139 clib_memcpy (t->src.as_u8, ip0->src_address.as_u8,
2140 sizeof (t->src.as_u8));
2141 clib_memcpy (t->dst.as_u8, ip0->dst_address.as_u8,
2142 sizeof (t->dst.as_u8));
2143 clib_memcpy (t->sr, sr0, sizeof (t->sr));
2147 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2148 to_next, n_left_to_next,
2152 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2154 return from_frame->n_vectors;
2159 VLIB_REGISTER_NODE (sr_fix_dst_addr_node) = {
2160 .function = sr_fix_dst_addr,
2161 .name = "sr-fix-dst-addr",
2162 /* Takes a vector of packets. */
2163 .vector_size = sizeof (u32),
2164 .format_trace = format_sr_fix_addr_trace,
2165 .format_buffer = format_ip6_sr_header_with_length,
2167 .runtime_data_bytes = 0,
2169 .n_errors = SR_FIX_DST_N_ERROR,
2170 .error_strings = sr_fix_dst_error_strings,
2172 .n_next_nodes = SR_FIX_DST_ADDR_N_NEXT,
2174 #define _(s,n) [SR_FIX_DST_ADDR_NEXT_##s] = n,
2175 foreach_sr_fix_dst_addr_next
2181 VLIB_NODE_FUNCTION_MULTIARCH (sr_fix_dst_addr_node, sr_fix_dst_addr)
2182 static clib_error_t *sr_init (vlib_main_t * vm)
2184 ip6_sr_main_t *sm = &sr_main;
2185 clib_error_t *error = 0;
2186 vlib_node_t *ip6_lookup_node, *ip6_rewrite_node;
2188 if ((error = vlib_call_init_function (vm, ip_main_init)))
2191 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
2195 sm->vnet_main = vnet_get_main ();
2197 vec_validate (sm->hmac_keys, 0);
2198 sm->hmac_keys[0].shared_secret = (u8 *) 0xdeadbeef;
2200 sm->tunnel_index_by_key =
2201 hash_create_mem (0, sizeof (ip6_sr_tunnel_key_t), sizeof (uword));
2203 sm->tunnel_index_by_name = hash_create_string (0, sizeof (uword));
2205 sm->policy_index_by_policy_name = hash_create_string (0, sizeof (uword));
2207 sm->policy_index_by_multicast_address =
2208 hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword));
2210 sm->hmac_key_by_shared_secret = hash_create_string (0, sizeof (uword));
2212 ip6_register_protocol (IPPROTO_IPV6_ROUTE, sr_local_node.index);
2214 ip6_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip6-lookup");
2215 ASSERT (ip6_lookup_node);
2217 ip6_rewrite_node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite");
2218 ASSERT (ip6_rewrite_node);
2220 #if DPDK > 0 /* Cannot run replicate without DPDK */
2221 /* Add a disposition to sr_replicate for the sr multicast replicate node */
2222 sm->ip6_lookup_sr_replicate_index =
2223 vlib_node_add_next (vm, ip6_lookup_node->index, sr_replicate_node.index);
2226 /* Add a disposition to ip6_rewrite for the sr dst address hack node */
2227 sm->ip6_rewrite_sr_next_index =
2228 vlib_node_add_next (vm, ip6_rewrite_node->index,
2229 sr_fix_dst_addr_node.index);
2231 OpenSSL_add_all_digests ();
2233 sm->md = (void *) EVP_get_digestbyname ("sha1");
2234 sm->hmac_ctx = clib_mem_alloc (sizeof (HMAC_CTX));
2236 sr_dpo_type = dpo_register_new_type (&sr_vft, sr_nodes);
2241 VLIB_INIT_FUNCTION (sr_init);
2244 * @brief Definition of next-nodes for SR local
2246 #define foreach_sr_local_next \
2247 _ (ERROR, "error-drop") \
2248 _ (IP6_LOOKUP, "ip6-lookup")
2251 * @brief Struct for definition of next-nodes for SR local
2255 #define _(s,n) SR_LOCAL_NEXT_##s,
2256 foreach_sr_local_next
2262 * @brief Struct for packet trace of SR local
2268 ip6_address_t src, dst;
2274 * @brief Definition of SR local error-strings
2276 static char *sr_local_error_strings[] = {
2277 #define sr_error(n,s) s,
2278 #include "sr_error.def"
2283 * @brief Struct for definition of SR local error-strings
2287 #define sr_error(n,s) SR_LOCAL_ERROR_##n,
2288 #include "sr_error.def"
2294 * @brief Format SR local trace
2297 * @param args va_list *
2302 format_sr_local_trace (u8 * s, va_list * args)
2304 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2305 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2306 sr_local_trace_t *t = va_arg (*args, sr_local_trace_t *);
2308 s = format (s, "SR-LOCAL: src %U dst %U len %u next_index %d",
2309 format_ip6_address, &t->src,
2310 format_ip6_address, &t->dst, t->length, t->next_index);
2313 format (s, "\n %U", format_ip6_sr_header, t->sr, 1 /* print_hmac */ );
2315 s = format (s, "\n popped SR header");
2321 /* $$$$ fixme: smp, don't copy data, cache input, output (maybe) */
2323 * @brief Validate the SR HMAC
2325 * @param sm ip6_sr_main_t *
2326 * @param ip ip6_header_t *
2327 * @param sr ip6_sr_header_t *
2329 * @return retval int
2332 sr_validate_hmac (ip6_sr_main_t * sm, ip6_header_t * ip, ip6_sr_header_t * sr)
2338 ip6_address_t *addrp;
2340 ip6_sr_hmac_key_t *hmac_key;
2341 static u8 *signature;
2344 key_index = sr->hmac_key;
2346 /* No signature? Pass... */
2350 /* We don't know about this key? Fail... */
2351 if (key_index >= vec_len (sm->hmac_keys))
2354 vec_validate (signature, SHA256_DIGEST_LENGTH - 1);
2356 hmac_key = sm->hmac_keys + key_index;
2358 vec_reset_length (keybuf);
2360 /* pkt ip6 src address */
2361 vec_add2 (keybuf, copy_target, sizeof (ip6_address_t));
2362 clib_memcpy (copy_target, ip->src_address.as_u8, sizeof (ip6_address_t));
2365 vec_add2 (keybuf, copy_target, 1);
2366 copy_target[0] = sr->first_segment;
2368 /* octet w/ bit 0 = "clean" flag */
2369 vec_add2 (keybuf, copy_target, 1);
2371 = (sr->flags & clib_host_to_net_u16 (IP6_SR_HEADER_FLAG_CLEANUP))
2375 vec_add2 (keybuf, copy_target, 1);
2376 copy_target[0] = sr->hmac_key;
2378 first_segment = sr->first_segment;
2380 addrp = sr->segments;
2383 for (i = 0; i <= first_segment; i++)
2385 vec_add2 (keybuf, copy_target, sizeof (ip6_address_t));
2386 clib_memcpy (copy_target, addrp->as_u8, sizeof (ip6_address_t));
2391 clib_warning ("verify key index %d keybuf: %U", key_index,
2392 format_hex_bytes, keybuf, vec_len (keybuf));
2396 /* SHA1 is shorter than SHA-256 */
2397 memset (signature, 0, vec_len (signature));
2399 HMAC_CTX_init (sm->hmac_ctx);
2400 if (!HMAC_Init (sm->hmac_ctx, hmac_key->shared_secret,
2401 vec_len (hmac_key->shared_secret), sm->md))
2402 clib_warning ("barf1");
2403 if (!HMAC_Update (sm->hmac_ctx, keybuf, vec_len (keybuf)))
2404 clib_warning ("barf2");
2405 if (!HMAC_Final (sm->hmac_ctx, signature, &sig_len))
2406 clib_warning ("barf3");
2407 HMAC_CTX_cleanup (sm->hmac_ctx);
2410 clib_warning ("computed signature len %d, value %U", sig_len,
2411 format_hex_bytes, signature, vec_len (signature));
2413 /* Point at the SHA signature in the packet */
2416 clib_warning ("read signature %U", format_hex_bytes, addrp,
2417 SHA256_DIGEST_LENGTH);
2419 return memcmp (signature, addrp, SHA256_DIGEST_LENGTH);
2423 * @brief SR local node
2426 * @param vm vlib_main_t *
2427 * @param node vlib_node_runtime_t *
2428 * @param from_frame vlib_frame_t *
2430 * @return from_frame->n_vectors uword
2433 sr_local (vlib_main_t * vm,
2434 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2436 u32 n_left_from, next_index, *from, *to_next;
2437 ip6_sr_main_t *sm = &sr_main;
2438 u32 (*sr_local_cb) (vlib_main_t *, vlib_node_runtime_t *,
2439 vlib_buffer_t *, ip6_header_t *, ip6_sr_header_t *);
2440 sr_local_cb = sm->sr_local_cb;
2442 from = vlib_frame_vector_args (from_frame);
2443 n_left_from = from_frame->n_vectors;
2445 next_index = node->cached_next_index;
2447 while (n_left_from > 0)
2451 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2453 while (n_left_from >= 4 && n_left_to_next >= 2)
2456 vlib_buffer_t *b0, *b1;
2457 ip6_header_t *ip0, *ip1;
2458 ip6_sr_header_t *sr0, *sr1;
2459 ip6_address_t *new_dst0, *new_dst1;
2460 u32 next0 = SR_LOCAL_NEXT_IP6_LOOKUP;
2461 u32 next1 = SR_LOCAL_NEXT_IP6_LOOKUP;
2462 /* Prefetch next iteration. */
2464 vlib_buffer_t *p2, *p3;
2466 p2 = vlib_get_buffer (vm, from[2]);
2467 p3 = vlib_get_buffer (vm, from[3]);
2469 vlib_prefetch_buffer_header (p2, LOAD);
2470 vlib_prefetch_buffer_header (p3, LOAD);
2472 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2473 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2482 n_left_to_next -= 2;
2486 b0 = vlib_get_buffer (vm, bi0);
2487 ip0 = vlib_buffer_get_current (b0);
2488 sr0 = (ip6_sr_header_t *) (ip0 + 1);
2490 if (PREDICT_FALSE (sr0->type != ROUTING_HEADER_TYPE_SR))
2492 next0 = SR_LOCAL_NEXT_ERROR;
2494 node->errors[SR_LOCAL_ERROR_BAD_ROUTING_HEADER_TYPE];
2498 /* Out of segments? Turf the packet */
2499 if (PREDICT_FALSE (sr0->segments_left == 0))
2501 next0 = SR_LOCAL_NEXT_ERROR;
2502 b0->error = node->errors[SR_LOCAL_ERROR_NO_MORE_SEGMENTS];
2506 if (PREDICT_FALSE (sm->validate_hmac))
2508 if (sr_validate_hmac (sm, ip0, sr0))
2510 next0 = SR_LOCAL_NEXT_ERROR;
2511 b0->error = node->errors[SR_LOCAL_ERROR_HMAC_INVALID];
2516 next0 = sr_local_cb ? sr_local_cb (vm, node, b0, ip0, sr0) : next0;
2519 * To suppress rewrite, return ~SR_LOCAL_NEXT_xxx
2521 if (PREDICT_FALSE (next0 & 0x80000000))
2523 next0 ^= 0xFFFFFFFF;
2524 if (PREDICT_FALSE (next0 == SR_LOCAL_NEXT_ERROR))
2525 b0->error = node->errors[SR_LOCAL_ERROR_APP_CALLBACK];
2531 segment_index0 = sr0->segments_left - 1;
2533 /* Rewrite the packet */
2534 new_dst0 = (ip6_address_t *) (sr0->segments + segment_index0);
2535 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
2536 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
2538 if (PREDICT_TRUE (sr0->segments_left > 0))
2539 sr0->segments_left -= 1;
2542 /* End of the path. Clean up the SR header, or not */
2544 (sr0->segments_left == 0 &&
2546 clib_host_to_net_u16 (IP6_SR_HEADER_FLAG_CLEANUP))))
2548 u64 *copy_dst0, *copy_src0;
2551 * Copy the ip6 header right by the (real) length of the
2552 * sr header. Here's another place which assumes that
2553 * the sr header is the only extention header.
2556 ip0->protocol = sr0->protocol;
2557 vlib_buffer_advance (b0, (sr0->length + 1) * 8);
2559 new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
2560 (sr0->length + 1) * 8;
2561 ip0->payload_length = clib_host_to_net_u16 (new_l0);
2563 copy_src0 = (u64 *) ip0;
2564 copy_dst0 = copy_src0 + (sr0->length + 1);
2566 copy_dst0[4] = copy_src0[4];
2567 copy_dst0[3] = copy_src0[3];
2568 copy_dst0[2] = copy_src0[2];
2569 copy_dst0[1] = copy_src0[1];
2570 copy_dst0[0] = copy_src0[0];
2576 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2578 sr_local_trace_t *tr = vlib_add_trace (vm, node,
2580 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
2581 sizeof (tr->src.as_u8));
2582 clib_memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
2583 sizeof (tr->dst.as_u8));
2584 tr->length = vlib_buffer_length_in_chain (vm, b0);
2585 tr->next_index = next0;
2586 tr->sr_valid = sr0 != 0;
2588 clib_memcpy (tr->sr, sr0, sizeof (tr->sr));
2591 b1 = vlib_get_buffer (vm, bi1);
2592 ip1 = vlib_buffer_get_current (b1);
2593 sr1 = (ip6_sr_header_t *) (ip1 + 1);
2595 if (PREDICT_FALSE (sr1->type != ROUTING_HEADER_TYPE_SR))
2597 next1 = SR_LOCAL_NEXT_ERROR;
2599 node->errors[SR_LOCAL_ERROR_BAD_ROUTING_HEADER_TYPE];
2603 /* Out of segments? Turf the packet */
2604 if (PREDICT_FALSE (sr1->segments_left == 0))
2606 next1 = SR_LOCAL_NEXT_ERROR;
2607 b1->error = node->errors[SR_LOCAL_ERROR_NO_MORE_SEGMENTS];
2611 if (PREDICT_FALSE (sm->validate_hmac))
2613 if (sr_validate_hmac (sm, ip1, sr1))
2615 next1 = SR_LOCAL_NEXT_ERROR;
2616 b1->error = node->errors[SR_LOCAL_ERROR_HMAC_INVALID];
2621 next1 = sr_local_cb ? sr_local_cb (vm, node, b1, ip1, sr1) : next1;
2624 * To suppress rewrite, return ~SR_LOCAL_NEXT_xxx
2626 if (PREDICT_FALSE (next1 & 0x80000000))
2628 next1 ^= 0xFFFFFFFF;
2629 if (PREDICT_FALSE (next1 == SR_LOCAL_NEXT_ERROR))
2630 b1->error = node->errors[SR_LOCAL_ERROR_APP_CALLBACK];
2636 segment_index1 = sr1->segments_left - 1;
2638 /* Rewrite the packet */
2639 new_dst1 = (ip6_address_t *) (sr1->segments + segment_index1);
2640 ip1->dst_address.as_u64[0] = new_dst1->as_u64[0];
2641 ip1->dst_address.as_u64[1] = new_dst1->as_u64[1];
2643 if (PREDICT_TRUE (sr1->segments_left > 0))
2644 sr1->segments_left -= 1;
2647 /* End of the path. Clean up the SR header, or not */
2649 (sr1->segments_left == 0 &&
2651 clib_host_to_net_u16 (IP6_SR_HEADER_FLAG_CLEANUP))))
2653 u64 *copy_dst1, *copy_src1;
2656 * Copy the ip6 header right by the (real) length of the
2657 * sr header. Here's another place which assumes that
2658 * the sr header is the only extention header.
2661 ip1->protocol = sr1->protocol;
2662 vlib_buffer_advance (b1, (sr1->length + 1) * 8);
2664 new_l1 = clib_net_to_host_u16 (ip1->payload_length) -
2665 (sr1->length + 1) * 8;
2666 ip1->payload_length = clib_host_to_net_u16 (new_l1);
2668 copy_src1 = (u64 *) ip1;
2669 copy_dst1 = copy_src1 + (sr1->length + 1);
2671 copy_dst1[4] = copy_src1[4];
2672 copy_dst1[3] = copy_src1[3];
2673 copy_dst1[2] = copy_src1[2];
2674 copy_dst1[1] = copy_src1[1];
2675 copy_dst1[0] = copy_src1[0];
2681 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
2683 sr_local_trace_t *tr = vlib_add_trace (vm, node,
2685 clib_memcpy (tr->src.as_u8, ip1->src_address.as_u8,
2686 sizeof (tr->src.as_u8));
2687 clib_memcpy (tr->dst.as_u8, ip1->dst_address.as_u8,
2688 sizeof (tr->dst.as_u8));
2689 tr->length = vlib_buffer_length_in_chain (vm, b1);
2690 tr->next_index = next1;
2691 tr->sr_valid = sr1 != 0;
2693 clib_memcpy (tr->sr, sr1, sizeof (tr->sr));
2696 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2697 to_next, n_left_to_next,
2698 bi0, bi1, next0, next1);
2701 while (n_left_from > 0 && n_left_to_next > 0)
2705 ip6_header_t *ip0 = 0;
2706 ip6_sr_header_t *sr0;
2707 ip6_address_t *new_dst0;
2708 u32 next0 = SR_LOCAL_NEXT_IP6_LOOKUP;
2715 n_left_to_next -= 1;
2717 b0 = vlib_get_buffer (vm, bi0);
2718 ip0 = vlib_buffer_get_current (b0);
2719 sr0 = (ip6_sr_header_t *) (ip0 + 1);
2721 if (PREDICT_FALSE (sr0->type != ROUTING_HEADER_TYPE_SR))
2723 next0 = SR_LOCAL_NEXT_ERROR;
2725 node->errors[SR_LOCAL_ERROR_BAD_ROUTING_HEADER_TYPE];
2729 /* Out of segments? Turf the packet */
2730 if (PREDICT_FALSE (sr0->segments_left == 0))
2732 next0 = SR_LOCAL_NEXT_ERROR;
2733 b0->error = node->errors[SR_LOCAL_ERROR_NO_MORE_SEGMENTS];
2737 if (PREDICT_FALSE (sm->validate_hmac))
2739 if (sr_validate_hmac (sm, ip0, sr0))
2741 next0 = SR_LOCAL_NEXT_ERROR;
2742 b0->error = node->errors[SR_LOCAL_ERROR_HMAC_INVALID];
2747 next0 = sr_local_cb ? sr_local_cb (vm, node, b0, ip0, sr0) : next0;
2750 * To suppress rewrite, return ~SR_LOCAL_NEXT_xxx
2752 if (PREDICT_FALSE (next0 & 0x80000000))
2754 next0 ^= 0xFFFFFFFF;
2755 if (PREDICT_FALSE (next0 == SR_LOCAL_NEXT_ERROR))
2756 b0->error = node->errors[SR_LOCAL_ERROR_APP_CALLBACK];
2762 segment_index0 = sr0->segments_left - 1;
2764 /* Rewrite the packet */
2765 new_dst0 = (ip6_address_t *) (sr0->segments + segment_index0);
2766 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
2767 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
2769 if (PREDICT_TRUE (sr0->segments_left > 0))
2770 sr0->segments_left -= 1;
2773 /* End of the path. Clean up the SR header, or not */
2775 (sr0->segments_left == 0 &&
2777 clib_host_to_net_u16 (IP6_SR_HEADER_FLAG_CLEANUP))))
2779 u64 *copy_dst0, *copy_src0;
2782 * Copy the ip6 header right by the (real) length of the
2783 * sr header. Here's another place which assumes that
2784 * the sr header is the only extention header.
2787 ip0->protocol = sr0->protocol;
2788 vlib_buffer_advance (b0, (sr0->length + 1) * 8);
2790 new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
2791 (sr0->length + 1) * 8;
2792 ip0->payload_length = clib_host_to_net_u16 (new_l0);
2794 copy_src0 = (u64 *) ip0;
2795 copy_dst0 = copy_src0 + (sr0->length + 1);
2797 copy_dst0[4] = copy_src0[4];
2798 copy_dst0[3] = copy_src0[3];
2799 copy_dst0[2] = copy_src0[2];
2800 copy_dst0[1] = copy_src0[1];
2801 copy_dst0[0] = copy_src0[0];
2807 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2809 sr_local_trace_t *tr = vlib_add_trace (vm, node,
2811 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
2812 sizeof (tr->src.as_u8));
2813 clib_memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
2814 sizeof (tr->dst.as_u8));
2815 tr->length = vlib_buffer_length_in_chain (vm, b0);
2816 tr->next_index = next0;
2817 tr->sr_valid = sr0 != 0;
2819 clib_memcpy (tr->sr, sr0, sizeof (tr->sr));
2822 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2823 to_next, n_left_to_next,
2827 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2829 vlib_node_increment_counter (vm, sr_local_node.index,
2830 SR_LOCAL_ERROR_PKTS_PROCESSED,
2831 from_frame->n_vectors);
2832 return from_frame->n_vectors;
2836 VLIB_REGISTER_NODE (sr_local_node, static) = {
2837 .function = sr_local,
2839 /* Takes a vector of packets. */
2840 .vector_size = sizeof (u32),
2841 .format_trace = format_sr_local_trace,
2843 .runtime_data_bytes = 0,
2845 .n_errors = SR_LOCAL_N_ERROR,
2846 .error_strings = sr_local_error_strings,
2848 .n_next_nodes = SR_LOCAL_N_NEXT,
2850 #define _(s,n) [SR_LOCAL_NEXT_##s] = n,
2851 foreach_sr_local_next
2857 VLIB_NODE_FUNCTION_MULTIARCH (sr_local_node, sr_local)
2858 ip6_sr_main_t *sr_get_main (vlib_main_t * vm)
2860 vlib_call_init_function (vm, sr_init);
2861 ASSERT (sr_local_node.index);
2866 * @brief CLI parser for SR fix destination rewrite node
2868 * @param vm vlib_main_t *
2869 * @param input unformat_input_t *
2870 * @param cmd vlib_cli_command_t *
2872 * @return error clib_error_t *
2874 static clib_error_t *
2875 set_ip6_sr_rewrite_fn (vlib_main_t * vm,
2876 unformat_input_t * input, vlib_cli_command_t * cmd)
2878 fib_prefix_t pfx = {
2879 .fp_proto = FIB_PROTOCOL_IP6,
2885 ip_adjacency_t *adj;
2886 vnet_hw_interface_t *hi;
2888 ip6_sr_main_t *sm = &sr_main;
2889 vnet_main_t *vnm = vnet_get_main ();
2890 fib_node_index_t fei;
2892 if (!unformat (input, "%U", unformat_ip6_address, &pfx.fp_addr.ip6))
2893 return clib_error_return (0, "ip6 address missing in '%U'",
2894 format_unformat_error, input);
2896 if (unformat (input, "rx-table-id %d", &fib_id))
2898 fib_index = fib_table_id_find_fib_index (FIB_PROTOCOL_IP6, fib_id);
2899 if (fib_index == ~0)
2900 return clib_error_return (0, "fib-id %d not found", fib_id);
2903 fei = fib_table_lookup_exact_match (fib_index, &pfx);
2905 if (FIB_NODE_INDEX_INVALID == fei)
2906 return clib_error_return (0, "no match for %U",
2907 format_ip6_address, &pfx.fp_addr.ip6);
2909 adj_index = fib_entry_get_adj_for_source (fei, FIB_SOURCE_SR);
2911 if (ADJ_INDEX_INVALID == adj_index)
2912 return clib_error_return (0, "%U not SR sourced",
2913 format_ip6_address, &pfx.fp_addr.ip6);
2915 adj = adj_get (adj_index);
2917 if (adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
2918 return clib_error_return (0, "%U unresolved (not a rewrite adj)",
2919 format_ip6_address, &pfx.fp_addr.ip6);
2921 adj->rewrite_header.next_index = sm->ip6_rewrite_sr_next_index;
2923 sw_if_index = adj->rewrite_header.sw_if_index;
2924 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2925 adj->rewrite_header.node_index = sr_fix_dst_addr_node.index;
2927 /* $$$$$ hack... steal the mcast group index */
2928 adj->mcast_group_index =
2929 vlib_node_add_next (vm, sr_fix_dst_addr_node.index,
2930 hi->output_node_index);
2936 VLIB_CLI_COMMAND (set_ip6_sr_rewrite, static) = {
2937 .path = "set ip6 sr rewrite",
2938 .short_help = "set ip6 sr rewrite <ip6-address> [fib-id <id>]",
2939 .function = set_ip6_sr_rewrite_fn,
2944 * @brief Register a callback routine to set next0 in sr_local
2949 vnet_register_sr_app_callback (void *cb)
2951 ip6_sr_main_t *sm = &sr_main;
2953 sm->sr_local_cb = cb;
2957 * @brief Test routine for validation of HMAC
2959 static clib_error_t *
2960 test_sr_hmac_validate_fn (vlib_main_t * vm,
2961 unformat_input_t * input, vlib_cli_command_t * cmd)
2963 ip6_sr_main_t *sm = &sr_main;
2965 if (unformat (input, "validate on"))
2966 sm->validate_hmac = 1;
2967 else if (unformat (input, "chunk-offset off"))
2968 sm->validate_hmac = 0;
2970 return clib_error_return (0, "expected validate on|off in '%U'",
2971 format_unformat_error, input);
2973 vlib_cli_output (vm, "hmac signature validation %s",
2974 sm->validate_hmac ? "on" : "off");
2979 VLIB_CLI_COMMAND (test_sr_hmac_validate, static) = {
2980 .path = "test sr hmac",
2981 .short_help = "test sr hmac validate [on|off]",
2982 .function = test_sr_hmac_validate_fn,
2987 * @brief Add or Delete HMAC key
2989 * @param sm ip6_sr_main_t *
2991 * @param shared_secret u8 *
2994 * @return retval i32
2996 // $$$ fixme shouldn't return i32
2998 sr_hmac_add_del_key (ip6_sr_main_t * sm, u32 key_id, u8 * shared_secret,
3002 ip6_sr_hmac_key_t *key;
3006 /* Specific key in use? Fail. */
3007 if (key_id && vec_len (sm->hmac_keys) > key_id
3008 && sm->hmac_keys[key_id].shared_secret)
3012 key = find_or_add_shared_secret (sm, shared_secret, &index);
3013 ASSERT (index == key_id);
3019 if (key_id) /* delete by key ID */
3021 if (vec_len (sm->hmac_keys) <= key_id)
3024 key = sm->hmac_keys + key_id;
3026 hash_unset_mem (sm->hmac_key_by_shared_secret, key->shared_secret);
3027 vec_free (key->shared_secret);
3032 key = find_or_add_shared_secret (sm, shared_secret, &index);
3033 hash_unset_mem (sm->hmac_key_by_shared_secret, key->shared_secret);
3034 vec_free (key->shared_secret);
3039 static clib_error_t *
3040 sr_hmac_add_del_key_fn (vlib_main_t * vm,
3041 unformat_input_t * input, vlib_cli_command_t * cmd)
3043 ip6_sr_main_t *sm = &sr_main;
3047 u8 *shared_secret = 0;
3050 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3052 if (unformat (input, "del"))
3054 else if (unformat (input, "id %d", &key_id))
3056 else if (unformat (input, "key %s", &shared_secret))
3058 /* Do not include the trailing NULL byte. Guaranteed interop issue */
3059 _vec_len (shared_secret) -= 1;
3065 if (is_del == 0 && shared_secret == 0)
3066 return clib_error_return (0, "shared secret must be set to add a key");
3068 if (shared_secret == 0 && key_id_set == 0)
3069 return clib_error_return (0, "shared secret and key id both unset");
3071 rv = sr_hmac_add_del_key (sm, key_id, shared_secret, is_del);
3073 vec_free (shared_secret);
3081 return clib_error_return (0, "sr_hmac_add_del_key returned %d", rv);
3088 VLIB_CLI_COMMAND (sr_hmac, static) = {
3090 .short_help = "sr hmac [del] id <nn> key <str>",
3091 .function = sr_hmac_add_del_key_fn,
3096 * @brief CLI parser for show HMAC key shared secrets
3098 * @param vm vlib_main_t *
3099 * @param input unformat_input_t *
3100 * @param cmd vlib_cli_command_t *
3102 * @return error clib_error_t *
3104 static clib_error_t *
3105 show_sr_hmac_fn (vlib_main_t * vm,
3106 unformat_input_t * input, vlib_cli_command_t * cmd)
3108 ip6_sr_main_t *sm = &sr_main;
3111 for (i = 1; i < vec_len (sm->hmac_keys); i++)
3113 if (sm->hmac_keys[i].shared_secret)
3114 vlib_cli_output (vm, "[%d]: %v", i, sm->hmac_keys[i].shared_secret);
3121 VLIB_CLI_COMMAND (show_sr_hmac, static) = {
3122 .path = "show sr hmac",
3123 .short_help = "show sr hmac",
3124 .function = show_sr_hmac_fn,
3129 * @brief Test for SR debug flag
3131 * @param vm vlib_main_t *
3132 * @param input unformat_input_t *
3133 * @param cmd vlib_cli_command_t *
3135 * @return error clib_error_t *
3137 static clib_error_t *
3138 test_sr_debug_fn (vlib_main_t * vm,
3139 unformat_input_t * input, vlib_cli_command_t * cmd)
3141 ip6_sr_main_t *sm = &sr_main;
3143 if (unformat (input, "on"))
3145 else if (unformat (input, "off"))
3148 return clib_error_return (0, "expected on|off in '%U'",
3149 format_unformat_error, input);
3151 vlib_cli_output (vm, "debug trace now %s", sm->is_debug ? "on" : "off");
3157 VLIB_CLI_COMMAND (test_sr_debug, static) = {
3158 .path = "test sr debug",
3159 .short_help = "test sr debug on|off",
3160 .function = test_sr_debug_fn,
3165 * fd.io coding-style-patch-verification: ON
3168 * eval: (c-set-style "gnu")