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;
100 #define foreach_snat_protocol \
101 _(UDP, 0, udp, "udp") \
102 _(TCP, 1, tcp, "tcp") \
103 _(ICMP, 2, icmp, "icmp")
106 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
107 foreach_snat_protocol
112 #define foreach_snat_session_state \
113 _(0, UNKNOWN, "unknown") \
114 _(1, UDP_ACTIVE, "udp-active") \
115 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
116 _(3, TCP_ESTABLISHED, "tcp-established") \
117 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
118 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
119 _(6, TCP_CLOSING, "tcp-closing") \
120 _(7, TCP_LAST_ACK, "tcp-last-ack") \
121 _(8, TCP_CLOSED, "tcp-closed") \
122 _(9, ICMP_ACTIVE, "icmp-active")
125 #define _(v, N, s) SNAT_SESSION_##N = v,
126 foreach_snat_session_state
128 } snat_session_state_t;
130 #define NAT44_SES_I2O_FIN 1
131 #define NAT44_SES_O2I_FIN 2
132 #define NAT44_SES_I2O_FIN_ACK 4
133 #define NAT44_SES_O2I_FIN_ACK 8
134 #define NAT44_SES_I2O_SYN 16
135 #define NAT44_SES_O2I_SYN 32
137 #define nat44_is_ses_closed(s) s->state == 0xf
139 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
140 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
141 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
142 #define SNAT_SESSION_FLAG_TWICE_NAT 8
143 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
144 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
145 #define SNAT_SESSION_FLAG_AFFINITY 64
147 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
148 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
150 typedef CLIB_PACKED(struct {
151 snat_session_key_t out2in; /* 0-15 */
153 snat_session_key_t in2out; /* 16-31 */
155 u32 flags; /* 32-35 */
157 /* per-user translations */
158 u32 per_user_index; /* 36-39 */
160 u32 per_user_list_head_index; /* 40-43 */
162 /* Last heard timer */
163 f64 last_heard; /* 44-51 */
165 u64 total_bytes; /* 52-59 */
167 u32 total_pkts; /* 60-63 */
169 /* Outside address */
170 u32 outside_address_index; /* 64-67 */
172 /* External host address and port */
173 ip4_address_t ext_host_addr; /* 68-71 */
174 u16 ext_host_port; /* 72-73 */
176 /* External host address and port after translation */
177 ip4_address_t ext_host_nat_addr; /* 74-77 */
178 u16 ext_host_nat_port; /* 78-79 */
180 /* TCP session state */
190 u32 sessions_per_user_list_head_index;
198 #define _(N, i, n, s) \
199 u16 busy_##n##_ports; \
200 u16 * busy_##n##_ports_per_thread; \
201 uword * busy_##n##_port_bitmap;
202 foreach_snat_protocol
213 snat_det_out_key_t out;
216 } snat_det_session_t;
219 ip4_address_t in_addr;
221 ip4_address_t out_addr;
226 /* vector of sessions */
227 snat_det_session_t * sessions;
237 } nat44_lb_addr_port_t;
252 ip4_address_t local_addr;
253 ip4_address_t external_addr;
257 twice_nat_type_t twice_nat;
261 snat_protocol_t proto;
265 nat44_lb_addr_port_t *locals;
266 u32 affinity_per_service_list_head_index;
267 } snat_static_mapping_t;
275 ip4_address_t l_addr;
280 snat_protocol_t proto;
285 } snat_static_map_resolve_t;
288 /* Main lookup tables */
289 clib_bihash_8_8_t out2in;
290 clib_bihash_8_8_t in2out;
292 /* Endpoint dependent sessions lookup tables */
293 clib_bihash_16_8_t out2in_ed;
294 clib_bihash_16_8_t in2out_ed;
296 /* Find-a-user => src address lookup */
297 clib_bihash_8_8_t user_hash;
303 snat_session_t * sessions;
305 /* Pool of doubly-linked list elements */
306 dlist_elt_t * list_pool;
308 u32 snat_thread_index;
309 } snat_main_per_thread_data_t;
313 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
314 vlib_node_runtime_t *node,
319 snat_session_key_t *p_value,
320 u8 *p_dont_translate,
324 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
326 typedef int nat_alloc_out_addr_and_port_function_t (snat_address_t * addresses,
329 snat_session_key_t * k,
330 u32 * address_indexp,
332 u32 snat_thread_index);
334 typedef struct snat_main_s {
335 snat_icmp_match_function_t * icmp_match_in2out_cb;
336 snat_icmp_match_function_t * icmp_match_out2in_cb;
339 u32 first_worker_index;
341 snat_get_worker_function_t * worker_in2out_cb;
342 snat_get_worker_function_t * worker_out2in_cb;
346 /* Per thread data */
347 snat_main_per_thread_data_t * per_thread_data;
349 /* Find a static mapping by local */
350 clib_bihash_8_8_t static_mapping_by_local;
352 /* Find a static mapping by external */
353 clib_bihash_8_8_t static_mapping_by_external;
355 /* Static mapping pool */
356 snat_static_mapping_t * static_mappings;
359 snat_interface_t * interfaces;
360 snat_interface_t * output_feature_interfaces;
362 /* Vector of outside addresses */
363 snat_address_t * addresses;
364 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
369 /* vector of outside fibs */
370 nat_outside_fib_t * outside_fibs;
372 /* Vector of twice NAT addresses for extenal hosts */
373 snat_address_t * twice_nat_addresses;
375 /* sw_if_indices whose intfc addresses should be auto-added */
376 u32 * auto_add_sw_if_indices;
377 u32 * auto_add_sw_if_indices_twice_nat;
379 /* vector of interface address static mappings to resolve. */
380 snat_static_map_resolve_t *to_resolve;
382 /* Randomize port allocation order */
385 /* Worker handoff index */
387 u32 fq_in2out_output_index;
390 /* in2out and out2in node index */
391 u32 in2out_node_index;
392 u32 in2out_output_node_index;
393 u32 out2in_node_index;
394 u32 error_node_index;
396 /* Deterministic NAT */
397 snat_det_map_t * det_maps;
399 /* If forwarding is enabled */
400 u8 forwarding_enabled;
402 /* Config parameters */
403 u8 static_mapping_only;
404 u8 static_mapping_connection_tracking;
407 u8 endpoint_dependent;
408 u32 translation_buckets;
409 u32 translation_memory_size;
410 u32 max_translations;
412 u32 user_memory_size;
413 u32 max_translations_per_user;
415 u32 outside_fib_index;
417 u32 inside_fib_index;
419 /* values of various timeouts */
421 u32 tcp_established_timeout;
422 u32 tcp_transitory_timeout;
425 /* API message ID base */
429 vlib_log_class_t log_class;
432 vlib_main_t * vlib_main;
433 vnet_main_t * vnet_main;
434 ip4_main_t * ip4_main;
435 ip_lookup_main_t * ip4_lookup_main;
436 api_main_t * api_main;
442 } nat44_is_idle_session_ctx_t;
444 extern snat_main_t snat_main;
445 extern vlib_node_registration_t snat_in2out_node;
446 extern vlib_node_registration_t snat_in2out_output_node;
447 extern vlib_node_registration_t snat_out2in_node;
448 extern vlib_node_registration_t snat_in2out_fast_node;
449 extern vlib_node_registration_t snat_out2in_fast_node;
450 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
451 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
452 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
453 extern vlib_node_registration_t snat_det_in2out_node;
454 extern vlib_node_registration_t snat_det_out2in_node;
455 extern vlib_node_registration_t snat_hairpin_dst_node;
456 extern vlib_node_registration_t snat_hairpin_src_node;
457 extern vlib_node_registration_t nat44_ed_in2out_node;
458 extern vlib_node_registration_t nat44_ed_in2out_output_node;
459 extern vlib_node_registration_t nat44_ed_out2in_node;
460 extern vlib_node_registration_t nat44_ed_hairpin_dst_node;
461 extern vlib_node_registration_t nat44_ed_hairpin_src_node;
462 extern vlib_node_registration_t nat44_ed_in2out_worker_handoff_node;
463 extern vlib_node_registration_t nat44_ed_in2out_output_worker_handoff_node;
464 extern vlib_node_registration_t nat44_ed_out2in_worker_handoff_node;
466 void snat_free_outside_address_and_port (snat_address_t * addresses,
468 snat_session_key_t * k);
470 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
473 snat_session_key_t * k,
474 u32 * address_indexp,
476 u32 snat_thread_index);
478 int snat_static_mapping_match (snat_main_t * sm,
479 snat_session_key_t match,
480 snat_session_key_t * mapping,
483 twice_nat_type_t *twice_nat,
485 ip4_address_t * ext_host_addr);
487 void snat_add_del_addr_to_fib (ip4_address_t * addr,
492 format_function_t format_snat_user;
493 format_function_t format_snat_static_mapping;
494 format_function_t format_snat_static_map_to_resolve;
495 format_function_t format_snat_session;
496 format_function_t format_det_map_ses;
499 u32 cached_sw_if_index;
500 u32 cached_ip4_address;
503 /** \brief Check if SNAT session is created from static mapping.
504 @param s SNAT session
505 @return 1 if SNAT session is created from static mapping otherwise 0
507 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
509 /** \brief Check if SNAT session for unknown protocol.
510 @param s SNAT session
511 @return 1 if SNAT session for unknown protocol otherwise 0
513 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
515 /** \brief Check if NAT session is twice NAT.
517 @return 1 if NAT session is twice NAT
519 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
521 /** \brief Check if NAT session is load-balancing.
523 @return 1 if NAT session is load-balancing
525 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
527 /** \brief Check if NAT session is forwarding bypass.
529 @return 1 if NAT session is load-balancing
531 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
533 /** \brief Check if NAT session is endpoint dependent.
535 @return 1 if NAT session is endpoint dependent
537 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
539 /** \brief Check if NAT session has affinity record.
541 @return 1 if NAT session has affinity record
543 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
545 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
546 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
548 #define nat_log_err(...) \
549 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
550 #define nat_log_warn(...) \
551 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
552 #define nat_log_notice(...) \
553 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
554 #define nat_log_info(...) \
555 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
556 #define nat_log_debug(...)\
557 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
560 * Why is this here? Because we don't need to touch this layer to
561 * simply reply to an icmp. We need to change id to a unique
562 * value to NAT an echo request/reply.
568 } icmp_echo_header_t;
571 u16 src_port, dst_port;
574 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
575 u32 thread_index, vlib_buffer_t *b0,
576 ip4_header_t *ip0, u8 *p_proto,
577 snat_session_key_t *p_value,
578 u8 *p_dont_translate, void *d, void *e);
579 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
580 u32 thread_index, vlib_buffer_t *b0,
581 ip4_header_t *ip0, u8 *p_proto,
582 snat_session_key_t *p_value,
583 u8 *p_dont_translate, void *d, void *e);
584 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
585 u32 thread_index, vlib_buffer_t *b0,
586 ip4_header_t *ip0, u8 *p_proto,
587 snat_session_key_t *p_value,
588 u8 *p_dont_translate, void *d, void *e);
589 u32 icmp_match_in2out_ed(snat_main_t *sm, vlib_node_runtime_t *node,
590 u32 thread_index, vlib_buffer_t *b0,
591 ip4_header_t *ip0, u8 *p_proto,
592 snat_session_key_t *p_value,
593 u8 *p_dont_translate, void *d, void *e);
594 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
595 u32 thread_index, vlib_buffer_t *b0,
596 ip4_header_t *ip0, u8 *p_proto,
597 snat_session_key_t *p_value,
598 u8 *p_dont_translate, void *d, void *e);
599 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
600 u32 thread_index, vlib_buffer_t *b0,
601 ip4_header_t *ip0, u8 *p_proto,
602 snat_session_key_t *p_value,
603 u8 *p_dont_translate, void *d, void *e);
604 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
605 u32 thread_index, vlib_buffer_t *b0,
606 ip4_header_t *ip0, u8 *p_proto,
607 snat_session_key_t *p_value,
608 u8 *p_dont_translate, void *d, void *e);
609 u32 icmp_match_out2in_ed(snat_main_t *sm, vlib_node_runtime_t *node,
610 u32 thread_index, vlib_buffer_t *b0,
611 ip4_header_t *ip0, u8 *p_proto,
612 snat_session_key_t *p_value,
613 u8 *p_dont_translate, void *d, void *e);
614 void increment_v4_address(ip4_address_t * a);
615 int snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
617 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
619 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
620 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
621 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
622 u32 sw_if_index, snat_protocol_t proto, int is_add,
623 twice_nat_type_t twice_nat, u8 out2in_only,
625 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
626 int snat_set_workers (uword * bitmap);
627 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
628 int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside,
630 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del,
632 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
633 u8 * format_snat_protocol(u8 * s, va_list * args);
634 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
635 snat_protocol_t proto,
636 nat44_lb_addr_port_t *locals, u8 is_add,
637 twice_nat_type_t twice_nat, u8 out2in_only,
638 u8 *tag, u32 affinity);
639 int nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
640 snat_protocol_t proto, u32 vrf_id, int is_in);
641 int nat44_del_ed_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
642 ip4_address_t *eh_addr, u16 eh_port, u8 proto,
643 u32 vrf_id, int is_in);
644 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
646 snat_user_t * nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr,
647 u32 fib_index, u32 thread_index);
648 snat_session_t * nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u,
650 snat_session_t * nat_ed_session_alloc (snat_main_t *sm, snat_user_t *u,
652 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
654 void nat_set_alloc_addr_and_port_default (void);
655 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t *kv, void *arg);
656 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t *kv, void *arg);
657 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t *kv, void *arg);
658 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t *kv, void *arg);
660 #endif /* __included_snat_h__ */