nat: VRF routing & FIB improvements
[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   vnet_main_t *vnet_main;
654
655   /* TCP session state machine table:
656    *   first dimension is possible states
657    *   second dimension is direction (in2out/out2in)
658    *   third dimension is TCP flag (SYN, RST, FIN)
659    *
660    *   value is next state to change to
661    */
662   nat44_ed_tcp_state_e tcp_state_change_table[NAT44_ED_TCP_N_STATE]
663                                              [NAT44_ED_N_DIR]
664                                              [NAT44_ED_TCP_N_FLAG];
665 } snat_main_t;
666
667 typedef struct
668 {
669   u32 thread_index;
670   f64 now;
671 } nat44_is_idle_session_ctx_t;
672
673 typedef struct
674 {
675   u32 cached_sw_if_index;
676   uword *cached_presence_by_ip4_address;
677 } snat_runtime_t;
678
679 /*
680  * Why is this here? Because we don't need to touch this layer to
681  * simply reply to an icmp. We need to change id to a unique
682  * value to NAT an echo request/reply.
683  */
684
685 extern snat_main_t snat_main;
686
687 extern vlib_node_registration_t nat_default_node;
688 extern vlib_node_registration_t nat_pre_in2out_node;
689 extern vlib_node_registration_t nat_pre_out2in_node;
690
691 extern vlib_node_registration_t nat44_ed_in2out_node;
692 extern vlib_node_registration_t nat44_ed_in2out_output_node;
693 extern vlib_node_registration_t nat44_ed_out2in_node;
694
695 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
696 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
697 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
698
699 /** \brief Check if SNAT session is created from static mapping.
700     @param s SNAT session
701     @return true if SNAT session is created from static mapping otherwise 0
702 */
703 always_inline bool
704 nat44_ed_is_session_static (snat_session_t *s)
705 {
706   return s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING;
707 }
708
709 /** \brief Check if NAT session is twice NAT.
710     @param s NAT session
711     @return true if NAT session is twice NAT
712 */
713 always_inline bool
714 nat44_ed_is_twice_nat_session (snat_session_t *s)
715 {
716   return s->flags & SNAT_SESSION_FLAG_TWICE_NAT;
717 }
718
719 /** \brief Check if NAT session is load-balancing.
720     @param s NAT session
721     @return true if NAT session is load-balancing
722 */
723 always_inline bool
724 nat44_ed_is_lb_session (snat_session_t *s)
725 {
726   return s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING;
727 }
728
729 /** \brief Check if NAT session is forwarding bypass.
730     @param s NAT session
731     @return true if NAT session is load-balancing
732 */
733 always_inline bool
734 na44_ed_is_fwd_bypass_session (snat_session_t *s)
735 {
736   return s->flags & SNAT_SESSION_FLAG_FWD_BYPASS;
737 }
738
739 /** \brief Check if NAT session has affinity record.
740     @param s NAT session
741     @return true if NAT session has affinity record
742 */
743 always_inline bool
744 nat44_ed_is_affinity_session (snat_session_t *s)
745 {
746   return s->flags & SNAT_SESSION_FLAG_AFFINITY;
747 }
748
749 /** \brief Check if exact pool address should be used.
750     @param s SNAT session
751     @return true if exact pool address
752 */
753 always_inline bool
754 nat44_ed_is_exact_address_session (snat_session_t *s)
755 {
756   return s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS;
757 }
758
759 /** \brief Check if NAT interface is inside.
760     @param i NAT interface
761     @return true if inside interface
762 */
763 always_inline bool
764 nat44_ed_is_interface_inside (snat_interface_t *i)
765 {
766   return i->flags & NAT_INTERFACE_FLAG_IS_INSIDE;
767 }
768
769 /** \brief Check if NAT interface is outside.
770     @param i NAT interface
771     @return true if outside interface
772 */
773 always_inline bool
774 nat44_ed_is_interface_outside (snat_interface_t *i)
775 {
776   return i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE;
777 }
778
779 /** \brief Check if client initiating TCP connection (received SYN from client)
780     @param t TCP header
781     @return true if client initiating TCP connection
782 */
783 always_inline bool
784 tcp_flags_is_init (u8 f)
785 {
786   return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
787 }
788
789 always_inline bool
790 is_sm_addr_only (u32 f)
791 {
792   return (f & NAT_SM_FLAG_ADDR_ONLY);
793 }
794
795 always_inline bool
796 is_sm_out2in_only (u32 f)
797 {
798   return (f & NAT_SM_FLAG_OUT2IN_ONLY);
799 }
800
801 always_inline bool
802 is_sm_identity_nat (u32 f)
803 {
804   return (f & NAT_SM_FLAG_IDENTITY_NAT);
805 }
806
807 always_inline bool
808 is_sm_lb (u32 f)
809 {
810   return (f & NAT_SM_FLAG_LB);
811 }
812
813 always_inline bool
814 is_sm_exact_address (u32 f)
815 {
816   return (f & NAT_SM_FLAG_EXACT_ADDRESS);
817 }
818
819 always_inline bool
820 is_sm_self_twice_nat (u32 f)
821 {
822   return (f & NAT_SM_FLAG_SELF_TWICE_NAT);
823 }
824
825 always_inline bool
826 is_sm_twice_nat (u32 f)
827 {
828   return (f & NAT_SM_FLAG_TWICE_NAT);
829 }
830
831 always_inline bool
832 is_sm_switch_address (u32 f)
833 {
834   return (f & NAT_SM_FLAG_SWITCH_ADDRESS);
835 }
836
837 #define nat_log_err(...) \
838   vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
839 #define nat_log_warn(...) \
840   vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
841 #define nat_log_info(...) \
842   vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
843 #define nat_log_debug(...)\
844   vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
845
846 clib_error_t *nat44_api_hookup (vlib_main_t *vm);
847
848 int snat_set_workers (uword *bitmap);
849
850 int nat44_plugin_enable (nat44_config_t c);
851 int nat44_plugin_disable ();
852
853 int nat44_ed_add_interface (u32 sw_if_index, u8 is_inside);
854 int nat44_ed_del_interface (u32 sw_if_index, u8 is_inside);
855 int nat44_ed_add_output_interface (u32 sw_if_index);
856 int nat44_ed_del_output_interface (u32 sw_if_index);
857
858 int nat44_ed_add_address (ip4_address_t *addr, u32 vrf_id, u8 twice_nat);
859 int nat44_ed_del_address (ip4_address_t addr, u8 twice_nat);
860 int nat44_ed_add_interface_address (u32 sw_if_index, u8 twice_nat);
861 int nat44_ed_del_interface_address (u32 sw_if_index, u8 twice_nat);
862
863 int nat44_ed_add_del_vrf_table (u32 table_vrf_id, bool is_add);
864 int nat44_ed_add_del_vrf_route (u32 table_vrf_id, u32 vrf_id, bool is_add);
865 void nat44_ed_del_vrf_tables ();
866
867 int nat44_ed_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
868                                  u16 l_port, u16 e_port, ip_protocol_t proto,
869                                  u32 vrf_id, u32 sw_if_index, u32 flags,
870                                  ip4_address_t pool_addr, u8 *tag);
871
872 int nat44_ed_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
873                                  u16 l_port, u16 e_port, ip_protocol_t proto,
874                                  u32 vrf_id, u32 sw_if_index, u32 flags);
875
876 int nat44_ed_add_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
877                                     ip_protocol_t proto,
878                                     nat44_lb_addr_port_t *locals, u32 flags,
879                                     u8 *tag, u32 affinity);
880
881 int nat44_ed_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
882                                     ip_protocol_t proto, u32 flags);
883
884 int nat44_ed_add_del_lb_static_mapping_local (ip4_address_t e_addr, u16 e_port,
885                                               ip4_address_t l_addr, u16 l_port,
886                                               ip_protocol_t proto, u32 vrf_id,
887                                               u8 probability, u8 is_add);
888
889 /**
890  * @brief Delete NAT44 endpoint-dependent session
891  *
892  * @param sm     snat global configuration data
893  * @param addr   IPv4 address
894  * @param port   L4 port number
895  * @param proto  L4 protocol
896  * @param vrf_id VRF ID
897  * @param is_in  1 = inside network address and port pair, 0 = outside
898  *
899  * @return 0 on success, non-zero value otherwise
900  */
901 int nat44_ed_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
902                           ip4_address_t *eh_addr, u16 eh_port, u8 proto,
903                           u32 vrf_id, int is_in);
904
905 void nat44_ed_free_session_data (snat_main_t *sm, snat_session_t *s,
906                                  u32 thread_index, u8 is_ha);
907
908 /**
909  * @brief Set NAT44 session limit (session limit, vrf id)
910  *
911  * @param session_limit Session limit
912  * @param vrf_id        VRF id
913  * @return 0 on success, non-zero value otherwise
914  */
915 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
916
917 /**
918  * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
919  *
920  * @param session_limit Session limit
921  * @param vrf_id        VRF id
922  * @return 0 on success, non-zero value otherwise
923  */
924 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
925
926 void expire_per_vrf_sessions (u32 fib_index);
927
928 int snat_static_mapping_match (vlib_main_t *vm, ip4_address_t match_addr,
929                                u16 match_port, u32 match_fib_index,
930                                ip_protocol_t match_protocol,
931                                ip4_address_t *mapping_addr, u16 *mapping_port,
932                                u32 *mapping_fib_index, int by_external,
933                                u8 *is_addr_only, twice_nat_type_t *twice_nat,
934                                lb_nat_type_t *lb, ip4_address_t *ext_host_addr,
935                                u8 *is_identity_nat,
936                                snat_static_mapping_t **out);
937
938 u32 get_thread_idx_by_port (u16 e_port);
939
940 u32 nat_calc_bihash_buckets (u32 n_elts);
941
942 void nat44_addresses_free (snat_address_t **addresses);
943
944 void nat44_ed_sessions_clear ();
945
946 int nat44_ed_set_frame_queue_nelts (u32 frame_queue_nelts);
947
948 void nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f);
949
950 snat_static_mapping_t *nat44_ed_sm_i2o_lookup (snat_main_t *sm,
951                                                ip4_address_t addr, u16 port,
952                                                u32 fib_index, u8 proto);
953
954 snat_static_mapping_t *nat44_ed_sm_o2i_lookup (snat_main_t *sm,
955                                                ip4_address_t addr, u16 port,
956                                                u32 fib_index, u8 proto);
957
958 void nat_syslog_nat44_sadd (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
959                             u16 isport, ip4_address_t *idaddr, u16 idport,
960                             ip4_address_t *xsaddr, u16 xsport,
961                             ip4_address_t *xdaddr, u16 xdport, u8 proto,
962                             u8 is_twicenat);
963
964 void nat_syslog_nat44_sdel (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
965                             u16 isport, ip4_address_t *idaddr, u16 idport,
966                             ip4_address_t *xsaddr, u16 xsport,
967                             ip4_address_t *xdaddr, u16 xdport, u8 proto,
968                             u8 is_twicenat);
969
970 typedef enum
971 {
972   NAT_ED_TRNSL_ERR_SUCCESS = 0,
973   NAT_ED_TRNSL_ERR_TRANSLATION_FAILED = 1,
974   NAT_ED_TRNSL_ERR_FLOW_MISMATCH = 2,
975   NAT_ED_TRNSL_ERR_PACKET_TRUNCATED = 3,
976   NAT_ED_TRNSL_ERR_INNER_IP_CORRUPT = 4,
977   NAT_ED_TRNSL_ERR_INVALID_CSUM = 5,
978 } nat_translation_error_e;
979
980 nat_translation_error_e nat_6t_flow_buf_translate_i2o (
981   vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
982   nat_6t_flow_t *f, ip_protocol_t proto, int is_output_feature);
983
984 nat_translation_error_e nat_6t_flow_buf_translate_o2i (
985   vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
986   nat_6t_flow_t *f, ip_protocol_t proto, int is_output_feature);
987
988 #endif /* __included_nat44_ed_h__ */
989 /*
990  * fd.io coding-style-patch-verification: ON
991  *
992  * Local Variables:
993  * eval: (c-set-style "gnu")
994  * End:
995  */