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 u32 next0 = SR_REWRITE_NEXT_IP6_LOOKUP;
310 u32 next1 = SR_REWRITE_NEXT_IP6_LOOKUP;
314 /* Prefetch next iteration. */
316 vlib_buffer_t * p2, * p3;
318 p2 = vlib_get_buffer (vm, from[2]);
319 p3 = vlib_get_buffer (vm, from[3]);
321 vlib_prefetch_buffer_header (p2, LOAD);
322 vlib_prefetch_buffer_header (p3, LOAD);
334 b0 = vlib_get_buffer (vm, bi0);
335 b1 = vlib_get_buffer (vm, bi1);
338 * $$$ parse through header(s) to pick the point
339 * where we punch in the SR extention header
342 adj0 = ip_get_adjacency (lm, vnet_buffer(b0)->ip.adj_index[VLIB_TX]);
343 adj1 = ip_get_adjacency (lm, vnet_buffer(b1)->ip.adj_index[VLIB_TX]);
344 t0 = pool_elt_at_index (sm->tunnels,
345 adj0->rewrite_header.sw_if_index);
346 t1 = pool_elt_at_index (sm->tunnels,
347 adj1->rewrite_header.sw_if_index);
349 ASSERT (VLIB_BUFFER_PRE_DATA_SIZE
350 >= ((word) vec_len (t0->rewrite)) + b0->current_data);
351 ASSERT (VLIB_BUFFER_PRE_DATA_SIZE
352 >= ((word) vec_len (t1->rewrite)) + b1->current_data);
354 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->tx_fib_index;
355 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->tx_fib_index;
357 ip0 = vlib_buffer_get_current (b0);
358 ip1 = vlib_buffer_get_current (b1);
361 * SR-unaware service chaining case: pkt coming back from
362 * service has the original dst address, and will already
363 * have an SR header. If so, send it to sr-local
365 if (PREDICT_FALSE(ip0->protocol == IPPROTO_IPV6_ROUTE))
367 vlib_buffer_advance (b0, sizeof(ip0));
368 sr0 = (ip6_sr_header_t *) (ip0+1);
369 new_l0 = clib_net_to_host_u16(ip0->payload_length);
370 next0 = SR_REWRITE_NEXT_SR_LOCAL;
375 * Copy data before the punch-in point left by the
376 * required amount. Assume (for the moment) that only
377 * the main packet header needs to be copied.
379 clib_memcpy (((u8 *)ip0) - vec_len (t0->rewrite),
380 ip0, sizeof (ip6_header_t));
381 vlib_buffer_advance (b0, - (word) vec_len(t0->rewrite));
382 ip0 = vlib_buffer_get_current (b0);
383 sr0 = (ip6_sr_header_t *) (ip0+1);
385 clib_memcpy (sr0, t0->rewrite, vec_len (t0->rewrite));
387 /* Fix the next header chain */
388 sr0->protocol = ip0->protocol;
389 ip0->protocol = IPPROTO_IPV6_ROUTE; /* routing extension header */
390 new_l0 = clib_net_to_host_u16(ip0->payload_length) +
391 vec_len (t0->rewrite);
392 ip0->payload_length = clib_host_to_net_u16(new_l0);
394 /* Copy dst address into the DA slot in the segment list */
395 clib_memcpy (sr0->segments, ip0->dst_address.as_u64,
396 sizeof (ip6_address_t));
397 /* Rewrite the ip6 dst address with the first hop */
398 clib_memcpy (ip0->dst_address.as_u64, t0->first_hop.as_u64,
399 sizeof (ip6_address_t));
401 sr_fix_hmac (sm, ip0, sr0);
403 next0 = sr_local_cb ? sr_local_cb (vm, node, b0, ip0, sr0) :
407 * Ignore "do not rewrite" shtik in this path
409 if (PREDICT_FALSE (next0 & 0x80000000))
412 if (PREDICT_FALSE(next0 == SR_REWRITE_NEXT_ERROR))
414 node->errors[SR_REWRITE_ERROR_APP_CALLBACK];
418 if (PREDICT_FALSE(ip1->protocol == IPPROTO_IPV6_ROUTE))
420 vlib_buffer_advance (b1, sizeof(ip1));
421 sr1 = (ip6_sr_header_t *) (ip1+1);
422 new_l1 = clib_net_to_host_u16(ip1->payload_length);
423 next1 = SR_REWRITE_NEXT_SR_LOCAL;
427 clib_memcpy (((u8 *)ip0) - vec_len (t0->rewrite),
428 ip0, sizeof (ip6_header_t));
429 vlib_buffer_advance (b1, - (word) vec_len(t1->rewrite));
430 ip1 = vlib_buffer_get_current (b1);
431 sr1 = (ip6_sr_header_t *) (ip1+1);
432 clib_memcpy (sr1, t1->rewrite, vec_len (t1->rewrite));
434 sr1->protocol = ip1->protocol;
435 ip1->protocol = IPPROTO_IPV6_ROUTE;
436 new_l1 = clib_net_to_host_u16(ip1->payload_length) +
437 vec_len (t1->rewrite);
438 ip1->payload_length = clib_host_to_net_u16(new_l1);
440 /* Copy dst address into the DA slot in the segment list */
441 clib_memcpy (sr1->segments, ip1->dst_address.as_u64,
442 sizeof (ip6_address_t));
443 /* Rewrite the ip6 dst address with the first hop */
444 clib_memcpy (ip1->dst_address.as_u64, t1->first_hop.as_u64,
445 sizeof (ip6_address_t));
447 sr_fix_hmac (sm, ip1, sr1);
449 next1 = sr_local_cb ? sr_local_cb (vm, node, b1, ip1, sr1) :
453 * Ignore "do not rewrite" shtik in this path
455 if (PREDICT_FALSE (next1 & 0x80000000))
458 if (PREDICT_FALSE(next1 == SR_REWRITE_NEXT_ERROR))
460 node->errors[SR_REWRITE_ERROR_APP_CALLBACK];
464 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
466 sr_rewrite_trace_t *tr = vlib_add_trace (vm, node,
468 tr->tunnel_index = t0 - sm->tunnels;
469 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
470 sizeof (tr->src.as_u8));
471 clib_memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
472 sizeof (tr->dst.as_u8));
474 tr->next_index = next0;
475 clib_memcpy (tr->sr, sr0, sizeof (tr->sr));
477 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
479 sr_rewrite_trace_t *tr = vlib_add_trace (vm, node,
481 tr->tunnel_index = t1 - sm->tunnels;
482 clib_memcpy (tr->src.as_u8, ip1->src_address.as_u8,
483 sizeof (tr->src.as_u8));
484 clib_memcpy (tr->dst.as_u8, ip1->dst_address.as_u8,
485 sizeof (tr->dst.as_u8));
487 tr->next_index = next1;
488 clib_memcpy (tr->sr, sr1, sizeof (tr->sr));
491 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
492 to_next, n_left_to_next,
493 bi0, bi1, next0, next1);
496 while (n_left_from > 0 && n_left_to_next > 0)
500 ip6_header_t * ip0 = 0;
501 ip_adjacency_t * adj0;
502 ip6_sr_header_t * sr0 = 0;
503 ip6_sr_tunnel_t * t0;
504 u32 next0 = SR_REWRITE_NEXT_IP6_LOOKUP;
514 b0 = vlib_get_buffer (vm, bi0);
517 * $$$ parse through header(s) to pick the point
518 * where we punch in the SR extention header
521 adj0 = ip_get_adjacency (lm, vnet_buffer(b0)->ip.adj_index[VLIB_TX]);
522 t0 = pool_elt_at_index (sm->tunnels,
523 adj0->rewrite_header.sw_if_index);
525 #if DPDK > 0 /* Cannot call replication node yet without DPDK */
526 /* add a replication node */
527 if(PREDICT_FALSE(t0->policy_index != ~0))
529 vnet_buffer(b0)->ip.save_protocol = t0->policy_index;
530 next0=SR_REWRITE_NEXT_SR_REPLICATE;
535 ASSERT (VLIB_BUFFER_PRE_DATA_SIZE
536 >= ((word) vec_len (t0->rewrite)) + b0->current_data);
538 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->tx_fib_index;
540 ip0 = vlib_buffer_get_current (b0);
543 * SR-unaware service chaining case: pkt coming back from
544 * service has the original dst address, and will already
545 * have an SR header. If so, send it to sr-local
547 if (PREDICT_FALSE(ip0->protocol == IPPROTO_IPV6_ROUTE))
549 vlib_buffer_advance (b0, sizeof(ip0));
550 sr0 = (ip6_sr_header_t *) (ip0+1);
551 new_l0 = clib_net_to_host_u16(ip0->payload_length);
552 next0 = SR_REWRITE_NEXT_SR_LOCAL;
557 * Copy data before the punch-in point left by the
558 * required amount. Assume (for the moment) that only
559 * the main packet header needs to be copied.
561 clib_memcpy (((u8 *)ip0) - vec_len (t0->rewrite),
562 ip0, sizeof (ip6_header_t));
563 vlib_buffer_advance (b0, - (word) vec_len(t0->rewrite));
564 ip0 = vlib_buffer_get_current (b0);
565 sr0 = (ip6_sr_header_t *) (ip0+1);
567 clib_memcpy (sr0, t0->rewrite, vec_len (t0->rewrite));
569 /* Fix the next header chain */
570 sr0->protocol = ip0->protocol;
571 ip0->protocol = IPPROTO_IPV6_ROUTE; /* routing extension header */
572 new_l0 = clib_net_to_host_u16(ip0->payload_length) +
573 vec_len (t0->rewrite);
574 ip0->payload_length = clib_host_to_net_u16(new_l0);
576 /* Copy dst address into the DA slot in the segment list */
577 clib_memcpy (sr0->segments, ip0->dst_address.as_u64,
578 sizeof (ip6_address_t));
579 /* Rewrite the ip6 dst address with the first hop */
580 clib_memcpy (ip0->dst_address.as_u64, t0->first_hop.as_u64,
581 sizeof (ip6_address_t));
583 sr_fix_hmac (sm, ip0, sr0);
585 next0 = sr_local_cb ? sr_local_cb (vm, node, b0, ip0, sr0) :
589 * Ignore "do not rewrite" shtik in this path
591 if (PREDICT_FALSE (next0 & 0x80000000))
594 if (PREDICT_FALSE(next0 == SR_REWRITE_NEXT_ERROR))
596 node->errors[SR_REWRITE_ERROR_APP_CALLBACK];
600 #if DPDK > 0 /* Cannot run replicate without DPDK and only replicate uses this label */
603 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
605 sr_rewrite_trace_t *tr = vlib_add_trace (vm, node,
607 tr->tunnel_index = t0 - sm->tunnels;
610 memcpy (tr->src.as_u8, ip0->src_address.as_u8,
611 sizeof (tr->src.as_u8));
612 memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
613 sizeof (tr->dst.as_u8));
616 tr->next_index = next0;
617 clib_memcpy (tr->sr, sr0, sizeof (tr->sr));
620 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
621 to_next, n_left_to_next,
625 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
627 return from_frame->n_vectors;
630 VLIB_REGISTER_NODE (sr_rewrite_node) = {
631 .function = sr_rewrite,
632 .name = "sr-rewrite",
633 /* Takes a vector of packets. */
634 .vector_size = sizeof (u32),
635 .format_trace = format_sr_rewrite_trace,
636 .format_buffer = format_ip6_sr_header_with_length,
638 .n_errors = SR_REWRITE_N_ERROR,
639 .error_strings = sr_rewrite_error_strings,
641 .runtime_data_bytes = 0,
643 .n_next_nodes = SR_REWRITE_N_NEXT,
645 #define _(s,n) [SR_REWRITE_NEXT_##s] = n,
646 foreach_sr_rewrite_next
651 VLIB_NODE_FUNCTION_MULTIARCH (sr_rewrite_node, sr_rewrite)
653 static int ip6_delete_route_no_next_hop (ip6_address_t *dst_address_arg,
654 u32 dst_address_length,
657 ip6_add_del_route_args_t a;
658 ip6_address_t dst_address;
660 ip6_main_t * im6 = &ip6_main;
661 BVT(clib_bihash_kv) kv, value;
663 fib = find_ip6_fib_by_table_index_or_id (im6, rx_table_id,
664 IP6_ROUTE_FLAG_TABLE_ID);
665 memset (&a, 0, sizeof (a));
666 a.flags |= IP4_ROUTE_FLAG_DEL;
667 a.dst_address_length = dst_address_length;
669 dst_address = *dst_address_arg;
671 ip6_address_mask (&dst_address,
672 &im6->fib_masks[dst_address_length]);
674 kv.key[0] = dst_address.as_u64[0];
675 kv.key[1] = dst_address.as_u64[1];
676 kv.key[2] = ((u64)((fib - im6->fibs))<<32) | dst_address_length;
678 if (BV(clib_bihash_search)(&im6->ip6_lookup_table, &kv, &value) < 0)
680 clib_warning ("%U/%d not in FIB",
681 format_ip6_address, &a.dst_address,
682 a.dst_address_length);
686 a.adj_index = value.value;
687 a.dst_address = dst_address;
689 ip6_add_del_route (im6, &a);
690 ip6_maybe_remap_adjacencies (im6, rx_table_id, IP6_ROUTE_FLAG_TABLE_ID);
694 static ip6_sr_hmac_key_t *
695 find_or_add_shared_secret (ip6_sr_main_t * sm, u8 * secret, u32 * indexp)
698 ip6_sr_hmac_key_t * key = 0;
701 p = hash_get_mem (sm->hmac_key_by_shared_secret, secret);
705 key = vec_elt_at_index (sm->hmac_keys, p[0]);
711 /* Specific key ID? */
712 if (indexp && *indexp)
714 vec_validate (sm->hmac_keys, *indexp);
715 key = sm->hmac_keys + *indexp;
719 for (i = 0; i < vec_len (sm->hmac_keys); i++)
721 if (sm->hmac_keys[i].shared_secret == 0)
722 key = sm->hmac_keys + i;
725 vec_validate (sm->hmac_keys, i);
726 key = sm->hmac_keys + i;
731 key->shared_secret = vec_dup (secret);
733 hash_set_mem (sm->hmac_key_by_shared_secret, key->shared_secret,
734 key - sm->hmac_keys);
737 *indexp = key - sm->hmac_keys;
742 int ip6_sr_add_del_tunnel (ip6_sr_add_del_tunnel_args_t * a)
744 ip6_main_t * im = &ip6_main;
745 ip_lookup_main_t * lm = &im->lookup_main;
746 ip6_sr_tunnel_key_t key;
749 ip6_sr_header_t * h = 0;
751 ip6_address_t * addrp, *this_address;
752 ip_adjacency_t adj, * ap, * add_adj = 0;
754 ip6_sr_main_t * sm = &sr_main;
756 u32 rx_fib_index, tx_fib_index;
757 ip6_add_del_route_args_t aa;
758 u32 hmac_key_index_u32;
759 u8 hmac_key_index = 0;
760 ip6_sr_policy_t * pt;
763 /* Make sure that the rx FIB exists */
764 p = hash_get (im->fib_index_by_table_id, a->rx_table_id);
769 /* remember the FIB index */
772 /* Make sure that the supplied FIB exists */
773 p = hash_get (im->fib_index_by_table_id, a->tx_table_id);
778 /* remember the FIB index */
781 clib_memcpy (key.src.as_u8, a->src_address->as_u8, sizeof (key.src));
782 clib_memcpy (key.dst.as_u8, a->dst_address->as_u8, sizeof (key.dst));
784 /* When adding a tunnel:
785 * - If a "name" is given, it must not exist.
786 * - The "key" is always checked, and must not exist.
787 * When deleting a tunnel:
788 * - If the "name" is given, and it exists, then use it.
789 * - If the "name" is not given, use the "key".
790 * - If the "name" and the "key" are given, then both must point to the same
795 p = hash_get_mem (sm->tunnel_index_by_key, &key);
797 /* If the name is given, look it up */
799 n = hash_get_mem(sm->tunnel_index_by_name, a->name);
803 /* validate key/name parameters */
804 if (!a->is_del) /* adding a tunnel */
806 if (a->name && n) /* name given & exists already */
808 if (p) /* key exists already */
811 else /* deleting a tunnel */
813 if (!p) /* key doesn't exist */
815 if (a->name && !n) /* name given & it doesn't exist */
818 if (n) /* name given & found */
820 if (n[0] != p[0]) /* name and key do not point to the same thing */
826 if (a->is_del) /* delete the tunnel */
830 /* Delete existing tunnel */
831 t = pool_elt_at_index (sm->tunnels, p[0]);
833 ip6_delete_route_no_next_hop (&t->key.dst, t->dst_mask_width,
835 vec_free (t->rewrite);
836 /* Remove tunnel from any policy if associated */
837 if (t->policy_index != ~0)
839 pt=pool_elt_at_index (sm->policies, t->policy_index);
840 for (i=0; i< vec_len (pt->tunnel_indices); i++)
842 if (pt->tunnel_indices[i] == t - sm->tunnels)
844 vec_delete (pt->tunnel_indices, 1, i);
848 clib_warning ("Tunnel index %d not found in policy_index %d",
849 t - sm->tunnels, pt - sm->policies);
851 /* If this is last tunnel in the policy, clean up the policy too */
852 if (vec_len (pt->tunnel_indices) == 0)
854 hash_unset_mem (sm->policy_index_by_policy_name, pt->name);
856 pool_put (sm->policies, pt);
860 /* Clean up the tunnel by name */
863 hash_unset_mem (sm->tunnel_index_by_name, t->name);
866 pool_put (sm->tunnels, t);
867 hp = hash_get_pair (sm->tunnel_index_by_key, &key);
868 key_copy = (void *)(hp->key);
869 hash_unset_mem (sm->tunnel_index_by_key, &key);
874 /* create a new tunnel */
875 pool_get (sm->tunnels, t);
876 memset (t, 0, sizeof (*t));
877 t->policy_index = ~0;
879 clib_memcpy (&t->key, &key, sizeof (t->key));
880 t->dst_mask_width = a->dst_mask_width;
881 t->rx_fib_index = rx_fib_index;
882 t->tx_fib_index = tx_fib_index;
884 if (!vec_len (a->segments))
885 /* there must be at least one segment... */
888 /* The first specified hop goes right into the dst address */
889 clib_memcpy(&t->first_hop, &a->segments[0], sizeof (ip6_address_t));
892 * Create the sr header rewrite string
893 * The list of segments needs an extra slot for the ultimate destination
894 * which is taken from the packet we add the SRH to.
896 header_length = sizeof (*h) +
897 sizeof (ip6_address_t) * (vec_len (a->segments) + 1 + vec_len (a->tags));
899 if (a->shared_secret)
901 /* Allocate a new key slot if we don't find the secret key */
902 hmac_key_index_u32 = 0;
903 (void) find_or_add_shared_secret (sm, a->shared_secret,
904 &hmac_key_index_u32);
906 /* Hey Vinz Clortho: Gozzer is pissed.. you're out of keys! */
907 if (hmac_key_index_u32 >= 256)
909 hmac_key_index = hmac_key_index_u32;
910 header_length += SHA256_DIGEST_LENGTH;
913 vec_validate (t->rewrite, header_length-1);
915 h = (ip6_sr_header_t *) t->rewrite;
917 h->protocol = 0xFF; /* we don't know yet */
919 h->length = (header_length/8) - 1;
920 h->type = ROUTING_HEADER_TYPE_SR;
922 /* first_segment and segments_left need to have the index of the last
923 * element in the list; a->segments has one element less than ends up
924 * in the header (it does not have the DA in it), so vec_len(a->segments)
925 * is the value we want.
927 h->first_segment = h->segments_left = vec_len (a->segments);
929 if (a->shared_secret)
930 h->hmac_key = hmac_key_index & 0xFF;
932 h->flags = a->flags_net_byte_order;
934 /* Paint on the segment list, in reverse.
935 * This is offset by one to leave room at the start for the ultimate
938 addrp = h->segments + vec_len (a->segments);
940 vec_foreach (this_address, a->segments)
942 clib_memcpy (addrp->as_u8, this_address->as_u8, sizeof (ip6_address_t));
947 * Since the ultimate destination address is not yet known, set that slot
948 * to a value we will instantly recognize as bogus.
950 memset (h->segments, 0xfe, sizeof (ip6_address_t));
952 /* Paint on the tag list, not reversed */
953 addrp = h->segments + vec_len(a->segments);
955 vec_foreach (this_address, a->tags)
957 clib_memcpy (addrp->as_u8, this_address->as_u8, sizeof (ip6_address_t));
961 key_copy = vec_new (ip6_sr_tunnel_key_t, 1);
962 clib_memcpy (key_copy, &key, sizeof (ip6_sr_tunnel_key_t));
963 hash_set_mem (sm->tunnel_index_by_key, key_copy, t - sm->tunnels);
965 memset(&adj, 0, sizeof (adj));
967 /* Create an adjacency and add to v6 fib */
968 adj.lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
969 adj.lookup_next_index = sm->ip6_lookup_sr_next_index;
970 adj.explicit_fib_index = ~0;
972 ap = ip_add_adjacency (lm, &adj, 1 /* one adj */,
976 * Stick the tunnel index into the rewrite header.
978 * Unfortunately, inserting an SR header according to the various
979 * RFC's requires parsing through the ip6 header, perhaps consing a
980 * buffer onto the head of the vlib_buffer_t, etc. We don't use the
981 * normal reverse bcopy rewrite code.
983 * We don't handle ugly RFC-related cases yet, but I'm sure PL will complain
986 ap->rewrite_header.sw_if_index = t - sm->tunnels;
988 vec_add1 (add_adj, ap[0]);
990 clib_memcpy (aa.dst_address.as_u8, a->dst_address, sizeof (aa.dst_address.as_u8));
991 aa.dst_address_length = a->dst_mask_width;
993 aa.flags = (a->is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD);
994 aa.flags |= IP6_ROUTE_FLAG_FIB_INDEX;
995 aa.table_index_or_table_id = rx_fib_index;
996 aa.add_adj = add_adj;
997 aa.adj_index = adj_index;
999 ip6_add_del_route (im, &aa);
1004 p=hash_get_mem (sm->policy_index_by_policy_name, a->policy_name);
1007 pt = pool_elt_at_index (sm->policies, p[0]);
1009 else /* no policy, lets create one */
1011 pool_get (sm->policies, pt);
1012 memset (pt, 0, sizeof(*pt));
1013 pt->name = format (0, "%s%c", a->policy_name, 0);
1014 hash_set_mem (sm->policy_index_by_policy_name, pt->name, pt - sm->policies);
1015 p=hash_get_mem (sm->policy_index_by_policy_name, a->policy_name);
1017 vec_add1 (pt->tunnel_indices, t - sm->tunnels);
1018 t->policy_index = p[0]; /* equiv. to (pt - sm->policies) */
1023 t->name = format (0, "%s%c", a->name, 0);
1024 hash_set_mem(sm->tunnel_index_by_name, t->name, t - sm->tunnels);
1030 static clib_error_t *
1031 sr_add_del_tunnel_command_fn (vlib_main_t * vm,
1032 unformat_input_t * input,
1033 vlib_cli_command_t * cmd)
1036 ip6_address_t src_address;
1037 int src_address_set = 0;
1038 ip6_address_t dst_address;
1040 int dst_address_set = 0;
1042 u8 *shared_secret = 0;
1044 u8 *policy_name = 0;
1045 u32 rx_table_id = 0;
1046 u32 tx_table_id = 0;
1047 ip6_address_t * segments = 0;
1048 ip6_address_t * this_seg;
1049 ip6_address_t * tags = 0;
1050 ip6_address_t * this_tag;
1051 ip6_sr_add_del_tunnel_args_t _a, *a=&_a;
1052 ip6_address_t next_address, tag;
1056 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1058 if (unformat (input, "del"))
1060 else if (unformat (input, "rx-fib-id %d", &rx_table_id))
1062 else if (unformat (input, "tx-fib-id %d", &tx_table_id))
1064 else if (unformat (input, "src %U", unformat_ip6_address, &src_address))
1065 src_address_set = 1;
1066 else if (unformat (input, "name %s", &name))
1068 else if (unformat (input, "policy %s", &policy_name))
1070 else if (unformat (input, "dst %U/%d",
1071 unformat_ip6_address, &dst_address,
1073 dst_address_set = 1;
1074 else if (unformat (input, "next %U", unformat_ip6_address,
1077 vec_add2 (segments, this_seg, 1);
1078 clib_memcpy (this_seg->as_u8, next_address.as_u8, sizeof (*this_seg));
1080 else if (unformat (input, "tag %U", unformat_ip6_address,
1083 vec_add2 (tags, this_tag, 1);
1084 clib_memcpy (this_tag->as_u8, tag.as_u8, sizeof (*this_tag));
1086 else if (unformat (input, "clean"))
1087 flags |= IP6_SR_HEADER_FLAG_CLEANUP;
1088 else if (unformat (input, "protected"))
1089 flags |= IP6_SR_HEADER_FLAG_PROTECTED;
1090 else if (unformat (input, "key %s", &shared_secret))
1091 /* Do not include the trailing NULL byte. Guaranteed interop issue */
1092 _vec_len (shared_secret) -= 1;
1093 else if (unformat (input, "InPE %d", &pl_index))
1095 if (pl_index <= 0 || pl_index > 4)
1097 pl_index_range_error:
1098 return clib_error_return
1099 (0, "Policy List Element Index %d out of range (1-4)", pl_index);
1102 flags |= IP6_SR_HEADER_FLAG_PL_ELT_INGRESS_PE
1103 << ip6_sr_policy_list_shift_from_index (pl_index);
1105 else if (unformat (input, "EgPE %d", &pl_index))
1107 if (pl_index <= 0 || pl_index > 4)
1108 goto pl_index_range_error;
1109 flags |= IP6_SR_HEADER_FLAG_PL_ELT_EGRESS_PE
1110 << ip6_sr_policy_list_shift_from_index (pl_index);
1112 else if (unformat (input, "OrgSrc %d", &pl_index))
1114 if (pl_index <= 0 || pl_index > 4)
1115 goto pl_index_range_error;
1116 flags |= IP6_SR_HEADER_FLAG_PL_ELT_ORIG_SRC_ADDR
1117 << ip6_sr_policy_list_shift_from_index (pl_index);
1123 if (!src_address_set)
1124 return clib_error_return (0, "src address required");
1126 if (!dst_address_set)
1127 return clib_error_return (0, "dst address required");
1130 return clib_error_return (0, "at least one sr segment required");
1132 memset (a, 0, sizeof (*a));
1133 a->src_address = &src_address;
1134 a->dst_address = &dst_address;
1135 a->dst_mask_width = dst_mask_width;
1136 a->segments = segments;
1138 a->flags_net_byte_order = clib_host_to_net_u16(flags);
1140 a->rx_table_id = rx_table_id;
1141 a->tx_table_id = tx_table_id;
1142 a->shared_secret = shared_secret;
1149 if (vec_len(policy_name))
1150 a->policy_name = policy_name;
1154 rv = ip6_sr_add_del_tunnel (a);
1156 vec_free (segments);
1158 vec_free (shared_secret);
1166 return clib_error_return (0, "SR tunnel src %U dst %U already exists",
1167 format_ip6_address, &src_address,
1168 format_ip6_address, &dst_address);
1171 return clib_error_return (0, "SR tunnel src %U dst %U does not exist",
1172 format_ip6_address, &src_address,
1173 format_ip6_address, &dst_address);
1176 return clib_error_return (0, "FIB table %d does not exist", rx_table_id);
1179 return clib_error_return (0, "At least one segment is required");
1182 return clib_error_return (0, "BUG: ip6_sr_add_del_tunnel returns %d",
1189 VLIB_CLI_COMMAND (sr_tunnel_command, static) = {
1190 .path = "sr tunnel",
1192 "sr tunnel [del] [name <name>] src <addr> dst <addr> [next <addr>] "
1193 "[clean] [reroute] [key <secret>] [policy <policy_name>]"
1194 "[rx-fib-id <fib_id>] [tx-fib-id <fib_id>]",
1195 .function = sr_add_del_tunnel_command_fn,
1199 ip6_sr_tunnel_display (vlib_main_t * vm,
1200 ip6_sr_tunnel_t * t)
1202 ip6_main_t * im = &ip6_main;
1203 ip6_sr_main_t * sm = &sr_main;
1204 ip6_fib_t * rx_fib, * tx_fib;
1205 ip6_sr_policy_t * pt;
1207 rx_fib = find_ip6_fib_by_table_index_or_id (im, t->rx_fib_index,
1208 IP6_ROUTE_FLAG_FIB_INDEX);
1210 tx_fib = find_ip6_fib_by_table_index_or_id (im, t->tx_fib_index,
1211 IP6_ROUTE_FLAG_FIB_INDEX);
1214 vlib_cli_output (vm,"sr tunnel name: %s", (char *)t->name);
1216 vlib_cli_output (vm, "src %U dst %U first hop %U",
1217 format_ip6_address, &t->key.src,
1218 format_ip6_address, &t->key.dst,
1219 format_ip6_address, &t->first_hop);
1220 vlib_cli_output (vm, " rx-fib-id %d tx-fib-id %d",
1221 rx_fib->table_id, tx_fib->table_id);
1222 vlib_cli_output (vm, " sr: %U", format_ip6_sr_header, t->rewrite,
1223 0 /* print_hmac */);
1225 if (t->policy_index != ~0)
1227 pt=pool_elt_at_index(sm->policies, t->policy_index);
1228 vlib_cli_output (vm,"sr policy: %s", (char *)pt->name);
1230 vlib_cli_output (vm, "-------");
1235 static clib_error_t *
1236 show_sr_tunnel_fn (vlib_main_t * vm,
1237 unformat_input_t * input,
1238 vlib_cli_command_t * cmd)
1240 static ip6_sr_tunnel_t ** tunnels;
1241 ip6_sr_tunnel_t * t;
1242 ip6_sr_main_t * sm = &sr_main;
1247 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1249 if (unformat (input, "name %s", &name))
1251 p=hash_get_mem (sm->tunnel_index_by_name, name);
1253 vlib_cli_output (vm, "No SR tunnel with name: %s. Showing all.", name);
1259 vec_reset_length (tunnels);
1261 if(!p) /* Either name parm not passed or no tunnel with that name found, show all */
1263 pool_foreach (t, sm->tunnels,
1265 vec_add1 (tunnels, t);
1268 else /* Just show the one tunnel by name */
1269 vec_add1 (tunnels, &sm->tunnels[p[0]]);
1271 if (vec_len (tunnels) == 0)
1272 vlib_cli_output (vm, "No SR tunnels configured");
1274 for (i = 0; i < vec_len (tunnels); i++)
1277 ip6_sr_tunnel_display (vm, t);
1283 VLIB_CLI_COMMAND (show_sr_tunnel_command, static) = {
1284 .path = "show sr tunnel",
1285 .short_help = "show sr tunnel [name <sr-tunnel-name>]",
1286 .function = show_sr_tunnel_fn,
1289 int ip6_sr_add_del_policy (ip6_sr_add_del_policy_args_t * a)
1291 ip6_sr_main_t * sm = &sr_main;
1293 ip6_sr_tunnel_t * t = 0;
1294 ip6_sr_policy_t * policy;
1295 u32 * tunnel_indices = 0;
1302 p=hash_get_mem (sm->policy_index_by_policy_name, a->name);
1304 return -6; /* policy name not found */
1306 policy = pool_elt_at_index(sm->policies, p[0]);
1308 vec_foreach_index (i, policy->tunnel_indices)
1310 t = pool_elt_at_index (sm->tunnels, policy->tunnel_indices[i]);
1311 t->policy_index = ~0;
1313 hash_unset_mem (sm->policy_index_by_policy_name, a->name);
1314 pool_put (sm->policies, policy);
1319 if (!vec_len(a->tunnel_names))
1320 return -3; /*tunnel name is required case */
1322 vec_reset_length (tunnel_indices);
1323 /* Check tunnel names, add tunnel_index to policy */
1324 for (i=0; i < vec_len (a->tunnel_names); i++)
1326 p = hash_get_mem (sm->tunnel_index_by_name, a->tunnel_names[i]);
1328 return -4; /* tunnel name not found case */
1330 t = pool_elt_at_index (sm->tunnels, p[0]);
1332 No need to check t==0. -3 condition above ensures name
1334 if (t->policy_index != ~0)
1335 return -5; /* tunnel name already associated with a policy */
1337 /* Add to tunnel indicies */
1338 vec_add1 (tunnel_indices, p[0]);
1341 /* Add policy to ip6_sr_main_t */
1342 pool_get (sm->policies, policy);
1343 policy->name = a->name;
1344 policy->tunnel_indices = tunnel_indices;
1345 hash_set_mem (sm->policy_index_by_policy_name, policy->name, policy - sm->policies);
1347 /* Yes, this could be construed as overkill but the last thing you should do is set
1348 the policy_index on the tunnel after everything is set in ip6_sr_main_t.
1349 If this is deemed overly cautious, could set this in the vec_len(tunnel_names) loop.
1351 for (i=0; i < vec_len(policy->tunnel_indices); i++)
1353 t = pool_elt_at_index (sm->tunnels, policy->tunnel_indices[i]);
1354 t->policy_index = policy - sm->policies;
1361 static clib_error_t *
1362 sr_add_del_policy_command_fn (vlib_main_t * vm,
1363 unformat_input_t * input,
1364 vlib_cli_command_t * cmd)
1367 u8 ** tunnel_names = 0;
1368 u8 * tunnel_name = 0;
1370 ip6_sr_add_del_policy_args_t _a, *a=&_a;
1373 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1375 if (unformat (input, "del"))
1377 else if (unformat (input, "name %s", &name))
1379 else if (unformat (input, "tunnel %s", &tunnel_name))
1383 vec_add1 (tunnel_names, tunnel_name);
1392 return clib_error_return (0, "name of SR policy required");
1395 memset(a, 0, sizeof(*a));
1399 a->tunnel_names = tunnel_names;
1401 rv = ip6_sr_add_del_policy (a);
1403 vec_free(tunnel_names);
1411 return clib_error_return (0, "tunnel name to associate to SR policy is required");
1414 return clib_error_return (0, "tunnel name not found");
1417 return clib_error_return (0, "tunnel already associated with policy");
1420 return clib_error_return (0, "policy name %s not found", name);
1423 return clib_error_return (0, "TODO: deleting policy name %s", name);
1426 return clib_error_return (0, "BUG: ip6_sr_add_del_policy returns %d", rv);
1432 VLIB_CLI_COMMAND (sr_policy_command, static) = {
1433 .path = "sr policy",
1435 "sr policy [del] name <policy-name> tunnel <sr-tunnel-name> [tunnel <sr-tunnel-name>]*",
1436 .function = sr_add_del_policy_command_fn,
1439 static clib_error_t *
1440 show_sr_policy_fn (vlib_main_t * vm,
1441 unformat_input_t * input,
1442 vlib_cli_command_t * cmd)
1444 static ip6_sr_policy_t ** policies;
1445 ip6_sr_policy_t * policy;
1446 ip6_sr_tunnel_t * t;
1447 ip6_sr_main_t * sm = &sr_main;
1452 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1454 if (unformat (input, "name %s", &name))
1456 p=hash_get_mem (sm->policy_index_by_policy_name, name);
1458 vlib_cli_output (vm, "policy with name %s not found. Showing all.", name);
1464 vec_reset_length (policies);
1466 if(!p) /* Either name parm not passed or no policy with that name found, show all */
1468 pool_foreach (policy, sm->policies,
1470 vec_add1 (policies, policy);
1473 else /* Just show the one policy by name and a summary of tunnel names */
1475 policy = pool_elt_at_index(sm->policies, p[0]);
1476 vec_add1 (policies, policy);
1479 if (vec_len (policies) == 0)
1480 vlib_cli_output (vm, "No SR policies configured");
1482 for (i = 0; i < vec_len (policies); i++)
1484 policy = policies [i];
1487 vlib_cli_output (vm,"SR policy name: %s", (char *)policy->name);
1488 for(j = 0; j < vec_len (policy->tunnel_indices); j++)
1490 t = pool_elt_at_index (sm->tunnels, policy->tunnel_indices[j]);
1491 ip6_sr_tunnel_display (vm, t);
1499 VLIB_CLI_COMMAND (show_sr_policy_command, static) = {
1500 .path = "show sr policy",
1501 .short_help = "show sr policy [name <sr-policy-name>]",
1502 .function = show_sr_policy_fn,
1505 int ip6_sr_add_del_multicastmap (ip6_sr_add_del_multicastmap_args_t * a)
1508 ip6_main_t * im = &ip6_main;
1509 ip_lookup_main_t * lm = &im->lookup_main;
1510 ip6_sr_tunnel_t * t;
1511 ip_adjacency_t adj, * ap, * add_adj = 0;
1513 ip6_sr_main_t * sm = &sr_main;
1514 ip6_add_del_route_args_t aa;
1515 ip6_sr_policy_t * pt;
1519 /* clean up the adjacency */
1520 p = hash_get_mem (sm->policy_index_by_multicast_address, a->multicast_address);
1524 /* Get our policy by policy_name */
1525 p = hash_get_mem (sm->policy_index_by_policy_name, a->policy_name);
1531 pt=pool_elt_at_index (sm->policies, p[0]);
1534 Get the first tunnel associated with policy populate the fib adjacency.
1535 From there, since this tunnel will have it's policy_index != ~0 it will
1536 be the trigger in the dual_loop to pull up the policy and make a copy-rewrite
1537 for each tunnel in the policy
1540 t = pool_elt_at_index (sm->tunnels, pt->tunnel_indices[0]);
1542 /* Construct a FIB entry for multicast using the rx/tx fib from the first tunnel */
1543 memset(&adj, 0, sizeof (adj));
1545 /* Create an adjacency and add to v6 fib */
1546 adj.lookup_next_index = sm->ip6_lookup_sr_replicate_index;
1547 adj.explicit_fib_index = ~0;
1549 ap = ip_add_adjacency (lm, &adj, 1 /* one adj */,
1553 * Stick the tunnel index into the rewrite header.
1555 * Unfortunately, inserting an SR header according to the various
1556 * RFC's requires parsing through the ip6 header, perhaps consing a
1557 * buffer onto the head of the vlib_buffer_t, etc. We don't use the
1558 * normal reverse bcopy rewrite code.
1560 * We don't handle ugly RFC-related cases yet, but I'm sure PL will complain
1563 ap->rewrite_header.sw_if_index = t - sm->tunnels;
1565 vec_add1 (add_adj, ap[0]);
1567 memcpy (aa.dst_address.as_u8, a->multicast_address, sizeof (aa.dst_address.as_u8));
1568 aa.dst_address_length = 128;
1570 aa.flags = (a->is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD);
1571 aa.flags |= IP6_ROUTE_FLAG_FIB_INDEX;
1572 aa.table_index_or_table_id = t->rx_fib_index;
1573 aa.add_adj = add_adj;
1574 aa.adj_index = adj_index;
1576 ip6_add_del_route (im, &aa);
1579 u8 * mcast_copy = 0;
1580 mcast_copy = vec_new (ip6_address_t, 1);
1581 memcpy (mcast_copy, a->multicast_address, sizeof (ip6_address_t));
1585 hash_unset_mem (sm->policy_index_by_multicast_address, mcast_copy);
1586 vec_free (mcast_copy);
1591 hash_set_mem (sm->policy_index_by_multicast_address, mcast_copy, pt - sm->policies);
1597 static clib_error_t *
1598 sr_add_del_multicast_map_command_fn (vlib_main_t * vm,
1599 unformat_input_t * input,
1600 vlib_cli_command_t * cmd)
1603 ip6_address_t multicast_address;
1604 u8 * policy_name = 0;
1605 int multicast_address_set = 0;
1606 ip6_sr_add_del_multicastmap_args_t _a, *a=&_a;
1609 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1611 if (unformat (input, "del"))
1613 else if (unformat (input, "address %U", unformat_ip6_address, &multicast_address))
1614 multicast_address_set = 1;
1615 else if (unformat (input, "sr-policy %s", &policy_name))
1621 if (!is_del && !policy_name)
1622 return clib_error_return (0, "name of sr policy required");
1624 if (!multicast_address_set)
1625 return clib_error_return (0, "multicast address required");
1627 memset(a, 0, sizeof(*a));
1630 a->multicast_address = &multicast_address;
1631 a->policy_name = policy_name;
1633 #if DPDK > 0 /*Cannot call replicate or configure multicast map yet without DPDK */
1634 rv = ip6_sr_add_del_multicastmap (a);
1636 return clib_error_return (0, "cannot use multicast replicate spray case without DPDK installed");
1644 return clib_error_return (0, "no policy with name: %s", policy_name);
1647 return clib_error_return (0, "multicast map someting ");
1650 return clib_error_return (0, "tunnel name to associate to SR policy is required");
1653 return clib_error_return (0, "TODO: deleting policy name %s", policy_name);
1656 return clib_error_return (0, "BUG: ip6_sr_add_del_policy returns %d", rv);
1664 VLIB_CLI_COMMAND (sr_multicast_map_command, static) = {
1665 .path = "sr multicast-map",
1667 "sr multicast-map address <multicast-ip6-address> sr-policy <sr-policy-name> [del]",
1668 .function = sr_add_del_multicast_map_command_fn,
1671 static clib_error_t *
1672 show_sr_multicast_map_fn (vlib_main_t * vm,
1673 unformat_input_t * input,
1674 vlib_cli_command_t * cmd)
1676 ip6_sr_main_t * sm = &sr_main;
1679 ip6_address_t multicast_address;
1680 ip6_sr_policy_t * pt ;
1682 /* pull all entries from the hash table into vector for display */
1684 hash_foreach_mem (key, value, sm->policy_index_by_multicast_address,
1687 vlib_cli_output (vm, "no multicast maps configured");
1690 multicast_address = *((ip6_address_t *)key);
1691 pt = pool_elt_at_index (sm->policies, value);
1694 vlib_cli_output (vm, "address: %U policy: %s",
1695 format_ip6_address, &multicast_address,
1699 vlib_cli_output (vm, "BUG: policy not found for address: %U with policy index %d",
1700 format_ip6_address, &multicast_address,
1710 VLIB_CLI_COMMAND (show_sr_multicast_map_command, static) = {
1711 .path = "show sr multicast-map",
1712 .short_help = "show sr multicast-map",
1713 .function = show_sr_multicast_map_fn,
1717 #define foreach_sr_fix_dst_addr_next \
1718 _(DROP, "error-drop")
1721 #define _(s,n) SR_FIX_DST_ADDR_NEXT_##s,
1722 foreach_sr_fix_dst_addr_next
1724 SR_FIX_DST_ADDR_N_NEXT,
1725 } sr_fix_dst_addr_next_t;
1727 static char * sr_fix_dst_error_strings[] = {
1728 #define sr_fix_dst_error(n,s) s,
1729 #include "sr_fix_dst_error.def"
1730 #undef sr_fix_dst_error
1734 #define sr_fix_dst_error(n,s) SR_FIX_DST_ERROR_##n,
1735 #include "sr_fix_dst_error.def"
1736 #undef sr_fix_dst_error
1738 } sr_fix_dst_error_t;
1741 ip6_address_t src, dst;
1745 } sr_fix_addr_trace_t;
1747 u8 * format_sr_fix_addr_trace (u8 * s, va_list * args)
1749 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1750 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1751 sr_fix_addr_trace_t * t = va_arg (*args, sr_fix_addr_trace_t *);
1752 vnet_hw_interface_t * hi = 0;
1753 ip_adjacency_t * adj;
1754 ip6_main_t * im = &ip6_main;
1755 ip_lookup_main_t * lm = &im->lookup_main;
1756 vnet_main_t * vnm = vnet_get_main();
1758 if (t->adj_index != ~0)
1760 adj = ip_get_adjacency (lm, t->adj_index);
1761 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
1764 s = format (s, "SR-FIX_ADDR: next %s ip6 src %U dst %U\n",
1765 (t->next_index == SR_FIX_DST_ADDR_NEXT_DROP)
1766 ? "drop" : "output",
1767 format_ip6_address, &t->src,
1768 format_ip6_address, &t->dst);
1769 if (t->next_index != SR_FIX_DST_ADDR_NEXT_DROP)
1771 s = format (s, "%U\n", format_ip6_sr_header, t->sr, 1 /* print_hmac */);
1772 s = format (s, " output via %s", hi ? (char *)(hi->name)
1779 sr_fix_dst_addr (vlib_main_t * vm,
1780 vlib_node_runtime_t * node,
1781 vlib_frame_t * from_frame)
1783 u32 n_left_from, next_index, * from, * to_next;
1784 ip6_main_t * im = &ip6_main;
1785 ip_lookup_main_t * lm = &im->lookup_main;
1787 from = vlib_frame_vector_args (from_frame);
1788 n_left_from = from_frame->n_vectors;
1790 next_index = node->cached_next_index;
1792 while (n_left_from > 0)
1796 vlib_get_next_frame (vm, node, next_index,
1797 to_next, n_left_to_next);
1800 while (0 && n_left_from >= 4 && n_left_to_next >= 2)
1803 __attribute__((unused)) vlib_buffer_t * b0, * b1;
1804 u32 next0 = SR_FIX_DST_ADDR_NEXT_DROP;
1805 u32 next1 = SR_FIX_DST_ADDR_NEXT_DROP;
1807 /* Prefetch next iteration. */
1809 vlib_buffer_t * p2, * p3;
1811 p2 = vlib_get_buffer (vm, from[2]);
1812 p3 = vlib_get_buffer (vm, from[3]);
1814 vlib_prefetch_buffer_header (p2, LOAD);
1815 vlib_prefetch_buffer_header (p3, LOAD);
1824 n_left_to_next -= 2;
1827 b0 = vlib_get_buffer (vm, bi0);
1828 b1 = vlib_get_buffer (vm, bi1);
1831 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1832 to_next, n_left_to_next,
1833 bi0, bi1, next0, next1);
1837 while (n_left_from > 0 && n_left_to_next > 0)
1842 ip_adjacency_t * adj0;
1843 ip6_sr_header_t * sr0;
1844 u32 next0 = SR_FIX_DST_ADDR_NEXT_DROP;
1845 ip6_address_t *new_dst0;
1846 ethernet_header_t * eh0;
1853 n_left_to_next -= 1;
1855 b0 = vlib_get_buffer (vm, bi0);
1857 adj0 = ip_get_adjacency (lm, vnet_buffer(b0)->ip.adj_index[VLIB_TX]);
1858 next0 = adj0->mcast_group_index;
1860 /* We should be pointing at an Ethernet header... */
1861 eh0 = vlib_buffer_get_current (b0);
1862 ip0 = (ip6_header_t *)(eh0+1);
1863 sr0 = (ip6_sr_header_t *) (ip0+1);
1865 /* We'd better find an SR header... */
1866 if (PREDICT_FALSE(ip0->protocol != IPPROTO_IPV6_ROUTE))
1868 b0->error = node->errors[SR_FIX_DST_ERROR_NO_SR_HEADER];
1874 * We get here from sr_rewrite or sr_local, with
1875 * sr->segments_left pointing at the (copy of the original) dst
1876 * address. Use it, then increment sr0->segments_left.
1879 /* Out of segments? Turf the packet */
1880 if (PREDICT_FALSE (sr0->segments_left == 0))
1882 b0->error = node->errors[SR_FIX_DST_ERROR_NO_MORE_SEGMENTS];
1887 * Rewrite the packet with the original dst address
1888 * We assume that the last segment (in processing order) contains
1889 * the original dst address. The list is reversed, so sr0->segments
1890 * contains the original dst address.
1892 new_dst0 = sr0->segments;
1893 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
1894 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
1899 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
1901 sr_fix_addr_trace_t *t = vlib_add_trace (vm, node,
1903 t->next_index = next0;
1906 if (next0 != SR_FIX_DST_ADDR_NEXT_DROP)
1908 t->adj_index = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
1909 clib_memcpy (t->src.as_u8, ip0->src_address.as_u8,
1910 sizeof (t->src.as_u8));
1911 clib_memcpy (t->dst.as_u8, ip0->dst_address.as_u8,
1912 sizeof (t->dst.as_u8));
1913 clib_memcpy (t->sr, sr0, sizeof (t->sr));
1917 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1918 to_next, n_left_to_next,
1922 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1924 return from_frame->n_vectors;
1928 VLIB_REGISTER_NODE (sr_fix_dst_addr_node) = {
1929 .function = sr_fix_dst_addr,
1930 .name = "sr-fix-dst-addr",
1931 /* Takes a vector of packets. */
1932 .vector_size = sizeof (u32),
1933 .format_trace = format_sr_fix_addr_trace,
1934 .format_buffer = format_ip6_sr_header_with_length,
1936 .runtime_data_bytes = 0,
1938 .n_errors = SR_FIX_DST_N_ERROR,
1939 .error_strings = sr_fix_dst_error_strings,
1941 .n_next_nodes = SR_FIX_DST_ADDR_N_NEXT,
1943 #define _(s,n) [SR_FIX_DST_ADDR_NEXT_##s] = n,
1944 foreach_sr_fix_dst_addr_next
1949 VLIB_NODE_FUNCTION_MULTIARCH (sr_fix_dst_addr_node, sr_fix_dst_addr)
1951 static clib_error_t * sr_init (vlib_main_t * vm)
1953 ip6_sr_main_t * sm = &sr_main;
1954 clib_error_t * error = 0;
1955 vlib_node_t * ip6_lookup_node, * ip6_rewrite_node;
1957 if ((error = vlib_call_init_function (vm, ip_main_init)))
1960 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
1964 sm->vnet_main = vnet_get_main();
1966 vec_validate (sm->hmac_keys, 0);
1967 sm->hmac_keys[0].shared_secret = (u8 *) 0xdeadbeef;
1969 sm->tunnel_index_by_key =
1970 hash_create_mem (0, sizeof (ip6_sr_tunnel_key_t), sizeof (uword));
1972 sm->tunnel_index_by_name =
1973 hash_create_string (0, sizeof (uword));
1975 sm->policy_index_by_policy_name =
1976 hash_create_string(0, sizeof (uword));
1978 sm->policy_index_by_multicast_address =
1979 hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword));
1981 sm->hmac_key_by_shared_secret = hash_create_string (0, sizeof(uword));
1983 ip6_register_protocol (IPPROTO_IPV6_ROUTE, sr_local_node.index);
1985 ip6_lookup_node = vlib_get_node_by_name (vm, (u8 *)"ip6-lookup");
1986 ASSERT(ip6_lookup_node);
1988 ip6_rewrite_node = vlib_get_node_by_name (vm, (u8 *)"ip6-rewrite");
1989 ASSERT(ip6_rewrite_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 OpenSSL_add_all_digests();
2008 sm->md = (void *) EVP_get_digestbyname ("sha1");
2009 sm->hmac_ctx = clib_mem_alloc (sizeof (HMAC_CTX));
2014 VLIB_INIT_FUNCTION (sr_init);
2016 #define foreach_sr_local_next \
2017 _ (ERROR, "error-drop") \
2018 _ (IP6_LOOKUP, "ip6-lookup")
2021 #define _(s,n) SR_LOCAL_NEXT_##s,
2022 foreach_sr_local_next
2030 ip6_address_t src, dst;
2035 static char * sr_local_error_strings[] = {
2036 #define sr_error(n,s) s,
2037 #include "sr_error.def"
2042 #define sr_error(n,s) SR_LOCAL_ERROR_##n,
2043 #include "sr_error.def"
2048 u8 * format_sr_local_trace (u8 * s, va_list * args)
2050 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2051 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2052 sr_local_trace_t * t = va_arg (*args, sr_local_trace_t *);
2054 s = format (s, "SR-LOCAL: src %U dst %U len %u next_index %d",
2055 format_ip6_address, &t->src,
2056 format_ip6_address, &t->dst, t->length, t->next_index);
2058 s = format (s, "\n %U", format_ip6_sr_header, t->sr, 1 /* print_hmac */);
2060 s = format (s, "\n popped SR header");
2066 /* $$$$ fixme: smp, don't copy data, cache input, output (maybe) */
2068 static int sr_validate_hmac (ip6_sr_main_t * sm, ip6_header_t * ip,
2069 ip6_sr_header_t * sr)
2075 ip6_address_t *addrp;
2077 ip6_sr_hmac_key_t * hmac_key;
2078 static u8 * signature;
2081 key_index = sr->hmac_key;
2083 /* No signature? Pass... */
2087 /* We don't know about this key? Fail... */
2088 if (key_index >= vec_len (sm->hmac_keys))
2091 vec_validate (signature, SHA256_DIGEST_LENGTH-1);
2093 hmac_key = sm->hmac_keys + key_index;
2095 vec_reset_length (keybuf);
2097 /* pkt ip6 src address */
2098 vec_add2 (keybuf, copy_target, sizeof (ip6_address_t));
2099 clib_memcpy (copy_target, ip->src_address.as_u8, sizeof (ip6_address_t));
2102 vec_add2 (keybuf, copy_target, 1);
2103 copy_target[0] = sr->first_segment;
2105 /* octet w/ bit 0 = "clean" flag */
2106 vec_add2 (keybuf, copy_target, 1);
2108 = (sr->flags & clib_host_to_net_u16 (IP6_SR_HEADER_FLAG_CLEANUP))
2112 vec_add2 (keybuf, copy_target, 1);
2113 copy_target[0] = sr->hmac_key;
2115 first_segment = sr->first_segment;
2117 addrp = sr->segments;
2120 for (i = 0; i <= first_segment; i++)
2122 vec_add2 (keybuf, copy_target, sizeof (ip6_address_t));
2123 clib_memcpy (copy_target, addrp->as_u8, sizeof (ip6_address_t));
2128 clib_warning ("verify key index %d keybuf: %U", key_index,
2129 format_hex_bytes, keybuf, vec_len(keybuf));
2133 /* SHA1 is shorter than SHA-256 */
2134 memset (signature, 0, vec_len(signature));
2136 HMAC_CTX_init(sm->hmac_ctx);
2137 if (!HMAC_Init(sm->hmac_ctx, hmac_key->shared_secret,
2138 vec_len(hmac_key->shared_secret),sm->md))
2139 clib_warning ("barf1");
2140 if (!HMAC_Update(sm->hmac_ctx,keybuf,vec_len(keybuf)))
2141 clib_warning ("barf2");
2142 if (!HMAC_Final(sm->hmac_ctx,signature,&sig_len))
2143 clib_warning ("barf3");
2144 HMAC_CTX_cleanup(sm->hmac_ctx);
2147 clib_warning ("computed signature len %d, value %U", sig_len,
2148 format_hex_bytes, signature, vec_len(signature));
2150 /* Point at the SHA signature in the packet */
2153 clib_warning ("read signature %U", format_hex_bytes, addrp,
2154 SHA256_DIGEST_LENGTH);
2156 return memcmp (signature, addrp, SHA256_DIGEST_LENGTH);
2160 sr_local (vlib_main_t * vm,
2161 vlib_node_runtime_t * node,
2162 vlib_frame_t * from_frame)
2164 u32 n_left_from, next_index, * from, * to_next;
2165 ip6_sr_main_t * sm = &sr_main;
2166 u32 (*sr_local_cb) (vlib_main_t *, vlib_node_runtime_t *,
2167 vlib_buffer_t *, ip6_header_t *,
2169 sr_local_cb = sm->sr_local_cb;
2171 from = vlib_frame_vector_args (from_frame);
2172 n_left_from = from_frame->n_vectors;
2174 next_index = node->cached_next_index;
2176 while (n_left_from > 0)
2180 vlib_get_next_frame (vm, node, next_index,
2181 to_next, n_left_to_next);
2183 while (n_left_from >= 4 && n_left_to_next >= 2)
2186 vlib_buffer_t * b0, * b1;
2187 ip6_header_t * ip0, *ip1;
2188 ip6_sr_header_t * sr0, *sr1;
2189 ip6_address_t * new_dst0, * new_dst1;
2190 u32 next0 = SR_LOCAL_NEXT_IP6_LOOKUP;
2191 u32 next1 = SR_LOCAL_NEXT_IP6_LOOKUP;
2192 /* Prefetch next iteration. */
2194 vlib_buffer_t * p2, * p3;
2196 p2 = vlib_get_buffer (vm, from[2]);
2197 p3 = vlib_get_buffer (vm, from[3]);
2199 vlib_prefetch_buffer_header (p2, LOAD);
2200 vlib_prefetch_buffer_header (p3, LOAD);
2202 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
2203 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
2212 n_left_to_next -= 2;
2216 b0 = vlib_get_buffer (vm, bi0);
2217 ip0 = vlib_buffer_get_current (b0);
2218 sr0 = (ip6_sr_header_t *)(ip0+1);
2220 if (PREDICT_FALSE(sr0->type != ROUTING_HEADER_TYPE_SR))
2222 next0 = SR_LOCAL_NEXT_ERROR;
2223 b0->error = node->errors[SR_LOCAL_ERROR_BAD_ROUTING_HEADER_TYPE];
2227 /* Out of segments? Turf the packet */
2228 if (PREDICT_FALSE (sr0->segments_left == 0))
2230 next0 = SR_LOCAL_NEXT_ERROR;
2231 b0->error = node->errors[SR_LOCAL_ERROR_NO_MORE_SEGMENTS];
2235 if (PREDICT_FALSE(sm->validate_hmac))
2237 if (sr_validate_hmac (sm, ip0, sr0))
2239 next0 = SR_LOCAL_NEXT_ERROR;
2240 b0->error = node->errors[SR_LOCAL_ERROR_HMAC_INVALID];
2245 next0 = sr_local_cb ? sr_local_cb (vm, node, b0, ip0, sr0) :
2249 * To suppress rewrite, return ~SR_LOCAL_NEXT_xxx
2251 if (PREDICT_FALSE (next0 & 0x80000000))
2253 next0 ^= 0xFFFFFFFF;
2254 if (PREDICT_FALSE(next0 == SR_LOCAL_NEXT_ERROR))
2256 node->errors[SR_LOCAL_ERROR_APP_CALLBACK];
2262 segment_index0 = sr0->segments_left - 1;
2264 /* Rewrite the packet */
2265 new_dst0 = (ip6_address_t *)(sr0->segments + segment_index0);
2266 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
2267 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
2269 if (PREDICT_TRUE (sr0->segments_left > 0))
2270 sr0->segments_left -= 1;
2273 /* End of the path. Clean up the SR header, or not */
2275 (sr0->segments_left == 0 &&
2276 (sr0->flags & clib_host_to_net_u16(IP6_SR_HEADER_FLAG_CLEANUP))))
2278 u64 *copy_dst0, *copy_src0;
2281 * Copy the ip6 header right by the (real) length of the
2282 * sr header. Here's another place which assumes that
2283 * the sr header is the only extention header.
2286 ip0->protocol = sr0->protocol;
2287 vlib_buffer_advance (b0, (sr0->length+1)*8);
2289 new_l0 = clib_net_to_host_u16(ip0->payload_length) -
2291 ip0->payload_length = clib_host_to_net_u16(new_l0);
2293 copy_src0 = (u64 *)ip0;
2294 copy_dst0 = copy_src0 + (sr0->length + 1);
2296 copy_dst0 [4] = copy_src0[4];
2297 copy_dst0 [3] = copy_src0[3];
2298 copy_dst0 [2] = copy_src0[2];
2299 copy_dst0 [1] = copy_src0[1];
2300 copy_dst0 [0] = copy_src0[0];
2306 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
2308 sr_local_trace_t *tr = vlib_add_trace (vm, node,
2310 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
2311 sizeof (tr->src.as_u8));
2312 clib_memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
2313 sizeof (tr->dst.as_u8));
2314 tr->length = vlib_buffer_length_in_chain (vm, b0);
2315 tr->next_index = next0;
2316 tr->sr_valid = sr0 != 0;
2318 clib_memcpy (tr->sr, sr0, sizeof (tr->sr));
2321 b1 = vlib_get_buffer (vm, bi1);
2322 ip1 = vlib_buffer_get_current (b1);
2323 sr1 = (ip6_sr_header_t *)(ip1+1);
2325 if (PREDICT_FALSE(sr1->type != ROUTING_HEADER_TYPE_SR))
2327 next1 = SR_LOCAL_NEXT_ERROR;
2328 b1->error = node->errors[SR_LOCAL_ERROR_BAD_ROUTING_HEADER_TYPE];
2332 /* Out of segments? Turf the packet */
2333 if (PREDICT_FALSE (sr1->segments_left == 0))
2335 next1 = SR_LOCAL_NEXT_ERROR;
2336 b1->error = node->errors[SR_LOCAL_ERROR_NO_MORE_SEGMENTS];
2340 if (PREDICT_FALSE(sm->validate_hmac))
2342 if (sr_validate_hmac (sm, ip1, sr1))
2344 next1 = SR_LOCAL_NEXT_ERROR;
2345 b1->error = node->errors[SR_LOCAL_ERROR_HMAC_INVALID];
2350 next1 = sr_local_cb ? sr_local_cb (vm, node, b1, ip1, sr1) :
2354 * To suppress rewrite, return ~SR_LOCAL_NEXT_xxx
2356 if (PREDICT_FALSE (next1 & 0x80000000))
2358 next1 ^= 0xFFFFFFFF;
2359 if (PREDICT_FALSE(next1 == SR_LOCAL_NEXT_ERROR))
2361 node->errors[SR_LOCAL_ERROR_APP_CALLBACK];
2367 segment_index1 = sr1->segments_left - 1;
2369 /* Rewrite the packet */
2370 new_dst1 = (ip6_address_t *)(sr1->segments + segment_index1);
2371 ip1->dst_address.as_u64[0] = new_dst1->as_u64[0];
2372 ip1->dst_address.as_u64[1] = new_dst1->as_u64[1];
2374 if (PREDICT_TRUE (sr1->segments_left > 0))
2375 sr1->segments_left -= 1;
2378 /* End of the path. Clean up the SR header, or not */
2380 (sr1->segments_left == 0 &&
2381 (sr1->flags & clib_host_to_net_u16(IP6_SR_HEADER_FLAG_CLEANUP))))
2383 u64 *copy_dst1, *copy_src1;
2386 * Copy the ip6 header right by the (real) length of the
2387 * sr header. Here's another place which assumes that
2388 * the sr header is the only extention header.
2391 ip1->protocol = sr1->protocol;
2392 vlib_buffer_advance (b1, (sr1->length+1)*8);
2394 new_l1 = clib_net_to_host_u16(ip1->payload_length) -
2396 ip1->payload_length = clib_host_to_net_u16(new_l1);
2398 copy_src1 = (u64 *)ip1;
2399 copy_dst1 = copy_src1 + (sr1->length + 1);
2401 copy_dst1 [4] = copy_src1[4];
2402 copy_dst1 [3] = copy_src1[3];
2403 copy_dst1 [2] = copy_src1[2];
2404 copy_dst1 [1] = copy_src1[1];
2405 copy_dst1 [0] = copy_src1[0];
2411 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
2413 sr_local_trace_t *tr = vlib_add_trace (vm, node,
2415 clib_memcpy (tr->src.as_u8, ip1->src_address.as_u8,
2416 sizeof (tr->src.as_u8));
2417 clib_memcpy (tr->dst.as_u8, ip1->dst_address.as_u8,
2418 sizeof (tr->dst.as_u8));
2419 tr->length = vlib_buffer_length_in_chain (vm, b1);
2420 tr->next_index = next1;
2421 tr->sr_valid = sr1 != 0;
2423 clib_memcpy (tr->sr, sr1, sizeof (tr->sr));
2426 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2427 to_next, n_left_to_next,
2428 bi0, bi1, next0, next1);
2431 while (n_left_from > 0 && n_left_to_next > 0)
2435 ip6_header_t * ip0 = 0;
2436 ip6_sr_header_t * sr0;
2437 ip6_address_t * new_dst0;
2438 u32 next0 = SR_LOCAL_NEXT_IP6_LOOKUP;
2445 n_left_to_next -= 1;
2447 b0 = vlib_get_buffer (vm, bi0);
2448 ip0 = vlib_buffer_get_current (b0);
2449 sr0 = (ip6_sr_header_t *)(ip0+1);
2451 if (PREDICT_FALSE(sr0->type != ROUTING_HEADER_TYPE_SR))
2453 next0 = SR_LOCAL_NEXT_ERROR;
2454 b0->error = node->errors[SR_LOCAL_ERROR_BAD_ROUTING_HEADER_TYPE];
2458 /* Out of segments? Turf the packet */
2459 if (PREDICT_FALSE (sr0->segments_left == 0))
2461 next0 = SR_LOCAL_NEXT_ERROR;
2462 b0->error = node->errors[SR_LOCAL_ERROR_NO_MORE_SEGMENTS];
2466 if (PREDICT_FALSE(sm->validate_hmac))
2468 if (sr_validate_hmac (sm, ip0, sr0))
2470 next0 = SR_LOCAL_NEXT_ERROR;
2471 b0->error = node->errors[SR_LOCAL_ERROR_HMAC_INVALID];
2476 next0 = sr_local_cb ? sr_local_cb (vm, node, b0, ip0, sr0) :
2480 * To suppress rewrite, return ~SR_LOCAL_NEXT_xxx
2482 if (PREDICT_FALSE (next0 & 0x80000000))
2484 next0 ^= 0xFFFFFFFF;
2485 if (PREDICT_FALSE(next0 == SR_LOCAL_NEXT_ERROR))
2487 node->errors[SR_LOCAL_ERROR_APP_CALLBACK];
2493 segment_index0 = sr0->segments_left - 1;
2495 /* Rewrite the packet */
2496 new_dst0 = (ip6_address_t *)(sr0->segments + segment_index0);
2497 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
2498 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
2500 if (PREDICT_TRUE (sr0->segments_left > 0))
2501 sr0->segments_left -= 1;
2504 /* End of the path. Clean up the SR header, or not */
2506 (sr0->segments_left == 0 &&
2507 (sr0->flags & clib_host_to_net_u16(IP6_SR_HEADER_FLAG_CLEANUP))))
2509 u64 *copy_dst0, *copy_src0;
2512 * Copy the ip6 header right by the (real) length of the
2513 * sr header. Here's another place which assumes that
2514 * the sr header is the only extention header.
2517 ip0->protocol = sr0->protocol;
2518 vlib_buffer_advance (b0, (sr0->length+1)*8);
2520 new_l0 = clib_net_to_host_u16(ip0->payload_length) -
2522 ip0->payload_length = clib_host_to_net_u16(new_l0);
2524 copy_src0 = (u64 *)ip0;
2525 copy_dst0 = copy_src0 + (sr0->length + 1);
2527 copy_dst0 [4] = copy_src0[4];
2528 copy_dst0 [3] = copy_src0[3];
2529 copy_dst0 [2] = copy_src0[2];
2530 copy_dst0 [1] = copy_src0[1];
2531 copy_dst0 [0] = copy_src0[0];
2537 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
2539 sr_local_trace_t *tr = vlib_add_trace (vm, node,
2541 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
2542 sizeof (tr->src.as_u8));
2543 clib_memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
2544 sizeof (tr->dst.as_u8));
2545 tr->length = vlib_buffer_length_in_chain (vm, b0);
2546 tr->next_index = next0;
2547 tr->sr_valid = sr0 != 0;
2549 clib_memcpy (tr->sr, sr0, sizeof (tr->sr));
2552 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2553 to_next, n_left_to_next,
2557 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2559 vlib_node_increment_counter (vm, sr_local_node.index,
2560 SR_LOCAL_ERROR_PKTS_PROCESSED,
2561 from_frame->n_vectors);
2562 return from_frame->n_vectors;
2565 VLIB_REGISTER_NODE (sr_local_node, static) = {
2566 .function = sr_local,
2568 /* Takes a vector of packets. */
2569 .vector_size = sizeof (u32),
2570 .format_trace = format_sr_local_trace,
2572 .runtime_data_bytes = 0,
2574 .n_errors = SR_LOCAL_N_ERROR,
2575 .error_strings = sr_local_error_strings,
2577 .n_next_nodes = SR_LOCAL_N_NEXT,
2579 #define _(s,n) [SR_LOCAL_NEXT_##s] = n,
2580 foreach_sr_local_next
2585 VLIB_NODE_FUNCTION_MULTIARCH (sr_local_node, sr_local)
2587 ip6_sr_main_t * sr_get_main (vlib_main_t * vm)
2589 vlib_call_init_function (vm, sr_init);
2590 ASSERT(sr_local_node.index);
2595 static clib_error_t *
2596 set_ip6_sr_rewrite_fn (vlib_main_t * vm,
2597 unformat_input_t * input,
2598 vlib_cli_command_t * cmd)
2601 ip6_main_t * im = &ip6_main;
2602 ip_lookup_main_t * lm = &im->lookup_main;
2607 ip_adjacency_t * adj;
2608 vnet_hw_interface_t * hi;
2610 ip6_sr_main_t * sm = &sr_main;
2611 vnet_main_t * vnm = vnet_get_main();
2613 if (!unformat (input, "%U", unformat_ip6_address, &a))
2614 return clib_error_return (0, "ip6 address missing in '%U'",
2615 format_unformat_error, input);
2617 if (unformat (input, "rx-table-id %d", &fib_id))
2619 p = hash_get (im->fib_index_by_table_id, fib_id);
2621 return clib_error_return (0, "fib-id %d not found");
2625 adj_index = ip6_fib_lookup_with_table (im, fib_index, &a);
2627 if (adj_index == lm->miss_adj_index)
2628 return clib_error_return (0, "no match for %U",
2629 format_ip6_address, &a);
2631 adj = ip_get_adjacency (lm, adj_index);
2633 if (adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
2634 return clib_error_return (0, "%U unresolved (not a rewrite adj)",
2635 format_ip6_address, &a);
2637 adj->rewrite_header.next_index = sm->ip6_rewrite_sr_next_index;
2639 sw_if_index = adj->rewrite_header.sw_if_index;
2640 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2641 adj->rewrite_header.node_index = sr_fix_dst_addr_node.index;
2643 /* $$$$$ hack... steal the mcast group index */
2644 adj->mcast_group_index =
2645 vlib_node_add_next (vm, sr_fix_dst_addr_node.index, hi->output_node_index);
2650 VLIB_CLI_COMMAND (set_ip6_sr_rewrite, static) = {
2651 .path = "set ip6 sr rewrite",
2652 .short_help = "set ip6 sr rewrite <ip6-address> [fib-id <id>]",
2653 .function = set_ip6_sr_rewrite_fn,
2656 void vnet_register_sr_app_callback (void *cb)
2658 ip6_sr_main_t * sm = &sr_main;
2660 sm->sr_local_cb = cb;
2663 static clib_error_t *
2664 test_sr_hmac_validate_fn (vlib_main_t * vm,
2665 unformat_input_t * input,
2666 vlib_cli_command_t * cmd)
2668 ip6_sr_main_t * sm = &sr_main;
2670 if (unformat (input, "validate on"))
2671 sm->validate_hmac = 1;
2672 else if (unformat (input, "chunk-offset off"))
2673 sm->validate_hmac = 0;
2675 return clib_error_return (0, "expected validate on|off in '%U'",
2676 format_unformat_error, input);
2678 vlib_cli_output (vm, "hmac signature validation %s",
2684 VLIB_CLI_COMMAND (test_sr_hmac_validate, static) = {
2685 .path = "test sr hmac",
2686 .short_help = "test sr hmac validate [on|off]",
2687 .function = test_sr_hmac_validate_fn,
2690 i32 sr_hmac_add_del_key (ip6_sr_main_t * sm, u32 key_id, u8 * shared_secret,
2694 ip6_sr_hmac_key_t * key;
2698 /* Specific key in use? Fail. */
2699 if (key_id && vec_len (sm->hmac_keys) > key_id
2700 && sm->hmac_keys[key_id].shared_secret)
2704 key = find_or_add_shared_secret (sm, shared_secret, &index);
2705 ASSERT(index == key_id);
2711 if (key_id) /* delete by key ID */
2713 if (vec_len (sm->hmac_keys) <= key_id)
2716 key = sm->hmac_keys + key_id;
2718 hash_unset_mem (sm->hmac_key_by_shared_secret, key->shared_secret);
2719 vec_free (key->shared_secret);
2724 key = find_or_add_shared_secret (sm, shared_secret, &index);
2725 hash_unset_mem (sm->hmac_key_by_shared_secret, key->shared_secret);
2726 vec_free (key->shared_secret);
2731 static clib_error_t *
2732 sr_hmac_add_del_key_fn (vlib_main_t * vm,
2733 unformat_input_t * input,
2734 vlib_cli_command_t * cmd)
2736 ip6_sr_main_t * sm = &sr_main;
2740 u8 * shared_secret = 0;
2743 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2745 if (unformat (input, "del"))
2747 else if (unformat (input, "id %d", &key_id))
2749 else if (unformat (input, "key %s", &shared_secret))
2751 /* Do not include the trailing NULL byte. Guaranteed interop issue */
2752 _vec_len (shared_secret) -= 1;
2758 if (is_del == 0 && shared_secret == 0)
2759 return clib_error_return (0, "shared secret must be set to add a key");
2761 if (shared_secret == 0 && key_id_set == 0)
2762 return clib_error_return (0, "shared secret and key id both unset");
2764 rv = sr_hmac_add_del_key (sm, key_id, shared_secret, is_del);
2766 vec_free (shared_secret);
2774 return clib_error_return (0, "sr_hmac_add_del_key returned %d",
2781 VLIB_CLI_COMMAND (sr_hmac, static) = {
2783 .short_help = "sr hmac [del] id <nn> key <str>",
2784 .function = sr_hmac_add_del_key_fn,
2788 static clib_error_t *
2789 show_sr_hmac_fn (vlib_main_t * vm,
2790 unformat_input_t * input,
2791 vlib_cli_command_t * cmd)
2793 ip6_sr_main_t * sm = &sr_main;
2796 for (i = 1; i < vec_len (sm->hmac_keys); i++)
2798 if (sm->hmac_keys[i].shared_secret)
2799 vlib_cli_output (vm, "[%d]: %v", i, sm->hmac_keys[i].shared_secret);
2805 VLIB_CLI_COMMAND (show_sr_hmac, static) = {
2806 .path = "show sr hmac",
2807 .short_help = "show sr hmac",
2808 .function = show_sr_hmac_fn,
2811 static clib_error_t *
2812 test_sr_debug_fn (vlib_main_t * vm,
2813 unformat_input_t * input,
2814 vlib_cli_command_t * cmd)
2816 ip6_sr_main_t * sm = &sr_main;
2818 if (unformat (input, "on"))
2820 else if (unformat (input, "off"))
2823 return clib_error_return (0, "expected on|off in '%U'",
2824 format_unformat_error, input);
2826 vlib_cli_output (vm, "debug trace now %s", sm->is_debug ? "on" : "off");
2831 VLIB_CLI_COMMAND (test_sr_debug, static) = {
2832 .path = "test sr debug",
2833 .short_help = "test sr debug on|off",
2834 .function = test_sr_debug_fn,