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/bihash_16_8.h>
28 #include <vppinfra/dlist.h>
29 #include <vppinfra/error.h>
30 #include <vlibapi/api.h>
33 #define SNAT_UDP_TIMEOUT 300
34 #define SNAT_UDP_TIMEOUT_MIN 120
35 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
36 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
37 #define SNAT_TCP_INCOMING_SYN 6
38 #define SNAT_ICMP_TIMEOUT 60
40 #define SNAT_FLAG_HAIRPINNING (1 << 0)
70 } snat_unk_proto_ses_key_t;
77 ip4_address_t ext_host_addr;
111 #define foreach_snat_protocol \
112 _(UDP, 0, udp, "udp") \
113 _(TCP, 1, tcp, "tcp") \
114 _(ICMP, 2, icmp, "icmp")
117 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
118 foreach_snat_protocol
123 #define foreach_snat_session_state \
124 _(0, UNKNOWN, "unknown") \
125 _(1, UDP_ACTIVE, "udp-active") \
126 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
127 _(3, TCP_ESTABLISHED, "tcp-established") \
128 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
129 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
130 _(6, TCP_LAST_ACK, "tcp-last-ack") \
131 _(7, ICMP_ACTIVE, "icmp-active")
134 #define _(v, N, s) SNAT_SESSION_##N = v,
135 foreach_snat_session_state
137 } snat_session_state_t;
140 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
141 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
143 typedef CLIB_PACKED(struct {
144 snat_session_key_t out2in; /* 0-15 */
146 snat_session_key_t in2out; /* 16-31 */
148 u32 flags; /* 32-35 */
150 /* per-user translations */
151 u32 per_user_index; /* 36-39 */
153 u32 per_user_list_head_index; /* 40-43 */
155 /* Last heard timer */
156 f64 last_heard; /* 44-51 */
158 u64 total_bytes; /* 52-59 */
160 u32 total_pkts; /* 60-63 */
162 /* Outside address */
163 u32 outside_address_index; /* 64-67 */
165 /* External host address */
166 ip4_address_t ext_host_addr; /* 68-71 */
174 u32 sessions_per_user_list_head_index;
182 #define _(N, i, n, s) \
183 u32 busy_##n##_ports; \
184 uword * busy_##n##_port_bitmap;
185 foreach_snat_protocol
191 snat_det_out_key_t out;
194 } snat_det_session_t;
197 ip4_address_t in_addr;
199 ip4_address_t out_addr;
204 /* vector of sessions */
205 snat_det_session_t * sessions;
209 ip4_address_t local_addr;
210 ip4_address_t external_addr;
216 snat_protocol_t proto;
217 } snat_static_mapping_t;
225 ip4_address_t l_addr;
230 snat_protocol_t proto;
233 } snat_static_map_resolve_t;
240 snat_session_t * sessions;
242 /* Pool of doubly-linked list elements */
243 dlist_elt_t * list_pool;
244 } snat_main_per_thread_data_t;
248 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
249 vlib_node_runtime_t *node,
253 snat_session_key_t *p_value,
254 u8 *p_dont_translate,
258 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
260 typedef struct snat_main_s {
261 /* Main lookup tables */
262 clib_bihash_8_8_t out2in;
263 clib_bihash_8_8_t in2out;
265 /* Unknown protocol sessions lookup tables */
266 clib_bihash_16_8_t out2in_unk_proto;
267 clib_bihash_16_8_t in2out_unk_proto;
269 /* Find-a-user => src address lookup */
270 clib_bihash_8_8_t user_hash;
272 /* Non-translated packets worker lookup => src address + VRF */
273 clib_bihash_8_8_t worker_by_in;
275 /* Translated packets worker lookup => IP address + port number */
276 clib_bihash_8_8_t worker_by_out;
278 snat_icmp_match_function_t * icmp_match_in2out_cb;
279 snat_icmp_match_function_t * icmp_match_out2in_cb;
282 u32 first_worker_index;
285 snat_get_worker_function_t * worker_in2out_cb;
286 snat_get_worker_function_t * worker_out2in_cb;
288 /* Per thread data */
289 snat_main_per_thread_data_t * per_thread_data;
291 /* Find a static mapping by local */
292 clib_bihash_8_8_t static_mapping_by_local;
294 /* Find a static mapping by external */
295 clib_bihash_8_8_t static_mapping_by_external;
297 /* Static mapping pool */
298 snat_static_mapping_t * static_mappings;
301 snat_interface_t * interfaces;
302 snat_interface_t * output_feature_interfaces;
304 /* Vector of outside addresses */
305 snat_address_t * addresses;
307 /* sw_if_indices whose intfc addresses should be auto-added */
308 u32 * auto_add_sw_if_indices;
310 /* vector of interface address static mappings to resolve. */
311 snat_static_map_resolve_t *to_resolve;
313 /* Randomize port allocation order */
316 /* Worker handoff index */
318 u32 fq_in2out_output_index;
321 /* in2out and out2in node index */
322 u32 in2out_node_index;
323 u32 in2out_output_node_index;
324 u32 out2in_node_index;
326 /* Deterministic NAT */
327 snat_det_map_t * det_maps;
329 /* Config parameters */
330 u8 static_mapping_only;
331 u8 static_mapping_connection_tracking;
333 u32 translation_buckets;
334 u32 translation_memory_size;
336 u32 user_memory_size;
337 u32 max_translations_per_user;
339 u32 outside_fib_index;
341 u32 inside_fib_index;
343 /* tenant VRF aware address pool activation flag */
346 /* values of various timeouts */
348 u32 tcp_established_timeout;
349 u32 tcp_transitory_timeout;
352 /* API message ID base */
356 vlib_main_t * vlib_main;
357 vnet_main_t * vnet_main;
358 ip4_main_t * ip4_main;
359 ip_lookup_main_t * ip4_lookup_main;
360 api_main_t * api_main;
363 extern snat_main_t snat_main;
364 extern vlib_node_registration_t snat_in2out_node;
365 extern vlib_node_registration_t snat_in2out_output_node;
366 extern vlib_node_registration_t snat_out2in_node;
367 extern vlib_node_registration_t snat_in2out_fast_node;
368 extern vlib_node_registration_t snat_out2in_fast_node;
369 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
370 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
371 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
372 extern vlib_node_registration_t snat_det_in2out_node;
373 extern vlib_node_registration_t snat_det_out2in_node;
374 extern vlib_node_registration_t snat_hairpin_dst_node;
375 extern vlib_node_registration_t snat_hairpin_src_node;
377 void snat_free_outside_address_and_port (snat_main_t * sm,
378 snat_session_key_t * k,
381 int snat_alloc_outside_address_and_port (snat_main_t * sm,
383 snat_session_key_t * k,
384 u32 * address_indexp);
386 int snat_static_mapping_match (snat_main_t * sm,
387 snat_session_key_t match,
388 snat_session_key_t * mapping,
392 void snat_add_del_addr_to_fib (ip4_address_t * addr,
397 format_function_t format_snat_user;
400 u32 cached_sw_if_index;
401 u32 cached_ip4_address;
404 /** \brief Check if SNAT session is created from static mapping.
405 @param s SNAT session
406 @return 1 if SNAT session is created from static mapping otherwise 0
408 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
410 /** \brief Check if SNAT session for unknown protocol.
411 @param s SNAT session
412 @return 1 if SNAT session for unknown protocol otherwise 0
414 #define snat_is_unk_proto_session(s) s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO
417 * Why is this here? Because we don't need to touch this layer to
418 * simply reply to an icmp. We need to change id to a unique
419 * value to NAT an echo request/reply.
425 } icmp_echo_header_t;
428 ip_proto_to_snat_proto (u8 ip_proto)
432 snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
433 snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
434 snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
435 snat_proto = (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
441 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
445 ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
446 ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
447 ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
453 u16 src_port, dst_port;
456 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
457 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
458 snat_session_key_t *p_value,
459 u8 *p_dont_translate, void *d, void *e);
460 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
461 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
462 snat_session_key_t *p_value,
463 u8 *p_dont_translate, void *d, void *e);
464 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
465 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
466 snat_session_key_t *p_value,
467 u8 *p_dont_translate, void *d, void *e);
468 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
469 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
470 snat_session_key_t *p_value,
471 u8 *p_dont_translate, void *d, void *e);
472 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
473 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
474 snat_session_key_t *p_value,
475 u8 *p_dont_translate, void *d, void *e);
476 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
477 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
478 snat_session_key_t *p_value,
479 u8 *p_dont_translate, void *d, void *e);
480 void increment_v4_address(ip4_address_t * a);
481 void snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id);
482 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm);
483 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
484 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
485 u32 sw_if_index, snat_protocol_t proto, int is_add);
486 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
487 int snat_set_workers (uword * bitmap);
488 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
489 int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside,
491 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del);
492 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
493 u8 * format_snat_protocol(u8 * s, va_list * args);
495 static_always_inline u8
496 icmp_is_error_message (icmp46_header_t * icmp)
500 case ICMP4_destination_unreachable:
501 case ICMP4_time_exceeded:
502 case ICMP4_parameter_problem:
503 case ICMP4_source_quench:
505 case ICMP4_alternate_host_address:
511 static_always_inline u8
512 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0,
515 snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
516 ip4_address_t * first_int_addr;
518 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
521 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
522 0 /* just want the address */);
523 rt->cached_sw_if_index = sw_if_index0;
525 rt->cached_ip4_address = first_int_addr->as_u32;
527 rt->cached_ip4_address = 0;
530 if (PREDICT_FALSE(ip4_addr == rt->cached_ip4_address))
536 #endif /* __included_snat_h__ */