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