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