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 & fib_entry_get_flags(fei)))
817 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
823 next0 = (error0 != ICMP6_ERROR_NONE
824 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
825 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
829 error0 = error0 == ICMP6_ERROR_NONE ?
830 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
833 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
835 vnet_sw_interface_t * sw_if0;
836 ethernet_interface_t * eth_if0;
837 ethernet_header_t *eth0;
839 /* dst address is either source address or the all-nodes mcast addr */
840 if(!ip6_sadd_unspecified)
841 ip0->dst_address = ip0->src_address;
843 ip6_set_reserved_multicast_address(&ip0->dst_address,
844 IP6_MULTICAST_SCOPE_link_local,
845 IP6_MULTICAST_GROUP_ID_all_hosts);
847 ip0->src_address = h0->target_address;
848 ip0->hop_limit = 255;
849 h0->icmp.type = ICMP6_neighbor_advertisement;
851 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
852 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
853 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
856 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
858 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
861 h0->advertisement_flags = clib_host_to_net_u32
862 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
863 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
865 h0->icmp.checksum = 0;
867 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
869 ASSERT(bogus_length == 0);
871 /* Reuse current MAC header, copy SMAC to DMAC and
872 * interface MAC to SMAC */
873 vlib_buffer_advance(p0, - ethernet_buffer_header_size(p0));
874 eth0 = vlib_buffer_get_current(p0);
875 clib_memcpy(eth0->dst_address, eth0->src_address, 6);
876 clib_memcpy(eth0->src_address, eth_if0->address, 6);
878 /* Setup input and output sw_if_index for packet */
879 ASSERT(vnet_buffer(p0)->sw_if_index[VLIB_RX] == sw_if_index0);
880 vnet_buffer(p0)->sw_if_index[VLIB_TX] = sw_if_index0;
881 vnet_buffer(p0)->sw_if_index[VLIB_RX] =
882 vnet_main.local_interface_sw_if_index;
884 n_advertisements_sent++;
887 p0->error = error_node->errors[error0];
889 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
890 to_next, n_left_to_next,
894 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
897 /* Account for advertisements sent. */
898 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, n_advertisements_sent);
900 return frame->n_vectors;
903 /* for "syslogging" - use elog for now */
904 #define foreach_log_level \
906 _ (INFO, "INFORMATION") \
907 _ (NOTICE, "NOTICE") \
908 _ (WARNING, "WARNING") \
910 _ (CRIT, "CRITICAL") \
912 _ (EMERG, "EMERGENCY")
915 #define _(f,s) LOG_##f,
920 static char * log_level_strings[] = {
926 static int logmask = 1 << LOG_DEBUG;
929 ip6_neighbor_syslog(vlib_main_t *vm, int priority, char * fmt, ...)
931 /* just use elog for now */
935 if( (priority > LOG_EMERG) ||
936 !(logmask & (1 << priority)))
942 what = va_format (0, fmt, &va);
944 ELOG_TYPE_DECLARE (e) = {
945 .format = "ip6 nd: (%s): %s",
946 .format_args = "T4T4",
948 struct { u32 s[2]; } * ed;
949 ed = ELOG_DATA (&vm->elog_main, e);
950 ed->s[0] = elog_string(&vm->elog_main, log_level_strings[priority]);
951 ed->s[1] = elog_string(&vm->elog_main, (char *)what);
957 /* ipv6 neighbor discovery - router advertisements */
959 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
960 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
961 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
962 ICMP6_ROUTER_SOLICITATION_N_NEXT,
963 } icmp6_router_solicitation_or_advertisement_next_t;
965 static_always_inline uword
966 icmp6_router_solicitation(vlib_main_t * vm,
967 vlib_node_runtime_t * node,
968 vlib_frame_t * frame)
970 vnet_main_t * vnm = vnet_get_main();
971 ip6_main_t * im = &ip6_main;
972 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
973 uword n_packets = frame->n_vectors;
974 u32 * from, * to_next;
975 u32 n_left_from, n_left_to_next, next_index;
976 u32 n_advertisements_sent = 0;
979 icmp6_neighbor_discovery_option_type_t option_type;
981 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
983 from = vlib_frame_vector_args (frame);
984 n_left_from = n_packets;
985 next_index = node->cached_next_index;
987 if (node->flags & VLIB_NODE_FLAG_TRACE)
988 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
990 sizeof (icmp6_input_trace_t));
992 /* source may append his LL address */
993 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
995 while (n_left_from > 0)
997 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
999 while (n_left_from > 0 && n_left_to_next > 0)
1003 ip6_radv_t *radv_info = 0;
1005 icmp6_neighbor_discovery_header_t * h0;
1006 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * o0;
1008 u32 bi0, options_len0, sw_if_index0, next0, error0;
1009 u32 is_solicitation = 1, is_dropped = 0;
1010 u32 is_unspecified, is_link_local;
1012 bi0 = to_next[0] = from[0];
1017 n_left_to_next -= 1;
1019 p0 = vlib_get_buffer (vm, bi0);
1020 ip0 = vlib_buffer_get_current (p0);
1021 h0 = ip6_next_header (ip0);
1022 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1023 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1024 is_link_local = ip6_address_is_link_local_unicast (&ip0->src_address);
1026 error0 = ICMP6_ERROR_NONE;
1027 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1029 /* check if solicitation (not from nd_timer node) */
1030 if (ip6_address_is_unspecified (&ip0->dst_address))
1031 is_solicitation = 0;
1033 /* Check that source address is unspecified, link-local or else on-link. */
1034 if (!is_unspecified && !is_link_local)
1036 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1038 if (ADJ_INDEX_INVALID != src_adj_index0)
1040 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main,
1043 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1044 ? ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1049 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1053 /* check for source LL option and process */
1054 o0 = (void *) (h0 + 1);
1055 o0 = ((options_len0 == 8
1056 && o0->header.type == option_type
1057 && o0->header.n_data_u64s == 1)
1061 /* if src address unspecified IGNORE any options */
1062 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1063 !is_unspecified && !is_link_local)) {
1064 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1065 if (nm->limit_neighbor_cache_size &&
1066 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
1067 unset_random_neighbor_entry();
1069 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1071 o0->ethernet_address,
1072 sizeof (o0->ethernet_address), 0);
1075 /* default is to drop */
1076 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1078 if (error0 == ICMP6_ERROR_NONE)
1080 vnet_sw_interface_t * sw_if0;
1081 ethernet_interface_t * eth_if0;
1084 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1085 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1086 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1088 /* only support ethernet interface type for now */
1089 error0 = (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF : error0;
1091 if (error0 == ICMP6_ERROR_NONE)
1095 /* adjust the sizeof the buffer to just include the ipv6 header */
1096 p0->current_length -= (options_len0 + sizeof(icmp6_neighbor_discovery_header_t));
1098 /* look up the radv_t information for this interface */
1099 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1101 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1104 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1106 error0 = ((!radv_info) ? ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : error0);
1108 if (error0 == ICMP6_ERROR_NONE)
1110 f64 now = vlib_time_now (vm);
1112 /* for solicited adverts - need to rate limit */
1115 if( (now - radv_info->last_radv_time) < MIN_DELAY_BETWEEN_RAS )
1118 radv_info->last_radv_time = now;
1122 icmp6_router_advertisement_header_t rh;
1124 rh.icmp.type = ICMP6_router_advertisement;
1126 rh.icmp.checksum = 0;
1128 rh.current_hop_limit = radv_info->curr_hop_limit;
1129 rh.router_lifetime_in_sec = clib_host_to_net_u16(radv_info->adv_router_lifetime_in_sec);
1130 rh.time_in_msec_between_retransmitted_neighbor_solicitations =
1131 clib_host_to_net_u32(radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1132 rh.neighbor_reachable_time_in_msec =
1133 clib_host_to_net_u32(radv_info->adv_neighbor_reachable_time_in_msec);
1135 rh.flags = (radv_info->adv_managed_flag) ? ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP : 0;
1136 rh.flags |= ( (radv_info->adv_other_flag) ? ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP : 0);
1139 u16 payload_length = sizeof(icmp6_router_advertisement_header_t);
1141 vlib_buffer_add_data (vm,
1142 p0->free_list_index,
1144 (void *)&rh, sizeof(icmp6_router_advertisement_header_t));
1146 if(radv_info->adv_link_layer_address)
1148 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t h;
1150 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1151 h.header.n_data_u64s = 1;
1153 /* copy ll address */
1154 clib_memcpy(&h.ethernet_address[0], eth_if0->address, 6);
1156 vlib_buffer_add_data (vm,
1157 p0->free_list_index,
1159 (void *)&h, sizeof(icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
1161 payload_length += sizeof(icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1164 /* add MTU option */
1165 if(radv_info->adv_link_mtu)
1167 icmp6_neighbor_discovery_mtu_option_t h;
1170 h.mtu = clib_host_to_net_u32(radv_info->adv_link_mtu);
1171 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1172 h.header.n_data_u64s = 1;
1174 payload_length += sizeof( icmp6_neighbor_discovery_mtu_option_t);
1176 vlib_buffer_add_data (vm,
1177 p0->free_list_index,
1179 (void *)&h, sizeof(icmp6_neighbor_discovery_mtu_option_t));
1182 /* add advertised prefix options */
1183 ip6_radv_prefix_t *pr_info;
1185 pool_foreach (pr_info, radv_info->adv_prefixes_pool, ({
1187 if(pr_info->enabled &&
1188 (!pr_info->decrement_lifetime_flag || (pr_info->pref_lifetime_expires >0)))
1190 /* advertise this prefix */
1191 icmp6_neighbor_discovery_prefix_information_option_t h;
1193 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1194 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1196 h.dst_address_length = pr_info->prefix_len;
1198 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1199 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
1201 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1203 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1204 h.preferred_time = 0;
1208 if(pr_info->decrement_lifetime_flag)
1210 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1211 (pr_info->valid_lifetime_expires - now) : 0;
1213 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1214 (pr_info->pref_lifetime_expires - now) : 0;
1217 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1218 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1222 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1224 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1226 vlib_buffer_add_data (vm,
1227 p0->free_list_index,
1229 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1234 /* add additional options before here */
1236 /* finish building the router advertisement... */
1237 if(!is_unspecified && radv_info->send_unicast)
1239 ip0->dst_address = ip0->src_address;
1243 /* target address is all-nodes mcast addr */
1244 ip6_set_reserved_multicast_address(&ip0->dst_address,
1245 IP6_MULTICAST_SCOPE_link_local,
1246 IP6_MULTICAST_GROUP_ID_all_hosts);
1249 /* source address MUST be the link-local address */
1250 ip0->src_address = radv_info->link_local_address;
1252 ip0->hop_limit = 255;
1253 ip0->payload_length = clib_host_to_net_u16 (payload_length);
1255 icmp6_router_advertisement_header_t * rh0 = (icmp6_router_advertisement_header_t *)(ip0 + 1);
1256 rh0->icmp.checksum =
1257 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1259 ASSERT(bogus_length == 0);
1261 /* setup output if and adjacency */
1262 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1263 vnet_main.local_interface_sw_if_index;
1265 if (is_solicitation)
1267 ethernet_header_t *eth0;
1268 /* Reuse current MAC header, copy SMAC to DMAC and
1269 * interface MAC to SMAC */
1270 vlib_buffer_reset (p0);
1271 eth0 = vlib_buffer_get_current(p0);
1272 clib_memcpy(eth0->dst_address, eth0->src_address, 6);
1273 clib_memcpy(eth0->src_address, eth_if0->address, 6);
1274 next0 = is_dropped ?
1275 next0 : ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1276 vnet_buffer(p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1280 adj_index0 = radv_info->all_nodes_adj_index;
1281 if (adj_index0 == 0)
1282 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1285 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, adj_index0);
1287 ((adj0->rewrite_header.sw_if_index != sw_if_index0
1288 || adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
1289 ? ICMP6_ERROR_ROUTER_SOLICITATION_DEST_UNKNOWN
1291 next0 = is_dropped ?
1292 next0 : ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1293 vnet_buffer (p0)->ip.adj_index[VLIB_RX] = adj_index0;
1297 radv_info->n_solicitations_dropped += is_dropped;
1298 radv_info->n_solicitations_rcvd += is_solicitation;
1300 if((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1302 radv_info->n_advertisements_sent++;
1303 n_advertisements_sent++;
1309 p0->error = error_node->errors[error0];
1311 if(error0 != ICMP6_ERROR_NONE)
1312 vlib_error_count (vm, error_node->node_index, error0, 1);
1314 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1315 to_next, n_left_to_next,
1320 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1323 /* Account for router advertisements sent. */
1324 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX, n_advertisements_sent);
1326 return frame->n_vectors;
1329 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1330 static_always_inline uword
1331 icmp6_router_advertisement(vlib_main_t * vm,
1332 vlib_node_runtime_t * node,
1333 vlib_frame_t * frame)
1335 vnet_main_t * vnm = vnet_get_main();
1336 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1337 uword n_packets = frame->n_vectors;
1338 u32 * from, * to_next;
1339 u32 n_left_from, n_left_to_next, next_index;
1340 u32 n_advertisements_rcvd = 0;
1342 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1344 from = vlib_frame_vector_args (frame);
1345 n_left_from = n_packets;
1346 next_index = node->cached_next_index;
1348 if (node->flags & VLIB_NODE_FLAG_TRACE)
1349 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1351 sizeof (icmp6_input_trace_t));
1353 while (n_left_from > 0)
1355 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1357 while (n_left_from > 0 && n_left_to_next > 0)
1361 ip6_radv_t *radv_info = 0;
1362 icmp6_router_advertisement_header_t * h0;
1363 u32 bi0, options_len0, sw_if_index0, next0, error0;
1365 bi0 = to_next[0] = from[0];
1370 n_left_to_next -= 1;
1372 p0 = vlib_get_buffer (vm, bi0);
1373 ip0 = vlib_buffer_get_current (p0);
1374 h0 = ip6_next_header (ip0);
1375 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1377 error0 = ICMP6_ERROR_NONE;
1378 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1380 /* Check that source address is link-local*/
1381 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1382 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1384 /* default is to drop */
1385 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1387 n_advertisements_rcvd++;
1389 if (error0 == ICMP6_ERROR_NONE)
1391 vnet_sw_interface_t * sw_if0;
1392 ethernet_interface_t * eth_if0;
1394 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1395 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1396 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1398 /* only support ethernet interface type for now */
1399 error0 = (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF : error0;
1401 if (error0 == ICMP6_ERROR_NONE)
1405 /* look up the radv_t information for this interface */
1406 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1408 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1411 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1413 error0 = ((!radv_info) ? ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : error0);
1415 if (error0 == ICMP6_ERROR_NONE)
1417 /* validate advertised information */
1418 if((h0->current_hop_limit && radv_info->curr_hop_limit) &&
1419 (h0->current_hop_limit != radv_info->curr_hop_limit))
1421 ip6_neighbor_syslog(vm, LOG_WARNING,
1422 "our AdvCurHopLimit on %U doesn't agree with %U",
1423 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1426 if((h0->flags & ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP) !=
1427 radv_info->adv_managed_flag)
1429 ip6_neighbor_syslog(vm, LOG_WARNING,
1430 "our AdvManagedFlag on %U doesn't agree with %U",
1431 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1434 if((h0->flags & ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP) !=
1435 radv_info->adv_other_flag)
1437 ip6_neighbor_syslog(vm, LOG_WARNING,
1438 "our AdvOtherConfigFlag on %U doesn't agree with %U",
1439 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1442 if((h0->time_in_msec_between_retransmitted_neighbor_solicitations &&
1443 radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations) &&
1444 (h0->time_in_msec_between_retransmitted_neighbor_solicitations !=
1445 clib_host_to_net_u32(radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
1447 ip6_neighbor_syslog(vm, LOG_WARNING,
1448 "our AdvRetransTimer on %U doesn't agree with %U",
1449 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1452 if((h0->neighbor_reachable_time_in_msec &&
1453 radv_info->adv_neighbor_reachable_time_in_msec) &&
1454 (h0->neighbor_reachable_time_in_msec !=
1455 clib_host_to_net_u32(radv_info->adv_neighbor_reachable_time_in_msec)))
1457 ip6_neighbor_syslog(vm, LOG_WARNING,
1458 "our AdvReachableTime on %U doesn't agree with %U",
1459 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1462 /* check for MTU or prefix options or .. */
1463 u8 * opt_hdr = (u8 *)(h0 + 1);
1464 while( options_len0 > 0)
1466 icmp6_neighbor_discovery_option_header_t *o0 = ( icmp6_neighbor_discovery_option_header_t *)opt_hdr;
1467 int opt_len = o0->n_data_u64s << 3;
1468 icmp6_neighbor_discovery_option_type_t option_type = o0->type;
1470 if(options_len0 < 2)
1472 ip6_neighbor_syslog(vm, LOG_ERR,
1473 "malformed RA packet on %U from %U",
1474 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1480 ip6_neighbor_syslog(vm, LOG_ERR,
1481 " zero length option in RA on %U from %U",
1482 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1485 else if( opt_len > options_len0)
1487 ip6_neighbor_syslog(vm, LOG_ERR,
1488 "option length in RA packet greater than total length on %U from %U",
1489 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1493 options_len0 -= opt_len;
1498 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
1500 icmp6_neighbor_discovery_mtu_option_t *h =
1501 (icmp6_neighbor_discovery_mtu_option_t *)(o0);
1503 if(opt_len < sizeof(*h))
1506 if((h->mtu && radv_info->adv_link_mtu) &&
1507 (h->mtu != clib_host_to_net_u32(radv_info->adv_link_mtu)))
1509 ip6_neighbor_syslog(vm, LOG_WARNING,
1510 "our AdvLinkMTU on %U doesn't agree with %U",
1511 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1516 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1518 icmp6_neighbor_discovery_prefix_information_option_t *h =
1519 (icmp6_neighbor_discovery_prefix_information_option_t *)(o0);
1521 /* validate advertised prefix options */
1522 ip6_radv_prefix_t *pr_info;
1523 u32 preferred, valid;
1525 if(opt_len < sizeof(*h))
1528 preferred = clib_net_to_host_u32(h->preferred_time);
1529 valid = clib_net_to_host_u32(h->valid_time);
1531 /* look for matching prefix - if we our advertising it, it better be consistant */
1532 pool_foreach (pr_info, radv_info->adv_prefixes_pool, ({
1535 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1537 if(pr_info->enabled &&
1538 (pr_info->prefix_len == h->dst_address_length) &&
1539 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1542 if(!pr_info->decrement_lifetime_flag &&
1543 valid != pr_info->adv_valid_lifetime_in_secs)
1545 ip6_neighbor_syslog(vm, LOG_WARNING,
1546 "our ADV validlifetime on %U for %U does not agree with %U",
1547 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1548 format_ip6_address, &h->dst_address);
1550 if(!pr_info->decrement_lifetime_flag &&
1551 preferred != pr_info->adv_pref_lifetime_in_secs)
1553 ip6_neighbor_syslog(vm, LOG_WARNING,
1554 "our ADV preferredlifetime on %U for %U does not agree with %U",
1555 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1556 format_ip6_address, &h->dst_address);
1572 p0->error = error_node->errors[error0];
1574 if(error0 != ICMP6_ERROR_NONE)
1575 vlib_error_count (vm, error_node->node_index, error0, 1);
1577 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1578 to_next, n_left_to_next,
1582 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1585 /* Account for router advertisements sent. */
1586 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX, n_advertisements_rcvd);
1588 return frame->n_vectors;
1591 /* create and initialize router advertisement parameters with default values for this intfc */
1593 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
1597 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1600 vnet_sw_interface_t * sw_if0;
1601 ethernet_interface_t * eth_if0 = 0;
1603 /* lookup radv container - ethernet interfaces only */
1604 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1605 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1606 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1611 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
1612 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1616 a = pool_elt_at_index (nm->if_radv_pool, ri);
1620 u32 i, * to_delete = 0;
1621 ip6_radv_prefix_t *p;
1622 ip6_mldp_group_t *m;
1624 /* remove adjacencies */
1625 adj_unlock(a->all_nodes_adj_index);
1626 adj_unlock(a->all_routers_adj_index);
1627 adj_unlock(a->all_mldv2_routers_adj_index);
1629 /* clean up prefix_pool */
1630 pool_foreach (p, a->adv_prefixes_pool, ({
1631 vec_add1 (to_delete, p - a->adv_prefixes_pool);
1634 for (i = 0; i < vec_len (to_delete); i++)
1636 p = pool_elt_at_index (a->adv_prefixes_pool, to_delete[i]);
1637 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
1638 pool_put (a->adv_prefixes_pool, p);
1641 vec_free (to_delete);
1644 /* clean up mldp group pool */
1645 pool_foreach (m, a->mldp_group_pool, ({
1646 vec_add1 (to_delete, m - a->mldp_group_pool);
1649 for (i = 0; i < vec_len (to_delete); i++)
1651 m = pool_elt_at_index (a->mldp_group_pool, to_delete[i]);
1652 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
1653 pool_put (a->mldp_group_pool, m);
1656 vec_free (to_delete);
1658 pool_put (nm->if_radv_pool, a);
1659 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
1661 ip6_sw_interface_enable_disable(sw_if_index, 0);
1668 vnet_hw_interface_t * hw_if0;
1670 ip6_sw_interface_enable_disable(sw_if_index, 1);
1671 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
1673 pool_get (nm->if_radv_pool, a);
1675 ri = a - nm->if_radv_pool;
1676 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
1678 /* initialize default values (most of which are zero) */
1679 memset (a, 0, sizeof (a[0]));
1681 a->sw_if_index = sw_if_index;
1683 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
1684 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
1685 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
1686 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
1688 a->adv_link_layer_address = 1; /* send ll address source address option */
1690 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
1691 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
1692 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
1693 a->seed = random_default_seed();
1695 /* for generating random interface ids */
1696 a->randomizer = 0x1119194911191949;
1697 a->randomizer = random_u64 ((u32 *)&a->randomizer);
1699 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS ;
1700 a->initial_adverts_sent = a->initial_adverts_count-1;
1701 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1703 /* deafult is to send */
1706 /* fill in radv_info for this interface that will be needed later */
1707 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
1709 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
1711 /* fill in default link-local address (this may be overridden) */
1712 ip6_link_local_address_from_ethernet_address (&a->link_local_address, eth_if0->address);
1713 a->link_local_prefix_len = 64;
1715 mhash_init (&a->address_to_prefix_index, sizeof (uword), sizeof (ip6_address_t));
1716 mhash_init (&a->address_to_mldp_index, sizeof (uword), sizeof (ip6_address_t));
1719 u8 link_layer_address[6] =
1720 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_all_hosts};
1722 a->all_nodes_adj_index = adj_rewrite_add_and_lock(FIB_PROTOCOL_IP6,
1725 link_layer_address);
1729 u8 link_layer_address[6] =
1730 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_all_routers};
1732 a->all_routers_adj_index = adj_rewrite_add_and_lock(FIB_PROTOCOL_IP6,
1735 link_layer_address);
1739 u8 link_layer_address[6] =
1740 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_mldv2_routers};
1742 a->all_mldv2_routers_adj_index =
1743 adj_rewrite_add_and_lock(FIB_PROTOCOL_IP6,
1746 link_layer_address);
1749 /* add multicast groups we will always be reporting */
1751 ip6_mldp_group_t *mcast_group_info;
1753 ip6_set_reserved_multicast_address (&addr,
1754 IP6_MULTICAST_SCOPE_link_local,
1755 IP6_MULTICAST_GROUP_ID_all_hosts);
1757 /* lookup mldp info for this interface */
1759 uword * p = mhash_get (&a->address_to_mldp_index, &addr);
1760 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1763 if(!mcast_group_info)
1767 pool_get (a->mldp_group_pool, mcast_group_info);
1769 mi = mcast_group_info - a->mldp_group_pool;
1770 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1772 mcast_group_info->type = 4;
1773 mcast_group_info->mcast_source_address_pool = 0;
1774 mcast_group_info->num_sources = 0;
1775 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1778 ip6_set_reserved_multicast_address (&addr,
1779 IP6_MULTICAST_SCOPE_link_local,
1780 IP6_MULTICAST_GROUP_ID_all_routers);
1782 p = mhash_get (&a->address_to_mldp_index, &addr);
1783 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1785 if(!mcast_group_info)
1789 pool_get (a->mldp_group_pool, mcast_group_info);
1791 mi = mcast_group_info - a->mldp_group_pool;
1792 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1794 mcast_group_info->type = 4;
1795 mcast_group_info->mcast_source_address_pool = 0;
1796 mcast_group_info->num_sources = 0;
1797 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1800 ip6_set_reserved_multicast_address (&addr,
1801 IP6_MULTICAST_SCOPE_link_local,
1802 IP6_MULTICAST_GROUP_ID_mldv2_routers);
1804 p = mhash_get (&a->address_to_mldp_index, &addr);
1805 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1807 if(!mcast_group_info)
1811 pool_get (a->mldp_group_pool, mcast_group_info);
1813 mi = mcast_group_info - a->mldp_group_pool;
1814 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1816 mcast_group_info->type = 4;
1817 mcast_group_info->mcast_source_address_pool = 0;
1818 mcast_group_info->num_sources = 0;
1819 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1826 /* send an mldpv2 report */
1828 ip6_neighbor_send_mldpv2_report(u32 sw_if_index)
1830 vnet_main_t * vnm = vnet_get_main();
1831 vlib_main_t * vm = vnm->vlib_main;
1832 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1833 vnet_sw_interface_t * sw_if0;
1834 ethernet_interface_t * eth_if0;
1838 ip6_radv_t *radv_info;
1848 icmp6_multicast_listener_report_header_t *rh0;
1849 icmp6_multicast_listener_report_packet_t *rp0;
1851 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1852 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1853 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1855 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
1858 /* look up the radv_t information for this interface */
1859 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
1861 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1866 /* send report now - build a mldpv2 report packet */
1867 n_allocated = vlib_buffer_alloc_from_free_list(vm,
1870 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1871 if (PREDICT_FALSE(n_allocated == 0))
1873 clib_warning ("buffer allocation failure");
1877 b0 = vlib_get_buffer (vm, bo0);
1879 /* adjust the sizeof the buffer to just include the ipv6 header */
1880 b0->current_length = sizeof(icmp6_multicast_listener_report_packet_t);
1882 payload_length = sizeof(icmp6_multicast_listener_report_header_t);
1884 b0->error = ICMP6_ERROR_NONE;
1886 rp0 = vlib_buffer_get_current (b0);
1887 ip0 = (ip6_header_t *)&rp0-> ip;
1888 rh0 = (icmp6_multicast_listener_report_header_t *)&rp0-> report_hdr;
1890 memset (rp0 , 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
1892 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
1894 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
1895 /* for DEBUG - vnet driver won't seem to emit router alerts */
1896 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
1899 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
1901 /* source address MUST be the link-local address */
1902 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1903 ip0->src_address = radv_info->link_local_address;
1905 /* destination is all mldpv2 routers */
1906 ip6_set_reserved_multicast_address(&ip0->dst_address,
1907 IP6_MULTICAST_SCOPE_link_local,
1908 IP6_MULTICAST_GROUP_ID_mldv2_routers);
1910 /* add reports here */
1911 ip6_mldp_group_t *m;
1912 int num_addr_records = 0;
1913 icmp6_multicast_address_record_t rr;
1915 /* fill in the hop-by-hop extension header (router alert) info */
1916 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
1917 rh0->ext_hdr.n_data_u64s = 0;
1919 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
1921 rh0->alert.value = 0;
1926 rh0->icmp.checksum = 0;
1928 pool_foreach (m, radv_info->mldp_group_pool, ({
1931 rr.aux_data_len_u32s = 0;
1932 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
1933 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
1937 vlib_buffer_add_data (vm,
1938 b0->free_list_index,
1940 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
1942 payload_length += sizeof( icmp6_multicast_address_record_t);
1946 rh0->num_addr_records = clib_host_to_net_u16(num_addr_records);
1948 /* update lengths */
1949 ip0->payload_length = clib_host_to_net_u16 (payload_length);
1951 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
1953 ASSERT(bogus_length == 0);
1956 * OK to override w/ no regard for actual FIB, because
1957 * ip6-rewrite-local only looks at the adjacency.
1959 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1960 vnet_main.local_interface_sw_if_index;
1962 vnet_buffer (b0)->ip.adj_index[VLIB_RX] =
1963 radv_info->all_mldv2_routers_adj_index;
1965 vlib_node_t * node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-local");
1967 f = vlib_get_frame_to_node (vm, node->index);
1968 to_next = vlib_frame_vector_args (f);
1972 vlib_put_frame_to_node (vm, node->index, f);
1976 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) = {
1977 .function = icmp6_router_solicitation,
1978 .name = "icmp6-router-solicitation",
1980 .vector_size = sizeof (u32),
1982 .format_trace = format_icmp6_input_trace,
1984 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
1986 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "error-drop",
1987 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-local",
1988 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
1992 /* send a RA or update the timer info etc.. */
1994 ip6_neighbor_process_timer_event (vlib_main_t * vm,
1995 vlib_node_runtime_t * node,
1996 vlib_frame_t * frame)
1998 vnet_main_t * vnm = vnet_get_main();
1999 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2000 ip6_radv_t *radv_info;
2001 vlib_frame_t * f = 0;
2002 u32 n_this_frame = 0;
2003 u32 n_left_to_next = 0;
2006 icmp6_router_solicitation_header_t * h0;
2008 f64 now = vlib_time_now (vm);
2010 /* Interface ip6 radv info list */
2011 pool_foreach (radv_info, nm->if_radv_pool, ({
2013 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2015 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2016 radv_info->next_multicast_time = now;
2017 radv_info->last_multicast_time = now;
2018 radv_info->last_radv_time = 0;
2019 radv_info->all_routers_mcast = 0;
2023 /* Make sure that we've joined the all-routers multicast group */
2024 if(!radv_info->all_routers_mcast)
2026 /* send MDLP_REPORT_EVENT message */
2027 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2028 radv_info->all_routers_mcast = 1;
2031 /* is it time to send a multicast RA on this interface? */
2032 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2037 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2038 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2040 /* multicast send - compute next multicast send time */
2041 if( radv_info->initial_adverts_sent > 0)
2043 radv_info->initial_adverts_sent--;
2044 if(rfn > radv_info-> initial_adverts_interval)
2045 rfn = radv_info-> initial_adverts_interval;
2047 /* check to see if we are ceasing to send */
2048 if( radv_info->initial_adverts_sent == 0)
2049 if(radv_info->cease_radv)
2050 radv_info->send_radv = 0;
2053 radv_info->next_multicast_time = rfn + now;
2054 radv_info->last_multicast_time = now;
2056 /* send advert now - build a "solicted" router advert with unspecified source address */
2057 n_allocated = vlib_buffer_alloc_from_free_list(vm,
2060 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2062 if (PREDICT_FALSE(n_allocated == 0))
2064 clib_warning ("buffer allocation failure");
2067 b0 = vlib_get_buffer (vm, bo0);
2068 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2069 b0->error = ICMP6_ERROR_NONE;
2070 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2072 h0 = vlib_buffer_get_current (b0);
2074 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2076 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2077 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2078 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2079 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2080 h0->ip.hop_limit = 255;
2082 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2083 h0->ip.src_address.as_u64[0] = 0;
2084 h0->ip.src_address.as_u64[1] = 0;
2086 h0->ip.dst_address.as_u64[0] = 0;
2087 h0->ip.dst_address.as_u64[1] = 0;
2089 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2091 if (PREDICT_FALSE(f == 0))
2093 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2094 to_next = vlib_frame_vector_args (f);
2095 n_left_to_next = VLIB_FRAME_SIZE;
2104 if (PREDICT_FALSE(n_left_to_next == 0))
2106 f->n_vectors = n_this_frame;
2107 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2115 ASSERT(n_this_frame);
2116 f->n_vectors = n_this_frame;
2117 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2123 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2124 vlib_node_runtime_t * node,
2125 vlib_frame_t * frame)
2128 ip6_icmp_neighbor_discovery_event_data_t * event_data;
2130 /* init code here */
2134 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */);
2136 event_data = vlib_process_get_event_data (vm, &event_type);
2140 /* No events found: timer expired. */
2141 /* process interface list and send RAs as appropriate, update timer info */
2142 ip6_neighbor_process_timer_event (vm, node, frame);
2146 switch (event_type) {
2148 case ICMP6_ND_EVENT_INIT:
2159 _vec_len (event_data) = 0;
2162 return frame->n_vectors;
2165 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) = {
2166 .function = icmp6_router_advertisement,
2167 .name = "icmp6-router-advertisement",
2169 .vector_size = sizeof (u32),
2171 .format_trace = format_icmp6_input_trace,
2179 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2181 .function = ip6_icmp_neighbor_discovery_event_process,
2182 .name = "ip6-icmp-neighbor-discovery-event-process",
2183 .type = VLIB_NODE_TYPE_PROCESS,
2187 icmp6_neighbor_solicitation (vlib_main_t * vm,
2188 vlib_node_runtime_t * node,
2189 vlib_frame_t * frame)
2190 { return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, /* is_solicitation */ 1); }
2193 icmp6_neighbor_advertisement (vlib_main_t * vm,
2194 vlib_node_runtime_t * node,
2195 vlib_frame_t * frame)
2196 { return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, /* is_solicitation */ 0); }
2198 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) = {
2199 .function = icmp6_neighbor_solicitation,
2200 .name = "icmp6-neighbor-solicitation",
2202 .vector_size = sizeof (u32),
2204 .format_trace = format_icmp6_input_trace,
2206 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2208 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "error-drop",
2209 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2213 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) = {
2214 .function = icmp6_neighbor_advertisement,
2215 .name = "icmp6-neighbor-advertisement",
2217 .vector_size = sizeof (u32),
2219 .format_trace = format_icmp6_input_trace,
2227 /* API support functions */
2229 ip6_neighbor_ra_config(vlib_main_t * vm, u32 sw_if_index,
2230 u8 suppress, u8 managed, u8 other,
2231 u8 ll_option, u8 send_unicast, u8 cease,
2232 u8 use_lifetime, u32 lifetime,
2233 u32 initial_count, u32 initial_interval,
2234 u32 max_interval, u32 min_interval,
2237 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2241 /* look up the radv_t information for this interface */
2242 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2243 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2244 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2249 ip6_radv_t * radv_info;
2250 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2252 if((max_interval != 0) && (min_interval ==0))
2253 min_interval = .75 * max_interval;
2255 max_interval = (max_interval != 0) ? ( (is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) : radv_info->max_radv_interval;
2256 min_interval = (min_interval != 0) ? ( (is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) : radv_info->min_radv_interval;
2257 lifetime = (use_lifetime != 0) ? ( (is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) : radv_info->adv_router_lifetime_in_sec;
2261 if(lifetime > MAX_DEF_RTR_LIFETIME)
2262 lifetime = MAX_DEF_RTR_LIFETIME;
2264 if(lifetime <= max_interval)
2265 return VNET_API_ERROR_INVALID_VALUE;
2268 if(min_interval != 0)
2270 if((min_interval > .75 * max_interval) ||
2272 return VNET_API_ERROR_INVALID_VALUE;
2275 if((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2276 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2277 return VNET_API_ERROR_INVALID_VALUE;
2280 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2281 if "flag" is clear don't change corresponding value
2283 radv_info->send_radv = (suppress != 0) ? ( (is_no != 0) ? 1 : 0 ) : radv_info->send_radv;
2284 radv_info->adv_managed_flag = ( managed != 0) ? ( (is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2285 radv_info->adv_other_flag = (other != 0) ? ( (is_no) ? 0: 1) : radv_info->adv_other_flag;
2286 radv_info->adv_link_layer_address = ( ll_option != 0) ? ( (is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2287 radv_info->send_unicast = (send_unicast != 0) ? ( (is_no) ? 0 : 1) : radv_info->send_unicast;
2288 radv_info->cease_radv = ( cease != 0) ? ( (is_no) ? 0 : 1) : radv_info->cease_radv;
2290 radv_info->min_radv_interval = min_interval;
2291 radv_info->max_radv_interval = max_interval;
2292 radv_info->adv_router_lifetime_in_sec = lifetime;
2294 radv_info->initial_adverts_count =
2295 (initial_count != 0) ? ( (is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) : radv_info->initial_adverts_count ;
2296 radv_info->initial_adverts_interval =
2297 (initial_interval != 0) ? ( (is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) : radv_info->initial_adverts_interval;
2300 if((cease != 0) && (is_no))
2301 radv_info-> send_radv = 1;
2303 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2304 radv_info->next_multicast_time = vlib_time_now (vm);
2305 radv_info->last_multicast_time = vlib_time_now (vm);
2306 radv_info->last_radv_time = 0;
2312 ip6_neighbor_ra_prefix(vlib_main_t * vm, u32 sw_if_index,
2313 ip6_address_t *prefix_addr, u8 prefix_len,
2314 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2315 u8 no_advertise, u8 off_link, u8 no_autoconfig, u8 no_onlink,
2318 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2323 /* look up the radv_t information for this interface */
2324 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2326 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2328 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2332 f64 now = vlib_time_now (vm);
2333 ip6_radv_t * radv_info;
2334 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2336 /* prefix info add, delete or update */
2337 ip6_radv_prefix_t * prefix;
2339 /* lookup prefix info for this address on this interface */
2340 uword * p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2342 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2348 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
2350 if(prefix->prefix_len != prefix_len)
2351 return VNET_API_ERROR_INVALID_VALUE_2;
2353 /* FIXME - Should the DP do this or the CP ?*/
2354 /* do specific delete processing here before returning */
2355 /* try to remove from routing table */
2357 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,/* old_value */ 0);
2358 pool_put (radv_info->adv_prefixes_pool, prefix);
2360 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2361 radv_info->next_multicast_time = vlib_time_now (vm);
2362 radv_info->last_multicast_time = vlib_time_now (vm);
2363 radv_info->last_radv_time = 0;
2367 /* adding or changing */
2372 pool_get (radv_info->adv_prefixes_pool, prefix);
2373 pi = prefix - radv_info->adv_prefixes_pool;
2374 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi, /* old_value */ 0);
2376 memset(prefix, 0x0, sizeof(ip6_radv_prefix_t));
2378 prefix->prefix_len = prefix_len;
2379 clib_memcpy(&prefix->prefix, prefix_addr, sizeof(ip6_address_t));
2381 /* initialize default values */
2382 prefix->adv_on_link_flag = 1; /* L bit set */
2383 prefix->adv_autonomous_flag = 1; /* A bit set */
2384 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
2385 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2386 prefix->enabled = 1;
2387 prefix->decrement_lifetime_flag = 1;
2388 prefix->deprecated_prefix_flag = 1;
2392 /* FIXME - Should the DP do this or the CP ?*/
2393 /* insert prefix into routing table as a connected prefix */
2402 if(prefix->prefix_len != prefix_len)
2403 return VNET_API_ERROR_INVALID_VALUE_2;
2407 /* FIXME - Should the DP do this or the CP ?*/
2408 /* remove from routing table if already there */
2412 if((val_lifetime == ~0) || (pref_lifetime == ~0))
2414 prefix->adv_valid_lifetime_in_secs = ~0;
2415 prefix->adv_pref_lifetime_in_secs = ~0;
2416 prefix->decrement_lifetime_flag = 0;
2420 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2421 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
2424 /* copy remaining */
2425 prefix->enabled = !(no_advertise != 0);
2426 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2427 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2431 /* fill in the expiration times */
2432 prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs;
2433 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
2435 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2436 radv_info->next_multicast_time = vlib_time_now (vm);
2437 radv_info->last_multicast_time = vlib_time_now (vm);
2438 radv_info->last_radv_time = 0;
2444 ip6_neighbor_cmd(vlib_main_t * vm, unformat_input_t * main_input, vlib_cli_command_t * cmd)
2446 vnet_main_t * vnm = vnet_get_main();
2447 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2448 clib_error_t * error = 0;
2450 u8 suppress = 0, managed = 0, other = 0;
2451 u8 suppress_ll_option = 0, send_unicast = 0, cease= 0;
2452 u8 use_lifetime = 0;
2453 u32 sw_if_index, ra_lifetime = 0, ra_initial_count = 0, ra_initial_interval = 0;
2454 u32 ra_max_interval = 0 , ra_min_interval = 0;
2456 unformat_input_t _line_input, * line_input = &_line_input;
2457 vnet_sw_interface_t * sw_if0;
2459 int add_radv_info = 1;
2460 __attribute__((unused)) ip6_radv_t * radv_info = 0;
2461 ip6_address_t ip6_addr;
2465 /* Get a line of input. */
2466 if (! unformat_user (main_input, unformat_line_input, line_input))
2469 /* get basic radv info for this interface */
2470 if(unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2473 if (unformat_user (line_input,
2474 unformat_vnet_sw_interface, vnm, &sw_if_index))
2477 ethernet_interface_t * eth_if0 = 0;
2479 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2480 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2481 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2485 error = clib_error_return (0, "Interface must be of ethernet type");
2489 /* look up the radv_t information for this interface */
2490 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2492 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2496 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2500 error = clib_error_return (0, "unknown interface %U'",
2501 format_unformat_error, line_input);
2507 error = clib_error_return (0, "invalid interface name %U'",
2508 format_unformat_error, line_input);
2513 /* get the rest of the command */
2514 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2516 if (unformat (line_input, "no"))
2518 else if(unformat (line_input, "prefix %U/%d",
2519 unformat_ip6_address, &ip6_addr,
2525 else if (unformat (line_input, "ra-managed-config-flag"))
2530 else if (unformat (line_input, "ra-other-config-flag"))
2535 else if (unformat (line_input, "ra-suppress") ||
2536 unformat (line_input, "ra-surpress"))
2541 else if (unformat (line_input, "ra-suppress-link-layer") ||
2542 unformat (line_input, "ra-surpress-link-layer"))
2544 suppress_ll_option = 1;
2547 else if (unformat (line_input, "ra-send-unicast"))
2552 else if (unformat (line_input, "ra-lifetime"))
2554 if (!unformat (line_input, "%d", &ra_lifetime))
2555 return(error = unformat_parse_error (line_input));
2559 else if (unformat (line_input, "ra-initial"))
2561 if (!unformat (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
2562 return(error = unformat_parse_error (line_input));
2565 else if (unformat (line_input, "ra-interval"))
2567 if (!unformat (line_input, "%d", &ra_max_interval))
2568 return(error = unformat_parse_error (line_input));
2570 if (!unformat (line_input, "%d", &ra_min_interval))
2571 ra_min_interval = 0;
2574 else if(unformat (line_input, "ra-cease"))
2580 return(unformat_parse_error (line_input));
2585 ip6_neighbor_ra_config(vm, sw_if_index,
2586 suppress, managed, other,
2587 suppress_ll_option, send_unicast, cease,
2588 use_lifetime, ra_lifetime,
2589 ra_initial_count, ra_initial_interval,
2590 ra_max_interval, ra_min_interval,
2595 u32 valid_lifetime_in_secs = 0;
2596 u32 pref_lifetime_in_secs = 0;
2597 u8 use_prefix_default_values = 0;
2598 u8 no_advertise = 0;
2600 u8 no_autoconfig = 0;
2603 /* get the rest of the command */
2604 while(unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2606 if(unformat (line_input, "default"))
2608 use_prefix_default_values = 1;
2611 else if(unformat (line_input, "infinite"))
2613 valid_lifetime_in_secs = ~0;
2614 pref_lifetime_in_secs = ~0;
2617 else if(unformat (line_input, "%d %d", &valid_lifetime_in_secs,
2618 &pref_lifetime_in_secs))
2625 /* get the rest of the command */
2626 while (!use_prefix_default_values &&
2627 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2629 if(unformat (line_input, "no-advertise"))
2631 else if(unformat (line_input, "off-link"))
2633 else if(unformat (line_input, "no-autoconfig"))
2635 else if(unformat (line_input, "no-onlink"))
2638 return(unformat_parse_error (line_input));
2641 ip6_neighbor_ra_prefix(vm, sw_if_index,
2642 &ip6_addr, addr_len,
2643 use_prefix_default_values,
2644 valid_lifetime_in_secs,
2645 pref_lifetime_in_secs,
2653 unformat_free (line_input);
2660 ip6_print_addrs(vlib_main_t * vm,
2663 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2666 for (i = 0; i < vec_len (addrs); i++)
2668 ip_interface_address_t * a = pool_elt_at_index(lm->if_address_pool, addrs[i]);
2669 ip6_address_t * address = ip_interface_address_get_address (lm, a);
2671 vlib_cli_output (vm, "\t\t%U/%d",
2672 format_ip6_address, address,
2677 static clib_error_t *
2678 show_ip6_interface_cmd (vlib_main_t * vm,
2679 unformat_input_t * input,
2680 vlib_cli_command_t * cmd)
2682 vnet_main_t * vnm = vnet_get_main();
2683 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2684 clib_error_t * error = 0;
2689 if (unformat_user (input,
2690 unformat_vnet_sw_interface, vnm, &sw_if_index))
2694 /* look up the radv_t information for this interface */
2695 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2697 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2701 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2702 ip6_radv_t * radv_info;
2703 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2705 vlib_cli_output (vm, "%U is admin %s\n", format_vnet_sw_interface_name, vnm,
2706 vnet_get_sw_interface (vnm, sw_if_index),
2707 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ? "up" : "down"));
2710 u32 *link_scope = 0, *global_scope = 0;
2711 u32 *local_scope = 0, *unknown_scope = 0;
2712 ip_interface_address_t * a;
2714 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
2715 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
2717 while (ai != (u32)~0)
2719 a = pool_elt_at_index(lm->if_address_pool, ai);
2720 ip6_address_t * address = ip_interface_address_get_address (lm, a);
2722 if (ip6_address_is_link_local_unicast (address))
2723 vec_add1 (link_scope, ai);
2724 else if(ip6_address_is_global_unicast (address))
2725 vec_add1 (global_scope, ai);
2726 else if(ip6_address_is_local_unicast (address))
2727 vec_add1 (local_scope, ai);
2729 vec_add1 (unknown_scope, ai);
2731 ai = a->next_this_sw_interface;
2734 if (vec_len (link_scope))
2736 vlib_cli_output (vm, "\tLink-local address(es):\n");
2737 ip6_print_addrs (vm, link_scope);
2738 vec_free (link_scope);
2741 if (vec_len (local_scope))
2743 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
2744 ip6_print_addrs (vm, local_scope);
2745 vec_free (local_scope);
2748 if (vec_len (global_scope))
2750 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
2751 ip6_print_addrs (vm, global_scope);
2752 vec_free (global_scope);
2755 if (vec_len (unknown_scope))
2757 vlib_cli_output (vm, "\tOther-scope address(es):\n");
2758 ip6_print_addrs (vm, unknown_scope);
2759 vec_free (unknown_scope);
2762 vlib_cli_output (vm, "\tJoined group address(es):\n");
2763 ip6_mldp_group_t *m;
2764 pool_foreach (m, radv_info->mldp_group_pool, ({
2765 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address, &m->mcast_address);
2768 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
2769 ip6_radv_prefix_t * p;
2770 pool_foreach (p, radv_info->adv_prefixes_pool, ({
2771 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
2772 format_ip6_address, &p->prefix, p->prefix_len);
2775 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
2776 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
2777 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
2778 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
2779 vlib_cli_output (vm, "\tND DAD is disabled\n");
2780 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
2781 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
2782 radv_info->adv_neighbor_reachable_time_in_msec);
2783 vlib_cli_output (vm, "\tND advertised retransmit interval is %d (msec)\n",
2784 radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations);
2786 u32 ra_interval = radv_info->max_radv_interval;
2787 u32 ra_interval_min = radv_info->min_radv_interval;
2788 vlib_cli_output (vm, "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
2789 ra_interval, ra_interval_min);
2790 vlib_cli_output (vm, "\tND router advertisements live for %d seconds\n",
2791 radv_info->adv_router_lifetime_in_sec);
2792 vlib_cli_output (vm, "\tHosts %s stateless autoconfig for addresses\n",
2793 (radv_info->adv_managed_flag) ? "use" :" don't use");
2794 vlib_cli_output (vm, "\tND router advertisements sent %d\n", radv_info->n_advertisements_sent);
2795 vlib_cli_output (vm, "\tND router solicitations received %d\n", radv_info->n_solicitations_rcvd);
2796 vlib_cli_output (vm, "\tND router solicitations dropped %d\n", radv_info->n_solicitations_dropped);
2800 error = clib_error_return (0, "IPv6 not enabled on interface",
2801 format_unformat_error, input);
2808 VLIB_CLI_COMMAND (show_ip6_interface_command, static) = {
2809 .path = "show ip6 interface",
2810 .function = show_ip6_interface_cmd,
2811 .short_help = "show ip6 interface <iface name>",
2815 disable_ip6_interface(vlib_main_t * vm,
2818 clib_error_t * error = 0;
2819 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2822 /* look up the radv_t information for this interface */
2823 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2824 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2826 /* if not created - do nothing */
2829 vnet_main_t * vnm = vnet_get_main();
2830 ip6_radv_t * radv_info;
2832 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2834 /* check radv_info ref count for other ip6 addresses on this interface */
2835 if(radv_info->ref_count == 0 )
2837 /* essentially "disables" ipv6 on this interface */
2838 error = ip6_add_del_interface_address (vm, sw_if_index,
2839 &radv_info->link_local_address,
2840 radv_info->link_local_prefix_len,
2843 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, 0/* is_add */);
2850 ip6_interface_enabled(vlib_main_t * vm,
2853 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2856 /* look up the radv_t information for this interface */
2857 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2859 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2865 enable_ip6_interface(vlib_main_t * vm,
2868 clib_error_t * error = 0;
2869 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2873 /* look up the radv_t information for this interface */
2874 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2876 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2878 /* if not created yet */
2881 vnet_main_t * vnm = vnet_get_main();
2882 vnet_sw_interface_t * sw_if0;
2884 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2885 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2887 ethernet_interface_t * eth_if0;
2889 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2892 /* create radv_info. for this interface. This holds all the info needed for router adverts */
2893 ri = ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
2897 ip6_radv_t * radv_info;
2898 ip6_address_t link_local_address;
2900 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2902 ip6_link_local_address_from_ethernet_mac_address (&link_local_address,
2905 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
2906 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB)
2908 /* make up an interface id */
2912 link_local_address.as_u64[0] = radv_info->randomizer;
2915 md5_add (&m, &link_local_address, 16);
2916 md5_finish (&m, digest);
2918 clib_memcpy(&link_local_address, digest, 16);
2920 radv_info->randomizer = link_local_address.as_u64[0];
2922 link_local_address.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2924 link_local_address.as_u8[8] &= 0xfd;
2927 /* essentially "enables" ipv6 on this interface */
2928 error = ip6_add_del_interface_address (vm, sw_if_index,
2929 &link_local_address,
2930 128 /* address width */,
2934 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, !is_add);
2937 radv_info->link_local_address = link_local_address;
2938 radv_info->link_local_prefix_len = 64;
2947 static clib_error_t *
2948 enable_ip6_interface_cmd (vlib_main_t * vm,
2949 unformat_input_t * input,
2950 vlib_cli_command_t * cmd)
2952 vnet_main_t * vnm = vnet_get_main();
2953 clib_error_t * error = 0;
2958 if (unformat_user (input,
2959 unformat_vnet_sw_interface, vnm, &sw_if_index))
2961 enable_ip6_interface(vm, sw_if_index);
2965 error = clib_error_return (0, "unknown interface\n'",
2966 format_unformat_error, input);
2972 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) = {
2973 .path = "enable ip6 interface",
2974 .function = enable_ip6_interface_cmd,
2975 .short_help = "enable ip6 interface <iface name>",
2978 static clib_error_t *
2979 disable_ip6_interface_cmd (vlib_main_t * vm,
2980 unformat_input_t * input,
2981 vlib_cli_command_t * cmd)
2983 vnet_main_t * vnm = vnet_get_main();
2984 clib_error_t * error = 0;
2989 if (unformat_user (input,
2990 unformat_vnet_sw_interface, vnm, &sw_if_index))
2992 error = disable_ip6_interface(vm, sw_if_index);
2996 error = clib_error_return (0, "unknown interface\n'",
2997 format_unformat_error, input);
3003 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) = {
3004 .path = "disable ip6 interface",
3005 .function = disable_ip6_interface_cmd,
3006 .short_help = "disable ip6 interface <iface name>",
3009 VLIB_CLI_COMMAND (ip6_nd_command, static) = {
3011 .short_help = "Set ip6 neighbor discovery parameters",
3012 .function = ip6_neighbor_cmd,
3016 set_ip6_link_local_address(vlib_main_t * vm,
3018 ip6_address_t *address,
3021 clib_error_t * error = 0;
3022 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3024 ip6_radv_t * radv_info;
3025 vnet_main_t * vnm = vnet_get_main();
3027 if( !ip6_address_is_link_local_unicast (address))
3029 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
3030 return(error = clib_error_return (0, "address not link-local",
3031 format_unformat_error));
3034 /* call enable ipv6 */
3035 enable_ip6_interface(vm, sw_if_index);
3037 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3041 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3043 /* save if link local address (overwrite default) */
3045 /* delete the old one */
3046 error = ip6_add_del_interface_address (vm, sw_if_index,
3047 &radv_info->link_local_address,
3048 radv_info->link_local_prefix_len /* address width */,
3053 /* add the new one */
3054 error = ip6_add_del_interface_address (vm, sw_if_index,
3056 address_length /* address width */,
3061 radv_info->link_local_address = *address;
3062 radv_info->link_local_prefix_len = address_length;
3068 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3069 error = clib_error_return (0, "ip6 not enabled for interface",
3070 format_unformat_error);
3076 set_ip6_link_local_address_cmd (vlib_main_t * vm,
3077 unformat_input_t * input,
3078 vlib_cli_command_t * cmd)
3080 vnet_main_t * vnm = vnet_get_main();
3081 clib_error_t * error = 0;
3083 ip6_address_t ip6_addr;
3086 if (unformat_user (input,
3087 unformat_vnet_sw_interface, vnm, &sw_if_index))
3089 /* get the rest of the command */
3090 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3092 if(unformat (input, "%U/%d",
3093 unformat_ip6_address, &ip6_addr,
3097 return(unformat_parse_error (input));
3100 error = set_ip6_link_local_address(vm,
3107 VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) = {
3108 .path = "set ip6 link-local address",
3109 .short_help = "Set ip6 interface link-local address <intfc> <address.>",
3110 .function = set_ip6_link_local_address_cmd,
3113 /* callback when an interface address is added or deleted */
3115 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3118 ip6_address_t * address,
3120 u32 if_address_index,
3123 vnet_main_t * vnm = vnet_get_main();
3124 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3126 vlib_main_t * vm = vnm->vlib_main;
3127 ip6_radv_t * radv_info;
3129 ip6_mldp_group_t *mcast_group_info;
3131 /* create solicited node multicast address for this interface adddress */
3132 ip6_set_solicited_node_multicast_address (&a, 0);
3134 a.as_u8[0xd] = address->as_u8[0xd];
3135 a.as_u8[0xe] = address->as_u8[0xe];
3136 a.as_u8[0xf] = address->as_u8[0xf];
3140 /* try to create radv_info - does nothing if ipv6 already enabled */
3141 enable_ip6_interface(vm, sw_if_index);
3143 /* look up the radv_t information for this interface */
3144 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
3145 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3149 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3152 if( !ip6_address_is_link_local_unicast (address))
3153 radv_info->ref_count++;
3155 /* lookup prefix info for this address on this interface */
3156 uword * p = mhash_get (&radv_info->address_to_mldp_index, &a);
3157 mcast_group_info = p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3159 /* add -solicted node multicast address */
3160 if(!mcast_group_info)
3164 pool_get (radv_info->mldp_group_pool, mcast_group_info);
3166 mi = mcast_group_info - radv_info->mldp_group_pool;
3167 mhash_set (&radv_info->address_to_mldp_index, &a, mi, /* old_value */ 0);
3169 mcast_group_info->type = 4;
3170 mcast_group_info->mcast_source_address_pool = 0;
3171 mcast_group_info->num_sources = 0;
3172 clib_memcpy(&mcast_group_info->mcast_address, &a, sizeof(ip6_address_t));
3180 /* look up the radv_t information for this interface */
3181 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
3182 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3186 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3188 /* lookup prefix info for this address on this interface */
3189 uword * p = mhash_get (&radv_info->address_to_mldp_index, &a);
3190 mcast_group_info = p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3192 if(mcast_group_info)
3194 mhash_unset (&radv_info->address_to_mldp_index, &a,/* old_value */ 0);
3195 pool_put (radv_info->mldp_group_pool, mcast_group_info);
3198 /* if interface up send MLDP "report" */
3199 radv_info->all_routers_mcast = 0;
3202 if( !ip6_address_is_link_local_unicast (address))
3203 radv_info->ref_count--;
3208 clib_error_t *ip6_set_neighbor_limit (u32 neighbor_limit)
3210 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3212 nm->limit_neighbor_cache_size = neighbor_limit;
3216 static clib_error_t * ip6_neighbor_init (vlib_main_t * vm)
3218 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3219 ip6_main_t * im = &ip6_main;
3221 mhash_init (&nm->neighbor_index_by_key,
3222 /* value size */ sizeof (uword),
3223 /* key size */ sizeof (ip6_neighbor_key_t));
3225 icmp6_register_type (vm, ICMP6_neighbor_solicitation, ip6_icmp_neighbor_solicitation_node.index);
3226 icmp6_register_type (vm, ICMP6_neighbor_advertisement, ip6_icmp_neighbor_advertisement_node.index);
3227 icmp6_register_type (vm, ICMP6_router_solicitation, ip6_icmp_router_solicitation_node.index);
3228 icmp6_register_type (vm, ICMP6_router_advertisement, ip6_icmp_router_advertisement_node.index);
3230 /* handler node for ip6 neighbor discovery events and timers */
3231 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3233 /* add call backs */
3234 ip6_add_del_interface_address_callback_t cb;
3235 memset(&cb, 0x0, sizeof(ip6_add_del_interface_address_callback_t));
3237 /* when an interface address changes... */
3238 cb.function = ip6_neighbor_add_del_interface_address;
3239 cb.function_opaque = 0;
3240 vec_add1 (im->add_del_interface_address_callbacks, cb);
3242 mhash_init (&nm->pending_resolutions_by_address,
3243 /* value size */ sizeof (uword),
3244 /* key size */ sizeof (ip6_address_t));
3246 mhash_init (&nm->mac_changes_by_address,
3247 /* value size */ sizeof (uword),
3248 /* key size */ sizeof (ip6_address_t));
3250 /* default, configurable */
3251 nm->limit_neighbor_cache_size = 50000;
3254 /* $$$$ Hack fix for today */
3255 vec_validate_init_empty
3256 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */);
3262 VLIB_INIT_FUNCTION (ip6_neighbor_init);
3265 void vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
3271 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3272 ip6_address_t * address = address_arg;
3274 pending_resolution_t * pr;
3276 pool_get (nm->pending_resolutions, pr);
3278 pr->next_index = ~0;
3279 pr->node_index = node_index;
3280 pr->type_opaque = type_opaque;
3283 p = mhash_get (&nm->pending_resolutions_by_address, address);
3286 /* Insert new resolution at the head of the list */
3287 pr->next_index = p[0];
3288 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
3291 mhash_set (&nm->pending_resolutions_by_address, address,
3292 pr - nm->pending_resolutions, 0 /* old value */);
3295 int vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
3296 void * data_callback,
3304 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3305 ip6_address_t * address = address_arg;
3307 pending_resolution_t * mc;
3308 void (*fp)(u32, u8 *) = data_callback;
3312 pool_get (nm->mac_changes, mc);
3314 mc->next_index = ~0;
3315 mc->node_index = node_index;
3316 mc->type_opaque = type_opaque;
3318 mc->data_callback = data_callback;
3321 p = mhash_get (&nm->mac_changes_by_address, address);
3324 /* Insert new resolution at the head of the list */
3325 mc->next_index = p[0];
3326 mhash_unset (&nm->mac_changes_by_address, address, 0);
3329 mhash_set (&nm->mac_changes_by_address, address,
3330 mc - nm->mac_changes, 0);
3336 pending_resolution_t * mc_last = 0;
3338 p = mhash_get (&nm->mac_changes_by_address, address);
3340 return VNET_API_ERROR_NO_SUCH_ENTRY;
3344 while (index != (u32)~0)
3346 mc = pool_elt_at_index (nm->mac_changes, index);
3347 if (mc->node_index == node_index &&
3348 mc->type_opaque == type_opaque &&
3351 /* Clients may need to clean up pool entries, too */
3353 (*fp)(mc->data, 0 /* no new mac addrs */);
3356 mhash_unset (&nm->mac_changes_by_address, address, 0);
3357 if (mc->next_index != ~0)
3358 mhash_set (&nm->mac_changes_by_address, address,
3360 pool_put (nm->mac_changes, mc);
3366 mc_last->next_index = mc->next_index;
3367 pool_put (nm->mac_changes, mc);
3372 index = mc->next_index;
3375 return VNET_API_ERROR_NO_SUCH_ENTRY;
3379 int vnet_ip6_nd_term (vlib_main_t * vm,
3380 vlib_node_runtime_t * node,
3382 ethernet_header_t * eth,
3388 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3389 icmp6_neighbor_solicitation_or_advertisement_header_t * ndh;
3390 pending_resolution_t * mc;
3393 ndh = ip6_next_header (ip);
3394 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
3395 ndh->icmp.type != ICMP6_neighbor_advertisement)
3398 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
3399 (p0->flags & VLIB_BUFFER_IS_TRACED)))
3401 u8 *t0 = vlib_add_trace (vm, node, p0,
3402 sizeof (icmp6_input_trace_t));
3403 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
3406 /* Check if anyone want ND events for L2 BDs */
3407 p = mhash_get (&nm->mac_changes_by_address, &ip6a_zero);
3409 { /* Only SHG 0 interface which is more likely local */
3410 u32 next_index = p[0];
3411 while (next_index != (u32)~0)
3413 int (*fp)(u32, u8 *, u32, ip6_address_t *);
3415 mc = pool_elt_at_index (nm->mac_changes, next_index);
3416 fp = mc->data_callback;
3417 /* Call the callback, return 1 to suppress dup events */
3418 if (fp) rv = (*fp)(mc->data,
3422 /* Signal the resolver process */
3424 vlib_process_signal_event (vm, mc->node_index,
3427 next_index = mc->next_index;
3431 /* Check if MAC entry exsist for solicited target IP */
3432 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
3434 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * opt;
3435 l2_bridge_domain_t *bd_config;
3438 opt = (void *) (ndh + 1);
3439 if ((opt->header.type !=
3440 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
3441 (opt->header.n_data_u64s != 1))
3442 return 0; /* source link layer address option not present */
3444 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
3445 macp = (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
3447 { /* found ip-mac entry, generate eighbor advertisement response */
3449 vlib_node_runtime_t * error_node =
3450 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
3451 ip->dst_address = ip->src_address;
3452 ip->src_address = ndh->target_address;
3453 ip->hop_limit = 255;
3455 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
3456 clib_memcpy (opt->ethernet_address, macp, 6);
3457 ndh->icmp.type = ICMP6_neighbor_advertisement;
3458 ndh->advertisement_flags = clib_host_to_net_u32
3459 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
3460 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
3461 ndh->icmp.checksum = 0;
3462 ndh->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum(vm, p0, ip,
3464 clib_memcpy(eth->dst_address, eth->src_address, 6);
3465 clib_memcpy(eth->src_address, macp, 6);
3466 vlib_error_count (vm, error_node->node_index,
3467 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
3477 ethernet_ndp_change_mac (vlib_main_t * vm, u32 sw_if_index)
3479 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3483 pool_foreach (n, nm->neighbor_pool, ({
3484 if (n->key.sw_if_index == sw_if_index)
3486 if (ADJ_INDEX_INVALID != n->adj_index)
3488 adj_nbr_update_rewrite(n->adj_index,
3489 n->link_layer_address);