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_UDP_TIMEOUT_MIN 120
34 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
35 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
36 #define SNAT_TCP_INCOMING_SYN 6
37 #define SNAT_ICMP_TIMEOUT 60
59 ip4_address_t ext_host_addr;
93 #define foreach_snat_protocol \
94 _(UDP, 0, udp, "udp") \
95 _(TCP, 1, tcp, "tcp") \
96 _(ICMP, 2, icmp, "icmp")
99 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
100 foreach_snat_protocol
105 #define foreach_snat_session_state \
106 _(0, UNKNOWN, "unknown") \
107 _(1, UDP_ACTIVE, "udp-active") \
108 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
109 _(3, TCP_ESTABLISHED, "tcp-established") \
110 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
111 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
112 _(6, TCP_LAST_ACK, "tcp-last-ack") \
113 _(7, ICMP_ACTIVE, "icmp-active")
116 #define _(v, N, s) SNAT_SESSION_##N = v,
117 foreach_snat_session_state
119 } snat_session_state_t;
122 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
124 typedef CLIB_PACKED(struct {
125 snat_session_key_t out2in; /* 0-15 */
127 snat_session_key_t in2out; /* 16-31 */
129 u32 flags; /* 32-35 */
131 /* per-user translations */
132 u32 per_user_index; /* 36-39 */
134 u32 per_user_list_head_index; /* 40-43 */
136 /* Last heard timer */
137 f64 last_heard; /* 44-51 */
139 u64 total_bytes; /* 52-59 */
141 u32 total_pkts; /* 60-63 */
143 /* Outside address */
144 u32 outside_address_index; /* 64-67 */
152 u32 sessions_per_user_list_head_index;
160 #define _(N, i, n, s) \
161 u32 busy_##n##_ports; \
162 uword * busy_##n##_port_bitmap;
163 foreach_snat_protocol
169 snat_det_out_key_t out;
172 } snat_det_session_t;
175 ip4_address_t in_addr;
177 ip4_address_t out_addr;
182 /* vector of sessions */
183 snat_det_session_t * sessions;
187 ip4_address_t local_addr;
188 ip4_address_t external_addr;
194 snat_protocol_t proto;
195 } snat_static_mapping_t;
203 ip4_address_t l_addr;
208 snat_protocol_t proto;
211 } snat_static_map_resolve_t;
218 snat_session_t * sessions;
220 /* Pool of doubly-linked list elements */
221 dlist_elt_t * list_pool;
222 } snat_main_per_thread_data_t;
226 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
227 vlib_node_runtime_t *node,
231 snat_session_key_t *p_value,
232 u8 *p_dont_translate,
236 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
238 typedef struct snat_main_s {
239 /* Main lookup tables */
240 clib_bihash_8_8_t out2in;
241 clib_bihash_8_8_t in2out;
243 /* Find-a-user => src address lookup */
244 clib_bihash_8_8_t user_hash;
246 /* Non-translated packets worker lookup => src address + VRF */
247 clib_bihash_8_8_t worker_by_in;
249 /* Translated packets worker lookup => IP address + port number */
250 clib_bihash_8_8_t worker_by_out;
252 snat_icmp_match_function_t * icmp_match_in2out_cb;
253 snat_icmp_match_function_t * icmp_match_out2in_cb;
256 u32 first_worker_index;
259 snat_get_worker_function_t * worker_in2out_cb;
260 snat_get_worker_function_t * worker_out2in_cb;
262 /* Per thread data */
263 snat_main_per_thread_data_t * per_thread_data;
265 /* Find a static mapping by local */
266 clib_bihash_8_8_t static_mapping_by_local;
268 /* Find a static mapping by external */
269 clib_bihash_8_8_t static_mapping_by_external;
271 /* Static mapping pool */
272 snat_static_mapping_t * static_mappings;
275 snat_interface_t * interfaces;
277 /* Vector of outside addresses */
278 snat_address_t * addresses;
280 /* sw_if_indices whose intfc addresses should be auto-added */
281 u32 * auto_add_sw_if_indices;
283 /* vector of interface address static mappings to resolve. */
284 snat_static_map_resolve_t *to_resolve;
286 /* Randomize port allocation order */
289 /* Worker handoff index */
293 /* in2out and out2in node index */
294 u32 in2out_node_index;
295 u32 out2in_node_index;
297 /* Deterministic NAT */
298 snat_det_map_t * det_maps;
300 /* Config parameters */
301 u8 static_mapping_only;
302 u8 static_mapping_connection_tracking;
304 u32 translation_buckets;
305 u32 translation_memory_size;
307 u32 user_memory_size;
308 u32 max_translations_per_user;
310 u32 outside_fib_index;
312 u32 inside_fib_index;
314 /* tenant VRF aware address pool activation flag */
317 /* values of various timeouts */
319 u32 tcp_established_timeout;
320 u32 tcp_transitory_timeout;
323 /* API message ID base */
327 vlib_main_t * vlib_main;
328 vnet_main_t * vnet_main;
329 ip4_main_t * ip4_main;
330 ip_lookup_main_t * ip4_lookup_main;
331 api_main_t * api_main;
334 extern snat_main_t snat_main;
335 extern vlib_node_registration_t snat_in2out_node;
336 extern vlib_node_registration_t snat_out2in_node;
337 extern vlib_node_registration_t snat_in2out_fast_node;
338 extern vlib_node_registration_t snat_out2in_fast_node;
339 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
340 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
341 extern vlib_node_registration_t snat_det_in2out_node;
342 extern vlib_node_registration_t snat_det_out2in_node;
344 void snat_free_outside_address_and_port (snat_main_t * sm,
345 snat_session_key_t * k,
348 int snat_alloc_outside_address_and_port (snat_main_t * sm,
350 snat_session_key_t * k,
351 u32 * address_indexp);
353 int snat_static_mapping_match (snat_main_t * sm,
354 snat_session_key_t match,
355 snat_session_key_t * mapping,
359 void snat_add_del_addr_to_fib (ip4_address_t * addr,
364 format_function_t format_snat_user;
367 u32 cached_sw_if_index;
368 u32 cached_ip4_address;
371 /** \brief Check if SNAT session is created from static mapping.
372 @param s SNAT session
373 @return 1 if SNAT session is created from static mapping otherwise 0
375 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
378 * Why is this here? Because we don't need to touch this layer to
379 * simply reply to an icmp. We need to change id to a unique
380 * value to NAT an echo request/reply.
386 } icmp_echo_header_t;
389 ip_proto_to_snat_proto (u8 ip_proto)
393 snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
394 snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
395 snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
396 snat_proto = (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
402 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
406 ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
407 ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
408 ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
414 u16 src_port, dst_port;
417 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
418 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
419 snat_session_key_t *p_value,
420 u8 *p_dont_translate, void *d, void *e);
421 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
422 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
423 snat_session_key_t *p_value,
424 u8 *p_dont_translate, void *d, void *e);
425 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
426 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
427 snat_session_key_t *p_value,
428 u8 *p_dont_translate, void *d, void *e);
429 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
430 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
431 snat_session_key_t *p_value,
432 u8 *p_dont_translate, void *d, void *e);
433 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
434 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
435 snat_session_key_t *p_value,
436 u8 *p_dont_translate, void *d, void *e);
437 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
438 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
439 snat_session_key_t *p_value,
440 u8 *p_dont_translate, void *d, void *e);
441 void increment_v4_address(ip4_address_t * a);
442 void snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id);
443 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm);
444 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
445 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
446 u32 sw_if_index, snat_protocol_t proto, int is_add);
447 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
448 int snat_set_workers (uword * bitmap);
449 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
450 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del);
451 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
452 u8 * format_snat_protocol(u8 * s, va_list * args);
454 static_always_inline u8
455 icmp_is_error_message (icmp46_header_t * icmp)
459 case ICMP4_destination_unreachable:
460 case ICMP4_time_exceeded:
461 case ICMP4_parameter_problem:
462 case ICMP4_source_quench:
464 case ICMP4_alternate_host_address:
470 static_always_inline u8
471 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0,
474 snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
475 ip4_address_t * first_int_addr;
477 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
480 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
481 0 /* just want the address */);
482 rt->cached_sw_if_index = sw_if_index0;
484 rt->cached_ip4_address = first_int_addr->as_u32;
486 rt->cached_ip4_address = 0;
489 if (PREDICT_FALSE(ip4_addr == rt->cached_ip4_address))
495 #endif /* __included_snat_h__ */