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