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>
22 #include <vnet/adj/adj.h>
23 #include <vnet/fib/fib_table.h>
24 #include <vnet/fib/ip6_fib.h>
27 #include <vnet/devices/dpdk/dpdk.h>
31 ip6_address_t ip6_address;
36 /* can't use sizeof link_layer_address, that's 8 */
37 #define ETHER_MAC_ADDR_LEN 6
40 ip6_neighbor_key_t key;
41 u8 link_layer_address[8];
43 #define IP6_NEIGHBOR_FLAG_STATIC (1 << 0)
44 #define IP6_NEIGHBOR_FLAG_DYNAMIC (2 << 0)
45 u64 cpu_time_last_updated;
46 adj_index_t adj_index;
49 /* advertised prefix option */
51 /* basic advertised information */
55 int adv_autonomous_flag;
56 u32 adv_valid_lifetime_in_secs;
57 u32 adv_pref_lifetime_in_secs;
59 /* advertised values are computed from these times if decrementing */
60 f64 valid_lifetime_expires;
61 f64 pref_lifetime_expires;
63 /* local information */
65 int deprecated_prefix_flag;
66 int decrement_lifetime_flag;
68 #define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
69 #define DEF_ADV_VALID_LIFETIME 2592000
70 #define DEF_ADV_PREF_LIFETIME 604800
72 /* extensions are added here, mobile, DNS etc.. */
77 /* group information */
79 ip6_address_t mcast_address;
81 ip6_address_t *mcast_source_address_pool;
84 /* configured router advertisement information per ipv6 interface */
87 /* advertised config information, zero means unspecified */
91 u16 adv_router_lifetime_in_sec;
92 u32 adv_neighbor_reachable_time_in_msec;
93 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
98 /* source link layer option */
99 u8 link_layer_address[8];
100 u8 link_layer_addr_len;
103 ip6_radv_prefix_t * adv_prefixes_pool;
105 /* Hash table mapping address to index in interface advertised prefix pool. */
106 mhash_t address_to_prefix_index;
108 /* MLDP group information */
109 ip6_mldp_group_t * mldp_group_pool;
111 /* Hash table mapping address to index in mldp address pool. */
112 mhash_t address_to_mldp_index;
114 /* local information */
117 int send_radv; /* radv on/off on this interface - set by config */
118 int cease_radv; /* we are ceasing to send - set byf config */
120 int adv_link_layer_address;
122 int failed_device_check;
123 int all_routers_mcast;
127 adj_index_t all_nodes_adj_index;
128 adj_index_t all_routers_adj_index;
129 adj_index_t all_mldv2_routers_adj_index;
131 /* timing information */
132 #define DEF_MAX_RADV_INTERVAL 200
133 #define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
134 #define DEF_CURR_HOP_LIMIT 64
135 #define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
136 #define MAX_DEF_RTR_LIFETIME 9000
138 #define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
139 #define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
140 #define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
141 #define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
142 #define MAX_RA_DELAY_TIME .5 /* seconds */
144 f64 max_radv_interval;
145 f64 min_radv_interval;
146 f64 min_delay_between_radv;
147 f64 max_delay_between_radv;
148 f64 max_rtr_default_lifetime;
151 f64 last_multicast_time;
152 f64 next_multicast_time;
155 u32 initial_adverts_count;
156 f64 initial_adverts_interval;
157 u32 initial_adverts_sent;
160 u32 n_advertisements_sent;
161 u32 n_solicitations_rcvd;
162 u32 n_solicitations_dropped;
164 /* Link local address to use (defaults to underlying physical for logical interfaces */
165 ip6_address_t link_local_address;
166 u8 link_local_prefix_len;
175 /* Used for nd event notification only */
176 void * data_callback;
178 } pending_resolution_t;
182 /* Hash tables mapping name to opcode. */
183 uword * opcode_by_name;
185 /* lite beer "glean" adjacency handling */
186 mhash_t pending_resolutions_by_address;
187 pending_resolution_t * pending_resolutions;
189 /* Mac address change notification */
190 mhash_t mac_changes_by_address;
191 pending_resolution_t * mac_changes;
193 u32 * neighbor_input_next_index_by_hw_if_index;
195 ip6_neighbor_t * neighbor_pool;
197 mhash_t neighbor_index_by_key;
199 u32 * if_radv_pool_index_by_sw_if_index;
201 ip6_radv_t * if_radv_pool;
203 /* Neighbor attack mitigation */
204 u32 limit_neighbor_cache_size;
205 u32 neighbor_delete_rotor;
207 } ip6_neighbor_main_t;
209 static ip6_neighbor_main_t ip6_neighbor_main;
210 static ip6_address_t ip6a_zero; /* ip6 address 0 */
212 static u8 * format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
214 vlib_main_t * vm = va_arg (*va, vlib_main_t *);
215 ip6_neighbor_t * n = va_arg (*va, ip6_neighbor_t *);
216 vnet_main_t * vnm = vnet_get_main();
217 vnet_sw_interface_t * si;
221 return format (s, "%=12s%=20s%=6s%=20s%=40s", "Time", "Address", "Flags", "Link layer", "Interface");
223 if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
224 flags = format(flags, "D");
226 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
227 flags = format(flags, "S");
229 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
230 s = format (s, "%=12U%=20U%=6s%=20U%=40U",
231 format_vlib_cpu_time, vm, n->cpu_time_last_updated,
232 format_ip6_address, &n->key.ip6_address,
233 flags ? (char *)flags : "",
234 format_ethernet_address, n->link_layer_address,
235 format_vnet_sw_interface_name, vnm, si);
241 static clib_error_t *
242 ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
246 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
249 if (! (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
251 u32 i, * to_delete = 0;
253 pool_foreach (n, nm->neighbor_pool, ({
254 if (n->key.sw_if_index == sw_if_index)
255 vec_add1 (to_delete, n - nm->neighbor_pool);
258 for (i = 0; i < vec_len (to_delete); i++)
260 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
261 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
262 pool_put (nm->neighbor_pool, n);
265 vec_free (to_delete);
271 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
273 static void unset_random_neighbor_entry (void)
275 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
276 vnet_main_t * vnm = vnet_get_main();
277 vlib_main_t * vm = vnm->vlib_main;
281 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
282 nm->neighbor_delete_rotor = index;
284 /* Try again from elt 0, could happen if an intfc goes down */
287 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
288 nm->neighbor_delete_rotor = index;
291 /* Nothing left in the pool */
295 e = pool_elt_at_index (nm->neighbor_pool, index);
297 vnet_unset_ip6_ethernet_neighbor (vm, e->key.sw_if_index,
299 e->link_layer_address,
306 u8 link_layer_address[6];
309 } ip6_neighbor_set_unset_rpc_args_t;
312 static void ip6_neighbor_set_unset_rpc_callback
313 ( ip6_neighbor_set_unset_rpc_args_t * a);
315 static void set_unset_ip6_neighbor_rpc
319 u8 *link_layer_addreess,
320 int is_add, int is_static)
322 ip6_neighbor_set_unset_rpc_args_t args;
323 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
325 args.sw_if_index = sw_if_index;
326 args.is_add = is_add;
327 args.is_static = is_static;
328 clib_memcpy (&args.addr, a, sizeof (*a));
329 clib_memcpy (args.link_layer_address, link_layer_addreess, 6);
331 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
332 (u8 *) &args, sizeof (args));
337 ip6_nd_mk_complete (ip6_neighbor_t * nbr)
341 .fp_proto = FIB_PROTOCOL_IP6,
343 .ip6 = nbr->key.ip6_address,
350 fib_index = im->fib_index_by_sw_if_index[nbr->key.sw_if_index];
352 /* only once please */
353 if (ADJ_INDEX_INVALID == nbr->adj_index)
356 adj_nbr_add_or_lock_w_rewrite(FIB_PROTOCOL_IP6,
359 nbr->key.sw_if_index,
360 nbr->link_layer_address);
361 ASSERT(ADJ_INDEX_INVALID != nbr->adj_index);
363 fib_table_entry_update_one_path(fib_index,
369 nbr->key.sw_if_index,
373 FIB_ROUTE_PATH_FLAG_NONE);
377 adj_nbr_update_rewrite(nbr->adj_index,
378 nbr->link_layer_address);
383 vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
386 u8 * link_layer_address,
387 uword n_bytes_link_layer_address,
390 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
391 ip6_neighbor_key_t k;
392 ip6_neighbor_t * n = 0;
393 int make_new_nd_cache_entry=1;
396 pending_resolution_t * pr, * mc;
399 if (os_get_cpu_number())
401 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
402 1 /* set new neighbor */, is_static);
407 k.sw_if_index = sw_if_index;
408 k.ip6_address = a[0];
411 vlib_worker_thread_barrier_sync (vm);
413 p = mhash_get (&nm->neighbor_index_by_key, &k);
415 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
416 /* Refuse to over-write static neighbor entry. */
418 (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
420 make_new_nd_cache_entry = 0;
423 if (make_new_nd_cache_entry) {
424 pool_get (nm->neighbor_pool, n);
425 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
428 n->adj_index = ADJ_INDEX_INVALID;
431 /* Update time stamp and ethernet address. */
432 clib_memcpy (n->link_layer_address,
434 n_bytes_link_layer_address);
436 n->cpu_time_last_updated = clib_cpu_time_now ();
438 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
440 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
442 ip6_nd_mk_complete(n);
444 /* Customer(s) waiting for this address to be resolved? */
445 p = mhash_get (&nm->pending_resolutions_by_address, a);
450 while (next_index != (u32)~0)
452 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
453 vlib_process_signal_event (vm, pr->node_index,
456 next_index = pr->next_index;
457 pool_put (nm->pending_resolutions, pr);
460 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
463 /* Customer(s) requesting ND event for this address? */
464 p = mhash_get (&nm->mac_changes_by_address, a);
469 while (next_index != (u32)~0)
471 int (*fp)(u32, u8 *, u32, ip6_address_t *);
473 mc = pool_elt_at_index (nm->mac_changes, next_index);
474 fp = mc->data_callback;
476 /* Call the user's data callback, return 1 to suppress dup events */
478 rv = (*fp)(mc->data, link_layer_address, sw_if_index, &ip6a_zero);
480 * Signal the resolver process, as long as the user
481 * says they want to be notified
484 vlib_process_signal_event (vm, mc->node_index,
487 next_index = mc->next_index;
491 vlib_worker_thread_barrier_release(vm);
496 ip6_nd_mk_incomplete (ip6_neighbor_t *nbr)
500 .fp_proto = FIB_PROTOCOL_IP6,
502 .ip6 = nbr->key.ip6_address,
509 fib_index = im->fib_index_by_sw_if_index[nbr->key.sw_if_index];
512 * revert the adj this ND entry sourced to incomplete
514 adj_nbr_update_rewrite(nbr->adj_index,
518 * remove the FIB entry the ND entry sourced
520 fib_table_entry_delete(fib_index, &pfx, FIB_SOURCE_ADJ);
523 * Unlock the adj now that the ARP entry is no longer a source
525 adj_unlock(nbr->adj_index);
526 nbr->adj_index = ADJ_INDEX_INVALID;
530 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
533 u8 * link_layer_address,
534 uword n_bytes_link_layer_address)
536 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
537 ip6_neighbor_key_t k;
543 if (os_get_cpu_number())
545 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
551 k.sw_if_index = sw_if_index;
552 k.ip6_address = a[0];
555 vlib_worker_thread_barrier_sync (vm);
557 p = mhash_get (&nm->neighbor_index_by_key, &k);
564 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
566 ip6_nd_mk_incomplete(n);
567 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
568 pool_put (nm->neighbor_pool, n);
571 vlib_worker_thread_barrier_release(vm);
576 static void ip6_neighbor_set_unset_rpc_callback
577 ( ip6_neighbor_set_unset_rpc_args_t * a)
579 vlib_main_t * vm = vlib_get_main();
581 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
582 a->link_layer_address, 6, a->is_static);
584 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
585 a->link_layer_address, 6);
590 ip6_neighbor_sort (void *a1, void *a2)
592 vnet_main_t * vnm = vnet_get_main();
593 ip6_neighbor_t * n1 = a1, * n2 = a2;
595 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
596 n2->key.sw_if_index);
598 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
602 static clib_error_t *
603 show_ip6_neighbors (vlib_main_t * vm,
604 unformat_input_t * input,
605 vlib_cli_command_t * cmd)
607 vnet_main_t * vnm = vnet_get_main();
608 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
609 ip6_neighbor_t * n, * ns;
610 clib_error_t * error = 0;
613 /* Filter entries by interface if given. */
615 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
618 pool_foreach (n, nm->neighbor_pool, ({ vec_add1 (ns, n[0]); }));
619 vec_sort_with_function (ns, ip6_neighbor_sort);
620 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
621 vec_foreach (n, ns) {
622 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
624 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
631 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
632 .path = "show ip6 neighbors",
633 .function = show_ip6_neighbors,
634 .short_help = "Show ip6 neighbors",
637 static clib_error_t *
638 set_ip6_neighbor (vlib_main_t * vm,
639 unformat_input_t * input,
640 vlib_cli_command_t * cmd)
642 vnet_main_t * vnm = vnet_get_main();
650 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
652 /* intfc, ip6-address, mac-address */
653 if (unformat (input, "%U %U %U",
654 unformat_vnet_sw_interface, vnm, &sw_if_index,
655 unformat_ip6_address, &addr,
656 unformat_ethernet_address, mac_address))
659 else if (unformat (input, "delete") || unformat (input, "del"))
661 else if (unformat (input, "static"))
668 return clib_error_return (0, "Missing interface, ip6 or hw address");
671 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
672 mac_address, sizeof(mac_address), is_static);
674 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
675 mac_address, sizeof(mac_address));
679 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) = {
680 .path = "set ip6 neighbor",
681 .function = set_ip6_neighbor,
682 .short_help = "set ip6 neighbor [del] <intfc> <ip6-address> <mac-address> [static]",
686 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
687 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
688 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
689 } icmp6_neighbor_solicitation_or_advertisement_next_t;
691 static_always_inline uword
692 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
693 vlib_node_runtime_t * node,
694 vlib_frame_t * frame,
695 uword is_solicitation)
697 vnet_main_t * vnm = vnet_get_main();
698 ip6_main_t * im = &ip6_main;
699 uword n_packets = frame->n_vectors;
700 u32 * from, * to_next;
701 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
702 icmp6_neighbor_discovery_option_type_t option_type;
703 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
706 from = vlib_frame_vector_args (frame);
707 n_left_from = n_packets;
708 next_index = node->cached_next_index;
710 if (node->flags & VLIB_NODE_FLAG_TRACE)
711 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
713 sizeof (icmp6_input_trace_t));
717 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
718 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
719 n_advertisements_sent = 0;
721 while (n_left_from > 0)
723 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
725 while (n_left_from > 0 && n_left_to_next > 0)
729 icmp6_neighbor_solicitation_or_advertisement_header_t * h0;
730 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * o0;
731 u32 bi0, options_len0, sw_if_index0, next0, error0;
732 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
736 bi0 = to_next[0] = from[0];
743 p0 = vlib_get_buffer (vm, bi0);
744 ip0 = vlib_buffer_get_current (p0);
745 h0 = ip6_next_header (ip0);
746 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
748 error0 = ICMP6_ERROR_NONE;
749 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
750 ip6_sadd_link_local = ip6_address_is_link_local_unicast(&ip0->src_address);
751 ip6_sadd_unspecified = ip6_address_is_unspecified (&ip0->src_address);
753 /* Check that source address is unspecified, link-local or else on-link. */
754 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
756 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
758 if (ADJ_INDEX_INVALID != src_adj_index0)
760 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, src_adj_index0);
762 /* Allow all realistic-looking rewrite adjacencies to pass */
763 ni0 = adj0->lookup_next_index;
764 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
765 (ni0 < IP6_LOOKUP_N_NEXT);
767 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
769 ? ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
774 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
778 o0 = (void *) (h0 + 1);
779 o0 = ((options_len0 == 8 && o0->header.type == option_type
780 && o0->header.n_data_u64s == 1) ? o0 : 0);
782 /* If src address unspecified or link local, donot learn neighbor MAC */
783 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
784 !ip6_sadd_unspecified && !ip6_sadd_link_local))
786 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
787 if (nm->limit_neighbor_cache_size &&
788 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
789 unset_random_neighbor_entry();
790 vnet_set_ip6_ethernet_neighbor (
792 is_solicitation ? &ip0->src_address : &h0->target_address,
793 o0->ethernet_address, sizeof (o0->ethernet_address), 0);
796 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
798 /* Check that target address is local to this router. */
799 fib_node_index_t fei;
802 fib_index = ip6_fib_table_get_index_for_sw_if_index(sw_if_index0);
806 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
810 fei = ip6_fib_table_lookup_exact_match(fib_index,
814 if (FIB_NODE_INDEX_INVALID == fei ||
815 !(FIB_ENTRY_FLAG_LOCAL &
816 fib_entry_get_flags_for_source(fei, FIB_SOURCE_INTERFACE)))
818 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
824 next0 = (error0 != ICMP6_ERROR_NONE
825 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
826 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
830 error0 = error0 == ICMP6_ERROR_NONE ?
831 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
834 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
836 vnet_sw_interface_t * sw_if0;
837 ethernet_interface_t * eth_if0;
838 ethernet_header_t *eth0;
840 /* dst address is either source address or the all-nodes mcast addr */
841 if(!ip6_sadd_unspecified)
842 ip0->dst_address = ip0->src_address;
844 ip6_set_reserved_multicast_address(&ip0->dst_address,
845 IP6_MULTICAST_SCOPE_link_local,
846 IP6_MULTICAST_GROUP_ID_all_hosts);
848 ip0->src_address = h0->target_address;
849 ip0->hop_limit = 255;
850 h0->icmp.type = ICMP6_neighbor_advertisement;
852 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
853 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
854 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
857 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
859 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
862 h0->advertisement_flags = clib_host_to_net_u32
863 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
864 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
866 h0->icmp.checksum = 0;
868 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
870 ASSERT(bogus_length == 0);
872 /* Reuse current MAC header, copy SMAC to DMAC and
873 * interface MAC to SMAC */
874 vlib_buffer_advance(p0, - ethernet_buffer_header_size(p0));
875 eth0 = vlib_buffer_get_current(p0);
876 clib_memcpy(eth0->dst_address, eth0->src_address, 6);
877 clib_memcpy(eth0->src_address, eth_if0->address, 6);
879 /* Setup input and output sw_if_index for packet */
880 ASSERT(vnet_buffer(p0)->sw_if_index[VLIB_RX] == sw_if_index0);
881 vnet_buffer(p0)->sw_if_index[VLIB_TX] = sw_if_index0;
882 vnet_buffer(p0)->sw_if_index[VLIB_RX] =
883 vnet_main.local_interface_sw_if_index;
885 n_advertisements_sent++;
888 p0->error = error_node->errors[error0];
890 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
891 to_next, n_left_to_next,
895 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
898 /* Account for advertisements sent. */
899 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, n_advertisements_sent);
901 return frame->n_vectors;
904 /* for "syslogging" - use elog for now */
905 #define foreach_log_level \
907 _ (INFO, "INFORMATION") \
908 _ (NOTICE, "NOTICE") \
909 _ (WARNING, "WARNING") \
911 _ (CRIT, "CRITICAL") \
913 _ (EMERG, "EMERGENCY")
916 #define _(f,s) LOG_##f,
921 static char * log_level_strings[] = {
927 static int logmask = 1 << LOG_DEBUG;
930 ip6_neighbor_syslog(vlib_main_t *vm, int priority, char * fmt, ...)
932 /* just use elog for now */
936 if( (priority > LOG_EMERG) ||
937 !(logmask & (1 << priority)))
943 what = va_format (0, fmt, &va);
945 ELOG_TYPE_DECLARE (e) = {
946 .format = "ip6 nd: (%s): %s",
947 .format_args = "T4T4",
949 struct { u32 s[2]; } * ed;
950 ed = ELOG_DATA (&vm->elog_main, e);
951 ed->s[0] = elog_string(&vm->elog_main, log_level_strings[priority]);
952 ed->s[1] = elog_string(&vm->elog_main, (char *)what);
958 /* ipv6 neighbor discovery - router advertisements */
960 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
961 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
962 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
963 ICMP6_ROUTER_SOLICITATION_N_NEXT,
964 } icmp6_router_solicitation_or_advertisement_next_t;
966 static_always_inline uword
967 icmp6_router_solicitation(vlib_main_t * vm,
968 vlib_node_runtime_t * node,
969 vlib_frame_t * frame)
971 vnet_main_t * vnm = vnet_get_main();
972 ip6_main_t * im = &ip6_main;
973 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
974 uword n_packets = frame->n_vectors;
975 u32 * from, * to_next;
976 u32 n_left_from, n_left_to_next, next_index;
977 u32 n_advertisements_sent = 0;
980 icmp6_neighbor_discovery_option_type_t option_type;
982 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
984 from = vlib_frame_vector_args (frame);
985 n_left_from = n_packets;
986 next_index = node->cached_next_index;
988 if (node->flags & VLIB_NODE_FLAG_TRACE)
989 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
991 sizeof (icmp6_input_trace_t));
993 /* source may append his LL address */
994 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
996 while (n_left_from > 0)
998 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1000 while (n_left_from > 0 && n_left_to_next > 0)
1004 ip6_radv_t *radv_info = 0;
1006 icmp6_neighbor_discovery_header_t * h0;
1007 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * o0;
1009 u32 bi0, options_len0, sw_if_index0, next0, error0;
1010 u32 is_solicitation = 1, is_dropped = 0;
1011 u32 is_unspecified, is_link_local;
1013 bi0 = to_next[0] = from[0];
1018 n_left_to_next -= 1;
1020 p0 = vlib_get_buffer (vm, bi0);
1021 ip0 = vlib_buffer_get_current (p0);
1022 h0 = ip6_next_header (ip0);
1023 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1024 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1025 is_link_local = ip6_address_is_link_local_unicast (&ip0->src_address);
1027 error0 = ICMP6_ERROR_NONE;
1028 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1030 /* check if solicitation (not from nd_timer node) */
1031 if (ip6_address_is_unspecified (&ip0->dst_address))
1032 is_solicitation = 0;
1034 /* Check that source address is unspecified, link-local or else on-link. */
1035 if (!is_unspecified && !is_link_local)
1037 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1039 if (ADJ_INDEX_INVALID != src_adj_index0)
1041 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main,
1044 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1045 ? ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1050 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1054 /* check for source LL option and process */
1055 o0 = (void *) (h0 + 1);
1056 o0 = ((options_len0 == 8
1057 && o0->header.type == option_type
1058 && o0->header.n_data_u64s == 1)
1062 /* if src address unspecified IGNORE any options */
1063 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1064 !is_unspecified && !is_link_local)) {
1065 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1066 if (nm->limit_neighbor_cache_size &&
1067 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
1068 unset_random_neighbor_entry();
1070 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1072 o0->ethernet_address,
1073 sizeof (o0->ethernet_address), 0);
1076 /* default is to drop */
1077 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1079 if (error0 == ICMP6_ERROR_NONE)
1081 vnet_sw_interface_t * sw_if0;
1082 ethernet_interface_t * eth_if0;
1085 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1086 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1087 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1089 /* only support ethernet interface type for now */
1090 error0 = (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF : error0;
1092 if (error0 == ICMP6_ERROR_NONE)
1096 /* adjust the sizeof the buffer to just include the ipv6 header */
1097 p0->current_length -= (options_len0 + sizeof(icmp6_neighbor_discovery_header_t));
1099 /* look up the radv_t information for this interface */
1100 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1102 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1105 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1107 error0 = ((!radv_info) ? ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : error0);
1109 if (error0 == ICMP6_ERROR_NONE)
1111 f64 now = vlib_time_now (vm);
1113 /* for solicited adverts - need to rate limit */
1116 if( (now - radv_info->last_radv_time) < MIN_DELAY_BETWEEN_RAS )
1119 radv_info->last_radv_time = now;
1123 icmp6_router_advertisement_header_t rh;
1125 rh.icmp.type = ICMP6_router_advertisement;
1127 rh.icmp.checksum = 0;
1129 rh.current_hop_limit = radv_info->curr_hop_limit;
1130 rh.router_lifetime_in_sec = clib_host_to_net_u16(radv_info->adv_router_lifetime_in_sec);
1131 rh.time_in_msec_between_retransmitted_neighbor_solicitations =
1132 clib_host_to_net_u32(radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1133 rh.neighbor_reachable_time_in_msec =
1134 clib_host_to_net_u32(radv_info->adv_neighbor_reachable_time_in_msec);
1136 rh.flags = (radv_info->adv_managed_flag) ? ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP : 0;
1137 rh.flags |= ( (radv_info->adv_other_flag) ? ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP : 0);
1140 u16 payload_length = sizeof(icmp6_router_advertisement_header_t);
1142 vlib_buffer_add_data (vm,
1143 p0->free_list_index,
1145 (void *)&rh, sizeof(icmp6_router_advertisement_header_t));
1147 if(radv_info->adv_link_layer_address)
1149 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t h;
1151 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1152 h.header.n_data_u64s = 1;
1154 /* copy ll address */
1155 clib_memcpy(&h.ethernet_address[0], eth_if0->address, 6);
1157 vlib_buffer_add_data (vm,
1158 p0->free_list_index,
1160 (void *)&h, sizeof(icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
1162 payload_length += sizeof(icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1165 /* add MTU option */
1166 if(radv_info->adv_link_mtu)
1168 icmp6_neighbor_discovery_mtu_option_t h;
1171 h.mtu = clib_host_to_net_u32(radv_info->adv_link_mtu);
1172 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1173 h.header.n_data_u64s = 1;
1175 payload_length += sizeof( icmp6_neighbor_discovery_mtu_option_t);
1177 vlib_buffer_add_data (vm,
1178 p0->free_list_index,
1180 (void *)&h, sizeof(icmp6_neighbor_discovery_mtu_option_t));
1183 /* add advertised prefix options */
1184 ip6_radv_prefix_t *pr_info;
1186 pool_foreach (pr_info, radv_info->adv_prefixes_pool, ({
1188 if(pr_info->enabled &&
1189 (!pr_info->decrement_lifetime_flag || (pr_info->pref_lifetime_expires >0)))
1191 /* advertise this prefix */
1192 icmp6_neighbor_discovery_prefix_information_option_t h;
1194 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1195 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1197 h.dst_address_length = pr_info->prefix_len;
1199 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1200 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
1202 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1204 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1205 h.preferred_time = 0;
1209 if(pr_info->decrement_lifetime_flag)
1211 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1212 (pr_info->valid_lifetime_expires - now) : 0;
1214 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1215 (pr_info->pref_lifetime_expires - now) : 0;
1218 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1219 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1223 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1225 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1227 vlib_buffer_add_data (vm,
1228 p0->free_list_index,
1230 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1235 /* add additional options before here */
1237 /* finish building the router advertisement... */
1238 if(!is_unspecified && radv_info->send_unicast)
1240 ip0->dst_address = ip0->src_address;
1244 /* target address is all-nodes mcast addr */
1245 ip6_set_reserved_multicast_address(&ip0->dst_address,
1246 IP6_MULTICAST_SCOPE_link_local,
1247 IP6_MULTICAST_GROUP_ID_all_hosts);
1250 /* source address MUST be the link-local address */
1251 ip0->src_address = radv_info->link_local_address;
1253 ip0->hop_limit = 255;
1254 ip0->payload_length = clib_host_to_net_u16 (payload_length);
1256 icmp6_router_advertisement_header_t * rh0 = (icmp6_router_advertisement_header_t *)(ip0 + 1);
1257 rh0->icmp.checksum =
1258 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1260 ASSERT(bogus_length == 0);
1262 /* setup output if and adjacency */
1263 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1264 vnet_main.local_interface_sw_if_index;
1266 if (is_solicitation)
1268 ethernet_header_t *eth0;
1269 /* Reuse current MAC header, copy SMAC to DMAC and
1270 * interface MAC to SMAC */
1271 vlib_buffer_reset (p0);
1272 eth0 = vlib_buffer_get_current(p0);
1273 clib_memcpy(eth0->dst_address, eth0->src_address, 6);
1274 clib_memcpy(eth0->src_address, eth_if0->address, 6);
1275 next0 = is_dropped ?
1276 next0 : ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1277 vnet_buffer(p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1281 adj_index0 = radv_info->all_nodes_adj_index;
1282 if (adj_index0 == 0)
1283 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1286 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, adj_index0);
1288 ((adj0->rewrite_header.sw_if_index != sw_if_index0
1289 || adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
1290 ? ICMP6_ERROR_ROUTER_SOLICITATION_DEST_UNKNOWN
1292 next0 = is_dropped ?
1293 next0 : ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1294 vnet_buffer (p0)->ip.adj_index[VLIB_RX] = adj_index0;
1298 radv_info->n_solicitations_dropped += is_dropped;
1299 radv_info->n_solicitations_rcvd += is_solicitation;
1301 if((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1303 radv_info->n_advertisements_sent++;
1304 n_advertisements_sent++;
1310 p0->error = error_node->errors[error0];
1312 if(error0 != ICMP6_ERROR_NONE)
1313 vlib_error_count (vm, error_node->node_index, error0, 1);
1315 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1316 to_next, n_left_to_next,
1321 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1324 /* Account for router advertisements sent. */
1325 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX, n_advertisements_sent);
1327 return frame->n_vectors;
1330 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1331 static_always_inline uword
1332 icmp6_router_advertisement(vlib_main_t * vm,
1333 vlib_node_runtime_t * node,
1334 vlib_frame_t * frame)
1336 vnet_main_t * vnm = vnet_get_main();
1337 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1338 uword n_packets = frame->n_vectors;
1339 u32 * from, * to_next;
1340 u32 n_left_from, n_left_to_next, next_index;
1341 u32 n_advertisements_rcvd = 0;
1343 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1345 from = vlib_frame_vector_args (frame);
1346 n_left_from = n_packets;
1347 next_index = node->cached_next_index;
1349 if (node->flags & VLIB_NODE_FLAG_TRACE)
1350 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1352 sizeof (icmp6_input_trace_t));
1354 while (n_left_from > 0)
1356 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1358 while (n_left_from > 0 && n_left_to_next > 0)
1362 ip6_radv_t *radv_info = 0;
1363 icmp6_router_advertisement_header_t * h0;
1364 u32 bi0, options_len0, sw_if_index0, next0, error0;
1366 bi0 = to_next[0] = from[0];
1371 n_left_to_next -= 1;
1373 p0 = vlib_get_buffer (vm, bi0);
1374 ip0 = vlib_buffer_get_current (p0);
1375 h0 = ip6_next_header (ip0);
1376 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1378 error0 = ICMP6_ERROR_NONE;
1379 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1381 /* Check that source address is link-local*/
1382 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1383 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1385 /* default is to drop */
1386 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1388 n_advertisements_rcvd++;
1390 if (error0 == ICMP6_ERROR_NONE)
1392 vnet_sw_interface_t * sw_if0;
1393 ethernet_interface_t * eth_if0;
1395 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1396 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1397 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1399 /* only support ethernet interface type for now */
1400 error0 = (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF : error0;
1402 if (error0 == ICMP6_ERROR_NONE)
1406 /* look up the radv_t information for this interface */
1407 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1409 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1412 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1414 error0 = ((!radv_info) ? ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : error0);
1416 if (error0 == ICMP6_ERROR_NONE)
1418 /* validate advertised information */
1419 if((h0->current_hop_limit && radv_info->curr_hop_limit) &&
1420 (h0->current_hop_limit != radv_info->curr_hop_limit))
1422 ip6_neighbor_syslog(vm, LOG_WARNING,
1423 "our AdvCurHopLimit on %U doesn't agree with %U",
1424 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1427 if((h0->flags & ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP) !=
1428 radv_info->adv_managed_flag)
1430 ip6_neighbor_syslog(vm, LOG_WARNING,
1431 "our AdvManagedFlag on %U doesn't agree with %U",
1432 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1435 if((h0->flags & ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP) !=
1436 radv_info->adv_other_flag)
1438 ip6_neighbor_syslog(vm, LOG_WARNING,
1439 "our AdvOtherConfigFlag on %U doesn't agree with %U",
1440 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1443 if((h0->time_in_msec_between_retransmitted_neighbor_solicitations &&
1444 radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations) &&
1445 (h0->time_in_msec_between_retransmitted_neighbor_solicitations !=
1446 clib_host_to_net_u32(radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
1448 ip6_neighbor_syslog(vm, LOG_WARNING,
1449 "our AdvRetransTimer 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->neighbor_reachable_time_in_msec &&
1454 radv_info->adv_neighbor_reachable_time_in_msec) &&
1455 (h0->neighbor_reachable_time_in_msec !=
1456 clib_host_to_net_u32(radv_info->adv_neighbor_reachable_time_in_msec)))
1458 ip6_neighbor_syslog(vm, LOG_WARNING,
1459 "our AdvReachableTime 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 /* check for MTU or prefix options or .. */
1464 u8 * opt_hdr = (u8 *)(h0 + 1);
1465 while( options_len0 > 0)
1467 icmp6_neighbor_discovery_option_header_t *o0 = ( icmp6_neighbor_discovery_option_header_t *)opt_hdr;
1468 int opt_len = o0->n_data_u64s << 3;
1469 icmp6_neighbor_discovery_option_type_t option_type = o0->type;
1471 if(options_len0 < 2)
1473 ip6_neighbor_syslog(vm, LOG_ERR,
1474 "malformed RA packet on %U from %U",
1475 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1481 ip6_neighbor_syslog(vm, LOG_ERR,
1482 " zero length option in RA on %U from %U",
1483 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1486 else if( opt_len > options_len0)
1488 ip6_neighbor_syslog(vm, LOG_ERR,
1489 "option length in RA packet greater than total length on %U from %U",
1490 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1494 options_len0 -= opt_len;
1499 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
1501 icmp6_neighbor_discovery_mtu_option_t *h =
1502 (icmp6_neighbor_discovery_mtu_option_t *)(o0);
1504 if(opt_len < sizeof(*h))
1507 if((h->mtu && radv_info->adv_link_mtu) &&
1508 (h->mtu != clib_host_to_net_u32(radv_info->adv_link_mtu)))
1510 ip6_neighbor_syslog(vm, LOG_WARNING,
1511 "our AdvLinkMTU on %U doesn't agree with %U",
1512 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1517 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1519 icmp6_neighbor_discovery_prefix_information_option_t *h =
1520 (icmp6_neighbor_discovery_prefix_information_option_t *)(o0);
1522 /* validate advertised prefix options */
1523 ip6_radv_prefix_t *pr_info;
1524 u32 preferred, valid;
1526 if(opt_len < sizeof(*h))
1529 preferred = clib_net_to_host_u32(h->preferred_time);
1530 valid = clib_net_to_host_u32(h->valid_time);
1532 /* look for matching prefix - if we our advertising it, it better be consistant */
1533 pool_foreach (pr_info, radv_info->adv_prefixes_pool, ({
1536 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1538 if(pr_info->enabled &&
1539 (pr_info->prefix_len == h->dst_address_length) &&
1540 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1543 if(!pr_info->decrement_lifetime_flag &&
1544 valid != pr_info->adv_valid_lifetime_in_secs)
1546 ip6_neighbor_syslog(vm, LOG_WARNING,
1547 "our ADV validlifetime on %U for %U does not agree with %U",
1548 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1549 format_ip6_address, &h->dst_address);
1551 if(!pr_info->decrement_lifetime_flag &&
1552 preferred != pr_info->adv_pref_lifetime_in_secs)
1554 ip6_neighbor_syslog(vm, LOG_WARNING,
1555 "our ADV preferredlifetime on %U for %U does not agree with %U",
1556 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1557 format_ip6_address, &h->dst_address);
1573 p0->error = error_node->errors[error0];
1575 if(error0 != ICMP6_ERROR_NONE)
1576 vlib_error_count (vm, error_node->node_index, error0, 1);
1578 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1579 to_next, n_left_to_next,
1583 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1586 /* Account for router advertisements sent. */
1587 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX, n_advertisements_rcvd);
1589 return frame->n_vectors;
1592 /* create and initialize router advertisement parameters with default values for this intfc */
1594 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
1598 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1601 vnet_sw_interface_t * sw_if0;
1602 ethernet_interface_t * eth_if0 = 0;
1604 /* lookup radv container - ethernet interfaces only */
1605 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1606 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1607 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1612 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
1613 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1617 a = pool_elt_at_index (nm->if_radv_pool, ri);
1621 u32 i, * to_delete = 0;
1622 ip6_radv_prefix_t *p;
1623 ip6_mldp_group_t *m;
1625 /* remove adjacencies */
1626 adj_unlock(a->all_nodes_adj_index);
1627 adj_unlock(a->all_routers_adj_index);
1628 adj_unlock(a->all_mldv2_routers_adj_index);
1630 /* clean up prefix_pool */
1631 pool_foreach (p, a->adv_prefixes_pool, ({
1632 vec_add1 (to_delete, p - a->adv_prefixes_pool);
1635 for (i = 0; i < vec_len (to_delete); i++)
1637 p = pool_elt_at_index (a->adv_prefixes_pool, to_delete[i]);
1638 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
1639 pool_put (a->adv_prefixes_pool, p);
1642 vec_free (to_delete);
1645 /* clean up mldp group pool */
1646 pool_foreach (m, a->mldp_group_pool, ({
1647 vec_add1 (to_delete, m - a->mldp_group_pool);
1650 for (i = 0; i < vec_len (to_delete); i++)
1652 m = pool_elt_at_index (a->mldp_group_pool, to_delete[i]);
1653 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
1654 pool_put (a->mldp_group_pool, m);
1657 vec_free (to_delete);
1659 pool_put (nm->if_radv_pool, a);
1660 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
1662 ip6_sw_interface_enable_disable(sw_if_index, 0);
1669 vnet_hw_interface_t * hw_if0;
1671 ip6_sw_interface_enable_disable(sw_if_index, 1);
1672 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
1674 pool_get (nm->if_radv_pool, a);
1676 ri = a - nm->if_radv_pool;
1677 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
1679 /* initialize default values (most of which are zero) */
1680 memset (a, 0, sizeof (a[0]));
1682 a->sw_if_index = sw_if_index;
1684 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
1685 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
1686 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
1687 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
1689 a->adv_link_layer_address = 1; /* send ll address source address option */
1691 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
1692 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
1693 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
1694 a->seed = random_default_seed();
1696 /* for generating random interface ids */
1697 a->randomizer = 0x1119194911191949;
1698 a->randomizer = random_u64 ((u32 *)&a->randomizer);
1700 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS ;
1701 a->initial_adverts_sent = a->initial_adverts_count-1;
1702 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1704 /* deafult is to send */
1707 /* fill in radv_info for this interface that will be needed later */
1708 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
1710 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
1712 /* fill in default link-local address (this may be overridden) */
1713 ip6_link_local_address_from_ethernet_address (&a->link_local_address, eth_if0->address);
1714 a->link_local_prefix_len = 64;
1716 mhash_init (&a->address_to_prefix_index, sizeof (uword), sizeof (ip6_address_t));
1717 mhash_init (&a->address_to_mldp_index, sizeof (uword), sizeof (ip6_address_t));
1720 u8 link_layer_address[6] =
1721 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_all_hosts};
1723 a->all_nodes_adj_index = adj_rewrite_add_and_lock(FIB_PROTOCOL_IP6,
1726 link_layer_address);
1730 u8 link_layer_address[6] =
1731 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_all_routers};
1733 a->all_routers_adj_index = adj_rewrite_add_and_lock(FIB_PROTOCOL_IP6,
1736 link_layer_address);
1740 u8 link_layer_address[6] =
1741 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_mldv2_routers};
1743 a->all_mldv2_routers_adj_index =
1744 adj_rewrite_add_and_lock(FIB_PROTOCOL_IP6,
1747 link_layer_address);
1750 /* add multicast groups we will always be reporting */
1752 ip6_mldp_group_t *mcast_group_info;
1754 ip6_set_reserved_multicast_address (&addr,
1755 IP6_MULTICAST_SCOPE_link_local,
1756 IP6_MULTICAST_GROUP_ID_all_hosts);
1758 /* lookup mldp info for this interface */
1760 uword * p = mhash_get (&a->address_to_mldp_index, &addr);
1761 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1764 if(!mcast_group_info)
1768 pool_get (a->mldp_group_pool, mcast_group_info);
1770 mi = mcast_group_info - a->mldp_group_pool;
1771 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1773 mcast_group_info->type = 4;
1774 mcast_group_info->mcast_source_address_pool = 0;
1775 mcast_group_info->num_sources = 0;
1776 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1779 ip6_set_reserved_multicast_address (&addr,
1780 IP6_MULTICAST_SCOPE_link_local,
1781 IP6_MULTICAST_GROUP_ID_all_routers);
1783 p = mhash_get (&a->address_to_mldp_index, &addr);
1784 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1786 if(!mcast_group_info)
1790 pool_get (a->mldp_group_pool, mcast_group_info);
1792 mi = mcast_group_info - a->mldp_group_pool;
1793 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1795 mcast_group_info->type = 4;
1796 mcast_group_info->mcast_source_address_pool = 0;
1797 mcast_group_info->num_sources = 0;
1798 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1801 ip6_set_reserved_multicast_address (&addr,
1802 IP6_MULTICAST_SCOPE_link_local,
1803 IP6_MULTICAST_GROUP_ID_mldv2_routers);
1805 p = mhash_get (&a->address_to_mldp_index, &addr);
1806 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1808 if(!mcast_group_info)
1812 pool_get (a->mldp_group_pool, mcast_group_info);
1814 mi = mcast_group_info - a->mldp_group_pool;
1815 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1817 mcast_group_info->type = 4;
1818 mcast_group_info->mcast_source_address_pool = 0;
1819 mcast_group_info->num_sources = 0;
1820 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1827 /* send an mldpv2 report */
1829 ip6_neighbor_send_mldpv2_report(u32 sw_if_index)
1831 vnet_main_t * vnm = vnet_get_main();
1832 vlib_main_t * vm = vnm->vlib_main;
1833 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1834 vnet_sw_interface_t * sw_if0;
1835 ethernet_interface_t * eth_if0;
1839 ip6_radv_t *radv_info;
1849 icmp6_multicast_listener_report_header_t *rh0;
1850 icmp6_multicast_listener_report_packet_t *rp0;
1852 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1853 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1854 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1856 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
1859 /* look up the radv_t information for this interface */
1860 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
1862 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1867 /* send report now - build a mldpv2 report packet */
1868 n_allocated = vlib_buffer_alloc_from_free_list(vm,
1871 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1872 if (PREDICT_FALSE(n_allocated == 0))
1874 clib_warning ("buffer allocation failure");
1878 b0 = vlib_get_buffer (vm, bo0);
1880 /* adjust the sizeof the buffer to just include the ipv6 header */
1881 b0->current_length = sizeof(icmp6_multicast_listener_report_packet_t);
1883 payload_length = sizeof(icmp6_multicast_listener_report_header_t);
1885 b0->error = ICMP6_ERROR_NONE;
1887 rp0 = vlib_buffer_get_current (b0);
1888 ip0 = (ip6_header_t *)&rp0-> ip;
1889 rh0 = (icmp6_multicast_listener_report_header_t *)&rp0-> report_hdr;
1891 memset (rp0 , 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
1893 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
1895 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
1896 /* for DEBUG - vnet driver won't seem to emit router alerts */
1897 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
1900 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
1902 /* source address MUST be the link-local address */
1903 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1904 ip0->src_address = radv_info->link_local_address;
1906 /* destination is all mldpv2 routers */
1907 ip6_set_reserved_multicast_address(&ip0->dst_address,
1908 IP6_MULTICAST_SCOPE_link_local,
1909 IP6_MULTICAST_GROUP_ID_mldv2_routers);
1911 /* add reports here */
1912 ip6_mldp_group_t *m;
1913 int num_addr_records = 0;
1914 icmp6_multicast_address_record_t rr;
1916 /* fill in the hop-by-hop extension header (router alert) info */
1917 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
1918 rh0->ext_hdr.n_data_u64s = 0;
1920 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
1922 rh0->alert.value = 0;
1927 rh0->icmp.checksum = 0;
1929 pool_foreach (m, radv_info->mldp_group_pool, ({
1932 rr.aux_data_len_u32s = 0;
1933 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
1934 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
1938 vlib_buffer_add_data (vm,
1939 b0->free_list_index,
1941 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
1943 payload_length += sizeof( icmp6_multicast_address_record_t);
1947 rh0->num_addr_records = clib_host_to_net_u16(num_addr_records);
1949 /* update lengths */
1950 ip0->payload_length = clib_host_to_net_u16 (payload_length);
1952 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
1954 ASSERT(bogus_length == 0);
1957 * OK to override w/ no regard for actual FIB, because
1958 * ip6-rewrite-local only looks at the adjacency.
1960 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1961 vnet_main.local_interface_sw_if_index;
1963 vnet_buffer (b0)->ip.adj_index[VLIB_RX] =
1964 radv_info->all_mldv2_routers_adj_index;
1966 vlib_node_t * node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-local");
1968 f = vlib_get_frame_to_node (vm, node->index);
1969 to_next = vlib_frame_vector_args (f);
1973 vlib_put_frame_to_node (vm, node->index, f);
1977 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) = {
1978 .function = icmp6_router_solicitation,
1979 .name = "icmp6-router-solicitation",
1981 .vector_size = sizeof (u32),
1983 .format_trace = format_icmp6_input_trace,
1985 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
1987 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "error-drop",
1988 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-local",
1989 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
1993 /* send a RA or update the timer info etc.. */
1995 ip6_neighbor_process_timer_event (vlib_main_t * vm,
1996 vlib_node_runtime_t * node,
1997 vlib_frame_t * frame)
1999 vnet_main_t * vnm = vnet_get_main();
2000 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2001 ip6_radv_t *radv_info;
2002 vlib_frame_t * f = 0;
2003 u32 n_this_frame = 0;
2004 u32 n_left_to_next = 0;
2007 icmp6_router_solicitation_header_t * h0;
2009 f64 now = vlib_time_now (vm);
2011 /* Interface ip6 radv info list */
2012 pool_foreach (radv_info, nm->if_radv_pool, ({
2014 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2016 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2017 radv_info->next_multicast_time = now;
2018 radv_info->last_multicast_time = now;
2019 radv_info->last_radv_time = 0;
2020 radv_info->all_routers_mcast = 0;
2024 /* Make sure that we've joined the all-routers multicast group */
2025 if(!radv_info->all_routers_mcast)
2027 /* send MDLP_REPORT_EVENT message */
2028 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2029 radv_info->all_routers_mcast = 1;
2032 /* is it time to send a multicast RA on this interface? */
2033 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2038 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2039 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2041 /* multicast send - compute next multicast send time */
2042 if( radv_info->initial_adverts_sent > 0)
2044 radv_info->initial_adverts_sent--;
2045 if(rfn > radv_info-> initial_adverts_interval)
2046 rfn = radv_info-> initial_adverts_interval;
2048 /* check to see if we are ceasing to send */
2049 if( radv_info->initial_adverts_sent == 0)
2050 if(radv_info->cease_radv)
2051 radv_info->send_radv = 0;
2054 radv_info->next_multicast_time = rfn + now;
2055 radv_info->last_multicast_time = now;
2057 /* send advert now - build a "solicted" router advert with unspecified source address */
2058 n_allocated = vlib_buffer_alloc_from_free_list(vm,
2061 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2063 if (PREDICT_FALSE(n_allocated == 0))
2065 clib_warning ("buffer allocation failure");
2068 b0 = vlib_get_buffer (vm, bo0);
2069 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2070 b0->error = ICMP6_ERROR_NONE;
2071 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2073 h0 = vlib_buffer_get_current (b0);
2075 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2077 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2078 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2079 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2080 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2081 h0->ip.hop_limit = 255;
2083 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2084 h0->ip.src_address.as_u64[0] = 0;
2085 h0->ip.src_address.as_u64[1] = 0;
2087 h0->ip.dst_address.as_u64[0] = 0;
2088 h0->ip.dst_address.as_u64[1] = 0;
2090 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2092 if (PREDICT_FALSE(f == 0))
2094 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2095 to_next = vlib_frame_vector_args (f);
2096 n_left_to_next = VLIB_FRAME_SIZE;
2105 if (PREDICT_FALSE(n_left_to_next == 0))
2107 f->n_vectors = n_this_frame;
2108 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2116 ASSERT(n_this_frame);
2117 f->n_vectors = n_this_frame;
2118 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2124 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2125 vlib_node_runtime_t * node,
2126 vlib_frame_t * frame)
2129 ip6_icmp_neighbor_discovery_event_data_t * event_data;
2131 /* init code here */
2135 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */);
2137 event_data = vlib_process_get_event_data (vm, &event_type);
2141 /* No events found: timer expired. */
2142 /* process interface list and send RAs as appropriate, update timer info */
2143 ip6_neighbor_process_timer_event (vm, node, frame);
2147 switch (event_type) {
2149 case ICMP6_ND_EVENT_INIT:
2160 _vec_len (event_data) = 0;
2163 return frame->n_vectors;
2166 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) = {
2167 .function = icmp6_router_advertisement,
2168 .name = "icmp6-router-advertisement",
2170 .vector_size = sizeof (u32),
2172 .format_trace = format_icmp6_input_trace,
2180 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2182 .function = ip6_icmp_neighbor_discovery_event_process,
2183 .name = "ip6-icmp-neighbor-discovery-event-process",
2184 .type = VLIB_NODE_TYPE_PROCESS,
2188 icmp6_neighbor_solicitation (vlib_main_t * vm,
2189 vlib_node_runtime_t * node,
2190 vlib_frame_t * frame)
2191 { return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, /* is_solicitation */ 1); }
2194 icmp6_neighbor_advertisement (vlib_main_t * vm,
2195 vlib_node_runtime_t * node,
2196 vlib_frame_t * frame)
2197 { return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, /* is_solicitation */ 0); }
2199 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) = {
2200 .function = icmp6_neighbor_solicitation,
2201 .name = "icmp6-neighbor-solicitation",
2203 .vector_size = sizeof (u32),
2205 .format_trace = format_icmp6_input_trace,
2207 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2209 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "error-drop",
2210 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2214 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) = {
2215 .function = icmp6_neighbor_advertisement,
2216 .name = "icmp6-neighbor-advertisement",
2218 .vector_size = sizeof (u32),
2220 .format_trace = format_icmp6_input_trace,
2228 /* API support functions */
2230 ip6_neighbor_ra_config(vlib_main_t * vm, u32 sw_if_index,
2231 u8 suppress, u8 managed, u8 other,
2232 u8 ll_option, u8 send_unicast, u8 cease,
2233 u8 use_lifetime, u32 lifetime,
2234 u32 initial_count, u32 initial_interval,
2235 u32 max_interval, u32 min_interval,
2238 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2242 /* look up the radv_t information for this interface */
2243 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2244 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2245 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2250 ip6_radv_t * radv_info;
2251 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2253 if((max_interval != 0) && (min_interval ==0))
2254 min_interval = .75 * max_interval;
2256 max_interval = (max_interval != 0) ? ( (is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) : radv_info->max_radv_interval;
2257 min_interval = (min_interval != 0) ? ( (is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) : radv_info->min_radv_interval;
2258 lifetime = (use_lifetime != 0) ? ( (is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) : radv_info->adv_router_lifetime_in_sec;
2262 if(lifetime > MAX_DEF_RTR_LIFETIME)
2263 lifetime = MAX_DEF_RTR_LIFETIME;
2265 if(lifetime <= max_interval)
2266 return VNET_API_ERROR_INVALID_VALUE;
2269 if(min_interval != 0)
2271 if((min_interval > .75 * max_interval) ||
2273 return VNET_API_ERROR_INVALID_VALUE;
2276 if((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2277 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2278 return VNET_API_ERROR_INVALID_VALUE;
2281 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2282 if "flag" is clear don't change corresponding value
2284 radv_info->send_radv = (suppress != 0) ? ( (is_no != 0) ? 1 : 0 ) : radv_info->send_radv;
2285 radv_info->adv_managed_flag = ( managed != 0) ? ( (is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2286 radv_info->adv_other_flag = (other != 0) ? ( (is_no) ? 0: 1) : radv_info->adv_other_flag;
2287 radv_info->adv_link_layer_address = ( ll_option != 0) ? ( (is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2288 radv_info->send_unicast = (send_unicast != 0) ? ( (is_no) ? 0 : 1) : radv_info->send_unicast;
2289 radv_info->cease_radv = ( cease != 0) ? ( (is_no) ? 0 : 1) : radv_info->cease_radv;
2291 radv_info->min_radv_interval = min_interval;
2292 radv_info->max_radv_interval = max_interval;
2293 radv_info->adv_router_lifetime_in_sec = lifetime;
2295 radv_info->initial_adverts_count =
2296 (initial_count != 0) ? ( (is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) : radv_info->initial_adverts_count ;
2297 radv_info->initial_adverts_interval =
2298 (initial_interval != 0) ? ( (is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) : radv_info->initial_adverts_interval;
2301 if((cease != 0) && (is_no))
2302 radv_info-> send_radv = 1;
2304 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2305 radv_info->next_multicast_time = vlib_time_now (vm);
2306 radv_info->last_multicast_time = vlib_time_now (vm);
2307 radv_info->last_radv_time = 0;
2313 ip6_neighbor_ra_prefix(vlib_main_t * vm, u32 sw_if_index,
2314 ip6_address_t *prefix_addr, u8 prefix_len,
2315 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2316 u8 no_advertise, u8 off_link, u8 no_autoconfig, u8 no_onlink,
2319 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2324 /* look up the radv_t information for this interface */
2325 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2327 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2329 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2333 f64 now = vlib_time_now (vm);
2334 ip6_radv_t * radv_info;
2335 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2337 /* prefix info add, delete or update */
2338 ip6_radv_prefix_t * prefix;
2340 /* lookup prefix info for this address on this interface */
2341 uword * p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2343 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2349 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
2351 if(prefix->prefix_len != prefix_len)
2352 return VNET_API_ERROR_INVALID_VALUE_2;
2354 /* FIXME - Should the DP do this or the CP ?*/
2355 /* do specific delete processing here before returning */
2356 /* try to remove from routing table */
2358 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,/* old_value */ 0);
2359 pool_put (radv_info->adv_prefixes_pool, prefix);
2361 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2362 radv_info->next_multicast_time = vlib_time_now (vm);
2363 radv_info->last_multicast_time = vlib_time_now (vm);
2364 radv_info->last_radv_time = 0;
2368 /* adding or changing */
2373 pool_get (radv_info->adv_prefixes_pool, prefix);
2374 pi = prefix - radv_info->adv_prefixes_pool;
2375 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi, /* old_value */ 0);
2377 memset(prefix, 0x0, sizeof(ip6_radv_prefix_t));
2379 prefix->prefix_len = prefix_len;
2380 clib_memcpy(&prefix->prefix, prefix_addr, sizeof(ip6_address_t));
2382 /* initialize default values */
2383 prefix->adv_on_link_flag = 1; /* L bit set */
2384 prefix->adv_autonomous_flag = 1; /* A bit set */
2385 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
2386 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2387 prefix->enabled = 1;
2388 prefix->decrement_lifetime_flag = 1;
2389 prefix->deprecated_prefix_flag = 1;
2393 /* FIXME - Should the DP do this or the CP ?*/
2394 /* insert prefix into routing table as a connected prefix */
2403 if(prefix->prefix_len != prefix_len)
2404 return VNET_API_ERROR_INVALID_VALUE_2;
2408 /* FIXME - Should the DP do this or the CP ?*/
2409 /* remove from routing table if already there */
2413 if((val_lifetime == ~0) || (pref_lifetime == ~0))
2415 prefix->adv_valid_lifetime_in_secs = ~0;
2416 prefix->adv_pref_lifetime_in_secs = ~0;
2417 prefix->decrement_lifetime_flag = 0;
2421 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2422 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
2425 /* copy remaining */
2426 prefix->enabled = !(no_advertise != 0);
2427 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2428 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2432 /* fill in the expiration times */
2433 prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs;
2434 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
2436 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2437 radv_info->next_multicast_time = vlib_time_now (vm);
2438 radv_info->last_multicast_time = vlib_time_now (vm);
2439 radv_info->last_radv_time = 0;
2445 ip6_neighbor_cmd(vlib_main_t * vm, unformat_input_t * main_input, vlib_cli_command_t * cmd)
2447 vnet_main_t * vnm = vnet_get_main();
2448 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2449 clib_error_t * error = 0;
2451 u8 suppress = 0, managed = 0, other = 0;
2452 u8 suppress_ll_option = 0, send_unicast = 0, cease= 0;
2453 u8 use_lifetime = 0;
2454 u32 sw_if_index, ra_lifetime = 0, ra_initial_count = 0, ra_initial_interval = 0;
2455 u32 ra_max_interval = 0 , ra_min_interval = 0;
2457 unformat_input_t _line_input, * line_input = &_line_input;
2458 vnet_sw_interface_t * sw_if0;
2460 int add_radv_info = 1;
2461 __attribute__((unused)) ip6_radv_t * radv_info = 0;
2462 ip6_address_t ip6_addr;
2466 /* Get a line of input. */
2467 if (! unformat_user (main_input, unformat_line_input, line_input))
2470 /* get basic radv info for this interface */
2471 if(unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2474 if (unformat_user (line_input,
2475 unformat_vnet_sw_interface, vnm, &sw_if_index))
2478 ethernet_interface_t * eth_if0 = 0;
2480 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2481 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2482 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2486 error = clib_error_return (0, "Interface must be of ethernet type");
2490 /* look up the radv_t information for this interface */
2491 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2493 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2497 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2501 error = clib_error_return (0, "unknown interface %U'",
2502 format_unformat_error, line_input);
2508 error = clib_error_return (0, "invalid interface name %U'",
2509 format_unformat_error, line_input);
2514 /* get the rest of the command */
2515 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2517 if (unformat (line_input, "no"))
2519 else if(unformat (line_input, "prefix %U/%d",
2520 unformat_ip6_address, &ip6_addr,
2526 else if (unformat (line_input, "ra-managed-config-flag"))
2531 else if (unformat (line_input, "ra-other-config-flag"))
2536 else if (unformat (line_input, "ra-suppress") ||
2537 unformat (line_input, "ra-surpress"))
2542 else if (unformat (line_input, "ra-suppress-link-layer") ||
2543 unformat (line_input, "ra-surpress-link-layer"))
2545 suppress_ll_option = 1;
2548 else if (unformat (line_input, "ra-send-unicast"))
2553 else if (unformat (line_input, "ra-lifetime"))
2555 if (!unformat (line_input, "%d", &ra_lifetime))
2556 return(error = unformat_parse_error (line_input));
2560 else if (unformat (line_input, "ra-initial"))
2562 if (!unformat (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
2563 return(error = unformat_parse_error (line_input));
2566 else if (unformat (line_input, "ra-interval"))
2568 if (!unformat (line_input, "%d", &ra_max_interval))
2569 return(error = unformat_parse_error (line_input));
2571 if (!unformat (line_input, "%d", &ra_min_interval))
2572 ra_min_interval = 0;
2575 else if(unformat (line_input, "ra-cease"))
2581 return(unformat_parse_error (line_input));
2586 ip6_neighbor_ra_config(vm, sw_if_index,
2587 suppress, managed, other,
2588 suppress_ll_option, send_unicast, cease,
2589 use_lifetime, ra_lifetime,
2590 ra_initial_count, ra_initial_interval,
2591 ra_max_interval, ra_min_interval,
2596 u32 valid_lifetime_in_secs = 0;
2597 u32 pref_lifetime_in_secs = 0;
2598 u8 use_prefix_default_values = 0;
2599 u8 no_advertise = 0;
2601 u8 no_autoconfig = 0;
2604 /* get the rest of the command */
2605 while(unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2607 if(unformat (line_input, "default"))
2609 use_prefix_default_values = 1;
2612 else if(unformat (line_input, "infinite"))
2614 valid_lifetime_in_secs = ~0;
2615 pref_lifetime_in_secs = ~0;
2618 else if(unformat (line_input, "%d %d", &valid_lifetime_in_secs,
2619 &pref_lifetime_in_secs))
2626 /* get the rest of the command */
2627 while (!use_prefix_default_values &&
2628 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2630 if(unformat (line_input, "no-advertise"))
2632 else if(unformat (line_input, "off-link"))
2634 else if(unformat (line_input, "no-autoconfig"))
2636 else if(unformat (line_input, "no-onlink"))
2639 return(unformat_parse_error (line_input));
2642 ip6_neighbor_ra_prefix(vm, sw_if_index,
2643 &ip6_addr, addr_len,
2644 use_prefix_default_values,
2645 valid_lifetime_in_secs,
2646 pref_lifetime_in_secs,
2654 unformat_free (line_input);
2661 ip6_print_addrs(vlib_main_t * vm,
2664 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2667 for (i = 0; i < vec_len (addrs); i++)
2669 ip_interface_address_t * a = pool_elt_at_index(lm->if_address_pool, addrs[i]);
2670 ip6_address_t * address = ip_interface_address_get_address (lm, a);
2672 vlib_cli_output (vm, "\t\t%U/%d",
2673 format_ip6_address, address,
2678 static clib_error_t *
2679 show_ip6_interface_cmd (vlib_main_t * vm,
2680 unformat_input_t * input,
2681 vlib_cli_command_t * cmd)
2683 vnet_main_t * vnm = vnet_get_main();
2684 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2685 clib_error_t * error = 0;
2690 if (unformat_user (input,
2691 unformat_vnet_sw_interface, vnm, &sw_if_index))
2695 /* look up the radv_t information for this interface */
2696 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2698 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2702 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2703 ip6_radv_t * radv_info;
2704 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2706 vlib_cli_output (vm, "%U is admin %s\n", format_vnet_sw_interface_name, vnm,
2707 vnet_get_sw_interface (vnm, sw_if_index),
2708 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ? "up" : "down"));
2711 u32 *link_scope = 0, *global_scope = 0;
2712 u32 *local_scope = 0, *unknown_scope = 0;
2713 ip_interface_address_t * a;
2715 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
2716 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
2718 while (ai != (u32)~0)
2720 a = pool_elt_at_index(lm->if_address_pool, ai);
2721 ip6_address_t * address = ip_interface_address_get_address (lm, a);
2723 if (ip6_address_is_link_local_unicast (address))
2724 vec_add1 (link_scope, ai);
2725 else if(ip6_address_is_global_unicast (address))
2726 vec_add1 (global_scope, ai);
2727 else if(ip6_address_is_local_unicast (address))
2728 vec_add1 (local_scope, ai);
2730 vec_add1 (unknown_scope, ai);
2732 ai = a->next_this_sw_interface;
2735 if (vec_len (link_scope))
2737 vlib_cli_output (vm, "\tLink-local address(es):\n");
2738 ip6_print_addrs (vm, link_scope);
2739 vec_free (link_scope);
2742 if (vec_len (local_scope))
2744 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
2745 ip6_print_addrs (vm, local_scope);
2746 vec_free (local_scope);
2749 if (vec_len (global_scope))
2751 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
2752 ip6_print_addrs (vm, global_scope);
2753 vec_free (global_scope);
2756 if (vec_len (unknown_scope))
2758 vlib_cli_output (vm, "\tOther-scope address(es):\n");
2759 ip6_print_addrs (vm, unknown_scope);
2760 vec_free (unknown_scope);
2763 vlib_cli_output (vm, "\tJoined group address(es):\n");
2764 ip6_mldp_group_t *m;
2765 pool_foreach (m, radv_info->mldp_group_pool, ({
2766 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address, &m->mcast_address);
2769 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
2770 ip6_radv_prefix_t * p;
2771 pool_foreach (p, radv_info->adv_prefixes_pool, ({
2772 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
2773 format_ip6_address, &p->prefix, p->prefix_len);
2776 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
2777 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
2778 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
2779 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
2780 vlib_cli_output (vm, "\tND DAD is disabled\n");
2781 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
2782 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
2783 radv_info->adv_neighbor_reachable_time_in_msec);
2784 vlib_cli_output (vm, "\tND advertised retransmit interval is %d (msec)\n",
2785 radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations);
2787 u32 ra_interval = radv_info->max_radv_interval;
2788 u32 ra_interval_min = radv_info->min_radv_interval;
2789 vlib_cli_output (vm, "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
2790 ra_interval, ra_interval_min);
2791 vlib_cli_output (vm, "\tND router advertisements live for %d seconds\n",
2792 radv_info->adv_router_lifetime_in_sec);
2793 vlib_cli_output (vm, "\tHosts %s stateless autoconfig for addresses\n",
2794 (radv_info->adv_managed_flag) ? "use" :" don't use");
2795 vlib_cli_output (vm, "\tND router advertisements sent %d\n", radv_info->n_advertisements_sent);
2796 vlib_cli_output (vm, "\tND router solicitations received %d\n", radv_info->n_solicitations_rcvd);
2797 vlib_cli_output (vm, "\tND router solicitations dropped %d\n", radv_info->n_solicitations_dropped);
2801 error = clib_error_return (0, "IPv6 not enabled on interface",
2802 format_unformat_error, input);
2809 VLIB_CLI_COMMAND (show_ip6_interface_command, static) = {
2810 .path = "show ip6 interface",
2811 .function = show_ip6_interface_cmd,
2812 .short_help = "show ip6 interface <iface name>",
2816 disable_ip6_interface(vlib_main_t * vm,
2819 clib_error_t * error = 0;
2820 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2823 /* look up the radv_t information for this interface */
2824 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2825 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2827 /* if not created - do nothing */
2830 vnet_main_t * vnm = vnet_get_main();
2831 ip6_radv_t * radv_info;
2833 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2835 /* check radv_info ref count for other ip6 addresses on this interface */
2836 if(radv_info->ref_count == 0 )
2838 /* essentially "disables" ipv6 on this interface */
2839 error = ip6_add_del_interface_address (vm, sw_if_index,
2840 &radv_info->link_local_address,
2841 radv_info->link_local_prefix_len,
2844 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, 0/* is_add */);
2851 ip6_interface_enabled(vlib_main_t * vm,
2854 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2857 /* look up the radv_t information for this interface */
2858 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2860 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2866 enable_ip6_interface(vlib_main_t * vm,
2869 clib_error_t * error = 0;
2870 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2874 /* look up the radv_t information for this interface */
2875 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2877 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2879 /* if not created yet */
2882 vnet_main_t * vnm = vnet_get_main();
2883 vnet_sw_interface_t * sw_if0;
2885 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2886 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2888 ethernet_interface_t * eth_if0;
2890 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2893 /* create radv_info. for this interface. This holds all the info needed for router adverts */
2894 ri = ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
2898 ip6_radv_t * radv_info;
2899 ip6_address_t link_local_address;
2901 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2903 ip6_link_local_address_from_ethernet_mac_address (&link_local_address,
2906 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
2907 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB)
2909 /* make up an interface id */
2913 link_local_address.as_u64[0] = radv_info->randomizer;
2916 md5_add (&m, &link_local_address, 16);
2917 md5_finish (&m, digest);
2919 clib_memcpy(&link_local_address, digest, 16);
2921 radv_info->randomizer = link_local_address.as_u64[0];
2923 link_local_address.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2925 link_local_address.as_u8[8] &= 0xfd;
2928 /* essentially "enables" ipv6 on this interface */
2929 error = ip6_add_del_interface_address (vm, sw_if_index,
2930 &link_local_address,
2931 128 /* address width */,
2935 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, !is_add);
2938 radv_info->link_local_address = link_local_address;
2939 radv_info->link_local_prefix_len = 64;
2948 static clib_error_t *
2949 enable_ip6_interface_cmd (vlib_main_t * vm,
2950 unformat_input_t * input,
2951 vlib_cli_command_t * cmd)
2953 vnet_main_t * vnm = vnet_get_main();
2954 clib_error_t * error = 0;
2959 if (unformat_user (input,
2960 unformat_vnet_sw_interface, vnm, &sw_if_index))
2962 enable_ip6_interface(vm, sw_if_index);
2966 error = clib_error_return (0, "unknown interface\n'",
2967 format_unformat_error, input);
2973 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) = {
2974 .path = "enable ip6 interface",
2975 .function = enable_ip6_interface_cmd,
2976 .short_help = "enable ip6 interface <iface name>",
2979 static clib_error_t *
2980 disable_ip6_interface_cmd (vlib_main_t * vm,
2981 unformat_input_t * input,
2982 vlib_cli_command_t * cmd)
2984 vnet_main_t * vnm = vnet_get_main();
2985 clib_error_t * error = 0;
2990 if (unformat_user (input,
2991 unformat_vnet_sw_interface, vnm, &sw_if_index))
2993 error = disable_ip6_interface(vm, sw_if_index);
2997 error = clib_error_return (0, "unknown interface\n'",
2998 format_unformat_error, input);
3004 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) = {
3005 .path = "disable ip6 interface",
3006 .function = disable_ip6_interface_cmd,
3007 .short_help = "disable ip6 interface <iface name>",
3010 VLIB_CLI_COMMAND (ip6_nd_command, static) = {
3012 .short_help = "Set ip6 neighbor discovery parameters",
3013 .function = ip6_neighbor_cmd,
3017 set_ip6_link_local_address(vlib_main_t * vm,
3019 ip6_address_t *address,
3022 clib_error_t * error = 0;
3023 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3025 ip6_radv_t * radv_info;
3026 vnet_main_t * vnm = vnet_get_main();
3028 if( !ip6_address_is_link_local_unicast (address))
3030 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
3031 return(error = clib_error_return (0, "address not link-local",
3032 format_unformat_error));
3035 /* call enable ipv6 */
3036 enable_ip6_interface(vm, sw_if_index);
3038 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3042 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3044 /* save if link local address (overwrite default) */
3046 /* delete the old one */
3047 error = ip6_add_del_interface_address (vm, sw_if_index,
3048 &radv_info->link_local_address,
3049 radv_info->link_local_prefix_len /* address width */,
3054 /* add the new one */
3055 error = ip6_add_del_interface_address (vm, sw_if_index,
3057 address_length /* address width */,
3062 radv_info->link_local_address = *address;
3063 radv_info->link_local_prefix_len = address_length;
3069 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3070 error = clib_error_return (0, "ip6 not enabled for interface",
3071 format_unformat_error);
3077 set_ip6_link_local_address_cmd (vlib_main_t * vm,
3078 unformat_input_t * input,
3079 vlib_cli_command_t * cmd)
3081 vnet_main_t * vnm = vnet_get_main();
3082 clib_error_t * error = 0;
3084 ip6_address_t ip6_addr;
3087 if (unformat_user (input,
3088 unformat_vnet_sw_interface, vnm, &sw_if_index))
3090 /* get the rest of the command */
3091 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3093 if(unformat (input, "%U/%d",
3094 unformat_ip6_address, &ip6_addr,
3098 return(unformat_parse_error (input));
3101 error = set_ip6_link_local_address(vm,
3108 VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) = {
3109 .path = "set ip6 link-local address",
3110 .short_help = "Set ip6 interface link-local address <intfc> <address.>",
3111 .function = set_ip6_link_local_address_cmd,
3114 /* callback when an interface address is added or deleted */
3116 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3119 ip6_address_t * address,
3121 u32 if_address_index,
3124 vnet_main_t * vnm = vnet_get_main();
3125 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3127 vlib_main_t * vm = vnm->vlib_main;
3128 ip6_radv_t * radv_info;
3130 ip6_mldp_group_t *mcast_group_info;
3132 /* create solicited node multicast address for this interface adddress */
3133 ip6_set_solicited_node_multicast_address (&a, 0);
3135 a.as_u8[0xd] = address->as_u8[0xd];
3136 a.as_u8[0xe] = address->as_u8[0xe];
3137 a.as_u8[0xf] = address->as_u8[0xf];
3141 /* try to create radv_info - does nothing if ipv6 already enabled */
3142 enable_ip6_interface(vm, sw_if_index);
3144 /* look up the radv_t information for this interface */
3145 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
3146 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3150 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3153 if( !ip6_address_is_link_local_unicast (address))
3154 radv_info->ref_count++;
3156 /* lookup prefix info for this address on this interface */
3157 uword * p = mhash_get (&radv_info->address_to_mldp_index, &a);
3158 mcast_group_info = p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3160 /* add -solicted node multicast address */
3161 if(!mcast_group_info)
3165 pool_get (radv_info->mldp_group_pool, mcast_group_info);
3167 mi = mcast_group_info - radv_info->mldp_group_pool;
3168 mhash_set (&radv_info->address_to_mldp_index, &a, mi, /* old_value */ 0);
3170 mcast_group_info->type = 4;
3171 mcast_group_info->mcast_source_address_pool = 0;
3172 mcast_group_info->num_sources = 0;
3173 clib_memcpy(&mcast_group_info->mcast_address, &a, sizeof(ip6_address_t));
3181 /* look up the radv_t information for this interface */
3182 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
3183 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3187 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3189 /* lookup prefix info for this address on this interface */
3190 uword * p = mhash_get (&radv_info->address_to_mldp_index, &a);
3191 mcast_group_info = p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3193 if(mcast_group_info)
3195 mhash_unset (&radv_info->address_to_mldp_index, &a,/* old_value */ 0);
3196 pool_put (radv_info->mldp_group_pool, mcast_group_info);
3199 /* if interface up send MLDP "report" */
3200 radv_info->all_routers_mcast = 0;
3203 if( !ip6_address_is_link_local_unicast (address))
3204 radv_info->ref_count--;
3209 clib_error_t *ip6_set_neighbor_limit (u32 neighbor_limit)
3211 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3213 nm->limit_neighbor_cache_size = neighbor_limit;
3217 static clib_error_t * ip6_neighbor_init (vlib_main_t * vm)
3219 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3220 ip6_main_t * im = &ip6_main;
3222 mhash_init (&nm->neighbor_index_by_key,
3223 /* value size */ sizeof (uword),
3224 /* key size */ sizeof (ip6_neighbor_key_t));
3226 icmp6_register_type (vm, ICMP6_neighbor_solicitation, ip6_icmp_neighbor_solicitation_node.index);
3227 icmp6_register_type (vm, ICMP6_neighbor_advertisement, ip6_icmp_neighbor_advertisement_node.index);
3228 icmp6_register_type (vm, ICMP6_router_solicitation, ip6_icmp_router_solicitation_node.index);
3229 icmp6_register_type (vm, ICMP6_router_advertisement, ip6_icmp_router_advertisement_node.index);
3231 /* handler node for ip6 neighbor discovery events and timers */
3232 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3234 /* add call backs */
3235 ip6_add_del_interface_address_callback_t cb;
3236 memset(&cb, 0x0, sizeof(ip6_add_del_interface_address_callback_t));
3238 /* when an interface address changes... */
3239 cb.function = ip6_neighbor_add_del_interface_address;
3240 cb.function_opaque = 0;
3241 vec_add1 (im->add_del_interface_address_callbacks, cb);
3243 mhash_init (&nm->pending_resolutions_by_address,
3244 /* value size */ sizeof (uword),
3245 /* key size */ sizeof (ip6_address_t));
3247 mhash_init (&nm->mac_changes_by_address,
3248 /* value size */ sizeof (uword),
3249 /* key size */ sizeof (ip6_address_t));
3251 /* default, configurable */
3252 nm->limit_neighbor_cache_size = 50000;
3255 /* $$$$ Hack fix for today */
3256 vec_validate_init_empty
3257 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */);
3263 VLIB_INIT_FUNCTION (ip6_neighbor_init);
3266 void vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
3272 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3273 ip6_address_t * address = address_arg;
3275 pending_resolution_t * pr;
3277 pool_get (nm->pending_resolutions, pr);
3279 pr->next_index = ~0;
3280 pr->node_index = node_index;
3281 pr->type_opaque = type_opaque;
3284 p = mhash_get (&nm->pending_resolutions_by_address, address);
3287 /* Insert new resolution at the head of the list */
3288 pr->next_index = p[0];
3289 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
3292 mhash_set (&nm->pending_resolutions_by_address, address,
3293 pr - nm->pending_resolutions, 0 /* old value */);
3296 int vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
3297 void * data_callback,
3305 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3306 ip6_address_t * address = address_arg;
3308 pending_resolution_t * mc;
3309 void (*fp)(u32, u8 *) = data_callback;
3313 pool_get (nm->mac_changes, mc);
3315 mc->next_index = ~0;
3316 mc->node_index = node_index;
3317 mc->type_opaque = type_opaque;
3319 mc->data_callback = data_callback;
3322 p = mhash_get (&nm->mac_changes_by_address, address);
3325 /* Insert new resolution at the head of the list */
3326 mc->next_index = p[0];
3327 mhash_unset (&nm->mac_changes_by_address, address, 0);
3330 mhash_set (&nm->mac_changes_by_address, address,
3331 mc - nm->mac_changes, 0);
3337 pending_resolution_t * mc_last = 0;
3339 p = mhash_get (&nm->mac_changes_by_address, address);
3341 return VNET_API_ERROR_NO_SUCH_ENTRY;
3345 while (index != (u32)~0)
3347 mc = pool_elt_at_index (nm->mac_changes, index);
3348 if (mc->node_index == node_index &&
3349 mc->type_opaque == type_opaque &&
3352 /* Clients may need to clean up pool entries, too */
3354 (*fp)(mc->data, 0 /* no new mac addrs */);
3357 mhash_unset (&nm->mac_changes_by_address, address, 0);
3358 if (mc->next_index != ~0)
3359 mhash_set (&nm->mac_changes_by_address, address,
3361 pool_put (nm->mac_changes, mc);
3367 mc_last->next_index = mc->next_index;
3368 pool_put (nm->mac_changes, mc);
3373 index = mc->next_index;
3376 return VNET_API_ERROR_NO_SUCH_ENTRY;
3380 int vnet_ip6_nd_term (vlib_main_t * vm,
3381 vlib_node_runtime_t * node,
3383 ethernet_header_t * eth,
3389 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3390 icmp6_neighbor_solicitation_or_advertisement_header_t * ndh;
3391 pending_resolution_t * mc;
3394 ndh = ip6_next_header (ip);
3395 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
3396 ndh->icmp.type != ICMP6_neighbor_advertisement)
3399 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
3400 (p0->flags & VLIB_BUFFER_IS_TRACED)))
3402 u8 *t0 = vlib_add_trace (vm, node, p0,
3403 sizeof (icmp6_input_trace_t));
3404 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
3407 /* Check if anyone want ND events for L2 BDs */
3408 p = mhash_get (&nm->mac_changes_by_address, &ip6a_zero);
3410 { /* Only SHG 0 interface which is more likely local */
3411 u32 next_index = p[0];
3412 while (next_index != (u32)~0)
3414 int (*fp)(u32, u8 *, u32, ip6_address_t *);
3416 mc = pool_elt_at_index (nm->mac_changes, next_index);
3417 fp = mc->data_callback;
3418 /* Call the callback, return 1 to suppress dup events */
3419 if (fp) rv = (*fp)(mc->data,
3423 /* Signal the resolver process */
3425 vlib_process_signal_event (vm, mc->node_index,
3428 next_index = mc->next_index;
3432 /* Check if MAC entry exsist for solicited target IP */
3433 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
3435 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * opt;
3436 l2_bridge_domain_t *bd_config;
3439 opt = (void *) (ndh + 1);
3440 if ((opt->header.type !=
3441 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
3442 (opt->header.n_data_u64s != 1))
3443 return 0; /* source link layer address option not present */
3445 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
3446 macp = (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
3448 { /* found ip-mac entry, generate eighbor advertisement response */
3450 vlib_node_runtime_t * error_node =
3451 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
3452 ip->dst_address = ip->src_address;
3453 ip->src_address = ndh->target_address;
3454 ip->hop_limit = 255;
3456 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
3457 clib_memcpy (opt->ethernet_address, macp, 6);
3458 ndh->icmp.type = ICMP6_neighbor_advertisement;
3459 ndh->advertisement_flags = clib_host_to_net_u32
3460 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
3461 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
3462 ndh->icmp.checksum = 0;
3463 ndh->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum(vm, p0, ip,
3465 clib_memcpy(eth->dst_address, eth->src_address, 6);
3466 clib_memcpy(eth->src_address, macp, 6);
3467 vlib_error_count (vm, error_node->node_index,
3468 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
3478 ethernet_ndp_change_mac (vlib_main_t * vm, u32 sw_if_index)
3480 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3484 pool_foreach (n, nm->neighbor_pool, ({
3485 if (n->key.sw_if_index == sw_if_index)
3487 if (ADJ_INDEX_INVALID != n->adj_index)
3489 adj_nbr_update_rewrite(n->adj_index,
3490 n->link_layer_address);