3 * nat.h - NAT plugin definitions
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
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>
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 #define NAT_FQ_NELTS 64
41 #define SNAT_FLAG_HAIRPINNING (1 << 0)
79 ip4_address_t ext_host_addr;
99 #define foreach_nat_addr_and_port_alloc_alg \
100 _(0, DEFAULT, "default") \
101 _(1, MAPE, "map-e") \
102 _(2, RANGE, "port-range")
105 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
106 foreach_nat_addr_and_port_alloc_alg
108 } nat_addr_and_port_alloc_alg_t;
110 #define foreach_snat_protocol \
111 _(UDP, 0, udp, "udp") \
112 _(TCP, 1, tcp, "tcp") \
113 _(ICMP, 2, icmp, "icmp")
116 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
117 foreach_snat_protocol
122 #define foreach_snat_session_state \
123 _(0, UNKNOWN, "unknown") \
124 _(1, UDP_ACTIVE, "udp-active") \
125 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
126 _(3, TCP_ESTABLISHED, "tcp-established") \
127 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
128 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
129 _(6, TCP_CLOSING, "tcp-closing") \
130 _(7, TCP_LAST_ACK, "tcp-last-ack") \
131 _(8, TCP_CLOSED, "tcp-closed") \
132 _(9, ICMP_ACTIVE, "icmp-active")
135 #define _(v, N, s) SNAT_SESSION_##N = v,
136 foreach_snat_session_state
138 } snat_session_state_t;
140 #define NAT44_SES_I2O_FIN 1
141 #define NAT44_SES_O2I_FIN 2
142 #define NAT44_SES_I2O_FIN_ACK 4
143 #define NAT44_SES_O2I_FIN_ACK 8
144 #define NAT44_SES_I2O_SYN 16
145 #define NAT44_SES_O2I_SYN 32
147 #define nat44_is_ses_closed(s) s->state == 0xf
149 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
150 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
151 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
152 #define SNAT_SESSION_FLAG_TWICE_NAT 8
153 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
154 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
155 #define SNAT_SESSION_FLAG_AFFINITY 64
157 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
158 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
160 typedef CLIB_PACKED(struct {
161 snat_session_key_t out2in; /* 0-15 */
163 snat_session_key_t in2out; /* 16-31 */
165 u32 flags; /* 32-35 */
167 /* per-user translations */
168 u32 per_user_index; /* 36-39 */
170 u32 per_user_list_head_index; /* 40-43 */
172 /* Last heard timer */
173 f64 last_heard; /* 44-51 */
175 u64 total_bytes; /* 52-59 */
177 u32 total_pkts; /* 60-63 */
179 /* Outside address */
180 u32 outside_address_index; /* 64-67 */
182 /* External host address and port */
183 ip4_address_t ext_host_addr; /* 68-71 */
184 u16 ext_host_port; /* 72-73 */
186 /* External host address and port after translation */
187 ip4_address_t ext_host_nat_addr; /* 74-77 */
188 u16 ext_host_nat_port; /* 78-79 */
190 /* TCP session state */
200 u32 sessions_per_user_list_head_index;
208 #define _(N, i, n, s) \
209 u16 busy_##n##_ports; \
210 u16 * busy_##n##_ports_per_thread; \
211 uword * busy_##n##_port_bitmap;
212 foreach_snat_protocol
223 snat_det_out_key_t out;
226 } snat_det_session_t;
229 ip4_address_t in_addr;
231 ip4_address_t out_addr;
236 /* vector of sessions */
237 snat_det_session_t * sessions;
247 } nat44_lb_addr_port_t;
262 ip4_address_t local_addr;
263 ip4_address_t external_addr;
267 twice_nat_type_t twice_nat;
271 snat_protocol_t proto;
275 nat44_lb_addr_port_t *locals;
276 u32 affinity_per_service_list_head_index;
277 } snat_static_mapping_t;
285 ip4_address_t l_addr;
290 snat_protocol_t proto;
295 } snat_static_map_resolve_t;
298 /* Main lookup tables */
299 clib_bihash_8_8_t out2in;
300 clib_bihash_8_8_t in2out;
302 /* Endpoint dependent sessions lookup tables */
303 clib_bihash_16_8_t out2in_ed;
304 clib_bihash_16_8_t in2out_ed;
306 /* Find-a-user => src address lookup */
307 clib_bihash_8_8_t user_hash;
313 snat_session_t * sessions;
315 /* Pool of doubly-linked list elements */
316 dlist_elt_t * list_pool;
318 u32 snat_thread_index;
319 } snat_main_per_thread_data_t;
323 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
324 vlib_node_runtime_t *node,
329 snat_session_key_t *p_value,
330 u8 *p_dont_translate,
334 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
336 typedef int nat_alloc_out_addr_and_port_function_t (snat_address_t * addresses,
339 snat_session_key_t * k,
340 u32 * address_indexp,
342 u32 snat_thread_index);
344 typedef struct snat_main_s {
345 snat_icmp_match_function_t * icmp_match_in2out_cb;
346 snat_icmp_match_function_t * icmp_match_out2in_cb;
349 u32 first_worker_index;
351 snat_get_worker_function_t * worker_in2out_cb;
352 snat_get_worker_function_t * worker_out2in_cb;
356 /* Per thread data */
357 snat_main_per_thread_data_t * per_thread_data;
359 /* Find a static mapping by local */
360 clib_bihash_8_8_t static_mapping_by_local;
362 /* Find a static mapping by external */
363 clib_bihash_8_8_t static_mapping_by_external;
365 /* Static mapping pool */
366 snat_static_mapping_t * static_mappings;
369 snat_interface_t * interfaces;
370 snat_interface_t * output_feature_interfaces;
372 /* Vector of outside addresses */
373 snat_address_t * addresses;
374 /* Address and port allocation function */
375 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
376 /* Address and port allocation type */
377 nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
378 /* Port set parameters (MAP-E) */
382 /* Port range parameters */
386 /* vector of outside fibs */
387 nat_outside_fib_t * outside_fibs;
389 /* Vector of twice NAT addresses for extenal hosts */
390 snat_address_t * twice_nat_addresses;
392 /* sw_if_indices whose intfc addresses should be auto-added */
393 u32 * auto_add_sw_if_indices;
394 u32 * auto_add_sw_if_indices_twice_nat;
396 /* vector of interface address static mappings to resolve. */
397 snat_static_map_resolve_t *to_resolve;
399 /* Randomize port allocation order */
402 /* Worker handoff index */
404 u32 fq_in2out_output_index;
407 /* in2out and out2in node index */
408 u32 in2out_node_index;
409 u32 in2out_output_node_index;
410 u32 out2in_node_index;
411 u32 error_node_index;
413 /* Deterministic NAT */
414 snat_det_map_t * det_maps;
416 /* If forwarding is enabled */
417 u8 forwarding_enabled;
419 /* Config parameters */
420 u8 static_mapping_only;
421 u8 static_mapping_connection_tracking;
424 u8 endpoint_dependent;
425 u32 translation_buckets;
426 u32 translation_memory_size;
427 u32 max_translations;
429 u32 user_memory_size;
430 u32 max_translations_per_user;
432 u32 outside_fib_index;
434 u32 inside_fib_index;
436 /* values of various timeouts */
438 u32 tcp_established_timeout;
439 u32 tcp_transitory_timeout;
442 /* TCP MSS clamping */
446 /* API message ID base */
450 vlib_log_class_t log_class;
453 vlib_main_t * vlib_main;
454 vnet_main_t * vnet_main;
455 ip4_main_t * ip4_main;
456 ip_lookup_main_t * ip4_lookup_main;
457 api_main_t * api_main;
463 } nat44_is_idle_session_ctx_t;
465 extern snat_main_t snat_main;
466 extern vlib_node_registration_t snat_in2out_node;
467 extern vlib_node_registration_t snat_in2out_output_node;
468 extern vlib_node_registration_t snat_out2in_node;
469 extern vlib_node_registration_t snat_in2out_fast_node;
470 extern vlib_node_registration_t snat_out2in_fast_node;
471 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
472 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
473 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
474 extern vlib_node_registration_t snat_det_in2out_node;
475 extern vlib_node_registration_t snat_det_out2in_node;
476 extern vlib_node_registration_t snat_hairpin_dst_node;
477 extern vlib_node_registration_t snat_hairpin_src_node;
478 extern vlib_node_registration_t nat44_ed_in2out_node;
479 extern vlib_node_registration_t nat44_ed_in2out_output_node;
480 extern vlib_node_registration_t nat44_ed_out2in_node;
481 extern vlib_node_registration_t nat44_ed_hairpin_dst_node;
482 extern vlib_node_registration_t nat44_ed_hairpin_src_node;
483 extern vlib_node_registration_t nat44_ed_in2out_worker_handoff_node;
484 extern vlib_node_registration_t nat44_ed_in2out_output_worker_handoff_node;
485 extern vlib_node_registration_t nat44_ed_out2in_worker_handoff_node;
487 void snat_free_outside_address_and_port (snat_address_t * addresses,
489 snat_session_key_t * k);
491 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
494 snat_session_key_t * k,
495 u32 * address_indexp,
497 u32 snat_thread_index);
499 int snat_static_mapping_match (snat_main_t * sm,
500 snat_session_key_t match,
501 snat_session_key_t * mapping,
504 twice_nat_type_t *twice_nat,
506 ip4_address_t * ext_host_addr);
508 void snat_add_del_addr_to_fib (ip4_address_t * addr,
513 format_function_t format_snat_user;
514 format_function_t format_snat_static_mapping;
515 format_function_t format_snat_static_map_to_resolve;
516 format_function_t format_snat_session;
517 format_function_t format_det_map_ses;
520 u32 cached_sw_if_index;
521 u32 cached_ip4_address;
524 /** \brief Check if SNAT session is created from static mapping.
525 @param s SNAT session
526 @return 1 if SNAT session is created from static mapping otherwise 0
528 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
530 /** \brief Check if SNAT session for unknown protocol.
531 @param s SNAT session
532 @return 1 if SNAT session for unknown protocol otherwise 0
534 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
536 /** \brief Check if NAT session is twice NAT.
538 @return 1 if NAT session is twice NAT
540 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
542 /** \brief Check if NAT session is load-balancing.
544 @return 1 if NAT session is load-balancing
546 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
548 /** \brief Check if NAT session is forwarding bypass.
550 @return 1 if NAT session is load-balancing
552 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
554 /** \brief Check if NAT session is endpoint dependent.
556 @return 1 if NAT session is endpoint dependent
558 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
560 /** \brief Check if NAT session has affinity record.
562 @return 1 if NAT session has affinity record
564 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
566 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
567 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
569 #define nat_log_err(...) \
570 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
571 #define nat_log_warn(...) \
572 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
573 #define nat_log_notice(...) \
574 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
575 #define nat_log_info(...) \
576 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
577 #define nat_log_debug(...)\
578 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
581 * Why is this here? Because we don't need to touch this layer to
582 * simply reply to an icmp. We need to change id to a unique
583 * value to NAT an echo request/reply.
589 } icmp_echo_header_t;
592 u16 src_port, dst_port;
595 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
596 u32 thread_index, vlib_buffer_t *b0,
597 ip4_header_t *ip0, u8 *p_proto,
598 snat_session_key_t *p_value,
599 u8 *p_dont_translate, void *d, void *e);
600 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
601 u32 thread_index, vlib_buffer_t *b0,
602 ip4_header_t *ip0, u8 *p_proto,
603 snat_session_key_t *p_value,
604 u8 *p_dont_translate, void *d, void *e);
605 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
606 u32 thread_index, vlib_buffer_t *b0,
607 ip4_header_t *ip0, u8 *p_proto,
608 snat_session_key_t *p_value,
609 u8 *p_dont_translate, void *d, void *e);
610 u32 icmp_match_in2out_ed(snat_main_t *sm, vlib_node_runtime_t *node,
611 u32 thread_index, vlib_buffer_t *b0,
612 ip4_header_t *ip0, u8 *p_proto,
613 snat_session_key_t *p_value,
614 u8 *p_dont_translate, void *d, void *e);
615 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
616 u32 thread_index, vlib_buffer_t *b0,
617 ip4_header_t *ip0, u8 *p_proto,
618 snat_session_key_t *p_value,
619 u8 *p_dont_translate, void *d, void *e);
620 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
621 u32 thread_index, vlib_buffer_t *b0,
622 ip4_header_t *ip0, u8 *p_proto,
623 snat_session_key_t *p_value,
624 u8 *p_dont_translate, void *d, void *e);
625 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
626 u32 thread_index, vlib_buffer_t *b0,
627 ip4_header_t *ip0, u8 *p_proto,
628 snat_session_key_t *p_value,
629 u8 *p_dont_translate, void *d, void *e);
630 u32 icmp_match_out2in_ed(snat_main_t *sm, vlib_node_runtime_t *node,
631 u32 thread_index, vlib_buffer_t *b0,
632 ip4_header_t *ip0, u8 *p_proto,
633 snat_session_key_t *p_value,
634 u8 *p_dont_translate, void *d, void *e);
635 void increment_v4_address(ip4_address_t * a);
636 int snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
638 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
640 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
641 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
642 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
643 u32 sw_if_index, snat_protocol_t proto, int is_add,
644 twice_nat_type_t twice_nat, u8 out2in_only,
646 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
647 int snat_set_workers (uword * bitmap);
648 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
649 int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside,
651 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del,
653 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
654 u8 * format_snat_protocol(u8 * s, va_list * args);
655 u8 * format_nat_addr_and_port_alloc_alg(u8 * s, va_list * args);
656 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
657 snat_protocol_t proto,
658 nat44_lb_addr_port_t *locals, u8 is_add,
659 twice_nat_type_t twice_nat, u8 out2in_only,
660 u8 *tag, u32 affinity);
661 int nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
662 snat_protocol_t proto, u32 vrf_id, int is_in);
663 int nat44_del_ed_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
664 ip4_address_t *eh_addr, u16 eh_port, u8 proto,
665 u32 vrf_id, int is_in);
666 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
668 snat_user_t * nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr,
669 u32 fib_index, u32 thread_index);
670 snat_session_t * nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u,
672 snat_session_t * nat_ed_session_alloc (snat_main_t *sm, snat_user_t *u,
674 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
676 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
677 void nat_set_alloc_addr_and_port_default (void);
678 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t *kv, void *arg);
679 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t *kv, void *arg);
680 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t *kv, void *arg);
681 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t *kv, void *arg);
683 #endif /* __included_snat_h__ */