2 * ip/ip6_neighbor.c: IP6 neighbor handling
4 * Copyright (c) 2010 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/ip/ip.h>
19 #include <vnet/ethernet/ethernet.h>
20 #include <vppinfra/mhash.h>
21 #include <vppinfra/md5.h>
24 #include <vnet/devices/dpdk/dpdk.h>
28 ip6_address_t ip6_address;
33 /* can't use sizeof link_layer_address, that's 8 */
34 #define ETHER_MAC_ADDR_LEN 6
37 ip6_neighbor_key_t key;
38 u8 link_layer_address[8];
40 #define IP6_NEIGHBOR_FLAG_STATIC (1 << 0)
41 #define IP6_NEIGHBOR_FLAG_GLEAN (2 << 0)
42 u64 cpu_time_last_updated;
46 /* advertised prefix option */
48 /* basic advertised information */
52 int adv_autonomous_flag;
53 u32 adv_valid_lifetime_in_secs;
54 u32 adv_pref_lifetime_in_secs;
56 /* advertised values are computed from these times if decrementing */
57 f64 valid_lifetime_expires;
58 f64 pref_lifetime_expires;
60 /* local information */
62 int deprecated_prefix_flag;
63 int decrement_lifetime_flag;
65 #define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
66 #define DEF_ADV_VALID_LIFETIME 2592000
67 #define DEF_ADV_PREF_LIFETIME 604800
69 /* extensions are added here, mobile, DNS etc.. */
74 /* group information */
76 ip6_address_t mcast_address;
78 ip6_address_t *mcast_source_address_pool;
81 /* configured router advertisement information per ipv6 interface */
84 /* advertised config information, zero means unspecified */
88 u16 adv_router_lifetime_in_sec;
89 u32 adv_neighbor_reachable_time_in_msec;
90 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
95 /* source link layer option */
96 u8 link_layer_address[8];
97 u8 link_layer_addr_len;
100 ip6_radv_prefix_t * adv_prefixes_pool;
102 /* Hash table mapping address to index in interface advertised prefix pool. */
103 mhash_t address_to_prefix_index;
105 /* MLDP group information */
106 ip6_mldp_group_t * mldp_group_pool;
108 /* Hash table mapping address to index in mldp address pool. */
109 mhash_t address_to_mldp_index;
111 /* local information */
114 int send_radv; /* radv on/off on this interface - set by config */
115 int cease_radv; /* we are ceasing to send - set byf config */
117 int adv_link_layer_address;
119 int failed_device_check;
120 int all_routers_mcast;
124 u32 all_nodes_adj_index;
125 u32 all_routers_adj_index;
126 u32 all_mldv2_routers_adj_index;
128 /* timing information */
129 #define DEF_MAX_RADV_INTERVAL 200
130 #define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
131 #define DEF_CURR_HOP_LIMIT 64
132 #define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
133 #define MAX_DEF_RTR_LIFETIME 9000
135 #define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
136 #define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
137 #define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
138 #define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
139 #define MAX_RA_DELAY_TIME .5 /* seconds */
141 f64 max_radv_interval;
142 f64 min_radv_interval;
143 f64 min_delay_between_radv;
144 f64 max_delay_between_radv;
145 f64 max_rtr_default_lifetime;
148 f64 last_multicast_time;
149 f64 next_multicast_time;
152 u32 initial_adverts_count;
153 f64 initial_adverts_interval;
154 u32 initial_adverts_sent;
157 u32 n_advertisements_sent;
158 u32 n_solicitations_rcvd;
159 u32 n_solicitations_dropped;
161 /* Link local address to use (defaults to underlying physical for logical interfaces */
162 ip6_address_t link_local_address;
163 u8 link_local_prefix_len;
172 } pending_resolution_t;
176 /* Hash tables mapping name to opcode. */
177 uword * opcode_by_name;
179 /* lite beer "glean" adjacency handling */
180 mhash_t pending_resolutions_by_address;
181 pending_resolution_t * pending_resolutions;
183 u32 * neighbor_input_next_index_by_hw_if_index;
185 ip6_neighbor_t * neighbor_pool;
187 mhash_t neighbor_index_by_key;
189 u32 * if_radv_pool_index_by_sw_if_index;
191 ip6_radv_t * if_radv_pool;
193 /* Neighbor attack mitigation */
194 u32 limit_neighbor_cache_size;
195 u32 neighbor_delete_rotor;
197 } ip6_neighbor_main_t;
199 static ip6_neighbor_main_t ip6_neighbor_main;
201 static u8 * format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
203 vlib_main_t * vm = va_arg (*va, vlib_main_t *);
204 ip6_neighbor_t * n = va_arg (*va, ip6_neighbor_t *);
205 vnet_main_t * vnm = vnet_get_main();
206 vnet_sw_interface_t * si;
210 return format (s, "%=12s%=20s%=6s%=20s%=40s", "Time", "Address", "Flags", "Link layer", "Interface");
212 if (n->flags & IP6_NEIGHBOR_FLAG_GLEAN)
213 flags = format(flags, "G");
215 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
216 flags = format(flags, "S");
218 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
219 s = format (s, "%=12U%=20U%=6s%=20U%=40U",
220 format_vlib_cpu_time, vm, n->cpu_time_last_updated,
221 format_ip6_address, &n->key.ip6_address,
222 flags ? (char *)flags : "",
223 format_ethernet_address, n->link_layer_address,
224 format_vnet_sw_interface_name, vnm, si);
230 static clib_error_t *
231 ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
235 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
238 if (! (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
240 u32 i, * to_delete = 0;
242 pool_foreach (n, nm->neighbor_pool, ({
243 if (n->key.sw_if_index == sw_if_index)
244 vec_add1 (to_delete, n - nm->neighbor_pool);
247 for (i = 0; i < vec_len (to_delete); i++)
249 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
250 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
251 pool_put (nm->neighbor_pool, n);
254 vec_free (to_delete);
260 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
262 static void unset_random_neighbor_entry (void)
264 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
265 vnet_main_t * vnm = vnet_get_main();
266 vlib_main_t * vm = vnm->vlib_main;
270 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
271 nm->neighbor_delete_rotor = index;
273 /* Try again from elt 0, could happen if an intfc goes down */
276 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
277 nm->neighbor_delete_rotor = index;
280 /* Nothing left in the pool */
284 e = pool_elt_at_index (nm->neighbor_pool, index);
286 vnet_unset_ip6_ethernet_neighbor (vm, e->key.sw_if_index,
288 e->link_layer_address,
295 u8 link_layer_address[6];
298 } ip6_neighbor_set_unset_rpc_args_t;
301 static void ip6_neighbor_set_unset_rpc_callback
302 ( ip6_neighbor_set_unset_rpc_args_t * a);
304 static void set_unset_ip6_neighbor_rpc
308 u8 *link_layer_addreess,
309 int is_add, int is_static)
311 ip6_neighbor_set_unset_rpc_args_t args;
312 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
314 args.sw_if_index = sw_if_index;
315 args.is_add = is_add;
316 args.is_static = is_static;
317 clib_memcpy (&args.addr, a, sizeof (*a));
318 clib_memcpy (args.link_layer_address, link_layer_addreess, 6);
320 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
321 (u8 *) &args, sizeof (args));
326 vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
329 u8 * link_layer_address,
330 uword n_bytes_link_layer_address,
333 vnet_main_t * vnm = vnet_get_main();
334 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
335 ip6_neighbor_key_t k;
336 ip6_neighbor_t * n = 0;
337 ip6_main_t * im = &ip6_main;
338 ip_lookup_main_t * lm = &im->lookup_main;
339 int make_new_nd_cache_entry=1;
343 ip_adjacency_t *existing_adj;
344 pending_resolution_t * pr;
347 if (os_get_cpu_number())
349 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
350 1 /* set new neighbor */, is_static);
355 k.sw_if_index = sw_if_index;
356 k.ip6_address = a[0];
359 vlib_worker_thread_barrier_sync (vm);
361 p = mhash_get (&nm->neighbor_index_by_key, &k);
363 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
364 /* Refuse to over-write static neighbor entry. */
366 (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
368 make_new_nd_cache_entry = 0;
371 /* Note: always install the route. It might have been deleted */
372 ip6_add_del_route_args_t args;
375 memset (&adj, 0, sizeof(adj));
376 adj.lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
377 adj.explicit_fib_index = ~0;
379 vnet_rewrite_for_sw_interface
381 VNET_L3_PACKET_TYPE_IP6,
383 ip6_rewrite_node.index,
386 sizeof (adj.rewrite_data));
388 /* result of this lookup should be next-hop adjacency */
389 adj_index = ip6_fib_lookup_with_table (im, im->fib_index_by_sw_if_index[sw_if_index], a);
390 existing_adj = ip_get_adjacency(lm, adj_index);
392 if (existing_adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
393 existing_adj->arp.next_hop.ip6.as_u64[0] == a->as_u64[0] &&
394 existing_adj->arp.next_hop.ip6.as_u64[1] == a->as_u64[1])
400 adjs = vec_dup(n->adjacencies);
402 clib_warning ("ip6 neighbor n not set");
404 /* Update all adj assigned to this arp entry */
405 vec_foreach(ai, adjs)
408 ip_adjacency_t * uadj = ip_get_adjacency(lm, *ai);
409 for (i = 0; i < uadj->n_adj; i++)
410 if (uadj[i].lookup_next_index == IP_LOOKUP_NEXT_ARP &&
411 uadj[i].arp.next_hop.ip6.as_u64[0] == a->as_u64[0] &&
412 uadj[i].arp.next_hop.ip6.as_u64[1] == a->as_u64[1])
413 ip_update_adjacency (lm, *ai + i, &adj);
420 args.table_index_or_table_id = im->fib_index_by_sw_if_index[sw_if_index];
421 args.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_ADD | IP6_ROUTE_FLAG_NEIGHBOR;
422 args.dst_address = a[0];
423 args.dst_address_length = 128;
427 ip6_add_del_route (im, &args);
430 if (make_new_nd_cache_entry) {
431 pool_get (nm->neighbor_pool, n);
432 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
437 /* Update time stamp and ethernet address. */
438 clib_memcpy (n->link_layer_address, link_layer_address, n_bytes_link_layer_address);
439 n->cpu_time_last_updated = clib_cpu_time_now ();
441 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
443 /* Customer(s) waiting for this address to be resolved? */
444 p = mhash_get (&nm->pending_resolutions_by_address, a);
450 while (next_index != (u32)~0)
452 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
453 vlib_process_signal_event (vm, pr->node_index,
456 next_index = pr->next_index;
457 pool_put (nm->pending_resolutions, pr);
460 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
463 vlib_worker_thread_barrier_release(vm);
468 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
471 u8 * link_layer_address,
472 uword n_bytes_link_layer_address)
474 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
475 ip6_neighbor_key_t k;
477 ip6_main_t * im = &ip6_main;
478 ip6_add_del_route_args_t args;
483 if (os_get_cpu_number())
485 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
491 k.sw_if_index = sw_if_index;
492 k.ip6_address = a[0];
495 vlib_worker_thread_barrier_sync (vm);
497 p = mhash_get (&nm->neighbor_index_by_key, &k);
504 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
505 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
506 pool_put (nm->neighbor_pool, n);
508 args.table_index_or_table_id = im->fib_index_by_sw_if_index[sw_if_index];
509 args.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_DEL
510 | IP6_ROUTE_FLAG_NEIGHBOR;
511 args.dst_address = a[0];
512 args.dst_address_length = 128;
516 ip6_add_del_route (im, &args);
518 vlib_worker_thread_barrier_release(vm);
524 vnet_ip6_neighbor_glean_add(u32 fib_index, void * next_hop_arg)
526 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
527 ip6_main_t * im = &ip6_main;
528 ip_lookup_main_t * lm = &im->lookup_main;
529 ip6_address_t * next_hop = next_hop_arg;
530 ip_adjacency_t add_adj, *adj;
531 ip6_add_del_route_args_t args;
533 ip6_neighbor_key_t k;
536 adj_index = ip6_fib_lookup_with_table(im, fib_index, next_hop);
537 adj = ip_get_adjacency(lm, adj_index);
539 if (!adj || adj->lookup_next_index != IP_LOOKUP_NEXT_ARP)
542 if (adj->arp.next_hop.ip6.as_u64[0] ||
543 adj->arp.next_hop.ip6.as_u64[1])
546 k.sw_if_index = adj->rewrite_header.sw_if_index;
547 k.ip6_address = *next_hop;
549 if (mhash_get (&nm->neighbor_index_by_key, &k))
552 pool_get (nm->neighbor_pool, n);
553 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool, /* old value */ 0);
555 n->cpu_time_last_updated = clib_cpu_time_now ();
556 n->flags = IP6_NEIGHBOR_FLAG_GLEAN;
558 memset(&args, 0, sizeof(args));
559 memcpy(&add_adj, adj, sizeof(add_adj));
560 add_adj.arp.next_hop.ip6 = *next_hop; /* install neighbor /128 route */
561 args.table_index_or_table_id = fib_index;
562 args.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_ADD | IP6_ROUTE_FLAG_NEIGHBOR;
563 args.dst_address = *next_hop;
564 args.dst_address_length = 128;
566 args.add_adj = &add_adj;
568 ip6_add_del_route (im, &args);
569 return ip6_fib_lookup_with_table (im, fib_index, next_hop);
573 static void ip6_neighbor_set_unset_rpc_callback
574 ( ip6_neighbor_set_unset_rpc_args_t * a)
576 vlib_main_t * vm = vlib_get_main();
578 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
579 a->link_layer_address, 6, a->is_static);
581 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
582 a->link_layer_address, 6);
587 ip6_neighbor_sort (void *a1, void *a2)
589 vnet_main_t * vnm = vnet_get_main();
590 ip6_neighbor_t * n1 = a1, * n2 = a2;
592 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
593 n2->key.sw_if_index);
595 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
599 static clib_error_t *
600 show_ip6_neighbors (vlib_main_t * vm,
601 unformat_input_t * input,
602 vlib_cli_command_t * cmd)
604 vnet_main_t * vnm = vnet_get_main();
605 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
606 ip6_neighbor_t * n, * ns;
607 clib_error_t * error = 0;
610 /* Filter entries by interface if given. */
612 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
615 pool_foreach (n, nm->neighbor_pool, ({ vec_add1 (ns, n[0]); }));
616 vec_sort_with_function (ns, ip6_neighbor_sort);
617 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
618 vec_foreach (n, ns) {
619 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
621 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
628 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
629 .path = "show ip6 neighbors",
630 .function = show_ip6_neighbors,
631 .short_help = "Show ip6 neighbors",
634 static clib_error_t *
635 set_ip6_neighbor (vlib_main_t * vm,
636 unformat_input_t * input,
637 vlib_cli_command_t * cmd)
639 vnet_main_t * vnm = vnet_get_main();
647 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
649 /* intfc, ip6-address, mac-address */
650 if (unformat (input, "%U %U %U",
651 unformat_vnet_sw_interface, vnm, &sw_if_index,
652 unformat_ip6_address, &addr,
653 unformat_ethernet_address, mac_address))
656 else if (unformat (input, "delete") || unformat (input, "del"))
658 else if (unformat (input, "static"))
665 return clib_error_return (0, "Missing interface, ip6 or hw address");
668 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
669 mac_address, sizeof(mac_address), is_static);
671 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
672 mac_address, sizeof(mac_address));
676 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) = {
677 .path = "set ip6 neighbor",
678 .function = set_ip6_neighbor,
679 .short_help = "set ip6 neighbor [del] <intfc> <ip6-address> <mac-address> [static]",
683 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
684 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
685 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
686 } icmp6_neighbor_solicitation_or_advertisement_next_t;
688 static_always_inline uword
689 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
690 vlib_node_runtime_t * node,
691 vlib_frame_t * frame,
692 uword is_solicitation)
694 vnet_main_t * vnm = vnet_get_main();
695 ip6_main_t * im = &ip6_main;
696 ip_lookup_main_t * lm = &im->lookup_main;
697 uword n_packets = frame->n_vectors;
698 u32 * from, * to_next;
699 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
700 icmp6_neighbor_discovery_option_type_t option_type;
701 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
704 from = vlib_frame_vector_args (frame);
705 n_left_from = n_packets;
706 next_index = node->cached_next_index;
708 if (node->flags & VLIB_NODE_FLAG_TRACE)
709 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
711 sizeof (icmp6_input_trace_t));
715 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
716 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
717 n_advertisements_sent = 0;
719 while (n_left_from > 0)
721 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
723 while (n_left_from > 0 && n_left_to_next > 0)
727 icmp6_neighbor_solicitation_or_advertisement_header_t * h0;
728 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * o0;
729 u32 bi0, options_len0, sw_if_index0, next0, error0;
730 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
734 bi0 = to_next[0] = from[0];
741 p0 = vlib_get_buffer (vm, bi0);
742 ip0 = vlib_buffer_get_current (p0);
743 h0 = ip6_next_header (ip0);
744 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
746 error0 = ICMP6_ERROR_NONE;
747 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
748 ip6_sadd_link_local = ip6_address_is_link_local_unicast(&ip0->src_address);
749 ip6_sadd_unspecified = ip6_address_is_unspecified (&ip0->src_address);
751 /* Check that source address is unspecified, link-local or else on-link. */
752 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
754 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
755 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, src_adj_index0);
757 /* Allow all realistic-looking rewrite adjacencies to pass */
758 ni0 = adj0->lookup_next_index;
759 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
760 (ni0 < IP6_LOOKUP_N_NEXT);
762 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
764 ? ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
768 o0 = (void *) (h0 + 1);
769 o0 = ((options_len0 == 8 && o0->header.type == option_type
770 && o0->header.n_data_u64s == 1) ? o0 : 0);
772 /* If src address unspecified or link local, donot learn neighbor MAC */
773 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
774 !ip6_sadd_unspecified && !ip6_sadd_link_local))
776 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
777 if (nm->limit_neighbor_cache_size &&
778 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
779 unset_random_neighbor_entry();
780 vnet_set_ip6_ethernet_neighbor (
782 is_solicitation ? &ip0->src_address : &h0->target_address,
783 o0->ethernet_address, sizeof (o0->ethernet_address), 0);
786 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
788 /* Check that target address is one that we know about. */
789 ip_interface_address_t * ia0;
790 ip6_address_fib_t ip6_af0;
793 ip6_addr_fib_init (&ip6_af0, &h0->target_address,
794 vec_elt (im->fib_index_by_sw_if_index,
797 /* Gross kludge, "thank you" MJ, don't even ask */
798 oldheap = clib_mem_set_heap (clib_per_cpu_mheaps[0]);
799 ia0 = ip_get_interface_address (lm, &ip6_af0);
800 clib_mem_set_heap (oldheap);
802 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN : error0;
806 next0 = (error0 != ICMP6_ERROR_NONE
807 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
808 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
812 error0 = error0 == ICMP6_ERROR_NONE ?
813 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
816 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
818 vnet_sw_interface_t * sw_if0;
819 ethernet_interface_t * eth_if0;
820 ethernet_header_t *eth0;
822 /* dst address is either source address or the all-nodes mcast addr */
823 if(!ip6_sadd_unspecified)
824 ip0->dst_address = ip0->src_address;
826 ip6_set_reserved_multicast_address(&ip0->dst_address,
827 IP6_MULTICAST_SCOPE_link_local,
828 IP6_MULTICAST_GROUP_ID_all_hosts);
830 ip0->src_address = h0->target_address;
831 ip0->hop_limit = 255;
832 h0->icmp.type = ICMP6_neighbor_advertisement;
834 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
835 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
836 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
839 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
841 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
844 h0->advertisement_flags = clib_host_to_net_u32
845 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
846 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
848 h0->icmp.checksum = 0;
850 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
852 ASSERT(bogus_length == 0);
854 /* Reuse current MAC header, copy SMAC to DMAC and
855 * interface MAC to SMAC */
856 vlib_buffer_advance(p0, - ethernet_buffer_header_size(p0));
857 eth0 = vlib_buffer_get_current(p0);
858 clib_memcpy(eth0->dst_address, eth0->src_address, 6);
859 clib_memcpy(eth0->src_address, eth_if0->address, 6);
861 /* Setup input and output sw_if_index for packet */
862 ASSERT(vnet_buffer(p0)->sw_if_index[VLIB_RX] == sw_if_index0);
863 vnet_buffer(p0)->sw_if_index[VLIB_TX] = sw_if_index0;
864 vnet_buffer(p0)->sw_if_index[VLIB_RX] =
865 vnet_main.local_interface_sw_if_index;
867 n_advertisements_sent++;
870 p0->error = error_node->errors[error0];
872 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
873 to_next, n_left_to_next,
877 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
880 /* Account for advertisements sent. */
881 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, n_advertisements_sent);
883 return frame->n_vectors;
886 /* for "syslogging" - use elog for now */
887 #define foreach_log_level \
889 _ (INFO, "INFORMATION") \
890 _ (NOTICE, "NOTICE") \
891 _ (WARNING, "WARNING") \
893 _ (CRIT, "CRITICAL") \
895 _ (EMERG, "EMERGENCY")
898 #define _(f,s) LOG_##f,
903 static char * log_level_strings[] = {
909 static int logmask = 1 << LOG_DEBUG;
912 ip6_neighbor_syslog(vlib_main_t *vm, int priority, char * fmt, ...)
914 /* just use elog for now */
918 if( (priority > LOG_EMERG) ||
919 !(logmask & (1 << priority)))
925 what = va_format (0, fmt, &va);
927 ELOG_TYPE_DECLARE (e) = {
928 .format = "ip6 nd: (%s): %s",
929 .format_args = "T4T4",
931 struct { u32 s[2]; } * ed;
932 ed = ELOG_DATA (&vm->elog_main, e);
933 ed->s[0] = elog_string(&vm->elog_main, log_level_strings[priority]);
934 ed->s[1] = elog_string(&vm->elog_main, (char *)what);
940 /* ipv6 neighbor discovery - router advertisements */
942 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
943 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
944 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
945 ICMP6_ROUTER_SOLICITATION_N_NEXT,
946 } icmp6_router_solicitation_or_advertisement_next_t;
948 static_always_inline uword
949 icmp6_router_solicitation(vlib_main_t * vm,
950 vlib_node_runtime_t * node,
951 vlib_frame_t * frame)
953 vnet_main_t * vnm = vnet_get_main();
954 ip6_main_t * im = &ip6_main;
955 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
956 uword n_packets = frame->n_vectors;
957 u32 * from, * to_next;
958 u32 n_left_from, n_left_to_next, next_index;
959 u32 n_advertisements_sent = 0;
962 icmp6_neighbor_discovery_option_type_t option_type;
964 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
966 from = vlib_frame_vector_args (frame);
967 n_left_from = n_packets;
968 next_index = node->cached_next_index;
970 if (node->flags & VLIB_NODE_FLAG_TRACE)
971 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
973 sizeof (icmp6_input_trace_t));
975 /* source may append his LL address */
976 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
978 while (n_left_from > 0)
980 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
982 while (n_left_from > 0 && n_left_to_next > 0)
986 ip6_radv_t *radv_info = 0;
988 icmp6_neighbor_discovery_header_t * h0;
989 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * o0;
991 u32 bi0, options_len0, sw_if_index0, next0, error0;
992 u32 is_solicitation = 1, is_dropped = 0;
993 u32 is_unspecified, is_link_local;
995 bi0 = to_next[0] = from[0];
1000 n_left_to_next -= 1;
1002 p0 = vlib_get_buffer (vm, bi0);
1003 ip0 = vlib_buffer_get_current (p0);
1004 h0 = ip6_next_header (ip0);
1005 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1006 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1007 is_link_local = ip6_address_is_link_local_unicast (&ip0->src_address);
1009 error0 = ICMP6_ERROR_NONE;
1010 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1012 /* check if solicitation (not from nd_timer node) */
1013 if (ip6_address_is_unspecified (&ip0->dst_address))
1014 is_solicitation = 0;
1016 /* Check that source address is unspecified, link-local or else on-link. */
1017 if (!is_unspecified && !is_link_local)
1019 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1020 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, src_adj_index0);
1022 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1023 || (adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP
1024 && adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE))
1025 ? ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1029 /* check for source LL option and process */
1030 o0 = (void *) (h0 + 1);
1031 o0 = ((options_len0 == 8
1032 && o0->header.type == option_type
1033 && o0->header.n_data_u64s == 1)
1037 /* if src address unspecified IGNORE any options */
1038 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1039 !is_unspecified && !is_link_local)) {
1040 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1041 if (nm->limit_neighbor_cache_size &&
1042 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
1043 unset_random_neighbor_entry();
1045 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1047 o0->ethernet_address,
1048 sizeof (o0->ethernet_address), 0);
1051 /* default is to drop */
1052 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1054 if (error0 == ICMP6_ERROR_NONE)
1056 vnet_sw_interface_t * sw_if0;
1057 ethernet_interface_t * eth_if0;
1060 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1061 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1062 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1064 /* only support ethernet interface type for now */
1065 error0 = (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF : error0;
1067 if (error0 == ICMP6_ERROR_NONE)
1071 /* adjust the sizeof the buffer to just include the ipv6 header */
1072 p0->current_length -= (options_len0 + sizeof(icmp6_neighbor_discovery_header_t));
1074 /* look up the radv_t information for this interface */
1075 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1077 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1080 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1082 error0 = ((!radv_info) ? ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : error0);
1084 if (error0 == ICMP6_ERROR_NONE)
1086 f64 now = vlib_time_now (vm);
1088 /* for solicited adverts - need to rate limit */
1091 if( (now - radv_info->last_radv_time) < MIN_DELAY_BETWEEN_RAS )
1094 radv_info->last_radv_time = now;
1098 icmp6_router_advertisement_header_t rh;
1100 rh.icmp.type = ICMP6_router_advertisement;
1102 rh.icmp.checksum = 0;
1104 rh.current_hop_limit = radv_info->curr_hop_limit;
1105 rh.router_lifetime_in_sec = clib_host_to_net_u16(radv_info->adv_router_lifetime_in_sec);
1106 rh.time_in_msec_between_retransmitted_neighbor_solicitations =
1107 clib_host_to_net_u32(radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1108 rh.neighbor_reachable_time_in_msec =
1109 clib_host_to_net_u32(radv_info->adv_neighbor_reachable_time_in_msec);
1111 rh.flags = (radv_info->adv_managed_flag) ? ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP : 0;
1112 rh.flags |= ( (radv_info->adv_other_flag) ? ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP : 0);
1115 u16 payload_length = sizeof(icmp6_router_advertisement_header_t);
1117 vlib_buffer_add_data (vm,
1118 p0->free_list_index,
1120 (void *)&rh, sizeof(icmp6_router_advertisement_header_t));
1122 if(radv_info->adv_link_layer_address)
1124 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t h;
1126 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1127 h.header.n_data_u64s = 1;
1129 /* copy ll address */
1130 clib_memcpy(&h.ethernet_address[0], eth_if0->address, 6);
1132 vlib_buffer_add_data (vm,
1133 p0->free_list_index,
1135 (void *)&h, sizeof(icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
1137 payload_length += sizeof(icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1140 /* add MTU option */
1141 if(radv_info->adv_link_mtu)
1143 icmp6_neighbor_discovery_mtu_option_t h;
1146 h.mtu = clib_host_to_net_u32(radv_info->adv_link_mtu);
1147 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1148 h.header.n_data_u64s = 1;
1150 payload_length += sizeof( icmp6_neighbor_discovery_mtu_option_t);
1152 vlib_buffer_add_data (vm,
1153 p0->free_list_index,
1155 (void *)&h, sizeof(icmp6_neighbor_discovery_mtu_option_t));
1158 /* add advertised prefix options */
1159 ip6_radv_prefix_t *pr_info;
1161 pool_foreach (pr_info, radv_info->adv_prefixes_pool, ({
1163 if(pr_info->enabled &&
1164 (!pr_info->decrement_lifetime_flag || (pr_info->pref_lifetime_expires >0)))
1166 /* advertise this prefix */
1167 icmp6_neighbor_discovery_prefix_information_option_t h;
1169 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1170 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1172 h.dst_address_length = pr_info->prefix_len;
1174 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1175 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
1177 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1179 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1180 h.preferred_time = 0;
1184 if(pr_info->decrement_lifetime_flag)
1186 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1187 (pr_info->valid_lifetime_expires - now) : 0;
1189 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1190 (pr_info->pref_lifetime_expires - now) : 0;
1193 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1194 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1198 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1200 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1202 vlib_buffer_add_data (vm,
1203 p0->free_list_index,
1205 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1210 /* add additional options before here */
1212 /* finish building the router advertisement... */
1213 if(!is_unspecified && radv_info->send_unicast)
1215 ip0->dst_address = ip0->src_address;
1219 /* target address is all-nodes mcast addr */
1220 ip6_set_reserved_multicast_address(&ip0->dst_address,
1221 IP6_MULTICAST_SCOPE_link_local,
1222 IP6_MULTICAST_GROUP_ID_all_hosts);
1225 /* source address MUST be the link-local address */
1226 ip0->src_address = radv_info->link_local_address;
1228 ip0->hop_limit = 255;
1229 ip0->payload_length = clib_host_to_net_u16 (payload_length);
1231 icmp6_router_advertisement_header_t * rh0 = (icmp6_router_advertisement_header_t *)(ip0 + 1);
1232 rh0->icmp.checksum =
1233 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1235 ASSERT(bogus_length == 0);
1237 /* setup output if and adjacency */
1238 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1239 vnet_main.local_interface_sw_if_index;
1241 if (is_solicitation)
1243 ethernet_header_t *eth0;
1244 /* Reuse current MAC header, copy SMAC to DMAC and
1245 * interface MAC to SMAC */
1246 vlib_buffer_reset (p0);
1247 eth0 = vlib_buffer_get_current(p0);
1248 clib_memcpy(eth0->dst_address, eth0->src_address, 6);
1249 clib_memcpy(eth0->src_address, eth_if0->address, 6);
1250 next0 = is_dropped ?
1251 next0 : ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1252 vnet_buffer(p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1256 adj_index0 = radv_info->all_nodes_adj_index;
1257 if (adj_index0 == 0)
1258 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1261 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, adj_index0);
1263 ((adj0->rewrite_header.sw_if_index != sw_if_index0
1264 || adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
1265 ? ICMP6_ERROR_ROUTER_SOLICITATION_DEST_UNKNOWN
1267 next0 = is_dropped ?
1268 next0 : ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1269 vnet_buffer (p0)->ip.adj_index[VLIB_RX] = adj_index0;
1273 radv_info->n_solicitations_dropped += is_dropped;
1274 radv_info->n_solicitations_rcvd += is_solicitation;
1276 if((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1278 radv_info->n_advertisements_sent++;
1279 n_advertisements_sent++;
1285 p0->error = error_node->errors[error0];
1287 if(error0 != ICMP6_ERROR_NONE)
1288 vlib_error_count (vm, error_node->node_index, error0, 1);
1290 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1291 to_next, n_left_to_next,
1296 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1299 /* Account for router advertisements sent. */
1300 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX, n_advertisements_sent);
1302 return frame->n_vectors;
1305 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1306 static_always_inline uword
1307 icmp6_router_advertisement(vlib_main_t * vm,
1308 vlib_node_runtime_t * node,
1309 vlib_frame_t * frame)
1311 vnet_main_t * vnm = vnet_get_main();
1312 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1313 uword n_packets = frame->n_vectors;
1314 u32 * from, * to_next;
1315 u32 n_left_from, n_left_to_next, next_index;
1316 u32 n_advertisements_rcvd = 0;
1318 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1320 from = vlib_frame_vector_args (frame);
1321 n_left_from = n_packets;
1322 next_index = node->cached_next_index;
1324 if (node->flags & VLIB_NODE_FLAG_TRACE)
1325 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1327 sizeof (icmp6_input_trace_t));
1329 while (n_left_from > 0)
1331 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1333 while (n_left_from > 0 && n_left_to_next > 0)
1337 ip6_radv_t *radv_info = 0;
1338 icmp6_router_advertisement_header_t * h0;
1339 u32 bi0, options_len0, sw_if_index0, next0, error0;
1341 bi0 = to_next[0] = from[0];
1346 n_left_to_next -= 1;
1348 p0 = vlib_get_buffer (vm, bi0);
1349 ip0 = vlib_buffer_get_current (p0);
1350 h0 = ip6_next_header (ip0);
1351 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1353 error0 = ICMP6_ERROR_NONE;
1354 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1356 /* Check that source address is link-local*/
1357 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1358 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1360 /* default is to drop */
1361 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1363 n_advertisements_rcvd++;
1365 if (error0 == ICMP6_ERROR_NONE)
1367 vnet_sw_interface_t * sw_if0;
1368 ethernet_interface_t * eth_if0;
1370 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1371 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1372 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1374 /* only support ethernet interface type for now */
1375 error0 = (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF : error0;
1377 if (error0 == ICMP6_ERROR_NONE)
1381 /* look up the radv_t information for this interface */
1382 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1384 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1387 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1389 error0 = ((!radv_info) ? ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : error0);
1391 if (error0 == ICMP6_ERROR_NONE)
1393 /* validate advertised information */
1394 if((h0->current_hop_limit && radv_info->curr_hop_limit) &&
1395 (h0->current_hop_limit != radv_info->curr_hop_limit))
1397 ip6_neighbor_syslog(vm, LOG_WARNING,
1398 "our AdvCurHopLimit on %U doesn't agree with %U",
1399 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1402 if((h0->flags & ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP) !=
1403 radv_info->adv_managed_flag)
1405 ip6_neighbor_syslog(vm, LOG_WARNING,
1406 "our AdvManagedFlag on %U doesn't agree with %U",
1407 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1410 if((h0->flags & ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP) !=
1411 radv_info->adv_other_flag)
1413 ip6_neighbor_syslog(vm, LOG_WARNING,
1414 "our AdvOtherConfigFlag on %U doesn't agree with %U",
1415 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1418 if((h0->time_in_msec_between_retransmitted_neighbor_solicitations &&
1419 radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations) &&
1420 (h0->time_in_msec_between_retransmitted_neighbor_solicitations !=
1421 clib_host_to_net_u32(radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
1423 ip6_neighbor_syslog(vm, LOG_WARNING,
1424 "our AdvRetransTimer on %U doesn't agree with %U",
1425 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1428 if((h0->neighbor_reachable_time_in_msec &&
1429 radv_info->adv_neighbor_reachable_time_in_msec) &&
1430 (h0->neighbor_reachable_time_in_msec !=
1431 clib_host_to_net_u32(radv_info->adv_neighbor_reachable_time_in_msec)))
1433 ip6_neighbor_syslog(vm, LOG_WARNING,
1434 "our AdvReachableTime on %U doesn't agree with %U",
1435 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1438 /* check for MTU or prefix options or .. */
1439 u8 * opt_hdr = (u8 *)(h0 + 1);
1440 while( options_len0 > 0 &&
1441 opt_hdr < p0->data + p0->current_data)
1443 icmp6_neighbor_discovery_option_header_t *o0 = ( icmp6_neighbor_discovery_option_header_t *)opt_hdr;
1444 int opt_len = o0->n_data_u64s << 3;
1445 icmp6_neighbor_discovery_option_type_t option_type = o0->type;
1447 if(options_len0 < 2)
1449 ip6_neighbor_syslog(vm, LOG_ERR,
1450 "malformed RA packet on %U from %U",
1451 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1457 ip6_neighbor_syslog(vm, LOG_ERR,
1458 " zero length option in RA on %U from %U",
1459 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1462 else if( opt_len > options_len0)
1464 ip6_neighbor_syslog(vm, LOG_ERR,
1465 "option length in RA packet greater than total length on %U from %U",
1466 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1470 options_len0 -= opt_len;
1475 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
1477 icmp6_neighbor_discovery_mtu_option_t *h =
1478 (icmp6_neighbor_discovery_mtu_option_t *)(o0);
1480 if(opt_len < sizeof(*h))
1483 if((h->mtu && radv_info->adv_link_mtu) &&
1484 (h->mtu != clib_host_to_net_u32(radv_info->adv_link_mtu)))
1486 ip6_neighbor_syslog(vm, LOG_WARNING,
1487 "our AdvLinkMTU on %U doesn't agree with %U",
1488 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1493 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1495 icmp6_neighbor_discovery_prefix_information_option_t *h =
1496 (icmp6_neighbor_discovery_prefix_information_option_t *)(o0);
1498 /* validate advertised prefix options */
1499 ip6_radv_prefix_t *pr_info;
1500 u32 preferred, valid;
1502 if(opt_len < sizeof(*h))
1505 preferred = clib_net_to_host_u32(h->preferred_time);
1506 valid = clib_net_to_host_u32(h->valid_time);
1508 /* look for matching prefix - if we our advertising it, it better be consistant */
1509 pool_foreach (pr_info, radv_info->adv_prefixes_pool, ({
1512 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1514 if(pr_info->enabled &&
1515 (pr_info->prefix_len == h->dst_address_length) &&
1516 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1519 if(!pr_info->decrement_lifetime_flag &&
1520 valid != pr_info->adv_valid_lifetime_in_secs)
1522 ip6_neighbor_syslog(vm, LOG_WARNING,
1523 "our ADV validlifetime on %U for %U does not agree with %U",
1524 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1525 format_ip6_address, &h->dst_address);
1527 if(!pr_info->decrement_lifetime_flag &&
1528 preferred != pr_info->adv_pref_lifetime_in_secs)
1530 ip6_neighbor_syslog(vm, LOG_WARNING,
1531 "our ADV preferredlifetime on %U for %U does not agree with %U",
1532 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1533 format_ip6_address, &h->dst_address);
1549 p0->error = error_node->errors[error0];
1551 if(error0 != ICMP6_ERROR_NONE)
1552 vlib_error_count (vm, error_node->node_index, error0, 1);
1554 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1555 to_next, n_left_to_next,
1559 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1562 /* Account for router advertisements sent. */
1563 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX, n_advertisements_rcvd);
1565 return frame->n_vectors;
1568 /* create and initialize router advertisement parameters with default values for this intfc */
1570 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
1574 ip6_main_t * im = &ip6_main;
1575 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1576 ip_lookup_main_t * lm = &im->lookup_main;
1579 vnet_sw_interface_t * sw_if0;
1580 ethernet_interface_t * eth_if0 = 0;
1582 /* lookup radv container - ethernet interfaces only */
1583 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1584 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1585 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1590 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
1591 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1595 a = pool_elt_at_index (nm->if_radv_pool, ri);
1599 u32 i, * to_delete = 0;
1600 ip6_radv_prefix_t *p;
1601 ip6_mldp_group_t *m;
1603 /* remove adjacencies */
1604 ip_del_adjacency (lm, a->all_nodes_adj_index);
1605 ip_del_adjacency (lm, a->all_routers_adj_index);
1606 ip_del_adjacency (lm, a->all_mldv2_routers_adj_index);
1608 /* clean up prefix_pool */
1609 pool_foreach (p, a->adv_prefixes_pool, ({
1610 vec_add1 (to_delete, p - a->adv_prefixes_pool);
1613 for (i = 0; i < vec_len (to_delete); i++)
1615 p = pool_elt_at_index (a->adv_prefixes_pool, to_delete[i]);
1616 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
1617 pool_put (a->adv_prefixes_pool, p);
1620 vec_free (to_delete);
1623 /* clean up mldp group pool */
1624 pool_foreach (m, a->mldp_group_pool, ({
1625 vec_add1 (to_delete, m - a->mldp_group_pool);
1628 for (i = 0; i < vec_len (to_delete); i++)
1630 m = pool_elt_at_index (a->mldp_group_pool, to_delete[i]);
1631 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
1632 pool_put (a->mldp_group_pool, m);
1635 vec_free (to_delete);
1637 pool_put (nm->if_radv_pool, a);
1638 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
1646 vnet_hw_interface_t * hw_if0;
1648 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
1650 pool_get (nm->if_radv_pool, a);
1652 ri = a - nm->if_radv_pool;
1653 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
1655 /* initialize default values (most of which are zero) */
1656 memset (a, 0, sizeof (a[0]));
1658 a->sw_if_index = sw_if_index;
1660 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
1661 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
1662 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
1663 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
1665 a->adv_link_layer_address = 1; /* send ll address source address option */
1667 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
1668 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
1669 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
1670 a->seed = (u32) (clib_cpu_time_now() & 0xFFFFFFFF);
1672 /* for generating random interface ids */
1673 a->randomizer = random_u64 (&a->seed);
1675 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS ;
1676 a->initial_adverts_sent = a->initial_adverts_count-1;
1677 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1679 /* deafult is to send */
1682 /* fill in radv_info for this interface that will be needed later */
1683 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
1685 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
1687 /* fill in default link-local address (this may be overridden) */
1688 ip6_link_local_address_from_ethernet_address (&a->link_local_address, eth_if0->address);
1689 a->link_local_prefix_len = 64;
1691 mhash_init (&a->address_to_prefix_index, sizeof (uword), sizeof (ip6_address_t));
1692 mhash_init (&a->address_to_mldp_index, sizeof (uword), sizeof (ip6_address_t));
1695 ip_adjacency_t *adj;
1696 u8 link_layer_address[6] =
1697 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_all_hosts};
1699 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1700 &a->all_nodes_adj_index);
1702 adj->lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
1703 adj->if_address_index = ~0;
1705 vnet_rewrite_for_sw_interface
1707 VNET_L3_PACKET_TYPE_IP6,
1709 ip6_rewrite_node.index,
1711 &adj->rewrite_header,
1712 sizeof (adj->rewrite_data));
1716 ip_adjacency_t *adj;
1717 u8 link_layer_address[6] =
1718 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_all_routers};
1720 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1721 &a->all_routers_adj_index);
1723 adj->lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
1724 adj->if_address_index = ~0;
1726 vnet_rewrite_for_sw_interface
1728 VNET_L3_PACKET_TYPE_IP6,
1730 ip6_rewrite_node.index,
1732 &adj->rewrite_header,
1733 sizeof (adj->rewrite_data));
1737 ip_adjacency_t *adj;
1738 u8 link_layer_address[6] =
1739 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_mldv2_routers};
1741 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1742 &a->all_mldv2_routers_adj_index);
1744 adj->lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
1745 adj->if_address_index = ~0;
1747 vnet_rewrite_for_sw_interface
1749 VNET_L3_PACKET_TYPE_IP6,
1751 ip6_rewrite_node.index,
1753 &adj->rewrite_header,
1754 sizeof (adj->rewrite_data));
1757 /* add multicast groups we will always be reporting */
1759 ip6_mldp_group_t *mcast_group_info;
1761 ip6_set_reserved_multicast_address (&addr,
1762 IP6_MULTICAST_SCOPE_link_local,
1763 IP6_MULTICAST_GROUP_ID_all_hosts);
1765 /* lookup mldp info for this interface */
1767 uword * p = mhash_get (&a->address_to_mldp_index, &addr);
1768 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1771 if(!mcast_group_info)
1775 pool_get (a->mldp_group_pool, mcast_group_info);
1777 mi = mcast_group_info - a->mldp_group_pool;
1778 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1780 mcast_group_info->type = 4;
1781 mcast_group_info->mcast_source_address_pool = 0;
1782 mcast_group_info->num_sources = 0;
1783 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1786 ip6_set_reserved_multicast_address (&addr,
1787 IP6_MULTICAST_SCOPE_link_local,
1788 IP6_MULTICAST_GROUP_ID_all_routers);
1790 p = mhash_get (&a->address_to_mldp_index, &addr);
1791 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1793 if(!mcast_group_info)
1797 pool_get (a->mldp_group_pool, mcast_group_info);
1799 mi = mcast_group_info - a->mldp_group_pool;
1800 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1802 mcast_group_info->type = 4;
1803 mcast_group_info->mcast_source_address_pool = 0;
1804 mcast_group_info->num_sources = 0;
1805 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1808 ip6_set_reserved_multicast_address (&addr,
1809 IP6_MULTICAST_SCOPE_link_local,
1810 IP6_MULTICAST_GROUP_ID_mldv2_routers);
1812 p = mhash_get (&a->address_to_mldp_index, &addr);
1813 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1815 if(!mcast_group_info)
1819 pool_get (a->mldp_group_pool, mcast_group_info);
1821 mi = mcast_group_info - a->mldp_group_pool;
1822 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1824 mcast_group_info->type = 4;
1825 mcast_group_info->mcast_source_address_pool = 0;
1826 mcast_group_info->num_sources = 0;
1827 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1834 /* send an mldpv2 report */
1836 ip6_neighbor_send_mldpv2_report(u32 sw_if_index)
1838 vnet_main_t * vnm = vnet_get_main();
1839 vlib_main_t * vm = vnm->vlib_main;
1840 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1841 vnet_sw_interface_t * sw_if0;
1842 ethernet_interface_t * eth_if0;
1846 ip6_radv_t *radv_info;
1856 icmp6_multicast_listener_report_header_t *rh0;
1857 icmp6_multicast_listener_report_packet_t *rp0;
1859 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1860 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1861 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1863 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
1866 /* look up the radv_t information for this interface */
1867 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
1869 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1874 /* send report now - build a mldpv2 report packet */
1875 n_allocated = vlib_buffer_alloc_from_free_list(vm,
1878 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1879 if (PREDICT_FALSE(n_allocated == 0))
1881 clib_warning ("buffer allocation failure");
1885 b0 = vlib_get_buffer (vm, bo0);
1887 /* adjust the sizeof the buffer to just include the ipv6 header */
1888 b0->current_length = sizeof(icmp6_multicast_listener_report_packet_t);
1890 payload_length = sizeof(icmp6_multicast_listener_report_header_t);
1892 b0->error = ICMP6_ERROR_NONE;
1894 rp0 = vlib_buffer_get_current (b0);
1895 ip0 = (ip6_header_t *)&rp0-> ip;
1896 rh0 = (icmp6_multicast_listener_report_header_t *)&rp0-> report_hdr;
1898 memset (rp0 , 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
1900 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
1902 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
1903 /* for DEBUG - vnet driver won't seem to emit router alerts */
1904 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
1907 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
1909 /* source address MUST be the link-local address */
1910 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1911 ip0->src_address = radv_info->link_local_address;
1913 /* destination is all mldpv2 routers */
1914 ip6_set_reserved_multicast_address(&ip0->dst_address,
1915 IP6_MULTICAST_SCOPE_link_local,
1916 IP6_MULTICAST_GROUP_ID_mldv2_routers);
1918 /* add reports here */
1919 ip6_mldp_group_t *m;
1920 int num_addr_records = 0;
1921 icmp6_multicast_address_record_t rr;
1923 /* fill in the hop-by-hop extension header (router alert) info */
1924 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
1925 rh0->ext_hdr.n_data_u64s = 0;
1927 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
1929 rh0->alert.value = 0;
1934 rh0->icmp.checksum = 0;
1936 pool_foreach (m, radv_info->mldp_group_pool, ({
1939 rr.aux_data_len_u32s = 0;
1940 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
1941 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
1945 vlib_buffer_add_data (vm,
1946 b0->free_list_index,
1948 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
1950 payload_length += sizeof( icmp6_multicast_address_record_t);
1954 rh0->num_addr_records = clib_host_to_net_u16(num_addr_records);
1956 /* update lengths */
1957 ip0->payload_length = clib_host_to_net_u16 (payload_length);
1959 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
1961 ASSERT(bogus_length == 0);
1964 * OK to override w/ no regard for actual FIB, because
1965 * ip6-rewrite-local only looks at the adjacency.
1967 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1968 vnet_main.local_interface_sw_if_index;
1970 vnet_buffer (b0)->ip.adj_index[VLIB_RX] =
1971 radv_info->all_mldv2_routers_adj_index;
1973 vlib_node_t * node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-local");
1975 f = vlib_get_frame_to_node (vm, node->index);
1976 to_next = vlib_frame_vector_args (f);
1980 vlib_put_frame_to_node (vm, node->index, f);
1984 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) = {
1985 .function = icmp6_router_solicitation,
1986 .name = "icmp6-router-solicitation",
1988 .vector_size = sizeof (u32),
1990 .format_trace = format_icmp6_input_trace,
1992 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
1994 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "error-drop",
1995 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-local",
1996 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2000 /* send a RA or update the timer info etc.. */
2002 ip6_neighbor_process_timer_event (vlib_main_t * vm,
2003 vlib_node_runtime_t * node,
2004 vlib_frame_t * frame)
2006 vnet_main_t * vnm = vnet_get_main();
2007 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2008 ip6_radv_t *radv_info;
2009 vlib_frame_t * f = 0;
2010 u32 n_this_frame = 0;
2011 u32 n_left_to_next = 0;
2014 icmp6_router_solicitation_header_t * h0;
2016 f64 now = vlib_time_now (vm);
2018 /* Interface ip6 radv info list */
2019 pool_foreach (radv_info, nm->if_radv_pool, ({
2021 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2023 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2024 radv_info->next_multicast_time = now;
2025 radv_info->last_multicast_time = now;
2026 radv_info->last_radv_time = 0;
2027 radv_info->all_routers_mcast = 0;
2031 /* Make sure that we've joined the all-routers multicast group */
2032 if(!radv_info->all_routers_mcast)
2034 /* send MDLP_REPORT_EVENT message */
2035 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2036 radv_info->all_routers_mcast = 1;
2039 /* is it time to send a multicast RA on this interface? */
2040 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2045 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2046 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2048 /* multicast send - compute next multicast send time */
2049 if( radv_info->initial_adverts_sent > 0)
2051 radv_info->initial_adverts_sent--;
2052 if(rfn > radv_info-> initial_adverts_interval)
2053 rfn = radv_info-> initial_adverts_interval;
2055 /* check to see if we are ceasing to send */
2056 if( radv_info->initial_adverts_sent == 0)
2057 if(radv_info->cease_radv)
2058 radv_info->send_radv = 0;
2061 radv_info->next_multicast_time = rfn + now;
2062 radv_info->last_multicast_time = now;
2064 /* send advert now - build a "solicted" router advert with unspecified source address */
2065 n_allocated = vlib_buffer_alloc_from_free_list(vm,
2068 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2070 if (PREDICT_FALSE(n_allocated == 0))
2072 clib_warning ("buffer allocation failure");
2075 b0 = vlib_get_buffer (vm, bo0);
2076 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2077 b0->error = ICMP6_ERROR_NONE;
2078 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2080 h0 = vlib_buffer_get_current (b0);
2082 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2084 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2085 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2086 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2087 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2088 h0->ip.hop_limit = 255;
2090 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2091 h0->ip.src_address.as_u64[0] = 0;
2092 h0->ip.src_address.as_u64[1] = 0;
2094 h0->ip.dst_address.as_u64[0] = 0;
2095 h0->ip.dst_address.as_u64[1] = 0;
2097 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2099 if (PREDICT_FALSE(f == 0))
2101 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2102 to_next = vlib_frame_vector_args (f);
2103 n_left_to_next = VLIB_FRAME_SIZE;
2112 if (PREDICT_FALSE(n_left_to_next == 0))
2114 f->n_vectors = n_this_frame;
2115 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2123 ASSERT(n_this_frame);
2124 f->n_vectors = n_this_frame;
2125 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2131 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2132 vlib_node_runtime_t * node,
2133 vlib_frame_t * frame)
2136 ip6_icmp_neighbor_discovery_event_data_t * event_data;
2138 /* init code here */
2142 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */);
2144 event_data = vlib_process_get_event_data (vm, &event_type);
2148 /* No events found: timer expired. */
2149 /* process interface list and send RAs as appropriate, update timer info */
2150 ip6_neighbor_process_timer_event (vm, node, frame);
2154 switch (event_type) {
2156 case ICMP6_ND_EVENT_INIT:
2167 _vec_len (event_data) = 0;
2170 return frame->n_vectors;
2173 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) = {
2174 .function = icmp6_router_advertisement,
2175 .name = "icmp6-router-advertisement",
2177 .vector_size = sizeof (u32),
2179 .format_trace = format_icmp6_input_trace,
2187 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2189 .function = ip6_icmp_neighbor_discovery_event_process,
2190 .name = "ip6-icmp-neighbor-discovery-event-process",
2191 .type = VLIB_NODE_TYPE_PROCESS,
2195 icmp6_neighbor_solicitation (vlib_main_t * vm,
2196 vlib_node_runtime_t * node,
2197 vlib_frame_t * frame)
2198 { return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, /* is_solicitation */ 1); }
2201 icmp6_neighbor_advertisement (vlib_main_t * vm,
2202 vlib_node_runtime_t * node,
2203 vlib_frame_t * frame)
2204 { return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, /* is_solicitation */ 0); }
2206 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) = {
2207 .function = icmp6_neighbor_solicitation,
2208 .name = "icmp6-neighbor-solicitation",
2210 .vector_size = sizeof (u32),
2212 .format_trace = format_icmp6_input_trace,
2214 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2216 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "error-drop",
2217 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2221 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) = {
2222 .function = icmp6_neighbor_advertisement,
2223 .name = "icmp6-neighbor-advertisement",
2225 .vector_size = sizeof (u32),
2227 .format_trace = format_icmp6_input_trace,
2235 /* API support functions */
2237 ip6_neighbor_ra_config(vlib_main_t * vm, u32 sw_if_index,
2238 u8 suppress, u8 managed, u8 other,
2239 u8 ll_option, u8 send_unicast, u8 cease,
2240 u8 use_lifetime, u32 lifetime,
2241 u32 initial_count, u32 initial_interval,
2242 u32 max_interval, u32 min_interval,
2245 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2249 /* look up the radv_t information for this interface */
2250 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2251 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2252 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2257 ip6_radv_t * radv_info;
2258 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2260 if((max_interval != 0) && (min_interval ==0))
2261 min_interval = .75 * max_interval;
2263 max_interval = (max_interval != 0) ? ( (is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) : radv_info->max_radv_interval;
2264 min_interval = (min_interval != 0) ? ( (is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) : radv_info->min_radv_interval;
2265 lifetime = (use_lifetime != 0) ? ( (is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) : radv_info->adv_router_lifetime_in_sec;
2269 if(lifetime > MAX_DEF_RTR_LIFETIME)
2270 lifetime = MAX_DEF_RTR_LIFETIME;
2272 if(lifetime <= max_interval)
2273 return VNET_API_ERROR_INVALID_VALUE;
2276 if(min_interval != 0)
2278 if((min_interval > .75 * max_interval) ||
2280 return VNET_API_ERROR_INVALID_VALUE;
2283 if((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2284 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2285 return VNET_API_ERROR_INVALID_VALUE;
2288 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2289 if "flag" is clear don't change corresponding value
2291 radv_info->send_radv = (suppress != 0) ? ( (is_no != 0) ? 1 : 0 ) : radv_info->send_radv;
2292 radv_info->adv_managed_flag = ( managed != 0) ? ( (is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2293 radv_info->adv_other_flag = (other != 0) ? ( (is_no) ? 0: 1) : radv_info->adv_other_flag;
2294 radv_info->adv_link_layer_address = ( ll_option != 0) ? ( (is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2295 radv_info->send_unicast = (send_unicast != 0) ? ( (is_no) ? 0 : 1) : radv_info->send_unicast;
2296 radv_info->cease_radv = ( cease != 0) ? ( (is_no) ? 0 : 1) : radv_info->cease_radv;
2298 radv_info->min_radv_interval = min_interval;
2299 radv_info->max_radv_interval = max_interval;
2300 radv_info->adv_router_lifetime_in_sec = lifetime;
2302 radv_info->initial_adverts_count =
2303 (initial_count != 0) ? ( (is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) : radv_info->initial_adverts_count ;
2304 radv_info->initial_adverts_interval =
2305 (initial_interval != 0) ? ( (is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) : radv_info->initial_adverts_interval;
2308 if((cease != 0) && (is_no))
2309 radv_info-> send_radv = 1;
2311 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2312 radv_info->next_multicast_time = vlib_time_now (vm);
2313 radv_info->last_multicast_time = vlib_time_now (vm);
2314 radv_info->last_radv_time = 0;
2320 ip6_neighbor_ra_prefix(vlib_main_t * vm, u32 sw_if_index,
2321 ip6_address_t *prefix_addr, u8 prefix_len,
2322 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2323 u8 no_advertise, u8 off_link, u8 no_autoconfig, u8 no_onlink,
2326 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2331 /* look up the radv_t information for this interface */
2332 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2334 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2336 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2340 f64 now = vlib_time_now (vm);
2341 ip6_radv_t * radv_info;
2342 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2344 /* prefix info add, delete or update */
2345 ip6_radv_prefix_t * prefix;
2347 /* lookup prefix info for this address on this interface */
2348 uword * p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2350 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2356 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
2358 if(prefix->prefix_len != prefix_len)
2359 return VNET_API_ERROR_INVALID_VALUE_2;
2361 /* FIXME - Should the DP do this or the CP ?*/
2362 /* do specific delete processing here before returning */
2363 /* try to remove from routing table */
2365 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,/* old_value */ 0);
2366 pool_put (radv_info->adv_prefixes_pool, prefix);
2368 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2369 radv_info->next_multicast_time = vlib_time_now (vm);
2370 radv_info->last_multicast_time = vlib_time_now (vm);
2371 radv_info->last_radv_time = 0;
2375 /* adding or changing */
2380 pool_get (radv_info->adv_prefixes_pool, prefix);
2381 pi = prefix - radv_info->adv_prefixes_pool;
2382 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi, /* old_value */ 0);
2384 memset(prefix, 0x0, sizeof(ip6_radv_prefix_t));
2386 prefix->prefix_len = prefix_len;
2387 clib_memcpy(&prefix->prefix, prefix_addr, sizeof(ip6_address_t));
2389 /* initialize default values */
2390 prefix->adv_on_link_flag = 1; /* L bit set */
2391 prefix->adv_autonomous_flag = 1; /* A bit set */
2392 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
2393 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2394 prefix->enabled = 1;
2395 prefix->decrement_lifetime_flag = 1;
2396 prefix->deprecated_prefix_flag = 1;
2400 /* FIXME - Should the DP do this or the CP ?*/
2401 /* insert prefix into routing table as a connected prefix */
2410 if(prefix->prefix_len != prefix_len)
2411 return VNET_API_ERROR_INVALID_VALUE_2;
2415 /* FIXME - Should the DP do this or the CP ?*/
2416 /* remove from routing table if already there */
2420 if((val_lifetime == ~0) || (pref_lifetime == ~0))
2422 prefix->adv_valid_lifetime_in_secs = ~0;
2423 prefix->adv_pref_lifetime_in_secs = ~0;
2424 prefix->decrement_lifetime_flag = 0;
2428 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2429 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
2432 /* copy remaining */
2433 prefix->enabled = !(no_advertise != 0);
2434 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2435 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2439 /* fill in the expiration times */
2440 prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs;
2441 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
2443 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2444 radv_info->next_multicast_time = vlib_time_now (vm);
2445 radv_info->last_multicast_time = vlib_time_now (vm);
2446 radv_info->last_radv_time = 0;
2452 ip6_neighbor_cmd(vlib_main_t * vm, unformat_input_t * main_input, vlib_cli_command_t * cmd)
2454 vnet_main_t * vnm = vnet_get_main();
2455 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2456 clib_error_t * error = 0;
2458 u8 suppress = 0, managed = 0, other = 0;
2459 u8 suppress_ll_option = 0, send_unicast = 0, cease= 0;
2460 u8 use_lifetime = 0;
2461 u32 sw_if_index, ra_lifetime = 0, ra_initial_count = 0, ra_initial_interval = 0;
2462 u32 ra_max_interval = 0 , ra_min_interval = 0;
2464 unformat_input_t _line_input, * line_input = &_line_input;
2465 vnet_sw_interface_t * sw_if0;
2467 int add_radv_info = 1;
2468 __attribute__((unused)) ip6_radv_t * radv_info = 0;
2469 ip6_address_t ip6_addr;
2473 /* Get a line of input. */
2474 if (! unformat_user (main_input, unformat_line_input, line_input))
2477 /* get basic radv info for this interface */
2478 if(unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2481 if (unformat_user (line_input,
2482 unformat_vnet_sw_interface, vnm, &sw_if_index))
2485 ethernet_interface_t * eth_if0 = 0;
2487 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2488 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2489 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2493 error = clib_error_return (0, "Interface must be of ethernet type");
2497 /* look up the radv_t information for this interface */
2498 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2500 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2504 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2508 error = clib_error_return (0, "unknown interface %U'",
2509 format_unformat_error, line_input);
2515 error = clib_error_return (0, "invalid interface name %U'",
2516 format_unformat_error, line_input);
2521 /* get the rest of the command */
2522 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2524 if (unformat (line_input, "no"))
2526 else if(unformat (line_input, "prefix %U/%d",
2527 unformat_ip6_address, &ip6_addr,
2533 else if (unformat (line_input, "ra-managed-config-flag"))
2538 else if (unformat (line_input, "ra-other-config-flag"))
2543 else if (unformat (line_input, "ra-suppress") ||
2544 unformat (line_input, "ra-surpress"))
2549 else if (unformat (line_input, "ra-suppress-link-layer") ||
2550 unformat (line_input, "ra-surpress-link-layer"))
2552 suppress_ll_option = 1;
2555 else if (unformat (line_input, "ra-send-unicast"))
2560 else if (unformat (line_input, "ra-lifetime"))
2562 if (!unformat (line_input, "%d", &ra_lifetime))
2563 return(error = unformat_parse_error (line_input));
2567 else if (unformat (line_input, "ra-initial"))
2569 if (!unformat (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
2570 return(error = unformat_parse_error (line_input));
2573 else if (unformat (line_input, "ra-interval"))
2575 if (!unformat (line_input, "%d", &ra_max_interval))
2576 return(error = unformat_parse_error (line_input));
2578 if (!unformat (line_input, "%d", &ra_min_interval))
2579 ra_min_interval = 0;
2582 else if(unformat (line_input, "ra-cease"))
2588 return(unformat_parse_error (line_input));
2593 ip6_neighbor_ra_config(vm, sw_if_index,
2594 suppress, managed, other,
2595 suppress_ll_option, send_unicast, cease,
2596 use_lifetime, ra_lifetime,
2597 ra_initial_count, ra_initial_interval,
2598 ra_max_interval, ra_min_interval,
2603 u32 valid_lifetime_in_secs = 0;
2604 u32 pref_lifetime_in_secs = 0;
2605 u8 use_prefix_default_values = 0;
2606 u8 no_advertise = 0;
2608 u8 no_autoconfig = 0;
2611 /* get the rest of the command */
2612 while(unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2614 if(unformat (line_input, "default"))
2616 use_prefix_default_values = 1;
2619 else if(unformat (line_input, "infinite"))
2621 valid_lifetime_in_secs = ~0;
2622 pref_lifetime_in_secs = ~0;
2625 else if(unformat (line_input, "%d %d", &valid_lifetime_in_secs,
2626 &pref_lifetime_in_secs))
2633 /* get the rest of the command */
2634 while (!use_prefix_default_values &&
2635 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2637 if(unformat (line_input, "no-advertise"))
2639 else if(unformat (line_input, "off-link"))
2641 else if(unformat (line_input, "no-autoconfig"))
2643 else if(unformat (line_input, "no-onlink"))
2646 return(unformat_parse_error (line_input));
2649 ip6_neighbor_ra_prefix(vm, sw_if_index,
2650 &ip6_addr, addr_len,
2651 use_prefix_default_values,
2652 valid_lifetime_in_secs,
2653 pref_lifetime_in_secs,
2661 unformat_free (line_input);
2668 ip6_print_addrs(vlib_main_t * vm,
2671 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2674 for (i = 0; i < vec_len (addrs); i++)
2676 ip_interface_address_t * a = pool_elt_at_index(lm->if_address_pool, addrs[i]);
2677 ip6_address_t * address = ip_interface_address_get_address (lm, a);
2679 vlib_cli_output (vm, "\t\t%U/%d",
2680 format_ip6_address, address,
2685 static clib_error_t *
2686 show_ip6_interface_cmd (vlib_main_t * vm,
2687 unformat_input_t * input,
2688 vlib_cli_command_t * cmd)
2690 vnet_main_t * vnm = vnet_get_main();
2691 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2692 clib_error_t * error = 0;
2697 if (unformat_user (input,
2698 unformat_vnet_sw_interface, vnm, &sw_if_index))
2702 /* look up the radv_t information for this interface */
2703 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2705 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2709 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2710 ip6_radv_t * radv_info;
2711 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2713 vlib_cli_output (vm, "%U is admin %s\n", format_vnet_sw_interface_name, vnm,
2714 vnet_get_sw_interface (vnm, sw_if_index),
2715 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ? "up" : "down"));
2718 u32 *link_scope = 0, *global_scope = 0;
2719 u32 *local_scope = 0, *unknown_scope = 0;
2720 ip_interface_address_t * a;
2722 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
2723 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
2725 while (ai != (u32)~0)
2727 a = pool_elt_at_index(lm->if_address_pool, ai);
2728 ip6_address_t * address = ip_interface_address_get_address (lm, a);
2730 if (ip6_address_is_link_local_unicast (address))
2731 vec_add1 (link_scope, ai);
2732 else if(ip6_address_is_global_unicast (address))
2733 vec_add1 (global_scope, ai);
2734 else if(ip6_address_is_local_unicast (address))
2735 vec_add1 (local_scope, ai);
2737 vec_add1 (unknown_scope, ai);
2739 ai = a->next_this_sw_interface;
2742 if (vec_len (link_scope))
2744 vlib_cli_output (vm, "\tLink-local address(es):\n");
2745 ip6_print_addrs (vm, link_scope);
2746 vec_free (link_scope);
2749 if (vec_len (local_scope))
2751 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
2752 ip6_print_addrs (vm, local_scope);
2753 vec_free (local_scope);
2756 if (vec_len (global_scope))
2758 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
2759 ip6_print_addrs (vm, global_scope);
2760 vec_free (global_scope);
2763 if (vec_len (unknown_scope))
2765 vlib_cli_output (vm, "\tOther-scope address(es):\n");
2766 ip6_print_addrs (vm, unknown_scope);
2767 vec_free (unknown_scope);
2770 vlib_cli_output (vm, "\tJoined group address(es):\n");
2771 ip6_mldp_group_t *m;
2772 pool_foreach (m, radv_info->mldp_group_pool, ({
2773 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address, &m->mcast_address);
2776 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
2777 ip6_radv_prefix_t * p;
2778 pool_foreach (p, radv_info->adv_prefixes_pool, ({
2779 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
2780 format_ip6_address, &p->prefix, p->prefix_len);
2783 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
2784 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
2785 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
2786 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
2787 vlib_cli_output (vm, "\tND DAD is disabled\n");
2788 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
2789 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
2790 radv_info->adv_neighbor_reachable_time_in_msec);
2791 vlib_cli_output (vm, "\tND advertised retransmit interval is %d (msec)\n",
2792 radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations);
2794 u32 ra_interval = radv_info->max_radv_interval;
2795 u32 ra_interval_min = radv_info->min_radv_interval;
2796 vlib_cli_output (vm, "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
2797 ra_interval, ra_interval_min);
2798 vlib_cli_output (vm, "\tND router advertisements live for %d seconds\n",
2799 radv_info->adv_router_lifetime_in_sec);
2800 vlib_cli_output (vm, "\tHosts %s stateless autoconfig for addresses\n",
2801 (radv_info->adv_managed_flag) ? "use" :" don't use");
2802 vlib_cli_output (vm, "\tND router advertisements sent %d\n", radv_info->n_advertisements_sent);
2803 vlib_cli_output (vm, "\tND router solicitations received %d\n", radv_info->n_solicitations_rcvd);
2804 vlib_cli_output (vm, "\tND router solicitations dropped %d\n", radv_info->n_solicitations_dropped);
2808 error = clib_error_return (0, "IPv6 not enabled on interface",
2809 format_unformat_error, input);
2816 VLIB_CLI_COMMAND (show_ip6_interface_command, static) = {
2817 .path = "show ip6 interface",
2818 .function = show_ip6_interface_cmd,
2819 .short_help = "show ip6 interface <iface name>",
2823 disable_ip6_interface(vlib_main_t * vm,
2826 clib_error_t * error = 0;
2827 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2830 /* look up the radv_t information for this interface */
2831 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2832 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2834 /* if not created - do nothing */
2837 vnet_main_t * vnm = vnet_get_main();
2838 ip6_radv_t * radv_info;
2840 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2842 /* check radv_info ref count for other ip6 addresses on this interface */
2843 if(radv_info->ref_count == 0 )
2845 /* essentially "disables" ipv6 on this interface */
2846 error = ip6_add_del_interface_address (vm, sw_if_index,
2847 &radv_info->link_local_address,
2848 radv_info->link_local_prefix_len,
2851 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, 0/* is_add */);
2858 ip6_interface_enabled(vlib_main_t * vm,
2861 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2864 /* look up the radv_t information for this interface */
2865 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2867 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2873 enable_ip6_interface(vlib_main_t * vm,
2876 clib_error_t * error = 0;
2877 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2881 /* look up the radv_t information for this interface */
2882 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2884 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2886 /* if not created yet */
2889 vnet_main_t * vnm = vnet_get_main();
2890 vnet_sw_interface_t * sw_if0;
2892 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2893 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2895 ethernet_interface_t * eth_if0;
2897 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2900 /* create radv_info. for this interface. This holds all the info needed for router adverts */
2901 ri = ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
2905 ip6_radv_t * radv_info;
2906 ip6_address_t link_local_address;
2908 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2910 ip6_link_local_address_from_ethernet_mac_address (&link_local_address,
2913 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
2914 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB)
2916 /* make up an interface id */
2920 link_local_address.as_u64[0] = radv_info->randomizer;
2923 md5_add (&m, &link_local_address, 16);
2924 md5_finish (&m, digest);
2926 clib_memcpy(&link_local_address, digest, 16);
2928 radv_info->randomizer = link_local_address.as_u64[0];
2930 link_local_address.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2932 link_local_address.as_u8[8] &= 0xfd;
2935 /* essentially "enables" ipv6 on this interface */
2936 error = ip6_add_del_interface_address (vm, sw_if_index,
2937 &link_local_address, 64 /* address width */,
2941 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, !is_add);
2944 radv_info->link_local_address = link_local_address;
2945 radv_info->link_local_prefix_len = 64;
2954 static clib_error_t *
2955 enable_ip6_interface_cmd (vlib_main_t * vm,
2956 unformat_input_t * input,
2957 vlib_cli_command_t * cmd)
2959 vnet_main_t * vnm = vnet_get_main();
2960 clib_error_t * error = 0;
2965 if (unformat_user (input,
2966 unformat_vnet_sw_interface, vnm, &sw_if_index))
2968 enable_ip6_interface(vm, sw_if_index);
2972 error = clib_error_return (0, "unknown interface\n'",
2973 format_unformat_error, input);
2979 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) = {
2980 .path = "enable ip6 interface",
2981 .function = enable_ip6_interface_cmd,
2982 .short_help = "enable ip6 interface <iface name>",
2985 static clib_error_t *
2986 disable_ip6_interface_cmd (vlib_main_t * vm,
2987 unformat_input_t * input,
2988 vlib_cli_command_t * cmd)
2990 vnet_main_t * vnm = vnet_get_main();
2991 clib_error_t * error = 0;
2996 if (unformat_user (input,
2997 unformat_vnet_sw_interface, vnm, &sw_if_index))
2999 error = disable_ip6_interface(vm, sw_if_index);
3003 error = clib_error_return (0, "unknown interface\n'",
3004 format_unformat_error, input);
3010 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) = {
3011 .path = "disable ip6 interface",
3012 .function = disable_ip6_interface_cmd,
3013 .short_help = "disable ip6 interface <iface name>",
3016 VLIB_CLI_COMMAND (ip6_nd_command, static) = {
3018 .short_help = "Set ip6 neighbor discovery parameters",
3019 .function = ip6_neighbor_cmd,
3023 set_ip6_link_local_address(vlib_main_t * vm,
3025 ip6_address_t *address,
3028 clib_error_t * error = 0;
3029 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3031 ip6_radv_t * radv_info;
3032 vnet_main_t * vnm = vnet_get_main();
3034 if( !ip6_address_is_link_local_unicast (address))
3036 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
3037 return(error = clib_error_return (0, "address not link-local",
3038 format_unformat_error));
3041 /* call enable ipv6 */
3042 enable_ip6_interface(vm, sw_if_index);
3044 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3048 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3050 /* save if link local address (overwrite default) */
3052 /* delete the old one */
3053 error = ip6_add_del_interface_address (vm, sw_if_index,
3054 &radv_info->link_local_address,
3055 radv_info->link_local_prefix_len /* address width */,
3060 /* add the new one */
3061 error = ip6_add_del_interface_address (vm, sw_if_index,
3063 address_length /* address width */,
3068 radv_info->link_local_address = *address;
3069 radv_info->link_local_prefix_len = address_length;
3075 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3076 error = clib_error_return (0, "ip6 not enabled for interface",
3077 format_unformat_error);
3083 set_ip6_link_local_address_cmd (vlib_main_t * vm,
3084 unformat_input_t * input,
3085 vlib_cli_command_t * cmd)
3087 vnet_main_t * vnm = vnet_get_main();
3088 clib_error_t * error = 0;
3090 ip6_address_t ip6_addr;
3093 if (unformat_user (input,
3094 unformat_vnet_sw_interface, vnm, &sw_if_index))
3096 /* get the rest of the command */
3097 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3099 if(unformat (input, "%U/%d",
3100 unformat_ip6_address, &ip6_addr,
3104 return(unformat_parse_error (input));
3107 error = set_ip6_link_local_address(vm,
3114 VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) = {
3115 .path = "set ip6 link-local address",
3116 .short_help = "Set ip6 interface link-local address <intfc> <address.>",
3117 .function = set_ip6_link_local_address_cmd,
3120 /* callback when an interface address is added or deleted */
3122 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3125 ip6_address_t * address,
3127 u32 if_address_index,
3130 vnet_main_t * vnm = vnet_get_main();
3131 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3133 vlib_main_t * vm = vnm->vlib_main;
3134 ip6_radv_t * radv_info;
3136 ip6_mldp_group_t *mcast_group_info;
3138 /* create solicited node multicast address for this interface adddress */
3139 ip6_set_solicited_node_multicast_address (&a, 0);
3141 a.as_u8[0xd] = address->as_u8[0xd];
3142 a.as_u8[0xe] = address->as_u8[0xe];
3143 a.as_u8[0xf] = address->as_u8[0xf];
3147 /* try to create radv_info - does nothing if ipv6 already enabled */
3148 enable_ip6_interface(vm, sw_if_index);
3150 /* look up the radv_t information for this interface */
3151 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
3152 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3156 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3159 if( !ip6_address_is_link_local_unicast (address))
3160 radv_info->ref_count++;
3162 /* lookup prefix info for this address on this interface */
3163 uword * p = mhash_get (&radv_info->address_to_mldp_index, &a);
3164 mcast_group_info = p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3166 /* add -solicted node multicast address */
3167 if(!mcast_group_info)
3171 pool_get (radv_info->mldp_group_pool, mcast_group_info);
3173 mi = mcast_group_info - radv_info->mldp_group_pool;
3174 mhash_set (&radv_info->address_to_mldp_index, &a, mi, /* old_value */ 0);
3176 mcast_group_info->type = 4;
3177 mcast_group_info->mcast_source_address_pool = 0;
3178 mcast_group_info->num_sources = 0;
3179 clib_memcpy(&mcast_group_info->mcast_address, &a, sizeof(ip6_address_t));
3187 /* look up the radv_t information for this interface */
3188 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
3189 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3193 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3195 /* lookup prefix info for this address on this interface */
3196 uword * p = mhash_get (&radv_info->address_to_mldp_index, &a);
3197 mcast_group_info = p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3199 if(mcast_group_info)
3201 mhash_unset (&radv_info->address_to_mldp_index, &a,/* old_value */ 0);
3202 pool_put (radv_info->mldp_group_pool, mcast_group_info);
3205 /* if interface up send MLDP "report" */
3206 radv_info->all_routers_mcast = 0;
3209 if( !ip6_address_is_link_local_unicast (address))
3210 radv_info->ref_count--;
3215 clib_error_t *ip6_set_neighbor_limit (u32 neighbor_limit)
3217 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3219 nm->limit_neighbor_cache_size = neighbor_limit;
3225 ip6_neighbor_entry_del_adj(ip6_neighbor_t *n, u32 adj_index)
3231 vec_foreach_index(i, n->adjacencies)
3232 if (vec_elt(n->adjacencies, i) == adj_index)
3234 vec_del1(n->adjacencies, i);
3242 ip6_neighbor_entry_add_adj(ip6_neighbor_t *n, u32 adj_index)
3245 vec_foreach_index(i, n->adjacencies)
3246 if (vec_elt(n->adjacencies, i) == adj_index)
3248 vec_add1(n->adjacencies, adj_index);
3252 ip6_neighbor_add_del_adj_cb (struct ip_lookup_main_t * lm,
3254 ip_adjacency_t * adj,
3257 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3258 ip6_neighbor_key_t k;
3259 ip6_neighbor_t *n = 0;
3263 for(ai = adj->heap_handle; ai < adj->heap_handle + adj->n_adj ; ai++)
3265 adj = ip_get_adjacency (lm, ai);
3266 if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
3267 (adj->arp.next_hop.ip6.as_u64[0] || adj->arp.next_hop.ip6.as_u64[1]))
3269 k.sw_if_index = adj->rewrite_header.sw_if_index;
3270 k.ip6_address.as_u64[0] = adj->arp.next_hop.ip6.as_u64[0];
3271 k.ip6_address.as_u64[1] = adj->arp.next_hop.ip6.as_u64[1];
3273 p = mhash_get (&nm->neighbor_index_by_key, &k);
3275 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
3283 clib_warning("Adjacency contains unknown ND next hop %U (del)",
3284 format_ip46_address, &adj->arp.next_hop, IP46_TYPE_IP6);
3286 ip6_neighbor_entry_del_adj(n, adj->heap_handle);
3291 clib_warning("Adjacency contains unknown ND next hop %U (add)",
3292 format_ip46_address, &adj->arp.next_hop, IP46_TYPE_IP6);
3294 ip6_neighbor_entry_add_adj(n, adj->heap_handle);
3299 static clib_error_t * ip6_neighbor_init (vlib_main_t * vm)
3301 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3302 ip6_main_t * im = &ip6_main;
3303 ip_lookup_main_t * lm = &im->lookup_main;
3305 mhash_init (&nm->neighbor_index_by_key,
3306 /* value size */ sizeof (uword),
3307 /* key size */ sizeof (ip6_neighbor_key_t));
3309 icmp6_register_type (vm, ICMP6_neighbor_solicitation, ip6_icmp_neighbor_solicitation_node.index);
3310 icmp6_register_type (vm, ICMP6_neighbor_advertisement, ip6_icmp_neighbor_advertisement_node.index);
3311 icmp6_register_type (vm, ICMP6_router_solicitation, ip6_icmp_router_solicitation_node.index);
3312 icmp6_register_type (vm, ICMP6_router_advertisement, ip6_icmp_router_advertisement_node.index);
3314 /* handler node for ip6 neighbor discovery events and timers */
3315 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3317 /* add call backs */
3318 ip6_add_del_interface_address_callback_t cb;
3319 memset(&cb, 0x0, sizeof(ip6_add_del_interface_address_callback_t));
3321 /* when an interface address changes... */
3322 cb.function = ip6_neighbor_add_del_interface_address;
3323 cb.function_opaque = 0;
3324 vec_add1 (im->add_del_interface_address_callbacks, cb);
3326 mhash_init (&nm->pending_resolutions_by_address,
3327 /* value size */ sizeof (uword),
3328 /* key size */ sizeof (ip6_address_t));
3330 /* default, configurable */
3331 nm->limit_neighbor_cache_size = 50000;
3334 /* $$$$ Hack fix for today */
3335 vec_validate_init_empty
3336 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */);
3339 ip_register_add_del_adjacency_callback(lm, ip6_neighbor_add_del_adj_cb);
3344 VLIB_INIT_FUNCTION (ip6_neighbor_init);
3347 void vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
3353 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3354 ip6_address_t * address = address_arg;
3356 pending_resolution_t * pr;
3358 pool_get (nm->pending_resolutions, pr);
3360 pr->next_index = ~0;
3361 pr->node_index = node_index;
3362 pr->type_opaque = type_opaque;
3365 p = mhash_get (&nm->pending_resolutions_by_address, address);
3368 /* Insert new resolution at the head of the list */
3369 pr->next_index = p[0];
3370 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
3373 mhash_set (&nm->pending_resolutions_by_address, address,
3374 pr - nm->pending_resolutions, 0 /* old value */);