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;
99 #define foreach_snat_protocol \
100 _(UDP, 0, udp, "udp") \
101 _(TCP, 1, tcp, "tcp") \
102 _(ICMP, 2, icmp, "icmp")
105 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
106 foreach_snat_protocol
111 #define foreach_snat_session_state \
112 _(0, UNKNOWN, "unknown") \
113 _(1, UDP_ACTIVE, "udp-active") \
114 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
115 _(3, TCP_ESTABLISHED, "tcp-established") \
116 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
117 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
118 _(6, TCP_LAST_ACK, "tcp-last-ack") \
119 _(7, ICMP_ACTIVE, "icmp-active")
122 #define _(v, N, s) SNAT_SESSION_##N = v,
123 foreach_snat_session_state
125 } snat_session_state_t;
128 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
129 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
130 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
131 #define SNAT_SESSION_FLAG_TWICE_NAT 8
133 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
134 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
136 typedef CLIB_PACKED(struct {
137 snat_session_key_t out2in; /* 0-15 */
139 snat_session_key_t in2out; /* 16-31 */
141 u32 flags; /* 32-35 */
143 /* per-user translations */
144 u32 per_user_index; /* 36-39 */
146 u32 per_user_list_head_index; /* 40-43 */
148 /* Last heard timer */
149 f64 last_heard; /* 44-51 */
151 u64 total_bytes; /* 52-59 */
153 u32 total_pkts; /* 60-63 */
155 /* Outside address */
156 u32 outside_address_index; /* 64-67 */
158 /* External host address and port */
159 ip4_address_t ext_host_addr; /* 68-71 */
160 u16 ext_host_port; /* 72-73 */
162 /* External hos address and port after translation */
163 ip4_address_t ext_host_nat_addr; /* 74-77 */
164 u16 ext_host_nat_port; /* 78-79 */
171 u32 sessions_per_user_list_head_index;
179 #define _(N, i, n, s) \
180 u16 busy_##n##_ports; \
181 u16 * busy_##n##_ports_per_thread; \
182 uword * busy_##n##_port_bitmap;
183 foreach_snat_protocol
189 snat_det_out_key_t out;
192 } snat_det_session_t;
195 ip4_address_t in_addr;
197 ip4_address_t out_addr;
202 /* vector of sessions */
203 snat_det_session_t * sessions;
211 } nat44_lb_addr_port_t;
214 ip4_address_t local_addr;
215 ip4_address_t external_addr;
223 snat_protocol_t proto;
226 nat44_lb_addr_port_t *locals;
227 } snat_static_mapping_t;
235 ip4_address_t l_addr;
240 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,
275 snat_session_key_t *p_value,
276 u8 *p_dont_translate,
280 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
282 typedef int nat_alloc_out_addr_and_port_function_t (snat_address_t * addresses,
285 snat_session_key_t * k,
286 u32 * address_indexp,
288 u32 snat_thread_index);
290 typedef struct snat_main_s {
291 /* Endpoint address dependent sessions lookup tables */
292 clib_bihash_16_8_t out2in_ed;
293 clib_bihash_16_8_t in2out_ed;
295 snat_icmp_match_function_t * icmp_match_in2out_cb;
296 snat_icmp_match_function_t * icmp_match_out2in_cb;
299 u32 first_worker_index;
302 snat_get_worker_function_t * worker_in2out_cb;
303 snat_get_worker_function_t * worker_out2in_cb;
307 /* Per thread data */
308 snat_main_per_thread_data_t * per_thread_data;
310 /* Find a static mapping by local */
311 clib_bihash_8_8_t static_mapping_by_local;
313 /* Find a static mapping by external */
314 clib_bihash_8_8_t static_mapping_by_external;
316 /* Static mapping pool */
317 snat_static_mapping_t * static_mappings;
320 snat_interface_t * interfaces;
321 snat_interface_t * output_feature_interfaces;
323 /* Vector of outside addresses */
324 snat_address_t * addresses;
325 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
330 /* Vector of twice NAT addresses for extenal hosts */
331 snat_address_t * twice_nat_addresses;
333 /* sw_if_indices whose intfc addresses should be auto-added */
334 u32 * auto_add_sw_if_indices;
335 u32 * auto_add_sw_if_indices_twice_nat;
337 /* vector of interface address static mappings to resolve. */
338 snat_static_map_resolve_t *to_resolve;
340 /* Randomize port allocation order */
343 /* Worker handoff index */
345 u32 fq_in2out_output_index;
348 /* in2out and out2in node index */
349 u32 in2out_node_index;
350 u32 in2out_output_node_index;
351 u32 out2in_node_index;
353 /* Deterministic NAT */
354 snat_det_map_t * det_maps;
356 /* If forwarding is enabled */
357 u8 forwarding_enabled;
359 /* Config parameters */
360 u8 static_mapping_only;
361 u8 static_mapping_connection_tracking;
364 u32 translation_buckets;
365 u32 translation_memory_size;
366 u32 max_translations;
368 u32 user_memory_size;
369 u32 max_translations_per_user;
371 u32 outside_fib_index;
373 u32 inside_fib_index;
375 /* values of various timeouts */
377 u32 tcp_established_timeout;
378 u32 tcp_transitory_timeout;
381 /* API message ID base */
385 vlib_main_t * vlib_main;
386 vnet_main_t * vnet_main;
387 ip4_main_t * ip4_main;
388 ip_lookup_main_t * ip4_lookup_main;
389 api_main_t * api_main;
392 extern snat_main_t snat_main;
393 extern vlib_node_registration_t snat_in2out_node;
394 extern vlib_node_registration_t snat_in2out_output_node;
395 extern vlib_node_registration_t snat_out2in_node;
396 extern vlib_node_registration_t snat_in2out_fast_node;
397 extern vlib_node_registration_t snat_out2in_fast_node;
398 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
399 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
400 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
401 extern vlib_node_registration_t snat_det_in2out_node;
402 extern vlib_node_registration_t snat_det_out2in_node;
403 extern vlib_node_registration_t snat_hairpin_dst_node;
404 extern vlib_node_registration_t snat_hairpin_src_node;
406 void snat_free_outside_address_and_port (snat_address_t * addresses,
408 snat_session_key_t * k,
411 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
414 snat_session_key_t * k,
415 u32 * address_indexp,
417 u32 snat_thread_index);
419 int snat_static_mapping_match (snat_main_t * sm,
420 snat_session_key_t match,
421 snat_session_key_t * mapping,
426 void snat_add_del_addr_to_fib (ip4_address_t * addr,
431 format_function_t format_snat_user;
432 format_function_t format_snat_static_mapping;
433 format_function_t format_snat_static_map_to_resolve;
434 format_function_t format_det_map_ses;
437 u32 cached_sw_if_index;
438 u32 cached_ip4_address;
441 /** \brief Check if SNAT session is created from static mapping.
442 @param s SNAT session
443 @return 1 if SNAT session is created from static mapping otherwise 0
445 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
447 /** \brief Check if SNAT session for unknown protocol.
448 @param s SNAT session
449 @return 1 if SNAT session for unknown protocol otherwise 0
451 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
453 /** \brief Check if NAT session is twice NAT.
455 @return 1 if NAT session is twice NAT
457 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
459 /** \brief Check if NAT session is load-balancing.
461 @return 1 if NAT session is load-balancing
463 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
465 /** \brief Check if NAT session is endpoint dependent.
467 @return 1 if NAT session is endpoint dependent
469 #define is_ed_session(s) (snat_is_unk_proto_session (s) || is_twice_nat_session (s) || is_lb_session (s))
471 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
472 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
475 * Why is this here? Because we don't need to touch this layer to
476 * simply reply to an icmp. We need to change id to a unique
477 * value to NAT an echo request/reply.
483 } icmp_echo_header_t;
486 ip_proto_to_snat_proto (u8 ip_proto)
490 snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
491 snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
492 snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
493 snat_proto = (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
499 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
503 ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
504 ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
505 ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
511 u16 src_port, dst_port;
514 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
515 u32 thread_index, vlib_buffer_t *b0,
516 ip4_header_t *ip0, u8 *p_proto,
517 snat_session_key_t *p_value,
518 u8 *p_dont_translate, void *d, void *e);
519 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
520 u32 thread_index, vlib_buffer_t *b0,
521 ip4_header_t *ip0, u8 *p_proto,
522 snat_session_key_t *p_value,
523 u8 *p_dont_translate, void *d, void *e);
524 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
525 u32 thread_index, vlib_buffer_t *b0,
526 ip4_header_t *ip0, u8 *p_proto,
527 snat_session_key_t *p_value,
528 u8 *p_dont_translate, void *d, void *e);
529 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
530 u32 thread_index, vlib_buffer_t *b0,
531 ip4_header_t *ip0, u8 *p_proto,
532 snat_session_key_t *p_value,
533 u8 *p_dont_translate, void *d, void *e);
534 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
535 u32 thread_index, vlib_buffer_t *b0,
536 ip4_header_t *ip0, u8 *p_proto,
537 snat_session_key_t *p_value,
538 u8 *p_dont_translate, void *d, void *e);
539 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
540 u32 thread_index, vlib_buffer_t *b0,
541 ip4_header_t *ip0, u8 *p_proto,
542 snat_session_key_t *p_value,
543 u8 *p_dont_translate, void *d, void *e);
544 void increment_v4_address(ip4_address_t * a);
545 void snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
547 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
549 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
550 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
551 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
552 u32 sw_if_index, snat_protocol_t proto, int is_add,
553 u8 twice_nat, u8 out2in_only, u8 *tag);
554 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
555 int snat_set_workers (uword * bitmap);
556 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
557 int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside,
559 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del,
561 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
562 u8 * format_snat_protocol(u8 * s, va_list * args);
563 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
564 snat_protocol_t proto, u32 vrf_id,
565 nat44_lb_addr_port_t *locals, u8 is_add,
566 u8 twice_nat, u8 out2in_only, u8 *tag);
567 int nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
568 snat_protocol_t proto, u32 vrf_id, int is_in);
569 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
571 snat_user_t * nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr,
572 u32 fib_index, u32 thread_index);
573 snat_session_t * nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u,
575 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
577 void nat_set_alloc_addr_and_port_default (void);
579 static_always_inline u8
580 icmp_is_error_message (icmp46_header_t * icmp)
584 case ICMP4_destination_unreachable:
585 case ICMP4_time_exceeded:
586 case ICMP4_parameter_problem:
587 case ICMP4_source_quench:
589 case ICMP4_alternate_host_address:
595 static_always_inline u8
596 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0,
599 snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
600 ip4_address_t * first_int_addr;
602 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
605 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
606 0 /* just want the address */);
607 rt->cached_sw_if_index = sw_if_index0;
609 rt->cached_ip4_address = first_int_addr->as_u32;
611 rt->cached_ip4_address = 0;
614 if (PREDICT_FALSE(ip4_addr == rt->cached_ip4_address))
621 maximum_sessions_exceeded (snat_main_t *sm, u32 thread_index)
623 if (pool_elts (sm->per_thread_data[thread_index].sessions) >= sm->max_translations)
629 static_always_inline void
630 nat_send_all_to_node(vlib_main_t *vm, u32 *bi_vector,
631 vlib_node_runtime_t *node, vlib_error_t *error, u32 next)
633 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
636 n_left_from = vec_len(bi_vector);
637 next_index = node->cached_next_index;
638 while (n_left_from > 0) {
639 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
640 while (n_left_from > 0 && n_left_to_next > 0) {
641 u32 bi0 = to_next[0] = from[0];
646 vlib_buffer_t *p0 = vlib_get_buffer(vm, bi0);
648 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
649 n_left_to_next, bi0, next);
651 vlib_put_next_frame(vm, node, next_index, n_left_to_next);
655 #endif /* __included_snat_h__ */