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_FAST_PATH,
58 NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
59 NAT_NEXT_OUT2IN_ED_FAST_PATH,
60 NAT_NEXT_OUT2IN_ED_SLOW_PATH,
61 NAT_NEXT_IN2OUT_CLASSIFY,
62 NAT_NEXT_OUT2IN_CLASSIFY,
72 /* user (internal host) key */
86 /* External address and port allocation modes */
87 #define foreach_nat_addr_and_port_alloc_alg \
88 _(0, DEFAULT, "default") \
90 _(2, RANGE, "port-range")
94 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
95 foreach_nat_addr_and_port_alloc_alg
97 } nat_addr_and_port_alloc_alg_t;
100 #define foreach_snat_session_state \
101 _(0, UNKNOWN, "unknown") \
102 _(1, UDP_ACTIVE, "udp-active") \
103 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
104 _(3, TCP_ESTABLISHED, "tcp-established") \
105 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
106 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
107 _(6, TCP_CLOSING, "tcp-closing") \
108 _(7, TCP_LAST_ACK, "tcp-last-ack") \
109 _(8, TCP_CLOSED, "tcp-closed") \
110 _(9, ICMP_ACTIVE, "icmp-active")
114 #define _(v, N, s) SNAT_SESSION_##N = v,
115 foreach_snat_session_state
117 } snat_session_state_t;
119 #define foreach_nat_in2out_ed_error \
120 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
121 _(OUT_OF_PORTS, "out of ports") \
122 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
123 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
124 _(NON_SYN, "non-SYN packet try to create session") \
125 _(TCP_CLOSED, "drops due to TCP in transitory timeout")
129 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
130 foreach_nat_in2out_ed_error
132 NAT_IN2OUT_ED_N_ERROR,
133 } nat_in2out_ed_error_t;
135 #define foreach_nat_out2in_ed_error \
136 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
137 _(OUT_OF_PORTS, "out of ports") \
138 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
139 _(NO_TRANSLATION, "no translation") \
140 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
141 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
142 _(CANNOT_CREATE_USER, "cannot create NAT user") \
143 _(NON_SYN, "non-SYN packet try to create session") \
144 _(TCP_CLOSED, "drops due to TCP in transitory timeout")
148 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
149 foreach_nat_out2in_ed_error
151 NAT_OUT2IN_ED_N_ERROR,
152 } nat_out2in_ed_error_t;
155 /* Endpoint dependent TCP session state */
156 #define NAT44_SES_I2O_FIN 1
157 #define NAT44_SES_O2I_FIN 2
158 #define NAT44_SES_I2O_FIN_ACK 4
159 #define NAT44_SES_O2I_FIN_ACK 8
160 #define NAT44_SES_I2O_SYN 16
161 #define NAT44_SES_O2I_SYN 32
162 #define NAT44_SES_RST 64
165 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
166 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
167 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
168 #define SNAT_SESSION_FLAG_TWICE_NAT 8
169 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
170 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
171 #define SNAT_SESSION_FLAG_AFFINITY 64
172 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
173 #define SNAT_SESSION_FLAG_EXACT_ADDRESS 256
175 /* NAT interface flags */
176 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
177 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
179 /* Static mapping flags */
180 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
181 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
182 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
183 #define NAT_STATIC_MAPPING_FLAG_LB 8
184 #define NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS 16
187 typedef CLIB_PACKED(struct
189 // number of sessions in this vrf
195 // is this vrf expired
197 }) per_vrf_sessions_t;
201 typedef CLIB_PACKED(struct
203 /* Outside network tuple */
211 /* Inside network tuple */
219 nat_protocol_t nat_proto;
224 /* Per-user translations */
226 u32 per_user_list_head_index;
228 /* head of LRU list in which this session is tracked */
230 /* index in global LRU list */
234 /* Last heard timer */
237 /* Last HA refresh */
238 f64 ha_last_refreshed;
244 /* External host address and port */
245 ip4_address_t ext_host_addr;
248 /* External host address and port after translation */
249 ip4_address_t ext_host_nat_addr;
250 u16 ext_host_nat_port;
252 /* TCP session state */
256 u64 tcp_closed_timestamp;
261 /* per vrf sessions index */
262 u32 per_vrf_sessions_index;
271 u32 sessions_per_user_list_head_index;
281 #define _(N, i, n, s) \
282 u16 busy_##n##_ports; \
283 u16 * busy_##n##_ports_per_thread; \
284 u32 busy_##n##_port_refcounts[65535];
304 /* backend IP address */
306 /* backend port number */
308 /* probability of the backend to be randomly matched */
311 /* backend FIB table */
314 } nat44_lb_addr_port_t;
318 /* twice-nat disabled */
320 /* twice-nat enabled */
322 /* twice-nat only when src IP equals dst IP after translation */
328 /* no load-balancing */
332 /* load-balancing with affinity */
338 /* prefered pool address */
339 ip4_address_t pool_addr;
340 /* local IP address */
341 ip4_address_t local_addr;
342 /* external IP address */
343 ip4_address_t external_addr;
349 twice_nat_type_t twice_nat;
350 /* local FIB table */
354 nat_protocol_t proto;
355 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
357 /* worker threads used by backends/local host */
359 /* opaque string tag */
361 /* backends for load-balancing mode */
362 nat44_lb_addr_port_t *locals;
363 /* affinity per service lis */
364 u32 affinity_per_service_list_head_index;
367 } snat_static_mapping_t;
377 ip4_address_t l_addr;
378 ip4_address_t pool_addr;
383 nat_protocol_t proto;
392 } snat_static_map_resolve_t;
396 /* Main lookup tables */
397 clib_bihash_8_8_t out2in;
398 clib_bihash_8_8_t in2out;
400 /* Endpoint dependent sessions lookup tables */
401 clib_bihash_16_8_t in2out_ed;
403 /* Find-a-user => src address lookup */
404 clib_bihash_8_8_t user_hash;
410 snat_session_t *sessions;
412 /* Pool of doubly-linked list elements */
413 dlist_elt_t *list_pool;
415 /* LRU session list - head is stale, tail is fresh */
416 dlist_elt_t *lru_pool;
417 u32 tcp_trans_lru_head_index;
418 u32 tcp_estab_lru_head_index;
419 u32 udp_lru_head_index;
420 u32 icmp_lru_head_index;
421 u32 unk_proto_lru_head_index;
423 /* NAT thread index */
424 u32 snat_thread_index;
426 /* real thread index */
429 per_vrf_sessions_t *per_vrf_sessions_vec;
431 } snat_main_per_thread_data_t;
435 /* ICMP session match function */
436 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
437 vlib_node_runtime_t * node,
441 ip4_address_t * addr,
444 nat_protocol_t * proto,
446 u8 * dont_translate);
448 /* Return worker thread index for given packet */
449 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
453 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
458 /* NAT address and port allocation function */
459 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
463 nat_protocol_t proto,
464 ip4_address_t * addr,
467 u32 snat_thread_index);
469 #define foreach_nat_counter _ (tcp) _ (udp) _ (icmp) _ (other) _ (drops)
471 typedef struct snat_main_s
473 /* ICMP session match functions */
474 snat_icmp_match_function_t *icmp_match_in2out_cb;
475 snat_icmp_match_function_t *icmp_match_out2in_cb;
477 /* Thread settings */
479 u32 first_worker_index;
481 snat_get_worker_in2out_function_t *worker_in2out_cb;
482 snat_get_worker_out2in_function_t *worker_out2in_cb;
485 /* Per thread data */
486 snat_main_per_thread_data_t *per_thread_data;
488 /* Find a static mapping by local */
489 clib_bihash_8_8_t static_mapping_by_local;
491 /* Find a static mapping by external */
492 clib_bihash_8_8_t static_mapping_by_external;
494 /* Static mapping pool */
495 snat_static_mapping_t *static_mappings;
497 /* Endpoint-dependent out2in mappings */
498 clib_bihash_16_8_t out2in_ed;
501 snat_interface_t *interfaces;
502 snat_interface_t *output_feature_interfaces;
504 /* Vector of outside addresses */
505 snat_address_t *addresses;
506 /* Address and port allocation function */
507 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
508 /* Address and port allocation type */
509 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
510 /* Port set parameters (MAP-E) */
514 /* Port range parameters */
521 /* vector of outside fibs */
522 nat_outside_fib_t *outside_fibs;
524 /* Vector of twice NAT addresses for external hosts */
525 snat_address_t *twice_nat_addresses;
527 /* sw_if_indices whose intfc addresses should be auto-added */
528 u32 *auto_add_sw_if_indices;
529 u32 *auto_add_sw_if_indices_twice_nat;
531 /* vector of interface address static mappings to resolve. */
532 snat_static_map_resolve_t *to_resolve;
534 /* Randomize port allocation order */
537 /* Worker handoff frame-queue index */
539 u32 fq_in2out_output_index;
543 u32 error_node_index;
545 /* handoff fq nodes */
546 u32 handoff_out2in_index;
547 u32 handoff_in2out_index;
548 u32 handoff_in2out_output_index;
550 /* respect feature arc nodes */
551 u32 pre_out2in_node_index;
552 u32 pre_in2out_node_index;
554 u32 in2out_node_index;
555 u32 in2out_output_node_index;
556 u32 in2out_fast_node_index;
557 u32 in2out_slowpath_node_index;
558 u32 in2out_slowpath_output_node_index;
559 u32 ed_in2out_node_index;
560 u32 ed_in2out_slowpath_node_index;
561 u32 out2in_node_index;
562 u32 out2in_fast_node_index;
563 u32 ed_out2in_node_index;
564 u32 ed_out2in_slowpath_node_index;
566 u32 hairpinning_node_index;
567 u32 hairpin_dst_node_index;
568 u32 hairpin_src_node_index;
569 u32 ed_hairpinning_node_index;
570 u32 ed_hairpin_dst_node_index;
571 u32 ed_hairpin_src_node_index;
573 /* If forwarding is enabled */
574 u8 forwarding_enabled;
576 /* Config parameters */
577 u8 static_mapping_only;
578 u8 static_mapping_connection_tracking;
580 u8 endpoint_dependent;
582 /* Is translation memory size calculated or user defined */
583 u8 translation_memory_size_set;
585 u32 translation_buckets;
586 uword translation_memory_size;
587 u32 max_translations_per_thread;
588 u32 *max_translations_per_fib;
589 u32 max_users_per_thread;
591 uword user_memory_size;
592 u32 max_translations_per_user;
595 u32 outside_fib_index;
597 u32 inside_fib_index;
599 /* values of various timeouts */
602 u32 tcp_transitory_timeout;
603 u32 tcp_established_timeout;
606 /* TCP MSS clamping */
609 /* counters/gauges */
610 vlib_simple_counter_main_t total_users;
611 vlib_simple_counter_main_t total_sessions;
612 vlib_simple_counter_main_t user_limit_reached;
614 #define _(x) vlib_simple_counter_main_t x;
663 vlib_simple_counter_main_t hairpinning;
667 /* API message ID base */
671 vlib_log_class_t log_class;
676 vnet_main_t *vnet_main;
677 ip4_main_t *ip4_main;
678 ip_lookup_main_t *ip4_lookup_main;
679 api_main_t *api_main;
686 } nat44_is_idle_session_ctx_t;
690 u32 cached_sw_if_index;
691 u32 cached_ip4_address;
694 extern snat_main_t snat_main;
696 // nat pre ed next_node feature classification
697 extern vlib_node_registration_t nat_default_node;
698 extern vlib_node_registration_t nat_pre_in2out_node;
699 extern vlib_node_registration_t nat_pre_out2in_node;
701 extern vlib_node_registration_t snat_in2out_node;
702 extern vlib_node_registration_t snat_in2out_output_node;
703 extern vlib_node_registration_t snat_out2in_node;
704 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
705 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
706 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
707 extern vlib_node_registration_t nat44_ed_in2out_node;
708 extern vlib_node_registration_t nat44_ed_in2out_output_node;
709 extern vlib_node_registration_t nat44_ed_out2in_node;
711 extern fib_source_t nat_fib_src_hi;
712 extern fib_source_t nat_fib_src_low;
714 /* format functions */
715 format_function_t format_snat_user;
716 format_function_t format_snat_static_mapping;
717 format_function_t format_snat_static_map_to_resolve;
718 format_function_t format_snat_session;
719 format_function_t format_snat_key;
720 format_function_t format_static_mapping_key;
721 format_function_t format_nat_protocol;
722 format_function_t format_nat_addr_and_port_alloc_alg;
723 /* unformat functions */
724 unformat_function_t unformat_nat_protocol;
726 /** \brief Check if SNAT session is created from static mapping.
727 @param s SNAT session
728 @return 1 if SNAT session is created from static mapping otherwise 0
730 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
732 /** \brief Check if SNAT session for unknown protocol.
733 @param s SNAT session
734 @return 1 if SNAT session for unknown protocol otherwise 0
736 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
738 /** \brief Check if NAT session is twice NAT.
740 @return 1 if NAT session is twice NAT
742 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
744 /** \brief Check if NAT session is load-balancing.
746 @return 1 if NAT session is load-balancing
748 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
750 /** \brief Check if NAT session is forwarding bypass.
752 @return 1 if NAT session is load-balancing
754 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
756 /** \brief Check if NAT session is endpoint dependent.
758 @return 1 if NAT session is endpoint dependent
760 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
762 /** \brief Check if NAT session has affinity record.
764 @return 1 if NAT session has affinity record
766 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
768 /** \brief Check if exact pool address should be used.
769 @param s SNAT session
770 @return 1 if exact pool address or 0
772 #define is_exact_address_session(s) (s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS)
774 /** \brief Check if NAT interface is inside.
775 @param i NAT interface
776 @return 1 if inside interface
778 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
780 /** \brief Check if NAT interface is outside.
781 @param i NAT interface
782 @return 1 if outside interface
784 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
786 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
788 @return 1 if session is closed
790 #define nat44_is_ses_closed(s) s->state == 0xf
792 /** \brief Check if NAT static mapping is address only (1:1NAT).
793 @param sm NAT static mapping
794 @return 1 if 1:1NAT, 0 if 1:1NAPT
796 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
798 /** \brief Check if NAT static mapping match only out2in direction.
799 @param sm NAT static mapping
800 @return 1 if rule match only out2in direction
802 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
804 /** \brief Check if NAT static mapping is identity NAT.
805 @param sm NAT static mapping
806 @return 1 if identity NAT
808 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
810 /** \brief Check if NAT static mapping is load-balancing.
811 @param sm NAT static mapping
812 @return 1 if load-balancing
814 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
816 /** \brief Check if exact pool address should be used.
817 @param s SNAT session
818 @return 1 if exact pool address or 0
820 #define is_exact_address(s) (s->flags & NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS)
822 /** \brief Check if client initiating TCP connection (received SYN from client)
824 @return 1 if client initiating TCP connection
827 tcp_flags_is_init (u8 f)
829 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
833 #define nat_log_err(...) \
834 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
835 #define nat_log_warn(...) \
836 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
837 #define nat_log_notice(...) \
838 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
839 #define nat_log_info(...) \
840 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
841 #define nat_log_debug(...)\
842 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
844 /* NAT API Logging Levels */
845 #define foreach_nat_log_level \
848 _(0x02, LOG_WARNING) \
849 _(0x03, LOG_NOTICE) \
853 typedef enum nat_log_level_t_
855 #define _(n,f) SNAT_##f = n,
856 foreach_nat_log_level
860 #define nat_elog(_level, _str) \
863 snat_main_t *sm = &snat_main; \
864 if (PREDICT_FALSE (sm->log_level >= _level)) \
866 ELOG_TYPE_DECLARE (e) = \
868 .format = "nat-msg " _str, \
871 ELOG_DATA (&vlib_global_main.elog_main, e); \
875 #define nat_elog_addr(_level, _str, _addr) \
878 if (PREDICT_FALSE (sm->log_level >= _level)) \
880 ELOG_TYPE_DECLARE (e) = \
882 .format = "nat-msg " _str " %d.%d.%d.%d", \
883 .format_args = "i1i1i1i1", \
892 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
893 ed->oct4 = _addr >> 24; \
894 ed->oct3 = _addr >> 16; \
895 ed->oct2 = _addr >> 8; \
900 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
903 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
905 ELOG_TYPE_DECLARE (e) = \
907 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
908 " tid from: %d to: %d fib: %d", \
909 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
925 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
926 ed->src_oct1 = _src >> 24; \
927 ed->src_oct2 = _src >> 16; \
928 ed->src_oct3 = _src >> 8; \
929 ed->src_oct4 = _src; \
930 ed->dst_oct1 = _dst >> 24; \
931 ed->dst_oct2 = _dst >> 16; \
932 ed->dst_oct3 = _dst >> 8; \
933 ed->dst_oct4 = _dst; \
934 ed->ftid = vlib_get_thread_index (); \
940 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
943 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
945 ELOG_TYPE_DECLARE (e) = \
947 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
948 " tid:%d prt:%d fib:%d", \
949 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
965 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
966 ed->src_oct1 = _src >> 24; \
967 ed->src_oct2 = _src >> 16; \
968 ed->src_oct3 = _src >> 8; \
969 ed->src_oct4 = _src; \
970 ed->dst_oct1 = _dst >> 24; \
971 ed->dst_oct2 = _dst >> 16; \
972 ed->dst_oct3 = _dst >> 8; \
973 ed->dst_oct4 = _dst; \
974 ed->tid = vlib_get_thread_index (); \
980 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
983 snat_main_t *sm = &snat_main; \
984 if (PREDICT_FALSE (sm->log_level >= _level)) \
986 ELOG_TYPE_DECLARE (e) = \
988 .format = "nat-msg " _fmt, \
989 .format_args = _arg, \
993 typeof (_val1) val1; \
995 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
1000 #define nat_elog_notice(nat_elog_str) \
1001 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
1002 #define nat_elog_warn(nat_elog_str) \
1003 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1004 #define nat_elog_err(nat_elog_str) \
1005 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1006 #define nat_elog_debug(nat_elog_str) \
1007 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1008 #define nat_elog_info(nat_elog_str) \
1009 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1011 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1012 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1013 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1014 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1015 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1016 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1017 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1018 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1019 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1020 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1022 /* ICMP session match functions */
1023 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1024 u32 thread_index, vlib_buffer_t * b0,
1025 ip4_header_t * ip0, ip4_address_t * addr,
1026 u16 * port, u32 * fib_index,
1027 nat_protocol_t * proto, void *d, void *e,
1028 u8 * dont_translate);
1029 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1030 u32 thread_index, vlib_buffer_t * b0,
1031 ip4_header_t * ip0, ip4_address_t * addr,
1032 u16 * port, u32 * fib_index,
1033 nat_protocol_t * proto, void *d, void *e,
1034 u8 * dont_translate);
1035 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1036 u32 thread_index, vlib_buffer_t * b0,
1037 ip4_header_t * ip0, ip4_address_t * addr,
1038 u16 * port, u32 * fib_index,
1039 nat_protocol_t * proto, void *d, void *e,
1040 u8 * dont_translate);
1041 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1042 u32 thread_index, vlib_buffer_t * b0,
1043 ip4_header_t * ip0, ip4_address_t * addr,
1044 u16 * port, u32 * fib_index,
1045 nat_protocol_t * proto, void *d, void *e,
1046 u8 * dont_translate);
1048 /* ICMP endpoint-dependent session match functions */
1049 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1050 u32 thread_index, vlib_buffer_t * b0,
1051 ip4_header_t * ip0, ip4_address_t * addr,
1052 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1053 void *d, void *e, u8 * dont_translate);
1054 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1055 u32 thread_index, vlib_buffer_t * b0,
1056 ip4_header_t * ip0, ip4_address_t * addr,
1057 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1058 void *d, void *e, u8 * dont_translate);
1060 u32 icmp_in2out (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 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1066 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1067 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1070 /* hairpinning functions */
1071 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1072 ip4_header_t * ip0, icmp46_header_t * icmp0,
1074 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1076 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1078 int snat_hairpinning (vlib_main_t * vm, vlib_node_runtime_t * node,
1079 snat_main_t * sm, vlib_buffer_t * b0,
1080 ip4_header_t * ip0, udp_header_t * udp0,
1081 tcp_header_t * tcp0, u32 proto0, int is_ed,
1084 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1085 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1086 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1087 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1088 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1091 * @brief Add external address to NAT44 pool
1093 * @param sm snat global configuration data
1094 * @param addr IPv4 address
1095 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1096 * @param twice_nat 1 if twice NAT address
1098 * @return 0 on success, non-zero value otherwise
1100 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1104 * @brief Delete external address from NAT44 pool
1106 * @param sm snat global configuration data
1107 * @param addr IPv4 address
1108 * @param delete_sm 1 if delete static mapping using address
1109 * @param twice_nat 1 if twice NAT address
1111 * @return 0 on success, non-zero value otherwise
1113 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1117 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1119 * @param addr IPv4 address
1120 * @param is_add 1 = add, 0 = delete
1122 * @return 0 on success, non-zero value otherwise
1124 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1127 * @brief Add/delete NAT44 static mapping
1129 * @param l_addr local IPv4 address
1130 * @param e_addr external IPv4 address
1131 * @param l_port local port number
1132 * @param e_port external port number
1133 * @param vrf_id local VRF ID
1134 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1135 * @param sw_if_index use interface address as external IPv4 address
1136 * @param proto L4 protocol
1137 * @param is_add 1 = add, 0 = delete
1138 * @param twice_nat twice-nat mode
1139 * @param out2in_only if 1 rule match only out2in direction
1140 * @param tag opaque string tag
1141 * @param identity_nat identity NAT
1142 * @param pool_addr pool IPv4 address
1143 * @param exact 1 = exact pool address
1145 * @return 0 on success, non-zero value otherwise
1147 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1148 u16 l_port, u16 e_port, u32 vrf_id,
1149 int addr_only, u32 sw_if_index,
1150 nat_protocol_t proto, int is_add,
1151 twice_nat_type_t twice_nat, u8 out2in_only,
1152 u8 * tag, u8 identity_nat,
1153 ip4_address_t pool_addr, int exact);
1156 * @brief Add/delete static mapping with load-balancing (multiple backends)
1158 * @param e_addr external IPv4 address
1159 * @param e_port external port number
1160 * @param proto L4 protocol
1161 * @param locals list of local backends
1162 * @param is_add 1 = add, 0 = delete
1163 * @param twice_nat twice-nat mode
1164 * @param out2in_only if 1 rule match only out2in direction
1165 * @param tag opaque string tag
1166 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1168 * @return 0 on success, non-zero value otherwise
1170 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1171 nat_protocol_t proto,
1172 nat44_lb_addr_port_t * locals, u8 is_add,
1173 twice_nat_type_t twice_nat,
1174 u8 out2in_only, u8 * tag, u32 affinity);
1176 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1177 ip4_address_t l_addr, u16 l_port,
1178 nat_protocol_t proto, u32 vrf_id,
1179 u8 probability, u8 is_add);
1181 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1184 * @brief Set NAT plugin workers
1186 * @param bitmap NAT workers bitmap
1188 * @return 0 on success, non-zero value otherwise
1190 int snat_set_workers (uword * bitmap);
1193 * @brief Enable/disable NAT44 feature on the interface
1195 * @param sw_if_index software index of the interface
1196 * @param is_inside 1 = inside, 0 = outside
1197 * @param is_del 1 = delete, 0 = add
1199 * @return 0 on success, non-zero value otherwise
1201 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1204 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1206 * @param sw_if_index software index of the interface
1207 * @param is_inside 1 = inside, 0 = outside
1208 * @param is_del 1 = delete, 0 = add
1210 * @return 0 on success, non-zero value otherwise
1212 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1216 * @brief Add/delete NAT44 pool address from specific interface
1218 * @param sw_if_index software index of the interface
1219 * @param is_del 1 = delete, 0 = add
1220 * @param twice_nat 1 = twice NAT address for external hosts
1222 * @return 0 on success, non-zero value otherwise
1224 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1228 * @brief Delete NAT44 session
1230 * @param addr IPv4 address
1231 * @param port L4 port number
1232 * @param proto L4 protocol
1233 * @param vrf_id VRF ID
1234 * @param is_in 1 = inside network address and port pair, 0 = outside
1236 * @return 0 on success, non-zero value otherwise
1238 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1239 nat_protocol_t proto, u32 vrf_id, int is_in);
1242 * @brief Delete NAT44 endpoint-dependent session
1244 * @param sm snat global configuration data
1245 * @param addr IPv4 address
1246 * @param port L4 port number
1247 * @param proto L4 protocol
1248 * @param vrf_id VRF ID
1249 * @param is_in 1 = inside network address and port pair, 0 = outside
1251 * @return 0 on success, non-zero value otherwise
1253 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1254 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1255 u32 vrf_id, int is_in);
1258 * @brief Free NAT44 session data (lookup keys, external address port)
1260 * @param sm snat global configuration data
1261 * @param s NAT session
1262 * @param thread_index thread index
1263 * @param is_ha is HA event
1265 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1266 u32 thread_index, u8 is_ha);
1269 * @brief Set NAT44 session limit (session limit, vrf id)
1271 * @param session_limit Session limit
1272 * @param vrf_id VRF id
1273 * @return 0 on success, non-zero value otherwise
1275 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1278 * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
1280 * @param session_limit Session limit
1281 * @param vrf_id VRF id
1282 * @return 0 on success, non-zero value otherwise
1284 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
1286 * @brief Free NAT44 ED session data (lookup keys, external address port)
1288 * @param s NAT session
1289 * @param thread_index thread index
1290 * @param is_ha is HA event
1293 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1294 u32 thread_index, u8 is_ha);
1297 * @brief Initialize NAT44 data
1299 * @param tsm per thread data
1301 void nat44_db_init (snat_main_per_thread_data_t * tsm);
1304 * @brief Free NAT44 data
1306 * @param tsm per thread data
1308 void nat44_db_free (snat_main_per_thread_data_t * tsm);
1310 void nat44_sessions_clear ();
1313 * @brief Find or create NAT user
1315 * @param sm snat global configuration data
1316 * @param addr IPv4 address
1317 * @param fib_index FIB table index
1318 * @param thread_index thread index
1320 * @return NAT user data structure on success otherwise zero value
1322 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1323 ip4_address_t * addr, u32 fib_index,
1327 * @brief Allocate new NAT session or recycle last used
1329 * @param sm snat global configuration data
1331 * @param thread_index thread index
1332 * @param now time now
1334 * @return session data structure on success otherwise zero value
1336 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1338 u32 thread_index, f64 now);
1341 * @brief Set address and port assignment algorithm for MAP-E CE
1343 * @param psid Port Set Identifier value
1344 * @param psid_offset number of offset bits
1345 * @param psid_length length of PSID
1347 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1351 * @brief Set address and port assignment algorithm for port range
1353 * @param start_port beginning of the port range
1354 * @param end_port end of the port range
1356 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1359 * @brief Set address and port assignment algorithm to default/standard
1361 void nat_set_alloc_addr_and_port_default (void);
1364 * @brief Free outside address and port pair
1366 * @param addresses vector of outside addresses
1367 * @param thread_index thread index
1368 * @param key address, port and protocol
1371 snat_free_outside_address_and_port (snat_address_t * addresses,
1373 ip4_address_t * addr,
1374 u16 port, nat_protocol_t protocol);
1377 * @brief Alloc outside address and port
1379 * @param addresses vector of outside addresses
1380 * @param fib_index FIB table index
1381 * @param thread_index thread index
1382 * @param port_per_thread number of ports per thread
1383 * @param snat_thread_index NAT thread index
1385 * @return 0 on success, non-zero value otherwise
1387 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1390 nat_protocol_t proto,
1391 ip4_address_t * addr,
1393 u16 port_per_thread,
1394 u32 snat_thread_index);
1396 void expire_per_vrf_sessions (u32 fib_index);
1399 * @brief Match NAT44 static mapping.
1401 * @param key address and port to match
1402 * @param addr external/local address of the matched mapping
1403 * @param port port of the matched mapping
1404 * @param fib_index fib index of the matched mapping
1405 * @param by_external if 0 match by local address otherwise match by external
1407 * @param is_addr_only 1 if matched mapping is address only
1408 * @param twice_nat matched mapping is twice NAT type
1409 * @param lb 1 if matched mapping is load-balanced
1410 * @param ext_host_addr external host address
1411 * @param is_identity_nat 1 if indentity mapping
1412 * @param out if !=0 set to pointer of the mapping structure
1414 * @returns 0 if match found otherwise 1.
1416 int snat_static_mapping_match (snat_main_t * sm,
1417 ip4_address_t match_addr,
1419 u32 match_fib_index,
1420 nat_protocol_t match_protocol,
1421 ip4_address_t * mapping_addr,
1423 u32 * mapping_fib_index,
1426 twice_nat_type_t * twice_nat,
1428 ip4_address_t * ext_host_addr,
1429 u8 * is_identity_nat,
1430 snat_static_mapping_t ** out);
1433 * @brief Add/del NAT address to FIB.
1435 * Add the external NAT address to the FIB as receive entries. This ensures
1436 * that VPP will reply to ARP for this address and we don't need to enable
1437 * proxy ARP on the outside interface.
1439 * @param addr IPv4 address
1440 * @param plen address prefix length
1441 * @param sw_if_index software index of the outside interface
1442 * @param is_add 0 = delete, 1 = add.
1444 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1445 u8 p_len, u32 sw_if_index, int is_add);
1448 * Why is this here? Because we don't need to touch this layer to
1449 * simply reply to an icmp. We need to change id to a unique
1450 * value to NAT an echo request/reply.
1457 } icmp_echo_header_t;
1461 u16 src_port, dst_port;
1464 u8 *format_user_kvp (u8 * s, va_list * args);
1466 #endif /* __included_nat_h__ */
1468 * fd.io coding-style-patch-verification: ON
1471 * eval: (c-set-style "gnu")