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 u32 translation_buckets;
594 uword translation_memory_size;
595 u32 max_translations_per_thread;
596 u32 *max_translations_per_fib;
597 u32 max_users_per_thread;
599 uword user_memory_size;
600 u32 max_translations_per_user;
603 u32 outside_fib_index;
605 u32 inside_fib_index;
607 /* values of various timeouts */
610 u32 tcp_transitory_timeout;
611 u32 tcp_established_timeout;
614 /* TCP MSS clamping */
617 /* counters/gauges */
618 vlib_simple_counter_main_t total_users;
619 vlib_simple_counter_main_t total_sessions;
620 vlib_simple_counter_main_t user_limit_reached;
622 #define _(x) vlib_simple_counter_main_t x;
671 vlib_simple_counter_main_t hairpinning;
675 /* API message ID base */
679 vlib_log_class_t log_class;
684 vnet_main_t *vnet_main;
685 ip4_main_t *ip4_main;
686 ip_lookup_main_t *ip4_lookup_main;
687 api_main_t *api_main;
694 } nat44_is_idle_session_ctx_t;
698 u32 cached_sw_if_index;
699 u32 cached_ip4_address;
702 extern snat_main_t snat_main;
704 // nat pre ed next_node feature classification
705 extern vlib_node_registration_t nat_default_node;
706 extern vlib_node_registration_t nat_pre_in2out_node;
707 extern vlib_node_registration_t nat_pre_out2in_node;
709 extern vlib_node_registration_t snat_in2out_node;
710 extern vlib_node_registration_t snat_in2out_output_node;
711 extern vlib_node_registration_t snat_out2in_node;
712 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
713 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
714 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
715 extern vlib_node_registration_t nat44_ed_in2out_node;
716 extern vlib_node_registration_t nat44_ed_in2out_output_node;
717 extern vlib_node_registration_t nat44_ed_out2in_node;
719 extern fib_source_t nat_fib_src_hi;
720 extern fib_source_t nat_fib_src_low;
722 /* format functions */
723 format_function_t format_snat_user;
724 format_function_t format_snat_static_mapping;
725 format_function_t format_snat_static_map_to_resolve;
726 format_function_t format_snat_session;
727 format_function_t format_snat_key;
728 format_function_t format_static_mapping_key;
729 format_function_t format_nat_protocol;
730 format_function_t format_nat_addr_and_port_alloc_alg;
731 /* unformat functions */
732 unformat_function_t unformat_nat_protocol;
734 /** \brief Check if SNAT session is created from static mapping.
735 @param s SNAT session
736 @return 1 if SNAT session is created from static mapping otherwise 0
738 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
740 /** \brief Check if SNAT session for unknown protocol.
741 @param s SNAT session
742 @return 1 if SNAT session for unknown protocol otherwise 0
744 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
746 /** \brief Check if NAT session is twice NAT.
748 @return 1 if NAT session is twice NAT
750 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
752 /** \brief Check if NAT session is load-balancing.
754 @return 1 if NAT session is load-balancing
756 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
758 /** \brief Check if NAT session is forwarding bypass.
760 @return 1 if NAT session is load-balancing
762 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
764 /** \brief Check if NAT session is endpoint dependent.
766 @return 1 if NAT session is endpoint dependent
768 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
770 /** \brief Check if NAT session has affinity record.
772 @return 1 if NAT session has affinity record
774 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
776 /** \brief Check if NAT interface is inside.
777 @param i NAT interface
778 @return 1 if inside interface
780 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
782 /** \brief Check if NAT interface is outside.
783 @param i NAT interface
784 @return 1 if outside interface
786 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
788 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
790 @return 1 if session is closed
792 #define nat44_is_ses_closed(s) s->state == 0xf
794 /** \brief Check if NAT static mapping is address only (1:1NAT).
795 @param sm NAT static mapping
796 @return 1 if 1:1NAT, 0 if 1:1NAPT
798 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
800 /** \brief Check if NAT static mapping match only out2in direction.
801 @param sm NAT static mapping
802 @return 1 if rule match only out2in direction
804 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
806 /** \brief Check if NAT static mapping is identity NAT.
807 @param sm NAT static mapping
808 @return 1 if identity NAT
810 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
812 /** \brief Check if NAT static mapping is load-balancing.
813 @param sm NAT static mapping
814 @return 1 if load-balancing
816 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
818 /** \brief Check if client initiating TCP connection (received SYN from client)
820 @return 1 if client initiating TCP connection
823 tcp_flags_is_init (u8 f)
825 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
829 #define nat_log_err(...) \
830 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
831 #define nat_log_warn(...) \
832 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
833 #define nat_log_notice(...) \
834 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
835 #define nat_log_info(...) \
836 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
837 #define nat_log_debug(...)\
838 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
840 /* NAT API Logging Levels */
841 #define foreach_nat_log_level \
844 _(0x02, LOG_WARNING) \
845 _(0x03, LOG_NOTICE) \
849 typedef enum nat_log_level_t_
851 #define _(n,f) SNAT_##f = n,
852 foreach_nat_log_level
856 #define nat_elog(_level, _str) \
859 snat_main_t *sm = &snat_main; \
860 if (PREDICT_FALSE (sm->log_level >= _level)) \
862 ELOG_TYPE_DECLARE (e) = \
864 .format = "nat-msg " _str, \
867 ELOG_DATA (&vlib_global_main.elog_main, e); \
871 #define nat_elog_addr(_level, _str, _addr) \
874 if (PREDICT_FALSE (sm->log_level >= _level)) \
876 ELOG_TYPE_DECLARE (e) = \
878 .format = "nat-msg " _str " %d.%d.%d.%d", \
879 .format_args = "i1i1i1i1", \
888 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
889 ed->oct4 = _addr >> 24; \
890 ed->oct3 = _addr >> 16; \
891 ed->oct2 = _addr >> 8; \
896 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
899 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
901 ELOG_TYPE_DECLARE (e) = \
903 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
904 " tid from: %d to: %d fib: %d", \
905 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
921 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
922 ed->src_oct1 = _src >> 24; \
923 ed->src_oct2 = _src >> 16; \
924 ed->src_oct3 = _src >> 8; \
925 ed->src_oct4 = _src; \
926 ed->dst_oct1 = _dst >> 24; \
927 ed->dst_oct2 = _dst >> 16; \
928 ed->dst_oct3 = _dst >> 8; \
929 ed->dst_oct4 = _dst; \
930 ed->ftid = vlib_get_thread_index (); \
936 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
939 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
941 ELOG_TYPE_DECLARE (e) = \
943 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
944 " tid:%d prt:%d fib:%d", \
945 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
961 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
962 ed->src_oct1 = _src >> 24; \
963 ed->src_oct2 = _src >> 16; \
964 ed->src_oct3 = _src >> 8; \
965 ed->src_oct4 = _src; \
966 ed->dst_oct1 = _dst >> 24; \
967 ed->dst_oct2 = _dst >> 16; \
968 ed->dst_oct3 = _dst >> 8; \
969 ed->dst_oct4 = _dst; \
970 ed->tid = vlib_get_thread_index (); \
976 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
979 snat_main_t *sm = &snat_main; \
980 if (PREDICT_FALSE (sm->log_level >= _level)) \
982 ELOG_TYPE_DECLARE (e) = \
984 .format = "nat-msg " _fmt, \
985 .format_args = _arg, \
989 typeof (_val1) val1; \
991 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
996 #define nat_elog_notice(nat_elog_str) \
997 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
998 #define nat_elog_warn(nat_elog_str) \
999 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1000 #define nat_elog_err(nat_elog_str) \
1001 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1002 #define nat_elog_debug(nat_elog_str) \
1003 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1004 #define nat_elog_info(nat_elog_str) \
1005 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1007 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1008 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1009 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1010 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1011 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1012 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1013 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1014 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1015 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1016 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1018 /* ICMP session match functions */
1019 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1020 u32 thread_index, vlib_buffer_t * b0,
1021 ip4_header_t * ip0, ip4_address_t * addr,
1022 u16 * port, u32 * fib_index,
1023 nat_protocol_t * proto, void *d, void *e,
1024 u8 * dont_translate);
1025 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1026 u32 thread_index, vlib_buffer_t * b0,
1027 ip4_header_t * ip0, ip4_address_t * addr,
1028 u16 * port, u32 * fib_index,
1029 nat_protocol_t * proto, void *d, void *e,
1030 u8 * dont_translate);
1031 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1032 u32 thread_index, vlib_buffer_t * b0,
1033 ip4_header_t * ip0, ip4_address_t * addr,
1034 u16 * port, u32 * fib_index,
1035 nat_protocol_t * proto, void *d, void *e,
1036 u8 * dont_translate);
1037 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1038 u32 thread_index, vlib_buffer_t * b0,
1039 ip4_header_t * ip0, ip4_address_t * addr,
1040 u16 * port, u32 * fib_index,
1041 nat_protocol_t * proto, void *d, void *e,
1042 u8 * dont_translate);
1044 /* ICMP endpoint-dependent session match functions */
1045 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1046 u32 thread_index, vlib_buffer_t * b0,
1047 ip4_header_t * ip0, ip4_address_t * addr,
1048 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1049 void *d, void *e, u8 * dont_translate);
1050 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1051 u32 thread_index, vlib_buffer_t * b0,
1052 ip4_header_t * ip0, ip4_address_t * addr,
1053 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1054 void *d, void *e, u8 * dont_translate);
1056 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1057 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1058 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1061 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1062 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1063 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1066 /* hairpinning functions */
1067 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1068 ip4_header_t * ip0, icmp46_header_t * icmp0,
1070 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1072 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1074 int snat_hairpinning (vlib_main_t * vm, vlib_node_runtime_t * node,
1075 snat_main_t * sm, vlib_buffer_t * b0,
1076 ip4_header_t * ip0, udp_header_t * udp0,
1077 tcp_header_t * tcp0, u32 proto0, int is_ed,
1080 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1081 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1082 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1083 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1084 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1087 * @brief Add external address to NAT44 pool
1089 * @param sm snat global configuration data
1090 * @param addr IPv4 address
1091 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1092 * @param twice_nat 1 if twice NAT address
1094 * @return 0 on success, non-zero value otherwise
1096 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1100 * @brief Delete external address from NAT44 pool
1102 * @param sm snat global configuration data
1103 * @param addr IPv4 address
1104 * @param delete_sm 1 if delete static mapping using address
1105 * @param twice_nat 1 if twice NAT address
1107 * @return 0 on success, non-zero value otherwise
1109 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1113 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1115 * @param addr IPv4 address
1116 * @param is_add 1 = add, 0 = delete
1118 * @return 0 on success, non-zero value otherwise
1120 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1123 * @brief Add/delete NAT44 static mapping
1125 * @param l_addr local IPv4 address
1126 * @param e_addr external IPv4 address
1127 * @param l_port local port number
1128 * @param e_port external port number
1129 * @param vrf_id local VRF ID
1130 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1131 * @param sw_if_index use interface address as external IPv4 address
1132 * @param proto L4 protocol
1133 * @param is_add 1 = add, 0 = delete
1134 * @param twice_nat twice-nat mode
1135 * @param out2in_only if 1 rule match only out2in direction
1136 * @param tag opaque string tag
1137 * @param identity_nat identity NAT
1139 * @return 0 on success, non-zero value otherwise
1141 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1142 u16 l_port, u16 e_port, u32 vrf_id,
1143 int addr_only, u32 sw_if_index,
1144 nat_protocol_t proto, int is_add,
1145 twice_nat_type_t twice_nat, u8 out2in_only,
1146 u8 * tag, u8 identity_nat);
1149 * @brief Add/delete static mapping with load-balancing (multiple backends)
1151 * @param e_addr external IPv4 address
1152 * @param e_port external port number
1153 * @param proto L4 protocol
1154 * @param locals list of local backends
1155 * @param is_add 1 = add, 0 = delete
1156 * @param twice_nat twice-nat mode
1157 * @param out2in_only if 1 rule match only out2in direction
1158 * @param tag opaque string tag
1159 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1161 * @return 0 on success, non-zero value otherwise
1163 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1164 nat_protocol_t proto,
1165 nat44_lb_addr_port_t * locals, u8 is_add,
1166 twice_nat_type_t twice_nat,
1167 u8 out2in_only, u8 * tag, u32 affinity);
1169 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1170 ip4_address_t l_addr, u16 l_port,
1171 nat_protocol_t proto, u32 vrf_id,
1172 u8 probability, u8 is_add);
1174 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1177 * @brief Set NAT plugin workers
1179 * @param bitmap NAT workers bitmap
1181 * @return 0 on success, non-zero value otherwise
1183 int snat_set_workers (uword * bitmap);
1186 * @brief Enable/disable NAT44 feature on the interface
1188 * @param sw_if_index software index of the interface
1189 * @param is_inside 1 = inside, 0 = outside
1190 * @param is_del 1 = delete, 0 = add
1192 * @return 0 on success, non-zero value otherwise
1194 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1197 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1199 * @param sw_if_index software index of the interface
1200 * @param is_inside 1 = inside, 0 = outside
1201 * @param is_del 1 = delete, 0 = add
1203 * @return 0 on success, non-zero value otherwise
1205 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1209 * @brief Add/delete NAT44 pool address from specific interface
1211 * @param sw_if_index software index of the interface
1212 * @param is_del 1 = delete, 0 = add
1213 * @param twice_nat 1 = twice NAT address for external hosts
1215 * @return 0 on success, non-zero value otherwise
1217 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1221 * @brief Delete NAT44 session
1223 * @param addr IPv4 address
1224 * @param port L4 port number
1225 * @param proto L4 protocol
1226 * @param vrf_id VRF ID
1227 * @param is_in 1 = inside network address and port pair, 0 = outside
1229 * @return 0 on success, non-zero value otherwise
1231 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1232 nat_protocol_t proto, u32 vrf_id, int is_in);
1235 * @brief Delete NAT44 endpoint-dependent session
1237 * @param sm snat global configuration data
1238 * @param addr IPv4 address
1239 * @param port L4 port number
1240 * @param proto L4 protocol
1241 * @param vrf_id VRF ID
1242 * @param is_in 1 = inside network address and port pair, 0 = outside
1244 * @return 0 on success, non-zero value otherwise
1246 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1247 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1248 u32 vrf_id, int is_in);
1251 * @brief Free NAT44 session data (lookup keys, external address port)
1253 * @param sm snat global configuration data
1254 * @param s NAT session
1255 * @param thread_index thread index
1256 * @param is_ha is HA event
1258 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1259 u32 thread_index, u8 is_ha);
1262 * @brief Set NAT44 session limit (session limit, vrf id)
1264 * @param session_limit Session limit
1265 * @param vrf_id VRF id
1266 * @return 0 on success, non-zero value otherwise
1268 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1271 * @brief Free NAT44 ED session data (lookup keys, external address port)
1273 * @param s NAT session
1274 * @param thread_index thread index
1275 * @param is_ha is HA event
1278 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1279 u32 thread_index, u8 is_ha);
1282 * @brief Initialize NAT44 data
1284 * @param tsm per thread data
1286 void nat44_db_init (snat_main_per_thread_data_t * tsm);
1289 * @brief Free NAT44 data
1291 * @param tsm per thread data
1293 void nat44_db_free (snat_main_per_thread_data_t * tsm);
1295 void nat44_sessions_clear ();
1298 * @brief Find or create NAT user
1300 * @param sm snat global configuration data
1301 * @param addr IPv4 address
1302 * @param fib_index FIB table index
1303 * @param thread_index thread index
1305 * @return NAT user data structure on success otherwise zero value
1307 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1308 ip4_address_t * addr, u32 fib_index,
1312 * @brief Allocate new NAT session or recycle last used
1314 * @param sm snat global configuration data
1316 * @param thread_index thread index
1317 * @param now time now
1319 * @return session data structure on success otherwise zero value
1321 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1323 u32 thread_index, f64 now);
1326 * @brief Set address and port assignment algorithm for MAP-E CE
1328 * @param psid Port Set Identifier value
1329 * @param psid_offset number of offset bits
1330 * @param psid_length length of PSID
1332 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1336 * @brief Set address and port assignment algorithm for port range
1338 * @param start_port beginning of the port range
1339 * @param end_port end of the port range
1341 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1344 * @brief Set address and port assignment algorithm to default/standard
1346 void nat_set_alloc_addr_and_port_default (void);
1349 * @brief Free outside address and port pair
1351 * @param addresses vector of outside addresses
1352 * @param thread_index thread index
1353 * @param key address, port and protocol
1356 snat_free_outside_address_and_port (snat_address_t * addresses,
1358 ip4_address_t * addr,
1359 u16 port, nat_protocol_t protocol);
1362 * @brief Alloc outside address and port
1364 * @param addresses vector of outside addresses
1365 * @param fib_index FIB table index
1366 * @param thread_index thread index
1367 * @param port_per_thread number of ports per thread
1368 * @param snat_thread_index NAT thread index
1370 * @return 0 on success, non-zero value otherwise
1372 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1375 nat_protocol_t proto,
1376 ip4_address_t * addr,
1378 u16 port_per_thread,
1379 u32 snat_thread_index);
1381 void expire_per_vrf_sessions (u32 fib_index);
1384 * @brief Match NAT44 static mapping.
1386 * @param key address and port to match
1387 * @param addr external/local address of the matched mapping
1388 * @param port port of the matched mapping
1389 * @param fib_index fib index of the matched mapping
1390 * @param by_external if 0 match by local address otherwise match by external
1392 * @param is_addr_only 1 if matched mapping is address only
1393 * @param twice_nat matched mapping is twice NAT type
1394 * @param lb 1 if matched mapping is load-balanced
1395 * @param ext_host_addr external host address
1397 * @returns 0 if match found otherwise 1.
1399 int snat_static_mapping_match (snat_main_t * sm,
1400 ip4_address_t match_addr,
1402 u32 match_fib_index,
1403 nat_protocol_t match_protocol,
1404 ip4_address_t * mapping_addr,
1406 u32 * mapping_fib_index,
1409 twice_nat_type_t * twice_nat,
1411 ip4_address_t * ext_host_addr,
1412 u8 * is_identity_nat);
1415 * @brief Add/del NAT address to FIB.
1417 * Add the external NAT address to the FIB as receive entries. This ensures
1418 * that VPP will reply to ARP for this address and we don't need to enable
1419 * proxy ARP on the outside interface.
1421 * @param addr IPv4 address
1422 * @param plen address prefix length
1423 * @param sw_if_index software index of the outside interface
1424 * @param is_add 0 = delete, 1 = add.
1426 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1427 u8 p_len, u32 sw_if_index, int is_add);
1430 * Why is this here? Because we don't need to touch this layer to
1431 * simply reply to an icmp. We need to change id to a unique
1432 * value to NAT an echo request/reply.
1439 } icmp_echo_header_t;
1443 u16 src_port, dst_port;
1446 u8 *format_user_kvp (u8 * s, va_list * args);
1448 #endif /* __included_nat_h__ */
1450 * fd.io coding-style-patch-verification: ON
1453 * eval: (c-set-style "gnu")