Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +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 | |
| 16 | #include <vnet/vnet.h> |
| 17 | #include <vlibmemory/api.h> |
| 18 | #include <vnet/vnet_msg_enum.h> |
| 19 | #include <vnet/dhcp/dhcp6_packet.h> |
| 20 | #include <vnet/dhcp/dhcp6_pd_client_dp.h> |
| 21 | #include <vnet/ip/ip.h> |
| 22 | #include <vnet/ip/ip6.h> |
| 23 | #include <float.h> |
| 24 | #include <math.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #define vl_typedefs /* define message structures */ |
| 28 | #include <vnet/vnet_all_api_h.h> |
| 29 | #undef vl_typedefs |
| 30 | |
| 31 | #define vl_endianfun /* define message structures */ |
| 32 | #include <vnet/vnet_all_api_h.h> |
| 33 | #undef vl_endianfun |
| 34 | |
| 35 | #include <vlibapi/api_helper_macros.h> |
| 36 | |
| 37 | #define foreach_dhcp6_pd_client_cp_msg \ |
| 38 | _(DHCP6_PD_CLIENT_ENABLE_DISABLE, dhcp6_pd_client_enable_disable) \ |
| 39 | _(IP6_ADD_DEL_ADDRESS_USING_PREFIX, ip6_add_del_address_using_prefix) |
| 40 | |
| 41 | #define vl_api_dhcp6_pd_client_enable_disable_t_print vl_noop_handler |
| 42 | #define vl_api_ip6_add_del_address_using_prefix_t_print vl_noop_handler |
| 43 | |
| 44 | typedef struct |
| 45 | { |
| 46 | u32 prefix_group_index; |
| 47 | uword opaque_data; // used by prefix publisher |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 48 | ip6_address_t prefix; |
| 49 | u8 prefix_length; |
| 50 | u32 preferred_lt; |
| 51 | u32 valid_lt; |
| 52 | f64 due_time; |
| 53 | } prefix_info_t; |
| 54 | |
| 55 | typedef struct |
| 56 | { |
| 57 | u8 enabled; |
| 58 | u32 prefix_group_index; |
| 59 | u32 server_index; |
| 60 | u32 T1; |
| 61 | u32 T2; |
| 62 | f64 T1_due_time; |
| 63 | f64 T2_due_time; |
| 64 | u32 prefix_count; |
| 65 | u8 rebinding; |
| 66 | } client_state_t; |
| 67 | |
| 68 | typedef struct |
| 69 | { |
| 70 | client_state_t *client_state_by_sw_if_index; |
| 71 | clib_bitmap_t *prefix_ownership_bitmap; |
| 72 | u32 n_clients; |
| 73 | f64 max_valid_due_time; |
| 74 | |
| 75 | /* convenience */ |
| 76 | vlib_main_t *vlib_main; |
| 77 | vnet_main_t *vnet_main; |
| 78 | api_main_t *api_main; |
| 79 | u32 node_index; |
| 80 | } dhcp6_pd_client_cp_main_t; |
| 81 | |
| 82 | static dhcp6_pd_client_cp_main_t dhcp6_pd_client_cp_main; |
| 83 | |
| 84 | typedef struct |
| 85 | { |
| 86 | prefix_info_t *prefix_pool; |
| 87 | const u8 **prefix_group_name_by_index; |
| 88 | } ip6_prefix_main_t; |
| 89 | |
| 90 | static ip6_prefix_main_t ip6_prefix_main; |
| 91 | |
| 92 | typedef struct |
| 93 | { |
| 94 | /* config */ |
| 95 | u32 sw_if_index; |
| 96 | u32 prefix_group_index; |
| 97 | ip6_address_t address; |
| 98 | u8 prefix_length; |
| 99 | |
| 100 | /* state */ |
| 101 | u8 configured_in_data_plane; |
| 102 | } ip6_address_info_t; |
| 103 | |
| 104 | typedef struct |
| 105 | { |
| 106 | ip6_address_info_t *addresses; |
| 107 | u32 *active_prefix_index_by_prefix_group_index; |
| 108 | } ip6_address_with_prefix_main_t; |
| 109 | |
| 110 | static ip6_address_with_prefix_main_t ip6_address_with_prefix_main; |
| 111 | |
| 112 | enum |
| 113 | { |
| 114 | DHCPV6_PD_EVENT_INTERRUPT, |
| 115 | DHCPV6_PD_EVENT_DISABLE, |
| 116 | }; |
| 117 | |
| 118 | static_always_inline u32 |
| 119 | active_prefix_index_by_prefix_group_index_get (u32 prefix_group_index) |
| 120 | { |
| 121 | ip6_address_with_prefix_main_t *apm = &ip6_address_with_prefix_main; |
| 122 | |
| 123 | if (prefix_group_index >= |
| 124 | vec_len (apm->active_prefix_index_by_prefix_group_index)) |
| 125 | return ~0; |
| 126 | |
| 127 | return apm->active_prefix_index_by_prefix_group_index[prefix_group_index]; |
| 128 | } |
| 129 | |
| 130 | static_always_inline void |
| 131 | active_prefix_index_by_prefix_group_index_set (u32 prefix_group_index, |
| 132 | u32 prefix_index) |
| 133 | { |
| 134 | ip6_address_with_prefix_main_t *apm = &ip6_address_with_prefix_main; |
| 135 | static const u32 empty = ~0; |
| 136 | |
| 137 | ASSERT (prefix_group_index != ~0); |
| 138 | |
| 139 | if (prefix_index == ~0 |
| 140 | && prefix_group_index >= |
| 141 | vec_len (apm->active_prefix_index_by_prefix_group_index)) |
| 142 | return; |
| 143 | |
| 144 | vec_validate_init_empty (apm->active_prefix_index_by_prefix_group_index, |
| 145 | prefix_group_index, empty); |
| 146 | apm->active_prefix_index_by_prefix_group_index[prefix_group_index] = |
| 147 | prefix_index; |
| 148 | } |
| 149 | |
| 150 | static_always_inline u8 |
| 151 | is_dhcpv6_pd_prefix (prefix_info_t * prefix_info) |
| 152 | { |
| 153 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 154 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 155 | u32 prefix_index; |
| 156 | |
| 157 | prefix_index = prefix_info - pm->prefix_pool; |
| 158 | return clib_bitmap_get (rm->prefix_ownership_bitmap, prefix_index); |
| 159 | } |
| 160 | |
| 161 | static_always_inline void |
| 162 | set_is_dhcpv6_pd_prefix (prefix_info_t * prefix_info, u8 value) |
| 163 | { |
| 164 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 165 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 166 | u32 prefix_index; |
| 167 | |
| 168 | prefix_index = prefix_info - pm->prefix_pool; |
| 169 | rm->prefix_ownership_bitmap = |
| 170 | clib_bitmap_set (rm->prefix_ownership_bitmap, prefix_index, value); |
| 171 | } |
| 172 | |
| 173 | static void |
| 174 | cp_ip6_address_prefix_add_del_handler (u32 prefix_index, u8 is_add); |
| 175 | |
| 176 | static void |
| 177 | notify_prefix_add_del (u32 prefix_index, u8 is_add) |
| 178 | { |
| 179 | // TODO: use registries |
| 180 | cp_ip6_address_prefix_add_del_handler (prefix_index, is_add); |
| 181 | } |
| 182 | |
| 183 | static void |
| 184 | send_client_message_start_stop (u32 sw_if_index, u32 server_index, |
| 185 | u8 msg_type, prefix_info_t * prefix_list, |
| 186 | u8 start) |
| 187 | { |
| 188 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 189 | dhcp6_pd_send_client_message_params_t params = { 0, }; |
| 190 | dhcp6_pd_send_client_message_params_prefix_t *prefixes = 0, *pref; |
| 191 | u32 i; |
| 192 | |
| 193 | ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) && |
| 194 | rm->client_state_by_sw_if_index[sw_if_index].enabled); |
| 195 | client_state_t *client_state = |
| 196 | &rm->client_state_by_sw_if_index[sw_if_index]; |
| 197 | |
| 198 | params.sw_if_index = sw_if_index; |
| 199 | params.server_index = server_index; |
| 200 | params.msg_type = msg_type; |
| 201 | if (start) |
| 202 | { |
| 203 | if (msg_type == DHCPV6_MSG_SOLICIT) |
| 204 | { |
| 205 | params.irt = 1; |
| 206 | params.mrt = 120; |
| 207 | } |
| 208 | else if (msg_type == DHCPV6_MSG_REQUEST) |
| 209 | { |
| 210 | params.irt = 1; |
| 211 | params.mrt = 30; |
| 212 | params.mrc = 10; |
| 213 | } |
| 214 | else if (msg_type == DHCPV6_MSG_RENEW) |
| 215 | { |
| 216 | params.irt = 10; |
| 217 | params.mrt = 600; |
| 218 | f64 current_time = vlib_time_now (rm->vlib_main); |
| 219 | i32 diff_time = client_state->T2 - current_time; |
| 220 | if (diff_time < 0) |
| 221 | diff_time = 0; |
| 222 | params.mrd = diff_time; |
| 223 | } |
| 224 | else if (msg_type == DHCPV6_MSG_REBIND) |
| 225 | { |
| 226 | params.irt = 10; |
| 227 | params.mrt = 600; |
| 228 | f64 current_time = vlib_time_now (rm->vlib_main); |
| 229 | i32 diff_time = rm->max_valid_due_time - current_time; |
| 230 | if (diff_time < 0) |
| 231 | diff_time = 0; |
| 232 | params.mrd = diff_time; |
| 233 | } |
| 234 | else if (msg_type == DHCPV6_MSG_RELEASE) |
| 235 | { |
| 236 | params.mrc = 1; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | params.T1 = 0; |
| 241 | params.T2 = 0; |
| 242 | if (vec_len (prefix_list) != 0) |
| 243 | vec_validate (prefixes, vec_len (prefix_list) - 1); |
| 244 | for (i = 0; i < vec_len (prefix_list); i++) |
| 245 | { |
| 246 | prefix_info_t *prefix = &prefix_list[i]; |
| 247 | pref = &prefixes[i]; |
| 248 | pref->valid_lt = prefix->valid_lt; |
| 249 | pref->preferred_lt = prefix->preferred_lt; |
| 250 | pref->prefix = prefix->prefix; |
| 251 | pref->prefix_length = prefix->prefix_length; |
| 252 | } |
| 253 | params.prefixes = prefixes; |
| 254 | |
| 255 | dhcp6_pd_send_client_message (rm->vlib_main, sw_if_index, !start, ¶ms); |
| 256 | |
| 257 | vec_free (params.prefixes); |
| 258 | } |
| 259 | |
| 260 | static void interrupt_process (void); |
| 261 | |
| 262 | static u32 |
| 263 | ip6_enable (u32 sw_if_index) |
| 264 | { |
| 265 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 266 | clib_error_t *rv; |
| 267 | |
| 268 | rv = enable_ip6_interface (rm->vlib_main, sw_if_index); |
| 269 | |
| 270 | return rv != 0; |
| 271 | } |
| 272 | |
| 273 | static u8 |
| 274 | ip6_prefixes_equal (ip6_address_t * prefix1, ip6_address_t * prefix2, u8 len) |
| 275 | { |
| 276 | if (len >= 64) |
| 277 | { |
| 278 | if (prefix1->as_u64[0] != prefix2->as_u64[0]) |
| 279 | return 0; |
| 280 | if (len == 64) |
| 281 | return 1; |
| 282 | return clib_net_to_host_u64 (prefix1->as_u64[1]) >> (128 - len) == |
| 283 | clib_net_to_host_u64 (prefix2->as_u64[1]) >> (128 - len); |
| 284 | } |
| 285 | return clib_net_to_host_u64 (prefix1->as_u64[0]) >> (64 - len) == |
| 286 | clib_net_to_host_u64 (prefix2->as_u64[0]) >> (64 - len); |
| 287 | } |
| 288 | |
| 289 | static clib_error_t * |
| 290 | dhcp6_pd_reply_event_handler (vl_api_dhcp6_pd_reply_event_t * mp) |
| 291 | { |
| 292 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 293 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 294 | vlib_main_t *vm = rm->vlib_main; |
| 295 | client_state_t *client_state; |
| 296 | ip6_address_t *prefix; |
| 297 | u32 sw_if_index; |
| 298 | u32 n_prefixes; |
| 299 | vl_api_dhcp6_pd_prefix_info_t *api_prefix; |
| 300 | u32 inner_status_code; |
| 301 | u32 status_code; |
| 302 | u32 server_index; |
| 303 | f64 current_time; |
| 304 | clib_error_t *error = 0; |
| 305 | u32 i; |
| 306 | |
| 307 | current_time = vlib_time_now (vm); |
| 308 | |
| 309 | sw_if_index = ntohl (mp->sw_if_index); |
| 310 | |
| 311 | if (sw_if_index >= vec_len (rm->client_state_by_sw_if_index)) |
| 312 | return 0; |
| 313 | |
| 314 | client_state = &rm->client_state_by_sw_if_index[sw_if_index]; |
| 315 | |
| 316 | if (!client_state->enabled) |
| 317 | return 0; |
| 318 | |
| 319 | server_index = ntohl (mp->server_index); |
| 320 | |
| 321 | n_prefixes = ntohl (mp->n_prefixes); |
| 322 | |
| 323 | inner_status_code = ntohs (mp->inner_status_code); |
| 324 | status_code = ntohs (mp->status_code); |
| 325 | |
| 326 | if (mp->msg_type == DHCPV6_MSG_ADVERTISE |
| 327 | && client_state->server_index == ~0) |
| 328 | { |
| 329 | prefix_info_t *prefix_list = 0, *prefix_info; |
| 330 | u8 prefix_length; |
| 331 | |
| 332 | if (inner_status_code == DHCPV6_STATUS_NOPREFIX_AVAIL) |
| 333 | { |
| 334 | clib_warning |
| 335 | ("Advertise message arrived with NoPrefixAvail status code"); |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | if (n_prefixes > 0) |
| 340 | vec_validate (prefix_list, n_prefixes - 1); |
| 341 | for (i = 0; i < n_prefixes; i++) |
| 342 | { |
| 343 | api_prefix = &mp->prefixes[i]; |
| 344 | prefix = (ip6_address_t *) api_prefix->prefix; |
| 345 | prefix_length = api_prefix->prefix_length; |
| 346 | |
| 347 | prefix_info = &prefix_list[i]; |
| 348 | prefix_info->prefix = *prefix; |
| 349 | prefix_info->prefix_length = prefix_length; |
| 350 | prefix_info->preferred_lt = 0; |
| 351 | prefix_info->valid_lt = 0; |
| 352 | } |
| 353 | |
| 354 | client_state->server_index = server_index; |
| 355 | |
| 356 | send_client_message_start_stop (sw_if_index, server_index, |
| 357 | DHCPV6_MSG_REQUEST, prefix_list, 1); |
| 358 | vec_free (prefix_list); |
| 359 | } |
| 360 | |
| 361 | if (mp->msg_type != DHCPV6_MSG_REPLY) |
| 362 | return 0; |
| 363 | |
| 364 | if (!client_state->rebinding && client_state->server_index != server_index) |
| 365 | { |
| 366 | clib_warning ("Reply message arrived with Server ID different " |
Juraj Sloboda | dd3b8f7 | 2018-05-04 14:20:06 +0200 | [diff] [blame] | 367 | "from that in Request or Renew message"); |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | if (inner_status_code == DHCPV6_STATUS_NOPREFIX_AVAIL) |
| 372 | { |
| 373 | clib_warning ("Reply message arrived with NoPrefixAvail status code"); |
| 374 | if (n_prefixes > 0) |
| 375 | { |
| 376 | clib_warning |
| 377 | ("Invalid Reply message arrived: It contains NoPrefixAvail " |
| 378 | "status code but also contains prefixes"); |
| 379 | return 0; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | if (status_code == DHCPV6_STATUS_UNSPEC_FAIL) |
| 384 | { |
| 385 | clib_warning ("Reply message arrived with UnspecFail status code"); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | send_client_message_start_stop (sw_if_index, server_index, |
| 390 | mp->msg_type, 0, 0); |
| 391 | |
| 392 | for (i = 0; i < n_prefixes; i++) |
| 393 | { |
| 394 | prefix_info_t *prefix_info = 0; |
| 395 | u8 prefix_length; |
| 396 | u32 valid_time; |
| 397 | u32 preferred_time; |
| 398 | |
| 399 | api_prefix = &mp->prefixes[i]; |
| 400 | |
| 401 | prefix = (ip6_address_t *) api_prefix->prefix; |
| 402 | prefix_length = api_prefix->prefix_length; |
| 403 | |
| 404 | if (ip6_address_is_link_local_unicast (prefix)) |
| 405 | continue; |
| 406 | |
| 407 | valid_time = ntohl (api_prefix->valid_time); |
| 408 | preferred_time = ntohl (api_prefix->preferred_time); |
| 409 | prefix_length = api_prefix->prefix_length; |
| 410 | |
| 411 | if (preferred_time > valid_time) |
| 412 | continue; |
| 413 | |
| 414 | u8 address_prefix_present = 0; |
| 415 | /* *INDENT-OFF* */ |
| 416 | pool_foreach (prefix_info, pm->prefix_pool, |
| 417 | ({ |
| 418 | if (is_dhcpv6_pd_prefix (prefix_info) && |
| 419 | prefix_info->opaque_data == sw_if_index && |
| 420 | prefix_info->prefix_length == prefix_length && |
| 421 | ip6_prefixes_equal (&prefix_info->prefix, prefix, prefix_length)) |
| 422 | { |
| 423 | address_prefix_present = 1; |
| 424 | goto prefix_pool_foreach_out; |
| 425 | } |
| 426 | })); |
| 427 | /* *INDENT-ON* */ |
| 428 | prefix_pool_foreach_out: |
| 429 | |
| 430 | if (address_prefix_present) |
| 431 | { |
| 432 | prefix_info->preferred_lt = preferred_time; |
| 433 | prefix_info->valid_lt = valid_time; |
| 434 | prefix_info->due_time = current_time + valid_time; |
Juraj Sloboda | dd3b8f7 | 2018-05-04 14:20:06 +0200 | [diff] [blame] | 435 | if (prefix_info->due_time > rm->max_valid_due_time) |
| 436 | rm->max_valid_due_time = prefix_info->due_time; |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 437 | continue; |
| 438 | } |
| 439 | |
| 440 | if (valid_time == 0) |
| 441 | continue; |
| 442 | |
| 443 | pool_get (pm->prefix_pool, prefix_info); |
| 444 | prefix_info->prefix_group_index = client_state->prefix_group_index; |
| 445 | set_is_dhcpv6_pd_prefix (prefix_info, 1); |
| 446 | prefix_info->opaque_data = sw_if_index; |
| 447 | prefix_info->prefix_length = prefix_length; |
| 448 | prefix_info->prefix = *prefix; |
| 449 | prefix_info->preferred_lt = preferred_time; |
| 450 | prefix_info->valid_lt = valid_time; |
| 451 | prefix_info->due_time = current_time + valid_time; |
Juraj Sloboda | dd3b8f7 | 2018-05-04 14:20:06 +0200 | [diff] [blame] | 452 | if (prefix_info->due_time > rm->max_valid_due_time) |
| 453 | rm->max_valid_due_time = prefix_info->due_time; |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 454 | rm->client_state_by_sw_if_index[sw_if_index].prefix_count++; |
| 455 | |
| 456 | u32 prefix_index = prefix_info - pm->prefix_pool; |
| 457 | notify_prefix_add_del (prefix_index, 1); |
| 458 | } |
| 459 | |
| 460 | client_state->server_index = server_index; |
| 461 | client_state->T1 = ntohl (mp->T1); |
| 462 | client_state->T2 = ntohl (mp->T2); |
| 463 | if (client_state->T1 != 0) |
| 464 | client_state->T1_due_time = current_time + client_state->T1; |
| 465 | if (client_state->T2 != 0) |
| 466 | client_state->T2_due_time = current_time + client_state->T2; |
| 467 | client_state->rebinding = 0; |
| 468 | |
| 469 | interrupt_process (); |
| 470 | |
| 471 | return error; |
| 472 | } |
| 473 | |
| 474 | static prefix_info_t * |
| 475 | create_prefix_list (u32 sw_if_index) |
| 476 | { |
| 477 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 478 | prefix_info_t *prefix_info, *prefix_list = 0;; |
| 479 | |
| 480 | /* *INDENT-OFF* */ |
| 481 | pool_foreach (prefix_info, pm->prefix_pool, |
| 482 | ({ |
| 483 | if (is_dhcpv6_pd_prefix (prefix_info) && |
| 484 | prefix_info->opaque_data == sw_if_index) |
| 485 | { |
| 486 | u32 pos = vec_len (prefix_list); |
| 487 | vec_validate (prefix_list, pos); |
| 488 | clib_memcpy (&prefix_list[pos], prefix_info, sizeof (*prefix_info)); |
| 489 | } |
| 490 | })); |
| 491 | /* *INDENT-ON* */ |
| 492 | |
| 493 | return prefix_list; |
| 494 | } |
| 495 | |
| 496 | VNET_DHCP6_PD_REPLY_EVENT_FUNCTION (dhcp6_pd_reply_event_handler); |
| 497 | |
| 498 | static uword |
| 499 | dhcp6_pd_client_cp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 500 | vlib_frame_t * f) |
| 501 | { |
| 502 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 503 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 504 | prefix_info_t *prefix_info; |
| 505 | client_state_t *client_state; |
| 506 | f64 sleep_time = 1e9; |
| 507 | f64 current_time; |
| 508 | f64 due_time; |
| 509 | uword event_type; |
| 510 | uword *event_data = 0; |
| 511 | int i; |
| 512 | |
| 513 | while (1) |
| 514 | { |
| 515 | vlib_process_wait_for_event_or_clock (vm, sleep_time); |
| 516 | event_type = vlib_process_get_events (vm, &event_data); |
| 517 | vec_reset_length (event_data); |
| 518 | |
| 519 | if (event_type == DHCPV6_PD_EVENT_DISABLE) |
| 520 | { |
| 521 | vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_DISABLED); |
| 522 | sleep_time = 1e9; |
| 523 | continue; |
| 524 | } |
| 525 | |
| 526 | current_time = vlib_time_now (vm); |
| 527 | do |
| 528 | { |
| 529 | due_time = current_time + 1e9; |
| 530 | /* *INDENT-OFF* */ |
| 531 | pool_foreach (prefix_info, pm->prefix_pool, |
| 532 | ({ |
| 533 | if (is_dhcpv6_pd_prefix (prefix_info)) |
| 534 | { |
| 535 | if (prefix_info->due_time > current_time) |
| 536 | { |
| 537 | if (prefix_info->due_time < due_time) |
| 538 | due_time = prefix_info->due_time; |
| 539 | } |
| 540 | else |
| 541 | { |
| 542 | u32 prefix_index = prefix_info - pm->prefix_pool; |
| 543 | notify_prefix_add_del (prefix_index, 0); |
| 544 | u32 sw_if_index = prefix_info->opaque_data; |
| 545 | set_is_dhcpv6_pd_prefix (prefix_info, 0); |
| 546 | pool_put (pm->prefix_pool, prefix_info); |
| 547 | client_state = &rm->client_state_by_sw_if_index[sw_if_index]; |
| 548 | if (--client_state->prefix_count == 0) |
| 549 | { |
| 550 | client_state->rebinding = 0; |
| 551 | client_state->server_index = ~0; |
| 552 | send_client_message_start_stop (sw_if_index, ~0, |
| 553 | DHCPV6_MSG_SOLICIT, |
| 554 | 0, 1); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | })); |
| 559 | /* *INDENT-ON* */ |
| 560 | for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++) |
| 561 | { |
| 562 | client_state_t *cs = &rm->client_state_by_sw_if_index[i]; |
| 563 | if (cs->enabled && cs->server_index != ~0) |
| 564 | { |
| 565 | if (cs->T2_due_time > current_time) |
| 566 | { |
| 567 | if (cs->T2_due_time < due_time) |
| 568 | due_time = cs->T2_due_time; |
| 569 | if (cs->T1_due_time > current_time) |
| 570 | { |
| 571 | if (cs->T1_due_time < due_time) |
| 572 | due_time = cs->T1_due_time; |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | cs->T1_due_time = DBL_MAX; |
| 577 | prefix_info_t *prefix_list; |
| 578 | prefix_list = create_prefix_list (i); |
| 579 | send_client_message_start_stop (i, cs->server_index, |
| 580 | DHCPV6_MSG_RENEW, |
| 581 | prefix_list, 1); |
| 582 | vec_free (prefix_list); |
| 583 | } |
| 584 | } |
| 585 | else |
| 586 | { |
| 587 | cs->T2_due_time = DBL_MAX; |
| 588 | prefix_info_t *prefix_list; |
| 589 | prefix_list = create_prefix_list (i); |
| 590 | cs->rebinding = 1; |
| 591 | send_client_message_start_stop (i, ~0, |
| 592 | DHCPV6_MSG_REBIND, |
| 593 | prefix_list, 1); |
| 594 | vec_free (prefix_list); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | current_time = vlib_time_now (vm); |
| 599 | } |
| 600 | while (due_time < current_time); |
| 601 | |
| 602 | sleep_time = due_time - current_time; |
| 603 | } |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | /* *INDENT-OFF* */ |
| 609 | VLIB_REGISTER_NODE (dhcp6_pd_client_cp_process_node) = { |
| 610 | .function = dhcp6_pd_client_cp_process, |
| 611 | .type = VLIB_NODE_TYPE_PROCESS, |
| 612 | .name = "dhcp6-pd-client-cp-process", |
| 613 | }; |
| 614 | /* *INDENT-ON* */ |
| 615 | |
| 616 | static void |
| 617 | interrupt_process (void) |
| 618 | { |
| 619 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 620 | vlib_main_t *vm = rm->vlib_main; |
| 621 | |
| 622 | vlib_process_signal_event (vm, dhcp6_pd_client_cp_process_node.index, |
| 623 | DHCPV6_PD_EVENT_INTERRUPT, 0); |
| 624 | } |
| 625 | |
| 626 | static void |
| 627 | disable_process (void) |
| 628 | { |
| 629 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 630 | vlib_main_t *vm = rm->vlib_main; |
| 631 | |
| 632 | vlib_process_signal_event (vm, dhcp6_pd_client_cp_process_node.index, |
| 633 | DHCPV6_PD_EVENT_DISABLE, 0); |
| 634 | } |
| 635 | |
| 636 | static void |
| 637 | enable_process (void) |
| 638 | { |
| 639 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 640 | vlib_main_t *vm = rm->vlib_main; |
| 641 | vlib_node_t *node; |
| 642 | |
| 643 | node = vec_elt (vm->node_main.nodes, rm->node_index); |
| 644 | |
| 645 | vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_POLLING); |
| 646 | vlib_start_process (vm, node->runtime_index); |
| 647 | } |
| 648 | |
| 649 | static u32 |
| 650 | cp_ip6_construct_address (ip6_address_info_t * address_info, u32 prefix_index, |
| 651 | ip6_address_t * r_addr) |
| 652 | { |
| 653 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 654 | prefix_info_t *prefix; |
| 655 | u64 mask, addr0, pref; |
| 656 | |
| 657 | addr0 = clib_net_to_host_u64 (address_info->address.as_u64[0]); |
| 658 | prefix = &pm->prefix_pool[prefix_index]; |
| 659 | if (prefix->prefix_length > 64) |
| 660 | { |
| 661 | clib_warning ("Prefix length is bigger that 64 bits"); |
| 662 | return 1; |
| 663 | } |
Juraj Sloboda | d9778c2 | 2018-06-12 10:21:05 +0200 | [diff] [blame] | 664 | mask = ((u64) 1 << (64 - prefix->prefix_length)) - 1; |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 665 | addr0 &= mask; |
| 666 | pref = clib_host_to_net_u64 (prefix->prefix.as_u64[0]); |
| 667 | pref &= ~mask; |
| 668 | addr0 |= pref; |
| 669 | r_addr->as_u64[0] = clib_host_to_net_u64 (addr0); |
| 670 | r_addr->as_u64[1] = address_info->address.as_u64[1]; |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | static void |
| 676 | cp_ip6_address_add_del_now (ip6_address_info_t * address_info, u8 is_add) |
| 677 | { |
| 678 | vlib_main_t *vm = vlib_get_main (); |
| 679 | u32 prefix_index; |
| 680 | ip6_address_t addr; |
| 681 | clib_error_t *error; |
| 682 | |
| 683 | if (address_info->prefix_group_index != ~0) |
| 684 | prefix_index = |
| 685 | active_prefix_index_by_prefix_group_index_get |
| 686 | (address_info->prefix_group_index); |
| 687 | else |
| 688 | prefix_index = ~0; |
| 689 | |
| 690 | if (is_add && !address_info->configured_in_data_plane) |
| 691 | { |
| 692 | if (prefix_index != ~0) |
| 693 | { |
| 694 | if (cp_ip6_construct_address (address_info, prefix_index, &addr) != |
| 695 | 0) |
| 696 | return; |
| 697 | error = |
| 698 | ip6_add_del_interface_address (vm, address_info->sw_if_index, |
| 699 | &addr, address_info->prefix_length, |
| 700 | 0 /* add */ ); |
| 701 | if (error) |
| 702 | clib_warning ("Failed adding IPv6 address: %U", |
| 703 | format_clib_error, error); |
| 704 | else |
| 705 | address_info->configured_in_data_plane = 1; |
| 706 | } |
| 707 | else |
| 708 | { |
| 709 | if (address_info->prefix_group_index == ~0) |
| 710 | { |
| 711 | error = |
| 712 | ip6_add_del_interface_address (vm, address_info->sw_if_index, |
| 713 | &address_info->address, |
| 714 | address_info->prefix_length, |
| 715 | 0 /* add */ ); |
| 716 | if (error) |
| 717 | clib_warning ("Failed adding IPv6 address: %U", |
| 718 | format_clib_error, error); |
| 719 | else |
| 720 | address_info->configured_in_data_plane = 1; |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | else if (!is_add && address_info->configured_in_data_plane) |
| 725 | { |
| 726 | if (prefix_index == ~0) |
| 727 | { |
| 728 | if (address_info->prefix_group_index == ~0) |
| 729 | { |
| 730 | error = |
| 731 | ip6_add_del_interface_address (vm, address_info->sw_if_index, |
| 732 | &address_info->address, |
| 733 | address_info->prefix_length, |
| 734 | 1 /* del */ ); |
| 735 | if (error) |
| 736 | clib_warning ("Failed deleting IPv6 address: %U", |
| 737 | format_clib_error, error); |
| 738 | address_info->configured_in_data_plane = 0; |
| 739 | } |
| 740 | else |
| 741 | clib_warning ("Deleting address with prefix " |
| 742 | "but active prefix index is not set"); |
| 743 | } |
| 744 | else |
| 745 | { |
| 746 | if (cp_ip6_construct_address (address_info, prefix_index, &addr) != |
| 747 | 0) |
| 748 | return; |
| 749 | error = |
| 750 | ip6_add_del_interface_address (vm, address_info->sw_if_index, |
| 751 | &addr, address_info->prefix_length, |
| 752 | 1 /* del */ ); |
| 753 | if (error) |
| 754 | clib_warning ("Failed deleting IPv6 address: %U", |
| 755 | format_clib_error, error); |
| 756 | address_info->configured_in_data_plane = 0; |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | static u32 |
| 762 | cp_ip6_address_find_new_active_prefix (u32 prefix_group_index, |
| 763 | u32 ignore_prefix_index) |
| 764 | { |
| 765 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 766 | prefix_info_t *prefix_info; |
| 767 | |
| 768 | /* *INDENT-OFF* */ |
| 769 | pool_foreach (prefix_info, pm->prefix_pool, |
| 770 | ({ |
| 771 | if (prefix_info->prefix_group_index == prefix_group_index && |
| 772 | prefix_info - pm->prefix_pool != ignore_prefix_index) |
| 773 | return prefix_info - pm->prefix_pool; |
| 774 | })); |
| 775 | /* *INDENT-ON* */ |
| 776 | return ~0; |
| 777 | } |
| 778 | |
| 779 | static void |
| 780 | cp_ip6_address_prefix_add_del_handler (u32 prefix_index, u8 is_add) |
| 781 | { |
| 782 | ip6_address_with_prefix_main_t *apm = &ip6_address_with_prefix_main; |
| 783 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 784 | ip6_address_info_t *address_info; |
| 785 | prefix_info_t *prefix; |
| 786 | u32 new_prefix_index; |
| 787 | u32 prefix_group_index; |
| 788 | u32 i; |
| 789 | |
| 790 | prefix = &pm->prefix_pool[prefix_index]; |
| 791 | prefix_group_index = prefix->prefix_group_index; |
| 792 | |
| 793 | if (is_add) |
| 794 | { |
| 795 | if (active_prefix_index_by_prefix_group_index_get |
| 796 | (prefix_group_index) == ~0) |
| 797 | { |
| 798 | active_prefix_index_by_prefix_group_index_set |
| 799 | (prefix_group_index, prefix_index); |
| 800 | for (i = 0; i < vec_len (apm->addresses); i++) |
| 801 | { |
| 802 | address_info = &apm->addresses[i]; |
| 803 | if (address_info->prefix_group_index == prefix_group_index) |
| 804 | cp_ip6_address_add_del_now (address_info, 1 /* add */ ); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | else |
| 809 | { |
| 810 | if (active_prefix_index_by_prefix_group_index_get |
| 811 | (prefix_group_index) == prefix_index) |
| 812 | { |
| 813 | for (i = 0; i < vec_len (apm->addresses); i++) |
| 814 | { |
| 815 | address_info = &apm->addresses[i]; |
| 816 | if (address_info->prefix_group_index == prefix_group_index) |
| 817 | cp_ip6_address_add_del_now (address_info, 0 /* del */ ); |
| 818 | } |
| 819 | active_prefix_index_by_prefix_group_index_set |
| 820 | (prefix_group_index, ~0); |
| 821 | new_prefix_index = |
| 822 | cp_ip6_address_find_new_active_prefix (prefix_group_index, |
| 823 | prefix_index); |
| 824 | if (new_prefix_index != ~0) |
| 825 | { |
| 826 | active_prefix_index_by_prefix_group_index_set |
| 827 | (prefix_group_index, new_prefix_index); |
| 828 | for (i = 0; i < vec_len (apm->addresses); i++) |
| 829 | { |
| 830 | address_info = &apm->addresses[i]; |
| 831 | if (address_info->prefix_group_index == prefix_group_index) |
| 832 | cp_ip6_address_add_del_now (address_info, 1 /* add */ ); |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | static u32 |
| 840 | prefix_group_find_or_create (const u8 * name, u8 create) |
| 841 | { |
| 842 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 843 | u32 free_index = ~0; |
| 844 | u8 *name_dup; |
| 845 | u32 i; |
| 846 | |
| 847 | for (i = 0; i < vec_len (pm->prefix_group_name_by_index); i++) |
| 848 | { |
| 849 | if (pm->prefix_group_name_by_index[i] == 0) |
| 850 | free_index = i; |
| 851 | else if (0 == |
| 852 | strcmp ((const char *) pm->prefix_group_name_by_index[i], |
| 853 | (const char *) name)) |
| 854 | return i; |
| 855 | } |
| 856 | if (!create) |
| 857 | return ~0; |
| 858 | name_dup = (u8 *) strdup ((const char *) name); |
| 859 | if (free_index != ~0) |
| 860 | { |
| 861 | pm->prefix_group_name_by_index[free_index] = name_dup; |
| 862 | return free_index; |
| 863 | } |
| 864 | else |
| 865 | { |
| 866 | vec_add1 (pm->prefix_group_name_by_index, name_dup); |
| 867 | return vec_len (pm->prefix_group_name_by_index) - 1; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | static int |
| 872 | cp_ip6_address_add_del (u32 sw_if_index, const u8 * prefix_group, |
| 873 | ip6_address_t address, u8 prefix_length, u8 is_add) |
| 874 | { |
| 875 | |
| 876 | ip6_address_with_prefix_main_t *apm = &ip6_address_with_prefix_main; |
Juraj Sloboda | d9778c2 | 2018-06-12 10:21:05 +0200 | [diff] [blame] | 877 | vnet_main_t *vnm = vnet_get_main (); |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 878 | ip6_address_info_t *address_info; |
| 879 | u32 prefix_group_index; |
| 880 | u32 n; |
| 881 | |
Juraj Sloboda | d9778c2 | 2018-06-12 10:21:05 +0200 | [diff] [blame] | 882 | if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index)) |
| 883 | { |
| 884 | clib_warning ("Invalid sw_if_index"); |
| 885 | return VNET_API_ERROR_INVALID_VALUE; |
| 886 | } |
| 887 | |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 888 | if (prefix_group != 0 && prefix_group[0] != '\0') |
| 889 | { |
| 890 | if (strnlen ((const char *) prefix_group, 64) == 64) |
| 891 | return VNET_API_ERROR_INVALID_VALUE; |
| 892 | |
| 893 | prefix_group_index = prefix_group_find_or_create (prefix_group, 1); |
| 894 | } |
| 895 | else |
| 896 | prefix_group_index = ~0; |
| 897 | |
| 898 | n = vec_len (apm->addresses); |
| 899 | |
| 900 | vec_foreach (address_info, apm->addresses) |
| 901 | { |
| 902 | if (address_info->sw_if_index == sw_if_index && |
| 903 | address_info->prefix_group_index == prefix_group_index && |
| 904 | address_info->prefix_length == prefix_length && |
| 905 | 0 == memcmp (&address_info->address, &address, 16)) |
| 906 | { |
| 907 | if (is_add) |
| 908 | return VNET_API_ERROR_DUPLICATE_IF_ADDRESS; |
| 909 | cp_ip6_address_add_del_now (address_info, 0 /* del */ ); |
| 910 | *address_info = apm->addresses[n - 1]; |
| 911 | _vec_len (apm->addresses) = n - 1; |
| 912 | return 0; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | if (!is_add) |
| 917 | return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE; |
| 918 | |
| 919 | vec_validate (apm->addresses, n); |
| 920 | address_info = &apm->addresses[n]; |
| 921 | address_info->sw_if_index = sw_if_index; |
| 922 | address_info->prefix_group_index = prefix_group_index; |
| 923 | address_info->address = address; |
| 924 | address_info->prefix_length = prefix_length; |
| 925 | cp_ip6_address_add_del_now (address_info, 1 /* add */ ); |
| 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | static void |
| 931 | vl_api_ip6_add_del_address_using_prefix_t_handler |
| 932 | (vl_api_ip6_add_del_address_using_prefix_t * mp) |
| 933 | { |
| 934 | vl_api_ip6_add_del_address_using_prefix_reply_t *rmp; |
| 935 | u32 sw_if_index; |
| 936 | ip6_address_t address; |
| 937 | u8 prefix_length; |
| 938 | int rv = 0; |
| 939 | |
Juraj Sloboda | d9778c2 | 2018-06-12 10:21:05 +0200 | [diff] [blame] | 940 | VALIDATE_SW_IF_INDEX (mp); |
| 941 | |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 942 | sw_if_index = ntohl (mp->sw_if_index); |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 943 | |
| 944 | memcpy (address.as_u8, mp->address, 16); |
| 945 | prefix_length = mp->prefix_length; |
| 946 | |
| 947 | rv = |
| 948 | cp_ip6_address_add_del (sw_if_index, mp->prefix_group, address, |
| 949 | prefix_length, mp->is_add); |
| 950 | |
| 951 | BAD_SW_IF_INDEX_LABEL; |
| 952 | |
| 953 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY); |
| 954 | } |
| 955 | |
| 956 | static clib_error_t * |
| 957 | cp_ip6_address_add_del_command_function (vlib_main_t * vm, |
| 958 | unformat_input_t * input, |
| 959 | vlib_cli_command_t * cmd) |
| 960 | { |
| 961 | vnet_main_t *vnm = vnet_get_main (); |
| 962 | clib_error_t *error = 0; |
| 963 | u32 sw_if_index = ~0; |
| 964 | u8 *prefix_group = 0; |
| 965 | ip6_address_t address; |
| 966 | u32 prefix_length; |
| 967 | u8 address_set = 0; |
| 968 | u8 add = 1; |
Dave Barach | 035ea67 | 2019-03-07 16:18:55 -0500 | [diff] [blame^] | 969 | unformat_input_t _line_input, *line_input = &_line_input; |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 970 | |
Dave Barach | 035ea67 | 2019-03-07 16:18:55 -0500 | [diff] [blame^] | 971 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 972 | return 0; |
| 973 | |
| 974 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 975 | { |
| 976 | if (unformat |
Dave Barach | 035ea67 | 2019-03-07 16:18:55 -0500 | [diff] [blame^] | 977 | (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)); |
| 978 | else if (unformat (line_input, "prefix group %s", &prefix_group)); |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 979 | else |
Dave Barach | 035ea67 | 2019-03-07 16:18:55 -0500 | [diff] [blame^] | 980 | if (unformat (line_input, "%U/%d", unformat_ip6_address, |
| 981 | &address, &prefix_length)) |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 982 | address_set = 1; |
Dave Barach | 035ea67 | 2019-03-07 16:18:55 -0500 | [diff] [blame^] | 983 | else if (unformat (line_input, "del")) |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 984 | add = 0; |
| 985 | else |
| 986 | { |
| 987 | error = clib_error_return (0, "unexpected input `%U'", |
Dave Barach | 035ea67 | 2019-03-07 16:18:55 -0500 | [diff] [blame^] | 988 | format_unformat_error, line_input); |
| 989 | unformat_free (line_input); |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 990 | goto done; |
| 991 | } |
| 992 | } |
| 993 | |
Dave Barach | 035ea67 | 2019-03-07 16:18:55 -0500 | [diff] [blame^] | 994 | unformat_free (line_input); |
| 995 | |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 996 | if (sw_if_index == ~0) |
| 997 | error = clib_error_return (0, "Missing sw_if_index"); |
| 998 | else if (address_set == 0) |
| 999 | error = clib_error_return (0, "Missing address"); |
| 1000 | else |
| 1001 | { |
| 1002 | if (cp_ip6_address_add_del |
| 1003 | (sw_if_index, prefix_group, address, prefix_length, add) != 0) |
| 1004 | error = clib_error_return (0, "Error adding or removing address"); |
| 1005 | } |
| 1006 | |
| 1007 | done: |
| 1008 | return error; |
| 1009 | } |
| 1010 | |
| 1011 | /*? |
| 1012 | * This command is used to add/delete IPv6 address |
| 1013 | * potentially using available prefix from specified prefix group |
| 1014 | * |
| 1015 | * @cliexpar |
| 1016 | * @parblock |
| 1017 | * Example of how to add IPv6 address: |
| 1018 | * @cliexcmd{set ip6 address GigabitEthernet2/0/0 |
| 1019 | * prefix group my-prefix-group ::7/64} |
| 1020 | * Example of how to delete IPv6 address: |
| 1021 | * @cliexcmd{set ip6 address GigabitEthernet2/0/0 |
| 1022 | * prefix group my-prefix-group ::7/64 del} |
| 1023 | * @endparblock |
| 1024 | ?*/ |
| 1025 | /* *INDENT-OFF* */ |
| 1026 | VLIB_CLI_COMMAND (ip6_address_add_del_command, static) = { |
| 1027 | .path = "set ip6 address", |
| 1028 | .short_help = "set ip6 address <interface> [prefix group <string>] " |
| 1029 | "<address> [del]", |
| 1030 | .function = cp_ip6_address_add_del_command_function, |
| 1031 | }; |
| 1032 | /* *INDENT-ON* */ |
| 1033 | |
| 1034 | static clib_error_t * |
| 1035 | cp_ip6_addresses_show_command_function (vlib_main_t * vm, |
| 1036 | unformat_input_t * input, |
| 1037 | vlib_cli_command_t * cmd) |
| 1038 | { |
| 1039 | ip6_address_with_prefix_main_t *apm = &ip6_address_with_prefix_main; |
| 1040 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 1041 | ip6_address_info_t *address_info; |
| 1042 | const u8 *prefix_group; |
| 1043 | clib_error_t *error = 0; |
| 1044 | int i; |
| 1045 | |
| 1046 | for (i = 0; i < vec_len (apm->addresses); i++) |
| 1047 | { |
| 1048 | address_info = &apm->addresses[i]; |
| 1049 | if (address_info->prefix_group_index == ~0) |
| 1050 | prefix_group = (const u8 *) "NONE"; |
| 1051 | else |
| 1052 | prefix_group = |
| 1053 | pm->prefix_group_name_by_index[address_info->prefix_group_index]; |
| 1054 | vlib_cli_output (vm, |
| 1055 | "sw_if_index: %u, prefix_group: %s, address: %U/%d", |
| 1056 | address_info->sw_if_index, prefix_group, |
| 1057 | format_ip6_address, &address_info->address, |
| 1058 | address_info->prefix_length); |
| 1059 | } |
| 1060 | |
| 1061 | return error; |
| 1062 | } |
| 1063 | |
| 1064 | /* *INDENT-OFF* */ |
| 1065 | VLIB_CLI_COMMAND (ip6_addresses_show_command, static) = { |
| 1066 | .path = "show ip6 addresses", |
| 1067 | .short_help = "show ip6 addresses", |
| 1068 | .function = cp_ip6_addresses_show_command_function, |
| 1069 | }; |
| 1070 | /* *INDENT-ON* */ |
| 1071 | |
| 1072 | static clib_error_t * |
| 1073 | cp_ip6_prefixes_show_command_function (vlib_main_t * vm, |
| 1074 | unformat_input_t * input, |
| 1075 | vlib_cli_command_t * cmd) |
| 1076 | { |
| 1077 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 1078 | clib_error_t *error = 0; |
| 1079 | prefix_info_t *prefix_info; |
| 1080 | const u8 *prefix_group; |
| 1081 | f64 current_time = vlib_time_now (vm); |
| 1082 | |
| 1083 | /* *INDENT-OFF* */ |
| 1084 | pool_foreach (prefix_info, pm->prefix_pool, |
| 1085 | ({ |
| 1086 | prefix_group = |
| 1087 | pm->prefix_group_name_by_index[prefix_info->prefix_group_index]; |
| 1088 | vlib_cli_output (vm, "opaque_data: %lu, prefix: %U/%d, prefix group: %s, " |
| 1089 | "preferred lifetime: %u, valid lifetime: %u " |
| 1090 | "(%f remaining)", |
| 1091 | prefix_info->opaque_data, format_ip6_address, |
| 1092 | &prefix_info->prefix, prefix_info->prefix_length, |
| 1093 | prefix_group, |
| 1094 | prefix_info->preferred_lt, prefix_info->valid_lt, |
| 1095 | prefix_info->due_time - current_time); |
| 1096 | })); |
| 1097 | /* *INDENT-ON* */ |
| 1098 | |
| 1099 | return error; |
| 1100 | } |
| 1101 | |
| 1102 | /* *INDENT-OFF* */ |
| 1103 | VLIB_CLI_COMMAND (ip6_prefixes_show_command, static) = { |
| 1104 | .path = "show ip6 prefixes", |
| 1105 | .short_help = "show ip6 prefixes", |
| 1106 | .function = cp_ip6_prefixes_show_command_function, |
| 1107 | }; |
| 1108 | /* *INDENT-ON* */ |
| 1109 | |
| 1110 | static clib_error_t * |
| 1111 | ip6_pd_clients_show_command_function (vlib_main_t * vm, |
| 1112 | unformat_input_t * input, |
| 1113 | vlib_cli_command_t * cmd) |
| 1114 | { |
| 1115 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 1116 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 1117 | clib_error_t *error = 0; |
| 1118 | client_state_t *cs; |
| 1119 | f64 current_time = vlib_time_now (vm); |
| 1120 | const u8 *prefix_group; |
| 1121 | char buf1[256]; |
| 1122 | char buf2[256]; |
| 1123 | const char *rebinding; |
| 1124 | u32 i; |
| 1125 | |
| 1126 | for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++) |
| 1127 | { |
| 1128 | cs = &rm->client_state_by_sw_if_index[i]; |
| 1129 | if (cs->enabled) |
| 1130 | { |
| 1131 | if (cs->T1_due_time != DBL_MAX && cs->T1_due_time > current_time) |
| 1132 | { |
| 1133 | sprintf (buf1, "%u remaining", |
| 1134 | (u32) round (cs->T1_due_time - current_time)); |
| 1135 | } |
| 1136 | else |
| 1137 | sprintf (buf1, "timeout"); |
| 1138 | if (cs->T2_due_time != DBL_MAX && cs->T2_due_time > current_time) |
| 1139 | sprintf (buf2, "%u remaining", |
| 1140 | (u32) round (cs->T2_due_time - current_time)); |
| 1141 | else |
| 1142 | sprintf (buf2, "timeout"); |
| 1143 | if (cs->rebinding) |
| 1144 | rebinding = ", REBINDING"; |
| 1145 | else |
| 1146 | rebinding = ""; |
| 1147 | prefix_group = |
| 1148 | pm->prefix_group_name_by_index[cs->prefix_group_index]; |
| 1149 | if (cs->T1) |
| 1150 | vlib_cli_output (vm, |
| 1151 | "sw_if_index: %u, prefix group: %s, T1: %u (%s), " |
| 1152 | "T2: %u (%s), server index: %d%s", i, |
| 1153 | prefix_group, cs->T1, buf1, cs->T2, buf2, |
| 1154 | cs->server_index, rebinding); |
| 1155 | else |
| 1156 | vlib_cli_output (vm, "sw_if_index: %u, prefix group: %s%s", i, |
| 1157 | prefix_group, rebinding); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | return error; |
| 1162 | } |
| 1163 | |
| 1164 | /* *INDENT-OFF* */ |
| 1165 | VLIB_CLI_COMMAND (ip6_pd_clients_show_command, static) = { |
| 1166 | .path = "show ip6 pd clients", |
| 1167 | .short_help = "show ip6 pd clients", |
| 1168 | .function = ip6_pd_clients_show_command_function, |
| 1169 | }; |
| 1170 | /* *INDENT-ON* */ |
| 1171 | |
| 1172 | static int |
| 1173 | dhcp6_pd_client_enable_disable (u32 sw_if_index, const u8 * prefix_group, |
| 1174 | u8 enable) |
| 1175 | { |
| 1176 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 1177 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 1178 | vnet_main_t *vnm = rm->vnet_main; |
| 1179 | client_state_t *client_state; |
| 1180 | static client_state_t empty_config = { |
| 1181 | 0 |
| 1182 | }; |
| 1183 | prefix_info_t *prefix_info; |
| 1184 | prefix_info_t *prefix_list = 0; |
| 1185 | u32 prefix_group_index; |
| 1186 | |
| 1187 | if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index)) |
| 1188 | { |
| 1189 | clib_warning ("Invalid sw_if_index"); |
| 1190 | return VNET_API_ERROR_INVALID_VALUE; |
| 1191 | } |
| 1192 | |
| 1193 | vec_validate_init_empty (rm->client_state_by_sw_if_index, sw_if_index, |
| 1194 | empty_config); |
| 1195 | client_state = &rm->client_state_by_sw_if_index[sw_if_index]; |
| 1196 | |
| 1197 | u8 old_enabled = client_state->enabled; |
| 1198 | |
| 1199 | if (enable) |
| 1200 | { |
| 1201 | if (strnlen ((const char *) prefix_group, 64) == 64 |
| 1202 | || prefix_group[0] == '\0') |
| 1203 | return VNET_API_ERROR_INVALID_VALUE; |
| 1204 | prefix_group_index = |
| 1205 | prefix_group_find_or_create (prefix_group, !old_enabled); |
| 1206 | if (old_enabled |
| 1207 | && prefix_group_index != client_state->prefix_group_index) |
| 1208 | return VNET_API_ERROR_INVALID_VALUE; |
| 1209 | } |
| 1210 | |
| 1211 | if (!old_enabled && enable) |
| 1212 | { |
| 1213 | client_state->enabled = 1; |
| 1214 | client_state->prefix_group_index = prefix_group_index; |
| 1215 | ASSERT (client_state->prefix_group_index != ~0); |
| 1216 | client_state->server_index = ~0; |
| 1217 | |
| 1218 | rm->n_clients++; |
| 1219 | if (rm->n_clients == 1) |
| 1220 | { |
| 1221 | enable_process (); |
| 1222 | dhcp6_clients_enable_disable (1); |
| 1223 | } |
| 1224 | |
| 1225 | ip6_enable (sw_if_index); |
| 1226 | send_client_message_start_stop (sw_if_index, ~0, DHCPV6_MSG_SOLICIT, |
| 1227 | 0, 1); |
| 1228 | } |
| 1229 | else if (old_enabled && !enable) |
| 1230 | { |
| 1231 | send_client_message_start_stop (sw_if_index, ~0, ~0, 0, 0); |
| 1232 | |
| 1233 | rm->n_clients--; |
| 1234 | if (rm->n_clients == 0) |
| 1235 | { |
| 1236 | dhcp6_clients_enable_disable (0); |
| 1237 | disable_process (); |
| 1238 | } |
| 1239 | |
| 1240 | vec_validate (prefix_list, 0); |
| 1241 | |
| 1242 | /* *INDENT-OFF* */ |
| 1243 | pool_foreach (prefix_info, pm->prefix_pool, |
| 1244 | ({ |
| 1245 | if (is_dhcpv6_pd_prefix (prefix_info) && |
| 1246 | prefix_info->opaque_data == sw_if_index) |
| 1247 | { |
| 1248 | ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) && |
| 1249 | rm->client_state_by_sw_if_index[sw_if_index].enabled); |
| 1250 | client_state_t *client_state = |
| 1251 | &rm->client_state_by_sw_if_index[sw_if_index]; |
| 1252 | prefix_list[0] = *prefix_info; |
| 1253 | send_client_message_start_stop (sw_if_index, |
| 1254 | client_state->server_index, |
| 1255 | DHCPV6_MSG_RELEASE, prefix_list, |
| 1256 | 1); |
| 1257 | u32 prefix_index = prefix_info - pm->prefix_pool; |
| 1258 | notify_prefix_add_del (prefix_index, 0); |
| 1259 | set_is_dhcpv6_pd_prefix (prefix_info, 0); |
| 1260 | pool_put (pm->prefix_pool, prefix_info); |
| 1261 | } |
| 1262 | })); |
| 1263 | /* *INDENT-ON* */ |
| 1264 | |
| 1265 | vec_free (prefix_list); |
| 1266 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1267 | clib_memset (client_state, 0, sizeof (*client_state)); |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | static clib_error_t * |
| 1274 | dhcp6_pd_client_enable_disable_command_fn (vlib_main_t * |
| 1275 | vm, |
| 1276 | unformat_input_t |
| 1277 | * input, vlib_cli_command_t * cmd) |
| 1278 | { |
| 1279 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 1280 | vnet_main_t *vnm = rm->vnet_main; |
| 1281 | clib_error_t *error = 0; |
| 1282 | u8 *prefix_group = 0; |
| 1283 | u32 sw_if_index = ~0; |
| 1284 | u8 enable = 1; |
Dave Barach | c919982 | 2019-03-04 08:52:54 -0500 | [diff] [blame] | 1285 | unformat_input_t _line_input, *line_input = &_line_input; |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 1286 | |
Dave Barach | c919982 | 2019-03-04 08:52:54 -0500 | [diff] [blame] | 1287 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 1288 | return 0; |
| 1289 | |
| 1290 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 1291 | { |
| 1292 | if (unformat |
Dave Barach | c919982 | 2019-03-04 08:52:54 -0500 | [diff] [blame] | 1293 | (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 1294 | ; |
Dave Barach | c919982 | 2019-03-04 08:52:54 -0500 | [diff] [blame] | 1295 | else if (unformat (line_input, "prefix group %s", &prefix_group)); |
| 1296 | else if (unformat (line_input, "disable")) |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 1297 | enable = 0; |
| 1298 | else |
| 1299 | { |
| 1300 | error = clib_error_return (0, "unexpected input `%U'", |
Dave Barach | c919982 | 2019-03-04 08:52:54 -0500 | [diff] [blame] | 1301 | format_unformat_error, line_input); |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 1302 | goto done; |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | if (prefix_group == 0 && enable) |
| 1307 | error = clib_error_return (0, "Prefix group must be set when enabling"); |
| 1308 | else if (sw_if_index != ~0) |
| 1309 | { |
| 1310 | if (dhcp6_pd_client_enable_disable (sw_if_index, prefix_group, enable) |
| 1311 | != 0) |
| 1312 | error = clib_error_return (0, "Invalid sw_if_index or prefix group"); |
| 1313 | } |
| 1314 | else |
| 1315 | error = clib_error_return (0, "Missing sw_if_index"); |
| 1316 | |
| 1317 | done: |
| 1318 | vec_free (prefix_group); |
Dave Barach | c919982 | 2019-03-04 08:52:54 -0500 | [diff] [blame] | 1319 | unformat_free (line_input); |
Juraj Sloboda | 81119e8 | 2018-05-25 14:02:20 +0200 | [diff] [blame] | 1320 | |
| 1321 | return error; |
| 1322 | } |
| 1323 | |
| 1324 | /*? |
| 1325 | * This command is used to enable/disable DHCPv6 PD client |
| 1326 | * on particular interface. |
| 1327 | * |
| 1328 | * @cliexpar |
| 1329 | * @parblock |
| 1330 | * Example of how to enable DHCPv6 PD client: |
| 1331 | * @cliexcmd{dhcp6 pd client GigabitEthernet2/0/0 prefix group my-pd-group} |
| 1332 | * Example of how to disable DHCPv6 PD client: |
| 1333 | * @cliexcmd{dhcp6 pd client GigabitEthernet2/0/0 disable} |
| 1334 | * @endparblock |
| 1335 | ?*/ |
| 1336 | /* *INDENT-OFF* */ |
| 1337 | VLIB_CLI_COMMAND (dhcp6_pd_client_enable_disable_command, static) = { |
| 1338 | .path = "dhcp6 pd client", |
| 1339 | .short_help = "dhcp6 pd client <interface> (prefix group <string> | disable)", |
| 1340 | .function = dhcp6_pd_client_enable_disable_command_fn, |
| 1341 | }; |
| 1342 | /* *INDENT-ON* */ |
| 1343 | |
| 1344 | static void |
| 1345 | vl_api_dhcp6_pd_client_enable_disable_t_handler |
| 1346 | (vl_api_dhcp6_pd_client_enable_disable_t * mp) |
| 1347 | { |
| 1348 | vl_api_dhcp6_pd_client_enable_disable_reply_t *rmp; |
| 1349 | u32 sw_if_index; |
| 1350 | int rv = 0; |
| 1351 | |
| 1352 | VALIDATE_SW_IF_INDEX (mp); |
| 1353 | |
| 1354 | sw_if_index = ntohl (mp->sw_if_index); |
| 1355 | |
| 1356 | rv = |
| 1357 | dhcp6_pd_client_enable_disable (sw_if_index, mp->prefix_group, |
| 1358 | mp->enable); |
| 1359 | |
| 1360 | BAD_SW_IF_INDEX_LABEL; |
| 1361 | |
| 1362 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY); |
| 1363 | } |
| 1364 | |
| 1365 | #define vl_msg_name_crc_list |
| 1366 | #include <vnet/dhcp/dhcp6_pd_client_cp.api.h> |
| 1367 | #undef vl_msg_name_crc_list |
| 1368 | |
| 1369 | static void |
| 1370 | setup_message_id_table (api_main_t * am) |
| 1371 | { |
| 1372 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 1373 | foreach_vl_msg_name_crc_dhcp6_pd_client_cp; |
| 1374 | #undef _ |
| 1375 | } |
| 1376 | |
| 1377 | static clib_error_t * |
| 1378 | dhcp_pd_client_cp_init (vlib_main_t * vm) |
| 1379 | { |
| 1380 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 1381 | api_main_t *am = &api_main; |
| 1382 | |
| 1383 | rm->vlib_main = vm; |
| 1384 | rm->vnet_main = vnet_get_main (); |
| 1385 | rm->api_main = am; |
| 1386 | rm->node_index = dhcp6_pd_client_cp_process_node.index; |
| 1387 | |
| 1388 | #define _(N,n) \ |
| 1389 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1390 | vl_api_##n##_t_handler, \ |
| 1391 | vl_noop_handler, \ |
| 1392 | vl_api_##n##_t_endian, \ |
| 1393 | vl_api_##n##_t_print, \ |
| 1394 | sizeof(vl_api_##n##_t), 0/* do NOT trace! */); |
| 1395 | foreach_dhcp6_pd_client_cp_msg; |
| 1396 | #undef _ |
| 1397 | |
| 1398 | /* |
| 1399 | * Set up the (msg_name, crc, message-id) table |
| 1400 | */ |
| 1401 | setup_message_id_table (am); |
| 1402 | |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | VLIB_INIT_FUNCTION (dhcp_pd_client_cp_init); |
| 1407 | |
| 1408 | /* |
| 1409 | * fd.io coding-style-patch-verification: ON |
| 1410 | * |
| 1411 | * Local Variables: |
| 1412 | * eval: (c-set-style "gnu") |
| 1413 | * End: |
| 1414 | */ |