2 * Copyright (c) 2020 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 * NAT44 endpoint independent plugin declarations
19 #ifndef __included_nat44_ei_h__
20 #define __included_nat44_ei_h__
23 #include <vlibapi/api.h>
25 #include <vnet/vnet.h>
26 #include <vnet/ip/ip.h>
27 #include <vnet/ethernet/ethernet.h>
28 #include <vnet/ip/icmp46_packet.h>
29 #include <vnet/api_errno.h>
30 #include <vnet/fib/fib_source.h>
32 #include <vppinfra/dlist.h>
33 #include <vppinfra/error.h>
34 #include <vppinfra/bihash_8_8.h>
35 #include <vppinfra/hash.h>
37 #include <nat/lib/lib.h>
38 #include <nat/lib/inlines.h>
39 #include <nat/lib/nat_proto.h>
41 /* default number of worker handoff frame queue elements */
42 #define NAT_FQ_NELTS_DEFAULT 64
44 /* External address and port allocation modes */
45 #define foreach_nat44_ei_addr_and_port_alloc_alg \
46 _ (0, DEFAULT, "default") \
47 _ (1, MAPE, "map-e") \
48 _ (2, RANGE, "port-range")
52 #define _(v, N, s) NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_##N = v,
53 foreach_nat44_ei_addr_and_port_alloc_alg
55 } nat44_ei_addr_and_port_alloc_alg_t;
58 #define NAT44_EI_INTERFACE_FLAG_IS_INSIDE (1 << 0)
59 #define NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE (1 << 1)
62 #define NAT44_EI_SESSION_FLAG_STATIC_MAPPING (1 << 0)
63 #define NAT44_EI_SESSION_FLAG_UNKNOWN_PROTO (1 << 1)
65 /* Static mapping flags */
66 #define NAT44_EI_SM_FLAG_ADDR_ONLY (1 << 0)
67 #define NAT44_EI_SM_FLAG_IDENTITY_NAT (1 << 1)
68 #define NAT44_EI_SM_FLAG_SWITCH_ADDRESS (1 << 2)
74 #define _(N, i, n, s) \
75 u32 busy_##n##_ports; \
76 u32 *busy_##n##_ports_per_thread; \
77 u32 busy_##n##_port_refcounts[0xffff + 1];
82 clib_error_t *nat44_ei_api_hookup (vlib_main_t *vm);
84 /* NAT address and port allocation function */
85 typedef int (nat44_ei_alloc_out_addr_and_port_function_t) (
86 nat44_ei_address_t *addresses, u32 fib_index, u32 thread_index,
87 nat_protocol_t proto, ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
88 u16 port_per_thread, u32 snat_thread_index);
98 u16 src_port, dst_port;
112 } nat44_ei_user_key_t;
116 /* maximum number of users */
118 /* maximum number of sessions */
120 /* maximum number of ssessions per user */
123 /* plugin features */
124 u8 static_mapping_only;
125 u8 connection_tracking;
135 ip4_address_t l_addr;
136 ip4_address_t pool_addr;
142 nat_protocol_t proto;
144 } nat44_ei_static_map_resolve_t;
148 /* backend IP address */
150 /* backend port number */
152 /* probability of the backend to be randomly matched */
155 /* backend FIB table */
158 } nat44_ei_lb_addr_port_t;
162 /* preferred pool address */
163 ip4_address_t pool_addr;
164 /* local IP address */
165 ip4_address_t local_addr;
166 /* external IP address */
167 ip4_address_t external_addr;
172 /* local FIB table */
176 nat_protocol_t proto;
177 /* worker threads used by backends/local host */
179 /* opaque string tag */
181 /* backends for load-balancing mode */
182 nat44_ei_lb_addr_port_t *locals;
185 } nat44_ei_static_mapping_t;
191 } nat44_ei_interface_t;
203 } nat44_ei_outside_fib_t;
205 typedef CLIB_PACKED (struct {
206 /* Outside network tuple */
214 /* Inside network tuple */
222 nat_protocol_t nat_proto;
227 /* Per-user translations */
229 u32 per_user_list_head_index;
231 /* head of LRU list in which this session is tracked */
233 /* index in global LRU list */
237 /* Last heard timer */
240 /* Last HA refresh */
241 f64 ha_last_refreshed;
247 /* External host address and port */
248 ip4_address_t ext_host_addr;
251 /* External host address and port after translation */
252 ip4_address_t ext_host_nat_addr;
253 u16 ext_host_nat_port;
255 /* TCP session state */
259 u64 tcp_closed_timestamp;
263 }) nat44_ei_session_t;
265 typedef CLIB_PACKED (struct {
268 u32 sessions_per_user_list_head_index;
275 /* Find-a-user => src address lookup */
276 clib_bihash_8_8_t user_hash;
279 nat44_ei_user_t *users;
282 nat44_ei_session_t *sessions;
284 /* Pool of doubly-linked list elements */
285 dlist_elt_t *list_pool;
287 /* LRU session list - head is stale, tail is fresh */
288 dlist_elt_t *lru_pool;
289 u32 tcp_trans_lru_head_index;
290 u32 tcp_estab_lru_head_index;
291 u32 udp_lru_head_index;
292 u32 icmp_lru_head_index;
293 u32 unk_proto_lru_head_index;
295 /* NAT thread index */
296 u32 snat_thread_index;
298 /* real thread index */
301 } nat44_ei_main_per_thread_data_t;
305 u32 cached_sw_if_index;
306 uword *cached_presence_by_ip4_address;
307 } nat44_ei_runtime_t;
313 } nat44_ei_is_idle_session_ctx_t;
315 typedef struct nat44_ei_main_s
318 u32 translation_buckets;
322 u8 forwarding_enabled;
323 u8 static_mapping_only;
324 u8 static_mapping_connection_tracking;
328 /* Find a static mapping by local */
329 clib_bihash_8_8_t static_mapping_by_local;
331 /* Find a static mapping by external */
332 clib_bihash_8_8_t static_mapping_by_external;
334 /* Static mapping pool */
335 nat44_ei_static_mapping_t *static_mappings;
338 nat44_ei_interface_t *interfaces;
339 nat44_ei_interface_t *output_feature_interfaces;
340 // broken api backward compatibility
341 nat44_ei_interface_t *output_feature_dummy_interfaces;
343 /* Is translation memory size calculated or user defined */
344 u8 translation_memory_size_set;
346 u32 max_users_per_thread;
347 u32 max_translations_per_thread;
348 u32 max_translations_per_user;
351 u32 inside_fib_index;
354 u32 outside_fib_index;
356 /* Thread settings */
358 u32 first_worker_index;
362 /* Main lookup tables */
363 clib_bihash_8_8_t out2in;
364 clib_bihash_8_8_t in2out;
366 /* Per thread data */
367 nat44_ei_main_per_thread_data_t *per_thread_data;
369 /* Vector of outside addresses */
370 nat44_ei_address_t *addresses;
372 nat44_ei_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
373 /* Address and port allocation type */
374 nat44_ei_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
375 /* Port set parameters (MAP-E) */
379 /* Port range parameters */
384 nat44_ei_fib_t *fibs;
386 /* vector of outside fibs */
387 nat44_ei_outside_fib_t *outside_fibs;
389 /* sw_if_indices whose intfc addresses should be auto-added */
390 u32 *auto_add_sw_if_indices;
392 /* vector of interface address static mappings to resolve. */
393 nat44_ei_static_map_resolve_t *to_resolve;
395 u32 in2out_node_index;
396 u32 out2in_node_index;
397 u32 in2out_output_node_index;
400 u32 fq_in2out_output_index;
403 /* Randomize port allocation order */
406 nat_timeouts_t timeouts;
409 vlib_simple_counter_main_t total_users;
410 vlib_simple_counter_main_t total_sessions;
411 vlib_simple_counter_main_t user_limit_reached;
413 #define _(x) vlib_simple_counter_main_t x;
443 vlib_simple_counter_main_t hairpinning;
447 /* API message ID base */
451 vlib_log_class_t log_class;
456 api_main_t *api_main;
457 ip4_main_t *ip4_main;
458 ip_lookup_main_t *ip4_lookup_main;
460 fib_source_t fib_src_hi;
461 fib_source_t fib_src_low;
463 /* pat (port address translation)
464 * dynamic mapping enabled or conneciton tracking */
467 /* number of worker handoff frame queue elements */
468 u32 frame_queue_nelts;
470 /* nat44 plugin enabled */
473 nat44_ei_config_t rconfig;
475 u32 in2out_hairpinning_finish_ip4_lookup_node_fq_index;
476 u32 in2out_hairpinning_finish_interface_output_node_fq_index;
477 u32 hairpinning_fq_index;
478 u32 hairpin_dst_fq_index;
480 vnet_main_t *vnet_main;
483 extern nat44_ei_main_t nat44_ei_main;
485 int nat44_ei_plugin_enable (nat44_ei_config_t c);
486 int nat44_ei_plugin_disable ();
488 int nat44_ei_add_interface (u32 sw_if_index, u8 is_inside);
489 int nat44_ei_del_interface (u32 sw_if_index, u8 is_inside);
490 int nat44_ei_add_output_interface (u32 sw_if_index);
491 int nat44_ei_del_output_interface (u32 sw_if_index);
492 int nat44_ei_add_address (ip4_address_t *addr, u32 vrf_id);
493 int nat44_ei_del_address (ip4_address_t addr, u8 delete_sm);
494 int nat44_ei_add_interface_address (u32 sw_if_index);
495 int nat44_ei_del_interface_address (u32 sw_if_index);
498 * @brief Delete specific NAT44 EI user and his sessions
500 * @param addr IPv4 address
501 * @param fib_index FIB table index
503 int nat44_ei_user_del (ip4_address_t *addr, u32 fib_index);
506 * @brief Delete session for static mapping
508 * @param addr IPv4 address
509 * @param fib_index FIB table index
511 void nat44_ei_static_mapping_del_sessions (
512 nat44_ei_main_t *nm, nat44_ei_main_per_thread_data_t *tnm,
513 nat44_ei_user_key_t u_key, int addr_only, ip4_address_t e_addr, u16 e_port);
515 u32 nat44_ei_get_in2out_worker_index (ip4_header_t *ip0, u32 rx_fib_index0,
518 u32 nat44_ei_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip0,
519 u32 rx_fib_index0, u8 is_output);
522 * @brief Set address and port assignment algorithm to default/standard
524 void nat44_ei_set_alloc_default (void);
527 * @brief Set address and port assignment algorithm for MAP-E CE
529 * @param psid Port Set Identifier value
530 * @param psid_offset number of offset bits
531 * @param psid_length length of PSID
533 void nat44_ei_set_alloc_mape (u16 psid, u16 psid_offset, u16 psid_length);
536 * @brief Set address and port assignment algorithm for port range
538 * @param start_port beginning of the port range
539 * @param end_port end of the port range
541 void nat44_ei_set_alloc_range (u16 start_port, u16 end_port);
543 int nat44_ei_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
544 u16 l_port, u16 e_port, nat_protocol_t proto,
545 u32 vrf_id, u32 sw_if_index, u32 flags,
546 ip4_address_t pool_addr, u8 *tag);
548 int nat44_ei_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
549 u16 l_port, u16 e_port, nat_protocol_t proto,
550 u32 vrf_id, u32 sw_if_index, u32 flags);
553 * @brief Delete NAT44-EI session
555 * @param addr IPv4 address
556 * @param port L4 port number
557 * @param proto L4 protocol
558 * @param vrf_id VRF ID
559 * @param is_in 1 = inside network address and port pair, 0 = outside
561 * @return 0 on success, non-zero value otherwise
563 int nat44_ei_del_session (nat44_ei_main_t *nm, ip4_address_t *addr, u16 port,
564 nat_protocol_t proto, u32 vrf_id, int is_in);
567 * @brief Match NAT44-EI static mapping.
569 * @param key address and port to match
570 * @param addr external/local address of the matched mapping
571 * @param port port of the matched mapping
572 * @param fib_index fib index of the matched mapping
573 * @param by_external if 0 match by local address otherwise match by
575 * @param is_addr_only 1 if matched mapping is address only
576 * @param is_identity_nat 1 if indentity mapping
578 * @returns 0 if match found otherwise 1.
580 int nat44_ei_static_mapping_match (ip4_address_t match_addr, u16 match_port,
582 nat_protocol_t match_protocol,
583 ip4_address_t *mapping_addr,
584 u16 *mapping_port, u32 *mapping_fib_index,
585 u8 by_external, u8 *is_addr_only,
586 u8 *is_identity_nat);
589 * @brief Clear all active NAT44-EI sessions.
591 void nat44_ei_sessions_clear ();
593 nat44_ei_user_t *nat44_ei_user_get_or_create (nat44_ei_main_t *nm,
595 u32 fib_index, u32 thread_index);
597 nat44_ei_session_t *nat44_ei_session_alloc_or_recycle (nat44_ei_main_t *nm,
602 void nat44_ei_free_session_data_v2 (nat44_ei_main_t *nm, nat44_ei_session_t *s,
603 u32 thread_index, u8 is_ha);
605 void nat44_ei_free_outside_address_and_port (nat44_ei_address_t *addresses,
607 ip4_address_t *addr, u16 port,
608 nat_protocol_t protocol);
610 int nat44_ei_set_outside_address_and_port (nat44_ei_address_t *addresses,
612 ip4_address_t addr, u16 port,
613 nat_protocol_t protocol);
615 void nat44_ei_free_session_data (nat44_ei_main_t *nm, nat44_ei_session_t *s,
616 u32 thread_index, u8 is_ha);
618 int nat44_ei_set_workers (uword *bitmap);
620 void nat44_ei_add_del_address_dpo (ip4_address_t addr, u8 is_add);
622 void nat44_ei_delete_session (nat44_ei_main_t *nm, nat44_ei_session_t *ses,
625 /* Call back functions for clib_bihash_add_or_overwrite_stale */
626 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t *kv, void *arg);
627 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t *kv, void *arg);
629 int nat44_ei_hairpinning (vlib_main_t *vm, vlib_node_runtime_t *node,
630 nat44_ei_main_t *nm, u32 thread_index,
631 vlib_buffer_t *b0, ip4_header_t *ip0,
632 udp_header_t *udp0, tcp_header_t *tcp0, u32 proto0,
633 int do_trace, u32 *required_thread_index);
635 void nat44_ei_hairpinning_sm_unknown_proto (nat44_ei_main_t *nm,
639 u32 nat44_ei_icmp_hairpinning (nat44_ei_main_t *nm, vlib_buffer_t *b0,
640 u32 thread_index, ip4_header_t *ip0,
641 icmp46_header_t *icmp0,
642 u32 *required_thread_index);
644 int nat44_ei_set_frame_queue_nelts (u32 frame_queue_nelts);
647 nat44_ei_is_session_static (nat44_ei_session_t *s)
649 return (s->flags & NAT44_EI_SESSION_FLAG_STATIC_MAPPING);
653 nat44_ei_is_unk_proto_session (nat44_ei_session_t *s)
655 return (s->flags & NAT44_EI_SESSION_FLAG_UNKNOWN_PROTO);
659 nat44_ei_interface_is_inside (nat44_ei_interface_t *i)
661 return (i->flags & NAT44_EI_INTERFACE_FLAG_IS_INSIDE);
665 nat44_ei_interface_is_outside (nat44_ei_interface_t *i)
667 return (i->flags & NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE);
671 is_sm_addr_only (u32 f)
673 return (f & NAT44_EI_SM_FLAG_ADDR_ONLY);
677 is_sm_identity_nat (u32 f)
679 return (f & NAT44_EI_SM_FLAG_IDENTITY_NAT);
683 is_sm_switch_address (u32 f)
685 return (f & NAT44_EI_SM_FLAG_SWITCH_ADDRESS);
689 #define nat44_ei_log_err(...) \
690 vlib_log (VLIB_LOG_LEVEL_ERR, nat44_ei_main.log_class, __VA_ARGS__)
691 #define nat44_ei_log_warn(...) \
692 vlib_log (VLIB_LOG_LEVEL_WARNING, nat44_ei_main.log_class, __VA_ARGS__)
693 #define nat44_ei_log_notice(...) \
694 vlib_log (VLIB_LOG_LEVEL_NOTICE, nat44_ei_main.log_class, __VA_ARGS__)
695 #define nat44_ei_log_info(...) \
696 vlib_log (VLIB_LOG_LEVEL_INFO, nat44_ei_main.log_class, __VA_ARGS__)
697 #define nat44_ei_log_debug(...) \
698 vlib_log (VLIB_LOG_LEVEL_DEBUG, nat44_ei_main.log_class, __VA_ARGS__)
700 #endif /* __included_nat44_ei_h__ */
702 * fd.io coding-style-patch-verification: ON
705 * eval: (c-set-style "gnu")