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; |
| 969 | |
| 970 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 971 | { |
| 972 | if (unformat |
| 973 | (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)); |
| 974 | else if (unformat (input, "prefix group %s", &prefix_group)); |
| 975 | else |
| 976 | if (unformat |
| 977 | (input, "%U/%d", unformat_ip6_address, &address, &prefix_length)) |
| 978 | address_set = 1; |
| 979 | else if (unformat (input, "del")) |
| 980 | add = 0; |
| 981 | else |
| 982 | { |
| 983 | error = clib_error_return (0, "unexpected input `%U'", |
| 984 | format_unformat_error, input); |
| 985 | goto done; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | if (sw_if_index == ~0) |
| 990 | error = clib_error_return (0, "Missing sw_if_index"); |
| 991 | else if (address_set == 0) |
| 992 | error = clib_error_return (0, "Missing address"); |
| 993 | else |
| 994 | { |
| 995 | if (cp_ip6_address_add_del |
| 996 | (sw_if_index, prefix_group, address, prefix_length, add) != 0) |
| 997 | error = clib_error_return (0, "Error adding or removing address"); |
| 998 | } |
| 999 | |
| 1000 | done: |
| 1001 | return error; |
| 1002 | } |
| 1003 | |
| 1004 | /*? |
| 1005 | * This command is used to add/delete IPv6 address |
| 1006 | * potentially using available prefix from specified prefix group |
| 1007 | * |
| 1008 | * @cliexpar |
| 1009 | * @parblock |
| 1010 | * Example of how to add IPv6 address: |
| 1011 | * @cliexcmd{set ip6 address GigabitEthernet2/0/0 |
| 1012 | * prefix group my-prefix-group ::7/64} |
| 1013 | * Example of how to delete IPv6 address: |
| 1014 | * @cliexcmd{set ip6 address GigabitEthernet2/0/0 |
| 1015 | * prefix group my-prefix-group ::7/64 del} |
| 1016 | * @endparblock |
| 1017 | ?*/ |
| 1018 | /* *INDENT-OFF* */ |
| 1019 | VLIB_CLI_COMMAND (ip6_address_add_del_command, static) = { |
| 1020 | .path = "set ip6 address", |
| 1021 | .short_help = "set ip6 address <interface> [prefix group <string>] " |
| 1022 | "<address> [del]", |
| 1023 | .function = cp_ip6_address_add_del_command_function, |
| 1024 | }; |
| 1025 | /* *INDENT-ON* */ |
| 1026 | |
| 1027 | static clib_error_t * |
| 1028 | cp_ip6_addresses_show_command_function (vlib_main_t * vm, |
| 1029 | unformat_input_t * input, |
| 1030 | vlib_cli_command_t * cmd) |
| 1031 | { |
| 1032 | ip6_address_with_prefix_main_t *apm = &ip6_address_with_prefix_main; |
| 1033 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 1034 | ip6_address_info_t *address_info; |
| 1035 | const u8 *prefix_group; |
| 1036 | clib_error_t *error = 0; |
| 1037 | int i; |
| 1038 | |
| 1039 | for (i = 0; i < vec_len (apm->addresses); i++) |
| 1040 | { |
| 1041 | address_info = &apm->addresses[i]; |
| 1042 | if (address_info->prefix_group_index == ~0) |
| 1043 | prefix_group = (const u8 *) "NONE"; |
| 1044 | else |
| 1045 | prefix_group = |
| 1046 | pm->prefix_group_name_by_index[address_info->prefix_group_index]; |
| 1047 | vlib_cli_output (vm, |
| 1048 | "sw_if_index: %u, prefix_group: %s, address: %U/%d", |
| 1049 | address_info->sw_if_index, prefix_group, |
| 1050 | format_ip6_address, &address_info->address, |
| 1051 | address_info->prefix_length); |
| 1052 | } |
| 1053 | |
| 1054 | return error; |
| 1055 | } |
| 1056 | |
| 1057 | /* *INDENT-OFF* */ |
| 1058 | VLIB_CLI_COMMAND (ip6_addresses_show_command, static) = { |
| 1059 | .path = "show ip6 addresses", |
| 1060 | .short_help = "show ip6 addresses", |
| 1061 | .function = cp_ip6_addresses_show_command_function, |
| 1062 | }; |
| 1063 | /* *INDENT-ON* */ |
| 1064 | |
| 1065 | static clib_error_t * |
| 1066 | cp_ip6_prefixes_show_command_function (vlib_main_t * vm, |
| 1067 | unformat_input_t * input, |
| 1068 | vlib_cli_command_t * cmd) |
| 1069 | { |
| 1070 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 1071 | clib_error_t *error = 0; |
| 1072 | prefix_info_t *prefix_info; |
| 1073 | const u8 *prefix_group; |
| 1074 | f64 current_time = vlib_time_now (vm); |
| 1075 | |
| 1076 | /* *INDENT-OFF* */ |
| 1077 | pool_foreach (prefix_info, pm->prefix_pool, |
| 1078 | ({ |
| 1079 | prefix_group = |
| 1080 | pm->prefix_group_name_by_index[prefix_info->prefix_group_index]; |
| 1081 | vlib_cli_output (vm, "opaque_data: %lu, prefix: %U/%d, prefix group: %s, " |
| 1082 | "preferred lifetime: %u, valid lifetime: %u " |
| 1083 | "(%f remaining)", |
| 1084 | prefix_info->opaque_data, format_ip6_address, |
| 1085 | &prefix_info->prefix, prefix_info->prefix_length, |
| 1086 | prefix_group, |
| 1087 | prefix_info->preferred_lt, prefix_info->valid_lt, |
| 1088 | prefix_info->due_time - current_time); |
| 1089 | })); |
| 1090 | /* *INDENT-ON* */ |
| 1091 | |
| 1092 | return error; |
| 1093 | } |
| 1094 | |
| 1095 | /* *INDENT-OFF* */ |
| 1096 | VLIB_CLI_COMMAND (ip6_prefixes_show_command, static) = { |
| 1097 | .path = "show ip6 prefixes", |
| 1098 | .short_help = "show ip6 prefixes", |
| 1099 | .function = cp_ip6_prefixes_show_command_function, |
| 1100 | }; |
| 1101 | /* *INDENT-ON* */ |
| 1102 | |
| 1103 | static clib_error_t * |
| 1104 | ip6_pd_clients_show_command_function (vlib_main_t * vm, |
| 1105 | unformat_input_t * input, |
| 1106 | vlib_cli_command_t * cmd) |
| 1107 | { |
| 1108 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 1109 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 1110 | clib_error_t *error = 0; |
| 1111 | client_state_t *cs; |
| 1112 | f64 current_time = vlib_time_now (vm); |
| 1113 | const u8 *prefix_group; |
| 1114 | char buf1[256]; |
| 1115 | char buf2[256]; |
| 1116 | const char *rebinding; |
| 1117 | u32 i; |
| 1118 | |
| 1119 | for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++) |
| 1120 | { |
| 1121 | cs = &rm->client_state_by_sw_if_index[i]; |
| 1122 | if (cs->enabled) |
| 1123 | { |
| 1124 | if (cs->T1_due_time != DBL_MAX && cs->T1_due_time > current_time) |
| 1125 | { |
| 1126 | sprintf (buf1, "%u remaining", |
| 1127 | (u32) round (cs->T1_due_time - current_time)); |
| 1128 | } |
| 1129 | else |
| 1130 | sprintf (buf1, "timeout"); |
| 1131 | if (cs->T2_due_time != DBL_MAX && cs->T2_due_time > current_time) |
| 1132 | sprintf (buf2, "%u remaining", |
| 1133 | (u32) round (cs->T2_due_time - current_time)); |
| 1134 | else |
| 1135 | sprintf (buf2, "timeout"); |
| 1136 | if (cs->rebinding) |
| 1137 | rebinding = ", REBINDING"; |
| 1138 | else |
| 1139 | rebinding = ""; |
| 1140 | prefix_group = |
| 1141 | pm->prefix_group_name_by_index[cs->prefix_group_index]; |
| 1142 | if (cs->T1) |
| 1143 | vlib_cli_output (vm, |
| 1144 | "sw_if_index: %u, prefix group: %s, T1: %u (%s), " |
| 1145 | "T2: %u (%s), server index: %d%s", i, |
| 1146 | prefix_group, cs->T1, buf1, cs->T2, buf2, |
| 1147 | cs->server_index, rebinding); |
| 1148 | else |
| 1149 | vlib_cli_output (vm, "sw_if_index: %u, prefix group: %s%s", i, |
| 1150 | prefix_group, rebinding); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | return error; |
| 1155 | } |
| 1156 | |
| 1157 | /* *INDENT-OFF* */ |
| 1158 | VLIB_CLI_COMMAND (ip6_pd_clients_show_command, static) = { |
| 1159 | .path = "show ip6 pd clients", |
| 1160 | .short_help = "show ip6 pd clients", |
| 1161 | .function = ip6_pd_clients_show_command_function, |
| 1162 | }; |
| 1163 | /* *INDENT-ON* */ |
| 1164 | |
| 1165 | static int |
| 1166 | dhcp6_pd_client_enable_disable (u32 sw_if_index, const u8 * prefix_group, |
| 1167 | u8 enable) |
| 1168 | { |
| 1169 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 1170 | ip6_prefix_main_t *pm = &ip6_prefix_main; |
| 1171 | vnet_main_t *vnm = rm->vnet_main; |
| 1172 | client_state_t *client_state; |
| 1173 | static client_state_t empty_config = { |
| 1174 | 0 |
| 1175 | }; |
| 1176 | prefix_info_t *prefix_info; |
| 1177 | prefix_info_t *prefix_list = 0; |
| 1178 | u32 prefix_group_index; |
| 1179 | |
| 1180 | if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index)) |
| 1181 | { |
| 1182 | clib_warning ("Invalid sw_if_index"); |
| 1183 | return VNET_API_ERROR_INVALID_VALUE; |
| 1184 | } |
| 1185 | |
| 1186 | vec_validate_init_empty (rm->client_state_by_sw_if_index, sw_if_index, |
| 1187 | empty_config); |
| 1188 | client_state = &rm->client_state_by_sw_if_index[sw_if_index]; |
| 1189 | |
| 1190 | u8 old_enabled = client_state->enabled; |
| 1191 | |
| 1192 | if (enable) |
| 1193 | { |
| 1194 | if (strnlen ((const char *) prefix_group, 64) == 64 |
| 1195 | || prefix_group[0] == '\0') |
| 1196 | return VNET_API_ERROR_INVALID_VALUE; |
| 1197 | prefix_group_index = |
| 1198 | prefix_group_find_or_create (prefix_group, !old_enabled); |
| 1199 | if (old_enabled |
| 1200 | && prefix_group_index != client_state->prefix_group_index) |
| 1201 | return VNET_API_ERROR_INVALID_VALUE; |
| 1202 | } |
| 1203 | |
| 1204 | if (!old_enabled && enable) |
| 1205 | { |
| 1206 | client_state->enabled = 1; |
| 1207 | client_state->prefix_group_index = prefix_group_index; |
| 1208 | ASSERT (client_state->prefix_group_index != ~0); |
| 1209 | client_state->server_index = ~0; |
| 1210 | |
| 1211 | rm->n_clients++; |
| 1212 | if (rm->n_clients == 1) |
| 1213 | { |
| 1214 | enable_process (); |
| 1215 | dhcp6_clients_enable_disable (1); |
| 1216 | } |
| 1217 | |
| 1218 | ip6_enable (sw_if_index); |
| 1219 | send_client_message_start_stop (sw_if_index, ~0, DHCPV6_MSG_SOLICIT, |
| 1220 | 0, 1); |
| 1221 | } |
| 1222 | else if (old_enabled && !enable) |
| 1223 | { |
| 1224 | send_client_message_start_stop (sw_if_index, ~0, ~0, 0, 0); |
| 1225 | |
| 1226 | rm->n_clients--; |
| 1227 | if (rm->n_clients == 0) |
| 1228 | { |
| 1229 | dhcp6_clients_enable_disable (0); |
| 1230 | disable_process (); |
| 1231 | } |
| 1232 | |
| 1233 | vec_validate (prefix_list, 0); |
| 1234 | |
| 1235 | /* *INDENT-OFF* */ |
| 1236 | pool_foreach (prefix_info, pm->prefix_pool, |
| 1237 | ({ |
| 1238 | if (is_dhcpv6_pd_prefix (prefix_info) && |
| 1239 | prefix_info->opaque_data == sw_if_index) |
| 1240 | { |
| 1241 | ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) && |
| 1242 | rm->client_state_by_sw_if_index[sw_if_index].enabled); |
| 1243 | client_state_t *client_state = |
| 1244 | &rm->client_state_by_sw_if_index[sw_if_index]; |
| 1245 | prefix_list[0] = *prefix_info; |
| 1246 | send_client_message_start_stop (sw_if_index, |
| 1247 | client_state->server_index, |
| 1248 | DHCPV6_MSG_RELEASE, prefix_list, |
| 1249 | 1); |
| 1250 | u32 prefix_index = prefix_info - pm->prefix_pool; |
| 1251 | notify_prefix_add_del (prefix_index, 0); |
| 1252 | set_is_dhcpv6_pd_prefix (prefix_info, 0); |
| 1253 | pool_put (pm->prefix_pool, prefix_info); |
| 1254 | } |
| 1255 | })); |
| 1256 | /* *INDENT-ON* */ |
| 1257 | |
| 1258 | vec_free (prefix_list); |
| 1259 | |
| 1260 | memset (client_state, 0, sizeof (*client_state)); |
| 1261 | } |
| 1262 | |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
| 1266 | static clib_error_t * |
| 1267 | dhcp6_pd_client_enable_disable_command_fn (vlib_main_t * |
| 1268 | vm, |
| 1269 | unformat_input_t |
| 1270 | * input, vlib_cli_command_t * cmd) |
| 1271 | { |
| 1272 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 1273 | vnet_main_t *vnm = rm->vnet_main; |
| 1274 | clib_error_t *error = 0; |
| 1275 | u8 *prefix_group = 0; |
| 1276 | u32 sw_if_index = ~0; |
| 1277 | u8 enable = 1; |
| 1278 | |
| 1279 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1280 | { |
| 1281 | if (unformat |
| 1282 | (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 1283 | ; |
| 1284 | else if (unformat (input, "prefix group %s", &prefix_group)); |
| 1285 | else if (unformat (input, "disable")) |
| 1286 | enable = 0; |
| 1287 | else |
| 1288 | { |
| 1289 | error = clib_error_return (0, "unexpected input `%U'", |
| 1290 | format_unformat_error, input); |
| 1291 | goto done; |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | if (prefix_group == 0 && enable) |
| 1296 | error = clib_error_return (0, "Prefix group must be set when enabling"); |
| 1297 | else if (sw_if_index != ~0) |
| 1298 | { |
| 1299 | if (dhcp6_pd_client_enable_disable (sw_if_index, prefix_group, enable) |
| 1300 | != 0) |
| 1301 | error = clib_error_return (0, "Invalid sw_if_index or prefix group"); |
| 1302 | } |
| 1303 | else |
| 1304 | error = clib_error_return (0, "Missing sw_if_index"); |
| 1305 | |
| 1306 | done: |
| 1307 | vec_free (prefix_group); |
| 1308 | |
| 1309 | return error; |
| 1310 | } |
| 1311 | |
| 1312 | /*? |
| 1313 | * This command is used to enable/disable DHCPv6 PD client |
| 1314 | * on particular interface. |
| 1315 | * |
| 1316 | * @cliexpar |
| 1317 | * @parblock |
| 1318 | * Example of how to enable DHCPv6 PD client: |
| 1319 | * @cliexcmd{dhcp6 pd client GigabitEthernet2/0/0 prefix group my-pd-group} |
| 1320 | * Example of how to disable DHCPv6 PD client: |
| 1321 | * @cliexcmd{dhcp6 pd client GigabitEthernet2/0/0 disable} |
| 1322 | * @endparblock |
| 1323 | ?*/ |
| 1324 | /* *INDENT-OFF* */ |
| 1325 | VLIB_CLI_COMMAND (dhcp6_pd_client_enable_disable_command, static) = { |
| 1326 | .path = "dhcp6 pd client", |
| 1327 | .short_help = "dhcp6 pd client <interface> (prefix group <string> | disable)", |
| 1328 | .function = dhcp6_pd_client_enable_disable_command_fn, |
| 1329 | }; |
| 1330 | /* *INDENT-ON* */ |
| 1331 | |
| 1332 | static void |
| 1333 | vl_api_dhcp6_pd_client_enable_disable_t_handler |
| 1334 | (vl_api_dhcp6_pd_client_enable_disable_t * mp) |
| 1335 | { |
| 1336 | vl_api_dhcp6_pd_client_enable_disable_reply_t *rmp; |
| 1337 | u32 sw_if_index; |
| 1338 | int rv = 0; |
| 1339 | |
| 1340 | VALIDATE_SW_IF_INDEX (mp); |
| 1341 | |
| 1342 | sw_if_index = ntohl (mp->sw_if_index); |
| 1343 | |
| 1344 | rv = |
| 1345 | dhcp6_pd_client_enable_disable (sw_if_index, mp->prefix_group, |
| 1346 | mp->enable); |
| 1347 | |
| 1348 | BAD_SW_IF_INDEX_LABEL; |
| 1349 | |
| 1350 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY); |
| 1351 | } |
| 1352 | |
| 1353 | #define vl_msg_name_crc_list |
| 1354 | #include <vnet/dhcp/dhcp6_pd_client_cp.api.h> |
| 1355 | #undef vl_msg_name_crc_list |
| 1356 | |
| 1357 | static void |
| 1358 | setup_message_id_table (api_main_t * am) |
| 1359 | { |
| 1360 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 1361 | foreach_vl_msg_name_crc_dhcp6_pd_client_cp; |
| 1362 | #undef _ |
| 1363 | } |
| 1364 | |
| 1365 | static clib_error_t * |
| 1366 | dhcp_pd_client_cp_init (vlib_main_t * vm) |
| 1367 | { |
| 1368 | dhcp6_pd_client_cp_main_t *rm = &dhcp6_pd_client_cp_main; |
| 1369 | api_main_t *am = &api_main; |
| 1370 | |
| 1371 | rm->vlib_main = vm; |
| 1372 | rm->vnet_main = vnet_get_main (); |
| 1373 | rm->api_main = am; |
| 1374 | rm->node_index = dhcp6_pd_client_cp_process_node.index; |
| 1375 | |
| 1376 | #define _(N,n) \ |
| 1377 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1378 | vl_api_##n##_t_handler, \ |
| 1379 | vl_noop_handler, \ |
| 1380 | vl_api_##n##_t_endian, \ |
| 1381 | vl_api_##n##_t_print, \ |
| 1382 | sizeof(vl_api_##n##_t), 0/* do NOT trace! */); |
| 1383 | foreach_dhcp6_pd_client_cp_msg; |
| 1384 | #undef _ |
| 1385 | |
| 1386 | /* |
| 1387 | * Set up the (msg_name, crc, message-id) table |
| 1388 | */ |
| 1389 | setup_message_id_table (am); |
| 1390 | |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
| 1394 | VLIB_INIT_FUNCTION (dhcp_pd_client_cp_init); |
| 1395 | |
| 1396 | /* |
| 1397 | * fd.io coding-style-patch-verification: ON |
| 1398 | * |
| 1399 | * Local Variables: |
| 1400 | * eval: (c-set-style "gnu") |
| 1401 | * End: |
| 1402 | */ |