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.
18 #include <vnet/vnet.h>
19 #include <vnet/sr/sr.h>
21 #include <openssl/hmac.h>
23 ip6_sr_main_t sr_main;
24 static vlib_node_registration_t sr_local_node;
26 void sr_fix_hmac (ip6_sr_main_t * sm, ip6_header_t * ip,
35 ip6_sr_hmac_key_t * hmac_key;
38 key_index = sr->hmac_key;
40 /* No signature? Pass... */
44 /* We don't know about this key? Fail... */
45 if (key_index >= vec_len (sm->hmac_keys))
48 hmac_key = sm->hmac_keys + key_index;
50 vec_reset_length (keybuf);
52 /* pkt ip6 src address */
53 vec_add2 (keybuf, copy_target, sizeof (ip6_address_t));
54 clib_memcpy (copy_target, ip->src_address.as_u8, sizeof (ip6_address_t));
57 vec_add2 (keybuf, copy_target, 1);
58 copy_target[0] = sr->first_segment;
60 /* octet w/ bit 0 = "clean" flag */
61 vec_add2 (keybuf, copy_target, 1);
63 = (sr->flags & clib_host_to_net_u16 (IP6_SR_HEADER_FLAG_CLEANUP))
67 vec_add2 (keybuf, copy_target, 1);
68 copy_target[0] = sr->hmac_key;
70 first_segment = sr->first_segment;
75 for (i = 0; i <= first_segment; i++)
77 vec_add2 (keybuf, copy_target, sizeof (ip6_address_t));
78 clib_memcpy (copy_target, addrp->as_u8, sizeof (ip6_address_t));
84 HMAC_CTX_init(sm->hmac_ctx);
85 if (!HMAC_Init(sm->hmac_ctx, hmac_key->shared_secret,
86 vec_len(hmac_key->shared_secret),sm->md))
87 clib_warning ("barf1");
88 if (!HMAC_Update(sm->hmac_ctx,keybuf,vec_len(keybuf)))
89 clib_warning ("barf2");
90 if (!HMAC_Final(sm->hmac_ctx, (unsigned char *) addrp, &sig_len))
91 clib_warning ("barf3");
92 HMAC_CTX_cleanup(sm->hmac_ctx);
95 u8 * format_ip6_sr_header_flags (u8 * s, va_list * args)
97 u16 flags = (u16) va_arg (*args, int);
99 int bswap_needed = va_arg (*args, int);
103 flags = clib_host_to_net_u16 (flags);
105 if (flags & IP6_SR_HEADER_FLAG_CLEANUP)
106 s = format (s, "cleanup ");
108 if (flags & IP6_SR_HEADER_FLAG_PROTECTED)
109 s = format (s, "reroute ");
111 s = format (s, "pl: ");
112 for (i = 1; i <= 4; i++)
114 pl_flag = ip6_sr_policy_list_flags (flags, i);
115 s = format (s, "[%d] ", i);
119 case IP6_SR_HEADER_FLAG_PL_ELT_NOT_PRESENT:
120 s = format (s, "NotPr ");
122 case IP6_SR_HEADER_FLAG_PL_ELT_INGRESS_PE:
123 s = format (s, "InPE ");
125 case IP6_SR_HEADER_FLAG_PL_ELT_EGRESS_PE:
126 s = format (s, "EgPE ");
129 case IP6_SR_HEADER_FLAG_PL_ELT_ORIG_SRC_ADDR:
130 s = format (s, "OrgSrc ");
137 u8 * format_ip6_sr_header (u8 * s, va_list * args)
139 ip6_sr_header_t * h = va_arg (*args, ip6_sr_header_t *);
140 int print_hmac = va_arg (*args, int);
141 int i, pl_index, max_segs;
142 int flags_host_byte_order = clib_net_to_host_u16(h->flags);
144 s = format (s, "next proto %d, len %d, type %d",
145 h->protocol, (h->length<<3)+8, h->type);
146 s = format (s, "\n segs left %d, first_segment %d, hmac key %d",
147 h->segments_left, h->first_segment, h->hmac_key);
148 s = format (s, "\n flags %U", format_ip6_sr_header_flags,
149 flags_host_byte_order, 0 /* bswap needed */ );
152 * Header length is in 8-byte units (minus one), so
153 * divide by 2 to ascertain the number of ip6 addresses in the
156 max_segs = (h->length>>1);
158 if (!print_hmac && h->hmac_key)
161 s = format (s, "\n Segments (in processing order):");
163 for (i = h->first_segment; i >= 0; i--)
164 s = format (s, "\n %U", format_ip6_address, h->segments + i);
166 s = format (s, "\n Policy List:");
168 pl_index = 1; /* to match the RFC text */
169 for (i = (h->first_segment+1); i < max_segs; i++, pl_index++)
172 char * tags[] = {" ", "InPE: ", "EgPE: ", "OrgSrc: "};
175 if (pl_index >=1 && pl_index <= 4)
177 int this_pl_flag = ip6_sr_policy_list_flags
178 (flags_host_byte_order, pl_index);
179 tag = tags[this_pl_flag];
182 s = format (s, "\n %s%U", tag, format_ip6_address, h->segments + i);
188 u8 * format_ip6_sr_header_with_length (u8 * s, va_list * args)
190 ip6_header_t * h = va_arg (*args, ip6_header_t *);
191 u32 max_header_bytes = va_arg (*args, u32);
194 header_bytes = sizeof (h[0]) + sizeof (ip6_sr_header_t);
195 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
196 return format (s, "ip6_sr header truncated");
198 s = format (s, "IP6: %U\n", format_ip6_header, h, max_header_bytes);
199 s = format (s, "SR: %U\n", format_ip6_sr_header, (ip6_sr_header_t *)(h+1),
200 0 /* print_hmac */, max_header_bytes);
204 #if DPDK > 0 /* Cannot call replicate yet without DPDK */
205 #define foreach_sr_rewrite_next \
206 _(ERROR, "error-drop") \
207 _(IP6_LOOKUP, "ip6-lookup") \
208 _(SR_LOCAL, "sr-local") \
209 _(SR_REPLICATE,"sr-replicate")
211 #define foreach_sr_rewrite_next \
212 _(ERROR, "error-drop") \
213 _(IP6_LOOKUP, "ip6-lookup") \
214 _(SR_LOCAL, "sr-local")
218 #define _(s,n) SR_REWRITE_NEXT_##s,
219 foreach_sr_rewrite_next
225 ip6_address_t src, dst;
230 } sr_rewrite_trace_t;
232 static char * sr_rewrite_error_strings[] = {
233 #define sr_error(n,s) s,
234 #include "sr_error.def"
239 #define sr_error(n,s) SR_REWRITE_ERROR_##n,
240 #include "sr_error.def"
243 } sr_rewrite_error_t;
246 u8 * format_sr_rewrite_trace (u8 * s, va_list * args)
248 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
249 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
250 sr_rewrite_trace_t * t = va_arg (*args, sr_rewrite_trace_t *);
251 ip6_main_t * im = &ip6_main;
252 ip6_sr_main_t * sm = &sr_main;
253 ip6_sr_tunnel_t *tun = pool_elt_at_index (sm->tunnels, t->tunnel_index);
254 ip6_fib_t * rx_fib, * tx_fib;
256 rx_fib = find_ip6_fib_by_table_index_or_id (im, tun->rx_fib_index,
257 IP6_ROUTE_FLAG_FIB_INDEX);
259 tx_fib = find_ip6_fib_by_table_index_or_id (im, tun->tx_fib_index,
260 IP6_ROUTE_FLAG_FIB_INDEX);
263 (s, "SR-REWRITE: next %s ip6 src %U dst %U len %u\n"
264 " rx-fib-id %d tx-fib-id %d\n%U",
265 (t->next_index == SR_REWRITE_NEXT_SR_LOCAL)
266 ? "sr-local" : "ip6-lookup",
267 format_ip6_address, &t->src,
268 format_ip6_address, &t->dst, t->length,
269 rx_fib->table_id, tx_fib->table_id,
270 format_ip6_sr_header, t->sr, 0 /* print_hmac */);
275 sr_rewrite (vlib_main_t * vm,
276 vlib_node_runtime_t * node,
277 vlib_frame_t * from_frame)
279 u32 n_left_from, next_index, * from, * to_next;
280 ip6_main_t * im = &ip6_main;
281 ip_lookup_main_t * lm = &im->lookup_main;
282 ip6_sr_main_t * sm = &sr_main;
283 u32 (*sr_local_cb) (vlib_main_t *, vlib_node_runtime_t *,
284 vlib_buffer_t *, ip6_header_t *,
286 sr_local_cb = sm->sr_local_cb;
288 from = vlib_frame_vector_args (from_frame);
289 n_left_from = from_frame->n_vectors;
291 next_index = node->cached_next_index;
293 while (n_left_from > 0)
297 vlib_get_next_frame (vm, node, next_index,
298 to_next, n_left_to_next);
300 /* Note 2x loop disabled */
301 while (0 && n_left_from >= 4 && n_left_to_next >= 2)
304 vlib_buffer_t * b0, * b1;
305 ip6_header_t * ip0, * ip1;
306 ip_adjacency_t * adj0, * adj1;
307 ip6_sr_header_t * sr0, * sr1;
308 ip6_sr_tunnel_t * t0, *t1;
309 u64 * copy_src0, * copy_dst0;
310 u64 * copy_src1, * copy_dst1;
311 u32 next0 = SR_REWRITE_NEXT_IP6_LOOKUP;
312 u32 next1 = SR_REWRITE_NEXT_IP6_LOOKUP;
316 /* Prefetch next iteration. */
318 vlib_buffer_t * p2, * p3;
320 p2 = vlib_get_buffer (vm, from[2]);
321 p3 = vlib_get_buffer (vm, from[3]);
323 vlib_prefetch_buffer_header (p2, LOAD);
324 vlib_prefetch_buffer_header (p3, LOAD);
336 b0 = vlib_get_buffer (vm, bi0);
337 b1 = vlib_get_buffer (vm, bi1);
340 * $$$ parse through header(s) to pick the point
341 * where we punch in the SR extention header
344 adj0 = ip_get_adjacency (lm, vnet_buffer(b0)->ip.adj_index[VLIB_TX]);
345 adj1 = ip_get_adjacency (lm, vnet_buffer(b1)->ip.adj_index[VLIB_TX]);
346 t0 = pool_elt_at_index (sm->tunnels,
347 adj0->rewrite_header.sw_if_index);
348 t1 = pool_elt_at_index (sm->tunnels,
349 adj1->rewrite_header.sw_if_index);
351 ASSERT (VLIB_BUFFER_PRE_DATA_SIZE
352 >= ((word) vec_len (t0->rewrite)) + b0->current_data);
353 ASSERT (VLIB_BUFFER_PRE_DATA_SIZE
354 >= ((word) vec_len (t1->rewrite)) + b1->current_data);
356 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->tx_fib_index;
357 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->tx_fib_index;
359 ip0 = vlib_buffer_get_current (b0);
360 ip1 = vlib_buffer_get_current (b1);
363 * SR-unaware service chaining case: pkt coming back from
364 * service has the original dst address, and will already
365 * have an SR header. If so, send it to sr-local
367 if (PREDICT_FALSE(ip0->protocol == 43))
369 vlib_buffer_advance (b0, sizeof(ip0));
370 sr0 = (ip6_sr_header_t *) (ip0+1);
371 new_l0 = clib_net_to_host_u16(ip0->payload_length);
372 next0 = SR_REWRITE_NEXT_SR_LOCAL;
376 copy_dst0 = (u64 *)(((u8 *)ip0) - vec_len (t0->rewrite));
377 copy_src0 = (u64 *) ip0;
380 * Copy data before the punch-in point left by the
381 * required amount. Assume (for the moment) that only
382 * the main packet header needs to be copied.
384 copy_dst0 [0] = copy_src0 [0];
385 copy_dst0 [1] = copy_src0 [1];
386 copy_dst0 [2] = copy_src0 [2];
387 copy_dst0 [3] = copy_src0 [3];
388 copy_dst0 [4] = copy_src0 [4];
389 vlib_buffer_advance (b0, - (word) vec_len(t0->rewrite));
390 ip0 = vlib_buffer_get_current (b0);
391 sr0 = (ip6_sr_header_t *) (ip0+1);
393 clib_memcpy (sr0, t0->rewrite, vec_len (t0->rewrite));
394 /* Fix the next header chain */
395 sr0->protocol = ip0->protocol;
396 ip0->protocol = 43; /* routing extension header */
397 new_l0 = clib_net_to_host_u16(ip0->payload_length) +
398 vec_len (t0->rewrite);
399 ip0->payload_length = clib_host_to_net_u16(new_l0);
400 /* Rewrite the ip6 dst address */
401 ip0->dst_address.as_u64[0] = t0->first_hop.as_u64[0];
402 ip0->dst_address.as_u64[1] = t0->first_hop.as_u64[1];
404 sr_fix_hmac (sm, ip0, sr0);
406 next0 = sr_local_cb ? sr_local_cb (vm, node, b0, ip0, sr0) :
410 * Ignore "do not rewrite" shtik in this path
412 if (PREDICT_FALSE (next0 & 0x80000000))
415 if (PREDICT_FALSE(next0 == SR_REWRITE_NEXT_ERROR))
417 node->errors[SR_REWRITE_ERROR_APP_CALLBACK];
421 if (PREDICT_FALSE(ip1->protocol == 43))
423 vlib_buffer_advance (b1, sizeof(ip1));
424 sr1 = (ip6_sr_header_t *) (ip1+1);
425 new_l1 = clib_net_to_host_u16(ip1->payload_length);
426 next1 = SR_REWRITE_NEXT_SR_LOCAL;
430 copy_dst1 = (u64 *)(((u8 *)ip1) - vec_len (t1->rewrite));
431 copy_src1 = (u64 *) ip1;
433 copy_dst1 [0] = copy_src1 [0];
434 copy_dst1 [1] = copy_src1 [1];
435 copy_dst1 [2] = copy_src1 [2];
436 copy_dst1 [3] = copy_src1 [3];
437 copy_dst1 [4] = copy_src1 [4];
438 vlib_buffer_advance (b1, - (word) vec_len(t1->rewrite));
439 ip1 = vlib_buffer_get_current (b1);
440 sr1 = (ip6_sr_header_t *) (ip1+1);
441 clib_memcpy (sr1, t1->rewrite, vec_len (t1->rewrite));
442 sr1->protocol = ip1->protocol;
444 new_l1 = clib_net_to_host_u16(ip1->payload_length) +
445 vec_len (t1->rewrite);
446 ip1->payload_length = clib_host_to_net_u16(new_l1);
447 ip1->dst_address.as_u64[0] = t1->first_hop.as_u64[0];
448 ip1->dst_address.as_u64[1] = t1->first_hop.as_u64[1];
450 sr_fix_hmac (sm, ip1, sr1);
452 next1 = sr_local_cb ? sr_local_cb (vm, node, b1, ip1, sr1) :
456 * Ignore "do not rewrite" shtik in this path
458 if (PREDICT_FALSE (next1 & 0x80000000))
461 if (PREDICT_FALSE(next1 == SR_REWRITE_NEXT_ERROR))
463 node->errors[SR_REWRITE_ERROR_APP_CALLBACK];
467 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
469 sr_rewrite_trace_t *tr = vlib_add_trace (vm, node,
471 tr->tunnel_index = t0 - sm->tunnels;
472 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
473 sizeof (tr->src.as_u8));
474 clib_memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
475 sizeof (tr->dst.as_u8));
477 tr->next_index = next0;
478 clib_memcpy (tr->sr, sr0, sizeof (tr->sr));
480 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
482 sr_rewrite_trace_t *tr = vlib_add_trace (vm, node,
484 tr->tunnel_index = t1 - sm->tunnels;
485 clib_memcpy (tr->src.as_u8, ip1->src_address.as_u8,
486 sizeof (tr->src.as_u8));
487 clib_memcpy (tr->dst.as_u8, ip1->dst_address.as_u8,
488 sizeof (tr->dst.as_u8));
490 tr->next_index = next1;
491 clib_memcpy (tr->sr, sr1, sizeof (tr->sr));
494 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
495 to_next, n_left_to_next,
496 bi0, bi1, next0, next1);
499 while (n_left_from > 0 && n_left_to_next > 0)
503 ip6_header_t * ip0 = 0;
504 ip_adjacency_t * adj0;
505 ip6_sr_header_t * sr0 = 0;
506 ip6_sr_tunnel_t * t0;
507 u64 * copy_src0, * copy_dst0;
508 u32 next0 = SR_REWRITE_NEXT_IP6_LOOKUP;
518 b0 = vlib_get_buffer (vm, bi0);
521 * $$$ parse through header(s) to pick the point
522 * where we punch in the SR extention header
525 adj0 = ip_get_adjacency (lm, vnet_buffer(b0)->ip.adj_index[VLIB_TX]);
526 t0 = pool_elt_at_index (sm->tunnels,
527 adj0->rewrite_header.sw_if_index);
529 #if DPDK > 0 /* Cannot call replication node yet without DPDK */
530 /* add a replication node */
531 if(PREDICT_FALSE(t0->policy_index != ~0))
533 vnet_buffer(b0)->ip.save_protocol = t0->policy_index;
534 next0=SR_REWRITE_NEXT_SR_REPLICATE;
539 ASSERT (VLIB_BUFFER_PRE_DATA_SIZE
540 >= ((word) vec_len (t0->rewrite)) + b0->current_data);
542 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->tx_fib_index;
544 ip0 = vlib_buffer_get_current (b0);
547 * SR-unaware service chaining case: pkt coming back from
548 * service has the original dst address, and will already
549 * have an SR header. If so, send it to sr-local
551 if (PREDICT_FALSE(ip0->protocol == 43))
553 vlib_buffer_advance (b0, sizeof(ip0));
554 sr0 = (ip6_sr_header_t *) (ip0+1);
555 new_l0 = clib_net_to_host_u16(ip0->payload_length);
556 next0 = SR_REWRITE_NEXT_SR_LOCAL;
560 copy_dst0 = (u64 *)(((u8 *)ip0) - vec_len (t0->rewrite));
561 copy_src0 = (u64 *) ip0;
564 * Copy data before the punch-in point left by the
565 * required amount. Assume (for the moment) that only
566 * the main packet header needs to be copied.
568 copy_dst0 [0] = copy_src0 [0];
569 copy_dst0 [1] = copy_src0 [1];
570 copy_dst0 [2] = copy_src0 [2];
571 copy_dst0 [3] = copy_src0 [3];
572 copy_dst0 [4] = copy_src0 [4];
573 vlib_buffer_advance (b0, - (word) vec_len(t0->rewrite));
574 ip0 = vlib_buffer_get_current (b0);
575 sr0 = (ip6_sr_header_t *) (ip0+1);
577 clib_memcpy (sr0, t0->rewrite, vec_len (t0->rewrite));
578 /* Fix the next header chain */
579 sr0->protocol = ip0->protocol;
580 ip0->protocol = 43; /* routing extension header */
581 new_l0 = clib_net_to_host_u16(ip0->payload_length) +
582 vec_len (t0->rewrite);
583 ip0->payload_length = clib_host_to_net_u16(new_l0);
584 /* Rewrite the ip6 dst address */
585 ip0->dst_address.as_u64[0] = t0->first_hop.as_u64[0];
586 ip0->dst_address.as_u64[1] = t0->first_hop.as_u64[1];
588 sr_fix_hmac (sm, ip0, sr0);
590 next0 = sr_local_cb ? sr_local_cb (vm, node, b0, ip0, sr0) :
594 * Ignore "do not rewrite" shtik in this path
596 if (PREDICT_FALSE (next0 & 0x80000000))
599 if (PREDICT_FALSE(next0 == SR_REWRITE_NEXT_ERROR))
601 node->errors[SR_REWRITE_ERROR_APP_CALLBACK];
605 #if DPDK > 0 /* Cannot run replicate without DPDK and only replicate uses this label */
608 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
610 sr_rewrite_trace_t *tr = vlib_add_trace (vm, node,
612 tr->tunnel_index = t0 - sm->tunnels;
615 memcpy (tr->src.as_u8, ip0->src_address.as_u8,
616 sizeof (tr->src.as_u8));
617 memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
618 sizeof (tr->dst.as_u8));
621 tr->next_index = next0;
622 clib_memcpy (tr->sr, sr0, sizeof (tr->sr));
625 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
626 to_next, n_left_to_next,
630 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
632 return from_frame->n_vectors;
635 VLIB_REGISTER_NODE (sr_rewrite_node) = {
636 .function = sr_rewrite,
637 .name = "sr-rewrite",
638 /* Takes a vector of packets. */
639 .vector_size = sizeof (u32),
640 .format_trace = format_sr_rewrite_trace,
641 .format_buffer = format_ip6_sr_header_with_length,
643 .n_errors = SR_REWRITE_N_ERROR,
644 .error_strings = sr_rewrite_error_strings,
646 .runtime_data_bytes = 0,
648 .n_next_nodes = SR_REWRITE_N_NEXT,
650 #define _(s,n) [SR_REWRITE_NEXT_##s] = n,
651 foreach_sr_rewrite_next
656 VLIB_NODE_FUNCTION_MULTIARCH (sr_rewrite_node, sr_rewrite)
658 static int ip6_delete_route_no_next_hop (ip6_address_t *dst_address_arg,
659 u32 dst_address_length,
662 ip6_add_del_route_args_t a;
663 ip6_address_t dst_address;
665 ip6_main_t * im6 = &ip6_main;
666 BVT(clib_bihash_kv) kv, value;
668 fib = find_ip6_fib_by_table_index_or_id (im6, rx_table_id,
669 IP6_ROUTE_FLAG_TABLE_ID);
670 memset (&a, 0, sizeof (a));
671 a.flags |= IP4_ROUTE_FLAG_DEL;
672 a.dst_address_length = dst_address_length;
674 dst_address = *dst_address_arg;
676 ip6_address_mask (&dst_address,
677 &im6->fib_masks[dst_address_length]);
679 kv.key[0] = dst_address.as_u64[0];
680 kv.key[1] = dst_address.as_u64[1];
681 kv.key[2] = ((u64)((fib - im6->fibs))<<32) | dst_address_length;
683 if (BV(clib_bihash_search)(&im6->ip6_lookup_table, &kv, &value) < 0)
685 clib_warning ("%U/%d not in FIB",
686 format_ip6_address, &a.dst_address,
687 a.dst_address_length);
691 a.adj_index = value.value;
692 a.dst_address = dst_address;
694 ip6_add_del_route (im6, &a);
695 ip6_maybe_remap_adjacencies (im6, rx_table_id, IP6_ROUTE_FLAG_TABLE_ID);
699 static ip6_sr_hmac_key_t *
700 find_or_add_shared_secret (ip6_sr_main_t * sm, u8 * secret, u32 * indexp)
703 ip6_sr_hmac_key_t * key = 0;
706 p = hash_get_mem (sm->hmac_key_by_shared_secret, secret);
710 key = vec_elt_at_index (sm->hmac_keys, p[0]);
716 /* Specific key ID? */
717 if (indexp && *indexp)
719 vec_validate (sm->hmac_keys, *indexp);
720 key = sm->hmac_keys + *indexp;
724 for (i = 0; i < vec_len (sm->hmac_keys); i++)
726 if (sm->hmac_keys[i].shared_secret == 0)
727 key = sm->hmac_keys + i;
730 vec_validate (sm->hmac_keys, i);
731 key = sm->hmac_keys + i;
736 key->shared_secret = vec_dup (secret);
738 hash_set_mem (sm->hmac_key_by_shared_secret, key->shared_secret,
739 key - sm->hmac_keys);
742 *indexp = key - sm->hmac_keys;
747 int ip6_sr_add_del_tunnel (ip6_sr_add_del_tunnel_args_t * a)
749 ip6_main_t * im = &ip6_main;
750 ip_lookup_main_t * lm = &im->lookup_main;
751 ip6_sr_tunnel_key_t key;
754 ip6_sr_header_t * h = 0;
756 ip6_address_t * addrp, *this_address;
757 ip_adjacency_t adj, * ap, * add_adj = 0;
759 ip6_sr_main_t * sm = &sr_main;
761 u32 rx_fib_index, tx_fib_index;
762 ip6_add_del_route_args_t aa;
763 u32 hmac_key_index_u32;
764 u8 hmac_key_index = 0;
765 ip6_sr_policy_t * pt;
768 /* Make sure that the rx FIB exists */
769 p = hash_get (im->fib_index_by_table_id, a->rx_table_id);
774 /* remember the FIB index */
777 /* Make sure that the supplied FIB exists */
778 p = hash_get (im->fib_index_by_table_id, a->tx_table_id);
783 /* remember the FIB index */
786 clib_memcpy (key.src.as_u8, a->src_address->as_u8, sizeof (key.src));
787 clib_memcpy (key.dst.as_u8, a->dst_address->as_u8, sizeof (key.dst));
789 /* When adding a tunnel:
790 * - If a "name" is given, it must not exist.
791 * - The "key" is always checked, and must not exist.
792 * When deleting a tunnel:
793 * - If the "name" is given, and it exists, then use it.
794 * - If the "name" is not given, use the "key".
795 * - If the "name" and the "key" are given, then both must point to the same
800 p = hash_get_mem (sm->tunnel_index_by_key, &key);
802 /* If the name is given, look it up */
804 n = hash_get_mem(sm->tunnel_index_by_name, a->name);
808 /* validate key/name parameters */
809 if (!a->is_del) /* adding a tunnel */
811 if (a->name && n) /* name given & exists already */
813 if (p) /* key exists already */
816 else /* deleting a tunnel */
818 if (!p) /* key doesn't exist */
820 if (a->name && !n) /* name given & it doesn't exist */
823 if (n) /* name given & found */
825 if (n[0] != p[0]) /* name and key do not point to the same thing */
831 if (a->is_del) /* delete the tunnel */
835 /* Delete existing tunnel */
836 t = pool_elt_at_index (sm->tunnels, p[0]);
838 ip6_delete_route_no_next_hop (&t->key.dst, t->dst_mask_width,
840 vec_free (t->rewrite);
841 /* Remove tunnel from any policy if associated */
842 if (t->policy_index != ~0)
844 pt=pool_elt_at_index (sm->policies, t->policy_index);
845 for (i=0; i< vec_len (pt->tunnel_indices); i++)
847 if (pt->tunnel_indices[i] == t - sm->tunnels)
849 vec_delete (pt->tunnel_indices, 1, i);
853 clib_warning ("Tunnel index %d not found in policy_index %d",
854 t - sm->tunnels, pt - sm->policies);
856 /* If this is last tunnel in the policy, clean up the policy too */
857 if (vec_len (pt->tunnel_indices) == 0)
859 hash_unset_mem (sm->policy_index_by_policy_name, pt->name);
861 pool_put (sm->policies, pt);
865 /* Clean up the tunnel by name */
868 hash_unset_mem (sm->tunnel_index_by_name, t->name);
871 pool_put (sm->tunnels, t);
872 hp = hash_get_pair (sm->tunnel_index_by_key, &key);
873 key_copy = (void *)(hp->key);
874 hash_unset_mem (sm->tunnel_index_by_key, &key);
879 /* create a new tunnel */
880 pool_get (sm->tunnels, t);
881 memset (t, 0, sizeof (*t));
882 t->policy_index = ~0;
884 clib_memcpy (&t->key, &key, sizeof (t->key));
885 t->dst_mask_width = a->dst_mask_width;
886 t->rx_fib_index = rx_fib_index;
887 t->tx_fib_index = tx_fib_index;
889 /* The first specified hop goes right into the dst address */
890 if (vec_len(a->segments))
892 t->first_hop = a->segments[0];
893 /* It won't feel nice if we do it twice */
894 vec_delete (a->segments, 1, 0);
896 else /* there must be at least one segment... */
900 * Create the sr header rewrite string
901 * We append the dst address to the set of next hops
902 * so the ultimate recipient can tell where the
903 * packet entered the SR domain
905 header_length = sizeof (*h) +
906 sizeof (ip6_address_t) * (vec_len (a->segments) + vec_len (a->tags));
908 if (a->shared_secret)
910 /* Allocate a new key slot if we don't find the secret key */
911 hmac_key_index_u32 = 0;
912 (void) find_or_add_shared_secret (sm, a->shared_secret,
913 &hmac_key_index_u32);
915 /* Hey Vinz Clortho: Gozzer is pissed.. you're out of keys! */
916 if (hmac_key_index_u32 >= 256)
918 hmac_key_index = hmac_key_index_u32;
919 header_length += SHA256_DIGEST_LENGTH;
922 vec_validate (t->rewrite, header_length-1);
924 h = (ip6_sr_header_t *) t->rewrite;
926 h->protocol = 0xFF; /* we don't know yet */
928 h->length = (header_length/8) - 1;
929 h->type = ROUTING_HEADER_TYPE_SR;
930 h->segments_left = vec_len (a->segments);
931 h->first_segment = vec_len(a->segments) -1;
932 if (a->shared_secret)
933 h->hmac_key = hmac_key_index & 0xFF;
935 h->flags = a->flags_net_byte_order;
937 /* Paint on the segment list, in reverse */
938 addrp = h->segments + (vec_len (a->segments) - 1);
940 vec_foreach (this_address, a->segments)
942 clib_memcpy (addrp->as_u8, this_address->as_u8, sizeof (ip6_address_t));
946 /* Paint on the tag list, not reversed */
947 addrp = h->segments + vec_len(a->segments);
949 vec_foreach (this_address, a->tags)
951 clib_memcpy (addrp->as_u8, this_address->as_u8, sizeof (ip6_address_t));
955 key_copy = vec_new (ip6_sr_tunnel_key_t, 1);
956 clib_memcpy (key_copy, &key, sizeof (ip6_sr_tunnel_key_t));
957 hash_set_mem (sm->tunnel_index_by_key, key_copy, t - sm->tunnels);
959 memset(&adj, 0, sizeof (adj));
961 /* Create an adjacency and add to v6 fib */
962 adj.lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
963 adj.lookup_next_index = sm->ip6_lookup_sr_next_index;
964 adj.explicit_fib_index = ~0;
966 ap = ip_add_adjacency (lm, &adj, 1 /* one adj */,
970 * Stick the tunnel index into the rewrite header.
972 * Unfortunately, inserting an SR header according to the various
973 * RFC's requires parsing through the ip6 header, perhaps consing a
974 * buffer onto the head of the vlib_buffer_t, etc. We don't use the
975 * normal reverse bcopy rewrite code.
977 * We don't handle ugly RFC-related cases yet, but I'm sure PL will complain
980 ap->rewrite_header.sw_if_index = t - sm->tunnels;
982 vec_add1 (add_adj, ap[0]);
984 clib_memcpy (aa.dst_address.as_u8, a->dst_address, sizeof (aa.dst_address.as_u8));
985 aa.dst_address_length = a->dst_mask_width;
987 aa.flags = (a->is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD);
988 aa.flags |= IP6_ROUTE_FLAG_FIB_INDEX;
989 aa.table_index_or_table_id = rx_fib_index;
990 aa.add_adj = add_adj;
991 aa.adj_index = adj_index;
993 ip6_add_del_route (im, &aa);
998 p=hash_get_mem (sm->policy_index_by_policy_name, a->policy_name);
1001 pt = pool_elt_at_index (sm->policies, p[0]);
1003 else /* no policy, lets create one */
1005 pool_get (sm->policies, pt);
1006 memset (pt, 0, sizeof(*pt));
1007 pt->name = format (0, "%s%c", a->policy_name, 0);
1008 hash_set_mem (sm->policy_index_by_policy_name, pt->name, pt - sm->policies);
1009 p=hash_get_mem (sm->policy_index_by_policy_name, a->policy_name);
1011 vec_add1 (pt->tunnel_indices, t - sm->tunnels);
1012 t->policy_index = p[0]; /* equiv. to (pt - sm->policies) */
1017 t->name = format (0, "%s%c", a->name, 0);
1018 hash_set_mem(sm->tunnel_index_by_name, t->name, t - sm->tunnels);
1024 static clib_error_t *
1025 sr_add_del_tunnel_command_fn (vlib_main_t * vm,
1026 unformat_input_t * input,
1027 vlib_cli_command_t * cmd)
1030 ip6_address_t src_address;
1031 int src_address_set = 0;
1032 ip6_address_t dst_address;
1034 int dst_address_set = 0;
1036 u8 *shared_secret = 0;
1038 u8 *policy_name = 0;
1039 u32 rx_table_id = 0;
1040 u32 tx_table_id = 0;
1041 ip6_address_t * segments = 0;
1042 ip6_address_t * this_seg;
1043 ip6_address_t * tags = 0;
1044 ip6_address_t * this_tag;
1045 ip6_sr_add_del_tunnel_args_t _a, *a=&_a;
1046 ip6_address_t next_address, tag;
1050 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1052 if (unformat (input, "del"))
1054 else if (unformat (input, "rx-fib-id %d", &rx_table_id))
1056 else if (unformat (input, "tx-fib-id %d", &tx_table_id))
1058 else if (unformat (input, "src %U", unformat_ip6_address, &src_address))
1059 src_address_set = 1;
1060 else if (unformat (input, "name %s", &name))
1062 else if (unformat (input, "policy %s", &policy_name))
1064 else if (unformat (input, "dst %U/%d",
1065 unformat_ip6_address, &dst_address,
1067 dst_address_set = 1;
1068 else if (unformat (input, "next %U", unformat_ip6_address,
1071 vec_add2 (segments, this_seg, 1);
1072 clib_memcpy (this_seg->as_u8, next_address.as_u8, sizeof (*this_seg));
1074 else if (unformat (input, "tag %U", unformat_ip6_address,
1077 vec_add2 (tags, this_tag, 1);
1078 clib_memcpy (this_tag->as_u8, tag.as_u8, sizeof (*this_tag));
1080 else if (unformat (input, "clean"))
1081 flags |= IP6_SR_HEADER_FLAG_CLEANUP;
1082 else if (unformat (input, "protected"))
1083 flags |= IP6_SR_HEADER_FLAG_PROTECTED;
1084 else if (unformat (input, "key %s", &shared_secret))
1085 /* Do not include the trailing NULL byte. Guaranteed interop issue */
1086 _vec_len (shared_secret) -= 1;
1087 else if (unformat (input, "InPE %d", &pl_index))
1089 if (pl_index <= 0 || pl_index > 4)
1091 pl_index_range_error:
1092 return clib_error_return
1093 (0, "Policy List Element Index %d out of range (1-4)", pl_index);
1096 flags |= IP6_SR_HEADER_FLAG_PL_ELT_INGRESS_PE
1097 << ip6_sr_policy_list_shift_from_index (pl_index);
1099 else if (unformat (input, "EgPE %d", &pl_index))
1101 if (pl_index <= 0 || pl_index > 4)
1102 goto pl_index_range_error;
1103 flags |= IP6_SR_HEADER_FLAG_PL_ELT_EGRESS_PE
1104 << ip6_sr_policy_list_shift_from_index (pl_index);
1106 else if (unformat (input, "OrgSrc %d", &pl_index))
1108 if (pl_index <= 0 || pl_index > 4)
1109 goto pl_index_range_error;
1110 flags |= IP6_SR_HEADER_FLAG_PL_ELT_ORIG_SRC_ADDR
1111 << ip6_sr_policy_list_shift_from_index (pl_index);
1117 if (!src_address_set)
1118 return clib_error_return (0, "src address required");
1120 if (!dst_address_set)
1121 return clib_error_return (0, "dst address required");
1124 return clib_error_return (0, "at least one sr segment required");
1126 memset (a, 0, sizeof (*a));
1127 a->src_address = &src_address;
1128 a->dst_address = &dst_address;
1129 a->dst_mask_width = dst_mask_width;
1130 a->segments = segments;
1132 a->flags_net_byte_order = clib_host_to_net_u16(flags);
1134 a->rx_table_id = rx_table_id;
1135 a->tx_table_id = tx_table_id;
1136 a->shared_secret = shared_secret;
1143 if (vec_len(policy_name))
1144 a->policy_name = policy_name;
1148 rv = ip6_sr_add_del_tunnel (a);
1150 vec_free (segments);
1152 vec_free (shared_secret);
1160 return clib_error_return (0, "SR tunnel src %U dst %U already exists",
1161 format_ip6_address, &src_address,
1162 format_ip6_address, &dst_address);
1165 return clib_error_return (0, "SR tunnel src %U dst %U does not exist",
1166 format_ip6_address, &src_address,
1167 format_ip6_address, &dst_address);
1170 return clib_error_return (0, "FIB table %d does not exist", rx_table_id);
1173 return clib_error_return (0, "At least one segment is required");
1176 return clib_error_return (0, "BUG: ip6_sr_add_del_tunnel returns %d",
1183 VLIB_CLI_COMMAND (sr_tunnel_command, static) = {
1184 .path = "sr tunnel",
1186 "sr tunnel [del] [name <name>] src <addr> dst <addr> [next <addr>] "
1187 "[clean] [reroute] [key <secret>] [policy <policy_name>]"
1188 "[rx-fib-id <fib_id>] [tx-fib-id <fib_id>]",
1189 .function = sr_add_del_tunnel_command_fn,
1193 ip6_sr_tunnel_display (vlib_main_t * vm,
1194 ip6_sr_tunnel_t * t)
1196 ip6_main_t * im = &ip6_main;
1197 ip6_sr_main_t * sm = &sr_main;
1198 ip6_fib_t * rx_fib, * tx_fib;
1199 ip6_sr_policy_t * pt;
1201 rx_fib = find_ip6_fib_by_table_index_or_id (im, t->rx_fib_index,
1202 IP6_ROUTE_FLAG_FIB_INDEX);
1204 tx_fib = find_ip6_fib_by_table_index_or_id (im, t->tx_fib_index,
1205 IP6_ROUTE_FLAG_FIB_INDEX);
1208 vlib_cli_output (vm,"sr tunnel name: %s", (char *)t->name);
1210 vlib_cli_output (vm, "src %U dst %U first hop %U",
1211 format_ip6_address, &t->key.src,
1212 format_ip6_address, &t->key.dst,
1213 format_ip6_address, &t->first_hop);
1214 vlib_cli_output (vm, " rx-fib-id %d tx-fib-id %d",
1215 rx_fib->table_id, tx_fib->table_id);
1216 vlib_cli_output (vm, " sr: %U", format_ip6_sr_header, t->rewrite,
1217 0 /* print_hmac */);
1219 if (t->policy_index != ~0)
1221 pt=pool_elt_at_index(sm->policies, t->policy_index);
1222 vlib_cli_output (vm,"sr policy: %s", (char *)pt->name);
1224 vlib_cli_output (vm, "-------");
1229 static clib_error_t *
1230 show_sr_tunnel_fn (vlib_main_t * vm,
1231 unformat_input_t * input,
1232 vlib_cli_command_t * cmd)
1234 static ip6_sr_tunnel_t ** tunnels;
1235 ip6_sr_tunnel_t * t;
1236 ip6_sr_main_t * sm = &sr_main;
1241 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1243 if (unformat (input, "name %s", &name))
1245 p=hash_get_mem (sm->tunnel_index_by_name, name);
1247 vlib_cli_output (vm, "No SR tunnel with name: %s. Showing all.", name);
1253 vec_reset_length (tunnels);
1255 if(!p) /* Either name parm not passed or no tunnel with that name found, show all */
1257 pool_foreach (t, sm->tunnels,
1259 vec_add1 (tunnels, t);
1262 else /* Just show the one tunnel by name */
1263 vec_add1 (tunnels, &sm->tunnels[p[0]]);
1265 if (vec_len (tunnels) == 0)
1266 vlib_cli_output (vm, "No SR tunnels configured");
1268 for (i = 0; i < vec_len (tunnels); i++)
1271 ip6_sr_tunnel_display (vm, t);
1277 VLIB_CLI_COMMAND (show_sr_tunnel_command, static) = {
1278 .path = "show sr tunnel",
1279 .short_help = "show sr tunnel [name <sr-tunnel-name>]",
1280 .function = show_sr_tunnel_fn,
1283 int ip6_sr_add_del_policy (ip6_sr_add_del_policy_args_t * a)
1285 ip6_sr_main_t * sm = &sr_main;
1287 ip6_sr_tunnel_t * t = 0;
1288 ip6_sr_policy_t * policy;
1289 u32 * tunnel_indices = 0;
1296 p=hash_get_mem (sm->policy_index_by_policy_name, a->name);
1298 return -6; /* policy name not found */
1300 policy = pool_elt_at_index(sm->policies, p[0]);
1302 vec_foreach_index (i, policy->tunnel_indices)
1304 t = pool_elt_at_index (sm->tunnels, policy->tunnel_indices[i]);
1305 t->policy_index = ~0;
1307 hash_unset_mem (sm->policy_index_by_policy_name, a->name);
1308 pool_put (sm->policies, policy);
1313 if (!vec_len(a->tunnel_names))
1314 return -3; /*tunnel name is required case */
1316 vec_reset_length (tunnel_indices);
1317 /* Check tunnel names, add tunnel_index to policy */
1318 for (i=0; i < vec_len (a->tunnel_names); i++)
1320 p = hash_get_mem (sm->tunnel_index_by_name, a->tunnel_names[i]);
1322 return -4; /* tunnel name not found case */
1324 t = pool_elt_at_index (sm->tunnels, p[0]);
1326 No need to check t==0. -3 condition above ensures name
1328 if (t->policy_index != ~0)
1329 return -5; /* tunnel name already associated with a policy */
1331 /* Add to tunnel indicies */
1332 vec_add1 (tunnel_indices, p[0]);
1335 /* Add policy to ip6_sr_main_t */
1336 pool_get (sm->policies, policy);
1337 policy->name = a->name;
1338 policy->tunnel_indices = tunnel_indices;
1339 hash_set_mem (sm->policy_index_by_policy_name, policy->name, policy - sm->policies);
1341 /* Yes, this could be construed as overkill but the last thing you should do is set
1342 the policy_index on the tunnel after everything is set in ip6_sr_main_t.
1343 If this is deemed overly cautious, could set this in the vec_len(tunnel_names) loop.
1345 for (i=0; i < vec_len(policy->tunnel_indices); i++)
1347 t = pool_elt_at_index (sm->tunnels, policy->tunnel_indices[i]);
1348 t->policy_index = policy - sm->policies;
1355 static clib_error_t *
1356 sr_add_del_policy_command_fn (vlib_main_t * vm,
1357 unformat_input_t * input,
1358 vlib_cli_command_t * cmd)
1361 u8 ** tunnel_names = 0;
1362 u8 * tunnel_name = 0;
1364 ip6_sr_add_del_policy_args_t _a, *a=&_a;
1367 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1369 if (unformat (input, "del"))
1371 else if (unformat (input, "name %s", &name))
1373 else if (unformat (input, "tunnel %s", &tunnel_name))
1377 vec_add1 (tunnel_names, tunnel_name);
1386 return clib_error_return (0, "name of SR policy required");
1389 memset(a, 0, sizeof(*a));
1393 a->tunnel_names = tunnel_names;
1395 rv = ip6_sr_add_del_policy (a);
1397 vec_free(tunnel_names);
1405 return clib_error_return (0, "tunnel name to associate to SR policy is required");
1408 return clib_error_return (0, "tunnel name not found");
1411 return clib_error_return (0, "tunnel already associated with policy");
1414 return clib_error_return (0, "policy name %s not found", name);
1417 return clib_error_return (0, "TODO: deleting policy name %s", name);
1420 return clib_error_return (0, "BUG: ip6_sr_add_del_policy returns %d", rv);
1426 VLIB_CLI_COMMAND (sr_policy_command, static) = {
1427 .path = "sr policy",
1429 "sr policy [del] name <policy-name> tunnel <sr-tunnel-name> [tunnel <sr-tunnel-name>]*",
1430 .function = sr_add_del_policy_command_fn,
1433 static clib_error_t *
1434 show_sr_policy_fn (vlib_main_t * vm,
1435 unformat_input_t * input,
1436 vlib_cli_command_t * cmd)
1438 static ip6_sr_policy_t ** policies;
1439 ip6_sr_policy_t * policy;
1440 ip6_sr_tunnel_t * t;
1441 ip6_sr_main_t * sm = &sr_main;
1446 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1448 if (unformat (input, "name %s", &name))
1450 p=hash_get_mem (sm->policy_index_by_policy_name, name);
1452 vlib_cli_output (vm, "policy with name %s not found. Showing all.", name);
1458 vec_reset_length (policies);
1460 if(!p) /* Either name parm not passed or no policy with that name found, show all */
1462 pool_foreach (policy, sm->policies,
1464 vec_add1 (policies, policy);
1467 else /* Just show the one policy by name and a summary of tunnel names */
1469 policy = pool_elt_at_index(sm->policies, p[0]);
1470 vec_add1 (policies, policy);
1473 if (vec_len (policies) == 0)
1474 vlib_cli_output (vm, "No SR policies configured");
1476 for (i = 0; i < vec_len (policies); i++)
1478 policy = policies [i];
1481 vlib_cli_output (vm,"SR policy name: %s", (char *)policy->name);
1482 for(j = 0; j < vec_len (policy->tunnel_indices); j++)
1484 t = pool_elt_at_index (sm->tunnels, policy->tunnel_indices[j]);
1485 ip6_sr_tunnel_display (vm, t);
1493 VLIB_CLI_COMMAND (show_sr_policy_command, static) = {
1494 .path = "show sr policy",
1495 .short_help = "show sr policy [name <sr-policy-name>]",
1496 .function = show_sr_policy_fn,
1499 int ip6_sr_add_del_multicastmap (ip6_sr_add_del_multicastmap_args_t * a)
1502 ip6_main_t * im = &ip6_main;
1503 ip_lookup_main_t * lm = &im->lookup_main;
1504 ip6_sr_tunnel_t * t;
1505 ip_adjacency_t adj, * ap, * add_adj = 0;
1507 ip6_sr_main_t * sm = &sr_main;
1508 ip6_add_del_route_args_t aa;
1509 ip6_sr_policy_t * pt;
1513 /* clean up the adjacency */
1514 p = hash_get_mem (sm->policy_index_by_multicast_address, a->multicast_address);
1518 /* Get our policy by policy_name */
1519 p = hash_get_mem (sm->policy_index_by_policy_name, a->policy_name);
1525 pt=pool_elt_at_index (sm->policies, p[0]);
1528 Get the first tunnel associated with policy populate the fib adjacency.
1529 From there, since this tunnel will have it's policy_index != ~0 it will
1530 be the trigger in the dual_loop to pull up the policy and make a copy-rewrite
1531 for each tunnel in the policy
1534 t = pool_elt_at_index (sm->tunnels, pt->tunnel_indices[0]);
1536 /* Construct a FIB entry for multicast using the rx/tx fib from the first tunnel */
1537 memset(&adj, 0, sizeof (adj));
1539 /* Create an adjacency and add to v6 fib */
1540 adj.lookup_next_index = sm->ip6_lookup_sr_replicate_index;
1541 adj.explicit_fib_index = ~0;
1543 ap = ip_add_adjacency (lm, &adj, 1 /* one adj */,
1547 * Stick the tunnel index into the rewrite header.
1549 * Unfortunately, inserting an SR header according to the various
1550 * RFC's requires parsing through the ip6 header, perhaps consing a
1551 * buffer onto the head of the vlib_buffer_t, etc. We don't use the
1552 * normal reverse bcopy rewrite code.
1554 * We don't handle ugly RFC-related cases yet, but I'm sure PL will complain
1557 ap->rewrite_header.sw_if_index = t - sm->tunnels;
1559 vec_add1 (add_adj, ap[0]);
1561 memcpy (aa.dst_address.as_u8, a->multicast_address, sizeof (aa.dst_address.as_u8));
1562 aa.dst_address_length = 128;
1564 aa.flags = (a->is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD);
1565 aa.flags |= IP6_ROUTE_FLAG_FIB_INDEX;
1566 aa.table_index_or_table_id = t->rx_fib_index;
1567 aa.add_adj = add_adj;
1568 aa.adj_index = adj_index;
1570 ip6_add_del_route (im, &aa);
1573 u8 * mcast_copy = 0;
1574 mcast_copy = vec_new (ip6_address_t, 1);
1575 memcpy (mcast_copy, a->multicast_address, sizeof (ip6_address_t));
1579 hash_unset_mem (sm->policy_index_by_multicast_address, mcast_copy);
1580 vec_free (mcast_copy);
1585 hash_set_mem (sm->policy_index_by_multicast_address, mcast_copy, pt - sm->policies);
1591 static clib_error_t *
1592 sr_add_del_multicast_map_command_fn (vlib_main_t * vm,
1593 unformat_input_t * input,
1594 vlib_cli_command_t * cmd)
1597 ip6_address_t multicast_address;
1598 u8 * policy_name = 0;
1599 int multicast_address_set = 0;
1600 ip6_sr_add_del_multicastmap_args_t _a, *a=&_a;
1603 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1605 if (unformat (input, "del"))
1607 else if (unformat (input, "address %U", unformat_ip6_address, &multicast_address))
1608 multicast_address_set = 1;
1609 else if (unformat (input, "sr-policy %s", &policy_name))
1615 if (!is_del && !policy_name)
1616 return clib_error_return (0, "name of sr policy required");
1618 if (!multicast_address_set)
1619 return clib_error_return (0, "multicast address required");
1621 memset(a, 0, sizeof(*a));
1624 a->multicast_address = &multicast_address;
1625 a->policy_name = policy_name;
1627 #if DPDK > 0 /*Cannot call replicate or configure multicast map yet without DPDK */
1628 rv = ip6_sr_add_del_multicastmap (a);
1630 return clib_error_return (0, "cannot use multicast replicate spray case without DPDK installed");
1638 return clib_error_return (0, "no policy with name: %s", policy_name);
1641 return clib_error_return (0, "multicast map someting ");
1644 return clib_error_return (0, "tunnel name to associate to SR policy is required");
1647 return clib_error_return (0, "TODO: deleting policy name %s", policy_name);
1650 return clib_error_return (0, "BUG: ip6_sr_add_del_policy returns %d", rv);
1658 VLIB_CLI_COMMAND (sr_multicast_map_command, static) = {
1659 .path = "sr multicast-map",
1661 "sr multicast-map address <multicast-ip6-address> sr-policy <sr-policy-name> [del]",
1662 .function = sr_add_del_multicast_map_command_fn,
1665 static clib_error_t *
1666 show_sr_multicast_map_fn (vlib_main_t * vm,
1667 unformat_input_t * input,
1668 vlib_cli_command_t * cmd)
1670 ip6_sr_main_t * sm = &sr_main;
1673 ip6_address_t multicast_address;
1674 ip6_sr_policy_t * pt ;
1676 /* pull all entries from the hash table into vector for display */
1678 hash_foreach_mem (key, value, sm->policy_index_by_multicast_address,
1681 vlib_cli_output (vm, "no multicast maps configured");
1684 multicast_address = *((ip6_address_t *)key);
1685 pt = pool_elt_at_index (sm->policies, value);
1688 vlib_cli_output (vm, "address: %U policy: %s",
1689 format_ip6_address, &multicast_address,
1693 vlib_cli_output (vm, "BUG: policy not found for address: %U with policy index %d",
1694 format_ip6_address, &multicast_address,
1704 VLIB_CLI_COMMAND (show_sr_multicast_map_command, static) = {
1705 .path = "show sr multicast-map",
1706 .short_help = "show sr multicast-map",
1707 .function = show_sr_multicast_map_fn,
1711 #define foreach_sr_fix_dst_addr_next \
1712 _(DROP, "error-drop")
1715 #define _(s,n) SR_FIX_DST_ADDR_NEXT_##s,
1716 foreach_sr_fix_dst_addr_next
1718 SR_FIX_DST_ADDR_N_NEXT,
1719 } sr_fix_dst_addr_next_t;
1721 static char * sr_fix_dst_error_strings[] = {
1722 #define sr_fix_dst_error(n,s) s,
1723 #include "sr_fix_dst_error.def"
1724 #undef sr_fix_dst_error
1728 #define sr_fix_dst_error(n,s) SR_FIX_DST_ERROR_##n,
1729 #include "sr_fix_dst_error.def"
1730 #undef sr_fix_dst_error
1732 } sr_fix_dst_error_t;
1735 ip6_address_t src, dst;
1739 } sr_fix_addr_trace_t;
1741 u8 * format_sr_fix_addr_trace (u8 * s, va_list * args)
1743 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1744 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1745 sr_fix_addr_trace_t * t = va_arg (*args, sr_fix_addr_trace_t *);
1746 vnet_hw_interface_t * hi = 0;
1747 ip_adjacency_t * adj;
1748 ip6_main_t * im = &ip6_main;
1749 ip_lookup_main_t * lm = &im->lookup_main;
1750 vnet_main_t * vnm = vnet_get_main();
1752 if (t->adj_index != ~0)
1754 adj = ip_get_adjacency (lm, t->adj_index);
1755 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
1758 s = format (s, "SR-FIX_ADDR: next %s ip6 src %U dst %U\n",
1759 (t->next_index == SR_FIX_DST_ADDR_NEXT_DROP)
1760 ? "drop" : "output",
1761 format_ip6_address, &t->src,
1762 format_ip6_address, &t->dst);
1763 if (t->next_index != SR_FIX_DST_ADDR_NEXT_DROP)
1765 s = format (s, "%U\n", format_ip6_sr_header, t->sr, 1 /* print_hmac */);
1766 s = format (s, " output via %s", hi ? (char *)(hi->name)
1773 sr_fix_dst_addr (vlib_main_t * vm,
1774 vlib_node_runtime_t * node,
1775 vlib_frame_t * from_frame)
1777 u32 n_left_from, next_index, * from, * to_next;
1778 ip6_main_t * im = &ip6_main;
1779 ip_lookup_main_t * lm = &im->lookup_main;
1781 from = vlib_frame_vector_args (from_frame);
1782 n_left_from = from_frame->n_vectors;
1784 next_index = node->cached_next_index;
1786 while (n_left_from > 0)
1790 vlib_get_next_frame (vm, node, next_index,
1791 to_next, n_left_to_next);
1794 while (0 && n_left_from >= 4 && n_left_to_next >= 2)
1797 __attribute__((unused)) vlib_buffer_t * b0, * b1;
1798 u32 next0 = SR_FIX_DST_ADDR_NEXT_DROP;
1799 u32 next1 = SR_FIX_DST_ADDR_NEXT_DROP;
1801 /* Prefetch next iteration. */
1803 vlib_buffer_t * p2, * p3;
1805 p2 = vlib_get_buffer (vm, from[2]);
1806 p3 = vlib_get_buffer (vm, from[3]);
1808 vlib_prefetch_buffer_header (p2, LOAD);
1809 vlib_prefetch_buffer_header (p3, LOAD);
1818 n_left_to_next -= 2;
1821 b0 = vlib_get_buffer (vm, bi0);
1822 b1 = vlib_get_buffer (vm, bi1);
1825 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1826 to_next, n_left_to_next,
1827 bi0, bi1, next0, next1);
1831 while (n_left_from > 0 && n_left_to_next > 0)
1836 ip_adjacency_t * adj0;
1837 ip6_sr_header_t * sr0;
1838 u32 next0 = SR_FIX_DST_ADDR_NEXT_DROP;
1839 ip6_address_t *new_dst0;
1840 ethernet_header_t * eh0;
1847 n_left_to_next -= 1;
1849 b0 = vlib_get_buffer (vm, bi0);
1851 adj0 = ip_get_adjacency (lm, vnet_buffer(b0)->ip.adj_index[VLIB_TX]);
1852 next0 = adj0->mcast_group_index;
1854 /* We should be pointing at an Ethernet header... */
1855 eh0 = vlib_buffer_get_current (b0);
1856 ip0 = (ip6_header_t *)(eh0+1);
1857 sr0 = (ip6_sr_header_t *) (ip0+1);
1859 /* We'd better find an SR header... */
1860 if (PREDICT_FALSE(ip0->protocol != 43))
1862 b0->error = node->errors[SR_FIX_DST_ERROR_NO_SR_HEADER];
1868 * We get here from sr_rewrite or sr_local, with
1869 * sr->segments_left pointing at the (copy of the original) dst
1870 * address. Use it, then increment sr0->segments_left.
1873 /* Out of segments? Turf the packet */
1874 if (PREDICT_FALSE (sr0->segments_left == 0))
1876 b0->error = node->errors[SR_FIX_DST_ERROR_NO_MORE_SEGMENTS];
1881 * Rewrite the packet with the original dst address
1882 * We assume that the last segment (in processing order) contains
1883 * the original dst address. The list is reversed, so sr0->segments
1884 * contains the original dst address.
1886 new_dst0 = sr0->segments;
1887 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
1888 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
1893 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
1895 sr_fix_addr_trace_t *t = vlib_add_trace (vm, node,
1897 t->next_index = next0;
1900 if (next0 != SR_FIX_DST_ADDR_NEXT_DROP)
1902 t->adj_index = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
1903 clib_memcpy (t->src.as_u8, ip0->src_address.as_u8,
1904 sizeof (t->src.as_u8));
1905 clib_memcpy (t->dst.as_u8, ip0->dst_address.as_u8,
1906 sizeof (t->dst.as_u8));
1907 clib_memcpy (t->sr, sr0, sizeof (t->sr));
1911 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1912 to_next, n_left_to_next,
1916 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1918 return from_frame->n_vectors;
1922 VLIB_REGISTER_NODE (sr_fix_dst_addr_node) = {
1923 .function = sr_fix_dst_addr,
1924 .name = "sr-fix-dst-addr",
1925 /* Takes a vector of packets. */
1926 .vector_size = sizeof (u32),
1927 .format_trace = format_sr_fix_addr_trace,
1928 .format_buffer = format_ip6_sr_header_with_length,
1930 .runtime_data_bytes = 0,
1932 .n_errors = SR_FIX_DST_N_ERROR,
1933 .error_strings = sr_fix_dst_error_strings,
1935 .n_next_nodes = SR_FIX_DST_ADDR_N_NEXT,
1937 #define _(s,n) [SR_FIX_DST_ADDR_NEXT_##s] = n,
1938 foreach_sr_fix_dst_addr_next
1943 VLIB_NODE_FUNCTION_MULTIARCH (sr_fix_dst_addr_node, sr_fix_dst_addr)
1945 static clib_error_t * sr_init (vlib_main_t * vm)
1947 ip6_sr_main_t * sm = &sr_main;
1948 clib_error_t * error = 0;
1949 vlib_node_t * ip6_lookup_node, * ip6_rewrite_node;
1950 vlib_node_t * ip6_rewrite_local_node;
1951 u32 verify_next_index;
1953 if ((error = vlib_call_init_function (vm, ip_main_init)))
1956 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
1960 sm->vnet_main = vnet_get_main();
1962 vec_validate (sm->hmac_keys, 0);
1963 sm->hmac_keys[0].shared_secret = (u8 *) 0xdeadbeef;
1965 sm->tunnel_index_by_key =
1966 hash_create_mem (0, sizeof (ip6_sr_tunnel_key_t), sizeof (uword));
1968 sm->tunnel_index_by_name =
1969 hash_create_string (0, sizeof (uword));
1971 sm->policy_index_by_policy_name =
1972 hash_create_string(0, sizeof (uword));
1974 sm->policy_index_by_multicast_address =
1975 hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword));
1977 sm->hmac_key_by_shared_secret = hash_create_string (0, sizeof(uword));
1979 ip6_register_protocol (43, sr_local_node.index);
1981 ip6_lookup_node = vlib_get_node_by_name (vm, (u8 *)"ip6-lookup");
1982 ASSERT(ip6_lookup_node);
1984 ip6_rewrite_node = vlib_get_node_by_name (vm, (u8 *)"ip6-rewrite");
1985 ASSERT(ip6_rewrite_node);
1987 ip6_rewrite_local_node = vlib_get_node_by_name (vm,
1988 (u8 *)"ip6-rewrite-local");
1989 ASSERT(ip6_rewrite_local_node);
1991 /* Add a disposition to ip6_lookup for the sr rewrite node */
1992 sm->ip6_lookup_sr_next_index =
1993 vlib_node_add_next (vm, ip6_lookup_node->index, sr_rewrite_node.index);
1995 #if DPDK > 0 /* Cannot run replicate without DPDK */
1996 /* Add a disposition to sr_replicate for the sr multicast replicate node */
1997 sm->ip6_lookup_sr_replicate_index =
1998 vlib_node_add_next (vm, ip6_lookup_node->index, sr_replicate_node.index);
2001 /* Add a disposition to ip6_rewrite for the sr dst address hack node */
2002 sm->ip6_rewrite_sr_next_index =
2003 vlib_node_add_next (vm, ip6_rewrite_node->index,
2004 sr_fix_dst_addr_node.index);
2006 * Fix ip6-rewrite-local, sibling of the above. The sibling bitmap
2007 * isn't set up at this point, so we have to do it manually
2009 verify_next_index = vlib_node_add_next
2010 (vm, ip6_rewrite_local_node->index,
2011 sr_fix_dst_addr_node.index);
2013 ASSERT(sm->ip6_rewrite_sr_next_index == verify_next_index);
2015 OpenSSL_add_all_digests();
2017 sm->md = (void *) EVP_get_digestbyname ("sha1");
2018 sm->hmac_ctx = clib_mem_alloc (sizeof (HMAC_CTX));
2023 VLIB_INIT_FUNCTION (sr_init);
2025 #define foreach_sr_local_next \
2026 _ (ERROR, "error-drop") \
2027 _ (IP6_LOOKUP, "ip6-lookup")
2030 #define _(s,n) SR_LOCAL_NEXT_##s,
2031 foreach_sr_local_next
2039 ip6_address_t src, dst;
2044 static char * sr_local_error_strings[] = {
2045 #define sr_error(n,s) s,
2046 #include "sr_error.def"
2051 #define sr_error(n,s) SR_LOCAL_ERROR_##n,
2052 #include "sr_error.def"
2057 u8 * format_sr_local_trace (u8 * s, va_list * args)
2059 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2060 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2061 sr_local_trace_t * t = va_arg (*args, sr_local_trace_t *);
2063 s = format (s, "SR-LOCAL: src %U dst %U len %u next_index %d",
2064 format_ip6_address, &t->src,
2065 format_ip6_address, &t->dst, t->length, t->next_index);
2067 s = format (s, "\n %U", format_ip6_sr_header, t->sr, 1 /* print_hmac */);
2069 s = format (s, "\n popped SR header");
2075 /* $$$$ fixme: smp, don't copy data, cache input, output (maybe) */
2077 static int sr_validate_hmac (ip6_sr_main_t * sm, ip6_header_t * ip,
2078 ip6_sr_header_t * sr)
2084 ip6_address_t *addrp;
2086 ip6_sr_hmac_key_t * hmac_key;
2087 static u8 * signature;
2090 key_index = sr->hmac_key;
2092 /* No signature? Pass... */
2096 /* We don't know about this key? Fail... */
2097 if (key_index >= vec_len (sm->hmac_keys))
2100 vec_validate (signature, SHA256_DIGEST_LENGTH-1);
2102 hmac_key = sm->hmac_keys + key_index;
2104 vec_reset_length (keybuf);
2106 /* pkt ip6 src address */
2107 vec_add2 (keybuf, copy_target, sizeof (ip6_address_t));
2108 clib_memcpy (copy_target, ip->src_address.as_u8, sizeof (ip6_address_t));
2111 vec_add2 (keybuf, copy_target, 1);
2112 copy_target[0] = sr->first_segment;
2114 /* octet w/ bit 0 = "clean" flag */
2115 vec_add2 (keybuf, copy_target, 1);
2117 = (sr->flags & clib_host_to_net_u16 (IP6_SR_HEADER_FLAG_CLEANUP))
2121 vec_add2 (keybuf, copy_target, 1);
2122 copy_target[0] = sr->hmac_key;
2124 first_segment = sr->first_segment;
2126 addrp = sr->segments;
2129 for (i = 0; i <= first_segment; i++)
2131 vec_add2 (keybuf, copy_target, sizeof (ip6_address_t));
2132 clib_memcpy (copy_target, addrp->as_u8, sizeof (ip6_address_t));
2137 clib_warning ("verify key index %d keybuf: %U", key_index,
2138 format_hex_bytes, keybuf, vec_len(keybuf));
2142 /* SHA1 is shorter than SHA-256 */
2143 memset (signature, 0, vec_len(signature));
2145 HMAC_CTX_init(sm->hmac_ctx);
2146 if (!HMAC_Init(sm->hmac_ctx, hmac_key->shared_secret,
2147 vec_len(hmac_key->shared_secret),sm->md))
2148 clib_warning ("barf1");
2149 if (!HMAC_Update(sm->hmac_ctx,keybuf,vec_len(keybuf)))
2150 clib_warning ("barf2");
2151 if (!HMAC_Final(sm->hmac_ctx,signature,&sig_len))
2152 clib_warning ("barf3");
2153 HMAC_CTX_cleanup(sm->hmac_ctx);
2156 clib_warning ("computed signature len %d, value %U", sig_len,
2157 format_hex_bytes, signature, vec_len(signature));
2159 /* Point at the SHA signature in the packet */
2162 clib_warning ("read signature %U", format_hex_bytes, addrp,
2163 SHA256_DIGEST_LENGTH);
2165 return memcmp (signature, addrp, SHA256_DIGEST_LENGTH);
2169 sr_local (vlib_main_t * vm,
2170 vlib_node_runtime_t * node,
2171 vlib_frame_t * from_frame)
2173 u32 n_left_from, next_index, * from, * to_next;
2174 ip6_sr_main_t * sm = &sr_main;
2175 u32 (*sr_local_cb) (vlib_main_t *, vlib_node_runtime_t *,
2176 vlib_buffer_t *, ip6_header_t *,
2178 sr_local_cb = sm->sr_local_cb;
2180 from = vlib_frame_vector_args (from_frame);
2181 n_left_from = from_frame->n_vectors;
2183 next_index = node->cached_next_index;
2185 while (n_left_from > 0)
2189 vlib_get_next_frame (vm, node, next_index,
2190 to_next, n_left_to_next);
2192 while (n_left_from >= 4 && n_left_to_next >= 2)
2195 vlib_buffer_t * b0, * b1;
2196 ip6_header_t * ip0, *ip1;
2197 ip6_sr_header_t * sr0, *sr1;
2198 ip6_address_t * new_dst0, * new_dst1;
2199 u32 next0 = SR_LOCAL_NEXT_IP6_LOOKUP;
2200 u32 next1 = SR_LOCAL_NEXT_IP6_LOOKUP;
2201 /* Prefetch next iteration. */
2203 vlib_buffer_t * p2, * p3;
2205 p2 = vlib_get_buffer (vm, from[2]);
2206 p3 = vlib_get_buffer (vm, from[3]);
2208 vlib_prefetch_buffer_header (p2, LOAD);
2209 vlib_prefetch_buffer_header (p3, LOAD);
2211 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
2212 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
2221 n_left_to_next -= 2;
2225 b0 = vlib_get_buffer (vm, bi0);
2226 ip0 = vlib_buffer_get_current (b0);
2227 sr0 = (ip6_sr_header_t *)(ip0+1);
2229 if (PREDICT_FALSE(sr0->type != ROUTING_HEADER_TYPE_SR))
2231 next0 = SR_LOCAL_NEXT_ERROR;
2232 b0->error = node->errors[SR_LOCAL_ERROR_BAD_ROUTING_HEADER_TYPE];
2236 /* Out of segments? Turf the packet */
2237 if (PREDICT_FALSE (sr0->segments_left == 0))
2239 next0 = SR_LOCAL_NEXT_ERROR;
2240 b0->error = node->errors[SR_LOCAL_ERROR_NO_MORE_SEGMENTS];
2244 if (PREDICT_FALSE(sm->validate_hmac))
2246 if (sr_validate_hmac (sm, ip0, sr0))
2248 next0 = SR_LOCAL_NEXT_ERROR;
2249 b0->error = node->errors[SR_LOCAL_ERROR_HMAC_INVALID];
2254 next0 = sr_local_cb ? sr_local_cb (vm, node, b0, ip0, sr0) :
2258 * To suppress rewrite, return ~SR_LOCAL_NEXT_xxx
2260 if (PREDICT_FALSE (next0 & 0x80000000))
2262 next0 ^= 0xFFFFFFFF;
2263 if (PREDICT_FALSE(next0 == SR_LOCAL_NEXT_ERROR))
2265 node->errors[SR_LOCAL_ERROR_APP_CALLBACK];
2271 segment_index0 = sr0->segments_left - 1;
2273 /* Rewrite the packet */
2274 new_dst0 = (ip6_address_t *)(sr0->segments + segment_index0);
2275 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
2276 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
2278 if (PREDICT_TRUE (sr0->segments_left > 0))
2279 sr0->segments_left -= 1;
2282 /* End of the path. Clean up the SR header, or not */
2284 (sr0->segments_left == 0 &&
2285 (sr0->flags & clib_host_to_net_u16(IP6_SR_HEADER_FLAG_CLEANUP))))
2287 u64 *copy_dst0, *copy_src0;
2290 * Copy the ip6 header right by the (real) length of the
2291 * sr header. Here's another place which assumes that
2292 * the sr header is the only extention header.
2295 ip0->protocol = sr0->protocol;
2296 vlib_buffer_advance (b0, (sr0->length+1)*8);
2298 new_l0 = clib_net_to_host_u16(ip0->payload_length) -
2300 ip0->payload_length = clib_host_to_net_u16(new_l0);
2302 copy_src0 = (u64 *)ip0;
2303 copy_dst0 = copy_src0 + (sr0->length + 1);
2305 copy_dst0 [4] = copy_src0[4];
2306 copy_dst0 [3] = copy_src0[3];
2307 copy_dst0 [2] = copy_src0[2];
2308 copy_dst0 [1] = copy_src0[1];
2309 copy_dst0 [0] = copy_src0[0];
2315 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
2317 sr_local_trace_t *tr = vlib_add_trace (vm, node,
2319 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
2320 sizeof (tr->src.as_u8));
2321 clib_memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
2322 sizeof (tr->dst.as_u8));
2323 tr->length = vlib_buffer_length_in_chain (vm, b0);
2324 tr->next_index = next0;
2325 tr->sr_valid = sr0 != 0;
2327 clib_memcpy (tr->sr, sr0, sizeof (tr->sr));
2330 b1 = vlib_get_buffer (vm, bi1);
2331 ip1 = vlib_buffer_get_current (b1);
2332 sr1 = (ip6_sr_header_t *)(ip1+1);
2334 if (PREDICT_FALSE(sr1->type != ROUTING_HEADER_TYPE_SR))
2336 next1 = SR_LOCAL_NEXT_ERROR;
2337 b1->error = node->errors[SR_LOCAL_ERROR_BAD_ROUTING_HEADER_TYPE];
2341 /* Out of segments? Turf the packet */
2342 if (PREDICT_FALSE (sr1->segments_left == 0))
2344 next1 = SR_LOCAL_NEXT_ERROR;
2345 b1->error = node->errors[SR_LOCAL_ERROR_NO_MORE_SEGMENTS];
2349 if (PREDICT_FALSE(sm->validate_hmac))
2351 if (sr_validate_hmac (sm, ip1, sr1))
2353 next1 = SR_LOCAL_NEXT_ERROR;
2354 b1->error = node->errors[SR_LOCAL_ERROR_HMAC_INVALID];
2359 next1 = sr_local_cb ? sr_local_cb (vm, node, b1, ip1, sr1) :
2363 * To suppress rewrite, return ~SR_LOCAL_NEXT_xxx
2365 if (PREDICT_FALSE (next1 & 0x80000000))
2367 next1 ^= 0xFFFFFFFF;
2368 if (PREDICT_FALSE(next1 == SR_LOCAL_NEXT_ERROR))
2370 node->errors[SR_LOCAL_ERROR_APP_CALLBACK];
2376 segment_index1 = sr1->segments_left - 1;
2378 /* Rewrite the packet */
2379 new_dst1 = (ip6_address_t *)(sr1->segments + segment_index1);
2380 ip1->dst_address.as_u64[0] = new_dst1->as_u64[0];
2381 ip1->dst_address.as_u64[1] = new_dst1->as_u64[1];
2383 if (PREDICT_TRUE (sr1->segments_left > 0))
2384 sr1->segments_left -= 1;
2387 /* End of the path. Clean up the SR header, or not */
2389 (sr1->segments_left == 0 &&
2390 (sr1->flags & clib_host_to_net_u16(IP6_SR_HEADER_FLAG_CLEANUP))))
2392 u64 *copy_dst1, *copy_src1;
2395 * Copy the ip6 header right by the (real) length of the
2396 * sr header. Here's another place which assumes that
2397 * the sr header is the only extention header.
2400 ip1->protocol = sr1->protocol;
2401 vlib_buffer_advance (b1, (sr1->length+1)*8);
2403 new_l1 = clib_net_to_host_u16(ip1->payload_length) -
2405 ip1->payload_length = clib_host_to_net_u16(new_l1);
2407 copy_src1 = (u64 *)ip1;
2408 copy_dst1 = copy_src1 + (sr1->length + 1);
2410 copy_dst1 [4] = copy_src1[4];
2411 copy_dst1 [3] = copy_src1[3];
2412 copy_dst1 [2] = copy_src1[2];
2413 copy_dst1 [1] = copy_src1[1];
2414 copy_dst1 [0] = copy_src1[0];
2420 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
2422 sr_local_trace_t *tr = vlib_add_trace (vm, node,
2424 clib_memcpy (tr->src.as_u8, ip1->src_address.as_u8,
2425 sizeof (tr->src.as_u8));
2426 clib_memcpy (tr->dst.as_u8, ip1->dst_address.as_u8,
2427 sizeof (tr->dst.as_u8));
2428 tr->length = vlib_buffer_length_in_chain (vm, b1);
2429 tr->next_index = next1;
2430 tr->sr_valid = sr1 != 0;
2432 clib_memcpy (tr->sr, sr1, sizeof (tr->sr));
2435 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2436 to_next, n_left_to_next,
2437 bi0, bi1, next0, next1);
2440 while (n_left_from > 0 && n_left_to_next > 0)
2444 ip6_header_t * ip0 = 0;
2445 ip6_sr_header_t * sr0;
2446 ip6_address_t * new_dst0;
2447 u32 next0 = SR_LOCAL_NEXT_IP6_LOOKUP;
2454 n_left_to_next -= 1;
2456 b0 = vlib_get_buffer (vm, bi0);
2457 ip0 = vlib_buffer_get_current (b0);
2458 sr0 = (ip6_sr_header_t *)(ip0+1);
2460 if (PREDICT_FALSE(sr0->type != ROUTING_HEADER_TYPE_SR))
2462 next0 = SR_LOCAL_NEXT_ERROR;
2463 b0->error = node->errors[SR_LOCAL_ERROR_BAD_ROUTING_HEADER_TYPE];
2467 /* Out of segments? Turf the packet */
2468 if (PREDICT_FALSE (sr0->segments_left == 0))
2470 next0 = SR_LOCAL_NEXT_ERROR;
2471 b0->error = node->errors[SR_LOCAL_ERROR_NO_MORE_SEGMENTS];
2475 if (PREDICT_FALSE(sm->validate_hmac))
2477 if (sr_validate_hmac (sm, ip0, sr0))
2479 next0 = SR_LOCAL_NEXT_ERROR;
2480 b0->error = node->errors[SR_LOCAL_ERROR_HMAC_INVALID];
2485 next0 = sr_local_cb ? sr_local_cb (vm, node, b0, ip0, sr0) :
2489 * To suppress rewrite, return ~SR_LOCAL_NEXT_xxx
2491 if (PREDICT_FALSE (next0 & 0x80000000))
2493 next0 ^= 0xFFFFFFFF;
2494 if (PREDICT_FALSE(next0 == SR_LOCAL_NEXT_ERROR))
2496 node->errors[SR_LOCAL_ERROR_APP_CALLBACK];
2502 segment_index0 = sr0->segments_left - 1;
2504 /* Rewrite the packet */
2505 new_dst0 = (ip6_address_t *)(sr0->segments + segment_index0);
2506 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
2507 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
2509 if (PREDICT_TRUE (sr0->segments_left > 0))
2510 sr0->segments_left -= 1;
2513 /* End of the path. Clean up the SR header, or not */
2515 (sr0->segments_left == 0 &&
2516 (sr0->flags & clib_host_to_net_u16(IP6_SR_HEADER_FLAG_CLEANUP))))
2518 u64 *copy_dst0, *copy_src0;
2521 * Copy the ip6 header right by the (real) length of the
2522 * sr header. Here's another place which assumes that
2523 * the sr header is the only extention header.
2526 ip0->protocol = sr0->protocol;
2527 vlib_buffer_advance (b0, (sr0->length+1)*8);
2529 new_l0 = clib_net_to_host_u16(ip0->payload_length) -
2531 ip0->payload_length = clib_host_to_net_u16(new_l0);
2533 copy_src0 = (u64 *)ip0;
2534 copy_dst0 = copy_src0 + (sr0->length + 1);
2536 copy_dst0 [4] = copy_src0[4];
2537 copy_dst0 [3] = copy_src0[3];
2538 copy_dst0 [2] = copy_src0[2];
2539 copy_dst0 [1] = copy_src0[1];
2540 copy_dst0 [0] = copy_src0[0];
2546 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
2548 sr_local_trace_t *tr = vlib_add_trace (vm, node,
2550 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
2551 sizeof (tr->src.as_u8));
2552 clib_memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
2553 sizeof (tr->dst.as_u8));
2554 tr->length = vlib_buffer_length_in_chain (vm, b0);
2555 tr->next_index = next0;
2556 tr->sr_valid = sr0 != 0;
2558 clib_memcpy (tr->sr, sr0, sizeof (tr->sr));
2561 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2562 to_next, n_left_to_next,
2566 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2568 vlib_node_increment_counter (vm, sr_local_node.index,
2569 SR_LOCAL_ERROR_PKTS_PROCESSED,
2570 from_frame->n_vectors);
2571 return from_frame->n_vectors;
2574 VLIB_REGISTER_NODE (sr_local_node, static) = {
2575 .function = sr_local,
2577 /* Takes a vector of packets. */
2578 .vector_size = sizeof (u32),
2579 .format_trace = format_sr_local_trace,
2581 .runtime_data_bytes = 0,
2583 .n_errors = SR_LOCAL_N_ERROR,
2584 .error_strings = sr_local_error_strings,
2586 .n_next_nodes = SR_LOCAL_N_NEXT,
2588 #define _(s,n) [SR_LOCAL_NEXT_##s] = n,
2589 foreach_sr_local_next
2594 VLIB_NODE_FUNCTION_MULTIARCH (sr_local_node, sr_local)
2596 ip6_sr_main_t * sr_get_main (vlib_main_t * vm)
2598 vlib_call_init_function (vm, sr_init);
2599 ASSERT(sr_local_node.index);
2604 static clib_error_t *
2605 set_ip6_sr_rewrite_fn (vlib_main_t * vm,
2606 unformat_input_t * input,
2607 vlib_cli_command_t * cmd)
2610 ip6_main_t * im = &ip6_main;
2611 ip_lookup_main_t * lm = &im->lookup_main;
2616 ip_adjacency_t * adj;
2617 vnet_hw_interface_t * hi;
2619 ip6_sr_main_t * sm = &sr_main;
2620 vnet_main_t * vnm = vnet_get_main();
2622 if (!unformat (input, "%U", unformat_ip6_address, &a))
2623 return clib_error_return (0, "ip6 address missing in '%U'",
2624 format_unformat_error, input);
2626 if (unformat (input, "rx-table-id %d", &fib_id))
2628 p = hash_get (im->fib_index_by_table_id, fib_id);
2630 return clib_error_return (0, "fib-id %d not found");
2634 adj_index = ip6_fib_lookup_with_table (im, fib_index, &a);
2636 if (adj_index == lm->miss_adj_index)
2637 return clib_error_return (0, "no match for %U",
2638 format_ip6_address, &a);
2640 adj = ip_get_adjacency (lm, adj_index);
2642 if (adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
2643 return clib_error_return (0, "%U unresolved (not a rewrite adj)",
2644 format_ip6_address, &a);
2646 adj->rewrite_header.next_index = sm->ip6_rewrite_sr_next_index;
2648 sw_if_index = adj->rewrite_header.sw_if_index;
2649 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2650 adj->rewrite_header.node_index = sr_fix_dst_addr_node.index;
2652 /* $$$$$ hack... steal the mcast group index */
2653 adj->mcast_group_index =
2654 vlib_node_add_next (vm, sr_fix_dst_addr_node.index, hi->output_node_index);
2659 VLIB_CLI_COMMAND (set_ip6_sr_rewrite, static) = {
2660 .path = "set ip6 sr rewrite",
2661 .short_help = "set ip6 sr rewrite <ip6-address> [fib-id <id>]",
2662 .function = set_ip6_sr_rewrite_fn,
2665 void vnet_register_sr_app_callback (void *cb)
2667 ip6_sr_main_t * sm = &sr_main;
2669 sm->sr_local_cb = cb;
2672 static clib_error_t *
2673 test_sr_hmac_validate_fn (vlib_main_t * vm,
2674 unformat_input_t * input,
2675 vlib_cli_command_t * cmd)
2677 ip6_sr_main_t * sm = &sr_main;
2679 if (unformat (input, "validate on"))
2680 sm->validate_hmac = 1;
2681 else if (unformat (input, "chunk-offset off"))
2682 sm->validate_hmac = 0;
2684 return clib_error_return (0, "expected validate on|off in '%U'",
2685 format_unformat_error, input);
2687 vlib_cli_output (vm, "hmac signature validation %s",
2693 VLIB_CLI_COMMAND (test_sr_hmac_validate, static) = {
2694 .path = "test sr hmac",
2695 .short_help = "test sr hmac validate [on|off]",
2696 .function = test_sr_hmac_validate_fn,
2699 i32 sr_hmac_add_del_key (ip6_sr_main_t * sm, u32 key_id, u8 * shared_secret,
2703 ip6_sr_hmac_key_t * key;
2707 /* Specific key in use? Fail. */
2708 if (key_id && vec_len (sm->hmac_keys) > key_id
2709 && sm->hmac_keys[key_id].shared_secret)
2713 key = find_or_add_shared_secret (sm, shared_secret, &index);
2714 ASSERT(index == key_id);
2720 if (key_id) /* delete by key ID */
2722 if (vec_len (sm->hmac_keys) <= key_id)
2725 key = sm->hmac_keys + key_id;
2727 hash_unset_mem (sm->hmac_key_by_shared_secret, key->shared_secret);
2728 vec_free (key->shared_secret);
2733 key = find_or_add_shared_secret (sm, shared_secret, &index);
2734 hash_unset_mem (sm->hmac_key_by_shared_secret, key->shared_secret);
2735 vec_free (key->shared_secret);
2740 static clib_error_t *
2741 sr_hmac_add_del_key_fn (vlib_main_t * vm,
2742 unformat_input_t * input,
2743 vlib_cli_command_t * cmd)
2745 ip6_sr_main_t * sm = &sr_main;
2749 u8 * shared_secret = 0;
2752 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2754 if (unformat (input, "del"))
2756 else if (unformat (input, "id %d", &key_id))
2758 else if (unformat (input, "key %s", &shared_secret))
2760 /* Do not include the trailing NULL byte. Guaranteed interop issue */
2761 _vec_len (shared_secret) -= 1;
2767 if (is_del == 0 && shared_secret == 0)
2768 return clib_error_return (0, "shared secret must be set to add a key");
2770 if (shared_secret == 0 && key_id_set == 0)
2771 return clib_error_return (0, "shared secret and key id both unset");
2773 rv = sr_hmac_add_del_key (sm, key_id, shared_secret, is_del);
2775 vec_free (shared_secret);
2783 return clib_error_return (0, "sr_hmac_add_del_key returned %d",
2790 VLIB_CLI_COMMAND (sr_hmac, static) = {
2792 .short_help = "sr hmac [del] id <nn> key <str>",
2793 .function = sr_hmac_add_del_key_fn,
2797 static clib_error_t *
2798 show_sr_hmac_fn (vlib_main_t * vm,
2799 unformat_input_t * input,
2800 vlib_cli_command_t * cmd)
2802 ip6_sr_main_t * sm = &sr_main;
2805 for (i = 1; i < vec_len (sm->hmac_keys); i++)
2807 if (sm->hmac_keys[i].shared_secret)
2808 vlib_cli_output (vm, "[%d]: %v", i, sm->hmac_keys[i].shared_secret);
2814 VLIB_CLI_COMMAND (show_sr_hmac, static) = {
2815 .path = "show sr hmac",
2816 .short_help = "show sr hmac",
2817 .function = show_sr_hmac_fn,
2820 static clib_error_t *
2821 test_sr_debug_fn (vlib_main_t * vm,
2822 unformat_input_t * input,
2823 vlib_cli_command_t * cmd)
2825 ip6_sr_main_t * sm = &sr_main;
2827 if (unformat (input, "on"))
2829 else if (unformat (input, "off"))
2832 return clib_error_return (0, "expected on|off in '%U'",
2833 format_unformat_error, input);
2835 vlib_cli_output (vm, "debug trace now %s", sm->is_debug ? "on" : "off");
2840 VLIB_CLI_COMMAND (test_sr_debug, static) = {
2841 .path = "test sr debug",
2842 .short_help = "test sr debug on|off",
2843 .function = test_sr_debug_fn,