LCOV - code coverage report
Current view: top level - plugins/dhcp - dhcp6_ia_na_client_cp.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 240 340 70.6 %
Date: 2023-07-05 22:20:52 Functions: 21 25 84.0 %

          Line data    Source code
       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 <dhcp/dhcp6_packet.h>
      19             : #include <dhcp/dhcp6_ia_na_client_dp.h>
      20             : #include <vnet/ip/ip.h>
      21             : #include <vnet/ip/ip6.h>
      22             : #include <vnet/ip/ip6_link.h>
      23             : #include <float.h>
      24             : #include <math.h>
      25             : 
      26             : typedef struct
      27             : {
      28             :   u32 sw_if_index;
      29             :   ip6_address_t address;
      30             :   u32 preferred_lt;
      31             :   u32 valid_lt;
      32             :   f64 due_time;
      33             : } address_info_t;
      34             : 
      35             : typedef struct
      36             : {
      37             :   u8 enabled;
      38             :   u32 server_index;
      39             :   u32 T1;
      40             :   u32 T2;
      41             :   f64 T1_due_time;
      42             :   f64 T2_due_time;
      43             :   u32 address_count;
      44             :   u8 rebinding;
      45             : } client_state_t;
      46             : 
      47             : typedef struct
      48             : {
      49             :   address_info_t *address_pool;
      50             :   client_state_t *client_state_by_sw_if_index;
      51             :   u32 n_clients;
      52             :   f64 max_valid_due_time;
      53             : 
      54             :   /* convenience */
      55             :   vlib_main_t *vlib_main;
      56             :   vnet_main_t *vnet_main;
      57             :   api_main_t *api_main;
      58             :   u32 node_index;
      59             : } dhcp6_client_cp_main_t;
      60             : 
      61             : static dhcp6_client_cp_main_t dhcp6_client_cp_main;
      62             : 
      63             : enum
      64             : {
      65             :   RD_CP_EVENT_INTERRUPT,
      66             :   RD_CP_EVENT_DISABLE,
      67             : };
      68             : 
      69             : static void
      70          28 : send_client_message_start_stop (u32 sw_if_index, u32 server_index,
      71             :                                 u8 msg_type, address_info_t * address_list,
      72             :                                 u8 start)
      73             : {
      74          28 :   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
      75          28 :   dhcp6_send_client_message_params_t params = { 0, };
      76          28 :   dhcp6_send_client_message_params_address_t *addresses = 0, *addr;
      77             :   u32 i;
      78             : 
      79          28 :   ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) &&
      80             :           rm->client_state_by_sw_if_index[sw_if_index].enabled);
      81          28 :   client_state_t *client_state =
      82          28 :     &rm->client_state_by_sw_if_index[sw_if_index];
      83             : 
      84          28 :   params.sw_if_index = sw_if_index;
      85          28 :   params.server_index = server_index;
      86          28 :   params.msg_type = msg_type;
      87          28 :   if (start)
      88             :     {
      89          16 :       if (msg_type == DHCPV6_MSG_SOLICIT)
      90             :         {
      91           8 :           params.irt = 1;
      92           8 :           params.mrt = 120;
      93             :         }
      94           8 :       else if (msg_type == DHCPV6_MSG_REQUEST)
      95             :         {
      96           5 :           params.irt = 1;
      97           5 :           params.mrt = 30;
      98           5 :           params.mrc = 10;
      99             :         }
     100           3 :       else if (msg_type == DHCPV6_MSG_RENEW)
     101             :         {
     102           2 :           params.irt = 10;
     103           2 :           params.mrt = 600;
     104           2 :           f64 current_time = vlib_time_now (rm->vlib_main);
     105           2 :           i32 diff_time = client_state->T2 - current_time;
     106           2 :           if (diff_time < 0)
     107           1 :             diff_time = 0;
     108           2 :           params.mrd = diff_time;
     109             :         }
     110           1 :       else if (msg_type == DHCPV6_MSG_REBIND)
     111             :         {
     112           1 :           params.irt = 10;
     113           1 :           params.mrt = 600;
     114           1 :           f64 current_time = vlib_time_now (rm->vlib_main);
     115           1 :           i32 diff_time = rm->max_valid_due_time - current_time;
     116           1 :           if (diff_time < 0)
     117           1 :             diff_time = 0;
     118           1 :           params.mrd = diff_time;
     119             :         }
     120           0 :       else if (msg_type == DHCPV6_MSG_RELEASE)
     121             :         {
     122           0 :           params.mrc = 1;
     123             :         }
     124             :     }
     125             : 
     126          28 :   params.T1 = 0;
     127          28 :   params.T2 = 0;
     128          28 :   if (vec_len (address_list) != 0)
     129           1 :     vec_validate (addresses, vec_len (address_list) - 1);
     130          29 :   for (i = 0; i < vec_len (address_list); i++)
     131             :     {
     132           1 :       address_info_t *address = &address_list[i];
     133           1 :       addr = &addresses[i];
     134           1 :       addr->valid_lt = address->valid_lt;
     135           1 :       addr->preferred_lt = address->preferred_lt;
     136           1 :       addr->address = address->address;
     137             :     }
     138          28 :   params.addresses = addresses;
     139             : 
     140          28 :   dhcp6_send_client_message (rm->vlib_main, sw_if_index, !start, &params);
     141             : 
     142          28 :   vec_free (params.addresses);
     143          28 : }
     144             : 
     145             : static void interrupt_process (void);
     146             : 
     147             : static u8
     148           0 : ip6_addresses_equal (ip6_address_t * address1, ip6_address_t * address2)
     149             : {
     150           0 :   if (address1->as_u64[0] != address2->as_u64[0])
     151           0 :     return 0;
     152           0 :   return address1->as_u64[1] == address2->as_u64[1];
     153             : }
     154             : 
     155             : static clib_error_t *
     156          14 : dhcp6_reply_event_handler (vl_api_dhcp6_reply_event_t * mp)
     157             : {
     158          14 :   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
     159          14 :   vlib_main_t *vm = rm->vlib_main;
     160             :   client_state_t *client_state;
     161             :   ip6_address_t *address;
     162             :   u32 sw_if_index;
     163             :   u32 n_addresses;
     164             :   vl_api_dhcp6_address_info_t *api_address;
     165             :   u32 inner_status_code;
     166             :   u32 status_code;
     167             :   u32 server_index;
     168             :   f64 current_time;
     169          14 :   clib_error_t *error = 0;
     170             :   u32 i;
     171             : 
     172          14 :   current_time = vlib_time_now (vm);
     173             : 
     174          14 :   sw_if_index = ntohl (mp->sw_if_index);
     175             : 
     176          14 :   if (sw_if_index >= vec_len (rm->client_state_by_sw_if_index))
     177           1 :     return 0;
     178             : 
     179          13 :   client_state = &rm->client_state_by_sw_if_index[sw_if_index];
     180             : 
     181          13 :   if (!client_state->enabled)
     182           0 :     return 0;
     183             : 
     184          13 :   server_index = ntohl (mp->server_index);
     185             : 
     186          13 :   n_addresses = ntohl (mp->n_addresses);
     187             : 
     188          13 :   inner_status_code = ntohs (mp->inner_status_code);
     189          13 :   status_code = ntohs (mp->status_code);
     190             : 
     191          13 :   if (mp->msg_type == DHCPV6_MSG_API_ADVERTISE
     192           7 :       && client_state->server_index == ~0)
     193             :     {
     194           6 :       address_info_t *address_list = 0, *address_info;
     195             : 
     196           6 :       if (inner_status_code == DHCPV6_STATUS_NOADDRS_AVAIL)
     197             :         {
     198           1 :           clib_warning
     199             :             ("Advertise message arrived with NoAddrsAvail status code");
     200           1 :           return 0;
     201             :         }
     202             : 
     203           5 :       if (n_addresses > 0)
     204           1 :         vec_validate (address_list, n_addresses - 1);
     205           6 :       for (i = 0; i < n_addresses; i++)
     206             :         {
     207           1 :           api_address = &mp->addresses[i];
     208           1 :           address = (ip6_address_t *) api_address->address;
     209             : 
     210           1 :           address_info = &address_list[i];
     211           1 :           address_info->address = *address;
     212           1 :           address_info->preferred_lt = 0;
     213           1 :           address_info->valid_lt = 0;
     214             :         }
     215             : 
     216           5 :       client_state->server_index = server_index;
     217             : 
     218           5 :       send_client_message_start_stop (sw_if_index, server_index,
     219             :                                       DHCPV6_MSG_REQUEST, address_list, 1);
     220           5 :       vec_free (address_list);
     221             :     }
     222             : 
     223          12 :   if (mp->msg_type != DHCPV6_MSG_API_REPLY)
     224           6 :     return 0;
     225             : 
     226           6 :   if (!client_state->rebinding && client_state->server_index != server_index)
     227             :     {
     228           1 :       clib_warning ("Reply message arrived with Server ID different "
     229             :                     "from that in Request or Renew message");
     230           1 :       return 0;
     231             :     }
     232             : 
     233           5 :   if (inner_status_code == DHCPV6_STATUS_NOADDRS_AVAIL)
     234             :     {
     235           0 :       clib_warning ("Reply message arrived with NoAddrsAvail status code");
     236           0 :       if (n_addresses > 0)
     237             :         {
     238           0 :           clib_warning
     239             :             ("Invalid Reply message arrived: It contains NoAddrsAvail "
     240             :              "status code but also contains addresses");
     241           0 :           return 0;
     242             :         }
     243             :     }
     244             : 
     245           5 :   if (status_code == DHCPV6_STATUS_UNSPEC_FAIL)
     246             :     {
     247           0 :       clib_warning ("Reply message arrived with UnspecFail status code");
     248           0 :       return 0;
     249             :     }
     250             : 
     251           5 :   send_client_message_start_stop (sw_if_index, server_index,
     252           5 :                                   mp->msg_type, 0, 0);
     253             : 
     254           7 :   for (i = 0; i < n_addresses; i++)
     255             :     {
     256           2 :       address_info_t *address_info = 0;
     257             :       u32 valid_time;
     258             :       u32 preferred_time;
     259             : 
     260           2 :       api_address = &mp->addresses[i];
     261             : 
     262           2 :       address = (ip6_address_t *) api_address->address;
     263             : 
     264           2 :       if (ip6_address_is_link_local_unicast (address))
     265           1 :         continue;
     266             : 
     267           2 :       valid_time = ntohl (api_address->valid_time);
     268           2 :       preferred_time = ntohl (api_address->preferred_time);
     269             : 
     270           2 :       if (preferred_time > valid_time)
     271           1 :         continue;
     272             : 
     273           1 :       u8 address_already_present = 0;
     274             :       /* *INDENT-OFF* */
     275           1 :       pool_foreach (address_info, rm->address_pool)
     276             :        {
     277           0 :         if (address_info->sw_if_index != sw_if_index)
     278             :           ;
     279           0 :         else if (!ip6_addresses_equal (&address_info->address, address))
     280             :           ;
     281             :         else
     282             :           {
     283           0 :             address_already_present = 1;
     284           0 :             goto address_pool_foreach_out;
     285             :           }
     286             :       }
     287             :       /* *INDENT-ON* */
     288           1 :     address_pool_foreach_out:
     289             : 
     290           1 :       if (address_already_present)
     291             :         {
     292           0 :           address_info->preferred_lt = preferred_time;
     293           0 :           address_info->valid_lt = valid_time;
     294           0 :           address_info->due_time = current_time;
     295             :           /* Renew the lease at the preferred time, if non-zero */
     296           0 :           address_info->due_time += (preferred_time > 0) ?
     297             :             preferred_time : valid_time;
     298             : 
     299           0 :           if (address_info->due_time > rm->max_valid_due_time)
     300           0 :             rm->max_valid_due_time = address_info->due_time;
     301           0 :           continue;
     302             :         }
     303             : 
     304           1 :       if (valid_time == 0)
     305           0 :         continue;
     306             : 
     307           1 :       pool_get (rm->address_pool, address_info);
     308           1 :       address_info->sw_if_index = sw_if_index;
     309           1 :       address_info->address = *address;
     310           1 :       address_info->preferred_lt = preferred_time;
     311           1 :       address_info->valid_lt = valid_time;
     312           1 :       address_info->due_time = current_time;
     313             :       /* Renew the lease at the preferred time, if non-zero */
     314           1 :       address_info->due_time += (preferred_time > 0) ?
     315             :         preferred_time : valid_time;
     316             : 
     317           1 :       if (address_info->due_time > rm->max_valid_due_time)
     318           1 :         rm->max_valid_due_time = address_info->due_time;
     319           1 :       rm->client_state_by_sw_if_index[sw_if_index].address_count++;
     320             : 
     321           1 :       error = ip6_add_del_interface_address (vm, sw_if_index,
     322           1 :                                              &address_info->address, 64, 0);
     323           1 :       if (error)
     324           0 :         clib_warning ("Failed to add interface address");
     325             :     }
     326             : 
     327           5 :   client_state->server_index = server_index;
     328           5 :   client_state->T1 = ntohl (mp->T1);
     329           5 :   client_state->T2 = ntohl (mp->T2);
     330           5 :   if (client_state->T1 != 0)
     331           4 :     client_state->T1_due_time = current_time + client_state->T1;
     332           5 :   if (client_state->T2 != 0)
     333           4 :     client_state->T2_due_time = current_time + client_state->T2;
     334           5 :   client_state->rebinding = 0;
     335             : 
     336           5 :   interrupt_process ();
     337             : 
     338           5 :   return error;
     339             : }
     340             : 
     341             : static address_info_t *
     342           3 : create_address_list (u32 sw_if_index)
     343             : {
     344           3 :   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
     345           3 :   address_info_t *address_info, *address_list = 0;;
     346             : 
     347             :   /* *INDENT-OFF* */
     348           3 :   pool_foreach (address_info, rm->address_pool)
     349             :    {
     350           0 :     if (address_info->sw_if_index == sw_if_index)
     351             :       {
     352           0 :         u32 pos = vec_len (address_list);
     353           0 :         vec_validate (address_list, pos);
     354           0 :         clib_memcpy (&address_list[pos], address_info, sizeof (*address_info));
     355             :       }
     356             :   }
     357             :   /* *INDENT-ON* */
     358             : 
     359           3 :   return address_list;
     360             : }
     361             : 
     362        1119 : VNET_DHCP6_REPLY_EVENT_FUNCTION (dhcp6_reply_event_handler);
     363             : 
     364             : static uword
     365         566 : dhcp6_client_cp_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
     366             :                          vlib_frame_t * f)
     367             : {
     368         566 :   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
     369             :   address_info_t *address_info;
     370             :   client_state_t *client_state;
     371         566 :   f64 sleep_time = 1e9;
     372             :   clib_error_t *error;
     373             :   f64 current_time;
     374             :   f64 due_time;
     375             :   uword event_type;
     376         566 :   uword *event_data = 0;
     377             :   int i;
     378             : 
     379             :   while (1)
     380             :     {
     381         606 :       vlib_process_wait_for_event_or_clock (vm, sleep_time);
     382          40 :       event_type = vlib_process_get_events (vm, &event_data);
     383          40 :       vec_reset_length (event_data);
     384             : 
     385          40 :       if (event_type == RD_CP_EVENT_DISABLE)
     386             :         {
     387           7 :           vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_DISABLED);
     388           7 :           sleep_time = 1e9;
     389           7 :           continue;
     390             :         }
     391             : 
     392          33 :       current_time = vlib_time_now (vm);
     393             :       do
     394             :         {
     395          35 :           due_time = current_time + 1e9;
     396             :           /* *INDENT-OFF* */
     397          44 :           pool_foreach (address_info, rm->address_pool)
     398             :            {
     399           9 :             if (address_info->due_time > current_time)
     400             :               {
     401           8 :                 if (address_info->due_time < due_time)
     402           8 :                   due_time = address_info->due_time;
     403             :               }
     404             :             else
     405             :               {
     406           1 :                 u32 sw_if_index = address_info->sw_if_index;
     407           1 :                 error = ip6_add_del_interface_address (vm, sw_if_index,
     408             :                                                        &address_info->address,
     409             :                                                        64, 1);
     410           1 :                 if (error)
     411           0 :                     clib_warning ("Failed to delete interface address");
     412           1 :                 pool_put (rm->address_pool, address_info);
     413             :                 /* make sure ip6 stays enabled */
     414           1 :                 ip6_link_enable (sw_if_index, NULL);
     415           1 :                 client_state = &rm->client_state_by_sw_if_index[sw_if_index];
     416           1 :                 if (--client_state->address_count == 0)
     417             :                   {
     418           1 :                     client_state->rebinding = 0;
     419           1 :                     client_state->server_index = ~0;
     420           1 :                     send_client_message_start_stop (sw_if_index, ~0,
     421             :                                                     DHCPV6_MSG_SOLICIT,
     422             :                                                     0, 1);
     423             :                   }
     424             :               }
     425             :           }
     426             :           /* *INDENT-ON* */
     427         105 :           for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++)
     428             :             {
     429          70 :               client_state_t *cs = &rm->client_state_by_sw_if_index[i];
     430          70 :               if (cs->enabled && cs->server_index != ~0)
     431             :                 {
     432          34 :                   if (cs->T2_due_time > current_time)
     433             :                     {
     434          33 :                       if (cs->T2_due_time < due_time)
     435          24 :                         due_time = cs->T2_due_time;
     436          33 :                       if (cs->T1_due_time > current_time)
     437             :                         {
     438          31 :                           if (cs->T1_due_time < due_time)
     439          16 :                             due_time = cs->T1_due_time;
     440             :                         }
     441             :                       else
     442             :                         {
     443           2 :                           cs->T1_due_time = DBL_MAX;
     444             :                           address_info_t *address_list;
     445           2 :                           address_list = create_address_list (i);
     446           2 :                           cs->rebinding = 1;
     447           2 :                           send_client_message_start_stop (i, cs->server_index,
     448             :                                                           DHCPV6_MSG_RENEW,
     449             :                                                           address_list, 1);
     450           2 :                           vec_free (address_list);
     451             :                         }
     452             :                     }
     453             :                   else
     454             :                     {
     455           1 :                       cs->T2_due_time = DBL_MAX;
     456             :                       address_info_t *address_list;
     457           1 :                       address_list = create_address_list (i);
     458           1 :                       cs->rebinding = 1;
     459           1 :                       send_client_message_start_stop (i, ~0,
     460             :                                                       DHCPV6_MSG_REBIND,
     461             :                                                       address_list, 1);
     462           1 :                       vec_free (address_list);
     463             :                     }
     464             :                 }
     465             :             }
     466          35 :           current_time = vlib_time_now (vm);
     467             :         }
     468          35 :       while (due_time < current_time);
     469             : 
     470          33 :       sleep_time = due_time - current_time;
     471             :     }
     472             : 
     473             :   return 0;
     474             : }
     475             : 
     476             : /* *INDENT-OFF* */
     477      149000 : VLIB_REGISTER_NODE (dhcp6_client_cp_process_node) = {
     478             :     .function = dhcp6_client_cp_process,
     479             :     .type = VLIB_NODE_TYPE_PROCESS,
     480             :     .name = "dhcp6-client-cp-process",
     481             : };
     482             : /* *INDENT-ON* */
     483             : 
     484             : static void
     485           5 : interrupt_process (void)
     486             : {
     487           5 :   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
     488           5 :   vlib_main_t *vm = rm->vlib_main;
     489             : 
     490           5 :   vlib_process_signal_event (vm, dhcp6_client_cp_process_node.index,
     491             :                              RD_CP_EVENT_INTERRUPT, 0);
     492           5 : }
     493             : 
     494             : static void
     495           7 : disable_process (void)
     496             : {
     497           7 :   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
     498           7 :   vlib_main_t *vm = rm->vlib_main;
     499             : 
     500           7 :   vlib_process_signal_event (vm, dhcp6_client_cp_process_node.index,
     501             :                              RD_CP_EVENT_DISABLE, 0);
     502           7 : }
     503             : 
     504             : static void
     505           7 : enable_process (void)
     506             : {
     507           7 :   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
     508           7 :   vlib_main_t *vm = rm->vlib_main;
     509             :   vlib_node_t *node;
     510             : 
     511           7 :   node = vec_elt (vm->node_main.nodes, rm->node_index);
     512             : 
     513           7 :   vlib_node_set_state (vm, rm->node_index, VLIB_NODE_STATE_POLLING);
     514           7 :   vlib_start_process (vm, node->runtime_index);
     515           7 : }
     516             : 
     517             : static clib_error_t *
     518           0 : dhcp6_addresses_show_command_function (vlib_main_t * vm,
     519             :                                        unformat_input_t * input,
     520             :                                        vlib_cli_command_t * cmd)
     521             : {
     522           0 :   dhcp6_client_cp_main_t *dm = &dhcp6_client_cp_main;
     523           0 :   clib_error_t *error = 0;
     524             :   address_info_t *address_info;
     525           0 :   f64 current_time = vlib_time_now (vm);
     526             : 
     527             :   /* *INDENT-OFF* */
     528           0 :   pool_foreach (address_info, dm->address_pool)
     529             :    {
     530           0 :     vlib_cli_output (vm, "address: %U, "
     531             :                      "preferred lifetime: %u, valid lifetime: %u "
     532             :                      "(%f remaining)",
     533             :                      format_ip6_address, &address_info->address,
     534             :                      address_info->preferred_lt, address_info->valid_lt,
     535           0 :                      address_info->due_time - current_time);
     536             :   }
     537             :   /* *INDENT-ON* */
     538             : 
     539           0 :   return error;
     540             : }
     541             : 
     542             : /* *INDENT-OFF* */
     543      224727 : VLIB_CLI_COMMAND (dhcp6_addresses_show_command, static) = {
     544             :   .path = "show dhcp6 addresses",
     545             :   .short_help = "show dhcp6 addresses",
     546             :   .function = dhcp6_addresses_show_command_function,
     547             : };
     548             : /* *INDENT-ON* */
     549             : 
     550             : static clib_error_t *
     551           0 : dhcp6_clients_show_command_function (vlib_main_t * vm,
     552             :                                      unformat_input_t * input,
     553             :                                      vlib_cli_command_t * cmd)
     554             : {
     555           0 :   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
     556           0 :   clib_error_t *error = 0;
     557             :   client_state_t *cs;
     558           0 :   f64 current_time = vlib_time_now (vm);
     559           0 :   u8 *buf1 = 0;
     560           0 :   u8 *buf2 = 0;
     561             :   const char *rebinding;
     562             :   u32 i;
     563             : 
     564           0 :   for (i = 0; i < vec_len (rm->client_state_by_sw_if_index); i++)
     565             :     {
     566           0 :       cs = &rm->client_state_by_sw_if_index[i];
     567           0 :       if (cs->enabled)
     568             :         {
     569           0 :           vec_reset_length (buf1);
     570           0 :           vec_reset_length (buf2);
     571           0 :           if (cs->T1_due_time != DBL_MAX && cs->T1_due_time > current_time)
     572             :             {
     573           0 :               buf1 = format (buf1, "%u remaining",
     574           0 :                              (u32) round (cs->T1_due_time - current_time));
     575             :             }
     576             :           else
     577           0 :             buf1 = format (buf1, "timeout");
     578           0 :           if (cs->T2_due_time != DBL_MAX && cs->T2_due_time > current_time)
     579           0 :             buf2 = format (buf2, "%u remaining",
     580           0 :                            (u32) round (cs->T2_due_time - current_time));
     581             :           else
     582           0 :             buf2 = format (buf2, "timeout");
     583           0 :           if (cs->rebinding)
     584           0 :             rebinding = ", REBINDING";
     585             :           else
     586           0 :             rebinding = "";
     587           0 :           if (cs->T1)
     588           0 :             vlib_cli_output (vm,
     589             :                              "sw_if_index: %u, T1: %u (%v), "
     590             :                              "T2: %u (%v), server index: %d%s", i,
     591             :                              cs->T1, buf1, cs->T2, buf2,
     592             :                              cs->server_index, rebinding);
     593             :           else
     594           0 :             vlib_cli_output (vm, "sw_if_index: %u%s", i, rebinding);
     595             :         }
     596             :     }
     597             : 
     598           0 :   vec_free (buf1);
     599           0 :   vec_free (buf2);
     600             : 
     601           0 :   return error;
     602             : }
     603             : 
     604             : /* *INDENT-OFF* */
     605      224727 : VLIB_CLI_COMMAND (dhcp6_clients_show_command, static) = {
     606             :   .path = "show dhcp6 clients",
     607             :   .short_help = "show dhcp6 clients",
     608             :   .function = dhcp6_clients_show_command_function,
     609             : };
     610             : /* *INDENT-ON* */
     611             : 
     612             : int
     613          14 : dhcp6_client_enable_disable (u32 sw_if_index, u8 enable)
     614             : {
     615          14 :   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
     616          14 :   vnet_main_t *vnm = rm->vnet_main;
     617          14 :   vlib_main_t *vm = rm->vlib_main;
     618             :   client_state_t *client_state;
     619          14 :   client_state_t empty_config = { 0 };
     620             :   address_info_t *address_info;
     621             :   clib_error_t *error;
     622             : 
     623          14 :   if (!vnet_sw_interface_is_api_valid (vnm, sw_if_index))
     624             :     {
     625           0 :       clib_warning ("Invalid sw_if_index");
     626           0 :       return 1;
     627             :     }
     628             : 
     629          16 :   vec_validate_init_empty (rm->client_state_by_sw_if_index, sw_if_index,
     630             :                            empty_config);
     631          14 :   client_state = &rm->client_state_by_sw_if_index[sw_if_index];
     632             : 
     633          14 :   u8 old_enabled = client_state->enabled;
     634          14 :   if (enable)
     635           7 :     client_state->enabled = 1;
     636          14 :   client_state->server_index = ~0;
     637             : 
     638          14 :   if (!old_enabled && enable)
     639             :     {
     640           7 :       rm->n_clients++;
     641           7 :       if (rm->n_clients == 1)
     642             :         {
     643           7 :           enable_process ();
     644           7 :           dhcp6_clients_enable_disable (1);
     645             :         }
     646             : 
     647           7 :       ip6_link_enable (sw_if_index, NULL);
     648           7 :       send_client_message_start_stop (sw_if_index, ~0, DHCPV6_MSG_SOLICIT,
     649             :                                       0, 1);
     650             :     }
     651           7 :   else if (old_enabled && !enable)
     652             :     {
     653           7 :       send_client_message_start_stop (sw_if_index, ~0, ~0, 0, 0);
     654             : 
     655           7 :       rm->n_clients--;
     656           7 :       if (rm->n_clients == 0)
     657             :         {
     658           7 :           dhcp6_clients_enable_disable (0);
     659           7 :           disable_process ();
     660             :         }
     661             : 
     662             :       /* *INDENT-OFF* */
     663           7 :       pool_foreach (address_info, rm->address_pool)
     664             :        {
     665           0 :         if (address_info->sw_if_index == sw_if_index)
     666             :           {
     667           0 :             ASSERT (sw_if_index < vec_len (rm->client_state_by_sw_if_index) &&
     668             :                     rm->client_state_by_sw_if_index[sw_if_index].enabled);
     669           0 :             client_state_t *client_state =
     670           0 :               &rm->client_state_by_sw_if_index[sw_if_index];
     671           0 :             send_client_message_start_stop (sw_if_index,
     672             :                                             client_state->server_index,
     673             :                                             DHCPV6_MSG_RELEASE, address_info,
     674             :                                             1);
     675           0 :             error = ip6_add_del_interface_address (vm, sw_if_index,
     676             :                                                    &address_info->address,
     677             :                                                    64, 1);
     678           0 :             if (error)
     679           0 :                 clib_warning ("Failed to delete interface address");
     680           0 :             pool_put (rm->address_pool, address_info);
     681             :           }
     682             :       }
     683             :       /* *INDENT-ON* */
     684             :     }
     685             : 
     686          14 :   if (!enable)
     687           7 :     client_state->enabled = 0;
     688             : 
     689          14 :   return 0;
     690             : }
     691             : 
     692             : static clib_error_t *
     693           0 : dhcp6_client_enable_disable_command_fn (vlib_main_t * vm,
     694             :                                         unformat_input_t * input,
     695             :                                         vlib_cli_command_t * cmd)
     696             : {
     697           0 :   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
     698           0 :   vnet_main_t *vnm = rm->vnet_main;
     699           0 :   clib_error_t *error = 0;
     700           0 :   u32 sw_if_index = ~0;
     701           0 :   u8 enable = 1;
     702           0 :   unformat_input_t _line_input, *line_input = &_line_input;
     703             : 
     704           0 :   if (!unformat_user (input, unformat_line_input, line_input))
     705           0 :     return 0;
     706             : 
     707           0 :   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     708             :     {
     709           0 :       if (unformat
     710             :           (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
     711             :         ;
     712           0 :       else if (unformat (line_input, "disable"))
     713           0 :         enable = 0;
     714             :       else
     715             :         {
     716           0 :           error = clib_error_return (0, "unexpected input `%U'",
     717             :                                      format_unformat_error, line_input);
     718           0 :           goto done;
     719             :         }
     720             :     }
     721             : 
     722           0 :   unformat_free (line_input);
     723             : 
     724           0 :   if (sw_if_index != ~0)
     725             :     {
     726           0 :       if (dhcp6_client_enable_disable (sw_if_index, enable) != 0)
     727           0 :         error = clib_error_return (0, "Invalid sw_if_index");
     728             :     }
     729             :   else
     730           0 :     error = clib_error_return (0, "Missing sw_if_index");
     731             : 
     732           0 : done:
     733           0 :   return error;
     734             : }
     735             : 
     736             : /*?
     737             :  * This command is used to enable/disable DHCPv6 client
     738             :  * on particular interface.
     739             :  *
     740             :  * @cliexpar
     741             :  * @parblock
     742             :  * Example of how to enable DHCPv6 client:
     743             :  * @cliexcmd{dhcp6 client GigabitEthernet2/0/0}
     744             :  * Example of how to disable DHCPv6 client:
     745             :  * @cliexcmd{dhcp6 client GigabitEthernet2/0/0 disable}
     746             :  * @endparblock
     747             : ?*/
     748             : /* *INDENT-OFF* */
     749      224727 : VLIB_CLI_COMMAND (dhcp6_client_enable_disable_command, static) = {
     750             :   .path = "dhcp6 client",
     751             :   .short_help = "dhcp6 client <interface> [disable]",
     752             :   .function = dhcp6_client_enable_disable_command_fn,
     753             : };
     754             : /* *INDENT-ON* */
     755             : 
     756             : static clib_error_t *
     757         559 : dhcp_ia_na_client_cp_init (vlib_main_t * vm)
     758             : {
     759         559 :   dhcp6_client_cp_main_t *rm = &dhcp6_client_cp_main;
     760             : 
     761         559 :   rm->vlib_main = vm;
     762         559 :   rm->vnet_main = vnet_get_main ();
     763         559 :   rm->api_main = vlibapi_get_main ();
     764         559 :   rm->node_index = dhcp6_client_cp_process_node.index;
     765             : 
     766         559 :   return NULL;
     767             : }
     768             : 
     769        2239 : VLIB_INIT_FUNCTION (dhcp_ia_na_client_cp_init);
     770             : 
     771             : /*
     772             :  * fd.io coding-style-patch-verification: ON
     773             :  *
     774             :  * Local Variables:
     775             :  * eval: (c-set-style "gnu")
     776             :  * End:
     777             :  */

Generated by: LCOV version 1.14