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>
34 #include <vppinfra/bihash_16_8.h>
36 /* default session timeouts */
37 #define SNAT_UDP_TIMEOUT 300
38 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
39 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
40 #define SNAT_ICMP_TIMEOUT 60
42 /* number of worker handoff frame queue elements */
43 #define NAT_FQ_NELTS 64
45 /* NAT buffer flags */
46 #define SNAT_FLAG_HAIRPINNING (1 << 0)
54 NAT_NEXT_IN2OUT_ED_FAST_PATH,
55 NAT_NEXT_IN2OUT_ED_SLOW_PATH,
56 NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
57 NAT_NEXT_OUT2IN_ED_FAST_PATH,
58 NAT_NEXT_OUT2IN_ED_SLOW_PATH,
67 /* session key (4-tuple) */
76 u16 protocol:3, fib_index:13;
82 /* deterministic session outside key */
89 ip4_address_t ext_host_addr;
97 /* user (internal host) key */
111 /* NAT API Configuration flags */
112 #define foreach_nat_config_flag \
113 _(0x01, IS_TWICE_NAT) \
114 _(0x02, IS_SELF_TWICE_NAT) \
115 _(0x04, IS_OUT2IN_ONLY) \
116 _(0x08, IS_ADDR_ONLY) \
117 _(0x10, IS_OUTSIDE) \
120 _(0x80, IS_EXT_HOST_VALID) \
122 typedef enum nat_config_flags_t_
124 #define _(n,f) NAT_API_##f = n,
125 foreach_nat_config_flag
127 } nat_config_flags_t;
129 /* External address and port allocation modes */
130 #define foreach_nat_addr_and_port_alloc_alg \
131 _(0, DEFAULT, "default") \
132 _(1, MAPE, "map-e") \
133 _(2, RANGE, "port-range")
137 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
138 foreach_nat_addr_and_port_alloc_alg
140 } nat_addr_and_port_alloc_alg_t;
143 /* Supported L4 protocols */
144 #define foreach_snat_protocol \
145 _(UDP, 0, udp, "udp") \
146 _(TCP, 1, tcp, "tcp") \
147 _(ICMP, 2, icmp, "icmp")
151 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
152 foreach_snat_protocol
158 #define foreach_snat_session_state \
159 _(0, UNKNOWN, "unknown") \
160 _(1, UDP_ACTIVE, "udp-active") \
161 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
162 _(3, TCP_ESTABLISHED, "tcp-established") \
163 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
164 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
165 _(6, TCP_CLOSING, "tcp-closing") \
166 _(7, TCP_LAST_ACK, "tcp-last-ack") \
167 _(8, TCP_CLOSED, "tcp-closed") \
168 _(9, ICMP_ACTIVE, "icmp-active")
172 #define _(v, N, s) SNAT_SESSION_##N = v,
173 foreach_snat_session_state
175 } snat_session_state_t;
177 #define foreach_nat_in2out_ed_error \
178 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
179 _(IN2OUT_PACKETS, "good in2out packets processed") \
180 _(OUT_OF_PORTS, "out of ports") \
181 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
182 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
183 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
184 _(DROP_FRAGMENT, "drop fragment") \
185 _(CANNOT_CREATE_USER, "cannot create NAT user") \
186 _(NON_SYN, "non-SYN packet try to create session") \
187 _(TCP_PACKETS, "TCP packets") \
188 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
189 _(UDP_PACKETS, "UDP packets") \
190 _(ICMP_PACKETS, "ICMP packets") \
191 _(OTHER_PACKETS, "other protocol packets") \
192 _(FRAGMENTS, "fragments") \
193 _(CACHED_FRAGMENTS, "cached fragments") \
194 _(PROCESSED_FRAGMENTS, "processed fragments")
198 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
199 foreach_nat_in2out_ed_error
201 NAT_IN2OUT_ED_N_ERROR,
202 } nat_in2out_ed_error_t;
204 #define foreach_nat_out2in_ed_error \
205 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
206 _(OUT2IN_PACKETS, "good out2in packets processed") \
207 _(OUT_OF_PORTS, "out of ports") \
208 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
209 _(NO_TRANSLATION, "no translation") \
210 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
211 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
212 _(DROP_FRAGMENT, "drop fragment") \
213 _(CANNOT_CREATE_USER, "cannot create NAT user") \
214 _(NON_SYN, "non-SYN packet try to create session") \
215 _(TCP_PACKETS, "TCP packets") \
216 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
217 _(UDP_PACKETS, "UDP packets") \
218 _(ICMP_PACKETS, "ICMP packets") \
219 _(OTHER_PACKETS, "other protocol packets") \
220 _(FRAGMENTS, "fragments") \
221 _(CACHED_FRAGMENTS, "cached fragments") \
222 _(PROCESSED_FRAGMENTS, "processed fragments")
226 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
227 foreach_nat_out2in_ed_error
229 NAT_OUT2IN_ED_N_ERROR,
230 } nat_out2in_ed_error_t;
233 /* Endpoint dependent TCP session state */
234 #define NAT44_SES_I2O_FIN 1
235 #define NAT44_SES_O2I_FIN 2
236 #define NAT44_SES_I2O_FIN_ACK 4
237 #define NAT44_SES_O2I_FIN_ACK 8
238 #define NAT44_SES_I2O_SYN 16
239 #define NAT44_SES_O2I_SYN 32
240 #define NAT44_SES_RST 64
243 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
244 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
245 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
246 #define SNAT_SESSION_FLAG_TWICE_NAT 8
247 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
248 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
249 #define SNAT_SESSION_FLAG_AFFINITY 64
250 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
252 /* NAT interface flags */
253 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
254 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
256 /* Static mapping flags */
257 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
258 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
259 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
260 #define NAT_STATIC_MAPPING_FLAG_LB 8
263 typedef CLIB_PACKED(struct
265 /* Outside network key */
266 snat_session_key_t out2in;
268 /* Inside network key */
269 snat_session_key_t in2out;
274 /* Per-user translations */
276 u32 per_user_list_head_index;
278 /* index in global LRU list */
279 u32 global_lru_index;
282 /* Last heard timer */
285 /* Last HA refresh */
286 f64 ha_last_refreshed;
292 /* External host address and port */
293 ip4_address_t ext_host_addr;
296 /* External host address and port after translation */
297 ip4_address_t ext_host_nat_addr;
298 u16 ext_host_nat_port;
300 /* TCP session state */
304 u32 tcp_close_timestamp;
316 u32 sessions_per_user_list_head_index;
326 #define _(N, i, n, s) \
327 u16 busy_##n##_ports; \
328 u16 * busy_##n##_ports_per_thread; \
329 u32 busy_##n##_port_refcounts[65535];
330 foreach_snat_protocol
343 /* Inside network port */
345 /* Outside network address and port */
346 snat_det_out_key_t out;
351 } snat_det_session_t;
355 /* inside IP address range */
356 ip4_address_t in_addr;
358 /* outside IP address range */
359 ip4_address_t out_addr;
361 /* inside IP addresses / outside IP addresses */
363 /* number of ports available to internal host */
365 /* session counter */
367 /* vector of sessions */
368 snat_det_session_t *sessions;
373 /* backend IP address */
375 /* backend port number */
377 /* probability of the backend to be randomly matched */
380 /* backend FIB table */
383 } nat44_lb_addr_port_t;
387 /* twice-nat disabled */
389 /* twice-nat enabled */
391 /* twice-nat only when src IP equals dst IP after translation */
397 /* no load-balancing */
401 /* load-balancing with affinity */
407 /* local IP address */
408 ip4_address_t local_addr;
409 /* external IP address */
410 ip4_address_t external_addr;
416 twice_nat_type_t twice_nat;
417 /* local FIB table */
421 snat_protocol_t proto;
422 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
424 /* worker threads used by backends/local host */
426 /* opaque string tag */
428 /* backends for load-balancing mode */
429 nat44_lb_addr_port_t *locals;
430 /* affinity per service lis */
431 u32 affinity_per_service_list_head_index;
434 } snat_static_mapping_t;
444 ip4_address_t l_addr;
449 snat_protocol_t proto;
457 } snat_static_map_resolve_t;
461 /* Main lookup tables */
462 clib_bihash_8_8_t out2in;
463 clib_bihash_8_8_t in2out;
465 /* Endpoint dependent sessions lookup tables */
466 clib_bihash_16_8_t out2in_ed;
467 clib_bihash_16_8_t in2out_ed;
469 /* Find-a-user => src address lookup */
470 clib_bihash_8_8_t user_hash;
476 snat_session_t *sessions;
478 /* Pool of doubly-linked list elements */
479 dlist_elt_t *list_pool;
481 /* LRU session list - head is stale, tail is fresh */
482 dlist_elt_t *global_lru_pool;
483 u32 global_lru_head_index;
485 /* NAT thread index */
486 u32 snat_thread_index;
488 /* real thread index */
491 } snat_main_per_thread_data_t;
495 /* ICMP session match function */
496 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
497 vlib_node_runtime_t * node,
500 ip4_header_t * ip0, u8 * p_proto,
501 snat_session_key_t * p_value,
502 u8 * p_dont_translate, void *d,
505 /* Return worker thread index for given packet */
506 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
510 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
515 /* NAT address and port allacotaion function */
516 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
520 snat_session_key_t * k,
522 u32 snat_thread_index);
525 typedef struct snat_main_s
527 /* ICMP session match functions */
528 snat_icmp_match_function_t *icmp_match_in2out_cb;
529 snat_icmp_match_function_t *icmp_match_out2in_cb;
531 /* Thread settings */
533 u32 first_worker_index;
535 snat_get_worker_in2out_function_t *worker_in2out_cb;
536 snat_get_worker_out2in_function_t *worker_out2in_cb;
540 /* Per thread data */
541 snat_main_per_thread_data_t *per_thread_data;
543 /* Find a static mapping by local */
544 clib_bihash_8_8_t static_mapping_by_local;
546 /* Find a static mapping by external */
547 clib_bihash_8_8_t static_mapping_by_external;
549 /* Static mapping pool */
550 snat_static_mapping_t *static_mappings;
553 snat_interface_t *interfaces;
554 snat_interface_t *output_feature_interfaces;
556 /* Vector of outside addresses */
557 snat_address_t *addresses;
558 /* Address and port allocation function */
559 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
560 /* Address and port allocation type */
561 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
562 /* Port set parameters (MAP-E) */
566 /* Port range parameters */
570 /* vector of outside fibs */
571 nat_outside_fib_t *outside_fibs;
573 /* Vector of twice NAT addresses for extenal hosts */
574 snat_address_t *twice_nat_addresses;
576 /* sw_if_indices whose intfc addresses should be auto-added */
577 u32 *auto_add_sw_if_indices;
578 u32 *auto_add_sw_if_indices_twice_nat;
580 /* vector of interface address static mappings to resolve. */
581 snat_static_map_resolve_t *to_resolve;
583 /* Randomize port allocation order */
586 /* Worker handoff frame-queue index */
588 u32 fq_in2out_output_index;
592 u32 error_node_index;
594 /* handoff fq nodes */
595 u32 handoff_out2in_index;
596 u32 handoff_in2out_index;
597 u32 handoff_in2out_output_index;
599 /* respect feature arc nodes */
600 u32 pre_out2in_node_index;
601 u32 pre_in2out_node_index;
603 u32 in2out_node_index;
604 u32 in2out_output_node_index;
605 u32 in2out_fast_node_index;
606 u32 in2out_slowpath_node_index;
607 u32 in2out_slowpath_output_node_index;
608 u32 ed_in2out_node_index;
609 u32 ed_in2out_slowpath_node_index;
610 u32 out2in_node_index;
611 u32 out2in_fast_node_index;
612 u32 ed_out2in_node_index;
613 u32 ed_out2in_slowpath_node_index;
614 u32 det_in2out_node_index;
615 u32 det_out2in_node_index;
617 u32 hairpinning_node_index;
618 u32 hairpin_dst_node_index;
619 u32 hairpin_src_node_index;
620 u32 ed_hairpinning_node_index;
621 u32 ed_hairpin_dst_node_index;
622 u32 ed_hairpin_src_node_index;
625 /* Deterministic NAT mappings */
626 snat_det_map_t *det_maps;
628 /* If forwarding is enabled */
629 u8 forwarding_enabled;
631 /* Config parameters */
632 u8 static_mapping_only;
633 u8 static_mapping_connection_tracking;
636 u8 endpoint_dependent;
638 u32 translation_buckets;
639 uword translation_memory_size;
640 u32 max_translations;
641 u32 *max_translations_per_fib;
644 uword user_memory_size;
645 u32 max_translations_per_user;
648 u32 outside_fib_index;
650 u32 inside_fib_index;
652 /* values of various timeouts */
655 u32 tcp_transitory_timeout;
656 u32 tcp_established_timeout;
659 /* TCP MSS clamping */
663 /* counters/gauges */
664 vlib_simple_counter_main_t total_users;
665 vlib_simple_counter_main_t total_sessions;
667 /* API message ID base */
671 vlib_log_class_t log_class;
676 vlib_main_t *vlib_main;
677 vnet_main_t *vnet_main;
678 ip4_main_t *ip4_main;
679 ip_lookup_main_t *ip4_lookup_main;
680 api_main_t *api_main;
687 } nat44_is_idle_session_ctx_t;
691 u32 cached_sw_if_index;
692 u32 cached_ip4_address;
695 extern snat_main_t snat_main;
697 // nat pre ed next_node feature classification
698 extern vlib_node_registration_t nat_default_node;
699 extern vlib_node_registration_t nat_pre_in2out_node;
700 extern vlib_node_registration_t nat_pre_out2in_node;
702 extern vlib_node_registration_t snat_in2out_node;
703 extern vlib_node_registration_t snat_in2out_output_node;
704 extern vlib_node_registration_t snat_out2in_node;
705 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
706 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
707 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
708 extern vlib_node_registration_t snat_det_in2out_node;
709 extern vlib_node_registration_t snat_det_out2in_node;
710 extern vlib_node_registration_t nat44_ed_in2out_node;
711 extern vlib_node_registration_t nat44_ed_in2out_output_node;
712 extern vlib_node_registration_t nat44_ed_out2in_node;
714 extern fib_source_t nat_fib_src_hi;
715 extern fib_source_t nat_fib_src_low;
717 /* format functions */
718 format_function_t format_snat_user;
719 format_function_t format_snat_static_mapping;
720 format_function_t format_snat_static_map_to_resolve;
721 format_function_t format_snat_session;
722 format_function_t format_det_map_ses;
723 format_function_t format_snat_key;
724 format_function_t format_static_mapping_key;
725 format_function_t format_snat_protocol;
726 format_function_t format_nat_addr_and_port_alloc_alg;
727 /* unformat functions */
728 unformat_function_t unformat_snat_protocol;
730 /** \brief Check if SNAT session is created from static mapping.
731 @param s SNAT session
732 @return 1 if SNAT session is created from static mapping otherwise 0
734 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
736 /** \brief Check if SNAT session for unknown protocol.
737 @param s SNAT session
738 @return 1 if SNAT session for unknown protocol otherwise 0
740 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
742 /** \brief Check if NAT session is twice NAT.
744 @return 1 if NAT session is twice NAT
746 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
748 /** \brief Check if NAT session is load-balancing.
750 @return 1 if NAT session is load-balancing
752 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
754 /** \brief Check if NAT session is forwarding bypass.
756 @return 1 if NAT session is load-balancing
758 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
760 /** \brief Check if NAT session is endpoint dependent.
762 @return 1 if NAT session is endpoint dependent
764 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
766 /** \brief Check if NAT session has affinity record.
768 @return 1 if NAT session has affinity record
770 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
772 /** \brief Check if NAT interface is inside.
773 @param i NAT interfce
774 @return 1 if inside interface
776 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
778 /** \brief Check if NAT interface is outside.
779 @param i NAT interfce
780 @return 1 if outside interface
782 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
784 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
786 @return 1 if session is closed
788 #define nat44_is_ses_closed(s) s->state == 0xf
790 /** \brief Check if NAT static mapping is address only (1:1NAT).
791 @param sm NAT static mapping
792 @return 1 if 1:1NAT, 0 if 1:1NAPT
794 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
796 /** \brief Check if NAT static mapping match only out2in direction.
797 @param sm NAT static mapping
798 @return 1 if rule match only out2in direction
800 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
802 /** \brief Check if NAT static mapping is identity NAT.
803 @param sm NAT static mapping
804 @return 1 if identity NAT
806 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
808 /** \brief Check if NAT static mapping is load-balancing.
809 @param sm NAT static mapping
810 @return 1 if load-balancing
812 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
814 /** \brief Check if client initiating TCP connection (received SYN from client)
816 @return 1 if client initiating TCP connection
819 tcp_flags_is_init (u8 f)
821 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
825 #define nat_log_err(...) \
826 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
827 #define nat_log_warn(...) \
828 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
829 #define nat_log_notice(...) \
830 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
831 #define nat_log_info(...) \
832 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
833 #define nat_log_debug(...)\
834 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
836 /* NAT API Logging Levels */
837 #define foreach_nat_log_level \
840 _(0x02, LOG_WARNING) \
841 _(0x03, LOG_NOTICE) \
845 typedef enum nat_log_level_t_
847 #define _(n,f) SNAT_##f = n,
848 foreach_nat_log_level
852 #define nat_elog(_level, _str) \
855 snat_main_t *sm = &snat_main; \
856 if (PREDICT_FALSE (sm->log_level >= _level)) \
858 ELOG_TYPE_DECLARE (e) = \
860 .format = "nat-msg " _str, \
863 ELOG_DATA (&sm->vlib_main->elog_main, e); \
867 #define nat_elog_addr(_level, _str, _addr) \
870 if (PREDICT_FALSE (sm->log_level >= _level)) \
872 ELOG_TYPE_DECLARE (e) = \
874 .format = "nat-msg " _str " %d.%d.%d.%d", \
875 .format_args = "i1i1i1i1", \
884 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
885 ed->oct4 = _addr >> 24; \
886 ed->oct3 = _addr >> 16; \
887 ed->oct2 = _addr >> 8; \
892 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
895 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
897 ELOG_TYPE_DECLARE (e) = \
899 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
900 " tid from: %d to: %d fib: %d", \
901 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
917 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
918 ed->src_oct1 = _src >> 24; \
919 ed->src_oct2 = _src >> 16; \
920 ed->src_oct3 = _src >> 8; \
921 ed->src_oct4 = _src; \
922 ed->dst_oct1 = _dst >> 24; \
923 ed->dst_oct2 = _dst >> 16; \
924 ed->dst_oct3 = _dst >> 8; \
925 ed->dst_oct4 = _dst; \
926 ed->ftid = vlib_get_thread_index (); \
932 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
935 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
937 ELOG_TYPE_DECLARE (e) = \
939 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
940 " tid:%d prt:%d fib:%d", \
941 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
957 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
958 ed->src_oct1 = _src >> 24; \
959 ed->src_oct2 = _src >> 16; \
960 ed->src_oct3 = _src >> 8; \
961 ed->src_oct4 = _src; \
962 ed->dst_oct1 = _dst >> 24; \
963 ed->dst_oct2 = _dst >> 16; \
964 ed->dst_oct3 = _dst >> 8; \
965 ed->dst_oct4 = _dst; \
966 ed->tid = vlib_get_thread_index (); \
972 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
975 snat_main_t *sm = &snat_main; \
976 if (PREDICT_FALSE (sm->log_level >= _level)) \
978 ELOG_TYPE_DECLARE (e) = \
980 .format = "nat-msg " _fmt, \
981 .format_args = _arg, \
985 typeof (_val1) val1; \
987 ed = ELOG_DATA (&sm->vlib_main->elog_main, e); \
992 #define nat_elog_notice(nat_elog_str) \
993 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
994 #define nat_elog_warn(nat_elog_str) \
995 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
996 #define nat_elog_err(nat_elog_str) \
997 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
998 #define nat_elog_debug(nat_elog_str) \
999 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1000 #define nat_elog_info(nat_elog_str) \
1001 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1003 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1004 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1005 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1006 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1007 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1008 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1009 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1010 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1011 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1012 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1014 /* ICMP session match functions */
1015 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1016 u32 thread_index, vlib_buffer_t * b0,
1017 ip4_header_t * ip0, u8 * p_proto,
1018 snat_session_key_t * p_value,
1019 u8 * p_dont_translate, void *d, void *e);
1020 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1021 u32 thread_index, vlib_buffer_t * b0,
1022 ip4_header_t * ip0, u8 * p_proto,
1023 snat_session_key_t * p_value,
1024 u8 * p_dont_translate, void *d, void *e);
1025 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1026 u32 thread_index, vlib_buffer_t * b0,
1027 ip4_header_t * ip0, u8 * p_proto,
1028 snat_session_key_t * p_value,
1029 u8 * p_dont_translate, void *d, void *e);
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, u8 * p_proto,
1033 snat_session_key_t * p_value,
1034 u8 * p_dont_translate, void *d, void *e);
1036 /* ICMP deterministic NAT session match functions */
1037 u32 icmp_match_out2in_det (snat_main_t * sm, vlib_node_runtime_t * node,
1038 u32 thread_index, vlib_buffer_t * b0,
1039 ip4_header_t * ip0, u8 * p_proto,
1040 snat_session_key_t * p_value,
1041 u8 * p_dont_translate, void *d, void *e);
1042 u32 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
1043 u32 thread_index, vlib_buffer_t * b0,
1044 ip4_header_t * ip0, u8 * p_proto,
1045 snat_session_key_t * p_value,
1046 u8 * p_dont_translate, void *d, void *e);
1048 /* ICMP endpoint-dependent session match functions */
1049 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1050 u32 thread_index, vlib_buffer_t * b0,
1051 ip4_header_t * ip0, u8 * p_proto,
1052 snat_session_key_t * p_value,
1053 u8 * p_dont_translate, void *d, void *e);
1054 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1055 u32 thread_index, vlib_buffer_t * b0,
1056 ip4_header_t * ip0, u8 * p_proto,
1057 snat_session_key_t * p_value,
1058 u8 * p_dont_translate, void *d, void *e);
1060 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1061 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1062 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1065 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1066 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1067 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1070 /* hairpinning functions */
1071 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1072 ip4_header_t * ip0, icmp46_header_t * icmp0,
1074 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1076 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1078 int snat_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1079 ip4_header_t * ip0, udp_header_t * udp0,
1080 tcp_header_t * tcp0, u32 proto0, int is_ed);
1082 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1083 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1084 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1085 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1086 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1089 * @brief Add external address to NAT44 pool
1091 * @param addr IPv4 address
1092 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1093 * @param twice_nat 1 if twice NAT address
1095 * @return 0 on success, non-zero value otherwise
1097 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1101 * @brief Delete external address from NAT44 pool
1103 * @param addr IPv4 address
1104 * @param delete_sm 1 if delete static mapping using address
1105 * @param twice_nat 1 if twice NAT address
1107 * @return 0 on success, non-zero value otherwise
1109 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1113 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1115 * @param addr IPv4 address
1116 * @param is_add 1 = add, 0 = delete
1118 * @return 0 on success, non-zero value otherwise
1120 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1123 * @brief Add/delete NAT44 static mapping
1125 * @param l_addr local IPv4 address
1126 * @param e_addr external IPv4 address
1127 * @param l_port local port number
1128 * @param e_port external port number
1129 * @param vrf_id local VRF ID
1130 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1131 * @param sw_if_index use interface address as external IPv4 address
1132 * @param proto L4 protocol
1133 * @param is_add 1 = add, 0 = delete
1134 * @param twice_nat twice-nat mode
1135 * @param out2in_only if 1 rule match only out2in direction
1136 * @param tag opaque string tag
1137 * @param identity_nat identity NAT
1139 * @return 0 on success, non-zero value otherwise
1141 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1142 u16 l_port, u16 e_port, u32 vrf_id,
1143 int addr_only, u32 sw_if_index,
1144 snat_protocol_t proto, int is_add,
1145 twice_nat_type_t twice_nat, u8 out2in_only,
1146 u8 * tag, u8 identity_nat);
1149 * @brief Add/delete static mapping with load-balancing (multiple backends)
1151 * @param e_addr external IPv4 address
1152 * @param e_port external port number
1153 * @param proto L4 protocol
1154 * @param locals list of local backends
1155 * @param is_add 1 = add, 0 = delete
1156 * @param twice_nat twice-nat mode
1157 * @param out2in_only if 1 rule match only out2in direction
1158 * @param tag opaque string tag
1159 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1161 * @return 0 on success, non-zero value otherwise
1163 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1164 snat_protocol_t proto,
1165 nat44_lb_addr_port_t * locals, u8 is_add,
1166 twice_nat_type_t twice_nat,
1167 u8 out2in_only, u8 * tag, u32 affinity);
1169 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1170 ip4_address_t l_addr, u16 l_port,
1171 snat_protocol_t proto, u32 vrf_id,
1172 u8 probability, u8 is_add);
1174 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1177 * @brief Set NAT plugin workers
1179 * @param bitmap NAT workers bitmap
1181 * @return 0 on success, non-zero value otherwise
1183 int snat_set_workers (uword * bitmap);
1186 * @brief Enable/disable NAT44 feature on the interface
1188 * @param sw_if_index software index of the interface
1189 * @param is_inside 1 = inside, 0 = outside
1190 * @param is_del 1 = delete, 0 = add
1192 * @return 0 on success, non-zero value otherwise
1194 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1197 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1199 * @param sw_if_index software index of the interface
1200 * @param is_inside 1 = inside, 0 = outside
1201 * @param is_del 1 = delete, 0 = add
1203 * @return 0 on success, non-zero value otherwise
1205 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1209 * @brief Add/delete NAT44 pool address from specific interfce
1211 * @param sw_if_index software index of the interface
1212 * @param is_del 1 = delete, 0 = add
1213 * @param twice_nat 1 = twice NAT address for extenal hosts
1215 * @return 0 on success, non-zero value otherwise
1217 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1221 * @brief Delete NAT44 session
1223 * @param addr IPv4 address
1224 * @param port L4 port number
1225 * @param proto L4 protocol
1226 * @param vrf_id VRF ID
1227 * @param is_in 1 = inside network address and port pair, 0 = outside
1229 * @return 0 on success, non-zero value otherwise
1231 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1232 snat_protocol_t proto, u32 vrf_id, int is_in);
1235 * @brief Delete NAT44 endpoint-dependent session
1237 * @param addr IPv4 address
1238 * @param port L4 port number
1239 * @param proto L4 protocol
1240 * @param vrf_id VRF ID
1241 * @param is_in 1 = inside network address and port pair, 0 = outside
1243 * @return 0 on success, non-zero value otherwise
1245 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1246 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1247 u32 vrf_id, int is_in);
1250 * @brief Free NAT44 session data (lookup keys, external addrres port)
1252 * @param s NAT session
1253 * @param thread_index thread index
1254 * @param is_ha is HA event
1256 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1257 u32 thread_index, u8 is_ha);
1260 * @brief Set NAT44 session limit (session limit, vrf id)
1262 * @param session_limit Session limit
1263 * @param vrf_id VRF id
1264 * @return 0 on success, non-zero value otherwise
1266 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1269 * @brief Free NAT44 ED session data (lookup keys, external addrres port)
1271 * @param s NAT session
1272 * @param thread_index thread index
1273 * @param is_ha is HA event
1276 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1277 u32 thread_index, u8 is_ha);
1280 * @brief Initialize NAT44 data
1282 * @param tsm per thread data
1284 void nat44_db_init (snat_main_per_thread_data_t * tsm);
1287 * @brief Free NAT44 data
1289 * @param tsm per thread data
1291 void nat44_db_free (snat_main_per_thread_data_t * tsm);
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,
1303 ip4_address_t * addr, u32 fib_index,
1307 * @brief Allocate new NAT session or recycle last used
1310 * @param thread_index thread index
1312 * @return session data structure on success otherwise zero value
1314 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1316 u32 thread_index, f64 now);
1319 * @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, u32 thread_index,
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 int nat_global_lru_free_one (snat_main_t * sm, int thread_index, f64 now);
1439 #endif /* __included_nat_h__ */
1441 * fd.io coding-style-patch-verification: ON
1444 * eval: (c-set-style "gnu")