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 : * @file 17 : * @brief NAT port/address allocation lib 18 : */ 19 : #ifndef included_nat_lib_h__ 20 : #define included_nat_lib_h__ 21 : 22 : #include <vlibapi/api.h> 23 : 24 : typedef struct 25 : { 26 : u16 identifier; 27 : u16 sequence; 28 : } nat_icmp_echo_header_t; 29 : 30 : typedef struct 31 : { 32 : u16 src_port, dst_port; 33 : } nat_tcp_udp_header_t; 34 : 35 : /* NAT API Configuration flags */ 36 : #define foreach_nat_config_flag \ 37 : _(0x01, IS_TWICE_NAT) \ 38 : _(0x02, IS_SELF_TWICE_NAT) \ 39 : _(0x04, IS_OUT2IN_ONLY) \ 40 : _(0x08, IS_ADDR_ONLY) \ 41 : _(0x10, IS_OUTSIDE) \ 42 : _(0x20, IS_INSIDE) \ 43 : _(0x40, IS_STATIC) \ 44 : _(0x80, IS_EXT_HOST_VALID) 45 : 46 : typedef enum nat_config_flags_t_ 47 : { 48 : #define _(n,f) NAT_API_##f = n, 49 : foreach_nat_config_flag 50 : #undef _ 51 : } nat_config_flags_t; 52 : 53 : #define foreach_nat_counter _ (tcp) _ (udp) _ (icmp) _ (other) _ (drops) 54 : 55 : #define foreach_nat_error \ 56 : _ (VALUE_EXIST, -1, "Value already exists") \ 57 : _ (NO_SUCH_ENTRY, -2, "No such entry") \ 58 : _ (UNKNOWN_PROTOCOL, -3, "Unknown protocol") \ 59 : _ (OUT_OF_TRANSLATIONS, -4, "Out of translations") 60 : 61 : typedef enum 62 : { 63 : #define _(N, i, s) NAT_ERROR_##N = i, 64 : foreach_nat_error 65 : #undef _ 66 : } nat_error_t; 67 : 68 : /* default protocol timeouts */ 69 : #define NAT_UDP_TIMEOUT 300 70 : #define NAT_TCP_TRANSITORY_TIMEOUT 240 71 : #define NAT_TCP_ESTABLISHED_TIMEOUT 7440 72 : #define NAT_ICMP_TIMEOUT 60 73 : 74 : typedef struct 75 : { 76 : struct 77 : { 78 : u32 established; 79 : u32 transitory; 80 : } tcp; 81 : 82 : u32 udp; 83 : u32 icmp; 84 : 85 : } nat_timeouts_t; 86 : 87 : static_always_inline void 88 711 : nat_reset_timeouts (nat_timeouts_t * timeouts) 89 : { 90 711 : timeouts->udp = NAT_UDP_TIMEOUT; 91 711 : timeouts->tcp.established = NAT_TCP_ESTABLISHED_TIMEOUT; 92 711 : timeouts->tcp.transitory = NAT_TCP_TRANSITORY_TIMEOUT; 93 711 : timeouts->icmp = NAT_ICMP_TIMEOUT; 94 711 : } 95 : 96 : static_always_inline u32 97 211 : nat_calc_bihash_buckets (u32 n_elts) 98 : { 99 211 : n_elts = n_elts / 2.5; 100 211 : u64 lower_pow2 = 1; 101 2308 : while (lower_pow2 * 2 < n_elts) 102 : { 103 2097 : lower_pow2 = 2 * lower_pow2; 104 : } 105 211 : u64 upper_pow2 = 2 * lower_pow2; 106 211 : if ((upper_pow2 - n_elts) < (n_elts - lower_pow2)) 107 : { 108 188 : if (upper_pow2 <= UINT32_MAX) 109 : { 110 188 : return upper_pow2; 111 : } 112 : } 113 23 : return lower_pow2; 114 : } 115 : 116 : #endif /* included_nat_lib_h__ */ 117 : /* 118 : * fd.io coding-style-patch-verification: ON 119 : * 120 : * Local Variables: 121 : * eval: (c-set-style "gnu") 122 : * End: 123 : */