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