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,
68 STATIC_ASSERT (sizeof (nat_buffer_opaque_t) <=
69 STRUCT_SIZE_OF (vnet_buffer_opaque_t, unused),
70 "Custom meta-data too large for vnet_buffer_opaque_t");
72 #define nat_buffer_opaque(b) \
73 ((nat_buffer_opaque_t *)((u8 *)((b)->opaque) + \
74 STRUCT_OFFSET_OF (vnet_buffer_opaque_t, unused)))*/
76 /* session key (4-tuple) */
85 u16 protocol:3, fib_index:13;
91 /* deterministic session outside key */
98 ip4_address_t ext_host_addr;
104 } snat_det_out_key_t;
106 /* user (internal host) key */
120 /* NAT API Configuration flags */
121 #define foreach_nat_config_flag \
122 _(0x01, IS_TWICE_NAT) \
123 _(0x02, IS_SELF_TWICE_NAT) \
124 _(0x04, IS_OUT2IN_ONLY) \
125 _(0x08, IS_ADDR_ONLY) \
126 _(0x10, IS_OUTSIDE) \
129 _(0x80, IS_EXT_HOST_VALID) \
131 typedef enum nat_config_flags_t_
133 #define _(n,f) NAT_API_##f = n,
134 foreach_nat_config_flag
136 } nat_config_flags_t;
138 /* External address and port allocation modes */
139 #define foreach_nat_addr_and_port_alloc_alg \
140 _(0, DEFAULT, "default") \
141 _(1, MAPE, "map-e") \
142 _(2, RANGE, "port-range")
146 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
147 foreach_nat_addr_and_port_alloc_alg
149 } nat_addr_and_port_alloc_alg_t;
152 /* Supported L4 protocols */
153 #define foreach_snat_protocol \
154 _(UDP, 0, udp, "udp") \
155 _(TCP, 1, tcp, "tcp") \
156 _(ICMP, 2, icmp, "icmp")
160 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
161 foreach_snat_protocol
167 #define foreach_snat_session_state \
168 _(0, UNKNOWN, "unknown") \
169 _(1, UDP_ACTIVE, "udp-active") \
170 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
171 _(3, TCP_ESTABLISHED, "tcp-established") \
172 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
173 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
174 _(6, TCP_CLOSING, "tcp-closing") \
175 _(7, TCP_LAST_ACK, "tcp-last-ack") \
176 _(8, TCP_CLOSED, "tcp-closed") \
177 _(9, ICMP_ACTIVE, "icmp-active")
181 #define _(v, N, s) SNAT_SESSION_##N = v,
182 foreach_snat_session_state
184 } snat_session_state_t;
186 #define foreach_nat_in2out_ed_error \
187 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
188 _(IN2OUT_PACKETS, "good in2out packets processed") \
189 _(OUT_OF_PORTS, "out of ports") \
190 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
191 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
192 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
193 _(DROP_FRAGMENT, "drop fragment") \
194 _(CANNOT_CREATE_USER, "cannot create NAT user") \
195 _(NON_SYN, "non-SYN packet try to create session") \
196 _(TCP_PACKETS, "TCP packets") \
197 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
198 _(UDP_PACKETS, "UDP packets") \
199 _(ICMP_PACKETS, "ICMP packets") \
200 _(OTHER_PACKETS, "other protocol packets") \
201 _(FRAGMENTS, "fragments") \
202 _(CACHED_FRAGMENTS, "cached fragments") \
203 _(PROCESSED_FRAGMENTS, "processed fragments")
207 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
208 foreach_nat_in2out_ed_error
210 NAT_IN2OUT_ED_N_ERROR,
211 } nat_in2out_ed_error_t;
213 #define foreach_nat_out2in_ed_error \
214 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
215 _(OUT2IN_PACKETS, "good out2in packets processed") \
216 _(OUT_OF_PORTS, "out of ports") \
217 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
218 _(NO_TRANSLATION, "no translation") \
219 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
220 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
221 _(DROP_FRAGMENT, "drop fragment") \
222 _(CANNOT_CREATE_USER, "cannot create NAT user") \
223 _(NON_SYN, "non-SYN packet try to create session") \
224 _(TCP_PACKETS, "TCP packets") \
225 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
226 _(UDP_PACKETS, "UDP packets") \
227 _(ICMP_PACKETS, "ICMP packets") \
228 _(OTHER_PACKETS, "other protocol packets") \
229 _(FRAGMENTS, "fragments") \
230 _(CACHED_FRAGMENTS, "cached fragments") \
231 _(PROCESSED_FRAGMENTS, "processed fragments")
235 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
236 foreach_nat_out2in_ed_error
238 NAT_OUT2IN_ED_N_ERROR,
239 } nat_out2in_ed_error_t;
242 /* Endpoint dependent TCP session state */
243 #define NAT44_SES_I2O_FIN 1
244 #define NAT44_SES_O2I_FIN 2
245 #define NAT44_SES_I2O_FIN_ACK 4
246 #define NAT44_SES_O2I_FIN_ACK 8
247 #define NAT44_SES_I2O_SYN 16
248 #define NAT44_SES_O2I_SYN 32
249 #define NAT44_SES_RST 64
252 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
253 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
254 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
255 #define SNAT_SESSION_FLAG_TWICE_NAT 8
256 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
257 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
258 #define SNAT_SESSION_FLAG_AFFINITY 64
259 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
261 /* NAT interface flags */
262 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
263 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
265 /* Static mapping flags */
266 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
267 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
268 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
269 #define NAT_STATIC_MAPPING_FLAG_LB 8
272 typedef CLIB_PACKED(struct
274 /* Outside network key */
275 snat_session_key_t out2in;
277 /* Inside network key */
278 snat_session_key_t in2out;
283 /* Per-user translations */
285 u32 per_user_list_head_index;
287 /* index in global LRU list */
288 u32 global_lru_index;
291 /* Last heard timer */
294 /* Last HA refresh */
295 f64 ha_last_refreshed;
301 /* External host address and port */
302 ip4_address_t ext_host_addr;
305 /* External host address and port after translation */
306 ip4_address_t ext_host_nat_addr;
307 u16 ext_host_nat_port;
309 /* TCP session state */
313 u32 tcp_close_timestamp;
325 u32 sessions_per_user_list_head_index;
335 #define _(N, i, n, s) \
336 u16 busy_##n##_ports; \
337 u16 * busy_##n##_ports_per_thread; \
338 u32 busy_##n##_port_refcounts[65535];
339 foreach_snat_protocol
352 /* Inside network port */
354 /* Outside network address and port */
355 snat_det_out_key_t out;
360 } snat_det_session_t;
364 /* inside IP address range */
365 ip4_address_t in_addr;
367 /* outside IP address range */
368 ip4_address_t out_addr;
370 /* inside IP addresses / outside IP addresses */
372 /* number of ports available to internal host */
374 /* session counter */
376 /* vector of sessions */
377 snat_det_session_t *sessions;
382 /* backend IP address */
384 /* backend port number */
386 /* probability of the backend to be randomly matched */
389 /* backend FIB table */
392 } nat44_lb_addr_port_t;
396 /* twice-nat disabled */
398 /* twice-nat enabled */
400 /* twice-nat only when src IP equals dst IP after translation */
406 /* no load-balancing */
410 /* load-balancing with affinity */
416 /* local IP address */
417 ip4_address_t local_addr;
418 /* external IP address */
419 ip4_address_t external_addr;
425 twice_nat_type_t twice_nat;
426 /* local FIB table */
430 snat_protocol_t proto;
431 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
433 /* worker threads used by backends/local host */
435 /* opaque string tag */
437 /* backends for load-balancing mode */
438 nat44_lb_addr_port_t *locals;
439 /* affinity per service lis */
440 u32 affinity_per_service_list_head_index;
443 } snat_static_mapping_t;
453 ip4_address_t l_addr;
458 snat_protocol_t proto;
466 } snat_static_map_resolve_t;
470 /* Main lookup tables */
471 clib_bihash_8_8_t out2in;
472 clib_bihash_8_8_t in2out;
474 /* Endpoint dependent sessions lookup tables */
475 clib_bihash_16_8_t out2in_ed;
476 clib_bihash_16_8_t in2out_ed;
478 /* Find-a-user => src address lookup */
479 clib_bihash_8_8_t user_hash;
485 snat_session_t *sessions;
487 /* Pool of doubly-linked list elements */
488 dlist_elt_t *list_pool;
490 /* LRU session list - head is stale, tail is fresh */
491 dlist_elt_t *global_lru_pool;
492 u32 global_lru_head_index;
494 /* NAT thread index */
495 u32 snat_thread_index;
497 /* real thread index */
500 } snat_main_per_thread_data_t;
504 /* ICMP session match function */
505 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
506 vlib_node_runtime_t * node,
509 ip4_header_t * ip0, u8 * p_proto,
510 snat_session_key_t * p_value,
511 u8 * p_dont_translate, void *d,
514 /* Return worker thread index for given packet */
515 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
519 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
524 /* NAT address and port allacotaion function */
525 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
529 snat_session_key_t * k,
531 u32 snat_thread_index);
534 typedef struct snat_main_s
536 /* ICMP session match functions */
537 snat_icmp_match_function_t *icmp_match_in2out_cb;
538 snat_icmp_match_function_t *icmp_match_out2in_cb;
540 /* Thread settings */
542 u32 first_worker_index;
544 snat_get_worker_in2out_function_t *worker_in2out_cb;
545 snat_get_worker_out2in_function_t *worker_out2in_cb;
549 /* Per thread data */
550 snat_main_per_thread_data_t *per_thread_data;
552 /* Find a static mapping by local */
553 clib_bihash_8_8_t static_mapping_by_local;
555 /* Find a static mapping by external */
556 clib_bihash_8_8_t static_mapping_by_external;
558 /* Static mapping pool */
559 snat_static_mapping_t *static_mappings;
562 snat_interface_t *interfaces;
563 snat_interface_t *output_feature_interfaces;
565 /* Vector of outside addresses */
566 snat_address_t *addresses;
567 /* Address and port allocation function */
568 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
569 /* Address and port allocation type */
570 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
571 /* Port set parameters (MAP-E) */
575 /* Port range parameters */
579 /* vector of outside fibs */
580 nat_outside_fib_t *outside_fibs;
582 /* Vector of twice NAT addresses for extenal hosts */
583 snat_address_t *twice_nat_addresses;
585 /* sw_if_indices whose intfc addresses should be auto-added */
586 u32 *auto_add_sw_if_indices;
587 u32 *auto_add_sw_if_indices_twice_nat;
589 /* vector of interface address static mappings to resolve. */
590 snat_static_map_resolve_t *to_resolve;
592 /* Randomize port allocation order */
595 /* Worker handoff frame-queue index */
597 u32 fq_in2out_output_index;
601 u32 error_node_index;
603 /* handoff fq nodes */
604 u32 handoff_out2in_index;
605 u32 handoff_in2out_index;
606 u32 handoff_in2out_output_index;
608 /* respect feature arc nodes */
609 u32 pre_out2in_node_index;
610 u32 pre_in2out_node_index;
612 u32 in2out_node_index;
613 u32 in2out_output_node_index;
614 u32 in2out_fast_node_index;
615 u32 in2out_slowpath_node_index;
616 u32 in2out_slowpath_output_node_index;
617 u32 ed_in2out_node_index;
618 u32 ed_in2out_slowpath_node_index;
619 u32 out2in_node_index;
620 u32 out2in_fast_node_index;
621 u32 ed_out2in_node_index;
622 u32 ed_out2in_slowpath_node_index;
623 u32 det_in2out_node_index;
624 u32 det_out2in_node_index;
626 u32 hairpinning_node_index;
627 u32 hairpin_dst_node_index;
628 u32 hairpin_src_node_index;
629 u32 ed_hairpinning_node_index;
630 u32 ed_hairpin_dst_node_index;
631 u32 ed_hairpin_src_node_index;
634 /* Deterministic NAT mappings */
635 snat_det_map_t *det_maps;
637 /* If forwarding is enabled */
638 u8 forwarding_enabled;
640 /* Config parameters */
641 u8 static_mapping_only;
642 u8 static_mapping_connection_tracking;
645 u8 endpoint_dependent;
646 u32 translation_buckets;
647 uword translation_memory_size;
648 u32 max_translations;
650 uword user_memory_size;
651 u32 max_translations_per_user;
653 u32 outside_fib_index;
655 u32 inside_fib_index;
657 /* values of various timeouts */
660 u32 tcp_transitory_timeout;
661 u32 tcp_established_timeout;
664 /* TCP MSS clamping */
668 /* counters/gauges */
669 vlib_simple_counter_main_t total_users;
670 vlib_simple_counter_main_t total_sessions;
672 /* API message ID base */
676 vlib_log_class_t log_class;
681 vlib_main_t *vlib_main;
682 vnet_main_t *vnet_main;
683 ip4_main_t *ip4_main;
684 ip_lookup_main_t *ip4_lookup_main;
685 api_main_t *api_main;
692 } nat44_is_idle_session_ctx_t;
696 u32 cached_sw_if_index;
697 u32 cached_ip4_address;
700 extern snat_main_t snat_main;
702 // nat pre ed next_node feature classification
703 extern vlib_node_registration_t nat_default_node;
704 extern vlib_node_registration_t nat_pre_in2out_node;
705 extern vlib_node_registration_t nat_pre_out2in_node;
707 extern vlib_node_registration_t snat_in2out_node;
708 extern vlib_node_registration_t snat_in2out_output_node;
709 extern vlib_node_registration_t snat_out2in_node;
710 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
711 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
712 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
713 extern vlib_node_registration_t snat_det_in2out_node;
714 extern vlib_node_registration_t snat_det_out2in_node;
715 extern vlib_node_registration_t nat44_ed_in2out_node;
716 extern vlib_node_registration_t nat44_ed_in2out_output_node;
717 extern vlib_node_registration_t nat44_ed_out2in_node;
719 extern fib_source_t nat_fib_src_hi;
720 extern fib_source_t nat_fib_src_low;
722 /* format functions */
723 format_function_t format_snat_user;
724 format_function_t format_snat_user_v2;
725 format_function_t format_snat_static_mapping;
726 format_function_t format_snat_static_map_to_resolve;
727 format_function_t format_snat_session;
728 format_function_t format_det_map_ses;
729 format_function_t format_snat_key;
730 format_function_t format_static_mapping_key;
731 format_function_t format_snat_protocol;
732 format_function_t format_nat_addr_and_port_alloc_alg;
733 /* unformat functions */
734 unformat_function_t unformat_snat_protocol;
736 /** \brief Check if SNAT session is created from static mapping.
737 @param s SNAT session
738 @return 1 if SNAT session is created from static mapping otherwise 0
740 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
742 /** \brief Check if SNAT session for unknown protocol.
743 @param s SNAT session
744 @return 1 if SNAT session for unknown protocol otherwise 0
746 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
748 /** \brief Check if NAT session is twice NAT.
750 @return 1 if NAT session is twice NAT
752 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
754 /** \brief Check if NAT session is load-balancing.
756 @return 1 if NAT session is load-balancing
758 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
760 /** \brief Check if NAT session is forwarding bypass.
762 @return 1 if NAT session is load-balancing
764 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
766 /** \brief Check if NAT session is endpoint dependent.
768 @return 1 if NAT session is endpoint dependent
770 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
772 /** \brief Check if NAT session has affinity record.
774 @return 1 if NAT session has affinity record
776 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
778 /** \brief Check if NAT interface is inside.
779 @param i NAT interfce
780 @return 1 if inside interface
782 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
784 /** \brief Check if NAT interface is outside.
785 @param i NAT interfce
786 @return 1 if outside interface
788 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
790 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
792 @return 1 if session is closed
794 #define nat44_is_ses_closed(s) s->state == 0xf
796 /** \brief Check if NAT static mapping is address only (1:1NAT).
797 @param sm NAT static mapping
798 @return 1 if 1:1NAT, 0 if 1:1NAPT
800 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
802 /** \brief Check if NAT static mapping match only out2in direction.
803 @param sm NAT static mapping
804 @return 1 if rule match only out2in direction
806 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
808 /** \brief Check if NAT static mapping is identity NAT.
809 @param sm NAT static mapping
810 @return 1 if identity NAT
812 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
814 /** \brief Check if NAT static mapping is load-balancing.
815 @param sm NAT static mapping
816 @return 1 if load-balancing
818 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
820 /** \brief Check if client initiating TCP connection (received SYN from client)
822 @return 1 if client initiating TCP connection
825 tcp_flags_is_init (u8 f)
827 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
831 #define nat_log_err(...) \
832 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
833 #define nat_log_warn(...) \
834 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
835 #define nat_log_notice(...) \
836 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
837 #define nat_log_info(...) \
838 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
839 #define nat_log_debug(...)\
840 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
842 /* NAT API Logging Levels */
843 #define foreach_nat_log_level \
846 _(0x02, LOG_WARNING) \
847 _(0x03, LOG_NOTICE) \
851 typedef enum nat_log_level_t_
853 #define _(n,f) SNAT_##f = n,
854 foreach_nat_log_level
858 #define nat_elog(_level, _str) \
861 snat_main_t *sm = &snat_main; \
862 if (PREDICT_FALSE (sm->log_level >= _level)) \
864 ELOG_TYPE_DECLARE (e) = \
866 .format = "nat-msg " _str, \
869 ELOG_DATA (&sm->vlib_main->elog_main, e); \
873 #define nat_elog_addr(_level, _str, _addr) \
876 if (PREDICT_FALSE (sm->log_level >= _level)) \
878 ELOG_TYPE_DECLARE (e) = \
880 .format = "nat-msg " _str " %d.%d.%d.%d", \
881 .format_args = "i1i1i1i1", \
890 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
891 ed->oct4 = _addr >> 24; \
892 ed->oct3 = _addr >> 16; \
893 ed->oct2 = _addr >> 8; \
898 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
901 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
903 ELOG_TYPE_DECLARE (e) = \
905 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
906 " tid from: %d to: %d fib: %d", \
907 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
923 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
924 ed->src_oct1 = _src >> 24; \
925 ed->src_oct2 = _src >> 16; \
926 ed->src_oct3 = _src >> 8; \
927 ed->src_oct4 = _src; \
928 ed->dst_oct1 = _dst >> 24; \
929 ed->dst_oct2 = _dst >> 16; \
930 ed->dst_oct3 = _dst >> 8; \
931 ed->dst_oct4 = _dst; \
932 ed->ftid = vlib_get_thread_index (); \
938 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
941 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
943 ELOG_TYPE_DECLARE (e) = \
945 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
946 " tid:%d prt:%d fib:%d", \
947 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
963 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
964 ed->src_oct1 = _src >> 24; \
965 ed->src_oct2 = _src >> 16; \
966 ed->src_oct3 = _src >> 8; \
967 ed->src_oct4 = _src; \
968 ed->dst_oct1 = _dst >> 24; \
969 ed->dst_oct2 = _dst >> 16; \
970 ed->dst_oct3 = _dst >> 8; \
971 ed->dst_oct4 = _dst; \
972 ed->tid = vlib_get_thread_index (); \
978 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
981 snat_main_t *sm = &snat_main; \
982 if (PREDICT_FALSE (sm->log_level >= _level)) \
984 ELOG_TYPE_DECLARE (e) = \
986 .format = "nat-msg " _fmt, \
987 .format_args = _arg, \
991 typeof (_val1) val1; \
993 ed = ELOG_DATA (&sm->vlib_main->elog_main, e); \
998 #define nat_elog_notice(nat_elog_str) \
999 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
1000 #define nat_elog_warn(nat_elog_str) \
1001 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1002 #define nat_elog_err(nat_elog_str) \
1003 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1004 #define nat_elog_debug(nat_elog_str) \
1005 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1006 #define nat_elog_info(nat_elog_str) \
1007 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1009 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1010 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1011 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1012 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1013 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1014 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1015 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1016 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1017 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1018 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1020 /* ICMP session match functions */
1021 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1022 u32 thread_index, vlib_buffer_t * b0,
1023 ip4_header_t * ip0, u8 * p_proto,
1024 snat_session_key_t * p_value,
1025 u8 * p_dont_translate, void *d, void *e);
1026 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1027 u32 thread_index, vlib_buffer_t * b0,
1028 ip4_header_t * ip0, u8 * p_proto,
1029 snat_session_key_t * p_value,
1030 u8 * p_dont_translate, void *d, void *e);
1031 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1032 u32 thread_index, vlib_buffer_t * b0,
1033 ip4_header_t * ip0, u8 * p_proto,
1034 snat_session_key_t * p_value,
1035 u8 * p_dont_translate, void *d, void *e);
1036 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1037 u32 thread_index, vlib_buffer_t * b0,
1038 ip4_header_t * ip0, u8 * p_proto,
1039 snat_session_key_t * p_value,
1040 u8 * p_dont_translate, void *d, void *e);
1042 /* ICMP deterministic NAT session match functions */
1043 u32 icmp_match_out2in_det (snat_main_t * sm, vlib_node_runtime_t * node,
1044 u32 thread_index, vlib_buffer_t * b0,
1045 ip4_header_t * ip0, u8 * p_proto,
1046 snat_session_key_t * p_value,
1047 u8 * p_dont_translate, void *d, void *e);
1048 u32 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
1049 u32 thread_index, vlib_buffer_t * b0,
1050 ip4_header_t * ip0, u8 * p_proto,
1051 snat_session_key_t * p_value,
1052 u8 * p_dont_translate, void *d, void *e);
1054 /* ICMP endpoint-dependent session match functions */
1055 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1056 u32 thread_index, vlib_buffer_t * b0,
1057 ip4_header_t * ip0, u8 * p_proto,
1058 snat_session_key_t * p_value,
1059 u8 * p_dont_translate, void *d, void *e);
1060 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1061 u32 thread_index, vlib_buffer_t * b0,
1062 ip4_header_t * ip0, u8 * p_proto,
1063 snat_session_key_t * p_value,
1064 u8 * p_dont_translate, void *d, void *e);
1066 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1067 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1068 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1071 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1072 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1073 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1076 /* hairpinning functions */
1077 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1078 ip4_header_t * ip0, icmp46_header_t * icmp0,
1080 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1082 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1084 int snat_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1085 ip4_header_t * ip0, udp_header_t * udp0,
1086 tcp_header_t * tcp0, u32 proto0, int is_ed);
1088 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1089 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1090 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1091 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1092 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1095 * @brief Add external address to NAT44 pool
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 addr IPv4 address
1110 * @param delete_sm 1 if delete static mapping using address
1111 * @param twice_nat 1 if twice NAT address
1113 * @return 0 on success, non-zero value otherwise
1115 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1119 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1121 * @param addr IPv4 address
1122 * @param is_add 1 = add, 0 = delete
1124 * @return 0 on success, non-zero value otherwise
1126 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1129 * @brief Add/delete NAT44 static mapping
1131 * @param l_addr local IPv4 address
1132 * @param e_addr external IPv4 address
1133 * @param l_port local port number
1134 * @param e_port external port number
1135 * @param vrf_id local VRF ID
1136 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1137 * @param sw_if_index use interface address as external IPv4 address
1138 * @param proto L4 protocol
1139 * @param is_add 1 = add, 0 = delete
1140 * @param twice_nat twice-nat mode
1141 * @param out2in_only if 1 rule match only out2in direction
1142 * @param tag opaque string tag
1143 * @param identity_nat identity NAT
1145 * @return 0 on success, non-zero value otherwise
1147 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1148 u16 l_port, u16 e_port, u32 vrf_id,
1149 int addr_only, u32 sw_if_index,
1150 snat_protocol_t proto, int is_add,
1151 twice_nat_type_t twice_nat, u8 out2in_only,
1152 u8 * tag, u8 identity_nat);
1155 * @brief Add/delete static mapping with load-balancing (multiple backends)
1157 * @param e_addr external IPv4 address
1158 * @param e_port external port number
1159 * @param proto L4 protocol
1160 * @param locals list of local backends
1161 * @param is_add 1 = add, 0 = delete
1162 * @param twice_nat twice-nat mode
1163 * @param out2in_only if 1 rule match only out2in direction
1164 * @param tag opaque string tag
1165 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1167 * @return 0 on success, non-zero value otherwise
1169 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1170 snat_protocol_t proto,
1171 nat44_lb_addr_port_t * locals, u8 is_add,
1172 twice_nat_type_t twice_nat,
1173 u8 out2in_only, u8 * tag, u32 affinity);
1175 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1176 ip4_address_t l_addr, u16 l_port,
1177 snat_protocol_t proto, u32 vrf_id,
1178 u8 probability, u8 is_add);
1180 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1183 * @brief Set NAT plugin workers
1185 * @param bitmap NAT workers bitmap
1187 * @return 0 on success, non-zero value otherwise
1189 int snat_set_workers (uword * bitmap);
1192 * @brief Enable/disable NAT44 feature on the interface
1194 * @param sw_if_index software index of the interface
1195 * @param is_inside 1 = inside, 0 = outside
1196 * @param is_del 1 = delete, 0 = add
1198 * @return 0 on success, non-zero value otherwise
1200 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1203 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1205 * @param sw_if_index software index of the interface
1206 * @param is_inside 1 = inside, 0 = outside
1207 * @param is_del 1 = delete, 0 = add
1209 * @return 0 on success, non-zero value otherwise
1211 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1215 * @brief Add/delete NAT44 pool address from specific interfce
1217 * @param sw_if_index software index of the interface
1218 * @param is_del 1 = delete, 0 = add
1219 * @param twice_nat 1 = twice NAT address for extenal hosts
1221 * @return 0 on success, non-zero value otherwise
1223 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1227 * @brief Delete NAT44 session
1229 * @param addr IPv4 address
1230 * @param port L4 port number
1231 * @param proto L4 protocol
1232 * @param vrf_id VRF ID
1233 * @param is_in 1 = inside network address and port pair, 0 = outside
1235 * @return 0 on success, non-zero value otherwise
1237 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1238 snat_protocol_t proto, u32 vrf_id, int is_in);
1241 * @brief Delete NAT44 endpoint-dependent session
1243 * @param addr IPv4 address
1244 * @param port L4 port number
1245 * @param proto L4 protocol
1246 * @param vrf_id VRF ID
1247 * @param is_in 1 = inside network address and port pair, 0 = outside
1249 * @return 0 on success, non-zero value otherwise
1251 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1252 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1253 u32 vrf_id, int is_in);
1256 * @brief Free NAT44 session data (lookup keys, external addrres port)
1258 * @param s NAT session
1259 * @param thread_index thread index
1260 * @param is_ha is HA event
1262 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1263 u32 thread_index, u8 is_ha);
1266 * @brief Free NAT44 ED session data (lookup keys, external addrres port)
1268 * @param s NAT session
1269 * @param thread_index thread index
1270 * @param is_ha is HA event
1273 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1274 u32 thread_index, u8 is_ha);
1276 * @brief Find or create NAT user
1278 * @param addr IPv4 address
1279 * @param fib_index FIB table index
1280 * @param thread_index thread index
1282 * @return NAT user data structure on success otherwise zero value
1284 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1285 ip4_address_t * addr, u32 fib_index,
1289 * @brief Allocate new NAT session or recycle last used
1292 * @param thread_index thread index
1294 * @return session data structure on success otherwise zero value
1296 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1298 u32 thread_index, f64 now);
1301 * @brief Allocate NAT endpoint-dependent session
1303 * @param thread_index thread index
1305 * @return session data structure on success otherwise zero value
1307 snat_session_t *nat_ed_session_alloc (snat_main_t * sm, u32 thread_index,
1311 * @brief Set address and port assignment algorithm for MAP-E CE
1313 * @param psid Port Set Identifier value
1314 * @param psid_offset number of offset bits
1315 * @param psid_length length of PSID
1317 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1321 * @brief Set address and port assignment algorithm for port range
1323 * @param start_port beginning of the port range
1324 * @param end_port end of the port range
1326 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1329 * @brief Set address and port assignment algorithm to default/standard
1331 void nat_set_alloc_addr_and_port_default (void);
1334 * @brief Free outside address and port pair
1336 * @param addresses vector of outside addresses
1337 * @param thread_index thread index
1338 * @param k address, port and protocol
1340 void snat_free_outside_address_and_port (snat_address_t * addresses,
1342 snat_session_key_t * k);
1345 * @brief Alloc outside address and port
1347 * @param addresses vector of outside addresses
1348 * @param fib_index FIB table index
1349 * @param thread_index thread index
1350 * @param k allocated address and port pair
1351 * @param port_per_thread number of ports per threead
1352 * @param snat_thread_index NAT thread index
1354 * @return 0 on success, non-zero value otherwise
1356 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1359 snat_session_key_t * k,
1360 u16 port_per_thread,
1361 u32 snat_thread_index);
1364 * @brief Match NAT44 static mapping.
1366 * @param match address and port to match
1367 * @param mapping external/local address and port of the matched mapping
1368 * @param by_external if 0 match by local address otherwise match by external
1370 * @param is_addr_only 1 if matched mapping is address only
1371 * @param twice_nat matched mapping is twice NAT type
1372 * @param lb 1 if matched mapping is load-balanced
1373 * @param ext_host_addr external host address
1375 * @returns 0 if match found otherwise 1.
1377 int snat_static_mapping_match (snat_main_t * sm,
1378 snat_session_key_t match,
1379 snat_session_key_t * mapping,
1382 twice_nat_type_t * twice_nat,
1384 ip4_address_t * ext_host_addr,
1385 u8 * is_identity_nat);
1388 * @brief Add/del NAT address to FIB.
1390 * Add the external NAT address to the FIB as receive entries. This ensures
1391 * that VPP will reply to ARP for this address and we don't need to enable
1392 * proxy ARP on the outside interface.
1394 * @param addr IPv4 address
1395 * @param plen address prefix length
1396 * @param sw_if_index software index of the outside interface
1397 * @param is_add 0 = delete, 1 = add.
1399 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1400 u8 p_len, u32 sw_if_index, int is_add);
1403 * Why is this here? Because we don't need to touch this layer to
1404 * simply reply to an icmp. We need to change id to a unique
1405 * value to NAT an echo request/reply.
1412 } icmp_echo_header_t;
1416 u16 src_port, dst_port;
1419 int nat_global_lru_free_one (snat_main_t * sm, int thread_index, f64 now);
1421 #endif /* __included_nat_h__ */
1423 * fd.io coding-style-patch-verification: ON
1426 * eval: (c-set-style "gnu")