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 /* session key (4-tuple) */
80 u16 protocol:3, fib_index:13;
86 /* deterministic session outside key */
93 ip4_address_t ext_host_addr;
101 /* user (internal host) key */
115 /* NAT API Configuration flags */
116 #define foreach_nat_config_flag \
117 _(0x01, IS_TWICE_NAT) \
118 _(0x02, IS_SELF_TWICE_NAT) \
119 _(0x04, IS_OUT2IN_ONLY) \
120 _(0x08, IS_ADDR_ONLY) \
121 _(0x10, IS_OUTSIDE) \
124 _(0x80, IS_EXT_HOST_VALID) \
126 typedef enum nat_config_flags_t_
128 #define _(n,f) NAT_API_##f = n,
129 foreach_nat_config_flag
131 } nat_config_flags_t;
133 /* External address and port allocation modes */
134 #define foreach_nat_addr_and_port_alloc_alg \
135 _(0, DEFAULT, "default") \
136 _(1, MAPE, "map-e") \
137 _(2, RANGE, "port-range")
141 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
142 foreach_nat_addr_and_port_alloc_alg
144 } nat_addr_and_port_alloc_alg_t;
147 #define foreach_snat_session_state \
148 _(0, UNKNOWN, "unknown") \
149 _(1, UDP_ACTIVE, "udp-active") \
150 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
151 _(3, TCP_ESTABLISHED, "tcp-established") \
152 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
153 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
154 _(6, TCP_CLOSING, "tcp-closing") \
155 _(7, TCP_LAST_ACK, "tcp-last-ack") \
156 _(8, TCP_CLOSED, "tcp-closed") \
157 _(9, ICMP_ACTIVE, "icmp-active")
161 #define _(v, N, s) SNAT_SESSION_##N = v,
162 foreach_snat_session_state
164 } snat_session_state_t;
166 #define foreach_nat_in2out_ed_error \
167 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
168 _(IN2OUT_PACKETS, "good in2out packets processed") \
169 _(OUT_OF_PORTS, "out of ports") \
170 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
171 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
172 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
173 _(DROP_FRAGMENT, "drop fragment") \
174 _(CANNOT_CREATE_USER, "cannot create NAT user") \
175 _(NON_SYN, "non-SYN packet try to create session") \
176 _(TCP_PACKETS, "TCP packets") \
177 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
178 _(UDP_PACKETS, "UDP packets") \
179 _(ICMP_PACKETS, "ICMP packets") \
180 _(OTHER_PACKETS, "other protocol packets") \
181 _(FRAGMENTS, "fragments") \
182 _(CACHED_FRAGMENTS, "cached fragments") \
183 _(PROCESSED_FRAGMENTS, "processed fragments")
187 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
188 foreach_nat_in2out_ed_error
190 NAT_IN2OUT_ED_N_ERROR,
191 } nat_in2out_ed_error_t;
193 #define foreach_nat_out2in_ed_error \
194 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
195 _(OUT2IN_PACKETS, "good out2in packets processed") \
196 _(OUT_OF_PORTS, "out of ports") \
197 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
198 _(NO_TRANSLATION, "no translation") \
199 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
200 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
201 _(DROP_FRAGMENT, "drop fragment") \
202 _(CANNOT_CREATE_USER, "cannot create NAT user") \
203 _(NON_SYN, "non-SYN packet try to create session") \
204 _(TCP_PACKETS, "TCP packets") \
205 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
206 _(UDP_PACKETS, "UDP packets") \
207 _(ICMP_PACKETS, "ICMP packets") \
208 _(OTHER_PACKETS, "other protocol packets") \
209 _(FRAGMENTS, "fragments") \
210 _(CACHED_FRAGMENTS, "cached fragments") \
211 _(PROCESSED_FRAGMENTS, "processed fragments")
215 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
216 foreach_nat_out2in_ed_error
218 NAT_OUT2IN_ED_N_ERROR,
219 } nat_out2in_ed_error_t;
222 /* Endpoint dependent TCP session state */
223 #define NAT44_SES_I2O_FIN 1
224 #define NAT44_SES_O2I_FIN 2
225 #define NAT44_SES_I2O_FIN_ACK 4
226 #define NAT44_SES_O2I_FIN_ACK 8
227 #define NAT44_SES_I2O_SYN 16
228 #define NAT44_SES_O2I_SYN 32
229 #define NAT44_SES_RST 64
232 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
233 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
234 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
235 #define SNAT_SESSION_FLAG_TWICE_NAT 8
236 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
237 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
238 #define SNAT_SESSION_FLAG_AFFINITY 64
239 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
241 /* NAT interface flags */
242 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
243 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
245 /* Static mapping flags */
246 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
247 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
248 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
249 #define NAT_STATIC_MAPPING_FLAG_LB 8
252 typedef CLIB_PACKED(struct
254 /* Outside network key */
255 snat_session_key_t out2in;
257 /* Inside network key */
258 snat_session_key_t in2out;
263 /* Per-user translations */
265 u32 per_user_list_head_index;
267 /* head of LRU list in which this session is tracked */
269 /* index in global LRU list */
273 /* Last heard timer */
276 /* Last HA refresh */
277 f64 ha_last_refreshed;
283 /* External host address and port */
284 ip4_address_t ext_host_addr;
287 /* External host address and port after translation */
288 ip4_address_t ext_host_nat_addr;
289 u16 ext_host_nat_port;
291 /* TCP session state */
295 u64 tcp_closed_timestamp;
307 u32 sessions_per_user_list_head_index;
317 #define _(N, i, n, s) \
318 u16 busy_##n##_ports; \
319 u16 * busy_##n##_ports_per_thread; \
320 u32 busy_##n##_port_refcounts[65535];
334 /* Inside network port */
336 /* Outside network address and port */
337 snat_det_out_key_t out;
342 } snat_det_session_t;
346 /* inside IP address range */
347 ip4_address_t in_addr;
349 /* outside IP address range */
350 ip4_address_t out_addr;
352 /* inside IP addresses / outside IP addresses */
354 /* number of ports available to internal host */
356 /* session counter */
358 /* vector of sessions */
359 snat_det_session_t *sessions;
364 /* backend IP address */
366 /* backend port number */
368 /* probability of the backend to be randomly matched */
371 /* backend FIB table */
374 } nat44_lb_addr_port_t;
378 /* twice-nat disabled */
380 /* twice-nat enabled */
382 /* twice-nat only when src IP equals dst IP after translation */
388 /* no load-balancing */
392 /* load-balancing with affinity */
398 /* local IP address */
399 ip4_address_t local_addr;
400 /* external IP address */
401 ip4_address_t external_addr;
407 twice_nat_type_t twice_nat;
408 /* local FIB table */
412 nat_protocol_t proto;
413 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
415 /* worker threads used by backends/local host */
417 /* opaque string tag */
419 /* backends for load-balancing mode */
420 nat44_lb_addr_port_t *locals;
421 /* affinity per service lis */
422 u32 affinity_per_service_list_head_index;
425 } snat_static_mapping_t;
435 ip4_address_t l_addr;
440 nat_protocol_t proto;
448 } snat_static_map_resolve_t;
452 /* Main lookup tables */
453 clib_bihash_8_8_t out2in;
454 clib_bihash_8_8_t in2out;
456 /* Endpoint dependent sessions lookup tables */
457 clib_bihash_16_8_t in2out_ed;
459 /* Find-a-user => src address lookup */
460 clib_bihash_8_8_t user_hash;
466 snat_session_t *sessions;
468 /* Pool of doubly-linked list elements */
469 dlist_elt_t *list_pool;
471 /* LRU session list - head is stale, tail is fresh */
472 dlist_elt_t *lru_pool;
473 u32 tcp_trans_lru_head_index;
474 u32 tcp_estab_lru_head_index;
475 u32 udp_lru_head_index;
476 u32 icmp_lru_head_index;
477 u32 unk_proto_lru_head_index;
479 /* NAT thread index */
480 u32 snat_thread_index;
482 /* real thread index */
485 } snat_main_per_thread_data_t;
489 /* ICMP session match function */
490 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
491 vlib_node_runtime_t * node,
494 ip4_header_t * ip0, u8 * p_proto,
495 snat_session_key_t * p_value,
496 u8 * p_dont_translate, void *d,
499 /* Return worker thread index for given packet */
500 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
504 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
509 /* NAT address and port allocation function */
510 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
514 snat_session_key_t * k,
516 u32 snat_thread_index);
519 typedef struct snat_main_s
521 /* ICMP session match functions */
522 snat_icmp_match_function_t *icmp_match_in2out_cb;
523 snat_icmp_match_function_t *icmp_match_out2in_cb;
525 /* Thread settings */
527 u32 first_worker_index;
529 snat_get_worker_in2out_function_t *worker_in2out_cb;
530 snat_get_worker_out2in_function_t *worker_out2in_cb;
533 /* Per thread data */
534 snat_main_per_thread_data_t *per_thread_data;
536 /* Find a static mapping by local */
537 clib_bihash_8_8_t static_mapping_by_local;
539 /* Find a static mapping by external */
540 clib_bihash_8_8_t static_mapping_by_external;
542 /* Static mapping pool */
543 snat_static_mapping_t *static_mappings;
545 /* Endpoint-dependent out2in mappings */
546 clib_bihash_16_8_t out2in_ed;
549 snat_interface_t *interfaces;
550 snat_interface_t *output_feature_interfaces;
552 /* Vector of outside addresses */
553 snat_address_t *addresses;
554 /* Address and port allocation function */
555 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
556 /* Address and port allocation type */
557 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
558 /* Port set parameters (MAP-E) */
562 /* Port range parameters */
566 /* vector of outside fibs */
567 nat_outside_fib_t *outside_fibs;
569 /* Vector of twice NAT addresses for external hosts */
570 snat_address_t *twice_nat_addresses;
572 /* sw_if_indices whose intfc addresses should be auto-added */
573 u32 *auto_add_sw_if_indices;
574 u32 *auto_add_sw_if_indices_twice_nat;
576 /* vector of interface address static mappings to resolve. */
577 snat_static_map_resolve_t *to_resolve;
579 /* Randomize port allocation order */
582 /* Worker handoff frame-queue index */
584 u32 fq_in2out_output_index;
588 u32 error_node_index;
590 /* handoff fq nodes */
591 u32 handoff_out2in_index;
592 u32 handoff_in2out_index;
593 u32 handoff_in2out_output_index;
595 /* respect feature arc nodes */
596 u32 pre_out2in_node_index;
597 u32 pre_in2out_node_index;
599 u32 in2out_node_index;
600 u32 in2out_output_node_index;
601 u32 in2out_fast_node_index;
602 u32 in2out_slowpath_node_index;
603 u32 in2out_slowpath_output_node_index;
604 u32 ed_in2out_node_index;
605 u32 ed_in2out_slowpath_node_index;
606 u32 out2in_node_index;
607 u32 out2in_fast_node_index;
608 u32 ed_out2in_node_index;
609 u32 ed_out2in_slowpath_node_index;
610 u32 det_in2out_node_index;
611 u32 det_out2in_node_index;
613 u32 hairpinning_node_index;
614 u32 hairpin_dst_node_index;
615 u32 hairpin_src_node_index;
616 u32 ed_hairpinning_node_index;
617 u32 ed_hairpin_dst_node_index;
618 u32 ed_hairpin_src_node_index;
621 /* Deterministic NAT mappings */
622 snat_det_map_t *det_maps;
624 /* If forwarding is enabled */
625 u8 forwarding_enabled;
627 /* Config parameters */
628 u8 static_mapping_only;
629 u8 static_mapping_connection_tracking;
632 u8 endpoint_dependent;
634 u32 translation_buckets;
635 uword translation_memory_size;
636 u32 max_translations;
637 u32 *max_translations_per_fib;
640 uword user_memory_size;
641 u32 max_translations_per_user;
644 u32 outside_fib_index;
646 u32 inside_fib_index;
648 /* values of various timeouts */
651 u32 tcp_transitory_timeout;
652 u32 tcp_established_timeout;
655 /* TCP MSS clamping */
659 /* counters/gauges */
660 vlib_simple_counter_main_t total_users;
661 vlib_simple_counter_main_t total_sessions;
663 /* API message ID base */
667 vlib_log_class_t log_class;
672 vnet_main_t *vnet_main;
673 ip4_main_t *ip4_main;
674 ip_lookup_main_t *ip4_lookup_main;
675 api_main_t *api_main;
682 } nat44_is_idle_session_ctx_t;
686 u32 cached_sw_if_index;
687 u32 cached_ip4_address;
690 extern snat_main_t snat_main;
692 // nat pre ed next_node feature classification
693 extern vlib_node_registration_t nat_default_node;
694 extern vlib_node_registration_t nat_pre_in2out_node;
695 extern vlib_node_registration_t nat_pre_out2in_node;
697 extern vlib_node_registration_t snat_in2out_node;
698 extern vlib_node_registration_t snat_in2out_output_node;
699 extern vlib_node_registration_t snat_out2in_node;
700 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
701 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
702 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
703 extern vlib_node_registration_t snat_det_in2out_node;
704 extern vlib_node_registration_t snat_det_out2in_node;
705 extern vlib_node_registration_t nat44_ed_in2out_node;
706 extern vlib_node_registration_t nat44_ed_in2out_output_node;
707 extern vlib_node_registration_t nat44_ed_out2in_node;
709 extern fib_source_t nat_fib_src_hi;
710 extern fib_source_t nat_fib_src_low;
712 /* format functions */
713 format_function_t format_snat_user;
714 format_function_t format_snat_static_mapping;
715 format_function_t format_snat_static_map_to_resolve;
716 format_function_t format_snat_session;
717 format_function_t format_det_map_ses;
718 format_function_t format_snat_key;
719 format_function_t format_static_mapping_key;
720 format_function_t format_nat_protocol;
721 format_function_t format_nat_addr_and_port_alloc_alg;
722 /* unformat functions */
723 unformat_function_t unformat_nat_protocol;
725 /** \brief Check if SNAT session is created from static mapping.
726 @param s SNAT session
727 @return 1 if SNAT session is created from static mapping otherwise 0
729 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
731 /** \brief Check if SNAT session for unknown protocol.
732 @param s SNAT session
733 @return 1 if SNAT session for unknown protocol otherwise 0
735 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
737 /** \brief Check if NAT session is twice NAT.
739 @return 1 if NAT session is twice NAT
741 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
743 /** \brief Check if NAT session is load-balancing.
745 @return 1 if NAT session is load-balancing
747 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
749 /** \brief Check if NAT session is forwarding bypass.
751 @return 1 if NAT session is load-balancing
753 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
755 /** \brief Check if NAT session is endpoint dependent.
757 @return 1 if NAT session is endpoint dependent
759 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
761 /** \brief Check if NAT session has affinity record.
763 @return 1 if NAT session has affinity record
765 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
767 /** \brief Check if NAT interface is inside.
768 @param i NAT interface
769 @return 1 if inside interface
771 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
773 /** \brief Check if NAT interface is outside.
774 @param i NAT interface
775 @return 1 if outside interface
777 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
779 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
781 @return 1 if session is closed
783 #define nat44_is_ses_closed(s) s->state == 0xf
785 /** \brief Check if NAT static mapping is address only (1:1NAT).
786 @param sm NAT static mapping
787 @return 1 if 1:1NAT, 0 if 1:1NAPT
789 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
791 /** \brief Check if NAT static mapping match only out2in direction.
792 @param sm NAT static mapping
793 @return 1 if rule match only out2in direction
795 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
797 /** \brief Check if NAT static mapping is identity NAT.
798 @param sm NAT static mapping
799 @return 1 if identity NAT
801 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
803 /** \brief Check if NAT static mapping is load-balancing.
804 @param sm NAT static mapping
805 @return 1 if load-balancing
807 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
809 /** \brief Check if client initiating TCP connection (received SYN from client)
811 @return 1 if client initiating TCP connection
814 tcp_flags_is_init (u8 f)
816 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
820 #define nat_log_err(...) \
821 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
822 #define nat_log_warn(...) \
823 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
824 #define nat_log_notice(...) \
825 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
826 #define nat_log_info(...) \
827 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
828 #define nat_log_debug(...)\
829 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
831 /* NAT API Logging Levels */
832 #define foreach_nat_log_level \
835 _(0x02, LOG_WARNING) \
836 _(0x03, LOG_NOTICE) \
840 typedef enum nat_log_level_t_
842 #define _(n,f) SNAT_##f = n,
843 foreach_nat_log_level
847 #define nat_elog(_level, _str) \
850 snat_main_t *sm = &snat_main; \
851 if (PREDICT_FALSE (sm->log_level >= _level)) \
853 ELOG_TYPE_DECLARE (e) = \
855 .format = "nat-msg " _str, \
858 ELOG_DATA (&vlib_global_main.elog_main, e); \
862 #define nat_elog_addr(_level, _str, _addr) \
865 if (PREDICT_FALSE (sm->log_level >= _level)) \
867 ELOG_TYPE_DECLARE (e) = \
869 .format = "nat-msg " _str " %d.%d.%d.%d", \
870 .format_args = "i1i1i1i1", \
879 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
880 ed->oct4 = _addr >> 24; \
881 ed->oct3 = _addr >> 16; \
882 ed->oct2 = _addr >> 8; \
887 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
890 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
892 ELOG_TYPE_DECLARE (e) = \
894 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
895 " tid from: %d to: %d fib: %d", \
896 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
912 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
913 ed->src_oct1 = _src >> 24; \
914 ed->src_oct2 = _src >> 16; \
915 ed->src_oct3 = _src >> 8; \
916 ed->src_oct4 = _src; \
917 ed->dst_oct1 = _dst >> 24; \
918 ed->dst_oct2 = _dst >> 16; \
919 ed->dst_oct3 = _dst >> 8; \
920 ed->dst_oct4 = _dst; \
921 ed->ftid = vlib_get_thread_index (); \
927 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
930 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
932 ELOG_TYPE_DECLARE (e) = \
934 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
935 " tid:%d prt:%d fib:%d", \
936 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
952 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
953 ed->src_oct1 = _src >> 24; \
954 ed->src_oct2 = _src >> 16; \
955 ed->src_oct3 = _src >> 8; \
956 ed->src_oct4 = _src; \
957 ed->dst_oct1 = _dst >> 24; \
958 ed->dst_oct2 = _dst >> 16; \
959 ed->dst_oct3 = _dst >> 8; \
960 ed->dst_oct4 = _dst; \
961 ed->tid = vlib_get_thread_index (); \
967 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
970 snat_main_t *sm = &snat_main; \
971 if (PREDICT_FALSE (sm->log_level >= _level)) \
973 ELOG_TYPE_DECLARE (e) = \
975 .format = "nat-msg " _fmt, \
976 .format_args = _arg, \
980 typeof (_val1) val1; \
982 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
987 #define nat_elog_notice(nat_elog_str) \
988 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
989 #define nat_elog_warn(nat_elog_str) \
990 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
991 #define nat_elog_err(nat_elog_str) \
992 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
993 #define nat_elog_debug(nat_elog_str) \
994 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
995 #define nat_elog_info(nat_elog_str) \
996 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
998 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
999 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1000 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1001 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1002 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1003 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1004 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1005 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1006 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1007 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1009 /* ICMP session match functions */
1010 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1011 u32 thread_index, vlib_buffer_t * b0,
1012 ip4_header_t * ip0, u8 * p_proto,
1013 snat_session_key_t * p_value,
1014 u8 * p_dont_translate, void *d, void *e);
1015 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1016 u32 thread_index, vlib_buffer_t * b0,
1017 ip4_header_t * ip0, u8 * p_proto,
1018 snat_session_key_t * p_value,
1019 u8 * p_dont_translate, void *d, void *e);
1020 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1021 u32 thread_index, vlib_buffer_t * b0,
1022 ip4_header_t * ip0, u8 * p_proto,
1023 snat_session_key_t * p_value,
1024 u8 * p_dont_translate, void *d, void *e);
1025 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1026 u32 thread_index, vlib_buffer_t * b0,
1027 ip4_header_t * ip0, u8 * p_proto,
1028 snat_session_key_t * p_value,
1029 u8 * p_dont_translate, void *d, void *e);
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, u8 * p_proto,
1035 snat_session_key_t * p_value,
1036 u8 * p_dont_translate, void *d, void *e);
1037 u32 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
1038 u32 thread_index, vlib_buffer_t * b0,
1039 ip4_header_t * ip0, u8 * p_proto,
1040 snat_session_key_t * p_value,
1041 u8 * p_dont_translate, void *d, void *e);
1043 /* ICMP endpoint-dependent session match functions */
1044 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1045 u32 thread_index, vlib_buffer_t * b0,
1046 ip4_header_t * ip0, u8 * p_proto,
1047 snat_session_key_t * p_value,
1048 u8 * p_dont_translate, void *d, void *e);
1049 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1050 u32 thread_index, vlib_buffer_t * b0,
1051 ip4_header_t * ip0, u8 * p_proto,
1052 snat_session_key_t * p_value,
1053 u8 * p_dont_translate, void *d, void *e);
1055 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1056 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1057 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1060 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1061 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1062 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1065 /* hairpinning functions */
1066 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1067 ip4_header_t * ip0, icmp46_header_t * icmp0,
1069 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1071 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1073 int snat_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1074 ip4_header_t * ip0, udp_header_t * udp0,
1075 tcp_header_t * tcp0, u32 proto0, int is_ed);
1077 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1078 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1079 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1080 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1081 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1084 * @brief Add external address to NAT44 pool
1086 * @param sm snat global configuration data
1087 * @param addr IPv4 address
1088 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1089 * @param twice_nat 1 if twice NAT address
1091 * @return 0 on success, non-zero value otherwise
1093 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1097 * @brief Delete external address from NAT44 pool
1099 * @param sm snat global configuration data
1100 * @param addr IPv4 address
1101 * @param delete_sm 1 if delete static mapping using address
1102 * @param twice_nat 1 if twice NAT address
1104 * @return 0 on success, non-zero value otherwise
1106 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1110 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1112 * @param addr IPv4 address
1113 * @param is_add 1 = add, 0 = delete
1115 * @return 0 on success, non-zero value otherwise
1117 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1120 * @brief Add/delete NAT44 static mapping
1122 * @param l_addr local IPv4 address
1123 * @param e_addr external IPv4 address
1124 * @param l_port local port number
1125 * @param e_port external port number
1126 * @param vrf_id local VRF ID
1127 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1128 * @param sw_if_index use interface address as external IPv4 address
1129 * @param proto L4 protocol
1130 * @param is_add 1 = add, 0 = delete
1131 * @param twice_nat twice-nat mode
1132 * @param out2in_only if 1 rule match only out2in direction
1133 * @param tag opaque string tag
1134 * @param identity_nat identity NAT
1136 * @return 0 on success, non-zero value otherwise
1138 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1139 u16 l_port, u16 e_port, u32 vrf_id,
1140 int addr_only, u32 sw_if_index,
1141 nat_protocol_t proto, int is_add,
1142 twice_nat_type_t twice_nat, u8 out2in_only,
1143 u8 * tag, u8 identity_nat);
1146 * @brief Add/delete static mapping with load-balancing (multiple backends)
1148 * @param e_addr external IPv4 address
1149 * @param e_port external port number
1150 * @param proto L4 protocol
1151 * @param locals list of local backends
1152 * @param is_add 1 = add, 0 = delete
1153 * @param twice_nat twice-nat mode
1154 * @param out2in_only if 1 rule match only out2in direction
1155 * @param tag opaque string tag
1156 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1158 * @return 0 on success, non-zero value otherwise
1160 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1161 nat_protocol_t proto,
1162 nat44_lb_addr_port_t * locals, u8 is_add,
1163 twice_nat_type_t twice_nat,
1164 u8 out2in_only, u8 * tag, u32 affinity);
1166 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1167 ip4_address_t l_addr, u16 l_port,
1168 nat_protocol_t proto, u32 vrf_id,
1169 u8 probability, u8 is_add);
1171 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1174 * @brief Set NAT plugin workers
1176 * @param bitmap NAT workers bitmap
1178 * @return 0 on success, non-zero value otherwise
1180 int snat_set_workers (uword * bitmap);
1183 * @brief Enable/disable NAT44 feature on the interface
1185 * @param sw_if_index software index of the interface
1186 * @param is_inside 1 = inside, 0 = outside
1187 * @param is_del 1 = delete, 0 = add
1189 * @return 0 on success, non-zero value otherwise
1191 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1194 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1196 * @param sw_if_index software index of the interface
1197 * @param is_inside 1 = inside, 0 = outside
1198 * @param is_del 1 = delete, 0 = add
1200 * @return 0 on success, non-zero value otherwise
1202 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1206 * @brief Add/delete NAT44 pool address from specific interface
1208 * @param sw_if_index software index of the interface
1209 * @param is_del 1 = delete, 0 = add
1210 * @param twice_nat 1 = twice NAT address for external hosts
1212 * @return 0 on success, non-zero value otherwise
1214 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1218 * @brief Delete NAT44 session
1220 * @param addr IPv4 address
1221 * @param port L4 port number
1222 * @param proto L4 protocol
1223 * @param vrf_id VRF ID
1224 * @param is_in 1 = inside network address and port pair, 0 = outside
1226 * @return 0 on success, non-zero value otherwise
1228 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1229 nat_protocol_t proto, u32 vrf_id, int is_in);
1232 * @brief Delete NAT44 endpoint-dependent session
1234 * @param sm snat global configuration data
1235 * @param addr IPv4 address
1236 * @param port L4 port number
1237 * @param proto L4 protocol
1238 * @param vrf_id VRF ID
1239 * @param is_in 1 = inside network address and port pair, 0 = outside
1241 * @return 0 on success, non-zero value otherwise
1243 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1244 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1245 u32 vrf_id, int is_in);
1248 * @brief Free NAT44 session data (lookup keys, external address port)
1250 * @param sm snat global configuration data
1251 * @param s NAT session
1252 * @param thread_index thread index
1253 * @param is_ha is HA event
1255 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1256 u32 thread_index, u8 is_ha);
1259 * @brief Set NAT44 session limit (session limit, vrf id)
1261 * @param session_limit Session limit
1262 * @param vrf_id VRF id
1263 * @return 0 on success, non-zero value otherwise
1265 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1268 * @brief Free NAT44 ED session data (lookup keys, external address port)
1270 * @param s NAT session
1271 * @param thread_index thread index
1272 * @param is_ha is HA event
1275 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1276 u32 thread_index, u8 is_ha);
1279 * @brief Initialize NAT44 data
1281 * @param tsm per thread data
1283 void nat44_db_init (snat_main_per_thread_data_t * tsm);
1286 * @brief Free NAT44 data
1288 * @param tsm per thread data
1290 void nat44_db_free (snat_main_per_thread_data_t * tsm);
1292 void nat44_sessions_clear ();
1295 * @brief Find or create NAT user
1297 * @param sm snat global configuration data
1298 * @param addr IPv4 address
1299 * @param fib_index FIB table index
1300 * @param thread_index thread index
1302 * @return NAT user data structure on success otherwise zero value
1304 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1305 ip4_address_t * addr, u32 fib_index,
1309 * @brief Allocate new NAT session or recycle last used
1311 * @param sm snat global configuration data
1313 * @param thread_index thread index
1314 * @param now time now
1316 * @return session data structure on success otherwise zero value
1318 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1320 u32 thread_index, f64 now);
1323 * @brief Set address and port assignment algorithm for MAP-E CE
1325 * @param psid Port Set Identifier value
1326 * @param psid_offset number of offset bits
1327 * @param psid_length length of PSID
1329 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1333 * @brief Set address and port assignment algorithm for port range
1335 * @param start_port beginning of the port range
1336 * @param end_port end of the port range
1338 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1341 * @brief Set address and port assignment algorithm to default/standard
1343 void nat_set_alloc_addr_and_port_default (void);
1346 * @brief Free outside address and port pair
1348 * @param addresses vector of outside addresses
1349 * @param thread_index thread index
1350 * @param k address, port and protocol
1352 void snat_free_outside_address_and_port (snat_address_t * addresses,
1354 snat_session_key_t * k);
1357 * @brief Alloc outside address and port
1359 * @param addresses vector of outside addresses
1360 * @param fib_index FIB table index
1361 * @param thread_index thread index
1362 * @param k allocated address and port pair
1363 * @param port_per_thread number of ports per thread
1364 * @param snat_thread_index NAT thread index
1366 * @return 0 on success, non-zero value otherwise
1368 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1371 snat_session_key_t * k,
1372 u16 port_per_thread,
1373 u32 snat_thread_index);
1376 * @brief Match NAT44 static mapping.
1378 * @param match address and port to match
1379 * @param mapping external/local address and port of the matched mapping
1380 * @param by_external if 0 match by local address otherwise match by external
1382 * @param is_addr_only 1 if matched mapping is address only
1383 * @param twice_nat matched mapping is twice NAT type
1384 * @param lb 1 if matched mapping is load-balanced
1385 * @param ext_host_addr external host address
1387 * @returns 0 if match found otherwise 1.
1389 int snat_static_mapping_match (snat_main_t * sm,
1390 snat_session_key_t match,
1391 snat_session_key_t * mapping,
1394 twice_nat_type_t * twice_nat,
1396 ip4_address_t * ext_host_addr,
1397 u8 * is_identity_nat);
1400 * @brief Add/del NAT address to FIB.
1402 * Add the external NAT address to the FIB as receive entries. This ensures
1403 * that VPP will reply to ARP for this address and we don't need to enable
1404 * proxy ARP on the outside interface.
1406 * @param addr IPv4 address
1407 * @param plen address prefix length
1408 * @param sw_if_index software index of the outside interface
1409 * @param is_add 0 = delete, 1 = add.
1411 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1412 u8 p_len, u32 sw_if_index, int is_add);
1415 * Why is this here? Because we don't need to touch this layer to
1416 * simply reply to an icmp. We need to change id to a unique
1417 * value to NAT an echo request/reply.
1424 } icmp_echo_header_t;
1428 u16 src_port, dst_port;
1431 u8 *format_user_kvp (u8 * s, va_list * args);
1432 #endif /* __included_nat_h__ */
1434 * fd.io coding-style-patch-verification: ON
1437 * eval: (c-set-style "gnu")