nat: nat44-ed cleanup & fixes
[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   u32 fib_index;
359   u32 ref_count;
360 } nat_fib_t;
361
362 typedef struct
363 {
364   u32 fib_index;
365   u32 refcount;
366 } nat_outside_fib_t;
367
368 typedef struct
369 {
370   /* backend IP address */
371   ip4_address_t addr;
372   /* backend port number */
373   u16 port;
374   /* probability of the backend to be randomly matched */
375   u8 probability;
376   u8 prefix;
377   /* backend FIB table */
378   u32 vrf_id;
379   u32 fib_index;
380 } nat44_lb_addr_port_t;
381
382 typedef enum
383 {
384   /* twice-nat disabled */
385   TWICE_NAT_DISABLED,
386   /* twice-nat enabled */
387   TWICE_NAT,
388   /* twice-nat only when src IP equals dst IP after translation */
389   TWICE_NAT_SELF,
390 } twice_nat_type_t;
391
392 typedef enum
393 {
394   /* no load-balancing */
395   NO_LB_NAT,
396   /* load-balancing */
397   LB_NAT,
398   /* load-balancing with affinity */
399   AFFINITY_LB_NAT,
400 } lb_nat_type_t;
401
402 typedef struct
403 {
404   /* preferred pool address */
405   ip4_address_t pool_addr;
406   /* local IP address */
407   ip4_address_t local_addr;
408   /* external IP address */
409   ip4_address_t external_addr;
410   /* local port */
411   u16 local_port;
412   /* external port */
413   u16 external_port;
414   /* local FIB table */
415   u32 vrf_id;
416   u32 fib_index;
417   /* protocol */
418   ip_protocol_t proto;
419   /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
420   u32 affinity;
421   /* worker threads used by backends/local host */
422   u32 *workers;
423   /* opaque string tag */
424   u8 *tag;
425   /* backends for load-balancing mode */
426   nat44_lb_addr_port_t *locals;
427   /* affinity per service lis */
428   u32 affinity_per_service_list_head_index;
429   /* flags */
430   u32 flags;
431 } snat_static_mapping_t;
432
433 typedef struct
434 {
435   u32 sw_if_index;
436   u8 flags;
437 } snat_interface_t;
438
439 typedef struct
440 {
441   u8 is_resolved;
442   ip4_address_t l_addr;
443   ip4_address_t pool_addr;
444   u16 l_port;
445   u16 e_port;
446   u32 sw_if_index;
447   u32 vrf_id;
448   ip_protocol_t proto;
449   u32 flags;
450   u8 *tag;
451 } snat_static_mapping_resolve_t;
452
453 typedef struct
454 {
455   u8 is_resolved;
456   u8 is_twice_nat;
457   u32 sw_if_index;
458 } snat_address_resolve_t;
459
460 typedef struct
461 {
462   u32 count;
463   u32 sw_if_index;
464   ip4_address_t addr;
465 } snat_fib_entry_reg_t;
466
467 typedef struct
468 {
469   /* Session pool */
470   snat_session_t *sessions;
471
472   /* Pool of doubly-linked list elements */
473   dlist_elt_t *list_pool;
474
475   /* LRU session list - head is stale, tail is fresh */
476   dlist_elt_t *lru_pool;
477   u32 tcp_trans_lru_head_index;
478   u32 tcp_estab_lru_head_index;
479   u32 udp_lru_head_index;
480   u32 icmp_lru_head_index;
481   u32 unk_proto_lru_head_index;
482
483   /* NAT thread index */
484   u32 snat_thread_index;
485
486   /* real thread index */
487   u32 thread_index;
488
489   per_vrf_sessions_t *per_vrf_sessions_vec;
490
491 } snat_main_per_thread_data_t;
492
493 struct snat_main_s;
494
495 u32 nat44_ed_get_in2out_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
496                                       u32 rx_fib_index, u8 is_output);
497 u32 nat44_ed_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
498                                       u32 rx_fib_index, u8 is_output);
499
500 /* Return worker thread index for given packet */
501 /* NAT address and port allocation function */
502 typedef int (nat_alloc_out_addr_and_port_function_t) (
503   snat_address_t *addresses, u32 fib_index, u32 thread_index,
504   ip_protocol_t proto, ip4_address_t *addr, u16 *port, u16 port_per_thread,
505   u32 snat_thread_index);
506
507 typedef struct snat_main_s
508 {
509   /* Thread settings */
510   u32 num_workers;
511   u32 first_worker_index;
512   u32 *workers;
513   u16 port_per_thread;
514
515   /* Per thread data */
516   snat_main_per_thread_data_t *per_thread_data;
517
518   /* Static mapping pool */
519   snat_static_mapping_t *static_mappings;
520
521   /* Endpoint independent lookup tables */
522   clib_bihash_8_8_t in2out;
523   clib_bihash_8_8_t out2in;
524
525   /* Endpoint dependent lookup table */
526   clib_bihash_16_8_t flow_hash;
527
528   /* Interface pool */
529   snat_interface_t *interfaces;
530   snat_interface_t *output_feature_interfaces;
531   // broken api backward compatibility
532   snat_interface_t *output_feature_dummy_interfaces;
533
534   /* Vector of outside addresses */
535   snat_address_t *addresses;
536   /* Vector of twice NAT addresses for external hosts */
537   snat_address_t *twice_nat_addresses;
538
539   /* first interface address should be auto-added */
540   snat_address_resolve_t *addr_to_resolve;
541
542   /* vector of fibs */
543   nat_fib_t *fibs;
544
545   /* vector of outside fibs */
546   nat_outside_fib_t *outside_fibs;
547
548   /* vector of fib entries */
549   snat_fib_entry_reg_t *fib_entry_reg;
550
551   /* vector of interface address static mappings to resolve. */
552   snat_static_mapping_resolve_t *sm_to_resolve;
553
554   /* Randomize port allocation order */
555   u32 random_seed;
556
557   /* Worker handoff frame-queue index */
558   u32 fq_in2out_index;
559   u32 fq_in2out_output_index;
560   u32 fq_out2in_index;
561
562   nat44_config_t rconfig;
563
564   /* If forwarding is enabled */
565   u8 forwarding_enabled;
566
567   /* Is translation memory size calculated or user defined */
568   u8 translation_memory_size_set;
569
570   u32 translation_buckets;
571   u32 max_translations_per_thread;
572   u32 *max_translations_per_fib;
573
574   u32 outside_vrf_id;
575   u32 outside_fib_index;
576   u32 inside_vrf_id;
577   u32 inside_fib_index;
578
579   nat_timeouts_t timeouts;
580
581   /* TCP MSS clamping */
582   u16 mss_clamping;
583
584   /* counters */
585   vlib_simple_counter_main_t total_sessions;
586   u32 max_cfg_sessions_gauge; /* Index of max configured sessions gauge in
587                                  stats */
588
589 #define _(x) vlib_simple_counter_main_t x;
590   struct
591   {
592     struct
593     {
594       struct
595       {
596         foreach_nat_counter;
597       } in2out;
598
599       struct
600       {
601         foreach_nat_counter;
602       } out2in;
603     } fastpath;
604
605     struct
606     {
607       struct
608       {
609         foreach_nat_counter;
610       } in2out;
611
612       struct
613       {
614         foreach_nat_counter;
615       } out2in;
616     } slowpath;
617
618     vlib_simple_counter_main_t hairpinning;
619   } counters;
620 #undef _
621
622   /* API message ID base */
623   u16 msg_id_base;
624
625   /* log class */
626   vlib_log_class_t log_class;
627   /* logging level */
628   u8 log_level;
629
630   /* convenience */
631   ip4_main_t *ip4_main;
632
633   fib_source_t fib_src_hi;
634   fib_source_t fib_src_low;
635
636   /* number of worker handoff frame queue elements */
637   u32 frame_queue_nelts;
638
639   /* nat44 plugin enabled */
640   u8 enabled;
641
642   vnet_main_t *vnet_main;
643
644   /* TCP session state machine table:
645    *   first dimension is possible states
646    *   second dimension is direction (in2out/out2in)
647    *   third dimension is TCP flag (SYN, RST, FIN)
648    *
649    *   value is next state to change to
650    */
651   nat44_ed_tcp_state_e tcp_state_change_table[NAT44_ED_TCP_N_STATE]
652                                              [NAT44_ED_N_DIR]
653                                              [NAT44_ED_TCP_N_FLAG];
654 } snat_main_t;
655
656 typedef struct
657 {
658   u32 thread_index;
659   f64 now;
660 } nat44_is_idle_session_ctx_t;
661
662 typedef struct
663 {
664   u32 cached_sw_if_index;
665   uword *cached_presence_by_ip4_address;
666 } snat_runtime_t;
667
668 /*
669  * Why is this here? Because we don't need to touch this layer to
670  * simply reply to an icmp. We need to change id to a unique
671  * value to NAT an echo request/reply.
672  */
673
674 extern snat_main_t snat_main;
675
676 extern vlib_node_registration_t nat_default_node;
677 extern vlib_node_registration_t nat_pre_in2out_node;
678 extern vlib_node_registration_t nat_pre_out2in_node;
679
680 extern vlib_node_registration_t nat44_ed_in2out_node;
681 extern vlib_node_registration_t nat44_ed_in2out_output_node;
682 extern vlib_node_registration_t nat44_ed_out2in_node;
683
684 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
685 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
686 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
687
688 /** \brief Check if SNAT session is created from static mapping.
689     @param s SNAT session
690     @return true if SNAT session is created from static mapping otherwise 0
691 */
692 always_inline bool
693 nat44_ed_is_session_static (snat_session_t *s)
694 {
695   return s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING;
696 }
697
698 /** \brief Check if NAT session is twice NAT.
699     @param s NAT session
700     @return true if NAT session is twice NAT
701 */
702 always_inline bool
703 nat44_ed_is_twice_nat_session (snat_session_t *s)
704 {
705   return s->flags & SNAT_SESSION_FLAG_TWICE_NAT;
706 }
707
708 /** \brief Check if NAT session is load-balancing.
709     @param s NAT session
710     @return true if NAT session is load-balancing
711 */
712 always_inline bool
713 nat44_ed_is_lb_session (snat_session_t *s)
714 {
715   return s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING;
716 }
717
718 /** \brief Check if NAT session is forwarding bypass.
719     @param s NAT session
720     @return true if NAT session is load-balancing
721 */
722 always_inline bool
723 na44_ed_is_fwd_bypass_session (snat_session_t *s)
724 {
725   return s->flags & SNAT_SESSION_FLAG_FWD_BYPASS;
726 }
727
728 /** \brief Check if NAT session has affinity record.
729     @param s NAT session
730     @return true if NAT session has affinity record
731 */
732 always_inline bool
733 nat44_ed_is_affinity_session (snat_session_t *s)
734 {
735   return s->flags & SNAT_SESSION_FLAG_AFFINITY;
736 }
737
738 /** \brief Check if exact pool address should be used.
739     @param s SNAT session
740     @return true if exact pool address
741 */
742 always_inline bool
743 nat44_ed_is_exact_address_session (snat_session_t *s)
744 {
745   return s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS;
746 }
747
748 /** \brief Check if NAT interface is inside.
749     @param i NAT interface
750     @return true if inside interface
751 */
752 always_inline bool
753 nat44_ed_is_interface_inside (snat_interface_t *i)
754 {
755   return i->flags & NAT_INTERFACE_FLAG_IS_INSIDE;
756 }
757
758 /** \brief Check if NAT interface is outside.
759     @param i NAT interface
760     @return true if outside interface
761 */
762 always_inline bool
763 nat44_ed_is_interface_outside (snat_interface_t *i)
764 {
765   return i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE;
766 }
767
768 /** \brief Check if client initiating TCP connection (received SYN from client)
769     @param t TCP header
770     @return true if client initiating TCP connection
771 */
772 always_inline bool
773 tcp_flags_is_init (u8 f)
774 {
775   return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
776 }
777
778 always_inline bool
779 is_sm_addr_only (u32 f)
780 {
781   return (f & NAT_SM_FLAG_ADDR_ONLY);
782 }
783
784 always_inline bool
785 is_sm_out2in_only (u32 f)
786 {
787   return (f & NAT_SM_FLAG_OUT2IN_ONLY);
788 }
789
790 always_inline bool
791 is_sm_identity_nat (u32 f)
792 {
793   return (f & NAT_SM_FLAG_IDENTITY_NAT);
794 }
795
796 always_inline bool
797 is_sm_lb (u32 f)
798 {
799   return (f & NAT_SM_FLAG_LB);
800 }
801
802 always_inline bool
803 is_sm_exact_address (u32 f)
804 {
805   return (f & NAT_SM_FLAG_EXACT_ADDRESS);
806 }
807
808 always_inline bool
809 is_sm_self_twice_nat (u32 f)
810 {
811   return (f & NAT_SM_FLAG_SELF_TWICE_NAT);
812 }
813
814 always_inline bool
815 is_sm_twice_nat (u32 f)
816 {
817   return (f & NAT_SM_FLAG_TWICE_NAT);
818 }
819
820 always_inline bool
821 is_sm_switch_address (u32 f)
822 {
823   return (f & NAT_SM_FLAG_SWITCH_ADDRESS);
824 }
825
826 #define nat_log_err(...) \
827   vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
828 #define nat_log_warn(...) \
829   vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
830 #define nat_log_info(...) \
831   vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
832 #define nat_log_debug(...)\
833   vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
834
835 clib_error_t *nat44_api_hookup (vlib_main_t *vm);
836
837 int snat_set_workers (uword *bitmap);
838
839 int nat44_plugin_enable (nat44_config_t c);
840 int nat44_plugin_disable ();
841
842 int nat44_ed_add_interface (u32 sw_if_index, u8 is_inside);
843 int nat44_ed_del_interface (u32 sw_if_index, u8 is_inside);
844 int nat44_ed_add_output_interface (u32 sw_if_index);
845 int nat44_ed_del_output_interface (u32 sw_if_index);
846
847 int nat44_ed_add_address (ip4_address_t *addr, u32 vrf_id, u8 twice_nat);
848 int nat44_ed_del_address (ip4_address_t addr, u8 twice_nat);
849 int nat44_ed_add_interface_address (u32 sw_if_index, u8 twice_nat);
850 int nat44_ed_del_interface_address (u32 sw_if_index, u8 twice_nat);
851
852 int nat44_ed_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
853                                  u16 l_port, u16 e_port, ip_protocol_t proto,
854                                  u32 vrf_id, u32 sw_if_index, u32 flags,
855                                  ip4_address_t pool_addr, u8 *tag);
856
857 int nat44_ed_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
858                                  u16 l_port, u16 e_port, ip_protocol_t proto,
859                                  u32 vrf_id, u32 sw_if_index, u32 flags);
860
861 int nat44_ed_add_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
862                                     ip_protocol_t proto,
863                                     nat44_lb_addr_port_t *locals, u32 flags,
864                                     u8 *tag, u32 affinity);
865
866 int nat44_ed_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
867                                     ip_protocol_t proto, u32 flags);
868
869 int nat44_ed_add_del_lb_static_mapping_local (ip4_address_t e_addr, u16 e_port,
870                                               ip4_address_t l_addr, u16 l_port,
871                                               ip_protocol_t proto, u32 vrf_id,
872                                               u8 probability, u8 is_add);
873
874 /**
875  * @brief Delete NAT44 endpoint-dependent session
876  *
877  * @param sm     snat global configuration data
878  * @param addr   IPv4 address
879  * @param port   L4 port number
880  * @param proto  L4 protocol
881  * @param vrf_id VRF ID
882  * @param is_in  1 = inside network address and port pair, 0 = outside
883  *
884  * @return 0 on success, non-zero value otherwise
885  */
886 int nat44_ed_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
887                           ip4_address_t *eh_addr, u16 eh_port, u8 proto,
888                           u32 vrf_id, int is_in);
889
890 void nat44_ed_free_session_data (snat_main_t *sm, snat_session_t *s,
891                                  u32 thread_index, u8 is_ha);
892
893 /**
894  * @brief Set NAT44 session limit (session limit, vrf id)
895  *
896  * @param session_limit Session limit
897  * @param vrf_id        VRF id
898  * @return 0 on success, non-zero value otherwise
899  */
900 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
901
902 /**
903  * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
904  *
905  * @param session_limit Session limit
906  * @param vrf_id        VRF id
907  * @return 0 on success, non-zero value otherwise
908  */
909 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
910
911 void expire_per_vrf_sessions (u32 fib_index);
912
913 /**
914  * @brief Match NAT44 static mapping.
915  *
916  * @param key             address and port to match
917  * @param addr            external/local address of the matched mapping
918  * @param port            port of the matched mapping
919  * @param fib_index       fib index of the matched mapping
920  * @param by_external     if 0 match by local address otherwise match by external
921  *                        address
922  * @param is_addr_only    1 if matched mapping is address only
923  * @param twice_nat       matched mapping is twice NAT type
924  * @param lb              1 if matched mapping is load-balanced
925  * @param ext_host_addr   external host address
926  * @param is_identity_nat 1 if indentity mapping
927  * @param out             if !=0 set to pointer of the mapping structure
928  *
929  * @returns 0 if match found otherwise 1.
930  */
931 int snat_static_mapping_match (
932   vlib_main_t *vm, snat_main_t *sm, ip4_address_t match_addr, u16 match_port,
933   u32 match_fib_index, ip_protocol_t match_protocol,
934   ip4_address_t *mapping_addr, u16 *mapping_port, u32 *mapping_fib_index,
935   int by_external, u8 *is_addr_only, twice_nat_type_t *twice_nat,
936   lb_nat_type_t *lb, ip4_address_t *ext_host_addr, u8 *is_identity_nat,
937   snat_static_mapping_t **out);
938
939 u32 get_thread_idx_by_port (u16 e_port);
940
941 u32 nat_calc_bihash_buckets (u32 n_elts);
942
943 void nat44_addresses_free (snat_address_t **addresses);
944
945 void nat44_ed_sessions_clear ();
946
947 int nat44_ed_set_frame_queue_nelts (u32 frame_queue_nelts);
948
949 void nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f);
950
951 snat_static_mapping_t *nat44_ed_sm_i2o_lookup (snat_main_t *sm,
952                                                ip4_address_t addr, u16 port,
953                                                u32 fib_index, u8 proto);
954
955 snat_static_mapping_t *nat44_ed_sm_o2i_lookup (snat_main_t *sm,
956                                                ip4_address_t addr, u16 port,
957                                                u32 fib_index, u8 proto);
958
959 void nat_syslog_nat44_sadd (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
960                             u16 isport, ip4_address_t *idaddr, u16 idport,
961                             ip4_address_t *xsaddr, u16 xsport,
962                             ip4_address_t *xdaddr, u16 xdport, u8 proto,
963                             u8 is_twicenat);
964
965 void nat_syslog_nat44_sdel (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
966                             u16 isport, ip4_address_t *idaddr, u16 idport,
967                             ip4_address_t *xsaddr, u16 xsport,
968                             ip4_address_t *xdaddr, u16 xdport, u8 proto,
969                             u8 is_twicenat);
970
971 typedef enum
972 {
973   NAT_ED_TRNSL_ERR_SUCCESS = 0,
974   NAT_ED_TRNSL_ERR_TRANSLATION_FAILED = 1,
975   NAT_ED_TRNSL_ERR_FLOW_MISMATCH = 2,
976   NAT_ED_TRNSL_ERR_PACKET_TRUNCATED = 3,
977   NAT_ED_TRNSL_ERR_INNER_IP_CORRUPT = 4,
978   NAT_ED_TRNSL_ERR_INVALID_CSUM = 5,
979 } nat_translation_error_e;
980
981 nat_translation_error_e nat_6t_flow_buf_translate_i2o (
982   vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
983   nat_6t_flow_t *f, ip_protocol_t proto, int is_output_feature);
984
985 nat_translation_error_e nat_6t_flow_buf_translate_o2i (
986   vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
987   nat_6t_flow_t *f, ip_protocol_t proto, int is_output_feature);
988
989 #endif /* __included_nat44_ed_h__ */
990 /*
991  * fd.io coding-style-patch-verification: ON
992  *
993  * Local Variables:
994  * eval: (c-set-style "gnu")
995  * End:
996  */