2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * NAT plugin global declarations
19 #ifndef __included_nat_h__
20 #define __included_nat_h__
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/ip/icmp46_packet.h>
26 #include <vnet/api_errno.h>
27 #include <vnet/fib/fib_source.h>
28 #include <vppinfra/elog.h>
29 #include <vppinfra/bihash_8_8.h>
30 #include <vppinfra/bihash_16_8.h>
31 #include <vppinfra/dlist.h>
32 #include <vppinfra/error.h>
33 #include <vlibapi/api.h>
35 #include <vppinfra/bihash_16_8.h>
37 /* default session timeouts */
38 #define SNAT_UDP_TIMEOUT 300
39 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
40 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
41 #define SNAT_ICMP_TIMEOUT 60
43 /* number of worker handoff frame queue elements */
44 #define NAT_FQ_NELTS 64
46 /* NAT buffer flags */
47 #define SNAT_FLAG_HAIRPINNING (1 << 0)
55 NAT_NEXT_IN2OUT_ED_FAST_PATH,
56 NAT_NEXT_IN2OUT_ED_SLOW_PATH,
57 NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
58 NAT_NEXT_OUT2IN_ED_FAST_PATH,
59 NAT_NEXT_OUT2IN_ED_SLOW_PATH,
68 /* session key (4-tuple) */
77 u16 protocol:3, fib_index:13;
83 /* deterministic session outside key */
90 ip4_address_t ext_host_addr;
98 /* user (internal host) key */
112 /* NAT API Configuration flags */
113 #define foreach_nat_config_flag \
114 _(0x01, IS_TWICE_NAT) \
115 _(0x02, IS_SELF_TWICE_NAT) \
116 _(0x04, IS_OUT2IN_ONLY) \
117 _(0x08, IS_ADDR_ONLY) \
118 _(0x10, IS_OUTSIDE) \
121 _(0x80, IS_EXT_HOST_VALID) \
123 typedef enum nat_config_flags_t_
125 #define _(n,f) NAT_API_##f = n,
126 foreach_nat_config_flag
128 } nat_config_flags_t;
130 /* External address and port allocation modes */
131 #define foreach_nat_addr_and_port_alloc_alg \
132 _(0, DEFAULT, "default") \
133 _(1, MAPE, "map-e") \
134 _(2, RANGE, "port-range")
138 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
139 foreach_nat_addr_and_port_alloc_alg
141 } nat_addr_and_port_alloc_alg_t;
144 /* Supported L4 protocols */
145 #define foreach_snat_protocol \
146 _(UDP, 0, udp, "udp") \
147 _(TCP, 1, tcp, "tcp") \
148 _(ICMP, 2, icmp, "icmp")
152 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
153 foreach_snat_protocol
159 #define foreach_snat_session_state \
160 _(0, UNKNOWN, "unknown") \
161 _(1, UDP_ACTIVE, "udp-active") \
162 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
163 _(3, TCP_ESTABLISHED, "tcp-established") \
164 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
165 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
166 _(6, TCP_CLOSING, "tcp-closing") \
167 _(7, TCP_LAST_ACK, "tcp-last-ack") \
168 _(8, TCP_CLOSED, "tcp-closed") \
169 _(9, ICMP_ACTIVE, "icmp-active")
173 #define _(v, N, s) SNAT_SESSION_##N = v,
174 foreach_snat_session_state
176 } snat_session_state_t;
178 #define foreach_nat_in2out_ed_error \
179 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
180 _(IN2OUT_PACKETS, "good in2out packets processed") \
181 _(OUT_OF_PORTS, "out of ports") \
182 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
183 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
184 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
185 _(DROP_FRAGMENT, "drop fragment") \
186 _(CANNOT_CREATE_USER, "cannot create NAT user") \
187 _(NON_SYN, "non-SYN packet try to create session") \
188 _(TCP_PACKETS, "TCP packets") \
189 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
190 _(UDP_PACKETS, "UDP packets") \
191 _(ICMP_PACKETS, "ICMP packets") \
192 _(OTHER_PACKETS, "other protocol packets") \
193 _(FRAGMENTS, "fragments") \
194 _(CACHED_FRAGMENTS, "cached fragments") \
195 _(PROCESSED_FRAGMENTS, "processed fragments")
199 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
200 foreach_nat_in2out_ed_error
202 NAT_IN2OUT_ED_N_ERROR,
203 } nat_in2out_ed_error_t;
205 #define foreach_nat_out2in_ed_error \
206 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
207 _(OUT2IN_PACKETS, "good out2in packets processed") \
208 _(OUT_OF_PORTS, "out of ports") \
209 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
210 _(NO_TRANSLATION, "no translation") \
211 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
212 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
213 _(DROP_FRAGMENT, "drop fragment") \
214 _(CANNOT_CREATE_USER, "cannot create NAT user") \
215 _(NON_SYN, "non-SYN packet try to create session") \
216 _(TCP_PACKETS, "TCP packets") \
217 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
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 /* index in global LRU list */
280 u32 global_lru_index;
283 /* Last heard timer */
286 /* Last HA refresh */
287 f64 ha_last_refreshed;
293 /* External host address and port */
294 ip4_address_t ext_host_addr;
297 /* External host address and port after translation */
298 ip4_address_t ext_host_nat_addr;
299 u16 ext_host_nat_port;
301 /* TCP session state */
305 u32 tcp_close_timestamp;
317 u32 sessions_per_user_list_head_index;
327 #define _(N, i, n, s) \
328 u16 busy_##n##_ports; \
329 u16 * busy_##n##_ports_per_thread; \
330 u32 busy_##n##_port_refcounts[65535];
331 foreach_snat_protocol
344 /* Inside network port */
346 /* Outside network address and port */
347 snat_det_out_key_t out;
352 } snat_det_session_t;
356 /* inside IP address range */
357 ip4_address_t in_addr;
359 /* outside IP address range */
360 ip4_address_t out_addr;
362 /* inside IP addresses / outside IP addresses */
364 /* number of ports available to internal host */
366 /* session counter */
368 /* vector of sessions */
369 snat_det_session_t *sessions;
374 /* backend IP address */
376 /* backend port number */
378 /* probability of the backend to be randomly matched */
381 /* backend FIB table */
384 } nat44_lb_addr_port_t;
388 /* twice-nat disabled */
390 /* twice-nat enabled */
392 /* twice-nat only when src IP equals dst IP after translation */
398 /* no load-balancing */
402 /* load-balancing with affinity */
408 /* local IP address */
409 ip4_address_t local_addr;
410 /* external IP address */
411 ip4_address_t external_addr;
417 twice_nat_type_t twice_nat;
418 /* local FIB table */
422 snat_protocol_t proto;
423 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
425 /* worker threads used by backends/local host */
427 /* opaque string tag */
429 /* backends for load-balancing mode */
430 nat44_lb_addr_port_t *locals;
431 /* affinity per service lis */
432 u32 affinity_per_service_list_head_index;
435 } snat_static_mapping_t;
445 ip4_address_t l_addr;
450 snat_protocol_t proto;
458 } snat_static_map_resolve_t;
462 /* Main lookup tables */
463 clib_bihash_8_8_t out2in;
464 clib_bihash_8_8_t in2out;
466 /* Endpoint dependent sessions lookup tables */
467 clib_bihash_16_8_t out2in_ed;
468 clib_bihash_16_8_t in2out_ed;
470 /* Find-a-user => src address lookup */
471 clib_bihash_8_8_t user_hash;
477 snat_session_t *sessions;
479 /* Pool of doubly-linked list elements */
480 dlist_elt_t *list_pool;
482 /* LRU session list - head is stale, tail is fresh */
483 dlist_elt_t *global_lru_pool;
484 u32 global_lru_head_index;
486 /* NAT thread index */
487 u32 snat_thread_index;
489 /* real thread index */
492 } snat_main_per_thread_data_t;
496 /* ICMP session match function */
497 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
498 vlib_node_runtime_t * node,
501 ip4_header_t * ip0, u8 * p_proto,
502 snat_session_key_t * p_value,
503 u8 * p_dont_translate, void *d,
506 /* Return worker thread index for given packet */
507 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
511 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
516 /* NAT address and port allocation function */
517 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
521 snat_session_key_t * k,
523 u32 snat_thread_index);
526 typedef struct snat_main_s
528 /* ICMP session match functions */
529 snat_icmp_match_function_t *icmp_match_in2out_cb;
530 snat_icmp_match_function_t *icmp_match_out2in_cb;
532 /* Thread settings */
534 u32 first_worker_index;
536 snat_get_worker_in2out_function_t *worker_in2out_cb;
537 snat_get_worker_out2in_function_t *worker_out2in_cb;
541 /* Per thread data */
542 snat_main_per_thread_data_t *per_thread_data;
544 /* Find a static mapping by local */
545 clib_bihash_8_8_t static_mapping_by_local;
547 /* Find a static mapping by external */
548 clib_bihash_8_8_t static_mapping_by_external;
550 /* Static mapping pool */
551 snat_static_mapping_t *static_mappings;
554 snat_interface_t *interfaces;
555 snat_interface_t *output_feature_interfaces;
557 /* Vector of outside addresses */
558 snat_address_t *addresses;
559 /* Address and port allocation function */
560 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
561 /* Address and port allocation type */
562 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
563 /* Port set parameters (MAP-E) */
567 /* Port range parameters */
571 /* vector of outside fibs */
572 nat_outside_fib_t *outside_fibs;
574 /* Vector of twice NAT addresses for external hosts */
575 snat_address_t *twice_nat_addresses;
577 /* sw_if_indices whose intfc addresses should be auto-added */
578 u32 *auto_add_sw_if_indices;
579 u32 *auto_add_sw_if_indices_twice_nat;
581 /* vector of interface address static mappings to resolve. */
582 snat_static_map_resolve_t *to_resolve;
584 /* Randomize port allocation order */
587 /* Worker handoff frame-queue index */
589 u32 fq_in2out_output_index;
593 u32 error_node_index;
595 /* handoff fq nodes */
596 u32 handoff_out2in_index;
597 u32 handoff_in2out_index;
598 u32 handoff_in2out_output_index;
600 /* respect feature arc nodes */
601 u32 pre_out2in_node_index;
602 u32 pre_in2out_node_index;
604 u32 in2out_node_index;
605 u32 in2out_output_node_index;
606 u32 in2out_fast_node_index;
607 u32 in2out_slowpath_node_index;
608 u32 in2out_slowpath_output_node_index;
609 u32 ed_in2out_node_index;
610 u32 ed_in2out_slowpath_node_index;
611 u32 out2in_node_index;
612 u32 out2in_fast_node_index;
613 u32 ed_out2in_node_index;
614 u32 ed_out2in_slowpath_node_index;
615 u32 det_in2out_node_index;
616 u32 det_out2in_node_index;
618 u32 hairpinning_node_index;
619 u32 hairpin_dst_node_index;
620 u32 hairpin_src_node_index;
621 u32 ed_hairpinning_node_index;
622 u32 ed_hairpin_dst_node_index;
623 u32 ed_hairpin_src_node_index;
626 /* Deterministic NAT mappings */
627 snat_det_map_t *det_maps;
629 /* If forwarding is enabled */
630 u8 forwarding_enabled;
632 /* Config parameters */
633 u8 static_mapping_only;
634 u8 static_mapping_connection_tracking;
637 u8 endpoint_dependent;
639 u32 translation_buckets;
640 uword translation_memory_size;
641 u32 max_translations;
642 u32 *max_translations_per_fib;
645 uword user_memory_size;
646 u32 max_translations_per_user;
649 u32 outside_fib_index;
651 u32 inside_fib_index;
653 /* values of various timeouts */
656 u32 tcp_transitory_timeout;
657 u32 tcp_established_timeout;
660 /* TCP MSS clamping */
664 /* counters/gauges */
665 vlib_simple_counter_main_t total_users;
666 vlib_simple_counter_main_t total_sessions;
668 /* API message ID base */
672 vlib_log_class_t log_class;
677 vlib_main_t *vlib_main;
678 vnet_main_t *vnet_main;
679 ip4_main_t *ip4_main;
680 ip_lookup_main_t *ip4_lookup_main;
681 api_main_t *api_main;
688 } nat44_is_idle_session_ctx_t;
692 u32 cached_sw_if_index;
693 u32 cached_ip4_address;
696 extern snat_main_t snat_main;
698 // nat pre ed next_node feature classification
699 extern vlib_node_registration_t nat_default_node;
700 extern vlib_node_registration_t nat_pre_in2out_node;
701 extern vlib_node_registration_t nat_pre_out2in_node;
703 extern vlib_node_registration_t snat_in2out_node;
704 extern vlib_node_registration_t snat_in2out_output_node;
705 extern vlib_node_registration_t snat_out2in_node;
706 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
707 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
708 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
709 extern vlib_node_registration_t snat_det_in2out_node;
710 extern vlib_node_registration_t snat_det_out2in_node;
711 extern vlib_node_registration_t nat44_ed_in2out_node;
712 extern vlib_node_registration_t nat44_ed_in2out_output_node;
713 extern vlib_node_registration_t nat44_ed_out2in_node;
715 extern fib_source_t nat_fib_src_hi;
716 extern fib_source_t nat_fib_src_low;
718 /* format functions */
719 format_function_t format_snat_user;
720 format_function_t format_snat_static_mapping;
721 format_function_t format_snat_static_map_to_resolve;
722 format_function_t format_snat_session;
723 format_function_t format_det_map_ses;
724 format_function_t format_snat_key;
725 format_function_t format_static_mapping_key;
726 format_function_t format_snat_protocol;
727 format_function_t format_nat_addr_and_port_alloc_alg;
728 /* unformat functions */
729 unformat_function_t unformat_snat_protocol;
731 /** \brief Check if SNAT session is created from static mapping.
732 @param s SNAT session
733 @return 1 if SNAT session is created from static mapping otherwise 0
735 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
737 /** \brief Check if SNAT session for unknown protocol.
738 @param s SNAT session
739 @return 1 if SNAT session for unknown protocol otherwise 0
741 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
743 /** \brief Check if NAT session is twice NAT.
745 @return 1 if NAT session is twice NAT
747 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
749 /** \brief Check if NAT session is load-balancing.
751 @return 1 if NAT session is load-balancing
753 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
755 /** \brief Check if NAT session is forwarding bypass.
757 @return 1 if NAT session is load-balancing
759 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
761 /** \brief Check if NAT session is endpoint dependent.
763 @return 1 if NAT session is endpoint dependent
765 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
767 /** \brief Check if NAT session has affinity record.
769 @return 1 if NAT session has affinity record
771 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
773 /** \brief Check if NAT interface is inside.
774 @param i NAT interface
775 @return 1 if inside interface
777 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
779 /** \brief Check if NAT interface is outside.
780 @param i NAT interface
781 @return 1 if outside interface
783 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
785 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
787 @return 1 if session is closed
789 #define nat44_is_ses_closed(s) s->state == 0xf
791 /** \brief Check if NAT static mapping is address only (1:1NAT).
792 @param sm NAT static mapping
793 @return 1 if 1:1NAT, 0 if 1:1NAPT
795 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
797 /** \brief Check if NAT static mapping match only out2in direction.
798 @param sm NAT static mapping
799 @return 1 if rule match only out2in direction
801 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
803 /** \brief Check if NAT static mapping is identity NAT.
804 @param sm NAT static mapping
805 @return 1 if identity NAT
807 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
809 /** \brief Check if NAT static mapping is load-balancing.
810 @param sm NAT static mapping
811 @return 1 if load-balancing
813 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
815 /** \brief Check if client initiating TCP connection (received SYN from client)
817 @return 1 if client initiating TCP connection
820 tcp_flags_is_init (u8 f)
822 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
826 #define nat_log_err(...) \
827 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
828 #define nat_log_warn(...) \
829 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
830 #define nat_log_notice(...) \
831 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
832 #define nat_log_info(...) \
833 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
834 #define nat_log_debug(...)\
835 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
837 /* NAT API Logging Levels */
838 #define foreach_nat_log_level \
841 _(0x02, LOG_WARNING) \
842 _(0x03, LOG_NOTICE) \
846 typedef enum nat_log_level_t_
848 #define _(n,f) SNAT_##f = n,
849 foreach_nat_log_level
853 #define nat_elog(_level, _str) \
856 snat_main_t *sm = &snat_main; \
857 if (PREDICT_FALSE (sm->log_level >= _level)) \
859 ELOG_TYPE_DECLARE (e) = \
861 .format = "nat-msg " _str, \
864 ELOG_DATA (&sm->vlib_main->elog_main, e); \
868 #define nat_elog_addr(_level, _str, _addr) \
871 if (PREDICT_FALSE (sm->log_level >= _level)) \
873 ELOG_TYPE_DECLARE (e) = \
875 .format = "nat-msg " _str " %d.%d.%d.%d", \
876 .format_args = "i1i1i1i1", \
885 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
886 ed->oct4 = _addr >> 24; \
887 ed->oct3 = _addr >> 16; \
888 ed->oct2 = _addr >> 8; \
893 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
896 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
898 ELOG_TYPE_DECLARE (e) = \
900 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
901 " tid from: %d to: %d fib: %d", \
902 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
918 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
919 ed->src_oct1 = _src >> 24; \
920 ed->src_oct2 = _src >> 16; \
921 ed->src_oct3 = _src >> 8; \
922 ed->src_oct4 = _src; \
923 ed->dst_oct1 = _dst >> 24; \
924 ed->dst_oct2 = _dst >> 16; \
925 ed->dst_oct3 = _dst >> 8; \
926 ed->dst_oct4 = _dst; \
927 ed->ftid = vlib_get_thread_index (); \
933 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
936 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
938 ELOG_TYPE_DECLARE (e) = \
940 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
941 " tid:%d prt:%d fib:%d", \
942 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
958 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
959 ed->src_oct1 = _src >> 24; \
960 ed->src_oct2 = _src >> 16; \
961 ed->src_oct3 = _src >> 8; \
962 ed->src_oct4 = _src; \
963 ed->dst_oct1 = _dst >> 24; \
964 ed->dst_oct2 = _dst >> 16; \
965 ed->dst_oct3 = _dst >> 8; \
966 ed->dst_oct4 = _dst; \
967 ed->tid = vlib_get_thread_index (); \
973 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
976 snat_main_t *sm = &snat_main; \
977 if (PREDICT_FALSE (sm->log_level >= _level)) \
979 ELOG_TYPE_DECLARE (e) = \
981 .format = "nat-msg " _fmt, \
982 .format_args = _arg, \
986 typeof (_val1) val1; \
988 ed = ELOG_DATA (&sm->vlib_main->elog_main, e); \
993 #define nat_elog_notice(nat_elog_str) \
994 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
995 #define nat_elog_warn(nat_elog_str) \
996 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
997 #define nat_elog_err(nat_elog_str) \
998 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
999 #define nat_elog_debug(nat_elog_str) \
1000 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1001 #define nat_elog_info(nat_elog_str) \
1002 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1004 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1005 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1006 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1007 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1008 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1009 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1010 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1011 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1012 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1013 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1015 /* ICMP session match functions */
1016 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1017 u32 thread_index, vlib_buffer_t * b0,
1018 ip4_header_t * ip0, u8 * p_proto,
1019 snat_session_key_t * p_value,
1020 u8 * p_dont_translate, void *d, void *e);
1021 u32 icmp_match_in2out_slow (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_out2in_fast (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_slow (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);
1037 /* ICMP deterministic NAT session match functions */
1038 u32 icmp_match_out2in_det (snat_main_t * sm, vlib_node_runtime_t * node,
1039 u32 thread_index, vlib_buffer_t * b0,
1040 ip4_header_t * ip0, u8 * p_proto,
1041 snat_session_key_t * p_value,
1042 u8 * p_dont_translate, void *d, void *e);
1043 u32 icmp_match_in2out_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);
1049 /* ICMP endpoint-dependent session match functions */
1050 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1051 u32 thread_index, vlib_buffer_t * b0,
1052 ip4_header_t * ip0, u8 * p_proto,
1053 snat_session_key_t * p_value,
1054 u8 * p_dont_translate, void *d, void *e);
1055 u32 icmp_match_in2out_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);
1061 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1062 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1063 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1066 u32 icmp_out2in (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 /* hairpinning functions */
1072 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1073 ip4_header_t * ip0, icmp46_header_t * icmp0,
1075 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1077 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1079 int snat_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1080 ip4_header_t * ip0, udp_header_t * udp0,
1081 tcp_header_t * tcp0, u32 proto0, int is_ed);
1083 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1084 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1085 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1086 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1087 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1090 * @brief Add external address to NAT44 pool
1092 * @param sm snat global configuration data
1093 * @param addr IPv4 address
1094 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1095 * @param twice_nat 1 if twice NAT address
1097 * @return 0 on success, non-zero value otherwise
1099 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1103 * @brief Delete external address from NAT44 pool
1105 * @param sm snat global configuration data
1106 * @param addr IPv4 address
1107 * @param delete_sm 1 if delete static mapping using address
1108 * @param twice_nat 1 if twice NAT address
1110 * @return 0 on success, non-zero value otherwise
1112 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1116 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1118 * @param addr IPv4 address
1119 * @param is_add 1 = add, 0 = delete
1121 * @return 0 on success, non-zero value otherwise
1123 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1126 * @brief Add/delete NAT44 static mapping
1128 * @param l_addr local IPv4 address
1129 * @param e_addr external IPv4 address
1130 * @param l_port local port number
1131 * @param e_port external port number
1132 * @param vrf_id local VRF ID
1133 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1134 * @param sw_if_index use interface address as external IPv4 address
1135 * @param proto L4 protocol
1136 * @param is_add 1 = add, 0 = delete
1137 * @param twice_nat twice-nat mode
1138 * @param out2in_only if 1 rule match only out2in direction
1139 * @param tag opaque string tag
1140 * @param identity_nat identity NAT
1142 * @return 0 on success, non-zero value otherwise
1144 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1145 u16 l_port, u16 e_port, u32 vrf_id,
1146 int addr_only, u32 sw_if_index,
1147 snat_protocol_t proto, int is_add,
1148 twice_nat_type_t twice_nat, u8 out2in_only,
1149 u8 * tag, u8 identity_nat);
1152 * @brief Add/delete static mapping with load-balancing (multiple backends)
1154 * @param e_addr external IPv4 address
1155 * @param e_port external port number
1156 * @param proto L4 protocol
1157 * @param locals list of local backends
1158 * @param is_add 1 = add, 0 = delete
1159 * @param twice_nat twice-nat mode
1160 * @param out2in_only if 1 rule match only out2in direction
1161 * @param tag opaque string tag
1162 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1164 * @return 0 on success, non-zero value otherwise
1166 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1167 snat_protocol_t proto,
1168 nat44_lb_addr_port_t * locals, u8 is_add,
1169 twice_nat_type_t twice_nat,
1170 u8 out2in_only, u8 * tag, u32 affinity);
1172 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1173 ip4_address_t l_addr, u16 l_port,
1174 snat_protocol_t proto, u32 vrf_id,
1175 u8 probability, u8 is_add);
1177 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1180 * @brief Set NAT plugin workers
1182 * @param bitmap NAT workers bitmap
1184 * @return 0 on success, non-zero value otherwise
1186 int snat_set_workers (uword * bitmap);
1189 * @brief Enable/disable NAT44 feature on the interface
1191 * @param sw_if_index software index of the interface
1192 * @param is_inside 1 = inside, 0 = outside
1193 * @param is_del 1 = delete, 0 = add
1195 * @return 0 on success, non-zero value otherwise
1197 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1200 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1202 * @param sw_if_index software index of the interface
1203 * @param is_inside 1 = inside, 0 = outside
1204 * @param is_del 1 = delete, 0 = add
1206 * @return 0 on success, non-zero value otherwise
1208 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1212 * @brief Add/delete NAT44 pool address from specific interface
1214 * @param sw_if_index software index of the interface
1215 * @param is_del 1 = delete, 0 = add
1216 * @param twice_nat 1 = twice NAT address for external hosts
1218 * @return 0 on success, non-zero value otherwise
1220 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1224 * @brief Delete NAT44 session
1226 * @param addr IPv4 address
1227 * @param port L4 port number
1228 * @param proto L4 protocol
1229 * @param vrf_id VRF ID
1230 * @param is_in 1 = inside network address and port pair, 0 = outside
1232 * @return 0 on success, non-zero value otherwise
1234 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1235 snat_protocol_t proto, u32 vrf_id, int is_in);
1238 * @brief Delete NAT44 endpoint-dependent session
1240 * @param sm snat global configuration data
1241 * @param addr IPv4 address
1242 * @param port L4 port number
1243 * @param proto L4 protocol
1244 * @param vrf_id VRF ID
1245 * @param is_in 1 = inside network address and port pair, 0 = outside
1247 * @return 0 on success, non-zero value otherwise
1249 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1250 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1251 u32 vrf_id, int is_in);
1254 * @brief Free NAT44 session data (lookup keys, external address port)
1256 * @param sm snat global configuration data
1257 * @param s NAT session
1258 * @param thread_index thread index
1259 * @param is_ha is HA event
1261 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1262 u32 thread_index, u8 is_ha);
1265 <<<<<<< bdfe5955f59a735fd8d70e9026f8c1867a4c8cc6
1266 * @brief Set NAT44 session limit (session limit, vrf id)
1268 * @param session_limit Session limit
1269 * @param vrf_id VRF id
1270 * @return 0 on success, non-zero value otherwise
1272 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1275 * @brief Free NAT44 ED session data (lookup keys, external addrres port)
1277 * @brief Free NAT44 ED session data (lookup keys, external address port)
1278 >>>>>>> docs: clean up make docs job
1280 * @param s NAT session
1281 * @param thread_index thread index
1282 * @param is_ha is HA event
1285 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1286 u32 thread_index, u8 is_ha);
1289 * @brief Initialize NAT44 data
1291 * @param tsm per thread data
1293 void nat44_db_init (snat_main_per_thread_data_t * tsm);
1296 * @brief Free NAT44 data
1298 * @param tsm per thread data
1300 void nat44_db_free (snat_main_per_thread_data_t * tsm);
1303 * @brief Find or create NAT user
1305 * @param sm snat global configuration data
1306 * @param addr IPv4 address
1307 * @param fib_index FIB table index
1308 * @param thread_index thread index
1310 * @return NAT user data structure on success otherwise zero value
1312 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1313 ip4_address_t * addr, u32 fib_index,
1317 * @brief Allocate new NAT session or recycle last used
1319 * @param sm snat global configuration data
1321 * @param thread_index thread index
1322 * @param now time now
1324 * @return session data structure on success otherwise zero value
1326 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1328 u32 thread_index, f64 now);
1331 * @brief Allocate NAT endpoint-dependent session
1333 * @param sm snat global configuration data
1334 * @param thread_index thread index
1335 * @param now time now
1337 * @return session data structure on success otherwise zero value
1339 snat_session_t *nat_ed_session_alloc (snat_main_t * sm, u32 thread_index,
1343 * @brief Set address and port assignment algorithm for MAP-E CE
1345 * @param psid Port Set Identifier value
1346 * @param psid_offset number of offset bits
1347 * @param psid_length length of PSID
1349 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1353 * @brief Set address and port assignment algorithm for port range
1355 * @param start_port beginning of the port range
1356 * @param end_port end of the port range
1358 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1361 * @brief Set address and port assignment algorithm to default/standard
1363 void nat_set_alloc_addr_and_port_default (void);
1366 * @brief Free outside address and port pair
1368 * @param addresses vector of outside addresses
1369 * @param thread_index thread index
1370 * @param k address, port and protocol
1372 void snat_free_outside_address_and_port (snat_address_t * addresses,
1374 snat_session_key_t * k);
1377 * @brief Alloc outside address and port
1379 * @param addresses vector of outside addresses
1380 * @param fib_index FIB table index
1381 * @param thread_index thread index
1382 * @param k allocated address and port pair
1383 * @param port_per_thread number of ports per thread
1384 * @param snat_thread_index NAT thread index
1386 * @return 0 on success, non-zero value otherwise
1388 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1391 snat_session_key_t * k,
1392 u16 port_per_thread,
1393 u32 snat_thread_index);
1396 * @brief Match NAT44 static mapping.
1398 * @param match address and port to match
1399 * @param mapping external/local address and port of the matched mapping
1400 * @param by_external if 0 match by local address otherwise match by external
1402 * @param is_addr_only 1 if matched mapping is address only
1403 * @param twice_nat matched mapping is twice NAT type
1404 * @param lb 1 if matched mapping is load-balanced
1405 * @param ext_host_addr external host address
1407 * @returns 0 if match found otherwise 1.
1409 int snat_static_mapping_match (snat_main_t * sm,
1410 snat_session_key_t match,
1411 snat_session_key_t * mapping,
1414 twice_nat_type_t * twice_nat,
1416 ip4_address_t * ext_host_addr,
1417 u8 * is_identity_nat);
1420 * @brief Add/del NAT address to FIB.
1422 * Add the external NAT address to the FIB as receive entries. This ensures
1423 * that VPP will reply to ARP for this address and we don't need to enable
1424 * proxy ARP on the outside interface.
1426 * @param addr IPv4 address
1427 * @param plen address prefix length
1428 * @param sw_if_index software index of the outside interface
1429 * @param is_add 0 = delete, 1 = add.
1431 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1432 u8 p_len, u32 sw_if_index, int is_add);
1435 * Why is this here? Because we don't need to touch this layer to
1436 * simply reply to an icmp. We need to change id to a unique
1437 * value to NAT an echo request/reply.
1444 } icmp_echo_header_t;
1448 u16 src_port, dst_port;
1451 int nat_global_lru_free_one (snat_main_t * sm, int thread_index, f64 now);
1453 #endif /* __included_nat_h__ */
1455 * fd.io coding-style-patch-verification: ON
1458 * eval: (c-set-style "gnu")