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/bihash_8_8.h>
27 #include <vppinfra/bihash_16_8.h>
28 #include <vppinfra/dlist.h>
29 #include <vppinfra/error.h>
30 #include <vlibapi/api.h>
33 /* default session timeouts */
34 #define SNAT_UDP_TIMEOUT 300
35 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
36 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
37 #define SNAT_ICMP_TIMEOUT 60
39 /* number of worker handoff frame queue elements */
40 #define NAT_FQ_NELTS 64
42 /* NAT buffer flags */
43 #define SNAT_FLAG_HAIRPINNING (1 << 0)
45 /* session key (4-tuple) */
54 u16 protocol:3, fib_index:13;
60 /* endpoint-dependent session key (6-tuple) */
69 u32 proto:8, fib_index:24;
77 /* deterministic session outside key */
84 ip4_address_t ext_host_addr;
92 /* user (internal host) key */
111 } nat44_reass_trace_t;
113 /* External address and port allocation modes */
114 #define foreach_nat_addr_and_port_alloc_alg \
115 _(0, DEFAULT, "default") \
116 _(1, MAPE, "map-e") \
117 _(2, RANGE, "port-range")
121 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
122 foreach_nat_addr_and_port_alloc_alg
124 } nat_addr_and_port_alloc_alg_t;
127 /* Supported L4 protocols */
128 #define foreach_snat_protocol \
129 _(UDP, 0, udp, "udp") \
130 _(TCP, 1, tcp, "tcp") \
131 _(ICMP, 2, icmp, "icmp")
135 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
136 foreach_snat_protocol
142 #define foreach_snat_session_state \
143 _(0, UNKNOWN, "unknown") \
144 _(1, UDP_ACTIVE, "udp-active") \
145 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
146 _(3, TCP_ESTABLISHED, "tcp-established") \
147 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
148 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
149 _(6, TCP_CLOSING, "tcp-closing") \
150 _(7, TCP_LAST_ACK, "tcp-last-ack") \
151 _(8, TCP_CLOSED, "tcp-closed") \
152 _(9, ICMP_ACTIVE, "icmp-active")
156 #define _(v, N, s) SNAT_SESSION_##N = v,
157 foreach_snat_session_state
159 } snat_session_state_t;
161 /* Endpoint dependent TCP session state */
162 #define NAT44_SES_I2O_FIN 1
163 #define NAT44_SES_O2I_FIN 2
164 #define NAT44_SES_I2O_FIN_ACK 4
165 #define NAT44_SES_O2I_FIN_ACK 8
166 #define NAT44_SES_I2O_SYN 16
167 #define NAT44_SES_O2I_SYN 32
168 #define NAT44_SES_RST 64
171 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
172 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
173 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
174 #define SNAT_SESSION_FLAG_TWICE_NAT 8
175 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
176 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
177 #define SNAT_SESSION_FLAG_AFFINITY 64
178 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
180 /* NAT interface flags */
181 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
182 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
184 /* Static mapping flags */
185 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
186 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
187 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
188 #define NAT_STATIC_MAPPING_FLAG_LB 8
191 typedef CLIB_PACKED(struct
193 /* Outside network key */
194 snat_session_key_t out2in;
196 /* Inside network key */
197 snat_session_key_t in2out;
202 /* Per-user translations */
204 u32 per_user_list_head_index;
206 /* Last heard timer */
213 /* External host address and port */
214 ip4_address_t ext_host_addr;
217 /* External host address and port after translation */
218 ip4_address_t ext_host_nat_addr;
219 u16 ext_host_nat_port;
221 /* TCP session state */
236 u32 sessions_per_user_list_head_index;
246 #define _(N, i, n, s) \
247 u16 busy_##n##_ports; \
248 u16 * busy_##n##_ports_per_thread; \
249 uword * busy_##n##_port_bitmap;
250 foreach_snat_protocol
263 /* Inside network port */
265 /* Outside network address and port */
266 snat_det_out_key_t out;
271 } snat_det_session_t;
275 /* inside IP address range */
276 ip4_address_t in_addr;
278 /* outside IP address range */
279 ip4_address_t out_addr;
281 /* inside IP addresses / outside IP addresses */
283 /* number of ports available to internal host */
285 /* session counter */
287 /* vector of sessions */
288 snat_det_session_t *sessions;
293 /* backend IP address */
295 /* backend port number */
297 /* probability of the backend to be randomly matched */
300 /* backend FIB table */
303 } nat44_lb_addr_port_t;
307 /* twice-nat disabled */
309 /* twice-nat enabled */
311 /* twice-nat only when src IP equals dst IP after translation */
317 /* no load-balancing */
321 /* load-balancing with affinity */
327 /* local IP address */
328 ip4_address_t local_addr;
329 /* external IP address */
330 ip4_address_t external_addr;
336 twice_nat_type_t twice_nat;
337 /* local FIB table */
341 snat_protocol_t proto;
342 /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
344 /* worker threads used by backends/local host */
346 /* opaque string tag */
348 /* backends for load-balancing mode */
349 nat44_lb_addr_port_t *locals;
350 /* affinity per service lis */
351 u32 affinity_per_service_list_head_index;
354 } snat_static_mapping_t;
364 ip4_address_t l_addr;
369 snat_protocol_t proto;
377 } snat_static_map_resolve_t;
381 /* Main lookup tables */
382 clib_bihash_8_8_t out2in;
383 clib_bihash_8_8_t in2out;
385 /* Endpoint dependent sessions lookup tables */
386 clib_bihash_16_8_t out2in_ed;
387 clib_bihash_16_8_t in2out_ed;
389 /* Find-a-user => src address lookup */
390 clib_bihash_8_8_t user_hash;
396 snat_session_t *sessions;
398 /* Pool of doubly-linked list elements */
399 dlist_elt_t *list_pool;
401 /* NAT thread index */
402 u32 snat_thread_index;
403 } snat_main_per_thread_data_t;
407 /* ICMP session match function */
408 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
409 vlib_node_runtime_t * node,
412 ip4_header_t * ip0, u8 * p_proto,
413 snat_session_key_t * p_value,
414 u8 * p_dont_translate, void *d,
417 /* Return worker thread index for given packet */
418 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip,
421 /* NAT address and port allacotaion function */
422 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
426 snat_session_key_t * k,
428 u32 snat_thread_index);
430 typedef struct snat_main_s
432 /* ICMP session match functions */
433 snat_icmp_match_function_t *icmp_match_in2out_cb;
434 snat_icmp_match_function_t *icmp_match_out2in_cb;
436 /* Thread settings */
438 u32 first_worker_index;
440 snat_get_worker_function_t *worker_in2out_cb;
441 snat_get_worker_function_t *worker_out2in_cb;
445 /* Per thread data */
446 snat_main_per_thread_data_t *per_thread_data;
448 /* Find a static mapping by local */
449 clib_bihash_8_8_t static_mapping_by_local;
451 /* Find a static mapping by external */
452 clib_bihash_8_8_t static_mapping_by_external;
454 /* Static mapping pool */
455 snat_static_mapping_t *static_mappings;
458 snat_interface_t *interfaces;
459 snat_interface_t *output_feature_interfaces;
461 /* Vector of outside addresses */
462 snat_address_t *addresses;
463 /* Address and port allocation function */
464 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
465 /* Address and port allocation type */
466 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
467 /* Port set parameters (MAP-E) */
471 /* Port range parameters */
475 /* vector of outside fibs */
476 nat_outside_fib_t *outside_fibs;
478 /* Vector of twice NAT addresses for extenal hosts */
479 snat_address_t *twice_nat_addresses;
481 /* sw_if_indices whose intfc addresses should be auto-added */
482 u32 *auto_add_sw_if_indices;
483 u32 *auto_add_sw_if_indices_twice_nat;
485 /* vector of interface address static mappings to resolve. */
486 snat_static_map_resolve_t *to_resolve;
488 /* Randomize port allocation order */
491 /* Worker handoff frame-queue index */
493 u32 fq_in2out_output_index;
496 /* in2out and out2in node index */
497 u32 in2out_node_index;
498 u32 in2out_output_node_index;
499 u32 out2in_node_index;
500 u32 error_node_index;
502 /* Deterministic NAT mappings */
503 snat_det_map_t *det_maps;
505 /* If forwarding is enabled */
506 u8 forwarding_enabled;
508 /* Config parameters */
509 u8 static_mapping_only;
510 u8 static_mapping_connection_tracking;
513 u8 endpoint_dependent;
514 u32 translation_buckets;
515 u32 translation_memory_size;
516 u32 max_translations;
518 u32 user_memory_size;
519 u32 max_translations_per_user;
521 u32 outside_fib_index;
523 u32 inside_fib_index;
525 /* values of various timeouts */
527 u32 tcp_established_timeout;
528 u32 tcp_transitory_timeout;
531 /* TCP MSS clamping */
535 /* counters/gauges */
536 vlib_simple_counter_main_t total_users;
537 vlib_simple_counter_main_t total_sessions;
539 /* API message ID base */
543 vlib_log_class_t log_class;
546 vlib_main_t *vlib_main;
547 vnet_main_t *vnet_main;
548 ip4_main_t *ip4_main;
549 ip_lookup_main_t *ip4_lookup_main;
550 api_main_t *api_main;
557 } nat44_is_idle_session_ctx_t;
561 u32 cached_sw_if_index;
562 u32 cached_ip4_address;
565 extern snat_main_t snat_main;
566 extern vlib_node_registration_t snat_in2out_node;
567 extern vlib_node_registration_t snat_in2out_output_node;
568 extern vlib_node_registration_t snat_out2in_node;
569 extern vlib_node_registration_t snat_in2out_fast_node;
570 extern vlib_node_registration_t snat_out2in_fast_node;
571 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
572 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
573 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
574 extern vlib_node_registration_t snat_det_in2out_node;
575 extern vlib_node_registration_t snat_det_out2in_node;
576 extern vlib_node_registration_t snat_hairpin_dst_node;
577 extern vlib_node_registration_t snat_hairpin_src_node;
578 extern vlib_node_registration_t nat44_ed_in2out_node;
579 extern vlib_node_registration_t nat44_ed_in2out_output_node;
580 extern vlib_node_registration_t nat44_ed_out2in_node;
581 extern vlib_node_registration_t nat44_ed_hairpin_dst_node;
582 extern vlib_node_registration_t nat44_ed_hairpin_src_node;
583 extern vlib_node_registration_t nat44_ed_in2out_worker_handoff_node;
584 extern vlib_node_registration_t nat44_ed_in2out_output_worker_handoff_node;
585 extern vlib_node_registration_t nat44_ed_out2in_worker_handoff_node;
587 /* format functions */
588 format_function_t format_snat_user;
589 format_function_t format_snat_static_mapping;
590 format_function_t format_snat_static_map_to_resolve;
591 format_function_t format_snat_session;
592 format_function_t format_det_map_ses;
593 format_function_t format_snat_key;
594 format_function_t format_static_mapping_key;
595 format_function_t format_snat_protocol;
596 format_function_t format_nat_addr_and_port_alloc_alg;
597 format_function_t format_nat44_reass_trace;
598 /* unformat functions */
599 unformat_function_t unformat_snat_protocol;
601 /** \brief Check if SNAT session is created from static mapping.
602 @param s SNAT session
603 @return 1 if SNAT session is created from static mapping otherwise 0
605 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
607 /** \brief Check if SNAT session for unknown protocol.
608 @param s SNAT session
609 @return 1 if SNAT session for unknown protocol otherwise 0
611 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
613 /** \brief Check if NAT session is twice NAT.
615 @return 1 if NAT session is twice NAT
617 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
619 /** \brief Check if NAT session is load-balancing.
621 @return 1 if NAT session is load-balancing
623 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
625 /** \brief Check if NAT session is forwarding bypass.
627 @return 1 if NAT session is load-balancing
629 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
631 /** \brief Check if NAT session is endpoint dependent.
633 @return 1 if NAT session is endpoint dependent
635 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
637 /** \brief Check if NAT session has affinity record.
639 @return 1 if NAT session has affinity record
641 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
643 /** \brief Check if NAT interface is inside.
644 @param i NAT interfce
645 @return 1 if inside interface
647 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
649 /** \brief Check if NAT interface is outside.
650 @param i NAT interfce
651 @return 1 if outside interface
653 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
655 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
657 @return 1 if session is closed
659 #define nat44_is_ses_closed(s) s->state == 0xf
661 /** \brief Check if NAT static mapping is address only (1:1NAT).
662 @param sm NAT static mapping
663 @return 1 if 1:1NAT, 0 if 1:1NAPT
665 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
667 /** \brief Check if NAT static mapping match only out2in direction.
668 @param sm NAT static mapping
669 @return 1 if rule match only out2in direction
671 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
673 /** \brief Check if NAT static mapping is identity NAT.
674 @param sm NAT static mapping
675 @return 1 if identity NAT
677 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
679 /** \brief Check if NAT static mapping is load-balancing.
680 @param sm NAT static mapping
681 @return 1 if load-balancing
683 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
685 /** \brief Check if client initiating TCP connection (received SYN from client)
687 @return 1 if client initiating TCP connection
689 #define tcp_is_init(t) ((t->flags & TCP_FLAG_SYN) && !(t->flags & TCP_FLAG_ACK))
692 #define nat_log_err(...) \
693 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
694 #define nat_log_warn(...) \
695 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
696 #define nat_log_notice(...) \
697 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
698 #define nat_log_info(...) \
699 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
700 #define nat_log_debug(...)\
701 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
703 /* ICMP session match functions */
704 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
705 u32 thread_index, vlib_buffer_t * b0,
706 ip4_header_t * ip0, u8 * p_proto,
707 snat_session_key_t * p_value,
708 u8 * p_dont_translate, void *d, void *e);
709 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
710 u32 thread_index, vlib_buffer_t * b0,
711 ip4_header_t * ip0, u8 * p_proto,
712 snat_session_key_t * p_value,
713 u8 * p_dont_translate, void *d, void *e);
714 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
715 u32 thread_index, vlib_buffer_t * b0,
716 ip4_header_t * ip0, u8 * p_proto,
717 snat_session_key_t * p_value,
718 u8 * p_dont_translate, void *d, void *e);
719 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
720 u32 thread_index, vlib_buffer_t * b0,
721 ip4_header_t * ip0, u8 * p_proto,
722 snat_session_key_t * p_value,
723 u8 * p_dont_translate, void *d, void *e);
725 /* ICMP deterministic NAT session match functions */
726 u32 icmp_match_out2in_det (snat_main_t * sm, vlib_node_runtime_t * node,
727 u32 thread_index, vlib_buffer_t * b0,
728 ip4_header_t * ip0, u8 * p_proto,
729 snat_session_key_t * p_value,
730 u8 * p_dont_translate, void *d, void *e);
731 u32 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
732 u32 thread_index, vlib_buffer_t * b0,
733 ip4_header_t * ip0, u8 * p_proto,
734 snat_session_key_t * p_value,
735 u8 * p_dont_translate, void *d, void *e);
737 /* ICMP endpoint-dependent session match functions */
738 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
739 u32 thread_index, vlib_buffer_t * b0,
740 ip4_header_t * ip0, u8 * p_proto,
741 snat_session_key_t * p_value,
742 u8 * p_dont_translate, void *d, void *e);
743 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
744 u32 thread_index, vlib_buffer_t * b0,
745 ip4_header_t * ip0, u8 * p_proto,
746 snat_session_key_t * p_value,
747 u8 * p_dont_translate, void *d, void *e);
749 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
750 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
751 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
754 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
755 icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
756 vlib_node_runtime_t * node, u32 next0, u32 thread_index,
759 /* hairpinning functions */
760 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
761 ip4_header_t * ip0, icmp46_header_t * icmp0,
763 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
765 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
767 int snat_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
768 ip4_header_t * ip0, udp_header_t * udp0,
769 tcp_header_t * tcp0, u32 proto0, int is_ed);
770 void nat44_reass_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
771 ip4_header_t * ip0, u16 sport, u16 dport,
772 u32 proto0, int is_ed);
774 /* Call back functions for clib_bihash_add_or_overwrite_stale */
775 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
776 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
777 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
778 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
781 * @brief Increment IPv4 address
783 void increment_v4_address (ip4_address_t * a);
786 * @brief Add external address to NAT44 pool
788 * @param addr IPv4 address
789 * @param vrf_id VRF id of tenant, ~0 means independent of VRF
790 * @param twice_nat 1 if twice NAT address
792 * @return 0 on success, non-zero value otherwise
794 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
798 * @brief Delete external address from NAT44 pool
800 * @param addr IPv4 address
801 * @param delete_sm 1 if delete static mapping using address
802 * @param twice_nat 1 if twice NAT address
804 * @return 0 on success, non-zero value otherwise
806 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
810 * @brief Add/delete external address to FIB DPO (out2in DPO mode)
812 * @param addr IPv4 address
813 * @param is_add 1 = add, 0 = delete
815 * @return 0 on success, non-zero value otherwise
817 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
820 * @brief Add/delete NAT44 static mapping
822 * @param l_addr local IPv4 address
823 * @param e_addr external IPv4 address
824 * @param l_port local port number
825 * @param e_port external port number
826 * @param vrf_id local VRF ID
827 * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
828 * @param sw_if_index use interface address as external IPv4 address
829 * @param proto L4 protocol
830 * @param is_add 1 = add, 0 = delete
831 * @param twice_nat twice-nat mode
832 * @param out2in_only if 1 rule match only out2in direction
833 * @param tag opaque string tag
834 * @param identity_nat identity NAT
836 * @return 0 on success, non-zero value otherwise
838 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
839 u16 l_port, u16 e_port, u32 vrf_id,
840 int addr_only, u32 sw_if_index,
841 snat_protocol_t proto, int is_add,
842 twice_nat_type_t twice_nat, u8 out2in_only,
843 u8 * tag, u8 identity_nat);
846 * @brief Add/delete static mapping with load-balancing (multiple backends)
848 * @param e_addr external IPv4 address
849 * @param e_port external port number
850 * @param proto L4 protocol
851 * @param locals list of local backends
852 * @param is_add 1 = add, 0 = delete
853 * @param twice_nat twice-nat mode
854 * @param out2in_only if 1 rule match only out2in direction
855 * @param tag opaque string tag
856 * @param affinity 0 = disabled, otherwise client IP affinity sticky time
858 * @return 0 on success, non-zero value otherwise
860 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
861 snat_protocol_t proto,
862 nat44_lb_addr_port_t * locals, u8 is_add,
863 twice_nat_type_t twice_nat,
864 u8 out2in_only, u8 * tag, u32 affinity);
866 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
867 ip4_address_t l_addr, u16 l_port,
868 snat_protocol_t proto, u32 vrf_id,
869 u8 probability, u8 is_add);
871 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
874 * @brief Set NAT plugin workers
876 * @param bitmap NAT workers bitmap
878 * @return 0 on success, non-zero value otherwise
880 int snat_set_workers (uword * bitmap);
883 * @brief Enable/disable NAT44 feature on the interface
885 * @param sw_if_index software index of the interface
886 * @param is_inside 1 = inside, 0 = outside
887 * @param is_del 1 = delete, 0 = add
889 * @return 0 on success, non-zero value otherwise
891 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
894 * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
896 * @param sw_if_index software index of the interface
897 * @param is_inside 1 = inside, 0 = outside
898 * @param is_del 1 = delete, 0 = add
900 * @return 0 on success, non-zero value otherwise
902 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
906 * @brief Add/delete NAT44 pool address from specific interfce
908 * @param sw_if_index software index of the interface
909 * @param is_del 1 = delete, 0 = add
910 * @param twice_nat 1 = twice NAT address for extenal hosts
912 * @return 0 on success, non-zero value otherwise
914 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
918 * @brief Delete NAT44 session
920 * @param addr IPv4 address
921 * @param port L4 port number
922 * @param proto L4 protocol
923 * @param vrf_id VRF ID
924 * @param is_in 1 = inside network addres and por pair, 0 = outside
926 * @return 0 on success, non-zero value otherwise
928 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
929 snat_protocol_t proto, u32 vrf_id, int is_in);
932 * @brief Delete NAT44 endpoint-dependent session
934 * @param addr IPv4 address
935 * @param port L4 port number
936 * @param proto L4 protocol
937 * @param vrf_id VRF ID
938 * @param is_in 1 = inside network addres and por pair, 0 = outside
940 * @return 0 on success, non-zero value otherwise
942 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
943 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
944 u32 vrf_id, int is_in);
947 * @brief Free NAT44 session data (lookup keys, external addrres port)
949 * @param s NAT session
950 * @param thread_index thread index
952 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
956 * @brief Find or create NAT user
958 * @param addr IPv4 address
959 * @param fib_index FIB table index
960 * @param thread_index thread index
962 * @return NAT user data structure on success otherwise zero value
964 snat_user_t *nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr,
965 u32 fib_index, u32 thread_index);
968 * @brief Allocate new NAT session or recycle last used
971 * @param thread_index thread index
973 * @return session data structure on success otherwise zero value
975 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
980 * @brief Allocate NAT endpoint-dependent session
983 * @param thread_index thread index
985 * @return session data structure on success otherwise zero value
987 snat_session_t *nat_ed_session_alloc (snat_main_t * sm, snat_user_t * u,
988 u32 thread_index, f64 now);
991 * @brief Set address and port assignment algorithm for MAP-E CE
993 * @param psid Port Set Identifier value
994 * @param psid_offset number of offset bits
995 * @param psid_length length of PSID
997 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1001 * @brief Set address and port assignment algorithm for port range
1003 * @param start_port beginning of the port range
1004 * @param end_port end of the port range
1006 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1009 * @brief Set address and port assignment algorithm to default/standard
1011 void nat_set_alloc_addr_and_port_default (void);
1014 * @brief Free outside address and port pair
1016 * @param addresses vector of outside addresses
1017 * @param thread_index thread index
1018 * @param k adddress, port and protocol
1020 void snat_free_outside_address_and_port (snat_address_t * addresses,
1022 snat_session_key_t * k);
1025 * @brief Alloc outside address and port
1027 * @param addresses vector of outside addresses
1028 * @param fib_index FIB table index
1029 * @param thread_index thread index
1030 * @param k allocated address and port pair
1031 * @param port_per_thread number of ports per threead
1032 * @param snat_thread_index NAT thread index
1034 * @return 0 on success, non-zero value otherwise
1036 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1039 snat_session_key_t * k,
1040 u16 port_per_thread,
1041 u32 snat_thread_index);
1044 * @brief Match NAT44 static mapping.
1046 * @param match address and port to match
1047 * @param mapping external/local address and port of the matched mapping
1048 * @param by_external if 0 match by local address otherwise match by external
1050 * @param is_addr_only 1 if matched mapping is address only
1051 * @param twice_nat matched mapping is twice NAT type
1052 * @param lb 1 if matched mapping is load-balanced
1053 * @param ext_host_addr external host address
1055 * @returns 0 if match found otherwise 1.
1057 int snat_static_mapping_match (snat_main_t * sm,
1058 snat_session_key_t match,
1059 snat_session_key_t * mapping,
1062 twice_nat_type_t * twice_nat,
1064 ip4_address_t * ext_host_addr,
1065 u8 * is_identity_nat);
1068 * @brief Add/del NAT address to FIB.
1070 * Add the external NAT address to the FIB as receive entries. This ensures
1071 * that VPP will reply to ARP for this address and we don't need to enable
1072 * proxy ARP on the outside interface.
1074 * @param addr IPv4 address
1075 * @param plen address prefix length
1076 * @param sw_if_index software index of the outside interface
1077 * @param is_add 0 = delete, 1 = add.
1079 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1080 u8 p_len, u32 sw_if_index, int is_add);
1084 * Why is this here? Because we don't need to touch this layer to
1085 * simply reply to an icmp. We need to change id to a unique
1086 * value to NAT an echo request/reply.
1093 } icmp_echo_header_t;
1097 u16 src_port, dst_port;
1100 #endif /* __included_nat_h__ */
1103 * fd.io coding-style-patch-verification: ON
1106 * eval: (c-set-style "gnu")