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 /* Used for nd event notification only */
173 void * data_callback;
175 } pending_resolution_t;
179 /* Hash tables mapping name to opcode. */
180 uword * opcode_by_name;
182 /* lite beer "glean" adjacency handling */
183 mhash_t pending_resolutions_by_address;
184 pending_resolution_t * pending_resolutions;
186 /* Mac address change notification */
187 mhash_t mac_changes_by_address;
188 pending_resolution_t * mac_changes;
190 u32 * neighbor_input_next_index_by_hw_if_index;
192 ip6_neighbor_t * neighbor_pool;
194 mhash_t neighbor_index_by_key;
196 u32 * if_radv_pool_index_by_sw_if_index;
198 ip6_radv_t * if_radv_pool;
200 /* Neighbor attack mitigation */
201 u32 limit_neighbor_cache_size;
202 u32 neighbor_delete_rotor;
204 } ip6_neighbor_main_t;
206 static ip6_neighbor_main_t ip6_neighbor_main;
207 static ip6_address_t ip6a_zero; /* ip6 address 0 */
209 static u8 * format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
211 vlib_main_t * vm = va_arg (*va, vlib_main_t *);
212 ip6_neighbor_t * n = va_arg (*va, ip6_neighbor_t *);
213 vnet_main_t * vnm = vnet_get_main();
214 vnet_sw_interface_t * si;
218 return format (s, "%=12s%=20s%=6s%=20s%=40s", "Time", "Address", "Flags", "Link layer", "Interface");
220 if (n->flags & IP6_NEIGHBOR_FLAG_GLEAN)
221 flags = format(flags, "G");
223 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
224 flags = format(flags, "S");
226 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
227 s = format (s, "%=12U%=20U%=6s%=20U%=40U",
228 format_vlib_cpu_time, vm, n->cpu_time_last_updated,
229 format_ip6_address, &n->key.ip6_address,
230 flags ? (char *)flags : "",
231 format_ethernet_address, n->link_layer_address,
232 format_vnet_sw_interface_name, vnm, si);
238 static clib_error_t *
239 ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
243 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
246 if (! (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
248 u32 i, * to_delete = 0;
250 pool_foreach (n, nm->neighbor_pool, ({
251 if (n->key.sw_if_index == sw_if_index)
252 vec_add1 (to_delete, n - nm->neighbor_pool);
255 for (i = 0; i < vec_len (to_delete); i++)
257 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
258 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
259 pool_put (nm->neighbor_pool, n);
262 vec_free (to_delete);
268 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
270 static void unset_random_neighbor_entry (void)
272 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
273 vnet_main_t * vnm = vnet_get_main();
274 vlib_main_t * vm = vnm->vlib_main;
278 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
279 nm->neighbor_delete_rotor = index;
281 /* Try again from elt 0, could happen if an intfc goes down */
284 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
285 nm->neighbor_delete_rotor = index;
288 /* Nothing left in the pool */
292 e = pool_elt_at_index (nm->neighbor_pool, index);
294 vnet_unset_ip6_ethernet_neighbor (vm, e->key.sw_if_index,
296 e->link_layer_address,
303 u8 link_layer_address[6];
306 } ip6_neighbor_set_unset_rpc_args_t;
309 static void ip6_neighbor_set_unset_rpc_callback
310 ( ip6_neighbor_set_unset_rpc_args_t * a);
312 static void set_unset_ip6_neighbor_rpc
316 u8 *link_layer_addreess,
317 int is_add, int is_static)
319 ip6_neighbor_set_unset_rpc_args_t args;
320 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
322 args.sw_if_index = sw_if_index;
323 args.is_add = is_add;
324 args.is_static = is_static;
325 clib_memcpy (&args.addr, a, sizeof (*a));
326 clib_memcpy (args.link_layer_address, link_layer_addreess, 6);
328 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
329 (u8 *) &args, sizeof (args));
334 vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
337 u8 * link_layer_address,
338 uword n_bytes_link_layer_address,
341 vnet_main_t * vnm = vnet_get_main();
342 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
343 ip6_neighbor_key_t k;
344 ip6_neighbor_t * n = 0;
345 ip6_main_t * im = &ip6_main;
346 ip_lookup_main_t * lm = &im->lookup_main;
347 int make_new_nd_cache_entry=1;
351 ip_adjacency_t *existing_adj;
352 pending_resolution_t * pr, * mc;
355 if (os_get_cpu_number())
357 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
358 1 /* set new neighbor */, is_static);
363 k.sw_if_index = sw_if_index;
364 k.ip6_address = a[0];
367 vlib_worker_thread_barrier_sync (vm);
369 p = mhash_get (&nm->neighbor_index_by_key, &k);
371 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
372 /* Refuse to over-write static neighbor entry. */
374 (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
376 make_new_nd_cache_entry = 0;
379 /* Note: always install the route. It might have been deleted */
380 ip6_add_del_route_args_t args;
383 memset (&adj, 0, sizeof(adj));
384 adj.lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
385 adj.explicit_fib_index = ~0;
387 vnet_rewrite_for_sw_interface
389 VNET_L3_PACKET_TYPE_IP6,
391 ip6_rewrite_node.index,
394 sizeof (adj.rewrite_data));
396 /* result of this lookup should be next-hop adjacency */
397 adj_index = ip6_fib_lookup_with_table (im, im->fib_index_by_sw_if_index[sw_if_index], a);
398 existing_adj = ip_get_adjacency(lm, adj_index);
400 if (existing_adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
401 existing_adj->arp.next_hop.ip6.as_u64[0] == a->as_u64[0] &&
402 existing_adj->arp.next_hop.ip6.as_u64[1] == a->as_u64[1])
408 adjs = vec_dup(n->adjacencies);
410 clib_warning ("ip6 neighbor n not set");
412 /* Update all adj assigned to this arp entry */
413 vec_foreach(ai, adjs)
416 ip_adjacency_t * uadj = ip_get_adjacency(lm, *ai);
417 for (i = 0; i < uadj->n_adj; i++)
418 if (uadj[i].lookup_next_index == IP_LOOKUP_NEXT_ARP &&
419 uadj[i].arp.next_hop.ip6.as_u64[0] == a->as_u64[0] &&
420 uadj[i].arp.next_hop.ip6.as_u64[1] == a->as_u64[1])
421 ip_update_adjacency (lm, *ai + i, &adj);
428 args.table_index_or_table_id = im->fib_index_by_sw_if_index[sw_if_index];
429 args.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_ADD | IP6_ROUTE_FLAG_NEIGHBOR;
430 args.dst_address = a[0];
431 args.dst_address_length = 128;
435 ip6_add_del_route (im, &args);
438 if (make_new_nd_cache_entry) {
439 pool_get (nm->neighbor_pool, n);
440 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
445 /* Update time stamp and ethernet address. */
446 clib_memcpy (n->link_layer_address, link_layer_address, n_bytes_link_layer_address);
447 n->cpu_time_last_updated = clib_cpu_time_now ();
449 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
451 /* Customer(s) waiting for this address to be resolved? */
452 p = mhash_get (&nm->pending_resolutions_by_address, a);
457 while (next_index != (u32)~0)
459 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
460 vlib_process_signal_event (vm, pr->node_index,
463 next_index = pr->next_index;
464 pool_put (nm->pending_resolutions, pr);
467 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
470 /* Customer(s) requesting ND event for this address? */
471 p = mhash_get (&nm->mac_changes_by_address, a);
476 while (next_index != (u32)~0)
478 int (*fp)(u32, u8 *, u32, ip6_address_t *);
480 mc = pool_elt_at_index (nm->mac_changes, next_index);
481 fp = mc->data_callback;
483 /* Call the user's data callback, return 1 to suppress dup events */
485 rv = (*fp)(mc->data, link_layer_address, sw_if_index, &ip6a_zero);
487 * Signal the resolver process, as long as the user
488 * says they want to be notified
491 vlib_process_signal_event (vm, mc->node_index,
494 next_index = mc->next_index;
498 vlib_worker_thread_barrier_release(vm);
503 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
506 u8 * link_layer_address,
507 uword n_bytes_link_layer_address)
509 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
510 ip6_neighbor_key_t k;
512 ip6_main_t * im = &ip6_main;
513 ip6_add_del_route_args_t args;
518 if (os_get_cpu_number())
520 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
526 k.sw_if_index = sw_if_index;
527 k.ip6_address = a[0];
530 vlib_worker_thread_barrier_sync (vm);
532 p = mhash_get (&nm->neighbor_index_by_key, &k);
539 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
540 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
541 pool_put (nm->neighbor_pool, n);
543 args.table_index_or_table_id = im->fib_index_by_sw_if_index[sw_if_index];
544 args.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_DEL
545 | IP6_ROUTE_FLAG_NEIGHBOR;
546 args.dst_address = a[0];
547 args.dst_address_length = 128;
551 ip6_add_del_route (im, &args);
553 vlib_worker_thread_barrier_release(vm);
559 vnet_ip6_neighbor_glean_add(u32 fib_index, void * next_hop_arg)
561 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
562 ip6_main_t * im = &ip6_main;
563 ip_lookup_main_t * lm = &im->lookup_main;
564 ip6_address_t * next_hop = next_hop_arg;
565 ip_adjacency_t add_adj, *adj;
566 ip6_add_del_route_args_t args;
568 ip6_neighbor_key_t k;
571 adj_index = ip6_fib_lookup_with_table(im, fib_index, next_hop);
572 adj = ip_get_adjacency(lm, adj_index);
574 if (!adj || adj->lookup_next_index != IP_LOOKUP_NEXT_ARP)
577 if (adj->arp.next_hop.ip6.as_u64[0] ||
578 adj->arp.next_hop.ip6.as_u64[1])
581 k.sw_if_index = adj->rewrite_header.sw_if_index;
582 k.ip6_address = *next_hop;
584 if (mhash_get (&nm->neighbor_index_by_key, &k))
587 pool_get (nm->neighbor_pool, n);
588 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool, /* old value */ 0);
590 n->cpu_time_last_updated = clib_cpu_time_now ();
591 n->flags = IP6_NEIGHBOR_FLAG_GLEAN;
593 memset(&args, 0, sizeof(args));
594 memcpy(&add_adj, adj, sizeof(add_adj));
595 add_adj.arp.next_hop.ip6 = *next_hop; /* install neighbor /128 route */
596 args.table_index_or_table_id = fib_index;
597 args.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_ADD | IP6_ROUTE_FLAG_NEIGHBOR;
598 args.dst_address = *next_hop;
599 args.dst_address_length = 128;
601 args.add_adj = &add_adj;
603 ip6_add_del_route (im, &args);
604 return ip6_fib_lookup_with_table (im, fib_index, next_hop);
608 static void ip6_neighbor_set_unset_rpc_callback
609 ( ip6_neighbor_set_unset_rpc_args_t * a)
611 vlib_main_t * vm = vlib_get_main();
613 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
614 a->link_layer_address, 6, a->is_static);
616 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
617 a->link_layer_address, 6);
622 ip6_neighbor_sort (void *a1, void *a2)
624 vnet_main_t * vnm = vnet_get_main();
625 ip6_neighbor_t * n1 = a1, * n2 = a2;
627 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
628 n2->key.sw_if_index);
630 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
634 static clib_error_t *
635 show_ip6_neighbors (vlib_main_t * vm,
636 unformat_input_t * input,
637 vlib_cli_command_t * cmd)
639 vnet_main_t * vnm = vnet_get_main();
640 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
641 ip6_neighbor_t * n, * ns;
642 clib_error_t * error = 0;
645 /* Filter entries by interface if given. */
647 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
650 pool_foreach (n, nm->neighbor_pool, ({ vec_add1 (ns, n[0]); }));
651 vec_sort_with_function (ns, ip6_neighbor_sort);
652 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
653 vec_foreach (n, ns) {
654 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
656 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
663 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
664 .path = "show ip6 neighbors",
665 .function = show_ip6_neighbors,
666 .short_help = "Show ip6 neighbors",
669 static clib_error_t *
670 set_ip6_neighbor (vlib_main_t * vm,
671 unformat_input_t * input,
672 vlib_cli_command_t * cmd)
674 vnet_main_t * vnm = vnet_get_main();
682 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
684 /* intfc, ip6-address, mac-address */
685 if (unformat (input, "%U %U %U",
686 unformat_vnet_sw_interface, vnm, &sw_if_index,
687 unformat_ip6_address, &addr,
688 unformat_ethernet_address, mac_address))
691 else if (unformat (input, "delete") || unformat (input, "del"))
693 else if (unformat (input, "static"))
700 return clib_error_return (0, "Missing interface, ip6 or hw address");
703 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
704 mac_address, sizeof(mac_address), is_static);
706 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
707 mac_address, sizeof(mac_address));
711 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) = {
712 .path = "set ip6 neighbor",
713 .function = set_ip6_neighbor,
714 .short_help = "set ip6 neighbor [del] <intfc> <ip6-address> <mac-address> [static]",
718 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
719 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
720 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
721 } icmp6_neighbor_solicitation_or_advertisement_next_t;
723 static_always_inline uword
724 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
725 vlib_node_runtime_t * node,
726 vlib_frame_t * frame,
727 uword is_solicitation)
729 vnet_main_t * vnm = vnet_get_main();
730 ip6_main_t * im = &ip6_main;
731 ip_lookup_main_t * lm = &im->lookup_main;
732 uword n_packets = frame->n_vectors;
733 u32 * from, * to_next;
734 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
735 icmp6_neighbor_discovery_option_type_t option_type;
736 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
739 from = vlib_frame_vector_args (frame);
740 n_left_from = n_packets;
741 next_index = node->cached_next_index;
743 if (node->flags & VLIB_NODE_FLAG_TRACE)
744 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
746 sizeof (icmp6_input_trace_t));
750 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
751 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
752 n_advertisements_sent = 0;
754 while (n_left_from > 0)
756 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
758 while (n_left_from > 0 && n_left_to_next > 0)
762 icmp6_neighbor_solicitation_or_advertisement_header_t * h0;
763 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * o0;
764 u32 bi0, options_len0, sw_if_index0, next0, error0;
765 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
769 bi0 = to_next[0] = from[0];
776 p0 = vlib_get_buffer (vm, bi0);
777 ip0 = vlib_buffer_get_current (p0);
778 h0 = ip6_next_header (ip0);
779 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
781 error0 = ICMP6_ERROR_NONE;
782 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
783 ip6_sadd_link_local = ip6_address_is_link_local_unicast(&ip0->src_address);
784 ip6_sadd_unspecified = ip6_address_is_unspecified (&ip0->src_address);
786 /* Check that source address is unspecified, link-local or else on-link. */
787 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
789 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
790 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, src_adj_index0);
792 /* Allow all realistic-looking rewrite adjacencies to pass */
793 ni0 = adj0->lookup_next_index;
794 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
795 (ni0 < IP6_LOOKUP_N_NEXT);
797 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
799 ? ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
803 o0 = (void *) (h0 + 1);
804 o0 = ((options_len0 == 8 && o0->header.type == option_type
805 && o0->header.n_data_u64s == 1) ? o0 : 0);
807 /* If src address unspecified or link local, donot learn neighbor MAC */
808 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
809 !ip6_sadd_unspecified && !ip6_sadd_link_local))
811 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
812 if (nm->limit_neighbor_cache_size &&
813 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
814 unset_random_neighbor_entry();
815 vnet_set_ip6_ethernet_neighbor (
817 is_solicitation ? &ip0->src_address : &h0->target_address,
818 o0->ethernet_address, sizeof (o0->ethernet_address), 0);
821 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
823 /* Check that target address is one that we know about. */
824 ip_interface_address_t * ia0;
825 ip6_address_fib_t ip6_af0;
828 ip6_addr_fib_init (&ip6_af0, &h0->target_address,
829 vec_elt (im->fib_index_by_sw_if_index,
832 /* Gross kludge, "thank you" MJ, don't even ask */
833 oldheap = clib_mem_set_heap (clib_per_cpu_mheaps[0]);
834 ia0 = ip_get_interface_address (lm, &ip6_af0);
835 clib_mem_set_heap (oldheap);
837 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN : error0;
841 next0 = (error0 != ICMP6_ERROR_NONE
842 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
843 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
847 error0 = error0 == ICMP6_ERROR_NONE ?
848 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
851 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
853 vnet_sw_interface_t * sw_if0;
854 ethernet_interface_t * eth_if0;
855 ethernet_header_t *eth0;
857 /* dst address is either source address or the all-nodes mcast addr */
858 if(!ip6_sadd_unspecified)
859 ip0->dst_address = ip0->src_address;
861 ip6_set_reserved_multicast_address(&ip0->dst_address,
862 IP6_MULTICAST_SCOPE_link_local,
863 IP6_MULTICAST_GROUP_ID_all_hosts);
865 ip0->src_address = h0->target_address;
866 ip0->hop_limit = 255;
867 h0->icmp.type = ICMP6_neighbor_advertisement;
869 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
870 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
871 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
874 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
876 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
879 h0->advertisement_flags = clib_host_to_net_u32
880 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
881 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
883 h0->icmp.checksum = 0;
885 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
887 ASSERT(bogus_length == 0);
889 /* Reuse current MAC header, copy SMAC to DMAC and
890 * interface MAC to SMAC */
891 vlib_buffer_advance(p0, - ethernet_buffer_header_size(p0));
892 eth0 = vlib_buffer_get_current(p0);
893 clib_memcpy(eth0->dst_address, eth0->src_address, 6);
894 clib_memcpy(eth0->src_address, eth_if0->address, 6);
896 /* Setup input and output sw_if_index for packet */
897 ASSERT(vnet_buffer(p0)->sw_if_index[VLIB_RX] == sw_if_index0);
898 vnet_buffer(p0)->sw_if_index[VLIB_TX] = sw_if_index0;
899 vnet_buffer(p0)->sw_if_index[VLIB_RX] =
900 vnet_main.local_interface_sw_if_index;
902 n_advertisements_sent++;
905 p0->error = error_node->errors[error0];
907 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
908 to_next, n_left_to_next,
912 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
915 /* Account for advertisements sent. */
916 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, n_advertisements_sent);
918 return frame->n_vectors;
921 /* for "syslogging" - use elog for now */
922 #define foreach_log_level \
924 _ (INFO, "INFORMATION") \
925 _ (NOTICE, "NOTICE") \
926 _ (WARNING, "WARNING") \
928 _ (CRIT, "CRITICAL") \
930 _ (EMERG, "EMERGENCY")
933 #define _(f,s) LOG_##f,
938 static char * log_level_strings[] = {
944 static int logmask = 1 << LOG_DEBUG;
947 ip6_neighbor_syslog(vlib_main_t *vm, int priority, char * fmt, ...)
949 /* just use elog for now */
953 if( (priority > LOG_EMERG) ||
954 !(logmask & (1 << priority)))
960 what = va_format (0, fmt, &va);
962 ELOG_TYPE_DECLARE (e) = {
963 .format = "ip6 nd: (%s): %s",
964 .format_args = "T4T4",
966 struct { u32 s[2]; } * ed;
967 ed = ELOG_DATA (&vm->elog_main, e);
968 ed->s[0] = elog_string(&vm->elog_main, log_level_strings[priority]);
969 ed->s[1] = elog_string(&vm->elog_main, (char *)what);
975 /* ipv6 neighbor discovery - router advertisements */
977 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
978 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
979 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
980 ICMP6_ROUTER_SOLICITATION_N_NEXT,
981 } icmp6_router_solicitation_or_advertisement_next_t;
983 static_always_inline uword
984 icmp6_router_solicitation(vlib_main_t * vm,
985 vlib_node_runtime_t * node,
986 vlib_frame_t * frame)
988 vnet_main_t * vnm = vnet_get_main();
989 ip6_main_t * im = &ip6_main;
990 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
991 uword n_packets = frame->n_vectors;
992 u32 * from, * to_next;
993 u32 n_left_from, n_left_to_next, next_index;
994 u32 n_advertisements_sent = 0;
997 icmp6_neighbor_discovery_option_type_t option_type;
999 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1001 from = vlib_frame_vector_args (frame);
1002 n_left_from = n_packets;
1003 next_index = node->cached_next_index;
1005 if (node->flags & VLIB_NODE_FLAG_TRACE)
1006 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1008 sizeof (icmp6_input_trace_t));
1010 /* source may append his LL address */
1011 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1013 while (n_left_from > 0)
1015 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1017 while (n_left_from > 0 && n_left_to_next > 0)
1021 ip6_radv_t *radv_info = 0;
1023 icmp6_neighbor_discovery_header_t * h0;
1024 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * o0;
1026 u32 bi0, options_len0, sw_if_index0, next0, error0;
1027 u32 is_solicitation = 1, is_dropped = 0;
1028 u32 is_unspecified, is_link_local;
1030 bi0 = to_next[0] = from[0];
1035 n_left_to_next -= 1;
1037 p0 = vlib_get_buffer (vm, bi0);
1038 ip0 = vlib_buffer_get_current (p0);
1039 h0 = ip6_next_header (ip0);
1040 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1041 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1042 is_link_local = ip6_address_is_link_local_unicast (&ip0->src_address);
1044 error0 = ICMP6_ERROR_NONE;
1045 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1047 /* check if solicitation (not from nd_timer node) */
1048 if (ip6_address_is_unspecified (&ip0->dst_address))
1049 is_solicitation = 0;
1051 /* Check that source address is unspecified, link-local or else on-link. */
1052 if (!is_unspecified && !is_link_local)
1054 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1055 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, src_adj_index0);
1057 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1058 || (adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP
1059 && adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE))
1060 ? ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1064 /* check for source LL option and process */
1065 o0 = (void *) (h0 + 1);
1066 o0 = ((options_len0 == 8
1067 && o0->header.type == option_type
1068 && o0->header.n_data_u64s == 1)
1072 /* if src address unspecified IGNORE any options */
1073 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1074 !is_unspecified && !is_link_local)) {
1075 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1076 if (nm->limit_neighbor_cache_size &&
1077 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
1078 unset_random_neighbor_entry();
1080 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1082 o0->ethernet_address,
1083 sizeof (o0->ethernet_address), 0);
1086 /* default is to drop */
1087 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1089 if (error0 == ICMP6_ERROR_NONE)
1091 vnet_sw_interface_t * sw_if0;
1092 ethernet_interface_t * eth_if0;
1095 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1096 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1097 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1099 /* only support ethernet interface type for now */
1100 error0 = (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF : error0;
1102 if (error0 == ICMP6_ERROR_NONE)
1106 /* adjust the sizeof the buffer to just include the ipv6 header */
1107 p0->current_length -= (options_len0 + sizeof(icmp6_neighbor_discovery_header_t));
1109 /* look up the radv_t information for this interface */
1110 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1112 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1115 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1117 error0 = ((!radv_info) ? ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : error0);
1119 if (error0 == ICMP6_ERROR_NONE)
1121 f64 now = vlib_time_now (vm);
1123 /* for solicited adverts - need to rate limit */
1126 if( (now - radv_info->last_radv_time) < MIN_DELAY_BETWEEN_RAS )
1129 radv_info->last_radv_time = now;
1133 icmp6_router_advertisement_header_t rh;
1135 rh.icmp.type = ICMP6_router_advertisement;
1137 rh.icmp.checksum = 0;
1139 rh.current_hop_limit = radv_info->curr_hop_limit;
1140 rh.router_lifetime_in_sec = clib_host_to_net_u16(radv_info->adv_router_lifetime_in_sec);
1141 rh.time_in_msec_between_retransmitted_neighbor_solicitations =
1142 clib_host_to_net_u32(radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1143 rh.neighbor_reachable_time_in_msec =
1144 clib_host_to_net_u32(radv_info->adv_neighbor_reachable_time_in_msec);
1146 rh.flags = (radv_info->adv_managed_flag) ? ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP : 0;
1147 rh.flags |= ( (radv_info->adv_other_flag) ? ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP : 0);
1150 u16 payload_length = sizeof(icmp6_router_advertisement_header_t);
1152 vlib_buffer_add_data (vm,
1153 p0->free_list_index,
1155 (void *)&rh, sizeof(icmp6_router_advertisement_header_t));
1157 if(radv_info->adv_link_layer_address)
1159 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t h;
1161 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1162 h.header.n_data_u64s = 1;
1164 /* copy ll address */
1165 clib_memcpy(&h.ethernet_address[0], eth_if0->address, 6);
1167 vlib_buffer_add_data (vm,
1168 p0->free_list_index,
1170 (void *)&h, sizeof(icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
1172 payload_length += sizeof(icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1175 /* add MTU option */
1176 if(radv_info->adv_link_mtu)
1178 icmp6_neighbor_discovery_mtu_option_t h;
1181 h.mtu = clib_host_to_net_u32(radv_info->adv_link_mtu);
1182 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1183 h.header.n_data_u64s = 1;
1185 payload_length += sizeof( icmp6_neighbor_discovery_mtu_option_t);
1187 vlib_buffer_add_data (vm,
1188 p0->free_list_index,
1190 (void *)&h, sizeof(icmp6_neighbor_discovery_mtu_option_t));
1193 /* add advertised prefix options */
1194 ip6_radv_prefix_t *pr_info;
1196 pool_foreach (pr_info, radv_info->adv_prefixes_pool, ({
1198 if(pr_info->enabled &&
1199 (!pr_info->decrement_lifetime_flag || (pr_info->pref_lifetime_expires >0)))
1201 /* advertise this prefix */
1202 icmp6_neighbor_discovery_prefix_information_option_t h;
1204 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1205 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1207 h.dst_address_length = pr_info->prefix_len;
1209 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1210 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
1212 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1214 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1215 h.preferred_time = 0;
1219 if(pr_info->decrement_lifetime_flag)
1221 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1222 (pr_info->valid_lifetime_expires - now) : 0;
1224 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1225 (pr_info->pref_lifetime_expires - now) : 0;
1228 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1229 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1233 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1235 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1237 vlib_buffer_add_data (vm,
1238 p0->free_list_index,
1240 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1245 /* add additional options before here */
1247 /* finish building the router advertisement... */
1248 if(!is_unspecified && radv_info->send_unicast)
1250 ip0->dst_address = ip0->src_address;
1254 /* target address is all-nodes mcast addr */
1255 ip6_set_reserved_multicast_address(&ip0->dst_address,
1256 IP6_MULTICAST_SCOPE_link_local,
1257 IP6_MULTICAST_GROUP_ID_all_hosts);
1260 /* source address MUST be the link-local address */
1261 ip0->src_address = radv_info->link_local_address;
1263 ip0->hop_limit = 255;
1264 ip0->payload_length = clib_host_to_net_u16 (payload_length);
1266 icmp6_router_advertisement_header_t * rh0 = (icmp6_router_advertisement_header_t *)(ip0 + 1);
1267 rh0->icmp.checksum =
1268 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1270 ASSERT(bogus_length == 0);
1272 /* setup output if and adjacency */
1273 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1274 vnet_main.local_interface_sw_if_index;
1276 if (is_solicitation)
1278 ethernet_header_t *eth0;
1279 /* Reuse current MAC header, copy SMAC to DMAC and
1280 * interface MAC to SMAC */
1281 vlib_buffer_reset (p0);
1282 eth0 = vlib_buffer_get_current(p0);
1283 clib_memcpy(eth0->dst_address, eth0->src_address, 6);
1284 clib_memcpy(eth0->src_address, eth_if0->address, 6);
1285 next0 = is_dropped ?
1286 next0 : ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1287 vnet_buffer(p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1291 adj_index0 = radv_info->all_nodes_adj_index;
1292 if (adj_index0 == 0)
1293 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1296 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, adj_index0);
1298 ((adj0->rewrite_header.sw_if_index != sw_if_index0
1299 || adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
1300 ? ICMP6_ERROR_ROUTER_SOLICITATION_DEST_UNKNOWN
1302 next0 = is_dropped ?
1303 next0 : ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1304 vnet_buffer (p0)->ip.adj_index[VLIB_RX] = adj_index0;
1308 radv_info->n_solicitations_dropped += is_dropped;
1309 radv_info->n_solicitations_rcvd += is_solicitation;
1311 if((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1313 radv_info->n_advertisements_sent++;
1314 n_advertisements_sent++;
1320 p0->error = error_node->errors[error0];
1322 if(error0 != ICMP6_ERROR_NONE)
1323 vlib_error_count (vm, error_node->node_index, error0, 1);
1325 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1326 to_next, n_left_to_next,
1331 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1334 /* Account for router advertisements sent. */
1335 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX, n_advertisements_sent);
1337 return frame->n_vectors;
1340 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1341 static_always_inline uword
1342 icmp6_router_advertisement(vlib_main_t * vm,
1343 vlib_node_runtime_t * node,
1344 vlib_frame_t * frame)
1346 vnet_main_t * vnm = vnet_get_main();
1347 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1348 uword n_packets = frame->n_vectors;
1349 u32 * from, * to_next;
1350 u32 n_left_from, n_left_to_next, next_index;
1351 u32 n_advertisements_rcvd = 0;
1353 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1355 from = vlib_frame_vector_args (frame);
1356 n_left_from = n_packets;
1357 next_index = node->cached_next_index;
1359 if (node->flags & VLIB_NODE_FLAG_TRACE)
1360 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1362 sizeof (icmp6_input_trace_t));
1364 while (n_left_from > 0)
1366 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1368 while (n_left_from > 0 && n_left_to_next > 0)
1372 ip6_radv_t *radv_info = 0;
1373 icmp6_router_advertisement_header_t * h0;
1374 u32 bi0, options_len0, sw_if_index0, next0, error0;
1376 bi0 = to_next[0] = from[0];
1381 n_left_to_next -= 1;
1383 p0 = vlib_get_buffer (vm, bi0);
1384 ip0 = vlib_buffer_get_current (p0);
1385 h0 = ip6_next_header (ip0);
1386 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1388 error0 = ICMP6_ERROR_NONE;
1389 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1391 /* Check that source address is link-local*/
1392 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1393 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1395 /* default is to drop */
1396 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1398 n_advertisements_rcvd++;
1400 if (error0 == ICMP6_ERROR_NONE)
1402 vnet_sw_interface_t * sw_if0;
1403 ethernet_interface_t * eth_if0;
1405 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1406 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1407 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1409 /* only support ethernet interface type for now */
1410 error0 = (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF : error0;
1412 if (error0 == ICMP6_ERROR_NONE)
1416 /* look up the radv_t information for this interface */
1417 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1419 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1422 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1424 error0 = ((!radv_info) ? ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : error0);
1426 if (error0 == ICMP6_ERROR_NONE)
1428 /* validate advertised information */
1429 if((h0->current_hop_limit && radv_info->curr_hop_limit) &&
1430 (h0->current_hop_limit != radv_info->curr_hop_limit))
1432 ip6_neighbor_syslog(vm, LOG_WARNING,
1433 "our AdvCurHopLimit on %U doesn't agree with %U",
1434 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1437 if((h0->flags & ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP) !=
1438 radv_info->adv_managed_flag)
1440 ip6_neighbor_syslog(vm, LOG_WARNING,
1441 "our AdvManagedFlag on %U doesn't agree with %U",
1442 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1445 if((h0->flags & ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP) !=
1446 radv_info->adv_other_flag)
1448 ip6_neighbor_syslog(vm, LOG_WARNING,
1449 "our AdvOtherConfigFlag on %U doesn't agree with %U",
1450 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1453 if((h0->time_in_msec_between_retransmitted_neighbor_solicitations &&
1454 radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations) &&
1455 (h0->time_in_msec_between_retransmitted_neighbor_solicitations !=
1456 clib_host_to_net_u32(radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
1458 ip6_neighbor_syslog(vm, LOG_WARNING,
1459 "our AdvRetransTimer on %U doesn't agree with %U",
1460 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1463 if((h0->neighbor_reachable_time_in_msec &&
1464 radv_info->adv_neighbor_reachable_time_in_msec) &&
1465 (h0->neighbor_reachable_time_in_msec !=
1466 clib_host_to_net_u32(radv_info->adv_neighbor_reachable_time_in_msec)))
1468 ip6_neighbor_syslog(vm, LOG_WARNING,
1469 "our AdvReachableTime on %U doesn't agree with %U",
1470 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1473 /* check for MTU or prefix options or .. */
1474 u8 * opt_hdr = (u8 *)(h0 + 1);
1475 while( options_len0 > 0 &&
1476 opt_hdr < p0->data + p0->current_data)
1478 icmp6_neighbor_discovery_option_header_t *o0 = ( icmp6_neighbor_discovery_option_header_t *)opt_hdr;
1479 int opt_len = o0->n_data_u64s << 3;
1480 icmp6_neighbor_discovery_option_type_t option_type = o0->type;
1482 if(options_len0 < 2)
1484 ip6_neighbor_syslog(vm, LOG_ERR,
1485 "malformed RA packet on %U from %U",
1486 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1492 ip6_neighbor_syslog(vm, LOG_ERR,
1493 " zero length option in RA on %U from %U",
1494 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1497 else if( opt_len > options_len0)
1499 ip6_neighbor_syslog(vm, LOG_ERR,
1500 "option length in RA packet greater than total length on %U from %U",
1501 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1505 options_len0 -= opt_len;
1510 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
1512 icmp6_neighbor_discovery_mtu_option_t *h =
1513 (icmp6_neighbor_discovery_mtu_option_t *)(o0);
1515 if(opt_len < sizeof(*h))
1518 if((h->mtu && radv_info->adv_link_mtu) &&
1519 (h->mtu != clib_host_to_net_u32(radv_info->adv_link_mtu)))
1521 ip6_neighbor_syslog(vm, LOG_WARNING,
1522 "our AdvLinkMTU on %U doesn't agree with %U",
1523 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1528 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1530 icmp6_neighbor_discovery_prefix_information_option_t *h =
1531 (icmp6_neighbor_discovery_prefix_information_option_t *)(o0);
1533 /* validate advertised prefix options */
1534 ip6_radv_prefix_t *pr_info;
1535 u32 preferred, valid;
1537 if(opt_len < sizeof(*h))
1540 preferred = clib_net_to_host_u32(h->preferred_time);
1541 valid = clib_net_to_host_u32(h->valid_time);
1543 /* look for matching prefix - if we our advertising it, it better be consistant */
1544 pool_foreach (pr_info, radv_info->adv_prefixes_pool, ({
1547 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1549 if(pr_info->enabled &&
1550 (pr_info->prefix_len == h->dst_address_length) &&
1551 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1554 if(!pr_info->decrement_lifetime_flag &&
1555 valid != pr_info->adv_valid_lifetime_in_secs)
1557 ip6_neighbor_syslog(vm, LOG_WARNING,
1558 "our ADV validlifetime on %U for %U does not agree with %U",
1559 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1560 format_ip6_address, &h->dst_address);
1562 if(!pr_info->decrement_lifetime_flag &&
1563 preferred != pr_info->adv_pref_lifetime_in_secs)
1565 ip6_neighbor_syslog(vm, LOG_WARNING,
1566 "our ADV preferredlifetime on %U for %U does not agree with %U",
1567 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1568 format_ip6_address, &h->dst_address);
1584 p0->error = error_node->errors[error0];
1586 if(error0 != ICMP6_ERROR_NONE)
1587 vlib_error_count (vm, error_node->node_index, error0, 1);
1589 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1590 to_next, n_left_to_next,
1594 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1597 /* Account for router advertisements sent. */
1598 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX, n_advertisements_rcvd);
1600 return frame->n_vectors;
1603 /* create and initialize router advertisement parameters with default values for this intfc */
1605 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
1609 ip6_main_t * im = &ip6_main;
1610 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1611 ip_lookup_main_t * lm = &im->lookup_main;
1614 vnet_sw_interface_t * sw_if0;
1615 ethernet_interface_t * eth_if0 = 0;
1617 /* lookup radv container - ethernet interfaces only */
1618 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1619 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1620 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1625 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
1626 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1630 a = pool_elt_at_index (nm->if_radv_pool, ri);
1634 u32 i, * to_delete = 0;
1635 ip6_radv_prefix_t *p;
1636 ip6_mldp_group_t *m;
1638 /* remove adjacencies */
1639 ip_del_adjacency (lm, a->all_nodes_adj_index);
1640 ip_del_adjacency (lm, a->all_routers_adj_index);
1641 ip_del_adjacency (lm, a->all_mldv2_routers_adj_index);
1643 /* clean up prefix_pool */
1644 pool_foreach (p, a->adv_prefixes_pool, ({
1645 vec_add1 (to_delete, p - a->adv_prefixes_pool);
1648 for (i = 0; i < vec_len (to_delete); i++)
1650 p = pool_elt_at_index (a->adv_prefixes_pool, to_delete[i]);
1651 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
1652 pool_put (a->adv_prefixes_pool, p);
1655 vec_free (to_delete);
1658 /* clean up mldp group pool */
1659 pool_foreach (m, a->mldp_group_pool, ({
1660 vec_add1 (to_delete, m - a->mldp_group_pool);
1663 for (i = 0; i < vec_len (to_delete); i++)
1665 m = pool_elt_at_index (a->mldp_group_pool, to_delete[i]);
1666 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
1667 pool_put (a->mldp_group_pool, m);
1670 vec_free (to_delete);
1672 pool_put (nm->if_radv_pool, a);
1673 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
1681 vnet_hw_interface_t * hw_if0;
1683 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
1685 pool_get (nm->if_radv_pool, a);
1687 ri = a - nm->if_radv_pool;
1688 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
1690 /* initialize default values (most of which are zero) */
1691 memset (a, 0, sizeof (a[0]));
1693 a->sw_if_index = sw_if_index;
1695 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
1696 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
1697 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
1698 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
1700 a->adv_link_layer_address = 1; /* send ll address source address option */
1702 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
1703 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
1704 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
1705 a->seed = (u32) (clib_cpu_time_now() & 0xFFFFFFFF);
1707 /* for generating random interface ids */
1708 a->randomizer = random_u64 (&a->seed);
1710 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS ;
1711 a->initial_adverts_sent = a->initial_adverts_count-1;
1712 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1714 /* deafult is to send */
1717 /* fill in radv_info for this interface that will be needed later */
1718 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
1720 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
1722 /* fill in default link-local address (this may be overridden) */
1723 ip6_link_local_address_from_ethernet_address (&a->link_local_address, eth_if0->address);
1724 a->link_local_prefix_len = 64;
1726 mhash_init (&a->address_to_prefix_index, sizeof (uword), sizeof (ip6_address_t));
1727 mhash_init (&a->address_to_mldp_index, sizeof (uword), sizeof (ip6_address_t));
1730 ip_adjacency_t *adj;
1731 u8 link_layer_address[6] =
1732 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_all_hosts};
1734 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1735 &a->all_nodes_adj_index);
1737 adj->lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
1738 adj->if_address_index = ~0;
1740 vnet_rewrite_for_sw_interface
1742 VNET_L3_PACKET_TYPE_IP6,
1744 ip6_rewrite_node.index,
1746 &adj->rewrite_header,
1747 sizeof (adj->rewrite_data));
1751 ip_adjacency_t *adj;
1752 u8 link_layer_address[6] =
1753 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_all_routers};
1755 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1756 &a->all_routers_adj_index);
1758 adj->lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
1759 adj->if_address_index = ~0;
1761 vnet_rewrite_for_sw_interface
1763 VNET_L3_PACKET_TYPE_IP6,
1765 ip6_rewrite_node.index,
1767 &adj->rewrite_header,
1768 sizeof (adj->rewrite_data));
1772 ip_adjacency_t *adj;
1773 u8 link_layer_address[6] =
1774 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_mldv2_routers};
1776 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1777 &a->all_mldv2_routers_adj_index);
1779 adj->lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
1780 adj->if_address_index = ~0;
1782 vnet_rewrite_for_sw_interface
1784 VNET_L3_PACKET_TYPE_IP6,
1786 ip6_rewrite_node.index,
1788 &adj->rewrite_header,
1789 sizeof (adj->rewrite_data));
1792 /* add multicast groups we will always be reporting */
1794 ip6_mldp_group_t *mcast_group_info;
1796 ip6_set_reserved_multicast_address (&addr,
1797 IP6_MULTICAST_SCOPE_link_local,
1798 IP6_MULTICAST_GROUP_ID_all_hosts);
1800 /* lookup mldp info for this interface */
1802 uword * p = mhash_get (&a->address_to_mldp_index, &addr);
1803 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1806 if(!mcast_group_info)
1810 pool_get (a->mldp_group_pool, mcast_group_info);
1812 mi = mcast_group_info - a->mldp_group_pool;
1813 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1815 mcast_group_info->type = 4;
1816 mcast_group_info->mcast_source_address_pool = 0;
1817 mcast_group_info->num_sources = 0;
1818 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1821 ip6_set_reserved_multicast_address (&addr,
1822 IP6_MULTICAST_SCOPE_link_local,
1823 IP6_MULTICAST_GROUP_ID_all_routers);
1825 p = mhash_get (&a->address_to_mldp_index, &addr);
1826 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1828 if(!mcast_group_info)
1832 pool_get (a->mldp_group_pool, mcast_group_info);
1834 mi = mcast_group_info - a->mldp_group_pool;
1835 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1837 mcast_group_info->type = 4;
1838 mcast_group_info->mcast_source_address_pool = 0;
1839 mcast_group_info->num_sources = 0;
1840 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1843 ip6_set_reserved_multicast_address (&addr,
1844 IP6_MULTICAST_SCOPE_link_local,
1845 IP6_MULTICAST_GROUP_ID_mldv2_routers);
1847 p = mhash_get (&a->address_to_mldp_index, &addr);
1848 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1850 if(!mcast_group_info)
1854 pool_get (a->mldp_group_pool, mcast_group_info);
1856 mi = mcast_group_info - a->mldp_group_pool;
1857 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1859 mcast_group_info->type = 4;
1860 mcast_group_info->mcast_source_address_pool = 0;
1861 mcast_group_info->num_sources = 0;
1862 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1869 /* send an mldpv2 report */
1871 ip6_neighbor_send_mldpv2_report(u32 sw_if_index)
1873 vnet_main_t * vnm = vnet_get_main();
1874 vlib_main_t * vm = vnm->vlib_main;
1875 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1876 vnet_sw_interface_t * sw_if0;
1877 ethernet_interface_t * eth_if0;
1881 ip6_radv_t *radv_info;
1891 icmp6_multicast_listener_report_header_t *rh0;
1892 icmp6_multicast_listener_report_packet_t *rp0;
1894 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1895 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1896 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1898 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
1901 /* look up the radv_t information for this interface */
1902 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
1904 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1909 /* send report now - build a mldpv2 report packet */
1910 n_allocated = vlib_buffer_alloc_from_free_list(vm,
1913 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1914 if (PREDICT_FALSE(n_allocated == 0))
1916 clib_warning ("buffer allocation failure");
1920 b0 = vlib_get_buffer (vm, bo0);
1922 /* adjust the sizeof the buffer to just include the ipv6 header */
1923 b0->current_length = sizeof(icmp6_multicast_listener_report_packet_t);
1925 payload_length = sizeof(icmp6_multicast_listener_report_header_t);
1927 b0->error = ICMP6_ERROR_NONE;
1929 rp0 = vlib_buffer_get_current (b0);
1930 ip0 = (ip6_header_t *)&rp0-> ip;
1931 rh0 = (icmp6_multicast_listener_report_header_t *)&rp0-> report_hdr;
1933 memset (rp0 , 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
1935 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
1937 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
1938 /* for DEBUG - vnet driver won't seem to emit router alerts */
1939 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
1942 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
1944 /* source address MUST be the link-local address */
1945 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1946 ip0->src_address = radv_info->link_local_address;
1948 /* destination is all mldpv2 routers */
1949 ip6_set_reserved_multicast_address(&ip0->dst_address,
1950 IP6_MULTICAST_SCOPE_link_local,
1951 IP6_MULTICAST_GROUP_ID_mldv2_routers);
1953 /* add reports here */
1954 ip6_mldp_group_t *m;
1955 int num_addr_records = 0;
1956 icmp6_multicast_address_record_t rr;
1958 /* fill in the hop-by-hop extension header (router alert) info */
1959 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
1960 rh0->ext_hdr.n_data_u64s = 0;
1962 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
1964 rh0->alert.value = 0;
1969 rh0->icmp.checksum = 0;
1971 pool_foreach (m, radv_info->mldp_group_pool, ({
1974 rr.aux_data_len_u32s = 0;
1975 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
1976 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
1980 vlib_buffer_add_data (vm,
1981 b0->free_list_index,
1983 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
1985 payload_length += sizeof( icmp6_multicast_address_record_t);
1989 rh0->num_addr_records = clib_host_to_net_u16(num_addr_records);
1991 /* update lengths */
1992 ip0->payload_length = clib_host_to_net_u16 (payload_length);
1994 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
1996 ASSERT(bogus_length == 0);
1999 * OK to override w/ no regard for actual FIB, because
2000 * ip6-rewrite-local only looks at the adjacency.
2002 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
2003 vnet_main.local_interface_sw_if_index;
2005 vnet_buffer (b0)->ip.adj_index[VLIB_RX] =
2006 radv_info->all_mldv2_routers_adj_index;
2008 vlib_node_t * node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-local");
2010 f = vlib_get_frame_to_node (vm, node->index);
2011 to_next = vlib_frame_vector_args (f);
2015 vlib_put_frame_to_node (vm, node->index, f);
2019 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) = {
2020 .function = icmp6_router_solicitation,
2021 .name = "icmp6-router-solicitation",
2023 .vector_size = sizeof (u32),
2025 .format_trace = format_icmp6_input_trace,
2027 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2029 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "error-drop",
2030 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-local",
2031 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2035 /* send a RA or update the timer info etc.. */
2037 ip6_neighbor_process_timer_event (vlib_main_t * vm,
2038 vlib_node_runtime_t * node,
2039 vlib_frame_t * frame)
2041 vnet_main_t * vnm = vnet_get_main();
2042 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2043 ip6_radv_t *radv_info;
2044 vlib_frame_t * f = 0;
2045 u32 n_this_frame = 0;
2046 u32 n_left_to_next = 0;
2049 icmp6_router_solicitation_header_t * h0;
2051 f64 now = vlib_time_now (vm);
2053 /* Interface ip6 radv info list */
2054 pool_foreach (radv_info, nm->if_radv_pool, ({
2056 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2058 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2059 radv_info->next_multicast_time = now;
2060 radv_info->last_multicast_time = now;
2061 radv_info->last_radv_time = 0;
2062 radv_info->all_routers_mcast = 0;
2066 /* Make sure that we've joined the all-routers multicast group */
2067 if(!radv_info->all_routers_mcast)
2069 /* send MDLP_REPORT_EVENT message */
2070 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2071 radv_info->all_routers_mcast = 1;
2074 /* is it time to send a multicast RA on this interface? */
2075 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2080 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2081 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2083 /* multicast send - compute next multicast send time */
2084 if( radv_info->initial_adverts_sent > 0)
2086 radv_info->initial_adverts_sent--;
2087 if(rfn > radv_info-> initial_adverts_interval)
2088 rfn = radv_info-> initial_adverts_interval;
2090 /* check to see if we are ceasing to send */
2091 if( radv_info->initial_adverts_sent == 0)
2092 if(radv_info->cease_radv)
2093 radv_info->send_radv = 0;
2096 radv_info->next_multicast_time = rfn + now;
2097 radv_info->last_multicast_time = now;
2099 /* send advert now - build a "solicted" router advert with unspecified source address */
2100 n_allocated = vlib_buffer_alloc_from_free_list(vm,
2103 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2105 if (PREDICT_FALSE(n_allocated == 0))
2107 clib_warning ("buffer allocation failure");
2110 b0 = vlib_get_buffer (vm, bo0);
2111 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2112 b0->error = ICMP6_ERROR_NONE;
2113 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2115 h0 = vlib_buffer_get_current (b0);
2117 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2119 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2120 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2121 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2122 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2123 h0->ip.hop_limit = 255;
2125 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2126 h0->ip.src_address.as_u64[0] = 0;
2127 h0->ip.src_address.as_u64[1] = 0;
2129 h0->ip.dst_address.as_u64[0] = 0;
2130 h0->ip.dst_address.as_u64[1] = 0;
2132 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2134 if (PREDICT_FALSE(f == 0))
2136 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2137 to_next = vlib_frame_vector_args (f);
2138 n_left_to_next = VLIB_FRAME_SIZE;
2147 if (PREDICT_FALSE(n_left_to_next == 0))
2149 f->n_vectors = n_this_frame;
2150 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2158 ASSERT(n_this_frame);
2159 f->n_vectors = n_this_frame;
2160 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2166 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2167 vlib_node_runtime_t * node,
2168 vlib_frame_t * frame)
2171 ip6_icmp_neighbor_discovery_event_data_t * event_data;
2173 /* init code here */
2177 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */);
2179 event_data = vlib_process_get_event_data (vm, &event_type);
2183 /* No events found: timer expired. */
2184 /* process interface list and send RAs as appropriate, update timer info */
2185 ip6_neighbor_process_timer_event (vm, node, frame);
2189 switch (event_type) {
2191 case ICMP6_ND_EVENT_INIT:
2202 _vec_len (event_data) = 0;
2205 return frame->n_vectors;
2208 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) = {
2209 .function = icmp6_router_advertisement,
2210 .name = "icmp6-router-advertisement",
2212 .vector_size = sizeof (u32),
2214 .format_trace = format_icmp6_input_trace,
2222 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2224 .function = ip6_icmp_neighbor_discovery_event_process,
2225 .name = "ip6-icmp-neighbor-discovery-event-process",
2226 .type = VLIB_NODE_TYPE_PROCESS,
2230 icmp6_neighbor_solicitation (vlib_main_t * vm,
2231 vlib_node_runtime_t * node,
2232 vlib_frame_t * frame)
2233 { return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, /* is_solicitation */ 1); }
2236 icmp6_neighbor_advertisement (vlib_main_t * vm,
2237 vlib_node_runtime_t * node,
2238 vlib_frame_t * frame)
2239 { return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, /* is_solicitation */ 0); }
2241 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) = {
2242 .function = icmp6_neighbor_solicitation,
2243 .name = "icmp6-neighbor-solicitation",
2245 .vector_size = sizeof (u32),
2247 .format_trace = format_icmp6_input_trace,
2249 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2251 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "error-drop",
2252 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2256 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) = {
2257 .function = icmp6_neighbor_advertisement,
2258 .name = "icmp6-neighbor-advertisement",
2260 .vector_size = sizeof (u32),
2262 .format_trace = format_icmp6_input_trace,
2270 /* API support functions */
2272 ip6_neighbor_ra_config(vlib_main_t * vm, u32 sw_if_index,
2273 u8 suppress, u8 managed, u8 other,
2274 u8 ll_option, u8 send_unicast, u8 cease,
2275 u8 use_lifetime, u32 lifetime,
2276 u32 initial_count, u32 initial_interval,
2277 u32 max_interval, u32 min_interval,
2280 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2284 /* look up the radv_t information for this interface */
2285 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2286 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2287 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2292 ip6_radv_t * radv_info;
2293 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2295 if((max_interval != 0) && (min_interval ==0))
2296 min_interval = .75 * max_interval;
2298 max_interval = (max_interval != 0) ? ( (is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) : radv_info->max_radv_interval;
2299 min_interval = (min_interval != 0) ? ( (is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) : radv_info->min_radv_interval;
2300 lifetime = (use_lifetime != 0) ? ( (is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) : radv_info->adv_router_lifetime_in_sec;
2304 if(lifetime > MAX_DEF_RTR_LIFETIME)
2305 lifetime = MAX_DEF_RTR_LIFETIME;
2307 if(lifetime <= max_interval)
2308 return VNET_API_ERROR_INVALID_VALUE;
2311 if(min_interval != 0)
2313 if((min_interval > .75 * max_interval) ||
2315 return VNET_API_ERROR_INVALID_VALUE;
2318 if((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2319 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2320 return VNET_API_ERROR_INVALID_VALUE;
2323 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2324 if "flag" is clear don't change corresponding value
2326 radv_info->send_radv = (suppress != 0) ? ( (is_no != 0) ? 1 : 0 ) : radv_info->send_radv;
2327 radv_info->adv_managed_flag = ( managed != 0) ? ( (is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2328 radv_info->adv_other_flag = (other != 0) ? ( (is_no) ? 0: 1) : radv_info->adv_other_flag;
2329 radv_info->adv_link_layer_address = ( ll_option != 0) ? ( (is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2330 radv_info->send_unicast = (send_unicast != 0) ? ( (is_no) ? 0 : 1) : radv_info->send_unicast;
2331 radv_info->cease_radv = ( cease != 0) ? ( (is_no) ? 0 : 1) : radv_info->cease_radv;
2333 radv_info->min_radv_interval = min_interval;
2334 radv_info->max_radv_interval = max_interval;
2335 radv_info->adv_router_lifetime_in_sec = lifetime;
2337 radv_info->initial_adverts_count =
2338 (initial_count != 0) ? ( (is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) : radv_info->initial_adverts_count ;
2339 radv_info->initial_adverts_interval =
2340 (initial_interval != 0) ? ( (is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) : radv_info->initial_adverts_interval;
2343 if((cease != 0) && (is_no))
2344 radv_info-> send_radv = 1;
2346 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2347 radv_info->next_multicast_time = vlib_time_now (vm);
2348 radv_info->last_multicast_time = vlib_time_now (vm);
2349 radv_info->last_radv_time = 0;
2355 ip6_neighbor_ra_prefix(vlib_main_t * vm, u32 sw_if_index,
2356 ip6_address_t *prefix_addr, u8 prefix_len,
2357 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2358 u8 no_advertise, u8 off_link, u8 no_autoconfig, u8 no_onlink,
2361 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2366 /* look up the radv_t information for this interface */
2367 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2369 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2371 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2375 f64 now = vlib_time_now (vm);
2376 ip6_radv_t * radv_info;
2377 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2379 /* prefix info add, delete or update */
2380 ip6_radv_prefix_t * prefix;
2382 /* lookup prefix info for this address on this interface */
2383 uword * p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2385 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2391 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
2393 if(prefix->prefix_len != prefix_len)
2394 return VNET_API_ERROR_INVALID_VALUE_2;
2396 /* FIXME - Should the DP do this or the CP ?*/
2397 /* do specific delete processing here before returning */
2398 /* try to remove from routing table */
2400 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,/* old_value */ 0);
2401 pool_put (radv_info->adv_prefixes_pool, prefix);
2403 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2404 radv_info->next_multicast_time = vlib_time_now (vm);
2405 radv_info->last_multicast_time = vlib_time_now (vm);
2406 radv_info->last_radv_time = 0;
2410 /* adding or changing */
2415 pool_get (radv_info->adv_prefixes_pool, prefix);
2416 pi = prefix - radv_info->adv_prefixes_pool;
2417 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi, /* old_value */ 0);
2419 memset(prefix, 0x0, sizeof(ip6_radv_prefix_t));
2421 prefix->prefix_len = prefix_len;
2422 clib_memcpy(&prefix->prefix, prefix_addr, sizeof(ip6_address_t));
2424 /* initialize default values */
2425 prefix->adv_on_link_flag = 1; /* L bit set */
2426 prefix->adv_autonomous_flag = 1; /* A bit set */
2427 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
2428 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2429 prefix->enabled = 1;
2430 prefix->decrement_lifetime_flag = 1;
2431 prefix->deprecated_prefix_flag = 1;
2435 /* FIXME - Should the DP do this or the CP ?*/
2436 /* insert prefix into routing table as a connected prefix */
2445 if(prefix->prefix_len != prefix_len)
2446 return VNET_API_ERROR_INVALID_VALUE_2;
2450 /* FIXME - Should the DP do this or the CP ?*/
2451 /* remove from routing table if already there */
2455 if((val_lifetime == ~0) || (pref_lifetime == ~0))
2457 prefix->adv_valid_lifetime_in_secs = ~0;
2458 prefix->adv_pref_lifetime_in_secs = ~0;
2459 prefix->decrement_lifetime_flag = 0;
2463 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2464 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
2467 /* copy remaining */
2468 prefix->enabled = !(no_advertise != 0);
2469 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2470 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2474 /* fill in the expiration times */
2475 prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs;
2476 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
2478 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2479 radv_info->next_multicast_time = vlib_time_now (vm);
2480 radv_info->last_multicast_time = vlib_time_now (vm);
2481 radv_info->last_radv_time = 0;
2487 ip6_neighbor_cmd(vlib_main_t * vm, unformat_input_t * main_input, vlib_cli_command_t * cmd)
2489 vnet_main_t * vnm = vnet_get_main();
2490 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2491 clib_error_t * error = 0;
2493 u8 suppress = 0, managed = 0, other = 0;
2494 u8 suppress_ll_option = 0, send_unicast = 0, cease= 0;
2495 u8 use_lifetime = 0;
2496 u32 sw_if_index, ra_lifetime = 0, ra_initial_count = 0, ra_initial_interval = 0;
2497 u32 ra_max_interval = 0 , ra_min_interval = 0;
2499 unformat_input_t _line_input, * line_input = &_line_input;
2500 vnet_sw_interface_t * sw_if0;
2502 int add_radv_info = 1;
2503 __attribute__((unused)) ip6_radv_t * radv_info = 0;
2504 ip6_address_t ip6_addr;
2508 /* Get a line of input. */
2509 if (! unformat_user (main_input, unformat_line_input, line_input))
2512 /* get basic radv info for this interface */
2513 if(unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2516 if (unformat_user (line_input,
2517 unformat_vnet_sw_interface, vnm, &sw_if_index))
2520 ethernet_interface_t * eth_if0 = 0;
2522 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2523 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2524 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2528 error = clib_error_return (0, "Interface must be of ethernet type");
2532 /* look up the radv_t information for this interface */
2533 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2535 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2539 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2543 error = clib_error_return (0, "unknown interface %U'",
2544 format_unformat_error, line_input);
2550 error = clib_error_return (0, "invalid interface name %U'",
2551 format_unformat_error, line_input);
2556 /* get the rest of the command */
2557 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2559 if (unformat (line_input, "no"))
2561 else if(unformat (line_input, "prefix %U/%d",
2562 unformat_ip6_address, &ip6_addr,
2568 else if (unformat (line_input, "ra-managed-config-flag"))
2573 else if (unformat (line_input, "ra-other-config-flag"))
2578 else if (unformat (line_input, "ra-suppress") ||
2579 unformat (line_input, "ra-surpress"))
2584 else if (unformat (line_input, "ra-suppress-link-layer") ||
2585 unformat (line_input, "ra-surpress-link-layer"))
2587 suppress_ll_option = 1;
2590 else if (unformat (line_input, "ra-send-unicast"))
2595 else if (unformat (line_input, "ra-lifetime"))
2597 if (!unformat (line_input, "%d", &ra_lifetime))
2598 return(error = unformat_parse_error (line_input));
2602 else if (unformat (line_input, "ra-initial"))
2604 if (!unformat (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
2605 return(error = unformat_parse_error (line_input));
2608 else if (unformat (line_input, "ra-interval"))
2610 if (!unformat (line_input, "%d", &ra_max_interval))
2611 return(error = unformat_parse_error (line_input));
2613 if (!unformat (line_input, "%d", &ra_min_interval))
2614 ra_min_interval = 0;
2617 else if(unformat (line_input, "ra-cease"))
2623 return(unformat_parse_error (line_input));
2628 ip6_neighbor_ra_config(vm, sw_if_index,
2629 suppress, managed, other,
2630 suppress_ll_option, send_unicast, cease,
2631 use_lifetime, ra_lifetime,
2632 ra_initial_count, ra_initial_interval,
2633 ra_max_interval, ra_min_interval,
2638 u32 valid_lifetime_in_secs = 0;
2639 u32 pref_lifetime_in_secs = 0;
2640 u8 use_prefix_default_values = 0;
2641 u8 no_advertise = 0;
2643 u8 no_autoconfig = 0;
2646 /* get the rest of the command */
2647 while(unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2649 if(unformat (line_input, "default"))
2651 use_prefix_default_values = 1;
2654 else if(unformat (line_input, "infinite"))
2656 valid_lifetime_in_secs = ~0;
2657 pref_lifetime_in_secs = ~0;
2660 else if(unformat (line_input, "%d %d", &valid_lifetime_in_secs,
2661 &pref_lifetime_in_secs))
2668 /* get the rest of the command */
2669 while (!use_prefix_default_values &&
2670 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2672 if(unformat (line_input, "no-advertise"))
2674 else if(unformat (line_input, "off-link"))
2676 else if(unformat (line_input, "no-autoconfig"))
2678 else if(unformat (line_input, "no-onlink"))
2681 return(unformat_parse_error (line_input));
2684 ip6_neighbor_ra_prefix(vm, sw_if_index,
2685 &ip6_addr, addr_len,
2686 use_prefix_default_values,
2687 valid_lifetime_in_secs,
2688 pref_lifetime_in_secs,
2696 unformat_free (line_input);
2703 ip6_print_addrs(vlib_main_t * vm,
2706 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2709 for (i = 0; i < vec_len (addrs); i++)
2711 ip_interface_address_t * a = pool_elt_at_index(lm->if_address_pool, addrs[i]);
2712 ip6_address_t * address = ip_interface_address_get_address (lm, a);
2714 vlib_cli_output (vm, "\t\t%U/%d",
2715 format_ip6_address, address,
2720 static clib_error_t *
2721 show_ip6_interface_cmd (vlib_main_t * vm,
2722 unformat_input_t * input,
2723 vlib_cli_command_t * cmd)
2725 vnet_main_t * vnm = vnet_get_main();
2726 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2727 clib_error_t * error = 0;
2732 if (unformat_user (input,
2733 unformat_vnet_sw_interface, vnm, &sw_if_index))
2737 /* look up the radv_t information for this interface */
2738 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2740 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2744 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2745 ip6_radv_t * radv_info;
2746 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2748 vlib_cli_output (vm, "%U is admin %s\n", format_vnet_sw_interface_name, vnm,
2749 vnet_get_sw_interface (vnm, sw_if_index),
2750 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ? "up" : "down"));
2753 u32 *link_scope = 0, *global_scope = 0;
2754 u32 *local_scope = 0, *unknown_scope = 0;
2755 ip_interface_address_t * a;
2757 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
2758 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
2760 while (ai != (u32)~0)
2762 a = pool_elt_at_index(lm->if_address_pool, ai);
2763 ip6_address_t * address = ip_interface_address_get_address (lm, a);
2765 if (ip6_address_is_link_local_unicast (address))
2766 vec_add1 (link_scope, ai);
2767 else if(ip6_address_is_global_unicast (address))
2768 vec_add1 (global_scope, ai);
2769 else if(ip6_address_is_local_unicast (address))
2770 vec_add1 (local_scope, ai);
2772 vec_add1 (unknown_scope, ai);
2774 ai = a->next_this_sw_interface;
2777 if (vec_len (link_scope))
2779 vlib_cli_output (vm, "\tLink-local address(es):\n");
2780 ip6_print_addrs (vm, link_scope);
2781 vec_free (link_scope);
2784 if (vec_len (local_scope))
2786 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
2787 ip6_print_addrs (vm, local_scope);
2788 vec_free (local_scope);
2791 if (vec_len (global_scope))
2793 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
2794 ip6_print_addrs (vm, global_scope);
2795 vec_free (global_scope);
2798 if (vec_len (unknown_scope))
2800 vlib_cli_output (vm, "\tOther-scope address(es):\n");
2801 ip6_print_addrs (vm, unknown_scope);
2802 vec_free (unknown_scope);
2805 vlib_cli_output (vm, "\tJoined group address(es):\n");
2806 ip6_mldp_group_t *m;
2807 pool_foreach (m, radv_info->mldp_group_pool, ({
2808 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address, &m->mcast_address);
2811 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
2812 ip6_radv_prefix_t * p;
2813 pool_foreach (p, radv_info->adv_prefixes_pool, ({
2814 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
2815 format_ip6_address, &p->prefix, p->prefix_len);
2818 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
2819 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
2820 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
2821 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
2822 vlib_cli_output (vm, "\tND DAD is disabled\n");
2823 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
2824 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
2825 radv_info->adv_neighbor_reachable_time_in_msec);
2826 vlib_cli_output (vm, "\tND advertised retransmit interval is %d (msec)\n",
2827 radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations);
2829 u32 ra_interval = radv_info->max_radv_interval;
2830 u32 ra_interval_min = radv_info->min_radv_interval;
2831 vlib_cli_output (vm, "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
2832 ra_interval, ra_interval_min);
2833 vlib_cli_output (vm, "\tND router advertisements live for %d seconds\n",
2834 radv_info->adv_router_lifetime_in_sec);
2835 vlib_cli_output (vm, "\tHosts %s stateless autoconfig for addresses\n",
2836 (radv_info->adv_managed_flag) ? "use" :" don't use");
2837 vlib_cli_output (vm, "\tND router advertisements sent %d\n", radv_info->n_advertisements_sent);
2838 vlib_cli_output (vm, "\tND router solicitations received %d\n", radv_info->n_solicitations_rcvd);
2839 vlib_cli_output (vm, "\tND router solicitations dropped %d\n", radv_info->n_solicitations_dropped);
2843 error = clib_error_return (0, "IPv6 not enabled on interface",
2844 format_unformat_error, input);
2851 VLIB_CLI_COMMAND (show_ip6_interface_command, static) = {
2852 .path = "show ip6 interface",
2853 .function = show_ip6_interface_cmd,
2854 .short_help = "show ip6 interface <iface name>",
2858 disable_ip6_interface(vlib_main_t * vm,
2861 clib_error_t * error = 0;
2862 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2865 /* look up the radv_t information for this interface */
2866 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];
2869 /* if not created - do nothing */
2872 vnet_main_t * vnm = vnet_get_main();
2873 ip6_radv_t * radv_info;
2875 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2877 /* check radv_info ref count for other ip6 addresses on this interface */
2878 if(radv_info->ref_count == 0 )
2880 /* essentially "disables" ipv6 on this interface */
2881 error = ip6_add_del_interface_address (vm, sw_if_index,
2882 &radv_info->link_local_address,
2883 radv_info->link_local_prefix_len,
2886 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, 0/* is_add */);
2893 ip6_interface_enabled(vlib_main_t * vm,
2896 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2899 /* look up the radv_t information for this interface */
2900 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2902 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2908 enable_ip6_interface(vlib_main_t * vm,
2911 clib_error_t * error = 0;
2912 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2916 /* look up the radv_t information for this interface */
2917 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2919 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2921 /* if not created yet */
2924 vnet_main_t * vnm = vnet_get_main();
2925 vnet_sw_interface_t * sw_if0;
2927 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2928 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2930 ethernet_interface_t * eth_if0;
2932 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2935 /* create radv_info. for this interface. This holds all the info needed for router adverts */
2936 ri = ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
2940 ip6_radv_t * radv_info;
2941 ip6_address_t link_local_address;
2943 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2945 ip6_link_local_address_from_ethernet_mac_address (&link_local_address,
2948 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
2949 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB)
2951 /* make up an interface id */
2955 link_local_address.as_u64[0] = radv_info->randomizer;
2958 md5_add (&m, &link_local_address, 16);
2959 md5_finish (&m, digest);
2961 clib_memcpy(&link_local_address, digest, 16);
2963 radv_info->randomizer = link_local_address.as_u64[0];
2965 link_local_address.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2967 link_local_address.as_u8[8] &= 0xfd;
2970 /* essentially "enables" ipv6 on this interface */
2971 error = ip6_add_del_interface_address (vm, sw_if_index,
2972 &link_local_address, 64 /* address width */,
2976 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, !is_add);
2979 radv_info->link_local_address = link_local_address;
2980 radv_info->link_local_prefix_len = 64;
2989 static clib_error_t *
2990 enable_ip6_interface_cmd (vlib_main_t * vm,
2991 unformat_input_t * input,
2992 vlib_cli_command_t * cmd)
2994 vnet_main_t * vnm = vnet_get_main();
2995 clib_error_t * error = 0;
3000 if (unformat_user (input,
3001 unformat_vnet_sw_interface, vnm, &sw_if_index))
3003 enable_ip6_interface(vm, sw_if_index);
3007 error = clib_error_return (0, "unknown interface\n'",
3008 format_unformat_error, input);
3014 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) = {
3015 .path = "enable ip6 interface",
3016 .function = enable_ip6_interface_cmd,
3017 .short_help = "enable ip6 interface <iface name>",
3020 static clib_error_t *
3021 disable_ip6_interface_cmd (vlib_main_t * vm,
3022 unformat_input_t * input,
3023 vlib_cli_command_t * cmd)
3025 vnet_main_t * vnm = vnet_get_main();
3026 clib_error_t * error = 0;
3031 if (unformat_user (input,
3032 unformat_vnet_sw_interface, vnm, &sw_if_index))
3034 error = disable_ip6_interface(vm, sw_if_index);
3038 error = clib_error_return (0, "unknown interface\n'",
3039 format_unformat_error, input);
3045 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) = {
3046 .path = "disable ip6 interface",
3047 .function = disable_ip6_interface_cmd,
3048 .short_help = "disable ip6 interface <iface name>",
3051 VLIB_CLI_COMMAND (ip6_nd_command, static) = {
3053 .short_help = "Set ip6 neighbor discovery parameters",
3054 .function = ip6_neighbor_cmd,
3058 set_ip6_link_local_address(vlib_main_t * vm,
3060 ip6_address_t *address,
3063 clib_error_t * error = 0;
3064 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3066 ip6_radv_t * radv_info;
3067 vnet_main_t * vnm = vnet_get_main();
3069 if( !ip6_address_is_link_local_unicast (address))
3071 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
3072 return(error = clib_error_return (0, "address not link-local",
3073 format_unformat_error));
3076 /* call enable ipv6 */
3077 enable_ip6_interface(vm, sw_if_index);
3079 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3083 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3085 /* save if link local address (overwrite default) */
3087 /* delete the old one */
3088 error = ip6_add_del_interface_address (vm, sw_if_index,
3089 &radv_info->link_local_address,
3090 radv_info->link_local_prefix_len /* address width */,
3095 /* add the new one */
3096 error = ip6_add_del_interface_address (vm, sw_if_index,
3098 address_length /* address width */,
3103 radv_info->link_local_address = *address;
3104 radv_info->link_local_prefix_len = address_length;
3110 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3111 error = clib_error_return (0, "ip6 not enabled for interface",
3112 format_unformat_error);
3118 set_ip6_link_local_address_cmd (vlib_main_t * vm,
3119 unformat_input_t * input,
3120 vlib_cli_command_t * cmd)
3122 vnet_main_t * vnm = vnet_get_main();
3123 clib_error_t * error = 0;
3125 ip6_address_t ip6_addr;
3128 if (unformat_user (input,
3129 unformat_vnet_sw_interface, vnm, &sw_if_index))
3131 /* get the rest of the command */
3132 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3134 if(unformat (input, "%U/%d",
3135 unformat_ip6_address, &ip6_addr,
3139 return(unformat_parse_error (input));
3142 error = set_ip6_link_local_address(vm,
3149 VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) = {
3150 .path = "set ip6 link-local address",
3151 .short_help = "Set ip6 interface link-local address <intfc> <address.>",
3152 .function = set_ip6_link_local_address_cmd,
3155 /* callback when an interface address is added or deleted */
3157 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3160 ip6_address_t * address,
3162 u32 if_address_index,
3165 vnet_main_t * vnm = vnet_get_main();
3166 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3168 vlib_main_t * vm = vnm->vlib_main;
3169 ip6_radv_t * radv_info;
3171 ip6_mldp_group_t *mcast_group_info;
3173 /* create solicited node multicast address for this interface adddress */
3174 ip6_set_solicited_node_multicast_address (&a, 0);
3176 a.as_u8[0xd] = address->as_u8[0xd];
3177 a.as_u8[0xe] = address->as_u8[0xe];
3178 a.as_u8[0xf] = address->as_u8[0xf];
3182 /* try to create radv_info - does nothing if ipv6 already enabled */
3183 enable_ip6_interface(vm, sw_if_index);
3185 /* look up the radv_t information for this interface */
3186 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
3187 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3191 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3194 if( !ip6_address_is_link_local_unicast (address))
3195 radv_info->ref_count++;
3197 /* lookup prefix info for this address on this interface */
3198 uword * p = mhash_get (&radv_info->address_to_mldp_index, &a);
3199 mcast_group_info = p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3201 /* add -solicted node multicast address */
3202 if(!mcast_group_info)
3206 pool_get (radv_info->mldp_group_pool, mcast_group_info);
3208 mi = mcast_group_info - radv_info->mldp_group_pool;
3209 mhash_set (&radv_info->address_to_mldp_index, &a, mi, /* old_value */ 0);
3211 mcast_group_info->type = 4;
3212 mcast_group_info->mcast_source_address_pool = 0;
3213 mcast_group_info->num_sources = 0;
3214 clib_memcpy(&mcast_group_info->mcast_address, &a, sizeof(ip6_address_t));
3222 /* look up the radv_t information for this interface */
3223 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
3224 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3228 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3230 /* lookup prefix info for this address on this interface */
3231 uword * p = mhash_get (&radv_info->address_to_mldp_index, &a);
3232 mcast_group_info = p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3234 if(mcast_group_info)
3236 mhash_unset (&radv_info->address_to_mldp_index, &a,/* old_value */ 0);
3237 pool_put (radv_info->mldp_group_pool, mcast_group_info);
3240 /* if interface up send MLDP "report" */
3241 radv_info->all_routers_mcast = 0;
3244 if( !ip6_address_is_link_local_unicast (address))
3245 radv_info->ref_count--;
3250 clib_error_t *ip6_set_neighbor_limit (u32 neighbor_limit)
3252 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3254 nm->limit_neighbor_cache_size = neighbor_limit;
3260 ip6_neighbor_entry_del_adj(ip6_neighbor_t *n, u32 adj_index)
3266 vec_foreach_index(i, n->adjacencies)
3267 if (vec_elt(n->adjacencies, i) == adj_index)
3269 vec_del1(n->adjacencies, i);
3277 ip6_neighbor_entry_add_adj(ip6_neighbor_t *n, u32 adj_index)
3280 vec_foreach_index(i, n->adjacencies)
3281 if (vec_elt(n->adjacencies, i) == adj_index)
3283 vec_add1(n->adjacencies, adj_index);
3287 ip6_neighbor_add_del_adj_cb (struct ip_lookup_main_t * lm,
3289 ip_adjacency_t * adj,
3292 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3293 ip6_neighbor_key_t k;
3294 ip6_neighbor_t *n = 0;
3298 for(ai = adj->heap_handle; ai < adj->heap_handle + adj->n_adj ; ai++)
3300 adj = ip_get_adjacency (lm, ai);
3301 if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
3302 (adj->arp.next_hop.ip6.as_u64[0] || adj->arp.next_hop.ip6.as_u64[1]))
3304 k.sw_if_index = adj->rewrite_header.sw_if_index;
3305 k.ip6_address.as_u64[0] = adj->arp.next_hop.ip6.as_u64[0];
3306 k.ip6_address.as_u64[1] = adj->arp.next_hop.ip6.as_u64[1];
3308 p = mhash_get (&nm->neighbor_index_by_key, &k);
3310 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
3318 clib_warning("Adjacency contains unknown ND next hop %U (del)",
3319 format_ip46_address, &adj->arp.next_hop, IP46_TYPE_IP6);
3321 ip6_neighbor_entry_del_adj(n, adj->heap_handle);
3326 clib_warning("Adjacency contains unknown ND next hop %U (add)",
3327 format_ip46_address, &adj->arp.next_hop, IP46_TYPE_IP6);
3329 ip6_neighbor_entry_add_adj(n, adj->heap_handle);
3334 static clib_error_t * ip6_neighbor_init (vlib_main_t * vm)
3336 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3337 ip6_main_t * im = &ip6_main;
3338 ip_lookup_main_t * lm = &im->lookup_main;
3340 mhash_init (&nm->neighbor_index_by_key,
3341 /* value size */ sizeof (uword),
3342 /* key size */ sizeof (ip6_neighbor_key_t));
3344 icmp6_register_type (vm, ICMP6_neighbor_solicitation, ip6_icmp_neighbor_solicitation_node.index);
3345 icmp6_register_type (vm, ICMP6_neighbor_advertisement, ip6_icmp_neighbor_advertisement_node.index);
3346 icmp6_register_type (vm, ICMP6_router_solicitation, ip6_icmp_router_solicitation_node.index);
3347 icmp6_register_type (vm, ICMP6_router_advertisement, ip6_icmp_router_advertisement_node.index);
3349 /* handler node for ip6 neighbor discovery events and timers */
3350 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3352 /* add call backs */
3353 ip6_add_del_interface_address_callback_t cb;
3354 memset(&cb, 0x0, sizeof(ip6_add_del_interface_address_callback_t));
3356 /* when an interface address changes... */
3357 cb.function = ip6_neighbor_add_del_interface_address;
3358 cb.function_opaque = 0;
3359 vec_add1 (im->add_del_interface_address_callbacks, cb);
3361 mhash_init (&nm->pending_resolutions_by_address,
3362 /* value size */ sizeof (uword),
3363 /* key size */ sizeof (ip6_address_t));
3365 mhash_init (&nm->mac_changes_by_address,
3366 /* value size */ sizeof (uword),
3367 /* key size */ sizeof (ip6_address_t));
3369 /* default, configurable */
3370 nm->limit_neighbor_cache_size = 50000;
3373 /* $$$$ Hack fix for today */
3374 vec_validate_init_empty
3375 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */);
3378 ip_register_add_del_adjacency_callback(lm, ip6_neighbor_add_del_adj_cb);
3383 VLIB_INIT_FUNCTION (ip6_neighbor_init);
3386 void vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
3392 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3393 ip6_address_t * address = address_arg;
3395 pending_resolution_t * pr;
3397 pool_get (nm->pending_resolutions, pr);
3399 pr->next_index = ~0;
3400 pr->node_index = node_index;
3401 pr->type_opaque = type_opaque;
3404 p = mhash_get (&nm->pending_resolutions_by_address, address);
3407 /* Insert new resolution at the head of the list */
3408 pr->next_index = p[0];
3409 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
3412 mhash_set (&nm->pending_resolutions_by_address, address,
3413 pr - nm->pending_resolutions, 0 /* old value */);
3416 int vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
3417 void * data_callback,
3425 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3426 ip6_address_t * address = address_arg;
3428 pending_resolution_t * mc;
3429 void (*fp)(u32, u8 *) = data_callback;
3433 pool_get (nm->mac_changes, mc);
3435 mc->next_index = ~0;
3436 mc->node_index = node_index;
3437 mc->type_opaque = type_opaque;
3439 mc->data_callback = data_callback;
3442 p = mhash_get (&nm->mac_changes_by_address, address);
3445 /* Insert new resolution at the head of the list */
3446 mc->next_index = p[0];
3447 mhash_unset (&nm->mac_changes_by_address, address, 0);
3450 mhash_set (&nm->mac_changes_by_address, address,
3451 mc - nm->mac_changes, 0);
3457 pending_resolution_t * mc_last = 0;
3459 p = mhash_get (&nm->mac_changes_by_address, address);
3461 return VNET_API_ERROR_NO_SUCH_ENTRY;
3465 while (index != (u32)~0)
3467 mc = pool_elt_at_index (nm->mac_changes, index);
3468 if (mc->node_index == node_index &&
3469 mc->type_opaque == type_opaque &&
3472 /* Clients may need to clean up pool entries, too */
3474 (*fp)(mc->data, 0 /* no new mac addrs */);
3477 mhash_unset (&nm->mac_changes_by_address, address, 0);
3478 if (mc->next_index != ~0)
3479 mhash_set (&nm->mac_changes_by_address, address,
3481 pool_put (nm->mac_changes, mc);
3487 mc_last->next_index = mc->next_index;
3488 pool_put (nm->mac_changes, mc);
3493 index = mc->next_index;
3496 return VNET_API_ERROR_NO_SUCH_ENTRY;
3500 int vnet_ip6_nd_term (vlib_main_t * vm,
3501 vlib_node_runtime_t * node,
3503 ethernet_header_t * eth,
3509 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3510 icmp6_neighbor_solicitation_or_advertisement_header_t * ndh;
3511 pending_resolution_t * mc;
3514 ndh = ip6_next_header (ip);
3515 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
3516 ndh->icmp.type != ICMP6_neighbor_advertisement)
3519 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
3520 (p0->flags & VLIB_BUFFER_IS_TRACED)))
3522 u8 *t0 = vlib_add_trace (vm, node, p0,
3523 sizeof (icmp6_input_trace_t));
3524 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
3527 /* Check if anyone want ND events for L2 BDs */
3528 p = mhash_get (&nm->mac_changes_by_address, &ip6a_zero);
3530 { /* Only SHG 0 interface which is more likely local */
3531 u32 next_index = p[0];
3532 while (next_index != (u32)~0)
3534 int (*fp)(u32, u8 *, u32, ip6_address_t *);
3536 mc = pool_elt_at_index (nm->mac_changes, next_index);
3537 fp = mc->data_callback;
3538 /* Call the callback, return 1 to suppress dup events */
3539 if (fp) rv = (*fp)(mc->data,
3543 /* Signal the resolver process */
3545 vlib_process_signal_event (vm, mc->node_index,
3548 next_index = mc->next_index;
3552 /* Check if MAC entry exsist for solicited target IP */
3553 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
3555 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * opt;
3556 l2_bridge_domain_t *bd_config;
3559 opt = (void *) (ndh + 1);
3560 if ((opt->header.type !=
3561 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
3562 (opt->header.n_data_u64s != 1))
3563 return 0; /* source link layer address option not present */
3565 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
3566 macp = (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
3568 { /* found ip-mac entry, generate eighbor advertisement response */
3570 vlib_node_runtime_t * error_node =
3571 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
3572 ip->dst_address = ip->src_address;
3573 ip->src_address = ndh->target_address;
3574 ip->hop_limit = 255;
3576 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
3577 clib_memcpy (opt->ethernet_address, macp, 6);
3578 ndh->icmp.type = ICMP6_neighbor_advertisement;
3579 ndh->advertisement_flags = clib_host_to_net_u32
3580 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
3581 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
3582 ndh->icmp.checksum = 0;
3583 ndh->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum(vm, p0, ip,
3585 clib_memcpy(eth->dst_address, eth->src_address, 6);
3586 clib_memcpy(eth->src_address, macp, 6);
3587 vlib_error_count (vm, error_node->node_index,
3588 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);