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