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)
51 } nat_buffer_opaque_t;
59 NAT_NEXT_IN2OUT_ED_FAST_PATH,
60 NAT_NEXT_IN2OUT_ED_SLOW_PATH,
61 NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
62 NAT_NEXT_OUT2IN_ED_FAST_PATH,
63 NAT_NEXT_OUT2IN_ED_SLOW_PATH,
72 #define nat_buffer_opaque(b) \
73 ((nat_buffer_opaque_t *)((vnet_buffer_opaque2_t *)b->opaque2)->__unused2)
76 STATIC_ASSERT (sizeof (nat_buffer_opaque_t) <=
77 STRUCT_SIZE_OF (vnet_buffer_opaque_t, unused),
78 "Custom meta-data too large for vnet_buffer_opaque_t");
80 #define nat_buffer_opaque(b) \
81 ((nat_buffer_opaque_t *)((u8 *)((b)->opaque) + \
82 STRUCT_OFFSET_OF (vnet_buffer_opaque_t, unused)))*/
84 /* session key (4-tuple) */
93 u16 protocol:3, fib_index:13;
99 /* endpoint-dependent session key (6-tuple) */
106 ip4_address_t l_addr;
107 ip4_address_t r_addr;
108 u32 proto:8, fib_index:24;
116 /* deterministic session outside key */
123 ip4_address_t ext_host_addr;
129 } snat_det_out_key_t;
131 /* user (internal host) key */
145 /* NAT API Configuration flags */
146 #define foreach_nat_config_flag \
147 _(0x01, IS_TWICE_NAT) \
148 _(0x02, IS_SELF_TWICE_NAT) \
149 _(0x04, IS_OUT2IN_ONLY) \
150 _(0x08, IS_ADDR_ONLY) \
151 _(0x10, IS_OUTSIDE) \
154 _(0x80, IS_EXT_HOST_VALID) \
156 typedef enum nat_config_flags_t_
158 #define _(n,f) NAT_API_##f = n,
159 foreach_nat_config_flag
161 } nat_config_flags_t;
163 /* External address and port allocation modes */
164 #define foreach_nat_addr_and_port_alloc_alg \
165 _(0, DEFAULT, "default") \
166 _(1, MAPE, "map-e") \
167 _(2, RANGE, "port-range")
171 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
172 foreach_nat_addr_and_port_alloc_alg
174 } nat_addr_and_port_alloc_alg_t;
177 /* Supported L4 protocols */
178 #define foreach_snat_protocol \
179 _(UDP, 0, udp, "udp") \
180 _(TCP, 1, tcp, "tcp") \
181 _(ICMP, 2, icmp, "icmp")
185 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
186 foreach_snat_protocol
192 #define foreach_snat_session_state \
193 _(0, UNKNOWN, "unknown") \
194 _(1, UDP_ACTIVE, "udp-active") \
195 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
196 _(3, TCP_ESTABLISHED, "tcp-established") \
197 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
198 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
199 _(6, TCP_CLOSING, "tcp-closing") \
200 _(7, TCP_LAST_ACK, "tcp-last-ack") \
201 _(8, TCP_CLOSED, "tcp-closed") \
202 _(9, ICMP_ACTIVE, "icmp-active")
206 #define _(v, N, s) SNAT_SESSION_##N = v,
207 foreach_snat_session_state
209 } snat_session_state_t;
211 #define foreach_nat_in2out_ed_error \
212 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
213 _(IN2OUT_PACKETS, "good in2out packets processed") \
214 _(OUT_OF_PORTS, "out of ports") \
215 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
216 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
217 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
218 _(DROP_FRAGMENT, "drop fragment") \
219 _(CANNOT_CREATE_USER, "cannot create NAT user") \
220 _(NON_SYN, "non-SYN packet try to create session") \
221 _(TCP_PACKETS, "TCP packets") \
222 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
223 _(UDP_PACKETS, "UDP packets") \
224 _(ICMP_PACKETS, "ICMP packets") \
225 _(OTHER_PACKETS, "other protocol packets") \
226 _(FRAGMENTS, "fragments") \
227 _(CACHED_FRAGMENTS, "cached fragments") \
228 _(PROCESSED_FRAGMENTS, "processed fragments")
232 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
233 foreach_nat_in2out_ed_error
235 NAT_IN2OUT_ED_N_ERROR,
236 } nat_in2out_ed_error_t;
238 #define foreach_nat_out2in_ed_error \
239 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
240 _(OUT2IN_PACKETS, "good out2in packets processed") \
241 _(OUT_OF_PORTS, "out of ports") \
242 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
243 _(NO_TRANSLATION, "no translation") \
244 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
245 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
246 _(DROP_FRAGMENT, "drop fragment") \
247 _(CANNOT_CREATE_USER, "cannot create NAT user") \
248 _(NON_SYN, "non-SYN packet try to create session") \
249 _(TCP_PACKETS, "TCP packets") \
250 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
251 _(UDP_PACKETS, "UDP packets") \
252 _(ICMP_PACKETS, "ICMP packets") \
253 _(OTHER_PACKETS, "other protocol packets") \
254 _(FRAGMENTS, "fragments") \
255 _(CACHED_FRAGMENTS, "cached fragments") \
256 _(PROCESSED_FRAGMENTS, "processed fragments")
260 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
261 foreach_nat_out2in_ed_error
263 NAT_OUT2IN_ED_N_ERROR,
264 } nat_out2in_ed_error_t;
267 /* Endpoint dependent TCP session state */
268 #define NAT44_SES_I2O_FIN 1
269 #define NAT44_SES_O2I_FIN 2
270 #define NAT44_SES_I2O_FIN_ACK 4
271 #define NAT44_SES_O2I_FIN_ACK 8
272 #define NAT44_SES_I2O_SYN 16
273 #define NAT44_SES_O2I_SYN 32
274 #define NAT44_SES_RST 64
277 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
278 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
279 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
280 #define SNAT_SESSION_FLAG_TWICE_NAT 8
281 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
282 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
283 #define SNAT_SESSION_FLAG_AFFINITY 64
284 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
286 /* NAT interface flags */
287 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
288 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
290 /* Static mapping flags */
291 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
292 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
293 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
294 #define NAT_STATIC_MAPPING_FLAG_LB 8
297 typedef CLIB_PACKED(struct
299 /* Outside network key */
300 snat_session_key_t out2in;
302 /* Inside network key */
303 snat_session_key_t in2out;
308 /* Per-user translations */
310 u32 per_user_list_head_index;
312 /* index in global LRU list */
313 u32 global_lru_index;
316 /* Last heard timer */
319 /* Last HA refresh */
320 f64 ha_last_refreshed;
326 /* External host address and port */
327 ip4_address_t ext_host_addr;
330 /* External host address and port after translation */
331 ip4_address_t ext_host_nat_addr;
332 u16 ext_host_nat_port;
334 /* TCP session state */
338 u32 tcp_close_timestamp;
350 u32 sessions_per_user_list_head_index;
360 #define _(N, i, n, s) \
361 u16 busy_##n##_ports; \
362 u16 * busy_##n##_ports_per_thread; \
363 u32 busy_##n##_port_refcounts[65535];
364 foreach_snat_protocol
377 /* Inside network port */
379 /* Outside network address and port */
380 snat_det_out_key_t out;
385 } snat_det_session_t;
389 /* inside IP address range */
390 ip4_address_t in_addr;
392 /* outside IP address range */
393 ip4_address_t out_addr;
395 /* inside IP addresses / outside IP addresses */
397 /* number of ports available to internal host */
399 /* session counter */
401 /* vector of sessions */
402 snat_det_session_t *sessions;
407 /* backend IP address */
409 /* backend port number */
411 /* probability of the backend to be randomly matched */
414 /* backend FIB table */
417 } nat44_lb_addr_port_t;
421 /* twice-nat disabled */
423 /* twice-nat enabled */
425 /* twice-nat only when src IP equals dst IP after translation */
431 /* no load-balancing */
435 /* load-balancing with affinity */
441 /* local IP address */
442 ip4_address_t local_addr;
443 /* external IP address */
444 ip4_address_t external_addr;
450 twice_nat_type_t twice_nat;
451 /* local FIB table */
455 snat_protocol_t proto;
456 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
458 /* worker threads used by backends/local host */
460 /* opaque string tag */
462 /* backends for load-balancing mode */
463 nat44_lb_addr_port_t *locals;
464 /* affinity per service lis */
465 u32 affinity_per_service_list_head_index;
468 } snat_static_mapping_t;
478 ip4_address_t l_addr;
483 snat_protocol_t proto;
491 } snat_static_map_resolve_t;
495 /* Main lookup tables */
496 clib_bihash_8_8_t out2in;
497 clib_bihash_8_8_t in2out;
499 /* Endpoint dependent sessions lookup tables */
500 clib_bihash_16_8_t out2in_ed;
501 clib_bihash_16_8_t in2out_ed;
503 /* Find-a-user => src address lookup */
504 clib_bihash_8_8_t user_hash;
510 snat_session_t *sessions;
512 /* Pool of doubly-linked list elements */
513 dlist_elt_t *list_pool;
515 /* LRU session list - head is stale, tail is fresh */
516 dlist_elt_t *global_lru_pool;
517 u32 global_lru_head_index;
519 /* NAT thread index */
520 u32 snat_thread_index;
522 /* real thread index */
525 } snat_main_per_thread_data_t;
529 /* ICMP session match function */
530 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
531 vlib_node_runtime_t * node,
534 ip4_header_t * ip0, u8 * p_proto,
535 snat_session_key_t * p_value,
536 u8 * p_dont_translate, void *d,
539 /* Return worker thread index for given packet */
540 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
544 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
549 /* NAT address and port allacotaion function */
550 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
554 snat_session_key_t * k,
556 u32 snat_thread_index);
558 typedef struct ed_bihash_key_s
567 typedef struct ed_bihash_kv_s
572 clib_bihash_kv_16_8_t kv;
576 STATIC_ASSERT (STRUCT_SIZE_OF (ed_bihash_kv_t, k) <=
577 STRUCT_SIZE_OF (ed_bihash_kv_t, kv.key),
578 "ed key needs to fit in bihash key");
580 typedef struct snat_main_s
582 /* ICMP session match functions */
583 snat_icmp_match_function_t *icmp_match_in2out_cb;
584 snat_icmp_match_function_t *icmp_match_out2in_cb;
586 /* Thread settings */
588 u32 first_worker_index;
590 snat_get_worker_in2out_function_t *worker_in2out_cb;
591 snat_get_worker_out2in_function_t *worker_out2in_cb;
595 /* Per thread data */
596 snat_main_per_thread_data_t *per_thread_data;
598 /* Find a static mapping by local */
599 clib_bihash_8_8_t static_mapping_by_local;
601 /* Find a static mapping by external */
602 clib_bihash_8_8_t static_mapping_by_external;
604 /* Static mapping pool */
605 snat_static_mapping_t *static_mappings;
608 snat_interface_t *interfaces;
609 snat_interface_t *output_feature_interfaces;
611 /* Vector of outside addresses */
612 snat_address_t *addresses;
613 /* Address and port allocation function */
614 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
615 /* Address and port allocation type */
616 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
617 /* Port set parameters (MAP-E) */
621 /* Port range parameters */
625 /* vector of outside fibs */
626 nat_outside_fib_t *outside_fibs;
628 /* Vector of twice NAT addresses for extenal hosts */
629 snat_address_t *twice_nat_addresses;
631 /* sw_if_indices whose intfc addresses should be auto-added */
632 u32 *auto_add_sw_if_indices;
633 u32 *auto_add_sw_if_indices_twice_nat;
635 /* vector of interface address static mappings to resolve. */
636 snat_static_map_resolve_t *to_resolve;
638 /* Randomize port allocation order */
641 /* Worker handoff frame-queue index */
643 u32 fq_in2out_output_index;
647 u32 error_node_index;
649 /* handoff fq nodes */
650 u32 handoff_out2in_index;
651 u32 handoff_in2out_index;
652 u32 handoff_in2out_output_index;
654 /* respect feature arc nodes */
655 u32 pre_out2in_node_index;
656 u32 pre_in2out_node_index;
658 u32 in2out_node_index;
659 u32 in2out_output_node_index;
660 u32 in2out_fast_node_index;
661 u32 in2out_slowpath_node_index;
662 u32 in2out_slowpath_output_node_index;
663 u32 ed_in2out_node_index;
664 u32 ed_in2out_slowpath_node_index;
665 u32 out2in_node_index;
666 u32 out2in_fast_node_index;
667 u32 ed_out2in_node_index;
668 u32 ed_out2in_slowpath_node_index;
669 u32 det_in2out_node_index;
670 u32 det_out2in_node_index;
672 u32 hairpinning_node_index;
673 u32 hairpin_dst_node_index;
674 u32 hairpin_src_node_index;
675 u32 ed_hairpinning_node_index;
676 u32 ed_hairpin_dst_node_index;
677 u32 ed_hairpin_src_node_index;
680 /* Deterministic NAT mappings */
681 snat_det_map_t *det_maps;
683 /* If forwarding is enabled */
684 u8 forwarding_enabled;
686 /* Config parameters */
687 u8 static_mapping_only;
688 u8 static_mapping_connection_tracking;
691 u8 endpoint_dependent;
692 u32 translation_buckets;
693 uword translation_memory_size;
694 u32 max_translations;
696 uword user_memory_size;
697 u32 max_translations_per_user;
699 u32 outside_fib_index;
701 u32 inside_fib_index;
703 /* values of various timeouts */
706 u32 tcp_transitory_timeout;
707 u32 tcp_established_timeout;
710 /* TCP MSS clamping */
714 /* counters/gauges */
715 vlib_simple_counter_main_t total_users;
716 vlib_simple_counter_main_t total_sessions;
718 /* API message ID base */
722 vlib_log_class_t log_class;
727 vlib_main_t *vlib_main;
728 vnet_main_t *vnet_main;
729 ip4_main_t *ip4_main;
730 ip_lookup_main_t *ip4_lookup_main;
731 api_main_t *api_main;
733 clib_bihash_16_8_t ed_ext_ports;
740 } nat44_is_idle_session_ctx_t;
744 u32 cached_sw_if_index;
745 u32 cached_ip4_address;
748 extern snat_main_t snat_main;
750 // nat pre ed next_node feature classification
751 extern vlib_node_registration_t nat_default_node;
752 extern vlib_node_registration_t nat_pre_in2out_node;
753 extern vlib_node_registration_t nat_pre_out2in_node;
755 extern vlib_node_registration_t snat_in2out_node;
756 extern vlib_node_registration_t snat_in2out_output_node;
757 extern vlib_node_registration_t snat_out2in_node;
758 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
759 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
760 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
761 extern vlib_node_registration_t snat_det_in2out_node;
762 extern vlib_node_registration_t snat_det_out2in_node;
763 extern vlib_node_registration_t nat44_ed_in2out_node;
764 extern vlib_node_registration_t nat44_ed_in2out_output_node;
765 extern vlib_node_registration_t nat44_ed_out2in_node;
767 extern fib_source_t nat_fib_src_hi;
768 extern fib_source_t nat_fib_src_low;
770 /* format functions */
771 format_function_t format_snat_user;
772 format_function_t format_snat_user_v2;
773 format_function_t format_snat_static_mapping;
774 format_function_t format_snat_static_map_to_resolve;
775 format_function_t format_snat_session;
776 format_function_t format_det_map_ses;
777 format_function_t format_snat_key;
778 format_function_t format_static_mapping_key;
779 format_function_t format_snat_protocol;
780 format_function_t format_nat_addr_and_port_alloc_alg;
781 /* unformat functions */
782 unformat_function_t unformat_snat_protocol;
784 /** \brief Check if SNAT session is created from static mapping.
785 @param s SNAT session
786 @return 1 if SNAT session is created from static mapping otherwise 0
788 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
790 /** \brief Check if SNAT session for unknown protocol.
791 @param s SNAT session
792 @return 1 if SNAT session for unknown protocol otherwise 0
794 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
796 /** \brief Check if NAT session is twice NAT.
798 @return 1 if NAT session is twice NAT
800 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
802 /** \brief Check if NAT session is load-balancing.
804 @return 1 if NAT session is load-balancing
806 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
808 /** \brief Check if NAT session is forwarding bypass.
810 @return 1 if NAT session is load-balancing
812 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
814 /** \brief Check if NAT session is endpoint dependent.
816 @return 1 if NAT session is endpoint dependent
818 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
820 /** \brief Check if NAT session has affinity record.
822 @return 1 if NAT session has affinity record
824 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
826 /** \brief Check if NAT interface is inside.
827 @param i NAT interfce
828 @return 1 if inside interface
830 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
832 /** \brief Check if NAT interface is outside.
833 @param i NAT interfce
834 @return 1 if outside interface
836 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
838 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
840 @return 1 if session is closed
842 #define nat44_is_ses_closed(s) s->state == 0xf
844 /** \brief Check if NAT static mapping is address only (1:1NAT).
845 @param sm NAT static mapping
846 @return 1 if 1:1NAT, 0 if 1:1NAPT
848 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
850 /** \brief Check if NAT static mapping match only out2in direction.
851 @param sm NAT static mapping
852 @return 1 if rule match only out2in direction
854 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
856 /** \brief Check if NAT static mapping is identity NAT.
857 @param sm NAT static mapping
858 @return 1 if identity NAT
860 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
862 /** \brief Check if NAT static mapping is load-balancing.
863 @param sm NAT static mapping
864 @return 1 if load-balancing
866 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
868 /** \brief Check if client initiating TCP connection (received SYN from client)
870 @return 1 if client initiating TCP connection
873 tcp_flags_is_init (u8 f)
875 return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
879 #define nat_log_err(...) \
880 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
881 #define nat_log_warn(...) \
882 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
883 #define nat_log_notice(...) \
884 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
885 #define nat_log_info(...) \
886 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
887 #define nat_log_debug(...)\
888 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
890 /* NAT API Logging Levels */
891 #define foreach_nat_log_level \
894 _(0x02, LOG_WARNING) \
895 _(0x03, LOG_NOTICE) \
899 typedef enum nat_log_level_t_
901 #define _(n,f) SNAT_##f = n,
902 foreach_nat_log_level
906 #define nat_elog(_level, _str) \
909 snat_main_t *sm = &snat_main; \
910 if (PREDICT_FALSE (sm->log_level >= _level)) \
912 ELOG_TYPE_DECLARE (e) = \
914 .format = "nat-msg " _str, \
917 ELOG_DATA (&sm->vlib_main->elog_main, e); \
921 #define nat_elog_addr(_level, _str, _addr) \
924 if (PREDICT_FALSE (sm->log_level >= _level)) \
926 ELOG_TYPE_DECLARE (e) = \
928 .format = "nat-msg " _str " %d.%d.%d.%d", \
929 .format_args = "i1i1i1i1", \
938 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
939 ed->oct4 = _addr >> 24; \
940 ed->oct3 = _addr >> 16; \
941 ed->oct2 = _addr >> 8; \
946 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
949 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
951 ELOG_TYPE_DECLARE (e) = \
953 .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
954 " tid from: %d to: %d fib: %d", \
955 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
971 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
972 ed->src_oct1 = _src >> 24; \
973 ed->src_oct2 = _src >> 16; \
974 ed->src_oct3 = _src >> 8; \
975 ed->src_oct4 = _src; \
976 ed->dst_oct1 = _dst >> 24; \
977 ed->dst_oct2 = _dst >> 16; \
978 ed->dst_oct3 = _dst >> 8; \
979 ed->dst_oct4 = _dst; \
980 ed->ftid = vlib_get_thread_index (); \
986 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
989 if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
991 ELOG_TYPE_DECLARE (e) = \
993 .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
994 " tid:%d prt:%d fib:%d", \
995 .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
1011 ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
1012 ed->src_oct1 = _src >> 24; \
1013 ed->src_oct2 = _src >> 16; \
1014 ed->src_oct3 = _src >> 8; \
1015 ed->src_oct4 = _src; \
1016 ed->dst_oct1 = _dst >> 24; \
1017 ed->dst_oct2 = _dst >> 16; \
1018 ed->dst_oct3 = _dst >> 8; \
1019 ed->dst_oct4 = _dst; \
1020 ed->tid = vlib_get_thread_index (); \
1026 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
1029 snat_main_t *sm = &snat_main; \
1030 if (PREDICT_FALSE (sm->log_level >= _level)) \
1032 ELOG_TYPE_DECLARE (e) = \
1034 .format = "nat-msg " _fmt, \
1035 .format_args = _arg, \
1037 CLIB_PACKED(struct \
1039 typeof (_val1) val1; \
1041 ed = ELOG_DATA (&sm->vlib_main->elog_main, e); \
1046 #define nat_elog_notice(nat_elog_str) \
1047 nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
1048 #define nat_elog_warn(nat_elog_str) \
1049 nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1050 #define nat_elog_err(nat_elog_str) \
1051 nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1052 #define nat_elog_debug(nat_elog_str) \
1053 nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1054 #define nat_elog_info(nat_elog_str) \
1055 nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1057 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1058 nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1059 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1060 nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1061 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1062 nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1063 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1064 nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1065 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1066 nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1068 /* ICMP session match functions */
1069 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1070 u32 thread_index, vlib_buffer_t * b0,
1071 ip4_header_t * ip0, u8 * p_proto,
1072 snat_session_key_t * p_value,
1073 u8 * p_dont_translate, void *d, void *e);
1074 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1075 u32 thread_index, vlib_buffer_t * b0,
1076 ip4_header_t * ip0, u8 * p_proto,
1077 snat_session_key_t * p_value,
1078 u8 * p_dont_translate, void *d, void *e);
1079 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1080 u32 thread_index, vlib_buffer_t * b0,
1081 ip4_header_t * ip0, u8 * p_proto,
1082 snat_session_key_t * p_value,
1083 u8 * p_dont_translate, void *d, void *e);
1084 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1085 u32 thread_index, vlib_buffer_t * b0,
1086 ip4_header_t * ip0, u8 * p_proto,
1087 snat_session_key_t * p_value,
1088 u8 * p_dont_translate, void *d, void *e);
1090 /* ICMP deterministic NAT session match functions */
1091 u32 icmp_match_out2in_det (snat_main_t * sm, vlib_node_runtime_t * node,
1092 u32 thread_index, vlib_buffer_t * b0,
1093 ip4_header_t * ip0, u8 * p_proto,
1094 snat_session_key_t * p_value,
1095 u8 * p_dont_translate, void *d, void *e);
1096 u32 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
1097 u32 thread_index, vlib_buffer_t * b0,
1098 ip4_header_t * ip0, u8 * p_proto,
1099 snat_session_key_t * p_value,
1100 u8 * p_dont_translate, void *d, void *e);
1102 /* ICMP endpoint-dependent session match functions */
1103 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1104 u32 thread_index, vlib_buffer_t * b0,
1105 ip4_header_t * ip0, u8 * p_proto,
1106 snat_session_key_t * p_value,
1107 u8 * p_dont_translate, void *d, void *e);
1108 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1109 u32 thread_index, vlib_buffer_t * b0,
1110 ip4_header_t * ip0, u8 * p_proto,
1111 snat_session_key_t * p_value,
1112 u8 * p_dont_translate, void *d, void *e);
1114 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1115 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1116 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1119 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1120 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1121 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1124 /* hairpinning functions */
1125 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1126 ip4_header_t * ip0, icmp46_header_t * icmp0,
1128 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1130 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1132 int snat_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1133 ip4_header_t * ip0, udp_header_t * udp0,
1134 tcp_header_t * tcp0, u32 proto0, int is_ed);
1136 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1137 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1138 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1139 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1140 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1143 * @brief Add external address to NAT44 pool
1145 * @param addr IPv4 address
1146 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1147 * @param twice_nat 1 if twice NAT address
1149 * @return 0 on success, non-zero value otherwise
1151 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1155 * @brief Delete external address from NAT44 pool
1157 * @param addr IPv4 address
1158 * @param delete_sm 1 if delete static mapping using address
1159 * @param twice_nat 1 if twice NAT address
1161 * @return 0 on success, non-zero value otherwise
1163 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1167 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1169 * @param addr IPv4 address
1170 * @param is_add 1 = add, 0 = delete
1172 * @return 0 on success, non-zero value otherwise
1174 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1177 * @brief Add/delete NAT44 static mapping
1179 * @param l_addr local IPv4 address
1180 * @param e_addr external IPv4 address
1181 * @param l_port local port number
1182 * @param e_port external port number
1183 * @param vrf_id local VRF ID
1184 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1185 * @param sw_if_index use interface address as external IPv4 address
1186 * @param proto L4 protocol
1187 * @param is_add 1 = add, 0 = delete
1188 * @param twice_nat twice-nat mode
1189 * @param out2in_only if 1 rule match only out2in direction
1190 * @param tag opaque string tag
1191 * @param identity_nat identity NAT
1193 * @return 0 on success, non-zero value otherwise
1195 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1196 u16 l_port, u16 e_port, u32 vrf_id,
1197 int addr_only, u32 sw_if_index,
1198 snat_protocol_t proto, int is_add,
1199 twice_nat_type_t twice_nat, u8 out2in_only,
1200 u8 * tag, u8 identity_nat);
1203 * @brief Add/delete static mapping with load-balancing (multiple backends)
1205 * @param e_addr external IPv4 address
1206 * @param e_port external port number
1207 * @param proto L4 protocol
1208 * @param locals list of local backends
1209 * @param is_add 1 = add, 0 = delete
1210 * @param twice_nat twice-nat mode
1211 * @param out2in_only if 1 rule match only out2in direction
1212 * @param tag opaque string tag
1213 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1215 * @return 0 on success, non-zero value otherwise
1217 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1218 snat_protocol_t proto,
1219 nat44_lb_addr_port_t * locals, u8 is_add,
1220 twice_nat_type_t twice_nat,
1221 u8 out2in_only, u8 * tag, u32 affinity);
1223 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1224 ip4_address_t l_addr, u16 l_port,
1225 snat_protocol_t proto, u32 vrf_id,
1226 u8 probability, u8 is_add);
1228 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1231 * @brief Set NAT plugin workers
1233 * @param bitmap NAT workers bitmap
1235 * @return 0 on success, non-zero value otherwise
1237 int snat_set_workers (uword * bitmap);
1240 * @brief Enable/disable NAT44 feature on the interface
1242 * @param sw_if_index software index of the interface
1243 * @param is_inside 1 = inside, 0 = outside
1244 * @param is_del 1 = delete, 0 = add
1246 * @return 0 on success, non-zero value otherwise
1248 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1251 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1253 * @param sw_if_index software index of the interface
1254 * @param is_inside 1 = inside, 0 = outside
1255 * @param is_del 1 = delete, 0 = add
1257 * @return 0 on success, non-zero value otherwise
1259 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1263 * @brief Add/delete NAT44 pool address from specific interfce
1265 * @param sw_if_index software index of the interface
1266 * @param is_del 1 = delete, 0 = add
1267 * @param twice_nat 1 = twice NAT address for extenal hosts
1269 * @return 0 on success, non-zero value otherwise
1271 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1275 * @brief Delete NAT44 session
1277 * @param addr IPv4 address
1278 * @param port L4 port number
1279 * @param proto L4 protocol
1280 * @param vrf_id VRF ID
1281 * @param is_in 1 = inside network address and port pair, 0 = outside
1283 * @return 0 on success, non-zero value otherwise
1285 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1286 snat_protocol_t proto, u32 vrf_id, int is_in);
1289 * @brief Delete NAT44 endpoint-dependent session
1291 * @param addr IPv4 address
1292 * @param port L4 port number
1293 * @param proto L4 protocol
1294 * @param vrf_id VRF ID
1295 * @param is_in 1 = inside network address and port pair, 0 = outside
1297 * @return 0 on success, non-zero value otherwise
1299 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1300 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1301 u32 vrf_id, int is_in);
1304 * @brief Free NAT44 session data (lookup keys, external addrres port)
1306 * @param s NAT session
1307 * @param thread_index thread index
1308 * @param is_ha is HA event
1310 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1311 u32 thread_index, u8 is_ha);
1314 * @brief Free NAT44 ED session data (lookup keys, external addrres port)
1316 * @param s NAT session
1317 * @param thread_index thread index
1318 * @param is_ha is HA event
1321 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1322 u32 thread_index, u8 is_ha);
1324 * @brief Find or create NAT user
1326 * @param addr IPv4 address
1327 * @param fib_index FIB table index
1328 * @param thread_index thread index
1330 * @return NAT user data structure on success otherwise zero value
1332 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1333 ip4_address_t * addr, u32 fib_index,
1337 * @brief Allocate new NAT session or recycle last used
1340 * @param thread_index thread index
1342 * @return session data structure on success otherwise zero value
1344 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1346 u32 thread_index, f64 now);
1349 * @brief Allocate NAT endpoint-dependent session
1352 * @param thread_index thread index
1354 * @return session data structure on success otherwise zero value
1356 snat_session_t *nat_ed_session_alloc (snat_main_t * sm, snat_user_t * u,
1357 u32 thread_index, f64 now);
1360 * @brief Set address and port assignment algorithm for MAP-E CE
1362 * @param psid Port Set Identifier value
1363 * @param psid_offset number of offset bits
1364 * @param psid_length length of PSID
1366 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1370 * @brief Set address and port assignment algorithm for port range
1372 * @param start_port beginning of the port range
1373 * @param end_port end of the port range
1375 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1378 * @brief Set address and port assignment algorithm to default/standard
1380 void nat_set_alloc_addr_and_port_default (void);
1383 * @brief Free outside address and port pair
1385 * @param addresses vector of outside addresses
1386 * @param thread_index thread index
1387 * @param k address, port and protocol
1389 void snat_free_outside_address_and_port (snat_address_t * addresses,
1391 snat_session_key_t * k);
1394 * @brief Alloc outside address and port
1396 * @param addresses vector of outside addresses
1397 * @param fib_index FIB table index
1398 * @param thread_index thread index
1399 * @param k allocated address and port pair
1400 * @param port_per_thread number of ports per threead
1401 * @param snat_thread_index NAT thread index
1403 * @return 0 on success, non-zero value otherwise
1405 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1408 snat_session_key_t * k,
1409 u16 port_per_thread,
1410 u32 snat_thread_index);
1413 * @brief Match NAT44 static mapping.
1415 * @param match address and port to match
1416 * @param mapping external/local address and port of the matched mapping
1417 * @param by_external if 0 match by local address otherwise match by external
1419 * @param is_addr_only 1 if matched mapping is address only
1420 * @param twice_nat matched mapping is twice NAT type
1421 * @param lb 1 if matched mapping is load-balanced
1422 * @param ext_host_addr external host address
1424 * @returns 0 if match found otherwise 1.
1426 int snat_static_mapping_match (snat_main_t * sm,
1427 snat_session_key_t match,
1428 snat_session_key_t * mapping,
1431 twice_nat_type_t * twice_nat,
1433 ip4_address_t * ext_host_addr,
1434 u8 * is_identity_nat);
1437 * @brief Add/del NAT address to FIB.
1439 * Add the external NAT address to the FIB as receive entries. This ensures
1440 * that VPP will reply to ARP for this address and we don't need to enable
1441 * proxy ARP on the outside interface.
1443 * @param addr IPv4 address
1444 * @param plen address prefix length
1445 * @param sw_if_index software index of the outside interface
1446 * @param is_add 0 = delete, 1 = add.
1448 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1449 u8 p_len, u32 sw_if_index, int is_add);
1452 * Why is this here? Because we don't need to touch this layer to
1453 * simply reply to an icmp. We need to change id to a unique
1454 * value to NAT an echo request/reply.
1461 } icmp_echo_header_t;
1465 u16 src_port, dst_port;
1468 #endif /* __included_nat_h__ */
1470 * fd.io coding-style-patch-verification: ON
1473 * eval: (c-set-style "gnu")