2 * Copyright (c) 2020 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 /* number of worker handoff frame queue elements */
40 #define NAT_FQ_NELTS 64
42 /* NAT buffer flags */
43 #define SNAT_FLAG_HAIRPINNING (1 << 0)
45 /* NAT44 API Configuration flags */
46 #define foreach_nat44_config_flag \
47 _(0x00, IS_ENDPOINT_INDEPENDENT) \
48 _(0x01, IS_ENDPOINT_DEPENDENT) \
49 _(0x02, IS_STATIC_MAPPING_ONLY) \
50 _(0x04, IS_CONNECTION_TRACKING) \
51 _(0x08, IS_OUT2IN_DPO)
53 typedef enum nat44_config_flags_t_
55 #define _(n,f) NAT44_API_##f = n,
56 foreach_nat44_config_flag
58 } nat44_config_flags_t;
62 /* nat44 plugin features */
63 u8 static_mapping_only;
64 u8 connection_tracking;
65 u8 endpoint_dependent;
71 /* maximum number of users */
74 /* maximum number of sessions */
77 /* maximum number of ssessions per user */
85 NAT_NEXT_IN2OUT_ED_FAST_PATH,
86 NAT_NEXT_IN2OUT_ED_SLOW_PATH,
87 NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH,
88 NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
89 NAT_NEXT_OUT2IN_ED_FAST_PATH,
90 NAT_NEXT_OUT2IN_ED_SLOW_PATH,
91 NAT_NEXT_IN2OUT_CLASSIFY,
92 NAT_NEXT_OUT2IN_CLASSIFY,
102 /* user (internal host) key */
116 /* External address and port allocation modes */
117 #define foreach_nat_addr_and_port_alloc_alg \
118 _(0, DEFAULT, "default") \
119 _(1, MAPE, "map-e") \
120 _(2, RANGE, "port-range")
124 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
125 foreach_nat_addr_and_port_alloc_alg
127 } nat_addr_and_port_alloc_alg_t;
130 #define foreach_snat_session_state \
131 _(0, UNKNOWN, "unknown") \
132 _(1, UDP_ACTIVE, "udp-active") \
133 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
134 _(3, TCP_ESTABLISHED, "tcp-established") \
135 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
136 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
137 _(6, TCP_CLOSING, "tcp-closing") \
138 _(7, TCP_LAST_ACK, "tcp-last-ack") \
139 _(8, TCP_CLOSED, "tcp-closed") \
140 _(9, ICMP_ACTIVE, "icmp-active")
144 #define _(v, N, s) SNAT_SESSION_##N = v,
145 foreach_snat_session_state
147 } snat_session_state_t;
149 #define foreach_nat_in2out_ed_error \
150 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
151 _(OUT_OF_PORTS, "out of ports") \
152 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
153 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
154 _(NON_SYN, "non-SYN packet try to create session") \
155 _(TCP_CLOSED, "drops due to TCP in transitory timeout")
159 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
160 foreach_nat_in2out_ed_error
162 NAT_IN2OUT_ED_N_ERROR,
163 } nat_in2out_ed_error_t;
165 #define foreach_nat_out2in_ed_error \
166 _ (UNSUPPORTED_PROTOCOL, "unsupported protocol") \
167 _ (OUT_OF_PORTS, "out of ports") \
168 _ (BAD_ICMP_TYPE, "unsupported ICMP type") \
169 _ (NO_TRANSLATION, "no translation") \
170 _ (MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
171 _ (MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
172 _ (CANNOT_CREATE_USER, "cannot create NAT user") \
173 _ (NON_SYN, "non-SYN packet try to create session") \
174 _ (TCP_CLOSED, "drops due to TCP in transitory timeout") \
175 _ (HASH_ADD_FAILED, "hash table add failed")
179 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
180 foreach_nat_out2in_ed_error
182 NAT_OUT2IN_ED_N_ERROR,
183 } nat_out2in_ed_error_t;
186 /* Endpoint dependent TCP session state */
187 #define NAT44_SES_I2O_FIN 1
188 #define NAT44_SES_O2I_FIN 2
189 #define NAT44_SES_I2O_FIN_ACK 4
190 #define NAT44_SES_O2I_FIN_ACK 8
191 #define NAT44_SES_I2O_SYN 16
192 #define NAT44_SES_O2I_SYN 32
193 #define NAT44_SES_RST 64
196 #define SNAT_SESSION_FLAG_STATIC_MAPPING (1 << 0)
197 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO (1 << 1)
198 #define SNAT_SESSION_FLAG_LOAD_BALANCING (1 << 2)
199 #define SNAT_SESSION_FLAG_TWICE_NAT (1 << 3)
200 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT (1 << 4)
201 #define SNAT_SESSION_FLAG_FWD_BYPASS (1 << 5)
202 #define SNAT_SESSION_FLAG_AFFINITY (1 << 6)
203 #define SNAT_SESSION_FLAG_EXACT_ADDRESS (1 << 7)
204 #define SNAT_SESSION_FLAG_HAIRPINNING (1 << 8)
206 /* NAT interface flags */
207 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
208 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
210 /* Static mapping flags */
211 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
212 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
213 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
214 #define NAT_STATIC_MAPPING_FLAG_LB 8
215 #define NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS 16
218 typedef CLIB_PACKED(struct
220 // number of sessions in this vrf
226 // is this vrf expired
228 }) per_vrf_sessions_t;
233 ip4_address_t saddr, daddr;
242 #define NAT_FLOW_OP_SADDR_REWRITE (1 << 1)
243 #define NAT_FLOW_OP_SPORT_REWRITE (1 << 2)
244 #define NAT_FLOW_OP_DADDR_REWRITE (1 << 3)
245 #define NAT_FLOW_OP_DPORT_REWRITE (1 << 4)
246 #define NAT_FLOW_OP_ICMP_ID_REWRITE (1 << 5)
247 #define NAT_FLOW_OP_TXFIB_REWRITE (1 << 6)
256 nat_6t_flow_saddr_rewrite_set (nat_6t_flow_t *f, u32 saddr)
258 f->ops |= NAT_FLOW_OP_SADDR_REWRITE;
259 f->rewrite.saddr.as_u32 = saddr;
263 nat_6t_flow_daddr_rewrite_set (nat_6t_flow_t *f, u32 daddr)
265 f->ops |= NAT_FLOW_OP_DADDR_REWRITE;
266 f->rewrite.daddr.as_u32 = daddr;
270 nat_6t_flow_sport_rewrite_set (nat_6t_flow_t *f, u32 sport)
272 f->ops |= NAT_FLOW_OP_SPORT_REWRITE;
273 f->rewrite.sport = sport;
277 nat_6t_flow_dport_rewrite_set (nat_6t_flow_t *f, u32 dport)
279 f->ops |= NAT_FLOW_OP_DPORT_REWRITE;
280 f->rewrite.dport = dport;
284 nat_6t_flow_txfib_rewrite_set (nat_6t_flow_t *f, u32 tx_fib_index)
286 f->ops |= NAT_FLOW_OP_TXFIB_REWRITE;
287 f->rewrite.fib_index = tx_fib_index;
291 nat_6t_flow_icmp_id_rewrite_set (nat_6t_flow_t *f, u16 id)
293 f->ops |= NAT_FLOW_OP_ICMP_ID_REWRITE;
294 f->rewrite.icmp_id = id;
298 typedef CLIB_PACKED(struct
300 /* Outside network tuple */
308 /* Inside network tuple */
316 nat_protocol_t nat_proto;
324 /* Per-user translations */
326 u32 per_user_list_head_index;
328 /* head of LRU list in which this session is tracked */
330 /* index in global LRU list */
334 /* Last heard timer */
337 /* Last HA refresh */
338 f64 ha_last_refreshed;
344 /* External host address and port */
345 ip4_address_t ext_host_addr;
348 /* External host address and port after translation */
349 ip4_address_t ext_host_nat_addr;
350 u16 ext_host_nat_port;
352 /* TCP session state */
356 u64 tcp_closed_timestamp;
361 /* per vrf sessions index */
362 u32 per_vrf_sessions_index;
371 u32 sessions_per_user_list_head_index;
381 #define _(N, i, n, s) \
382 u32 busy_##n##_ports; \
383 u32 * busy_##n##_ports_per_thread; \
384 u32 busy_##n##_port_refcounts[65535];
404 /* backend IP address */
406 /* backend port number */
408 /* probability of the backend to be randomly matched */
411 /* backend FIB table */
414 } nat44_lb_addr_port_t;
418 /* twice-nat disabled */
420 /* twice-nat enabled */
422 /* twice-nat only when src IP equals dst IP after translation */
428 /* no load-balancing */
432 /* load-balancing with affinity */
438 /* prefered pool address */
439 ip4_address_t pool_addr;
440 /* local IP address */
441 ip4_address_t local_addr;
442 /* external IP address */
443 ip4_address_t external_addr;
449 twice_nat_type_t twice_nat;
450 /* local FIB table */
454 nat_protocol_t proto;
455 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
457 /* worker threads used by backends/local host */
459 /* opaque string tag */
461 /* backends for load-balancing mode */
462 nat44_lb_addr_port_t *locals;
463 /* affinity per service lis */
464 u32 affinity_per_service_list_head_index;
467 } snat_static_mapping_t;
477 ip4_address_t l_addr;
478 ip4_address_t pool_addr;
483 nat_protocol_t proto;
491 } snat_static_map_resolve_t;
495 /* Main lookup tables */
496 clib_bihash_8_8_t out2in;
497 clib_bihash_8_8_t in2out;
499 /* Find-a-user => src address lookup */
500 clib_bihash_8_8_t user_hash;
506 snat_session_t *sessions;
508 /* Pool of doubly-linked list elements */
509 dlist_elt_t *list_pool;
511 /* LRU session list - head is stale, tail is fresh */
512 dlist_elt_t *lru_pool;
513 u32 tcp_trans_lru_head_index;
514 u32 tcp_estab_lru_head_index;
515 u32 udp_lru_head_index;
516 u32 icmp_lru_head_index;
517 u32 unk_proto_lru_head_index;
519 /* NAT thread index */
520 u32 snat_thread_index;
522 /* real thread index */
525 per_vrf_sessions_t *per_vrf_sessions_vec;
527 } snat_main_per_thread_data_t;
531 /* ICMP session match function */
532 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
533 vlib_node_runtime_t * node,
537 ip4_address_t * addr,
540 nat_protocol_t * proto,
542 u8 * dont_translate);
544 /* Return worker thread index for given packet */
545 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
549 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
554 /* NAT address and port allocation function */
555 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
559 nat_protocol_t proto,
560 ip4_address_t * addr,
563 u32 snat_thread_index);
565 #define foreach_nat_counter _ (tcp) _ (udp) _ (icmp) _ (other) _ (drops)
567 typedef struct snat_main_s
569 /* ICMP session match functions */
570 snat_icmp_match_function_t *icmp_match_in2out_cb;
571 snat_icmp_match_function_t *icmp_match_out2in_cb;
573 /* Thread settings */
575 u32 first_worker_index;
577 snat_get_worker_in2out_function_t *worker_in2out_cb;
578 snat_get_worker_out2in_function_t *worker_out2in_cb;
581 /* Per thread data */
582 snat_main_per_thread_data_t *per_thread_data;
584 /* Find a static mapping by local */
585 clib_bihash_8_8_t static_mapping_by_local;
587 /* Find a static mapping by external */
588 clib_bihash_8_8_t static_mapping_by_external;
590 /* Static mapping pool */
591 snat_static_mapping_t *static_mappings;
593 /* Endpoint dependent lookup table */
594 clib_bihash_16_8_t flow_hash;
597 snat_interface_t *interfaces;
598 snat_interface_t *output_feature_interfaces;
600 /* Vector of outside addresses */
601 snat_address_t *addresses;
602 /* Address and port allocation function */
603 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
604 /* Address and port allocation type */
605 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
606 /* Port set parameters (MAP-E) */
610 /* Port range parameters */
617 /* vector of outside fibs */
618 nat_outside_fib_t *outside_fibs;
620 /* Vector of twice NAT addresses for external hosts */
621 snat_address_t *twice_nat_addresses;
623 /* sw_if_indices whose intfc addresses should be auto-added */
624 u32 *auto_add_sw_if_indices;
625 u32 *auto_add_sw_if_indices_twice_nat;
627 /* vector of interface address static mappings to resolve. */
628 snat_static_map_resolve_t *to_resolve;
630 /* Randomize port allocation order */
633 /* Worker handoff frame-queue index */
635 u32 fq_in2out_output_index;
639 u32 error_node_index;
641 /* handoff fq nodes */
642 u32 handoff_out2in_index;
643 u32 handoff_in2out_index;
644 u32 handoff_in2out_output_index;
646 /* respect feature arc nodes */
647 u32 pre_out2in_node_index;
648 u32 pre_in2out_node_index;
650 u32 out2in_node_index;
651 u32 in2out_node_index;
652 u32 in2out_output_node_index;
654 u32 in2out_fast_node_index;
655 u32 in2out_slowpath_node_index;
656 u32 in2out_slowpath_output_node_index;
657 u32 out2in_fast_node_index;
659 u32 ei_out2in_node_index;
660 u32 ei_in2out_node_index;
661 u32 ei_in2out_output_node_index;
663 u32 ed_out2in_node_index;
664 u32 ed_in2out_node_index;
665 u32 ed_in2out_output_node_index;
667 u32 ed_in2out_slowpath_node_index;
668 u32 ed_out2in_slowpath_node_index;
670 u32 hairpinning_node_index;
671 u32 hairpin_dst_node_index;
672 u32 hairpin_src_node_index;
674 nat44_config_t rconfig;
675 //nat44_config_t cconfig;
677 /* If forwarding is enabled */
678 u8 forwarding_enabled;
680 /* Config parameters */
681 u8 endpoint_dependent;
684 /* static mapping config */
685 u8 static_mapping_only;
686 u8 static_mapping_connection_tracking;
688 /* Is translation memory size calculated or user defined */
689 u8 translation_memory_size_set;
691 u32 translation_buckets;
692 u32 max_translations_per_thread;
693 u32 *max_translations_per_fib;
694 u32 max_users_per_thread;
696 u32 max_translations_per_user;
699 u32 outside_fib_index;
701 u32 inside_fib_index;
703 nat_timeouts_t timeouts;
705 /* TCP MSS clamping */
709 vlib_simple_counter_main_t total_users;
710 vlib_simple_counter_main_t total_sessions;
711 vlib_simple_counter_main_t user_limit_reached;
713 #define _(x) vlib_simple_counter_main_t x;
762 vlib_simple_counter_main_t hairpinning;
766 /* API message ID base */
770 vlib_log_class_t log_class;
775 api_main_t *api_main;
776 ip4_main_t *ip4_main;
777 ip_lookup_main_t *ip4_lookup_main;
779 fib_source_t fib_src_hi;
780 fib_source_t fib_src_low;
782 /* pat - dynamic mapping enabled or conneciton tracking */
785 /* nat44 plugin enabled */
788 vnet_main_t *vnet_main;
795 } nat44_is_idle_session_ctx_t;
799 u32 cached_sw_if_index;
800 u32 cached_ip4_address;
803 extern snat_main_t snat_main;
805 // nat pre ed next_node feature classification
806 extern vlib_node_registration_t nat_default_node;
807 extern vlib_node_registration_t nat_pre_in2out_node;
808 extern vlib_node_registration_t nat_pre_out2in_node;
810 extern vlib_node_registration_t snat_in2out_node;
811 extern vlib_node_registration_t snat_in2out_output_node;
812 extern vlib_node_registration_t snat_out2in_node;
813 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
814 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
815 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
816 extern vlib_node_registration_t nat44_ed_in2out_node;
817 extern vlib_node_registration_t nat44_ed_in2out_output_node;
818 extern vlib_node_registration_t nat44_ed_out2in_node;
820 extern fib_source_t nat_fib_src_hi;
821 extern fib_source_t nat_fib_src_low;
823 /* format functions */
824 format_function_t format_snat_user;
825 format_function_t format_snat_static_mapping;
826 format_function_t format_snat_static_map_to_resolve;
827 format_function_t format_snat_session;
828 format_function_t format_snat_key;
829 format_function_t format_static_mapping_key;
830 format_function_t format_nat_protocol;
831 format_function_t format_nat_addr_and_port_alloc_alg;
832 /* unformat functions */
833 unformat_function_t unformat_nat_protocol;
835 /** \brief Check if SNAT session is created from static mapping.
836 @param s SNAT session
837 @return 1 if SNAT session is created from static mapping otherwise 0
839 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
841 /** \brief Check if SNAT session for unknown protocol.
842 @param s SNAT session
843 @return 1 if SNAT session for unknown protocol otherwise 0
845 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
847 /** \brief Check if NAT session is twice NAT.
849 @return 1 if NAT session is twice NAT
851 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
853 /** \brief Check if NAT session is load-balancing.
855 @return 1 if NAT session is load-balancing
857 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
859 /** \brief Check if NAT session is forwarding bypass.
861 @return 1 if NAT session is load-balancing
863 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
865 /** \brief Check if NAT session is endpoint dependent.
867 @return 1 if NAT session is endpoint dependent
869 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
871 /** \brief Check if NAT session has affinity record.
873 @return 1 if NAT session has affinity record
875 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
877 /** \brief Check if exact pool address should be used.
878 @param s SNAT session
879 @return 1 if exact pool address or 0
881 #define is_exact_address_session(s) (s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS)
883 /** \brief Check if NAT interface is inside.
884 @param i NAT interface
885 @return 1 if inside interface
887 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
889 /** \brief Check if NAT interface is outside.
890 @param i NAT interface
891 @return 1 if outside interface
893 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
895 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
897 @return 1 if session is closed
899 #define nat44_is_ses_closed(s) s->state == 0xf
901 /** \brief Check if NAT static mapping is address only (1:1NAT).
902 @param sm NAT static mapping
903 @return 1 if 1:1NAT, 0 if 1:1NAPT
905 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
907 /** \brief Check if NAT static mapping match only out2in direction.
908 @param sm NAT static mapping
909 @return 1 if rule match only out2in direction
911 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
913 /** \brief Check if NAT static mapping is identity NAT.
914 @param sm NAT static mapping
915 @return 1 if identity NAT
917 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
919 /** \brief Check if NAT static mapping is load-balancing.
920 @param sm NAT static mapping
921 @return 1 if load-balancing
923 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
925 /** \brief Check if exact pool address should be used.
926 @param s SNAT session
927 @return 1 if exact pool address or 0
929 #define is_exact_address(s) (s->flags & NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS)
931 /** \brief Check if client initiating TCP connection (received SYN from client)
933 @return 1 if client initiating TCP connection
936 tcp_flags_is_init (u8 f)
938 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
942 #define nat_log_err(...) \
943 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
944 #define nat_log_warn(...) \
945 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
946 #define nat_log_notice(...) \
947 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
948 #define nat_log_info(...) \
949 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
950 #define nat_log_debug(...)\
951 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
953 /* NAT API Logging Levels */
954 #define foreach_nat_log_level \
957 _(0x02, LOG_WARNING) \
958 _(0x03, LOG_NOTICE) \
962 typedef enum nat_log_level_t_
964 #define _(n,f) SNAT_##f = n,
965 foreach_nat_log_level
969 #define nat_elog(_level, _str) \
972 snat_main_t *sm = &snat_main; \
973 if (PREDICT_FALSE (sm->log_level >= _level)) \
975 ELOG_TYPE_DECLARE (e) = \
977 .format = "nat-msg " _str, \
980 ELOG_DATA (&vlib_global_main.elog_main, e); \
984 #define nat_elog_addr(_level, _str, _addr) \
987 if (PREDICT_FALSE (sm->log_level >= _level)) \
989 ELOG_TYPE_DECLARE (e) = \
991 .format = "nat-msg " _str " %d.%d.%d.%d", \
992 .format_args = "i1i1i1i1", \
1001 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
1002 ed->oct4 = _addr >> 24; \
1003 ed->oct3 = _addr >> 16; \
1004 ed->oct2 = _addr >> 8; \
1009 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
1012 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
1014 ELOG_TYPE_DECLARE (e) = \
1016 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
1017 " tid from: %d to: %d fib: %d", \
1018 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
1020 CLIB_PACKED(struct \
1034 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
1035 ed->src_oct1 = _src >> 24; \
1036 ed->src_oct2 = _src >> 16; \
1037 ed->src_oct3 = _src >> 8; \
1038 ed->src_oct4 = _src; \
1039 ed->dst_oct1 = _dst >> 24; \
1040 ed->dst_oct2 = _dst >> 16; \
1041 ed->dst_oct3 = _dst >> 8; \
1042 ed->dst_oct4 = _dst; \
1043 ed->ftid = vlib_get_thread_index (); \
1049 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
1052 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
1054 ELOG_TYPE_DECLARE (e) = \
1056 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
1057 " tid:%d prt:%d fib:%d", \
1058 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
1060 CLIB_PACKED(struct \
1074 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
1075 ed->src_oct1 = _src >> 24; \
1076 ed->src_oct2 = _src >> 16; \
1077 ed->src_oct3 = _src >> 8; \
1078 ed->src_oct4 = _src; \
1079 ed->dst_oct1 = _dst >> 24; \
1080 ed->dst_oct2 = _dst >> 16; \
1081 ed->dst_oct3 = _dst >> 8; \
1082 ed->dst_oct4 = _dst; \
1083 ed->tid = vlib_get_thread_index (); \
1089 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
1092 snat_main_t *sm = &snat_main; \
1093 if (PREDICT_FALSE (sm->log_level >= _level)) \
1095 ELOG_TYPE_DECLARE (e) = \
1097 .format = "nat-msg " _fmt, \
1098 .format_args = _arg, \
1100 CLIB_PACKED(struct \
1102 typeof (_val1) val1; \
1104 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
1109 #define nat_elog_notice(nat_elog_str) \
1110 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
1111 #define nat_elog_warn(nat_elog_str) \
1112 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1113 #define nat_elog_err(nat_elog_str) \
1114 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1115 #define nat_elog_debug(nat_elog_str) \
1116 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1117 #define nat_elog_info(nat_elog_str) \
1118 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1120 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1121 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1122 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1123 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1124 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1125 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1126 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1127 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1128 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1129 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1131 /* ICMP session match functions */
1132 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1133 u32 thread_index, vlib_buffer_t * b0,
1134 ip4_header_t * ip0, ip4_address_t * addr,
1135 u16 * port, u32 * fib_index,
1136 nat_protocol_t * proto, void *d, void *e,
1137 u8 * dont_translate);
1138 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1139 u32 thread_index, vlib_buffer_t * b0,
1140 ip4_header_t * ip0, ip4_address_t * addr,
1141 u16 * port, u32 * fib_index,
1142 nat_protocol_t * proto, void *d, void *e,
1143 u8 * dont_translate);
1144 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1145 u32 thread_index, vlib_buffer_t * b0,
1146 ip4_header_t * ip0, ip4_address_t * addr,
1147 u16 * port, u32 * fib_index,
1148 nat_protocol_t * proto, void *d, void *e,
1149 u8 * dont_translate);
1150 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1151 u32 thread_index, vlib_buffer_t * b0,
1152 ip4_header_t * ip0, ip4_address_t * addr,
1153 u16 * port, u32 * fib_index,
1154 nat_protocol_t * proto, void *d, void *e,
1155 u8 * dont_translate);
1157 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1158 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1159 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1162 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1163 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1164 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1167 /* hairpinning functions */
1168 u32 snat_icmp_hairpinning (snat_main_t *sm, vlib_buffer_t *b0,
1169 ip4_header_t *ip0, icmp46_header_t *icmp0);
1171 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1173 int snat_hairpinning (vlib_main_t *vm, vlib_node_runtime_t *node,
1174 snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0,
1175 udp_header_t *udp0, tcp_header_t *tcp0, u32 proto0,
1178 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1179 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1180 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1184 * @brief Enable NAT44 plugin
1186 * @param c nat44_config_t
1188 * @return 0 on success, non-zero value otherwise
1190 int nat44_plugin_enable (nat44_config_t c);
1193 * @brief Disable NAT44 plugin
1195 * @return 0 on success, non-zero value otherwise
1197 int nat44_plugin_disable ();
1200 * @brief Add external address to NAT44 pool
1202 * @param sm snat global configuration data
1203 * @param addr IPv4 address
1204 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1205 * @param twice_nat 1 if twice NAT address
1207 * @return 0 on success, non-zero value otherwise
1209 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1213 * @brief Delete external address from NAT44 pool
1215 * @param sm snat global configuration data
1216 * @param addr IPv4 address
1217 * @param delete_sm 1 if delete static mapping using address
1218 * @param twice_nat 1 if twice NAT address
1220 * @return 0 on success, non-zero value otherwise
1222 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1226 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1228 * @param addr IPv4 address
1229 * @param is_add 1 = add, 0 = delete
1231 * @return 0 on success, non-zero value otherwise
1233 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1236 * @brief Add/delete NAT44 static mapping
1238 * @param l_addr local IPv4 address
1239 * @param e_addr external IPv4 address
1240 * @param l_port local port number
1241 * @param e_port external port number
1242 * @param vrf_id local VRF ID
1243 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1244 * @param sw_if_index use interface address as external IPv4 address
1245 * @param proto L4 protocol
1246 * @param is_add 1 = add, 0 = delete
1247 * @param twice_nat twice-nat mode
1248 * @param out2in_only if 1 rule match only out2in direction
1249 * @param tag opaque string tag
1250 * @param identity_nat identity NAT
1251 * @param pool_addr pool IPv4 address
1252 * @param exact 1 = exact pool address
1254 * @return 0 on success, non-zero value otherwise
1256 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1257 u16 l_port, u16 e_port, u32 vrf_id,
1258 int addr_only, u32 sw_if_index,
1259 nat_protocol_t proto, int is_add,
1260 twice_nat_type_t twice_nat, u8 out2in_only,
1261 u8 * tag, u8 identity_nat,
1262 ip4_address_t pool_addr, int exact);
1265 * @brief Add/delete static mapping with load-balancing (multiple backends)
1267 * @param e_addr external IPv4 address
1268 * @param e_port external port number
1269 * @param proto L4 protocol
1270 * @param locals list of local backends
1271 * @param is_add 1 = add, 0 = delete
1272 * @param twice_nat twice-nat mode
1273 * @param out2in_only if 1 rule match only out2in direction
1274 * @param tag opaque string tag
1275 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1277 * @return 0 on success, non-zero value otherwise
1279 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1280 nat_protocol_t proto,
1281 nat44_lb_addr_port_t * locals, u8 is_add,
1282 twice_nat_type_t twice_nat,
1283 u8 out2in_only, u8 * tag, u32 affinity);
1285 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1286 ip4_address_t l_addr, u16 l_port,
1287 nat_protocol_t proto, u32 vrf_id,
1288 u8 probability, u8 is_add);
1290 clib_error_t *nat44_api_hookup (vlib_main_t * vm);
1293 * @brief Set NAT plugin workers
1295 * @param bitmap NAT workers bitmap
1297 * @return 0 on success, non-zero value otherwise
1299 int snat_set_workers (uword * bitmap);
1302 * @brief Enable/disable NAT44 feature on the interface
1304 * @param sw_if_index software index of the interface
1305 * @param is_inside 1 = inside, 0 = outside
1306 * @param is_del 1 = delete, 0 = add
1308 * @return 0 on success, non-zero value otherwise
1310 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1313 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1315 * @param sw_if_index software index of the interface
1316 * @param is_inside 1 = inside, 0 = outside
1317 * @param is_del 1 = delete, 0 = add
1319 * @return 0 on success, non-zero value otherwise
1321 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1325 * @brief Add/delete NAT44 pool address from specific interface
1327 * @param sw_if_index software index of the interface
1328 * @param is_del 1 = delete, 0 = add
1329 * @param twice_nat 1 = twice NAT address for external hosts
1331 * @return 0 on success, non-zero value otherwise
1333 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1337 * @brief Delete NAT44 endpoint-dependent session
1339 * @param sm snat global configuration data
1340 * @param addr IPv4 address
1341 * @param port L4 port number
1342 * @param proto L4 protocol
1343 * @param vrf_id VRF ID
1344 * @param is_in 1 = inside network address and port pair, 0 = outside
1346 * @return 0 on success, non-zero value otherwise
1348 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1349 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1350 u32 vrf_id, int is_in);
1353 * @brief Free NAT44 session data (lookup keys, external address port)
1355 * @param sm snat global configuration data
1356 * @param s NAT session
1357 * @param thread_index thread index
1358 * @param is_ha is HA event
1360 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1361 u32 thread_index, u8 is_ha);
1364 * @brief Set NAT44 session limit (session limit, vrf id)
1366 * @param session_limit Session limit
1367 * @param vrf_id VRF id
1368 * @return 0 on success, non-zero value otherwise
1370 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1373 * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
1375 * @param session_limit Session limit
1376 * @param vrf_id VRF id
1377 * @return 0 on success, non-zero value otherwise
1379 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
1382 * @brief Free all NAT44 sessions
1384 void nat44_sessions_clear ();
1387 * @brief Find or create NAT user
1389 * @param sm snat global configuration data
1390 * @param addr IPv4 address
1391 * @param fib_index FIB table index
1392 * @param thread_index thread index
1394 * @return NAT user data structure on success otherwise zero value
1396 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1397 ip4_address_t * addr, u32 fib_index,
1401 * @brief Allocate new NAT session or recycle last used
1403 * @param sm snat global configuration data
1405 * @param thread_index thread index
1406 * @param now time now
1408 * @return session data structure on success otherwise zero value
1410 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1412 u32 thread_index, f64 now);
1415 * @brief Free outside address and port pair
1417 * @param addresses vector of outside addresses
1418 * @param thread_index thread index
1419 * @param key address, port and protocol
1422 snat_free_outside_address_and_port (snat_address_t * addresses,
1424 ip4_address_t * addr,
1425 u16 port, nat_protocol_t protocol);
1427 void expire_per_vrf_sessions (u32 fib_index);
1430 * @brief Match NAT44 static mapping.
1432 * @param key address and port to match
1433 * @param addr external/local address of the matched mapping
1434 * @param port port of the matched mapping
1435 * @param fib_index fib index of the matched mapping
1436 * @param by_external if 0 match by local address otherwise match by external
1438 * @param is_addr_only 1 if matched mapping is address only
1439 * @param twice_nat matched mapping is twice NAT type
1440 * @param lb 1 if matched mapping is load-balanced
1441 * @param ext_host_addr external host address
1442 * @param is_identity_nat 1 if indentity mapping
1443 * @param out if !=0 set to pointer of the mapping structure
1445 * @returns 0 if match found otherwise 1.
1447 int snat_static_mapping_match (snat_main_t * sm,
1448 ip4_address_t match_addr,
1450 u32 match_fib_index,
1451 nat_protocol_t match_protocol,
1452 ip4_address_t * mapping_addr,
1454 u32 * mapping_fib_index,
1457 twice_nat_type_t * twice_nat,
1459 ip4_address_t * ext_host_addr,
1460 u8 * is_identity_nat,
1461 snat_static_mapping_t ** out);
1464 * @brief Add/del NAT address to FIB.
1466 * Add the external NAT address to the FIB as receive entries. This ensures
1467 * that VPP will reply to ARP for this address and we don't need to enable
1468 * proxy ARP on the outside interface.
1470 * @param addr IPv4 address
1471 * @param plen address prefix length
1472 * @param sw_if_index software index of the outside interface
1473 * @param is_add 0 = delete, 1 = add.
1475 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1476 u8 p_len, u32 sw_if_index, int is_add);
1480 nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
1481 ip4_address_t * out_addr, u16 out_port,
1482 ip4_address_t * eh_addr, u16 eh_port,
1483 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
1484 u32 fib_index, u16 flags, u32 thread_index);
1487 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
1488 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1489 u32 fib_index, u32 ti);
1492 nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
1493 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1494 u32 fib_index, u32 total_pkts, u64 total_bytes,
1498 int nat_set_outside_address_and_port (snat_address_t *addresses,
1499 u32 thread_index, ip4_address_t addr,
1500 u16 port, nat_protocol_t protocol);
1503 * Why is this here? Because we don't need to touch this layer to
1504 * simply reply to an icmp. We need to change id to a unique
1505 * value to NAT an echo request/reply.
1512 } icmp_echo_header_t;
1516 u16 src_port, dst_port;
1519 u8 *format_user_kvp (u8 * s, va_list * args);
1521 u32 get_thread_idx_by_port (u16 e_port);
1523 u8 *format_static_mapping_kvp (u8 *s, va_list *args);
1525 u8 *format_session_kvp (u8 *s, va_list *args);
1527 u8 *format_user_kvp (u8 *s, va_list *args);
1529 u32 nat_calc_bihash_buckets (u32 n_elts);
1531 void nat44_addresses_free (snat_address_t **addresses);
1535 NAT_ED_TRNSL_ERR_SUCCESS = 0,
1536 NAT_ED_TRNSL_ERR_TRANSLATION_FAILED = 1,
1537 NAT_ED_TRNSL_ERR_FLOW_MISMATCH = 2,
1538 } nat_translation_error_e;
1540 nat_translation_error_e
1541 nat_6t_flow_buf_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
1542 nat_6t_flow_t *f, nat_protocol_t proto,
1543 int is_output_feature);
1545 void nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f);
1547 format_function_t format_nat_ed_translation_error;
1548 format_function_t format_nat_6t_flow;
1549 format_function_t format_ed_session_kvp;
1551 #endif /* __included_nat_h__ */
1553 * fd.io coding-style-patch-verification: ON
1556 * eval: (c-set-style "gnu")