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_nat44_ed_h__
20 #define __included_nat44_ed_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 <nat/lib/lib.h>
36 #include <nat/lib/inlines.h>
38 /* default number of worker handoff frame queue elements */
39 #define NAT_FQ_NELTS_DEFAULT 64
41 /* NAT buffer flags */
42 #define SNAT_FLAG_HAIRPINNING (1 << 0)
44 /* NAT44 API Configuration flags */
45 #define foreach_nat44_config_flag \
46 _(0x00, IS_ENDPOINT_INDEPENDENT) \
47 _(0x01, IS_ENDPOINT_DEPENDENT) \
48 _(0x02, IS_STATIC_MAPPING_ONLY) \
49 _(0x04, IS_CONNECTION_TRACKING) \
50 _(0x08, IS_OUT2IN_DPO)
52 typedef enum nat44_config_flags_t_
54 #define _(n,f) NAT44_API_##f = n,
55 foreach_nat44_config_flag
57 } nat44_config_flags_t;
61 /* nat44 plugin features */
62 u8 static_mapping_only;
63 u8 connection_tracking;
68 /* maximum number of sessions */
77 NAT_NEXT_IN2OUT_ED_FAST_PATH,
78 NAT_NEXT_IN2OUT_ED_SLOW_PATH,
79 NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH,
80 NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
81 NAT_NEXT_OUT2IN_ED_FAST_PATH,
82 NAT_NEXT_OUT2IN_ED_SLOW_PATH,
83 NAT_NEXT_IN2OUT_CLASSIFY,
84 NAT_NEXT_OUT2IN_CLASSIFY,
94 /* External address and port allocation modes */
95 #define foreach_nat_addr_and_port_alloc_alg \
96 _(0, DEFAULT, "default") \
98 _(2, RANGE, "port-range")
102 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
103 foreach_nat_addr_and_port_alloc_alg
105 } nat_addr_and_port_alloc_alg_t;
108 #define foreach_snat_session_state \
109 _(0, UNKNOWN, "unknown") \
110 _(1, UDP_ACTIVE, "udp-active") \
111 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
112 _(3, TCP_ESTABLISHED, "tcp-established") \
113 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
114 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
115 _(6, TCP_CLOSING, "tcp-closing") \
116 _(7, TCP_LAST_ACK, "tcp-last-ack") \
117 _(8, TCP_CLOSED, "tcp-closed") \
118 _(9, ICMP_ACTIVE, "icmp-active")
122 #define _(v, N, s) SNAT_SESSION_##N = v,
123 foreach_snat_session_state
125 } snat_session_state_t;
127 #define foreach_nat_in2out_ed_error \
128 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
129 _(OUT_OF_PORTS, "out of ports") \
130 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
131 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
132 _(NON_SYN, "non-SYN packet try to create session") \
133 _(TCP_CLOSED, "drops due to TCP in transitory timeout")
137 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
138 foreach_nat_in2out_ed_error
140 NAT_IN2OUT_ED_N_ERROR,
141 } nat_in2out_ed_error_t;
143 #define foreach_nat_out2in_ed_error \
144 _ (UNSUPPORTED_PROTOCOL, "unsupported protocol") \
145 _ (OUT_OF_PORTS, "out of ports") \
146 _ (BAD_ICMP_TYPE, "unsupported ICMP type") \
147 _ (NO_TRANSLATION, "no translation") \
148 _ (MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
149 _ (NON_SYN, "non-SYN packet try to create session") \
150 _ (TCP_CLOSED, "drops due to TCP in transitory timeout") \
151 _ (HASH_ADD_FAILED, "hash table add failed")
155 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
156 foreach_nat_out2in_ed_error
158 NAT_OUT2IN_ED_N_ERROR,
159 } nat_out2in_ed_error_t;
162 /* Endpoint dependent TCP session state */
163 #define NAT44_SES_I2O_FIN 1
164 #define NAT44_SES_O2I_FIN 2
165 #define NAT44_SES_I2O_FIN_ACK 4
166 #define NAT44_SES_O2I_FIN_ACK 8
167 #define NAT44_SES_I2O_SYN 16
168 #define NAT44_SES_O2I_SYN 32
169 #define NAT44_SES_RST 64
172 #define SNAT_SESSION_FLAG_STATIC_MAPPING (1 << 0)
173 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO (1 << 1)
174 #define SNAT_SESSION_FLAG_LOAD_BALANCING (1 << 2)
175 #define SNAT_SESSION_FLAG_TWICE_NAT (1 << 3)
176 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT (1 << 4)
177 #define SNAT_SESSION_FLAG_FWD_BYPASS (1 << 5)
178 #define SNAT_SESSION_FLAG_AFFINITY (1 << 6)
179 #define SNAT_SESSION_FLAG_EXACT_ADDRESS (1 << 7)
180 #define SNAT_SESSION_FLAG_HAIRPINNING (1 << 8)
182 /* NAT interface flags */
183 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
184 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
186 /* Static mapping flags */
187 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
188 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
189 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
190 #define NAT_STATIC_MAPPING_FLAG_LB 8
191 #define NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS 16
193 typedef CLIB_PACKED(struct
195 // number of sessions in this vrf
201 // is this vrf expired
203 }) per_vrf_sessions_t;
209 ip4_address_t saddr, daddr;
210 u16 sport; // ICMP id for ICMP case
219 STATIC_ASSERT_SIZEOF (nat_6t_t, 2 * sizeof (u64));
223 #define NAT_FLOW_OP_SADDR_REWRITE (1 << 1)
224 #define NAT_FLOW_OP_SPORT_REWRITE (1 << 2)
225 #define NAT_FLOW_OP_DADDR_REWRITE (1 << 3)
226 #define NAT_FLOW_OP_DPORT_REWRITE (1 << 4)
227 #define NAT_FLOW_OP_ICMP_ID_REWRITE (1 << 5)
228 #define NAT_FLOW_OP_TXFIB_REWRITE (1 << 6)
233 ip4_address_t saddr, daddr;
244 void nat44_ed_forwarding_enable_disable (u8 is_enable);
247 nat_6t_flow_saddr_rewrite_set (nat_6t_flow_t *f, u32 saddr)
249 f->ops |= NAT_FLOW_OP_SADDR_REWRITE;
250 f->rewrite.saddr.as_u32 = saddr;
254 nat_6t_flow_daddr_rewrite_set (nat_6t_flow_t *f, u32 daddr)
256 f->ops |= NAT_FLOW_OP_DADDR_REWRITE;
257 f->rewrite.daddr.as_u32 = daddr;
261 nat_6t_flow_sport_rewrite_set (nat_6t_flow_t *f, u32 sport)
263 f->ops |= NAT_FLOW_OP_SPORT_REWRITE;
264 f->rewrite.sport = sport;
268 nat_6t_flow_dport_rewrite_set (nat_6t_flow_t *f, u32 dport)
270 f->ops |= NAT_FLOW_OP_DPORT_REWRITE;
271 f->rewrite.dport = dport;
275 nat_6t_flow_txfib_rewrite_set (nat_6t_flow_t *f, u32 tx_fib_index)
277 f->ops |= NAT_FLOW_OP_TXFIB_REWRITE;
278 f->rewrite.fib_index = tx_fib_index;
282 nat_6t_flow_icmp_id_rewrite_set (nat_6t_flow_t *f, u16 id)
284 f->ops |= NAT_FLOW_OP_ICMP_ID_REWRITE;
285 f->rewrite.icmp_id = id;
288 typedef CLIB_PACKED(struct
290 /* Outside network tuple */
298 /* Inside network tuple */
306 nat_protocol_t nat_proto;
314 /* head of LRU list in which this session is tracked */
316 /* index in global LRU list */
320 /* Last heard timer */
323 /* Last HA refresh */
324 f64 ha_last_refreshed;
330 /* External host address and port */
331 ip4_address_t ext_host_addr;
334 /* External host address and port after translation */
335 ip4_address_t ext_host_nat_addr;
336 u16 ext_host_nat_port;
338 /* TCP session state */
342 u64 tcp_closed_timestamp;
344 /* per vrf sessions index */
345 u32 per_vrf_sessions_index;
353 #define _(N, i, n, s) \
354 u32 busy_##n##_ports; \
355 u32 * busy_##n##_ports_per_thread; \
356 u32 busy_##n##_port_refcounts[65535];
375 /* backend IP address */
377 /* backend port number */
379 /* probability of the backend to be randomly matched */
382 /* backend FIB table */
385 } nat44_lb_addr_port_t;
389 /* twice-nat disabled */
391 /* twice-nat enabled */
393 /* twice-nat only when src IP equals dst IP after translation */
399 /* no load-balancing */
403 /* load-balancing with affinity */
409 /* prefered pool address */
410 ip4_address_t pool_addr;
411 /* local IP address */
412 ip4_address_t local_addr;
413 /* external IP address */
414 ip4_address_t external_addr;
420 twice_nat_type_t twice_nat;
421 /* local FIB table */
425 nat_protocol_t proto;
426 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
428 /* worker threads used by backends/local host */
430 /* opaque string tag */
432 /* backends for load-balancing mode */
433 nat44_lb_addr_port_t *locals;
434 /* affinity per service lis */
435 u32 affinity_per_service_list_head_index;
438 } snat_static_mapping_t;
448 ip4_address_t l_addr;
449 ip4_address_t pool_addr;
454 nat_protocol_t proto;
462 } snat_static_map_resolve_t;
467 snat_session_t *sessions;
469 /* Pool of doubly-linked list elements */
470 dlist_elt_t *list_pool;
472 /* LRU session list - head is stale, tail is fresh */
473 dlist_elt_t *lru_pool;
474 u32 tcp_trans_lru_head_index;
475 u32 tcp_estab_lru_head_index;
476 u32 udp_lru_head_index;
477 u32 icmp_lru_head_index;
478 u32 unk_proto_lru_head_index;
480 /* NAT thread index */
481 u32 snat_thread_index;
483 /* real thread index */
486 per_vrf_sessions_t *per_vrf_sessions_vec;
488 } snat_main_per_thread_data_t;
492 u32 nat44_ed_get_in2out_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
493 u32 rx_fib_index, u8 is_output);
494 u32 nat44_ed_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
495 u32 rx_fib_index, u8 is_output);
497 /* Return worker thread index for given packet */
498 /* NAT address and port allocation function */
499 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
503 nat_protocol_t proto,
504 ip4_address_t * addr,
507 u32 snat_thread_index);
509 typedef struct snat_main_s
511 /* Thread settings */
513 u32 first_worker_index;
517 /* Per thread data */
518 snat_main_per_thread_data_t *per_thread_data;
520 /* Find a static mapping by local */
521 clib_bihash_8_8_t static_mapping_by_local;
523 /* Find a static mapping by external */
524 clib_bihash_8_8_t static_mapping_by_external;
526 /* Static mapping pool */
527 snat_static_mapping_t *static_mappings;
529 /* Endpoint independent lookup tables */
530 clib_bihash_8_8_t in2out;
531 clib_bihash_8_8_t out2in;
533 /* Endpoint dependent lookup table */
534 clib_bihash_16_8_t flow_hash;
537 snat_interface_t *interfaces;
538 snat_interface_t *output_feature_interfaces;
540 /* Vector of outside addresses */
541 snat_address_t *addresses;
542 /* Address and port allocation function */
543 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
544 /* Address and port allocation type */
545 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
546 /* Port set parameters (MAP-E) */
550 /* Port range parameters */
557 /* vector of outside fibs */
558 nat_outside_fib_t *outside_fibs;
560 /* Vector of twice NAT addresses for external hosts */
561 snat_address_t *twice_nat_addresses;
563 /* sw_if_indices whose intfc addresses should be auto-added */
564 u32 *auto_add_sw_if_indices;
565 u32 *auto_add_sw_if_indices_twice_nat;
567 /* vector of interface address static mappings to resolve. */
568 snat_static_map_resolve_t *to_resolve;
570 /* Randomize port allocation order */
573 /* Worker handoff frame-queue index */
575 u32 fq_in2out_output_index;
578 u32 out2in_node_index;
579 u32 in2out_node_index;
580 u32 in2out_output_node_index;
582 nat44_config_t rconfig;
583 //nat44_config_t cconfig;
585 /* If forwarding is enabled */
586 u8 forwarding_enabled;
588 /* static mapping config */
589 u8 static_mapping_only;
590 u8 static_mapping_connection_tracking;
592 /* Is translation memory size calculated or user defined */
593 u8 translation_memory_size_set;
595 u32 translation_buckets;
596 u32 max_translations_per_thread;
597 u32 *max_translations_per_fib;
600 u32 outside_fib_index;
602 u32 inside_fib_index;
604 nat_timeouts_t timeouts;
606 /* TCP MSS clamping */
610 vlib_simple_counter_main_t total_sessions;
611 u32 max_cfg_sessions_gauge; /* Index of max configured sessions gauge in
614 #define _(x) vlib_simple_counter_main_t x;
643 vlib_simple_counter_main_t hairpinning;
647 /* API message ID base */
651 vlib_log_class_t log_class;
656 api_main_t *api_main;
657 ip4_main_t *ip4_main;
658 ip_lookup_main_t *ip4_lookup_main;
660 fib_source_t fib_src_hi;
661 fib_source_t fib_src_low;
663 /* pat - dynamic mapping enabled or conneciton tracking */
666 /* number of worker handoff frame queue elements */
667 u32 frame_queue_nelts;
669 /* nat44 plugin enabled */
672 vnet_main_t *vnet_main;
680 } nat44_is_idle_session_ctx_t;
684 u32 cached_sw_if_index;
685 u32 cached_ip4_address;
688 extern snat_main_t snat_main;
690 // nat pre ed next_node feature classification
691 extern vlib_node_registration_t nat_default_node;
692 extern vlib_node_registration_t nat_pre_in2out_node;
693 extern vlib_node_registration_t nat_pre_out2in_node;
695 extern vlib_node_registration_t snat_in2out_node;
696 extern vlib_node_registration_t snat_in2out_output_node;
697 extern vlib_node_registration_t snat_out2in_node;
698 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
699 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
700 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
701 extern vlib_node_registration_t nat44_ed_in2out_node;
702 extern vlib_node_registration_t nat44_ed_in2out_output_node;
703 extern vlib_node_registration_t nat44_ed_out2in_node;
705 extern fib_source_t nat_fib_src_hi;
706 extern fib_source_t nat_fib_src_low;
708 /* format functions */
709 format_function_t format_snat_static_mapping;
710 format_function_t format_snat_static_map_to_resolve;
711 format_function_t format_snat_session;
712 format_function_t format_snat_key;
713 format_function_t format_static_mapping_key;
714 format_function_t format_nat_protocol;
715 format_function_t format_nat_addr_and_port_alloc_alg;
716 /* unformat functions */
717 unformat_function_t unformat_nat_protocol;
719 /** \brief Check if SNAT session is created from static mapping.
720 @param s SNAT session
721 @return 1 if SNAT session is created from static mapping otherwise 0
723 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
725 /** \brief Check if SNAT session for unknown protocol.
726 @param s SNAT session
727 @return 1 if SNAT session for unknown protocol otherwise 0
729 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
731 /** \brief Check if NAT session is twice NAT.
733 @return 1 if NAT session is twice NAT
735 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
737 /** \brief Check if NAT session is load-balancing.
739 @return 1 if NAT session is load-balancing
741 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
743 /** \brief Check if NAT session is forwarding bypass.
745 @return 1 if NAT session is load-balancing
747 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
749 /** \brief Check if NAT session is endpoint dependent.
751 @return 1 if NAT session is endpoint dependent
753 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
755 /** \brief Check if NAT session has affinity record.
757 @return 1 if NAT session has affinity record
759 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
761 /** \brief Check if exact pool address should be used.
762 @param s SNAT session
763 @return 1 if exact pool address or 0
765 #define is_exact_address_session(s) (s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS)
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 exact pool address should be used.
810 @param s SNAT session
811 @return 1 if exact pool address or 0
813 #define is_exact_address(s) (s->flags & NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS)
815 /** \brief Check if client initiating TCP connection (received SYN from client)
817 @return 1 if client initiating TCP connection
820 tcp_flags_is_init (u8 f)
822 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
826 #define nat_log_err(...) \
827 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
828 #define nat_log_warn(...) \
829 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
830 #define nat_log_notice(...) \
831 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
832 #define nat_log_info(...) \
833 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
834 #define nat_log_debug(...)\
835 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
838 * @brief Enable NAT44 plugin
840 * @param c nat44_config_t
842 * @return 0 on success, non-zero value otherwise
844 int nat44_plugin_enable (nat44_config_t c);
847 * @brief Disable NAT44 plugin
849 * @return 0 on success, non-zero value otherwise
851 int nat44_plugin_disable ();
854 * @brief Add external address to NAT44 pool
856 * @param sm snat global configuration data
857 * @param addr IPv4 address
858 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
859 * @param twice_nat 1 if twice NAT address
861 * @return 0 on success, non-zero value otherwise
863 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
867 * @brief Delete external address from NAT44 pool
869 * @param sm snat global configuration data
870 * @param addr IPv4 address
871 * @param delete_sm 1 if delete static mapping using address
872 * @param twice_nat 1 if twice NAT address
874 * @return 0 on success, non-zero value otherwise
876 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
880 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
882 * @param addr IPv4 address
883 * @param is_add 1 = add, 0 = delete
885 * @return 0 on success, non-zero value otherwise
887 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
890 * @brief Add/delete NAT44 static mapping
892 * @param l_addr local IPv4 address
893 * @param e_addr external IPv4 address
894 * @param l_port local port number
895 * @param e_port external port number
896 * @param vrf_id local VRF ID
897 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
898 * @param sw_if_index use interface address as external IPv4 address
899 * @param proto L4 protocol
900 * @param is_add 1 = add, 0 = delete
901 * @param twice_nat twice-nat mode
902 * @param out2in_only if 1 rule match only out2in direction
903 * @param tag opaque string tag
904 * @param identity_nat identity NAT
905 * @param pool_addr pool IPv4 address
906 * @param exact 1 = exact pool address
908 * @return 0 on success, non-zero value otherwise
910 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
911 u16 l_port, u16 e_port, u32 vrf_id,
912 int addr_only, u32 sw_if_index,
913 nat_protocol_t proto, int is_add,
914 twice_nat_type_t twice_nat, u8 out2in_only,
915 u8 * tag, u8 identity_nat,
916 ip4_address_t pool_addr, int exact);
919 * @brief Add/delete static mapping with load-balancing (multiple backends)
921 * @param e_addr external IPv4 address
922 * @param e_port external port number
923 * @param proto L4 protocol
924 * @param locals list of local backends
925 * @param is_add 1 = add, 0 = delete
926 * @param twice_nat twice-nat mode
927 * @param out2in_only if 1 rule match only out2in direction
928 * @param tag opaque string tag
929 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
931 * @return 0 on success, non-zero value otherwise
933 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
934 nat_protocol_t proto,
935 nat44_lb_addr_port_t * locals, u8 is_add,
936 twice_nat_type_t twice_nat,
937 u8 out2in_only, u8 * tag, u32 affinity);
939 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
940 ip4_address_t l_addr, u16 l_port,
941 nat_protocol_t proto, u32 vrf_id,
942 u8 probability, u8 is_add);
944 clib_error_t *nat44_api_hookup (vlib_main_t * vm);
947 * @brief Set NAT plugin workers
949 * @param bitmap NAT workers bitmap
951 * @return 0 on success, non-zero value otherwise
953 int snat_set_workers (uword * bitmap);
956 * @brief Set NAT plugin number of frame queue elements
958 * @param frame_queue_nelts number of worker handoff frame queue elements
960 * @return 0 on success, non-zero value otherwise
962 int snat_set_frame_queue_nelts (u32 frame_queue_nelts);
965 * @brief Enable/disable NAT44 feature on the interface
967 * @param sw_if_index software index of the interface
968 * @param is_inside 1 = inside, 0 = outside
969 * @param is_del 1 = delete, 0 = add
971 * @return 0 on success, non-zero value otherwise
973 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
976 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
978 * @param sw_if_index software index of the interface
979 * @param is_inside 1 = inside, 0 = outside
980 * @param is_del 1 = delete, 0 = add
982 * @return 0 on success, non-zero value otherwise
984 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
988 * @brief Add/delete NAT44 pool address from specific interface
990 * @param sw_if_index software index of the interface
991 * @param is_del 1 = delete, 0 = add
992 * @param twice_nat 1 = twice NAT address for external hosts
994 * @return 0 on success, non-zero value otherwise
996 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1000 * @brief Delete NAT44 endpoint-dependent session
1002 * @param sm snat global configuration data
1003 * @param addr IPv4 address
1004 * @param port L4 port number
1005 * @param proto L4 protocol
1006 * @param vrf_id VRF ID
1007 * @param is_in 1 = inside network address and port pair, 0 = outside
1009 * @return 0 on success, non-zero value otherwise
1011 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1012 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1013 u32 vrf_id, int is_in);
1016 * @brief Free NAT44 session data (lookup keys, external address port)
1018 * @param sm snat global configuration data
1019 * @param s NAT session
1020 * @param thread_index thread index
1021 * @param is_ha is HA event
1023 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1024 u32 thread_index, u8 is_ha);
1027 * @brief Set NAT44 session limit (session limit, vrf id)
1029 * @param session_limit Session limit
1030 * @param vrf_id VRF id
1031 * @return 0 on success, non-zero value otherwise
1033 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1036 * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
1038 * @param session_limit Session limit
1039 * @param vrf_id VRF id
1040 * @return 0 on success, non-zero value otherwise
1042 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
1045 * @brief Free outside address and port pair
1047 * @param addresses vector of outside addresses
1048 * @param thread_index thread index
1049 * @param key address, port and protocol
1052 snat_free_outside_address_and_port (snat_address_t * addresses,
1054 ip4_address_t * addr,
1055 u16 port, nat_protocol_t protocol);
1057 void expire_per_vrf_sessions (u32 fib_index);
1060 * @brief Match NAT44 static mapping.
1062 * @param key address and port to match
1063 * @param addr external/local address of the matched mapping
1064 * @param port port of the matched mapping
1065 * @param fib_index fib index of the matched mapping
1066 * @param by_external if 0 match by local address otherwise match by external
1068 * @param is_addr_only 1 if matched mapping is address only
1069 * @param twice_nat matched mapping is twice NAT type
1070 * @param lb 1 if matched mapping is load-balanced
1071 * @param ext_host_addr external host address
1072 * @param is_identity_nat 1 if indentity mapping
1073 * @param out if !=0 set to pointer of the mapping structure
1075 * @returns 0 if match found otherwise 1.
1077 int snat_static_mapping_match (
1078 vlib_main_t *vm, snat_main_t *sm, ip4_address_t match_addr, u16 match_port,
1079 u32 match_fib_index, nat_protocol_t match_protocol,
1080 ip4_address_t *mapping_addr, u16 *mapping_port, u32 *mapping_fib_index,
1081 u8 by_external, u8 *is_addr_only, twice_nat_type_t *twice_nat,
1082 lb_nat_type_t *lb, ip4_address_t *ext_host_addr, u8 *is_identity_nat,
1083 snat_static_mapping_t **out);
1086 * @brief Add/del NAT address to FIB.
1088 * Add the external NAT address to the FIB as receive entries. This ensures
1089 * that VPP will reply to ARP for this address and we don't need to enable
1090 * proxy ARP on the outside interface.
1092 * @param addr IPv4 address
1093 * @param plen address prefix length
1094 * @param sw_if_index software index of the outside interface
1095 * @param is_add 0 = delete, 1 = add.
1097 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1098 u8 p_len, u32 sw_if_index, int is_add);
1102 nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
1103 ip4_address_t * out_addr, u16 out_port,
1104 ip4_address_t * eh_addr, u16 eh_port,
1105 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
1106 u32 fib_index, u16 flags, u32 thread_index);
1109 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
1110 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1111 u32 fib_index, u32 ti);
1114 nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
1115 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1116 u32 fib_index, u32 total_pkts, u64 total_bytes,
1120 int nat_set_outside_address_and_port (snat_address_t *addresses,
1121 u32 thread_index, ip4_address_t addr,
1122 u16 port, nat_protocol_t protocol);
1125 * Why is this here? Because we don't need to touch this layer to
1126 * simply reply to an icmp. We need to change id to a unique
1127 * value to NAT an echo request/reply.
1134 } icmp_echo_header_t;
1138 u16 src_port, dst_port;
1141 u32 get_thread_idx_by_port (u16 e_port);
1143 u8 *format_static_mapping_kvp (u8 *s, va_list *args);
1145 u8 *format_session_kvp (u8 *s, va_list *args);
1147 u32 nat_calc_bihash_buckets (u32 n_elts);
1149 void nat44_addresses_free (snat_address_t **addresses);
1151 void nat44_ed_sessions_clear ();
1153 int nat44_ed_set_frame_queue_nelts (u32 frame_queue_nelts);
1157 NAT_ED_TRNSL_ERR_SUCCESS = 0,
1158 NAT_ED_TRNSL_ERR_TRANSLATION_FAILED = 1,
1159 NAT_ED_TRNSL_ERR_FLOW_MISMATCH = 2,
1160 } nat_translation_error_e;
1162 nat_translation_error_e
1163 nat_6t_flow_buf_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
1164 nat_6t_flow_t *f, nat_protocol_t proto,
1165 int is_output_feature);
1167 void nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f);
1169 format_function_t format_nat_ed_translation_error;
1170 format_function_t format_nat_6t_flow;
1171 format_function_t format_ed_session_kvp;
1173 #endif /* __included_nat44_ed_h__ */
1175 * fd.io coding-style-patch-verification: ON
1178 * eval: (c-set-style "gnu")