2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * NAT plugin global declarations
19 #ifndef __included_nat_h__
20 #define __included_nat_h__
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/ip/icmp46_packet.h>
26 #include <vnet/api_errno.h>
27 #include <vnet/fib/fib_source.h>
28 #include <vppinfra/elog.h>
29 #include <vppinfra/bihash_8_8.h>
30 #include <vppinfra/bihash_16_8.h>
31 #include <vppinfra/dlist.h>
32 #include <vppinfra/error.h>
33 #include <vlibapi/api.h>
35 #include <vppinfra/bihash_16_8.h>
36 #include <nat/lib/lib.h>
37 #include <nat/lib/inlines.h>
39 /* default session timeouts */
40 #define SNAT_UDP_TIMEOUT 300
41 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
42 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
43 #define SNAT_ICMP_TIMEOUT 60
45 /* number of worker handoff frame queue elements */
46 #define NAT_FQ_NELTS 64
48 /* NAT buffer flags */
49 #define SNAT_FLAG_HAIRPINNING (1 << 0)
51 /* NAT44 API Configuration flags */
52 #define foreach_nat44_config_flag \
53 _(0x00, IS_ENDPOINT_INDEPENDENT) \
54 _(0x01, IS_ENDPOINT_DEPENDENT) \
55 _(0x02, IS_STATIC_MAPPING_ONLY) \
56 _(0x04, IS_CONNECTION_TRACKING) \
57 _(0x08, IS_OUT2IN_DPO)
59 typedef enum nat44_config_flags_t_
61 #define _(n,f) NAT44_API_##f = n,
62 foreach_nat44_config_flag
64 } nat44_config_flags_t;
68 /* nat44 plugin features */
69 u8 static_mapping_only;
70 u8 connection_tracking;
71 u8 endpoint_dependent;
77 /* maximum number of users */
80 /* maximum number of sessions */
83 /* maximum number of ssessions per user */
91 NAT_NEXT_IN2OUT_ED_FAST_PATH,
92 NAT_NEXT_IN2OUT_ED_SLOW_PATH,
93 NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH,
94 NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
95 NAT_NEXT_OUT2IN_ED_FAST_PATH,
96 NAT_NEXT_OUT2IN_ED_SLOW_PATH,
97 NAT_NEXT_IN2OUT_CLASSIFY,
98 NAT_NEXT_OUT2IN_CLASSIFY,
108 /* user (internal host) key */
122 /* External address and port allocation modes */
123 #define foreach_nat_addr_and_port_alloc_alg \
124 _(0, DEFAULT, "default") \
125 _(1, MAPE, "map-e") \
126 _(2, RANGE, "port-range")
130 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
131 foreach_nat_addr_and_port_alloc_alg
133 } nat_addr_and_port_alloc_alg_t;
136 #define foreach_snat_session_state \
137 _(0, UNKNOWN, "unknown") \
138 _(1, UDP_ACTIVE, "udp-active") \
139 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
140 _(3, TCP_ESTABLISHED, "tcp-established") \
141 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
142 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
143 _(6, TCP_CLOSING, "tcp-closing") \
144 _(7, TCP_LAST_ACK, "tcp-last-ack") \
145 _(8, TCP_CLOSED, "tcp-closed") \
146 _(9, ICMP_ACTIVE, "icmp-active")
150 #define _(v, N, s) SNAT_SESSION_##N = v,
151 foreach_snat_session_state
153 } snat_session_state_t;
155 #define foreach_nat_in2out_ed_error \
156 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
157 _(OUT_OF_PORTS, "out of ports") \
158 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
159 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
160 _(NON_SYN, "non-SYN packet try to create session") \
161 _(TCP_CLOSED, "drops due to TCP in transitory timeout")
165 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
166 foreach_nat_in2out_ed_error
168 NAT_IN2OUT_ED_N_ERROR,
169 } nat_in2out_ed_error_t;
171 #define foreach_nat_out2in_ed_error \
172 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
173 _(OUT_OF_PORTS, "out of ports") \
174 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
175 _(NO_TRANSLATION, "no translation") \
176 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
177 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
178 _(CANNOT_CREATE_USER, "cannot create NAT user") \
179 _(NON_SYN, "non-SYN packet try to create session") \
180 _(TCP_CLOSED, "drops due to TCP in transitory timeout")
184 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
185 foreach_nat_out2in_ed_error
187 NAT_OUT2IN_ED_N_ERROR,
188 } nat_out2in_ed_error_t;
191 /* Endpoint dependent TCP session state */
192 #define NAT44_SES_I2O_FIN 1
193 #define NAT44_SES_O2I_FIN 2
194 #define NAT44_SES_I2O_FIN_ACK 4
195 #define NAT44_SES_O2I_FIN_ACK 8
196 #define NAT44_SES_I2O_SYN 16
197 #define NAT44_SES_O2I_SYN 32
198 #define NAT44_SES_RST 64
201 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
202 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
203 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
204 #define SNAT_SESSION_FLAG_TWICE_NAT 8
205 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
206 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
207 #define SNAT_SESSION_FLAG_AFFINITY 64
208 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
209 #define SNAT_SESSION_FLAG_EXACT_ADDRESS 256
211 /* NAT interface flags */
212 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
213 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
215 /* Static mapping flags */
216 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
217 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
218 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
219 #define NAT_STATIC_MAPPING_FLAG_LB 8
220 #define NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS 16
223 typedef CLIB_PACKED(struct
225 // number of sessions in this vrf
231 // is this vrf expired
233 }) per_vrf_sessions_t;
237 typedef CLIB_PACKED(struct
239 /* Outside network tuple */
247 /* Inside network tuple */
255 nat_protocol_t nat_proto;
260 /* Per-user translations */
262 u32 per_user_list_head_index;
264 /* head of LRU list in which this session is tracked */
266 /* index in global LRU list */
270 /* Last heard timer */
273 /* Last HA refresh */
274 f64 ha_last_refreshed;
280 /* External host address and port */
281 ip4_address_t ext_host_addr;
284 /* External host address and port after translation */
285 ip4_address_t ext_host_nat_addr;
286 u16 ext_host_nat_port;
288 /* TCP session state */
292 u64 tcp_closed_timestamp;
297 /* per vrf sessions index */
298 u32 per_vrf_sessions_index;
307 u32 sessions_per_user_list_head_index;
317 #define _(N, i, n, s) \
318 u32 busy_##n##_ports; \
319 u32 * busy_##n##_ports_per_thread; \
320 u32 busy_##n##_port_refcounts[65535];
340 /* backend IP address */
342 /* backend port number */
344 /* probability of the backend to be randomly matched */
347 /* backend FIB table */
350 } nat44_lb_addr_port_t;
354 /* twice-nat disabled */
356 /* twice-nat enabled */
358 /* twice-nat only when src IP equals dst IP after translation */
364 /* no load-balancing */
368 /* load-balancing with affinity */
374 /* prefered pool address */
375 ip4_address_t pool_addr;
376 /* local IP address */
377 ip4_address_t local_addr;
378 /* external IP address */
379 ip4_address_t external_addr;
385 twice_nat_type_t twice_nat;
386 /* local FIB table */
390 nat_protocol_t proto;
391 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
393 /* worker threads used by backends/local host */
395 /* opaque string tag */
397 /* backends for load-balancing mode */
398 nat44_lb_addr_port_t *locals;
399 /* affinity per service lis */
400 u32 affinity_per_service_list_head_index;
403 } snat_static_mapping_t;
413 ip4_address_t l_addr;
414 ip4_address_t pool_addr;
419 nat_protocol_t proto;
428 } snat_static_map_resolve_t;
432 /* Main lookup tables */
433 clib_bihash_8_8_t out2in;
434 clib_bihash_8_8_t in2out;
436 /* Endpoint dependent sessions lookup tables */
437 clib_bihash_16_8_t in2out_ed;
439 /* Find-a-user => src address lookup */
440 clib_bihash_8_8_t user_hash;
446 snat_session_t *sessions;
448 /* Pool of doubly-linked list elements */
449 dlist_elt_t *list_pool;
451 /* LRU session list - head is stale, tail is fresh */
452 dlist_elt_t *lru_pool;
453 u32 tcp_trans_lru_head_index;
454 u32 tcp_estab_lru_head_index;
455 u32 udp_lru_head_index;
456 u32 icmp_lru_head_index;
457 u32 unk_proto_lru_head_index;
459 /* NAT thread index */
460 u32 snat_thread_index;
462 /* real thread index */
465 per_vrf_sessions_t *per_vrf_sessions_vec;
467 } snat_main_per_thread_data_t;
471 /* ICMP session match function */
472 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
473 vlib_node_runtime_t * node,
477 ip4_address_t * addr,
480 nat_protocol_t * proto,
482 u8 * dont_translate);
484 /* Return worker thread index for given packet */
485 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
489 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
494 /* NAT address and port allocation function */
495 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
499 nat_protocol_t proto,
500 ip4_address_t * addr,
503 u32 snat_thread_index);
505 #define foreach_nat_counter _ (tcp) _ (udp) _ (icmp) _ (other) _ (drops)
507 typedef struct snat_main_s
509 /* ICMP session match functions */
510 snat_icmp_match_function_t *icmp_match_in2out_cb;
511 snat_icmp_match_function_t *icmp_match_out2in_cb;
513 /* Thread settings */
515 u32 first_worker_index;
517 snat_get_worker_in2out_function_t *worker_in2out_cb;
518 snat_get_worker_out2in_function_t *worker_out2in_cb;
521 /* Per thread data */
522 snat_main_per_thread_data_t *per_thread_data;
524 /* Find a static mapping by local */
525 clib_bihash_8_8_t static_mapping_by_local;
527 /* Find a static mapping by external */
528 clib_bihash_8_8_t static_mapping_by_external;
530 /* Static mapping pool */
531 snat_static_mapping_t *static_mappings;
533 /* Endpoint-dependent out2in mappings */
534 clib_bihash_16_8_t out2in_ed;
537 snat_interface_t *interfaces;
538 snat_interface_t *output_feature_interfaces;
540 /* Vector of outside addresses */
541 snat_address_t *addresses;
542 /* Address and port allocation function */
543 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
544 /* Address and port allocation type */
545 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
546 /* Port set parameters (MAP-E) */
550 /* Port range parameters */
557 /* vector of outside fibs */
558 nat_outside_fib_t *outside_fibs;
560 /* Vector of twice NAT addresses for external hosts */
561 snat_address_t *twice_nat_addresses;
563 /* sw_if_indices whose intfc addresses should be auto-added */
564 u32 *auto_add_sw_if_indices;
565 u32 *auto_add_sw_if_indices_twice_nat;
567 /* vector of interface address static mappings to resolve. */
568 snat_static_map_resolve_t *to_resolve;
570 /* Randomize port allocation order */
573 /* Worker handoff frame-queue index */
575 u32 fq_in2out_output_index;
579 u32 error_node_index;
581 /* handoff fq nodes */
582 u32 handoff_out2in_index;
583 u32 handoff_in2out_index;
584 u32 handoff_in2out_output_index;
586 /* respect feature arc nodes */
587 u32 pre_out2in_node_index;
588 u32 pre_in2out_node_index;
590 u32 out2in_node_index;
591 u32 in2out_node_index;
592 u32 in2out_output_node_index;
594 u32 in2out_fast_node_index;
595 u32 in2out_slowpath_node_index;
596 u32 in2out_slowpath_output_node_index;
597 u32 out2in_fast_node_index;
599 u32 ei_out2in_node_index;
600 u32 ei_in2out_node_index;
601 u32 ei_in2out_output_node_index;
603 u32 ed_out2in_node_index;
604 u32 ed_in2out_node_index;
605 u32 ed_in2out_output_node_index;
607 u32 ed_in2out_slowpath_node_index;
608 u32 ed_out2in_slowpath_node_index;
610 u32 hairpinning_node_index;
611 u32 hairpin_dst_node_index;
612 u32 hairpin_src_node_index;
613 u32 ed_hairpinning_node_index;
614 u32 ed_hairpin_dst_node_index;
615 u32 ed_hairpin_src_node_index;
617 nat44_config_t rconfig;
618 //nat44_config_t cconfig;
620 /* If forwarding is enabled */
621 u8 forwarding_enabled;
623 /* Config parameters */
624 u8 endpoint_dependent;
627 /* static mapping config */
628 u8 static_mapping_only;
629 u8 static_mapping_connection_tracking;
631 /* Is translation memory size calculated or user defined */
632 u8 translation_memory_size_set;
634 u32 translation_buckets;
635 u32 max_translations_per_thread;
636 u32 *max_translations_per_fib;
637 u32 max_users_per_thread;
639 u32 max_translations_per_user;
642 u32 outside_fib_index;
644 u32 inside_fib_index;
646 /* values of various timeouts */
648 u32 tcp_transitory_timeout;
649 u32 tcp_established_timeout;
652 /* TCP MSS clamping */
656 vlib_simple_counter_main_t total_users;
657 vlib_simple_counter_main_t total_sessions;
658 vlib_simple_counter_main_t user_limit_reached;
660 #define _(x) vlib_simple_counter_main_t x;
709 vlib_simple_counter_main_t hairpinning;
713 /* API message ID base */
717 vlib_log_class_t log_class;
722 api_main_t *api_main;
723 ip4_main_t *ip4_main;
724 ip_lookup_main_t *ip4_lookup_main;
726 fib_source_t fib_src_hi;
727 fib_source_t fib_src_low;
729 /* nat44 plugin enabled */
732 vnet_main_t *vnet_main;
739 } nat44_is_idle_session_ctx_t;
743 u32 cached_sw_if_index;
744 u32 cached_ip4_address;
747 extern snat_main_t snat_main;
749 // nat pre ed next_node feature classification
750 extern vlib_node_registration_t nat_default_node;
751 extern vlib_node_registration_t nat_pre_in2out_node;
752 extern vlib_node_registration_t nat_pre_out2in_node;
754 extern vlib_node_registration_t snat_in2out_node;
755 extern vlib_node_registration_t snat_in2out_output_node;
756 extern vlib_node_registration_t snat_out2in_node;
757 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
758 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
759 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
760 extern vlib_node_registration_t nat44_ed_in2out_node;
761 extern vlib_node_registration_t nat44_ed_in2out_output_node;
762 extern vlib_node_registration_t nat44_ed_out2in_node;
764 extern fib_source_t nat_fib_src_hi;
765 extern fib_source_t nat_fib_src_low;
767 /* format functions */
768 format_function_t format_snat_user;
769 format_function_t format_snat_static_mapping;
770 format_function_t format_snat_static_map_to_resolve;
771 format_function_t format_snat_session;
772 format_function_t format_snat_key;
773 format_function_t format_static_mapping_key;
774 format_function_t format_nat_protocol;
775 format_function_t format_nat_addr_and_port_alloc_alg;
776 /* unformat functions */
777 unformat_function_t unformat_nat_protocol;
779 /** \brief Check if SNAT session is created from static mapping.
780 @param s SNAT session
781 @return 1 if SNAT session is created from static mapping otherwise 0
783 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
785 /** \brief Check if SNAT session for unknown protocol.
786 @param s SNAT session
787 @return 1 if SNAT session for unknown protocol otherwise 0
789 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
791 /** \brief Check if NAT session is twice NAT.
793 @return 1 if NAT session is twice NAT
795 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
797 /** \brief Check if NAT session is load-balancing.
799 @return 1 if NAT session is load-balancing
801 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
803 /** \brief Check if NAT session is forwarding bypass.
805 @return 1 if NAT session is load-balancing
807 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
809 /** \brief Check if NAT session is endpoint dependent.
811 @return 1 if NAT session is endpoint dependent
813 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
815 /** \brief Check if NAT session has affinity record.
817 @return 1 if NAT session has affinity record
819 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
821 /** \brief Check if exact pool address should be used.
822 @param s SNAT session
823 @return 1 if exact pool address or 0
825 #define is_exact_address_session(s) (s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS)
827 /** \brief Check if NAT interface is inside.
828 @param i NAT interface
829 @return 1 if inside interface
831 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
833 /** \brief Check if NAT interface is outside.
834 @param i NAT interface
835 @return 1 if outside interface
837 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
839 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
841 @return 1 if session is closed
843 #define nat44_is_ses_closed(s) s->state == 0xf
845 /** \brief Check if NAT static mapping is address only (1:1NAT).
846 @param sm NAT static mapping
847 @return 1 if 1:1NAT, 0 if 1:1NAPT
849 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
851 /** \brief Check if NAT static mapping match only out2in direction.
852 @param sm NAT static mapping
853 @return 1 if rule match only out2in direction
855 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
857 /** \brief Check if NAT static mapping is identity NAT.
858 @param sm NAT static mapping
859 @return 1 if identity NAT
861 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
863 /** \brief Check if NAT static mapping is load-balancing.
864 @param sm NAT static mapping
865 @return 1 if load-balancing
867 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
869 /** \brief Check if exact pool address should be used.
870 @param s SNAT session
871 @return 1 if exact pool address or 0
873 #define is_exact_address(s) (s->flags & NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS)
875 /** \brief Check if client initiating TCP connection (received SYN from client)
877 @return 1 if client initiating TCP connection
880 tcp_flags_is_init (u8 f)
882 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
886 #define nat_log_err(...) \
887 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
888 #define nat_log_warn(...) \
889 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
890 #define nat_log_notice(...) \
891 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
892 #define nat_log_info(...) \
893 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
894 #define nat_log_debug(...)\
895 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
897 /* NAT API Logging Levels */
898 #define foreach_nat_log_level \
901 _(0x02, LOG_WARNING) \
902 _(0x03, LOG_NOTICE) \
906 typedef enum nat_log_level_t_
908 #define _(n,f) SNAT_##f = n,
909 foreach_nat_log_level
913 #define nat_elog(_level, _str) \
916 snat_main_t *sm = &snat_main; \
917 if (PREDICT_FALSE (sm->log_level >= _level)) \
919 ELOG_TYPE_DECLARE (e) = \
921 .format = "nat-msg " _str, \
924 ELOG_DATA (&vlib_global_main.elog_main, e); \
928 #define nat_elog_addr(_level, _str, _addr) \
931 if (PREDICT_FALSE (sm->log_level >= _level)) \
933 ELOG_TYPE_DECLARE (e) = \
935 .format = "nat-msg " _str " %d.%d.%d.%d", \
936 .format_args = "i1i1i1i1", \
945 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
946 ed->oct4 = _addr >> 24; \
947 ed->oct3 = _addr >> 16; \
948 ed->oct2 = _addr >> 8; \
953 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
956 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
958 ELOG_TYPE_DECLARE (e) = \
960 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
961 " tid from: %d to: %d fib: %d", \
962 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
978 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
979 ed->src_oct1 = _src >> 24; \
980 ed->src_oct2 = _src >> 16; \
981 ed->src_oct3 = _src >> 8; \
982 ed->src_oct4 = _src; \
983 ed->dst_oct1 = _dst >> 24; \
984 ed->dst_oct2 = _dst >> 16; \
985 ed->dst_oct3 = _dst >> 8; \
986 ed->dst_oct4 = _dst; \
987 ed->ftid = vlib_get_thread_index (); \
993 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
996 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
998 ELOG_TYPE_DECLARE (e) = \
1000 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
1001 " tid:%d prt:%d fib:%d", \
1002 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
1004 CLIB_PACKED(struct \
1018 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
1019 ed->src_oct1 = _src >> 24; \
1020 ed->src_oct2 = _src >> 16; \
1021 ed->src_oct3 = _src >> 8; \
1022 ed->src_oct4 = _src; \
1023 ed->dst_oct1 = _dst >> 24; \
1024 ed->dst_oct2 = _dst >> 16; \
1025 ed->dst_oct3 = _dst >> 8; \
1026 ed->dst_oct4 = _dst; \
1027 ed->tid = vlib_get_thread_index (); \
1033 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
1036 snat_main_t *sm = &snat_main; \
1037 if (PREDICT_FALSE (sm->log_level >= _level)) \
1039 ELOG_TYPE_DECLARE (e) = \
1041 .format = "nat-msg " _fmt, \
1042 .format_args = _arg, \
1044 CLIB_PACKED(struct \
1046 typeof (_val1) val1; \
1048 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
1053 #define nat_elog_notice(nat_elog_str) \
1054 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
1055 #define nat_elog_warn(nat_elog_str) \
1056 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1057 #define nat_elog_err(nat_elog_str) \
1058 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1059 #define nat_elog_debug(nat_elog_str) \
1060 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1061 #define nat_elog_info(nat_elog_str) \
1062 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1064 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1065 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1066 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1067 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1068 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1069 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1070 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1071 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1072 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1073 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1075 /* ICMP session match functions */
1076 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1077 u32 thread_index, vlib_buffer_t * b0,
1078 ip4_header_t * ip0, ip4_address_t * addr,
1079 u16 * port, u32 * fib_index,
1080 nat_protocol_t * proto, void *d, void *e,
1081 u8 * dont_translate);
1082 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1083 u32 thread_index, vlib_buffer_t * b0,
1084 ip4_header_t * ip0, ip4_address_t * addr,
1085 u16 * port, u32 * fib_index,
1086 nat_protocol_t * proto, void *d, void *e,
1087 u8 * dont_translate);
1088 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1089 u32 thread_index, vlib_buffer_t * b0,
1090 ip4_header_t * ip0, ip4_address_t * addr,
1091 u16 * port, u32 * fib_index,
1092 nat_protocol_t * proto, void *d, void *e,
1093 u8 * dont_translate);
1094 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1095 u32 thread_index, vlib_buffer_t * b0,
1096 ip4_header_t * ip0, ip4_address_t * addr,
1097 u16 * port, u32 * fib_index,
1098 nat_protocol_t * proto, void *d, void *e,
1099 u8 * dont_translate);
1101 /* ICMP endpoint-dependent session match functions */
1102 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1103 u32 thread_index, vlib_buffer_t * b0,
1104 ip4_header_t * ip0, ip4_address_t * addr,
1105 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1106 void *d, void *e, u8 * dont_translate);
1107 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1108 u32 thread_index, vlib_buffer_t * b0,
1109 ip4_header_t * ip0, ip4_address_t * addr,
1110 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1111 void *d, void *e, u8 * dont_translate);
1113 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1114 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1115 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1118 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1119 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1120 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1123 /* hairpinning functions */
1124 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1125 ip4_header_t * ip0, icmp46_header_t * icmp0,
1127 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1129 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1131 int snat_hairpinning (vlib_main_t * vm, vlib_node_runtime_t * node,
1132 snat_main_t * sm, vlib_buffer_t * b0,
1133 ip4_header_t * ip0, udp_header_t * udp0,
1134 tcp_header_t * tcp0, u32 proto0, int is_ed,
1137 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1138 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1139 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1140 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1141 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1145 * @brief Enable NAT44 plugin
1147 * @param c nat44_config_t
1149 * @return 0 on success, non-zero value otherwise
1151 int nat44_plugin_enable (nat44_config_t c);
1154 * @brief Disable NAT44 plugin
1156 * @return 0 on success, non-zero value otherwise
1158 int nat44_plugin_disable ();
1161 * @brief Add external address to NAT44 pool
1163 * @param sm snat global configuration data
1164 * @param addr IPv4 address
1165 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1166 * @param twice_nat 1 if twice NAT address
1168 * @return 0 on success, non-zero value otherwise
1170 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1174 * @brief Delete external address from NAT44 pool
1176 * @param sm snat global configuration data
1177 * @param addr IPv4 address
1178 * @param delete_sm 1 if delete static mapping using address
1179 * @param twice_nat 1 if twice NAT address
1181 * @return 0 on success, non-zero value otherwise
1183 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1187 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1189 * @param addr IPv4 address
1190 * @param is_add 1 = add, 0 = delete
1192 * @return 0 on success, non-zero value otherwise
1194 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1197 * @brief Add/delete NAT44 static mapping
1199 * @param l_addr local IPv4 address
1200 * @param e_addr external IPv4 address
1201 * @param l_port local port number
1202 * @param e_port external port number
1203 * @param vrf_id local VRF ID
1204 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1205 * @param sw_if_index use interface address as external IPv4 address
1206 * @param proto L4 protocol
1207 * @param is_add 1 = add, 0 = delete
1208 * @param twice_nat twice-nat mode
1209 * @param out2in_only if 1 rule match only out2in direction
1210 * @param tag opaque string tag
1211 * @param identity_nat identity NAT
1212 * @param pool_addr pool IPv4 address
1213 * @param exact 1 = exact pool address
1215 * @return 0 on success, non-zero value otherwise
1217 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1218 u16 l_port, u16 e_port, u32 vrf_id,
1219 int addr_only, u32 sw_if_index,
1220 nat_protocol_t proto, int is_add,
1221 twice_nat_type_t twice_nat, u8 out2in_only,
1222 u8 * tag, u8 identity_nat,
1223 ip4_address_t pool_addr, int exact);
1226 * @brief Add/delete static mapping with load-balancing (multiple backends)
1228 * @param e_addr external IPv4 address
1229 * @param e_port external port number
1230 * @param proto L4 protocol
1231 * @param locals list of local backends
1232 * @param is_add 1 = add, 0 = delete
1233 * @param twice_nat twice-nat mode
1234 * @param out2in_only if 1 rule match only out2in direction
1235 * @param tag opaque string tag
1236 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1238 * @return 0 on success, non-zero value otherwise
1240 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1241 nat_protocol_t proto,
1242 nat44_lb_addr_port_t * locals, u8 is_add,
1243 twice_nat_type_t twice_nat,
1244 u8 out2in_only, u8 * tag, u32 affinity);
1246 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1247 ip4_address_t l_addr, u16 l_port,
1248 nat_protocol_t proto, u32 vrf_id,
1249 u8 probability, u8 is_add);
1251 clib_error_t *nat44_api_hookup (vlib_main_t * vm);
1254 * @brief Set NAT plugin workers
1256 * @param bitmap NAT workers bitmap
1258 * @return 0 on success, non-zero value otherwise
1260 int snat_set_workers (uword * bitmap);
1263 * @brief Enable/disable NAT44 feature on the interface
1265 * @param sw_if_index software index of the interface
1266 * @param is_inside 1 = inside, 0 = outside
1267 * @param is_del 1 = delete, 0 = add
1269 * @return 0 on success, non-zero value otherwise
1271 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1274 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1276 * @param sw_if_index software index of the interface
1277 * @param is_inside 1 = inside, 0 = outside
1278 * @param is_del 1 = delete, 0 = add
1280 * @return 0 on success, non-zero value otherwise
1282 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1286 * @brief Add/delete NAT44 pool address from specific interface
1288 * @param sw_if_index software index of the interface
1289 * @param is_del 1 = delete, 0 = add
1290 * @param twice_nat 1 = twice NAT address for external hosts
1292 * @return 0 on success, non-zero value otherwise
1294 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1298 * @brief Delete NAT44 session
1300 * @param addr IPv4 address
1301 * @param port L4 port number
1302 * @param proto L4 protocol
1303 * @param vrf_id VRF ID
1304 * @param is_in 1 = inside network address and port pair, 0 = outside
1306 * @return 0 on success, non-zero value otherwise
1308 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1309 nat_protocol_t proto, u32 vrf_id, int is_in);
1312 * @brief Delete NAT44 endpoint-dependent session
1314 * @param sm snat global configuration data
1315 * @param addr IPv4 address
1316 * @param port L4 port number
1317 * @param proto L4 protocol
1318 * @param vrf_id VRF ID
1319 * @param is_in 1 = inside network address and port pair, 0 = outside
1321 * @return 0 on success, non-zero value otherwise
1323 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1324 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1325 u32 vrf_id, int is_in);
1328 * @brief Free NAT44 session data (lookup keys, external address port)
1330 * @param sm snat global configuration data
1331 * @param s NAT session
1332 * @param thread_index thread index
1333 * @param is_ha is HA event
1335 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1336 u32 thread_index, u8 is_ha);
1339 * @brief Set NAT44 session limit (session limit, vrf id)
1341 * @param session_limit Session limit
1342 * @param vrf_id VRF id
1343 * @return 0 on success, non-zero value otherwise
1345 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1348 * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
1350 * @param session_limit Session limit
1351 * @param vrf_id VRF id
1352 * @return 0 on success, non-zero value otherwise
1354 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
1357 * @brief Initialize NAT44 data
1359 * @param tsm per thread data
1361 void nat44_db_init (snat_main_per_thread_data_t * tsm);
1364 * @brief Free NAT44 data
1366 * @param tsm per thread data
1368 void nat44_db_free (snat_main_per_thread_data_t * tsm);
1371 * @brief Delete specific NAT44 EI user and his sessions
1373 * @param addr IPv4 address
1374 * @param fib_index FIB table index
1376 int nat44_ei_user_del (ip4_address_t * addr, u32 fib_index);
1379 * @brief Free all NAT44 sessions
1381 void nat44_sessions_clear ();
1384 * @brief Find or create NAT user
1386 * @param sm snat global configuration data
1387 * @param addr IPv4 address
1388 * @param fib_index FIB table index
1389 * @param thread_index thread index
1391 * @return NAT user data structure on success otherwise zero value
1393 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1394 ip4_address_t * addr, u32 fib_index,
1398 * @brief Allocate new NAT session or recycle last used
1400 * @param sm snat global configuration data
1402 * @param thread_index thread index
1403 * @param now time now
1405 * @return session data structure on success otherwise zero value
1407 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1409 u32 thread_index, f64 now);
1412 * @brief Set address and port assignment algorithm for MAP-E CE
1414 * @param psid Port Set Identifier value
1415 * @param psid_offset number of offset bits
1416 * @param psid_length length of PSID
1418 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1422 * @brief Set address and port assignment algorithm for port range
1424 * @param start_port beginning of the port range
1425 * @param end_port end of the port range
1427 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1430 * @brief Set address and port assignment algorithm to default/standard
1432 void nat_set_alloc_addr_and_port_default (void);
1435 * @brief Free outside address and port pair
1437 * @param addresses vector of outside addresses
1438 * @param thread_index thread index
1439 * @param key address, port and protocol
1442 snat_free_outside_address_and_port (snat_address_t * addresses,
1444 ip4_address_t * addr,
1445 u16 port, nat_protocol_t protocol);
1448 * @brief Alloc outside address and port
1450 * @param addresses vector of outside addresses
1451 * @param fib_index FIB table index
1452 * @param thread_index thread index
1453 * @param port_per_thread number of ports per thread
1454 * @param snat_thread_index NAT thread index
1456 * @return 0 on success, non-zero value otherwise
1458 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1461 nat_protocol_t proto,
1462 ip4_address_t * addr,
1464 u16 port_per_thread,
1465 u32 snat_thread_index);
1467 void expire_per_vrf_sessions (u32 fib_index);
1470 * @brief Match NAT44 static mapping.
1472 * @param key address and port to match
1473 * @param addr external/local address of the matched mapping
1474 * @param port port of the matched mapping
1475 * @param fib_index fib index of the matched mapping
1476 * @param by_external if 0 match by local address otherwise match by external
1478 * @param is_addr_only 1 if matched mapping is address only
1479 * @param twice_nat matched mapping is twice NAT type
1480 * @param lb 1 if matched mapping is load-balanced
1481 * @param ext_host_addr external host address
1482 * @param is_identity_nat 1 if indentity mapping
1483 * @param out if !=0 set to pointer of the mapping structure
1485 * @returns 0 if match found otherwise 1.
1487 int snat_static_mapping_match (snat_main_t * sm,
1488 ip4_address_t match_addr,
1490 u32 match_fib_index,
1491 nat_protocol_t match_protocol,
1492 ip4_address_t * mapping_addr,
1494 u32 * mapping_fib_index,
1497 twice_nat_type_t * twice_nat,
1499 ip4_address_t * ext_host_addr,
1500 u8 * is_identity_nat,
1501 snat_static_mapping_t ** out);
1504 * @brief Add/del NAT address to FIB.
1506 * Add the external NAT address to the FIB as receive entries. This ensures
1507 * that VPP will reply to ARP for this address and we don't need to enable
1508 * proxy ARP on the outside interface.
1510 * @param addr IPv4 address
1511 * @param plen address prefix length
1512 * @param sw_if_index software index of the outside interface
1513 * @param is_add 0 = delete, 1 = add.
1515 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1516 u8 p_len, u32 sw_if_index, int is_add);
1519 nat_ha_sadd_cb (ip4_address_t * in_addr, u16 in_port,
1520 ip4_address_t * out_addr, u16 out_port,
1521 ip4_address_t * eh_addr, u16 eh_port,
1522 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
1523 u32 fib_index, u16 flags, u32 thread_index);
1526 nat_ha_sdel_cb (ip4_address_t * out_addr, u16 out_port,
1527 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
1531 nat_ha_sref_cb (ip4_address_t * out_addr, u16 out_port,
1532 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
1533 u32 total_pkts, u64 total_bytes, u32 thread_index);
1536 nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
1537 ip4_address_t * out_addr, u16 out_port,
1538 ip4_address_t * eh_addr, u16 eh_port,
1539 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
1540 u32 fib_index, u16 flags, u32 thread_index);
1543 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
1544 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1545 u32 fib_index, u32 ti);
1548 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
1549 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1550 u32 fib_index, u32 ti);
1553 nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
1554 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1555 u32 fib_index, u32 total_pkts, u64 total_bytes,
1559 * Why is this here? Because we don't need to touch this layer to
1560 * simply reply to an icmp. We need to change id to a unique
1561 * value to NAT an echo request/reply.
1568 } icmp_echo_header_t;
1572 u16 src_port, dst_port;
1575 u8 *format_user_kvp (u8 * s, va_list * args);
1577 #endif /* __included_nat_h__ */
1579 * fd.io coding-style-patch-verification: ON
1582 * eval: (c-set-style "gnu")