3 * snat.h - simple nat definitions
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 #ifndef __included_snat_h__
19 #define __included_snat_h__
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/ip/icmp46_packet.h>
25 #include <vnet/api_errno.h>
26 #include <vppinfra/bihash_8_8.h>
27 #include <vppinfra/dlist.h>
28 #include <vppinfra/error.h>
29 #include <vlibapi/api.h>
69 } snat_static_mapping_key_t;
73 SNAT_PROTOCOL_UDP = 0,
79 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
81 typedef CLIB_PACKED(struct {
82 snat_session_key_t out2in; /* 0-15 */
84 snat_session_key_t in2out; /* 16-31 */
86 u32 flags; /* 32-35 */
88 /* per-user translations */
89 u32 per_user_index; /* 36-39 */
91 u32 per_user_list_head_index; /* 40-43 */
93 /* Last heard timer */
94 f64 last_heard; /* 44-51 */
96 u64 total_bytes; /* 52-59 */
98 u32 total_pkts; /* 60-63 */
100 /* Outside address */
101 u32 outside_address_index; /* 64-67 */
108 u32 sessions_per_user_list_head_index;
116 uword * busy_port_bitmap;
120 ip4_address_t local_addr;
121 ip4_address_t external_addr;
127 } snat_static_mapping_t;
135 ip4_address_t l_addr;
142 } snat_static_map_resolve_t;
149 snat_session_t * sessions;
151 /* Pool of doubly-linked list elements */
152 dlist_elt_t * list_pool;
153 } snat_main_per_thread_data_t;
156 /* Main lookup tables */
157 clib_bihash_8_8_t out2in;
158 clib_bihash_8_8_t in2out;
160 /* Find-a-user => src address lookup */
161 clib_bihash_8_8_t user_hash;
163 /* Non-translated packets worker lookup => src address + VRF */
164 clib_bihash_8_8_t worker_by_in;
166 /* Translated packets worker lookup => IP address + port number */
167 clib_bihash_8_8_t worker_by_out;
170 u32 first_worker_index;
174 /* Per thread data */
175 snat_main_per_thread_data_t * per_thread_data;
177 /* Find a static mapping by local */
178 clib_bihash_8_8_t static_mapping_by_local;
180 /* Find a static mapping by external */
181 clib_bihash_8_8_t static_mapping_by_external;
183 /* Static mapping pool */
184 snat_static_mapping_t * static_mappings;
187 snat_interface_t * interfaces;
189 /* Vector of outside addresses */
190 snat_address_t * addresses;
192 /* sw_if_indices whose intfc addresses should be auto-added */
193 u32 * auto_add_sw_if_indices;
195 /* vector of interface address static mappings to resolve. */
196 snat_static_map_resolve_t *to_resolve;
198 /* Randomize port allocation order */
201 /* Worker handoff index */
205 /* Config parameters */
206 u8 static_mapping_only;
207 u8 static_mapping_connection_tracking;
208 u32 translation_buckets;
209 u32 translation_memory_size;
211 u32 user_memory_size;
212 u32 max_translations_per_user;
214 u32 outside_fib_index;
216 u32 inside_fib_index;
218 /* API message ID base */
222 vlib_main_t * vlib_main;
223 vnet_main_t * vnet_main;
224 ip4_main_t * ip4_main;
225 ip_lookup_main_t * ip4_lookup_main;
226 ethernet_main_t * ethernet_main;
227 api_main_t * api_main;
230 extern snat_main_t snat_main;
231 extern vlib_node_registration_t snat_in2out_node;
232 extern vlib_node_registration_t snat_out2in_node;
233 extern vlib_node_registration_t snat_in2out_fast_node;
234 extern vlib_node_registration_t snat_out2in_fast_node;
235 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
236 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
238 void snat_free_outside_address_and_port (snat_main_t * sm,
239 snat_session_key_t * k,
242 int snat_alloc_outside_address_and_port (snat_main_t * sm,
243 snat_session_key_t * k,
244 u32 * address_indexp);
246 int snat_static_mapping_match (snat_main_t * sm,
247 snat_session_key_t match,
248 snat_session_key_t * mapping,
251 format_function_t format_snat_user;
254 u32 cached_sw_if_index;
255 u32 cached_ip4_address;
258 /** \brief Check if SNAT session is created from static mapping.
259 @param s SNAT session
260 @return 1 if SNAT session is created from static mapping otherwise 0
262 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
265 * Why is this here? Because we don't need to touch this layer to
266 * simply reply to an icmp. We need to change id to a unique
267 * value to NAT an echo request/reply.
273 } icmp_echo_header_t;
275 #endif /* __included_snat_h__ */