2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * NAT plugin global declarations
19 #ifndef __included_nat_h__
20 #define __included_nat_h__
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/ip/icmp46_packet.h>
26 #include <vnet/api_errno.h>
27 #include <vnet/fib/fib_source.h>
28 #include <vppinfra/elog.h>
29 #include <vppinfra/bihash_8_8.h>
30 #include <vppinfra/bihash_16_8.h>
31 #include <vppinfra/dlist.h>
32 #include <vppinfra/error.h>
33 #include <vlibapi/api.h>
35 #include <vppinfra/bihash_16_8.h>
36 #include <nat/lib/lib.h>
37 #include <nat/lib/inlines.h>
39 /* default session timeouts */
40 #define SNAT_UDP_TIMEOUT 300
41 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
42 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
43 #define SNAT_ICMP_TIMEOUT 60
45 /* number of worker handoff frame queue elements */
46 #define NAT_FQ_NELTS 64
48 /* NAT buffer flags */
49 #define SNAT_FLAG_HAIRPINNING (1 << 0)
55 NAT_NEXT_IN2OUT_ED_FAST_PATH,
56 NAT_NEXT_IN2OUT_ED_SLOW_PATH,
57 NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
58 NAT_NEXT_OUT2IN_ED_FAST_PATH,
59 NAT_NEXT_OUT2IN_ED_SLOW_PATH,
60 NAT_NEXT_IN2OUT_CLASSIFY,
61 NAT_NEXT_OUT2IN_CLASSIFY,
71 /* deterministic session outside key */
78 ip4_address_t ext_host_addr;
86 /* user (internal host) key */
100 /* NAT API Configuration flags */
101 #define foreach_nat_config_flag \
102 _(0x01, IS_TWICE_NAT) \
103 _(0x02, IS_SELF_TWICE_NAT) \
104 _(0x04, IS_OUT2IN_ONLY) \
105 _(0x08, IS_ADDR_ONLY) \
106 _(0x10, IS_OUTSIDE) \
109 _(0x80, IS_EXT_HOST_VALID) \
111 typedef enum nat_config_flags_t_
113 #define _(n,f) NAT_API_##f = n,
114 foreach_nat_config_flag
116 } nat_config_flags_t;
118 /* External address and port allocation modes */
119 #define foreach_nat_addr_and_port_alloc_alg \
120 _(0, DEFAULT, "default") \
121 _(1, MAPE, "map-e") \
122 _(2, RANGE, "port-range")
126 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
127 foreach_nat_addr_and_port_alloc_alg
129 } nat_addr_and_port_alloc_alg_t;
132 #define foreach_snat_session_state \
133 _(0, UNKNOWN, "unknown") \
134 _(1, UDP_ACTIVE, "udp-active") \
135 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
136 _(3, TCP_ESTABLISHED, "tcp-established") \
137 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
138 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
139 _(6, TCP_CLOSING, "tcp-closing") \
140 _(7, TCP_LAST_ACK, "tcp-last-ack") \
141 _(8, TCP_CLOSED, "tcp-closed") \
142 _(9, ICMP_ACTIVE, "icmp-active")
146 #define _(v, N, s) SNAT_SESSION_##N = v,
147 foreach_snat_session_state
149 } snat_session_state_t;
151 #define foreach_nat_in2out_ed_error \
152 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
153 _(IN2OUT_PACKETS, "good in2out packets processed") \
154 _(OUT_OF_PORTS, "out of ports") \
155 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
156 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
157 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
158 _(DROP_FRAGMENT, "drop fragment") \
159 _(CANNOT_CREATE_USER, "cannot create NAT user") \
160 _(NON_SYN, "non-SYN packet try to create session") \
161 _(TCP_PACKETS, "TCP packets") \
162 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
163 _(UDP_PACKETS, "UDP packets") \
164 _(ICMP_PACKETS, "ICMP packets") \
165 _(OTHER_PACKETS, "other protocol packets") \
166 _(FRAGMENTS, "fragments") \
167 _(CACHED_FRAGMENTS, "cached fragments") \
168 _(PROCESSED_FRAGMENTS, "processed fragments")
172 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
173 foreach_nat_in2out_ed_error
175 NAT_IN2OUT_ED_N_ERROR,
176 } nat_in2out_ed_error_t;
178 #define foreach_nat_out2in_ed_error \
179 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
180 _(OUT2IN_PACKETS, "good out2in packets processed") \
181 _(OUT_OF_PORTS, "out of ports") \
182 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
183 _(NO_TRANSLATION, "no translation") \
184 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
185 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
186 _(DROP_FRAGMENT, "drop fragment") \
187 _(CANNOT_CREATE_USER, "cannot create NAT user") \
188 _(NON_SYN, "non-SYN packet try to create session") \
189 _(TCP_PACKETS, "TCP packets") \
190 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
191 _(UDP_PACKETS, "UDP packets") \
192 _(ICMP_PACKETS, "ICMP packets") \
193 _(OTHER_PACKETS, "other protocol packets") \
194 _(FRAGMENTS, "fragments") \
195 _(CACHED_FRAGMENTS, "cached fragments") \
196 _(PROCESSED_FRAGMENTS, "processed fragments")
200 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
201 foreach_nat_out2in_ed_error
203 NAT_OUT2IN_ED_N_ERROR,
204 } nat_out2in_ed_error_t;
207 /* Endpoint dependent TCP session state */
208 #define NAT44_SES_I2O_FIN 1
209 #define NAT44_SES_O2I_FIN 2
210 #define NAT44_SES_I2O_FIN_ACK 4
211 #define NAT44_SES_O2I_FIN_ACK 8
212 #define NAT44_SES_I2O_SYN 16
213 #define NAT44_SES_O2I_SYN 32
214 #define NAT44_SES_RST 64
217 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
218 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
219 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
220 #define SNAT_SESSION_FLAG_TWICE_NAT 8
221 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
222 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
223 #define SNAT_SESSION_FLAG_AFFINITY 64
224 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
226 /* NAT interface flags */
227 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
228 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
230 /* Static mapping flags */
231 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
232 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
233 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
234 #define NAT_STATIC_MAPPING_FLAG_LB 8
237 typedef CLIB_PACKED(struct
239 /* Outside network tuple */
247 /* Inside network tuple */
255 nat_protocol_t nat_proto;
260 /* Per-user translations */
262 u32 per_user_list_head_index;
264 /* head of LRU list in which this session is tracked */
266 /* index in global LRU list */
270 /* Last heard timer */
273 /* Last HA refresh */
274 f64 ha_last_refreshed;
280 /* External host address and port */
281 ip4_address_t ext_host_addr;
284 /* External host address and port after translation */
285 ip4_address_t ext_host_nat_addr;
286 u16 ext_host_nat_port;
288 /* TCP session state */
292 u64 tcp_closed_timestamp;
304 u32 sessions_per_user_list_head_index;
314 #define _(N, i, n, s) \
315 u16 busy_##n##_ports; \
316 u16 * busy_##n##_ports_per_thread; \
317 u32 busy_##n##_port_refcounts[65535];
331 /* Inside network port */
333 /* Outside network address and port */
334 snat_det_out_key_t out;
339 } snat_det_session_t;
343 /* inside IP address range */
344 ip4_address_t in_addr;
346 /* outside IP address range */
347 ip4_address_t out_addr;
349 /* inside IP addresses / outside IP addresses */
351 /* number of ports available to internal host */
353 /* session counter */
355 /* vector of sessions */
356 snat_det_session_t *sessions;
361 /* backend IP address */
363 /* backend port number */
365 /* probability of the backend to be randomly matched */
368 /* backend FIB table */
371 } nat44_lb_addr_port_t;
375 /* twice-nat disabled */
377 /* twice-nat enabled */
379 /* twice-nat only when src IP equals dst IP after translation */
385 /* no load-balancing */
389 /* load-balancing with affinity */
395 /* local IP address */
396 ip4_address_t local_addr;
397 /* external IP address */
398 ip4_address_t external_addr;
404 twice_nat_type_t twice_nat;
405 /* local FIB table */
409 nat_protocol_t proto;
410 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
412 /* worker threads used by backends/local host */
414 /* opaque string tag */
416 /* backends for load-balancing mode */
417 nat44_lb_addr_port_t *locals;
418 /* affinity per service lis */
419 u32 affinity_per_service_list_head_index;
422 } snat_static_mapping_t;
432 ip4_address_t l_addr;
437 nat_protocol_t proto;
445 } snat_static_map_resolve_t;
449 /* Main lookup tables */
450 clib_bihash_8_8_t out2in;
451 clib_bihash_8_8_t in2out;
453 /* Endpoint dependent sessions lookup tables */
454 clib_bihash_16_8_t in2out_ed;
456 /* Find-a-user => src address lookup */
457 clib_bihash_8_8_t user_hash;
463 snat_session_t *sessions;
465 /* Pool of doubly-linked list elements */
466 dlist_elt_t *list_pool;
468 /* LRU session list - head is stale, tail is fresh */
469 dlist_elt_t *lru_pool;
470 u32 tcp_trans_lru_head_index;
471 u32 tcp_estab_lru_head_index;
472 u32 udp_lru_head_index;
473 u32 icmp_lru_head_index;
474 u32 unk_proto_lru_head_index;
476 /* NAT thread index */
477 u32 snat_thread_index;
479 /* real thread index */
482 } snat_main_per_thread_data_t;
486 /* ICMP session match function */
487 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
488 vlib_node_runtime_t * node,
492 ip4_address_t * addr,
495 nat_protocol_t * proto,
497 u8 * dont_translate);
499 /* Return worker thread index for given packet */
500 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
504 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
509 /* NAT address and port allocation function */
510 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
514 nat_protocol_t proto,
515 ip4_address_t * addr,
518 u32 snat_thread_index);
521 typedef struct snat_main_s
523 /* ICMP session match functions */
524 snat_icmp_match_function_t *icmp_match_in2out_cb;
525 snat_icmp_match_function_t *icmp_match_out2in_cb;
527 /* Thread settings */
529 u32 first_worker_index;
531 snat_get_worker_in2out_function_t *worker_in2out_cb;
532 snat_get_worker_out2in_function_t *worker_out2in_cb;
535 /* Per thread data */
536 snat_main_per_thread_data_t *per_thread_data;
538 /* Find a static mapping by local */
539 clib_bihash_8_8_t static_mapping_by_local;
541 /* Find a static mapping by external */
542 clib_bihash_8_8_t static_mapping_by_external;
544 /* Static mapping pool */
545 snat_static_mapping_t *static_mappings;
547 /* Endpoint-dependent out2in mappings */
548 clib_bihash_16_8_t out2in_ed;
551 snat_interface_t *interfaces;
552 snat_interface_t *output_feature_interfaces;
554 /* Vector of outside addresses */
555 snat_address_t *addresses;
556 /* Address and port allocation function */
557 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
558 /* Address and port allocation type */
559 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
560 /* Port set parameters (MAP-E) */
564 /* Port range parameters */
568 /* vector of outside fibs */
569 nat_outside_fib_t *outside_fibs;
571 /* Vector of twice NAT addresses for external hosts */
572 snat_address_t *twice_nat_addresses;
574 /* sw_if_indices whose intfc addresses should be auto-added */
575 u32 *auto_add_sw_if_indices;
576 u32 *auto_add_sw_if_indices_twice_nat;
578 /* vector of interface address static mappings to resolve. */
579 snat_static_map_resolve_t *to_resolve;
581 /* Randomize port allocation order */
584 /* Worker handoff frame-queue index */
586 u32 fq_in2out_output_index;
590 u32 error_node_index;
592 /* handoff fq nodes */
593 u32 handoff_out2in_index;
594 u32 handoff_in2out_index;
595 u32 handoff_in2out_output_index;
597 /* respect feature arc nodes */
598 u32 pre_out2in_node_index;
599 u32 pre_in2out_node_index;
601 u32 in2out_node_index;
602 u32 in2out_output_node_index;
603 u32 in2out_fast_node_index;
604 u32 in2out_slowpath_node_index;
605 u32 in2out_slowpath_output_node_index;
606 u32 ed_in2out_node_index;
607 u32 ed_in2out_slowpath_node_index;
608 u32 out2in_node_index;
609 u32 out2in_fast_node_index;
610 u32 ed_out2in_node_index;
611 u32 ed_out2in_slowpath_node_index;
612 u32 det_in2out_node_index;
613 u32 det_out2in_node_index;
615 u32 hairpinning_node_index;
616 u32 hairpin_dst_node_index;
617 u32 hairpin_src_node_index;
618 u32 ed_hairpinning_node_index;
619 u32 ed_hairpin_dst_node_index;
620 u32 ed_hairpin_src_node_index;
623 /* Deterministic NAT mappings */
624 snat_det_map_t *det_maps;
626 /* If forwarding is enabled */
627 u8 forwarding_enabled;
629 /* Config parameters */
630 u8 static_mapping_only;
631 u8 static_mapping_connection_tracking;
634 u8 endpoint_dependent;
636 u32 translation_buckets;
637 uword translation_memory_size;
638 u32 max_translations_per_thread;
639 u32 *max_translations_per_fib;
640 u32 max_users_per_thread;
642 uword user_memory_size;
643 u32 max_translations_per_user;
646 u32 outside_fib_index;
648 u32 inside_fib_index;
650 /* values of various timeouts */
653 u32 tcp_transitory_timeout;
654 u32 tcp_established_timeout;
657 /* TCP MSS clamping */
660 /* counters/gauges */
661 vlib_simple_counter_main_t total_users;
662 vlib_simple_counter_main_t total_sessions;
663 vlib_simple_counter_main_t user_limit_reached;;
665 /* API message ID base */
669 vlib_log_class_t log_class;
674 vnet_main_t *vnet_main;
675 ip4_main_t *ip4_main;
676 ip_lookup_main_t *ip4_lookup_main;
677 api_main_t *api_main;
684 } nat44_is_idle_session_ctx_t;
688 u32 cached_sw_if_index;
689 u32 cached_ip4_address;
692 extern snat_main_t snat_main;
694 // nat pre ed next_node feature classification
695 extern vlib_node_registration_t nat_default_node;
696 extern vlib_node_registration_t nat_pre_in2out_node;
697 extern vlib_node_registration_t nat_pre_out2in_node;
699 extern vlib_node_registration_t snat_in2out_node;
700 extern vlib_node_registration_t snat_in2out_output_node;
701 extern vlib_node_registration_t snat_out2in_node;
702 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
703 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
704 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
705 extern vlib_node_registration_t snat_det_in2out_node;
706 extern vlib_node_registration_t snat_det_out2in_node;
707 extern vlib_node_registration_t nat44_ed_in2out_node;
708 extern vlib_node_registration_t nat44_ed_in2out_output_node;
709 extern vlib_node_registration_t nat44_ed_out2in_node;
711 extern fib_source_t nat_fib_src_hi;
712 extern fib_source_t nat_fib_src_low;
714 /* format functions */
715 format_function_t format_snat_user;
716 format_function_t format_snat_static_mapping;
717 format_function_t format_snat_static_map_to_resolve;
718 format_function_t format_snat_session;
719 format_function_t format_det_map_ses;
720 format_function_t format_snat_key;
721 format_function_t format_static_mapping_key;
722 format_function_t format_nat_protocol;
723 format_function_t format_nat_addr_and_port_alloc_alg;
724 /* unformat functions */
725 unformat_function_t unformat_nat_protocol;
727 /** \brief Check if SNAT session is created from static mapping.
728 @param s SNAT session
729 @return 1 if SNAT session is created from static mapping otherwise 0
731 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
733 /** \brief Check if SNAT session for unknown protocol.
734 @param s SNAT session
735 @return 1 if SNAT session for unknown protocol otherwise 0
737 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
739 /** \brief Check if NAT session is twice NAT.
741 @return 1 if NAT session is twice NAT
743 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
745 /** \brief Check if NAT session is load-balancing.
747 @return 1 if NAT session is load-balancing
749 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
751 /** \brief Check if NAT session is forwarding bypass.
753 @return 1 if NAT session is load-balancing
755 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
757 /** \brief Check if NAT session is endpoint dependent.
759 @return 1 if NAT session is endpoint dependent
761 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
763 /** \brief Check if NAT session has affinity record.
765 @return 1 if NAT session has affinity record
767 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
769 /** \brief Check if NAT interface is inside.
770 @param i NAT interface
771 @return 1 if inside interface
773 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
775 /** \brief Check if NAT interface is outside.
776 @param i NAT interface
777 @return 1 if outside interface
779 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
781 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
783 @return 1 if session is closed
785 #define nat44_is_ses_closed(s) s->state == 0xf
787 /** \brief Check if NAT static mapping is address only (1:1NAT).
788 @param sm NAT static mapping
789 @return 1 if 1:1NAT, 0 if 1:1NAPT
791 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
793 /** \brief Check if NAT static mapping match only out2in direction.
794 @param sm NAT static mapping
795 @return 1 if rule match only out2in direction
797 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
799 /** \brief Check if NAT static mapping is identity NAT.
800 @param sm NAT static mapping
801 @return 1 if identity NAT
803 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
805 /** \brief Check if NAT static mapping is load-balancing.
806 @param sm NAT static mapping
807 @return 1 if load-balancing
809 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
811 /** \brief Check if client initiating TCP connection (received SYN from client)
813 @return 1 if client initiating TCP connection
816 tcp_flags_is_init (u8 f)
818 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
822 #define nat_log_err(...) \
823 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
824 #define nat_log_warn(...) \
825 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
826 #define nat_log_notice(...) \
827 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
828 #define nat_log_info(...) \
829 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
830 #define nat_log_debug(...)\
831 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
833 /* NAT API Logging Levels */
834 #define foreach_nat_log_level \
837 _(0x02, LOG_WARNING) \
838 _(0x03, LOG_NOTICE) \
842 typedef enum nat_log_level_t_
844 #define _(n,f) SNAT_##f = n,
845 foreach_nat_log_level
849 #define nat_elog(_level, _str) \
852 snat_main_t *sm = &snat_main; \
853 if (PREDICT_FALSE (sm->log_level >= _level)) \
855 ELOG_TYPE_DECLARE (e) = \
857 .format = "nat-msg " _str, \
860 ELOG_DATA (&vlib_global_main.elog_main, e); \
864 #define nat_elog_addr(_level, _str, _addr) \
867 if (PREDICT_FALSE (sm->log_level >= _level)) \
869 ELOG_TYPE_DECLARE (e) = \
871 .format = "nat-msg " _str " %d.%d.%d.%d", \
872 .format_args = "i1i1i1i1", \
881 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
882 ed->oct4 = _addr >> 24; \
883 ed->oct3 = _addr >> 16; \
884 ed->oct2 = _addr >> 8; \
889 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
892 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
894 ELOG_TYPE_DECLARE (e) = \
896 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
897 " tid from: %d to: %d fib: %d", \
898 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
914 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
915 ed->src_oct1 = _src >> 24; \
916 ed->src_oct2 = _src >> 16; \
917 ed->src_oct3 = _src >> 8; \
918 ed->src_oct4 = _src; \
919 ed->dst_oct1 = _dst >> 24; \
920 ed->dst_oct2 = _dst >> 16; \
921 ed->dst_oct3 = _dst >> 8; \
922 ed->dst_oct4 = _dst; \
923 ed->ftid = vlib_get_thread_index (); \
929 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
932 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
934 ELOG_TYPE_DECLARE (e) = \
936 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
937 " tid:%d prt:%d fib:%d", \
938 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
954 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
955 ed->src_oct1 = _src >> 24; \
956 ed->src_oct2 = _src >> 16; \
957 ed->src_oct3 = _src >> 8; \
958 ed->src_oct4 = _src; \
959 ed->dst_oct1 = _dst >> 24; \
960 ed->dst_oct2 = _dst >> 16; \
961 ed->dst_oct3 = _dst >> 8; \
962 ed->dst_oct4 = _dst; \
963 ed->tid = vlib_get_thread_index (); \
969 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
972 snat_main_t *sm = &snat_main; \
973 if (PREDICT_FALSE (sm->log_level >= _level)) \
975 ELOG_TYPE_DECLARE (e) = \
977 .format = "nat-msg " _fmt, \
978 .format_args = _arg, \
982 typeof (_val1) val1; \
984 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
989 #define nat_elog_notice(nat_elog_str) \
990 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
991 #define nat_elog_warn(nat_elog_str) \
992 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
993 #define nat_elog_err(nat_elog_str) \
994 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
995 #define nat_elog_debug(nat_elog_str) \
996 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
997 #define nat_elog_info(nat_elog_str) \
998 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1000 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1001 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1002 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1003 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1004 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1005 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1006 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1007 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1008 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1009 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1011 /* ICMP session match functions */
1012 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1013 u32 thread_index, vlib_buffer_t * b0,
1014 ip4_header_t * ip0, ip4_address_t * addr,
1015 u16 * port, u32 * fib_index,
1016 nat_protocol_t * proto, void *d, void *e,
1017 u8 * dont_translate);
1018 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1019 u32 thread_index, vlib_buffer_t * b0,
1020 ip4_header_t * ip0, ip4_address_t * addr,
1021 u16 * port, u32 * fib_index,
1022 nat_protocol_t * proto, void *d, void *e,
1023 u8 * dont_translate);
1024 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1025 u32 thread_index, vlib_buffer_t * b0,
1026 ip4_header_t * ip0, ip4_address_t * addr,
1027 u16 * port, u32 * fib_index,
1028 nat_protocol_t * proto, void *d, void *e,
1029 u8 * dont_translate);
1030 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1031 u32 thread_index, vlib_buffer_t * b0,
1032 ip4_header_t * ip0, ip4_address_t * addr,
1033 u16 * port, u32 * fib_index,
1034 nat_protocol_t * proto, void *d, void *e,
1035 u8 * dont_translate);
1037 /* ICMP deterministic NAT session match functions */
1038 u32 icmp_match_out2in_det (snat_main_t * sm, vlib_node_runtime_t * node,
1039 u32 thread_index, vlib_buffer_t * b0,
1040 ip4_header_t * ip0, ip4_address_t * addr,
1041 u16 * port, u32 * fib_index,
1042 nat_protocol_t * proto, void *d, void *e,
1043 u8 * dont_translate);
1044 u32 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
1045 u32 thread_index, vlib_buffer_t * b0,
1046 ip4_header_t * ip0, ip4_address_t * addr,
1047 u16 * port, u32 * fib_index,
1048 nat_protocol_t * proto, void *d, void *e,
1049 u8 * dont_translate);
1051 /* ICMP endpoint-dependent session match functions */
1052 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1053 u32 thread_index, vlib_buffer_t * b0,
1054 ip4_header_t * ip0, ip4_address_t * addr,
1055 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1056 void *d, void *e, u8 * dont_translate);
1057 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1058 u32 thread_index, vlib_buffer_t * b0,
1059 ip4_header_t * ip0, ip4_address_t * addr,
1060 u16 * port, u32 * fib_index, nat_protocol_t * proto,
1061 void *d, void *e, u8 * dont_translate);
1063 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1064 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1065 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1068 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1069 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1070 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1073 /* hairpinning functions */
1074 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1075 ip4_header_t * ip0, icmp46_header_t * icmp0,
1077 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1079 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1081 int snat_hairpinning (vlib_main_t * vm, vlib_node_runtime_t * node,
1082 snat_main_t * sm, vlib_buffer_t * b0,
1083 ip4_header_t * ip0, udp_header_t * udp0,
1084 tcp_header_t * tcp0, u32 proto0, int is_ed,
1087 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1088 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1089 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1090 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1091 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1094 * @brief Add external address to NAT44 pool
1096 * @param sm snat global configuration data
1097 * @param addr IPv4 address
1098 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1099 * @param twice_nat 1 if twice NAT address
1101 * @return 0 on success, non-zero value otherwise
1103 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1107 * @brief Delete external address from NAT44 pool
1109 * @param sm snat global configuration data
1110 * @param addr IPv4 address
1111 * @param delete_sm 1 if delete static mapping using address
1112 * @param twice_nat 1 if twice NAT address
1114 * @return 0 on success, non-zero value otherwise
1116 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1120 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1122 * @param addr IPv4 address
1123 * @param is_add 1 = add, 0 = delete
1125 * @return 0 on success, non-zero value otherwise
1127 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1130 * @brief Add/delete NAT44 static mapping
1132 * @param l_addr local IPv4 address
1133 * @param e_addr external IPv4 address
1134 * @param l_port local port number
1135 * @param e_port external port number
1136 * @param vrf_id local VRF ID
1137 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1138 * @param sw_if_index use interface address as external IPv4 address
1139 * @param proto L4 protocol
1140 * @param is_add 1 = add, 0 = delete
1141 * @param twice_nat twice-nat mode
1142 * @param out2in_only if 1 rule match only out2in direction
1143 * @param tag opaque string tag
1144 * @param identity_nat identity NAT
1146 * @return 0 on success, non-zero value otherwise
1148 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1149 u16 l_port, u16 e_port, u32 vrf_id,
1150 int addr_only, u32 sw_if_index,
1151 nat_protocol_t proto, int is_add,
1152 twice_nat_type_t twice_nat, u8 out2in_only,
1153 u8 * tag, u8 identity_nat);
1156 * @brief Add/delete static mapping with load-balancing (multiple backends)
1158 * @param e_addr external IPv4 address
1159 * @param e_port external port number
1160 * @param proto L4 protocol
1161 * @param locals list of local backends
1162 * @param is_add 1 = add, 0 = delete
1163 * @param twice_nat twice-nat mode
1164 * @param out2in_only if 1 rule match only out2in direction
1165 * @param tag opaque string tag
1166 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1168 * @return 0 on success, non-zero value otherwise
1170 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1171 nat_protocol_t proto,
1172 nat44_lb_addr_port_t * locals, u8 is_add,
1173 twice_nat_type_t twice_nat,
1174 u8 out2in_only, u8 * tag, u32 affinity);
1176 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1177 ip4_address_t l_addr, u16 l_port,
1178 nat_protocol_t proto, u32 vrf_id,
1179 u8 probability, u8 is_add);
1181 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1184 * @brief Set NAT plugin workers
1186 * @param bitmap NAT workers bitmap
1188 * @return 0 on success, non-zero value otherwise
1190 int snat_set_workers (uword * bitmap);
1193 * @brief Enable/disable NAT44 feature on the interface
1195 * @param sw_if_index software index of the interface
1196 * @param is_inside 1 = inside, 0 = outside
1197 * @param is_del 1 = delete, 0 = add
1199 * @return 0 on success, non-zero value otherwise
1201 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1204 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1206 * @param sw_if_index software index of the interface
1207 * @param is_inside 1 = inside, 0 = outside
1208 * @param is_del 1 = delete, 0 = add
1210 * @return 0 on success, non-zero value otherwise
1212 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1216 * @brief Add/delete NAT44 pool address from specific interface
1218 * @param sw_if_index software index of the interface
1219 * @param is_del 1 = delete, 0 = add
1220 * @param twice_nat 1 = twice NAT address for external hosts
1222 * @return 0 on success, non-zero value otherwise
1224 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1228 * @brief Delete NAT44 session
1230 * @param addr IPv4 address
1231 * @param port L4 port number
1232 * @param proto L4 protocol
1233 * @param vrf_id VRF ID
1234 * @param is_in 1 = inside network address and port pair, 0 = outside
1236 * @return 0 on success, non-zero value otherwise
1238 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1239 nat_protocol_t proto, u32 vrf_id, int is_in);
1242 * @brief Delete NAT44 endpoint-dependent session
1244 * @param sm snat global configuration data
1245 * @param addr IPv4 address
1246 * @param port L4 port number
1247 * @param proto L4 protocol
1248 * @param vrf_id VRF ID
1249 * @param is_in 1 = inside network address and port pair, 0 = outside
1251 * @return 0 on success, non-zero value otherwise
1253 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1254 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1255 u32 vrf_id, int is_in);
1258 * @brief Free NAT44 session data (lookup keys, external address port)
1260 * @param sm snat global configuration data
1261 * @param s NAT session
1262 * @param thread_index thread index
1263 * @param is_ha is HA event
1265 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1266 u32 thread_index, u8 is_ha);
1269 * @brief Set NAT44 session limit (session limit, vrf id)
1271 * @param session_limit Session limit
1272 * @param vrf_id VRF id
1273 * @return 0 on success, non-zero value otherwise
1275 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1278 * @brief Free NAT44 ED session data (lookup keys, external address port)
1280 * @param s NAT session
1281 * @param thread_index thread index
1282 * @param is_ha is HA event
1285 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1286 u32 thread_index, u8 is_ha);
1289 * @brief Initialize NAT44 data
1291 * @param tsm per thread data
1293 void nat44_db_init (snat_main_per_thread_data_t * tsm);
1296 * @brief Free NAT44 data
1298 * @param tsm per thread data
1300 void nat44_db_free (snat_main_per_thread_data_t * tsm);
1302 void nat44_sessions_clear ();
1305 * @brief Find or create NAT user
1307 * @param sm snat global configuration data
1308 * @param addr IPv4 address
1309 * @param fib_index FIB table index
1310 * @param thread_index thread index
1312 * @return NAT user data structure on success otherwise zero value
1314 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1315 ip4_address_t * addr, u32 fib_index,
1319 * @brief Allocate new NAT session or recycle last used
1321 * @param sm snat global configuration data
1323 * @param thread_index thread index
1324 * @param now time now
1326 * @return session data structure on success otherwise zero value
1328 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1330 u32 thread_index, f64 now);
1333 * @brief Set address and port assignment algorithm for MAP-E CE
1335 * @param psid Port Set Identifier value
1336 * @param psid_offset number of offset bits
1337 * @param psid_length length of PSID
1339 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1343 * @brief Set address and port assignment algorithm for port range
1345 * @param start_port beginning of the port range
1346 * @param end_port end of the port range
1348 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1351 * @brief Set address and port assignment algorithm to default/standard
1353 void nat_set_alloc_addr_and_port_default (void);
1356 * @brief Free outside address and port pair
1358 * @param addresses vector of outside addresses
1359 * @param thread_index thread index
1360 * @param key address, port and protocol
1363 snat_free_outside_address_and_port (snat_address_t * addresses,
1365 ip4_address_t * addr,
1366 u16 port, nat_protocol_t protocol);
1369 * @brief Alloc outside address and port
1371 * @param addresses vector of outside addresses
1372 * @param fib_index FIB table index
1373 * @param thread_index thread index
1374 * @param port_per_thread number of ports per thread
1375 * @param snat_thread_index NAT thread index
1377 * @return 0 on success, non-zero value otherwise
1379 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1382 nat_protocol_t proto,
1383 ip4_address_t * addr,
1385 u16 port_per_thread,
1386 u32 snat_thread_index);
1389 * @brief Match NAT44 static mapping.
1391 * @param key address and port to match
1392 * @param addr external/local address of the matched mapping
1393 * @param port port of the matched mapping
1394 * @param fib_index fib index of the matched mapping
1395 * @param by_external if 0 match by local address otherwise match by external
1397 * @param is_addr_only 1 if matched mapping is address only
1398 * @param twice_nat matched mapping is twice NAT type
1399 * @param lb 1 if matched mapping is load-balanced
1400 * @param ext_host_addr external host address
1402 * @returns 0 if match found otherwise 1.
1404 int snat_static_mapping_match (snat_main_t * sm,
1405 ip4_address_t match_addr,
1407 u32 match_fib_index,
1408 nat_protocol_t match_protocol,
1409 ip4_address_t * mapping_addr,
1411 u32 * mapping_fib_index,
1414 twice_nat_type_t * twice_nat,
1416 ip4_address_t * ext_host_addr,
1417 u8 * is_identity_nat);
1420 * @brief Add/del NAT address to FIB.
1422 * Add the external NAT address to the FIB as receive entries. This ensures
1423 * that VPP will reply to ARP for this address and we don't need to enable
1424 * proxy ARP on the outside interface.
1426 * @param addr IPv4 address
1427 * @param plen address prefix length
1428 * @param sw_if_index software index of the outside interface
1429 * @param is_add 0 = delete, 1 = add.
1431 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1432 u8 p_len, u32 sw_if_index, int is_add);
1435 * Why is this here? Because we don't need to touch this layer to
1436 * simply reply to an icmp. We need to change id to a unique
1437 * value to NAT an echo request/reply.
1444 } icmp_echo_header_t;
1448 u16 src_port, dst_port;
1451 u8 *format_user_kvp (u8 * s, va_list * args);
1452 #endif /* __included_nat_h__ */
1454 * fd.io coding-style-patch-verification: ON
1457 * eval: (c-set-style "gnu")