2 * Copyright (c) 2016 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)
55 NAT_NEXT_IN2OUT_ED_FAST_PATH,
56 NAT_NEXT_IN2OUT_ED_SLOW_PATH,
57 NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
58 NAT_NEXT_OUT2IN_ED_FAST_PATH,
59 NAT_NEXT_OUT2IN_ED_SLOW_PATH,
60 NAT_NEXT_IN2OUT_CLASSIFY,
61 NAT_NEXT_OUT2IN_CLASSIFY,
71 /* user (internal host) key */
85 /* NAT API Configuration flags */
86 #define foreach_nat_config_flag \
87 _(0x01, IS_TWICE_NAT) \
88 _(0x02, IS_SELF_TWICE_NAT) \
89 _(0x04, IS_OUT2IN_ONLY) \
90 _(0x08, IS_ADDR_ONLY) \
94 _(0x80, IS_EXT_HOST_VALID) \
96 typedef enum nat_config_flags_t_
98 #define _(n,f) NAT_API_##f = n,
99 foreach_nat_config_flag
101 } nat_config_flags_t;
103 /* External address and port allocation modes */
104 #define foreach_nat_addr_and_port_alloc_alg \
105 _(0, DEFAULT, "default") \
106 _(1, MAPE, "map-e") \
107 _(2, RANGE, "port-range")
111 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
112 foreach_nat_addr_and_port_alloc_alg
114 } nat_addr_and_port_alloc_alg_t;
117 #define foreach_snat_session_state \
118 _(0, UNKNOWN, "unknown") \
119 _(1, UDP_ACTIVE, "udp-active") \
120 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
121 _(3, TCP_ESTABLISHED, "tcp-established") \
122 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
123 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
124 _(6, TCP_CLOSING, "tcp-closing") \
125 _(7, TCP_LAST_ACK, "tcp-last-ack") \
126 _(8, TCP_CLOSED, "tcp-closed") \
127 _(9, ICMP_ACTIVE, "icmp-active")
131 #define _(v, N, s) SNAT_SESSION_##N = v,
132 foreach_snat_session_state
134 } snat_session_state_t;
136 #define foreach_nat_in2out_ed_error \
137 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
138 _(OUT_OF_PORTS, "out of ports") \
139 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
140 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
141 _(NON_SYN, "non-SYN packet try to create session") \
142 _(TCP_CLOSED, "drops due to TCP in transitory timeout")
146 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
147 foreach_nat_in2out_ed_error
149 NAT_IN2OUT_ED_N_ERROR,
150 } nat_in2out_ed_error_t;
152 #define foreach_nat_out2in_ed_error \
153 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
154 _(OUT_OF_PORTS, "out of ports") \
155 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
156 _(NO_TRANSLATION, "no translation") \
157 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
158 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
159 _(CANNOT_CREATE_USER, "cannot create NAT user") \
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_OUT2IN_ED_ERROR_##sym,
166 foreach_nat_out2in_ed_error
168 NAT_OUT2IN_ED_N_ERROR,
169 } nat_out2in_ed_error_t;
172 /* Endpoint dependent TCP session state */
173 #define NAT44_SES_I2O_FIN 1
174 #define NAT44_SES_O2I_FIN 2
175 #define NAT44_SES_I2O_FIN_ACK 4
176 #define NAT44_SES_O2I_FIN_ACK 8
177 #define NAT44_SES_I2O_SYN 16
178 #define NAT44_SES_O2I_SYN 32
179 #define NAT44_SES_RST 64
182 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
183 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
184 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
185 #define SNAT_SESSION_FLAG_TWICE_NAT 8
186 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
187 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
188 #define SNAT_SESSION_FLAG_AFFINITY 64
189 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
191 /* NAT interface flags */
192 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
193 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
195 /* Static mapping flags */
196 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
197 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
198 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
199 #define NAT_STATIC_MAPPING_FLAG_LB 8
202 typedef CLIB_PACKED(struct
204 // number of sessions in this vrf
210 // is this vrf expired
212 }) per_vrf_sessions_t;
216 typedef CLIB_PACKED(struct
218 /* Outside network tuple */
226 /* Inside network tuple */
234 nat_protocol_t nat_proto;
239 /* Per-user translations */
241 u32 per_user_list_head_index;
243 /* head of LRU list in which this session is tracked */
245 /* index in global LRU list */
249 /* Last heard timer */
252 /* Last HA refresh */
253 f64 ha_last_refreshed;
259 /* External host address and port */
260 ip4_address_t ext_host_addr;
263 /* External host address and port after translation */
264 ip4_address_t ext_host_nat_addr;
265 u16 ext_host_nat_port;
267 /* TCP session state */
271 u64 tcp_closed_timestamp;
276 /* per vrf sessions index */
277 u32 per_vrf_sessions_index;
286 u32 sessions_per_user_list_head_index;
296 #define _(N, i, n, s) \
297 u16 busy_##n##_ports; \
298 u16 * busy_##n##_ports_per_thread; \
299 u32 busy_##n##_port_refcounts[65535];
319 /* backend IP address */
321 /* backend port number */
323 /* probability of the backend to be randomly matched */
326 /* backend FIB table */
329 } nat44_lb_addr_port_t;
333 /* twice-nat disabled */
335 /* twice-nat enabled */
337 /* twice-nat only when src IP equals dst IP after translation */
343 /* no load-balancing */
347 /* load-balancing with affinity */
353 /* local IP address */
354 ip4_address_t local_addr;
355 /* external IP address */
356 ip4_address_t external_addr;
362 twice_nat_type_t twice_nat;
363 /* local FIB table */
367 nat_protocol_t proto;
368 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
370 /* worker threads used by backends/local host */
372 /* opaque string tag */
374 /* backends for load-balancing mode */
375 nat44_lb_addr_port_t *locals;
376 /* affinity per service lis */
377 u32 affinity_per_service_list_head_index;
380 } snat_static_mapping_t;
390 ip4_address_t l_addr;
395 nat_protocol_t proto;
403 } snat_static_map_resolve_t;
407 /* Main lookup tables */
408 clib_bihash_8_8_t out2in;
409 clib_bihash_8_8_t in2out;
411 /* Endpoint dependent sessions lookup tables */
412 clib_bihash_16_8_t in2out_ed;
414 /* Find-a-user => src address lookup */
415 clib_bihash_8_8_t user_hash;
421 snat_session_t *sessions;
423 /* Pool of doubly-linked list elements */
424 dlist_elt_t *list_pool;
426 /* LRU session list - head is stale, tail is fresh */
427 dlist_elt_t *lru_pool;
428 u32 tcp_trans_lru_head_index;
429 u32 tcp_estab_lru_head_index;
430 u32 udp_lru_head_index;
431 u32 icmp_lru_head_index;
432 u32 unk_proto_lru_head_index;
434 /* NAT thread index */
435 u32 snat_thread_index;
437 /* real thread index */
440 per_vrf_sessions_t *per_vrf_sessions_vec;
442 } snat_main_per_thread_data_t;
446 /* ICMP session match function */
447 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
448 vlib_node_runtime_t * node,
452 ip4_address_t * addr,
455 nat_protocol_t * proto,
457 u8 * dont_translate);
459 /* Return worker thread index for given packet */
460 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
464 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
469 /* NAT address and port allocation function */
470 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
474 nat_protocol_t proto,
475 ip4_address_t * addr,
478 u32 snat_thread_index);
480 #define foreach_nat_counter _ (tcp) _ (udp) _ (icmp) _ (other) _ (drops)
482 typedef struct snat_main_s
484 /* ICMP session match functions */
485 snat_icmp_match_function_t *icmp_match_in2out_cb;
486 snat_icmp_match_function_t *icmp_match_out2in_cb;
488 /* Thread settings */
490 u32 first_worker_index;
492 snat_get_worker_in2out_function_t *worker_in2out_cb;
493 snat_get_worker_out2in_function_t *worker_out2in_cb;
496 /* Per thread data */
497 snat_main_per_thread_data_t *per_thread_data;
499 /* Find a static mapping by local */
500 clib_bihash_8_8_t static_mapping_by_local;
502 /* Find a static mapping by external */
503 clib_bihash_8_8_t static_mapping_by_external;
505 /* Static mapping pool */
506 snat_static_mapping_t *static_mappings;
508 /* Endpoint-dependent out2in mappings */
509 clib_bihash_16_8_t out2in_ed;
512 snat_interface_t *interfaces;
513 snat_interface_t *output_feature_interfaces;
515 /* Vector of outside addresses */
516 snat_address_t *addresses;
517 /* Address and port allocation function */
518 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
519 /* Address and port allocation type */
520 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
521 /* Port set parameters (MAP-E) */
525 /* Port range parameters */
532 /* vector of outside fibs */
533 nat_outside_fib_t *outside_fibs;
535 /* Vector of twice NAT addresses for external hosts */
536 snat_address_t *twice_nat_addresses;
538 /* sw_if_indices whose intfc addresses should be auto-added */
539 u32 *auto_add_sw_if_indices;
540 u32 *auto_add_sw_if_indices_twice_nat;
542 /* vector of interface address static mappings to resolve. */
543 snat_static_map_resolve_t *to_resolve;
545 /* Randomize port allocation order */
548 /* Worker handoff frame-queue index */
550 u32 fq_in2out_output_index;
554 u32 error_node_index;
556 /* handoff fq nodes */
557 u32 handoff_out2in_index;
558 u32 handoff_in2out_index;
559 u32 handoff_in2out_output_index;
561 /* respect feature arc nodes */
562 u32 pre_out2in_node_index;
563 u32 pre_in2out_node_index;
565 u32 in2out_node_index;
566 u32 in2out_output_node_index;
567 u32 in2out_fast_node_index;
568 u32 in2out_slowpath_node_index;
569 u32 in2out_slowpath_output_node_index;
570 u32 ed_in2out_node_index;
571 u32 ed_in2out_slowpath_node_index;
572 u32 out2in_node_index;
573 u32 out2in_fast_node_index;
574 u32 ed_out2in_node_index;
575 u32 ed_out2in_slowpath_node_index;
577 u32 hairpinning_node_index;
578 u32 hairpin_dst_node_index;
579 u32 hairpin_src_node_index;
580 u32 ed_hairpinning_node_index;
581 u32 ed_hairpin_dst_node_index;
582 u32 ed_hairpin_src_node_index;
584 /* If forwarding is enabled */
585 u8 forwarding_enabled;
587 /* Config parameters */
588 u8 static_mapping_only;
589 u8 static_mapping_connection_tracking;
591 u8 endpoint_dependent;
593 /* Is translation memory size calculated or user defined */
594 u8 translation_memory_size_set;
596 u32 translation_buckets;
597 uword translation_memory_size;
598 u32 max_translations_per_thread;
599 u32 *max_translations_per_fib;
600 u32 max_users_per_thread;
602 uword user_memory_size;
603 u32 max_translations_per_user;
606 u32 outside_fib_index;
608 u32 inside_fib_index;
610 /* values of various timeouts */
613 u32 tcp_transitory_timeout;
614 u32 tcp_established_timeout;
617 /* TCP MSS clamping */
620 /* counters/gauges */
621 vlib_simple_counter_main_t total_users;
622 vlib_simple_counter_main_t total_sessions;
623 vlib_simple_counter_main_t user_limit_reached;
625 #define _(x) vlib_simple_counter_main_t x;
674 vlib_simple_counter_main_t hairpinning;
678 /* API message ID base */
682 vlib_log_class_t log_class;
687 vnet_main_t *vnet_main;
688 ip4_main_t *ip4_main;
689 ip_lookup_main_t *ip4_lookup_main;
690 api_main_t *api_main;
697 } nat44_is_idle_session_ctx_t;
701 u32 cached_sw_if_index;
702 u32 cached_ip4_address;
705 extern snat_main_t snat_main;
707 // nat pre ed next_node feature classification
708 extern vlib_node_registration_t nat_default_node;
709 extern vlib_node_registration_t nat_pre_in2out_node;
710 extern vlib_node_registration_t nat_pre_out2in_node;
712 extern vlib_node_registration_t snat_in2out_node;
713 extern vlib_node_registration_t snat_in2out_output_node;
714 extern vlib_node_registration_t snat_out2in_node;
715 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
716 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
717 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
718 extern vlib_node_registration_t nat44_ed_in2out_node;
719 extern vlib_node_registration_t nat44_ed_in2out_output_node;
720 extern vlib_node_registration_t nat44_ed_out2in_node;
722 extern fib_source_t nat_fib_src_hi;
723 extern fib_source_t nat_fib_src_low;
725 /* format functions */
726 format_function_t format_snat_user;
727 format_function_t format_snat_static_mapping;
728 format_function_t format_snat_static_map_to_resolve;
729 format_function_t format_snat_session;
730 format_function_t format_snat_key;
731 format_function_t format_static_mapping_key;
732 format_function_t format_nat_protocol;
733 format_function_t format_nat_addr_and_port_alloc_alg;
734 /* unformat functions */
735 unformat_function_t unformat_nat_protocol;
737 /** \brief Check if SNAT session is created from static mapping.
738 @param s SNAT session
739 @return 1 if SNAT session is created from static mapping otherwise 0
741 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
743 /** \brief Check if SNAT session for unknown protocol.
744 @param s SNAT session
745 @return 1 if SNAT session for unknown protocol otherwise 0
747 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
749 /** \brief Check if NAT session is twice NAT.
751 @return 1 if NAT session is twice NAT
753 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
755 /** \brief Check if NAT session is load-balancing.
757 @return 1 if NAT session is load-balancing
759 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
761 /** \brief Check if NAT session is forwarding bypass.
763 @return 1 if NAT session is load-balancing
765 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
767 /** \brief Check if NAT session is endpoint dependent.
769 @return 1 if NAT session is endpoint dependent
771 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
773 /** \brief Check if NAT session has affinity record.
775 @return 1 if NAT session has affinity record
777 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
779 /** \brief Check if NAT interface is inside.
780 @param i NAT interface
781 @return 1 if inside interface
783 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
785 /** \brief Check if NAT interface is outside.
786 @param i NAT interface
787 @return 1 if outside interface
789 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
791 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
793 @return 1 if session is closed
795 #define nat44_is_ses_closed(s) s->state == 0xf
797 /** \brief Check if NAT static mapping is address only (1:1NAT).
798 @param sm NAT static mapping
799 @return 1 if 1:1NAT, 0 if 1:1NAPT
801 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
803 /** \brief Check if NAT static mapping match only out2in direction.
804 @param sm NAT static mapping
805 @return 1 if rule match only out2in direction
807 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
809 /** \brief Check if NAT static mapping is identity NAT.
810 @param sm NAT static mapping
811 @return 1 if identity NAT
813 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
815 /** \brief Check if NAT static mapping is load-balancing.
816 @param sm NAT static mapping
817 @return 1 if load-balancing
819 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
821 /** \brief Check if client initiating TCP connection (received SYN from client)
823 @return 1 if client initiating TCP connection
826 tcp_flags_is_init (u8 f)
828 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
832 #define nat_log_err(...) \
833 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
834 #define nat_log_warn(...) \
835 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
836 #define nat_log_notice(...) \
837 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
838 #define nat_log_info(...) \
839 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
840 #define nat_log_debug(...)\
841 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
843 /* NAT API Logging Levels */
844 #define foreach_nat_log_level \
847 _(0x02, LOG_WARNING) \
848 _(0x03, LOG_NOTICE) \
852 typedef enum nat_log_level_t_
854 #define _(n,f) SNAT_##f = n,
855 foreach_nat_log_level
859 #define nat_elog(_level, _str) \
862 snat_main_t *sm = &snat_main; \
863 if (PREDICT_FALSE (sm->log_level >= _level)) \
865 ELOG_TYPE_DECLARE (e) = \
867 .format = "nat-msg " _str, \
870 ELOG_DATA (&vlib_global_main.elog_main, e); \
874 #define nat_elog_addr(_level, _str, _addr) \
877 if (PREDICT_FALSE (sm->log_level >= _level)) \
879 ELOG_TYPE_DECLARE (e) = \
881 .format = "nat-msg " _str " %d.%d.%d.%d", \
882 .format_args = "i1i1i1i1", \
891 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
892 ed->oct4 = _addr >> 24; \
893 ed->oct3 = _addr >> 16; \
894 ed->oct2 = _addr >> 8; \
899 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
902 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
904 ELOG_TYPE_DECLARE (e) = \
906 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
907 " tid from: %d to: %d fib: %d", \
908 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
924 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
925 ed->src_oct1 = _src >> 24; \
926 ed->src_oct2 = _src >> 16; \
927 ed->src_oct3 = _src >> 8; \
928 ed->src_oct4 = _src; \
929 ed->dst_oct1 = _dst >> 24; \
930 ed->dst_oct2 = _dst >> 16; \
931 ed->dst_oct3 = _dst >> 8; \
932 ed->dst_oct4 = _dst; \
933 ed->ftid = vlib_get_thread_index (); \
939 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
942 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
944 ELOG_TYPE_DECLARE (e) = \
946 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
947 " tid:%d prt:%d fib:%d", \
948 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
964 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
965 ed->src_oct1 = _src >> 24; \
966 ed->src_oct2 = _src >> 16; \
967 ed->src_oct3 = _src >> 8; \
968 ed->src_oct4 = _src; \
969 ed->dst_oct1 = _dst >> 24; \
970 ed->dst_oct2 = _dst >> 16; \
971 ed->dst_oct3 = _dst >> 8; \
972 ed->dst_oct4 = _dst; \
973 ed->tid = vlib_get_thread_index (); \
979 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
982 snat_main_t *sm = &snat_main; \
983 if (PREDICT_FALSE (sm->log_level >= _level)) \
985 ELOG_TYPE_DECLARE (e) = \
987 .format = "nat-msg " _fmt, \
988 .format_args = _arg, \
992 typeof (_val1) val1; \
994 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
999 #define nat_elog_notice(nat_elog_str) \
1000 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
1001 #define nat_elog_warn(nat_elog_str) \
1002 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1003 #define nat_elog_err(nat_elog_str) \
1004 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1005 #define nat_elog_debug(nat_elog_str) \
1006 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1007 #define nat_elog_info(nat_elog_str) \
1008 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1010 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1011 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1012 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1013 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1014 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1015 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1016 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1017 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1018 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1019 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1021 /* ICMP session match functions */
1022 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1023 u32 thread_index, vlib_buffer_t * b0,
1024 ip4_header_t * ip0, ip4_address_t * addr,
1025 u16 * port, u32 * fib_index,
1026 nat_protocol_t * proto, void *d, void *e,
1027 u8 * dont_translate);
1028 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1029 u32 thread_index, vlib_buffer_t * b0,
1030 ip4_header_t * ip0, ip4_address_t * addr,
1031 u16 * port, u32 * fib_index,
1032 nat_protocol_t * proto, void *d, void *e,
1033 u8 * dont_translate);
1034 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1035 u32 thread_index, vlib_buffer_t * b0,
1036 ip4_header_t * ip0, ip4_address_t * addr,
1037 u16 * port, u32 * fib_index,
1038 nat_protocol_t * proto, void *d, void *e,
1039 u8 * dont_translate);
1040 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1041 u32 thread_index, vlib_buffer_t * b0,
1042 ip4_header_t * ip0, ip4_address_t * addr,
1043 u16 * port, u32 * fib_index,
1044 nat_protocol_t * proto, void *d, void *e,
1045 u8 * dont_translate);
1047 /* ICMP endpoint-dependent session match functions */
1048 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1049 u32 thread_index, vlib_buffer_t * b0,
1050 ip4_header_t * ip0, ip4_address_t * addr,
1051 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1052 void *d, void *e, u8 * dont_translate);
1053 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1054 u32 thread_index, vlib_buffer_t * b0,
1055 ip4_header_t * ip0, ip4_address_t * addr,
1056 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1057 void *d, void *e, u8 * dont_translate);
1059 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1060 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1061 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1064 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1065 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1066 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1069 /* hairpinning functions */
1070 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1071 ip4_header_t * ip0, icmp46_header_t * icmp0,
1073 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1075 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1077 int snat_hairpinning (vlib_main_t * vm, vlib_node_runtime_t * node,
1078 snat_main_t * sm, vlib_buffer_t * b0,
1079 ip4_header_t * ip0, udp_header_t * udp0,
1080 tcp_header_t * tcp0, u32 proto0, int is_ed,
1083 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1084 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1085 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1086 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1087 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1090 * @brief Add external address to NAT44 pool
1092 * @param sm snat global configuration data
1093 * @param addr IPv4 address
1094 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1095 * @param twice_nat 1 if twice NAT address
1097 * @return 0 on success, non-zero value otherwise
1099 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1103 * @brief Delete external address from NAT44 pool
1105 * @param sm snat global configuration data
1106 * @param addr IPv4 address
1107 * @param delete_sm 1 if delete static mapping using address
1108 * @param twice_nat 1 if twice NAT address
1110 * @return 0 on success, non-zero value otherwise
1112 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1116 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1118 * @param addr IPv4 address
1119 * @param is_add 1 = add, 0 = delete
1121 * @return 0 on success, non-zero value otherwise
1123 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1126 * @brief Add/delete NAT44 static mapping
1128 * @param l_addr local IPv4 address
1129 * @param e_addr external IPv4 address
1130 * @param l_port local port number
1131 * @param e_port external port number
1132 * @param vrf_id local VRF ID
1133 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1134 * @param sw_if_index use interface address as external IPv4 address
1135 * @param proto L4 protocol
1136 * @param is_add 1 = add, 0 = delete
1137 * @param twice_nat twice-nat mode
1138 * @param out2in_only if 1 rule match only out2in direction
1139 * @param tag opaque string tag
1140 * @param identity_nat identity NAT
1142 * @return 0 on success, non-zero value otherwise
1144 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1145 u16 l_port, u16 e_port, u32 vrf_id,
1146 int addr_only, u32 sw_if_index,
1147 nat_protocol_t proto, int is_add,
1148 twice_nat_type_t twice_nat, u8 out2in_only,
1149 u8 * tag, u8 identity_nat);
1152 * @brief Add/delete static mapping with load-balancing (multiple backends)
1154 * @param e_addr external IPv4 address
1155 * @param e_port external port number
1156 * @param proto L4 protocol
1157 * @param locals list of local backends
1158 * @param is_add 1 = add, 0 = delete
1159 * @param twice_nat twice-nat mode
1160 * @param out2in_only if 1 rule match only out2in direction
1161 * @param tag opaque string tag
1162 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1164 * @return 0 on success, non-zero value otherwise
1166 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1167 nat_protocol_t proto,
1168 nat44_lb_addr_port_t * locals, u8 is_add,
1169 twice_nat_type_t twice_nat,
1170 u8 out2in_only, u8 * tag, u32 affinity);
1172 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1173 ip4_address_t l_addr, u16 l_port,
1174 nat_protocol_t proto, u32 vrf_id,
1175 u8 probability, u8 is_add);
1177 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1180 * @brief Set NAT plugin workers
1182 * @param bitmap NAT workers bitmap
1184 * @return 0 on success, non-zero value otherwise
1186 int snat_set_workers (uword * bitmap);
1189 * @brief Enable/disable NAT44 feature on the interface
1191 * @param sw_if_index software index of the interface
1192 * @param is_inside 1 = inside, 0 = outside
1193 * @param is_del 1 = delete, 0 = add
1195 * @return 0 on success, non-zero value otherwise
1197 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1200 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1202 * @param sw_if_index software index of the interface
1203 * @param is_inside 1 = inside, 0 = outside
1204 * @param is_del 1 = delete, 0 = add
1206 * @return 0 on success, non-zero value otherwise
1208 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1212 * @brief Add/delete NAT44 pool address from specific interface
1214 * @param sw_if_index software index of the interface
1215 * @param is_del 1 = delete, 0 = add
1216 * @param twice_nat 1 = twice NAT address for external hosts
1218 * @return 0 on success, non-zero value otherwise
1220 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1224 * @brief Delete NAT44 session
1226 * @param addr IPv4 address
1227 * @param port L4 port number
1228 * @param proto L4 protocol
1229 * @param vrf_id VRF ID
1230 * @param is_in 1 = inside network address and port pair, 0 = outside
1232 * @return 0 on success, non-zero value otherwise
1234 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1235 nat_protocol_t proto, u32 vrf_id, int is_in);
1238 * @brief Delete NAT44 endpoint-dependent session
1240 * @param sm snat global configuration data
1241 * @param addr IPv4 address
1242 * @param port L4 port number
1243 * @param proto L4 protocol
1244 * @param vrf_id VRF ID
1245 * @param is_in 1 = inside network address and port pair, 0 = outside
1247 * @return 0 on success, non-zero value otherwise
1249 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1250 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1251 u32 vrf_id, int is_in);
1254 * @brief Free NAT44 session data (lookup keys, external address port)
1256 * @param sm snat global configuration data
1257 * @param s NAT session
1258 * @param thread_index thread index
1259 * @param is_ha is HA event
1261 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1262 u32 thread_index, u8 is_ha);
1265 * @brief Set NAT44 session limit (session limit, vrf id)
1267 * @param session_limit Session limit
1268 * @param vrf_id VRF id
1269 * @return 0 on success, non-zero value otherwise
1271 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1274 * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
1276 * @param session_limit Session limit
1277 * @param vrf_id VRF id
1278 * @return 0 on success, non-zero value otherwise
1280 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
1282 * @brief Free NAT44 ED session data (lookup keys, external address port)
1284 * @param s NAT session
1285 * @param thread_index thread index
1286 * @param is_ha is HA event
1289 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1290 u32 thread_index, u8 is_ha);
1293 * @brief Initialize NAT44 data
1295 * @param tsm per thread data
1297 void nat44_db_init (snat_main_per_thread_data_t * tsm);
1300 * @brief Free NAT44 data
1302 * @param tsm per thread data
1304 void nat44_db_free (snat_main_per_thread_data_t * tsm);
1306 void nat44_sessions_clear ();
1309 * @brief Find or create NAT user
1311 * @param sm snat global configuration data
1312 * @param addr IPv4 address
1313 * @param fib_index FIB table index
1314 * @param thread_index thread index
1316 * @return NAT user data structure on success otherwise zero value
1318 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1319 ip4_address_t * addr, u32 fib_index,
1323 * @brief Allocate new NAT session or recycle last used
1325 * @param sm snat global configuration data
1327 * @param thread_index thread index
1328 * @param now time now
1330 * @return session data structure on success otherwise zero value
1332 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1334 u32 thread_index, f64 now);
1337 * @brief Set address and port assignment algorithm for MAP-E CE
1339 * @param psid Port Set Identifier value
1340 * @param psid_offset number of offset bits
1341 * @param psid_length length of PSID
1343 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1347 * @brief Set address and port assignment algorithm for port range
1349 * @param start_port beginning of the port range
1350 * @param end_port end of the port range
1352 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1355 * @brief Set address and port assignment algorithm to default/standard
1357 void nat_set_alloc_addr_and_port_default (void);
1360 * @brief Free outside address and port pair
1362 * @param addresses vector of outside addresses
1363 * @param thread_index thread index
1364 * @param key address, port and protocol
1367 snat_free_outside_address_and_port (snat_address_t * addresses,
1369 ip4_address_t * addr,
1370 u16 port, nat_protocol_t protocol);
1373 * @brief Alloc outside address and port
1375 * @param addresses vector of outside addresses
1376 * @param fib_index FIB table index
1377 * @param thread_index thread index
1378 * @param port_per_thread number of ports per thread
1379 * @param snat_thread_index NAT thread index
1381 * @return 0 on success, non-zero value otherwise
1383 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1386 nat_protocol_t proto,
1387 ip4_address_t * addr,
1389 u16 port_per_thread,
1390 u32 snat_thread_index);
1392 void expire_per_vrf_sessions (u32 fib_index);
1395 * @brief Match NAT44 static mapping.
1397 * @param key address and port to match
1398 * @param addr external/local address of the matched mapping
1399 * @param port port of the matched mapping
1400 * @param fib_index fib index of the matched mapping
1401 * @param by_external if 0 match by local address otherwise match by external
1403 * @param is_addr_only 1 if matched mapping is address only
1404 * @param twice_nat matched mapping is twice NAT type
1405 * @param lb 1 if matched mapping is load-balanced
1406 * @param ext_host_addr external host address
1408 * @returns 0 if match found otherwise 1.
1410 int snat_static_mapping_match (snat_main_t * sm,
1411 ip4_address_t match_addr,
1413 u32 match_fib_index,
1414 nat_protocol_t match_protocol,
1415 ip4_address_t * mapping_addr,
1417 u32 * mapping_fib_index,
1420 twice_nat_type_t * twice_nat,
1422 ip4_address_t * ext_host_addr,
1423 u8 * is_identity_nat);
1426 * @brief Add/del NAT address to FIB.
1428 * Add the external NAT address to the FIB as receive entries. This ensures
1429 * that VPP will reply to ARP for this address and we don't need to enable
1430 * proxy ARP on the outside interface.
1432 * @param addr IPv4 address
1433 * @param plen address prefix length
1434 * @param sw_if_index software index of the outside interface
1435 * @param is_add 0 = delete, 1 = add.
1437 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1438 u8 p_len, u32 sw_if_index, int is_add);
1441 * Why is this here? Because we don't need to touch this layer to
1442 * simply reply to an icmp. We need to change id to a unique
1443 * value to NAT an echo request/reply.
1450 } icmp_echo_header_t;
1454 u16 src_port, dst_port;
1457 u8 *format_user_kvp (u8 * s, va_list * args);
1459 #endif /* __included_nat_h__ */
1461 * fd.io coding-style-patch-verification: ON
1464 * eval: (c-set-style "gnu")