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 /* number of worker handoff frame queue elements */
40 #define NAT_FQ_NELTS 64
42 /* NAT buffer flags */
43 #define SNAT_FLAG_HAIRPINNING (1 << 0)
45 /* NAT44 API Configuration flags */
46 #define foreach_nat44_config_flag \
47 _(0x00, IS_ENDPOINT_INDEPENDENT) \
48 _(0x01, IS_ENDPOINT_DEPENDENT) \
49 _(0x02, IS_STATIC_MAPPING_ONLY) \
50 _(0x04, IS_CONNECTION_TRACKING) \
51 _(0x08, IS_OUT2IN_DPO)
53 typedef enum nat44_config_flags_t_
55 #define _(n,f) NAT44_API_##f = n,
56 foreach_nat44_config_flag
58 } nat44_config_flags_t;
62 /* nat44 plugin features */
63 u8 static_mapping_only;
64 u8 connection_tracking;
65 u8 endpoint_dependent;
71 /* maximum number of users */
74 /* maximum number of sessions */
77 /* maximum number of ssessions per user */
85 NAT_NEXT_IN2OUT_ED_FAST_PATH,
86 NAT_NEXT_IN2OUT_ED_SLOW_PATH,
87 NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH,
88 NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
89 NAT_NEXT_OUT2IN_ED_FAST_PATH,
90 NAT_NEXT_OUT2IN_ED_SLOW_PATH,
91 NAT_NEXT_IN2OUT_CLASSIFY,
92 NAT_NEXT_OUT2IN_CLASSIFY,
102 /* user (internal host) key */
116 /* External address and port allocation modes */
117 #define foreach_nat_addr_and_port_alloc_alg \
118 _(0, DEFAULT, "default") \
119 _(1, MAPE, "map-e") \
120 _(2, RANGE, "port-range")
124 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
125 foreach_nat_addr_and_port_alloc_alg
127 } nat_addr_and_port_alloc_alg_t;
130 #define foreach_snat_session_state \
131 _(0, UNKNOWN, "unknown") \
132 _(1, UDP_ACTIVE, "udp-active") \
133 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
134 _(3, TCP_ESTABLISHED, "tcp-established") \
135 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
136 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
137 _(6, TCP_CLOSING, "tcp-closing") \
138 _(7, TCP_LAST_ACK, "tcp-last-ack") \
139 _(8, TCP_CLOSED, "tcp-closed") \
140 _(9, ICMP_ACTIVE, "icmp-active")
144 #define _(v, N, s) SNAT_SESSION_##N = v,
145 foreach_snat_session_state
147 } snat_session_state_t;
149 #define foreach_nat_in2out_ed_error \
150 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
151 _(OUT_OF_PORTS, "out of ports") \
152 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
153 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
154 _(NON_SYN, "non-SYN packet try to create session") \
155 _(TCP_CLOSED, "drops due to TCP in transitory timeout")
159 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
160 foreach_nat_in2out_ed_error
162 NAT_IN2OUT_ED_N_ERROR,
163 } nat_in2out_ed_error_t;
165 #define foreach_nat_out2in_ed_error \
166 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
167 _(OUT_OF_PORTS, "out of ports") \
168 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
169 _(NO_TRANSLATION, "no translation") \
170 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
171 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
172 _(CANNOT_CREATE_USER, "cannot create NAT user") \
173 _(NON_SYN, "non-SYN packet try to create session") \
174 _(TCP_CLOSED, "drops due to TCP in transitory timeout")
178 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
179 foreach_nat_out2in_ed_error
181 NAT_OUT2IN_ED_N_ERROR,
182 } nat_out2in_ed_error_t;
185 /* Endpoint dependent TCP session state */
186 #define NAT44_SES_I2O_FIN 1
187 #define NAT44_SES_O2I_FIN 2
188 #define NAT44_SES_I2O_FIN_ACK 4
189 #define NAT44_SES_O2I_FIN_ACK 8
190 #define NAT44_SES_I2O_SYN 16
191 #define NAT44_SES_O2I_SYN 32
192 #define NAT44_SES_RST 64
195 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
196 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
197 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
198 #define SNAT_SESSION_FLAG_TWICE_NAT 8
199 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
200 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
201 #define SNAT_SESSION_FLAG_AFFINITY 64
202 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
203 #define SNAT_SESSION_FLAG_EXACT_ADDRESS 256
205 /* NAT interface flags */
206 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
207 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
209 /* Static mapping flags */
210 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
211 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
212 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
213 #define NAT_STATIC_MAPPING_FLAG_LB 8
214 #define NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS 16
217 typedef CLIB_PACKED(struct
219 // number of sessions in this vrf
225 // is this vrf expired
227 }) per_vrf_sessions_t;
231 typedef CLIB_PACKED(struct
233 /* Outside network tuple */
241 /* Inside network tuple */
249 nat_protocol_t nat_proto;
254 /* Per-user translations */
256 u32 per_user_list_head_index;
258 /* head of LRU list in which this session is tracked */
260 /* index in global LRU list */
264 /* Last heard timer */
267 /* Last HA refresh */
268 f64 ha_last_refreshed;
274 /* External host address and port */
275 ip4_address_t ext_host_addr;
278 /* External host address and port after translation */
279 ip4_address_t ext_host_nat_addr;
280 u16 ext_host_nat_port;
282 /* TCP session state */
286 u64 tcp_closed_timestamp;
291 /* per vrf sessions index */
292 u32 per_vrf_sessions_index;
301 u32 sessions_per_user_list_head_index;
311 #define _(N, i, n, s) \
312 u32 busy_##n##_ports; \
313 u32 * busy_##n##_ports_per_thread; \
314 u32 busy_##n##_port_refcounts[65535];
334 /* backend IP address */
336 /* backend port number */
338 /* probability of the backend to be randomly matched */
341 /* backend FIB table */
344 } nat44_lb_addr_port_t;
348 /* twice-nat disabled */
350 /* twice-nat enabled */
352 /* twice-nat only when src IP equals dst IP after translation */
358 /* no load-balancing */
362 /* load-balancing with affinity */
368 /* prefered pool address */
369 ip4_address_t pool_addr;
370 /* local IP address */
371 ip4_address_t local_addr;
372 /* external IP address */
373 ip4_address_t external_addr;
379 twice_nat_type_t twice_nat;
380 /* local FIB table */
384 nat_protocol_t proto;
385 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
387 /* worker threads used by backends/local host */
389 /* opaque string tag */
391 /* backends for load-balancing mode */
392 nat44_lb_addr_port_t *locals;
393 /* affinity per service lis */
394 u32 affinity_per_service_list_head_index;
397 } snat_static_mapping_t;
407 ip4_address_t l_addr;
408 ip4_address_t pool_addr;
413 nat_protocol_t proto;
422 } snat_static_map_resolve_t;
426 /* Main lookup tables */
427 clib_bihash_8_8_t out2in;
428 clib_bihash_8_8_t in2out;
430 /* Endpoint dependent sessions lookup tables */
431 clib_bihash_16_8_t in2out_ed;
433 /* Find-a-user => src address lookup */
434 clib_bihash_8_8_t user_hash;
440 snat_session_t *sessions;
442 /* Pool of doubly-linked list elements */
443 dlist_elt_t *list_pool;
445 /* LRU session list - head is stale, tail is fresh */
446 dlist_elt_t *lru_pool;
447 u32 tcp_trans_lru_head_index;
448 u32 tcp_estab_lru_head_index;
449 u32 udp_lru_head_index;
450 u32 icmp_lru_head_index;
451 u32 unk_proto_lru_head_index;
453 /* NAT thread index */
454 u32 snat_thread_index;
456 /* real thread index */
459 per_vrf_sessions_t *per_vrf_sessions_vec;
461 } snat_main_per_thread_data_t;
465 /* ICMP session match function */
466 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
467 vlib_node_runtime_t * node,
471 ip4_address_t * addr,
474 nat_protocol_t * proto,
476 u8 * dont_translate);
478 /* Return worker thread index for given packet */
479 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
483 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
488 /* NAT address and port allocation function */
489 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
493 nat_protocol_t proto,
494 ip4_address_t * addr,
497 u32 snat_thread_index);
499 #define foreach_nat_counter _ (tcp) _ (udp) _ (icmp) _ (other) _ (drops)
501 typedef struct snat_main_s
503 /* ICMP session match functions */
504 snat_icmp_match_function_t *icmp_match_in2out_cb;
505 snat_icmp_match_function_t *icmp_match_out2in_cb;
507 /* Thread settings */
509 u32 first_worker_index;
511 snat_get_worker_in2out_function_t *worker_in2out_cb;
512 snat_get_worker_out2in_function_t *worker_out2in_cb;
515 /* Per thread data */
516 snat_main_per_thread_data_t *per_thread_data;
518 /* Find a static mapping by local */
519 clib_bihash_8_8_t static_mapping_by_local;
521 /* Find a static mapping by external */
522 clib_bihash_8_8_t static_mapping_by_external;
524 /* Static mapping pool */
525 snat_static_mapping_t *static_mappings;
527 /* Endpoint-dependent out2in mappings */
528 clib_bihash_16_8_t out2in_ed;
531 snat_interface_t *interfaces;
532 snat_interface_t *output_feature_interfaces;
534 /* Vector of outside addresses */
535 snat_address_t *addresses;
536 /* Address and port allocation function */
537 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
538 /* Address and port allocation type */
539 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
540 /* Port set parameters (MAP-E) */
544 /* Port range parameters */
551 /* vector of outside fibs */
552 nat_outside_fib_t *outside_fibs;
554 /* Vector of twice NAT addresses for external hosts */
555 snat_address_t *twice_nat_addresses;
557 /* sw_if_indices whose intfc addresses should be auto-added */
558 u32 *auto_add_sw_if_indices;
559 u32 *auto_add_sw_if_indices_twice_nat;
561 /* vector of interface address static mappings to resolve. */
562 snat_static_map_resolve_t *to_resolve;
564 /* Randomize port allocation order */
567 /* Worker handoff frame-queue index */
569 u32 fq_in2out_output_index;
573 u32 error_node_index;
575 /* handoff fq nodes */
576 u32 handoff_out2in_index;
577 u32 handoff_in2out_index;
578 u32 handoff_in2out_output_index;
580 /* respect feature arc nodes */
581 u32 pre_out2in_node_index;
582 u32 pre_in2out_node_index;
584 u32 out2in_node_index;
585 u32 in2out_node_index;
586 u32 in2out_output_node_index;
588 u32 in2out_fast_node_index;
589 u32 in2out_slowpath_node_index;
590 u32 in2out_slowpath_output_node_index;
591 u32 out2in_fast_node_index;
593 u32 ei_out2in_node_index;
594 u32 ei_in2out_node_index;
595 u32 ei_in2out_output_node_index;
597 u32 ed_out2in_node_index;
598 u32 ed_in2out_node_index;
599 u32 ed_in2out_output_node_index;
601 u32 ed_in2out_slowpath_node_index;
602 u32 ed_out2in_slowpath_node_index;
604 u32 hairpinning_node_index;
605 u32 hairpin_dst_node_index;
606 u32 hairpin_src_node_index;
607 u32 ed_hairpinning_node_index;
608 u32 ed_hairpin_dst_node_index;
609 u32 ed_hairpin_src_node_index;
611 nat44_config_t rconfig;
612 //nat44_config_t cconfig;
614 /* If forwarding is enabled */
615 u8 forwarding_enabled;
617 /* Config parameters */
618 u8 endpoint_dependent;
621 /* static mapping config */
622 u8 static_mapping_only;
623 u8 static_mapping_connection_tracking;
625 /* Is translation memory size calculated or user defined */
626 u8 translation_memory_size_set;
628 u32 translation_buckets;
629 u32 max_translations_per_thread;
630 u32 *max_translations_per_fib;
631 u32 max_users_per_thread;
633 u32 max_translations_per_user;
636 u32 outside_fib_index;
638 u32 inside_fib_index;
640 nat_timeouts_t timeouts;
642 /* TCP MSS clamping */
646 vlib_simple_counter_main_t total_users;
647 vlib_simple_counter_main_t total_sessions;
648 vlib_simple_counter_main_t user_limit_reached;
650 #define _(x) vlib_simple_counter_main_t x;
699 vlib_simple_counter_main_t hairpinning;
703 /* API message ID base */
707 vlib_log_class_t log_class;
712 api_main_t *api_main;
713 ip4_main_t *ip4_main;
714 ip_lookup_main_t *ip4_lookup_main;
716 fib_source_t fib_src_hi;
717 fib_source_t fib_src_low;
719 /* nat44 plugin enabled */
722 vnet_main_t *vnet_main;
729 } nat44_is_idle_session_ctx_t;
733 u32 cached_sw_if_index;
734 u32 cached_ip4_address;
737 extern snat_main_t snat_main;
739 // nat pre ed next_node feature classification
740 extern vlib_node_registration_t nat_default_node;
741 extern vlib_node_registration_t nat_pre_in2out_node;
742 extern vlib_node_registration_t nat_pre_out2in_node;
744 extern vlib_node_registration_t snat_in2out_node;
745 extern vlib_node_registration_t snat_in2out_output_node;
746 extern vlib_node_registration_t snat_out2in_node;
747 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
748 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
749 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
750 extern vlib_node_registration_t nat44_ed_in2out_node;
751 extern vlib_node_registration_t nat44_ed_in2out_output_node;
752 extern vlib_node_registration_t nat44_ed_out2in_node;
754 extern fib_source_t nat_fib_src_hi;
755 extern fib_source_t nat_fib_src_low;
757 /* format functions */
758 format_function_t format_snat_user;
759 format_function_t format_snat_static_mapping;
760 format_function_t format_snat_static_map_to_resolve;
761 format_function_t format_snat_session;
762 format_function_t format_snat_key;
763 format_function_t format_static_mapping_key;
764 format_function_t format_nat_protocol;
765 format_function_t format_nat_addr_and_port_alloc_alg;
766 /* unformat functions */
767 unformat_function_t unformat_nat_protocol;
769 /** \brief Check if SNAT session is created from static mapping.
770 @param s SNAT session
771 @return 1 if SNAT session is created from static mapping otherwise 0
773 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
775 /** \brief Check if SNAT session for unknown protocol.
776 @param s SNAT session
777 @return 1 if SNAT session for unknown protocol otherwise 0
779 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
781 /** \brief Check if NAT session is twice NAT.
783 @return 1 if NAT session is twice NAT
785 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
787 /** \brief Check if NAT session is load-balancing.
789 @return 1 if NAT session is load-balancing
791 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
793 /** \brief Check if NAT session is forwarding bypass.
795 @return 1 if NAT session is load-balancing
797 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
799 /** \brief Check if NAT session is endpoint dependent.
801 @return 1 if NAT session is endpoint dependent
803 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
805 /** \brief Check if NAT session has affinity record.
807 @return 1 if NAT session has affinity record
809 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
811 /** \brief Check if exact pool address should be used.
812 @param s SNAT session
813 @return 1 if exact pool address or 0
815 #define is_exact_address_session(s) (s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS)
817 /** \brief Check if NAT interface is inside.
818 @param i NAT interface
819 @return 1 if inside interface
821 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
823 /** \brief Check if NAT interface is outside.
824 @param i NAT interface
825 @return 1 if outside interface
827 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
829 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
831 @return 1 if session is closed
833 #define nat44_is_ses_closed(s) s->state == 0xf
835 /** \brief Check if NAT static mapping is address only (1:1NAT).
836 @param sm NAT static mapping
837 @return 1 if 1:1NAT, 0 if 1:1NAPT
839 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
841 /** \brief Check if NAT static mapping match only out2in direction.
842 @param sm NAT static mapping
843 @return 1 if rule match only out2in direction
845 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
847 /** \brief Check if NAT static mapping is identity NAT.
848 @param sm NAT static mapping
849 @return 1 if identity NAT
851 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
853 /** \brief Check if NAT static mapping is load-balancing.
854 @param sm NAT static mapping
855 @return 1 if load-balancing
857 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
859 /** \brief Check if exact pool address should be used.
860 @param s SNAT session
861 @return 1 if exact pool address or 0
863 #define is_exact_address(s) (s->flags & NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS)
865 /** \brief Check if client initiating TCP connection (received SYN from client)
867 @return 1 if client initiating TCP connection
870 tcp_flags_is_init (u8 f)
872 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
876 #define nat_log_err(...) \
877 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
878 #define nat_log_warn(...) \
879 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
880 #define nat_log_notice(...) \
881 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
882 #define nat_log_info(...) \
883 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
884 #define nat_log_debug(...)\
885 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
887 /* NAT API Logging Levels */
888 #define foreach_nat_log_level \
891 _(0x02, LOG_WARNING) \
892 _(0x03, LOG_NOTICE) \
896 typedef enum nat_log_level_t_
898 #define _(n,f) SNAT_##f = n,
899 foreach_nat_log_level
903 #define nat_elog(_level, _str) \
906 snat_main_t *sm = &snat_main; \
907 if (PREDICT_FALSE (sm->log_level >= _level)) \
909 ELOG_TYPE_DECLARE (e) = \
911 .format = "nat-msg " _str, \
914 ELOG_DATA (&vlib_global_main.elog_main, e); \
918 #define nat_elog_addr(_level, _str, _addr) \
921 if (PREDICT_FALSE (sm->log_level >= _level)) \
923 ELOG_TYPE_DECLARE (e) = \
925 .format = "nat-msg " _str " %d.%d.%d.%d", \
926 .format_args = "i1i1i1i1", \
935 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
936 ed->oct4 = _addr >> 24; \
937 ed->oct3 = _addr >> 16; \
938 ed->oct2 = _addr >> 8; \
943 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
946 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
948 ELOG_TYPE_DECLARE (e) = \
950 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
951 " tid from: %d to: %d fib: %d", \
952 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
968 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
969 ed->src_oct1 = _src >> 24; \
970 ed->src_oct2 = _src >> 16; \
971 ed->src_oct3 = _src >> 8; \
972 ed->src_oct4 = _src; \
973 ed->dst_oct1 = _dst >> 24; \
974 ed->dst_oct2 = _dst >> 16; \
975 ed->dst_oct3 = _dst >> 8; \
976 ed->dst_oct4 = _dst; \
977 ed->ftid = vlib_get_thread_index (); \
983 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
986 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
988 ELOG_TYPE_DECLARE (e) = \
990 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
991 " tid:%d prt:%d fib:%d", \
992 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
1008 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
1009 ed->src_oct1 = _src >> 24; \
1010 ed->src_oct2 = _src >> 16; \
1011 ed->src_oct3 = _src >> 8; \
1012 ed->src_oct4 = _src; \
1013 ed->dst_oct1 = _dst >> 24; \
1014 ed->dst_oct2 = _dst >> 16; \
1015 ed->dst_oct3 = _dst >> 8; \
1016 ed->dst_oct4 = _dst; \
1017 ed->tid = vlib_get_thread_index (); \
1023 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
1026 snat_main_t *sm = &snat_main; \
1027 if (PREDICT_FALSE (sm->log_level >= _level)) \
1029 ELOG_TYPE_DECLARE (e) = \
1031 .format = "nat-msg " _fmt, \
1032 .format_args = _arg, \
1034 CLIB_PACKED(struct \
1036 typeof (_val1) val1; \
1038 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
1043 #define nat_elog_notice(nat_elog_str) \
1044 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
1045 #define nat_elog_warn(nat_elog_str) \
1046 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1047 #define nat_elog_err(nat_elog_str) \
1048 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1049 #define nat_elog_debug(nat_elog_str) \
1050 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1051 #define nat_elog_info(nat_elog_str) \
1052 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1054 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1055 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1056 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1057 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1058 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1059 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1060 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1061 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1062 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1063 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1065 /* ICMP session match functions */
1066 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1067 u32 thread_index, vlib_buffer_t * b0,
1068 ip4_header_t * ip0, ip4_address_t * addr,
1069 u16 * port, u32 * fib_index,
1070 nat_protocol_t * proto, void *d, void *e,
1071 u8 * dont_translate);
1072 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1073 u32 thread_index, vlib_buffer_t * b0,
1074 ip4_header_t * ip0, ip4_address_t * addr,
1075 u16 * port, u32 * fib_index,
1076 nat_protocol_t * proto, void *d, void *e,
1077 u8 * dont_translate);
1078 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1079 u32 thread_index, vlib_buffer_t * b0,
1080 ip4_header_t * ip0, ip4_address_t * addr,
1081 u16 * port, u32 * fib_index,
1082 nat_protocol_t * proto, void *d, void *e,
1083 u8 * dont_translate);
1084 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1085 u32 thread_index, vlib_buffer_t * b0,
1086 ip4_header_t * ip0, ip4_address_t * addr,
1087 u16 * port, u32 * fib_index,
1088 nat_protocol_t * proto, void *d, void *e,
1089 u8 * dont_translate);
1091 /* ICMP endpoint-dependent session match functions */
1092 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1093 u32 thread_index, vlib_buffer_t * b0,
1094 ip4_header_t * ip0, ip4_address_t * addr,
1095 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1096 void *d, void *e, u8 * dont_translate);
1097 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1098 u32 thread_index, vlib_buffer_t * b0,
1099 ip4_header_t * ip0, ip4_address_t * addr,
1100 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1101 void *d, void *e, u8 * dont_translate);
1103 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1104 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1105 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1108 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1109 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1110 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1113 /* hairpinning functions */
1114 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1115 ip4_header_t * ip0, icmp46_header_t * icmp0,
1117 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1119 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1121 int snat_hairpinning (vlib_main_t * vm, vlib_node_runtime_t * node,
1122 snat_main_t * sm, vlib_buffer_t * b0,
1123 ip4_header_t * ip0, udp_header_t * udp0,
1124 tcp_header_t * tcp0, u32 proto0, int is_ed,
1127 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1128 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1129 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1130 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1131 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1135 * @brief Enable NAT44 plugin
1137 * @param c nat44_config_t
1139 * @return 0 on success, non-zero value otherwise
1141 int nat44_plugin_enable (nat44_config_t c);
1144 * @brief Disable NAT44 plugin
1146 * @return 0 on success, non-zero value otherwise
1148 int nat44_plugin_disable ();
1151 * @brief Add external address to NAT44 pool
1153 * @param sm snat global configuration data
1154 * @param addr IPv4 address
1155 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1156 * @param twice_nat 1 if twice NAT address
1158 * @return 0 on success, non-zero value otherwise
1160 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1164 * @brief Delete external address from NAT44 pool
1166 * @param sm snat global configuration data
1167 * @param addr IPv4 address
1168 * @param delete_sm 1 if delete static mapping using address
1169 * @param twice_nat 1 if twice NAT address
1171 * @return 0 on success, non-zero value otherwise
1173 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1177 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1179 * @param addr IPv4 address
1180 * @param is_add 1 = add, 0 = delete
1182 * @return 0 on success, non-zero value otherwise
1184 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1187 * @brief Add/delete NAT44 static mapping
1189 * @param l_addr local IPv4 address
1190 * @param e_addr external IPv4 address
1191 * @param l_port local port number
1192 * @param e_port external port number
1193 * @param vrf_id local VRF ID
1194 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1195 * @param sw_if_index use interface address as external IPv4 address
1196 * @param proto L4 protocol
1197 * @param is_add 1 = add, 0 = delete
1198 * @param twice_nat twice-nat mode
1199 * @param out2in_only if 1 rule match only out2in direction
1200 * @param tag opaque string tag
1201 * @param identity_nat identity NAT
1202 * @param pool_addr pool IPv4 address
1203 * @param exact 1 = exact pool address
1205 * @return 0 on success, non-zero value otherwise
1207 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1208 u16 l_port, u16 e_port, u32 vrf_id,
1209 int addr_only, u32 sw_if_index,
1210 nat_protocol_t proto, int is_add,
1211 twice_nat_type_t twice_nat, u8 out2in_only,
1212 u8 * tag, u8 identity_nat,
1213 ip4_address_t pool_addr, int exact);
1216 * @brief Add/delete static mapping with load-balancing (multiple backends)
1218 * @param e_addr external IPv4 address
1219 * @param e_port external port number
1220 * @param proto L4 protocol
1221 * @param locals list of local backends
1222 * @param is_add 1 = add, 0 = delete
1223 * @param twice_nat twice-nat mode
1224 * @param out2in_only if 1 rule match only out2in direction
1225 * @param tag opaque string tag
1226 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1228 * @return 0 on success, non-zero value otherwise
1230 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1231 nat_protocol_t proto,
1232 nat44_lb_addr_port_t * locals, u8 is_add,
1233 twice_nat_type_t twice_nat,
1234 u8 out2in_only, u8 * tag, u32 affinity);
1236 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1237 ip4_address_t l_addr, u16 l_port,
1238 nat_protocol_t proto, u32 vrf_id,
1239 u8 probability, u8 is_add);
1241 clib_error_t *nat44_api_hookup (vlib_main_t * vm);
1244 * @brief Set NAT plugin workers
1246 * @param bitmap NAT workers bitmap
1248 * @return 0 on success, non-zero value otherwise
1250 int snat_set_workers (uword * bitmap);
1253 * @brief Enable/disable NAT44 feature on the interface
1255 * @param sw_if_index software index of the interface
1256 * @param is_inside 1 = inside, 0 = outside
1257 * @param is_del 1 = delete, 0 = add
1259 * @return 0 on success, non-zero value otherwise
1261 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1264 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1266 * @param sw_if_index software index of the interface
1267 * @param is_inside 1 = inside, 0 = outside
1268 * @param is_del 1 = delete, 0 = add
1270 * @return 0 on success, non-zero value otherwise
1272 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1276 * @brief Add/delete NAT44 pool address from specific interface
1278 * @param sw_if_index software index of the interface
1279 * @param is_del 1 = delete, 0 = add
1280 * @param twice_nat 1 = twice NAT address for external hosts
1282 * @return 0 on success, non-zero value otherwise
1284 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1288 * @brief Delete NAT44 session
1290 * @param addr IPv4 address
1291 * @param port L4 port number
1292 * @param proto L4 protocol
1293 * @param vrf_id VRF ID
1294 * @param is_in 1 = inside network address and port pair, 0 = outside
1296 * @return 0 on success, non-zero value otherwise
1298 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1299 nat_protocol_t proto, u32 vrf_id, int is_in);
1302 * @brief Delete NAT44 endpoint-dependent session
1304 * @param sm snat global configuration data
1305 * @param addr IPv4 address
1306 * @param port L4 port number
1307 * @param proto L4 protocol
1308 * @param vrf_id VRF ID
1309 * @param is_in 1 = inside network address and port pair, 0 = outside
1311 * @return 0 on success, non-zero value otherwise
1313 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1314 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1315 u32 vrf_id, int is_in);
1318 * @brief Free NAT44 session data (lookup keys, external address port)
1320 * @param sm snat global configuration data
1321 * @param s NAT session
1322 * @param thread_index thread index
1323 * @param is_ha is HA event
1325 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1326 u32 thread_index, u8 is_ha);
1329 * @brief Set NAT44 session limit (session limit, vrf id)
1331 * @param session_limit Session limit
1332 * @param vrf_id VRF id
1333 * @return 0 on success, non-zero value otherwise
1335 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1338 * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
1340 * @param session_limit Session limit
1341 * @param vrf_id VRF id
1342 * @return 0 on success, non-zero value otherwise
1344 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
1347 * @brief Initialize NAT44 data
1349 * @param tsm per thread data
1351 void nat44_db_init (snat_main_per_thread_data_t * tsm);
1354 * @brief Free NAT44 data
1356 * @param tsm per thread data
1358 void nat44_db_free (snat_main_per_thread_data_t * tsm);
1361 * @brief Delete specific NAT44 EI user and his sessions
1363 * @param addr IPv4 address
1364 * @param fib_index FIB table index
1366 int nat44_ei_user_del (ip4_address_t * addr, u32 fib_index);
1369 * @brief Free all NAT44 sessions
1371 void nat44_sessions_clear ();
1374 * @brief Find or create NAT user
1376 * @param sm snat global configuration data
1377 * @param addr IPv4 address
1378 * @param fib_index FIB table index
1379 * @param thread_index thread index
1381 * @return NAT user data structure on success otherwise zero value
1383 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1384 ip4_address_t * addr, u32 fib_index,
1388 * @brief Allocate new NAT session or recycle last used
1390 * @param sm snat global configuration data
1392 * @param thread_index thread index
1393 * @param now time now
1395 * @return session data structure on success otherwise zero value
1397 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1399 u32 thread_index, f64 now);
1402 * @brief Set address and port assignment algorithm for MAP-E CE
1404 * @param psid Port Set Identifier value
1405 * @param psid_offset number of offset bits
1406 * @param psid_length length of PSID
1408 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1412 * @brief Set address and port assignment algorithm for port range
1414 * @param start_port beginning of the port range
1415 * @param end_port end of the port range
1417 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1420 * @brief Set address and port assignment algorithm to default/standard
1422 void nat_set_alloc_addr_and_port_default (void);
1425 * @brief Free outside address and port pair
1427 * @param addresses vector of outside addresses
1428 * @param thread_index thread index
1429 * @param key address, port and protocol
1432 snat_free_outside_address_and_port (snat_address_t * addresses,
1434 ip4_address_t * addr,
1435 u16 port, nat_protocol_t protocol);
1438 * @brief Alloc outside address and port
1440 * @param addresses vector of outside addresses
1441 * @param fib_index FIB table index
1442 * @param thread_index thread index
1443 * @param port_per_thread number of ports per thread
1444 * @param snat_thread_index NAT thread index
1446 * @return 0 on success, non-zero value otherwise
1448 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1451 nat_protocol_t proto,
1452 ip4_address_t * addr,
1454 u16 port_per_thread,
1455 u32 snat_thread_index);
1457 void expire_per_vrf_sessions (u32 fib_index);
1460 * @brief Match NAT44 static mapping.
1462 * @param key address and port to match
1463 * @param addr external/local address of the matched mapping
1464 * @param port port of the matched mapping
1465 * @param fib_index fib index of the matched mapping
1466 * @param by_external if 0 match by local address otherwise match by external
1468 * @param is_addr_only 1 if matched mapping is address only
1469 * @param twice_nat matched mapping is twice NAT type
1470 * @param lb 1 if matched mapping is load-balanced
1471 * @param ext_host_addr external host address
1472 * @param is_identity_nat 1 if indentity mapping
1473 * @param out if !=0 set to pointer of the mapping structure
1475 * @returns 0 if match found otherwise 1.
1477 int snat_static_mapping_match (snat_main_t * sm,
1478 ip4_address_t match_addr,
1480 u32 match_fib_index,
1481 nat_protocol_t match_protocol,
1482 ip4_address_t * mapping_addr,
1484 u32 * mapping_fib_index,
1487 twice_nat_type_t * twice_nat,
1489 ip4_address_t * ext_host_addr,
1490 u8 * is_identity_nat,
1491 snat_static_mapping_t ** out);
1494 * @brief Add/del NAT address to FIB.
1496 * Add the external NAT address to the FIB as receive entries. This ensures
1497 * that VPP will reply to ARP for this address and we don't need to enable
1498 * proxy ARP on the outside interface.
1500 * @param addr IPv4 address
1501 * @param plen address prefix length
1502 * @param sw_if_index software index of the outside interface
1503 * @param is_add 0 = delete, 1 = add.
1505 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1506 u8 p_len, u32 sw_if_index, int is_add);
1509 nat_ha_sadd_cb (ip4_address_t * in_addr, u16 in_port,
1510 ip4_address_t * out_addr, u16 out_port,
1511 ip4_address_t * eh_addr, u16 eh_port,
1512 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
1513 u32 fib_index, u16 flags, u32 thread_index);
1516 nat_ha_sdel_cb (ip4_address_t * out_addr, u16 out_port,
1517 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
1521 nat_ha_sref_cb (ip4_address_t * out_addr, u16 out_port,
1522 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
1523 u32 total_pkts, u64 total_bytes, u32 thread_index);
1526 nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
1527 ip4_address_t * out_addr, u16 out_port,
1528 ip4_address_t * eh_addr, u16 eh_port,
1529 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
1530 u32 fib_index, u16 flags, u32 thread_index);
1533 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
1534 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1535 u32 fib_index, u32 ti);
1538 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
1539 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1540 u32 fib_index, u32 ti);
1543 nat_ha_sref_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 total_pkts, u64 total_bytes,
1549 * Why is this here? Because we don't need to touch this layer to
1550 * simply reply to an icmp. We need to change id to a unique
1551 * value to NAT an echo request/reply.
1558 } icmp_echo_header_t;
1562 u16 src_port, dst_port;
1565 u8 *format_user_kvp (u8 * s, va_list * args);
1567 #endif /* __included_nat_h__ */
1569 * fd.io coding-style-patch-verification: ON
1572 * eval: (c-set-style "gnu")