nat: nat44 cli bug fix
[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 #ifndef __included_nat44_ed_h__
17 #define __included_nat44_ed_h__
18
19 #include <vnet/vnet.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/ip/icmp46_packet.h>
23 #include <vnet/api_errno.h>
24 #include <vnet/fib/fib_source.h>
25 #include <vppinfra/elog.h>
26 #include <vppinfra/bihash_8_8.h>
27 #include <vppinfra/bihash_16_8.h>
28 #include <vppinfra/hash.h>
29 #include <vppinfra/dlist.h>
30 #include <vppinfra/error.h>
31 #include <vlibapi/api.h>
32
33 #include <nat/lib/lib.h>
34 #include <nat/lib/inlines.h>
35
36 /* default number of worker handoff frame queue elements */
37 #define NAT_FQ_NELTS_DEFAULT 64
38
39 /* number of attempts to get a port for ED overloading algorithm, if rolling
40  * a dice this many times doesn't produce a free port, it's treated
41  * as if there were no free ports available to conserve resources */
42 #define ED_PORT_ALLOC_ATTEMPTS (10)
43
44 /* NAT buffer flags */
45 #define SNAT_FLAG_HAIRPINNING (1 << 0)
46
47 /* NAT44 API Configuration flags */
48 #define foreach_nat44_config_flag                                             \
49   _ (0x00, IS_ENDPOINT_INDEPENDENT)                                           \
50   _ (0x01, IS_ENDPOINT_DEPENDENT)                                             \
51   _ (0x02, IS_STATIC_MAPPING_ONLY)                                            \
52   _ (0x04, IS_CONNECTION_TRACKING)
53
54 typedef enum nat44_config_flags_t_
55 {
56 #define _(n,f) NAT44_API_##f = n,
57   foreach_nat44_config_flag
58 #undef _
59 } nat44_config_flags_t;
60
61 typedef struct
62 {
63   u32 inside_vrf;
64   u32 outside_vrf;
65   u32 sessions;
66 } nat44_config_t;
67
68 typedef enum
69 {
70   NAT_NEXT_DROP,
71   NAT_NEXT_ICMP_ERROR,
72   NAT_NEXT_IN2OUT_ED_FAST_PATH,
73   NAT_NEXT_IN2OUT_ED_SLOW_PATH,
74   NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH,
75   NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
76   NAT_NEXT_OUT2IN_ED_FAST_PATH,
77   NAT_NEXT_OUT2IN_ED_SLOW_PATH,
78   NAT_NEXT_IN2OUT_CLASSIFY,
79   NAT_NEXT_OUT2IN_CLASSIFY,
80   NAT_N_NEXT,
81 } nat_next_t;
82
83 typedef struct
84 {
85   u32 next_index;
86   u32 arc_next_index;
87 } nat_pre_trace_t;
88
89 #define foreach_nat_in2out_ed_error                                           \
90   _ (UNSUPPORTED_PROTOCOL, "unsupported protocol")                            \
91   _ (OUT_OF_PORTS, "out of ports")                                            \
92   _ (BAD_ICMP_TYPE, "unsupported ICMP type")                                  \
93   _ (MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")                      \
94   _ (NON_SYN, "non-SYN packet try to create session")                         \
95   _ (TRNSL_FAILED, "couldn't translate packet")
96
97 typedef enum
98 {
99 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
100   foreach_nat_in2out_ed_error
101 #undef _
102     NAT_IN2OUT_ED_N_ERROR,
103 } nat_in2out_ed_error_t;
104
105 #define foreach_nat_out2in_ed_error                                           \
106   _ (UNSUPPORTED_PROTOCOL, "unsupported protocol")                            \
107   _ (OUT_OF_PORTS, "out of ports")                                            \
108   _ (BAD_ICMP_TYPE, "unsupported ICMP type")                                  \
109   _ (NO_TRANSLATION, "no translation")                                        \
110   _ (MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")                      \
111   _ (NON_SYN, "non-SYN packet try to create session")                         \
112   _ (TCP_CLOSED, "drops due to TCP in transitory timeout")                    \
113   _ (HASH_ADD_FAILED, "hash table add failed")                                \
114   _ (TRNSL_FAILED, "couldn't translate packet")
115
116 typedef enum
117 {
118 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
119   foreach_nat_out2in_ed_error
120 #undef _
121     NAT_OUT2IN_ED_N_ERROR,
122 } nat_out2in_ed_error_t;
123
124 typedef enum
125 {
126   NAT44_ED_TCP_FLAG_NONE = 0,
127   NAT44_ED_TCP_FLAG_FIN,
128   NAT44_ED_TCP_FLAG_SYN,
129   NAT44_ED_TCP_FLAG_SYNFIN,
130   NAT44_ED_TCP_FLAG_RST,
131   NAT44_ED_TCP_FLAG_FINRST,
132   NAT44_ED_TCP_FLAG_SYNRST,
133   NAT44_ED_TCP_FLAG_SYNFINRST,
134   NAT44_ED_TCP_N_FLAG,
135 } nat44_ed_tcp_flag_e;
136
137 typedef enum
138 {
139   NAT44_ED_DIR_I2O = 0,
140   NAT44_ED_DIR_O2I,
141   NAT44_ED_N_DIR,
142 } nat44_ed_dir_e;
143
144 /* Endpoint dependent TCP session state */
145 typedef enum
146 {
147   NAT44_ED_TCP_STATE_CLOSED = 0,
148   NAT44_ED_TCP_STATE_SYN_I2O,
149   NAT44_ED_TCP_STATE_SYN_O2I,
150   NAT44_ED_TCP_STATE_ESTABLISHED,
151   NAT44_ED_TCP_STATE_FIN_I2O,
152   NAT44_ED_TCP_STATE_FIN_O2I,
153   NAT44_ED_TCP_STATE_RST_TRANS,
154   NAT44_ED_TCP_STATE_FIN_TRANS,
155   NAT44_ED_TCP_STATE_FIN_REOPEN_SYN_I2O,
156   NAT44_ED_TCP_STATE_FIN_REOPEN_SYN_O2I,
157   NAT44_ED_TCP_N_STATE,
158 } nat44_ed_tcp_state_e;
159
160 format_function_t format_ed_session_kvp;
161 format_function_t format_snat_session;
162 format_function_t format_snat_static_mapping;
163 format_function_t format_snat_static_map_to_resolve;
164 format_function_t format_nat_ed_translation_error;
165 format_function_t format_nat_6t_flow;
166 format_function_t format_nat_6t;
167 format_function_t format_nat44_ed_tcp_state;
168
169 /* Session flags */
170 #define SNAT_SESSION_FLAG_STATIC_MAPPING     (1 << 0)
171 #define SNAT_SESSION_FLAG_LOAD_BALANCING     (1 << 2)
172 #define SNAT_SESSION_FLAG_TWICE_NAT          (1 << 3)
173 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT (1 << 4)
174 #define SNAT_SESSION_FLAG_FWD_BYPASS         (1 << 5)
175 #define SNAT_SESSION_FLAG_AFFINITY           (1 << 6)
176 #define SNAT_SESSION_FLAG_EXACT_ADDRESS      (1 << 7)
177 #define SNAT_SESSION_FLAG_HAIRPINNING        (1 << 8)
178
179 /* NAT interface flags */
180 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
181 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
182
183 /* Static mapping flags */
184 #define NAT_SM_FLAG_SELF_TWICE_NAT (1 << 1)
185 #define NAT_SM_FLAG_TWICE_NAT      (1 << 2)
186 #define NAT_SM_FLAG_IDENTITY_NAT   (1 << 3)
187 #define NAT_SM_FLAG_ADDR_ONLY      (1 << 4)
188 #define NAT_SM_FLAG_EXACT_ADDRESS  (1 << 5)
189 #define NAT_SM_FLAG_OUT2IN_ONLY    (1 << 6)
190 #define NAT_SM_FLAG_LB             (1 << 7)
191 #define NAT_SM_FLAG_SWITCH_ADDRESS (1 << 8)
192
193 typedef CLIB_PACKED(struct
194 {
195   // number of sessions in this vrf
196   u32 ses_count;
197
198   u32 rx_fib_index;
199   u32 tx_fib_index;
200
201   // is this vrf expired
202   u8 expired;
203 }) per_vrf_sessions_t;
204
205 typedef union
206 {
207   struct
208   {
209     ip4_address_t saddr, daddr;
210     u16 sport; // ICMP id for ICMP case
211     u16 dport;
212     u32 fib_index : 24;
213     u8 proto;
214   };
215   u64 as_u64[2];
216   u64x2u as_u128;
217 } nat_6t_t;
218
219 STATIC_ASSERT_SIZEOF (nat_6t_t, 2 * sizeof (u64));
220
221 typedef struct
222 {
223 #define NAT_FLOW_OP_SADDR_REWRITE   (1 << 1)
224 #define NAT_FLOW_OP_SPORT_REWRITE   (1 << 2)
225 #define NAT_FLOW_OP_DADDR_REWRITE   (1 << 3)
226 #define NAT_FLOW_OP_DPORT_REWRITE   (1 << 4)
227 #define NAT_FLOW_OP_ICMP_ID_REWRITE (1 << 5)
228 #define NAT_FLOW_OP_TXFIB_REWRITE   (1 << 6)
229   int ops;
230   nat_6t_t match;
231   struct
232   {
233     ip4_address_t saddr, daddr;
234     u16 sport;
235     u16 dport;
236     u32 fib_index;
237     u8 proto;
238     u16 icmp_id;
239   } rewrite;
240   uword l3_csum_delta;
241   uword l4_csum_delta;
242 } nat_6t_flow_t;
243
244 void nat44_ed_forwarding_enable_disable (u8 is_enable);
245
246 always_inline void
247 nat_6t_flow_saddr_rewrite_set (nat_6t_flow_t *f, u32 saddr)
248 {
249   f->ops |= NAT_FLOW_OP_SADDR_REWRITE;
250   f->rewrite.saddr.as_u32 = saddr;
251 }
252
253 always_inline void
254 nat_6t_flow_daddr_rewrite_set (nat_6t_flow_t *f, u32 daddr)
255 {
256   f->ops |= NAT_FLOW_OP_DADDR_REWRITE;
257   f->rewrite.daddr.as_u32 = daddr;
258 }
259
260 always_inline void
261 nat_6t_flow_sport_rewrite_set (nat_6t_flow_t *f, u32 sport)
262 {
263   f->ops |= NAT_FLOW_OP_SPORT_REWRITE;
264   f->rewrite.sport = sport;
265 }
266
267 always_inline void
268 nat_6t_flow_dport_rewrite_set (nat_6t_flow_t *f, u32 dport)
269 {
270   f->ops |= NAT_FLOW_OP_DPORT_REWRITE;
271   f->rewrite.dport = dport;
272 }
273
274 always_inline void
275 nat_6t_flow_txfib_rewrite_set (nat_6t_flow_t *f, u32 tx_fib_index)
276 {
277   f->ops |= NAT_FLOW_OP_TXFIB_REWRITE;
278   f->rewrite.fib_index = tx_fib_index;
279 }
280
281 always_inline void
282 nat_6t_flow_icmp_id_rewrite_set (nat_6t_flow_t *f, u16 id)
283 {
284   f->ops |= NAT_FLOW_OP_ICMP_ID_REWRITE;
285   f->rewrite.icmp_id = id;
286 }
287
288 typedef CLIB_PACKED(struct
289 {
290   /* Outside network tuple */
291   struct
292   {
293     ip4_address_t addr;
294     u32 fib_index;
295     u16 port;
296   } out2in;
297
298   /* Inside network tuple */
299   struct
300   {
301     ip4_address_t addr;
302     u32 fib_index;
303     u16 port;
304   } in2out;
305
306   ip_protocol_t proto;
307
308   nat_6t_flow_t i2o;
309   nat_6t_flow_t o2i;
310
311   /* Flags */
312   u32 flags;
313
314   /* head of LRU list in which this session is tracked */
315   u32 lru_head_index;
316   /* index in global LRU list */
317   u32 lru_index;
318   f64 last_lru_update;
319
320   /* Last heard timer */
321   f64 last_heard;
322
323   /* Last HA refresh */
324   f64 ha_last_refreshed;
325
326   /* Counters */
327   u64 total_bytes;
328   u32 total_pkts;
329
330   /* External host address and port */
331   ip4_address_t ext_host_addr;
332   u16 ext_host_port;
333
334   /* External host address and port after translation */
335   ip4_address_t ext_host_nat_addr;
336   u16 ext_host_nat_port;
337
338   /* TCP session state */
339   nat44_ed_tcp_state_e tcp_state;
340
341   /* per vrf sessions index */
342   u32 per_vrf_sessions_index;
343
344   u32 thread_index;
345 }) snat_session_t;
346
347 typedef struct
348 {
349   ip4_address_t addr;
350   ip4_address_t net;
351   u32 sw_if_index;
352   u32 fib_index;
353   u32 addr_len;
354 } snat_address_t;
355
356 typedef struct
357 {
358   /* backend IP address */
359   ip4_address_t addr;
360   /* backend port number */
361   u16 port;
362   /* probability of the backend to be randomly matched */
363   u8 probability;
364   u8 prefix;
365   /* backend FIB table */
366   u32 vrf_id;
367   u32 fib_index;
368 } nat44_lb_addr_port_t;
369
370 typedef enum
371 {
372   /* twice-nat disabled */
373   TWICE_NAT_DISABLED,
374   /* twice-nat enabled */
375   TWICE_NAT,
376   /* twice-nat only when src IP equals dst IP after translation */
377   TWICE_NAT_SELF,
378 } twice_nat_type_t;
379
380 typedef enum
381 {
382   /* no load-balancing */
383   NO_LB_NAT,
384   /* load-balancing */
385   LB_NAT,
386   /* load-balancing with affinity */
387   AFFINITY_LB_NAT,
388 } lb_nat_type_t;
389
390 typedef struct
391 {
392   /* preferred pool address */
393   ip4_address_t pool_addr;
394   /* local IP address */
395   ip4_address_t local_addr;
396   /* external IP address */
397   ip4_address_t external_addr;
398   /* local port */
399   u16 local_port;
400   /* external port */
401   u16 external_port;
402   /* local FIB table */
403   u32 vrf_id;
404   u32 fib_index;
405   /* protocol */
406   ip_protocol_t proto;
407   /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
408   u32 affinity;
409   /* worker threads used by backends/local host */
410   u32 *workers;
411   /* opaque string tag */
412   u8 *tag;
413   /* backends for load-balancing mode */
414   nat44_lb_addr_port_t *locals;
415   /* affinity per service lis */
416   u32 affinity_per_service_list_head_index;
417   /* flags */
418   u32 flags;
419 } snat_static_mapping_t;
420
421 typedef struct
422 {
423   u32 sw_if_index;
424   u8 flags;
425 } snat_interface_t;
426
427 typedef struct
428 {
429   u8 is_resolved;
430   ip4_address_t l_addr;
431   ip4_address_t pool_addr;
432   u16 l_port;
433   u16 e_port;
434   u32 sw_if_index;
435   u32 vrf_id;
436   ip_protocol_t proto;
437   u32 flags;
438   u8 *tag;
439 } snat_static_mapping_resolve_t;
440
441 typedef struct
442 {
443   u8 is_resolved;
444   u8 is_twice_nat;
445   u32 sw_if_index;
446 } snat_address_resolve_t;
447
448 typedef struct
449 {
450   u32 count;
451   u32 sw_if_index;
452   ip4_address_t addr;
453 } snat_fib_entry_reg_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 u32 nat44_ed_get_in2out_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
484                                       u32 rx_fib_index, u8 is_output);
485 u32 nat44_ed_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
486                                       u32 rx_fib_index, u8 is_output);
487
488 /* Return worker thread index for given packet */
489 /* NAT address and port allocation function */
490 typedef int (nat_alloc_out_addr_and_port_function_t) (
491   snat_address_t *addresses, u32 fib_index, u32 thread_index,
492   ip_protocol_t proto, ip4_address_t *addr, u16 *port, u16 port_per_thread,
493   u32 snat_thread_index);
494
495 typedef struct nat_fib_s
496 {
497   u32 fib_index;
498   u32 ref_count;
499 } nat_fib_t;
500
501 typedef struct vrf_route_s
502 {
503   u32 vrf_id;
504   u32 fib_index;
505 } vrf_route_t;
506
507 typedef struct vrf_table_s
508 {
509   u32 table_vrf_id;
510   u32 table_fib_index;
511   vrf_route_t *routes;
512 } vrf_table_t;
513
514 typedef struct snat_main_s
515 {
516   /* Thread settings */
517   u32 num_workers;
518   u32 first_worker_index;
519   u32 *workers;
520   u16 port_per_thread;
521
522   /* Per thread data */
523   snat_main_per_thread_data_t *per_thread_data;
524
525   /* Static mapping pool */
526   snat_static_mapping_t *static_mappings;
527
528   /* Endpoint independent lookup tables */
529   clib_bihash_8_8_t in2out;
530   clib_bihash_8_8_t out2in;
531
532   /* Endpoint dependent lookup table */
533   clib_bihash_16_8_t flow_hash;
534
535   // vector of fibs
536   nat_fib_t *fibs;
537
538   u32 inside_vrf_id;
539   u32 inside_fib_index;
540
541   u32 outside_vrf_id;
542   u32 outside_fib_index;
543
544   // vector of outside fibs
545   nat_fib_t *outside_fibs;
546
547   // VRF routing table for dynamic sessions
548   vrf_table_t *vrf_tables;
549
550   /* Interface pool */
551   snat_interface_t *interfaces;
552   snat_interface_t *output_feature_interfaces;
553   // broken api backward compatibility
554   snat_interface_t *output_feature_dummy_interfaces;
555
556   /* Vector of outside addresses */
557   snat_address_t *addresses;
558   /* Vector of twice NAT addresses for external hosts */
559   snat_address_t *twice_nat_addresses;
560
561   /* first interface address should be auto-added */
562   snat_address_resolve_t *addr_to_resolve;
563
564   /* vector of fib entries */
565   snat_fib_entry_reg_t *fib_entry_reg;
566
567   /* vector of interface address static mappings to resolve. */
568   snat_static_mapping_resolve_t *sm_to_resolve;
569
570   /* Randomize port allocation order */
571   u32 random_seed;
572
573   /* Worker handoff frame-queue index */
574   u32 fq_in2out_index;
575   u32 fq_in2out_output_index;
576   u32 fq_out2in_index;
577
578   nat44_config_t rconfig;
579
580   /* If forwarding is enabled */
581   u8 forwarding_enabled;
582
583   /* Is translation memory size calculated or user defined */
584   u8 translation_memory_size_set;
585
586   u32 translation_buckets;
587   u32 max_translations_per_thread;
588   u32 *max_translations_per_fib;
589
590   nat_timeouts_t timeouts;
591
592   /* TCP MSS clamping */
593   u16 mss_clamping;
594
595   /* counters */
596   vlib_simple_counter_main_t total_sessions;
597   u32 max_cfg_sessions_gauge; /* Index of max configured sessions gauge in
598                                  stats */
599
600 #define _(x) vlib_simple_counter_main_t x;
601   struct
602   {
603     struct
604     {
605       struct
606       {
607         foreach_nat_counter;
608       } in2out;
609
610       struct
611       {
612         foreach_nat_counter;
613       } out2in;
614     } fastpath;
615
616     struct
617     {
618       struct
619       {
620         foreach_nat_counter;
621       } in2out;
622
623       struct
624       {
625         foreach_nat_counter;
626       } out2in;
627     } slowpath;
628
629     vlib_simple_counter_main_t hairpinning;
630   } counters;
631 #undef _
632
633   /* API message ID base */
634   u16 msg_id_base;
635
636   /* log class */
637   vlib_log_class_t log_class;
638   /* logging level */
639   u8 log_level;
640
641   /* convenience */
642   ip4_main_t *ip4_main;
643
644   fib_source_t fib_src_hi;
645   fib_source_t fib_src_low;
646
647   /* number of worker handoff frame queue elements */
648   u32 frame_queue_nelts;
649
650   /* nat44 plugin enabled */
651   u8 enabled;
652
653   /* TCP session state machine table:
654    *   first dimension is possible states
655    *   second dimension is direction (in2out/out2in)
656    *   third dimension is TCP flag (SYN, RST, FIN)
657    *
658    *   value is next state to change to
659    */
660   nat44_ed_tcp_state_e tcp_state_change_table[NAT44_ED_TCP_N_STATE]
661                                              [NAT44_ED_N_DIR]
662                                              [NAT44_ED_TCP_N_FLAG];
663 } snat_main_t;
664
665 typedef struct
666 {
667   u32 thread_index;
668   f64 now;
669 } nat44_is_idle_session_ctx_t;
670
671 typedef struct
672 {
673   u32 cached_sw_if_index;
674   uword *cached_presence_by_ip4_address;
675 } snat_runtime_t;
676
677 /*
678  * Why is this here? Because we don't need to touch this layer to
679  * simply reply to an icmp. We need to change id to a unique
680  * value to NAT an echo request/reply.
681  */
682
683 extern snat_main_t snat_main;
684
685 extern vlib_node_registration_t nat_default_node;
686 extern vlib_node_registration_t nat_pre_in2out_node;
687 extern vlib_node_registration_t nat_pre_out2in_node;
688
689 extern vlib_node_registration_t nat44_ed_in2out_node;
690 extern vlib_node_registration_t nat44_ed_in2out_output_node;
691 extern vlib_node_registration_t nat44_ed_out2in_node;
692
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
697 /** \brief Check if SNAT session is created from static mapping.
698     @param s SNAT session
699     @return true if SNAT session is created from static mapping otherwise 0
700 */
701 always_inline bool
702 nat44_ed_is_session_static (snat_session_t *s)
703 {
704   return s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING;
705 }
706
707 /** \brief Check if NAT session is twice NAT.
708     @param s NAT session
709     @return true if NAT session is twice NAT
710 */
711 always_inline bool
712 nat44_ed_is_twice_nat_session (snat_session_t *s)
713 {
714   return s->flags & SNAT_SESSION_FLAG_TWICE_NAT;
715 }
716
717 /** \brief Check if NAT session is load-balancing.
718     @param s NAT session
719     @return true if NAT session is load-balancing
720 */
721 always_inline bool
722 nat44_ed_is_lb_session (snat_session_t *s)
723 {
724   return s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING;
725 }
726
727 /** \brief Check if NAT session is forwarding bypass.
728     @param s NAT session
729     @return true if NAT session is load-balancing
730 */
731 always_inline bool
732 na44_ed_is_fwd_bypass_session (snat_session_t *s)
733 {
734   return s->flags & SNAT_SESSION_FLAG_FWD_BYPASS;
735 }
736
737 /** \brief Check if NAT session has affinity record.
738     @param s NAT session
739     @return true if NAT session has affinity record
740 */
741 always_inline bool
742 nat44_ed_is_affinity_session (snat_session_t *s)
743 {
744   return s->flags & SNAT_SESSION_FLAG_AFFINITY;
745 }
746
747 /** \brief Check if exact pool address should be used.
748     @param s SNAT session
749     @return true if exact pool address
750 */
751 always_inline bool
752 nat44_ed_is_exact_address_session (snat_session_t *s)
753 {
754   return s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS;
755 }
756
757 /** \brief Check if NAT interface is inside.
758     @param i NAT interface
759     @return true if inside interface
760 */
761 always_inline bool
762 nat44_ed_is_interface_inside (snat_interface_t *i)
763 {
764   return i->flags & NAT_INTERFACE_FLAG_IS_INSIDE;
765 }
766
767 /** \brief Check if NAT interface is outside.
768     @param i NAT interface
769     @return true if outside interface
770 */
771 always_inline bool
772 nat44_ed_is_interface_outside (snat_interface_t *i)
773 {
774   return i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE;
775 }
776
777 /** \brief Check if client initiating TCP connection (received SYN from client)
778     @param t TCP header
779     @return true if client initiating TCP connection
780 */
781 always_inline bool
782 tcp_flags_is_init (u8 f)
783 {
784   return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
785 }
786
787 always_inline bool
788 is_sm_addr_only (u32 f)
789 {
790   return (f & NAT_SM_FLAG_ADDR_ONLY);
791 }
792
793 always_inline bool
794 is_sm_out2in_only (u32 f)
795 {
796   return (f & NAT_SM_FLAG_OUT2IN_ONLY);
797 }
798
799 always_inline bool
800 is_sm_identity_nat (u32 f)
801 {
802   return (f & NAT_SM_FLAG_IDENTITY_NAT);
803 }
804
805 always_inline bool
806 is_sm_lb (u32 f)
807 {
808   return (f & NAT_SM_FLAG_LB);
809 }
810
811 always_inline bool
812 is_sm_exact_address (u32 f)
813 {
814   return (f & NAT_SM_FLAG_EXACT_ADDRESS);
815 }
816
817 always_inline bool
818 is_sm_self_twice_nat (u32 f)
819 {
820   return (f & NAT_SM_FLAG_SELF_TWICE_NAT);
821 }
822
823 always_inline bool
824 is_sm_twice_nat (u32 f)
825 {
826   return (f & NAT_SM_FLAG_TWICE_NAT);
827 }
828
829 always_inline bool
830 is_sm_switch_address (u32 f)
831 {
832   return (f & NAT_SM_FLAG_SWITCH_ADDRESS);
833 }
834
835 #define nat_log_err(...) \
836   vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
837 #define nat_log_warn(...) \
838   vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
839 #define nat_log_info(...) \
840   vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
841 #define nat_log_debug(...)\
842   vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
843
844 clib_error_t *nat44_api_hookup (vlib_main_t *vm);
845
846 int snat_set_workers (uword *bitmap);
847
848 int nat44_plugin_enable (nat44_config_t c);
849 int nat44_plugin_disable ();
850
851 int nat44_ed_add_interface (u32 sw_if_index, u8 is_inside);
852 int nat44_ed_del_interface (u32 sw_if_index, u8 is_inside);
853 int nat44_ed_add_output_interface (u32 sw_if_index);
854 int nat44_ed_del_output_interface (u32 sw_if_index);
855
856 int nat44_ed_add_address (ip4_address_t *addr, u32 vrf_id, u8 twice_nat);
857 int nat44_ed_del_address (ip4_address_t addr, u8 twice_nat);
858 int nat44_ed_add_interface_address (u32 sw_if_index, u8 twice_nat);
859 int nat44_ed_del_interface_address (u32 sw_if_index, u8 twice_nat);
860
861 int nat44_ed_add_del_vrf_table (u32 table_vrf_id, bool is_add);
862 int nat44_ed_add_del_vrf_route (u32 table_vrf_id, u32 vrf_id, bool is_add);
863 void nat44_ed_del_vrf_tables ();
864
865 int nat44_ed_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
866                                  u16 l_port, u16 e_port, ip_protocol_t proto,
867                                  u32 vrf_id, u32 sw_if_index, u32 flags,
868                                  ip4_address_t pool_addr, u8 *tag);
869
870 int nat44_ed_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
871                                  u16 l_port, u16 e_port, ip_protocol_t proto,
872                                  u32 vrf_id, u32 sw_if_index, u32 flags);
873
874 int nat44_ed_add_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
875                                     ip_protocol_t proto,
876                                     nat44_lb_addr_port_t *locals, u32 flags,
877                                     u8 *tag, u32 affinity);
878
879 int nat44_ed_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
880                                     ip_protocol_t proto, u32 flags);
881
882 int nat44_ed_add_del_lb_static_mapping_local (ip4_address_t e_addr, u16 e_port,
883                                               ip4_address_t l_addr, u16 l_port,
884                                               ip_protocol_t proto, u32 vrf_id,
885                                               u8 probability, u8 is_add);
886
887 /**
888  * @brief Delete NAT44 endpoint-dependent session
889  *
890  * @param sm     snat global configuration data
891  * @param addr   IPv4 address
892  * @param port   L4 port number
893  * @param proto  L4 protocol
894  * @param vrf_id VRF ID
895  * @param is_in  1 = inside network address and port pair, 0 = outside
896  *
897  * @return 0 on success, non-zero value otherwise
898  */
899 int nat44_ed_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
900                           ip4_address_t *eh_addr, u16 eh_port, u8 proto,
901                           u32 vrf_id, int is_in);
902
903 void nat44_ed_free_session_data (snat_main_t *sm, snat_session_t *s,
904                                  u32 thread_index, u8 is_ha);
905
906 /**
907  * @brief Set NAT44 session limit (session limit, vrf id)
908  *
909  * @param session_limit Session limit
910  * @param vrf_id        VRF id
911  * @return 0 on success, non-zero value otherwise
912  */
913 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
914
915 /**
916  * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
917  *
918  * @param session_limit Session limit
919  * @param vrf_id        VRF id
920  * @return 0 on success, non-zero value otherwise
921  */
922 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
923
924 void expire_per_vrf_sessions (u32 fib_index);
925
926 int snat_static_mapping_match (vlib_main_t *vm, ip4_address_t match_addr,
927                                u16 match_port, u32 match_fib_index,
928                                ip_protocol_t match_protocol,
929                                ip4_address_t *mapping_addr, u16 *mapping_port,
930                                u32 *mapping_fib_index, int by_external,
931                                u8 *is_addr_only, twice_nat_type_t *twice_nat,
932                                lb_nat_type_t *lb, ip4_address_t *ext_host_addr,
933                                u8 *is_identity_nat,
934                                snat_static_mapping_t **out);
935
936 u32 get_thread_idx_by_port (u16 e_port);
937
938 u32 nat_calc_bihash_buckets (u32 n_elts);
939
940 void nat44_addresses_free (snat_address_t **addresses);
941
942 void nat44_ed_sessions_clear ();
943
944 int nat44_ed_set_frame_queue_nelts (u32 frame_queue_nelts);
945
946 void nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f);
947
948 snat_static_mapping_t *nat44_ed_sm_i2o_lookup (snat_main_t *sm,
949                                                ip4_address_t addr, u16 port,
950                                                u32 fib_index, u8 proto);
951
952 snat_static_mapping_t *nat44_ed_sm_o2i_lookup (snat_main_t *sm,
953                                                ip4_address_t addr, u16 port,
954                                                u32 fib_index, u8 proto);
955
956 void nat_syslog_nat44_sadd (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
957                             u16 isport, ip4_address_t *idaddr, u16 idport,
958                             ip4_address_t *xsaddr, u16 xsport,
959                             ip4_address_t *xdaddr, u16 xdport, u8 proto,
960                             u8 is_twicenat);
961
962 void nat_syslog_nat44_sdel (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
963                             u16 isport, ip4_address_t *idaddr, u16 idport,
964                             ip4_address_t *xsaddr, u16 xsport,
965                             ip4_address_t *xdaddr, u16 xdport, u8 proto,
966                             u8 is_twicenat);
967
968 typedef enum
969 {
970   NAT_ED_TRNSL_ERR_SUCCESS = 0,
971   NAT_ED_TRNSL_ERR_TRANSLATION_FAILED = 1,
972   NAT_ED_TRNSL_ERR_FLOW_MISMATCH = 2,
973   NAT_ED_TRNSL_ERR_PACKET_TRUNCATED = 3,
974   NAT_ED_TRNSL_ERR_INNER_IP_CORRUPT = 4,
975   NAT_ED_TRNSL_ERR_INVALID_CSUM = 5,
976 } nat_translation_error_e;
977
978 nat_translation_error_e nat_6t_flow_buf_translate_i2o (
979   vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
980   nat_6t_flow_t *f, ip_protocol_t proto, int is_output_feature);
981
982 nat_translation_error_e nat_6t_flow_buf_translate_o2i (
983   vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
984   nat_6t_flow_t *f, ip_protocol_t proto, int is_output_feature);
985
986 #endif /* __included_nat44_ed_h__ */
987 /*
988  * fd.io coding-style-patch-verification: ON
989  *
990  * Local Variables:
991  * eval: (c-set-style "gnu")
992  * End:
993  */