LCOV - code coverage report
Current view: top level - plugins/nat/lib - nat_inlines.h (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 30 35 85.7 %
Date: 2023-10-26 01:39:38 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2020 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             : #ifndef __included_lib_nat_inlines_h__
      17             : #define __included_lib_nat_inlines_h__
      18             : 
      19             : #include <vnet/tcp/tcp_packet.h>
      20             : #include <vnet/ip/ip4_packet.h>
      21             : 
      22             : static inline void
      23         381 : increment_v4_address (ip4_address_t * a)
      24             : {
      25             :   u32 v;
      26             : 
      27         381 :   v = clib_net_to_host_u32 (a->as_u32) + 1;
      28         381 :   a->as_u32 = clib_host_to_net_u32 (v);
      29         381 : }
      30             : 
      31             : always_inline void
      32       13999 : mss_clamping (u16 mss_clamping, tcp_header_t * tcp, ip_csum_t * sum)
      33             : {
      34             :   u8 *data;
      35             :   u8 opt_len, opts_len, kind;
      36             :   u16 mss;
      37             : 
      38       13999 :   if (!(mss_clamping && tcp_syn (tcp)))
      39       13997 :     return;
      40             : 
      41           2 :   opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t);
      42           2 :   data = (u8 *) (tcp + 1);
      43           2 :   for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
      44             :     {
      45           2 :       kind = data[0];
      46             : 
      47           2 :       if (kind == TCP_OPTION_EOL)
      48           0 :         break;
      49           2 :       else if (kind == TCP_OPTION_NOOP)
      50             :         {
      51           0 :           opt_len = 1;
      52           0 :           continue;
      53             :         }
      54             :       else
      55             :         {
      56           2 :           if (opts_len < 2)
      57           0 :             return;
      58           2 :           opt_len = data[1];
      59             : 
      60           2 :           if (opt_len < 2 || opt_len > opts_len)
      61           0 :             return;
      62             :         }
      63             : 
      64           2 :       if (kind == TCP_OPTION_MSS)
      65             :         {
      66           2 :           mss = *(u16 *) (data + 2);
      67           2 :           if (clib_net_to_host_u16 (mss) > mss_clamping)
      68             :             {
      69           1 :               u16 mss_value_net = clib_host_to_net_u16 (mss_clamping);
      70           1 :               *sum =
      71           1 :                 ip_csum_update (*sum, mss, mss_value_net, ip4_header_t,
      72             :                                 length);
      73           1 :               clib_memcpy_fast (data + 2, &mss_value_net, 2);
      74             :             }
      75           2 :           return;
      76             :         }
      77             :     }
      78             : }
      79             : 
      80             : static_always_inline u16
      81       10467 : nat_random_port (u32 *random_seed, u16 min, u16 max)
      82             : {
      83             :   u32 rwide;
      84             :   u16 r;
      85             : 
      86       10467 :   rwide = random_u32 (random_seed);
      87       10467 :   r = rwide & 0xFFFF;
      88       10467 :   if (r >= min && r <= max)
      89       10300 :     return r;
      90             : 
      91         167 :   return min + (rwide % (max - min + 1));
      92             : }
      93             : 
      94             : #endif /* __included_lib_nat_inlines_h__ */
      95             : 
      96             : /*
      97             :  * fd.io coding-style-patch-verification: ON
      98             :  *
      99             :  * Local Variables:
     100             :  * eval: (c-set-style "gnu")
     101             :  * End:
     102             :  */

Generated by: LCOV version 1.14