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.
16 * @file NAT plugin global declarations
18 #ifndef __included_nat_h__
19 #define __included_nat_h__
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/ip/icmp46_packet.h>
25 #include <vnet/api_errno.h>
26 #include <vnet/fib/fib_source.h>
27 #include <vppinfra/elog.h>
28 #include <vppinfra/bihash_8_8.h>
29 #include <vppinfra/bihash_16_8.h>
30 #include <vppinfra/dlist.h>
31 #include <vppinfra/error.h>
32 #include <vlibapi/api.h>
35 /* default session timeouts */
36 #define SNAT_UDP_TIMEOUT 300
37 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
38 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
39 #define SNAT_ICMP_TIMEOUT 60
41 /* number of worker handoff frame queue elements */
42 #define NAT_FQ_NELTS 64
44 /* NAT buffer flags */
45 #define SNAT_FLAG_HAIRPINNING (1 << 0)
50 } nat_buffer_opaque_t;
58 NAT_NEXT_IN2OUT_ED_FAST_PATH,
59 NAT_NEXT_IN2OUT_ED_SLOW_PATH,
60 NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
61 NAT_NEXT_OUT2IN_ED_FAST_PATH,
62 NAT_NEXT_OUT2IN_ED_SLOW_PATH,
71 #define nat_buffer_opaque(b) \
72 ((nat_buffer_opaque_t *)((vnet_buffer_opaque2_t *)b->opaque2)->__unused2)
75 STATIC_ASSERT (sizeof (nat_buffer_opaque_t) <=
76 STRUCT_SIZE_OF (vnet_buffer_opaque_t, unused),
77 "Custom meta-data too large for vnet_buffer_opaque_t");
79 #define nat_buffer_opaque(b) \
80 ((nat_buffer_opaque_t *)((u8 *)((b)->opaque) + \
81 STRUCT_OFFSET_OF (vnet_buffer_opaque_t, unused)))*/
83 /* session key (4-tuple) */
92 u16 protocol:3, fib_index:13;
98 /* endpoint-dependent session key (6-tuple) */
105 ip4_address_t l_addr;
106 ip4_address_t r_addr;
107 u32 proto:8, fib_index:24;
115 /* deterministic session outside key */
122 ip4_address_t ext_host_addr;
128 } snat_det_out_key_t;
130 /* user (internal host) key */
149 } nat44_reass_trace_t;
151 /* NAT API Configuration flags */
152 #define foreach_nat_config_flag \
153 _(0x01, IS_TWICE_NAT) \
154 _(0x02, IS_SELF_TWICE_NAT) \
155 _(0x04, IS_OUT2IN_ONLY) \
156 _(0x08, IS_ADDR_ONLY) \
157 _(0x10, IS_OUTSIDE) \
160 _(0x80, IS_EXT_HOST_VALID) \
162 typedef enum nat_config_flags_t_
164 #define _(n,f) NAT_API_##f = n,
165 foreach_nat_config_flag
167 } nat_config_flags_t;
169 /* External address and port allocation modes */
170 #define foreach_nat_addr_and_port_alloc_alg \
171 _(0, DEFAULT, "default") \
172 _(1, MAPE, "map-e") \
173 _(2, RANGE, "port-range")
177 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
178 foreach_nat_addr_and_port_alloc_alg
180 } nat_addr_and_port_alloc_alg_t;
183 /* Supported L4 protocols */
184 #define foreach_snat_protocol \
185 _(UDP, 0, udp, "udp") \
186 _(TCP, 1, tcp, "tcp") \
187 _(ICMP, 2, icmp, "icmp")
191 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
192 foreach_snat_protocol
198 #define foreach_snat_session_state \
199 _(0, UNKNOWN, "unknown") \
200 _(1, UDP_ACTIVE, "udp-active") \
201 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
202 _(3, TCP_ESTABLISHED, "tcp-established") \
203 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
204 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
205 _(6, TCP_CLOSING, "tcp-closing") \
206 _(7, TCP_LAST_ACK, "tcp-last-ack") \
207 _(8, TCP_CLOSED, "tcp-closed") \
208 _(9, ICMP_ACTIVE, "icmp-active")
212 #define _(v, N, s) SNAT_SESSION_##N = v,
213 foreach_snat_session_state
215 } snat_session_state_t;
217 #define foreach_nat_in2out_ed_error \
218 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
219 _(IN2OUT_PACKETS, "good in2out packets processed") \
220 _(OUT_OF_PORTS, "out of ports") \
221 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
222 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
223 _(DROP_FRAGMENT, "drop fragment") \
224 _(MAX_REASS, "maximum reassemblies exceeded") \
225 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
226 _(NON_SYN, "non-SYN packet try to create session") \
227 _(TCP_PACKETS, "TCP packets") \
228 _(UDP_PACKETS, "UDP packets") \
229 _(ICMP_PACKETS, "ICMP packets") \
230 _(OTHER_PACKETS, "other protocol packets") \
231 _(FRAGMENTS, "fragments") \
232 _(CACHED_FRAGMENTS, "cached fragments") \
233 _(PROCESSED_FRAGMENTS, "processed fragments")
237 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
238 foreach_nat_in2out_ed_error
240 NAT_IN2OUT_ED_N_ERROR,
241 } nat_in2out_ed_error_t;
243 #define foreach_nat_out2in_ed_error \
244 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
245 _(OUT2IN_PACKETS, "good out2in packets processed") \
246 _(OUT_OF_PORTS, "out of ports") \
247 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
248 _(NO_TRANSLATION, "no translation") \
249 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
250 _(DROP_FRAGMENT, "drop fragment") \
251 _(MAX_REASS, "maximum reassemblies exceeded") \
252 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
253 _(NON_SYN, "non-SYN packet try to create session") \
254 _(TCP_PACKETS, "TCP packets") \
255 _(UDP_PACKETS, "UDP packets") \
256 _(ICMP_PACKETS, "ICMP packets") \
257 _(OTHER_PACKETS, "other protocol packets") \
258 _(FRAGMENTS, "fragments") \
259 _(CACHED_FRAGMENTS, "cached fragments") \
260 _(PROCESSED_FRAGMENTS, "processed fragments")
264 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
265 foreach_nat_out2in_ed_error
267 NAT_OUT2IN_ED_N_ERROR,
268 } nat_out2in_ed_error_t;
271 /* Endpoint dependent TCP session state */
272 #define NAT44_SES_I2O_FIN 1
273 #define NAT44_SES_O2I_FIN 2
274 #define NAT44_SES_I2O_FIN_ACK 4
275 #define NAT44_SES_O2I_FIN_ACK 8
276 #define NAT44_SES_I2O_SYN 16
277 #define NAT44_SES_O2I_SYN 32
278 #define NAT44_SES_RST 64
281 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
282 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
283 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
284 #define SNAT_SESSION_FLAG_TWICE_NAT 8
285 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
286 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
287 #define SNAT_SESSION_FLAG_AFFINITY 64
288 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
290 /* NAT interface flags */
291 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
292 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
294 /* Static mapping flags */
295 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
296 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
297 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
298 #define NAT_STATIC_MAPPING_FLAG_LB 8
301 typedef CLIB_PACKED(struct
303 /* Outside network key */
304 snat_session_key_t out2in;
306 /* Inside network key */
307 snat_session_key_t in2out;
312 /* Per-user translations */
314 u32 per_user_list_head_index;
316 /* Last heard timer */
319 /* Last HA refresh */
320 f64 ha_last_refreshed;
326 /* External host address and port */
327 ip4_address_t ext_host_addr;
330 /* External host address and port after translation */
331 ip4_address_t ext_host_nat_addr;
332 u16 ext_host_nat_port;
334 /* TCP session state */
349 u32 sessions_per_user_list_head_index;
359 #define _(N, i, n, s) \
360 u16 busy_##n##_ports; \
361 u16 * busy_##n##_ports_per_thread; \
362 uword * busy_##n##_port_bitmap;
363 foreach_snat_protocol
376 /* Inside network port */
378 /* Outside network address and port */
379 snat_det_out_key_t out;
384 } snat_det_session_t;
388 /* inside IP address range */
389 ip4_address_t in_addr;
391 /* outside IP address range */
392 ip4_address_t out_addr;
394 /* inside IP addresses / outside IP addresses */
396 /* number of ports available to internal host */
398 /* session counter */
400 /* vector of sessions */
401 snat_det_session_t *sessions;
406 /* backend IP address */
408 /* backend port number */
410 /* probability of the backend to be randomly matched */
413 /* backend FIB table */
416 } nat44_lb_addr_port_t;
420 /* twice-nat disabled */
422 /* twice-nat enabled */
424 /* twice-nat only when src IP equals dst IP after translation */
430 /* no load-balancing */
434 /* load-balancing with affinity */
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 snat_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;
482 snat_protocol_t proto;
490 } snat_static_map_resolve_t;
494 /* Main lookup tables */
495 clib_bihash_8_8_t out2in;
496 clib_bihash_8_8_t in2out;
498 /* Endpoint dependent sessions lookup tables */
499 clib_bihash_16_8_t out2in_ed;
500 clib_bihash_16_8_t in2out_ed;
502 /* Find-a-user => src address lookup */
503 clib_bihash_8_8_t user_hash;
509 snat_session_t *sessions;
511 /* Pool of doubly-linked list elements */
512 dlist_elt_t *list_pool;
514 /* NAT thread index */
515 u32 snat_thread_index;
517 /* real thread index */
519 } snat_main_per_thread_data_t;
523 /* ICMP session match function */
524 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
525 vlib_node_runtime_t * node,
528 ip4_header_t * ip0, u8 * p_proto,
529 snat_session_key_t * p_value,
530 u8 * p_dont_translate, void *d,
533 /* Return worker thread index for given packet */
534 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
538 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
543 /* NAT address and port allacotaion function */
544 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
548 snat_session_key_t * k,
550 u32 snat_thread_index);
552 typedef struct snat_main_s
554 /* ICMP session match functions */
555 snat_icmp_match_function_t *icmp_match_in2out_cb;
556 snat_icmp_match_function_t *icmp_match_out2in_cb;
558 /* Thread settings */
560 u32 first_worker_index;
562 snat_get_worker_in2out_function_t *worker_in2out_cb;
563 snat_get_worker_out2in_function_t *worker_out2in_cb;
567 /* Per thread data */
568 snat_main_per_thread_data_t *per_thread_data;
570 /* Find a static mapping by local */
571 clib_bihash_8_8_t static_mapping_by_local;
573 /* Find a static mapping by external */
574 clib_bihash_8_8_t static_mapping_by_external;
576 /* Static mapping pool */
577 snat_static_mapping_t *static_mappings;
580 snat_interface_t *interfaces;
581 snat_interface_t *output_feature_interfaces;
583 /* Vector of outside addresses */
584 snat_address_t *addresses;
585 /* Address and port allocation function */
586 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
587 /* Address and port allocation type */
588 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
589 /* Port set parameters (MAP-E) */
593 /* Port range parameters */
597 /* vector of outside fibs */
598 nat_outside_fib_t *outside_fibs;
600 /* Vector of twice NAT addresses for extenal hosts */
601 snat_address_t *twice_nat_addresses;
603 /* sw_if_indices whose intfc addresses should be auto-added */
604 u32 *auto_add_sw_if_indices;
605 u32 *auto_add_sw_if_indices_twice_nat;
607 /* vector of interface address static mappings to resolve. */
608 snat_static_map_resolve_t *to_resolve;
610 /* Randomize port allocation order */
613 /* Worker handoff frame-queue index */
615 u32 fq_in2out_output_index;
619 u32 error_node_index;
621 /* handoff fq nodes */
622 u32 handoff_out2in_index;
623 u32 handoff_in2out_index;
624 u32 handoff_in2out_output_index;
626 /* respect feature arc nodes */
627 u32 pre_out2in_node_index;
628 u32 pre_in2out_node_index;
630 u32 in2out_node_index;
631 u32 in2out_output_node_index;
632 u32 in2out_fast_node_index;
633 u32 in2out_slowpath_node_index;
634 u32 in2out_slowpath_output_node_index;
635 u32 ed_in2out_node_index;
636 u32 ed_in2out_slowpath_node_index;
637 u32 out2in_node_index;
638 u32 out2in_fast_node_index;
639 u32 ed_out2in_node_index;
640 u32 ed_out2in_slowpath_node_index;
641 u32 det_in2out_node_index;
642 u32 det_out2in_node_index;
644 u32 hairpinning_node_index;
645 u32 hairpin_dst_node_index;
646 u32 hairpin_src_node_index;
647 u32 ed_hairpinning_node_index;
648 u32 ed_hairpin_dst_node_index;
649 u32 ed_hairpin_src_node_index;
652 /* Deterministic NAT mappings */
653 snat_det_map_t *det_maps;
655 /* If forwarding is enabled */
656 u8 forwarding_enabled;
658 /* Config parameters */
659 u8 static_mapping_only;
660 u8 static_mapping_connection_tracking;
663 u8 endpoint_dependent;
664 u32 translation_buckets;
665 u32 translation_memory_size;
666 u32 max_translations;
668 u32 user_memory_size;
669 u32 max_translations_per_user;
671 u32 outside_fib_index;
673 u32 inside_fib_index;
675 /* values of various timeouts */
678 u32 tcp_transitory_timeout;
679 u32 tcp_established_timeout;
681 /* TCP MSS clamping */
685 /* counters/gauges */
686 vlib_simple_counter_main_t total_users;
687 vlib_simple_counter_main_t total_sessions;
689 /* API message ID base */
693 vlib_log_class_t log_class;
698 vlib_main_t *vlib_main;
699 vnet_main_t *vnet_main;
700 ip4_main_t *ip4_main;
701 ip_lookup_main_t *ip4_lookup_main;
702 api_main_t *api_main;
709 } nat44_is_idle_session_ctx_t;
713 u32 cached_sw_if_index;
714 u32 cached_ip4_address;
717 extern snat_main_t snat_main;
719 // nat pre ed next_node feature classification
720 extern vlib_node_registration_t nat_default_node;
721 extern vlib_node_registration_t nat_pre_in2out_node;
722 extern vlib_node_registration_t nat_pre_out2in_node;
724 extern vlib_node_registration_t snat_in2out_node;
725 extern vlib_node_registration_t snat_in2out_output_node;
726 extern vlib_node_registration_t snat_out2in_node;
727 extern vlib_node_registration_t snat_in2out_fast_node;
728 extern vlib_node_registration_t snat_out2in_fast_node;
729 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
730 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
731 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
732 extern vlib_node_registration_t snat_det_in2out_node;
733 extern vlib_node_registration_t snat_det_out2in_node;
734 extern vlib_node_registration_t snat_hairpin_dst_node;
735 extern vlib_node_registration_t snat_hairpin_src_node;
736 extern vlib_node_registration_t nat44_ed_in2out_node;
737 extern vlib_node_registration_t nat44_ed_in2out_output_node;
738 extern vlib_node_registration_t nat44_ed_out2in_node;
739 extern vlib_node_registration_t nat44_ed_hairpin_dst_node;
740 extern vlib_node_registration_t nat44_ed_hairpin_src_node;
741 extern vlib_node_registration_t nat44_ed_in2out_worker_handoff_node;
742 extern vlib_node_registration_t nat44_ed_in2out_output_worker_handoff_node;
743 extern vlib_node_registration_t nat44_ed_out2in_worker_handoff_node;
745 extern fib_source_t nat_fib_src_hi;
746 extern fib_source_t nat_fib_src_low;
748 /* format functions */
749 format_function_t format_snat_user;
750 format_function_t format_snat_static_mapping;
751 format_function_t format_snat_static_map_to_resolve;
752 format_function_t format_snat_session;
753 format_function_t format_det_map_ses;
754 format_function_t format_snat_key;
755 format_function_t format_static_mapping_key;
756 format_function_t format_snat_protocol;
757 format_function_t format_nat_addr_and_port_alloc_alg;
758 /* unformat functions */
759 unformat_function_t unformat_snat_protocol;
761 /** \brief Check if SNAT session is created from static mapping.
762 @param s SNAT session
763 @return 1 if SNAT session is created from static mapping otherwise 0
765 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
767 /** \brief Check if SNAT session for unknown protocol.
768 @param s SNAT session
769 @return 1 if SNAT session for unknown protocol otherwise 0
771 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
773 /** \brief Check if NAT session is twice NAT.
775 @return 1 if NAT session is twice NAT
777 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
779 /** \brief Check if NAT session is load-balancing.
781 @return 1 if NAT session is load-balancing
783 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
785 /** \brief Check if NAT session is forwarding bypass.
787 @return 1 if NAT session is load-balancing
789 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
791 /** \brief Check if NAT session is endpoint dependent.
793 @return 1 if NAT session is endpoint dependent
795 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
797 /** \brief Check if NAT session has affinity record.
799 @return 1 if NAT session has affinity record
801 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
803 /** \brief Check if NAT interface is inside.
804 @param i NAT interfce
805 @return 1 if inside interface
807 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
809 /** \brief Check if NAT interface is outside.
810 @param i NAT interfce
811 @return 1 if outside interface
813 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
815 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
817 @return 1 if session is closed
819 #define nat44_is_ses_closed(s) s->state == 0xf
821 /** \brief Check if NAT static mapping is address only (1:1NAT).
822 @param sm NAT static mapping
823 @return 1 if 1:1NAT, 0 if 1:1NAPT
825 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
827 /** \brief Check if NAT static mapping match only out2in direction.
828 @param sm NAT static mapping
829 @return 1 if rule match only out2in direction
831 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
833 /** \brief Check if NAT static mapping is identity NAT.
834 @param sm NAT static mapping
835 @return 1 if identity NAT
837 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
839 /** \brief Check if NAT static mapping is load-balancing.
840 @param sm NAT static mapping
841 @return 1 if load-balancing
843 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
845 /** \brief Check if client initiating TCP connection (received SYN from client)
847 @return 1 if client initiating TCP connection
850 tcp_flags_is_init (u8 f)
852 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
856 #define nat_log_err(...) \
857 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
858 #define nat_log_warn(...) \
859 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
860 #define nat_log_notice(...) \
861 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
862 #define nat_log_info(...) \
863 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
864 #define nat_log_debug(...)\
865 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
867 /* NAT API Logging Levels */
868 #define foreach_nat_log_level \
871 _(0x02, LOG_WARNING) \
872 _(0x03, LOG_NOTICE) \
876 typedef enum nat_log_level_t_
878 #define _(n,f) SNAT_##f = n,
879 foreach_nat_log_level
883 #define nat_elog(_level, _str) \
886 snat_main_t *sm = &snat_main; \
887 if (PREDICT_FALSE (sm->log_level >= _level)) \
889 ELOG_TYPE_DECLARE (e) = \
891 .format = "nat-msg " _str, \
894 ELOG_DATA (&sm->vlib_main->elog_main, e); \
898 #define nat_elog_addr(_level, _str, _addr) \
901 if (PREDICT_FALSE (sm->log_level >= _level)) \
903 ELOG_TYPE_DECLARE (e) = \
905 .format = "nat-msg " _str " %d.%d.%d.%d", \
906 .format_args = "i1i1i1i1", \
915 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
916 ed->oct4 = _addr >> 24; \
917 ed->oct3 = _addr >> 16; \
918 ed->oct2 = _addr >> 8; \
923 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
926 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
928 ELOG_TYPE_DECLARE (e) = \
930 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
931 " tid from: %d to: %d fib: %d", \
932 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
948 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
949 ed->src_oct1 = _src >> 24; \
950 ed->src_oct2 = _src >> 16; \
951 ed->src_oct3 = _src >> 8; \
952 ed->src_oct4 = _src; \
953 ed->dst_oct1 = _dst >> 24; \
954 ed->dst_oct2 = _dst >> 16; \
955 ed->dst_oct3 = _dst >> 8; \
956 ed->dst_oct4 = _dst; \
957 ed->ftid = vlib_get_thread_index (); \
963 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
966 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
968 ELOG_TYPE_DECLARE (e) = \
970 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
971 " tid:%d prt:%d fib:%d", \
972 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
988 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
989 ed->src_oct1 = _src >> 24; \
990 ed->src_oct2 = _src >> 16; \
991 ed->src_oct3 = _src >> 8; \
992 ed->src_oct4 = _src; \
993 ed->dst_oct1 = _dst >> 24; \
994 ed->dst_oct2 = _dst >> 16; \
995 ed->dst_oct3 = _dst >> 8; \
996 ed->dst_oct4 = _dst; \
997 ed->tid = vlib_get_thread_index (); \
1003 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
1006 snat_main_t *sm = &snat_main; \
1007 if (PREDICT_FALSE (sm->log_level >= _level)) \
1009 ELOG_TYPE_DECLARE (e) = \
1011 .format = "nat-msg " _fmt, \
1012 .format_args = _arg, \
1014 CLIB_PACKED(struct \
1016 typeof (_val1) val1; \
1018 ed = ELOG_DATA (&sm->vlib_main->elog_main, e); \
1023 #define nat_elog_notice(nat_elog_str) \
1024 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
1025 #define nat_elog_warn(nat_elog_str) \
1026 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1027 #define nat_elog_err(nat_elog_str) \
1028 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1029 #define nat_elog_debug(nat_elog_str) \
1030 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1031 #define nat_elog_info(nat_elog_str) \
1032 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1034 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1035 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1036 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1037 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1038 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1039 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1040 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1041 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1042 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1043 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1045 /* ICMP session match functions */
1046 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1047 u32 thread_index, vlib_buffer_t * b0,
1048 ip4_header_t * ip0, u8 * p_proto,
1049 snat_session_key_t * p_value,
1050 u8 * p_dont_translate, void *d, void *e);
1051 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1052 u32 thread_index, vlib_buffer_t * b0,
1053 ip4_header_t * ip0, u8 * p_proto,
1054 snat_session_key_t * p_value,
1055 u8 * p_dont_translate, void *d, void *e);
1056 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1057 u32 thread_index, vlib_buffer_t * b0,
1058 ip4_header_t * ip0, u8 * p_proto,
1059 snat_session_key_t * p_value,
1060 u8 * p_dont_translate, void *d, void *e);
1061 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1062 u32 thread_index, vlib_buffer_t * b0,
1063 ip4_header_t * ip0, u8 * p_proto,
1064 snat_session_key_t * p_value,
1065 u8 * p_dont_translate, void *d, void *e);
1067 /* ICMP deterministic NAT session match functions */
1068 u32 icmp_match_out2in_det (snat_main_t * sm, vlib_node_runtime_t * node,
1069 u32 thread_index, vlib_buffer_t * b0,
1070 ip4_header_t * ip0, u8 * p_proto,
1071 snat_session_key_t * p_value,
1072 u8 * p_dont_translate, void *d, void *e);
1073 u32 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
1074 u32 thread_index, vlib_buffer_t * b0,
1075 ip4_header_t * ip0, u8 * p_proto,
1076 snat_session_key_t * p_value,
1077 u8 * p_dont_translate, void *d, void *e);
1079 /* ICMP endpoint-dependent session match functions */
1080 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1081 u32 thread_index, vlib_buffer_t * b0,
1082 ip4_header_t * ip0, u8 * p_proto,
1083 snat_session_key_t * p_value,
1084 u8 * p_dont_translate, void *d, void *e);
1085 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1086 u32 thread_index, vlib_buffer_t * b0,
1087 ip4_header_t * ip0, u8 * p_proto,
1088 snat_session_key_t * p_value,
1089 u8 * p_dont_translate, void *d, void *e);
1091 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1092 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1093 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1096 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1097 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1098 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1101 /* hairpinning functions */
1102 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1103 ip4_header_t * ip0, icmp46_header_t * icmp0,
1105 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1107 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1109 int snat_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1110 ip4_header_t * ip0, udp_header_t * udp0,
1111 tcp_header_t * tcp0, u32 proto0, int is_ed);
1112 void nat44_reass_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1113 ip4_header_t * ip0, u16 sport, u16 dport,
1114 u32 proto0, int is_ed);
1116 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1117 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1118 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1119 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1120 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1123 * @brief Add external address to NAT44 pool
1125 * @param addr IPv4 address
1126 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1127 * @param twice_nat 1 if twice NAT address
1129 * @return 0 on success, non-zero value otherwise
1131 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1135 * @brief Delete external address from NAT44 pool
1137 * @param addr IPv4 address
1138 * @param delete_sm 1 if delete static mapping using address
1139 * @param twice_nat 1 if twice NAT address
1141 * @return 0 on success, non-zero value otherwise
1143 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1147 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1149 * @param addr IPv4 address
1150 * @param is_add 1 = add, 0 = delete
1152 * @return 0 on success, non-zero value otherwise
1154 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1157 * @brief Add/delete NAT44 static mapping
1159 * @param l_addr local IPv4 address
1160 * @param e_addr external IPv4 address
1161 * @param l_port local port number
1162 * @param e_port external port number
1163 * @param vrf_id local VRF ID
1164 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1165 * @param sw_if_index use interface address as external IPv4 address
1166 * @param proto L4 protocol
1167 * @param is_add 1 = add, 0 = delete
1168 * @param twice_nat twice-nat mode
1169 * @param out2in_only if 1 rule match only out2in direction
1170 * @param tag opaque string tag
1171 * @param identity_nat identity NAT
1173 * @return 0 on success, non-zero value otherwise
1175 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1176 u16 l_port, u16 e_port, u32 vrf_id,
1177 int addr_only, u32 sw_if_index,
1178 snat_protocol_t proto, int is_add,
1179 twice_nat_type_t twice_nat, u8 out2in_only,
1180 u8 * tag, u8 identity_nat);
1183 * @brief Add/delete static mapping with load-balancing (multiple backends)
1185 * @param e_addr external IPv4 address
1186 * @param e_port external port number
1187 * @param proto L4 protocol
1188 * @param locals list of local backends
1189 * @param is_add 1 = add, 0 = delete
1190 * @param twice_nat twice-nat mode
1191 * @param out2in_only if 1 rule match only out2in direction
1192 * @param tag opaque string tag
1193 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1195 * @return 0 on success, non-zero value otherwise
1197 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1198 snat_protocol_t proto,
1199 nat44_lb_addr_port_t * locals, u8 is_add,
1200 twice_nat_type_t twice_nat,
1201 u8 out2in_only, u8 * tag, u32 affinity);
1203 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1204 ip4_address_t l_addr, u16 l_port,
1205 snat_protocol_t proto, u32 vrf_id,
1206 u8 probability, u8 is_add);
1208 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1211 * @brief Set NAT plugin workers
1213 * @param bitmap NAT workers bitmap
1215 * @return 0 on success, non-zero value otherwise
1217 int snat_set_workers (uword * bitmap);
1220 * @brief Enable/disable NAT44 feature on the interface
1222 * @param sw_if_index software index of the interface
1223 * @param is_inside 1 = inside, 0 = outside
1224 * @param is_del 1 = delete, 0 = add
1226 * @return 0 on success, non-zero value otherwise
1228 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1231 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1233 * @param sw_if_index software index of the interface
1234 * @param is_inside 1 = inside, 0 = outside
1235 * @param is_del 1 = delete, 0 = add
1237 * @return 0 on success, non-zero value otherwise
1239 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1243 * @brief Add/delete NAT44 pool address from specific interfce
1245 * @param sw_if_index software index of the interface
1246 * @param is_del 1 = delete, 0 = add
1247 * @param twice_nat 1 = twice NAT address for extenal hosts
1249 * @return 0 on success, non-zero value otherwise
1251 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1255 * @brief Delete NAT44 session
1257 * @param addr IPv4 address
1258 * @param port L4 port number
1259 * @param proto L4 protocol
1260 * @param vrf_id VRF ID
1261 * @param is_in 1 = inside network address and port pair, 0 = outside
1263 * @return 0 on success, non-zero value otherwise
1265 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1266 snat_protocol_t proto, u32 vrf_id, int is_in);
1269 * @brief Delete NAT44 endpoint-dependent session
1271 * @param addr IPv4 address
1272 * @param port L4 port number
1273 * @param proto L4 protocol
1274 * @param vrf_id VRF ID
1275 * @param is_in 1 = inside network address and port pair, 0 = outside
1277 * @return 0 on success, non-zero value otherwise
1279 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1280 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1281 u32 vrf_id, int is_in);
1284 * @brief Free NAT44 session data (lookup keys, external addrres port)
1286 * @param s NAT session
1287 * @param thread_index thread index
1288 * @param is_ha is HA event
1290 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1291 u32 thread_index, u8 is_ha);
1294 * @brief Find or create NAT user
1296 * @param addr IPv4 address
1297 * @param fib_index FIB table index
1298 * @param thread_index thread index
1300 * @return NAT user data structure on success otherwise zero value
1302 snat_user_t *nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr,
1303 u32 fib_index, u32 thread_index);
1306 * @brief Allocate new NAT session or recycle last used
1309 * @param thread_index thread index
1311 * @return session data structure on success otherwise zero value
1313 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1315 u32 thread_index, f64 now);
1318 * @brief Allocate NAT endpoint-dependent session
1321 * @param thread_index thread index
1323 * @return session data structure on success otherwise zero value
1325 snat_session_t *nat_ed_session_alloc (snat_main_t * sm, snat_user_t * u,
1326 u32 thread_index, f64 now);
1329 * @brief Set address and port assignment algorithm for MAP-E CE
1331 * @param psid Port Set Identifier value
1332 * @param psid_offset number of offset bits
1333 * @param psid_length length of PSID
1335 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1339 * @brief Set address and port assignment algorithm for port range
1341 * @param start_port beginning of the port range
1342 * @param end_port end of the port range
1344 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1347 * @brief Set address and port assignment algorithm to default/standard
1349 void nat_set_alloc_addr_and_port_default (void);
1352 * @brief Free outside address and port pair
1354 * @param addresses vector of outside addresses
1355 * @param thread_index thread index
1356 * @param k address, port and protocol
1358 void snat_free_outside_address_and_port (snat_address_t * addresses,
1360 snat_session_key_t * k);
1363 * @brief Alloc outside address and port
1365 * @param addresses vector of outside addresses
1366 * @param fib_index FIB table index
1367 * @param thread_index thread index
1368 * @param k allocated address and port pair
1369 * @param port_per_thread number of ports per threead
1370 * @param snat_thread_index NAT thread index
1372 * @return 0 on success, non-zero value otherwise
1374 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1377 snat_session_key_t * k,
1378 u16 port_per_thread,
1379 u32 snat_thread_index);
1382 * @brief Match NAT44 static mapping.
1384 * @param match address and port to match
1385 * @param mapping external/local address and port of the matched mapping
1386 * @param by_external if 0 match by local address otherwise match by external
1388 * @param is_addr_only 1 if matched mapping is address only
1389 * @param twice_nat matched mapping is twice NAT type
1390 * @param lb 1 if matched mapping is load-balanced
1391 * @param ext_host_addr external host address
1393 * @returns 0 if match found otherwise 1.
1395 int snat_static_mapping_match (snat_main_t * sm,
1396 snat_session_key_t match,
1397 snat_session_key_t * mapping,
1400 twice_nat_type_t * twice_nat,
1402 ip4_address_t * ext_host_addr,
1403 u8 * is_identity_nat);
1406 * @brief Add/del NAT address to FIB.
1408 * Add the external NAT address to the FIB as receive entries. This ensures
1409 * that VPP will reply to ARP for this address and we don't need to enable
1410 * proxy ARP on the outside interface.
1412 * @param addr IPv4 address
1413 * @param plen address prefix length
1414 * @param sw_if_index software index of the outside interface
1415 * @param is_add 0 = delete, 1 = add.
1417 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1418 u8 p_len, u32 sw_if_index, int is_add);
1421 * Why is this here? Because we don't need to touch this layer to
1422 * simply reply to an icmp. We need to change id to a unique
1423 * value to NAT an echo request/reply.
1430 } icmp_echo_header_t;
1434 u16 src_port, dst_port;
1437 #endif /* __included_nat_h__ */
1440 * fd.io coding-style-patch-verification: ON
1443 * eval: (c-set-style "gnu")