nat: optimize flow matching in ED NAT
[vpp.git] / src / plugins / nat / nat44-ed / nat44_ed.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_nat44_ed_h__
20 #define __included_nat44_ed_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
35 #include <nat/lib/lib.h>
36 #include <nat/lib/inlines.h>
37
38 /* default number of worker handoff frame queue elements */
39 #define NAT_FQ_NELTS_DEFAULT 64
40
41 /* NAT buffer flags */
42 #define SNAT_FLAG_HAIRPINNING (1 << 0)
43
44 /* NAT44 API Configuration flags */
45 #define foreach_nat44_config_flag  \
46   _(0x00, IS_ENDPOINT_INDEPENDENT) \
47   _(0x01, IS_ENDPOINT_DEPENDENT)   \
48   _(0x02, IS_STATIC_MAPPING_ONLY)  \
49   _(0x04, IS_CONNECTION_TRACKING)  \
50   _(0x08, IS_OUT2IN_DPO)
51
52 typedef enum nat44_config_flags_t_
53 {
54 #define _(n,f) NAT44_API_##f = n,
55   foreach_nat44_config_flag
56 #undef _
57 } nat44_config_flags_t;
58
59 typedef struct
60 {
61   /* nat44 plugin features */
62   u8 static_mapping_only;
63   u8 connection_tracking;
64
65   u32 inside_vrf;
66   u32 outside_vrf;
67
68   /* maximum number of sessions */
69   u32 sessions;
70
71 } nat44_config_t;
72
73 typedef enum
74 {
75   NAT_NEXT_DROP,
76   NAT_NEXT_ICMP_ERROR,
77   NAT_NEXT_IN2OUT_ED_FAST_PATH,
78   NAT_NEXT_IN2OUT_ED_SLOW_PATH,
79   NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH,
80   NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
81   NAT_NEXT_OUT2IN_ED_FAST_PATH,
82   NAT_NEXT_OUT2IN_ED_SLOW_PATH,
83   NAT_NEXT_IN2OUT_CLASSIFY,
84   NAT_NEXT_OUT2IN_CLASSIFY,
85   NAT_N_NEXT,
86 } nat_next_t;
87
88 typedef struct
89 {
90   u32 next_index;
91   u32 arc_next_index;
92 } nat_pre_trace_t;
93
94 /* External address and port allocation modes */
95 #define foreach_nat_addr_and_port_alloc_alg \
96   _(0, DEFAULT, "default")         \
97   _(1, MAPE, "map-e")              \
98   _(2, RANGE, "port-range")
99
100 typedef enum
101 {
102 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
103   foreach_nat_addr_and_port_alloc_alg
104 #undef _
105 } nat_addr_and_port_alloc_alg_t;
106
107 /* Session state */
108 #define foreach_snat_session_state          \
109   _(0, UNKNOWN, "unknown")                 \
110   _(1, UDP_ACTIVE, "udp-active")           \
111   _(2, TCP_SYN_SENT, "tcp-syn-sent")       \
112   _(3, TCP_ESTABLISHED, "tcp-established") \
113   _(4, TCP_FIN_WAIT, "tcp-fin-wait")       \
114   _(5, TCP_CLOSE_WAIT, "tcp-close-wait")   \
115   _(6, TCP_CLOSING, "tcp-closing")         \
116   _(7, TCP_LAST_ACK, "tcp-last-ack")       \
117   _(8, TCP_CLOSED, "tcp-closed")           \
118   _(9, ICMP_ACTIVE, "icmp-active")
119
120 typedef enum
121 {
122 #define _(v, N, s) SNAT_SESSION_##N = v,
123   foreach_snat_session_state
124 #undef _
125 } snat_session_state_t;
126
127 #define foreach_nat_in2out_ed_error                     \
128 _(UNSUPPORTED_PROTOCOL, "unsupported protocol")         \
129 _(OUT_OF_PORTS, "out of ports")                         \
130 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
131 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")   \
132 _(NON_SYN, "non-SYN packet try to create session")      \
133 _(TCP_CLOSED, "drops due to TCP in transitory timeout")
134
135 typedef enum
136 {
137 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
138   foreach_nat_in2out_ed_error
139 #undef _
140     NAT_IN2OUT_ED_N_ERROR,
141 } nat_in2out_ed_error_t;
142
143 #define foreach_nat_out2in_ed_error                                           \
144   _ (UNSUPPORTED_PROTOCOL, "unsupported protocol")                            \
145   _ (OUT_OF_PORTS, "out of ports")                                            \
146   _ (BAD_ICMP_TYPE, "unsupported ICMP type")                                  \
147   _ (NO_TRANSLATION, "no translation")                                        \
148   _ (MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")                      \
149   _ (NON_SYN, "non-SYN packet try to create session")                         \
150   _ (TCP_CLOSED, "drops due to TCP in transitory timeout")                    \
151   _ (HASH_ADD_FAILED, "hash table add failed")
152
153 typedef enum
154 {
155 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
156   foreach_nat_out2in_ed_error
157 #undef _
158     NAT_OUT2IN_ED_N_ERROR,
159 } nat_out2in_ed_error_t;
160
161
162 /* Endpoint dependent TCP session state */
163 #define NAT44_SES_I2O_FIN 1
164 #define NAT44_SES_O2I_FIN 2
165 #define NAT44_SES_I2O_FIN_ACK 4
166 #define NAT44_SES_O2I_FIN_ACK 8
167 #define NAT44_SES_I2O_SYN 16
168 #define NAT44_SES_O2I_SYN 32
169 #define NAT44_SES_RST     64
170
171 /* Session flags */
172 #define SNAT_SESSION_FLAG_STATIC_MAPPING     (1 << 0)
173 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO      (1 << 1)
174 #define SNAT_SESSION_FLAG_LOAD_BALANCING     (1 << 2)
175 #define SNAT_SESSION_FLAG_TWICE_NAT          (1 << 3)
176 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT (1 << 4)
177 #define SNAT_SESSION_FLAG_FWD_BYPASS         (1 << 5)
178 #define SNAT_SESSION_FLAG_AFFINITY           (1 << 6)
179 #define SNAT_SESSION_FLAG_EXACT_ADDRESS      (1 << 7)
180 #define SNAT_SESSION_FLAG_HAIRPINNING        (1 << 8)
181
182 /* NAT interface flags */
183 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
184 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
185
186 /* Static mapping flags */
187 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY      1
188 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY    2
189 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT   4
190 #define NAT_STATIC_MAPPING_FLAG_LB             8
191 #define NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS  16
192
193 /* *INDENT-OFF* */
194 typedef CLIB_PACKED(struct
195 {
196   // number of sessions in this vrf
197   u32 ses_count;
198
199   u32 rx_fib_index;
200   u32 tx_fib_index;
201
202   // is this vrf expired
203   u8 expired;
204 }) per_vrf_sessions_t;
205 /* *INDENT-ON* */
206
207 typedef union
208 {
209   struct
210   {
211     ip4_address_t saddr, daddr;
212     u16 sport; // ICMP id for ICMP case
213     u16 dport;
214     u32 fib_index : 24;
215     u8 proto;
216   };
217   u64 as_u64[2];
218   u64x2u as_u128;
219 } nat_6t_t;
220
221 STATIC_ASSERT_SIZEOF (nat_6t_t, 2 * sizeof (u64));
222
223 typedef struct
224 {
225 #define NAT_FLOW_OP_SADDR_REWRITE   (1 << 1)
226 #define NAT_FLOW_OP_SPORT_REWRITE   (1 << 2)
227 #define NAT_FLOW_OP_DADDR_REWRITE   (1 << 3)
228 #define NAT_FLOW_OP_DPORT_REWRITE   (1 << 4)
229 #define NAT_FLOW_OP_ICMP_ID_REWRITE (1 << 5)
230 #define NAT_FLOW_OP_TXFIB_REWRITE   (1 << 6)
231   int ops;
232   nat_6t_t match;
233   struct
234   {
235     ip4_address_t saddr, daddr;
236     u16 sport;
237     u16 dport;
238     u32 fib_index;
239     u8 proto;
240     u16 icmp_id;
241   } rewrite;
242   uword l3_csum_delta;
243   uword l4_csum_delta;
244 } nat_6t_flow_t;
245
246 void nat44_ed_forwarding_enable_disable (u8 is_enable);
247
248 always_inline void
249 nat_6t_flow_saddr_rewrite_set (nat_6t_flow_t *f, u32 saddr)
250 {
251   f->ops |= NAT_FLOW_OP_SADDR_REWRITE;
252   f->rewrite.saddr.as_u32 = saddr;
253 }
254
255 always_inline void
256 nat_6t_flow_daddr_rewrite_set (nat_6t_flow_t *f, u32 daddr)
257 {
258   f->ops |= NAT_FLOW_OP_DADDR_REWRITE;
259   f->rewrite.daddr.as_u32 = daddr;
260 }
261
262 always_inline void
263 nat_6t_flow_sport_rewrite_set (nat_6t_flow_t *f, u32 sport)
264 {
265   f->ops |= NAT_FLOW_OP_SPORT_REWRITE;
266   f->rewrite.sport = sport;
267 }
268
269 always_inline void
270 nat_6t_flow_dport_rewrite_set (nat_6t_flow_t *f, u32 dport)
271 {
272   f->ops |= NAT_FLOW_OP_DPORT_REWRITE;
273   f->rewrite.dport = dport;
274 }
275
276 always_inline void
277 nat_6t_flow_txfib_rewrite_set (nat_6t_flow_t *f, u32 tx_fib_index)
278 {
279   f->ops |= NAT_FLOW_OP_TXFIB_REWRITE;
280   f->rewrite.fib_index = tx_fib_index;
281 }
282
283 always_inline void
284 nat_6t_flow_icmp_id_rewrite_set (nat_6t_flow_t *f, u16 id)
285 {
286   f->ops |= NAT_FLOW_OP_ICMP_ID_REWRITE;
287   f->rewrite.icmp_id = id;
288 }
289
290 /* *INDENT-OFF* */
291 typedef CLIB_PACKED(struct
292 {
293   /* Outside network tuple */
294   struct
295   {
296     ip4_address_t addr;
297     u32 fib_index;
298     u16 port;
299   } out2in;
300
301   /* Inside network tuple */
302   struct
303   {
304     ip4_address_t addr;
305     u32 fib_index;
306     u16 port;
307   } in2out;
308
309   nat_protocol_t nat_proto;
310
311   nat_6t_flow_t i2o;
312   nat_6t_flow_t o2i;
313
314   /* Flags */
315   u32 flags;
316
317   /* head of LRU list in which this session is tracked */
318   u32 lru_head_index;
319   /* index in global LRU list */
320   u32 lru_index;
321   f64 last_lru_update;
322
323   /* Last heard timer */
324   f64 last_heard;
325
326   /* Last HA refresh */
327   f64 ha_last_refreshed;
328
329   /* Counters */
330   u64 total_bytes;
331   u32 total_pkts;
332
333   /* External host address and port */
334   ip4_address_t ext_host_addr;
335   u16 ext_host_port;
336
337   /* External host address and port after translation */
338   ip4_address_t ext_host_nat_addr;
339   u16 ext_host_nat_port;
340
341   /* TCP session state */
342   u8 state;
343   u32 i2o_fin_seq;
344   u32 o2i_fin_seq;
345   u64 tcp_closed_timestamp;
346
347   /* per vrf sessions index */
348   u32 per_vrf_sessions_index;
349
350 }) snat_session_t;
351 /* *INDENT-ON* */
352
353 typedef struct
354 {
355   ip4_address_t addr;
356   u32 fib_index;
357 /* *INDENT-OFF* */
358 #define _(N, i, n, s) \
359   u32 busy_##n##_ports; \
360   u32 * busy_##n##_ports_per_thread; \
361   u32 busy_##n##_port_refcounts[65535];
362   foreach_nat_protocol
363 #undef _
364 /* *INDENT-ON* */
365 } snat_address_t;
366
367 typedef struct
368 {
369   u32 fib_index;
370   u32 ref_count;
371 } nat_fib_t;
372
373 typedef struct
374 {
375   u32 fib_index;
376   u32 refcount;
377 } nat_outside_fib_t;
378
379 typedef struct
380 {
381   /* backend IP address */
382   ip4_address_t addr;
383   /* backend port number */
384   u16 port;
385   /* probability of the backend to be randomly matched */
386   u8 probability;
387   u8 prefix;
388   /* backend FIB table */
389   u32 vrf_id;
390   u32 fib_index;
391 } nat44_lb_addr_port_t;
392
393 typedef enum
394 {
395   /* twice-nat disabled */
396   TWICE_NAT_DISABLED,
397   /* twice-nat enabled */
398   TWICE_NAT,
399   /* twice-nat only when src IP equals dst IP after translation */
400   TWICE_NAT_SELF,
401 } twice_nat_type_t;
402
403 typedef enum
404 {
405   /* no load-balancing */
406   NO_LB_NAT,
407   /* load-balancing */
408   LB_NAT,
409   /* load-balancing with affinity */
410   AFFINITY_LB_NAT,
411 } lb_nat_type_t;
412
413 typedef struct
414 {
415   /* prefered pool address */
416   ip4_address_t pool_addr;
417   /* local IP address */
418   ip4_address_t local_addr;
419   /* external IP address */
420   ip4_address_t external_addr;
421   /* local port */
422   u16 local_port;
423   /* external port */
424   u16 external_port;
425   /* is twice-nat */
426   twice_nat_type_t twice_nat;
427   /* local FIB table */
428   u32 vrf_id;
429   u32 fib_index;
430   /* protocol */
431   nat_protocol_t proto;
432   /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
433   u32 affinity;
434   /* worker threads used by backends/local host */
435   u32 *workers;
436   /* opaque string tag */
437   u8 *tag;
438   /* backends for load-balancing mode */
439   nat44_lb_addr_port_t *locals;
440   /* affinity per service lis */
441   u32 affinity_per_service_list_head_index;
442   /* flags */
443   u32 flags;
444 } snat_static_mapping_t;
445
446 typedef struct
447 {
448   u32 sw_if_index;
449   u8 flags;
450 } snat_interface_t;
451
452 typedef struct
453 {
454   ip4_address_t l_addr;
455   ip4_address_t pool_addr;
456   u16 l_port;
457   u16 e_port;
458   u32 sw_if_index;
459   u32 vrf_id;
460   nat_protocol_t proto;
461   u32 flags;
462   int addr_only;
463   int twice_nat;
464   int out2in_only;
465   int identity_nat;
466   int exact;
467   u8 *tag;
468 } snat_static_map_resolve_t;
469
470 typedef struct
471 {
472   /* Session pool */
473   snat_session_t *sessions;
474
475   /* Pool of doubly-linked list elements */
476   dlist_elt_t *list_pool;
477
478   /* LRU session list - head is stale, tail is fresh */
479   dlist_elt_t *lru_pool;
480   u32 tcp_trans_lru_head_index;
481   u32 tcp_estab_lru_head_index;
482   u32 udp_lru_head_index;
483   u32 icmp_lru_head_index;
484   u32 unk_proto_lru_head_index;
485
486   /* NAT thread index */
487   u32 snat_thread_index;
488
489   /* real thread index */
490   u32 thread_index;
491
492   per_vrf_sessions_t *per_vrf_sessions_vec;
493
494 } snat_main_per_thread_data_t;
495
496 struct snat_main_s;
497
498 /* Return worker thread index for given packet */
499 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
500                                                  u32 rx_fib_index,
501                                                  u8 is_output);
502
503 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
504                                                  ip4_header_t * ip,
505                                                  u32 rx_fib_index,
506                                                  u8 is_output);
507
508 /* NAT address and port allocation function */
509 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
510                                                       addresses,
511                                                       u32 fib_index,
512                                                       u32 thread_index,
513                                                       nat_protocol_t proto,
514                                                       ip4_address_t * addr,
515                                                       u16 * port,
516                                                       u16 port_per_thread,
517                                                       u32 snat_thread_index);
518
519 typedef struct snat_main_s
520 {
521   /* Thread settings */
522   u32 num_workers;
523   u32 first_worker_index;
524   u32 *workers;
525   snat_get_worker_in2out_function_t *worker_in2out_cb;
526   snat_get_worker_out2in_function_t *worker_out2in_cb;
527   u16 port_per_thread;
528
529   /* Per thread data */
530   snat_main_per_thread_data_t *per_thread_data;
531
532   /* Find a static mapping by local */
533   clib_bihash_8_8_t static_mapping_by_local;
534
535   /* Find a static mapping by external */
536   clib_bihash_8_8_t static_mapping_by_external;
537
538   /* Static mapping pool */
539   snat_static_mapping_t *static_mappings;
540
541   /* Endpoint independent lookup tables */
542   clib_bihash_8_8_t in2out;
543   clib_bihash_8_8_t out2in;
544
545   /* Endpoint dependent lookup table */
546   clib_bihash_16_8_t flow_hash;
547
548   /* Interface pool */
549   snat_interface_t *interfaces;
550   snat_interface_t *output_feature_interfaces;
551
552   /* Vector of outside addresses */
553   snat_address_t *addresses;
554   /* Address and port allocation function */
555   nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
556   /* Address and port allocation type */
557   nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
558   /* Port set parameters (MAP-E) */
559   u8 psid_offset;
560   u8 psid_length;
561   u16 psid;
562   /* Port range parameters */
563   u16 start_port;
564   u16 end_port;
565
566   /* vector of fibs */
567   nat_fib_t *fibs;
568
569   /* vector of outside fibs */
570   nat_outside_fib_t *outside_fibs;
571
572   /* Vector of twice NAT addresses for external hosts */
573   snat_address_t *twice_nat_addresses;
574
575   /* sw_if_indices whose intfc addresses should be auto-added */
576   u32 *auto_add_sw_if_indices;
577   u32 *auto_add_sw_if_indices_twice_nat;
578
579   /* vector of interface address static mappings to resolve. */
580   snat_static_map_resolve_t *to_resolve;
581
582   /* Randomize port allocation order */
583   u32 random_seed;
584
585   /* Worker handoff frame-queue index */
586   u32 fq_in2out_index;
587   u32 fq_in2out_output_index;
588   u32 fq_out2in_index;
589
590   u32 out2in_node_index;
591   u32 in2out_node_index;
592   u32 in2out_output_node_index;
593
594   nat44_config_t rconfig;
595   //nat44_config_t cconfig;
596
597   /* If forwarding is enabled */
598   u8 forwarding_enabled;
599
600   /* static mapping config */
601   u8 static_mapping_only;
602   u8 static_mapping_connection_tracking;
603
604   /* Is translation memory size calculated or user defined */
605   u8 translation_memory_size_set;
606
607   u32 translation_buckets;
608   u32 max_translations_per_thread;
609   u32 *max_translations_per_fib;
610
611   u32 outside_vrf_id;
612   u32 outside_fib_index;
613   u32 inside_vrf_id;
614   u32 inside_fib_index;
615
616   nat_timeouts_t timeouts;
617
618   /* TCP MSS clamping */
619   u16 mss_clamping;
620
621   /* counters */
622   vlib_simple_counter_main_t total_sessions;
623
624 #define _(x) vlib_simple_counter_main_t x;
625   struct
626   {
627     struct
628     {
629       struct
630       {
631         foreach_nat_counter;
632       } in2out;
633
634       struct
635       {
636         foreach_nat_counter;
637       } out2in;
638     } fastpath;
639
640     struct
641     {
642       struct
643       {
644         foreach_nat_counter;
645       } in2out;
646
647       struct
648       {
649         foreach_nat_counter;
650       } out2in;
651     } slowpath;
652
653     vlib_simple_counter_main_t hairpinning;
654   } counters;
655 #undef _
656
657   /* API message ID base */
658   u16 msg_id_base;
659
660   /* log class */
661   vlib_log_class_t log_class;
662   /* logging level */
663   u8 log_level;
664
665   /* convenience */
666   api_main_t *api_main;
667   ip4_main_t *ip4_main;
668   ip_lookup_main_t *ip4_lookup_main;
669
670   fib_source_t fib_src_hi;
671   fib_source_t fib_src_low;
672
673   /* pat - dynamic mapping enabled or conneciton tracking */
674   u8 pat;
675
676   /* number of worker handoff frame queue elements */
677   u32 frame_queue_nelts;
678
679   /* nat44 plugin enabled */
680   u8 enabled;
681
682   vnet_main_t *vnet_main;
683
684 } snat_main_t;
685
686 typedef struct
687 {
688   u32 thread_index;
689   f64 now;
690 } nat44_is_idle_session_ctx_t;
691
692 typedef struct
693 {
694   u32 cached_sw_if_index;
695   u32 cached_ip4_address;
696 } snat_runtime_t;
697
698 extern snat_main_t snat_main;
699
700 // nat pre ed next_node feature classification
701 extern vlib_node_registration_t nat_default_node;
702 extern vlib_node_registration_t nat_pre_in2out_node;
703 extern vlib_node_registration_t nat_pre_out2in_node;
704
705 extern vlib_node_registration_t snat_in2out_node;
706 extern vlib_node_registration_t snat_in2out_output_node;
707 extern vlib_node_registration_t snat_out2in_node;
708 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
709 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
710 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
711 extern vlib_node_registration_t nat44_ed_in2out_node;
712 extern vlib_node_registration_t nat44_ed_in2out_output_node;
713 extern vlib_node_registration_t nat44_ed_out2in_node;
714
715 extern fib_source_t nat_fib_src_hi;
716 extern fib_source_t nat_fib_src_low;
717
718 /* format functions */
719 format_function_t format_snat_static_mapping;
720 format_function_t format_snat_static_map_to_resolve;
721 format_function_t format_snat_session;
722 format_function_t format_snat_key;
723 format_function_t format_static_mapping_key;
724 format_function_t format_nat_protocol;
725 format_function_t format_nat_addr_and_port_alloc_alg;
726 /* unformat functions */
727 unformat_function_t unformat_nat_protocol;
728
729 /** \brief Check if SNAT session is created from static mapping.
730     @param s SNAT session
731     @return 1 if SNAT session is created from static mapping otherwise 0
732 */
733 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
734
735 /** \brief Check if SNAT session for unknown protocol.
736     @param s SNAT session
737     @return 1 if SNAT session for unknown protocol otherwise 0
738 */
739 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
740
741 /** \brief Check if NAT session is twice NAT.
742     @param s NAT session
743     @return 1 if NAT session is twice NAT
744 */
745 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
746
747 /** \brief Check if NAT session is load-balancing.
748     @param s NAT session
749     @return 1 if NAT session is load-balancing
750 */
751 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
752
753 /** \brief Check if NAT session is forwarding bypass.
754     @param s NAT session
755     @return 1 if NAT session is load-balancing
756 */
757 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
758
759 /** \brief Check if NAT session is endpoint dependent.
760     @param s NAT session
761     @return 1 if NAT session is endpoint dependent
762 */
763 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
764
765 /** \brief Check if NAT session has affinity record.
766     @param s NAT session
767     @return 1 if NAT session has affinity record
768 */
769 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
770
771 /** \brief Check if exact pool address should be used.
772     @param s SNAT session
773     @return 1 if exact pool address or 0
774 */
775 #define is_exact_address_session(s) (s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS)
776
777 /** \brief Check if NAT interface is inside.
778     @param i NAT interface
779     @return 1 if inside interface
780 */
781 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
782
783 /** \brief Check if NAT interface is outside.
784     @param i NAT interface
785     @return 1 if outside interface
786 */
787 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
788
789 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
790     @param s NAT session
791     @return 1 if session is closed
792 */
793 #define nat44_is_ses_closed(s) s->state == 0xf
794
795 /** \brief Check if NAT static mapping is address only (1:1NAT).
796     @param sm NAT static mapping
797     @return 1 if 1:1NAT, 0 if 1:1NAPT
798 */
799 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
800
801 /** \brief Check if NAT static mapping match only out2in direction.
802     @param sm NAT static mapping
803     @return 1 if rule match only out2in direction
804 */
805 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
806
807 /** \brief Check if NAT static mapping is identity NAT.
808     @param sm NAT static mapping
809     @return 1 if identity NAT
810 */
811 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
812
813 /** \brief Check if NAT static mapping is load-balancing.
814     @param sm NAT static mapping
815     @return 1 if load-balancing
816 */
817 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
818
819 /** \brief Check if exact pool address should be used.
820     @param s SNAT session
821     @return 1 if exact pool address or 0
822 */
823 #define is_exact_address(s) (s->flags & NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS)
824
825 /** \brief Check if client initiating TCP connection (received SYN from client)
826     @param t TCP header
827     @return 1 if client initiating TCP connection
828 */
829 always_inline bool
830 tcp_flags_is_init (u8 f)
831 {
832   return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
833 }
834
835 /* logging */
836 #define nat_log_err(...) \
837   vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
838 #define nat_log_warn(...) \
839   vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
840 #define nat_log_notice(...) \
841   vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
842 #define nat_log_info(...) \
843   vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
844 #define nat_log_debug(...)\
845   vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
846
847 /**
848  * @brief Enable NAT44 plugin
849  *
850  * @param c         nat44_config_t
851  *
852  * @return 0 on success, non-zero value otherwise
853  */
854 int nat44_plugin_enable (nat44_config_t c);
855
856 /**
857  * @brief Disable NAT44 plugin
858  *
859  * @return 0 on success, non-zero value otherwise
860  */
861 int nat44_plugin_disable ();
862
863 /**
864  * @brief Add external address to NAT44 pool
865  *
866  * @param sm        snat global configuration data
867  * @param addr      IPv4 address
868  * @param vrf_id    VRF id of tenant, ~0 means independent of VRF
869  * @param twice_nat 1 if twice NAT address
870  *
871  * @return 0 on success, non-zero value otherwise
872  */
873 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
874                       u8 twice_nat);
875
876 /**
877  * @brief Delete external address from NAT44 pool
878  *
879  * @param sm        snat global configuration data
880  * @param addr      IPv4 address
881  * @param delete_sm 1 if delete static mapping using address
882  * @param twice_nat 1 if twice NAT address
883  *
884  * @return 0 on success, non-zero value otherwise
885  */
886 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
887                       u8 twice_nat);
888
889 /**
890  * @brief Add/delete external address to FIB DPO (out2in DPO mode)
891  *
892  * @param addr   IPv4 address
893  * @param is_add 1 = add, 0 = delete
894  *
895  * @return 0 on success, non-zero value otherwise
896  */
897 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
898
899 /**
900  * @brief Add/delete NAT44 static mapping
901  *
902  * @param l_addr       local IPv4 address
903  * @param e_addr       external IPv4 address
904  * @param l_port       local port number
905  * @param e_port       external port number
906  * @param vrf_id       local VRF ID
907  * @param addr_only    1 = 1:1NAT, 0 = 1:1NAPT
908  * @param sw_if_index  use interface address as external IPv4 address
909  * @param proto        L4 protocol
910  * @param is_add       1 = add, 0 = delete
911  * @param twice_nat    twice-nat mode
912  * @param out2in_only  if 1 rule match only out2in direction
913  * @param tag          opaque string tag
914  * @param identity_nat identity NAT
915  * @param pool_addr    pool IPv4 address
916  * @param exact        1 = exact pool address
917  *
918  * @return 0 on success, non-zero value otherwise
919  */
920 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
921                              u16 l_port, u16 e_port, u32 vrf_id,
922                              int addr_only, u32 sw_if_index,
923                              nat_protocol_t proto, int is_add,
924                              twice_nat_type_t twice_nat, u8 out2in_only,
925                              u8 * tag, u8 identity_nat,
926                              ip4_address_t pool_addr, int exact);
927
928 /**
929  * @brief Add/delete static mapping with load-balancing (multiple backends)
930  *
931  * @param e_addr      external IPv4 address
932  * @param e_port      external port number
933  * @param proto       L4 protocol
934  * @param locals      list of local backends
935  * @param is_add      1 = add, 0 = delete
936  * @param twice_nat   twice-nat mode
937  * @param out2in_only if 1 rule match only out2in direction
938  * @param tag         opaque string tag
939  * @param affinity    0 = disabled, otherwise client IP affinity sticky time
940  *
941  * @return 0 on success, non-zero value otherwise
942  */
943 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
944                                      nat_protocol_t proto,
945                                      nat44_lb_addr_port_t * locals, u8 is_add,
946                                      twice_nat_type_t twice_nat,
947                                      u8 out2in_only, u8 * tag, u32 affinity);
948
949 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
950                                            ip4_address_t l_addr, u16 l_port,
951                                            nat_protocol_t proto, u32 vrf_id,
952                                            u8 probability, u8 is_add);
953
954 clib_error_t *nat44_api_hookup (vlib_main_t * vm);
955
956 /**
957  * @brief Set NAT plugin workers
958  *
959  * @param bitmap NAT workers bitmap
960  *
961  * @return 0 on success, non-zero value otherwise
962  */
963 int snat_set_workers (uword * bitmap);
964
965 /**
966  * @brief Set NAT plugin number of frame queue elements
967  *
968  * @param frame_queue_nelts number of worker handoff frame queue elements
969  *
970  * @return 0 on success, non-zero value otherwise
971  */
972 int snat_set_frame_queue_nelts (u32 frame_queue_nelts);
973
974 /**
975  * @brief Enable/disable NAT44 feature on the interface
976  *
977  * @param sw_if_index software index of the interface
978  * @param is_inside   1 = inside, 0 = outside
979  * @param is_del      1 = delete, 0 = add
980  *
981  * @return 0 on success, non-zero value otherwise
982  */
983 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
984
985 /**
986  * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
987  *
988  * @param sw_if_index software index of the interface
989  * @param is_inside   1 = inside, 0 = outside
990  * @param is_del      1 = delete, 0 = add
991  *
992  * @return 0 on success, non-zero value otherwise
993  */
994 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
995                                            int is_del);
996
997 /**
998  * @brief Add/delete NAT44 pool address from specific interface
999  *
1000  * @param sw_if_index software index of the interface
1001  * @param is_del      1 = delete, 0 = add
1002  * @param twice_nat   1 = twice NAT address for external hosts
1003  *
1004  * @return 0 on success, non-zero value otherwise
1005  */
1006 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1007                                 u8 twice_nat);
1008
1009 /**
1010  * @brief Delete NAT44 endpoint-dependent session
1011  *
1012  * @param sm     snat global configuration data
1013  * @param addr   IPv4 address
1014  * @param port   L4 port number
1015  * @param proto  L4 protocol
1016  * @param vrf_id VRF ID
1017  * @param is_in  1 = inside network address and port pair, 0 = outside
1018  *
1019  * @return 0 on success, non-zero value otherwise
1020  */
1021 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1022                           ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1023                           u32 vrf_id, int is_in);
1024
1025 /**
1026  * @brief Free NAT44 session data (lookup keys, external address port)
1027  *
1028  * @param sm           snat global configuration data
1029  * @param s            NAT session
1030  * @param thread_index thread index
1031  * @param is_ha        is HA event
1032  */
1033 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1034                             u32 thread_index, u8 is_ha);
1035
1036 /**
1037  * @brief Set NAT44 session limit (session limit, vrf id)
1038  *
1039  * @param session_limit Session limit
1040  * @param vrf_id        VRF id
1041  * @return 0 on success, non-zero value otherwise
1042  */
1043 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1044
1045 /**
1046  * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
1047  *
1048  * @param session_limit Session limit
1049  * @param vrf_id        VRF id
1050  * @return 0 on success, non-zero value otherwise
1051  */
1052 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
1053
1054 /**
1055  * @brief Free outside address and port pair
1056  *
1057  * @param addresses    vector of outside addresses
1058  * @param thread_index thread index
1059  * @param key          address, port and protocol
1060  */
1061 void
1062 snat_free_outside_address_and_port (snat_address_t * addresses,
1063                                     u32 thread_index,
1064                                     ip4_address_t * addr,
1065                                     u16 port, nat_protocol_t protocol);
1066
1067 void expire_per_vrf_sessions (u32 fib_index);
1068
1069 /**
1070  * @brief Match NAT44 static mapping.
1071  *
1072  * @param key             address and port to match
1073  * @param addr            external/local address of the matched mapping
1074  * @param port            port of the matched mapping
1075  * @param fib_index       fib index of the matched mapping
1076  * @param by_external     if 0 match by local address otherwise match by external
1077  *                        address
1078  * @param is_addr_only    1 if matched mapping is address only
1079  * @param twice_nat       matched mapping is twice NAT type
1080  * @param lb              1 if matched mapping is load-balanced
1081  * @param ext_host_addr   external host address
1082  * @param is_identity_nat 1 if indentity mapping
1083  * @param out             if !=0 set to pointer of the mapping structure
1084  *
1085  * @returns 0 if match found otherwise 1.
1086  */
1087 int snat_static_mapping_match (snat_main_t * sm,
1088                                ip4_address_t match_addr,
1089                                u16 match_port,
1090                                u32 match_fib_index,
1091                                nat_protocol_t match_protocol,
1092                                ip4_address_t * mapping_addr,
1093                                u16 * mapping_port,
1094                                u32 * mapping_fib_index,
1095                                u8 by_external,
1096                                u8 * is_addr_only,
1097                                twice_nat_type_t * twice_nat,
1098                                lb_nat_type_t * lb,
1099                                ip4_address_t * ext_host_addr,
1100                                u8 * is_identity_nat,
1101                                snat_static_mapping_t ** out);
1102
1103 /**
1104  * @brief Add/del NAT address to FIB.
1105  *
1106  * Add the external NAT address to the FIB as receive entries. This ensures
1107  * that VPP will reply to ARP for this address and we don't need to enable
1108  * proxy ARP on the outside interface.
1109  *
1110  * @param addr        IPv4 address
1111  * @param plen        address prefix length
1112  * @param sw_if_index software index of the outside interface
1113  * @param is_add      0 = delete, 1 = add.
1114  */
1115 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1116                                u8 p_len, u32 sw_if_index, int is_add);
1117
1118 #if 0
1119 void
1120 nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
1121                    ip4_address_t * out_addr, u16 out_port,
1122                    ip4_address_t * eh_addr, u16 eh_port,
1123                    ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
1124                    u32 fib_index, u16 flags, u32 thread_index);
1125
1126 void
1127 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
1128                    ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1129                    u32 fib_index, u32 ti);
1130
1131 void
1132 nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
1133                    ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1134                    u32 fib_index, u32 total_pkts, u64 total_bytes,
1135                    u32 thread_index);
1136 #endif
1137
1138 int nat_set_outside_address_and_port (snat_address_t *addresses,
1139                                       u32 thread_index, ip4_address_t addr,
1140                                       u16 port, nat_protocol_t protocol);
1141
1142 /*
1143  * Why is this here? Because we don't need to touch this layer to
1144  * simply reply to an icmp. We need to change id to a unique
1145  * value to NAT an echo request/reply.
1146  */
1147
1148 typedef struct
1149 {
1150   u16 identifier;
1151   u16 sequence;
1152 } icmp_echo_header_t;
1153
1154 typedef struct
1155 {
1156   u16 src_port, dst_port;
1157 } tcp_udp_header_t;
1158
1159 u32 get_thread_idx_by_port (u16 e_port);
1160
1161 u8 *format_static_mapping_kvp (u8 *s, va_list *args);
1162
1163 u8 *format_session_kvp (u8 *s, va_list *args);
1164
1165 u32 nat_calc_bihash_buckets (u32 n_elts);
1166
1167 void nat44_addresses_free (snat_address_t **addresses);
1168
1169 void nat44_ed_sessions_clear ();
1170
1171 int nat44_ed_set_frame_queue_nelts (u32 frame_queue_nelts);
1172
1173 typedef enum
1174 {
1175   NAT_ED_TRNSL_ERR_SUCCESS = 0,
1176   NAT_ED_TRNSL_ERR_TRANSLATION_FAILED = 1,
1177   NAT_ED_TRNSL_ERR_FLOW_MISMATCH = 2,
1178 } nat_translation_error_e;
1179
1180 nat_translation_error_e
1181 nat_6t_flow_buf_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
1182                            nat_6t_flow_t *f, nat_protocol_t proto,
1183                            int is_output_feature);
1184
1185 void nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f);
1186
1187 format_function_t format_nat_ed_translation_error;
1188 format_function_t format_nat_6t_flow;
1189 format_function_t format_ed_session_kvp;
1190
1191 #endif /* __included_nat44_ed_h__ */
1192 /*
1193  * fd.io coding-style-patch-verification: ON
1194  *
1195  * Local Variables:
1196  * eval: (c-set-style "gnu")
1197  * End:
1198  */