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