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