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