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