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
35 #define SNAT_ICMP_TIMEOUT 60
57 ip4_address_t ext_host_addr;
91 #define foreach_snat_protocol \
92 _(UDP, 0, udp, "udp") \
93 _(TCP, 1, tcp, "tcp") \
94 _(ICMP, 2, icmp, "icmp")
97 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
103 #define foreach_snat_session_state \
104 _(0, UNKNOWN, "unknown") \
105 _(1, UDP_ACTIVE, "udp-active") \
106 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
107 _(3, TCP_ESTABLISHED, "tcp-established") \
108 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
109 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
110 _(6, TCP_LAST_ACK, "tcp-last-ack") \
111 _(7, ICMP_ACTIVE, "icmp-active")
114 #define _(v, N, s) SNAT_SESSION_##N = v,
115 foreach_snat_session_state
117 } snat_session_state_t;
120 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
122 typedef CLIB_PACKED(struct {
123 snat_session_key_t out2in; /* 0-15 */
125 snat_session_key_t in2out; /* 16-31 */
127 u32 flags; /* 32-35 */
129 /* per-user translations */
130 u32 per_user_index; /* 36-39 */
132 u32 per_user_list_head_index; /* 40-43 */
134 /* Last heard timer */
135 f64 last_heard; /* 44-51 */
137 u64 total_bytes; /* 52-59 */
139 u32 total_pkts; /* 60-63 */
141 /* Outside address */
142 u32 outside_address_index; /* 64-67 */
150 u32 sessions_per_user_list_head_index;
158 #define _(N, i, n, s) \
159 u32 busy_##n##_ports; \
160 uword * busy_##n##_port_bitmap;
161 foreach_snat_protocol
167 snat_det_out_key_t out;
170 } snat_det_session_t;
173 ip4_address_t in_addr;
175 ip4_address_t out_addr;
180 /* vector of sessions */
181 snat_det_session_t * sessions;
185 ip4_address_t local_addr;
186 ip4_address_t external_addr;
192 snat_protocol_t proto;
193 } snat_static_mapping_t;
201 ip4_address_t l_addr;
206 snat_protocol_t proto;
209 } snat_static_map_resolve_t;
216 snat_session_t * sessions;
218 /* Pool of doubly-linked list elements */
219 dlist_elt_t * list_pool;
220 } snat_main_per_thread_data_t;
224 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
225 vlib_node_runtime_t *node,
229 snat_session_key_t *p_value,
230 u8 *p_dont_translate,
234 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
236 typedef struct snat_main_s {
237 /* Main lookup tables */
238 clib_bihash_8_8_t out2in;
239 clib_bihash_8_8_t in2out;
241 /* Find-a-user => src address lookup */
242 clib_bihash_8_8_t user_hash;
244 /* Non-translated packets worker lookup => src address + VRF */
245 clib_bihash_8_8_t worker_by_in;
247 /* Translated packets worker lookup => IP address + port number */
248 clib_bihash_8_8_t worker_by_out;
250 snat_icmp_match_function_t * icmp_match_in2out_cb;
251 snat_icmp_match_function_t * icmp_match_out2in_cb;
254 u32 first_worker_index;
257 snat_get_worker_function_t * worker_in2out_cb;
258 snat_get_worker_function_t * worker_out2in_cb;
260 /* Per thread data */
261 snat_main_per_thread_data_t * per_thread_data;
263 /* Find a static mapping by local */
264 clib_bihash_8_8_t static_mapping_by_local;
266 /* Find a static mapping by external */
267 clib_bihash_8_8_t static_mapping_by_external;
269 /* Static mapping pool */
270 snat_static_mapping_t * static_mappings;
273 snat_interface_t * interfaces;
275 /* Vector of outside addresses */
276 snat_address_t * addresses;
278 /* sw_if_indices whose intfc addresses should be auto-added */
279 u32 * auto_add_sw_if_indices;
281 /* vector of interface address static mappings to resolve. */
282 snat_static_map_resolve_t *to_resolve;
284 /* Randomize port allocation order */
287 /* Worker handoff index */
291 /* in2out and out2in node index */
292 u32 in2out_node_index;
293 u32 out2in_node_index;
295 /* Deterministic NAT */
296 snat_det_map_t * det_maps;
298 /* Config parameters */
299 u8 static_mapping_only;
300 u8 static_mapping_connection_tracking;
302 u32 translation_buckets;
303 u32 translation_memory_size;
305 u32 user_memory_size;
306 u32 max_translations_per_user;
308 u32 outside_fib_index;
310 u32 inside_fib_index;
312 /* tenant VRF aware address pool activation flag */
315 /* values of various timeouts */
317 u32 tcp_established_timeout;
318 u32 tcp_transitory_timeout;
321 /* API message ID base */
325 vlib_main_t * vlib_main;
326 vnet_main_t * vnet_main;
327 ip4_main_t * ip4_main;
328 ip_lookup_main_t * ip4_lookup_main;
329 api_main_t * api_main;
332 extern snat_main_t snat_main;
333 extern vlib_node_registration_t snat_in2out_node;
334 extern vlib_node_registration_t snat_out2in_node;
335 extern vlib_node_registration_t snat_in2out_fast_node;
336 extern vlib_node_registration_t snat_out2in_fast_node;
337 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
338 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
339 extern vlib_node_registration_t snat_det_in2out_node;
340 extern vlib_node_registration_t snat_det_out2in_node;
342 void snat_free_outside_address_and_port (snat_main_t * sm,
343 snat_session_key_t * k,
346 int snat_alloc_outside_address_and_port (snat_main_t * sm,
348 snat_session_key_t * k,
349 u32 * address_indexp);
351 int snat_static_mapping_match (snat_main_t * sm,
352 snat_session_key_t match,
353 snat_session_key_t * mapping,
357 void snat_add_del_addr_to_fib (ip4_address_t * addr,
362 format_function_t format_snat_user;
365 u32 cached_sw_if_index;
366 u32 cached_ip4_address;
369 /** \brief Check if SNAT session is created from static mapping.
370 @param s SNAT session
371 @return 1 if SNAT session is created from static mapping otherwise 0
373 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
376 * Why is this here? Because we don't need to touch this layer to
377 * simply reply to an icmp. We need to change id to a unique
378 * value to NAT an echo request/reply.
384 } icmp_echo_header_t;
386 always_inline snat_protocol_t
387 ip_proto_to_snat_proto (u8 ip_proto)
389 snat_protocol_t snat_proto = ~0;
391 snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
392 snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
393 snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
399 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
403 ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
404 ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
405 ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
411 u16 src_port, dst_port;
414 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
415 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
416 snat_session_key_t *p_value,
417 u8 *p_dont_translate, void *d, void *e);
418 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
419 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
420 snat_session_key_t *p_value,
421 u8 *p_dont_translate, void *d, void *e);
422 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
423 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
424 snat_session_key_t *p_value,
425 u8 *p_dont_translate, void *d, void *e);
426 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
427 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
428 snat_session_key_t *p_value,
429 u8 *p_dont_translate, void *d, void *e);
430 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
431 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
432 snat_session_key_t *p_value,
433 u8 *p_dont_translate, void *d, void *e);
434 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
435 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
436 snat_session_key_t *p_value,
437 u8 *p_dont_translate, void *d, void *e);
439 static_always_inline u8
440 icmp_is_error_message (icmp46_header_t * icmp)
444 case ICMP4_destination_unreachable:
445 case ICMP4_time_exceeded:
446 case ICMP4_parameter_problem:
447 case ICMP4_source_quench:
449 case ICMP4_alternate_host_address:
455 static_always_inline u8
456 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0,
459 snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
460 ip4_address_t * first_int_addr;
462 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
465 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
466 0 /* just want the address */);
467 rt->cached_sw_if_index = sw_if_index0;
469 rt->cached_ip4_address = first_int_addr->as_u32;
471 rt->cached_ip4_address = 0;
474 if (PREDICT_FALSE(ip4_addr == rt->cached_ip4_address))
480 #endif /* __included_snat_h__ */