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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __CNAT_TYPES_H__
17 #define __CNAT_TYPES_H__
19 #include <vnet/fib/fib_node.h>
20 #include <vnet/fib/fib_source.h>
21 #include <vnet/ip/ip_types.h>
22 #include <vnet/ip/ip.h>
24 /* only in the default table for v4 and v6 */
25 #define CNAT_FIB_TABLE 0
27 /* default lifetime of NAT sessions (seconds) */
28 #define CNAT_DEFAULT_SESSION_MAX_AGE 30
29 /* lifetime of TCP conn NAT sessions after SYNACK (seconds) */
30 #define CNAT_DEFAULT_TCP_MAX_AGE 3600
31 /* lifetime of TCP conn NAT sessions after RST/FIN (seconds) */
32 #define CNAT_DEFAULT_TCP_RST_TIMEOUT 5
33 #define CNAT_DEFAULT_SCANNER_TIMEOUT (1.0)
35 #define CNAT_DEFAULT_SESSION_BUCKETS 1024
36 #define CNAT_DEFAULT_TRANSLATION_BUCKETS 1024
37 #define CNAT_DEFAULT_SNAT_BUCKETS 1024
39 #define CNAT_DEFAULT_SESSION_MEMORY (1 << 20)
40 #define CNAT_DEFAULT_TRANSLATION_MEMORY (256 << 10)
41 #define CNAT_DEFAULT_SNAT_MEMORY (64 << 20)
43 /* This should be strictly lower than FIB_SOURCE_INTERFACE
44 * from fib_source.h */
45 #define CNAT_FIB_SOURCE_PRIORITY 0x02
47 /* Initial refcnt for timestamps (2 : session & rsession) */
48 #define CNAT_TIMESTAMP_INIT_REFCNT 2
50 #define MIN_SRC_PORT ((u16) 0xC000)
54 /* Endpoint addr has been resolved */
55 CNAT_EP_FLAG_RESOLVED = 1,
58 typedef struct cnat_endpoint_t_
66 typedef struct cnat_endpoint_tuple_t_
68 cnat_endpoint_t dst_ep;
69 cnat_endpoint_t src_ep;
70 } cnat_endpoint_tuple_t;
80 u32 dst_address_length_refcounts[129];
81 u16 *prefix_lengths_in_search_order;
82 uword *non_empty_dst_address_length_bitmap;
83 } cnat_snat_pfx_table_meta_t;
87 /* Stores (ip family, prefix & mask) */
88 clib_bihash_24_8_t ip_hash;
89 /* family dependant cache */
90 cnat_snat_pfx_table_meta_t meta[2];
91 /* Precomputed ip masks (ip4 & ip6) */
92 ip6_address_t ip_masks[129];
93 } cnat_snat_pfx_table_t;
95 typedef struct cnat_main_
97 /* Memory size of the session bihash */
98 uword session_hash_memory;
100 /* Number of buckets of the session bihash */
101 u32 session_hash_buckets;
103 /* Memory size of the translation bihash */
104 uword translation_hash_memory;
106 /* Number of buckets of the translation bihash */
107 u32 translation_hash_buckets;
109 /* Memory size of the source NAT prefix bihash */
110 uword snat_hash_memory;
112 /* Number of buckets of the source NAT prefix bihash */
113 u32 snat_hash_buckets;
115 /* Timeout after which to clear sessions (in seconds) */
118 /* Timeout after which to clear an established TCP
119 * session (in seconds) */
122 /* delay in seconds between two scans of session/clients tables */
125 /* Lock for the timestamp pool */
126 clib_rwlock_t ts_lock;
128 /* Ip4 Address to use for source NATing */
129 cnat_endpoint_t snat_ip4;
131 /* Ip6 Address to use for source NATing */
132 cnat_endpoint_t snat_ip6;
134 /* Longest prefix Match table for source NATing */
135 cnat_snat_pfx_table_t snat_pfx_table;
137 /* Index of the scanner process node */
138 uword scanner_node_index;
140 /* Did we do lazy init ? */
143 /* Enable or Disable the scanner on startup */
144 u8 default_scanner_state;
147 typedef struct cnat_timestamp_t_
149 /* Last time said session was seen */
151 /* expire after N seconds */
153 /* Users refcount, initially 3 (session, rsession, dpo) */
157 typedef struct cnat_node_ctx_
162 ip_address_family_t af;
166 cnat_main_t *cnat_get_main ();
167 extern u8 *format_cnat_endpoint (u8 * s, va_list * args);
168 extern uword unformat_cnat_ep_tuple (unformat_input_t * input,
170 extern uword unformat_cnat_ep (unformat_input_t * input, va_list * args);
171 extern cnat_timestamp_t *cnat_timestamps;
172 extern fib_source_t cnat_fib_source;
173 extern cnat_main_t cnat_main;
174 extern throttle_t cnat_throttle;
176 extern char *cnat_error_strings[];
180 #define cnat_error(n,s) CNAT_ERROR_##n,
181 #include <cnat/cnat_error.def>
186 typedef enum cnat_scanner_cmd_t_
190 } cnat_scanner_cmd_t;
193 * Lazy initialization when first adding a translation
196 extern void cnat_lazy_init ();
199 * Enable/Disable session cleanup
201 extern void cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type);
204 * Resolve endpoint address
206 extern u8 cnat_resolve_ep (cnat_endpoint_t * ep);
207 extern u8 cnat_resolve_addr (u32 sw_if_index, ip_address_family_t af,
208 ip_address_t * addr);
212 * fd.io coding-style-patch-verification: ON
215 * eval: (c-set-style "gnu")