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 <vppinfra/elog.h>
27 #include <vppinfra/bihash_8_8.h>
28 #include <vppinfra/bihash_16_8.h>
29 #include <vppinfra/dlist.h>
30 #include <vppinfra/error.h>
31 #include <vlibapi/api.h>
34 /* default session timeouts */
35 #define SNAT_UDP_TIMEOUT 300
36 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
37 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
38 #define SNAT_ICMP_TIMEOUT 60
40 /* number of worker handoff frame queue elements */
41 #define NAT_FQ_NELTS 64
43 /* NAT buffer flags */
44 #define SNAT_FLAG_HAIRPINNING (1 << 0)
46 /* session key (4-tuple) */
55 u16 protocol:3, fib_index:13;
61 /* endpoint-dependent session key (6-tuple) */
70 u32 proto:8, fib_index:24;
78 /* deterministic session outside key */
85 ip4_address_t ext_host_addr;
93 /* user (internal host) key */
112 } nat44_reass_trace_t;
114 /* NAT API Configuration flags */
115 #define foreach_nat_config_flag \
116 _(0x01, IS_TWICE_NAT) \
117 _(0x02, IS_SELF_TWICE_NAT) \
118 _(0x04, IS_OUT2IN_ONLY) \
119 _(0x08, IS_ADDR_ONLY) \
120 _(0x10, IS_OUTSIDE) \
123 _(0x80, IS_EXT_HOST_VALID) \
125 typedef enum nat_config_flags_t_
127 #define _(n,f) NAT_API_##f = n,
128 foreach_nat_config_flag
130 } nat_config_flags_t;
132 /* External address and port allocation modes */
133 #define foreach_nat_addr_and_port_alloc_alg \
134 _(0, DEFAULT, "default") \
135 _(1, MAPE, "map-e") \
136 _(2, RANGE, "port-range")
140 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
141 foreach_nat_addr_and_port_alloc_alg
143 } nat_addr_and_port_alloc_alg_t;
146 /* Supported L4 protocols */
147 #define foreach_snat_protocol \
148 _(UDP, 0, udp, "udp") \
149 _(TCP, 1, tcp, "tcp") \
150 _(ICMP, 2, icmp, "icmp")
154 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
155 foreach_snat_protocol
161 #define foreach_snat_session_state \
162 _(0, UNKNOWN, "unknown") \
163 _(1, UDP_ACTIVE, "udp-active") \
164 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
165 _(3, TCP_ESTABLISHED, "tcp-established") \
166 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
167 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
168 _(6, TCP_CLOSING, "tcp-closing") \
169 _(7, TCP_LAST_ACK, "tcp-last-ack") \
170 _(8, TCP_CLOSED, "tcp-closed") \
171 _(9, ICMP_ACTIVE, "icmp-active")
175 #define _(v, N, s) SNAT_SESSION_##N = v,
176 foreach_snat_session_state
178 } snat_session_state_t;
180 #define foreach_nat_in2out_ed_error \
181 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
182 _(IN2OUT_PACKETS, "good in2out packets processed") \
183 _(OUT_OF_PORTS, "out of ports") \
184 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
185 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
186 _(DROP_FRAGMENT, "drop fragment") \
187 _(MAX_REASS, "maximum reassemblies exceeded") \
188 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
189 _(NON_SYN, "non-SYN packet try to create session") \
190 _(TCP_PACKETS, "TCP packets") \
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_IN2OUT_ED_ERROR_##sym,
201 foreach_nat_in2out_ed_error
203 NAT_IN2OUT_ED_N_ERROR,
204 } nat_in2out_ed_error_t;
206 #define foreach_nat_out2in_ed_error \
207 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
208 _(OUT2IN_PACKETS, "good out2in packets processed") \
209 _(OUT_OF_PORTS, "out of ports") \
210 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
211 _(NO_TRANSLATION, "no translation") \
212 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
213 _(DROP_FRAGMENT, "drop fragment") \
214 _(MAX_REASS, "maximum reassemblies exceeded") \
215 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
216 _(NON_SYN, "non-SYN packet try to create session") \
217 _(TCP_PACKETS, "TCP packets") \
218 _(UDP_PACKETS, "UDP packets") \
219 _(ICMP_PACKETS, "ICMP packets") \
220 _(OTHER_PACKETS, "other protocol packets") \
221 _(FRAGMENTS, "fragments") \
222 _(CACHED_FRAGMENTS, "cached fragments") \
223 _(PROCESSED_FRAGMENTS, "processed fragments")
227 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
228 foreach_nat_out2in_ed_error
230 NAT_OUT2IN_ED_N_ERROR,
231 } nat_out2in_ed_error_t;
234 /* Endpoint dependent TCP session state */
235 #define NAT44_SES_I2O_FIN 1
236 #define NAT44_SES_O2I_FIN 2
237 #define NAT44_SES_I2O_FIN_ACK 4
238 #define NAT44_SES_O2I_FIN_ACK 8
239 #define NAT44_SES_I2O_SYN 16
240 #define NAT44_SES_O2I_SYN 32
241 #define NAT44_SES_RST 64
244 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
245 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
246 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
247 #define SNAT_SESSION_FLAG_TWICE_NAT 8
248 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
249 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
250 #define SNAT_SESSION_FLAG_AFFINITY 64
251 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
253 /* NAT interface flags */
254 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
255 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
257 /* Static mapping flags */
258 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
259 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
260 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
261 #define NAT_STATIC_MAPPING_FLAG_LB 8
264 typedef CLIB_PACKED(struct
266 /* Outside network key */
267 snat_session_key_t out2in;
269 /* Inside network key */
270 snat_session_key_t in2out;
275 /* Per-user translations */
277 u32 per_user_list_head_index;
279 /* Last heard timer */
282 /* Last HA refresh */
283 f64 ha_last_refreshed;
289 /* External host address and port */
290 ip4_address_t ext_host_addr;
293 /* External host address and port after translation */
294 ip4_address_t ext_host_nat_addr;
295 u16 ext_host_nat_port;
297 /* TCP session state */
312 u32 sessions_per_user_list_head_index;
322 #define _(N, i, n, s) \
323 u16 busy_##n##_ports; \
324 u16 * busy_##n##_ports_per_thread; \
325 uword * busy_##n##_port_bitmap;
326 foreach_snat_protocol
339 /* Inside network port */
341 /* Outside network address and port */
342 snat_det_out_key_t out;
347 } snat_det_session_t;
351 /* inside IP address range */
352 ip4_address_t in_addr;
354 /* outside IP address range */
355 ip4_address_t out_addr;
357 /* inside IP addresses / outside IP addresses */
359 /* number of ports available to internal host */
361 /* session counter */
363 /* vector of sessions */
364 snat_det_session_t *sessions;
369 /* backend IP address */
371 /* backend port number */
373 /* probability of the backend to be randomly matched */
376 /* backend FIB table */
379 } nat44_lb_addr_port_t;
383 /* twice-nat disabled */
385 /* twice-nat enabled */
387 /* twice-nat only when src IP equals dst IP after translation */
393 /* no load-balancing */
397 /* load-balancing with affinity */
403 /* local IP address */
404 ip4_address_t local_addr;
405 /* external IP address */
406 ip4_address_t external_addr;
412 twice_nat_type_t twice_nat;
413 /* local FIB table */
417 snat_protocol_t proto;
418 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
420 /* worker threads used by backends/local host */
422 /* opaque string tag */
424 /* backends for load-balancing mode */
425 nat44_lb_addr_port_t *locals;
426 /* affinity per service lis */
427 u32 affinity_per_service_list_head_index;
430 } snat_static_mapping_t;
440 ip4_address_t l_addr;
445 snat_protocol_t proto;
453 } snat_static_map_resolve_t;
457 /* Main lookup tables */
458 clib_bihash_8_8_t out2in;
459 clib_bihash_8_8_t in2out;
461 /* Endpoint dependent sessions lookup tables */
462 clib_bihash_16_8_t out2in_ed;
463 clib_bihash_16_8_t in2out_ed;
465 /* Find-a-user => src address lookup */
466 clib_bihash_8_8_t user_hash;
472 snat_session_t *sessions;
474 /* Pool of doubly-linked list elements */
475 dlist_elt_t *list_pool;
477 /* NAT thread index */
478 u32 snat_thread_index;
480 /* 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,
491 ip4_header_t * ip0, u8 * p_proto,
492 snat_session_key_t * p_value,
493 u8 * p_dont_translate, void *d,
496 /* Return worker thread index for given packet */
497 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip,
498 u32 rx_fib_index, u8 is_output);
500 /* NAT address and port allacotaion function */
501 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
505 snat_session_key_t * k,
507 u32 snat_thread_index);
509 typedef struct snat_main_s
511 /* ICMP session match functions */
512 snat_icmp_match_function_t *icmp_match_in2out_cb;
513 snat_icmp_match_function_t *icmp_match_out2in_cb;
515 /* Thread settings */
517 u32 first_worker_index;
519 snat_get_worker_function_t *worker_in2out_cb;
520 snat_get_worker_function_t *worker_out2in_cb;
524 /* Per thread data */
525 snat_main_per_thread_data_t *per_thread_data;
527 /* Find a static mapping by local */
528 clib_bihash_8_8_t static_mapping_by_local;
530 /* Find a static mapping by external */
531 clib_bihash_8_8_t static_mapping_by_external;
533 /* Static mapping pool */
534 snat_static_mapping_t *static_mappings;
537 snat_interface_t *interfaces;
538 snat_interface_t *output_feature_interfaces;
540 /* Vector of outside addresses */
541 snat_address_t *addresses;
542 /* Address and port allocation function */
543 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
544 /* Address and port allocation type */
545 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
546 /* Port set parameters (MAP-E) */
550 /* Port range parameters */
554 /* vector of outside fibs */
555 nat_outside_fib_t *outside_fibs;
557 /* Vector of twice NAT addresses for extenal hosts */
558 snat_address_t *twice_nat_addresses;
560 /* sw_if_indices whose intfc addresses should be auto-added */
561 u32 *auto_add_sw_if_indices;
562 u32 *auto_add_sw_if_indices_twice_nat;
564 /* vector of interface address static mappings to resolve. */
565 snat_static_map_resolve_t *to_resolve;
567 /* Randomize port allocation order */
570 /* Worker handoff frame-queue index */
572 u32 fq_in2out_output_index;
576 u32 error_node_index;
578 u32 in2out_node_index;
579 u32 in2out_output_node_index;
580 u32 in2out_fast_node_index;
581 u32 in2out_slowpath_node_index;
582 u32 in2out_slowpath_output_node_index;
583 u32 in2out_reass_node_index;
584 u32 ed_in2out_node_index;
585 u32 ed_in2out_slowpath_node_index;
586 u32 ed_in2out_reass_node_index;
587 u32 out2in_node_index;
588 u32 out2in_fast_node_index;
589 u32 out2in_reass_node_index;
590 u32 ed_out2in_node_index;
591 u32 ed_out2in_slowpath_node_index;
592 u32 ed_out2in_reass_node_index;
593 u32 det_in2out_node_index;
594 u32 det_out2in_node_index;
596 u32 hairpinning_node_index;
597 u32 hairpin_dst_node_index;
598 u32 hairpin_src_node_index;
599 u32 ed_hairpinning_node_index;
600 u32 ed_hairpin_dst_node_index;
601 u32 ed_hairpin_src_node_index;
604 /* Deterministic NAT mappings */
605 snat_det_map_t *det_maps;
607 /* If forwarding is enabled */
608 u8 forwarding_enabled;
610 /* Config parameters */
611 u8 static_mapping_only;
612 u8 static_mapping_connection_tracking;
615 u8 endpoint_dependent;
616 u32 translation_buckets;
617 u32 translation_memory_size;
618 u32 max_translations;
620 u32 user_memory_size;
621 u32 max_translations_per_user;
623 u32 outside_fib_index;
625 u32 inside_fib_index;
627 /* values of various timeouts */
629 u32 tcp_established_timeout;
630 u32 tcp_transitory_timeout;
633 /* TCP MSS clamping */
637 /* counters/gauges */
638 vlib_simple_counter_main_t total_users;
639 vlib_simple_counter_main_t total_sessions;
641 /* API message ID base */
645 vlib_log_class_t log_class;
650 vlib_main_t *vlib_main;
651 vnet_main_t *vnet_main;
652 ip4_main_t *ip4_main;
653 ip_lookup_main_t *ip4_lookup_main;
654 api_main_t *api_main;
661 } nat44_is_idle_session_ctx_t;
665 u32 cached_sw_if_index;
666 u32 cached_ip4_address;
669 extern snat_main_t snat_main;
670 extern vlib_node_registration_t snat_in2out_node;
671 extern vlib_node_registration_t snat_in2out_output_node;
672 extern vlib_node_registration_t snat_out2in_node;
673 extern vlib_node_registration_t snat_in2out_fast_node;
674 extern vlib_node_registration_t snat_out2in_fast_node;
675 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
676 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
677 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
678 extern vlib_node_registration_t snat_det_in2out_node;
679 extern vlib_node_registration_t snat_det_out2in_node;
680 extern vlib_node_registration_t snat_hairpin_dst_node;
681 extern vlib_node_registration_t snat_hairpin_src_node;
682 extern vlib_node_registration_t nat44_ed_in2out_node;
683 extern vlib_node_registration_t nat44_ed_in2out_output_node;
684 extern vlib_node_registration_t nat44_ed_out2in_node;
685 extern vlib_node_registration_t nat44_ed_hairpin_dst_node;
686 extern vlib_node_registration_t nat44_ed_hairpin_src_node;
687 extern vlib_node_registration_t nat44_ed_in2out_worker_handoff_node;
688 extern vlib_node_registration_t nat44_ed_in2out_output_worker_handoff_node;
689 extern vlib_node_registration_t nat44_ed_out2in_worker_handoff_node;
691 /* format functions */
692 format_function_t format_snat_user;
693 format_function_t format_snat_static_mapping;
694 format_function_t format_snat_static_map_to_resolve;
695 format_function_t format_snat_session;
696 format_function_t format_det_map_ses;
697 format_function_t format_snat_key;
698 format_function_t format_static_mapping_key;
699 format_function_t format_snat_protocol;
700 format_function_t format_nat_addr_and_port_alloc_alg;
701 format_function_t format_nat44_reass_trace;
702 /* unformat functions */
703 unformat_function_t unformat_snat_protocol;
705 /** \brief Check if SNAT session is created from static mapping.
706 @param s SNAT session
707 @return 1 if SNAT session is created from static mapping otherwise 0
709 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
711 /** \brief Check if SNAT session for unknown protocol.
712 @param s SNAT session
713 @return 1 if SNAT session for unknown protocol otherwise 0
715 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
717 /** \brief Check if NAT session is twice NAT.
719 @return 1 if NAT session is twice NAT
721 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
723 /** \brief Check if NAT session is load-balancing.
725 @return 1 if NAT session is load-balancing
727 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
729 /** \brief Check if NAT session is forwarding bypass.
731 @return 1 if NAT session is load-balancing
733 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
735 /** \brief Check if NAT session is endpoint dependent.
737 @return 1 if NAT session is endpoint dependent
739 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
741 /** \brief Check if NAT session has affinity record.
743 @return 1 if NAT session has affinity record
745 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
747 /** \brief Check if NAT interface is inside.
748 @param i NAT interfce
749 @return 1 if inside interface
751 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
753 /** \brief Check if NAT interface is outside.
754 @param i NAT interfce
755 @return 1 if outside interface
757 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
759 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
761 @return 1 if session is closed
763 #define nat44_is_ses_closed(s) s->state == 0xf
765 /** \brief Check if NAT static mapping is address only (1:1NAT).
766 @param sm NAT static mapping
767 @return 1 if 1:1NAT, 0 if 1:1NAPT
769 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
771 /** \brief Check if NAT static mapping match only out2in direction.
772 @param sm NAT static mapping
773 @return 1 if rule match only out2in direction
775 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
777 /** \brief Check if NAT static mapping is identity NAT.
778 @param sm NAT static mapping
779 @return 1 if identity NAT
781 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
783 /** \brief Check if NAT static mapping is load-balancing.
784 @param sm NAT static mapping
785 @return 1 if load-balancing
787 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
789 /** \brief Check if client initiating TCP connection (received SYN from client)
791 @return 1 if client initiating TCP connection
793 #define tcp_is_init(t) ((t->flags & TCP_FLAG_SYN) && !(t->flags & TCP_FLAG_ACK))
796 #define nat_log_err(...) \
797 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
798 #define nat_log_warn(...) \
799 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
800 #define nat_log_notice(...) \
801 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
802 #define nat_log_info(...) \
803 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
804 #define nat_log_debug(...)\
805 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
807 /* NAT API Logging Levels */
808 #define foreach_nat_log_level \
811 _(0x02, LOG_WARNING) \
812 _(0x03, LOG_NOTICE) \
816 typedef enum nat_log_level_t_
818 #define _(n,f) SNAT_##f = n,
819 foreach_nat_log_level
823 #define nat_elog(_level, _str) \
826 snat_main_t *sm = &snat_main; \
827 if (PREDICT_FALSE (sm->log_level >= _level)) \
829 ELOG_TYPE_DECLARE (e) = \
831 .format = "nat-msg " _str, \
834 ELOG_DATA (&sm->vlib_main->elog_main, e); \
838 #define nat_elog_addr(_level, _str, _addr) \
841 if (PREDICT_FALSE (sm->log_level >= _level)) \
843 ELOG_TYPE_DECLARE (e) = \
845 .format = "nat-msg " _str " %d.%d.%d.%d", \
846 .format_args = "i1i1i1i1", \
855 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
856 ed->oct4 = _addr >> 24; \
857 ed->oct3 = _addr >> 16; \
858 ed->oct2 = _addr >> 8; \
863 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
866 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
868 ELOG_TYPE_DECLARE (e) = \
870 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
871 " tid from: %d to: %d fib: %d", \
872 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
888 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
889 ed->src_oct1 = _src >> 24; \
890 ed->src_oct2 = _src >> 16; \
891 ed->src_oct3 = _src >> 8; \
892 ed->src_oct4 = _src; \
893 ed->dst_oct1 = _dst >> 24; \
894 ed->dst_oct2 = _dst >> 16; \
895 ed->dst_oct3 = _dst >> 8; \
896 ed->dst_oct4 = _dst; \
897 ed->ftid = vlib_get_thread_index (); \
903 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
906 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
908 ELOG_TYPE_DECLARE (e) = \
910 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
911 " tid:%d prt:%d fib:%d", \
912 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
928 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
929 ed->src_oct1 = _src >> 24; \
930 ed->src_oct2 = _src >> 16; \
931 ed->src_oct3 = _src >> 8; \
932 ed->src_oct4 = _src; \
933 ed->dst_oct1 = _dst >> 24; \
934 ed->dst_oct2 = _dst >> 16; \
935 ed->dst_oct3 = _dst >> 8; \
936 ed->dst_oct4 = _dst; \
937 ed->tid = vlib_get_thread_index (); \
943 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
946 snat_main_t *sm = &snat_main; \
947 if (PREDICT_FALSE (sm->log_level >= _level)) \
949 ELOG_TYPE_DECLARE (e) = \
951 .format = "nat-msg " _fmt, \
952 .format_args = _arg, \
956 typeof (_val1) val1; \
958 ed = ELOG_DATA (&sm->vlib_main->elog_main, e); \
963 #define nat_elog_notice(nat_elog_str) \
964 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
965 #define nat_elog_warn(nat_elog_str) \
966 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
967 #define nat_elog_err(nat_elog_str) \
968 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
969 #define nat_elog_debug(nat_elog_str) \
970 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
971 #define nat_elog_info(nat_elog_str) \
972 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
974 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
975 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
976 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
977 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
978 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
979 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
980 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
981 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
982 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
983 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
985 /* ICMP session match functions */
986 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
987 u32 thread_index, vlib_buffer_t * b0,
988 ip4_header_t * ip0, u8 * p_proto,
989 snat_session_key_t * p_value,
990 u8 * p_dont_translate, void *d, void *e);
991 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
992 u32 thread_index, vlib_buffer_t * b0,
993 ip4_header_t * ip0, u8 * p_proto,
994 snat_session_key_t * p_value,
995 u8 * p_dont_translate, void *d, void *e);
996 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
997 u32 thread_index, vlib_buffer_t * b0,
998 ip4_header_t * ip0, u8 * p_proto,
999 snat_session_key_t * p_value,
1000 u8 * p_dont_translate, void *d, void *e);
1001 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1002 u32 thread_index, vlib_buffer_t * b0,
1003 ip4_header_t * ip0, u8 * p_proto,
1004 snat_session_key_t * p_value,
1005 u8 * p_dont_translate, void *d, void *e);
1007 /* ICMP deterministic NAT session match functions */
1008 u32 icmp_match_out2in_det (snat_main_t * sm, vlib_node_runtime_t * node,
1009 u32 thread_index, vlib_buffer_t * b0,
1010 ip4_header_t * ip0, u8 * p_proto,
1011 snat_session_key_t * p_value,
1012 u8 * p_dont_translate, void *d, void *e);
1013 u32 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
1014 u32 thread_index, vlib_buffer_t * b0,
1015 ip4_header_t * ip0, u8 * p_proto,
1016 snat_session_key_t * p_value,
1017 u8 * p_dont_translate, void *d, void *e);
1019 /* ICMP endpoint-dependent session match functions */
1020 u32 icmp_match_out2in_ed (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_in2out_ed (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);
1031 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1032 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1033 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1036 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1037 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1038 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1041 /* hairpinning functions */
1042 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1043 ip4_header_t * ip0, icmp46_header_t * icmp0,
1045 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1047 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1049 int snat_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1050 ip4_header_t * ip0, udp_header_t * udp0,
1051 tcp_header_t * tcp0, u32 proto0, int is_ed);
1052 void nat44_reass_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1053 ip4_header_t * ip0, u16 sport, u16 dport,
1054 u32 proto0, int is_ed);
1056 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1057 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1058 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1059 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1060 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1063 * @brief Increment IPv4 address
1065 void increment_v4_address (ip4_address_t * a);
1068 * @brief Add external address to NAT44 pool
1070 * @param addr IPv4 address
1071 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1072 * @param twice_nat 1 if twice NAT address
1074 * @return 0 on success, non-zero value otherwise
1076 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1080 * @brief Delete external address from NAT44 pool
1082 * @param addr IPv4 address
1083 * @param delete_sm 1 if delete static mapping using address
1084 * @param twice_nat 1 if twice NAT address
1086 * @return 0 on success, non-zero value otherwise
1088 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1092 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1094 * @param addr IPv4 address
1095 * @param is_add 1 = add, 0 = delete
1097 * @return 0 on success, non-zero value otherwise
1099 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1102 * @brief Add/delete NAT44 static mapping
1104 * @param l_addr local IPv4 address
1105 * @param e_addr external IPv4 address
1106 * @param l_port local port number
1107 * @param e_port external port number
1108 * @param vrf_id local VRF ID
1109 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1110 * @param sw_if_index use interface address as external IPv4 address
1111 * @param proto L4 protocol
1112 * @param is_add 1 = add, 0 = delete
1113 * @param twice_nat twice-nat mode
1114 * @param out2in_only if 1 rule match only out2in direction
1115 * @param tag opaque string tag
1116 * @param identity_nat identity NAT
1118 * @return 0 on success, non-zero value otherwise
1120 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1121 u16 l_port, u16 e_port, u32 vrf_id,
1122 int addr_only, u32 sw_if_index,
1123 snat_protocol_t proto, int is_add,
1124 twice_nat_type_t twice_nat, u8 out2in_only,
1125 u8 * tag, u8 identity_nat);
1128 * @brief Add/delete static mapping with load-balancing (multiple backends)
1130 * @param e_addr external IPv4 address
1131 * @param e_port external port number
1132 * @param proto L4 protocol
1133 * @param locals list of local backends
1134 * @param is_add 1 = add, 0 = delete
1135 * @param twice_nat twice-nat mode
1136 * @param out2in_only if 1 rule match only out2in direction
1137 * @param tag opaque string tag
1138 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1140 * @return 0 on success, non-zero value otherwise
1142 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1143 snat_protocol_t proto,
1144 nat44_lb_addr_port_t * locals, u8 is_add,
1145 twice_nat_type_t twice_nat,
1146 u8 out2in_only, u8 * tag, u32 affinity);
1148 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1149 ip4_address_t l_addr, u16 l_port,
1150 snat_protocol_t proto, u32 vrf_id,
1151 u8 probability, u8 is_add);
1153 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1156 * @brief Set NAT plugin workers
1158 * @param bitmap NAT workers bitmap
1160 * @return 0 on success, non-zero value otherwise
1162 int snat_set_workers (uword * bitmap);
1165 * @brief Enable/disable NAT44 feature on the interface
1167 * @param sw_if_index software index of the interface
1168 * @param is_inside 1 = inside, 0 = outside
1169 * @param is_del 1 = delete, 0 = add
1171 * @return 0 on success, non-zero value otherwise
1173 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1176 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1178 * @param sw_if_index software index of the interface
1179 * @param is_inside 1 = inside, 0 = outside
1180 * @param is_del 1 = delete, 0 = add
1182 * @return 0 on success, non-zero value otherwise
1184 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1188 * @brief Add/delete NAT44 pool address from specific interfce
1190 * @param sw_if_index software index of the interface
1191 * @param is_del 1 = delete, 0 = add
1192 * @param twice_nat 1 = twice NAT address for extenal hosts
1194 * @return 0 on success, non-zero value otherwise
1196 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1200 * @brief Delete NAT44 session
1202 * @param addr IPv4 address
1203 * @param port L4 port number
1204 * @param proto L4 protocol
1205 * @param vrf_id VRF ID
1206 * @param is_in 1 = inside network address and port pair, 0 = outside
1208 * @return 0 on success, non-zero value otherwise
1210 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1211 snat_protocol_t proto, u32 vrf_id, int is_in);
1214 * @brief Delete NAT44 endpoint-dependent session
1216 * @param addr IPv4 address
1217 * @param port L4 port number
1218 * @param proto L4 protocol
1219 * @param vrf_id VRF ID
1220 * @param is_in 1 = inside network address and port pair, 0 = outside
1222 * @return 0 on success, non-zero value otherwise
1224 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1225 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1226 u32 vrf_id, int is_in);
1229 * @brief Free NAT44 session data (lookup keys, external addrres port)
1231 * @param s NAT session
1232 * @param thread_index thread index
1233 * @param is_ha is HA event
1235 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1236 u32 thread_index, u8 is_ha);
1239 * @brief Find or create NAT user
1241 * @param addr IPv4 address
1242 * @param fib_index FIB table index
1243 * @param thread_index thread index
1245 * @return NAT user data structure on success otherwise zero value
1247 snat_user_t *nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr,
1248 u32 fib_index, u32 thread_index);
1251 * @brief Allocate new NAT session or recycle last used
1254 * @param thread_index thread index
1256 * @return session data structure on success otherwise zero value
1258 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1260 u32 thread_index, f64 now);
1263 * @brief Allocate NAT endpoint-dependent session
1266 * @param thread_index thread index
1268 * @return session data structure on success otherwise zero value
1270 snat_session_t *nat_ed_session_alloc (snat_main_t * sm, snat_user_t * u,
1271 u32 thread_index, f64 now);
1274 * @brief Set address and port assignment algorithm for MAP-E CE
1276 * @param psid Port Set Identifier value
1277 * @param psid_offset number of offset bits
1278 * @param psid_length length of PSID
1280 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1284 * @brief Set address and port assignment algorithm for port range
1286 * @param start_port beginning of the port range
1287 * @param end_port end of the port range
1289 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1292 * @brief Set address and port assignment algorithm to default/standard
1294 void nat_set_alloc_addr_and_port_default (void);
1297 * @brief Free outside address and port pair
1299 * @param addresses vector of outside addresses
1300 * @param thread_index thread index
1301 * @param k address, port and protocol
1303 void snat_free_outside_address_and_port (snat_address_t * addresses,
1305 snat_session_key_t * k);
1308 * @brief Alloc outside address and port
1310 * @param addresses vector of outside addresses
1311 * @param fib_index FIB table index
1312 * @param thread_index thread index
1313 * @param k allocated address and port pair
1314 * @param port_per_thread number of ports per threead
1315 * @param snat_thread_index NAT thread index
1317 * @return 0 on success, non-zero value otherwise
1319 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1322 snat_session_key_t * k,
1323 u16 port_per_thread,
1324 u32 snat_thread_index);
1327 * @brief Match NAT44 static mapping.
1329 * @param match address and port to match
1330 * @param mapping external/local address and port of the matched mapping
1331 * @param by_external if 0 match by local address otherwise match by external
1333 * @param is_addr_only 1 if matched mapping is address only
1334 * @param twice_nat matched mapping is twice NAT type
1335 * @param lb 1 if matched mapping is load-balanced
1336 * @param ext_host_addr external host address
1338 * @returns 0 if match found otherwise 1.
1340 int snat_static_mapping_match (snat_main_t * sm,
1341 snat_session_key_t match,
1342 snat_session_key_t * mapping,
1345 twice_nat_type_t * twice_nat,
1347 ip4_address_t * ext_host_addr,
1348 u8 * is_identity_nat);
1351 * @brief Add/del NAT address to FIB.
1353 * Add the external NAT address to the FIB as receive entries. This ensures
1354 * that VPP will reply to ARP for this address and we don't need to enable
1355 * proxy ARP on the outside interface.
1357 * @param addr IPv4 address
1358 * @param plen address prefix length
1359 * @param sw_if_index software index of the outside interface
1360 * @param is_add 0 = delete, 1 = add.
1362 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1363 u8 p_len, u32 sw_if_index, int is_add);
1366 * Why is this here? Because we don't need to touch this layer to
1367 * simply reply to an icmp. We need to change id to a unique
1368 * value to NAT an echo request/reply.
1375 } icmp_echo_header_t;
1379 u16 src_port, dst_port;
1382 #endif /* __included_nat_h__ */
1385 * fd.io coding-style-patch-verification: ON
1388 * eval: (c-set-style "gnu")