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])
397 u32 * adjs = vec_dup(n->adjacencies);
398 /* Update all adj assigned to this arp entry */
399 vec_foreach(ai, adjs)
402 ip_adjacency_t * uadj = ip_get_adjacency(lm, *ai);
403 for (i = 0; i < uadj->n_adj; i++)
404 if (uadj[i].lookup_next_index == IP_LOOKUP_NEXT_ARP &&
405 uadj[i].arp.next_hop.ip6.as_u64[0] == a->as_u64[0] &&
406 uadj[i].arp.next_hop.ip6.as_u64[1] == a->as_u64[1])
407 ip_update_adjacency (lm, *ai + i, &adj);
414 args.table_index_or_table_id = im->fib_index_by_sw_if_index[sw_if_index];
415 args.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_ADD | IP6_ROUTE_FLAG_NEIGHBOR;
416 args.dst_address = a[0];
417 args.dst_address_length = 128;
421 ip6_add_del_route (im, &args);
424 if (make_new_nd_cache_entry) {
425 pool_get (nm->neighbor_pool, n);
426 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
431 /* Update time stamp and ethernet address. */
432 clib_memcpy (n->link_layer_address, link_layer_address, n_bytes_link_layer_address);
433 n->cpu_time_last_updated = clib_cpu_time_now ();
435 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
437 /* Customer(s) waiting for this address to be resolved? */
438 p = mhash_get (&nm->pending_resolutions_by_address, a);
444 while (next_index != (u32)~0)
446 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
447 vlib_process_signal_event (vm, pr->node_index,
450 next_index = pr->next_index;
451 pool_put (nm->pending_resolutions, pr);
454 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
457 vlib_worker_thread_barrier_release(vm);
462 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
465 u8 * link_layer_address,
466 uword n_bytes_link_layer_address)
468 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
469 ip6_neighbor_key_t k;
471 ip6_main_t * im = &ip6_main;
472 ip6_add_del_route_args_t args;
477 if (os_get_cpu_number())
479 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
485 k.sw_if_index = sw_if_index;
486 k.ip6_address = a[0];
489 vlib_worker_thread_barrier_sync (vm);
491 p = mhash_get (&nm->neighbor_index_by_key, &k);
498 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
499 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
500 pool_put (nm->neighbor_pool, n);
502 args.table_index_or_table_id = im->fib_index_by_sw_if_index[sw_if_index];
503 args.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_DEL
504 | IP6_ROUTE_FLAG_NEIGHBOR;
505 args.dst_address = a[0];
506 args.dst_address_length = 128;
510 ip6_add_del_route (im, &args);
512 vlib_worker_thread_barrier_release(vm);
518 vnet_ip6_neighbor_glean_add(u32 fib_index, void * next_hop_arg)
520 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
521 ip6_main_t * im = &ip6_main;
522 ip_lookup_main_t * lm = &im->lookup_main;
523 ip6_address_t * next_hop = next_hop_arg;
524 ip_adjacency_t add_adj, *adj;
525 ip6_add_del_route_args_t args;
527 ip6_neighbor_key_t k;
530 adj_index = ip6_fib_lookup_with_table(im, fib_index, next_hop);
531 adj = ip_get_adjacency(lm, adj_index);
533 if (!adj || adj->lookup_next_index != IP_LOOKUP_NEXT_ARP)
536 if (adj->arp.next_hop.ip6.as_u64[0] ||
537 adj->arp.next_hop.ip6.as_u64[1])
540 k.sw_if_index = adj->rewrite_header.sw_if_index;
541 k.ip6_address = *next_hop;
543 if (mhash_get (&nm->neighbor_index_by_key, &k))
546 pool_get (nm->neighbor_pool, n);
547 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool, /* old value */ 0);
549 n->cpu_time_last_updated = clib_cpu_time_now ();
550 n->flags = IP6_NEIGHBOR_FLAG_GLEAN;
552 memset(&args, 0, sizeof(args));
553 memcpy(&add_adj, adj, sizeof(add_adj));
554 add_adj.arp.next_hop.ip6 = *next_hop; /* install neighbor /128 route */
555 args.table_index_or_table_id = fib_index;
556 args.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_ADD | IP6_ROUTE_FLAG_NEIGHBOR;
557 args.dst_address = *next_hop;
558 args.dst_address_length = 128;
560 args.add_adj = &add_adj;
562 ip6_add_del_route (im, &args);
563 return ip6_fib_lookup_with_table (im, fib_index, next_hop);
567 static void ip6_neighbor_set_unset_rpc_callback
568 ( ip6_neighbor_set_unset_rpc_args_t * a)
570 vlib_main_t * vm = vlib_get_main();
572 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
573 a->link_layer_address, 6, a->is_static);
575 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
576 a->link_layer_address, 6);
581 ip6_neighbor_sort (void *a1, void *a2)
583 vnet_main_t * vnm = vnet_get_main();
584 ip6_neighbor_t * n1 = a1, * n2 = a2;
586 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
587 n2->key.sw_if_index);
589 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
593 static clib_error_t *
594 show_ip6_neighbors (vlib_main_t * vm,
595 unformat_input_t * input,
596 vlib_cli_command_t * cmd)
598 vnet_main_t * vnm = vnet_get_main();
599 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
600 ip6_neighbor_t * n, * ns;
601 clib_error_t * error = 0;
604 /* Filter entries by interface if given. */
606 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
609 pool_foreach (n, nm->neighbor_pool, ({ vec_add1 (ns, n[0]); }));
610 vec_sort_with_function (ns, ip6_neighbor_sort);
611 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
612 vec_foreach (n, ns) {
613 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
615 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
622 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
623 .path = "show ip6 neighbors",
624 .function = show_ip6_neighbors,
625 .short_help = "Show ip6 neighbors",
628 static clib_error_t *
629 set_ip6_neighbor (vlib_main_t * vm,
630 unformat_input_t * input,
631 vlib_cli_command_t * cmd)
633 vnet_main_t * vnm = vnet_get_main();
641 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
643 /* intfc, ip6-address, mac-address */
644 if (unformat (input, "%U %U %U",
645 unformat_vnet_sw_interface, vnm, &sw_if_index,
646 unformat_ip6_address, &addr,
647 unformat_ethernet_address, mac_address))
650 else if (unformat (input, "delete") || unformat (input, "del"))
652 else if (unformat (input, "static"))
659 return clib_error_return (0, "Missing interface, ip6 or hw address");
662 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
663 mac_address, sizeof(mac_address), is_static);
665 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
666 mac_address, sizeof(mac_address));
670 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) = {
671 .path = "set ip6 neighbor",
672 .function = set_ip6_neighbor,
673 .short_help = "set ip6 neighbor [del] <intfc> <ip6-address> <mac-address> [static]",
677 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
678 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
679 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
680 } icmp6_neighbor_solicitation_or_advertisement_next_t;
682 static_always_inline uword
683 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
684 vlib_node_runtime_t * node,
685 vlib_frame_t * frame,
686 uword is_solicitation)
688 vnet_main_t * vnm = vnet_get_main();
689 ip6_main_t * im = &ip6_main;
690 ip_lookup_main_t * lm = &im->lookup_main;
691 uword n_packets = frame->n_vectors;
692 u32 * from, * to_next;
693 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
694 icmp6_neighbor_discovery_option_type_t option_type;
695 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
698 from = vlib_frame_vector_args (frame);
699 n_left_from = n_packets;
700 next_index = node->cached_next_index;
702 if (node->flags & VLIB_NODE_FLAG_TRACE)
703 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
705 sizeof (icmp6_input_trace_t));
709 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
710 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
711 n_advertisements_sent = 0;
713 while (n_left_from > 0)
715 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
717 while (n_left_from > 0 && n_left_to_next > 0)
721 icmp6_neighbor_solicitation_or_advertisement_header_t * h0;
722 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * o0;
723 u32 bi0, options_len0, sw_if_index0, next0, error0;
724 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
728 bi0 = to_next[0] = from[0];
735 p0 = vlib_get_buffer (vm, bi0);
736 ip0 = vlib_buffer_get_current (p0);
737 h0 = ip6_next_header (ip0);
738 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
740 error0 = ICMP6_ERROR_NONE;
741 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
742 ip6_sadd_link_local = ip6_address_is_link_local_unicast(&ip0->src_address);
743 ip6_sadd_unspecified = ip6_address_is_unspecified (&ip0->src_address);
745 /* Check that source address is unspecified, link-local or else on-link. */
746 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
748 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
749 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, src_adj_index0);
751 /* Allow all realistic-looking rewrite adjacencies to pass */
752 ni0 = adj0->lookup_next_index;
753 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
754 (ni0 < IP6_LOOKUP_N_NEXT);
756 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
758 ? ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
762 o0 = (void *) (h0 + 1);
763 o0 = ((options_len0 == 8 && o0->header.type == option_type
764 && o0->header.n_data_u64s == 1) ? o0 : 0);
766 /* If src address unspecified or link local, donot learn neighbor MAC */
767 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
768 !ip6_sadd_unspecified && !ip6_sadd_link_local))
770 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
771 if (nm->limit_neighbor_cache_size &&
772 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
773 unset_random_neighbor_entry();
774 vnet_set_ip6_ethernet_neighbor (
776 is_solicitation ? &ip0->src_address : &h0->target_address,
777 o0->ethernet_address, sizeof (o0->ethernet_address), 0);
780 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
782 /* Check that target address is one that we know about. */
783 ip_interface_address_t * ia0;
784 ip6_address_fib_t ip6_af0;
787 ip6_addr_fib_init (&ip6_af0, &h0->target_address,
788 vec_elt (im->fib_index_by_sw_if_index,
791 /* Gross kludge, "thank you" MJ, don't even ask */
792 oldheap = clib_mem_set_heap (clib_per_cpu_mheaps[0]);
793 ia0 = ip_get_interface_address (lm, &ip6_af0);
794 clib_mem_set_heap (oldheap);
796 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN : error0;
800 next0 = (error0 != ICMP6_ERROR_NONE
801 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
802 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
806 error0 = error0 == ICMP6_ERROR_NONE ?
807 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
810 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
812 vnet_sw_interface_t * sw_if0;
813 ethernet_interface_t * eth_if0;
814 ethernet_header_t *eth0;
816 /* dst address is either source address or the all-nodes mcast addr */
817 if(!ip6_sadd_unspecified)
818 ip0->dst_address = ip0->src_address;
820 ip6_set_reserved_multicast_address(&ip0->dst_address,
821 IP6_MULTICAST_SCOPE_link_local,
822 IP6_MULTICAST_GROUP_ID_all_hosts);
824 ip0->src_address = h0->target_address;
825 ip0->hop_limit = 255;
826 h0->icmp.type = ICMP6_neighbor_advertisement;
828 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
829 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
830 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
833 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
835 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
838 h0->advertisement_flags = clib_host_to_net_u32
839 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
840 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
842 h0->icmp.checksum = 0;
844 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
846 ASSERT(bogus_length == 0);
848 /* Reuse current MAC header, copy SMAC to DMAC and
849 * interface MAC to SMAC */
850 vlib_buffer_advance(p0, - ethernet_buffer_header_size(p0));
851 eth0 = vlib_buffer_get_current(p0);
852 clib_memcpy(eth0->dst_address, eth0->src_address, 6);
853 clib_memcpy(eth0->src_address, eth_if0->address, 6);
855 /* Setup input and output sw_if_index for packet */
856 ASSERT(vnet_buffer(p0)->sw_if_index[VLIB_RX] == sw_if_index0);
857 vnet_buffer(p0)->sw_if_index[VLIB_TX] = sw_if_index0;
858 vnet_buffer(p0)->sw_if_index[VLIB_RX] =
859 vnet_main.local_interface_sw_if_index;
861 n_advertisements_sent++;
864 p0->error = error_node->errors[error0];
866 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
867 to_next, n_left_to_next,
871 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
874 /* Account for advertisements sent. */
875 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, n_advertisements_sent);
877 return frame->n_vectors;
880 /* for "syslogging" - use elog for now */
881 #define foreach_log_level \
883 _ (INFO, "INFORMATION") \
884 _ (NOTICE, "NOTICE") \
885 _ (WARNING, "WARNING") \
887 _ (CRIT, "CRITICAL") \
889 _ (EMERG, "EMERGENCY")
892 #define _(f,s) LOG_##f,
897 static char * log_level_strings[] = {
903 static int logmask = 1 << LOG_DEBUG;
906 ip6_neighbor_syslog(vlib_main_t *vm, int priority, char * fmt, ...)
908 /* just use elog for now */
912 if( (priority > LOG_EMERG) ||
913 !(logmask & (1 << priority)))
919 what = va_format (0, fmt, &va);
921 ELOG_TYPE_DECLARE (e) = {
922 .format = "ip6 nd: (%s): %s",
923 .format_args = "T4T4",
925 struct { u32 s[2]; } * ed;
926 ed = ELOG_DATA (&vm->elog_main, e);
927 ed->s[0] = elog_string(&vm->elog_main, log_level_strings[priority]);
928 ed->s[1] = elog_string(&vm->elog_main, (char *)what);
934 /* ipv6 neighbor discovery - router advertisements */
936 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
937 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
938 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
939 ICMP6_ROUTER_SOLICITATION_N_NEXT,
940 } icmp6_router_solicitation_or_advertisement_next_t;
942 static_always_inline uword
943 icmp6_router_solicitation(vlib_main_t * vm,
944 vlib_node_runtime_t * node,
945 vlib_frame_t * frame)
947 vnet_main_t * vnm = vnet_get_main();
948 ip6_main_t * im = &ip6_main;
949 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
950 uword n_packets = frame->n_vectors;
951 u32 * from, * to_next;
952 u32 n_left_from, n_left_to_next, next_index;
953 u32 n_advertisements_sent = 0;
956 icmp6_neighbor_discovery_option_type_t option_type;
958 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
960 from = vlib_frame_vector_args (frame);
961 n_left_from = n_packets;
962 next_index = node->cached_next_index;
964 if (node->flags & VLIB_NODE_FLAG_TRACE)
965 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
967 sizeof (icmp6_input_trace_t));
969 /* source may append his LL address */
970 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
972 while (n_left_from > 0)
974 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
976 while (n_left_from > 0 && n_left_to_next > 0)
980 ip6_radv_t *radv_info = 0;
982 icmp6_neighbor_discovery_header_t * h0;
983 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * o0;
985 u32 bi0, options_len0, sw_if_index0, next0, error0;
986 u32 is_solicitation = 1, is_dropped = 0;
987 u32 is_unspecified, is_link_local;
989 bi0 = to_next[0] = from[0];
996 p0 = vlib_get_buffer (vm, bi0);
997 ip0 = vlib_buffer_get_current (p0);
998 h0 = ip6_next_header (ip0);
999 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1000 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1001 is_link_local = ip6_address_is_link_local_unicast (&ip0->src_address);
1003 error0 = ICMP6_ERROR_NONE;
1004 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1006 /* check if solicitation (not from nd_timer node) */
1007 if (ip6_address_is_unspecified (&ip0->dst_address))
1008 is_solicitation = 0;
1010 /* Check that source address is unspecified, link-local or else on-link. */
1011 if (!is_unspecified && !is_link_local)
1013 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1014 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, src_adj_index0);
1016 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1017 || (adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP
1018 && adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE))
1019 ? ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1023 /* check for source LL option and process */
1024 o0 = (void *) (h0 + 1);
1025 o0 = ((options_len0 == 8
1026 && o0->header.type == option_type
1027 && o0->header.n_data_u64s == 1)
1031 /* if src address unspecified IGNORE any options */
1032 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1033 !is_unspecified && !is_link_local)) {
1034 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1035 if (nm->limit_neighbor_cache_size &&
1036 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
1037 unset_random_neighbor_entry();
1039 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1041 o0->ethernet_address,
1042 sizeof (o0->ethernet_address), 0);
1045 /* default is to drop */
1046 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1048 if (error0 == ICMP6_ERROR_NONE)
1050 vnet_sw_interface_t * sw_if0;
1051 ethernet_interface_t * eth_if0;
1054 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1055 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1056 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1058 /* only support ethernet interface type for now */
1059 error0 = (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF : error0;
1061 if (error0 == ICMP6_ERROR_NONE)
1065 /* adjust the sizeof the buffer to just include the ipv6 header */
1066 p0->current_length -= (options_len0 + sizeof(icmp6_neighbor_discovery_header_t));
1068 /* look up the radv_t information for this interface */
1069 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1071 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1074 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1076 error0 = ((!radv_info) ? ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : error0);
1078 if (error0 == ICMP6_ERROR_NONE)
1080 f64 now = vlib_time_now (vm);
1082 /* for solicited adverts - need to rate limit */
1085 if( (now - radv_info->last_radv_time) < MIN_DELAY_BETWEEN_RAS )
1088 radv_info->last_radv_time = now;
1092 icmp6_router_advertisement_header_t rh;
1094 rh.icmp.type = ICMP6_router_advertisement;
1096 rh.icmp.checksum = 0;
1098 rh.current_hop_limit = radv_info->curr_hop_limit;
1099 rh.router_lifetime_in_sec = clib_host_to_net_u16(radv_info->adv_router_lifetime_in_sec);
1100 rh.time_in_msec_between_retransmitted_neighbor_solicitations =
1101 clib_host_to_net_u32(radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1102 rh.neighbor_reachable_time_in_msec =
1103 clib_host_to_net_u32(radv_info->adv_neighbor_reachable_time_in_msec);
1105 rh.flags = (radv_info->adv_managed_flag) ? ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP : 0;
1106 rh.flags |= ( (radv_info->adv_other_flag) ? ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP : 0);
1109 u16 payload_length = sizeof(icmp6_router_advertisement_header_t);
1111 vlib_buffer_add_data (vm,
1112 p0->free_list_index,
1114 (void *)&rh, sizeof(icmp6_router_advertisement_header_t));
1116 if(radv_info->adv_link_layer_address)
1118 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t h;
1120 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1121 h.header.n_data_u64s = 1;
1123 /* copy ll address */
1124 clib_memcpy(&h.ethernet_address[0], eth_if0->address, 6);
1126 vlib_buffer_add_data (vm,
1127 p0->free_list_index,
1129 (void *)&h, sizeof(icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
1131 payload_length += sizeof(icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1134 /* add MTU option */
1135 if(radv_info->adv_link_mtu)
1137 icmp6_neighbor_discovery_mtu_option_t h;
1140 h.mtu = clib_host_to_net_u32(radv_info->adv_link_mtu);
1141 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1142 h.header.n_data_u64s = 1;
1144 payload_length += sizeof( icmp6_neighbor_discovery_mtu_option_t);
1146 vlib_buffer_add_data (vm,
1147 p0->free_list_index,
1149 (void *)&h, sizeof(icmp6_neighbor_discovery_mtu_option_t));
1152 /* add advertised prefix options */
1153 ip6_radv_prefix_t *pr_info;
1155 pool_foreach (pr_info, radv_info->adv_prefixes_pool, ({
1157 if(pr_info->enabled &&
1158 (!pr_info->decrement_lifetime_flag || (pr_info->pref_lifetime_expires >0)))
1160 /* advertise this prefix */
1161 icmp6_neighbor_discovery_prefix_information_option_t h;
1163 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1164 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1166 h.dst_address_length = pr_info->prefix_len;
1168 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1169 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
1171 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1173 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1174 h.preferred_time = 0;
1178 if(pr_info->decrement_lifetime_flag)
1180 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1181 (pr_info->valid_lifetime_expires - now) : 0;
1183 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1184 (pr_info->pref_lifetime_expires - now) : 0;
1187 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1188 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1192 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1194 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1196 vlib_buffer_add_data (vm,
1197 p0->free_list_index,
1199 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1204 /* add additional options before here */
1206 /* finish building the router advertisement... */
1207 if(!is_unspecified && radv_info->send_unicast)
1209 ip0->dst_address = ip0->src_address;
1213 /* target address is all-nodes mcast addr */
1214 ip6_set_reserved_multicast_address(&ip0->dst_address,
1215 IP6_MULTICAST_SCOPE_link_local,
1216 IP6_MULTICAST_GROUP_ID_all_hosts);
1219 /* source address MUST be the link-local address */
1220 ip0->src_address = radv_info->link_local_address;
1222 ip0->hop_limit = 255;
1223 ip0->payload_length = clib_host_to_net_u16 (payload_length);
1225 icmp6_router_advertisement_header_t * rh0 = (icmp6_router_advertisement_header_t *)(ip0 + 1);
1226 rh0->icmp.checksum =
1227 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1229 ASSERT(bogus_length == 0);
1231 /* setup output if and adjacency */
1232 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1233 vnet_main.local_interface_sw_if_index;
1235 if (is_solicitation)
1237 ethernet_header_t *eth0;
1238 /* Reuse current MAC header, copy SMAC to DMAC and
1239 * interface MAC to SMAC */
1240 vlib_buffer_reset (p0);
1241 eth0 = vlib_buffer_get_current(p0);
1242 clib_memcpy(eth0->dst_address, eth0->src_address, 6);
1243 clib_memcpy(eth0->src_address, eth_if0->address, 6);
1244 next0 = is_dropped ?
1245 next0 : ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1246 vnet_buffer(p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1250 adj_index0 = radv_info->all_nodes_adj_index;
1251 if (adj_index0 == 0)
1252 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1255 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, adj_index0);
1257 ((adj0->rewrite_header.sw_if_index != sw_if_index0
1258 || adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
1259 ? ICMP6_ERROR_ROUTER_SOLICITATION_DEST_UNKNOWN
1261 next0 = is_dropped ?
1262 next0 : ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1263 vnet_buffer (p0)->ip.adj_index[VLIB_RX] = adj_index0;
1267 radv_info->n_solicitations_dropped += is_dropped;
1268 radv_info->n_solicitations_rcvd += is_solicitation;
1270 if((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1272 radv_info->n_advertisements_sent++;
1273 n_advertisements_sent++;
1279 p0->error = error_node->errors[error0];
1281 if(error0 != ICMP6_ERROR_NONE)
1282 vlib_error_count (vm, error_node->node_index, error0, 1);
1284 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1285 to_next, n_left_to_next,
1290 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1293 /* Account for router advertisements sent. */
1294 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX, n_advertisements_sent);
1296 return frame->n_vectors;
1299 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1300 static_always_inline uword
1301 icmp6_router_advertisement(vlib_main_t * vm,
1302 vlib_node_runtime_t * node,
1303 vlib_frame_t * frame)
1305 vnet_main_t * vnm = vnet_get_main();
1306 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1307 uword n_packets = frame->n_vectors;
1308 u32 * from, * to_next;
1309 u32 n_left_from, n_left_to_next, next_index;
1310 u32 n_advertisements_rcvd = 0;
1312 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1314 from = vlib_frame_vector_args (frame);
1315 n_left_from = n_packets;
1316 next_index = node->cached_next_index;
1318 if (node->flags & VLIB_NODE_FLAG_TRACE)
1319 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1321 sizeof (icmp6_input_trace_t));
1323 while (n_left_from > 0)
1325 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1327 while (n_left_from > 0 && n_left_to_next > 0)
1331 ip6_radv_t *radv_info = 0;
1332 icmp6_router_advertisement_header_t * h0;
1333 u32 bi0, options_len0, sw_if_index0, next0, error0;
1335 bi0 = to_next[0] = from[0];
1340 n_left_to_next -= 1;
1342 p0 = vlib_get_buffer (vm, bi0);
1343 ip0 = vlib_buffer_get_current (p0);
1344 h0 = ip6_next_header (ip0);
1345 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1347 error0 = ICMP6_ERROR_NONE;
1348 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1350 /* Check that source address is link-local*/
1351 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1352 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1354 /* default is to drop */
1355 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1357 n_advertisements_rcvd++;
1359 if (error0 == ICMP6_ERROR_NONE)
1361 vnet_sw_interface_t * sw_if0;
1362 ethernet_interface_t * eth_if0;
1364 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1365 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1366 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1368 /* only support ethernet interface type for now */
1369 error0 = (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF : error0;
1371 if (error0 == ICMP6_ERROR_NONE)
1375 /* look up the radv_t information for this interface */
1376 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1378 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1381 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1383 error0 = ((!radv_info) ? ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : error0);
1385 if (error0 == ICMP6_ERROR_NONE)
1387 /* validate advertised information */
1388 if((h0->current_hop_limit && radv_info->curr_hop_limit) &&
1389 (h0->current_hop_limit != radv_info->curr_hop_limit))
1391 ip6_neighbor_syslog(vm, LOG_WARNING,
1392 "our AdvCurHopLimit on %U doesn't agree with %U",
1393 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1396 if((h0->flags & ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP) !=
1397 radv_info->adv_managed_flag)
1399 ip6_neighbor_syslog(vm, LOG_WARNING,
1400 "our AdvManagedFlag on %U doesn't agree with %U",
1401 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1404 if((h0->flags & ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP) !=
1405 radv_info->adv_other_flag)
1407 ip6_neighbor_syslog(vm, LOG_WARNING,
1408 "our AdvOtherConfigFlag on %U doesn't agree with %U",
1409 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1412 if((h0->time_in_msec_between_retransmitted_neighbor_solicitations &&
1413 radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations) &&
1414 (h0->time_in_msec_between_retransmitted_neighbor_solicitations !=
1415 clib_host_to_net_u32(radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
1417 ip6_neighbor_syslog(vm, LOG_WARNING,
1418 "our AdvRetransTimer on %U doesn't agree with %U",
1419 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1422 if((h0->neighbor_reachable_time_in_msec &&
1423 radv_info->adv_neighbor_reachable_time_in_msec) &&
1424 (h0->neighbor_reachable_time_in_msec !=
1425 clib_host_to_net_u32(radv_info->adv_neighbor_reachable_time_in_msec)))
1427 ip6_neighbor_syslog(vm, LOG_WARNING,
1428 "our AdvReachableTime on %U doesn't agree with %U",
1429 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1432 /* check for MTU or prefix options or .. */
1433 u8 * opt_hdr = (u8 *)(h0 + 1);
1434 while( options_len0 > 0)
1436 icmp6_neighbor_discovery_option_header_t *o0 = ( icmp6_neighbor_discovery_option_header_t *)opt_hdr;
1437 int opt_len = o0->n_data_u64s << 3;
1438 icmp6_neighbor_discovery_option_type_t option_type = o0->type;
1440 if(options_len0 < 2)
1442 ip6_neighbor_syslog(vm, LOG_ERR,
1443 "malformed RA packet on %U from %U",
1444 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1450 ip6_neighbor_syslog(vm, LOG_ERR,
1451 " zero length option in RA on %U from %U",
1452 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1455 else if( opt_len > options_len0)
1457 ip6_neighbor_syslog(vm, LOG_ERR,
1458 "option length in RA packet greater than total length on %U from %U",
1459 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1463 options_len0 -= opt_len;
1468 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
1470 icmp6_neighbor_discovery_mtu_option_t *h =
1471 (icmp6_neighbor_discovery_mtu_option_t *)(o0);
1473 if(opt_len < sizeof(*h))
1476 if((h->mtu && radv_info->adv_link_mtu) &&
1477 (h->mtu != clib_host_to_net_u32(radv_info->adv_link_mtu)))
1479 ip6_neighbor_syslog(vm, LOG_WARNING,
1480 "our AdvLinkMTU on %U doesn't agree with %U",
1481 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1486 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1488 icmp6_neighbor_discovery_prefix_information_option_t *h =
1489 (icmp6_neighbor_discovery_prefix_information_option_t *)(o0);
1491 /* validate advertised prefix options */
1492 ip6_radv_prefix_t *pr_info;
1493 u32 preferred, valid;
1495 if(opt_len < sizeof(*h))
1498 preferred = clib_net_to_host_u32(h->preferred_time);
1499 valid = clib_net_to_host_u32(h->valid_time);
1501 /* look for matching prefix - if we our advertising it, it better be consistant */
1502 pool_foreach (pr_info, radv_info->adv_prefixes_pool, ({
1505 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1507 if(pr_info->enabled &&
1508 (pr_info->prefix_len == h->dst_address_length) &&
1509 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1512 if(!pr_info->decrement_lifetime_flag &&
1513 valid != pr_info->adv_valid_lifetime_in_secs)
1515 ip6_neighbor_syslog(vm, LOG_WARNING,
1516 "our ADV validlifetime on %U for %U does not agree with %U",
1517 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1518 format_ip6_address, &h->dst_address);
1520 if(!pr_info->decrement_lifetime_flag &&
1521 preferred != pr_info->adv_pref_lifetime_in_secs)
1523 ip6_neighbor_syslog(vm, LOG_WARNING,
1524 "our ADV preferredlifetime on %U for %U does not agree with %U",
1525 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1526 format_ip6_address, &h->dst_address);
1542 p0->error = error_node->errors[error0];
1544 if(error0 != ICMP6_ERROR_NONE)
1545 vlib_error_count (vm, error_node->node_index, error0, 1);
1547 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1548 to_next, n_left_to_next,
1552 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1555 /* Account for router advertisements sent. */
1556 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX, n_advertisements_rcvd);
1558 return frame->n_vectors;
1561 /* create and initialize router advertisement parameters with default values for this intfc */
1563 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
1567 ip6_main_t * im = &ip6_main;
1568 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1569 ip_lookup_main_t * lm = &im->lookup_main;
1572 vnet_sw_interface_t * sw_if0;
1573 ethernet_interface_t * eth_if0 = 0;
1575 /* lookup radv container - ethernet interfaces only */
1576 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1577 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1578 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1583 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
1584 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1588 a = pool_elt_at_index (nm->if_radv_pool, ri);
1592 u32 i, * to_delete = 0;
1593 ip6_radv_prefix_t *p;
1594 ip6_mldp_group_t *m;
1596 /* remove adjacencies */
1597 ip_del_adjacency (lm, a->all_nodes_adj_index);
1598 ip_del_adjacency (lm, a->all_routers_adj_index);
1599 ip_del_adjacency (lm, a->all_mldv2_routers_adj_index);
1601 /* clean up prefix_pool */
1602 pool_foreach (p, a->adv_prefixes_pool, ({
1603 vec_add1 (to_delete, p - a->adv_prefixes_pool);
1606 for (i = 0; i < vec_len (to_delete); i++)
1608 p = pool_elt_at_index (a->adv_prefixes_pool, to_delete[i]);
1609 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
1610 pool_put (a->adv_prefixes_pool, p);
1613 vec_free (to_delete);
1616 /* clean up mldp group pool */
1617 pool_foreach (m, a->mldp_group_pool, ({
1618 vec_add1 (to_delete, m - a->mldp_group_pool);
1621 for (i = 0; i < vec_len (to_delete); i++)
1623 m = pool_elt_at_index (a->mldp_group_pool, to_delete[i]);
1624 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
1625 pool_put (a->mldp_group_pool, m);
1628 vec_free (to_delete);
1630 pool_put (nm->if_radv_pool, a);
1631 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
1639 vnet_hw_interface_t * hw_if0;
1641 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
1643 pool_get (nm->if_radv_pool, a);
1645 ri = a - nm->if_radv_pool;
1646 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
1648 /* initialize default values (most of which are zero) */
1649 memset (a, 0, sizeof (a[0]));
1651 a->sw_if_index = sw_if_index;
1653 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
1654 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
1655 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
1656 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
1658 a->adv_link_layer_address = 1; /* send ll address source address option */
1660 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
1661 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
1662 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
1663 a->seed = random_default_seed();
1665 /* for generating random interface ids */
1666 a->randomizer = 0x1119194911191949;
1667 a->randomizer = random_u64 ((u32 *)&a->randomizer);
1669 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS ;
1670 a->initial_adverts_sent = a->initial_adverts_count-1;
1671 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1673 /* deafult is to send */
1676 /* fill in radv_info for this interface that will be needed later */
1677 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
1679 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
1681 /* fill in default link-local address (this may be overridden) */
1682 ip6_link_local_address_from_ethernet_address (&a->link_local_address, eth_if0->address);
1683 a->link_local_prefix_len = 64;
1685 mhash_init (&a->address_to_prefix_index, sizeof (uword), sizeof (ip6_address_t));
1686 mhash_init (&a->address_to_mldp_index, sizeof (uword), sizeof (ip6_address_t));
1689 ip_adjacency_t *adj;
1690 u8 link_layer_address[6] =
1691 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_all_hosts};
1693 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1694 &a->all_nodes_adj_index);
1696 adj->lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
1697 adj->if_address_index = ~0;
1699 vnet_rewrite_for_sw_interface
1701 VNET_L3_PACKET_TYPE_IP6,
1703 ip6_rewrite_node.index,
1705 &adj->rewrite_header,
1706 sizeof (adj->rewrite_data));
1710 ip_adjacency_t *adj;
1711 u8 link_layer_address[6] =
1712 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_all_routers};
1714 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1715 &a->all_routers_adj_index);
1717 adj->lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
1718 adj->if_address_index = ~0;
1720 vnet_rewrite_for_sw_interface
1722 VNET_L3_PACKET_TYPE_IP6,
1724 ip6_rewrite_node.index,
1726 &adj->rewrite_header,
1727 sizeof (adj->rewrite_data));
1731 ip_adjacency_t *adj;
1732 u8 link_layer_address[6] =
1733 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_mldv2_routers};
1735 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1736 &a->all_mldv2_routers_adj_index);
1738 adj->lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
1739 adj->if_address_index = ~0;
1741 vnet_rewrite_for_sw_interface
1743 VNET_L3_PACKET_TYPE_IP6,
1745 ip6_rewrite_node.index,
1747 &adj->rewrite_header,
1748 sizeof (adj->rewrite_data));
1751 /* add multicast groups we will always be reporting */
1753 ip6_mldp_group_t *mcast_group_info;
1755 ip6_set_reserved_multicast_address (&addr,
1756 IP6_MULTICAST_SCOPE_link_local,
1757 IP6_MULTICAST_GROUP_ID_all_hosts);
1759 /* lookup mldp info for this interface */
1761 uword * p = mhash_get (&a->address_to_mldp_index, &addr);
1762 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1765 if(!mcast_group_info)
1769 pool_get (a->mldp_group_pool, mcast_group_info);
1771 mi = mcast_group_info - a->mldp_group_pool;
1772 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1774 mcast_group_info->type = 4;
1775 mcast_group_info->mcast_source_address_pool = 0;
1776 mcast_group_info->num_sources = 0;
1777 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1780 ip6_set_reserved_multicast_address (&addr,
1781 IP6_MULTICAST_SCOPE_link_local,
1782 IP6_MULTICAST_GROUP_ID_all_routers);
1784 p = mhash_get (&a->address_to_mldp_index, &addr);
1785 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1787 if(!mcast_group_info)
1791 pool_get (a->mldp_group_pool, mcast_group_info);
1793 mi = mcast_group_info - a->mldp_group_pool;
1794 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1796 mcast_group_info->type = 4;
1797 mcast_group_info->mcast_source_address_pool = 0;
1798 mcast_group_info->num_sources = 0;
1799 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1802 ip6_set_reserved_multicast_address (&addr,
1803 IP6_MULTICAST_SCOPE_link_local,
1804 IP6_MULTICAST_GROUP_ID_mldv2_routers);
1806 p = mhash_get (&a->address_to_mldp_index, &addr);
1807 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1809 if(!mcast_group_info)
1813 pool_get (a->mldp_group_pool, mcast_group_info);
1815 mi = mcast_group_info - a->mldp_group_pool;
1816 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1818 mcast_group_info->type = 4;
1819 mcast_group_info->mcast_source_address_pool = 0;
1820 mcast_group_info->num_sources = 0;
1821 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1828 /* send an mldpv2 report */
1830 ip6_neighbor_send_mldpv2_report(u32 sw_if_index)
1832 vnet_main_t * vnm = vnet_get_main();
1833 vlib_main_t * vm = vnm->vlib_main;
1834 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1835 vnet_sw_interface_t * sw_if0;
1836 ethernet_interface_t * eth_if0;
1840 ip6_radv_t *radv_info;
1850 icmp6_multicast_listener_report_header_t *rh0;
1851 icmp6_multicast_listener_report_packet_t *rp0;
1853 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1854 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1855 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1857 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
1860 /* look up the radv_t information for this interface */
1861 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
1863 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1868 /* send report now - build a mldpv2 report packet */
1869 n_allocated = vlib_buffer_alloc_from_free_list(vm,
1872 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1873 if (PREDICT_FALSE(n_allocated == 0))
1875 clib_warning ("buffer allocation failure");
1879 b0 = vlib_get_buffer (vm, bo0);
1881 /* adjust the sizeof the buffer to just include the ipv6 header */
1882 b0->current_length = sizeof(icmp6_multicast_listener_report_packet_t);
1884 payload_length = sizeof(icmp6_multicast_listener_report_header_t);
1886 b0->error = ICMP6_ERROR_NONE;
1888 rp0 = vlib_buffer_get_current (b0);
1889 ip0 = (ip6_header_t *)&rp0-> ip;
1890 rh0 = (icmp6_multicast_listener_report_header_t *)&rp0-> report_hdr;
1892 memset (rp0 , 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
1894 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
1896 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
1897 /* for DEBUG - vnet driver won't seem to emit router alerts */
1898 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
1901 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
1903 /* source address MUST be the link-local address */
1904 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1905 ip0->src_address = radv_info->link_local_address;
1907 /* destination is all mldpv2 routers */
1908 ip6_set_reserved_multicast_address(&ip0->dst_address,
1909 IP6_MULTICAST_SCOPE_link_local,
1910 IP6_MULTICAST_GROUP_ID_mldv2_routers);
1912 /* add reports here */
1913 ip6_mldp_group_t *m;
1914 int num_addr_records = 0;
1915 icmp6_multicast_address_record_t rr;
1917 /* fill in the hop-by-hop extension header (router alert) info */
1918 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
1919 rh0->ext_hdr.n_data_u64s = 0;
1921 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
1923 rh0->alert.value = 0;
1928 rh0->icmp.checksum = 0;
1930 pool_foreach (m, radv_info->mldp_group_pool, ({
1933 rr.aux_data_len_u32s = 0;
1934 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
1935 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
1939 vlib_buffer_add_data (vm,
1940 b0->free_list_index,
1942 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
1944 payload_length += sizeof( icmp6_multicast_address_record_t);
1948 rh0->num_addr_records = clib_host_to_net_u16(num_addr_records);
1950 /* update lengths */
1951 ip0->payload_length = clib_host_to_net_u16 (payload_length);
1953 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
1955 ASSERT(bogus_length == 0);
1958 * OK to override w/ no regard for actual FIB, because
1959 * ip6-rewrite-local only looks at the adjacency.
1961 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1962 vnet_main.local_interface_sw_if_index;
1964 vnet_buffer (b0)->ip.adj_index[VLIB_RX] =
1965 radv_info->all_mldv2_routers_adj_index;
1967 vlib_node_t * node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-local");
1969 f = vlib_get_frame_to_node (vm, node->index);
1970 to_next = vlib_frame_vector_args (f);
1974 vlib_put_frame_to_node (vm, node->index, f);
1978 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) = {
1979 .function = icmp6_router_solicitation,
1980 .name = "icmp6-router-solicitation",
1982 .vector_size = sizeof (u32),
1984 .format_trace = format_icmp6_input_trace,
1986 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
1988 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "error-drop",
1989 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-local",
1990 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
1994 /* send a RA or update the timer info etc.. */
1996 ip6_neighbor_process_timer_event (vlib_main_t * vm,
1997 vlib_node_runtime_t * node,
1998 vlib_frame_t * frame)
2000 vnet_main_t * vnm = vnet_get_main();
2001 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2002 ip6_radv_t *radv_info;
2003 vlib_frame_t * f = 0;
2004 u32 n_this_frame = 0;
2005 u32 n_left_to_next = 0;
2008 icmp6_router_solicitation_header_t * h0;
2010 f64 now = vlib_time_now (vm);
2012 /* Interface ip6 radv info list */
2013 pool_foreach (radv_info, nm->if_radv_pool, ({
2015 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2017 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2018 radv_info->next_multicast_time = now;
2019 radv_info->last_multicast_time = now;
2020 radv_info->last_radv_time = 0;
2021 radv_info->all_routers_mcast = 0;
2025 /* Make sure that we've joined the all-routers multicast group */
2026 if(!radv_info->all_routers_mcast)
2028 /* send MDLP_REPORT_EVENT message */
2029 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2030 radv_info->all_routers_mcast = 1;
2033 /* is it time to send a multicast RA on this interface? */
2034 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2039 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2040 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2042 /* multicast send - compute next multicast send time */
2043 if( radv_info->initial_adverts_sent > 0)
2045 radv_info->initial_adverts_sent--;
2046 if(rfn > radv_info-> initial_adverts_interval)
2047 rfn = radv_info-> initial_adverts_interval;
2049 /* check to see if we are ceasing to send */
2050 if( radv_info->initial_adverts_sent == 0)
2051 if(radv_info->cease_radv)
2052 radv_info->send_radv = 0;
2055 radv_info->next_multicast_time = rfn + now;
2056 radv_info->last_multicast_time = now;
2058 /* send advert now - build a "solicted" router advert with unspecified source address */
2059 n_allocated = vlib_buffer_alloc_from_free_list(vm,
2062 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2064 if (PREDICT_FALSE(n_allocated == 0))
2066 clib_warning ("buffer allocation failure");
2069 b0 = vlib_get_buffer (vm, bo0);
2070 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2071 b0->error = ICMP6_ERROR_NONE;
2072 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2074 h0 = vlib_buffer_get_current (b0);
2076 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2078 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2079 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2080 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2081 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2082 h0->ip.hop_limit = 255;
2084 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2085 h0->ip.src_address.as_u64[0] = 0;
2086 h0->ip.src_address.as_u64[1] = 0;
2088 h0->ip.dst_address.as_u64[0] = 0;
2089 h0->ip.dst_address.as_u64[1] = 0;
2091 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2093 if (PREDICT_FALSE(f == 0))
2095 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2096 to_next = vlib_frame_vector_args (f);
2097 n_left_to_next = VLIB_FRAME_SIZE;
2106 if (PREDICT_FALSE(n_left_to_next == 0))
2108 f->n_vectors = n_this_frame;
2109 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2117 ASSERT(n_this_frame);
2118 f->n_vectors = n_this_frame;
2119 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2125 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2126 vlib_node_runtime_t * node,
2127 vlib_frame_t * frame)
2130 ip6_icmp_neighbor_discovery_event_data_t * event_data;
2132 /* init code here */
2136 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */);
2138 event_data = vlib_process_get_event_data (vm, &event_type);
2142 /* No events found: timer expired. */
2143 /* process interface list and send RAs as appropriate, update timer info */
2144 ip6_neighbor_process_timer_event (vm, node, frame);
2148 switch (event_type) {
2150 case ICMP6_ND_EVENT_INIT:
2161 _vec_len (event_data) = 0;
2164 return frame->n_vectors;
2167 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) = {
2168 .function = icmp6_router_advertisement,
2169 .name = "icmp6-router-advertisement",
2171 .vector_size = sizeof (u32),
2173 .format_trace = format_icmp6_input_trace,
2181 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2183 .function = ip6_icmp_neighbor_discovery_event_process,
2184 .name = "ip6-icmp-neighbor-discovery-event-process",
2185 .type = VLIB_NODE_TYPE_PROCESS,
2189 icmp6_neighbor_solicitation (vlib_main_t * vm,
2190 vlib_node_runtime_t * node,
2191 vlib_frame_t * frame)
2192 { return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, /* is_solicitation */ 1); }
2195 icmp6_neighbor_advertisement (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 */ 0); }
2200 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) = {
2201 .function = icmp6_neighbor_solicitation,
2202 .name = "icmp6-neighbor-solicitation",
2204 .vector_size = sizeof (u32),
2206 .format_trace = format_icmp6_input_trace,
2208 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2210 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "error-drop",
2211 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2215 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) = {
2216 .function = icmp6_neighbor_advertisement,
2217 .name = "icmp6-neighbor-advertisement",
2219 .vector_size = sizeof (u32),
2221 .format_trace = format_icmp6_input_trace,
2229 /* API support functions */
2231 ip6_neighbor_ra_config(vlib_main_t * vm, u32 sw_if_index,
2232 u8 suppress, u8 managed, u8 other,
2233 u8 ll_option, u8 send_unicast, u8 cease,
2234 u8 use_lifetime, u32 lifetime,
2235 u32 initial_count, u32 initial_interval,
2236 u32 max_interval, u32 min_interval,
2239 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2243 /* look up the radv_t information for this interface */
2244 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2245 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2246 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2251 ip6_radv_t * radv_info;
2252 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2254 if((max_interval != 0) && (min_interval ==0))
2255 min_interval = .75 * max_interval;
2257 max_interval = (max_interval != 0) ? ( (is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) : radv_info->max_radv_interval;
2258 min_interval = (min_interval != 0) ? ( (is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) : radv_info->min_radv_interval;
2259 lifetime = (use_lifetime != 0) ? ( (is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) : radv_info->adv_router_lifetime_in_sec;
2263 if(lifetime > MAX_DEF_RTR_LIFETIME)
2264 lifetime = MAX_DEF_RTR_LIFETIME;
2266 if(lifetime <= max_interval)
2267 return VNET_API_ERROR_INVALID_VALUE;
2270 if(min_interval != 0)
2272 if((min_interval > .75 * max_interval) ||
2274 return VNET_API_ERROR_INVALID_VALUE;
2277 if((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2278 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2279 return VNET_API_ERROR_INVALID_VALUE;
2282 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2283 if "flag" is clear don't change corresponding value
2285 radv_info->send_radv = (suppress != 0) ? ( (is_no != 0) ? 1 : 0 ) : radv_info->send_radv;
2286 radv_info->adv_managed_flag = ( managed != 0) ? ( (is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2287 radv_info->adv_other_flag = (other != 0) ? ( (is_no) ? 0: 1) : radv_info->adv_other_flag;
2288 radv_info->adv_link_layer_address = ( ll_option != 0) ? ( (is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2289 radv_info->send_unicast = (send_unicast != 0) ? ( (is_no) ? 0 : 1) : radv_info->send_unicast;
2290 radv_info->cease_radv = ( cease != 0) ? ( (is_no) ? 0 : 1) : radv_info->cease_radv;
2292 radv_info->min_radv_interval = min_interval;
2293 radv_info->max_radv_interval = max_interval;
2294 radv_info->adv_router_lifetime_in_sec = lifetime;
2296 radv_info->initial_adverts_count =
2297 (initial_count != 0) ? ( (is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) : radv_info->initial_adverts_count ;
2298 radv_info->initial_adverts_interval =
2299 (initial_interval != 0) ? ( (is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) : radv_info->initial_adverts_interval;
2302 if((cease != 0) && (is_no))
2303 radv_info-> send_radv = 1;
2305 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2306 radv_info->next_multicast_time = vlib_time_now (vm);
2307 radv_info->last_multicast_time = vlib_time_now (vm);
2308 radv_info->last_radv_time = 0;
2314 ip6_neighbor_ra_prefix(vlib_main_t * vm, u32 sw_if_index,
2315 ip6_address_t *prefix_addr, u8 prefix_len,
2316 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2317 u8 no_advertise, u8 off_link, u8 no_autoconfig, u8 no_onlink,
2320 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2325 /* look up the radv_t information for this interface */
2326 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2328 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2330 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2334 f64 now = vlib_time_now (vm);
2335 ip6_radv_t * radv_info;
2336 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2338 /* prefix info add, delete or update */
2339 ip6_radv_prefix_t * prefix;
2341 /* lookup prefix info for this address on this interface */
2342 uword * p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2344 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2350 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
2352 if(prefix->prefix_len != prefix_len)
2353 return VNET_API_ERROR_INVALID_VALUE_2;
2355 /* FIXME - Should the DP do this or the CP ?*/
2356 /* do specific delete processing here before returning */
2357 /* try to remove from routing table */
2359 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,/* old_value */ 0);
2360 pool_put (radv_info->adv_prefixes_pool, prefix);
2362 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2363 radv_info->next_multicast_time = vlib_time_now (vm);
2364 radv_info->last_multicast_time = vlib_time_now (vm);
2365 radv_info->last_radv_time = 0;
2369 /* adding or changing */
2374 pool_get (radv_info->adv_prefixes_pool, prefix);
2375 pi = prefix - radv_info->adv_prefixes_pool;
2376 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi, /* old_value */ 0);
2378 memset(prefix, 0x0, sizeof(ip6_radv_prefix_t));
2380 prefix->prefix_len = prefix_len;
2381 clib_memcpy(&prefix->prefix, prefix_addr, sizeof(ip6_address_t));
2383 /* initialize default values */
2384 prefix->adv_on_link_flag = 1; /* L bit set */
2385 prefix->adv_autonomous_flag = 1; /* A bit set */
2386 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
2387 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2388 prefix->enabled = 1;
2389 prefix->decrement_lifetime_flag = 1;
2390 prefix->deprecated_prefix_flag = 1;
2394 /* FIXME - Should the DP do this or the CP ?*/
2395 /* insert prefix into routing table as a connected prefix */
2404 if(prefix->prefix_len != prefix_len)
2405 return VNET_API_ERROR_INVALID_VALUE_2;
2409 /* FIXME - Should the DP do this or the CP ?*/
2410 /* remove from routing table if already there */
2414 if((val_lifetime == ~0) || (pref_lifetime == ~0))
2416 prefix->adv_valid_lifetime_in_secs = ~0;
2417 prefix->adv_pref_lifetime_in_secs = ~0;
2418 prefix->decrement_lifetime_flag = 0;
2422 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2423 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
2426 /* copy remaining */
2427 prefix->enabled = !(no_advertise != 0);
2428 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2429 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2433 /* fill in the expiration times */
2434 prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs;
2435 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
2437 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2438 radv_info->next_multicast_time = vlib_time_now (vm);
2439 radv_info->last_multicast_time = vlib_time_now (vm);
2440 radv_info->last_radv_time = 0;
2446 ip6_neighbor_cmd(vlib_main_t * vm, unformat_input_t * main_input, vlib_cli_command_t * cmd)
2448 vnet_main_t * vnm = vnet_get_main();
2449 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2450 clib_error_t * error = 0;
2452 u8 suppress = 0, managed = 0, other = 0;
2453 u8 suppress_ll_option = 0, send_unicast = 0, cease= 0;
2454 u8 use_lifetime = 0;
2455 u32 sw_if_index, ra_lifetime = 0, ra_initial_count = 0, ra_initial_interval = 0;
2456 u32 ra_max_interval = 0 , ra_min_interval = 0;
2458 unformat_input_t _line_input, * line_input = &_line_input;
2459 vnet_sw_interface_t * sw_if0;
2461 int add_radv_info = 1;
2462 __attribute__((unused)) ip6_radv_t * radv_info = 0;
2463 ip6_address_t ip6_addr;
2467 /* Get a line of input. */
2468 if (! unformat_user (main_input, unformat_line_input, line_input))
2471 /* get basic radv info for this interface */
2472 if(unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2475 if (unformat_user (line_input,
2476 unformat_vnet_sw_interface, vnm, &sw_if_index))
2479 ethernet_interface_t * eth_if0 = 0;
2481 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2482 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2483 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2487 error = clib_error_return (0, "Interface must be of ethernet type");
2491 /* look up the radv_t information for this interface */
2492 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2494 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2498 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2502 error = clib_error_return (0, "unknown interface %U'",
2503 format_unformat_error, line_input);
2509 error = clib_error_return (0, "invalid interface name %U'",
2510 format_unformat_error, line_input);
2515 /* get the rest of the command */
2516 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2518 if (unformat (line_input, "no"))
2520 else if(unformat (line_input, "prefix %U/%d",
2521 unformat_ip6_address, &ip6_addr,
2527 else if (unformat (line_input, "ra-managed-config-flag"))
2532 else if (unformat (line_input, "ra-other-config-flag"))
2537 else if (unformat (line_input, "ra-suppress") ||
2538 unformat (line_input, "ra-surpress"))
2543 else if (unformat (line_input, "ra-suppress-link-layer") ||
2544 unformat (line_input, "ra-surpress-link-layer"))
2546 suppress_ll_option = 1;
2549 else if (unformat (line_input, "ra-send-unicast"))
2554 else if (unformat (line_input, "ra-lifetime"))
2556 if (!unformat (line_input, "%d", &ra_lifetime))
2557 return(error = unformat_parse_error (line_input));
2561 else if (unformat (line_input, "ra-initial"))
2563 if (!unformat (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
2564 return(error = unformat_parse_error (line_input));
2567 else if (unformat (line_input, "ra-interval"))
2569 if (!unformat (line_input, "%d", &ra_max_interval))
2570 return(error = unformat_parse_error (line_input));
2572 if (!unformat (line_input, "%d", &ra_min_interval))
2573 ra_min_interval = 0;
2576 else if(unformat (line_input, "ra-cease"))
2582 return(unformat_parse_error (line_input));
2587 ip6_neighbor_ra_config(vm, sw_if_index,
2588 suppress, managed, other,
2589 suppress_ll_option, send_unicast, cease,
2590 use_lifetime, ra_lifetime,
2591 ra_initial_count, ra_initial_interval,
2592 ra_max_interval, ra_min_interval,
2597 u32 valid_lifetime_in_secs = 0;
2598 u32 pref_lifetime_in_secs = 0;
2599 u8 use_prefix_default_values = 0;
2600 u8 no_advertise = 0;
2602 u8 no_autoconfig = 0;
2605 /* get the rest of the command */
2606 while(unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2608 if(unformat (line_input, "default"))
2610 use_prefix_default_values = 1;
2613 else if(unformat (line_input, "infinite"))
2615 valid_lifetime_in_secs = ~0;
2616 pref_lifetime_in_secs = ~0;
2619 else if(unformat (line_input, "%d %d", &valid_lifetime_in_secs,
2620 &pref_lifetime_in_secs))
2627 /* get the rest of the command */
2628 while (!use_prefix_default_values &&
2629 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2631 if(unformat (line_input, "no-advertise"))
2633 else if(unformat (line_input, "off-link"))
2635 else if(unformat (line_input, "no-autoconfig"))
2637 else if(unformat (line_input, "no-onlink"))
2640 return(unformat_parse_error (line_input));
2643 ip6_neighbor_ra_prefix(vm, sw_if_index,
2644 &ip6_addr, addr_len,
2645 use_prefix_default_values,
2646 valid_lifetime_in_secs,
2647 pref_lifetime_in_secs,
2655 unformat_free (line_input);
2661 static clib_error_t *
2662 show_ip6_interface_cmd (vlib_main_t * vm,
2663 unformat_input_t * input,
2664 vlib_cli_command_t * cmd)
2666 vnet_main_t * vnm = vnet_get_main();
2667 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2668 clib_error_t * error = 0;
2673 if (unformat_user (input,
2674 unformat_vnet_sw_interface, vnm, &sw_if_index))
2678 /* look up the radv_t information for this interface */
2679 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2681 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2685 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2686 ip6_radv_t * radv_info;
2687 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2689 vlib_cli_output (vm, "%U is admin %s\n", format_vnet_sw_interface_name, vnm,
2690 vnet_get_sw_interface (vnm, sw_if_index),
2691 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ? "up" : "down"));
2694 u32 *global_scope = 0,i;
2695 ip_interface_address_t * a;
2697 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
2698 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
2700 while (ai != (u32)~0)
2702 a = pool_elt_at_index(lm->if_address_pool, ai);
2703 ip6_address_t * address = ip_interface_address_get_address (lm, a);
2705 if( ip6_address_is_link_local_unicast (address))
2706 vlib_cli_output (vm, "\tIPv6 is enabled, link-local address is %U\n", format_ip6_address,
2709 if((address->as_u8[0] & 0xe0) == 0x20)
2710 vec_add1 (global_scope, ai);
2712 ai = a->next_this_sw_interface;
2715 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
2716 for (i = 0; i < vec_len (global_scope); i++)
2718 a = pool_elt_at_index(lm->if_address_pool, global_scope[i]);
2719 ip6_address_t * address = ip_interface_address_get_address (lm, a);
2720 ip6_address_t mask, subnet;
2723 ip6_address_mask_from_width(&mask, a->address_length);
2724 ip6_address_mask(&subnet, &mask);
2726 vlib_cli_output (vm, "\t\t%U, subnet is %U/%d",
2727 format_ip6_address, address,
2728 format_ip6_address,&subnet,
2731 vec_free (global_scope);
2732 vlib_cli_output (vm, "\tJoined group address(es):\n");
2733 ip6_mldp_group_t *m;
2734 pool_foreach (m, radv_info->mldp_group_pool, ({
2735 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address, &m->mcast_address);
2738 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
2739 ip6_radv_prefix_t * p;
2740 pool_foreach (p, radv_info->adv_prefixes_pool, ({
2741 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
2742 format_ip6_address, &p->prefix, p->prefix_len);
2745 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
2746 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
2747 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
2748 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
2749 vlib_cli_output (vm, "\tND DAD is disabled\n");
2750 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
2751 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
2752 radv_info->adv_neighbor_reachable_time_in_msec);
2753 vlib_cli_output (vm, "\tND advertised retransmit interval is %d (msec)\n",
2754 radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations);
2756 u32 ra_interval = radv_info->max_radv_interval;
2757 u32 ra_interval_min = radv_info->min_radv_interval;
2758 vlib_cli_output (vm, "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
2759 ra_interval, ra_interval_min);
2760 vlib_cli_output (vm, "\tND router advertisements live for %d seconds\n",
2761 radv_info->adv_router_lifetime_in_sec);
2762 vlib_cli_output (vm, "\tHosts %s stateless autoconfig for addresses\n",
2763 (radv_info->adv_managed_flag) ? "use" :" don't use");
2764 vlib_cli_output (vm, "\tND router advertisements sent %d\n", radv_info->n_advertisements_sent);
2765 vlib_cli_output (vm, "\tND router solicitations received %d\n", radv_info->n_solicitations_rcvd);
2766 vlib_cli_output (vm, "\tND router solicitations dropped %d\n", radv_info->n_solicitations_dropped);
2770 error = clib_error_return (0, "Ipv6 not enabled on interface",
2771 format_unformat_error, input);
2778 VLIB_CLI_COMMAND (show_ip6_interface_command, static) = {
2779 .path = "show ip6 interface",
2780 .function = show_ip6_interface_cmd,
2781 .short_help = "Show ip6 interface <iface name>",
2785 disable_ip6_interface(vlib_main_t * vm,
2788 clib_error_t * error = 0;
2789 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2792 /* look up the radv_t information for this interface */
2793 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2794 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2796 /* if not created - do nothing */
2799 vnet_main_t * vnm = vnet_get_main();
2800 ip6_radv_t * radv_info;
2802 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2804 /* check radv_info ref count for other ip6 addresses on this interface */
2805 if(radv_info->ref_count == 0 )
2807 /* essentially "disables" ipv6 on this interface */
2808 error = ip6_add_del_interface_address (vm, sw_if_index,
2809 &radv_info->link_local_address,
2810 radv_info->link_local_prefix_len,
2813 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, 0/* is_add */);
2820 ip6_interface_enabled(vlib_main_t * vm,
2823 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2826 /* look up the radv_t information for this interface */
2827 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2829 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2835 enable_ip6_interface(vlib_main_t * vm,
2838 clib_error_t * error = 0;
2839 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2843 /* look up the radv_t information for this interface */
2844 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2846 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2848 /* if not created yet */
2851 vnet_main_t * vnm = vnet_get_main();
2852 vnet_sw_interface_t * sw_if0;
2854 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2855 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2857 ethernet_interface_t * eth_if0;
2859 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2862 /* create radv_info. for this interface. This holds all the info needed for router adverts */
2863 ri = ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
2867 ip6_radv_t * radv_info;
2868 ip6_address_t link_local_address;
2870 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2872 ip6_link_local_address_from_ethernet_mac_address (&link_local_address,
2875 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
2876 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB)
2878 /* make up an interface id */
2882 link_local_address.as_u64[0] = radv_info->randomizer;
2885 md5_add (&m, &link_local_address, 16);
2886 md5_finish (&m, digest);
2888 clib_memcpy(&link_local_address, digest, 16);
2890 radv_info->randomizer = link_local_address.as_u64[0];
2892 link_local_address.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2894 link_local_address.as_u8[8] &= 0xfd;
2897 /* essentially "enables" ipv6 on this interface */
2898 error = ip6_add_del_interface_address (vm, sw_if_index,
2899 &link_local_address, 64 /* address width */,
2903 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, !is_add);
2906 radv_info->link_local_address = link_local_address;
2907 radv_info->link_local_prefix_len = 64;
2916 static clib_error_t *
2917 enable_ip6_interface_cmd (vlib_main_t * vm,
2918 unformat_input_t * input,
2919 vlib_cli_command_t * cmd)
2921 vnet_main_t * vnm = vnet_get_main();
2922 clib_error_t * error = 0;
2927 if (unformat_user (input,
2928 unformat_vnet_sw_interface, vnm, &sw_if_index))
2930 enable_ip6_interface(vm, sw_if_index);
2934 error = clib_error_return (0, "unknown interface\n'",
2935 format_unformat_error, input);
2941 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) = {
2942 .path = "enable ip6 interface",
2943 .function = enable_ip6_interface_cmd,
2944 .short_help = "enable ip6 interface <iface name>",
2947 static clib_error_t *
2948 disable_ip6_interface_cmd (vlib_main_t * vm,
2949 unformat_input_t * input,
2950 vlib_cli_command_t * cmd)
2952 vnet_main_t * vnm = vnet_get_main();
2953 clib_error_t * error = 0;
2958 if (unformat_user (input,
2959 unformat_vnet_sw_interface, vnm, &sw_if_index))
2961 error = disable_ip6_interface(vm, sw_if_index);
2965 error = clib_error_return (0, "unknown interface\n'",
2966 format_unformat_error, input);
2972 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) = {
2973 .path = "disable ip6 interface",
2974 .function = disable_ip6_interface_cmd,
2975 .short_help = "disable ip6 interface <iface name>",
2978 VLIB_CLI_COMMAND (ip6_nd_command, static) = {
2980 .short_help = "Set ip6 neighbor discovery parameters",
2981 .function = ip6_neighbor_cmd,
2985 set_ip6_link_local_address(vlib_main_t * vm,
2987 ip6_address_t *address,
2990 clib_error_t * error = 0;
2991 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2993 ip6_radv_t * radv_info;
2994 vnet_main_t * vnm = vnet_get_main();
2996 if( !ip6_address_is_link_local_unicast (address))
2998 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
2999 return(error = clib_error_return (0, "address not link-local",
3000 format_unformat_error));
3003 /* call enable ipv6 */
3004 enable_ip6_interface(vm, sw_if_index);
3006 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3010 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3012 /* save if link local address (overwrite default) */
3014 /* delete the old one */
3015 error = ip6_add_del_interface_address (vm, sw_if_index,
3016 &radv_info->link_local_address,
3017 radv_info->link_local_prefix_len /* address width */,
3022 /* add the new one */
3023 error = ip6_add_del_interface_address (vm, sw_if_index,
3025 address_length /* address width */,
3030 radv_info->link_local_address = *address;
3031 radv_info->link_local_prefix_len = address_length;
3037 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3038 error = clib_error_return (0, "ip6 not enabled for interface",
3039 format_unformat_error);
3045 set_ip6_link_local_address_cmd (vlib_main_t * vm,
3046 unformat_input_t * input,
3047 vlib_cli_command_t * cmd)
3049 vnet_main_t * vnm = vnet_get_main();
3050 clib_error_t * error = 0;
3052 ip6_address_t ip6_addr;
3055 if (unformat_user (input,
3056 unformat_vnet_sw_interface, vnm, &sw_if_index))
3058 /* get the rest of the command */
3059 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3061 if(unformat (input, "%U/%d",
3062 unformat_ip6_address, &ip6_addr,
3066 return(unformat_parse_error (input));
3069 error = set_ip6_link_local_address(vm,
3076 VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) = {
3077 .path = "set ip6 link-local address",
3078 .short_help = "Set ip6 interface link-local address <intfc> <address.>",
3079 .function = set_ip6_link_local_address_cmd,
3082 /* callback when an interface address is added or deleted */
3084 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3087 ip6_address_t * address,
3089 u32 if_address_index,
3092 vnet_main_t * vnm = vnet_get_main();
3093 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3095 vlib_main_t * vm = vnm->vlib_main;
3096 ip6_radv_t * radv_info;
3098 ip6_mldp_group_t *mcast_group_info;
3100 /* create solicited node multicast address for this interface adddress */
3101 ip6_set_solicited_node_multicast_address (&a, 0);
3103 a.as_u8[0xd] = address->as_u8[0xd];
3104 a.as_u8[0xe] = address->as_u8[0xe];
3105 a.as_u8[0xf] = address->as_u8[0xf];
3109 /* try to create radv_info - does nothing if ipv6 already enabled */
3110 enable_ip6_interface(vm, sw_if_index);
3112 /* look up the radv_t information for this interface */
3113 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
3114 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3118 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3121 if( !ip6_address_is_link_local_unicast (address))
3122 radv_info->ref_count++;
3124 /* lookup prefix info for this address on this interface */
3125 uword * p = mhash_get (&radv_info->address_to_mldp_index, &a);
3126 mcast_group_info = p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3128 /* add -solicted node multicast address */
3129 if(!mcast_group_info)
3133 pool_get (radv_info->mldp_group_pool, mcast_group_info);
3135 mi = mcast_group_info - radv_info->mldp_group_pool;
3136 mhash_set (&radv_info->address_to_mldp_index, &a, mi, /* old_value */ 0);
3138 mcast_group_info->type = 4;
3139 mcast_group_info->mcast_source_address_pool = 0;
3140 mcast_group_info->num_sources = 0;
3141 clib_memcpy(&mcast_group_info->mcast_address, &a, sizeof(ip6_address_t));
3149 /* look up the radv_t information for this interface */
3150 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
3151 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3155 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3157 /* lookup prefix info for this address on this interface */
3158 uword * p = mhash_get (&radv_info->address_to_mldp_index, &a);
3159 mcast_group_info = p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3161 if(mcast_group_info)
3163 mhash_unset (&radv_info->address_to_mldp_index, &a,/* old_value */ 0);
3164 pool_put (radv_info->mldp_group_pool, mcast_group_info);
3167 /* if interface up send MLDP "report" */
3168 radv_info->all_routers_mcast = 0;
3171 if( !ip6_address_is_link_local_unicast (address))
3172 radv_info->ref_count--;
3177 clib_error_t *ip6_set_neighbor_limit (u32 neighbor_limit)
3179 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3181 nm->limit_neighbor_cache_size = neighbor_limit;
3187 ip6_neighbor_entry_del_adj(ip6_neighbor_t *n, u32 adj_index)
3193 vec_foreach_index(i, n->adjacencies)
3194 if (vec_elt(n->adjacencies, i) == adj_index)
3196 vec_del1(n->adjacencies, i);
3204 ip6_neighbor_entry_add_adj(ip6_neighbor_t *n, u32 adj_index)
3207 vec_foreach_index(i, n->adjacencies)
3208 if (vec_elt(n->adjacencies, i) == adj_index)
3210 vec_add1(n->adjacencies, adj_index);
3214 ip6_neighbor_add_del_adj_cb (struct ip_lookup_main_t * lm,
3216 ip_adjacency_t * adj,
3219 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3220 ip6_neighbor_key_t k;
3221 ip6_neighbor_t *n = 0;
3225 for(ai = adj->heap_handle; ai < adj->heap_handle + adj->n_adj ; ai++)
3227 adj = ip_get_adjacency (lm, ai);
3228 if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
3229 (adj->arp.next_hop.ip6.as_u64[0] || adj->arp.next_hop.ip6.as_u64[1]))
3231 k.sw_if_index = adj->rewrite_header.sw_if_index;
3232 k.ip6_address.as_u64[0] = adj->arp.next_hop.ip6.as_u64[0];
3233 k.ip6_address.as_u64[1] = adj->arp.next_hop.ip6.as_u64[1];
3235 p = mhash_get (&nm->neighbor_index_by_key, &k);
3237 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
3245 clib_warning("Adjacency contains unknown ND next hop %U (del)",
3246 format_ip46_address, &adj->arp.next_hop, IP46_TYPE_IP6);
3248 ip6_neighbor_entry_del_adj(n, adj->heap_handle);
3253 clib_warning("Adjacency contains unknown ND next hop %U (add)",
3254 format_ip46_address, &adj->arp.next_hop, IP46_TYPE_IP6);
3256 ip6_neighbor_entry_add_adj(n, adj->heap_handle);
3261 static clib_error_t * ip6_neighbor_init (vlib_main_t * vm)
3263 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3264 ip6_main_t * im = &ip6_main;
3265 ip_lookup_main_t * lm = &im->lookup_main;
3267 mhash_init (&nm->neighbor_index_by_key,
3268 /* value size */ sizeof (uword),
3269 /* key size */ sizeof (ip6_neighbor_key_t));
3271 icmp6_register_type (vm, ICMP6_neighbor_solicitation, ip6_icmp_neighbor_solicitation_node.index);
3272 icmp6_register_type (vm, ICMP6_neighbor_advertisement, ip6_icmp_neighbor_advertisement_node.index);
3273 icmp6_register_type (vm, ICMP6_router_solicitation, ip6_icmp_router_solicitation_node.index);
3274 icmp6_register_type (vm, ICMP6_router_advertisement, ip6_icmp_router_advertisement_node.index);
3276 /* handler node for ip6 neighbor discovery events and timers */
3277 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3279 /* add call backs */
3280 ip6_add_del_interface_address_callback_t cb;
3281 memset(&cb, 0x0, sizeof(ip6_add_del_interface_address_callback_t));
3283 /* when an interface address changes... */
3284 cb.function = ip6_neighbor_add_del_interface_address;
3285 cb.function_opaque = 0;
3286 vec_add1 (im->add_del_interface_address_callbacks, cb);
3288 mhash_init (&nm->pending_resolutions_by_address,
3289 /* value size */ sizeof (uword),
3290 /* key size */ sizeof (ip6_address_t));
3292 /* default, configurable */
3293 nm->limit_neighbor_cache_size = 50000;
3296 /* $$$$ Hack fix for today */
3297 vec_validate_init_empty
3298 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */);
3301 ip_register_add_del_adjacency_callback(lm, ip6_neighbor_add_del_adj_cb);
3306 VLIB_INIT_FUNCTION (ip6_neighbor_init);
3309 void vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
3315 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3316 ip6_address_t * address = address_arg;
3318 pending_resolution_t * pr;
3320 pool_get (nm->pending_resolutions, pr);
3322 pr->next_index = ~0;
3323 pr->node_index = node_index;
3324 pr->type_opaque = type_opaque;
3327 p = mhash_get (&nm->pending_resolutions_by_address, address);
3330 /* Insert new resolution at the head of the list */
3331 pr->next_index = p[0];
3332 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
3335 mhash_set (&nm->pending_resolutions_by_address, address,
3336 pr - nm->pending_resolutions, 0 /* old value */);