nat: use proper type for counters
[vpp.git] / src / plugins / nat / nat.h
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /**
16  * @file nat.c
17  * NAT plugin global declarations
18  */
19 #ifndef __included_nat_h__
20 #define __included_nat_h__
21
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/ip/icmp46_packet.h>
26 #include <vnet/api_errno.h>
27 #include <vnet/fib/fib_source.h>
28 #include <vppinfra/elog.h>
29 #include <vppinfra/bihash_8_8.h>
30 #include <vppinfra/bihash_16_8.h>
31 #include <vppinfra/dlist.h>
32 #include <vppinfra/error.h>
33 #include <vlibapi/api.h>
34 #include <vlib/log.h>
35 #include <vppinfra/bihash_16_8.h>
36 #include <nat/lib/lib.h>
37 #include <nat/lib/inlines.h>
38
39 /* default session timeouts */
40 #define SNAT_UDP_TIMEOUT 300
41 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
42 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
43 #define SNAT_ICMP_TIMEOUT 60
44
45 /* number of worker handoff frame queue elements */
46 #define NAT_FQ_NELTS 64
47
48 /* NAT buffer flags */
49 #define SNAT_FLAG_HAIRPINNING (1 << 0)
50
51 typedef enum
52 {
53   NAT_NEXT_DROP,
54   NAT_NEXT_ICMP_ERROR,
55   NAT_NEXT_IN2OUT_ED_FAST_PATH,
56   NAT_NEXT_IN2OUT_ED_SLOW_PATH,
57   NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH,
58   NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
59   NAT_NEXT_OUT2IN_ED_FAST_PATH,
60   NAT_NEXT_OUT2IN_ED_SLOW_PATH,
61   NAT_NEXT_IN2OUT_CLASSIFY,
62   NAT_NEXT_OUT2IN_CLASSIFY,
63   NAT_N_NEXT,
64 } nat_next_t;
65
66 typedef struct
67 {
68   u32 next_index;
69   u32 arc_next_index;
70 } nat_pre_trace_t;
71
72 /* user (internal host) key */
73 typedef struct
74 {
75   union
76   {
77     struct
78     {
79       ip4_address_t addr;
80       u32 fib_index;
81     };
82     u64 as_u64;
83   };
84 } snat_user_key_t;
85
86 /* External address and port allocation modes */
87 #define foreach_nat_addr_and_port_alloc_alg \
88   _(0, DEFAULT, "default")         \
89   _(1, MAPE, "map-e")              \
90   _(2, RANGE, "port-range")
91
92 typedef enum
93 {
94 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
95   foreach_nat_addr_and_port_alloc_alg
96 #undef _
97 } nat_addr_and_port_alloc_alg_t;
98
99 /* Session state */
100 #define foreach_snat_session_state          \
101   _(0, UNKNOWN, "unknown")                 \
102   _(1, UDP_ACTIVE, "udp-active")           \
103   _(2, TCP_SYN_SENT, "tcp-syn-sent")       \
104   _(3, TCP_ESTABLISHED, "tcp-established") \
105   _(4, TCP_FIN_WAIT, "tcp-fin-wait")       \
106   _(5, TCP_CLOSE_WAIT, "tcp-close-wait")   \
107   _(6, TCP_CLOSING, "tcp-closing")         \
108   _(7, TCP_LAST_ACK, "tcp-last-ack")       \
109   _(8, TCP_CLOSED, "tcp-closed")           \
110   _(9, ICMP_ACTIVE, "icmp-active")
111
112 typedef enum
113 {
114 #define _(v, N, s) SNAT_SESSION_##N = v,
115   foreach_snat_session_state
116 #undef _
117 } snat_session_state_t;
118
119 #define foreach_nat_in2out_ed_error                     \
120 _(UNSUPPORTED_PROTOCOL, "unsupported protocol")         \
121 _(OUT_OF_PORTS, "out of ports")                         \
122 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
123 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")   \
124 _(NON_SYN, "non-SYN packet try to create session")      \
125 _(TCP_CLOSED, "drops due to TCP in transitory timeout")
126
127 typedef enum
128 {
129 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
130   foreach_nat_in2out_ed_error
131 #undef _
132     NAT_IN2OUT_ED_N_ERROR,
133 } nat_in2out_ed_error_t;
134
135 #define foreach_nat_out2in_ed_error                     \
136 _(UNSUPPORTED_PROTOCOL, "unsupported protocol")         \
137 _(OUT_OF_PORTS, "out of ports")                         \
138 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
139 _(NO_TRANSLATION, "no translation")                     \
140 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")   \
141 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
142 _(CANNOT_CREATE_USER, "cannot create NAT user")         \
143 _(NON_SYN, "non-SYN packet try to create session")      \
144 _(TCP_CLOSED, "drops due to TCP in transitory timeout")
145
146 typedef enum
147 {
148 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
149   foreach_nat_out2in_ed_error
150 #undef _
151     NAT_OUT2IN_ED_N_ERROR,
152 } nat_out2in_ed_error_t;
153
154
155 /* Endpoint dependent TCP session state */
156 #define NAT44_SES_I2O_FIN 1
157 #define NAT44_SES_O2I_FIN 2
158 #define NAT44_SES_I2O_FIN_ACK 4
159 #define NAT44_SES_O2I_FIN_ACK 8
160 #define NAT44_SES_I2O_SYN 16
161 #define NAT44_SES_O2I_SYN 32
162 #define NAT44_SES_RST     64
163
164 /* Session flags */
165 #define SNAT_SESSION_FLAG_STATIC_MAPPING       1
166 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO        2
167 #define SNAT_SESSION_FLAG_LOAD_BALANCING       4
168 #define SNAT_SESSION_FLAG_TWICE_NAT            8
169 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT   16
170 #define SNAT_SESSION_FLAG_FWD_BYPASS           32
171 #define SNAT_SESSION_FLAG_AFFINITY             64
172 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE       128
173 #define SNAT_SESSION_FLAG_EXACT_ADDRESS        256
174
175 /* NAT interface flags */
176 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
177 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
178
179 /* Static mapping flags */
180 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY      1
181 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY    2
182 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT   4
183 #define NAT_STATIC_MAPPING_FLAG_LB             8
184 #define NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS  16
185
186 /* *INDENT-OFF* */
187 typedef CLIB_PACKED(struct
188 {
189   // number of sessions in this vrf
190   u32 ses_count;
191
192   u32 rx_fib_index;
193   u32 tx_fib_index;
194
195   // is this vrf expired
196   u8 expired;
197 }) per_vrf_sessions_t;
198 /* *INDENT-ON* */
199
200 /* *INDENT-OFF* */
201 typedef CLIB_PACKED(struct
202 {
203   /* Outside network tuple */
204   struct
205   {
206     ip4_address_t addr;
207     u32 fib_index;
208     u16 port;
209   } out2in;
210
211   /* Inside network tuple */
212   struct
213   {
214     ip4_address_t addr;
215     u32 fib_index;
216     u16 port;
217   } in2out;
218
219   nat_protocol_t nat_proto;
220
221   /* Flags */
222   u32 flags;
223
224   /* Per-user translations */
225   u32 per_user_index;
226   u32 per_user_list_head_index;
227
228   /* head of LRU list in which this session is tracked */
229   u32 lru_head_index;
230   /* index in global LRU list */
231   u32 lru_index;
232   f64 last_lru_update;
233
234   /* Last heard timer */
235   f64 last_heard;
236
237   /* Last HA refresh */
238   f64 ha_last_refreshed;
239
240   /* Counters */
241   u64 total_bytes;
242   u32 total_pkts;
243
244   /* External host address and port */
245   ip4_address_t ext_host_addr;
246   u16 ext_host_port;
247
248   /* External host address and port after translation */
249   ip4_address_t ext_host_nat_addr;
250   u16 ext_host_nat_port;
251
252   /* TCP session state */
253   u8 state;
254   u32 i2o_fin_seq;
255   u32 o2i_fin_seq;
256   u64 tcp_closed_timestamp;
257
258   /* user index */
259   u32 user_index;
260
261   /* per vrf sessions index */
262   u32 per_vrf_sessions_index;
263
264 }) snat_session_t;
265 /* *INDENT-ON* */
266
267 typedef struct
268 {
269   ip4_address_t addr;
270   u32 fib_index;
271   u32 sessions_per_user_list_head_index;
272   u32 nsessions;
273   u32 nstaticsessions;
274 } snat_user_t;
275
276 typedef struct
277 {
278   ip4_address_t addr;
279   u32 fib_index;
280 /* *INDENT-OFF* */
281 #define _(N, i, n, s) \
282   u32 busy_##n##_ports; \
283   u32 * busy_##n##_ports_per_thread; \
284   u32 busy_##n##_port_refcounts[65535];
285   foreach_nat_protocol
286 #undef _
287 /* *INDENT-ON* */
288 } snat_address_t;
289
290 typedef struct
291 {
292   u32 fib_index;
293   u32 ref_count;
294 } nat_fib_t;
295
296 typedef struct
297 {
298   u32 fib_index;
299   u32 refcount;
300 } nat_outside_fib_t;
301
302 typedef struct
303 {
304   /* backend IP address */
305   ip4_address_t addr;
306   /* backend port number */
307   u16 port;
308   /* probability of the backend to be randomly matched */
309   u8 probability;
310   u8 prefix;
311   /* backend FIB table */
312   u32 vrf_id;
313   u32 fib_index;
314 } nat44_lb_addr_port_t;
315
316 typedef enum
317 {
318   /* twice-nat disabled */
319   TWICE_NAT_DISABLED,
320   /* twice-nat enabled */
321   TWICE_NAT,
322   /* twice-nat only when src IP equals dst IP after translation */
323   TWICE_NAT_SELF,
324 } twice_nat_type_t;
325
326 typedef enum
327 {
328   /* no load-balancing */
329   NO_LB_NAT,
330   /* load-balancing */
331   LB_NAT,
332   /* load-balancing with affinity */
333   AFFINITY_LB_NAT,
334 } lb_nat_type_t;
335
336 typedef struct
337 {
338   /* prefered pool address */
339   ip4_address_t pool_addr;
340   /* local IP address */
341   ip4_address_t local_addr;
342   /* external IP address */
343   ip4_address_t external_addr;
344   /* local port */
345   u16 local_port;
346   /* external port */
347   u16 external_port;
348   /* is twice-nat */
349   twice_nat_type_t twice_nat;
350   /* local FIB table */
351   u32 vrf_id;
352   u32 fib_index;
353   /* protocol */
354   nat_protocol_t proto;
355   /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
356   u32 affinity;
357   /* worker threads used by backends/local host */
358   u32 *workers;
359   /* opaque string tag */
360   u8 *tag;
361   /* backends for load-balancing mode */
362   nat44_lb_addr_port_t *locals;
363   /* affinity per service lis */
364   u32 affinity_per_service_list_head_index;
365   /* flags */
366   u32 flags;
367 } snat_static_mapping_t;
368
369 typedef struct
370 {
371   u32 sw_if_index;
372   u8 flags;
373 } snat_interface_t;
374
375 typedef struct
376 {
377   ip4_address_t l_addr;
378   ip4_address_t pool_addr;
379   u16 l_port;
380   u16 e_port;
381   u32 sw_if_index;
382   u32 vrf_id;
383   nat_protocol_t proto;
384   u32 flags;
385   int addr_only;
386   int twice_nat;
387   int is_add;
388   int out2in_only;
389   int identity_nat;
390   int exact;
391   u8 *tag;
392 } snat_static_map_resolve_t;
393
394 typedef struct
395 {
396   /* Main lookup tables */
397   clib_bihash_8_8_t out2in;
398   clib_bihash_8_8_t in2out;
399
400   /* Endpoint dependent sessions lookup tables */
401   clib_bihash_16_8_t in2out_ed;
402
403   /* Find-a-user => src address lookup */
404   clib_bihash_8_8_t user_hash;
405
406   /* User pool */
407   snat_user_t *users;
408
409   /* Session pool */
410   snat_session_t *sessions;
411
412   /* Pool of doubly-linked list elements */
413   dlist_elt_t *list_pool;
414
415   /* LRU session list - head is stale, tail is fresh */
416   dlist_elt_t *lru_pool;
417   u32 tcp_trans_lru_head_index;
418   u32 tcp_estab_lru_head_index;
419   u32 udp_lru_head_index;
420   u32 icmp_lru_head_index;
421   u32 unk_proto_lru_head_index;
422
423   /* NAT thread index */
424   u32 snat_thread_index;
425
426   /* real thread index */
427   u32 thread_index;
428
429   per_vrf_sessions_t *per_vrf_sessions_vec;
430
431 } snat_main_per_thread_data_t;
432
433 struct snat_main_s;
434
435 /* ICMP session match function */
436 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
437                                           vlib_node_runtime_t * node,
438                                           u32 thread_index,
439                                           vlib_buffer_t * b0,
440                                           ip4_header_t * ip0,
441                                           ip4_address_t * addr,
442                                           u16 * port,
443                                           u32 * fib_index,
444                                           nat_protocol_t * proto,
445                                           void *d, void *e,
446                                           u8 * dont_translate);
447
448 /* Return worker thread index for given packet */
449 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
450                                                  u32 rx_fib_index,
451                                                  u8 is_output);
452
453 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
454                                                  ip4_header_t * ip,
455                                                  u32 rx_fib_index,
456                                                  u8 is_output);
457
458 /* NAT address and port allocation function */
459 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
460                                                       addresses,
461                                                       u32 fib_index,
462                                                       u32 thread_index,
463                                                       nat_protocol_t proto,
464                                                       ip4_address_t * addr,
465                                                       u16 * port,
466                                                       u16 port_per_thread,
467                                                       u32 snat_thread_index);
468
469 #define foreach_nat_counter _ (tcp) _ (udp) _ (icmp) _ (other) _ (drops)
470
471 typedef struct snat_main_s
472 {
473   /* ICMP session match functions */
474   snat_icmp_match_function_t *icmp_match_in2out_cb;
475   snat_icmp_match_function_t *icmp_match_out2in_cb;
476
477   /* Thread settings */
478   u32 num_workers;
479   u32 first_worker_index;
480   u32 *workers;
481   snat_get_worker_in2out_function_t *worker_in2out_cb;
482   snat_get_worker_out2in_function_t *worker_out2in_cb;
483   u16 port_per_thread;
484
485   /* Per thread data */
486   snat_main_per_thread_data_t *per_thread_data;
487
488   /* Find a static mapping by local */
489   clib_bihash_8_8_t static_mapping_by_local;
490
491   /* Find a static mapping by external */
492   clib_bihash_8_8_t static_mapping_by_external;
493
494   /* Static mapping pool */
495   snat_static_mapping_t *static_mappings;
496
497   /* Endpoint-dependent out2in mappings */
498   clib_bihash_16_8_t out2in_ed;
499
500   /* Interface pool */
501   snat_interface_t *interfaces;
502   snat_interface_t *output_feature_interfaces;
503
504   /* Vector of outside addresses */
505   snat_address_t *addresses;
506   /* Address and port allocation function */
507   nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
508   /* Address and port allocation type */
509   nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
510   /* Port set parameters (MAP-E) */
511   u8 psid_offset;
512   u8 psid_length;
513   u16 psid;
514   /* Port range parameters */
515   u16 start_port;
516   u16 end_port;
517
518   /* vector of fibs */
519   nat_fib_t *fibs;
520
521   /* vector of outside fibs */
522   nat_outside_fib_t *outside_fibs;
523
524   /* Vector of twice NAT addresses for external hosts */
525   snat_address_t *twice_nat_addresses;
526
527   /* sw_if_indices whose intfc addresses should be auto-added */
528   u32 *auto_add_sw_if_indices;
529   u32 *auto_add_sw_if_indices_twice_nat;
530
531   /* vector of interface address static mappings to resolve. */
532   snat_static_map_resolve_t *to_resolve;
533
534   /* Randomize port allocation order */
535   u32 random_seed;
536
537   /* Worker handoff frame-queue index */
538   u32 fq_in2out_index;
539   u32 fq_in2out_output_index;
540   u32 fq_out2in_index;
541
542   /* node indexes */
543   u32 error_node_index;
544
545   /* handoff fq nodes  */
546   u32 handoff_out2in_index;
547   u32 handoff_in2out_index;
548   u32 handoff_in2out_output_index;
549
550   /* respect feature arc nodes */
551   u32 pre_out2in_node_index;
552   u32 pre_in2out_node_index;
553
554   u32 in2out_node_index;
555   u32 in2out_output_node_index;
556   u32 in2out_fast_node_index;
557   u32 in2out_slowpath_node_index;
558   u32 in2out_slowpath_output_node_index;
559   u32 ed_in2out_node_index;
560   u32 ed_in2out_slowpath_node_index;
561   u32 out2in_node_index;
562   u32 out2in_fast_node_index;
563   u32 ed_out2in_node_index;
564   u32 ed_out2in_slowpath_node_index;
565
566   u32 hairpinning_node_index;
567   u32 hairpin_dst_node_index;
568   u32 hairpin_src_node_index;
569   u32 ed_hairpinning_node_index;
570   u32 ed_hairpin_dst_node_index;
571   u32 ed_hairpin_src_node_index;
572
573   /* If forwarding is enabled */
574   u8 forwarding_enabled;
575
576   /* Config parameters */
577   u8 static_mapping_only;
578   u8 static_mapping_connection_tracking;
579   u8 out2in_dpo;
580   u8 endpoint_dependent;
581
582   /* Is translation memory size calculated or user defined */
583   u8 translation_memory_size_set;
584
585   u32 translation_buckets;
586   uword translation_memory_size;
587   u32 max_translations_per_thread;
588   u32 *max_translations_per_fib;
589   u32 max_users_per_thread;
590   u32 user_buckets;
591   uword user_memory_size;
592   u32 max_translations_per_user;
593
594   u32 outside_vrf_id;
595   u32 outside_fib_index;
596   u32 inside_vrf_id;
597   u32 inside_fib_index;
598
599   /* values of various timeouts */
600   // proto timeouts
601   u32 udp_timeout;
602   u32 tcp_transitory_timeout;
603   u32 tcp_established_timeout;
604   u32 icmp_timeout;
605
606   /* TCP MSS clamping */
607   u16 mss_clamping;
608
609   /* counters/gauges */
610   vlib_simple_counter_main_t total_users;
611   vlib_simple_counter_main_t total_sessions;
612   vlib_simple_counter_main_t user_limit_reached;
613
614 #define _(x) vlib_simple_counter_main_t x;
615   struct
616   {
617     struct
618     {
619       struct
620       {
621         foreach_nat_counter;
622       } in2out;
623
624       struct
625       {
626         foreach_nat_counter;
627       } out2in;
628
629       struct
630       {
631         foreach_nat_counter;
632       } in2out_ed;
633
634       struct
635       {
636         foreach_nat_counter;
637       } out2in_ed;
638     } fastpath;
639
640     struct
641     {
642       struct
643       {
644         foreach_nat_counter;
645       } in2out;
646
647       struct
648       {
649         foreach_nat_counter;
650       } out2in;
651
652       struct
653       {
654         foreach_nat_counter;
655       } in2out_ed;
656
657       struct
658       {
659         foreach_nat_counter;
660       } out2in_ed;
661     } slowpath;
662
663     vlib_simple_counter_main_t hairpinning;
664   } counters;
665 #undef _
666
667   /* API message ID base */
668   u16 msg_id_base;
669
670   /* log class */
671   vlib_log_class_t log_class;
672   /* logging level */
673   u8 log_level;
674
675   /* convenience */
676   vnet_main_t *vnet_main;
677   ip4_main_t *ip4_main;
678   ip_lookup_main_t *ip4_lookup_main;
679   api_main_t *api_main;
680 } snat_main_t;
681
682 typedef struct
683 {
684   u32 thread_index;
685   f64 now;
686 } nat44_is_idle_session_ctx_t;
687
688 typedef struct
689 {
690   u32 cached_sw_if_index;
691   u32 cached_ip4_address;
692 } snat_runtime_t;
693
694 extern snat_main_t snat_main;
695
696 // nat pre ed next_node feature classification
697 extern vlib_node_registration_t nat_default_node;
698 extern vlib_node_registration_t nat_pre_in2out_node;
699 extern vlib_node_registration_t nat_pre_out2in_node;
700
701 extern vlib_node_registration_t snat_in2out_node;
702 extern vlib_node_registration_t snat_in2out_output_node;
703 extern vlib_node_registration_t snat_out2in_node;
704 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
705 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
706 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
707 extern vlib_node_registration_t nat44_ed_in2out_node;
708 extern vlib_node_registration_t nat44_ed_in2out_output_node;
709 extern vlib_node_registration_t nat44_ed_out2in_node;
710
711 extern fib_source_t nat_fib_src_hi;
712 extern fib_source_t nat_fib_src_low;
713
714 /* format functions */
715 format_function_t format_snat_user;
716 format_function_t format_snat_static_mapping;
717 format_function_t format_snat_static_map_to_resolve;
718 format_function_t format_snat_session;
719 format_function_t format_snat_key;
720 format_function_t format_static_mapping_key;
721 format_function_t format_nat_protocol;
722 format_function_t format_nat_addr_and_port_alloc_alg;
723 /* unformat functions */
724 unformat_function_t unformat_nat_protocol;
725
726 /** \brief Check if SNAT session is created from static mapping.
727     @param s SNAT session
728     @return 1 if SNAT session is created from static mapping otherwise 0
729 */
730 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
731
732 /** \brief Check if SNAT session for unknown protocol.
733     @param s SNAT session
734     @return 1 if SNAT session for unknown protocol otherwise 0
735 */
736 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
737
738 /** \brief Check if NAT session is twice NAT.
739     @param s NAT session
740     @return 1 if NAT session is twice NAT
741 */
742 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
743
744 /** \brief Check if NAT session is load-balancing.
745     @param s NAT session
746     @return 1 if NAT session is load-balancing
747 */
748 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
749
750 /** \brief Check if NAT session is forwarding bypass.
751     @param s NAT session
752     @return 1 if NAT session is load-balancing
753 */
754 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
755
756 /** \brief Check if NAT session is endpoint dependent.
757     @param s NAT session
758     @return 1 if NAT session is endpoint dependent
759 */
760 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
761
762 /** \brief Check if NAT session has affinity record.
763     @param s NAT session
764     @return 1 if NAT session has affinity record
765 */
766 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
767
768 /** \brief Check if exact pool address should be used.
769     @param s SNAT session
770     @return 1 if exact pool address or 0
771 */
772 #define is_exact_address_session(s) (s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS)
773
774 /** \brief Check if NAT interface is inside.
775     @param i NAT interface
776     @return 1 if inside interface
777 */
778 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
779
780 /** \brief Check if NAT interface is outside.
781     @param i NAT interface
782     @return 1 if outside interface
783 */
784 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
785
786 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
787     @param s NAT session
788     @return 1 if session is closed
789 */
790 #define nat44_is_ses_closed(s) s->state == 0xf
791
792 /** \brief Check if NAT static mapping is address only (1:1NAT).
793     @param sm NAT static mapping
794     @return 1 if 1:1NAT, 0 if 1:1NAPT
795 */
796 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
797
798 /** \brief Check if NAT static mapping match only out2in direction.
799     @param sm NAT static mapping
800     @return 1 if rule match only out2in direction
801 */
802 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
803
804 /** \brief Check if NAT static mapping is identity NAT.
805     @param sm NAT static mapping
806     @return 1 if identity NAT
807 */
808 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
809
810 /** \brief Check if NAT static mapping is load-balancing.
811     @param sm NAT static mapping
812     @return 1 if load-balancing
813 */
814 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
815
816 /** \brief Check if exact pool address should be used.
817     @param s SNAT session
818     @return 1 if exact pool address or 0
819 */
820 #define is_exact_address(s) (s->flags & NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS)
821
822 /** \brief Check if client initiating TCP connection (received SYN from client)
823     @param t TCP header
824     @return 1 if client initiating TCP connection
825 */
826 always_inline bool
827 tcp_flags_is_init (u8 f)
828 {
829   return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
830 }
831
832 /* logging */
833 #define nat_log_err(...) \
834   vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
835 #define nat_log_warn(...) \
836   vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
837 #define nat_log_notice(...) \
838   vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
839 #define nat_log_info(...) \
840   vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
841 #define nat_log_debug(...)\
842   vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
843
844 /* NAT API Logging Levels */
845 #define foreach_nat_log_level \
846   _(0x00, LOG_NONE)           \
847   _(0x01, LOG_ERROR)          \
848   _(0x02, LOG_WARNING)        \
849   _(0x03, LOG_NOTICE)         \
850   _(0x04, LOG_INFO)           \
851   _(0x05, LOG_DEBUG)
852
853 typedef enum nat_log_level_t_
854 {
855 #define _(n,f) SNAT_##f = n,
856   foreach_nat_log_level
857 #undef _
858 } nat_log_level_t;
859
860 #define nat_elog(_level, _str)                           \
861 do                                                       \
862   {                                                      \
863     snat_main_t *sm = &snat_main;                        \
864     if (PREDICT_FALSE (sm->log_level >= _level))         \
865       {                                                  \
866         ELOG_TYPE_DECLARE (e) =                          \
867           {                                              \
868             .format = "nat-msg " _str,                   \
869             .format_args = "",                           \
870           };                                             \
871         ELOG_DATA (&vlib_global_main.elog_main, e);      \
872       }                                                  \
873   } while (0);
874
875 #define nat_elog_addr(_level, _str, _addr)               \
876 do                                                       \
877   {                                                      \
878     if (PREDICT_FALSE (sm->log_level >= _level))         \
879       {                                                  \
880         ELOG_TYPE_DECLARE (e) =                          \
881           {                                              \
882             .format = "nat-msg " _str " %d.%d.%d.%d",    \
883             .format_args = "i1i1i1i1",                   \
884           };                                             \
885         CLIB_PACKED(struct                               \
886           {                                              \
887             u8 oct1;                                     \
888             u8 oct2;                                     \
889             u8 oct3;                                     \
890             u8 oct4;                                     \
891           }) *ed;                                        \
892         ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
893         ed->oct4 = _addr >> 24;                          \
894         ed->oct3 = _addr >> 16;                          \
895         ed->oct2 = _addr >> 8;                           \
896         ed->oct1 = _addr;                                \
897     }                                                    \
898   } while (0);
899
900 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst)                \
901 do                                                                          \
902   {                                                                         \
903   if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG))                      \
904     {                                                                       \
905       ELOG_TYPE_DECLARE (e) =                                               \
906         {                                                                   \
907           .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
908                                     " tid from: %d to: %d fib: %d",         \
909         .format_args = "i1i1i1i1i1i1i1i1i4i4i4",                            \
910       };                                                                    \
911       CLIB_PACKED(struct                                                    \
912         {                                                                   \
913           u8 src_oct1;                                                      \
914           u8 src_oct2;                                                      \
915           u8 src_oct3;                                                      \
916           u8 src_oct4;                                                      \
917           u8 dst_oct1;                                                      \
918           u8 dst_oct2;                                                      \
919           u8 dst_oct3;                                                      \
920           u8 dst_oct4;                                                      \
921           u32 ftid;                                                         \
922           u32 ttid;                                                         \
923           u32 fib;                                                          \
924         }) *ed;                                                             \
925       ed = ELOG_DATA (&vlib_global_main.elog_main, e);                      \
926       ed->src_oct1 = _src >> 24;                                            \
927       ed->src_oct2 = _src >> 16;                                            \
928       ed->src_oct3 = _src >> 8;                                             \
929       ed->src_oct4 = _src;                                                  \
930       ed->dst_oct1 = _dst >> 24;                                            \
931       ed->dst_oct2 = _dst >> 16;                                            \
932       ed->dst_oct3 = _dst >> 8;                                             \
933       ed->dst_oct4 = _dst;                                                  \
934       ed->ftid = vlib_get_thread_index ();                                  \
935       ed->ttid = _tid;                                                      \
936       ed->fib = _fib;                                                       \
937     }                                                                       \
938   } while (0);
939
940 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst)              \
941 do                                                                           \
942   {                                                                          \
943   if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG))                       \
944     {                                                                        \
945       ELOG_TYPE_DECLARE (e) =                                                \
946         {                                                                    \
947           .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
948                                     " tid:%d prt:%d fib:%d",                 \
949         .format_args = "i1i1i1i1i1i1i1i1i4i4i4",                             \
950       };                                                                     \
951       CLIB_PACKED(struct                                                     \
952         {                                                                    \
953           u8 src_oct1;                                                       \
954           u8 src_oct2;                                                       \
955           u8 src_oct3;                                                       \
956           u8 src_oct4;                                                       \
957           u8 dst_oct1;                                                       \
958           u8 dst_oct2;                                                       \
959           u8 dst_oct3;                                                       \
960           u8 dst_oct4;                                                       \
961           u32 tid;                                                           \
962           u32 prt;                                                           \
963           u32 fib;                                                           \
964         }) *ed;                                                              \
965       ed = ELOG_DATA (&vlib_global_main.elog_main, e);                       \
966       ed->src_oct1 = _src >> 24;                                             \
967       ed->src_oct2 = _src >> 16;                                             \
968       ed->src_oct3 = _src >> 8;                                              \
969       ed->src_oct4 = _src;                                                   \
970       ed->dst_oct1 = _dst >> 24;                                             \
971       ed->dst_oct2 = _dst >> 16;                                             \
972       ed->dst_oct3 = _dst >> 8;                                              \
973       ed->dst_oct4 = _dst;                                                   \
974       ed->tid = vlib_get_thread_index ();                                    \
975       ed->prt = _prt;                                                        \
976       ed->fib = _fib;                                                        \
977     }                                                                        \
978   } while (0);
979
980 #define nat_elog_X1(_level, _fmt, _arg, _val1)            \
981 do                                                        \
982   {                                                       \
983     snat_main_t *sm = &snat_main;                         \
984     if (PREDICT_FALSE (sm->log_level >= _level))          \
985       {                                                   \
986         ELOG_TYPE_DECLARE (e) =                           \
987           {                                               \
988             .format = "nat-msg " _fmt,                    \
989             .format_args = _arg,                          \
990           };                                              \
991         CLIB_PACKED(struct                                \
992           {                                               \
993             typeof (_val1) val1;                          \
994           }) *ed;                                         \
995         ed = ELOG_DATA (&vlib_global_main.elog_main, e);  \
996         ed->val1 = _val1;                                 \
997       }                                                   \
998   } while (0);
999
1000 #define nat_elog_notice(nat_elog_str) \
1001   nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
1002 #define nat_elog_warn(nat_elog_str) \
1003   nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1004 #define nat_elog_err(nat_elog_str) \
1005   nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1006 #define nat_elog_debug(nat_elog_str) \
1007   nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1008 #define nat_elog_info(nat_elog_str) \
1009   nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1010
1011 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1012   nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1013 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1014   nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1015 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1016   nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1017 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1018   nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1019 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1020   nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1021
1022 /* ICMP session match functions */
1023 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1024                             u32 thread_index, vlib_buffer_t * b0,
1025                             ip4_header_t * ip0, ip4_address_t * addr,
1026                             u16 * port, u32 * fib_index,
1027                             nat_protocol_t * proto, void *d, void *e,
1028                             u8 * dont_translate);
1029 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1030                             u32 thread_index, vlib_buffer_t * b0,
1031                             ip4_header_t * ip0, ip4_address_t * addr,
1032                             u16 * port, u32 * fib_index,
1033                             nat_protocol_t * proto, void *d, void *e,
1034                             u8 * dont_translate);
1035 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1036                             u32 thread_index, vlib_buffer_t * b0,
1037                             ip4_header_t * ip0, ip4_address_t * addr,
1038                             u16 * port, u32 * fib_index,
1039                             nat_protocol_t * proto, void *d, void *e,
1040                             u8 * dont_translate);
1041 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1042                             u32 thread_index, vlib_buffer_t * b0,
1043                             ip4_header_t * ip0, ip4_address_t * addr,
1044                             u16 * port, u32 * fib_index,
1045                             nat_protocol_t * proto, void *d, void *e,
1046                             u8 * dont_translate);
1047
1048 /* ICMP endpoint-dependent session match functions */
1049 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1050                           u32 thread_index, vlib_buffer_t * b0,
1051                           ip4_header_t * ip0, ip4_address_t * addr,
1052                           u16 * port, u32 * fib_index, nat_protocol_t * proto,
1053                           void *d, void *e, u8 * dont_translate);
1054 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1055                           u32 thread_index, vlib_buffer_t * b0,
1056                           ip4_header_t * ip0, ip4_address_t * addr,
1057                           u16 * port, u32 * fib_index, nat_protocol_t * proto,
1058                           void *d, void *e, u8 * dont_translate);
1059
1060 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1061                  icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1062                  vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1063                  void *d, void *e);
1064
1065 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1066                  icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1067                  vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1068                  void *d, void *e);
1069
1070 /* hairpinning functions */
1071 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1072                            ip4_header_t * ip0, icmp46_header_t * icmp0,
1073                            int is_ed);
1074 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1075                                        ip4_header_t * ip);
1076 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1077                                          ip4_header_t * ip);
1078 int snat_hairpinning (vlib_main_t * vm, vlib_node_runtime_t * node,
1079                       snat_main_t * sm, vlib_buffer_t * b0,
1080                       ip4_header_t * ip0, udp_header_t * udp0,
1081                       tcp_header_t * tcp0, u32 proto0, int is_ed,
1082                       int do_trace);
1083
1084 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1085 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1086 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1087 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1088 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1089
1090 /**
1091  * @brief Add external address to NAT44 pool
1092  *
1093  * @param sm        snat global configuration data
1094  * @param addr      IPv4 address
1095  * @param vrf_id    VRF id of tenant, ~0 means independent of VRF
1096  * @param twice_nat 1 if twice NAT address
1097  *
1098  * @return 0 on success, non-zero value otherwise
1099  */
1100 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1101                       u8 twice_nat);
1102
1103 /**
1104  * @brief Delete external address from NAT44 pool
1105  *
1106  * @param sm        snat global configuration data
1107  * @param addr      IPv4 address
1108  * @param delete_sm 1 if delete static mapping using address
1109  * @param twice_nat 1 if twice NAT address
1110  *
1111  * @return 0 on success, non-zero value otherwise
1112  */
1113 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1114                       u8 twice_nat);
1115
1116 /**
1117  * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1118  *
1119  * @param addr   IPv4 address
1120  * @param is_add 1 = add, 0 = delete
1121  *
1122  * @return 0 on success, non-zero value otherwise
1123  */
1124 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1125
1126 /**
1127  * @brief Add/delete NAT44 static mapping
1128  *
1129  * @param l_addr       local IPv4 address
1130  * @param e_addr       external IPv4 address
1131  * @param l_port       local port number
1132  * @param e_port       external port number
1133  * @param vrf_id       local VRF ID
1134  * @param addr_only    1 = 1:1NAT, 0 = 1:1NAPT
1135  * @param sw_if_index  use interface address as external IPv4 address
1136  * @param proto        L4 protocol
1137  * @param is_add       1 = add, 0 = delete
1138  * @param twice_nat    twice-nat mode
1139  * @param out2in_only  if 1 rule match only out2in direction
1140  * @param tag          opaque string tag
1141  * @param identity_nat identity NAT
1142  * @param pool_addr    pool IPv4 address
1143  * @param exact        1 = exact pool address
1144  *
1145  * @return 0 on success, non-zero value otherwise
1146  */
1147 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1148                              u16 l_port, u16 e_port, u32 vrf_id,
1149                              int addr_only, u32 sw_if_index,
1150                              nat_protocol_t proto, int is_add,
1151                              twice_nat_type_t twice_nat, u8 out2in_only,
1152                              u8 * tag, u8 identity_nat,
1153                              ip4_address_t pool_addr, int exact);
1154
1155 /**
1156  * @brief Add/delete static mapping with load-balancing (multiple backends)
1157  *
1158  * @param e_addr      external IPv4 address
1159  * @param e_port      external port number
1160  * @param proto       L4 protocol
1161  * @param locals      list of local backends
1162  * @param is_add      1 = add, 0 = delete
1163  * @param twice_nat   twice-nat mode
1164  * @param out2in_only if 1 rule match only out2in direction
1165  * @param tag         opaque string tag
1166  * @param affinity    0 = disabled, otherwise client IP affinity sticky time
1167  *
1168  * @return 0 on success, non-zero value otherwise
1169  */
1170 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1171                                      nat_protocol_t proto,
1172                                      nat44_lb_addr_port_t * locals, u8 is_add,
1173                                      twice_nat_type_t twice_nat,
1174                                      u8 out2in_only, u8 * tag, u32 affinity);
1175
1176 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1177                                            ip4_address_t l_addr, u16 l_port,
1178                                            nat_protocol_t proto, u32 vrf_id,
1179                                            u8 probability, u8 is_add);
1180
1181 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1182
1183 /**
1184  * @brief Set NAT plugin workers
1185  *
1186  * @param bitmap NAT workers bitmap
1187  *
1188  * @return 0 on success, non-zero value otherwise
1189  */
1190 int snat_set_workers (uword * bitmap);
1191
1192 /**
1193  * @brief Enable/disable NAT44 feature on the interface
1194  *
1195  * @param sw_if_index software index of the interface
1196  * @param is_inside   1 = inside, 0 = outside
1197  * @param is_del      1 = delete, 0 = add
1198  *
1199  * @return 0 on success, non-zero value otherwise
1200  */
1201 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1202
1203 /**
1204  * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1205  *
1206  * @param sw_if_index software index of the interface
1207  * @param is_inside   1 = inside, 0 = outside
1208  * @param is_del      1 = delete, 0 = add
1209  *
1210  * @return 0 on success, non-zero value otherwise
1211  */
1212 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1213                                            int is_del);
1214
1215 /**
1216  * @brief Add/delete NAT44 pool address from specific interface
1217  *
1218  * @param sw_if_index software index of the interface
1219  * @param is_del      1 = delete, 0 = add
1220  * @param twice_nat   1 = twice NAT address for external hosts
1221  *
1222  * @return 0 on success, non-zero value otherwise
1223  */
1224 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1225                                 u8 twice_nat);
1226
1227 /**
1228  * @brief Delete NAT44 session
1229  *
1230  * @param addr   IPv4 address
1231  * @param port   L4 port number
1232  * @param proto  L4 protocol
1233  * @param vrf_id VRF ID
1234  * @param is_in  1 = inside network address and port pair, 0 = outside
1235  *
1236  * @return 0 on success, non-zero value otherwise
1237  */
1238 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1239                        nat_protocol_t proto, u32 vrf_id, int is_in);
1240
1241 /**
1242  * @brief Delete NAT44 endpoint-dependent session
1243  *
1244  * @param sm     snat global configuration data
1245  * @param addr   IPv4 address
1246  * @param port   L4 port number
1247  * @param proto  L4 protocol
1248  * @param vrf_id VRF ID
1249  * @param is_in  1 = inside network address and port pair, 0 = outside
1250  *
1251  * @return 0 on success, non-zero value otherwise
1252  */
1253 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1254                           ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1255                           u32 vrf_id, int is_in);
1256
1257 /**
1258  * @brief Free NAT44 session data (lookup keys, external address port)
1259  *
1260  * @param sm           snat global configuration data
1261  * @param s            NAT session
1262  * @param thread_index thread index
1263  * @param is_ha        is HA event
1264  */
1265 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1266                             u32 thread_index, u8 is_ha);
1267
1268 /**
1269  * @brief Set NAT44 session limit (session limit, vrf id)
1270  *
1271  * @param session_limit Session limit
1272  * @param vrf_id        VRF id
1273  * @return 0 on success, non-zero value otherwise
1274  */
1275 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1276
1277 /**
1278  * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
1279  *
1280  * @param session_limit Session limit
1281  * @param vrf_id        VRF id
1282  * @return 0 on success, non-zero value otherwise
1283  */
1284 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
1285 /**
1286  * @brief Free NAT44 ED session data (lookup keys, external address port)
1287  *
1288  * @param s            NAT session
1289  * @param thread_index thread index
1290  * @param is_ha        is HA event
1291  */
1292 void
1293 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1294                          u32 thread_index, u8 is_ha);
1295
1296 /**
1297  * @brief Initialize NAT44 data
1298  *
1299  * @param tsm          per thread data
1300  */
1301 void nat44_db_init (snat_main_per_thread_data_t * tsm);
1302
1303 /**
1304  * @brief Free NAT44 data
1305  *
1306  * @param tsm          per thread data
1307  */
1308 void nat44_db_free (snat_main_per_thread_data_t * tsm);
1309
1310 void nat44_sessions_clear ();
1311
1312 /**
1313  * @brief Find or create NAT user
1314  *
1315  * @param sm           snat global configuration data
1316  * @param addr         IPv4 address
1317  * @param fib_index    FIB table index
1318  * @param thread_index thread index
1319  *
1320  * @return NAT user data structure on success otherwise zero value
1321  */
1322 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1323                                      ip4_address_t * addr, u32 fib_index,
1324                                      u32 thread_index);
1325
1326 /**
1327  * @brief Allocate new NAT session or recycle last used
1328  *
1329  * @param sm           snat global configuration data
1330  * @param u            NAT user
1331  * @param thread_index thread index
1332  * @param now          time now
1333  *
1334  * @return session data structure on success otherwise zero value
1335  */
1336 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1337                                               snat_user_t * u,
1338                                               u32 thread_index, f64 now);
1339
1340 /**
1341  * @brief Set address and port assignment algorithm for MAP-E CE
1342  *
1343  * @param psid        Port Set Identifier value
1344  * @param psid_offset number of offset bits
1345  * @param psid_length length of PSID
1346  */
1347 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1348                                        u16 psid_length);
1349
1350 /**
1351  * @brief Set address and port assignment algorithm for port range
1352  *
1353  * @param start_port beginning of the port range
1354  * @param end_port   end of the port range
1355  */
1356 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1357
1358 /**
1359  * @brief Set address and port assignment algorithm to default/standard
1360  */
1361 void nat_set_alloc_addr_and_port_default (void);
1362
1363 /**
1364  * @brief Free outside address and port pair
1365  *
1366  * @param addresses    vector of outside addresses
1367  * @param thread_index thread index
1368  * @param key          address, port and protocol
1369  */
1370 void
1371 snat_free_outside_address_and_port (snat_address_t * addresses,
1372                                     u32 thread_index,
1373                                     ip4_address_t * addr,
1374                                     u16 port, nat_protocol_t protocol);
1375
1376 /**
1377  * @brief Alloc outside address and port
1378  *
1379  * @param addresses         vector of outside addresses
1380  * @param fib_index         FIB table index
1381  * @param thread_index      thread index
1382  * @param port_per_thread   number of ports per thread
1383  * @param snat_thread_index NAT thread index
1384  *
1385  * @return 0 on success, non-zero value otherwise
1386  */
1387 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1388                                          u32 fib_index,
1389                                          u32 thread_index,
1390                                          nat_protocol_t proto,
1391                                          ip4_address_t * addr,
1392                                          u16 * port,
1393                                          u16 port_per_thread,
1394                                          u32 snat_thread_index);
1395
1396 void expire_per_vrf_sessions (u32 fib_index);
1397
1398 /**
1399  * @brief Match NAT44 static mapping.
1400  *
1401  * @param key             address and port to match
1402  * @param addr            external/local address of the matched mapping
1403  * @param port            port of the matched mapping
1404  * @param fib_index       fib index of the matched mapping
1405  * @param by_external     if 0 match by local address otherwise match by external
1406  *                        address
1407  * @param is_addr_only    1 if matched mapping is address only
1408  * @param twice_nat       matched mapping is twice NAT type
1409  * @param lb              1 if matched mapping is load-balanced
1410  * @param ext_host_addr   external host address
1411  * @param is_identity_nat 1 if indentity mapping
1412  * @param out             if !=0 set to pointer of the mapping structure
1413  *
1414  * @returns 0 if match found otherwise 1.
1415  */
1416 int snat_static_mapping_match (snat_main_t * sm,
1417                                ip4_address_t match_addr,
1418                                u16 match_port,
1419                                u32 match_fib_index,
1420                                nat_protocol_t match_protocol,
1421                                ip4_address_t * mapping_addr,
1422                                u16 * mapping_port,
1423                                u32 * mapping_fib_index,
1424                                u8 by_external,
1425                                u8 * is_addr_only,
1426                                twice_nat_type_t * twice_nat,
1427                                lb_nat_type_t * lb,
1428                                ip4_address_t * ext_host_addr,
1429                                u8 * is_identity_nat,
1430                                snat_static_mapping_t ** out);
1431
1432 /**
1433  * @brief Add/del NAT address to FIB.
1434  *
1435  * Add the external NAT address to the FIB as receive entries. This ensures
1436  * that VPP will reply to ARP for this address and we don't need to enable
1437  * proxy ARP on the outside interface.
1438  *
1439  * @param addr        IPv4 address
1440  * @param plen        address prefix length
1441  * @param sw_if_index software index of the outside interface
1442  * @param is_add      0 = delete, 1 = add.
1443  */
1444 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1445                                u8 p_len, u32 sw_if_index, int is_add);
1446
1447 /*
1448  * Why is this here? Because we don't need to touch this layer to
1449  * simply reply to an icmp. We need to change id to a unique
1450  * value to NAT an echo request/reply.
1451  */
1452
1453 typedef struct
1454 {
1455   u16 identifier;
1456   u16 sequence;
1457 } icmp_echo_header_t;
1458
1459 typedef struct
1460 {
1461   u16 src_port, dst_port;
1462 } tcp_udp_header_t;
1463
1464 u8 *format_user_kvp (u8 * s, va_list * args);
1465
1466 #endif /* __included_nat_h__ */
1467 /*
1468  * fd.io coding-style-patch-verification: ON
1469  *
1470  * Local Variables:
1471  * eval: (c-set-style "gnu")
1472  * End:
1473  */