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>
32 #define SNAT_UDP_TIMEOUT 300
33 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
34 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
56 ip4_address_t ext_host_addr;
90 #define foreach_snat_protocol \
91 _(UDP, 0, udp, "udp") \
92 _(TCP, 1, tcp, "tcp") \
93 _(ICMP, 2, icmp, "icmp")
96 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
102 #define foreach_snat_session_state \
103 _(0, UNKNOWN, "unknown") \
104 _(1, UDP_ACTIVE, "udp-active") \
105 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
106 _(3, TCP_ESTABLISHED, "tcp-established") \
107 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
108 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
109 _(6, TCP_LAST_ACK, "tcp-last-ack")
112 #define _(v, N, s) SNAT_SESSION_##N = v,
113 foreach_snat_session_state
115 } snat_session_state_t;
118 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
120 typedef CLIB_PACKED(struct {
121 snat_session_key_t out2in; /* 0-15 */
123 snat_session_key_t in2out; /* 16-31 */
125 u32 flags; /* 32-35 */
127 /* per-user translations */
128 u32 per_user_index; /* 36-39 */
130 u32 per_user_list_head_index; /* 40-43 */
132 /* Last heard timer */
133 f64 last_heard; /* 44-51 */
135 u64 total_bytes; /* 52-59 */
137 u32 total_pkts; /* 60-63 */
139 /* Outside address */
140 u32 outside_address_index; /* 64-67 */
148 u32 sessions_per_user_list_head_index;
156 #define _(N, i, n, s) \
157 u32 busy_##n##_ports; \
158 uword * busy_##n##_port_bitmap;
159 foreach_snat_protocol
165 snat_det_out_key_t out;
168 } snat_det_session_t;
171 ip4_address_t in_addr;
173 ip4_address_t out_addr;
178 /* vector of sessions */
179 snat_det_session_t * sessions;
183 ip4_address_t local_addr;
184 ip4_address_t external_addr;
190 snat_protocol_t proto;
191 } snat_static_mapping_t;
199 ip4_address_t l_addr;
204 snat_protocol_t proto;
207 } snat_static_map_resolve_t;
214 snat_session_t * sessions;
216 /* Pool of doubly-linked list elements */
217 dlist_elt_t * list_pool;
218 } snat_main_per_thread_data_t;
222 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
223 vlib_node_runtime_t *node,
226 snat_session_key_t *p_key,
227 snat_session_key_t *p_value,
228 u8 *p_dont_translate,
231 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
233 typedef struct snat_main_s {
234 /* Main lookup tables */
235 clib_bihash_8_8_t out2in;
236 clib_bihash_8_8_t in2out;
238 /* Find-a-user => src address lookup */
239 clib_bihash_8_8_t user_hash;
241 /* Non-translated packets worker lookup => src address + VRF */
242 clib_bihash_8_8_t worker_by_in;
244 /* Translated packets worker lookup => IP address + port number */
245 clib_bihash_8_8_t worker_by_out;
247 snat_icmp_match_function_t * icmp_match_in2out_cb;
248 snat_icmp_match_function_t * icmp_match_out2in_cb;
251 u32 first_worker_index;
254 snat_get_worker_function_t * worker_in2out_cb;
255 snat_get_worker_function_t * worker_out2in_cb;
257 /* Per thread data */
258 snat_main_per_thread_data_t * per_thread_data;
260 /* Find a static mapping by local */
261 clib_bihash_8_8_t static_mapping_by_local;
263 /* Find a static mapping by external */
264 clib_bihash_8_8_t static_mapping_by_external;
266 /* Static mapping pool */
267 snat_static_mapping_t * static_mappings;
270 snat_interface_t * interfaces;
272 /* Vector of outside addresses */
273 snat_address_t * addresses;
275 /* sw_if_indices whose intfc addresses should be auto-added */
276 u32 * auto_add_sw_if_indices;
278 /* vector of interface address static mappings to resolve. */
279 snat_static_map_resolve_t *to_resolve;
281 /* Randomize port allocation order */
284 /* Worker handoff index */
288 /* in2out and out2in node index */
289 u32 in2out_node_index;
290 u32 out2in_node_index;
292 /* Deterministic NAT */
293 snat_det_map_t * det_maps;
295 /* Config parameters */
296 u8 static_mapping_only;
297 u8 static_mapping_connection_tracking;
299 u32 translation_buckets;
300 u32 translation_memory_size;
302 u32 user_memory_size;
303 u32 max_translations_per_user;
305 u32 outside_fib_index;
307 u32 inside_fib_index;
309 /* tenant VRF aware address pool activation flag */
312 /* API message ID base */
316 vlib_main_t * vlib_main;
317 vnet_main_t * vnet_main;
318 ip4_main_t * ip4_main;
319 ip_lookup_main_t * ip4_lookup_main;
320 api_main_t * api_main;
323 extern snat_main_t snat_main;
324 extern vlib_node_registration_t snat_in2out_node;
325 extern vlib_node_registration_t snat_out2in_node;
326 extern vlib_node_registration_t snat_in2out_fast_node;
327 extern vlib_node_registration_t snat_out2in_fast_node;
328 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
329 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
330 extern vlib_node_registration_t snat_det_in2out_node;
331 extern vlib_node_registration_t snat_det_out2in_node;
333 void snat_free_outside_address_and_port (snat_main_t * sm,
334 snat_session_key_t * k,
337 int snat_alloc_outside_address_and_port (snat_main_t * sm,
339 snat_session_key_t * k,
340 u32 * address_indexp);
342 int snat_static_mapping_match (snat_main_t * sm,
343 snat_session_key_t match,
344 snat_session_key_t * mapping,
347 void snat_add_del_addr_to_fib (ip4_address_t * addr,
352 format_function_t format_snat_user;
355 u32 cached_sw_if_index;
356 u32 cached_ip4_address;
359 /** \brief Check if SNAT session is created from static mapping.
360 @param s SNAT session
361 @return 1 if SNAT session is created from static mapping otherwise 0
363 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
366 * Why is this here? Because we don't need to touch this layer to
367 * simply reply to an icmp. We need to change id to a unique
368 * value to NAT an echo request/reply.
374 } icmp_echo_header_t;
376 always_inline snat_protocol_t
377 ip_proto_to_snat_proto (u8 ip_proto)
379 snat_protocol_t snat_proto = ~0;
381 snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
382 snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
383 snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
389 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
393 ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
394 ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
395 ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
401 u16 src_port, dst_port;
404 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
405 u32 thread_index, vlib_buffer_t *b0,
406 snat_session_key_t *p_key,
407 snat_session_key_t *p_value,
408 u8 *p_dont_translate, void *d);
409 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
410 u32 thread_index, vlib_buffer_t *b0,
411 snat_session_key_t *p_key,
412 snat_session_key_t *p_value,
413 u8 *p_dont_translate, void *d);
414 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
415 u32 thread_index, vlib_buffer_t *b0,
416 snat_session_key_t *p_key,
417 snat_session_key_t *p_value,
418 u8 *p_dont_translate, void *d);
419 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
420 u32 thread_index, vlib_buffer_t *b0,
421 snat_session_key_t *p_key,
422 snat_session_key_t *p_value,
423 u8 *p_dont_translate, void *d);
425 static_always_inline u8
426 icmp_is_error_message (icmp46_header_t * icmp)
430 case ICMP4_destination_unreachable:
431 case ICMP4_time_exceeded:
432 case ICMP4_parameter_problem:
433 case ICMP4_source_quench:
435 case ICMP4_alternate_host_address:
441 #endif /* __included_snat_h__ */