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 /* deterministic session outside key */
78 ip4_address_t ext_host_addr;
86 /* user (internal host) key */
100 /* NAT API Configuration flags */
101 #define foreach_nat_config_flag \
102 _(0x01, IS_TWICE_NAT) \
103 _(0x02, IS_SELF_TWICE_NAT) \
104 _(0x04, IS_OUT2IN_ONLY) \
105 _(0x08, IS_ADDR_ONLY) \
106 _(0x10, IS_OUTSIDE) \
109 _(0x80, IS_EXT_HOST_VALID) \
111 typedef enum nat_config_flags_t_
113 #define _(n,f) NAT_API_##f = n,
114 foreach_nat_config_flag
116 } nat_config_flags_t;
118 /* External address and port allocation modes */
119 #define foreach_nat_addr_and_port_alloc_alg \
120 _(0, DEFAULT, "default") \
121 _(1, MAPE, "map-e") \
122 _(2, RANGE, "port-range")
126 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
127 foreach_nat_addr_and_port_alloc_alg
129 } nat_addr_and_port_alloc_alg_t;
132 #define foreach_snat_session_state \
133 _(0, UNKNOWN, "unknown") \
134 _(1, UDP_ACTIVE, "udp-active") \
135 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
136 _(3, TCP_ESTABLISHED, "tcp-established") \
137 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
138 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
139 _(6, TCP_CLOSING, "tcp-closing") \
140 _(7, TCP_LAST_ACK, "tcp-last-ack") \
141 _(8, TCP_CLOSED, "tcp-closed") \
142 _(9, ICMP_ACTIVE, "icmp-active")
146 #define _(v, N, s) SNAT_SESSION_##N = v,
147 foreach_snat_session_state
149 } snat_session_state_t;
151 #define foreach_nat_in2out_ed_error \
152 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
153 _(IN2OUT_PACKETS, "good in2out packets processed") \
154 _(OUT_OF_PORTS, "out of ports") \
155 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
156 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
157 _(DROP_FRAGMENT, "drop fragment") \
158 _(NON_SYN, "non-SYN packet try to create session") \
159 _(TCP_PACKETS, "TCP packets") \
160 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
161 _(UDP_PACKETS, "UDP packets") \
162 _(ICMP_PACKETS, "ICMP packets") \
163 _(OTHER_PACKETS, "other protocol packets") \
164 _(FRAGMENTS, "fragments")
168 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
169 foreach_nat_in2out_ed_error
171 NAT_IN2OUT_ED_N_ERROR,
172 } nat_in2out_ed_error_t;
174 #define foreach_nat_out2in_ed_error \
175 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
176 _(OUT2IN_PACKETS, "good out2in packets processed") \
177 _(OUT_OF_PORTS, "out of ports") \
178 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
179 _(NO_TRANSLATION, "no translation") \
180 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
181 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
182 _(DROP_FRAGMENT, "drop fragment") \
183 _(CANNOT_CREATE_USER, "cannot create NAT user") \
184 _(NON_SYN, "non-SYN packet try to create session") \
185 _(TCP_PACKETS, "TCP packets") \
186 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
187 _(UDP_PACKETS, "UDP packets") \
188 _(ICMP_PACKETS, "ICMP packets") \
189 _(OTHER_PACKETS, "other protocol packets") \
190 _(FRAGMENTS, "fragments")
194 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
195 foreach_nat_out2in_ed_error
197 NAT_OUT2IN_ED_N_ERROR,
198 } nat_out2in_ed_error_t;
201 /* Endpoint dependent TCP session state */
202 #define NAT44_SES_I2O_FIN 1
203 #define NAT44_SES_O2I_FIN 2
204 #define NAT44_SES_I2O_FIN_ACK 4
205 #define NAT44_SES_O2I_FIN_ACK 8
206 #define NAT44_SES_I2O_SYN 16
207 #define NAT44_SES_O2I_SYN 32
208 #define NAT44_SES_RST 64
211 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
212 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
213 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
214 #define SNAT_SESSION_FLAG_TWICE_NAT 8
215 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
216 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
217 #define SNAT_SESSION_FLAG_AFFINITY 64
218 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
220 /* NAT interface flags */
221 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
222 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
224 /* Static mapping flags */
225 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
226 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
227 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
228 #define NAT_STATIC_MAPPING_FLAG_LB 8
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;
298 u32 sessions_per_user_list_head_index;
308 #define _(N, i, n, s) \
309 u16 busy_##n##_ports; \
310 u16 * busy_##n##_ports_per_thread; \
311 u32 busy_##n##_port_refcounts[65535];
325 /* Inside network port */
327 /* Outside network address and port */
328 snat_det_out_key_t out;
333 } snat_det_session_t;
337 /* inside IP address range */
338 ip4_address_t in_addr;
340 /* outside IP address range */
341 ip4_address_t out_addr;
343 /* inside IP addresses / outside IP addresses */
345 /* number of ports available to internal host */
347 /* session counter */
349 /* vector of sessions */
350 snat_det_session_t *sessions;
355 /* backend IP address */
357 /* backend port number */
359 /* probability of the backend to be randomly matched */
362 /* backend FIB table */
365 } nat44_lb_addr_port_t;
369 /* twice-nat disabled */
371 /* twice-nat enabled */
373 /* twice-nat only when src IP equals dst IP after translation */
379 /* no load-balancing */
383 /* load-balancing with affinity */
389 /* local IP address */
390 ip4_address_t local_addr;
391 /* external IP address */
392 ip4_address_t external_addr;
398 twice_nat_type_t twice_nat;
399 /* local FIB table */
403 nat_protocol_t proto;
404 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
406 /* worker threads used by backends/local host */
408 /* opaque string tag */
410 /* backends for load-balancing mode */
411 nat44_lb_addr_port_t *locals;
412 /* affinity per service lis */
413 u32 affinity_per_service_list_head_index;
416 } snat_static_mapping_t;
426 ip4_address_t l_addr;
431 nat_protocol_t proto;
439 } snat_static_map_resolve_t;
443 /* Main lookup tables */
444 clib_bihash_8_8_t out2in;
445 clib_bihash_8_8_t in2out;
447 /* Endpoint dependent sessions lookup tables */
448 clib_bihash_16_8_t in2out_ed;
450 /* Find-a-user => src address lookup */
451 clib_bihash_8_8_t user_hash;
457 snat_session_t *sessions;
459 /* Pool of doubly-linked list elements */
460 dlist_elt_t *list_pool;
462 /* LRU session list - head is stale, tail is fresh */
463 dlist_elt_t *lru_pool;
464 u32 tcp_trans_lru_head_index;
465 u32 tcp_estab_lru_head_index;
466 u32 udp_lru_head_index;
467 u32 icmp_lru_head_index;
468 u32 unk_proto_lru_head_index;
470 /* NAT thread index */
471 u32 snat_thread_index;
473 /* real thread index */
476 } snat_main_per_thread_data_t;
480 /* ICMP session match function */
481 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
482 vlib_node_runtime_t * node,
486 ip4_address_t * addr,
489 nat_protocol_t * proto,
491 u8 * dont_translate);
493 /* Return worker thread index for given packet */
494 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
498 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
503 /* NAT address and port allocation function */
504 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
508 nat_protocol_t proto,
509 ip4_address_t * addr,
512 u32 snat_thread_index);
515 typedef struct snat_main_s
517 /* ICMP session match functions */
518 snat_icmp_match_function_t *icmp_match_in2out_cb;
519 snat_icmp_match_function_t *icmp_match_out2in_cb;
521 /* Thread settings */
523 u32 first_worker_index;
525 snat_get_worker_in2out_function_t *worker_in2out_cb;
526 snat_get_worker_out2in_function_t *worker_out2in_cb;
529 /* Per thread data */
530 snat_main_per_thread_data_t *per_thread_data;
532 /* Find a static mapping by local */
533 clib_bihash_8_8_t static_mapping_by_local;
535 /* Find a static mapping by external */
536 clib_bihash_8_8_t static_mapping_by_external;
538 /* Static mapping pool */
539 snat_static_mapping_t *static_mappings;
541 /* Endpoint-dependent out2in mappings */
542 clib_bihash_16_8_t out2in_ed;
545 snat_interface_t *interfaces;
546 snat_interface_t *output_feature_interfaces;
548 /* Vector of outside addresses */
549 snat_address_t *addresses;
550 /* Address and port allocation function */
551 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
552 /* Address and port allocation type */
553 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
554 /* Port set parameters (MAP-E) */
558 /* Port range parameters */
562 /* vector of outside fibs */
563 nat_outside_fib_t *outside_fibs;
565 /* Vector of twice NAT addresses for external hosts */
566 snat_address_t *twice_nat_addresses;
568 /* sw_if_indices whose intfc addresses should be auto-added */
569 u32 *auto_add_sw_if_indices;
570 u32 *auto_add_sw_if_indices_twice_nat;
572 /* vector of interface address static mappings to resolve. */
573 snat_static_map_resolve_t *to_resolve;
575 /* Randomize port allocation order */
578 /* Worker handoff frame-queue index */
580 u32 fq_in2out_output_index;
584 u32 error_node_index;
586 /* handoff fq nodes */
587 u32 handoff_out2in_index;
588 u32 handoff_in2out_index;
589 u32 handoff_in2out_output_index;
591 /* respect feature arc nodes */
592 u32 pre_out2in_node_index;
593 u32 pre_in2out_node_index;
595 u32 in2out_node_index;
596 u32 in2out_output_node_index;
597 u32 in2out_fast_node_index;
598 u32 in2out_slowpath_node_index;
599 u32 in2out_slowpath_output_node_index;
600 u32 ed_in2out_node_index;
601 u32 ed_in2out_slowpath_node_index;
602 u32 out2in_node_index;
603 u32 out2in_fast_node_index;
604 u32 ed_out2in_node_index;
605 u32 ed_out2in_slowpath_node_index;
606 u32 det_in2out_node_index;
607 u32 det_out2in_node_index;
609 u32 hairpinning_node_index;
610 u32 hairpin_dst_node_index;
611 u32 hairpin_src_node_index;
612 u32 ed_hairpinning_node_index;
613 u32 ed_hairpin_dst_node_index;
614 u32 ed_hairpin_src_node_index;
617 /* Deterministic NAT mappings */
618 snat_det_map_t *det_maps;
620 /* If forwarding is enabled */
621 u8 forwarding_enabled;
623 /* Config parameters */
624 u8 static_mapping_only;
625 u8 static_mapping_connection_tracking;
628 u8 endpoint_dependent;
630 u32 translation_buckets;
631 uword translation_memory_size;
632 u32 max_translations_per_thread;
633 u32 *max_translations_per_fib;
634 u32 max_users_per_thread;
636 uword user_memory_size;
637 u32 max_translations_per_user;
640 u32 outside_fib_index;
642 u32 inside_fib_index;
644 /* values of various timeouts */
647 u32 tcp_transitory_timeout;
648 u32 tcp_established_timeout;
651 /* TCP MSS clamping */
654 /* counters/gauges */
655 vlib_simple_counter_main_t total_users;
656 vlib_simple_counter_main_t total_sessions;
657 vlib_simple_counter_main_t user_limit_reached;;
659 /* API message ID base */
663 vlib_log_class_t log_class;
668 vnet_main_t *vnet_main;
669 ip4_main_t *ip4_main;
670 ip_lookup_main_t *ip4_lookup_main;
671 api_main_t *api_main;
678 } nat44_is_idle_session_ctx_t;
682 u32 cached_sw_if_index;
683 u32 cached_ip4_address;
686 extern snat_main_t snat_main;
688 // nat pre ed next_node feature classification
689 extern vlib_node_registration_t nat_default_node;
690 extern vlib_node_registration_t nat_pre_in2out_node;
691 extern vlib_node_registration_t nat_pre_out2in_node;
693 extern vlib_node_registration_t snat_in2out_node;
694 extern vlib_node_registration_t snat_in2out_output_node;
695 extern vlib_node_registration_t snat_out2in_node;
696 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
697 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
698 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
699 extern vlib_node_registration_t snat_det_in2out_node;
700 extern vlib_node_registration_t snat_det_out2in_node;
701 extern vlib_node_registration_t nat44_ed_in2out_node;
702 extern vlib_node_registration_t nat44_ed_in2out_output_node;
703 extern vlib_node_registration_t nat44_ed_out2in_node;
705 extern fib_source_t nat_fib_src_hi;
706 extern fib_source_t nat_fib_src_low;
708 /* format functions */
709 format_function_t format_snat_user;
710 format_function_t format_snat_static_mapping;
711 format_function_t format_snat_static_map_to_resolve;
712 format_function_t format_snat_session;
713 format_function_t format_det_map_ses;
714 format_function_t format_snat_key;
715 format_function_t format_static_mapping_key;
716 format_function_t format_nat_protocol;
717 format_function_t format_nat_addr_and_port_alloc_alg;
718 /* unformat functions */
719 unformat_function_t unformat_nat_protocol;
721 /** \brief Check if SNAT session is created from static mapping.
722 @param s SNAT session
723 @return 1 if SNAT session is created from static mapping otherwise 0
725 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
727 /** \brief Check if SNAT session for unknown protocol.
728 @param s SNAT session
729 @return 1 if SNAT session for unknown protocol otherwise 0
731 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
733 /** \brief Check if NAT session is twice NAT.
735 @return 1 if NAT session is twice NAT
737 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
739 /** \brief Check if NAT session is load-balancing.
741 @return 1 if NAT session is load-balancing
743 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
745 /** \brief Check if NAT session is forwarding bypass.
747 @return 1 if NAT session is load-balancing
749 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
751 /** \brief Check if NAT session is endpoint dependent.
753 @return 1 if NAT session is endpoint dependent
755 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
757 /** \brief Check if NAT session has affinity record.
759 @return 1 if NAT session has affinity record
761 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
763 /** \brief Check if NAT interface is inside.
764 @param i NAT interface
765 @return 1 if inside interface
767 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
769 /** \brief Check if NAT interface is outside.
770 @param i NAT interface
771 @return 1 if outside interface
773 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
775 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
777 @return 1 if session is closed
779 #define nat44_is_ses_closed(s) s->state == 0xf
781 /** \brief Check if NAT static mapping is address only (1:1NAT).
782 @param sm NAT static mapping
783 @return 1 if 1:1NAT, 0 if 1:1NAPT
785 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
787 /** \brief Check if NAT static mapping match only out2in direction.
788 @param sm NAT static mapping
789 @return 1 if rule match only out2in direction
791 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
793 /** \brief Check if NAT static mapping is identity NAT.
794 @param sm NAT static mapping
795 @return 1 if identity NAT
797 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
799 /** \brief Check if NAT static mapping is load-balancing.
800 @param sm NAT static mapping
801 @return 1 if load-balancing
803 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
805 /** \brief Check if client initiating TCP connection (received SYN from client)
807 @return 1 if client initiating TCP connection
810 tcp_flags_is_init (u8 f)
812 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
816 #define nat_log_err(...) \
817 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
818 #define nat_log_warn(...) \
819 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
820 #define nat_log_notice(...) \
821 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
822 #define nat_log_info(...) \
823 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
824 #define nat_log_debug(...)\
825 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
827 /* NAT API Logging Levels */
828 #define foreach_nat_log_level \
831 _(0x02, LOG_WARNING) \
832 _(0x03, LOG_NOTICE) \
836 typedef enum nat_log_level_t_
838 #define _(n,f) SNAT_##f = n,
839 foreach_nat_log_level
843 #define nat_elog(_level, _str) \
846 snat_main_t *sm = &snat_main; \
847 if (PREDICT_FALSE (sm->log_level >= _level)) \
849 ELOG_TYPE_DECLARE (e) = \
851 .format = "nat-msg " _str, \
854 ELOG_DATA (&vlib_global_main.elog_main, e); \
858 #define nat_elog_addr(_level, _str, _addr) \
861 if (PREDICT_FALSE (sm->log_level >= _level)) \
863 ELOG_TYPE_DECLARE (e) = \
865 .format = "nat-msg " _str " %d.%d.%d.%d", \
866 .format_args = "i1i1i1i1", \
875 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
876 ed->oct4 = _addr >> 24; \
877 ed->oct3 = _addr >> 16; \
878 ed->oct2 = _addr >> 8; \
883 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
886 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
888 ELOG_TYPE_DECLARE (e) = \
890 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
891 " tid from: %d to: %d fib: %d", \
892 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
908 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
909 ed->src_oct1 = _src >> 24; \
910 ed->src_oct2 = _src >> 16; \
911 ed->src_oct3 = _src >> 8; \
912 ed->src_oct4 = _src; \
913 ed->dst_oct1 = _dst >> 24; \
914 ed->dst_oct2 = _dst >> 16; \
915 ed->dst_oct3 = _dst >> 8; \
916 ed->dst_oct4 = _dst; \
917 ed->ftid = vlib_get_thread_index (); \
923 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
926 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
928 ELOG_TYPE_DECLARE (e) = \
930 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
931 " tid:%d prt:%d fib:%d", \
932 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
948 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
949 ed->src_oct1 = _src >> 24; \
950 ed->src_oct2 = _src >> 16; \
951 ed->src_oct3 = _src >> 8; \
952 ed->src_oct4 = _src; \
953 ed->dst_oct1 = _dst >> 24; \
954 ed->dst_oct2 = _dst >> 16; \
955 ed->dst_oct3 = _dst >> 8; \
956 ed->dst_oct4 = _dst; \
957 ed->tid = vlib_get_thread_index (); \
963 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
966 snat_main_t *sm = &snat_main; \
967 if (PREDICT_FALSE (sm->log_level >= _level)) \
969 ELOG_TYPE_DECLARE (e) = \
971 .format = "nat-msg " _fmt, \
972 .format_args = _arg, \
976 typeof (_val1) val1; \
978 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
983 #define nat_elog_notice(nat_elog_str) \
984 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
985 #define nat_elog_warn(nat_elog_str) \
986 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
987 #define nat_elog_err(nat_elog_str) \
988 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
989 #define nat_elog_debug(nat_elog_str) \
990 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
991 #define nat_elog_info(nat_elog_str) \
992 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
994 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
995 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
996 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
997 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
998 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
999 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1000 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1001 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1002 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1003 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1005 /* ICMP session match functions */
1006 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1007 u32 thread_index, vlib_buffer_t * b0,
1008 ip4_header_t * ip0, ip4_address_t * addr,
1009 u16 * port, u32 * fib_index,
1010 nat_protocol_t * proto, void *d, void *e,
1011 u8 * dont_translate);
1012 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1013 u32 thread_index, vlib_buffer_t * b0,
1014 ip4_header_t * ip0, ip4_address_t * addr,
1015 u16 * port, u32 * fib_index,
1016 nat_protocol_t * proto, void *d, void *e,
1017 u8 * dont_translate);
1018 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1019 u32 thread_index, vlib_buffer_t * b0,
1020 ip4_header_t * ip0, ip4_address_t * addr,
1021 u16 * port, u32 * fib_index,
1022 nat_protocol_t * proto, void *d, void *e,
1023 u8 * dont_translate);
1024 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1025 u32 thread_index, vlib_buffer_t * b0,
1026 ip4_header_t * ip0, ip4_address_t * addr,
1027 u16 * port, u32 * fib_index,
1028 nat_protocol_t * proto, void *d, void *e,
1029 u8 * dont_translate);
1031 /* ICMP deterministic NAT session match functions */
1032 u32 icmp_match_out2in_det (snat_main_t * sm, vlib_node_runtime_t * node,
1033 u32 thread_index, vlib_buffer_t * b0,
1034 ip4_header_t * ip0, ip4_address_t * addr,
1035 u16 * port, u32 * fib_index,
1036 nat_protocol_t * proto, void *d, void *e,
1037 u8 * dont_translate);
1038 u32 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
1039 u32 thread_index, vlib_buffer_t * b0,
1040 ip4_header_t * ip0, ip4_address_t * addr,
1041 u16 * port, u32 * fib_index,
1042 nat_protocol_t * proto, void *d, void *e,
1043 u8 * dont_translate);
1045 /* ICMP endpoint-dependent session match functions */
1046 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1047 u32 thread_index, vlib_buffer_t * b0,
1048 ip4_header_t * ip0, ip4_address_t * addr,
1049 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1050 void *d, void *e, u8 * dont_translate);
1051 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1052 u32 thread_index, vlib_buffer_t * b0,
1053 ip4_header_t * ip0, ip4_address_t * addr,
1054 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1055 void *d, void *e, u8 * dont_translate);
1057 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1058 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1059 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1062 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1063 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1064 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1067 /* hairpinning functions */
1068 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1069 ip4_header_t * ip0, icmp46_header_t * icmp0,
1071 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1073 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1075 int snat_hairpinning (vlib_main_t * vm, vlib_node_runtime_t * node,
1076 snat_main_t * sm, vlib_buffer_t * b0,
1077 ip4_header_t * ip0, udp_header_t * udp0,
1078 tcp_header_t * tcp0, u32 proto0, int is_ed,
1081 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1082 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1083 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1084 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1085 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1088 * @brief Add external address to NAT44 pool
1090 * @param sm snat global configuration data
1091 * @param addr IPv4 address
1092 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1093 * @param twice_nat 1 if twice NAT address
1095 * @return 0 on success, non-zero value otherwise
1097 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1101 * @brief Delete external address from NAT44 pool
1103 * @param sm snat global configuration data
1104 * @param addr IPv4 address
1105 * @param delete_sm 1 if delete static mapping using address
1106 * @param twice_nat 1 if twice NAT address
1108 * @return 0 on success, non-zero value otherwise
1110 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1114 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1116 * @param addr IPv4 address
1117 * @param is_add 1 = add, 0 = delete
1119 * @return 0 on success, non-zero value otherwise
1121 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1124 * @brief Add/delete NAT44 static mapping
1126 * @param l_addr local IPv4 address
1127 * @param e_addr external IPv4 address
1128 * @param l_port local port number
1129 * @param e_port external port number
1130 * @param vrf_id local VRF ID
1131 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1132 * @param sw_if_index use interface address as external IPv4 address
1133 * @param proto L4 protocol
1134 * @param is_add 1 = add, 0 = delete
1135 * @param twice_nat twice-nat mode
1136 * @param out2in_only if 1 rule match only out2in direction
1137 * @param tag opaque string tag
1138 * @param identity_nat identity NAT
1140 * @return 0 on success, non-zero value otherwise
1142 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1143 u16 l_port, u16 e_port, u32 vrf_id,
1144 int addr_only, u32 sw_if_index,
1145 nat_protocol_t proto, int is_add,
1146 twice_nat_type_t twice_nat, u8 out2in_only,
1147 u8 * tag, u8 identity_nat);
1150 * @brief Add/delete static mapping with load-balancing (multiple backends)
1152 * @param e_addr external IPv4 address
1153 * @param e_port external port number
1154 * @param proto L4 protocol
1155 * @param locals list of local backends
1156 * @param is_add 1 = add, 0 = delete
1157 * @param twice_nat twice-nat mode
1158 * @param out2in_only if 1 rule match only out2in direction
1159 * @param tag opaque string tag
1160 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1162 * @return 0 on success, non-zero value otherwise
1164 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1165 nat_protocol_t proto,
1166 nat44_lb_addr_port_t * locals, u8 is_add,
1167 twice_nat_type_t twice_nat,
1168 u8 out2in_only, u8 * tag, u32 affinity);
1170 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1171 ip4_address_t l_addr, u16 l_port,
1172 nat_protocol_t proto, u32 vrf_id,
1173 u8 probability, u8 is_add);
1175 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1178 * @brief Set NAT plugin workers
1180 * @param bitmap NAT workers bitmap
1182 * @return 0 on success, non-zero value otherwise
1184 int snat_set_workers (uword * bitmap);
1187 * @brief Enable/disable NAT44 feature on the interface
1189 * @param sw_if_index software index of the interface
1190 * @param is_inside 1 = inside, 0 = outside
1191 * @param is_del 1 = delete, 0 = add
1193 * @return 0 on success, non-zero value otherwise
1195 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1198 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1200 * @param sw_if_index software index of the interface
1201 * @param is_inside 1 = inside, 0 = outside
1202 * @param is_del 1 = delete, 0 = add
1204 * @return 0 on success, non-zero value otherwise
1206 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1210 * @brief Add/delete NAT44 pool address from specific interface
1212 * @param sw_if_index software index of the interface
1213 * @param is_del 1 = delete, 0 = add
1214 * @param twice_nat 1 = twice NAT address for external hosts
1216 * @return 0 on success, non-zero value otherwise
1218 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1222 * @brief Delete NAT44 session
1224 * @param addr IPv4 address
1225 * @param port L4 port number
1226 * @param proto L4 protocol
1227 * @param vrf_id VRF ID
1228 * @param is_in 1 = inside network address and port pair, 0 = outside
1230 * @return 0 on success, non-zero value otherwise
1232 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1233 nat_protocol_t proto, u32 vrf_id, int is_in);
1236 * @brief Delete NAT44 endpoint-dependent session
1238 * @param sm snat global configuration data
1239 * @param addr IPv4 address
1240 * @param port L4 port number
1241 * @param proto L4 protocol
1242 * @param vrf_id VRF ID
1243 * @param is_in 1 = inside network address and port pair, 0 = outside
1245 * @return 0 on success, non-zero value otherwise
1247 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1248 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1249 u32 vrf_id, int is_in);
1252 * @brief Free NAT44 session data (lookup keys, external address port)
1254 * @param sm snat global configuration data
1255 * @param s NAT session
1256 * @param thread_index thread index
1257 * @param is_ha is HA event
1259 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1260 u32 thread_index, u8 is_ha);
1263 * @brief Set NAT44 session limit (session limit, vrf id)
1265 * @param session_limit Session limit
1266 * @param vrf_id VRF id
1267 * @return 0 on success, non-zero value otherwise
1269 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1272 * @brief Free NAT44 ED session data (lookup keys, external address port)
1274 * @param s NAT session
1275 * @param thread_index thread index
1276 * @param is_ha is HA event
1279 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1280 u32 thread_index, u8 is_ha);
1283 * @brief Initialize NAT44 data
1285 * @param tsm per thread data
1287 void nat44_db_init (snat_main_per_thread_data_t * tsm);
1290 * @brief Free NAT44 data
1292 * @param tsm per thread data
1294 void nat44_db_free (snat_main_per_thread_data_t * tsm);
1296 void nat44_sessions_clear ();
1299 * @brief Find or create NAT user
1301 * @param sm snat global configuration data
1302 * @param addr IPv4 address
1303 * @param fib_index FIB table index
1304 * @param thread_index thread index
1306 * @return NAT user data structure on success otherwise zero value
1308 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1309 ip4_address_t * addr, u32 fib_index,
1313 * @brief Allocate new NAT session or recycle last used
1315 * @param sm snat global configuration data
1317 * @param thread_index thread index
1318 * @param now time now
1320 * @return session data structure on success otherwise zero value
1322 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1324 u32 thread_index, f64 now);
1327 * @brief Set address and port assignment algorithm for MAP-E CE
1329 * @param psid Port Set Identifier value
1330 * @param psid_offset number of offset bits
1331 * @param psid_length length of PSID
1333 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1337 * @brief Set address and port assignment algorithm for port range
1339 * @param start_port beginning of the port range
1340 * @param end_port end of the port range
1342 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1345 * @brief Set address and port assignment algorithm to default/standard
1347 void nat_set_alloc_addr_and_port_default (void);
1350 * @brief Free outside address and port pair
1352 * @param addresses vector of outside addresses
1353 * @param thread_index thread index
1354 * @param key address, port and protocol
1357 snat_free_outside_address_and_port (snat_address_t * addresses,
1359 ip4_address_t * addr,
1360 u16 port, nat_protocol_t protocol);
1363 * @brief Alloc outside address and port
1365 * @param addresses vector of outside addresses
1366 * @param fib_index FIB table index
1367 * @param thread_index thread index
1368 * @param port_per_thread number of ports per thread
1369 * @param snat_thread_index NAT thread index
1371 * @return 0 on success, non-zero value otherwise
1373 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1376 nat_protocol_t proto,
1377 ip4_address_t * addr,
1379 u16 port_per_thread,
1380 u32 snat_thread_index);
1383 * @brief Match NAT44 static mapping.
1385 * @param key address and port to match
1386 * @param addr external/local address of the matched mapping
1387 * @param port port of the matched mapping
1388 * @param fib_index fib index of the matched mapping
1389 * @param by_external if 0 match by local address otherwise match by external
1391 * @param is_addr_only 1 if matched mapping is address only
1392 * @param twice_nat matched mapping is twice NAT type
1393 * @param lb 1 if matched mapping is load-balanced
1394 * @param ext_host_addr external host address
1396 * @returns 0 if match found otherwise 1.
1398 int snat_static_mapping_match (snat_main_t * sm,
1399 ip4_address_t match_addr,
1401 u32 match_fib_index,
1402 nat_protocol_t match_protocol,
1403 ip4_address_t * mapping_addr,
1405 u32 * mapping_fib_index,
1408 twice_nat_type_t * twice_nat,
1410 ip4_address_t * ext_host_addr,
1411 u8 * is_identity_nat);
1414 * @brief Add/del NAT address to FIB.
1416 * Add the external NAT address to the FIB as receive entries. This ensures
1417 * that VPP will reply to ARP for this address and we don't need to enable
1418 * proxy ARP on the outside interface.
1420 * @param addr IPv4 address
1421 * @param plen address prefix length
1422 * @param sw_if_index software index of the outside interface
1423 * @param is_add 0 = delete, 1 = add.
1425 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1426 u8 p_len, u32 sw_if_index, int is_add);
1429 * Why is this here? Because we don't need to touch this layer to
1430 * simply reply to an icmp. We need to change id to a unique
1431 * value to NAT an echo request/reply.
1438 } icmp_echo_header_t;
1442 u16 src_port, dst_port;
1445 u8 *format_user_kvp (u8 * s, va_list * args);
1446 #endif /* __included_nat_h__ */
1448 * fd.io coding-style-patch-verification: ON
1451 * eval: (c-set-style "gnu")