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