diff options
Diffstat (limited to 'src/vnet/ip6-nd')
-rw-r--r-- | src/vnet/ip6-nd/ip6_mld.c | 541 | ||||
-rw-r--r-- | src/vnet/ip6-nd/ip6_nd.api | 236 | ||||
-rw-r--r-- | src/vnet/ip6-nd/ip6_nd.c | 482 | ||||
-rw-r--r-- | src/vnet/ip6-nd/ip6_nd.h | 35 | ||||
-rw-r--r-- | src/vnet/ip6-nd/ip6_nd_api.c | 382 | ||||
-rw-r--r-- | src/vnet/ip6-nd/ip6_nd_proxy.c | 127 | ||||
-rw-r--r-- | src/vnet/ip6-nd/ip6_nd_test.c | 330 | ||||
-rw-r--r-- | src/vnet/ip6-nd/ip6_ra.c | 2261 | ||||
-rw-r--r-- | src/vnet/ip6-nd/ip6_ra.h | 89 | ||||
-rw-r--r-- | src/vnet/ip6-nd/rd_cp.api | 44 | ||||
-rw-r--r-- | src/vnet/ip6-nd/rd_cp.c | 623 | ||||
-rw-r--r-- | src/vnet/ip6-nd/rd_cp.h | 33 | ||||
-rw-r--r-- | src/vnet/ip6-nd/rd_cp_api.c | 73 |
13 files changed, 5256 insertions, 0 deletions
diff --git a/src/vnet/ip6-nd/ip6_mld.c b/src/vnet/ip6-nd/ip6_mld.c new file mode 100644 index 00000000000..bd5f8489b7b --- /dev/null +++ b/src/vnet/ip6-nd/ip6_mld.c @@ -0,0 +1,541 @@ +/* + * ip/ip6_neighbor.c: IP6 neighbor handling + * + * Copyright (c) 2010 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vnet/ip6-nd/ip6_nd.h> + +#include <vnet/ip/ip.h> +#include <vnet/ip-neighbor/ip_neighbor_dp.h> + +#include <vnet/fib/ip6_fib.h> +#include <vnet/ip/ip6_link.h> +#include <vnet/ip/ip6_ll_table.h> + +/** + * @file + * @brief IPv6 Neighbor Adjacency and Neighbor Discovery. + * + * The files contains the API and CLI code for managing IPv6 neighbor + * adjacency tables and neighbor discovery logic. + */ + +/* *INDENT-OFF*/ +/* multicast listener report packet format for ethernet. */ +typedef CLIB_PACKED (struct +{ + ip6_hop_by_hop_ext_t ext_hdr; + ip6_router_alert_option_t alert; + ip6_padN_option_t pad; + icmp46_header_t icmp; + u16 rsvd; + u16 num_addr_records; + icmp6_multicast_address_record_t records[0]; +}) icmp6_multicast_listener_report_header_t; + +typedef CLIB_PACKED (struct +{ + ip6_header_t ip; + icmp6_multicast_listener_report_header_t report_hdr; +}) icmp6_multicast_listener_report_packet_t; +/* *INDENT-ON*/ + +typedef struct +{ + /* group information */ + u16 num_sources; + u8 type; + ip6_address_t mcast_address; + ip6_address_t *mcast_source_address_pool; +} ip6_mldp_group_t; + +typedef struct ip6_nd_t_ +{ + /* local information */ + u32 sw_if_index; + int all_routers_mcast; + + /* MLDP group information */ + ip6_mldp_group_t *mldp_group_pool; + + /* Hash table mapping address to index in mldp address pool. */ + mhash_t address_to_mldp_index; + +} ip6_mld_t; + + +static ip6_link_delegate_id_t ip6_mld_delegate_id; +static ip6_mld_t *ip6_mld_pool; + +///// + +static inline ip6_mld_t * +ip6_mld_get_itf (u32 sw_if_index) +{ + index_t imi; + + imi = ip6_link_delegate_get (sw_if_index, ip6_mld_delegate_id); + + if (INDEX_INVALID != imi) + return (pool_elt_at_index (ip6_mld_pool, imi)); + + return (NULL); +} + +/** + * @brief Add a multicast Address to the advertised MLD set + */ +static void +ip6_neighbor_add_mld_prefix (ip6_mld_t * imd, ip6_address_t * addr) +{ + ip6_mldp_group_t *mcast_group_info; + uword *p; + + /* lookup mldp info for this interface */ + p = mhash_get (&imd->address_to_mldp_index, addr); + mcast_group_info = p ? pool_elt_at_index (imd->mldp_group_pool, p[0]) : 0; + + /* add address */ + if (!mcast_group_info) + { + /* add */ + u32 mi; + pool_get_zero (imd->mldp_group_pool, mcast_group_info); + + mi = mcast_group_info - imd->mldp_group_pool; + mhash_set (&imd->address_to_mldp_index, addr, mi, /* old_value */ + 0); + + mcast_group_info->type = 4; + mcast_group_info->mcast_source_address_pool = 0; + mcast_group_info->num_sources = 0; + clib_memcpy (&mcast_group_info->mcast_address, addr, + sizeof (ip6_address_t)); + } +} + +/** + * @brief Delete a multicast Address from the advertised MLD set + */ +static void +ip6_neighbor_del_mld_prefix (ip6_mld_t * imd, ip6_address_t * addr) +{ + ip6_mldp_group_t *mcast_group_info; + uword *p; + + p = mhash_get (&imd->address_to_mldp_index, &addr); + mcast_group_info = p ? pool_elt_at_index (imd->mldp_group_pool, p[0]) : 0; + + if (mcast_group_info) + { + mhash_unset (&imd->address_to_mldp_index, &addr, + /* old_value */ 0); + pool_put (imd->mldp_group_pool, mcast_group_info); + } +} + +/** + * @brief Add a multicast Address to the advertised MLD set + */ +static void +ip6_neighbor_add_mld_grp (ip6_mld_t * a, + ip6_multicast_address_scope_t scope, + ip6_multicast_link_local_group_id_t group) +{ + ip6_address_t addr; + + ip6_set_reserved_multicast_address (&addr, scope, group); + + ip6_neighbor_add_mld_prefix (a, &addr); +} + +static const ethernet_interface_t * +ip6_mld_get_eth_itf (u32 sw_if_index) +{ + const vnet_sw_interface_t *sw; + + /* lookup radv container - ethernet interfaces only */ + sw = vnet_get_sup_sw_interface (vnet_get_main (), sw_if_index); + if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE) + return (ethernet_get_interface (ðernet_main, sw->hw_if_index)); + + return (NULL); +} + +/** + * @brief create and initialize router advertisement parameters with default + * values for this intfc + */ +static void +ip6_mld_link_enable (u32 sw_if_index) +{ + const ethernet_interface_t *eth; + ip6_mld_t *imd; + + eth = ip6_mld_get_eth_itf (sw_if_index); + + if (NULL == eth) + return; + + ASSERT (INDEX_INVALID == ip6_link_delegate_get (sw_if_index, + ip6_mld_delegate_id)); + + pool_get_zero (ip6_mld_pool, imd); + + imd->sw_if_index = sw_if_index; + + mhash_init (&imd->address_to_mldp_index, sizeof (uword), + sizeof (ip6_address_t)); + + /* add multicast groups we will always be reporting */ + ip6_neighbor_add_mld_grp (imd, + IP6_MULTICAST_SCOPE_link_local, + IP6_MULTICAST_GROUP_ID_all_hosts); + ip6_neighbor_add_mld_grp (imd, + IP6_MULTICAST_SCOPE_link_local, + IP6_MULTICAST_GROUP_ID_all_routers); + ip6_neighbor_add_mld_grp (imd, + IP6_MULTICAST_SCOPE_link_local, + IP6_MULTICAST_GROUP_ID_mldv2_routers); + + ip6_link_delegate_update (sw_if_index, ip6_mld_delegate_id, + imd - ip6_mld_pool); +} + +static void +ip6_mld_delegate_disable (index_t imdi) +{ + ip6_mldp_group_t *m; + ip6_mld_t *imd; + + imd = pool_elt_at_index (ip6_mld_pool, imdi); + + /* clean MLD pools */ + /* *INDENT-OFF* */ + pool_flush (m, imd->mldp_group_pool, + ({ + mhash_unset (&imd->address_to_mldp_index, &m->mcast_address, 0); + })); + /* *INDENT-ON* */ + + pool_free (imd->mldp_group_pool); + + mhash_free (&imd->address_to_mldp_index); + + pool_put (ip6_mld_pool, imd); +} + +/* send an mldpv2 report */ +static void +ip6_neighbor_send_mldpv2_report (u32 sw_if_index) +{ + vnet_main_t *vnm = vnet_get_main (); + vlib_main_t *vm = vnm->vlib_main; + int bogus_length; + + ip6_mld_t *imd; + u16 payload_length; + vlib_buffer_t *b0; + ip6_header_t *ip0; + u32 *to_next; + vlib_frame_t *f; + u32 bo0; + u32 n_to_alloc = 1; + + icmp6_multicast_listener_report_header_t *rh0; + icmp6_multicast_listener_report_packet_t *rp0; + + if (! !vnet_sw_interface_is_admin_up (vnm, sw_if_index)) + return; + + imd = ip6_mld_get_itf (sw_if_index); + + if (NULL == imd) + return; + + /* send report now - build a mldpv2 report packet */ + if (0 == vlib_buffer_alloc (vm, &bo0, n_to_alloc)) + { + alloc_fail: + clib_warning ("buffer allocation failure"); + return; + } + + b0 = vlib_get_buffer (vm, bo0); + + /* adjust the sizeof the buffer to just include the ipv6 header */ + b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t); + + payload_length = sizeof (icmp6_multicast_listener_report_header_t); + + b0->error = ICMP6_ERROR_NONE; + + rp0 = vlib_buffer_get_current (b0); + ip0 = (ip6_header_t *) & rp0->ip; + rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr; + + clib_memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t)); + + ip0->ip_version_traffic_class_and_flow_label = + clib_host_to_net_u32 (0x6 << 28); + + ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS; + /* for DEBUG - vnet driver won't seem to emit router alerts */ + /* ip0->protocol = IP_PROTOCOL_ICMP6; */ + ip0->hop_limit = 1; + + rh0->icmp.type = ICMP6_multicast_listener_report_v2; + + /* source address MUST be the link-local address */ + ip6_address_copy (&ip0->src_address, + ip6_get_link_local_address (sw_if_index)); + + /* destination is all mldpv2 routers */ + ip6_set_reserved_multicast_address (&ip0->dst_address, + IP6_MULTICAST_SCOPE_link_local, + IP6_MULTICAST_GROUP_ID_mldv2_routers); + + /* add reports here */ + ip6_mldp_group_t *m; + int num_addr_records = 0; + icmp6_multicast_address_record_t rr; + + /* fill in the hop-by-hop extension header (router alert) info */ + rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6; + rh0->ext_hdr.n_data_u64s = 0; + + rh0->alert.type = IP6_MLDP_ALERT_TYPE; + rh0->alert.len = 2; + rh0->alert.value = 0; + + rh0->pad.type = 1; + rh0->pad.len = 0; + + rh0->icmp.checksum = 0; + + /* *INDENT-OFF* */ + pool_foreach (m, imd->mldp_group_pool, + ({ + rr.type = m->type; + rr.aux_data_len_u32s = 0; + rr.num_sources = clib_host_to_net_u16 (m->num_sources); + clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t)); + + num_addr_records++; + + if(vlib_buffer_add_data (vm, &bo0, (void *)&rr, + sizeof(icmp6_multicast_address_record_t))) + { + vlib_buffer_free (vm, &bo0, 1); + goto alloc_fail; + } + + payload_length += sizeof( icmp6_multicast_address_record_t); + })); + /* *INDENT-ON* */ + + rh0->rsvd = 0; + rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records); + + /* update lengths */ + ip0->payload_length = clib_host_to_net_u16 (payload_length); + + rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, + &bogus_length); + ASSERT (bogus_length == 0); + + /* + * OK to override w/ no regard for actual FIB, because + * ip6-rewrite only looks at the adjacency. + */ + vnet_buffer (b0)->sw_if_index[VLIB_RX] = + vnet_main.local_interface_sw_if_index; + + vnet_buffer (b0)->ip.adj_index[VLIB_TX] = + ip6_link_get_mcast_adj (sw_if_index); + b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; + + vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast"); + + f = vlib_get_frame_to_node (vm, node->index); + to_next = vlib_frame_vector_args (f); + to_next[0] = bo0; + f->n_vectors = 1; + + vlib_put_frame_to_node (vm, node->index, f); + return; +} + +/* send a RA or update the timer info etc.. */ +static uword +ip6_mld_timer_event (vlib_main_t * vm, + vlib_node_runtime_t * node, vlib_frame_t * frame) +{ + vnet_main_t *vnm = vnet_get_main (); + ip6_mld_t *imd; + + /* Interface ip6 radv info list */ + /* *INDENT-OFF* */ + pool_foreach (imd, ip6_mld_pool, + ({ + if(!vnet_sw_interface_is_admin_up (vnm, imd->sw_if_index)) + { + imd->all_routers_mcast = 0; + continue; + } + + /* Make sure that we've joined the all-routers multicast group */ + if(!imd->all_routers_mcast) + { + /* send MDLP_REPORT_EVENT message */ + ip6_neighbor_send_mldpv2_report(imd->sw_if_index); + imd->all_routers_mcast = 1; + } + })); + /* *INDENT-ON* */ + + return 0; +} + +static uword +ip6_mld_event_process (vlib_main_t * vm, + vlib_node_runtime_t * node, vlib_frame_t * frame) +{ + uword event_type; + + /* init code here */ + + while (1) + { + vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ ); + + if (!vlib_process_get_event_data (vm, &event_type)) + { + /* No events found: timer expired. */ + /* process interface list and send RAs as appropriate, update timer info */ + ip6_mld_timer_event (vm, node, frame); + } + /* else; no events */ + } + return frame->n_vectors; +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (ip6_mld_event_process_node) = { + .function = ip6_mld_event_process, + .name = "ip6-mld-process", + .type = VLIB_NODE_TYPE_PROCESS, +}; +/* *INDENT-ON* */ + +static u8 * +format_ip6_mld (u8 * s, va_list * args) +{ + index_t imi = va_arg (*args, index_t); + u32 indent = va_arg (*args, u32); + ip6_mldp_group_t *m; + ip6_mld_t *imd; + + imd = pool_elt_at_index (ip6_mld_pool, imi); + + s = format (s, "%UJoined group address(es):\n", format_white_space, indent); + + /* *INDENT-OFF* */ + pool_foreach (m, imd->mldp_group_pool, + ({ + s = format (s, "%U%U\n", + format_white_space, indent+2, + format_ip6_address, + &m->mcast_address); + })); + /* *INDENT-ON* */ + + return (s); +} + +/** + * @brief callback when an interface address is added or deleted + */ +static void +ip6_mld_address_add (u32 imi, + const ip6_address_t * address, u8 address_oength) +{ + ip6_mld_t *imd; + ip6_address_t a; + + imd = pool_elt_at_index (ip6_mld_pool, imi); + + /* create solicited node multicast address for this interface address */ + ip6_set_solicited_node_multicast_address (&a, 0); + + a.as_u8[0xd] = address->as_u8[0xd]; + a.as_u8[0xe] = address->as_u8[0xe]; + a.as_u8[0xf] = address->as_u8[0xf]; + + ip6_neighbor_add_mld_prefix (imd, &a); +} + +static void +ip6_mld_address_del (u32 imi, + const ip6_address_t * address, u8 address_oength) +{ + ip6_mld_t *imd; + ip6_address_t a; + + imd = pool_elt_at_index (ip6_mld_pool, imi); + + /* create solicited node multicast address for this interface address */ + ip6_set_solicited_node_multicast_address (&a, 0); + + a.as_u8[0xd] = address->as_u8[0xd]; + a.as_u8[0xe] = address->as_u8[0xe]; + a.as_u8[0xf] = address->as_u8[0xf]; + + ip6_neighbor_del_mld_prefix (imd, &a); +} + +/** + * VFT for registering as a delegate to an IP6 link + */ +const static ip6_link_delegate_vft_t ip6_mld_delegate_vft = { + .ildv_disable = ip6_mld_delegate_disable, + .ildv_enable = ip6_mld_link_enable, + .ildv_format = format_ip6_mld, + .ildv_addr_add = ip6_mld_address_add, + .ildv_addr_del = ip6_mld_address_del, +}; + +static clib_error_t * +ip6_mld_init (vlib_main_t * vm) +{ + ip6_mld_delegate_id = ip6_link_delegate_register (&ip6_mld_delegate_vft); + + return (NULL); +} + +/* *INDENT-OFF* */ +VLIB_INIT_FUNCTION (ip6_mld_init) = +{ + .runs_after = VLIB_INITS("icmp6_init"), +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ip6-nd/ip6_nd.api b/src/vnet/ip6-nd/ip6_nd.api new file mode 100644 index 00000000000..91b5faf9bdf --- /dev/null +++ b/src/vnet/ip6-nd/ip6_nd.api @@ -0,0 +1,236 @@ +/* Hey Emacs use -*- mode: C -*- */ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** \file + + This file defines vpp IP control-plane API messages which are generally + called through a shared memory interface. +*/ + +option version = "1.0.0"; + +import "vnet/ip/ip_types.api"; +import "vnet/interface_types.api"; + +/** \brief IPv6 router advertisement config request + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param suppress - + @param managed - + @param other - + @param ll_option - + @param send_unicast - + @param cease - + @param is_no - + @param default_router - + @param max_interval - + @param min_interval - + @param lifetime - + @param initial_count - + @param initial_interval - +*/ +autoreply define sw_interface_ip6nd_ra_config +{ + u32 client_index; + u32 context; + vl_api_interface_index_t sw_if_index; + u8 suppress; + u8 managed; + u8 other; + u8 ll_option; + u8 send_unicast; + u8 cease; + bool is_no; + u8 default_router; + u32 max_interval; + u32 min_interval; + u32 lifetime; + u32 initial_count; + u32 initial_interval; +}; + +/** \brief IPv6 router advertisement prefix config request + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param sw_if_index - The interface the RA prefix information is for + @param prefix - The prefix to advertise + @param use_default - Revert to default settings + @param no_advertise - Do not advertise this prefix + @param off_link - The prefix is off link (it is not configured on the interface) + Configures the L-flag, When set, indicates that this + prefix can be used for on-link determination. + @param no_autoconfig - Setting for the A-flag. When + set indicates that this prefix can be used for + stateless address configuration. + @param no_onlink - The prefix is not on link. Make sure this is consistent + with the off_link parameter else YMMV + @param is_no - add/delete + @param val_lifetime - The length of time in + seconds (relative to the time the packet is sent) + that the prefix is valid for the purpose of on-link + determination. A value of all one bits + (0xffffffff) represents infinity + @param pref_lifetime - The length of time in + seconds (relative to the time the packet is sent) + that addresses generated from the prefix via + stateless address autoconfiguration remain + preferred [ADDRCONF]. A value of all one bits + (0xffffffff) represents infinity. +*/ +autoreply define sw_interface_ip6nd_ra_prefix +{ + u32 client_index; + u32 context; + vl_api_interface_index_t sw_if_index; + vl_api_prefix_t prefix; + bool use_default; + bool no_advertise; + bool off_link; + bool no_autoconfig; + bool no_onlink; + bool is_no; + u32 val_lifetime; + u32 pref_lifetime; +}; + +/** \brief IPv6 ND proxy config + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param sw_if_index - The interface the host is on + @param ip - The address of the host for which to proxy for + @param is_add - Adding or deleting +*/ +autoreply define ip6nd_proxy_add_del +{ + u32 client_index; + u32 context; + vl_api_interface_index_t sw_if_index; + bool is_add; + vl_api_ip6_address_t ip; +}; + +/** \brief IPv6 ND proxy details returned after request + @param context - sender context, to match reply w/ request + @param sw_if_index - The interface the host is on + @param ip - The address of the host for which to proxy for +*/ +define ip6nd_proxy_details +{ + u32 context; + vl_api_interface_index_t sw_if_index; + vl_api_ip6_address_t ip; +}; + +/** \brief IPv6 ND proxy dump request + @param context - sender context, to match reply w/ request +*/ +define ip6nd_proxy_dump +{ + u32 client_index; + u32 context; +}; + +/** \brief Start / stop sending router solicitation + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param irt - initial retransmission time + @param mrt - maximum retransmission time + @param mrc - maximum retransmission count + @param mrd - maximum retransmission duration + @param sw_if_index - software interface index of interface + for sending router solicitation + @param stop - if non-zero then stop sending router solicitation, + otherwise start sending router solicitation +*/ +autoreply define ip6nd_send_router_solicitation +{ + u32 client_index; + u32 context; + u32 irt; + u32 mrt; + u32 mrc; + u32 mrd; + vl_api_interface_index_t sw_if_index; + bool stop; +}; + +service { + rpc want_ip6_ra_events returns want_ip6_ra_events_reply + events ip6_ra_event; +}; + +/** \brief Register for ip6 router advertisement events + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param enable - 1 => register for events, 0 => cancel registration + @param pid - sender's pid +*/ +autoreply define want_ip6_ra_events +{ + u32 client_index; + u32 context; + bool enable; + u32 pid; +}; + +/** \brief Struct representing RA prefix info + @param prefix - RA prefix info destination address + @param flags - RA prefix info flags + @param valid_time - RA prefix info valid time + @param preferred_time - RA prefix info preferred time +*/ +typedef ip6_ra_prefix_info +{ + vl_api_prefix_t prefix; + u8 flags; + u32 valid_time; + u32 preferred_time; +}; + +/** \brief Tell client about a router advertisement event + @param client_index - opaque cookie to identify the sender + @param pid - client pid registered to receive notification + @param current_hop_limit - RA current hop limit + @param flags - RA flags + @param router_lifetime_in_sec - RA lifetime in seconds + @param router_addr - The router's address + @param neighbor_reachable_time_in_msec - RA neighbor reachable time in msec + @param time_in_msec_between_retransmitted_neighbor_solicitations - + time in msec between retransmitted neighbor solicitations + @param n_prefixes - + @param prefixes - +*/ +define ip6_ra_event +{ + u32 client_index; + u32 pid; + vl_api_interface_index_t sw_if_index; + vl_api_ip6_address_t router_addr; + u8 current_hop_limit; + u8 flags; + u16 router_lifetime_in_sec; + u32 neighbor_reachable_time_in_msec; + u32 time_in_msec_between_retransmitted_neighbor_solicitations; + u32 n_prefixes; + vl_api_ip6_ra_prefix_info_t prefixes[n_prefixes]; +}; + + +/* + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ip6-nd/ip6_nd.c b/src/vnet/ip6-nd/ip6_nd.c new file mode 100644 index 00000000000..eb89eb817f6 --- /dev/null +++ b/src/vnet/ip6-nd/ip6_nd.c @@ -0,0 +1,482 @@ +/* + * ip/ip6_neighbor.c: IP6 neighbor handling + * + * Copyright (c) 2010 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vnet/ip6-nd/ip6_nd.h> + +#include <vnet/ip-neighbor/ip_neighbor.h> +#include <vnet/ip-neighbor/ip_neighbor_dp.h> + +#include <vnet/fib/ip6_fib.h> +#include <vnet/ip/ip6_link.h> +#include <vnet/ip/ip6_ll_table.h> + +/** + * @file + * @brief IPv6 Neighbor Adjacency and Neighbor Discovery. + * + * The files contains the API and CLI code for managing IPv6 neighbor + * adjacency tables and neighbor discovery logic. + */ + +#define DEF_MAX_RADV_INTERVAL 200 +#define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL + +typedef struct ip6_nd_t_ +{ + /* local information */ + u32 sw_if_index; + + /* stats */ + u32 n_solicitations_rcvd; + u32 n_solicitations_dropped; +} ip6_nd_t; + +static ip6_link_delegate_id_t ip6_nd_delegate_id; +static ip6_nd_t *ip6_nd_pool; + + +typedef enum +{ + ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP, + ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY, + ICMP6_NEIGHBOR_SOLICITATION_N_NEXT, +} icmp6_neighbor_solicitation_or_advertisement_next_t; + +static_always_inline uword +icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame, + uword is_solicitation) +{ + vnet_main_t *vnm = vnet_get_main (); + ip6_main_t *im = &ip6_main; + uword n_packets = frame->n_vectors; + u32 *from, *to_next; + u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent; + icmp6_neighbor_discovery_option_type_t option_type; + vlib_node_runtime_t *error_node = + vlib_node_get_runtime (vm, ip6_icmp_input_node.index); + int bogus_length; + + from = vlib_frame_vector_args (frame); + n_left_from = n_packets; + next_index = node->cached_next_index; + + if (node->flags & VLIB_NODE_FLAG_TRACE) + vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors, + /* stride */ 1, + sizeof (icmp6_input_trace_t)); + + option_type = + (is_solicitation + ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address + : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address); + n_advertisements_sent = 0; + + while (n_left_from > 0) + { + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + while (n_left_from > 0 && n_left_to_next > 0) + { + vlib_buffer_t *p0; + ip6_header_t *ip0; + icmp6_neighbor_solicitation_or_advertisement_header_t *h0; + icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0; + u32 bi0, options_len0, sw_if_index0, next0, error0; + u32 ip6_sadd_link_local, ip6_sadd_unspecified; + int is_rewrite0; + u32 ni0; + + bi0 = to_next[0] = from[0]; + + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + p0 = vlib_get_buffer (vm, bi0); + ip0 = vlib_buffer_get_current (p0); + h0 = ip6_next_header (ip0); + options_len0 = + clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]); + + error0 = ICMP6_ERROR_NONE; + sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX]; + ip6_sadd_link_local = + ip6_address_is_link_local_unicast (&ip0->src_address); + ip6_sadd_unspecified = + ip6_address_is_unspecified (&ip0->src_address); + + /* Check that source address is unspecified, link-local or else on-link. */ + if (!ip6_sadd_unspecified && !ip6_sadd_link_local) + { + u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0); + + if (ADJ_INDEX_INVALID != src_adj_index0) + { + ip_adjacency_t *adj0 = adj_get (src_adj_index0); + + /* Allow all realistic-looking rewrite adjacencies to pass */ + ni0 = adj0->lookup_next_index; + is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) && + (ni0 < IP6_LOOKUP_N_NEXT); + + error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0 + || !is_rewrite0) + ? + ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK + : error0); + } + else + { + error0 = + ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK; + } + } + + o0 = (void *) (h0 + 1); + o0 = ((options_len0 == 8 && o0->header.type == option_type + && o0->header.n_data_u64s == 1) ? o0 : 0); + + /* If src address unspecified or link local, donot learn neighbor MAC */ + if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 && + !ip6_sadd_unspecified)) + { + ip_neighbor_learn_t learn = { + .sw_if_index = sw_if_index0, + .type = IP46_TYPE_IP6, + .ip.ip6 = (is_solicitation ? + ip0->src_address : h0->target_address), + }; + memcpy (&learn.mac, o0->ethernet_address, sizeof (learn.mac)); + ip_neighbor_learn_dp (&learn); + } + + if (is_solicitation && error0 == ICMP6_ERROR_NONE) + { + /* Check that target address is local to this router. */ + fib_node_index_t fei; + u32 fib_index; + + fib_index = + ip6_fib_table_get_index_for_sw_if_index (sw_if_index0); + + if (~0 == fib_index) + { + error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN; + } + else + { + if (ip6_address_is_link_local_unicast (&h0->target_address)) + { + fei = ip6_fib_table_lookup_exact_match + (ip6_ll_fib_get (sw_if_index0), + &h0->target_address, 128); + } + else + { + fei = ip6_fib_table_lookup_exact_match (fib_index, + &h0->target_address, + 128); + } + + if (FIB_NODE_INDEX_INVALID == fei) + { + /* The target address is not in the FIB */ + error0 = + ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN; + } + else + { + if (FIB_ENTRY_FLAG_LOCAL & + fib_entry_get_flags_for_source (fei, + FIB_SOURCE_INTERFACE)) + { + /* It's an address that belongs to one of our interfaces + * that's good. */ + } + else + if (fib_entry_is_sourced + (fei, FIB_SOURCE_IP6_ND_PROXY) || + fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND)) + { + /* The address was added by IPv6 Proxy ND config. + * We should only respond to these if the NS arrived on + * the link that has a matching covering prefix */ + } + else + { + error0 = + ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN; + } + } + } + } + + if (is_solicitation) + next0 = (error0 != ICMP6_ERROR_NONE + ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP + : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY); + else + { + next0 = 0; + error0 = error0 == ICMP6_ERROR_NONE ? + ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0; + } + + if (is_solicitation && error0 == ICMP6_ERROR_NONE) + { + vnet_sw_interface_t *sw_if0; + ethernet_interface_t *eth_if0; + ethernet_header_t *eth0; + + /* dst address is either source address or the all-nodes mcast addr */ + if (!ip6_sadd_unspecified) + ip0->dst_address = ip0->src_address; + else + ip6_set_reserved_multicast_address (&ip0->dst_address, + IP6_MULTICAST_SCOPE_link_local, + IP6_MULTICAST_GROUP_ID_all_hosts); + + ip0->src_address = h0->target_address; + ip0->hop_limit = 255; + h0->icmp.type = ICMP6_neighbor_advertisement; + + sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0); + ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE); + eth_if0 = + ethernet_get_interface (ðernet_main, sw_if0->hw_if_index); + if (eth_if0 && o0) + { + clib_memcpy (o0->ethernet_address, eth_if0->address, 6); + o0->header.type = + ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address; + } + + h0->advertisement_flags = clib_host_to_net_u32 + (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED + | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE); + + h0->icmp.checksum = 0; + h0->icmp.checksum = + ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, + &bogus_length); + ASSERT (bogus_length == 0); + + /* Reuse current MAC header, copy SMAC to DMAC and + * interface MAC to SMAC */ + vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0)); + eth0 = vlib_buffer_get_current (p0); + clib_memcpy (eth0->dst_address, eth0->src_address, 6); + if (eth_if0) + clib_memcpy (eth0->src_address, eth_if0->address, 6); + + /* Setup input and output sw_if_index for packet */ + ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0); + vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0; + vnet_buffer (p0)->sw_if_index[VLIB_RX] = + vnet_main.local_interface_sw_if_index; + + n_advertisements_sent++; + } + + p0->error = error_node->errors[error0]; + + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + /* Account for advertisements sent. */ + vlib_error_count (vm, error_node->node_index, + ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, + n_advertisements_sent); + + return frame->n_vectors; +} + +static const ethernet_interface_t * +ip6_nd_get_eth_itf (u32 sw_if_index) +{ + const vnet_sw_interface_t *sw; + + /* lookup radv container - ethernet interfaces only */ + sw = vnet_get_sup_sw_interface (vnet_get_main (), sw_if_index); + if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE) + return (ethernet_get_interface (ðernet_main, sw->hw_if_index)); + + return (NULL); +} + +/** + * @brief called when IP6 is enabled on a link. + * create and initialize router advertisement parameters with default + * values for this intfc + */ +static void +ip6_nd_link_enable (u32 sw_if_index) +{ + const ethernet_interface_t *eth; + ip6_nd_t *ind; + + eth = ip6_nd_get_eth_itf (sw_if_index); + + if (NULL == eth) + return; + + ASSERT (INDEX_INVALID == ip6_link_delegate_get (sw_if_index, + ip6_nd_delegate_id)); + + pool_get_zero (ip6_nd_pool, ind); + + ind->sw_if_index = sw_if_index; + + ip6_link_delegate_update (sw_if_index, ip6_nd_delegate_id, + ind - ip6_nd_pool); +} + +static void +ip6_nd_delegate_disable (index_t indi) +{ + ip6_nd_t *ind; + + ind = pool_elt_at_index (ip6_nd_pool, indi); + + pool_put (ip6_nd_pool, ind); +} + +static uword +icmp6_neighbor_solicitation (vlib_main_t * vm, + vlib_node_runtime_t * node, vlib_frame_t * frame) +{ + return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, + /* is_solicitation */ + 1); +} + +static uword +icmp6_neighbor_advertisement (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame, + /* is_solicitation */ + 0); +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) = +{ + .function = icmp6_neighbor_solicitation, + .name = "icmp6-neighbor-solicitation", + + .vector_size = sizeof (u32), + + .format_trace = format_icmp6_input_trace, + + .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT, + .next_nodes = { + [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop", + [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output", + }, +}; + +VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) = +{ + .function = icmp6_neighbor_advertisement, + .name = "icmp6-neighbor-advertisement", + + .vector_size = sizeof (u32), + + .format_trace = format_icmp6_input_trace, + + .n_next_nodes = 1, + .next_nodes = { + [0] = "ip6-drop", + }, +}; +/* *INDENT-ON* */ + +static u8 * +format_ip6_nd (u8 * s, va_list * args) +{ + CLIB_UNUSED (index_t indi) = va_arg (*args, index_t); + u32 indent = va_arg (*args, u32); + + s = format (s, "%UNeighbor Discovery: enabled\n", + format_white_space, indent); + + s = format (s, "%UICMP redirects are disabled\n", + format_white_space, indent + 2); + s = format (s, "%UICMP unreachables are not sent\n", + format_white_space, indent + 2); + s = format (s, "%UND DAD is disabled\n", format_white_space, indent + 2); + //s = format (s, "%UND reachable time is %d milliseconds\n",); + + return (s); +} + +/** + * VFT to act as an implementation of a neighbour protocol + */ +const static ip_neighbor_vft_t ip6_nd_impl_vft = { + .inv_proxy6_add = ip6_nd_proxy_add, + .inv_proxy6_del = ip6_nd_proxy_del, +}; + +/** + * VFT for registering as a delegate to an IP6 link + */ +const static ip6_link_delegate_vft_t ip6_nd_delegate_vft = { + .ildv_disable = ip6_nd_delegate_disable, + .ildv_enable = ip6_nd_link_enable, + .ildv_format = format_ip6_nd, +}; + +static clib_error_t * +ip6_nd_init (vlib_main_t * vm) +{ + icmp6_register_type (vm, ICMP6_neighbor_solicitation, + ip6_icmp_neighbor_solicitation_node.index); + icmp6_register_type (vm, ICMP6_neighbor_advertisement, + ip6_icmp_neighbor_advertisement_node.index); + + ip_neighbor_register (IP46_TYPE_IP6, &ip6_nd_impl_vft); + + ip6_nd_delegate_id = ip6_link_delegate_register (&ip6_nd_delegate_vft); + + return 0; +} + +/* *INDENT-OFF* */ +VLIB_INIT_FUNCTION (ip6_nd_init) = +{ + .runs_after = VLIB_INITS("icmp6_init"), +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ip6-nd/ip6_nd.h b/src/vnet/ip6-nd/ip6_nd.h new file mode 100644 index 00000000000..4dab7440b4a --- /dev/null +++ b/src/vnet/ip6-nd/ip6_nd.h @@ -0,0 +1,35 @@ +/* + * + * ip6_neighboor.h: ip6 neighbor structures + * + * Copyright (c) 2016 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IP6_ND_H__ +#define __IP6_ND_H__ + +#include <vnet/ip/ip6_packet.h> + +extern int ip6_nd_proxy_add (u32 sw_if_index, const ip6_address_t * addr); +extern int ip6_nd_proxy_del (u32 sw_if_index, const ip6_address_t * addr); + +#endif /* included_ip6_neighbor_h */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ip6-nd/ip6_nd_api.c b/src/vnet/ip6-nd/ip6_nd_api.c new file mode 100644 index 00000000000..65b3ee381b3 --- /dev/null +++ b/src/vnet/ip6-nd/ip6_nd_api.c @@ -0,0 +1,382 @@ +/* + *------------------------------------------------------------------ + * ip_api.c - vnet ip api + * + * Copyright (c) 2016 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *------------------------------------------------------------------ + */ + +#include <stddef.h> + +#include <vnet/ip6-nd/ip6_nd.h> +#include <vnet/ip6-nd/ip6_ra.h> + +#include <vnet/fib/fib_table.h> +#include <vnet/ip/ip_types_api.h> + +#include <vpp/app/version.h> + +#include <vlibapi/api.h> +#include <vlibmemory/api.h> + +/* define message IDs */ +#include <vnet/format_fns.h> +#include <vnet/ip6-nd/ip6_nd.api_enum.h> +#include <vnet/ip6-nd/ip6_nd.api_types.h> + +/** + * Base message ID fot the plugin + */ +static u32 ip6_nd_base_msg_id; +#define REPLY_MSG_ID_BASE ip6_nd_base_msg_id + +#include <vlibapi/api_helper_macros.h> + +static void +send_ip6nd_proxy_details (vl_api_registration_t * reg, + u32 context, + const ip46_address_t * addr, u32 sw_if_index) +{ + vl_api_ip6nd_proxy_details_t *mp; + + mp = vl_msg_api_alloc (sizeof (*mp)); + clib_memset (mp, 0, sizeof (*mp)); + mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS); + mp->context = context; + mp->sw_if_index = htonl (sw_if_index); + + ip6_address_encode (&addr->ip6, mp->ip); + + vl_api_send_msg (reg, (u8 *) mp); +} + +typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_ +{ + u32 *indices; +} api_ip6nd_proxy_fib_table_walk_ctx_t; + +static fib_table_walk_rc_t +api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg) +{ + api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg; + + if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY)) + { + vec_add1 (ctx->indices, fei); + } + + return (FIB_TABLE_WALK_CONTINUE); +} + +static void +vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp) +{ + ip6_main_t *im6 = &ip6_main; + fib_table_t *fib_table; + api_ip6nd_proxy_fib_table_walk_ctx_t ctx = { + .indices = NULL, + }; + fib_node_index_t *feip; + const fib_prefix_t *pfx; + vl_api_registration_t *reg; + + reg = vl_api_client_index_to_registration (mp->client_index); + if (!reg) + return; + + /* *INDENT-OFF* */ + pool_foreach (fib_table, im6->fibs, + ({ + fib_table_walk(fib_table->ft_index, + FIB_PROTOCOL_IP6, + api_ip6nd_proxy_fib_table_walk, + &ctx); + })); + /* *INDENT-ON* */ + + vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort); + + vec_foreach (feip, ctx.indices) + { + pfx = fib_entry_get_prefix (*feip); + + send_ip6nd_proxy_details (reg, + mp->context, + &pfx->fp_addr, + fib_entry_get_resolving_interface (*feip)); + } + + vec_free (ctx.indices); +} + +static void +vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp) +{ + vl_api_ip6nd_proxy_add_del_reply_t *rmp; + ip6_address_t ip6; + int rv = 0; + + VALIDATE_SW_IF_INDEX (mp); + + ip6_address_decode (mp->ip, &ip6); + if (mp->is_add) + rv = ip6_nd_proxy_add (ntohl (mp->sw_if_index), &ip6); + else + rv = ip6_nd_proxy_del (ntohl (mp->sw_if_index), &ip6); + + BAD_SW_IF_INDEX_LABEL; + REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY); +} + +static void + vl_api_sw_interface_ip6nd_ra_config_t_handler + (vl_api_sw_interface_ip6nd_ra_config_t * mp) +{ + vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp; + vlib_main_t *vm = vlib_get_main (); + int rv = 0; + u8 is_no, suppress, managed, other, ll_option, send_unicast, cease, + default_router; + + is_no = mp->is_no == 1; + suppress = mp->suppress == 1; + managed = mp->managed == 1; + other = mp->other == 1; + ll_option = mp->ll_option == 1; + send_unicast = mp->send_unicast == 1; + cease = mp->cease == 1; + default_router = mp->default_router == 1; + + VALIDATE_SW_IF_INDEX (mp); + + rv = ip6_ra_config (vm, ntohl (mp->sw_if_index), + suppress, managed, other, + ll_option, send_unicast, cease, + default_router, ntohl (mp->lifetime), + ntohl (mp->initial_count), + ntohl (mp->initial_interval), + ntohl (mp->max_interval), + ntohl (mp->min_interval), is_no); + + BAD_SW_IF_INDEX_LABEL; + + REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY); +} + +static void + vl_api_sw_interface_ip6nd_ra_prefix_t_handler + (vl_api_sw_interface_ip6nd_ra_prefix_t * mp) +{ + vlib_main_t *vm = vlib_get_main (); + vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp; + fib_prefix_t pfx; + int rv = 0; + u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink; + + VALIDATE_SW_IF_INDEX (mp); + + ip_prefix_decode (&mp->prefix, &pfx); + is_no = mp->is_no == 1; + use_default = mp->use_default == 1; + no_advertise = mp->no_advertise == 1; + off_link = mp->off_link == 1; + no_autoconfig = mp->no_autoconfig == 1; + no_onlink = mp->no_onlink == 1; + + rv = ip6_ra_prefix (vm, ntohl (mp->sw_if_index), + &pfx.fp_addr.ip6, + pfx.fp_len, use_default, + ntohl (mp->val_lifetime), + ntohl (mp->pref_lifetime), no_advertise, + off_link, no_autoconfig, no_onlink, is_no); + + BAD_SW_IF_INDEX_LABEL; + REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY); +} + +static void + vl_api_ip6nd_send_router_solicitation_t_handler + (vl_api_ip6nd_send_router_solicitation_t * mp) +{ + vl_api_ip6nd_send_router_solicitation_reply_t *rmp; + icmp6_send_router_solicitation_params_t params; + vlib_main_t *vm = vlib_get_main (); + int rv = 0; + + VALIDATE_SW_IF_INDEX (mp); + + BAD_SW_IF_INDEX_LABEL; + REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY); + + if (rv != 0) + return; + + params.irt = ntohl (mp->irt); + params.mrt = ntohl (mp->mrt); + params.mrc = ntohl (mp->mrc); + params.mrd = ntohl (mp->mrd); + + icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop, + ¶ms); +} + +static void +ip6_ra_handle_report (const ip6_ra_report_t * rap) +{ + /* *INDENT-OFF* */ + vpe_client_registration_t *rp; + + pool_foreach(rp, vpe_api_main.ip6_ra_events_registrations, + ({ + vl_api_registration_t *vl_reg; + + vl_reg = vl_api_client_index_to_registration (rp->client_index); + + if (vl_reg && vl_api_can_send_msg (vl_reg)) + { + vl_api_ip6_ra_prefix_info_t *prefix; + vl_api_ip6_ra_event_t *event; + + u32 event_size = (sizeof (vl_api_ip6_ra_event_t) + + vec_len (rap->prefixes) * + sizeof (vl_api_ip6_ra_prefix_info_t)); + event = vl_msg_api_alloc_zero (event_size); + + event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT + REPLY_MSG_ID_BASE); + event->client_index = rp->client_index; + event->pid = rp->client_pid; + event->sw_if_index = clib_host_to_net_u32 (rap->sw_if_index); + + ip6_address_encode (&rap->router_address, + event->router_addr); + + event->current_hop_limit = rap->current_hop_limit; + event->flags = rap->flags; + event->router_lifetime_in_sec = + clib_host_to_net_u16 (rap->router_lifetime_in_sec); + event->neighbor_reachable_time_in_msec = + clib_host_to_net_u32 (rap->neighbor_reachable_time_in_msec); + event->time_in_msec_between_retransmitted_neighbor_solicitations = + clib_host_to_net_u32 (rap->time_in_msec_between_retransmitted_neighbor_solicitations); + event->n_prefixes = clib_host_to_net_u32 (vec_len (rap->prefixes)); + + prefix = event->prefixes; + // (typeof (prefix)) event->prefixes; + u32 j; + for (j = 0; j < vec_len (rap->prefixes); j++) + { + ra_report_prefix_info_t *info = &rap->prefixes[j]; + ip_prefix_encode(&info->prefix, &prefix->prefix); + prefix->flags = info->flags; + prefix->valid_time = clib_host_to_net_u32 (info->valid_time); + prefix->preferred_time = + clib_host_to_net_u32 (info->preferred_time); + prefix++; + } + + vl_api_send_msg (vl_reg, (u8 *) event); + } + })); + /* *INDENT-ON* */ +} + +static void +vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp) +{ + vpe_api_main_t *am = &vpe_api_main; + vl_api_want_ip6_ra_events_reply_t *rmp; + int rv = 0, had_reg, have_reg; + + had_reg = hash_elts (am->ip6_ra_events_registration_hash); + uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index); + vpe_client_registration_t *rp; + if (p) + { + if (mp->enable) + { + clib_warning ("pid %d: already enabled...", ntohl (mp->pid)); + rv = VNET_API_ERROR_INVALID_REGISTRATION; + goto reply; + } + else + { + rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]); + pool_put (am->ip6_ra_events_registrations, rp); + hash_unset (am->ip6_ra_events_registration_hash, mp->client_index); + goto reply; + } + } + if (mp->enable == 0) + { + clib_warning ("pid %d: already disabled...", ntohl (mp->pid)); + rv = VNET_API_ERROR_INVALID_REGISTRATION; + goto reply; + } + pool_get (am->ip6_ra_events_registrations, rp); + rp->client_index = mp->client_index; + rp->client_pid = ntohl (mp->pid); + hash_set (am->ip6_ra_events_registration_hash, rp->client_index, + rp - am->ip6_ra_events_registrations); + +reply: + have_reg = hash_elts (am->ip6_ra_events_registration_hash); + + if (!had_reg && have_reg) + ip6_ra_report_register (ip6_ra_handle_report); + else if (had_reg && !have_reg) + ip6_ra_report_unregister (ip6_ra_handle_report); + + REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY); +} + +static clib_error_t * +want_ip6_ra_events_reaper (u32 client_index) +{ + vpe_api_main_t *am = &vpe_api_main; + vpe_client_registration_t *rp; + uword *p; + + p = hash_get (am->ip6_ra_events_registration_hash, client_index); + + if (p) + { + rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]); + pool_put (am->ip6_ra_events_registrations, rp); + hash_unset (am->ip6_ra_events_registration_hash, client_index); + } + return (NULL); +} + +VL_MSG_API_REAPER_FUNCTION (want_ip6_ra_events_reaper); + +#include <vnet/ip6-nd/ip6_nd.api.c> + +static clib_error_t * +ip6_nd_api_init (vlib_main_t * vm) +{ + /* Ask for a correctly-sized block of API message decode slots */ + ip6_nd_base_msg_id = setup_message_id_table (); + + return 0; +} + +VLIB_INIT_FUNCTION (ip6_nd_api_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ip6-nd/ip6_nd_proxy.c b/src/vnet/ip6-nd/ip6_nd_proxy.c new file mode 100644 index 00000000000..110847fff84 --- /dev/null +++ b/src/vnet/ip6-nd/ip6_nd_proxy.c @@ -0,0 +1,127 @@ +/* + * ip/ip6_neighbor.c: IP6 neighbor handling + * + * Copyright (c) 2010 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vnet/ip6-nd/ip6_nd.h> +#include <vnet/ip-neighbor/ip_neighbor.h> + +#include <vnet/fib/ip6_fib.h> + +static int +ip6_nd_proxy_add_del (u32 sw_if_index, const ip6_address_t * addr, u8 is_del) +{ + /* *INDENT-OFF* */ + u32 fib_index; + fib_prefix_t pfx = { + .fp_len = 128, + .fp_proto = FIB_PROTOCOL_IP6, + .fp_addr = { + .ip6 = *addr, + }, + }; + ip46_address_t nh = { + .ip6 = *addr, + }; + /* *INDENT-ON* */ + + fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index); + + if (~0 == fib_index) + return VNET_API_ERROR_NO_SUCH_FIB; + + if (is_del) + { + fib_table_entry_path_remove (fib_index, + &pfx, + FIB_SOURCE_IP6_ND_PROXY, + DPO_PROTO_IP6, + &nh, + sw_if_index, + ~0, 1, FIB_ROUTE_PATH_FLAG_NONE); + /* flush the ND cache of this address if it's there */ + ip_neighbor_del (&nh, IP46_TYPE_IP6, sw_if_index); + } + else + { + fib_table_entry_path_add (fib_index, + &pfx, + FIB_SOURCE_IP6_ND_PROXY, + FIB_ENTRY_FLAG_NONE, + DPO_PROTO_IP6, + &nh, + sw_if_index, + ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE); + } + return (0); +} + +int +ip6_nd_proxy_add (u32 sw_if_index, const ip6_address_t * addr) +{ + return (ip6_nd_proxy_add_del (sw_if_index, addr, 0)); +} + +int +ip6_nd_proxy_del (u32 sw_if_index, const ip6_address_t * addr) +{ + return (ip6_nd_proxy_add_del (sw_if_index, addr, 1)); +} + +static clib_error_t * +set_ip6_nd_proxy_cmd (vlib_main_t * vm, + unformat_input_t * input, vlib_cli_command_t * cmd) +{ + vnet_main_t *vnm = vnet_get_main (); + clib_error_t *error = 0; + ip6_address_t addr; + u32 sw_if_index; + u8 is_del = 0; + + if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) + { + /* get the rest of the command */ + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "%U", unformat_ip6_address, &addr)) + break; + else if (unformat (input, "delete") || unformat (input, "del")) + is_del = 1; + else + return (unformat_parse_error (input)); + } + } + + ip6_nd_proxy_add_del (sw_if_index, &addr, is_del); + + return error; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) = +{ + .path = "set ip6 nd proxy", + .short_help = "set ip6 nd proxy <HOST> <INTERFACE>", + .function = set_ip6_nd_proxy_cmd, +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ip6-nd/ip6_nd_test.c b/src/vnet/ip6-nd/ip6_nd_test.c new file mode 100644 index 00000000000..5ca37029a76 --- /dev/null +++ b/src/vnet/ip6-nd/ip6_nd_test.c @@ -0,0 +1,330 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vat/vat.h> +#include <vlibapi/api.h> +#include <vlibmemory/api.h> +#include <vppinfra/error.h> +#include <vpp/api/types.h> + +#include <vnet/ip/ip_format_fns.h> +#include <vnet/ethernet/ethernet_format_fns.h> + +/* define message IDs */ +#include <ip6-nd/ip6_nd.api_enum.h> +#include <ip6-nd/ip6_nd.api_types.h> +#include <vpp/api/vpe.api_types.h> + +typedef struct +{ + /* API message ID base */ + u16 msg_id_base; + u32 ping_id; + vat_main_t *vat_main; +} ip6_nd_test_main_t; + +ip6_nd_test_main_t ip6_nd_test_main; + +#define __plugin_msg_base ip6_nd_test_main.msg_id_base +#include <vlibapi/vat_helper_macros.h> + +static int +api_want_ip6_ra_events (vat_main_t * vam) +{ + return -1; +} + +static int +api_ip6nd_send_router_solicitation (vat_main_t * vam) +{ + return -1; +} + +static int +api_ip6nd_proxy_add_del (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + vl_api_ip6nd_proxy_add_del_t *mp; + u32 sw_if_index = ~0; + u8 v6_address_set = 0; + vl_api_ip6_address_t v6address; + u8 is_add = 1; + int ret; + + /* Parse args required to build the message */ + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index)) + ; + else if (unformat (i, "sw_if_index %d", &sw_if_index)) + ; + else if (unformat (i, "%U", unformat_vl_api_ip6_address, &v6address)) + v6_address_set = 1; + if (unformat (i, "del")) + is_add = 0; + else + { + clib_warning ("parse error '%U'", format_unformat_error, i); + return -99; + } + } + + if (sw_if_index == ~0) + { + errmsg ("missing interface name or sw_if_index"); + return -99; + } + if (!v6_address_set) + { + errmsg ("no address set"); + return -99; + } + + /* Construct the API message */ + M (IP6ND_PROXY_ADD_DEL, mp); + + mp->is_add = is_add; + mp->sw_if_index = ntohl (sw_if_index); + clib_memcpy (mp->ip, v6address, sizeof (v6address)); + + /* send it... */ + S (mp); + + /* Wait for a reply, return good/bad news */ + W (ret); + return ret; +} + +static int +api_ip6nd_proxy_dump (vat_main_t * vam) +{ + vl_api_ip6nd_proxy_dump_t *mp; + vl_api_control_ping_t *mp_ping; + int ret; + + M (IP6ND_PROXY_DUMP, mp); + + S (mp); + + /* Use a control ping for synchronization */ + /* Use a control ping for synchronization */ + mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping)); + mp_ping->_vl_msg_id = htons (ip6_nd_test_main.ping_id); + mp_ping->client_index = vam->my_client_index; + vam->result_ready = 0; + + S (mp_ping); + + W (ret); + return ret; +} + +static void vl_api_ip6nd_proxy_details_t_handler + (vl_api_ip6nd_proxy_details_t * mp) +{ + vat_main_t *vam = &vat_main; + + print (vam->ofp, "host %U sw_if_index %d", + format_vl_api_ip6_address, mp->ip, ntohl (mp->sw_if_index)); +} + +static int +api_sw_interface_ip6nd_ra_prefix (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + vl_api_sw_interface_ip6nd_ra_prefix_t *mp; + u32 sw_if_index; + u8 sw_if_index_set = 0; + u8 v6_address_set = 0; + vl_api_prefix_t pfx; + u8 use_default = 0; + u8 no_advertise = 0; + u8 off_link = 0; + u8 no_autoconfig = 0; + u8 no_onlink = 0; + u8 is_no = 0; + u32 val_lifetime = 0; + u32 pref_lifetime = 0; + int ret; + + /* Parse args required to build the message */ + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index)) + sw_if_index_set = 1; + else if (unformat (i, "sw_if_index %d", &sw_if_index)) + sw_if_index_set = 1; + else if (unformat (i, "%U", unformat_vl_api_prefix, &pfx)) + v6_address_set = 1; + else if (unformat (i, "val_life %d", &val_lifetime)) + ; + else if (unformat (i, "pref_life %d", &pref_lifetime)) + ; + else if (unformat (i, "def")) + use_default = 1; + else if (unformat (i, "noadv")) + no_advertise = 1; + else if (unformat (i, "offl")) + off_link = 1; + else if (unformat (i, "noauto")) + no_autoconfig = 1; + else if (unformat (i, "nolink")) + no_onlink = 1; + else if (unformat (i, "isno")) + is_no = 1; + else + { + clib_warning ("parse error '%U'", format_unformat_error, i); + return -99; + } + } + + if (sw_if_index_set == 0) + { + errmsg ("missing interface name or sw_if_index"); + return -99; + } + if (!v6_address_set) + { + errmsg ("no address set"); + return -99; + } + + /* Construct the API message */ + M (SW_INTERFACE_IP6ND_RA_PREFIX, mp); + + mp->sw_if_index = ntohl (sw_if_index); + clib_memcpy (&mp->prefix, &pfx, sizeof (pfx)); + mp->use_default = use_default; + mp->no_advertise = no_advertise; + mp->off_link = off_link; + mp->no_autoconfig = no_autoconfig; + mp->no_onlink = no_onlink; + mp->is_no = is_no; + mp->val_lifetime = ntohl (val_lifetime); + mp->pref_lifetime = ntohl (pref_lifetime); + + /* send it... */ + S (mp); + + /* Wait for a reply, return good/bad news */ + W (ret); + return ret; +} + +static int +api_sw_interface_ip6nd_ra_config (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + vl_api_sw_interface_ip6nd_ra_config_t *mp; + u32 sw_if_index; + u8 sw_if_index_set = 0; + u8 suppress = 0; + u8 managed = 0; + u8 other = 0; + u8 ll_option = 0; + u8 send_unicast = 0; + u8 cease = 0; + u8 is_no = 0; + u8 default_router = 0; + u32 max_interval = 0; + u32 min_interval = 0; + u32 lifetime = 0; + u32 initial_count = 0; + u32 initial_interval = 0; + int ret; + + + /* Parse args required to build the message */ + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index)) + sw_if_index_set = 1; + else if (unformat (i, "sw_if_index %d", &sw_if_index)) + sw_if_index_set = 1; + else if (unformat (i, "maxint %d", &max_interval)) + ; + else if (unformat (i, "minint %d", &min_interval)) + ; + else if (unformat (i, "life %d", &lifetime)) + ; + else if (unformat (i, "count %d", &initial_count)) + ; + else if (unformat (i, "interval %d", &initial_interval)) + ; + else if (unformat (i, "suppress") || unformat (i, "surpress")) + suppress = 1; + else if (unformat (i, "managed")) + managed = 1; + else if (unformat (i, "other")) + other = 1; + else if (unformat (i, "ll")) + ll_option = 1; + else if (unformat (i, "send")) + send_unicast = 1; + else if (unformat (i, "cease")) + cease = 1; + else if (unformat (i, "isno")) + is_no = 1; + else if (unformat (i, "def")) + default_router = 1; + else + { + clib_warning ("parse error '%U'", format_unformat_error, i); + return -99; + } + } + + if (sw_if_index_set == 0) + { + errmsg ("missing interface name or sw_if_index"); + return -99; + } + + /* Construct the API message */ + M (SW_INTERFACE_IP6ND_RA_CONFIG, mp); + + mp->sw_if_index = ntohl (sw_if_index); + mp->max_interval = ntohl (max_interval); + mp->min_interval = ntohl (min_interval); + mp->lifetime = ntohl (lifetime); + mp->initial_count = ntohl (initial_count); + mp->initial_interval = ntohl (initial_interval); + mp->suppress = suppress; + mp->managed = managed; + mp->other = other; + mp->ll_option = ll_option; + mp->send_unicast = send_unicast; + mp->cease = cease; + mp->is_no = is_no; + mp->default_router = default_router; + + /* send it... */ + S (mp); + + /* Wait for a reply, return good/bad news */ + W (ret); + return ret; +} + +#include <ip6-nd/ip6_nd.api_test.c> + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ip6-nd/ip6_ra.c b/src/vnet/ip6-nd/ip6_ra.c new file mode 100644 index 00000000000..ebc2c4be417 --- /dev/null +++ b/src/vnet/ip6-nd/ip6_ra.c @@ -0,0 +1,2261 @@ +/* + * ip/ip6_neighbor.c: IP6 neighbor handling + * + * Copyright (c) 2010 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vnet/ip6-nd/ip6_ra.h> + +#include <vnet/ip/ip.h> +#include <vnet/ip-neighbor/ip_neighbor_dp.h> + +#include <vnet/fib/ip6_fib.h> +#include <vnet/ip/ip6_link.h> + +/** + * @file + * @brief IPv6 Router Advertisements. + * + * The files contains the API and CLI code for managing IPv6 RAs + */ + +/* *INDENT-OFF* */ +/* Router solicitation packet format for ethernet. */ +typedef CLIB_PACKED (struct +{ + ip6_header_t ip; + icmp6_neighbor_discovery_header_t neighbor; + icmp6_neighbor_discovery_ethernet_link_layer_address_option_t + link_layer_option; +}) icmp6_router_solicitation_header_t; + +/* router advertisement packet format for ethernet. */ +typedef CLIB_PACKED (struct +{ + ip6_header_t ip; + icmp6_router_advertisement_header_t router; + icmp6_neighbor_discovery_ethernet_link_layer_address_option_t + link_layer_option; + icmp6_neighbor_discovery_mtu_option_t mtu_option; + icmp6_neighbor_discovery_prefix_information_option_t + prefix[0]; +}) icmp6_router_advertisement_packet_t; +/* *INDENT-ON* */ + +#define DEF_MAX_RADV_INTERVAL 200 +#define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL +#define DEF_CURR_HOP_LIMIT 64 +#define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL +#define MAX_DEF_RTR_LIFETIME 9000 + +#define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */ +#define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */ +#define MIN_DELAY_BETWEEN_RAS 3 /* seconds */ +#define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */ +#define MAX_RA_DELAY_TIME .5 /* seconds */ + +/* advertised prefix option */ +typedef struct +{ + /* basic advertised information */ + ip6_address_t prefix; + u8 prefix_len; + int adv_on_link_flag; + int adv_autonomous_flag; + u32 adv_valid_lifetime_in_secs; + u32 adv_pref_lifetime_in_secs; + + /* advertised values are computed from these times if decrementing */ + f64 valid_lifetime_expires; + f64 pref_lifetime_expires; + + /* local information */ + int enabled; + int deprecated_prefix_flag; + int decrement_lifetime_flag; + +#define MIN_ADV_VALID_LIFETIME 7203 /* seconds */ +#define DEF_ADV_VALID_LIFETIME 2592000 +#define DEF_ADV_PREF_LIFETIME 604800 + + /* extensions are added here, mobile, DNS etc.. */ +} ip6_radv_prefix_t; + +typedef struct ip6_ra_t_ +{ + /* advertised config information, zero means unspecified */ + u8 curr_hop_limit; + int adv_managed_flag; + int adv_other_flag; + u16 adv_router_lifetime_in_sec; + u32 adv_neighbor_reachable_time_in_msec; + u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations; + + /* mtu option */ + u32 adv_link_mtu; + + /* local information */ + u32 sw_if_index; + int send_radv; /* radv on/off on this interface - set by config */ + int cease_radv; /* we are ceasing to send - set byf config */ + int send_unicast; + int adv_link_layer_address; + int prefix_option; + int failed_device_check; + int ref_count; + + /* prefix option */ + ip6_radv_prefix_t *adv_prefixes_pool; + + /* Hash table mapping address to index in interface advertised prefix pool. */ + mhash_t address_to_prefix_index; + + f64 max_radv_interval; + f64 min_radv_interval; + f64 min_delay_between_radv; + f64 max_delay_between_radv; + f64 max_rtr_default_lifetime; + + f64 last_radv_time; + f64 last_multicast_time; + f64 next_multicast_time; + + + u32 initial_adverts_count; + f64 initial_adverts_interval; + u32 initial_adverts_sent; + + /* stats */ + u32 n_advertisements_sent; + u32 n_solicitations_rcvd; + u32 n_solicitations_dropped; + + /* router solicitations sending state */ + u8 keep_sending_rs; /* when true then next fields are valid */ + icmp6_send_router_solicitation_params_t params; + f64 sleep_interval; + f64 due_time; + u32 n_left; + f64 start_time; + vlib_buffer_t *buffer; + + u32 seed; + +} ip6_ra_t; + +static ip6_link_delegate_id_t ip6_ra_delegate_id; +static ip6_ra_t *ip6_ra_pool; + + +/* vector of registered RA report listeners */ +static ip6_ra_report_notify_t *ip6_ra_listeners; + +static int ip6_ra_publish (ip6_ra_report_t * r); + +void +ip6_ra_report_register (ip6_ra_report_notify_t fn) +{ + ip6_ra_report_notify_t *listener; + vec_foreach (listener, ip6_ra_listeners) + { + if (*listener == fn) + return; + } + + vec_add1 (ip6_ra_listeners, fn); +} + +void +ip6_ra_report_unregister (ip6_ra_report_notify_t fn) +{ + u32 ii; + + vec_foreach_index (ii, ip6_ra_listeners) + { + if (ip6_ra_listeners[ii] == fn) + { + vec_del1 (ip6_ra_listeners, ii); + break; + } + } +} + +static inline ip6_ra_t * +ip6_ra_get_itf (u32 sw_if_index) +{ + index_t rai; + + rai = ip6_link_delegate_get (sw_if_index, ip6_ra_delegate_id); + + if (INDEX_INVALID != rai) + return (pool_elt_at_index (ip6_ra_pool, rai)); + + return (NULL); +} + +/* for "syslogging" - use elog for now */ +#define foreach_log_level \ + _ (DEBUG, "DEBUG") \ + _ (INFO, "INFORMATION") \ + _ (NOTICE, "NOTICE") \ + _ (WARNING, "WARNING") \ + _ (ERR, "ERROR") \ + _ (CRIT, "CRITICAL") \ + _ (ALERT, "ALERT") \ + _ (EMERG, "EMERGENCY") + +typedef enum +{ +#define _(f,s) LOG_##f, + foreach_log_level +#undef _ +} log_level_t; + +static char *log_level_strings[] = { +#define _(f,s) s, + foreach_log_level +#undef _ +}; + +static int logmask = 1 << LOG_DEBUG; + +static void +ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...) +{ + /* just use elog for now */ + u8 *what; + va_list va; + + if ((priority > LOG_EMERG) || !(logmask & (1 << priority))) + return; + + va_start (va, fmt); + if (fmt) + { + what = va_format (0, fmt, &va); + + ELOG_TYPE_DECLARE (e) = + { + .format = "ip6 nd: (%s): %s",.format_args = "T4T4",}; + struct + { + u32 s[2]; + } *ed; + ed = ELOG_DATA (&vm->elog_main, e); + ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]); + ed->s[1] = elog_string (&vm->elog_main, (char *) what); + } + va_end (va); + return; +} + +/* ipv6 neighbor discovery - router advertisements */ +typedef enum +{ + ICMP6_ROUTER_SOLICITATION_NEXT_DROP, + ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW, + ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX, + ICMP6_ROUTER_SOLICITATION_N_NEXT, +} icmp6_router_solicitation_or_advertisement_next_t; + +static_always_inline uword +icmp6_router_solicitation (vlib_main_t * vm, + vlib_node_runtime_t * node, vlib_frame_t * frame) +{ + vnet_main_t *vnm = vnet_get_main (); + ip6_main_t *im = &ip6_main; + uword n_packets = frame->n_vectors; + u32 *from, *to_next; + u32 n_left_from, n_left_to_next, next_index; + u32 n_advertisements_sent = 0; + int bogus_length; + + icmp6_neighbor_discovery_option_type_t option_type; + + vlib_node_runtime_t *error_node = + vlib_node_get_runtime (vm, ip6_icmp_input_node.index); + + from = vlib_frame_vector_args (frame); + n_left_from = n_packets; + next_index = node->cached_next_index; + + if (node->flags & VLIB_NODE_FLAG_TRACE) + vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors, + /* stride */ 1, + sizeof (icmp6_input_trace_t)); + + /* source may append his LL address */ + option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address; + + while (n_left_from > 0) + { + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + while (n_left_from > 0 && n_left_to_next > 0) + { + vlib_buffer_t *p0; + ip6_header_t *ip0; + ip6_ra_t *radv_info = NULL; + + icmp6_neighbor_discovery_header_t *h0; + icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0; + + u32 bi0, options_len0, sw_if_index0, next0, error0; + u32 is_solicitation = 1, is_dropped = 0; + u32 is_unspecified, is_link_local; + + bi0 = to_next[0] = from[0]; + + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + p0 = vlib_get_buffer (vm, bi0); + ip0 = vlib_buffer_get_current (p0); + h0 = ip6_next_header (ip0); + options_len0 = + clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]); + is_unspecified = ip6_address_is_unspecified (&ip0->src_address); + is_link_local = + ip6_address_is_link_local_unicast (&ip0->src_address); + + error0 = ICMP6_ERROR_NONE; + sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX]; + + /* check if solicitation (not from nd_timer node) */ + if (ip6_address_is_unspecified (&ip0->dst_address)) + is_solicitation = 0; + + /* Check that source address is unspecified, link-local or else on-link. */ + if (!is_unspecified && !is_link_local) + { + u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0); + + if (ADJ_INDEX_INVALID != src_adj_index0) + { + ip_adjacency_t *adj0 = adj_get (src_adj_index0); + + error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0 + ? + ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK + : error0); + } + else + { + error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK; + } + } + + /* check for source LL option and process */ + o0 = (void *) (h0 + 1); + o0 = ((options_len0 == 8 + && o0->header.type == option_type + && o0->header.n_data_u64s == 1) ? o0 : 0); + + /* if src address unspecified IGNORE any options */ + if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 && + !is_unspecified && !is_link_local)) + { + ip_neighbor_learn_t learn = { + .type = IP46_TYPE_IP6, + .sw_if_index = sw_if_index0, + .ip.ip6 = ip0->src_address, + }; + memcpy (&learn.mac, o0->ethernet_address, sizeof (learn.mac)); + ip_neighbor_learn_dp (&learn); + } + + /* default is to drop */ + next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP; + + if (error0 == ICMP6_ERROR_NONE) + { + vnet_sw_interface_t *sw_if0; + ethernet_interface_t *eth_if0; + u32 adj_index0; + + sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0); + ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE); + eth_if0 = + ethernet_get_interface (ðernet_main, sw_if0->hw_if_index); + + /* only support ethernet interface type for now */ + error0 = + (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF + : error0; + + if (error0 == ICMP6_ERROR_NONE) + { + /* adjust the sizeof the buffer to just include the ipv6 header */ + p0->current_length -= + (options_len0 + + sizeof (icmp6_neighbor_discovery_header_t)); + + radv_info = ip6_ra_get_itf (sw_if_index0); + + error0 = ((!radv_info) ? + ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : + error0); + + if (error0 == ICMP6_ERROR_NONE) + { + f64 now = vlib_time_now (vm); + + /* for solicited adverts - need to rate limit */ + if (is_solicitation) + { + if (0 != radv_info->last_radv_time && + (now - radv_info->last_radv_time) < + MIN_DELAY_BETWEEN_RAS) + is_dropped = 1; + else + radv_info->last_radv_time = now; + } + + /* send now */ + icmp6_router_advertisement_header_t rh; + + rh.icmp.type = ICMP6_router_advertisement; + rh.icmp.code = 0; + rh.icmp.checksum = 0; + + rh.current_hop_limit = radv_info->curr_hop_limit; + rh.router_lifetime_in_sec = + clib_host_to_net_u16 + (radv_info->adv_router_lifetime_in_sec); + rh. + time_in_msec_between_retransmitted_neighbor_solicitations + = + clib_host_to_net_u32 (radv_info-> + adv_time_in_msec_between_retransmitted_neighbor_solicitations); + rh.neighbor_reachable_time_in_msec = + clib_host_to_net_u32 (radv_info-> + adv_neighbor_reachable_time_in_msec); + + rh.flags = + (radv_info->adv_managed_flag) ? + ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP : + 0; + rh.flags |= + ((radv_info->adv_other_flag) ? + ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP : + 0); + + + u16 payload_length = + sizeof (icmp6_router_advertisement_header_t); + + if (vlib_buffer_add_data + (vm, &bi0, (void *) &rh, + sizeof (icmp6_router_advertisement_header_t))) + { + /* buffer allocation failed, drop the pkt */ + error0 = ICMP6_ERROR_ALLOC_FAILURE; + goto drop0; + } + + if (radv_info->adv_link_layer_address) + { + icmp6_neighbor_discovery_ethernet_link_layer_address_option_t + h; + + h.header.type = + ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address; + h.header.n_data_u64s = 1; + + /* copy ll address */ + clib_memcpy (&h.ethernet_address[0], + eth_if0->address, 6); + + if (vlib_buffer_add_data + (vm, &bi0, (void *) &h, + sizeof + (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t))) + { + error0 = ICMP6_ERROR_ALLOC_FAILURE; + goto drop0; + } + + payload_length += + sizeof + (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t); + } + + /* add MTU option */ + if (radv_info->adv_link_mtu) + { + icmp6_neighbor_discovery_mtu_option_t h; + + h.unused = 0; + h.mtu = + clib_host_to_net_u32 (radv_info->adv_link_mtu); + h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu; + h.header.n_data_u64s = 1; + + payload_length += + sizeof (icmp6_neighbor_discovery_mtu_option_t); + + if (vlib_buffer_add_data + (vm, &bi0, (void *) &h, + sizeof + (icmp6_neighbor_discovery_mtu_option_t))) + { + error0 = ICMP6_ERROR_ALLOC_FAILURE; + goto drop0; + } + } + + /* add advertised prefix options */ + ip6_radv_prefix_t *pr_info; + + /* *INDENT-OFF* */ + pool_foreach (pr_info, radv_info->adv_prefixes_pool, + ({ + if(pr_info->enabled && + (!pr_info->decrement_lifetime_flag + || (pr_info->pref_lifetime_expires >0))) + { + /* advertise this prefix */ + icmp6_neighbor_discovery_prefix_information_option_t h; + + h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information; + h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3); + + h.dst_address_length = pr_info->prefix_len; + + h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0; + h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0; + + if(radv_info->cease_radv && pr_info->deprecated_prefix_flag) + { + h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME); + h.preferred_time = 0; + } + else + { + if(pr_info->decrement_lifetime_flag) + { + pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ? + (pr_info->valid_lifetime_expires - now) : 0; + + pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ? + (pr_info->pref_lifetime_expires - now) : 0; + } + + h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs); + h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ; + } + h.unused = 0; + + clib_warning ("Prefix %U valid %u preferred %u", + format_ip6_address, &pr_info->prefix, + clib_net_to_host_u32 (h.valid_time), + clib_net_to_host_u32 (h.preferred_time)); + + if (h.valid_time == 0) + clib_warning ("WARNING: valid_time 0!!!"); + + clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t)); + + payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t); + + if (vlib_buffer_add_data + (vm, &bi0, (void *)&h, + sizeof(icmp6_neighbor_discovery_prefix_information_option_t))) + { + error0 = ICMP6_ERROR_ALLOC_FAILURE; + goto drop0; + } + + } + })); + /* *INDENT-ON* */ + + /* add additional options before here */ + + /* finish building the router advertisement... */ + if (!is_unspecified && radv_info->send_unicast) + { + ip0->dst_address = ip0->src_address; + } + else + { + /* target address is all-nodes mcast addr */ + ip6_set_reserved_multicast_address + (&ip0->dst_address, + IP6_MULTICAST_SCOPE_link_local, + IP6_MULTICAST_GROUP_ID_all_hosts); + } + + /* source address MUST be the link-local address */ + ip6_address_copy (&ip0->src_address, + ip6_get_link_local_address + (radv_info->sw_if_index)); + + ip0->hop_limit = 255; + ip0->payload_length = + clib_host_to_net_u16 (payload_length); + + icmp6_router_advertisement_header_t *rh0 = + (icmp6_router_advertisement_header_t *) (ip0 + 1); + rh0->icmp.checksum = + ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, + &bogus_length); + ASSERT (bogus_length == 0); + + /* setup output if and adjacency */ + vnet_buffer (p0)->sw_if_index[VLIB_RX] = + vnet_main.local_interface_sw_if_index; + + if (is_solicitation) + { + ethernet_header_t *eth0; + /* Reuse current MAC header, copy SMAC to DMAC and + * interface MAC to SMAC */ + vlib_buffer_reset (p0); + eth0 = vlib_buffer_get_current (p0); + clib_memcpy (eth0->dst_address, eth0->src_address, + 6); + clib_memcpy (eth0->src_address, eth_if0->address, + 6); + next0 = + is_dropped ? next0 : + ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX; + vnet_buffer (p0)->sw_if_index[VLIB_TX] = + sw_if_index0; + } + else + { + adj_index0 = ip6_link_get_mcast_adj (sw_if_index0); + if (adj_index0 == INDEX_INVALID) + error0 = ICMP6_ERROR_DST_LOOKUP_MISS; + else + { + next0 = + is_dropped ? next0 : + ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW; + vnet_buffer (p0)->ip.adj_index[VLIB_TX] = + adj_index0; + } + } + p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; + + radv_info->n_solicitations_dropped += is_dropped; + radv_info->n_solicitations_rcvd += is_solicitation; + + if ((error0 == ICMP6_ERROR_NONE) && !is_dropped) + { + radv_info->n_advertisements_sent++; + n_advertisements_sent++; + } + } + } + } + + drop0: + p0->error = error_node->errors[error0]; + + if (error0 != ICMP6_ERROR_NONE) + vlib_error_count (vm, error_node->node_index, error0, 1); + + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + /* Account for router advertisements sent. */ + vlib_error_count (vm, error_node->node_index, + ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX, + n_advertisements_sent); + + return frame->n_vectors; +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) = +{ + .function = icmp6_router_solicitation, + .name = "icmp6-router-solicitation", + + .vector_size = sizeof (u32), + + .format_trace = format_icmp6_input_trace, + + .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT, + .next_nodes = { + [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop", + [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast", + [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output", + }, +}; +/* *INDENT-ON* */ + + /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */ +static_always_inline uword +icmp6_router_advertisement (vlib_main_t * vm, + vlib_node_runtime_t * node, vlib_frame_t * frame) +{ + vnet_main_t *vnm = vnet_get_main (); + uword n_packets = frame->n_vectors; + u32 *from, *to_next; + u32 n_left_from, n_left_to_next, next_index; + u32 n_advertisements_rcvd = 0; + + vlib_node_runtime_t *error_node = + vlib_node_get_runtime (vm, ip6_icmp_input_node.index); + + from = vlib_frame_vector_args (frame); + n_left_from = n_packets; + next_index = node->cached_next_index; + + if (node->flags & VLIB_NODE_FLAG_TRACE) + vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors, + /* stride */ 1, + sizeof (icmp6_input_trace_t)); + + while (n_left_from > 0) + { + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + while (n_left_from > 0 && n_left_to_next > 0) + { + vlib_buffer_t *p0; + ip6_header_t *ip0; + ip6_ra_t *radv_info = 0; + icmp6_router_advertisement_header_t *h0; + u32 bi0, options_len0, sw_if_index0, next0, error0; + + bi0 = to_next[0] = from[0]; + + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + p0 = vlib_get_buffer (vm, bi0); + ip0 = vlib_buffer_get_current (p0); + h0 = ip6_next_header (ip0); + options_len0 = + clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]); + + error0 = ICMP6_ERROR_NONE; + sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX]; + + /* Check that source address is link-local */ + error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ? + ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0; + + /* default is to drop */ + next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP; + + n_advertisements_rcvd++; + + if (error0 == ICMP6_ERROR_NONE) + { + vnet_sw_interface_t *sw_if0; + ethernet_interface_t *eth_if0; + + sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0); + ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE); + eth_if0 = + ethernet_get_interface (ðernet_main, sw_if0->hw_if_index); + + /* only support ethernet interface type for now */ + error0 = + (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF + : error0; + + if (error0 == ICMP6_ERROR_NONE) + { + /* look up the radv_t information for this interface */ + radv_info = ip6_ra_get_itf (sw_if_index0); + + error0 = ((!radv_info) ? + ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG : + error0); + + if (error0 == ICMP6_ERROR_NONE) + { + radv_info->keep_sending_rs = 0; + + ip6_ra_report_t r; + + r.sw_if_index = sw_if_index0; + memcpy (&r.router_address, &ip0->src_address, 16); + r.current_hop_limit = h0->current_hop_limit; + r.flags = h0->flags; + r.router_lifetime_in_sec = + clib_net_to_host_u16 (h0->router_lifetime_in_sec); + r.neighbor_reachable_time_in_msec = + clib_net_to_host_u32 + (h0->neighbor_reachable_time_in_msec); + r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations); + r.prefixes = 0; + + /* validate advertised information */ + if ((h0->current_hop_limit && radv_info->curr_hop_limit) + && (h0->current_hop_limit != + radv_info->curr_hop_limit)) + { + ip6_neighbor_syslog (vm, LOG_WARNING, + "our AdvCurHopLimit on %U doesn't agree with %U", + format_vnet_sw_if_index_name, + vnm, sw_if_index0, + format_ip6_address, + &ip0->src_address); + } + + if ((h0->flags & + ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP) + != radv_info->adv_managed_flag) + { + ip6_neighbor_syslog (vm, LOG_WARNING, + "our AdvManagedFlag on %U doesn't agree with %U", + format_vnet_sw_if_index_name, + vnm, sw_if_index0, + format_ip6_address, + &ip0->src_address); + } + + if ((h0->flags & + ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP) + != radv_info->adv_other_flag) + { + ip6_neighbor_syslog (vm, LOG_WARNING, + "our AdvOtherConfigFlag on %U doesn't agree with %U", + format_vnet_sw_if_index_name, + vnm, sw_if_index0, + format_ip6_address, + &ip0->src_address); + } + + if ((h0-> + time_in_msec_between_retransmitted_neighbor_solicitations + && radv_info-> + adv_time_in_msec_between_retransmitted_neighbor_solicitations) + && (h0-> + time_in_msec_between_retransmitted_neighbor_solicitations + != + clib_host_to_net_u32 (radv_info-> + adv_time_in_msec_between_retransmitted_neighbor_solicitations))) + { + ip6_neighbor_syslog (vm, LOG_WARNING, + "our AdvRetransTimer on %U doesn't agree with %U", + format_vnet_sw_if_index_name, + vnm, sw_if_index0, + format_ip6_address, + &ip0->src_address); + } + + if ((h0->neighbor_reachable_time_in_msec && + radv_info->adv_neighbor_reachable_time_in_msec) && + (h0->neighbor_reachable_time_in_msec != + clib_host_to_net_u32 + (radv_info->adv_neighbor_reachable_time_in_msec))) + { + ip6_neighbor_syslog (vm, LOG_WARNING, + "our AdvReachableTime on %U doesn't agree with %U", + format_vnet_sw_if_index_name, + vnm, sw_if_index0, + format_ip6_address, + &ip0->src_address); + } + + /* check for MTU or prefix options or .. */ + u8 *opt_hdr = (u8 *) (h0 + 1); + while (options_len0 > 0) + { + icmp6_neighbor_discovery_option_header_t *o0 = + (icmp6_neighbor_discovery_option_header_t *) + opt_hdr; + int opt_len = o0->n_data_u64s << 3; + icmp6_neighbor_discovery_option_type_t option_type = + o0->type; + + if (options_len0 < 2) + { + ip6_neighbor_syslog (vm, LOG_ERR, + "malformed RA packet on %U from %U", + format_vnet_sw_if_index_name, + vnm, sw_if_index0, + format_ip6_address, + &ip0->src_address); + break; + } + + if (opt_len == 0) + { + ip6_neighbor_syslog (vm, LOG_ERR, + " zero length option in RA on %U from %U", + format_vnet_sw_if_index_name, + vnm, sw_if_index0, + format_ip6_address, + &ip0->src_address); + break; + } + else if (opt_len > options_len0) + { + ip6_neighbor_syslog (vm, LOG_ERR, + "option length in RA packet greater than total length on %U from %U", + format_vnet_sw_if_index_name, + vnm, sw_if_index0, + format_ip6_address, + &ip0->src_address); + break; + } + + options_len0 -= opt_len; + opt_hdr += opt_len; + + switch (option_type) + { + case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address: + { + icmp6_neighbor_discovery_ethernet_link_layer_address_option_t + * h = + (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t + *) (o0); + + if (opt_len < sizeof (*h)) + break; + + memcpy (r.slla, h->ethernet_address, 6); + } + break; + + case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu: + { + icmp6_neighbor_discovery_mtu_option_t *h = + (icmp6_neighbor_discovery_mtu_option_t + *) (o0); + + if (opt_len < sizeof (*h)) + break; + + r.mtu = clib_net_to_host_u32 (h->mtu); + + if ((h->mtu && radv_info->adv_link_mtu) && + (h->mtu != + clib_host_to_net_u32 + (radv_info->adv_link_mtu))) + { + ip6_neighbor_syslog (vm, LOG_WARNING, + "our AdvLinkMTU on %U doesn't agree with %U", + format_vnet_sw_if_index_name, + vnm, sw_if_index0, + format_ip6_address, + &ip0->src_address); + } + } + break; + + case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information: + { + icmp6_neighbor_discovery_prefix_information_option_t + * h = + (icmp6_neighbor_discovery_prefix_information_option_t + *) (o0); + + /* validate advertised prefix options */ + ip6_radv_prefix_t *pr_info; + u32 preferred, valid; + + if (opt_len < sizeof (*h)) + break; + + vec_validate (r.prefixes, + vec_len (r.prefixes)); + ra_report_prefix_info_t *prefix = + vec_elt_at_index (r.prefixes, + vec_len (r.prefixes) - 1); + + preferred = + clib_net_to_host_u32 (h->preferred_time); + valid = clib_net_to_host_u32 (h->valid_time); + + prefix->preferred_time = preferred; + prefix->valid_time = valid; + prefix->flags = h->flags & 0xc0; + prefix->prefix.fp_len = h->dst_address_length; + prefix->prefix.fp_addr.ip6 = h->dst_address; + prefix->prefix.fp_proto = FIB_PROTOCOL_IP6; + + /* look for matching prefix - if we our advertising it, it better be consistant */ + /* *INDENT-OFF* */ + pool_foreach (pr_info, radv_info->adv_prefixes_pool, + ({ + + ip6_address_t mask; + ip6_address_mask_from_width(&mask, pr_info->prefix_len); + + if(pr_info->enabled && + (pr_info->prefix_len == h->dst_address_length) && + ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask)) + { + /* found it */ + if(!pr_info->decrement_lifetime_flag && + valid != pr_info->adv_valid_lifetime_in_secs) + { + ip6_neighbor_syslog(vm, LOG_WARNING, + "our ADV validlifetime on %U for %U does not agree with %U", + format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix, + format_ip6_address, &h->dst_address); + } + if(!pr_info->decrement_lifetime_flag && + preferred != pr_info->adv_pref_lifetime_in_secs) + { + ip6_neighbor_syslog(vm, LOG_WARNING, + "our ADV preferredlifetime on %U for %U does not agree with %U", + format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix, + format_ip6_address, &h->dst_address); + } + } + break; + })); + /* *INDENT-ON* */ + break; + } + default: + /* skip this one */ + break; + } + } + ip6_ra_publish (&r); + } + } + } + + p0->error = error_node->errors[error0]; + + if (error0 != ICMP6_ERROR_NONE) + vlib_error_count (vm, error_node->node_index, error0, 1); + + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + /* Account for router advertisements received. */ + vlib_error_count (vm, error_node->node_index, + ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX, + n_advertisements_rcvd); + + return frame->n_vectors; +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) = +{ + .function = icmp6_router_advertisement, + .name = "icmp6-router-advertisement", + + .vector_size = sizeof (u32), + + .format_trace = format_icmp6_input_trace, + + .n_next_nodes = 1, + .next_nodes = { + [0] = "ip6-drop", + }, +}; +/* *INDENT-ON* */ + +static inline f64 +random_f64_from_to (f64 from, f64 to) +{ + static u32 seed = 0; + static u8 seed_set = 0; + if (!seed_set) + { + seed = random_default_seed (); + seed_set = 1; + } + return random_f64 (&seed) * (to - from) + from; +} + +static inline u8 +get_mac_address (u32 sw_if_index, u8 * address) +{ + vnet_main_t *vnm = vnet_get_main (); + vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index); + if (!hw_if->hw_address) + return 1; + clib_memcpy (address, hw_if->hw_address, 6); + return 0; +} + +static inline vlib_buffer_t * +create_buffer_for_rs (vlib_main_t * vm, ip6_ra_t * radv_info) +{ + u32 bi0; + vlib_buffer_t *p0; + icmp6_router_solicitation_header_t *rh; + u16 payload_length; + int bogus_length; + u32 sw_if_index; + + sw_if_index = radv_info->sw_if_index; + + if (vlib_buffer_alloc (vm, &bi0, 1) != 1) + { + clib_warning ("buffer allocation failure"); + return 0; + } + + p0 = vlib_get_buffer (vm, bi0); + VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0); + p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; + + vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index; + vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index; + + vnet_buffer (p0)->ip.adj_index[VLIB_TX] = + ip6_link_get_mcast_adj (sw_if_index); + + rh = vlib_buffer_get_current (p0); + p0->current_length = sizeof (*rh); + + rh->neighbor.icmp.type = ICMP6_router_solicitation; + rh->neighbor.icmp.code = 0; + rh->neighbor.icmp.checksum = 0; + rh->neighbor.reserved_must_be_zero = 0; + + rh->link_layer_option.header.type = + ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address; + if (0 != get_mac_address (sw_if_index, + rh->link_layer_option.ethernet_address)) + { + clib_warning ("interface with sw_if_index %u has no mac address", + sw_if_index); + vlib_buffer_free (vm, &bi0, 1); + return 0; + } + rh->link_layer_option.header.n_data_u64s = 1; + + payload_length = sizeof (rh->neighbor) + sizeof (u64); + + rh->ip.ip_version_traffic_class_and_flow_label = + clib_host_to_net_u32 (0x6 << 28); + rh->ip.payload_length = clib_host_to_net_u16 (payload_length); + rh->ip.protocol = IP_PROTOCOL_ICMP6; + rh->ip.hop_limit = 255; + ip6_address_copy (&rh->ip.src_address, + ip6_get_link_local_address (radv_info->sw_if_index)); + /* set address ff02::2 */ + rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48); + rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2); + + rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0, + &rh->ip, + &bogus_length); + + return p0; +} + +static inline void +stop_sending_rs (vlib_main_t * vm, ip6_ra_t * ra) +{ + u32 bi0; + + ra->keep_sending_rs = 0; + if (ra->buffer) + { + bi0 = vlib_get_buffer_index (vm, ra->buffer); + vlib_buffer_free (vm, &bi0, 1); + ra->buffer = 0; + } +} + +static inline bool +check_send_rs (vlib_main_t * vm, ip6_ra_t * radv_info, f64 current_time, + f64 * due_time) +{ + vlib_buffer_t *p0; + vlib_frame_t *f; + u32 *to_next; + u32 next_index; + vlib_buffer_t *c0; + u32 ci0; + + icmp6_send_router_solicitation_params_t *params; + + if (!radv_info->keep_sending_rs) + return false; + + params = &radv_info->params; + + if (radv_info->due_time > current_time) + { + *due_time = radv_info->due_time; + return true; + } + + p0 = radv_info->buffer; + + next_index = ip6_rewrite_mcast_node.index; + + c0 = vlib_buffer_copy (vm, p0); + ci0 = vlib_get_buffer_index (vm, c0); + + f = vlib_get_frame_to_node (vm, next_index); + to_next = vlib_frame_vector_args (f); + to_next[0] = ci0; + f->n_vectors = 1; + vlib_put_frame_to_node (vm, next_index, f); + + if (params->mrc != 0 && --radv_info->n_left == 0) + stop_sending_rs (vm, radv_info); + else + { + radv_info->sleep_interval = + (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval; + if (radv_info->sleep_interval > params->mrt) + radv_info->sleep_interval = + (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt; + + radv_info->due_time = current_time + radv_info->sleep_interval; + + if (params->mrd != 0 + && current_time > radv_info->start_time + params->mrd) + stop_sending_rs (vm, radv_info); + else + *due_time = radv_info->due_time; + } + + return radv_info->keep_sending_rs; +} + +static uword +send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt, + vlib_frame_t * f0) +{ + uword *event_data = NULL; + f64 sleep_time = 1e9; + ip6_ra_t *radv_info; + f64 current_time; + f64 due_time; + f64 dt = 0; + + while (true) + { + vlib_process_wait_for_event_or_clock (vm, sleep_time); + vlib_process_get_events (vm, &event_data); + vec_reset_length (event_data); + + current_time = vlib_time_now (vm); + do + { + due_time = current_time + 1e9; + /* *INDENT-OFF* */ + pool_foreach (radv_info, ip6_ra_pool, + ({ + if (check_send_rs (vm, radv_info, current_time, &dt) + && (dt < due_time)) + due_time = dt; + })); + /* *INDENT-ON* */ + current_time = vlib_time_now (vm); + } + while (due_time < current_time); + + sleep_time = due_time - current_time; + } + + return 0; +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (ip6_rs_process_node) = { + .function = send_rs_process, + .type = VLIB_NODE_TYPE_PROCESS, + .name = "ip6-rs-process", +}; +/* *INDENT-ON* */ + +void +icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop, + const icmp6_send_router_solicitation_params_t + * params) +{ + ip6_ra_t *ra; + + ASSERT (~0 != sw_if_index); + + ra = ip6_ra_get_itf (sw_if_index); + + if (!ra) + return; + + stop_sending_rs (vm, ra); + + if (!stop) + { + ra->keep_sending_rs = 1; + ra->params = *params; + ra->n_left = params->mrc; + ra->start_time = vlib_time_now (vm); + ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt; + ra->due_time = 0; /* send first packet ASAP */ + ra->buffer = create_buffer_for_rs (vm, ra); + if (!ra->buffer) + ra->keep_sending_rs = 0; + else + vlib_process_signal_event (vm, ip6_rs_process_node.index, 1, 0); + } +} + +static const ethernet_interface_t * +ip6_ra_get_eth_itf (u32 sw_if_index) +{ + const vnet_sw_interface_t *sw; + + /* lookup radv container - ethernet interfaces only */ + sw = vnet_get_sup_sw_interface (vnet_get_main (), sw_if_index); + if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE) + return (ethernet_get_interface (ðernet_main, sw->hw_if_index)); + + return (NULL); +} + +/** + * @brief called when IP6 is enabled on an interface + *create and initialize router advertisement parameters with default + * values for this intfc + */ +static void +ip6_ra_link_enable (u32 sw_if_index) +{ + const ethernet_interface_t *eth; + ip6_ra_t *radv_info; + + eth = ip6_ra_get_eth_itf (sw_if_index); + + if (NULL == eth) + return; + + ASSERT (INDEX_INVALID == ip6_link_delegate_get (sw_if_index, + ip6_ra_delegate_id)); + + pool_get_zero (ip6_ra_pool, radv_info); + + radv_info->seed = (u32) clib_cpu_time_now (); + random_u32 (&radv_info->seed); + + radv_info->sw_if_index = sw_if_index; + radv_info->max_radv_interval = DEF_MAX_RADV_INTERVAL; + radv_info->min_radv_interval = DEF_MIN_RADV_INTERVAL; + radv_info->curr_hop_limit = DEF_CURR_HOP_LIMIT; + radv_info->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME; + + /* send ll address source address option */ + radv_info->adv_link_layer_address = 1; + + radv_info->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS; + radv_info->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS; + radv_info->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME; + + radv_info->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS; + radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1; + radv_info->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL; + + /* deafult is to send */ + radv_info->send_radv = 1; + + /* fill in delegate for this interface that will be needed later */ + radv_info->adv_link_mtu = + vnet_sw_interface_get_mtu (vnet_get_main (), sw_if_index, VNET_MTU_IP6); + + mhash_init (&radv_info->address_to_prefix_index, sizeof (uword), + sizeof (ip6_address_t)); + + ip6_link_delegate_update (sw_if_index, ip6_ra_delegate_id, + radv_info - ip6_ra_pool); +} + +static void +ip6_ra_delegate_disable (index_t rai) +{ + ip6_radv_prefix_t *p; + ip6_ra_t *radv_info; + + radv_info = pool_elt_at_index (ip6_ra_pool, rai); + + /* clean up prefix and MDP pools */ + /* *INDENT-OFF* */ + pool_flush(p, radv_info->adv_prefixes_pool, + ({ + mhash_unset (&radv_info->address_to_prefix_index, &p->prefix, 0); + })); + /* *INDENT-ON* */ + + pool_free (radv_info->adv_prefixes_pool); + + mhash_free (&radv_info->address_to_prefix_index); + + pool_put (ip6_ra_pool, radv_info); +} + +/* send a RA or update the timer info etc.. */ +static uword +ip6_ra_process_timer_event (vlib_main_t * vm, + vlib_node_runtime_t * node, vlib_frame_t * frame) +{ + vnet_main_t *vnm = vnet_get_main (); + ip6_ra_t *radv_info; + vlib_frame_t *f = 0; + u32 n_this_frame = 0; + u32 n_left_to_next = 0; + u32 *to_next = 0; + u32 bo0; + icmp6_router_solicitation_header_t *h0; + vlib_buffer_t *b0; + f64 now = vlib_time_now (vm); + + /* Interface ip6 radv info list */ + /* *INDENT-OFF* */ + pool_foreach (radv_info, ip6_ra_pool, + ({ + if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index)) + { + radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1; + radv_info->next_multicast_time = now; + radv_info->last_multicast_time = now; + radv_info->last_radv_time = 0; + continue; + } + + /* is it time to send a multicast RA on this interface? */ + if(radv_info->send_radv && (now >= radv_info->next_multicast_time)) + { + u32 n_to_alloc = 1; + u32 n_allocated; + + f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) * + random_f64 (&radv_info->seed) + radv_info->min_radv_interval; + + /* multicast send - compute next multicast send time */ + if( radv_info->initial_adverts_sent > 0) + { + radv_info->initial_adverts_sent--; + if(rfn > radv_info->initial_adverts_interval) + rfn = radv_info->initial_adverts_interval; + + /* check to see if we are ceasing to send */ + if( radv_info->initial_adverts_sent == 0) + if(radv_info->cease_radv) + radv_info->send_radv = 0; + } + + radv_info->next_multicast_time = rfn + now; + radv_info->last_multicast_time = now; + + /* send advert now - build a "solicted" router advert with unspecified source address */ + n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc); + + if (PREDICT_FALSE(n_allocated == 0)) + { + clib_warning ("buffer allocation failure"); + continue; + } + b0 = vlib_get_buffer (vm, bo0); + b0->current_length = sizeof( icmp6_router_solicitation_header_t); + b0->error = ICMP6_ERROR_NONE; + vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index; + + h0 = vlib_buffer_get_current (b0); + + clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t)); + + h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28); + h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t) + - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor)); + h0->ip.protocol = IP_PROTOCOL_ICMP6; + h0->ip.hop_limit = 255; + + /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */ + h0->ip.src_address.as_u64[0] = 0; + h0->ip.src_address.as_u64[1] = 0; + + h0->ip.dst_address.as_u64[0] = 0; + h0->ip.dst_address.as_u64[1] = 0; + + h0->neighbor.icmp.type = ICMP6_router_solicitation; + + if (PREDICT_FALSE(f == 0)) + { + f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index); + to_next = vlib_frame_vector_args (f); + n_left_to_next = VLIB_FRAME_SIZE; + n_this_frame = 0; + } + + n_this_frame++; + n_left_to_next--; + to_next[0] = bo0; + to_next += 1; + + if (PREDICT_FALSE(n_left_to_next == 0)) + { + f->n_vectors = n_this_frame; + vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f); + f = 0; + } + } + })); + /* *INDENT-ON* */ + + if (f) + { + ASSERT (n_this_frame); + f->n_vectors = n_this_frame; + vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f); + } + return 0; +} + +static void +ip6_ra_handle_report (const ip6_ra_report_t * rap) +{ + u32 ii; + + vec_foreach_index (ii, ip6_ra_listeners) ip6_ra_listeners[ii] (rap); +} + +static uword +ip6_ra_event_process (vlib_main_t * vm, + vlib_node_runtime_t * node, vlib_frame_t * frame) +{ + ip6_ra_report_t *r, *rs; + uword event_type; + + /* init code here */ + + while (1) + { + vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ ); + + rs = vlib_process_get_event_data (vm, &event_type); + + if (NULL == rs) + { + /* No events found: timer expired. */ + /* process interface list and send RAs as appropriate, update timer info */ + ip6_ra_process_timer_event (vm, node, frame); + } + else + { + vec_foreach (r, rs) ip6_ra_handle_report (r); + vec_reset_length (rs); + } + } + return frame->n_vectors; +} + +VLIB_REGISTER_NODE (ip6_ra_process_node) = +{ +.function = ip6_ra_event_process,.name = "ip6-ra-process",.type = + VLIB_NODE_TYPE_PROCESS,}; + +static void +ip6_ra_signal_report (ip6_ra_report_t * r) +{ + vlib_main_t *vm = vlib_get_main (); + ip6_ra_report_t *q; + + if (!vec_len (ip6_ra_listeners)) + return; + + q = vlib_process_signal_event_data (vm, + ip6_ra_process_node.index, + 0, 1, sizeof *q); + *q = *r; +} + +static int +ip6_ra_publish (ip6_ra_report_t * r) +{ + void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length); + vl_api_rpc_call_main_thread (ip6_ra_signal_report, (u8 *) r, sizeof *r); + return 0; +} + +/* API support functions */ +int +ip6_ra_config (vlib_main_t * vm, u32 sw_if_index, + u8 suppress, u8 managed, u8 other, + u8 ll_option, u8 send_unicast, u8 cease, + u8 use_lifetime, u32 lifetime, + u32 initial_count, u32 initial_interval, + u32 max_interval, u32 min_interval, u8 is_no) +{ + ip6_ra_t *radv_info; + + /* look up the radv_t information for this interface */ + radv_info = ip6_ra_get_itf (sw_if_index); + + if (!radv_info) + return (VNET_API_ERROR_IP6_NOT_ENABLED); + + if ((max_interval != 0) && (min_interval == 0)) + min_interval = .75 * max_interval; + + max_interval = + (max_interval != + 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) : + radv_info->max_radv_interval; + min_interval = + (min_interval != + 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) : + radv_info->min_radv_interval; + lifetime = + (use_lifetime != + 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) : + radv_info->adv_router_lifetime_in_sec; + + if (lifetime) + { + if (lifetime > MAX_DEF_RTR_LIFETIME) + lifetime = MAX_DEF_RTR_LIFETIME; + + if (lifetime <= max_interval) + return VNET_API_ERROR_INVALID_VALUE; + } + + if (min_interval != 0) + { + if ((min_interval > .75 * max_interval) || (min_interval < 3)) + return VNET_API_ERROR_INVALID_VALUE; + } + + if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) || + (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)) + return VNET_API_ERROR_INVALID_VALUE; + + /* + if "flag" is set and is_no is true then restore default value else set value corresponding to "flag" + if "flag" is clear don't change corresponding value + */ + radv_info->send_radv = + (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv; + radv_info->adv_managed_flag = + (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag; + radv_info->adv_other_flag = + (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag; + radv_info->adv_link_layer_address = + (ll_option != 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address; + radv_info->send_unicast = + (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast; + radv_info->cease_radv = + (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv; + + radv_info->min_radv_interval = min_interval; + radv_info->max_radv_interval = max_interval; + radv_info->adv_router_lifetime_in_sec = lifetime; + + radv_info->initial_adverts_count = + (initial_count != + 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) : + radv_info->initial_adverts_count; + radv_info->initial_adverts_interval = + (initial_interval != + 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) : + radv_info->initial_adverts_interval; + + /* restart */ + if ((cease != 0) && (is_no)) + radv_info->send_radv = 1; + + radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1; + radv_info->next_multicast_time = vlib_time_now (vm); + radv_info->last_multicast_time = vlib_time_now (vm); + radv_info->last_radv_time = 0; + + return (0); +} + + +int +ip6_ra_prefix (vlib_main_t * vm, u32 sw_if_index, + ip6_address_t * prefix_addr, u8 prefix_len, + u8 use_default, u32 val_lifetime, u32 pref_lifetime, + u8 no_advertise, u8 off_link, u8 no_autoconfig, + u8 no_onlink, u8 is_no) +{ + ip6_ra_t *radv_info; + + /* look up the radv_t information for this interface */ + radv_info = ip6_ra_get_itf (sw_if_index); + + if (!radv_info) + return (VNET_API_ERROR_IP6_NOT_ENABLED); + + f64 now = vlib_time_now (vm); + + /* prefix info add, delete or update */ + ip6_radv_prefix_t *prefix; + + /* lookup prefix info for this address on this interface */ + uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr); + + prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0; + + if (is_no) + { + /* delete */ + if (!prefix) + return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */ + + if (prefix->prefix_len != prefix_len) + return VNET_API_ERROR_INVALID_VALUE_2; + + /* FIXME - Should the DP do this or the CP ? */ + /* do specific delete processing here before returning */ + /* try to remove from routing table */ + + mhash_unset (&radv_info->address_to_prefix_index, prefix_addr, + /* old_value */ 0); + pool_put (radv_info->adv_prefixes_pool, prefix); + + radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1; + radv_info->next_multicast_time = vlib_time_now (vm); + radv_info->last_multicast_time = vlib_time_now (vm); + radv_info->last_radv_time = 0; + return (0); + } + + /* adding or changing */ + if (!prefix) + { + /* add */ + u32 pi; + pool_get_zero (radv_info->adv_prefixes_pool, prefix); + pi = prefix - radv_info->adv_prefixes_pool; + mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi, + /* old_value */ 0); + + clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t)); + + prefix->prefix_len = prefix_len; + clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t)); + + /* initialize default values */ + prefix->adv_on_link_flag = 1; /* L bit set */ + prefix->adv_autonomous_flag = 1; /* A bit set */ + prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME; + prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME; + prefix->enabled = 1; + prefix->decrement_lifetime_flag = 1; + prefix->deprecated_prefix_flag = 1; + + if (off_link == 0) + { + /* FIXME - Should the DP do this or the CP ? */ + /* insert prefix into routing table as a connected prefix */ + } + + if (use_default) + goto restart; + } + else + { + + if (prefix->prefix_len != prefix_len) + return VNET_API_ERROR_INVALID_VALUE_2; + + if (off_link != 0) + { + /* FIXME - Should the DP do this or the CP ? */ + /* remove from routing table if already there */ + } + } + + if ((val_lifetime == ~0) || (pref_lifetime == ~0)) + { + prefix->adv_valid_lifetime_in_secs = ~0; + prefix->adv_pref_lifetime_in_secs = ~0; + prefix->decrement_lifetime_flag = 0; + } + else + { + prefix->adv_valid_lifetime_in_secs = val_lifetime;; + prefix->adv_pref_lifetime_in_secs = pref_lifetime; + } + + /* copy remaining */ + prefix->enabled = !(no_advertise != 0); + prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0)); + prefix->adv_autonomous_flag = !(no_autoconfig != 0); + +restart: + /* restart */ + /* fill in the expiration times */ + prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs; + prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs; + + radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1; + radv_info->next_multicast_time = vlib_time_now (vm); + radv_info->last_multicast_time = vlib_time_now (vm); + radv_info->last_radv_time = 0; + + return (0); +} + +clib_error_t * +ip6_ra_cmd (vlib_main_t * vm, + unformat_input_t * main_input, vlib_cli_command_t * cmd) +{ + vnet_main_t *vnm = vnet_get_main (); + clib_error_t *error = 0; + u8 is_no = 0; + u8 suppress = 0, managed = 0, other = 0; + u8 suppress_ll_option = 0, send_unicast = 0, cease = 0; + u8 use_lifetime = 0; + u32 sw_if_index, ra_lifetime = 0, ra_initial_count = + 0, ra_initial_interval = 0; + u32 ra_max_interval = 0, ra_min_interval = 0; + + unformat_input_t _line_input, *line_input = &_line_input; + + int add_radv_info = 1; + ip6_address_t ip6_addr; + u32 addr_len; + + + /* Get a line of input. */ + if (!unformat_user (main_input, unformat_line_input, line_input)) + return 0; + + /* get basic radv info for this interface */ + if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) + { + + if (unformat_user (line_input, + unformat_vnet_sw_interface, vnm, &sw_if_index)) + { + if (!ip6_ra_get_eth_itf (sw_if_index)) + { + error = + clib_error_return (0, "Interface must be of ethernet type"); + goto done; + } + + if (!ip6_link_is_enabled (sw_if_index)) + { + error = clib_error_return (0, "IP6 nt enabler interface %U'", + format_unformat_error, line_input); + goto done; + } + } + else + { + error = clib_error_return (0, "invalid interface name %U'", + format_unformat_error, line_input); + goto done; + } + } + + /* get the rest of the command */ + while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (line_input, "no")) + is_no = 1; + else if (unformat (line_input, "prefix %U/%d", + unformat_ip6_address, &ip6_addr, &addr_len)) + { + add_radv_info = 0; + break; + } + else if (unformat (line_input, "ra-managed-config-flag")) + { + managed = 1; + break; + } + else if (unformat (line_input, "ra-other-config-flag")) + { + other = 1; + break; + } + else if (unformat (line_input, "ra-suppress") || + unformat (line_input, "ra-surpress")) + { + suppress = 1; + break; + } + else if (unformat (line_input, "ra-suppress-link-layer") || + unformat (line_input, "ra-surpress-link-layer")) + { + suppress_ll_option = 1; + break; + } + else if (unformat (line_input, "ra-send-unicast")) + { + send_unicast = 1; + break; + } + else if (unformat (line_input, "ra-lifetime")) + { + if (!unformat (line_input, "%d", &ra_lifetime)) + { + error = unformat_parse_error (line_input); + goto done; + } + use_lifetime = 1; + break; + } + else if (unformat (line_input, "ra-initial")) + { + if (!unformat + (line_input, "%d %d", &ra_initial_count, &ra_initial_interval)) + { + error = unformat_parse_error (line_input); + goto done; + } + break; + } + else if (unformat (line_input, "ra-interval")) + { + if (!unformat (line_input, "%d", &ra_max_interval)) + { + error = unformat_parse_error (line_input); + goto done; + } + + if (!unformat (line_input, "%d", &ra_min_interval)) + ra_min_interval = 0; + break; + } + else if (unformat (line_input, "ra-cease")) + { + cease = 1; + break; + } + else + { + error = unformat_parse_error (line_input); + goto done; + } + } + + if (add_radv_info) + { + ip6_ra_config (vm, sw_if_index, + suppress, managed, other, + suppress_ll_option, send_unicast, cease, + use_lifetime, ra_lifetime, + ra_initial_count, ra_initial_interval, + ra_max_interval, ra_min_interval, is_no); + } + else + { + u32 valid_lifetime_in_secs = 0; + u32 pref_lifetime_in_secs = 0; + u8 use_prefix_default_values = 0; + u8 no_advertise = 0; + u8 off_link = 0; + u8 no_autoconfig = 0; + u8 no_onlink = 0; + + /* get the rest of the command */ + while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (line_input, "default")) + { + use_prefix_default_values = 1; + break; + } + else if (unformat (line_input, "infinite")) + { + valid_lifetime_in_secs = ~0; + pref_lifetime_in_secs = ~0; + break; + } + else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs, + &pref_lifetime_in_secs)) + break; + else + break; + } + + + /* get the rest of the command */ + while (!use_prefix_default_values && + unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (line_input, "no-advertise")) + no_advertise = 1; + else if (unformat (line_input, "off-link")) + off_link = 1; + else if (unformat (line_input, "no-autoconfig")) + no_autoconfig = 1; + else if (unformat (line_input, "no-onlink")) + no_onlink = 1; + else + { + error = unformat_parse_error (line_input); + goto done; + } + } + + ip6_ra_prefix (vm, sw_if_index, + &ip6_addr, addr_len, + use_prefix_default_values, + valid_lifetime_in_secs, + pref_lifetime_in_secs, + no_advertise, off_link, no_autoconfig, no_onlink, is_no); + } + +done: + unformat_free (line_input); + + return error; +} + +static u8 * +format_ip6_ra (u8 * s, va_list * args) +{ + index_t rai = va_arg (*args, index_t); + u32 indent = va_arg (*args, u32); + ip6_radv_prefix_t *p; + ip6_ra_t *radv_info; + + radv_info = pool_elt_at_index (ip6_ra_pool, rai); + + s = format (s, "%UAdvertised Prefixes:\n", format_white_space, indent); + + indent += 2; + + /* *INDENT-OFF* */ + pool_foreach (p, radv_info->adv_prefixes_pool, + ({ + s = format (s, "%Uprefix %U, length %d\n", + format_white_space, indent+2, + format_ip6_address, &p->prefix, p->prefix_len); + })); + /* *INDENT-ON* */ + + s = format (s, "%UMTU is %d\n", + format_white_space, indent, radv_info->adv_link_mtu); + s = format (s, "%UICMP error messages are unlimited\n", + format_white_space, indent); + s = format (s, "%UICMP redirects are disabled\n", + format_white_space, indent); + s = format (s, "%UICMP unreachables are not sent\n", + format_white_space, indent); + s = format (s, "%UND DAD is disabled\n", format_white_space, indent); + //s = format (s, "%UND reachable time is %d milliseconds\n",); + s = format (s, "%UND advertised reachable time is %d\n", + format_white_space, indent, + radv_info->adv_neighbor_reachable_time_in_msec); + s = format (s, + "%UND advertised retransmit interval is %d (msec)\n", + format_white_space, indent, + radv_info-> + adv_time_in_msec_between_retransmitted_neighbor_solicitations); + s = + format (s, + "%UND router advertisements are sent every %0.1f seconds (min interval is %0.1f)\n", + format_white_space, indent, radv_info->max_radv_interval, + radv_info->min_radv_interval); + s = + format (s, "%UND router advertisements live for %d seconds\n", + format_white_space, indent, + radv_info->adv_router_lifetime_in_sec); + s = + format (s, "%UHosts %s stateless autoconfig for addresses\n", + format_white_space, indent, + (radv_info->adv_managed_flag) ? "use" : " don't use"); + s = + format (s, "%UND router advertisements sent %d\n", format_white_space, + indent, radv_info->n_advertisements_sent); + s = + format (s, "%UND router solicitations received %d\n", format_white_space, + indent, radv_info->n_solicitations_rcvd); + s = + format (s, "%UND router solicitations dropped %d\n", format_white_space, + indent, radv_info->n_solicitations_dropped); + + return (s); +} + + +/*? + * This command is used to configure the neighbor discovery + * parameters on a given interface. Use the '<em>show ip6 interface</em>' + * command to display some of the current neighbor discovery parameters + * on a given interface. This command has three formats: + * + * + * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command) + * + * '<em><b>ip6 nd <interface> [no] [ra-managed-config-flag] | [ra-other-config-flag] | [ra-suppress] | [ra-suppress-link-layer] | [ra-send-unicast] | [ra-lifetime <lifetime>] | [ra-initial <cnt> <interval>] | [ra-interval <max-interval> [<min-interval>]] | [ra-cease]</b></em>' + * + * Where: + * + * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6 + * router-advertisement messages to use stateful address + * auto-configuration to obtain address information (sets the M-bit). + * Default is the M-bit is not set and the '<em>no</em>' option + * returns it to this default state. + * + * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6 + * router-advertisement messages that hosts use stateful auto + * configuration to obtain nonaddress related information (sets + * the O-bit). Default is the O-bit is not set and the '<em>no</em>' + * option returns it to this default state. + * + * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement + * messages. The '<em>no</em>' option implies to enable sending ICMPv6 + * router-advertisement messages. + * + * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the + * optional source link-layer address in the ICMPv6 router-advertisement + * messages. Default is to include the optional source link-layer address + * and the '<em>no</em>' option returns it to this default state. + * + * <em>[no] ra-send-unicast</em> - Use the source address of the + * router-solicitation message if availiable. The default is to use + * multicast address of all nodes, and the '<em>no</em>' option returns + * it to this default state. + * + * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a + * default router in ICMPv6 router-advertisement messages. The range is + * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than + * '<em><max-interval></em>'. The default value is 600 seconds and the + * '<em>no</em>' option returns it to this default value. + * + * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6 + * router-advertisement messages sent and the interval between each + * message. Range for count is 1 - 3 and default is 3. Range for interval + * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option + * returns both to their default value. + * + * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the + * interval between sending ICMPv6 router-advertisement messages. The + * range for max-interval is from 4 to 200 seconds. min-interval can not + * be more than 75% of max-interval. If not set, min-interval will be + * set to 75% of max-interval. The range for min-interval is from 3 to + * 150 seconds. The '<em>no</em>' option returns both to their default + * value. + * + * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages. + * The '<em>no</em>' options implies to start (or restart) sending + * ICMPv6 router-advertisement messages. + * + * + * <b>Format 2 - Prefix Options:</b> + * + * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> [<valid-lifetime> <pref-lifetime> | infinite] [no-advertise] [off-link] [no-autoconfig] [no-onlink]</b></em>' + * + * Where: + * + * <em>no</em> - All additional flags are ignored and the prefix is deleted. + * + * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the + * length of time in seconds during what the prefix is valid for the purpose of + * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000 + * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the + * length of time in seconds during what addresses generated from the prefix remain + * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days). + * + * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>' + * are inifinte, no timeout. + * + * <em>no-advertise</em> - Do not send full router address in prefix + * advertisement. Default is to advertise (i.e. - This flag is off by default). + * + * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link + * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can + * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit. + * + * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet. + * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default. + * + * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet. + * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and + * this prefix can be used for on-link determination). '<em>off-link</em>' also controls + * the L-bit. + * + * + * <b>Format 3: - Default of Prefix:</b> + * + * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>' + * + * When a new prefix is added (or existing one is being overwritten) <em>default</em> + * uses default values for the prefix. If <em>no</em> is used, the <em>default</em> + * is ignored and the prefix is deleted. + * + * + * @cliexpar + * Example of how set a router advertisement option: + * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20} + * Example of how to add a prefix: + * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise} + * Example of how to delete a prefix: + * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64} +?*/ +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (ip6_nd_command, static) = +{ + .path = "ip6 nd", + .short_help = "ip6 nd <interface> ...", + .function = ip6_ra_cmd, +}; +/* *INDENT-ON* */ + +/** + * VFT for registering as a delegate to an IP6 link + */ +const static ip6_link_delegate_vft_t ip6_ra_delegate_vft = { + .ildv_disable = ip6_ra_delegate_disable, + .ildv_enable = ip6_ra_link_enable, + .ildv_format = format_ip6_ra, +}; + +static clib_error_t * +ip6_ra_init (vlib_main_t * vm) +{ + vlib_call_init_function (vm, icmp6_init); + + icmp6_register_type (vm, ICMP6_router_solicitation, + ip6_icmp_router_solicitation_node.index); + icmp6_register_type (vm, ICMP6_router_advertisement, + ip6_icmp_router_advertisement_node.index); + + ip6_ra_delegate_id = ip6_link_delegate_register (&ip6_ra_delegate_vft); + + return (NULL); +} + +/* *INDENT-OFF* */ +VLIB_INIT_FUNCTION (ip6_ra_init) = +{ + .runs_after = VLIB_INITS("icmp6_init"), +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ip6-nd/ip6_ra.h b/src/vnet/ip6-nd/ip6_ra.h new file mode 100644 index 00000000000..4efd92e6968 --- /dev/null +++ b/src/vnet/ip6-nd/ip6_ra.h @@ -0,0 +1,89 @@ +/* + * + * ip6_neighboor.h: ip6 neighbor structures + * + * Copyright (c) 2016 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IP6_RA_H__ +#define __IP6_RA_H__ + +#include <vnet/fib/fib_types.h> + +extern int ip6_ra_config (vlib_main_t * vm, u32 sw_if_index, + u8 suppress, u8 managed, u8 other, + u8 ll_option, u8 send_unicast, u8 cease, + u8 use_lifetime, u32 lifetime, + u32 initial_count, u32 initial_interval, + u32 max_interval, u32 min_interval, u8 is_no); + +extern int ip6_ra_prefix (vlib_main_t * vm, u32 sw_if_index, + ip6_address_t * prefix_addr, u8 prefix_len, + u8 use_default, u32 val_lifetime, + u32 pref_lifetime, u8 no_advertise, + u8 off_link, u8 no_autoconfig, + u8 no_onlink, u8 is_no); + +typedef struct +{ + u32 irt; + u32 mrt; + u32 mrc; + u32 mrd; +} icmp6_send_router_solicitation_params_t; + +extern void icmp6_send_router_solicitation (vlib_main_t * vm, + u32 sw_if_index, + u8 stop, + const + icmp6_send_router_solicitation_params_t + * params); + +typedef struct +{ + fib_prefix_t prefix; + u8 flags; + u32 valid_time; + u32 preferred_time; +} ra_report_prefix_info_t; + +typedef struct +{ + ip6_address_t router_address; + u32 sw_if_index; + u8 current_hop_limit; + u8 flags; + u16 router_lifetime_in_sec; + u32 neighbor_reachable_time_in_msec; + u32 time_in_msec_between_retransmitted_neighbor_solicitations; + u8 slla[6]; + u32 mtu; + ra_report_prefix_info_t *prefixes; +} ip6_ra_report_t; + + +typedef void (*ip6_ra_report_notify_t) (const ip6_ra_report_t * rap); + +extern void ip6_ra_report_register (ip6_ra_report_notify_t fn); +extern void ip6_ra_report_unregister (ip6_ra_report_notify_t fn); + +#endif /* included_ip6_neighbor_h */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ip6-nd/rd_cp.api b/src/vnet/ip6-nd/rd_cp.api new file mode 100644 index 00000000000..58480789d16 --- /dev/null +++ b/src/vnet/ip6-nd/rd_cp.api @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +option version = "1.0.1"; + +import "vnet/interface_types.api"; + +/** \brief Enable/disable IPv6 ND address autoconfiguration + and setting up default routes + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param sw_if_index - interface to enable the autoconfigutation on + @param enable - 1 to enable address autoconfiguration, 0 to disable + @param install_default_routes - 1 to enable installing defaut routes, + 0 to disable installing defaut routes, + the value is ignored (and taken as 0) + when enable param is set to 0 +*/ +autoreply define ip6_nd_address_autoconfig +{ + u32 client_index; + u32 context; + vl_api_interface_index_t sw_if_index; + bool enable; + bool install_default_routes; +}; + +/* + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ip6-nd/rd_cp.c b/src/vnet/ip6-nd/rd_cp.c new file mode 100644 index 00000000000..ee7c323f8b1 --- /dev/null +++ b/src/vnet/ip6-nd/rd_cp.c @@ -0,0 +1,623 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vnet/ip6-nd/ip6_ra.h> + +#include <vnet/ip/ip6.h> +#include <vnet/ip/ip6_link.h> +#include <vnet/ethernet/ethernet.h> +#include <vnet/fib/fib_table.h> +#include <signal.h> +#include <math.h> + +typedef struct +{ + u32 sw_if_index; + u8 address_length; + ip6_address_t address; + f64 due_time; +} slaac_address_t; + +typedef struct +{ + u32 sw_if_index; + ip6_address_t router_address; + f64 due_time; +} default_route_t; + +typedef struct +{ + u8 enabled; + u8 install_default_routes; +} interface_config_t; + +typedef struct +{ + u8 enabled; + u8 events_on; + + interface_config_t *config_by_sw_if_index; + slaac_address_t *slaac_address_pool; + default_route_t *default_route_pool; + + /* binary API client */ + u8 api_connected; + u32 my_client_index; + + /* logging */ + vlib_log_class_t log_class; + + /* convenience */ + vlib_main_t *vlib_main; + vnet_main_t *vnet_main; + u32 node_index; +} rd_cp_main_t; + +rd_cp_main_t rd_cp_main; + +enum +{ + RD_CP_EVENT_INTERRUPT, +}; + +#define vl_api_ip6_nd_address_autoconfig_t_print vl_noop_handler + +static void +router_solicitation_start_stop (u32 sw_if_index, u8 start) +{ + rd_cp_main_t *rm = &rd_cp_main; + icmp6_send_router_solicitation_params_t params = { 0, }; + + if (start) + { + params.irt = 1; + params.mrt = 120; + } + + icmp6_send_router_solicitation (rm->vlib_main, sw_if_index, !start, + ¶ms); +} + +static void interrupt_process (void); + +static int +add_slaac_address (vlib_main_t * vm, u32 sw_if_index, u8 address_length, + const ip6_address_t * address, f64 due_time) +{ + rd_cp_main_t *rm = &rd_cp_main; + slaac_address_t *slaac_address; + clib_error_t *rv = 0; + + pool_get_zero (rm->slaac_address_pool, slaac_address); + + slaac_address->sw_if_index = sw_if_index; + slaac_address->address_length = address_length; + slaac_address->address = *address; + slaac_address->due_time = due_time; + + rv = + ip6_add_del_interface_address (vm, sw_if_index, &slaac_address->address, + address_length, 0); + + return rv != 0; +} + +static void +add_default_route (vlib_main_t * vm, u32 sw_if_index, + const ip6_address_t * next_hop_address, f64 due_time) +{ + rd_cp_main_t *rm = &rd_cp_main; + default_route_t *default_route; + + pool_get_zero (rm->default_route_pool, default_route); + + default_route->sw_if_index = sw_if_index; + default_route->router_address = *next_hop_address; + default_route->due_time = due_time; + + { + u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, + default_route-> + sw_if_index); + fib_prefix_t pfx = { + .fp_proto = FIB_PROTOCOL_IP6, + }; + ip46_address_t nh = { + .ip6 = default_route->router_address, + }; + fib_table_entry_update_one_path (fib_index, &pfx, + FIB_SOURCE_API, + FIB_ENTRY_FLAG_NONE, + DPO_PROTO_IP6, + &nh, + default_route->sw_if_index, + 0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE); + } +} + +static int +remove_slaac_address (vlib_main_t * vm, slaac_address_t * slaac_address) +{ + rd_cp_main_t *rm = &rd_cp_main; + clib_error_t *rv = 0; + + rv = ip6_add_del_interface_address (vm, slaac_address->sw_if_index, + &slaac_address->address, + slaac_address->address_length, 1); + + pool_put (rm->slaac_address_pool, slaac_address); + + return rv != 0; +} + +static void +remove_default_route (vlib_main_t * vm, default_route_t * default_route) +{ + rd_cp_main_t *rm = &rd_cp_main; + + { + u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, + default_route-> + sw_if_index); + fib_prefix_t pfx = { + .fp_proto = FIB_PROTOCOL_IP6, + }; + ip46_address_t nh = { + .ip6 = default_route->router_address, + }; + fib_table_entry_path_remove (fib_index, &pfx, + FIB_SOURCE_API, + DPO_PROTO_IP6, + &nh, + default_route->sw_if_index, + 0, 1, FIB_ROUTE_PATH_FLAG_NONE); + } + + pool_put (rm->default_route_pool, default_route); +} + +static u32 +get_interface_mac_address (u32 sw_if_index, u8 mac[]) +{ + rd_cp_main_t *rm = &rd_cp_main; + vnet_sw_interface_t *si; + ethernet_interface_t *eth_if = 0; + + if (!vnet_sw_interface_is_api_valid (rm->vnet_main, sw_if_index)) + { + vlib_log_warn (rm->log_class, "Invalid sw_if_index"); + return 1; + } + + si = vnet_get_sup_sw_interface (rm->vnet_main, sw_if_index); + if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE) + eth_if = ethernet_get_interface (ðernet_main, si->hw_if_index); + + if (!eth_if) + { + vlib_log_warn (rm->log_class, "Failed to get hardware interface"); + return 1; + } + + clib_memcpy_fast (mac, eth_if->address, 6); + + return 0; +} + +static u8 +ip6_prefixes_equal (ip6_address_t * prefix1, ip6_address_t * prefix2, u8 len) +{ + if (len >= 64) + { + if (prefix1->as_u64[0] != prefix2->as_u64[0]) + return 0; + if (len == 64) + return 1; + return prefix1->as_u64[1] >> (128 - len) == + prefix2->as_u64[1] >> (128 - len); + } + return prefix1->as_u64[0] >> (64 - len) == prefix2->as_u64[0] >> (64 - len); +} + +#define PREFIX_FLAG_A (1 << 6) +#define PREFIX_FLAG_L (1 << 7) + +static void +ip6_ra_report_handler (const ip6_ra_report_t * r) +{ + rd_cp_main_t *rm = &rd_cp_main; + vlib_main_t *vm = rm->vlib_main; + interface_config_t *if_config; + default_route_t *default_route; + slaac_address_t *slaac_address; + u32 sw_if_index; + u16 router_lifetime_in_sec; + u32 n_prefixes; + ra_report_prefix_info_t *prefix; + u8 mac[6]; + f64 current_time; + u32 i; + + current_time = vlib_time_now (vm); + + sw_if_index = r->sw_if_index; + + if (sw_if_index >= vec_len (rm->config_by_sw_if_index)) + return; + if_config = &rm->config_by_sw_if_index[sw_if_index]; + + if (if_config->install_default_routes) + { + router_lifetime_in_sec = r->router_lifetime_in_sec; + u8 route_already_present = 0; + /* *INDENT-OFF* */ + pool_foreach (default_route, rm->default_route_pool, + ({ + if (default_route->sw_if_index != sw_if_index) + ; + else if (0 != memcmp (&default_route->router_address, + &r->router_address, 16)) + ; + else + { + route_already_present = 1; + goto default_route_pool_foreach_out; + } + })); + /* *INDENT-ON* */ + default_route_pool_foreach_out: + + if (!route_already_present) + { + if (router_lifetime_in_sec != 0) + add_default_route (vm, sw_if_index, &r->router_address, + current_time + router_lifetime_in_sec); + } + else + { + if (router_lifetime_in_sec != 0) + default_route->due_time = current_time + router_lifetime_in_sec; + else + remove_default_route (vm, default_route); + } + } + + if (get_interface_mac_address (sw_if_index, mac) != 0) + { + vlib_log_warn (rm->log_class, "Error getting MAC address"); + return; + } + + if (!if_config->enabled) + return; + + n_prefixes = vec_len (r->prefixes); + for (i = 0; i < n_prefixes; i++) + { + ip6_address_t *dst_address; + u8 prefix_length; + u32 valid_time; + u32 preferred_time; + f64 due_time; + + prefix = &r->prefixes[i]; + + if (!(prefix->flags & PREFIX_FLAG_A)) + continue; + + dst_address = &prefix->prefix.fp_addr.ip6; + prefix_length = prefix->prefix.fp_len; + + if (ip6_address_is_link_local_unicast (dst_address)) + continue; + + valid_time = prefix->valid_time; + preferred_time = prefix->preferred_time; + + if (preferred_time > valid_time) + continue; + + if (prefix_length != 64) + continue; + + u8 address_already_present = 0; + /* *INDENT-OFF* */ + pool_foreach (slaac_address, rm->slaac_address_pool, + ({ + if (slaac_address->sw_if_index != sw_if_index) + ; + else if (slaac_address->address_length != prefix_length) + ; + else if (!ip6_prefixes_equal (&slaac_address->address, dst_address, + prefix_length)) + ; + else + { + address_already_present = 1; + goto slaac_address_pool_foreach_out; + } + })); + /* *INDENT-ON* */ + slaac_address_pool_foreach_out: + + if (address_already_present) + { + f64 remaining_life_time = slaac_address->due_time - current_time; + if (valid_time > 2 * 60 * 60 || valid_time > remaining_life_time) + slaac_address->due_time = current_time + valid_time; + else if (remaining_life_time > 2 * 60 * 60) + slaac_address->due_time = current_time + 2 * 60 * 60; + continue; + } + + if (valid_time == 0) + continue; + + due_time = current_time + valid_time; + + ip6_address_t addr; + addr.as_u64[0] = dst_address->as_u64[0]; + /* Invert the "u" bit */ + addr.as_u8[8] = mac[0] ^ (1 << 1); + addr.as_u8[9] = mac[1]; + addr.as_u8[10] = mac[2]; + addr.as_u8[11] = 0xFF; + addr.as_u8[12] = 0xFE; + addr.as_u8[13] = mac[3]; + addr.as_u8[14] = mac[4]; + addr.as_u8[15] = mac[5]; + + add_slaac_address (vm, sw_if_index, prefix_length, &addr, due_time); + } + + interrupt_process (); + + return; +} + +static uword +rd_cp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f) +{ + uword *event_data = 0; + rd_cp_main_t *rm = &rd_cp_main; + slaac_address_t *slaac_address; + default_route_t *default_route; + f64 sleep_time = 1e9; + f64 current_time; + f64 due_time; + + while (1) + { + vlib_process_wait_for_event_or_clock (vm, sleep_time); + vlib_process_get_events (vm, &event_data); + + vec_reset_length (event_data); + + current_time = vlib_time_now (vm); + do + { + due_time = current_time + 1e9; + u32 index; + /* + * we do not use pool_foreach() to iterate over pool elements here + * as we are removing elements inside the loop body + */ + /* *INDENT-OFF* */ + pool_foreach_index (index, rm->slaac_address_pool, + ({ + slaac_address = pool_elt_at_index(rm->slaac_address_pool, index); + if (slaac_address->due_time > current_time) + { + if (slaac_address->due_time < due_time) + due_time = slaac_address->due_time; + } + else + { + u32 sw_if_index = slaac_address->sw_if_index; + remove_slaac_address (vm, slaac_address); + /* make sure ip6 stays enabled */ + ip6_link_enable (sw_if_index); + } + })); + pool_foreach_index (index, rm->default_route_pool, + ({ + default_route = pool_elt_at_index(rm->default_route_pool, index); + if (default_route->due_time > current_time) + { + if (default_route->due_time < due_time) + due_time = default_route->due_time; + } + else + remove_default_route (vm, default_route); + })); + /* *INDENT-ON* */ + current_time = vlib_time_now (vm); + } + while (due_time < current_time); + + sleep_time = due_time - current_time; + } + + return 0; +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (rd_cp_process_node) = { + .function = rd_cp_process, + .type = VLIB_NODE_TYPE_PROCESS, + .name = "rd-cp-process", +}; +/* *INDENT-ON* */ + +static void +interrupt_process (void) +{ + rd_cp_main_t *rm = &rd_cp_main; + vlib_main_t *vm = rm->vlib_main; + + vlib_process_signal_event (vm, rd_cp_process_node.index, + RD_CP_EVENT_INTERRUPT, 0); +} + +int +rd_cp_set_address_autoconfig (u32 sw_if_index, + u8 enable, u8 install_default_routes) +{ + rd_cp_main_t *rm = &rd_cp_main; + vlib_main_t *vm = rm->vlib_main; + vnet_main_t *vnm = rm->vnet_main; + interface_config_t *if_config; + interface_config_t empty_config = { 0, 0 }; + slaac_address_t *slaac_address; + default_route_t *default_route; + + if (!enable) + install_default_routes = 0; + + if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index)) + { + vlib_log_warn (rm->log_class, "Invalid sw_if_index"); + return 1; + } + + if (!rm->enabled) + { + /* process kickoff */ + interrupt_process (); + rm->enabled = 1; + } + + vec_validate_init_empty (rm->config_by_sw_if_index, sw_if_index, + empty_config); + if_config = &rm->config_by_sw_if_index[sw_if_index]; + + if (!if_config->enabled && enable) + ip6_link_enable (sw_if_index); + + if ((!if_config->enabled && enable) + || (!if_config->install_default_routes && install_default_routes)) + router_solicitation_start_stop (sw_if_index, 1); + else if (if_config->enabled && !enable) + router_solicitation_start_stop (sw_if_index, 0); + + if (if_config->enabled && !enable) + { + /* *INDENT-OFF* */ + pool_foreach (slaac_address, rm->slaac_address_pool, + ({ + remove_slaac_address (vm, slaac_address); + })); + /* *INDENT-ON* */ + } + if (if_config->install_default_routes && !install_default_routes) + { + /* *INDENT-OFF* */ + pool_foreach (default_route, rm->default_route_pool, + ({ + remove_default_route (vm, default_route); + })); + /* *INDENT-ON* */ + } + + if_config->enabled = enable; + if_config->install_default_routes = install_default_routes; + + return 0; +} + +static clib_error_t * +ip6_nd_address_autoconfig (vlib_main_t * vm, + unformat_input_t * input, vlib_cli_command_t * cmd) +{ + rd_cp_main_t *rm = &rd_cp_main; + vnet_main_t *vnm = rm->vnet_main; + clib_error_t *error = 0; + u32 sw_if_index = ~0; + u8 enable = 1; + u8 default_route = 0; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat + (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) + ; + if (unformat (input, "default-route")) + default_route = 1; + if (unformat (input, "disable")) + enable = 0; + else + break; + } + + if (sw_if_index != ~0) + { + if (rd_cp_set_address_autoconfig (sw_if_index, enable, default_route) != + 0) + error = clib_error_return (0, "Invalid sw_if_index"); + } + else + error = clib_error_return (0, "Missing sw_if_index"); + + return error; +} + +/*? + * This command is used to enable ND address autoconfiguration + * on particular interface including setting up default routes. + * + * @cliexpar + * @parblock + * Example of how to enable ND address autoconfiguration: + * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0} + * Example of how to enable ND address autoconfiguration + * with setting up default routes: + * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0 default-route} + * Example of how to disable ND address autoconfiguration: + * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0 disable} + * @endparblock +?*/ +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (ip6_nd_address_autoconfig_command, static) = { + .path = "ip6 nd address autoconfig", + .short_help = "ip6 nd address autoconfig <interface> [default-route|disable]", + .function = ip6_nd_address_autoconfig, +}; +/* *INDENT-ON* */ + +static clib_error_t * +rd_cp_init (vlib_main_t * vm) +{ + rd_cp_main_t *rm = &rd_cp_main; + + rm->vlib_main = vm; + rm->vnet_main = vnet_get_main (); + rm->node_index = rd_cp_process_node.index; + + rm->log_class = vlib_log_register_class ("rd_cp", 0); + + ip6_ra_report_register (ip6_ra_report_handler); + + return (NULL); +} + +VLIB_INIT_FUNCTION (rd_cp_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ip6-nd/rd_cp.h b/src/vnet/ip6-nd/rd_cp.h new file mode 100644 index 00000000000..2fe43da621b --- /dev/null +++ b/src/vnet/ip6-nd/rd_cp.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __RD_CP_H__ +#define __RD_CP_H__ + +#include <vnet/ip6-nd/ip6_nd.h> + +extern int rd_cp_set_address_autoconfig (u32 sw_if_index, + u8 enable, + u8 install_default_routes); + +#endif + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ip6-nd/rd_cp_api.c b/src/vnet/ip6-nd/rd_cp_api.c new file mode 100644 index 00000000000..1f0d8587970 --- /dev/null +++ b/src/vnet/ip6-nd/rd_cp_api.c @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vnet/ip6-nd/rd_cp.h> + +#include <vlibapi/api.h> +#include <vlibmemory/api.h> + +/* define message IDs */ +#include <vnet/format_fns.h> +#include <vnet/ip6-nd/rd_cp.api_enum.h> +#include <vnet/ip6-nd/rd_cp.api_types.h> + +/** + * Base message ID fot the plugin + */ +static u32 rd_cp_base_msg_id; +#define REPLY_MSG_ID_BASE rd_cp_base_msg_id + +#include <vlibapi/api_helper_macros.h> + + +static void +vl_api_ip6_nd_address_autoconfig_t_handler (vl_api_ip6_nd_address_autoconfig_t + * mp) +{ + vl_api_ip6_nd_address_autoconfig_reply_t *rmp; + u32 sw_if_index; + int rv = 0; + + VALIDATE_SW_IF_INDEX (mp); + + sw_if_index = ntohl (mp->sw_if_index); + + rv = rd_cp_set_address_autoconfig (sw_if_index, + mp->enable, mp->install_default_routes); + + BAD_SW_IF_INDEX_LABEL; + + REPLY_MACRO (VL_API_IP6_ND_ADDRESS_AUTOCONFIG_REPLY); +} + +#include <vnet/ip6-nd/rd_cp.api.c> + +static clib_error_t * +rd_cp_api_init (vlib_main_t * vm) +{ + rd_cp_base_msg_id = setup_message_id_table (); + + return (NULL); +} + +VLIB_INIT_FUNCTION (rd_cp_api_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ |