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;
433 * prevent a DoS attack from the data-plane that
434 * spams us with no-op updates to the MAC address
436 if (0 == memcmp(n->link_layer_address,
438 n_bytes_link_layer_address))
442 /* Update time stamp and ethernet address. */
443 clib_memcpy (n->link_layer_address,
445 n_bytes_link_layer_address);
447 n->cpu_time_last_updated = clib_cpu_time_now ();
449 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
451 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
453 ip6_nd_mk_complete(n);
455 /* Customer(s) waiting for this address to be resolved? */
456 p = mhash_get (&nm->pending_resolutions_by_address, a);
461 while (next_index != (u32)~0)
463 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
464 vlib_process_signal_event (vm, pr->node_index,
467 next_index = pr->next_index;
468 pool_put (nm->pending_resolutions, pr);
471 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
474 /* Customer(s) requesting ND event for this address? */
475 p = mhash_get (&nm->mac_changes_by_address, a);
480 while (next_index != (u32)~0)
482 int (*fp)(u32, u8 *, u32, ip6_address_t *);
484 mc = pool_elt_at_index (nm->mac_changes, next_index);
485 fp = mc->data_callback;
487 /* Call the user's data callback, return 1 to suppress dup events */
489 rv = (*fp)(mc->data, link_layer_address, sw_if_index, &ip6a_zero);
491 * Signal the resolver process, as long as the user
492 * says they want to be notified
495 vlib_process_signal_event (vm, mc->node_index,
498 next_index = mc->next_index;
502 vlib_worker_thread_barrier_release(vm);
507 ip6_nd_mk_incomplete (ip6_neighbor_t *nbr)
511 .fp_proto = FIB_PROTOCOL_IP6,
513 .ip6 = nbr->key.ip6_address,
520 fib_index = im->fib_index_by_sw_if_index[nbr->key.sw_if_index];
523 * revert the adj this ND entry sourced to incomplete
525 adj_nbr_update_rewrite(nbr->adj_index,
529 * remove the FIB entry the ND entry sourced
531 fib_table_entry_delete(fib_index, &pfx, FIB_SOURCE_ADJ);
534 * Unlock the adj now that the ARP entry is no longer a source
536 adj_unlock(nbr->adj_index);
537 nbr->adj_index = ADJ_INDEX_INVALID;
541 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
544 u8 * link_layer_address,
545 uword n_bytes_link_layer_address)
547 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
548 ip6_neighbor_key_t k;
554 if (os_get_cpu_number())
556 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
562 k.sw_if_index = sw_if_index;
563 k.ip6_address = a[0];
566 vlib_worker_thread_barrier_sync (vm);
568 p = mhash_get (&nm->neighbor_index_by_key, &k);
575 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
577 ip6_nd_mk_incomplete(n);
578 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
579 pool_put (nm->neighbor_pool, n);
582 vlib_worker_thread_barrier_release(vm);
587 static void ip6_neighbor_set_unset_rpc_callback
588 ( ip6_neighbor_set_unset_rpc_args_t * a)
590 vlib_main_t * vm = vlib_get_main();
592 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
593 a->link_layer_address, 6, a->is_static);
595 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
596 a->link_layer_address, 6);
601 ip6_neighbor_sort (void *a1, void *a2)
603 vnet_main_t * vnm = vnet_get_main();
604 ip6_neighbor_t * n1 = a1, * n2 = a2;
606 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
607 n2->key.sw_if_index);
609 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
613 static clib_error_t *
614 show_ip6_neighbors (vlib_main_t * vm,
615 unformat_input_t * input,
616 vlib_cli_command_t * cmd)
618 vnet_main_t * vnm = vnet_get_main();
619 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
620 ip6_neighbor_t * n, * ns;
621 clib_error_t * error = 0;
624 /* Filter entries by interface if given. */
626 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
629 pool_foreach (n, nm->neighbor_pool, ({ vec_add1 (ns, n[0]); }));
630 vec_sort_with_function (ns, ip6_neighbor_sort);
631 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
632 vec_foreach (n, ns) {
633 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
635 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
642 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
643 .path = "show ip6 neighbors",
644 .function = show_ip6_neighbors,
645 .short_help = "Show ip6 neighbors",
648 static clib_error_t *
649 set_ip6_neighbor (vlib_main_t * vm,
650 unformat_input_t * input,
651 vlib_cli_command_t * cmd)
653 vnet_main_t * vnm = vnet_get_main();
661 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
663 /* intfc, ip6-address, mac-address */
664 if (unformat (input, "%U %U %U",
665 unformat_vnet_sw_interface, vnm, &sw_if_index,
666 unformat_ip6_address, &addr,
667 unformat_ethernet_address, mac_address))
670 else if (unformat (input, "delete") || unformat (input, "del"))
672 else if (unformat (input, "static"))
679 return clib_error_return (0, "Missing interface, ip6 or hw address");
682 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
683 mac_address, sizeof(mac_address), is_static);
685 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
686 mac_address, sizeof(mac_address));
690 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) = {
691 .path = "set ip6 neighbor",
692 .function = set_ip6_neighbor,
693 .short_help = "set ip6 neighbor [del] <intfc> <ip6-address> <mac-address> [static]",
697 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
698 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
699 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
700 } icmp6_neighbor_solicitation_or_advertisement_next_t;
702 static_always_inline uword
703 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
704 vlib_node_runtime_t * node,
705 vlib_frame_t * frame,
706 uword is_solicitation)
708 vnet_main_t * vnm = vnet_get_main();
709 ip6_main_t * im = &ip6_main;
710 uword n_packets = frame->n_vectors;
711 u32 * from, * to_next;
712 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
713 icmp6_neighbor_discovery_option_type_t option_type;
714 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
717 from = vlib_frame_vector_args (frame);
718 n_left_from = n_packets;
719 next_index = node->cached_next_index;
721 if (node->flags & VLIB_NODE_FLAG_TRACE)
722 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
724 sizeof (icmp6_input_trace_t));
728 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
729 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
730 n_advertisements_sent = 0;
732 while (n_left_from > 0)
734 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
736 while (n_left_from > 0 && n_left_to_next > 0)
740 icmp6_neighbor_solicitation_or_advertisement_header_t * h0;
741 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * o0;
742 u32 bi0, options_len0, sw_if_index0, next0, error0;
743 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
747 bi0 = to_next[0] = from[0];
754 p0 = vlib_get_buffer (vm, bi0);
755 ip0 = vlib_buffer_get_current (p0);
756 h0 = ip6_next_header (ip0);
757 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
759 error0 = ICMP6_ERROR_NONE;
760 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
761 ip6_sadd_link_local = ip6_address_is_link_local_unicast(&ip0->src_address);
762 ip6_sadd_unspecified = ip6_address_is_unspecified (&ip0->src_address);
764 /* Check that source address is unspecified, link-local or else on-link. */
765 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
767 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
769 if (ADJ_INDEX_INVALID != src_adj_index0)
771 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, src_adj_index0);
773 /* Allow all realistic-looking rewrite adjacencies to pass */
774 ni0 = adj0->lookup_next_index;
775 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
776 (ni0 < IP6_LOOKUP_N_NEXT);
778 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
780 ? ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
785 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
789 o0 = (void *) (h0 + 1);
790 o0 = ((options_len0 == 8 && o0->header.type == option_type
791 && o0->header.n_data_u64s == 1) ? o0 : 0);
793 /* If src address unspecified or link local, donot learn neighbor MAC */
794 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
795 !ip6_sadd_unspecified && !ip6_sadd_link_local))
797 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
798 if (nm->limit_neighbor_cache_size &&
799 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
800 unset_random_neighbor_entry();
801 vnet_set_ip6_ethernet_neighbor (
803 is_solicitation ? &ip0->src_address : &h0->target_address,
804 o0->ethernet_address, sizeof (o0->ethernet_address), 0);
807 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
809 /* Check that target address is local to this router. */
810 fib_node_index_t fei;
813 fib_index = ip6_fib_table_get_index_for_sw_if_index(sw_if_index0);
817 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
821 fei = ip6_fib_table_lookup_exact_match(fib_index,
825 if (FIB_NODE_INDEX_INVALID == fei ||
826 !(FIB_ENTRY_FLAG_LOCAL &
827 fib_entry_get_flags_for_source(fei, FIB_SOURCE_INTERFACE)))
829 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
835 next0 = (error0 != ICMP6_ERROR_NONE
836 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
837 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
841 error0 = error0 == ICMP6_ERROR_NONE ?
842 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
845 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
847 vnet_sw_interface_t * sw_if0;
848 ethernet_interface_t * eth_if0;
849 ethernet_header_t *eth0;
851 /* dst address is either source address or the all-nodes mcast addr */
852 if(!ip6_sadd_unspecified)
853 ip0->dst_address = ip0->src_address;
855 ip6_set_reserved_multicast_address(&ip0->dst_address,
856 IP6_MULTICAST_SCOPE_link_local,
857 IP6_MULTICAST_GROUP_ID_all_hosts);
859 ip0->src_address = h0->target_address;
860 ip0->hop_limit = 255;
861 h0->icmp.type = ICMP6_neighbor_advertisement;
863 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
864 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
865 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
868 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
870 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
873 h0->advertisement_flags = clib_host_to_net_u32
874 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
875 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
877 h0->icmp.checksum = 0;
879 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
881 ASSERT(bogus_length == 0);
883 /* Reuse current MAC header, copy SMAC to DMAC and
884 * interface MAC to SMAC */
885 vlib_buffer_advance(p0, - ethernet_buffer_header_size(p0));
886 eth0 = vlib_buffer_get_current(p0);
887 clib_memcpy(eth0->dst_address, eth0->src_address, 6);
888 clib_memcpy(eth0->src_address, eth_if0->address, 6);
890 /* Setup input and output sw_if_index for packet */
891 ASSERT(vnet_buffer(p0)->sw_if_index[VLIB_RX] == sw_if_index0);
892 vnet_buffer(p0)->sw_if_index[VLIB_TX] = sw_if_index0;
893 vnet_buffer(p0)->sw_if_index[VLIB_RX] =
894 vnet_main.local_interface_sw_if_index;
896 n_advertisements_sent++;
899 p0->error = error_node->errors[error0];
901 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
902 to_next, n_left_to_next,
906 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
909 /* Account for advertisements sent. */
910 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, n_advertisements_sent);
912 return frame->n_vectors;
915 /* for "syslogging" - use elog for now */
916 #define foreach_log_level \
918 _ (INFO, "INFORMATION") \
919 _ (NOTICE, "NOTICE") \
920 _ (WARNING, "WARNING") \
922 _ (CRIT, "CRITICAL") \
924 _ (EMERG, "EMERGENCY")
927 #define _(f,s) LOG_##f,
932 static char * log_level_strings[] = {
938 static int logmask = 1 << LOG_DEBUG;
941 ip6_neighbor_syslog(vlib_main_t *vm, int priority, char * fmt, ...)
943 /* just use elog for now */
947 if( (priority > LOG_EMERG) ||
948 !(logmask & (1 << priority)))
954 what = va_format (0, fmt, &va);
956 ELOG_TYPE_DECLARE (e) = {
957 .format = "ip6 nd: (%s): %s",
958 .format_args = "T4T4",
960 struct { u32 s[2]; } * ed;
961 ed = ELOG_DATA (&vm->elog_main, e);
962 ed->s[0] = elog_string(&vm->elog_main, log_level_strings[priority]);
963 ed->s[1] = elog_string(&vm->elog_main, (char *)what);
969 /* ipv6 neighbor discovery - router advertisements */
971 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
972 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
973 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
974 ICMP6_ROUTER_SOLICITATION_N_NEXT,
975 } icmp6_router_solicitation_or_advertisement_next_t;
977 static_always_inline uword
978 icmp6_router_solicitation(vlib_main_t * vm,
979 vlib_node_runtime_t * node,
980 vlib_frame_t * frame)
982 vnet_main_t * vnm = vnet_get_main();
983 ip6_main_t * im = &ip6_main;
984 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
985 uword n_packets = frame->n_vectors;
986 u32 * from, * to_next;
987 u32 n_left_from, n_left_to_next, next_index;
988 u32 n_advertisements_sent = 0;
991 icmp6_neighbor_discovery_option_type_t option_type;
993 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
995 from = vlib_frame_vector_args (frame);
996 n_left_from = n_packets;
997 next_index = node->cached_next_index;
999 if (node->flags & VLIB_NODE_FLAG_TRACE)
1000 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1002 sizeof (icmp6_input_trace_t));
1004 /* source may append his LL address */
1005 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1007 while (n_left_from > 0)
1009 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1011 while (n_left_from > 0 && n_left_to_next > 0)
1015 ip6_radv_t *radv_info = 0;
1017 icmp6_neighbor_discovery_header_t * h0;
1018 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * o0;
1020 u32 bi0, options_len0, sw_if_index0, next0, error0;
1021 u32 is_solicitation = 1, is_dropped = 0;
1022 u32 is_unspecified, is_link_local;
1024 bi0 = to_next[0] = from[0];
1029 n_left_to_next -= 1;
1031 p0 = vlib_get_buffer (vm, bi0);
1032 ip0 = vlib_buffer_get_current (p0);
1033 h0 = ip6_next_header (ip0);
1034 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1035 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1036 is_link_local = ip6_address_is_link_local_unicast (&ip0->src_address);
1038 error0 = ICMP6_ERROR_NONE;
1039 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1041 /* check if solicitation (not from nd_timer node) */
1042 if (ip6_address_is_unspecified (&ip0->dst_address))
1043 is_solicitation = 0;
1045 /* Check that source address is unspecified, link-local or else on-link. */
1046 if (!is_unspecified && !is_link_local)
1048 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1050 if (ADJ_INDEX_INVALID != src_adj_index0)
1052 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main,
1055 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1056 ? ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1061 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1065 /* check for source LL option and process */
1066 o0 = (void *) (h0 + 1);
1067 o0 = ((options_len0 == 8
1068 && o0->header.type == option_type
1069 && o0->header.n_data_u64s == 1)
1073 /* if src address unspecified IGNORE any options */
1074 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1075 !is_unspecified && !is_link_local)) {
1076 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1077 if (nm->limit_neighbor_cache_size &&
1078 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
1079 unset_random_neighbor_entry();
1081 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1083 o0->ethernet_address,
1084 sizeof (o0->ethernet_address), 0);
1087 /* default is to drop */
1088 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1090 if (error0 == ICMP6_ERROR_NONE)
1092 vnet_sw_interface_t * sw_if0;
1093 ethernet_interface_t * eth_if0;
1096 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1097 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1098 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1100 /* only support ethernet interface type for now */
1101 error0 = (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF : error0;
1103 if (error0 == ICMP6_ERROR_NONE)
1107 /* adjust the sizeof the buffer to just include the ipv6 header */
1108 p0->current_length -= (options_len0 + sizeof(icmp6_neighbor_discovery_header_t));
1110 /* look up the radv_t information for this interface */
1111 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1113 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1116 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1118 error0 = ((!radv_info) ? ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : error0);
1120 if (error0 == ICMP6_ERROR_NONE)
1122 f64 now = vlib_time_now (vm);
1124 /* for solicited adverts - need to rate limit */
1127 if( (now - radv_info->last_radv_time) < MIN_DELAY_BETWEEN_RAS )
1130 radv_info->last_radv_time = now;
1134 icmp6_router_advertisement_header_t rh;
1136 rh.icmp.type = ICMP6_router_advertisement;
1138 rh.icmp.checksum = 0;
1140 rh.current_hop_limit = radv_info->curr_hop_limit;
1141 rh.router_lifetime_in_sec = clib_host_to_net_u16(radv_info->adv_router_lifetime_in_sec);
1142 rh.time_in_msec_between_retransmitted_neighbor_solicitations =
1143 clib_host_to_net_u32(radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1144 rh.neighbor_reachable_time_in_msec =
1145 clib_host_to_net_u32(radv_info->adv_neighbor_reachable_time_in_msec);
1147 rh.flags = (radv_info->adv_managed_flag) ? ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP : 0;
1148 rh.flags |= ( (radv_info->adv_other_flag) ? ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP : 0);
1151 u16 payload_length = sizeof(icmp6_router_advertisement_header_t);
1153 vlib_buffer_add_data (vm,
1154 p0->free_list_index,
1156 (void *)&rh, sizeof(icmp6_router_advertisement_header_t));
1158 if(radv_info->adv_link_layer_address)
1160 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t h;
1162 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1163 h.header.n_data_u64s = 1;
1165 /* copy ll address */
1166 clib_memcpy(&h.ethernet_address[0], eth_if0->address, 6);
1168 vlib_buffer_add_data (vm,
1169 p0->free_list_index,
1171 (void *)&h, sizeof(icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
1173 payload_length += sizeof(icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1176 /* add MTU option */
1177 if(radv_info->adv_link_mtu)
1179 icmp6_neighbor_discovery_mtu_option_t h;
1182 h.mtu = clib_host_to_net_u32(radv_info->adv_link_mtu);
1183 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1184 h.header.n_data_u64s = 1;
1186 payload_length += sizeof( icmp6_neighbor_discovery_mtu_option_t);
1188 vlib_buffer_add_data (vm,
1189 p0->free_list_index,
1191 (void *)&h, sizeof(icmp6_neighbor_discovery_mtu_option_t));
1194 /* add advertised prefix options */
1195 ip6_radv_prefix_t *pr_info;
1197 pool_foreach (pr_info, radv_info->adv_prefixes_pool, ({
1199 if(pr_info->enabled &&
1200 (!pr_info->decrement_lifetime_flag || (pr_info->pref_lifetime_expires >0)))
1202 /* advertise this prefix */
1203 icmp6_neighbor_discovery_prefix_information_option_t h;
1205 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1206 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1208 h.dst_address_length = pr_info->prefix_len;
1210 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1211 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
1213 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1215 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1216 h.preferred_time = 0;
1220 if(pr_info->decrement_lifetime_flag)
1222 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1223 (pr_info->valid_lifetime_expires - now) : 0;
1225 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1226 (pr_info->pref_lifetime_expires - now) : 0;
1229 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1230 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1234 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1236 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1238 vlib_buffer_add_data (vm,
1239 p0->free_list_index,
1241 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1246 /* add additional options before here */
1248 /* finish building the router advertisement... */
1249 if(!is_unspecified && radv_info->send_unicast)
1251 ip0->dst_address = ip0->src_address;
1255 /* target address is all-nodes mcast addr */
1256 ip6_set_reserved_multicast_address(&ip0->dst_address,
1257 IP6_MULTICAST_SCOPE_link_local,
1258 IP6_MULTICAST_GROUP_ID_all_hosts);
1261 /* source address MUST be the link-local address */
1262 ip0->src_address = radv_info->link_local_address;
1264 ip0->hop_limit = 255;
1265 ip0->payload_length = clib_host_to_net_u16 (payload_length);
1267 icmp6_router_advertisement_header_t * rh0 = (icmp6_router_advertisement_header_t *)(ip0 + 1);
1268 rh0->icmp.checksum =
1269 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1271 ASSERT(bogus_length == 0);
1273 /* setup output if and adjacency */
1274 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1275 vnet_main.local_interface_sw_if_index;
1277 if (is_solicitation)
1279 ethernet_header_t *eth0;
1280 /* Reuse current MAC header, copy SMAC to DMAC and
1281 * interface MAC to SMAC */
1282 vlib_buffer_reset (p0);
1283 eth0 = vlib_buffer_get_current(p0);
1284 clib_memcpy(eth0->dst_address, eth0->src_address, 6);
1285 clib_memcpy(eth0->src_address, eth_if0->address, 6);
1286 next0 = is_dropped ?
1287 next0 : ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1288 vnet_buffer(p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1292 adj_index0 = radv_info->all_nodes_adj_index;
1293 if (adj_index0 == 0)
1294 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1297 ip_adjacency_t * adj0 = ip_get_adjacency (&im->lookup_main, adj_index0);
1299 ((adj0->rewrite_header.sw_if_index != sw_if_index0
1300 || adj0->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
1301 ? ICMP6_ERROR_ROUTER_SOLICITATION_DEST_UNKNOWN
1303 next0 = is_dropped ?
1304 next0 : ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1305 vnet_buffer (p0)->ip.adj_index[VLIB_RX] = adj_index0;
1309 radv_info->n_solicitations_dropped += is_dropped;
1310 radv_info->n_solicitations_rcvd += is_solicitation;
1312 if((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1314 radv_info->n_advertisements_sent++;
1315 n_advertisements_sent++;
1321 p0->error = error_node->errors[error0];
1323 if(error0 != ICMP6_ERROR_NONE)
1324 vlib_error_count (vm, error_node->node_index, error0, 1);
1326 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1327 to_next, n_left_to_next,
1332 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1335 /* Account for router advertisements sent. */
1336 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX, n_advertisements_sent);
1338 return frame->n_vectors;
1341 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1342 static_always_inline uword
1343 icmp6_router_advertisement(vlib_main_t * vm,
1344 vlib_node_runtime_t * node,
1345 vlib_frame_t * frame)
1347 vnet_main_t * vnm = vnet_get_main();
1348 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1349 uword n_packets = frame->n_vectors;
1350 u32 * from, * to_next;
1351 u32 n_left_from, n_left_to_next, next_index;
1352 u32 n_advertisements_rcvd = 0;
1354 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1356 from = vlib_frame_vector_args (frame);
1357 n_left_from = n_packets;
1358 next_index = node->cached_next_index;
1360 if (node->flags & VLIB_NODE_FLAG_TRACE)
1361 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1363 sizeof (icmp6_input_trace_t));
1365 while (n_left_from > 0)
1367 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1369 while (n_left_from > 0 && n_left_to_next > 0)
1373 ip6_radv_t *radv_info = 0;
1374 icmp6_router_advertisement_header_t * h0;
1375 u32 bi0, options_len0, sw_if_index0, next0, error0;
1377 bi0 = to_next[0] = from[0];
1382 n_left_to_next -= 1;
1384 p0 = vlib_get_buffer (vm, bi0);
1385 ip0 = vlib_buffer_get_current (p0);
1386 h0 = ip6_next_header (ip0);
1387 options_len0 = clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1389 error0 = ICMP6_ERROR_NONE;
1390 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1392 /* Check that source address is link-local*/
1393 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1394 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1396 /* default is to drop */
1397 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1399 n_advertisements_rcvd++;
1401 if (error0 == ICMP6_ERROR_NONE)
1403 vnet_sw_interface_t * sw_if0;
1404 ethernet_interface_t * eth_if0;
1406 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1407 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1408 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1410 /* only support ethernet interface type for now */
1411 error0 = (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF : error0;
1413 if (error0 == ICMP6_ERROR_NONE)
1417 /* look up the radv_t information for this interface */
1418 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1420 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1423 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1425 error0 = ((!radv_info) ? ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : error0);
1427 if (error0 == ICMP6_ERROR_NONE)
1429 /* validate advertised information */
1430 if((h0->current_hop_limit && radv_info->curr_hop_limit) &&
1431 (h0->current_hop_limit != radv_info->curr_hop_limit))
1433 ip6_neighbor_syslog(vm, LOG_WARNING,
1434 "our AdvCurHopLimit on %U doesn't agree with %U",
1435 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1438 if((h0->flags & ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP) !=
1439 radv_info->adv_managed_flag)
1441 ip6_neighbor_syslog(vm, LOG_WARNING,
1442 "our AdvManagedFlag on %U doesn't agree with %U",
1443 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1446 if((h0->flags & ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP) !=
1447 radv_info->adv_other_flag)
1449 ip6_neighbor_syslog(vm, LOG_WARNING,
1450 "our AdvOtherConfigFlag on %U doesn't agree with %U",
1451 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1454 if((h0->time_in_msec_between_retransmitted_neighbor_solicitations &&
1455 radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations) &&
1456 (h0->time_in_msec_between_retransmitted_neighbor_solicitations !=
1457 clib_host_to_net_u32(radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
1459 ip6_neighbor_syslog(vm, LOG_WARNING,
1460 "our AdvRetransTimer on %U doesn't agree with %U",
1461 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1464 if((h0->neighbor_reachable_time_in_msec &&
1465 radv_info->adv_neighbor_reachable_time_in_msec) &&
1466 (h0->neighbor_reachable_time_in_msec !=
1467 clib_host_to_net_u32(radv_info->adv_neighbor_reachable_time_in_msec)))
1469 ip6_neighbor_syslog(vm, LOG_WARNING,
1470 "our AdvReachableTime on %U doesn't agree with %U",
1471 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1474 /* check for MTU or prefix options or .. */
1475 u8 * opt_hdr = (u8 *)(h0 + 1);
1476 while( options_len0 > 0)
1478 icmp6_neighbor_discovery_option_header_t *o0 = ( icmp6_neighbor_discovery_option_header_t *)opt_hdr;
1479 int opt_len = o0->n_data_u64s << 3;
1480 icmp6_neighbor_discovery_option_type_t option_type = o0->type;
1482 if(options_len0 < 2)
1484 ip6_neighbor_syslog(vm, LOG_ERR,
1485 "malformed RA packet on %U from %U",
1486 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1492 ip6_neighbor_syslog(vm, LOG_ERR,
1493 " zero length option in RA on %U from %U",
1494 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1497 else if( opt_len > options_len0)
1499 ip6_neighbor_syslog(vm, LOG_ERR,
1500 "option length in RA packet greater than total length on %U from %U",
1501 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1505 options_len0 -= opt_len;
1510 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
1512 icmp6_neighbor_discovery_mtu_option_t *h =
1513 (icmp6_neighbor_discovery_mtu_option_t *)(o0);
1515 if(opt_len < sizeof(*h))
1518 if((h->mtu && radv_info->adv_link_mtu) &&
1519 (h->mtu != clib_host_to_net_u32(radv_info->adv_link_mtu)))
1521 ip6_neighbor_syslog(vm, LOG_WARNING,
1522 "our AdvLinkMTU on %U doesn't agree with %U",
1523 format_vnet_sw_if_index_name, vnm, sw_if_index0, format_ip6_address, &ip0->src_address);
1528 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1530 icmp6_neighbor_discovery_prefix_information_option_t *h =
1531 (icmp6_neighbor_discovery_prefix_information_option_t *)(o0);
1533 /* validate advertised prefix options */
1534 ip6_radv_prefix_t *pr_info;
1535 u32 preferred, valid;
1537 if(opt_len < sizeof(*h))
1540 preferred = clib_net_to_host_u32(h->preferred_time);
1541 valid = clib_net_to_host_u32(h->valid_time);
1543 /* look for matching prefix - if we our advertising it, it better be consistant */
1544 pool_foreach (pr_info, radv_info->adv_prefixes_pool, ({
1547 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1549 if(pr_info->enabled &&
1550 (pr_info->prefix_len == h->dst_address_length) &&
1551 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1554 if(!pr_info->decrement_lifetime_flag &&
1555 valid != pr_info->adv_valid_lifetime_in_secs)
1557 ip6_neighbor_syslog(vm, LOG_WARNING,
1558 "our ADV validlifetime on %U for %U does not agree with %U",
1559 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1560 format_ip6_address, &h->dst_address);
1562 if(!pr_info->decrement_lifetime_flag &&
1563 preferred != pr_info->adv_pref_lifetime_in_secs)
1565 ip6_neighbor_syslog(vm, LOG_WARNING,
1566 "our ADV preferredlifetime on %U for %U does not agree with %U",
1567 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1568 format_ip6_address, &h->dst_address);
1584 p0->error = error_node->errors[error0];
1586 if(error0 != ICMP6_ERROR_NONE)
1587 vlib_error_count (vm, error_node->node_index, error0, 1);
1589 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1590 to_next, n_left_to_next,
1594 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1597 /* Account for router advertisements sent. */
1598 vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX, n_advertisements_rcvd);
1600 return frame->n_vectors;
1603 /* create and initialize router advertisement parameters with default values for this intfc */
1605 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
1609 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1612 vnet_sw_interface_t * sw_if0;
1613 ethernet_interface_t * eth_if0 = 0;
1615 /* lookup radv container - ethernet interfaces only */
1616 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1617 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1618 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1623 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
1624 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1628 a = pool_elt_at_index (nm->if_radv_pool, ri);
1632 u32 i, * to_delete = 0;
1633 ip6_radv_prefix_t *p;
1634 ip6_mldp_group_t *m;
1636 /* remove adjacencies */
1637 adj_unlock(a->all_nodes_adj_index);
1638 adj_unlock(a->all_routers_adj_index);
1639 adj_unlock(a->all_mldv2_routers_adj_index);
1641 /* clean up prefix_pool */
1642 pool_foreach (p, a->adv_prefixes_pool, ({
1643 vec_add1 (to_delete, p - a->adv_prefixes_pool);
1646 for (i = 0; i < vec_len (to_delete); i++)
1648 p = pool_elt_at_index (a->adv_prefixes_pool, to_delete[i]);
1649 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
1650 pool_put (a->adv_prefixes_pool, p);
1653 vec_free (to_delete);
1656 /* clean up mldp group pool */
1657 pool_foreach (m, a->mldp_group_pool, ({
1658 vec_add1 (to_delete, m - a->mldp_group_pool);
1661 for (i = 0; i < vec_len (to_delete); i++)
1663 m = pool_elt_at_index (a->mldp_group_pool, to_delete[i]);
1664 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
1665 pool_put (a->mldp_group_pool, m);
1668 vec_free (to_delete);
1670 pool_put (nm->if_radv_pool, a);
1671 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
1673 ip6_sw_interface_enable_disable(sw_if_index, 0);
1680 vnet_hw_interface_t * hw_if0;
1682 ip6_sw_interface_enable_disable(sw_if_index, 1);
1683 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
1685 pool_get (nm->if_radv_pool, a);
1687 ri = a - nm->if_radv_pool;
1688 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
1690 /* initialize default values (most of which are zero) */
1691 memset (a, 0, sizeof (a[0]));
1693 a->sw_if_index = sw_if_index;
1695 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
1696 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
1697 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
1698 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
1700 a->adv_link_layer_address = 1; /* send ll address source address option */
1702 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
1703 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
1704 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
1705 a->seed = random_default_seed();
1707 /* for generating random interface ids */
1708 a->randomizer = 0x1119194911191949;
1709 a->randomizer = random_u64 ((u32 *)&a->randomizer);
1711 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS ;
1712 a->initial_adverts_sent = a->initial_adverts_count-1;
1713 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1715 /* deafult is to send */
1718 /* fill in radv_info for this interface that will be needed later */
1719 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
1721 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
1723 /* fill in default link-local address (this may be overridden) */
1724 ip6_link_local_address_from_ethernet_address (&a->link_local_address, eth_if0->address);
1725 a->link_local_prefix_len = 64;
1727 mhash_init (&a->address_to_prefix_index, sizeof (uword), sizeof (ip6_address_t));
1728 mhash_init (&a->address_to_mldp_index, sizeof (uword), sizeof (ip6_address_t));
1731 u8 link_layer_address[6] =
1732 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_all_hosts};
1734 a->all_nodes_adj_index = adj_rewrite_add_and_lock(FIB_PROTOCOL_IP6,
1737 link_layer_address);
1741 u8 link_layer_address[6] =
1742 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_all_routers};
1744 a->all_routers_adj_index = adj_rewrite_add_and_lock(FIB_PROTOCOL_IP6,
1747 link_layer_address);
1751 u8 link_layer_address[6] =
1752 {0x33, 0x33, 0x00, 0x00, 0x00, IP6_MULTICAST_GROUP_ID_mldv2_routers};
1754 a->all_mldv2_routers_adj_index =
1755 adj_rewrite_add_and_lock(FIB_PROTOCOL_IP6,
1758 link_layer_address);
1761 /* add multicast groups we will always be reporting */
1763 ip6_mldp_group_t *mcast_group_info;
1765 ip6_set_reserved_multicast_address (&addr,
1766 IP6_MULTICAST_SCOPE_link_local,
1767 IP6_MULTICAST_GROUP_ID_all_hosts);
1769 /* lookup mldp info for this interface */
1771 uword * p = mhash_get (&a->address_to_mldp_index, &addr);
1772 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1775 if(!mcast_group_info)
1779 pool_get (a->mldp_group_pool, mcast_group_info);
1781 mi = mcast_group_info - a->mldp_group_pool;
1782 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1784 mcast_group_info->type = 4;
1785 mcast_group_info->mcast_source_address_pool = 0;
1786 mcast_group_info->num_sources = 0;
1787 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1790 ip6_set_reserved_multicast_address (&addr,
1791 IP6_MULTICAST_SCOPE_link_local,
1792 IP6_MULTICAST_GROUP_ID_all_routers);
1794 p = mhash_get (&a->address_to_mldp_index, &addr);
1795 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1797 if(!mcast_group_info)
1801 pool_get (a->mldp_group_pool, mcast_group_info);
1803 mi = mcast_group_info - a->mldp_group_pool;
1804 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1806 mcast_group_info->type = 4;
1807 mcast_group_info->mcast_source_address_pool = 0;
1808 mcast_group_info->num_sources = 0;
1809 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1812 ip6_set_reserved_multicast_address (&addr,
1813 IP6_MULTICAST_SCOPE_link_local,
1814 IP6_MULTICAST_GROUP_ID_mldv2_routers);
1816 p = mhash_get (&a->address_to_mldp_index, &addr);
1817 mcast_group_info = p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
1819 if(!mcast_group_info)
1823 pool_get (a->mldp_group_pool, mcast_group_info);
1825 mi = mcast_group_info - a->mldp_group_pool;
1826 mhash_set (&a->address_to_mldp_index, &addr, mi, /* old_value */ 0);
1828 mcast_group_info->type = 4;
1829 mcast_group_info->mcast_source_address_pool = 0;
1830 mcast_group_info->num_sources = 0;
1831 clib_memcpy(&mcast_group_info->mcast_address, &addr, sizeof(ip6_address_t));
1838 /* send an mldpv2 report */
1840 ip6_neighbor_send_mldpv2_report(u32 sw_if_index)
1842 vnet_main_t * vnm = vnet_get_main();
1843 vlib_main_t * vm = vnm->vlib_main;
1844 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
1845 vnet_sw_interface_t * sw_if0;
1846 ethernet_interface_t * eth_if0;
1850 ip6_radv_t *radv_info;
1860 icmp6_multicast_listener_report_header_t *rh0;
1861 icmp6_multicast_listener_report_packet_t *rp0;
1863 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1864 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1865 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1867 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
1870 /* look up the radv_t information for this interface */
1871 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
1873 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1878 /* send report now - build a mldpv2 report packet */
1879 n_allocated = vlib_buffer_alloc_from_free_list(vm,
1882 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1883 if (PREDICT_FALSE(n_allocated == 0))
1885 clib_warning ("buffer allocation failure");
1889 b0 = vlib_get_buffer (vm, bo0);
1891 /* adjust the sizeof the buffer to just include the ipv6 header */
1892 b0->current_length = sizeof(icmp6_multicast_listener_report_packet_t);
1894 payload_length = sizeof(icmp6_multicast_listener_report_header_t);
1896 b0->error = ICMP6_ERROR_NONE;
1898 rp0 = vlib_buffer_get_current (b0);
1899 ip0 = (ip6_header_t *)&rp0-> ip;
1900 rh0 = (icmp6_multicast_listener_report_header_t *)&rp0-> report_hdr;
1902 memset (rp0 , 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
1904 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
1906 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
1907 /* for DEBUG - vnet driver won't seem to emit router alerts */
1908 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
1911 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
1913 /* source address MUST be the link-local address */
1914 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1915 ip0->src_address = radv_info->link_local_address;
1917 /* destination is all mldpv2 routers */
1918 ip6_set_reserved_multicast_address(&ip0->dst_address,
1919 IP6_MULTICAST_SCOPE_link_local,
1920 IP6_MULTICAST_GROUP_ID_mldv2_routers);
1922 /* add reports here */
1923 ip6_mldp_group_t *m;
1924 int num_addr_records = 0;
1925 icmp6_multicast_address_record_t rr;
1927 /* fill in the hop-by-hop extension header (router alert) info */
1928 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
1929 rh0->ext_hdr.n_data_u64s = 0;
1931 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
1933 rh0->alert.value = 0;
1938 rh0->icmp.checksum = 0;
1940 pool_foreach (m, radv_info->mldp_group_pool, ({
1943 rr.aux_data_len_u32s = 0;
1944 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
1945 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
1949 vlib_buffer_add_data (vm,
1950 b0->free_list_index,
1952 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
1954 payload_length += sizeof( icmp6_multicast_address_record_t);
1958 rh0->num_addr_records = clib_host_to_net_u16(num_addr_records);
1960 /* update lengths */
1961 ip0->payload_length = clib_host_to_net_u16 (payload_length);
1963 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
1965 ASSERT(bogus_length == 0);
1968 * OK to override w/ no regard for actual FIB, because
1969 * ip6-rewrite-local only looks at the adjacency.
1971 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
1972 vnet_main.local_interface_sw_if_index;
1974 vnet_buffer (b0)->ip.adj_index[VLIB_RX] =
1975 radv_info->all_mldv2_routers_adj_index;
1977 vlib_node_t * node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-local");
1979 f = vlib_get_frame_to_node (vm, node->index);
1980 to_next = vlib_frame_vector_args (f);
1984 vlib_put_frame_to_node (vm, node->index, f);
1988 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) = {
1989 .function = icmp6_router_solicitation,
1990 .name = "icmp6-router-solicitation",
1992 .vector_size = sizeof (u32),
1994 .format_trace = format_icmp6_input_trace,
1996 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
1998 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "error-drop",
1999 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-local",
2000 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2004 /* send a RA or update the timer info etc.. */
2006 ip6_neighbor_process_timer_event (vlib_main_t * vm,
2007 vlib_node_runtime_t * node,
2008 vlib_frame_t * frame)
2010 vnet_main_t * vnm = vnet_get_main();
2011 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2012 ip6_radv_t *radv_info;
2013 vlib_frame_t * f = 0;
2014 u32 n_this_frame = 0;
2015 u32 n_left_to_next = 0;
2018 icmp6_router_solicitation_header_t * h0;
2020 f64 now = vlib_time_now (vm);
2022 /* Interface ip6 radv info list */
2023 pool_foreach (radv_info, nm->if_radv_pool, ({
2025 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2027 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2028 radv_info->next_multicast_time = now;
2029 radv_info->last_multicast_time = now;
2030 radv_info->last_radv_time = 0;
2031 radv_info->all_routers_mcast = 0;
2035 /* Make sure that we've joined the all-routers multicast group */
2036 if(!radv_info->all_routers_mcast)
2038 /* send MDLP_REPORT_EVENT message */
2039 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2040 radv_info->all_routers_mcast = 1;
2043 /* is it time to send a multicast RA on this interface? */
2044 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2049 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2050 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2052 /* multicast send - compute next multicast send time */
2053 if( radv_info->initial_adverts_sent > 0)
2055 radv_info->initial_adverts_sent--;
2056 if(rfn > radv_info-> initial_adverts_interval)
2057 rfn = radv_info-> initial_adverts_interval;
2059 /* check to see if we are ceasing to send */
2060 if( radv_info->initial_adverts_sent == 0)
2061 if(radv_info->cease_radv)
2062 radv_info->send_radv = 0;
2065 radv_info->next_multicast_time = rfn + now;
2066 radv_info->last_multicast_time = now;
2068 /* send advert now - build a "solicted" router advert with unspecified source address */
2069 n_allocated = vlib_buffer_alloc_from_free_list(vm,
2072 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2074 if (PREDICT_FALSE(n_allocated == 0))
2076 clib_warning ("buffer allocation failure");
2079 b0 = vlib_get_buffer (vm, bo0);
2080 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2081 b0->error = ICMP6_ERROR_NONE;
2082 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2084 h0 = vlib_buffer_get_current (b0);
2086 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2088 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2089 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2090 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2091 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2092 h0->ip.hop_limit = 255;
2094 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2095 h0->ip.src_address.as_u64[0] = 0;
2096 h0->ip.src_address.as_u64[1] = 0;
2098 h0->ip.dst_address.as_u64[0] = 0;
2099 h0->ip.dst_address.as_u64[1] = 0;
2101 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2103 if (PREDICT_FALSE(f == 0))
2105 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2106 to_next = vlib_frame_vector_args (f);
2107 n_left_to_next = VLIB_FRAME_SIZE;
2116 if (PREDICT_FALSE(n_left_to_next == 0))
2118 f->n_vectors = n_this_frame;
2119 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2127 ASSERT(n_this_frame);
2128 f->n_vectors = n_this_frame;
2129 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2135 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2136 vlib_node_runtime_t * node,
2137 vlib_frame_t * frame)
2140 ip6_icmp_neighbor_discovery_event_data_t * event_data;
2142 /* init code here */
2146 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */);
2148 event_data = vlib_process_get_event_data (vm, &event_type);
2152 /* No events found: timer expired. */
2153 /* process interface list and send RAs as appropriate, update timer info */
2154 ip6_neighbor_process_timer_event (vm, node, frame);
2158 switch (event_type) {
2160 case ICMP6_ND_EVENT_INIT:
2171 _vec_len (event_data) = 0;
2174 return frame->n_vectors;
2177 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) = {
2178 .function = icmp6_router_advertisement,
2179 .name = "icmp6-router-advertisement",
2181 .vector_size = sizeof (u32),
2183 .format_trace = format_icmp6_input_trace,
2191 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2193 .function = ip6_icmp_neighbor_discovery_event_process,
2194 .name = "ip6-icmp-neighbor-discovery-event-process",
2195 .type = VLIB_NODE_TYPE_PROCESS,
2199 icmp6_neighbor_solicitation (vlib_main_t * vm,
2200 vlib_node_runtime_t * node,
2201 vlib_frame_t * frame)
2202 { return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, /* is_solicitation */ 1); }
2205 icmp6_neighbor_advertisement (vlib_main_t * vm,
2206 vlib_node_runtime_t * node,
2207 vlib_frame_t * frame)
2208 { return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, /* is_solicitation */ 0); }
2210 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) = {
2211 .function = icmp6_neighbor_solicitation,
2212 .name = "icmp6-neighbor-solicitation",
2214 .vector_size = sizeof (u32),
2216 .format_trace = format_icmp6_input_trace,
2218 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2220 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "error-drop",
2221 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2225 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) = {
2226 .function = icmp6_neighbor_advertisement,
2227 .name = "icmp6-neighbor-advertisement",
2229 .vector_size = sizeof (u32),
2231 .format_trace = format_icmp6_input_trace,
2239 /* API support functions */
2241 ip6_neighbor_ra_config(vlib_main_t * vm, u32 sw_if_index,
2242 u8 suppress, u8 managed, u8 other,
2243 u8 ll_option, u8 send_unicast, u8 cease,
2244 u8 use_lifetime, u32 lifetime,
2245 u32 initial_count, u32 initial_interval,
2246 u32 max_interval, u32 min_interval,
2249 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2253 /* look up the radv_t information for this interface */
2254 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2255 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2256 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2261 ip6_radv_t * radv_info;
2262 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2264 if((max_interval != 0) && (min_interval ==0))
2265 min_interval = .75 * max_interval;
2267 max_interval = (max_interval != 0) ? ( (is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) : radv_info->max_radv_interval;
2268 min_interval = (min_interval != 0) ? ( (is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) : radv_info->min_radv_interval;
2269 lifetime = (use_lifetime != 0) ? ( (is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) : radv_info->adv_router_lifetime_in_sec;
2273 if(lifetime > MAX_DEF_RTR_LIFETIME)
2274 lifetime = MAX_DEF_RTR_LIFETIME;
2276 if(lifetime <= max_interval)
2277 return VNET_API_ERROR_INVALID_VALUE;
2280 if(min_interval != 0)
2282 if((min_interval > .75 * max_interval) ||
2284 return VNET_API_ERROR_INVALID_VALUE;
2287 if((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2288 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2289 return VNET_API_ERROR_INVALID_VALUE;
2292 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2293 if "flag" is clear don't change corresponding value
2295 radv_info->send_radv = (suppress != 0) ? ( (is_no != 0) ? 1 : 0 ) : radv_info->send_radv;
2296 radv_info->adv_managed_flag = ( managed != 0) ? ( (is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2297 radv_info->adv_other_flag = (other != 0) ? ( (is_no) ? 0: 1) : radv_info->adv_other_flag;
2298 radv_info->adv_link_layer_address = ( ll_option != 0) ? ( (is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2299 radv_info->send_unicast = (send_unicast != 0) ? ( (is_no) ? 0 : 1) : radv_info->send_unicast;
2300 radv_info->cease_radv = ( cease != 0) ? ( (is_no) ? 0 : 1) : radv_info->cease_radv;
2302 radv_info->min_radv_interval = min_interval;
2303 radv_info->max_radv_interval = max_interval;
2304 radv_info->adv_router_lifetime_in_sec = lifetime;
2306 radv_info->initial_adverts_count =
2307 (initial_count != 0) ? ( (is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) : radv_info->initial_adverts_count ;
2308 radv_info->initial_adverts_interval =
2309 (initial_interval != 0) ? ( (is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) : radv_info->initial_adverts_interval;
2312 if((cease != 0) && (is_no))
2313 radv_info-> send_radv = 1;
2315 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2316 radv_info->next_multicast_time = vlib_time_now (vm);
2317 radv_info->last_multicast_time = vlib_time_now (vm);
2318 radv_info->last_radv_time = 0;
2324 ip6_neighbor_ra_prefix(vlib_main_t * vm, u32 sw_if_index,
2325 ip6_address_t *prefix_addr, u8 prefix_len,
2326 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2327 u8 no_advertise, u8 off_link, u8 no_autoconfig, u8 no_onlink,
2330 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2335 /* look up the radv_t information for this interface */
2336 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2338 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2340 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2344 f64 now = vlib_time_now (vm);
2345 ip6_radv_t * radv_info;
2346 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2348 /* prefix info add, delete or update */
2349 ip6_radv_prefix_t * prefix;
2351 /* lookup prefix info for this address on this interface */
2352 uword * p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2354 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2360 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
2362 if(prefix->prefix_len != prefix_len)
2363 return VNET_API_ERROR_INVALID_VALUE_2;
2365 /* FIXME - Should the DP do this or the CP ?*/
2366 /* do specific delete processing here before returning */
2367 /* try to remove from routing table */
2369 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,/* old_value */ 0);
2370 pool_put (radv_info->adv_prefixes_pool, prefix);
2372 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2373 radv_info->next_multicast_time = vlib_time_now (vm);
2374 radv_info->last_multicast_time = vlib_time_now (vm);
2375 radv_info->last_radv_time = 0;
2379 /* adding or changing */
2384 pool_get (radv_info->adv_prefixes_pool, prefix);
2385 pi = prefix - radv_info->adv_prefixes_pool;
2386 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi, /* old_value */ 0);
2388 memset(prefix, 0x0, sizeof(ip6_radv_prefix_t));
2390 prefix->prefix_len = prefix_len;
2391 clib_memcpy(&prefix->prefix, prefix_addr, sizeof(ip6_address_t));
2393 /* initialize default values */
2394 prefix->adv_on_link_flag = 1; /* L bit set */
2395 prefix->adv_autonomous_flag = 1; /* A bit set */
2396 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
2397 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2398 prefix->enabled = 1;
2399 prefix->decrement_lifetime_flag = 1;
2400 prefix->deprecated_prefix_flag = 1;
2404 /* FIXME - Should the DP do this or the CP ?*/
2405 /* insert prefix into routing table as a connected prefix */
2414 if(prefix->prefix_len != prefix_len)
2415 return VNET_API_ERROR_INVALID_VALUE_2;
2419 /* FIXME - Should the DP do this or the CP ?*/
2420 /* remove from routing table if already there */
2424 if((val_lifetime == ~0) || (pref_lifetime == ~0))
2426 prefix->adv_valid_lifetime_in_secs = ~0;
2427 prefix->adv_pref_lifetime_in_secs = ~0;
2428 prefix->decrement_lifetime_flag = 0;
2432 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2433 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
2436 /* copy remaining */
2437 prefix->enabled = !(no_advertise != 0);
2438 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2439 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2443 /* fill in the expiration times */
2444 prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs;
2445 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
2447 radv_info->initial_adverts_sent = radv_info->initial_adverts_count -1;
2448 radv_info->next_multicast_time = vlib_time_now (vm);
2449 radv_info->last_multicast_time = vlib_time_now (vm);
2450 radv_info->last_radv_time = 0;
2456 ip6_neighbor_cmd(vlib_main_t * vm, unformat_input_t * main_input, vlib_cli_command_t * cmd)
2458 vnet_main_t * vnm = vnet_get_main();
2459 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2460 clib_error_t * error = 0;
2462 u8 suppress = 0, managed = 0, other = 0;
2463 u8 suppress_ll_option = 0, send_unicast = 0, cease= 0;
2464 u8 use_lifetime = 0;
2465 u32 sw_if_index, ra_lifetime = 0, ra_initial_count = 0, ra_initial_interval = 0;
2466 u32 ra_max_interval = 0 , ra_min_interval = 0;
2468 unformat_input_t _line_input, * line_input = &_line_input;
2469 vnet_sw_interface_t * sw_if0;
2471 int add_radv_info = 1;
2472 __attribute__((unused)) ip6_radv_t * radv_info = 0;
2473 ip6_address_t ip6_addr;
2477 /* Get a line of input. */
2478 if (! unformat_user (main_input, unformat_line_input, line_input))
2481 /* get basic radv info for this interface */
2482 if(unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2485 if (unformat_user (line_input,
2486 unformat_vnet_sw_interface, vnm, &sw_if_index))
2489 ethernet_interface_t * eth_if0 = 0;
2491 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2492 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2493 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2497 error = clib_error_return (0, "Interface must be of ethernet type");
2501 /* look up the radv_t information for this interface */
2502 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2504 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2508 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2512 error = clib_error_return (0, "unknown interface %U'",
2513 format_unformat_error, line_input);
2519 error = clib_error_return (0, "invalid interface name %U'",
2520 format_unformat_error, line_input);
2525 /* get the rest of the command */
2526 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2528 if (unformat (line_input, "no"))
2530 else if(unformat (line_input, "prefix %U/%d",
2531 unformat_ip6_address, &ip6_addr,
2537 else if (unformat (line_input, "ra-managed-config-flag"))
2542 else if (unformat (line_input, "ra-other-config-flag"))
2547 else if (unformat (line_input, "ra-suppress") ||
2548 unformat (line_input, "ra-surpress"))
2553 else if (unformat (line_input, "ra-suppress-link-layer") ||
2554 unformat (line_input, "ra-surpress-link-layer"))
2556 suppress_ll_option = 1;
2559 else if (unformat (line_input, "ra-send-unicast"))
2564 else if (unformat (line_input, "ra-lifetime"))
2566 if (!unformat (line_input, "%d", &ra_lifetime))
2567 return(error = unformat_parse_error (line_input));
2571 else if (unformat (line_input, "ra-initial"))
2573 if (!unformat (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
2574 return(error = unformat_parse_error (line_input));
2577 else if (unformat (line_input, "ra-interval"))
2579 if (!unformat (line_input, "%d", &ra_max_interval))
2580 return(error = unformat_parse_error (line_input));
2582 if (!unformat (line_input, "%d", &ra_min_interval))
2583 ra_min_interval = 0;
2586 else if(unformat (line_input, "ra-cease"))
2592 return(unformat_parse_error (line_input));
2597 ip6_neighbor_ra_config(vm, sw_if_index,
2598 suppress, managed, other,
2599 suppress_ll_option, send_unicast, cease,
2600 use_lifetime, ra_lifetime,
2601 ra_initial_count, ra_initial_interval,
2602 ra_max_interval, ra_min_interval,
2607 u32 valid_lifetime_in_secs = 0;
2608 u32 pref_lifetime_in_secs = 0;
2609 u8 use_prefix_default_values = 0;
2610 u8 no_advertise = 0;
2612 u8 no_autoconfig = 0;
2615 /* get the rest of the command */
2616 while(unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2618 if(unformat (line_input, "default"))
2620 use_prefix_default_values = 1;
2623 else if(unformat (line_input, "infinite"))
2625 valid_lifetime_in_secs = ~0;
2626 pref_lifetime_in_secs = ~0;
2629 else if(unformat (line_input, "%d %d", &valid_lifetime_in_secs,
2630 &pref_lifetime_in_secs))
2637 /* get the rest of the command */
2638 while (!use_prefix_default_values &&
2639 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2641 if(unformat (line_input, "no-advertise"))
2643 else if(unformat (line_input, "off-link"))
2645 else if(unformat (line_input, "no-autoconfig"))
2647 else if(unformat (line_input, "no-onlink"))
2650 return(unformat_parse_error (line_input));
2653 ip6_neighbor_ra_prefix(vm, sw_if_index,
2654 &ip6_addr, addr_len,
2655 use_prefix_default_values,
2656 valid_lifetime_in_secs,
2657 pref_lifetime_in_secs,
2665 unformat_free (line_input);
2672 ip6_print_addrs(vlib_main_t * vm,
2675 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2678 for (i = 0; i < vec_len (addrs); i++)
2680 ip_interface_address_t * a = pool_elt_at_index(lm->if_address_pool, addrs[i]);
2681 ip6_address_t * address = ip_interface_address_get_address (lm, a);
2683 vlib_cli_output (vm, "\t\t%U/%d",
2684 format_ip6_address, address,
2689 static clib_error_t *
2690 show_ip6_interface_cmd (vlib_main_t * vm,
2691 unformat_input_t * input,
2692 vlib_cli_command_t * cmd)
2694 vnet_main_t * vnm = vnet_get_main();
2695 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2696 clib_error_t * error = 0;
2701 if (unformat_user (input,
2702 unformat_vnet_sw_interface, vnm, &sw_if_index))
2706 /* look up the radv_t information for this interface */
2707 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2709 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2713 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2714 ip6_radv_t * radv_info;
2715 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2717 vlib_cli_output (vm, "%U is admin %s\n", format_vnet_sw_interface_name, vnm,
2718 vnet_get_sw_interface (vnm, sw_if_index),
2719 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ? "up" : "down"));
2722 u32 *link_scope = 0, *global_scope = 0;
2723 u32 *local_scope = 0, *unknown_scope = 0;
2724 ip_interface_address_t * a;
2726 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
2727 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
2729 while (ai != (u32)~0)
2731 a = pool_elt_at_index(lm->if_address_pool, ai);
2732 ip6_address_t * address = ip_interface_address_get_address (lm, a);
2734 if (ip6_address_is_link_local_unicast (address))
2735 vec_add1 (link_scope, ai);
2736 else if(ip6_address_is_global_unicast (address))
2737 vec_add1 (global_scope, ai);
2738 else if(ip6_address_is_local_unicast (address))
2739 vec_add1 (local_scope, ai);
2741 vec_add1 (unknown_scope, ai);
2743 ai = a->next_this_sw_interface;
2746 if (vec_len (link_scope))
2748 vlib_cli_output (vm, "\tLink-local address(es):\n");
2749 ip6_print_addrs (vm, link_scope);
2750 vec_free (link_scope);
2753 if (vec_len (local_scope))
2755 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
2756 ip6_print_addrs (vm, local_scope);
2757 vec_free (local_scope);
2760 if (vec_len (global_scope))
2762 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
2763 ip6_print_addrs (vm, global_scope);
2764 vec_free (global_scope);
2767 if (vec_len (unknown_scope))
2769 vlib_cli_output (vm, "\tOther-scope address(es):\n");
2770 ip6_print_addrs (vm, unknown_scope);
2771 vec_free (unknown_scope);
2774 vlib_cli_output (vm, "\tJoined group address(es):\n");
2775 ip6_mldp_group_t *m;
2776 pool_foreach (m, radv_info->mldp_group_pool, ({
2777 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address, &m->mcast_address);
2780 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
2781 ip6_radv_prefix_t * p;
2782 pool_foreach (p, radv_info->adv_prefixes_pool, ({
2783 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
2784 format_ip6_address, &p->prefix, p->prefix_len);
2787 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
2788 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
2789 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
2790 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
2791 vlib_cli_output (vm, "\tND DAD is disabled\n");
2792 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
2793 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
2794 radv_info->adv_neighbor_reachable_time_in_msec);
2795 vlib_cli_output (vm, "\tND advertised retransmit interval is %d (msec)\n",
2796 radv_info->adv_time_in_msec_between_retransmitted_neighbor_solicitations);
2798 u32 ra_interval = radv_info->max_radv_interval;
2799 u32 ra_interval_min = radv_info->min_radv_interval;
2800 vlib_cli_output (vm, "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
2801 ra_interval, ra_interval_min);
2802 vlib_cli_output (vm, "\tND router advertisements live for %d seconds\n",
2803 radv_info->adv_router_lifetime_in_sec);
2804 vlib_cli_output (vm, "\tHosts %s stateless autoconfig for addresses\n",
2805 (radv_info->adv_managed_flag) ? "use" :" don't use");
2806 vlib_cli_output (vm, "\tND router advertisements sent %d\n", radv_info->n_advertisements_sent);
2807 vlib_cli_output (vm, "\tND router solicitations received %d\n", radv_info->n_solicitations_rcvd);
2808 vlib_cli_output (vm, "\tND router solicitations dropped %d\n", radv_info->n_solicitations_dropped);
2812 error = clib_error_return (0, "IPv6 not enabled on interface",
2813 format_unformat_error, input);
2820 VLIB_CLI_COMMAND (show_ip6_interface_command, static) = {
2821 .path = "show ip6 interface",
2822 .function = show_ip6_interface_cmd,
2823 .short_help = "show ip6 interface <iface name>",
2827 disable_ip6_interface(vlib_main_t * vm,
2830 clib_error_t * error = 0;
2831 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2834 /* look up the radv_t information for this interface */
2835 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2836 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2838 /* if not created - do nothing */
2841 vnet_main_t * vnm = vnet_get_main();
2842 ip6_radv_t * radv_info;
2844 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2846 /* check radv_info ref count for other ip6 addresses on this interface */
2847 if(radv_info->ref_count == 0 )
2849 /* essentially "disables" ipv6 on this interface */
2850 error = ip6_add_del_interface_address (vm, sw_if_index,
2851 &radv_info->link_local_address,
2852 radv_info->link_local_prefix_len,
2855 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, 0/* is_add */);
2862 ip6_interface_enabled(vlib_main_t * vm,
2865 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2868 /* look up the radv_t information for this interface */
2869 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2871 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2877 enable_ip6_interface(vlib_main_t * vm,
2880 clib_error_t * error = 0;
2881 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
2885 /* look up the radv_t information for this interface */
2886 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
2888 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2890 /* if not created yet */
2893 vnet_main_t * vnm = vnet_get_main();
2894 vnet_sw_interface_t * sw_if0;
2896 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2897 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2899 ethernet_interface_t * eth_if0;
2901 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2904 /* create radv_info. for this interface. This holds all the info needed for router adverts */
2905 ri = ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
2909 ip6_radv_t * radv_info;
2910 ip6_address_t link_local_address;
2912 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2914 ip6_link_local_address_from_ethernet_mac_address (&link_local_address,
2917 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
2918 if(sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB)
2920 /* make up an interface id */
2924 link_local_address.as_u64[0] = radv_info->randomizer;
2927 md5_add (&m, &link_local_address, 16);
2928 md5_finish (&m, digest);
2930 clib_memcpy(&link_local_address, digest, 16);
2932 radv_info->randomizer = link_local_address.as_u64[0];
2934 link_local_address.as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2936 link_local_address.as_u8[8] &= 0xfd;
2939 /* essentially "enables" ipv6 on this interface */
2940 error = ip6_add_del_interface_address (vm, sw_if_index,
2941 &link_local_address,
2942 128 /* address width */,
2946 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, !is_add);
2949 radv_info->link_local_address = link_local_address;
2950 radv_info->link_local_prefix_len = 64;
2959 static clib_error_t *
2960 enable_ip6_interface_cmd (vlib_main_t * vm,
2961 unformat_input_t * input,
2962 vlib_cli_command_t * cmd)
2964 vnet_main_t * vnm = vnet_get_main();
2965 clib_error_t * error = 0;
2970 if (unformat_user (input,
2971 unformat_vnet_sw_interface, vnm, &sw_if_index))
2973 enable_ip6_interface(vm, sw_if_index);
2977 error = clib_error_return (0, "unknown interface\n'",
2978 format_unformat_error, input);
2984 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) = {
2985 .path = "enable ip6 interface",
2986 .function = enable_ip6_interface_cmd,
2987 .short_help = "enable ip6 interface <iface name>",
2990 static clib_error_t *
2991 disable_ip6_interface_cmd (vlib_main_t * vm,
2992 unformat_input_t * input,
2993 vlib_cli_command_t * cmd)
2995 vnet_main_t * vnm = vnet_get_main();
2996 clib_error_t * error = 0;
3001 if (unformat_user (input,
3002 unformat_vnet_sw_interface, vnm, &sw_if_index))
3004 error = disable_ip6_interface(vm, sw_if_index);
3008 error = clib_error_return (0, "unknown interface\n'",
3009 format_unformat_error, input);
3015 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) = {
3016 .path = "disable ip6 interface",
3017 .function = disable_ip6_interface_cmd,
3018 .short_help = "disable ip6 interface <iface name>",
3021 VLIB_CLI_COMMAND (ip6_nd_command, static) = {
3023 .short_help = "Set ip6 neighbor discovery parameters",
3024 .function = ip6_neighbor_cmd,
3028 set_ip6_link_local_address(vlib_main_t * vm,
3030 ip6_address_t *address,
3033 clib_error_t * error = 0;
3034 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3036 ip6_radv_t * radv_info;
3037 vnet_main_t * vnm = vnet_get_main();
3039 if( !ip6_address_is_link_local_unicast (address))
3041 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
3042 return(error = clib_error_return (0, "address not link-local",
3043 format_unformat_error));
3046 /* call enable ipv6 */
3047 enable_ip6_interface(vm, sw_if_index);
3049 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3053 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3055 /* save if link local address (overwrite default) */
3057 /* delete the old one */
3058 error = ip6_add_del_interface_address (vm, sw_if_index,
3059 &radv_info->link_local_address,
3060 radv_info->link_local_prefix_len /* address width */,
3065 /* add the new one */
3066 error = ip6_add_del_interface_address (vm, sw_if_index,
3068 address_length /* address width */,
3073 radv_info->link_local_address = *address;
3074 radv_info->link_local_prefix_len = address_length;
3080 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3081 error = clib_error_return (0, "ip6 not enabled for interface",
3082 format_unformat_error);
3088 set_ip6_link_local_address_cmd (vlib_main_t * vm,
3089 unformat_input_t * input,
3090 vlib_cli_command_t * cmd)
3092 vnet_main_t * vnm = vnet_get_main();
3093 clib_error_t * error = 0;
3095 ip6_address_t ip6_addr;
3098 if (unformat_user (input,
3099 unformat_vnet_sw_interface, vnm, &sw_if_index))
3101 /* get the rest of the command */
3102 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3104 if(unformat (input, "%U/%d",
3105 unformat_ip6_address, &ip6_addr,
3109 return(unformat_parse_error (input));
3112 error = set_ip6_link_local_address(vm,
3119 VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) = {
3120 .path = "set ip6 link-local address",
3121 .short_help = "Set ip6 interface link-local address <intfc> <address.>",
3122 .function = set_ip6_link_local_address_cmd,
3125 /* callback when an interface address is added or deleted */
3127 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3130 ip6_address_t * address,
3132 u32 if_address_index,
3135 vnet_main_t * vnm = vnet_get_main();
3136 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3138 vlib_main_t * vm = vnm->vlib_main;
3139 ip6_radv_t * radv_info;
3141 ip6_mldp_group_t *mcast_group_info;
3143 /* create solicited node multicast address for this interface adddress */
3144 ip6_set_solicited_node_multicast_address (&a, 0);
3146 a.as_u8[0xd] = address->as_u8[0xd];
3147 a.as_u8[0xe] = address->as_u8[0xe];
3148 a.as_u8[0xf] = address->as_u8[0xf];
3152 /* try to create radv_info - does nothing if ipv6 already enabled */
3153 enable_ip6_interface(vm, sw_if_index);
3155 /* look up the radv_t information for this interface */
3156 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
3157 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3161 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3164 if( !ip6_address_is_link_local_unicast (address))
3165 radv_info->ref_count++;
3167 /* lookup prefix info for this address on this interface */
3168 uword * p = mhash_get (&radv_info->address_to_mldp_index, &a);
3169 mcast_group_info = p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3171 /* add -solicted node multicast address */
3172 if(!mcast_group_info)
3176 pool_get (radv_info->mldp_group_pool, mcast_group_info);
3178 mi = mcast_group_info - radv_info->mldp_group_pool;
3179 mhash_set (&radv_info->address_to_mldp_index, &a, mi, /* old_value */ 0);
3181 mcast_group_info->type = 4;
3182 mcast_group_info->mcast_source_address_pool = 0;
3183 mcast_group_info->num_sources = 0;
3184 clib_memcpy(&mcast_group_info->mcast_address, &a, sizeof(ip6_address_t));
3192 /* look up the radv_t information for this interface */
3193 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index, ~0);
3194 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3198 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3200 /* lookup prefix info for this address on this interface */
3201 uword * p = mhash_get (&radv_info->address_to_mldp_index, &a);
3202 mcast_group_info = p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3204 if(mcast_group_info)
3206 mhash_unset (&radv_info->address_to_mldp_index, &a,/* old_value */ 0);
3207 pool_put (radv_info->mldp_group_pool, mcast_group_info);
3210 /* if interface up send MLDP "report" */
3211 radv_info->all_routers_mcast = 0;
3214 if( !ip6_address_is_link_local_unicast (address))
3215 radv_info->ref_count--;
3220 clib_error_t *ip6_set_neighbor_limit (u32 neighbor_limit)
3222 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3224 nm->limit_neighbor_cache_size = neighbor_limit;
3228 static clib_error_t * ip6_neighbor_init (vlib_main_t * vm)
3230 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3231 ip6_main_t * im = &ip6_main;
3233 mhash_init (&nm->neighbor_index_by_key,
3234 /* value size */ sizeof (uword),
3235 /* key size */ sizeof (ip6_neighbor_key_t));
3237 icmp6_register_type (vm, ICMP6_neighbor_solicitation, ip6_icmp_neighbor_solicitation_node.index);
3238 icmp6_register_type (vm, ICMP6_neighbor_advertisement, ip6_icmp_neighbor_advertisement_node.index);
3239 icmp6_register_type (vm, ICMP6_router_solicitation, ip6_icmp_router_solicitation_node.index);
3240 icmp6_register_type (vm, ICMP6_router_advertisement, ip6_icmp_router_advertisement_node.index);
3242 /* handler node for ip6 neighbor discovery events and timers */
3243 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3245 /* add call backs */
3246 ip6_add_del_interface_address_callback_t cb;
3247 memset(&cb, 0x0, sizeof(ip6_add_del_interface_address_callback_t));
3249 /* when an interface address changes... */
3250 cb.function = ip6_neighbor_add_del_interface_address;
3251 cb.function_opaque = 0;
3252 vec_add1 (im->add_del_interface_address_callbacks, cb);
3254 mhash_init (&nm->pending_resolutions_by_address,
3255 /* value size */ sizeof (uword),
3256 /* key size */ sizeof (ip6_address_t));
3258 mhash_init (&nm->mac_changes_by_address,
3259 /* value size */ sizeof (uword),
3260 /* key size */ sizeof (ip6_address_t));
3262 /* default, configurable */
3263 nm->limit_neighbor_cache_size = 50000;
3266 /* $$$$ Hack fix for today */
3267 vec_validate_init_empty
3268 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */);
3274 VLIB_INIT_FUNCTION (ip6_neighbor_init);
3277 void vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
3283 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3284 ip6_address_t * address = address_arg;
3286 pending_resolution_t * pr;
3288 pool_get (nm->pending_resolutions, pr);
3290 pr->next_index = ~0;
3291 pr->node_index = node_index;
3292 pr->type_opaque = type_opaque;
3295 p = mhash_get (&nm->pending_resolutions_by_address, address);
3298 /* Insert new resolution at the head of the list */
3299 pr->next_index = p[0];
3300 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
3303 mhash_set (&nm->pending_resolutions_by_address, address,
3304 pr - nm->pending_resolutions, 0 /* old value */);
3307 int vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
3308 void * data_callback,
3316 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3317 ip6_address_t * address = address_arg;
3319 pending_resolution_t * mc;
3320 void (*fp)(u32, u8 *) = data_callback;
3324 pool_get (nm->mac_changes, mc);
3326 mc->next_index = ~0;
3327 mc->node_index = node_index;
3328 mc->type_opaque = type_opaque;
3330 mc->data_callback = data_callback;
3333 p = mhash_get (&nm->mac_changes_by_address, address);
3336 /* Insert new resolution at the head of the list */
3337 mc->next_index = p[0];
3338 mhash_unset (&nm->mac_changes_by_address, address, 0);
3341 mhash_set (&nm->mac_changes_by_address, address,
3342 mc - nm->mac_changes, 0);
3348 pending_resolution_t * mc_last = 0;
3350 p = mhash_get (&nm->mac_changes_by_address, address);
3352 return VNET_API_ERROR_NO_SUCH_ENTRY;
3356 while (index != (u32)~0)
3358 mc = pool_elt_at_index (nm->mac_changes, index);
3359 if (mc->node_index == node_index &&
3360 mc->type_opaque == type_opaque &&
3363 /* Clients may need to clean up pool entries, too */
3365 (*fp)(mc->data, 0 /* no new mac addrs */);
3368 mhash_unset (&nm->mac_changes_by_address, address, 0);
3369 if (mc->next_index != ~0)
3370 mhash_set (&nm->mac_changes_by_address, address,
3372 pool_put (nm->mac_changes, mc);
3378 mc_last->next_index = mc->next_index;
3379 pool_put (nm->mac_changes, mc);
3384 index = mc->next_index;
3387 return VNET_API_ERROR_NO_SUCH_ENTRY;
3391 int vnet_ip6_nd_term (vlib_main_t * vm,
3392 vlib_node_runtime_t * node,
3394 ethernet_header_t * eth,
3400 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3401 icmp6_neighbor_solicitation_or_advertisement_header_t * ndh;
3402 pending_resolution_t * mc;
3405 ndh = ip6_next_header (ip);
3406 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
3407 ndh->icmp.type != ICMP6_neighbor_advertisement)
3410 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
3411 (p0->flags & VLIB_BUFFER_IS_TRACED)))
3413 u8 *t0 = vlib_add_trace (vm, node, p0,
3414 sizeof (icmp6_input_trace_t));
3415 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
3418 /* Check if anyone want ND events for L2 BDs */
3419 p = mhash_get (&nm->mac_changes_by_address, &ip6a_zero);
3420 if (p && shg == 0 && /* Only SHG 0 interface which is more likely local */
3421 !ip6_address_is_link_local_unicast (&ip->src_address))
3423 u32 next_index = p[0];
3424 while (next_index != (u32)~0)
3426 int (*fp)(u32, u8 *, u32, ip6_address_t *);
3428 mc = pool_elt_at_index (nm->mac_changes, next_index);
3429 fp = mc->data_callback;
3430 /* Call the callback, return 1 to suppress dup events */
3431 if (fp) rv = (*fp)(mc->data,
3435 /* Signal the resolver process */
3437 vlib_process_signal_event (vm, mc->node_index,
3440 next_index = mc->next_index;
3444 /* Check if MAC entry exsist for solicited target IP */
3445 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
3447 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t * opt;
3448 l2_bridge_domain_t *bd_config;
3451 opt = (void *) (ndh + 1);
3452 if ((opt->header.type !=
3453 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
3454 (opt->header.n_data_u64s != 1))
3455 return 0; /* source link layer address option not present */
3457 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
3458 macp = (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
3460 { /* found ip-mac entry, generate eighbor advertisement response */
3462 vlib_node_runtime_t * error_node =
3463 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
3464 ip->dst_address = ip->src_address;
3465 ip->src_address = ndh->target_address;
3466 ip->hop_limit = 255;
3468 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
3469 clib_memcpy (opt->ethernet_address, macp, 6);
3470 ndh->icmp.type = ICMP6_neighbor_advertisement;
3471 ndh->advertisement_flags = clib_host_to_net_u32
3472 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
3473 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
3474 ndh->icmp.checksum = 0;
3475 ndh->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum(vm, p0, ip,
3477 clib_memcpy(eth->dst_address, eth->src_address, 6);
3478 clib_memcpy(eth->src_address, macp, 6);
3479 vlib_error_count (vm, error_node->node_index,
3480 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
3490 ethernet_ndp_change_mac (vlib_main_t * vm, u32 sw_if_index)
3492 ip6_neighbor_main_t * nm = &ip6_neighbor_main;
3496 pool_foreach (n, nm->neighbor_pool, ({
3497 if (n->key.sw_if_index == sw_if_index)
3499 if (ADJ_INDEX_INVALID != n->adj_index)
3501 adj_nbr_update_rewrite(n->adj_index,
3502 n->link_layer_address);