LCOV - code coverage report
Current view: top level - vnet/ip - ip6_format.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 134 187 71.7 %
Date: 2023-10-26 01:39:38 Functions: 7 10 70.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015 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             :  * ip/ip6_format.c: ip6 formatting
      17             :  *
      18             :  * Copyright (c) 2008 Eliot Dresselhaus
      19             :  *
      20             :  * Permission is hereby granted, free of charge, to any person obtaining
      21             :  * a copy of this software and associated documentation files (the
      22             :  * "Software"), to deal in the Software without restriction, including
      23             :  * without limitation the rights to use, copy, modify, merge, publish,
      24             :  * distribute, sublicense, and/or sell copies of the Software, and to
      25             :  * permit persons to whom the Software is furnished to do so, subject to
      26             :  * the following conditions:
      27             :  *
      28             :  * The above copyright notice and this permission notice shall be
      29             :  * included in all copies or substantial portions of the Software.
      30             :  *
      31             :  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      32             :  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      33             :  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
      34             :  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      35             :  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
      36             :  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
      37             :  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      38             :  */
      39             : 
      40             : #include <vnet/ip/ip.h>
      41             : 
      42             : /* Format an IP6 address. */
      43             : u8 *
      44     1571790 : format_ip6_address (u8 * s, va_list * args)
      45             : {
      46     1571790 :   ip6_address_t *a = va_arg (*args, ip6_address_t *);
      47     1571790 :   u32 max_zero_run = 0, this_zero_run = 0;
      48     1571790 :   int max_zero_run_index = -1, this_zero_run_index = 0;
      49     1571790 :   int in_zero_run = 0, i;
      50     1571790 :   int last_double_colon = 0;
      51             : 
      52             :   /* Ugh, this is a pain. Scan forward looking for runs of 0's */
      53    14146100 :   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
      54             :     {
      55    12574300 :       if (a->as_u16[i] == 0)
      56             :         {
      57     6881450 :           if (in_zero_run)
      58     5534760 :             this_zero_run++;
      59             :           else
      60             :             {
      61     1346690 :               in_zero_run = 1;
      62     1346690 :               this_zero_run = 1;
      63     1346690 :               this_zero_run_index = i;
      64             :             }
      65             :         }
      66             :       else
      67             :         {
      68     5692900 :           if (in_zero_run)
      69             :             {
      70             :               /* offer to compress the biggest run of > 1 zero */
      71     1306380 :               if (this_zero_run > max_zero_run && this_zero_run > 1)
      72             :                 {
      73     1305330 :                   max_zero_run_index = this_zero_run_index;
      74     1305330 :                   max_zero_run = this_zero_run;
      75             :                 }
      76             :             }
      77     5692900 :           in_zero_run = 0;
      78     5692900 :           this_zero_run = 0;
      79             :         }
      80             :     }
      81             : 
      82     1571790 :   if (in_zero_run)
      83             :     {
      84       40303 :       if (this_zero_run > max_zero_run && this_zero_run > 1)
      85             :         {
      86       39367 :           max_zero_run_index = this_zero_run_index;
      87       39367 :           max_zero_run = this_zero_run;
      88             :         }
      89             :     }
      90             : 
      91     8611380 :   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
      92             :     {
      93     7039590 :       if (i == max_zero_run_index)
      94             :         {
      95     1344700 :           s = format (s, "::");
      96     1344700 :           i += max_zero_run - 1;
      97     1344700 :           last_double_colon = 1;
      98             :         }
      99             :       else
     100             :         {
     101    10084500 :           s = format (s, "%s%x",
     102     4389560 :                       (last_double_colon || i == 0) ? "" : ":",
     103     5694900 :                       clib_net_to_host_u16 (a->as_u16[i]));
     104     5694900 :           last_double_colon = 0;
     105             :         }
     106             :     }
     107             : 
     108     1571790 :   return s;
     109             : }
     110             : 
     111             : /* Format an IP6 route destination and length. */
     112             : u8 *
     113           5 : format_ip6_address_and_length (u8 * s, va_list * args)
     114             : {
     115           5 :   ip6_address_t *a = va_arg (*args, ip6_address_t *);
     116           5 :   u8 l = va_arg (*args, u32);
     117           5 :   return format (s, "%U/%d", format_ip6_address, a, l);
     118             : }
     119             : 
     120             : u8 *
     121           0 : format_ip6_address_and_mask (u8 * s, va_list * args)
     122             : {
     123           0 :   ip6_address_and_mask_t *am = va_arg (*args, ip6_address_and_mask_t *);
     124             : 
     125           0 :   if (am->addr.as_u64[0] == 0 && am->addr.as_u64[1] == 0 &&
     126           0 :       am->mask.as_u64[0] == 0 && am->mask.as_u64[1] == 0)
     127           0 :     return format (s, "any");
     128             : 
     129           0 :   if (am->mask.as_u64[0] == ~0 && am->mask.as_u64[1] == ~0)
     130           0 :     return format (s, "%U", format_ip6_address, &am->addr);
     131             : 
     132           0 :   return format (s, "%U/%U", format_ip6_address, &am->addr,
     133             :                  format_ip6_address, &am->mask);
     134             : }
     135             : 
     136             : /* Parse an IP6 address. */
     137             : uword
     138         743 : unformat_ip6_address (unformat_input_t * input, va_list * args)
     139             : {
     140         743 :   ip6_address_t *result = va_arg (*args, ip6_address_t *);
     141             :   u16 hex_quads[8];
     142             :   uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
     143             :   uword c, n_colon, double_colon_index;
     144             : 
     145         743 :   n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
     146         743 :   double_colon_index = ARRAY_LEN (hex_quads);
     147        4275 :   while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
     148             :     {
     149        4187 :       hex_digit = 16;
     150        4187 :       if (c >= '0' && c <= '9')
     151        1954 :         hex_digit = c - '0';
     152        2233 :       else if (c >= 'a' && c <= 'f')
     153         558 :         hex_digit = c + 10 - 'a';
     154        1675 :       else if (c >= 'A' && c <= 'F')
     155          24 :         hex_digit = c + 10 - 'A';
     156        1651 :       else if (c == ':' && n_colon < 2)
     157         996 :         n_colon++;
     158             :       else
     159             :         {
     160         655 :           unformat_put_input (input);
     161         655 :           break;
     162             :         }
     163             : 
     164             :       /* Too many hex quads. */
     165        3532 :       if (n_hex_quads >= ARRAY_LEN (hex_quads))
     166           0 :         return 0;
     167             : 
     168        3532 :       if (hex_digit < 16)
     169             :         {
     170        2536 :           hex_quad = (hex_quad << 4) | hex_digit;
     171             : 
     172             :           /* Hex quad must fit in 16 bits. */
     173        2536 :           if (n_hex_digits >= 4)
     174           0 :             return 0;
     175             : 
     176        2536 :           n_colon = 0;
     177        2536 :           n_hex_digits++;
     178             :         }
     179             : 
     180             :       /* Save position of :: */
     181        3532 :       if (n_colon == 2)
     182             :         {
     183             :           /* More than one :: ? */
     184         435 :           if (double_colon_index < ARRAY_LEN (hex_quads))
     185           0 :             return 0;
     186         435 :           double_colon_index = n_hex_quads;
     187             :         }
     188             : 
     189        3532 :       if (n_colon > 0 && n_hex_digits > 0)
     190             :         {
     191         561 :           hex_quads[n_hex_quads++] = hex_quad;
     192         561 :           hex_quad = 0;
     193         561 :           n_hex_digits = 0;
     194             :         }
     195             :     }
     196             : 
     197         743 :   if (n_hex_digits > 0)
     198         417 :     hex_quads[n_hex_quads++] = hex_quad;
     199             : 
     200             :   {
     201             :     word i;
     202             : 
     203             :     /* Expand :: to appropriate number of zero hex quads. */
     204         743 :     if (double_colon_index < ARRAY_LEN (hex_quads))
     205             :       {
     206         435 :         word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
     207             : 
     208         754 :         for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
     209         319 :           hex_quads[n_zero + i] = hex_quads[i];
     210             : 
     211        3049 :         for (i = 0; i < n_zero; i++)
     212             :           {
     213        2614 :             ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
     214        2614 :             hex_quads[double_colon_index + i] = 0;
     215             :           }
     216             : 
     217         435 :         n_hex_quads = ARRAY_LEN (hex_quads);
     218             :       }
     219             : 
     220             :     /* Too few hex quads given. */
     221         743 :     if (n_hex_quads < ARRAY_LEN (hex_quads))
     222         306 :       return 0;
     223             : 
     224        3933 :     for (i = 0; i < ARRAY_LEN (hex_quads); i++)
     225        3496 :       result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
     226             : 
     227         437 :     return 1;
     228             :   }
     229             : }
     230             : 
     231             : uword
     232           0 : unformat_ip6_address_and_mask (unformat_input_t * input, va_list * args)
     233             : {
     234           0 :   ip6_address_and_mask_t *am = va_arg (*args, ip6_address_and_mask_t *);
     235             :   ip6_address_t addr, mask;
     236             : 
     237           0 :   clib_memset (&addr, 0, sizeof (ip6_address_t));
     238           0 :   clib_memset (&mask, 0, sizeof (ip6_address_t));
     239             : 
     240           0 :   if (unformat (input, "any"))
     241             :     ;
     242           0 :   else if (unformat (input, "%U/%U", unformat_ip6_address, &addr,
     243             :                      unformat_ip6_address, &mask))
     244             :     ;
     245           0 :   else if (unformat (input, "%U", unformat_ip6_address, &addr))
     246           0 :     mask.as_u64[0] = mask.as_u64[1] = ~0;
     247             :   else
     248           0 :     return 0;
     249             : 
     250           0 :   am->addr.as_u64[0] = addr.as_u64[0];
     251           0 :   am->addr.as_u64[1] = addr.as_u64[1];
     252           0 :   am->mask.as_u64[0] = mask.as_u64[0];
     253           0 :   am->mask.as_u64[1] = mask.as_u64[1];
     254           0 :   return 1;
     255             : }
     256             : 
     257             : /* Format an IP6 header. */
     258             : u8 *
     259      618974 : format_ip6_header (u8 * s, va_list * args)
     260             : {
     261      618974 :   ip6_header_t *ip = va_arg (*args, ip6_header_t *);
     262      618974 :   u32 max_header_bytes = va_arg (*args, u32);
     263             :   u32 i, ip_version, traffic_class, flow_label;
     264             :   u32 indent;
     265             : 
     266             :   /* Nothing to do. */
     267      618974 :   if (max_header_bytes < sizeof (ip[0]))
     268           0 :     return format (s, "IP header truncated");
     269             : 
     270      618974 :   indent = format_get_indent (s);
     271      618974 :   indent += 2;
     272             : 
     273      618974 :   s = format (s, "%U: %U -> %U",
     274      618974 :               format_ip_protocol, ip->protocol,
     275             :               format_ip6_address, &ip->src_address,
     276             :               format_ip6_address, &ip->dst_address);
     277             : 
     278      618974 :   i = clib_net_to_host_u32 (ip->ip_version_traffic_class_and_flow_label);
     279      618974 :   ip_version = (i >> 28);
     280      618974 :   traffic_class = (i >> 20) & 0xff;
     281      618974 :   flow_label = i & pow2_mask (20);
     282             : 
     283      618974 :   if (ip_version != 6)
     284        2491 :     s = format (s, "\n%Uversion %d", format_white_space, indent, ip_version);
     285             : 
     286             :   s =
     287      618974 :     format (s,
     288             :             "\n%Utos 0x%02x, flow label 0x%x, hop limit %d, payload length %d",
     289             :             format_white_space, indent, traffic_class, flow_label,
     290      618974 :             ip->hop_limit, clib_net_to_host_u16 (ip->payload_length));
     291             : #if 0
     292             :   /* Recurse into next protocol layer. */
     293             :   if (max_header_bytes != 0 && sizeof (ip[0]) < max_header_bytes)
     294             :     {
     295             :       ip_main_t *im = &ip_main;
     296             :       ip_protocol_info_t *pi = ip_get_protocol_info (im, ip->protocol);
     297             : 
     298             :       if (pi && pi->format_header)
     299             :         s = format (s, "\n%U%U",
     300             :                     format_white_space, indent - 2, pi->format_header,
     301             :                     /* next protocol header */ (void *) (ip + 1),
     302             :                     max_header_bytes - sizeof (ip[0]));
     303             :     }
     304             : #endif
     305      618974 :   return s;
     306             : }
     307             : 
     308             : /* Parse an IP6 header. */
     309             : uword
     310           0 : unformat_ip6_header (unformat_input_t * input, va_list * args)
     311             : {
     312           0 :   u8 **result = va_arg (*args, u8 **);
     313             :   ip6_header_t *ip;
     314             :   int old_length;
     315             : 
     316             :   /* Allocate space for IP header. */
     317             :   {
     318             :     void *p;
     319             : 
     320           0 :     old_length = vec_len (*result);
     321           0 :     vec_add2 (*result, p, sizeof (ip[0]));
     322           0 :     ip = p;
     323             :   }
     324             : 
     325           0 :   clib_memset (ip, 0, sizeof (ip[0]));
     326           0 :   ip->ip_version_traffic_class_and_flow_label =
     327           0 :     clib_host_to_net_u32 (6 << 28);
     328             : 
     329           0 :   if (!unformat (input, "%U: %U -> %U",
     330             :                  unformat_ip_protocol, &ip->protocol,
     331             :                  unformat_ip6_address, &ip->src_address,
     332             :                  unformat_ip6_address, &ip->dst_address))
     333           0 :     return 0;
     334             : 
     335             :   /* Parse options. */
     336             :   while (1)
     337           0 :     {
     338             :       int i;
     339             : 
     340           0 :       if (unformat (input, "tos %U", unformat_vlib_number, &i))
     341           0 :         ip->ip_version_traffic_class_and_flow_label |=
     342           0 :           clib_host_to_net_u32 ((i & 0xff) << 20);
     343             : 
     344           0 :       else if (unformat (input, "hop-limit %U", unformat_vlib_number, &i))
     345           0 :         ip->hop_limit = i;
     346             : 
     347             :       /* Can't parse input: try next protocol level. */
     348             :       else
     349           0 :         break;
     350             :     }
     351             : 
     352             :   /* Recurse into next protocol layer. */
     353             :   {
     354           0 :     ip_main_t *im = &ip_main;
     355           0 :     ip_protocol_info_t *pi = ip_get_protocol_info (im, ip->protocol);
     356             : 
     357           0 :     if (pi && pi->unformat_header)
     358             :       {
     359           0 :         if (!unformat_user (input, pi->unformat_header, result))
     360           0 :           return 0;
     361             : 
     362             :         /* Result may have moved. */
     363           0 :         ip = (void *) *result + old_length;
     364             :       }
     365             :   }
     366             : 
     367           0 :   ip->payload_length =
     368           0 :     clib_host_to_net_u16 (vec_len (*result) - (old_length + sizeof (ip[0])));
     369             : 
     370           0 :   return 1;
     371             : }
     372             : 
     373             : /* Parse an IP46 address. */
     374             : uword
     375         439 : unformat_ip46_address (unformat_input_t * input, va_list * args)
     376             : {
     377         439 :   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
     378         439 :   ip46_type_t type = va_arg (*args, ip46_type_t);
     379         878 :   if ((type != IP46_TYPE_IP6) &&
     380         439 :       unformat (input, "%U", unformat_ip4_address, &ip46->ip4))
     381             :     {
     382         138 :       ip46_address_mask_ip4 (ip46);
     383         138 :       return 1;
     384             :     }
     385         602 :   else if ((type != IP46_TYPE_IP4) &&
     386         301 :            unformat (input, "%U", unformat_ip6_address, &ip46->ip6))
     387             :     {
     388          71 :       return 1;
     389             :     }
     390         230 :   return 0;
     391             : }
     392             : 
     393             : /* Format an IP46 address. */
     394             : u8 *
     395      736444 : format_ip46_address (u8 * s, va_list * args)
     396             : {
     397      736444 :   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
     398      736444 :   ip46_type_t type = va_arg (*args, ip46_type_t);
     399      736444 :   int is_ip4 = 1;
     400             : 
     401      736444 :   switch (type)
     402             :     {
     403       69221 :     case IP46_TYPE_ANY:
     404       69221 :       is_ip4 = ip46_address_is_ip4 (ip46);
     405       69221 :       break;
     406      404820 :     case IP46_TYPE_IP4:
     407      404820 :       is_ip4 = 1;
     408      404820 :       break;
     409      262403 :     case IP46_TYPE_IP6:
     410      262403 :       is_ip4 = 0;
     411      262403 :       break;
     412             :     }
     413             : 
     414      736444 :   return is_ip4 ?
     415     1039820 :     format (s, "%U", format_ip4_address, &ip46->ip4) :
     416      303374 :     format (s, "%U", format_ip6_address, &ip46->ip6);
     417             : }
     418             : 
     419             : u8 *
     420         159 : format_ip6_frag_hdr (u8 * s, va_list * args)
     421             : {
     422         159 :   ip6_frag_hdr_t *h = va_arg (*args, ip6_frag_hdr_t *);
     423         159 :   u32 max_header_bytes = va_arg (*args, u32);
     424             :   u32 header_bytes;
     425             : 
     426         159 :   header_bytes = sizeof (h[0]);
     427         159 :   if (max_header_bytes != 0 && header_bytes > max_header_bytes)
     428           0 :     return format (s, "ipv6 frag header truncated");
     429             : 
     430             :   s =
     431         159 :     format (s,
     432             :             "IPV6_FRAG_HDR: next_hdr: %u, rsv: %u, frag_offset_and_more: %u, id: %u",
     433         159 :             h->next_hdr, h->rsv, h->fragment_offset_and_more,
     434             :             clib_net_to_host_u32 (h->identification));
     435         159 :   return s;
     436             : }
     437             : 
     438             : /*
     439             :  * fd.io coding-style-patch-verification: ON
     440             :  *
     441             :  * Local Variables:
     442             :  * eval: (c-set-style "gnu")
     443             :  * End:
     444             :  */

Generated by: LCOV version 1.14