3 * nat.h - NAT plugin 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_nat_h__
19 #define __included_nat_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)
78 ip4_address_t ext_host_addr;
112 #define foreach_snat_protocol \
113 _(UDP, 0, udp, "udp") \
114 _(TCP, 1, tcp, "tcp") \
115 _(ICMP, 2, icmp, "icmp")
118 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
119 foreach_snat_protocol
124 #define foreach_snat_session_state \
125 _(0, UNKNOWN, "unknown") \
126 _(1, UDP_ACTIVE, "udp-active") \
127 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
128 _(3, TCP_ESTABLISHED, "tcp-established") \
129 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
130 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
131 _(6, TCP_LAST_ACK, "tcp-last-ack") \
132 _(7, ICMP_ACTIVE, "icmp-active")
135 #define _(v, N, s) SNAT_SESSION_##N = v,
136 foreach_snat_session_state
138 } snat_session_state_t;
141 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
142 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
143 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
145 typedef CLIB_PACKED(struct {
146 snat_session_key_t out2in; /* 0-15 */
148 snat_session_key_t in2out; /* 16-31 */
150 u32 flags; /* 32-35 */
152 /* per-user translations */
153 u32 per_user_index; /* 36-39 */
155 u32 per_user_list_head_index; /* 40-43 */
157 /* Last heard timer */
158 f64 last_heard; /* 44-51 */
160 u64 total_bytes; /* 52-59 */
162 u32 total_pkts; /* 60-63 */
164 /* Outside address */
165 u32 outside_address_index; /* 64-67 */
167 /* External host address */
168 ip4_address_t ext_host_addr; /* 68-71 */
176 u32 sessions_per_user_list_head_index;
184 #define _(N, i, n, s) \
185 u16 busy_##n##_ports; \
186 u16 * busy_##n##_ports_per_thread; \
187 uword * busy_##n##_port_bitmap;
188 foreach_snat_protocol
194 snat_det_out_key_t out;
197 } snat_det_session_t;
200 ip4_address_t in_addr;
202 ip4_address_t out_addr;
207 /* vector of sessions */
208 snat_det_session_t * sessions;
216 } nat44_lb_addr_port_t;
219 ip4_address_t local_addr;
220 ip4_address_t external_addr;
226 snat_protocol_t proto;
228 nat44_lb_addr_port_t *locals;
229 } snat_static_mapping_t;
237 ip4_address_t l_addr;
242 snat_protocol_t proto;
245 } snat_static_map_resolve_t;
248 /* Main lookup tables */
249 clib_bihash_8_8_t out2in;
250 clib_bihash_8_8_t in2out;
252 /* Find-a-user => src address lookup */
253 clib_bihash_8_8_t user_hash;
259 snat_session_t * sessions;
261 /* Pool of doubly-linked list elements */
262 dlist_elt_t * list_pool;
264 u32 snat_thread_index;
265 } snat_main_per_thread_data_t;
269 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
270 vlib_node_runtime_t *node,
274 snat_session_key_t *p_value,
275 u8 *p_dont_translate,
279 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
281 typedef struct snat_main_s {
282 /* Endpoint address dependent sessions lookup tables */
283 clib_bihash_16_8_t out2in_ed;
284 clib_bihash_16_8_t in2out_ed;
286 /* Non-translated packets worker lookup => src address + VRF */
287 clib_bihash_8_8_t worker_by_in;
289 snat_icmp_match_function_t * icmp_match_in2out_cb;
290 snat_icmp_match_function_t * icmp_match_out2in_cb;
293 u32 first_worker_index;
296 snat_get_worker_function_t * worker_in2out_cb;
297 snat_get_worker_function_t * worker_out2in_cb;
301 /* Per thread data */
302 snat_main_per_thread_data_t * per_thread_data;
304 /* Find a static mapping by local */
305 clib_bihash_8_8_t static_mapping_by_local;
307 /* Find a static mapping by external */
308 clib_bihash_8_8_t static_mapping_by_external;
310 /* Static mapping pool */
311 snat_static_mapping_t * static_mappings;
314 snat_interface_t * interfaces;
315 snat_interface_t * output_feature_interfaces;
317 /* Vector of outside addresses */
318 snat_address_t * addresses;
320 /* sw_if_indices whose intfc addresses should be auto-added */
321 u32 * auto_add_sw_if_indices;
323 /* vector of interface address static mappings to resolve. */
324 snat_static_map_resolve_t *to_resolve;
326 /* Randomize port allocation order */
329 /* Worker handoff index */
331 u32 fq_in2out_output_index;
334 /* in2out and out2in node index */
335 u32 in2out_node_index;
336 u32 in2out_output_node_index;
337 u32 out2in_node_index;
339 /* Deterministic NAT */
340 snat_det_map_t * det_maps;
342 /* Config parameters */
343 u8 static_mapping_only;
344 u8 static_mapping_connection_tracking;
346 u32 translation_buckets;
347 u32 translation_memory_size;
348 u32 max_translations;
350 u32 user_memory_size;
351 u32 max_translations_per_user;
353 u32 outside_fib_index;
355 u32 inside_fib_index;
357 /* tenant VRF aware address pool activation flag */
360 /* values of various timeouts */
362 u32 tcp_established_timeout;
363 u32 tcp_transitory_timeout;
366 /* API message ID base */
370 vlib_main_t * vlib_main;
371 vnet_main_t * vnet_main;
372 ip4_main_t * ip4_main;
373 ip_lookup_main_t * ip4_lookup_main;
374 api_main_t * api_main;
377 extern snat_main_t snat_main;
378 extern vlib_node_registration_t snat_in2out_node;
379 extern vlib_node_registration_t snat_in2out_output_node;
380 extern vlib_node_registration_t snat_out2in_node;
381 extern vlib_node_registration_t snat_in2out_fast_node;
382 extern vlib_node_registration_t snat_out2in_fast_node;
383 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
384 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
385 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
386 extern vlib_node_registration_t snat_det_in2out_node;
387 extern vlib_node_registration_t snat_det_out2in_node;
388 extern vlib_node_registration_t snat_hairpin_dst_node;
389 extern vlib_node_registration_t snat_hairpin_src_node;
391 void snat_free_outside_address_and_port (snat_main_t * sm,
393 snat_session_key_t * k,
396 int snat_alloc_outside_address_and_port (snat_main_t * sm,
399 snat_session_key_t * k,
400 u32 * address_indexp);
402 int snat_static_mapping_match (snat_main_t * sm,
403 snat_session_key_t match,
404 snat_session_key_t * mapping,
408 void snat_add_del_addr_to_fib (ip4_address_t * addr,
413 format_function_t format_snat_user;
416 u32 cached_sw_if_index;
417 u32 cached_ip4_address;
420 /** \brief Check if SNAT session is created from static mapping.
421 @param s SNAT session
422 @return 1 if SNAT session is created from static mapping otherwise 0
424 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
426 /** \brief Check if SNAT session for unknown protocol.
427 @param s SNAT session
428 @return 1 if SNAT session for unknown protocol otherwise 0
430 #define snat_is_unk_proto_session(s) s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO
433 * Why is this here? Because we don't need to touch this layer to
434 * simply reply to an icmp. We need to change id to a unique
435 * value to NAT an echo request/reply.
441 } icmp_echo_header_t;
444 ip_proto_to_snat_proto (u8 ip_proto)
448 snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
449 snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
450 snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
451 snat_proto = (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
457 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
461 ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
462 ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
463 ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
469 u16 src_port, dst_port;
472 u32 icmp_match_in2out_fast(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_in2out_slow(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 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
481 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
482 snat_session_key_t *p_value,
483 u8 *p_dont_translate, void *d, void *e);
484 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
485 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
486 snat_session_key_t *p_value,
487 u8 *p_dont_translate, void *d, void *e);
488 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
489 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
490 snat_session_key_t *p_value,
491 u8 *p_dont_translate, void *d, void *e);
492 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
493 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
494 snat_session_key_t *p_value,
495 u8 *p_dont_translate, void *d, void *e);
496 void increment_v4_address(ip4_address_t * a);
497 void snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id);
498 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm);
499 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
500 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
501 u32 sw_if_index, snat_protocol_t proto, int is_add);
502 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
503 int snat_set_workers (uword * bitmap);
504 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
505 int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside,
507 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del);
508 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
509 u8 * format_snat_protocol(u8 * s, va_list * args);
510 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
511 snat_protocol_t proto, u32 vrf_id,
512 nat44_lb_addr_port_t *locals, u8 is_add);
514 static_always_inline u8
515 icmp_is_error_message (icmp46_header_t * icmp)
519 case ICMP4_destination_unreachable:
520 case ICMP4_time_exceeded:
521 case ICMP4_parameter_problem:
522 case ICMP4_source_quench:
524 case ICMP4_alternate_host_address:
530 static_always_inline u8
531 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0,
534 snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
535 ip4_address_t * first_int_addr;
537 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
540 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
541 0 /* just want the address */);
542 rt->cached_sw_if_index = sw_if_index0;
544 rt->cached_ip4_address = first_int_addr->as_u32;
546 rt->cached_ip4_address = 0;
549 if (PREDICT_FALSE(ip4_addr == rt->cached_ip4_address))
556 maximum_sessions_exceeded (snat_main_t *sm, u32 thread_index)
558 if (pool_elts (sm->per_thread_data[thread_index].sessions) >= sm->max_translations)
564 #endif /* __included_nat_h__ */