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_UDP_TIMEOUT_MIN 120
36 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
37 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
38 #define SNAT_TCP_INCOMING_SYN 6
39 #define SNAT_ICMP_TIMEOUT 60
41 #define NAT_FQ_NELTS 64
43 #define SNAT_FLAG_HAIRPINNING (1 << 0)
81 ip4_address_t ext_host_addr;
102 #define foreach_snat_protocol \
103 _(UDP, 0, udp, "udp") \
104 _(TCP, 1, tcp, "tcp") \
105 _(ICMP, 2, icmp, "icmp")
108 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
109 foreach_snat_protocol
114 #define foreach_snat_session_state \
115 _(0, UNKNOWN, "unknown") \
116 _(1, UDP_ACTIVE, "udp-active") \
117 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
118 _(3, TCP_ESTABLISHED, "tcp-established") \
119 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
120 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
121 _(6, TCP_CLOSING, "tcp-closing") \
122 _(7, TCP_LAST_ACK, "tcp-last-ack") \
123 _(8, TCP_CLOSED, "tcp-closed") \
124 _(9, ICMP_ACTIVE, "icmp-active")
127 #define _(v, N, s) SNAT_SESSION_##N = v,
128 foreach_snat_session_state
130 } snat_session_state_t;
132 #define NAT44_SES_I2O_FIN 1
133 #define NAT44_SES_O2I_FIN 2
134 #define NAT44_SES_I2O_FIN_ACK 4
135 #define NAT44_SES_O2I_FIN_ACK 8
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
146 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
147 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
149 typedef CLIB_PACKED(struct {
150 snat_session_key_t out2in; /* 0-15 */
152 snat_session_key_t in2out; /* 16-31 */
154 u32 flags; /* 32-35 */
156 /* per-user translations */
157 u32 per_user_index; /* 36-39 */
159 u32 per_user_list_head_index; /* 40-43 */
161 /* Last heard timer */
162 f64 last_heard; /* 44-51 */
164 u64 total_bytes; /* 52-59 */
166 u32 total_pkts; /* 60-63 */
168 /* Outside address */
169 u32 outside_address_index; /* 64-67 */
171 /* External host address and port */
172 ip4_address_t ext_host_addr; /* 68-71 */
173 u16 ext_host_port; /* 72-73 */
175 /* External hos address and port after translation */
176 ip4_address_t ext_host_nat_addr; /* 74-77 */
177 u16 ext_host_nat_port; /* 78-79 */
179 /* TCP session state */
189 u32 sessions_per_user_list_head_index;
197 #define _(N, i, n, s) \
198 u16 busy_##n##_ports; \
199 u16 * busy_##n##_ports_per_thread; \
200 uword * busy_##n##_port_bitmap;
201 foreach_snat_protocol
212 snat_det_out_key_t out;
215 } snat_det_session_t;
218 ip4_address_t in_addr;
220 ip4_address_t out_addr;
225 /* vector of sessions */
226 snat_det_session_t * sessions;
234 } nat44_lb_addr_port_t;
243 ip4_address_t local_addr;
244 ip4_address_t external_addr;
248 twice_nat_type_t twice_nat;
252 snat_protocol_t proto;
255 nat44_lb_addr_port_t *locals;
256 } snat_static_mapping_t;
264 ip4_address_t l_addr;
269 snat_protocol_t proto;
274 } snat_static_map_resolve_t;
277 /* Main lookup tables */
278 clib_bihash_8_8_t out2in;
279 clib_bihash_8_8_t in2out;
281 /* Endpoint dependent sessions lookup tables */
282 clib_bihash_16_8_t out2in_ed;
283 clib_bihash_16_8_t in2out_ed;
285 /* Find-a-user => src address lookup */
286 clib_bihash_8_8_t user_hash;
292 snat_session_t * sessions;
294 /* Pool of doubly-linked list elements */
295 dlist_elt_t * list_pool;
297 u32 snat_thread_index;
298 } snat_main_per_thread_data_t;
302 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
303 vlib_node_runtime_t *node,
308 snat_session_key_t *p_value,
309 u8 *p_dont_translate,
313 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
315 typedef int nat_alloc_out_addr_and_port_function_t (snat_address_t * addresses,
318 snat_session_key_t * k,
319 u32 * address_indexp,
321 u32 snat_thread_index);
323 typedef struct snat_main_s {
324 snat_icmp_match_function_t * icmp_match_in2out_cb;
325 snat_icmp_match_function_t * icmp_match_out2in_cb;
328 u32 first_worker_index;
330 snat_get_worker_function_t * worker_in2out_cb;
331 snat_get_worker_function_t * worker_out2in_cb;
335 /* Per thread data */
336 snat_main_per_thread_data_t * per_thread_data;
338 /* Find a static mapping by local */
339 clib_bihash_8_8_t static_mapping_by_local;
341 /* Find a static mapping by external */
342 clib_bihash_8_8_t static_mapping_by_external;
344 /* Static mapping pool */
345 snat_static_mapping_t * static_mappings;
348 snat_interface_t * interfaces;
349 snat_interface_t * output_feature_interfaces;
351 /* Vector of outside addresses */
352 snat_address_t * addresses;
353 nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
358 /* vector of outside fibs */
359 nat_outside_fib_t * outside_fibs;
361 /* Vector of twice NAT addresses for extenal hosts */
362 snat_address_t * twice_nat_addresses;
364 /* sw_if_indices whose intfc addresses should be auto-added */
365 u32 * auto_add_sw_if_indices;
366 u32 * auto_add_sw_if_indices_twice_nat;
368 /* vector of interface address static mappings to resolve. */
369 snat_static_map_resolve_t *to_resolve;
371 /* Randomize port allocation order */
374 /* Worker handoff index */
376 u32 fq_in2out_output_index;
379 /* in2out and out2in node index */
380 u32 in2out_node_index;
381 u32 in2out_output_node_index;
382 u32 out2in_node_index;
383 u32 error_node_index;
385 /* Deterministic NAT */
386 snat_det_map_t * det_maps;
388 /* If forwarding is enabled */
389 u8 forwarding_enabled;
391 /* Config parameters */
392 u8 static_mapping_only;
393 u8 static_mapping_connection_tracking;
396 u8 endpoint_dependent;
397 u32 translation_buckets;
398 u32 translation_memory_size;
399 u32 max_translations;
401 u32 user_memory_size;
402 u32 max_translations_per_user;
404 u32 outside_fib_index;
406 u32 inside_fib_index;
408 /* values of various timeouts */
410 u32 tcp_established_timeout;
411 u32 tcp_transitory_timeout;
414 /* API message ID base */
418 vlib_log_class_t log_class;
421 vlib_main_t * vlib_main;
422 vnet_main_t * vnet_main;
423 ip4_main_t * ip4_main;
424 ip_lookup_main_t * ip4_lookup_main;
425 api_main_t * api_main;
428 extern snat_main_t snat_main;
429 extern vlib_node_registration_t snat_in2out_node;
430 extern vlib_node_registration_t snat_in2out_output_node;
431 extern vlib_node_registration_t snat_out2in_node;
432 extern vlib_node_registration_t snat_in2out_fast_node;
433 extern vlib_node_registration_t snat_out2in_fast_node;
434 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
435 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
436 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
437 extern vlib_node_registration_t snat_det_in2out_node;
438 extern vlib_node_registration_t snat_det_out2in_node;
439 extern vlib_node_registration_t snat_hairpin_dst_node;
440 extern vlib_node_registration_t snat_hairpin_src_node;
441 extern vlib_node_registration_t nat44_ed_in2out_node;
442 extern vlib_node_registration_t nat44_ed_in2out_output_node;
443 extern vlib_node_registration_t nat44_ed_out2in_node;
444 extern vlib_node_registration_t nat44_ed_hairpin_dst_node;
445 extern vlib_node_registration_t nat44_ed_hairpin_src_node;
446 extern vlib_node_registration_t nat44_ed_in2out_worker_handoff_node;
447 extern vlib_node_registration_t nat44_ed_in2out_output_worker_handoff_node;
448 extern vlib_node_registration_t nat44_ed_out2in_worker_handoff_node;
450 void snat_free_outside_address_and_port (snat_address_t * addresses,
452 snat_session_key_t * k,
455 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
458 snat_session_key_t * k,
459 u32 * address_indexp,
461 u32 snat_thread_index);
463 int snat_static_mapping_match (snat_main_t * sm,
464 snat_session_key_t match,
465 snat_session_key_t * mapping,
468 twice_nat_type_t *twice_nat,
471 void snat_add_del_addr_to_fib (ip4_address_t * addr,
476 format_function_t format_snat_user;
477 format_function_t format_snat_static_mapping;
478 format_function_t format_snat_static_map_to_resolve;
479 format_function_t format_snat_session;
480 format_function_t format_det_map_ses;
483 u32 cached_sw_if_index;
484 u32 cached_ip4_address;
487 /** \brief Check if SNAT session is created from static mapping.
488 @param s SNAT session
489 @return 1 if SNAT session is created from static mapping otherwise 0
491 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
493 /** \brief Check if SNAT session for unknown protocol.
494 @param s SNAT session
495 @return 1 if SNAT session for unknown protocol otherwise 0
497 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
499 /** \brief Check if NAT session is twice NAT.
501 @return 1 if NAT session is twice NAT
503 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
505 /** \brief Check if NAT session is load-balancing.
507 @return 1 if NAT session is load-balancing
509 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
511 /** \brief Check if NAT session is forwarding bypass.
513 @return 1 if NAT session is load-balancing
515 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
517 /** \brief Check if NAT session is endpoint dependent.
519 @return 1 if NAT session is endpoint dependent
521 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
523 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
524 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
526 #define nat_log_err(...) \
527 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
528 #define nat_log_warn(...) \
529 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
530 #define nat_log_notice(...) \
531 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
532 #define nat_log_info(...) \
533 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
534 #define nat_log_debug(...)\
535 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
538 * Why is this here? Because we don't need to touch this layer to
539 * simply reply to an icmp. We need to change id to a unique
540 * value to NAT an echo request/reply.
546 } icmp_echo_header_t;
549 u16 src_port, dst_port;
552 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
553 u32 thread_index, vlib_buffer_t *b0,
554 ip4_header_t *ip0, u8 *p_proto,
555 snat_session_key_t *p_value,
556 u8 *p_dont_translate, void *d, void *e);
557 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
558 u32 thread_index, vlib_buffer_t *b0,
559 ip4_header_t *ip0, u8 *p_proto,
560 snat_session_key_t *p_value,
561 u8 *p_dont_translate, void *d, void *e);
562 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
563 u32 thread_index, vlib_buffer_t *b0,
564 ip4_header_t *ip0, u8 *p_proto,
565 snat_session_key_t *p_value,
566 u8 *p_dont_translate, void *d, void *e);
567 u32 icmp_match_in2out_ed(snat_main_t *sm, vlib_node_runtime_t *node,
568 u32 thread_index, vlib_buffer_t *b0,
569 ip4_header_t *ip0, u8 *p_proto,
570 snat_session_key_t *p_value,
571 u8 *p_dont_translate, void *d, void *e);
572 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
573 u32 thread_index, vlib_buffer_t *b0,
574 ip4_header_t *ip0, u8 *p_proto,
575 snat_session_key_t *p_value,
576 u8 *p_dont_translate, void *d, void *e);
577 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
578 u32 thread_index, vlib_buffer_t *b0,
579 ip4_header_t *ip0, u8 *p_proto,
580 snat_session_key_t *p_value,
581 u8 *p_dont_translate, void *d, void *e);
582 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
583 u32 thread_index, vlib_buffer_t *b0,
584 ip4_header_t *ip0, u8 *p_proto,
585 snat_session_key_t *p_value,
586 u8 *p_dont_translate, void *d, void *e);
587 u32 icmp_match_out2in_ed(snat_main_t *sm, vlib_node_runtime_t *node,
588 u32 thread_index, vlib_buffer_t *b0,
589 ip4_header_t *ip0, u8 *p_proto,
590 snat_session_key_t *p_value,
591 u8 *p_dont_translate, void *d, void *e);
592 void increment_v4_address(ip4_address_t * a);
593 int snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
595 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
597 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
598 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
599 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
600 u32 sw_if_index, snat_protocol_t proto, int is_add,
601 twice_nat_type_t twice_nat, u8 out2in_only,
603 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
604 int snat_set_workers (uword * bitmap);
605 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
606 int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside,
608 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del,
610 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
611 u8 * format_snat_protocol(u8 * s, va_list * args);
612 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
613 snat_protocol_t proto, u32 vrf_id,
614 nat44_lb_addr_port_t *locals, u8 is_add,
615 twice_nat_type_t twice_nat, u8 out2in_only,
617 int nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
618 snat_protocol_t proto, u32 vrf_id, int is_in);
619 int nat44_del_ed_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
620 ip4_address_t *eh_addr, u16 eh_port, u8 proto,
621 u32 vrf_id, int is_in);
622 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
624 snat_user_t * nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr,
625 u32 fib_index, u32 thread_index);
626 snat_session_t * nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u,
628 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
630 void nat_set_alloc_addr_and_port_default (void);
632 #endif /* __included_snat_h__ */