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>
72 #define foreach_snat_protocol \
73 _(UDP, 0, udp, "udp") \
74 _(TCP, 1, tcp, "tcp") \
75 _(ICMP, 2, icmp, "icmp")
78 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
84 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
86 typedef CLIB_PACKED(struct {
87 snat_session_key_t out2in; /* 0-15 */
89 snat_session_key_t in2out; /* 16-31 */
91 u32 flags; /* 32-35 */
93 /* per-user translations */
94 u32 per_user_index; /* 36-39 */
96 u32 per_user_list_head_index; /* 40-43 */
98 /* Last heard timer */
99 f64 last_heard; /* 44-51 */
101 u64 total_bytes; /* 52-59 */
103 u32 total_pkts; /* 60-63 */
105 /* Outside address */
106 u32 outside_address_index; /* 64-67 */
114 u32 sessions_per_user_list_head_index;
122 #define _(N, i, n, s) \
123 u32 busy_##n##_ports; \
124 uword * busy_##n##_port_bitmap;
125 foreach_snat_protocol
130 ip4_address_t local_addr;
131 ip4_address_t external_addr;
137 snat_protocol_t proto;
138 } snat_static_mapping_t;
146 ip4_address_t l_addr;
151 snat_protocol_t proto;
154 } snat_static_map_resolve_t;
161 snat_session_t * sessions;
163 /* Pool of doubly-linked list elements */
164 dlist_elt_t * list_pool;
165 } snat_main_per_thread_data_t;
168 /* Main lookup tables */
169 clib_bihash_8_8_t out2in;
170 clib_bihash_8_8_t in2out;
172 /* Find-a-user => src address lookup */
173 clib_bihash_8_8_t user_hash;
175 /* Non-translated packets worker lookup => src address + VRF */
176 clib_bihash_8_8_t worker_by_in;
178 /* Translated packets worker lookup => IP address + port number */
179 clib_bihash_8_8_t worker_by_out;
182 u32 first_worker_index;
186 /* Per thread data */
187 snat_main_per_thread_data_t * per_thread_data;
189 /* Find a static mapping by local */
190 clib_bihash_8_8_t static_mapping_by_local;
192 /* Find a static mapping by external */
193 clib_bihash_8_8_t static_mapping_by_external;
195 /* Static mapping pool */
196 snat_static_mapping_t * static_mappings;
199 snat_interface_t * interfaces;
201 /* Vector of outside addresses */
202 snat_address_t * addresses;
204 /* sw_if_indices whose intfc addresses should be auto-added */
205 u32 * auto_add_sw_if_indices;
207 /* vector of interface address static mappings to resolve. */
208 snat_static_map_resolve_t *to_resolve;
210 /* Randomize port allocation order */
213 /* Worker handoff index */
217 /* Config parameters */
218 u8 static_mapping_only;
219 u8 static_mapping_connection_tracking;
220 u32 translation_buckets;
221 u32 translation_memory_size;
223 u32 user_memory_size;
224 u32 max_translations_per_user;
226 u32 outside_fib_index;
228 u32 inside_fib_index;
230 /* tenant VRF aware address pool activation flag */
233 /* API message ID base */
237 vlib_main_t * vlib_main;
238 vnet_main_t * vnet_main;
239 ip4_main_t * ip4_main;
240 ip_lookup_main_t * ip4_lookup_main;
241 api_main_t * api_main;
244 extern snat_main_t snat_main;
245 extern vlib_node_registration_t snat_in2out_node;
246 extern vlib_node_registration_t snat_out2in_node;
247 extern vlib_node_registration_t snat_in2out_fast_node;
248 extern vlib_node_registration_t snat_out2in_fast_node;
249 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
250 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
252 void snat_free_outside_address_and_port (snat_main_t * sm,
253 snat_session_key_t * k,
256 int snat_alloc_outside_address_and_port (snat_main_t * sm,
258 snat_session_key_t * k,
259 u32 * address_indexp);
261 int snat_static_mapping_match (snat_main_t * sm,
262 snat_session_key_t match,
263 snat_session_key_t * mapping,
266 format_function_t format_snat_user;
269 u32 cached_sw_if_index;
270 u32 cached_ip4_address;
273 /** \brief Check if SNAT session is created from static mapping.
274 @param s SNAT session
275 @return 1 if SNAT session is created from static mapping otherwise 0
277 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
280 * Why is this here? Because we don't need to touch this layer to
281 * simply reply to an icmp. We need to change id to a unique
282 * value to NAT an echo request/reply.
288 } icmp_echo_header_t;
290 always_inline snat_protocol_t
291 ip_proto_to_snat_proto (u8 ip_proto)
293 snat_protocol_t snat_proto = ~0;
295 snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
296 snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
297 snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
303 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
307 ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
308 ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
309 ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
314 #endif /* __included_snat_h__ */