Juraj Sloboda | 0012fcc | 2018-05-17 12:05:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 16 | #include <vnet/ip6-nd/ip6_ra.h> |
| 17 | |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 18 | #include <vnet/ip/ip6.h> |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 19 | #include <vnet/ip/ip6_link.h> |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 20 | #include <vnet/ethernet/ethernet.h> |
Juraj Sloboda | 0012fcc | 2018-05-17 12:05:27 +0200 | [diff] [blame] | 21 | #include <vnet/fib/fib_table.h> |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 22 | #include <signal.h> |
| 23 | #include <math.h> |
| 24 | |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 25 | typedef struct |
| 26 | { |
| 27 | u32 sw_if_index; |
| 28 | u8 address_length; |
| 29 | ip6_address_t address; |
| 30 | f64 due_time; |
| 31 | } slaac_address_t; |
| 32 | |
| 33 | typedef struct |
| 34 | { |
| 35 | u32 sw_if_index; |
| 36 | ip6_address_t router_address; |
| 37 | f64 due_time; |
| 38 | } default_route_t; |
| 39 | |
| 40 | typedef struct |
| 41 | { |
| 42 | u8 enabled; |
| 43 | u8 install_default_routes; |
| 44 | } interface_config_t; |
| 45 | |
| 46 | typedef struct |
| 47 | { |
| 48 | u8 enabled; |
| 49 | u8 events_on; |
| 50 | |
| 51 | interface_config_t *config_by_sw_if_index; |
| 52 | slaac_address_t *slaac_address_pool; |
| 53 | default_route_t *default_route_pool; |
| 54 | |
| 55 | /* binary API client */ |
| 56 | u8 api_connected; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 57 | u32 my_client_index; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 58 | |
Juraj Sloboda | c65770d | 2018-05-15 11:43:56 +0200 | [diff] [blame] | 59 | /* logging */ |
| 60 | vlib_log_class_t log_class; |
| 61 | |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 62 | /* convenience */ |
| 63 | vlib_main_t *vlib_main; |
| 64 | vnet_main_t *vnet_main; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 65 | u32 node_index; |
| 66 | } rd_cp_main_t; |
| 67 | |
| 68 | rd_cp_main_t rd_cp_main; |
| 69 | |
| 70 | enum |
| 71 | { |
| 72 | RD_CP_EVENT_INTERRUPT, |
| 73 | }; |
| 74 | |
| 75 | #define vl_api_ip6_nd_address_autoconfig_t_print vl_noop_handler |
| 76 | |
| 77 | static void |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 78 | router_solicitation_start_stop (u32 sw_if_index, u8 start) |
| 79 | { |
| 80 | rd_cp_main_t *rm = &rd_cp_main; |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 81 | icmp6_send_router_solicitation_params_t params = { 0, }; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 82 | |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 83 | if (start) |
| 84 | { |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 85 | params.irt = 1; |
| 86 | params.mrt = 120; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 87 | } |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 88 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 89 | icmp6_send_router_solicitation (rm->vlib_main, sw_if_index, !start, |
| 90 | ¶ms); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void interrupt_process (void); |
| 94 | |
| 95 | static int |
| 96 | add_slaac_address (vlib_main_t * vm, u32 sw_if_index, u8 address_length, |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 97 | const ip6_address_t * address, f64 due_time) |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 98 | { |
| 99 | rd_cp_main_t *rm = &rd_cp_main; |
| 100 | slaac_address_t *slaac_address; |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 101 | clib_error_t *rv = 0; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 102 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 103 | pool_get_zero (rm->slaac_address_pool, slaac_address); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 104 | |
| 105 | slaac_address->sw_if_index = sw_if_index; |
| 106 | slaac_address->address_length = address_length; |
| 107 | slaac_address->address = *address; |
| 108 | slaac_address->due_time = due_time; |
| 109 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 110 | rv = |
| 111 | ip6_add_del_interface_address (vm, sw_if_index, &slaac_address->address, |
| 112 | address_length, 0); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 113 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 114 | return rv != 0; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Juraj Sloboda | 0012fcc | 2018-05-17 12:05:27 +0200 | [diff] [blame] | 117 | static void |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 118 | add_default_route (vlib_main_t * vm, u32 sw_if_index, |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 119 | const ip6_address_t * next_hop_address, f64 due_time) |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 120 | { |
| 121 | rd_cp_main_t *rm = &rd_cp_main; |
| 122 | default_route_t *default_route; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 123 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 124 | pool_get_zero (rm->default_route_pool, default_route); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 125 | |
| 126 | default_route->sw_if_index = sw_if_index; |
| 127 | default_route->router_address = *next_hop_address; |
| 128 | default_route->due_time = due_time; |
| 129 | |
Juraj Sloboda | 0012fcc | 2018-05-17 12:05:27 +0200 | [diff] [blame] | 130 | { |
| 131 | u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, |
| 132 | default_route-> |
| 133 | sw_if_index); |
| 134 | fib_prefix_t pfx = { |
| 135 | .fp_proto = FIB_PROTOCOL_IP6, |
| 136 | }; |
| 137 | ip46_address_t nh = { |
| 138 | .ip6 = default_route->router_address, |
| 139 | }; |
| 140 | fib_table_entry_update_one_path (fib_index, &pfx, |
| 141 | FIB_SOURCE_API, |
| 142 | FIB_ENTRY_FLAG_NONE, |
| 143 | DPO_PROTO_IP6, |
| 144 | &nh, |
| 145 | default_route->sw_if_index, |
| 146 | 0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE); |
| 147 | } |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 150 | static int |
| 151 | remove_slaac_address (vlib_main_t * vm, slaac_address_t * slaac_address) |
| 152 | { |
Juraj Sloboda | 0012fcc | 2018-05-17 12:05:27 +0200 | [diff] [blame] | 153 | rd_cp_main_t *rm = &rd_cp_main; |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 154 | clib_error_t *rv = 0; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 155 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 156 | rv = ip6_add_del_interface_address (vm, slaac_address->sw_if_index, |
| 157 | &slaac_address->address, |
| 158 | slaac_address->address_length, 1); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 159 | |
Juraj Sloboda | 0012fcc | 2018-05-17 12:05:27 +0200 | [diff] [blame] | 160 | pool_put (rm->slaac_address_pool, slaac_address); |
| 161 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 162 | return rv != 0; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Juraj Sloboda | 0012fcc | 2018-05-17 12:05:27 +0200 | [diff] [blame] | 165 | static void |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 166 | remove_default_route (vlib_main_t * vm, default_route_t * default_route) |
| 167 | { |
| 168 | rd_cp_main_t *rm = &rd_cp_main; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 169 | |
Juraj Sloboda | 0012fcc | 2018-05-17 12:05:27 +0200 | [diff] [blame] | 170 | { |
| 171 | u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, |
| 172 | default_route-> |
| 173 | sw_if_index); |
| 174 | fib_prefix_t pfx = { |
| 175 | .fp_proto = FIB_PROTOCOL_IP6, |
| 176 | }; |
| 177 | ip46_address_t nh = { |
| 178 | .ip6 = default_route->router_address, |
| 179 | }; |
| 180 | fib_table_entry_path_remove (fib_index, &pfx, |
| 181 | FIB_SOURCE_API, |
| 182 | DPO_PROTO_IP6, |
| 183 | &nh, |
| 184 | default_route->sw_if_index, |
| 185 | 0, 1, FIB_ROUTE_PATH_FLAG_NONE); |
| 186 | } |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 187 | |
Juraj Sloboda | 0012fcc | 2018-05-17 12:05:27 +0200 | [diff] [blame] | 188 | pool_put (rm->default_route_pool, default_route); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static u32 |
| 192 | get_interface_mac_address (u32 sw_if_index, u8 mac[]) |
| 193 | { |
| 194 | rd_cp_main_t *rm = &rd_cp_main; |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 195 | vnet_sw_interface_t *si; |
| 196 | ethernet_interface_t *eth_if = 0; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 197 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 198 | if (!vnet_sw_interface_is_api_valid (rm->vnet_main, sw_if_index)) |
| 199 | { |
Juraj Sloboda | c65770d | 2018-05-15 11:43:56 +0200 | [diff] [blame] | 200 | vlib_log_warn (rm->log_class, "Invalid sw_if_index"); |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 201 | return 1; |
| 202 | } |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 203 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 204 | si = vnet_get_sup_sw_interface (rm->vnet_main, sw_if_index); |
| 205 | if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE) |
| 206 | eth_if = ethernet_get_interface (ðernet_main, si->hw_if_index); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 207 | |
Juraj Sloboda | c65770d | 2018-05-15 11:43:56 +0200 | [diff] [blame] | 208 | if (!eth_if) |
| 209 | { |
| 210 | vlib_log_warn (rm->log_class, "Failed to get hardware interface"); |
| 211 | return 1; |
| 212 | } |
| 213 | |
Benoît Ganne | b44c77d | 2020-10-20 16:24:17 +0200 | [diff] [blame] | 214 | clib_memcpy_fast (mac, ð_if->address, 6); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 215 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 216 | return 0; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 219 | static u8 |
| 220 | ip6_prefixes_equal (ip6_address_t * prefix1, ip6_address_t * prefix2, u8 len) |
| 221 | { |
| 222 | if (len >= 64) |
| 223 | { |
| 224 | if (prefix1->as_u64[0] != prefix2->as_u64[0]) |
| 225 | return 0; |
| 226 | if (len == 64) |
| 227 | return 1; |
| 228 | return prefix1->as_u64[1] >> (128 - len) == |
| 229 | prefix2->as_u64[1] >> (128 - len); |
| 230 | } |
| 231 | return prefix1->as_u64[0] >> (64 - len) == prefix2->as_u64[0] >> (64 - len); |
| 232 | } |
| 233 | |
| 234 | #define PREFIX_FLAG_A (1 << 6) |
| 235 | #define PREFIX_FLAG_L (1 << 7) |
| 236 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 237 | static void |
| 238 | ip6_ra_report_handler (const ip6_ra_report_t * r) |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 239 | { |
| 240 | rd_cp_main_t *rm = &rd_cp_main; |
| 241 | vlib_main_t *vm = rm->vlib_main; |
| 242 | interface_config_t *if_config; |
| 243 | default_route_t *default_route; |
| 244 | slaac_address_t *slaac_address; |
| 245 | u32 sw_if_index; |
| 246 | u16 router_lifetime_in_sec; |
| 247 | u32 n_prefixes; |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 248 | ra_report_prefix_info_t *prefix; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 249 | u8 mac[6]; |
| 250 | f64 current_time; |
| 251 | u32 i; |
| 252 | |
| 253 | current_time = vlib_time_now (vm); |
| 254 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 255 | sw_if_index = r->sw_if_index; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 256 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 257 | if (sw_if_index >= vec_len (rm->config_by_sw_if_index)) |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 258 | return; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 259 | if_config = &rm->config_by_sw_if_index[sw_if_index]; |
| 260 | |
| 261 | if (if_config->install_default_routes) |
| 262 | { |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 263 | router_lifetime_in_sec = r->router_lifetime_in_sec; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 264 | u8 route_already_present = 0; |
| 265 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 266 | pool_foreach (default_route, rm->default_route_pool) |
| 267 | { |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 268 | if (default_route->sw_if_index != sw_if_index) |
| 269 | ; |
| 270 | else if (0 != memcmp (&default_route->router_address, |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 271 | &r->router_address, 16)) |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 272 | ; |
| 273 | else |
| 274 | { |
| 275 | route_already_present = 1; |
| 276 | goto default_route_pool_foreach_out; |
| 277 | } |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 278 | } |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 279 | /* *INDENT-ON* */ |
| 280 | default_route_pool_foreach_out: |
| 281 | |
| 282 | if (!route_already_present) |
| 283 | { |
| 284 | if (router_lifetime_in_sec != 0) |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 285 | add_default_route (vm, sw_if_index, &r->router_address, |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 286 | current_time + router_lifetime_in_sec); |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | if (router_lifetime_in_sec != 0) |
| 291 | default_route->due_time = current_time + router_lifetime_in_sec; |
| 292 | else |
| 293 | remove_default_route (vm, default_route); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (get_interface_mac_address (sw_if_index, mac) != 0) |
| 298 | { |
Juraj Sloboda | c65770d | 2018-05-15 11:43:56 +0200 | [diff] [blame] | 299 | vlib_log_warn (rm->log_class, "Error getting MAC address"); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 300 | return; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | if (!if_config->enabled) |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 304 | return; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 305 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 306 | n_prefixes = vec_len (r->prefixes); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 307 | for (i = 0; i < n_prefixes; i++) |
| 308 | { |
| 309 | ip6_address_t *dst_address; |
| 310 | u8 prefix_length; |
| 311 | u32 valid_time; |
| 312 | u32 preferred_time; |
| 313 | f64 due_time; |
| 314 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 315 | prefix = &r->prefixes[i]; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 316 | |
| 317 | if (!(prefix->flags & PREFIX_FLAG_A)) |
| 318 | continue; |
| 319 | |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 320 | dst_address = &prefix->prefix.fp_addr.ip6; |
| 321 | prefix_length = prefix->prefix.fp_len; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 322 | |
| 323 | if (ip6_address_is_link_local_unicast (dst_address)) |
| 324 | continue; |
| 325 | |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 326 | valid_time = prefix->valid_time; |
| 327 | preferred_time = prefix->preferred_time; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 328 | |
| 329 | if (preferred_time > valid_time) |
| 330 | continue; |
| 331 | |
| 332 | if (prefix_length != 64) |
| 333 | continue; |
| 334 | |
| 335 | u8 address_already_present = 0; |
| 336 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 337 | pool_foreach (slaac_address, rm->slaac_address_pool) |
| 338 | { |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 339 | if (slaac_address->sw_if_index != sw_if_index) |
| 340 | ; |
| 341 | else if (slaac_address->address_length != prefix_length) |
| 342 | ; |
| 343 | else if (!ip6_prefixes_equal (&slaac_address->address, dst_address, |
| 344 | prefix_length)) |
| 345 | ; |
| 346 | else |
| 347 | { |
| 348 | address_already_present = 1; |
| 349 | goto slaac_address_pool_foreach_out; |
| 350 | } |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 351 | } |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 352 | /* *INDENT-ON* */ |
| 353 | slaac_address_pool_foreach_out: |
| 354 | |
| 355 | if (address_already_present) |
| 356 | { |
| 357 | f64 remaining_life_time = slaac_address->due_time - current_time; |
| 358 | if (valid_time > 2 * 60 * 60 || valid_time > remaining_life_time) |
| 359 | slaac_address->due_time = current_time + valid_time; |
| 360 | else if (remaining_life_time > 2 * 60 * 60) |
| 361 | slaac_address->due_time = current_time + 2 * 60 * 60; |
| 362 | continue; |
| 363 | } |
| 364 | |
| 365 | if (valid_time == 0) |
| 366 | continue; |
| 367 | |
| 368 | due_time = current_time + valid_time; |
| 369 | |
| 370 | ip6_address_t addr; |
| 371 | addr.as_u64[0] = dst_address->as_u64[0]; |
| 372 | /* Invert the "u" bit */ |
| 373 | addr.as_u8[8] = mac[0] ^ (1 << 1); |
| 374 | addr.as_u8[9] = mac[1]; |
| 375 | addr.as_u8[10] = mac[2]; |
| 376 | addr.as_u8[11] = 0xFF; |
| 377 | addr.as_u8[12] = 0xFE; |
| 378 | addr.as_u8[13] = mac[3]; |
| 379 | addr.as_u8[14] = mac[4]; |
| 380 | addr.as_u8[15] = mac[5]; |
| 381 | |
| 382 | add_slaac_address (vm, sw_if_index, prefix_length, &addr, due_time); |
| 383 | } |
| 384 | |
| 385 | interrupt_process (); |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 386 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 387 | return; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static uword |
| 391 | rd_cp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 392 | { |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 393 | uword *event_data = 0; |
| 394 | rd_cp_main_t *rm = &rd_cp_main; |
| 395 | slaac_address_t *slaac_address; |
| 396 | default_route_t *default_route; |
| 397 | f64 sleep_time = 1e9; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 398 | f64 current_time; |
| 399 | f64 due_time; |
| 400 | |
| 401 | while (1) |
| 402 | { |
Juraj Sloboda | 5257452 | 2018-05-03 10:03:50 +0200 | [diff] [blame] | 403 | vlib_process_wait_for_event_or_clock (vm, sleep_time); |
| 404 | vlib_process_get_events (vm, &event_data); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 405 | |
| 406 | vec_reset_length (event_data); |
| 407 | |
| 408 | current_time = vlib_time_now (vm); |
| 409 | do |
| 410 | { |
| 411 | due_time = current_time + 1e9; |
Benoît Ganne | 79c9d36 | 2019-09-30 10:55:33 +0200 | [diff] [blame] | 412 | u32 index; |
| 413 | /* |
| 414 | * we do not use pool_foreach() to iterate over pool elements here |
| 415 | * as we are removing elements inside the loop body |
| 416 | */ |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 417 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 418 | pool_foreach_index (index, rm->slaac_address_pool) |
| 419 | { |
Benoît Ganne | 79c9d36 | 2019-09-30 10:55:33 +0200 | [diff] [blame] | 420 | slaac_address = pool_elt_at_index(rm->slaac_address_pool, index); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 421 | if (slaac_address->due_time > current_time) |
| 422 | { |
| 423 | if (slaac_address->due_time < due_time) |
| 424 | due_time = slaac_address->due_time; |
| 425 | } |
| 426 | else |
| 427 | { |
Benoît Ganne | 79c9d36 | 2019-09-30 10:55:33 +0200 | [diff] [blame] | 428 | u32 sw_if_index = slaac_address->sw_if_index; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 429 | remove_slaac_address (vm, slaac_address); |
| 430 | /* make sure ip6 stays enabled */ |
Neale Ranns | ec40a7d | 2020-04-23 07:36:12 +0000 | [diff] [blame] | 431 | ip6_link_enable (sw_if_index, NULL); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 432 | } |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 433 | } |
| 434 | pool_foreach_index (index, rm->default_route_pool) |
| 435 | { |
Benoît Ganne | 79c9d36 | 2019-09-30 10:55:33 +0200 | [diff] [blame] | 436 | default_route = pool_elt_at_index(rm->default_route_pool, index); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 437 | if (default_route->due_time > current_time) |
| 438 | { |
| 439 | if (default_route->due_time < due_time) |
| 440 | due_time = default_route->due_time; |
| 441 | } |
| 442 | else |
| 443 | remove_default_route (vm, default_route); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 444 | } |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 445 | /* *INDENT-ON* */ |
| 446 | current_time = vlib_time_now (vm); |
| 447 | } |
| 448 | while (due_time < current_time); |
| 449 | |
| 450 | sleep_time = due_time - current_time; |
| 451 | } |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | /* *INDENT-OFF* */ |
| 457 | VLIB_REGISTER_NODE (rd_cp_process_node) = { |
| 458 | .function = rd_cp_process, |
| 459 | .type = VLIB_NODE_TYPE_PROCESS, |
| 460 | .name = "rd-cp-process", |
| 461 | }; |
| 462 | /* *INDENT-ON* */ |
| 463 | |
| 464 | static void |
| 465 | interrupt_process (void) |
| 466 | { |
| 467 | rd_cp_main_t *rm = &rd_cp_main; |
| 468 | vlib_main_t *vm = rm->vlib_main; |
| 469 | |
| 470 | vlib_process_signal_event (vm, rd_cp_process_node.index, |
| 471 | RD_CP_EVENT_INTERRUPT, 0); |
| 472 | } |
| 473 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 474 | int |
| 475 | rd_cp_set_address_autoconfig (u32 sw_if_index, |
| 476 | u8 enable, u8 install_default_routes) |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 477 | { |
| 478 | rd_cp_main_t *rm = &rd_cp_main; |
| 479 | vlib_main_t *vm = rm->vlib_main; |
| 480 | vnet_main_t *vnm = rm->vnet_main; |
| 481 | interface_config_t *if_config; |
| 482 | interface_config_t empty_config = { 0, 0 }; |
| 483 | slaac_address_t *slaac_address; |
| 484 | default_route_t *default_route; |
| 485 | |
| 486 | if (!enable) |
| 487 | install_default_routes = 0; |
| 488 | |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 489 | if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index)) |
| 490 | { |
Juraj Sloboda | c65770d | 2018-05-15 11:43:56 +0200 | [diff] [blame] | 491 | vlib_log_warn (rm->log_class, "Invalid sw_if_index"); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 492 | return 1; |
| 493 | } |
| 494 | |
| 495 | if (!rm->enabled) |
| 496 | { |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 497 | /* process kickoff */ |
| 498 | interrupt_process (); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 499 | rm->enabled = 1; |
| 500 | } |
| 501 | |
| 502 | vec_validate_init_empty (rm->config_by_sw_if_index, sw_if_index, |
| 503 | empty_config); |
| 504 | if_config = &rm->config_by_sw_if_index[sw_if_index]; |
| 505 | |
| 506 | if (!if_config->enabled && enable) |
Neale Ranns | ec40a7d | 2020-04-23 07:36:12 +0000 | [diff] [blame] | 507 | ip6_link_enable (sw_if_index, NULL); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 508 | |
| 509 | if ((!if_config->enabled && enable) |
| 510 | || (!if_config->install_default_routes && install_default_routes)) |
| 511 | router_solicitation_start_stop (sw_if_index, 1); |
| 512 | else if (if_config->enabled && !enable) |
| 513 | router_solicitation_start_stop (sw_if_index, 0); |
| 514 | |
| 515 | if (if_config->enabled && !enable) |
| 516 | { |
| 517 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 518 | pool_foreach (slaac_address, rm->slaac_address_pool) |
| 519 | { |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 520 | remove_slaac_address (vm, slaac_address); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 521 | } |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 522 | /* *INDENT-ON* */ |
| 523 | } |
| 524 | if (if_config->install_default_routes && !install_default_routes) |
| 525 | { |
| 526 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 527 | pool_foreach (default_route, rm->default_route_pool) |
| 528 | { |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 529 | remove_default_route (vm, default_route); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 530 | } |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 531 | /* *INDENT-ON* */ |
| 532 | } |
| 533 | |
| 534 | if_config->enabled = enable; |
| 535 | if_config->install_default_routes = install_default_routes; |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static clib_error_t * |
| 541 | ip6_nd_address_autoconfig (vlib_main_t * vm, |
| 542 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 543 | { |
| 544 | rd_cp_main_t *rm = &rd_cp_main; |
| 545 | vnet_main_t *vnm = rm->vnet_main; |
| 546 | clib_error_t *error = 0; |
| 547 | u32 sw_if_index = ~0; |
| 548 | u8 enable = 1; |
| 549 | u8 default_route = 0; |
| 550 | |
| 551 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 552 | { |
| 553 | if (unformat |
| 554 | (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 555 | ; |
| 556 | if (unformat (input, "default-route")) |
| 557 | default_route = 1; |
| 558 | if (unformat (input, "disable")) |
| 559 | enable = 0; |
| 560 | else |
| 561 | break; |
| 562 | } |
| 563 | |
| 564 | if (sw_if_index != ~0) |
| 565 | { |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 566 | if (rd_cp_set_address_autoconfig (sw_if_index, enable, default_route) != |
| 567 | 0) |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 568 | error = clib_error_return (0, "Invalid sw_if_index"); |
| 569 | } |
| 570 | else |
| 571 | error = clib_error_return (0, "Missing sw_if_index"); |
| 572 | |
| 573 | return error; |
| 574 | } |
| 575 | |
| 576 | /*? |
| 577 | * This command is used to enable ND address autoconfiguration |
| 578 | * on particular interface including setting up default routes. |
| 579 | * |
| 580 | * @cliexpar |
| 581 | * @parblock |
| 582 | * Example of how to enable ND address autoconfiguration: |
| 583 | * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0} |
| 584 | * Example of how to enable ND address autoconfiguration |
| 585 | * with setting up default routes: |
| 586 | * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0 default-route} |
| 587 | * Example of how to disable ND address autoconfiguration: |
| 588 | * @cliexcmd{ip6 nd address autoconfig GigabitEthernet2/0/0 disable} |
| 589 | * @endparblock |
| 590 | ?*/ |
| 591 | /* *INDENT-OFF* */ |
| 592 | VLIB_CLI_COMMAND (ip6_nd_address_autoconfig_command, static) = { |
| 593 | .path = "ip6 nd address autoconfig", |
| 594 | .short_help = "ip6 nd address autoconfig <interface> [default-route|disable]", |
| 595 | .function = ip6_nd_address_autoconfig, |
| 596 | }; |
| 597 | /* *INDENT-ON* */ |
| 598 | |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 599 | static clib_error_t * |
| 600 | rd_cp_init (vlib_main_t * vm) |
| 601 | { |
| 602 | rd_cp_main_t *rm = &rd_cp_main; |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 603 | |
| 604 | rm->vlib_main = vm; |
| 605 | rm->vnet_main = vnet_get_main (); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 606 | rm->node_index = rd_cp_process_node.index; |
| 607 | |
Juraj Sloboda | c65770d | 2018-05-15 11:43:56 +0200 | [diff] [blame] | 608 | rm->log_class = vlib_log_register_class ("rd_cp", 0); |
| 609 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 610 | ip6_ra_report_register (ip6_ra_report_handler); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 611 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 612 | return (NULL); |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | VLIB_INIT_FUNCTION (rd_cp_init); |
| 616 | |
| 617 | /* |
| 618 | * fd.io coding-style-patch-verification: ON |
| 619 | * |
| 620 | * Local Variables: |
| 621 | * eval: (c-set-style "gnu") |
| 622 | * End: |
| 623 | */ |