d8cc0d3aece7aef3ba866929a24cd894be373c54
[vpp.git] / src / plugins / nat / nat44-ed / nat44_ed.h
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file nat.c
17  * NAT plugin global declarations
18  */
19 #ifndef __included_nat44_ed_h__
20 #define __included_nat44_ed_h__
21
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/ip/icmp46_packet.h>
26 #include <vnet/api_errno.h>
27 #include <vnet/fib/fib_source.h>
28 #include <vppinfra/elog.h>
29 #include <vppinfra/bihash_8_8.h>
30 #include <vppinfra/bihash_16_8.h>
31 #include <vppinfra/hash.h>
32 #include <vppinfra/dlist.h>
33 #include <vppinfra/error.h>
34 #include <vlibapi/api.h>
35
36 #include <nat/lib/lib.h>
37 #include <nat/lib/inlines.h>
38
39 /* default number of worker handoff frame queue elements */
40 #define NAT_FQ_NELTS_DEFAULT 64
41
42 /* number of attempts to get a port for ED overloading algorithm, if rolling
43  * a dice this many times doesn't produce a free port, it's treated
44  * as if there were no free ports available to conserve resources */
45 #define ED_PORT_ALLOC_ATTEMPTS (10)
46
47 /* NAT buffer flags */
48 #define SNAT_FLAG_HAIRPINNING (1 << 0)
49
50 /* NAT44 API Configuration flags */
51 #define foreach_nat44_config_flag                                             \
52   _ (0x00, IS_ENDPOINT_INDEPENDENT)                                           \
53   _ (0x01, IS_ENDPOINT_DEPENDENT)                                             \
54   _ (0x02, IS_STATIC_MAPPING_ONLY)                                            \
55   _ (0x04, IS_CONNECTION_TRACKING)
56
57 typedef enum nat44_config_flags_t_
58 {
59 #define _(n,f) NAT44_API_##f = n,
60   foreach_nat44_config_flag
61 #undef _
62 } nat44_config_flags_t;
63
64 typedef struct
65 {
66   /* nat44 plugin features */
67   u8 static_mapping_only;
68   u8 connection_tracking;
69
70   u32 inside_vrf;
71   u32 outside_vrf;
72
73   /* maximum number of sessions */
74   u32 sessions;
75
76 } nat44_config_t;
77
78 typedef enum
79 {
80   NAT_NEXT_DROP,
81   NAT_NEXT_ICMP_ERROR,
82   NAT_NEXT_IN2OUT_ED_FAST_PATH,
83   NAT_NEXT_IN2OUT_ED_SLOW_PATH,
84   NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH,
85   NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
86   NAT_NEXT_OUT2IN_ED_FAST_PATH,
87   NAT_NEXT_OUT2IN_ED_SLOW_PATH,
88   NAT_NEXT_IN2OUT_CLASSIFY,
89   NAT_NEXT_OUT2IN_CLASSIFY,
90   NAT_N_NEXT,
91 } nat_next_t;
92
93 typedef struct
94 {
95   u32 next_index;
96   u32 arc_next_index;
97 } nat_pre_trace_t;
98
99 /* External address and port allocation modes */
100 #define foreach_nat_addr_and_port_alloc_alg \
101   _(0, DEFAULT, "default")         \
102   _(1, MAPE, "map-e")              \
103   _(2, RANGE, "port-range")
104
105 typedef enum
106 {
107 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
108   foreach_nat_addr_and_port_alloc_alg
109 #undef _
110 } nat_addr_and_port_alloc_alg_t;
111
112 /* Session state */
113 #define foreach_snat_session_state          \
114   _(0, UNKNOWN, "unknown")                 \
115   _(1, UDP_ACTIVE, "udp-active")           \
116   _(2, TCP_SYN_SENT, "tcp-syn-sent")       \
117   _(3, TCP_ESTABLISHED, "tcp-established") \
118   _(4, TCP_FIN_WAIT, "tcp-fin-wait")       \
119   _(5, TCP_CLOSE_WAIT, "tcp-close-wait")   \
120   _(6, TCP_CLOSING, "tcp-closing")         \
121   _(7, TCP_LAST_ACK, "tcp-last-ack")       \
122   _(8, TCP_CLOSED, "tcp-closed")           \
123   _(9, ICMP_ACTIVE, "icmp-active")
124
125 typedef enum
126 {
127 #define _(v, N, s) SNAT_SESSION_##N = v,
128   foreach_snat_session_state
129 #undef _
130 } snat_session_state_t;
131
132 #define foreach_nat_in2out_ed_error                                           \
133   _ (UNSUPPORTED_PROTOCOL, "unsupported protocol")                            \
134   _ (OUT_OF_PORTS, "out of ports")                                            \
135   _ (BAD_ICMP_TYPE, "unsupported ICMP type")                                  \
136   _ (MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")                      \
137   _ (NON_SYN, "non-SYN packet try to create session")                         \
138   _ (TCP_CLOSED, "drops due to TCP in transitory timeout")                    \
139   _ (TRNSL_FAILED, "couldn't translate packet")
140
141 typedef enum
142 {
143 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
144   foreach_nat_in2out_ed_error
145 #undef _
146     NAT_IN2OUT_ED_N_ERROR,
147 } nat_in2out_ed_error_t;
148
149 #define foreach_nat_out2in_ed_error                                           \
150   _ (UNSUPPORTED_PROTOCOL, "unsupported protocol")                            \
151   _ (OUT_OF_PORTS, "out of ports")                                            \
152   _ (BAD_ICMP_TYPE, "unsupported ICMP type")                                  \
153   _ (NO_TRANSLATION, "no translation")                                        \
154   _ (MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")                      \
155   _ (NON_SYN, "non-SYN packet try to create session")                         \
156   _ (TCP_CLOSED, "drops due to TCP in transitory timeout")                    \
157   _ (HASH_ADD_FAILED, "hash table add failed")                                \
158   _ (TRNSL_FAILED, "couldn't translate packet")
159
160 typedef enum
161 {
162 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
163   foreach_nat_out2in_ed_error
164 #undef _
165     NAT_OUT2IN_ED_N_ERROR,
166 } nat_out2in_ed_error_t;
167
168
169 /* Endpoint dependent TCP session state */
170 #define NAT44_SES_I2O_FIN 1
171 #define NAT44_SES_O2I_FIN 2
172 #define NAT44_SES_I2O_FIN_ACK 4
173 #define NAT44_SES_O2I_FIN_ACK 8
174 #define NAT44_SES_I2O_SYN 16
175 #define NAT44_SES_O2I_SYN 32
176 #define NAT44_SES_RST     64
177
178 /* Session flags */
179 #define SNAT_SESSION_FLAG_STATIC_MAPPING     (1 << 0)
180 #define SNAT_SESSION_FLAG_LOAD_BALANCING     (1 << 2)
181 #define SNAT_SESSION_FLAG_TWICE_NAT          (1 << 3)
182 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT (1 << 4)
183 #define SNAT_SESSION_FLAG_FWD_BYPASS         (1 << 5)
184 #define SNAT_SESSION_FLAG_AFFINITY           (1 << 6)
185 #define SNAT_SESSION_FLAG_EXACT_ADDRESS      (1 << 7)
186 #define SNAT_SESSION_FLAG_HAIRPINNING        (1 << 8)
187
188 /* NAT interface flags */
189 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
190 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
191
192 /* Static mapping flags */
193 #define NAT_SM_FLAG_SELF_TWICE_NAT (1 << 1)
194 #define NAT_SM_FLAG_TWICE_NAT      (1 << 2)
195 #define NAT_SM_FLAG_IDENTITY_NAT   (1 << 3)
196 #define NAT_SM_FLAG_ADDR_ONLY      (1 << 4)
197 #define NAT_SM_FLAG_EXACT_ADDRESS  (1 << 5)
198 #define NAT_SM_FLAG_OUT2IN_ONLY    (1 << 6)
199 #define NAT_SM_FLAG_LB             (1 << 7)
200 #define NAT_SM_FLAG_SWITCH_ADDRESS (1 << 8)
201
202 typedef CLIB_PACKED(struct
203 {
204   // number of sessions in this vrf
205   u32 ses_count;
206
207   u32 rx_fib_index;
208   u32 tx_fib_index;
209
210   // is this vrf expired
211   u8 expired;
212 }) per_vrf_sessions_t;
213
214 typedef union
215 {
216   struct
217   {
218     ip4_address_t saddr, daddr;
219     u16 sport; // ICMP id for ICMP case
220     u16 dport;
221     u32 fib_index : 24;
222     u8 proto;
223   };
224   u64 as_u64[2];
225   u64x2u as_u128;
226 } nat_6t_t;
227
228 STATIC_ASSERT_SIZEOF (nat_6t_t, 2 * sizeof (u64));
229
230 typedef struct
231 {
232 #define NAT_FLOW_OP_SADDR_REWRITE   (1 << 1)
233 #define NAT_FLOW_OP_SPORT_REWRITE   (1 << 2)
234 #define NAT_FLOW_OP_DADDR_REWRITE   (1 << 3)
235 #define NAT_FLOW_OP_DPORT_REWRITE   (1 << 4)
236 #define NAT_FLOW_OP_ICMP_ID_REWRITE (1 << 5)
237 #define NAT_FLOW_OP_TXFIB_REWRITE   (1 << 6)
238   int ops;
239   nat_6t_t match;
240   struct
241   {
242     ip4_address_t saddr, daddr;
243     u16 sport;
244     u16 dport;
245     u32 fib_index;
246     u8 proto;
247     u16 icmp_id;
248   } rewrite;
249   uword l3_csum_delta;
250   uword l4_csum_delta;
251 } nat_6t_flow_t;
252
253 void nat44_ed_forwarding_enable_disable (u8 is_enable);
254
255 always_inline void
256 nat_6t_flow_saddr_rewrite_set (nat_6t_flow_t *f, u32 saddr)
257 {
258   f->ops |= NAT_FLOW_OP_SADDR_REWRITE;
259   f->rewrite.saddr.as_u32 = saddr;
260 }
261
262 always_inline void
263 nat_6t_flow_daddr_rewrite_set (nat_6t_flow_t *f, u32 daddr)
264 {
265   f->ops |= NAT_FLOW_OP_DADDR_REWRITE;
266   f->rewrite.daddr.as_u32 = daddr;
267 }
268
269 always_inline void
270 nat_6t_flow_sport_rewrite_set (nat_6t_flow_t *f, u32 sport)
271 {
272   f->ops |= NAT_FLOW_OP_SPORT_REWRITE;
273   f->rewrite.sport = sport;
274 }
275
276 always_inline void
277 nat_6t_flow_dport_rewrite_set (nat_6t_flow_t *f, u32 dport)
278 {
279   f->ops |= NAT_FLOW_OP_DPORT_REWRITE;
280   f->rewrite.dport = dport;
281 }
282
283 always_inline void
284 nat_6t_flow_txfib_rewrite_set (nat_6t_flow_t *f, u32 tx_fib_index)
285 {
286   f->ops |= NAT_FLOW_OP_TXFIB_REWRITE;
287   f->rewrite.fib_index = tx_fib_index;
288 }
289
290 always_inline void
291 nat_6t_flow_icmp_id_rewrite_set (nat_6t_flow_t *f, u16 id)
292 {
293   f->ops |= NAT_FLOW_OP_ICMP_ID_REWRITE;
294   f->rewrite.icmp_id = id;
295 }
296
297 typedef CLIB_PACKED(struct
298 {
299   /* Outside network tuple */
300   struct
301   {
302     ip4_address_t addr;
303     u32 fib_index;
304     u16 port;
305   } out2in;
306
307   /* Inside network tuple */
308   struct
309   {
310     ip4_address_t addr;
311     u32 fib_index;
312     u16 port;
313   } in2out;
314
315   ip_protocol_t proto;
316
317   nat_6t_flow_t i2o;
318   nat_6t_flow_t o2i;
319
320   /* Flags */
321   u32 flags;
322
323   /* head of LRU list in which this session is tracked */
324   u32 lru_head_index;
325   /* index in global LRU list */
326   u32 lru_index;
327   f64 last_lru_update;
328
329   /* Last heard timer */
330   f64 last_heard;
331
332   /* Last HA refresh */
333   f64 ha_last_refreshed;
334
335   /* Counters */
336   u64 total_bytes;
337   u32 total_pkts;
338
339   /* External host address and port */
340   ip4_address_t ext_host_addr;
341   u16 ext_host_port;
342
343   /* External host address and port after translation */
344   ip4_address_t ext_host_nat_addr;
345   u16 ext_host_nat_port;
346
347   /* TCP session state */
348   u8 state;
349   u32 i2o_fin_seq;
350   u32 o2i_fin_seq;
351   u64 tcp_closed_timestamp;
352
353   /* per vrf sessions index */
354   u32 per_vrf_sessions_index;
355
356   u32 thread_index;
357 }) snat_session_t;
358
359 typedef struct
360 {
361   ip4_address_t addr;
362   ip4_address_t net;
363   u32 sw_if_index;
364   u32 fib_index;
365   u32 addr_len;
366 } snat_address_t;
367
368 typedef struct
369 {
370   u32 fib_index;
371   u32 ref_count;
372 } nat_fib_t;
373
374 typedef struct
375 {
376   u32 fib_index;
377   u32 refcount;
378 } nat_outside_fib_t;
379
380 typedef struct
381 {
382   /* backend IP address */
383   ip4_address_t addr;
384   /* backend port number */
385   u16 port;
386   /* probability of the backend to be randomly matched */
387   u8 probability;
388   u8 prefix;
389   /* backend FIB table */
390   u32 vrf_id;
391   u32 fib_index;
392 } nat44_lb_addr_port_t;
393
394 typedef enum
395 {
396   /* twice-nat disabled */
397   TWICE_NAT_DISABLED,
398   /* twice-nat enabled */
399   TWICE_NAT,
400   /* twice-nat only when src IP equals dst IP after translation */
401   TWICE_NAT_SELF,
402 } twice_nat_type_t;
403
404 typedef enum
405 {
406   /* no load-balancing */
407   NO_LB_NAT,
408   /* load-balancing */
409   LB_NAT,
410   /* load-balancing with affinity */
411   AFFINITY_LB_NAT,
412 } lb_nat_type_t;
413
414 typedef struct
415 {
416   /* preferred pool address */
417   ip4_address_t pool_addr;
418   /* local IP address */
419   ip4_address_t local_addr;
420   /* external IP address */
421   ip4_address_t external_addr;
422   /* local port */
423   u16 local_port;
424   /* external port */
425   u16 external_port;
426   /* local FIB table */
427   u32 vrf_id;
428   u32 fib_index;
429   /* protocol */
430   ip_protocol_t proto;
431   /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
432   u32 affinity;
433   /* worker threads used by backends/local host */
434   u32 *workers;
435   /* opaque string tag */
436   u8 *tag;
437   /* backends for load-balancing mode */
438   nat44_lb_addr_port_t *locals;
439   /* affinity per service lis */
440   u32 affinity_per_service_list_head_index;
441   /* flags */
442   u32 flags;
443 } snat_static_mapping_t;
444
445 typedef struct
446 {
447   u32 sw_if_index;
448   u8 flags;
449 } snat_interface_t;
450
451 typedef struct
452 {
453   ip4_address_t l_addr;
454   ip4_address_t pool_addr;
455   u16 l_port;
456   u16 e_port;
457   u32 sw_if_index;
458   u32 vrf_id;
459   ip_protocol_t proto;
460   u32 flags;
461   u8 *tag;
462 } snat_static_map_resolve_t;
463
464 typedef struct
465 {
466   /* Session pool */
467   snat_session_t *sessions;
468
469   /* Pool of doubly-linked list elements */
470   dlist_elt_t *list_pool;
471
472   /* LRU session list - head is stale, tail is fresh */
473   dlist_elt_t *lru_pool;
474   u32 tcp_trans_lru_head_index;
475   u32 tcp_estab_lru_head_index;
476   u32 udp_lru_head_index;
477   u32 icmp_lru_head_index;
478   u32 unk_proto_lru_head_index;
479
480   /* NAT thread index */
481   u32 snat_thread_index;
482
483   /* real thread index */
484   u32 thread_index;
485
486   per_vrf_sessions_t *per_vrf_sessions_vec;
487
488 } snat_main_per_thread_data_t;
489
490 struct snat_main_s;
491
492 u32 nat44_ed_get_in2out_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
493                                       u32 rx_fib_index, u8 is_output);
494 u32 nat44_ed_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
495                                       u32 rx_fib_index, u8 is_output);
496
497 /* Return worker thread index for given packet */
498 /* NAT address and port allocation function */
499 typedef int (nat_alloc_out_addr_and_port_function_t) (
500   snat_address_t *addresses, u32 fib_index, u32 thread_index,
501   ip_protocol_t proto, ip4_address_t *addr, u16 *port, u16 port_per_thread,
502   u32 snat_thread_index);
503
504 typedef struct snat_main_s
505 {
506   /* Thread settings */
507   u32 num_workers;
508   u32 first_worker_index;
509   u32 *workers;
510   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   /* Interface pool */
526   snat_interface_t *interfaces;
527   snat_interface_t *output_feature_interfaces;
528   // broken api backward compatibility
529   snat_interface_t *output_feature_dummy_interfaces;
530
531   /* Vector of outside addresses */
532   snat_address_t *addresses;
533   /* Vector of twice NAT addresses for external hosts */
534   snat_address_t *twice_nat_addresses;
535
536   /* sw_if_indices whose intfc addresses should be auto-added */
537   u32 *auto_add_sw_if_indices;
538   u32 *auto_add_sw_if_indices_twice_nat;
539
540   /* Address and port allocation function */
541   nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
542   /* Address and port allocation type */
543   nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
544   /* Port set parameters (MAP-E) */
545   u8 psid_offset;
546   u8 psid_length;
547   u16 psid;
548   /* Port range parameters */
549   u16 start_port;
550   u16 end_port;
551
552   /* vector of fibs */
553   nat_fib_t *fibs;
554
555   /* vector of outside fibs */
556   nat_outside_fib_t *outside_fibs;
557
558   /* vector of interface address static mappings to resolve. */
559   snat_static_map_resolve_t *to_resolve;
560
561   /* Randomize port allocation order */
562   u32 random_seed;
563
564   /* Worker handoff frame-queue index */
565   u32 fq_in2out_index;
566   u32 fq_in2out_output_index;
567   u32 fq_out2in_index;
568
569   u32 out2in_node_index;
570   u32 in2out_node_index;
571   u32 in2out_output_node_index;
572
573   nat44_config_t rconfig;
574   //nat44_config_t cconfig;
575
576   /* If forwarding is enabled */
577   u8 forwarding_enabled;
578
579   /* static mapping config */
580   u8 static_mapping_only;
581   u8 static_mapping_connection_tracking;
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   u32 outside_vrf_id;
591   u32 outside_fib_index;
592   u32 inside_vrf_id;
593   u32 inside_fib_index;
594
595   nat_timeouts_t timeouts;
596
597   /* TCP MSS clamping */
598   u16 mss_clamping;
599
600   /* counters */
601   vlib_simple_counter_main_t total_sessions;
602   u32 max_cfg_sessions_gauge; /* Index of max configured sessions gauge in
603                                  stats */
604
605 #define _(x) vlib_simple_counter_main_t x;
606   struct
607   {
608     struct
609     {
610       struct
611       {
612         foreach_nat_counter;
613       } in2out;
614
615       struct
616       {
617         foreach_nat_counter;
618       } out2in;
619     } fastpath;
620
621     struct
622     {
623       struct
624       {
625         foreach_nat_counter;
626       } in2out;
627
628       struct
629       {
630         foreach_nat_counter;
631       } out2in;
632     } slowpath;
633
634     vlib_simple_counter_main_t hairpinning;
635   } counters;
636 #undef _
637
638   /* API message ID base */
639   u16 msg_id_base;
640
641   /* log class */
642   vlib_log_class_t log_class;
643   /* logging level */
644   u8 log_level;
645
646   /* convenience */
647   api_main_t *api_main;
648   ip4_main_t *ip4_main;
649   ip_lookup_main_t *ip4_lookup_main;
650
651   fib_source_t fib_src_hi;
652   fib_source_t fib_src_low;
653
654   /* pat - dynamic mapping enabled or conneciton tracking */
655   u8 pat;
656
657   /* number of worker handoff frame queue elements */
658   u32 frame_queue_nelts;
659
660   /* nat44 plugin enabled */
661   u8 enabled;
662
663   vnet_main_t *vnet_main;
664
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 extern snat_main_t snat_main;
680
681 // nat pre ed next_node feature classification
682 extern vlib_node_registration_t nat_default_node;
683 extern vlib_node_registration_t nat_pre_in2out_node;
684 extern vlib_node_registration_t nat_pre_out2in_node;
685
686 extern vlib_node_registration_t snat_in2out_node;
687 extern vlib_node_registration_t snat_in2out_output_node;
688 extern vlib_node_registration_t snat_out2in_node;
689 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
690 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
691 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
692 extern vlib_node_registration_t nat44_ed_in2out_node;
693 extern vlib_node_registration_t nat44_ed_in2out_output_node;
694 extern vlib_node_registration_t nat44_ed_out2in_node;
695
696 extern fib_source_t nat_fib_src_hi;
697 extern fib_source_t nat_fib_src_low;
698
699 /* format functions */
700 format_function_t format_snat_static_mapping;
701 format_function_t format_snat_static_map_to_resolve;
702 format_function_t format_snat_session;
703 format_function_t format_static_mapping_key;
704 format_function_t format_nat_addr_and_port_alloc_alg;
705
706 /** \brief Check if SNAT session is created from static mapping.
707     @param s SNAT session
708     @return true if SNAT session is created from static mapping otherwise 0
709 */
710 always_inline bool
711 nat44_ed_is_session_static (snat_session_t *s)
712 {
713   return s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING;
714 }
715
716 /** \brief Check if NAT session is twice NAT.
717     @param s NAT session
718     @return true if NAT session is twice NAT
719 */
720 always_inline bool
721 nat44_ed_is_twice_nat_session (snat_session_t *s)
722 {
723   return s->flags & SNAT_SESSION_FLAG_TWICE_NAT;
724 }
725
726 /** \brief Check if NAT session is load-balancing.
727     @param s NAT session
728     @return true if NAT session is load-balancing
729 */
730 always_inline bool
731 nat44_ed_is_lb_session (snat_session_t *s)
732 {
733   return s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING;
734 }
735
736 /** \brief Check if NAT session is forwarding bypass.
737     @param s NAT session
738     @return true if NAT session is load-balancing
739 */
740 always_inline bool
741 na44_ed_is_fwd_bypass_session (snat_session_t *s)
742 {
743   return s->flags & SNAT_SESSION_FLAG_FWD_BYPASS;
744 }
745
746 /** \brief Check if NAT session has affinity record.
747     @param s NAT session
748     @return true if NAT session has affinity record
749 */
750 always_inline bool
751 nat44_ed_is_affinity_session (snat_session_t *s)
752 {
753   return s->flags & SNAT_SESSION_FLAG_AFFINITY;
754 }
755
756 /** \brief Check if exact pool address should be used.
757     @param s SNAT session
758     @return true if exact pool address
759 */
760 always_inline bool
761 nat44_ed_is_exact_address_session (snat_session_t *s)
762 {
763   return s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS;
764 }
765
766 /** \brief Check if NAT interface is inside.
767     @param i NAT interface
768     @return true if inside interface
769 */
770 always_inline bool
771 nat44_ed_is_interface_inside (snat_interface_t *i)
772 {
773   return i->flags & NAT_INTERFACE_FLAG_IS_INSIDE;
774 }
775
776 /** \brief Check if NAT interface is outside.
777     @param i NAT interface
778     @return true if outside interface
779 */
780 always_inline bool
781 nat44_ed_is_interface_outside (snat_interface_t *i)
782 {
783   return i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE;
784 }
785
786 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
787     @param s NAT session
788     @return true if session is closed
789 */
790 always_inline bool
791 nat44_is_ses_closed (snat_session_t *s)
792 {
793   return s->state == 0xf;
794 }
795
796 /** \brief Check if client initiating TCP connection (received SYN from client)
797     @param t TCP header
798     @return true if client initiating TCP connection
799 */
800 always_inline bool
801 tcp_flags_is_init (u8 f)
802 {
803   return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
804 }
805
806 always_inline bool
807 is_sm_addr_only (u32 f)
808 {
809   return (f & NAT_SM_FLAG_ADDR_ONLY);
810 }
811
812 always_inline bool
813 is_sm_out2in_only (u32 f)
814 {
815   return (f & NAT_SM_FLAG_OUT2IN_ONLY);
816 }
817
818 always_inline bool
819 is_sm_identity_nat (u32 f)
820 {
821   return (f & NAT_SM_FLAG_IDENTITY_NAT);
822 }
823
824 always_inline bool
825 is_sm_lb (u32 f)
826 {
827   return (f & NAT_SM_FLAG_LB);
828 }
829
830 always_inline bool
831 is_sm_exact_address (u32 f)
832 {
833   return (f & NAT_SM_FLAG_EXACT_ADDRESS);
834 }
835
836 always_inline bool
837 is_sm_self_twice_nat (u32 f)
838 {
839   return (f & NAT_SM_FLAG_SELF_TWICE_NAT);
840 }
841
842 always_inline bool
843 is_sm_twice_nat (u32 f)
844 {
845   return (f & NAT_SM_FLAG_TWICE_NAT);
846 }
847
848 always_inline bool
849 is_sm_switch_address (u32 f)
850 {
851   return (f & NAT_SM_FLAG_SWITCH_ADDRESS);
852 }
853
854 /* logging */
855 #define nat_log_err(...) \
856   vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
857 #define nat_log_warn(...) \
858   vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
859 #define nat_log_notice(...) \
860   vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
861 #define nat_log_info(...) \
862   vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
863 #define nat_log_debug(...)\
864   vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
865
866 clib_error_t *nat44_api_hookup (vlib_main_t *vm);
867
868 int snat_set_workers (uword *bitmap);
869
870 int nat44_plugin_enable (nat44_config_t c);
871 int nat44_plugin_disable ();
872
873 int nat44_ed_add_interface (u32 sw_if_index, u8 is_inside);
874 int nat44_ed_del_interface (u32 sw_if_index, u8 is_inside);
875 int nat44_ed_add_output_interface (u32 sw_if_index);
876 int nat44_ed_del_output_interface (u32 sw_if_index);
877
878 int nat44_ed_add_address (ip4_address_t *addr, u32 vrf_id, u8 twice_nat);
879 int nat44_ed_del_address (ip4_address_t addr, u8 delete_sm, u8 twice_nat);
880 int nat44_ed_add_interface_address (u32 sw_if_index, u8 twice_nat);
881 int nat44_ed_del_interface_address (u32 sw_if_index, u8 twice_nat);
882
883 int nat44_ed_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
884                                  u16 l_port, u16 e_port, ip_protocol_t proto,
885                                  u32 vrf_id, u32 sw_if_index, u32 flags,
886                                  ip4_address_t pool_addr, u8 *tag);
887
888 int nat44_ed_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
889                                  u16 l_port, u16 e_port, ip_protocol_t proto,
890                                  u32 vrf_id, u32 sw_if_index, u32 flags);
891
892 int nat44_ed_add_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
893                                     ip_protocol_t proto,
894                                     nat44_lb_addr_port_t *locals, u32 flags,
895                                     u8 *tag, u32 affinity);
896
897 int nat44_ed_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
898                                     ip_protocol_t proto, u32 flags);
899
900 int nat44_ed_add_del_lb_static_mapping_local (ip4_address_t e_addr, u16 e_port,
901                                               ip4_address_t l_addr, u16 l_port,
902                                               ip_protocol_t proto, u32 vrf_id,
903                                               u8 probability, u8 is_add);
904
905 /**
906  * @brief Delete NAT44 endpoint-dependent session
907  *
908  * @param sm     snat global configuration data
909  * @param addr   IPv4 address
910  * @param port   L4 port number
911  * @param proto  L4 protocol
912  * @param vrf_id VRF ID
913  * @param is_in  1 = inside network address and port pair, 0 = outside
914  *
915  * @return 0 on success, non-zero value otherwise
916  */
917 int nat44_ed_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
918                           ip4_address_t *eh_addr, u16 eh_port, u8 proto,
919                           u32 vrf_id, int is_in);
920
921 void nat44_ed_free_session_data (snat_main_t *sm, snat_session_t *s,
922                                  u32 thread_index, u8 is_ha);
923
924 /**
925  * @brief Set NAT44 session limit (session limit, vrf id)
926  *
927  * @param session_limit Session limit
928  * @param vrf_id        VRF id
929  * @return 0 on success, non-zero value otherwise
930  */
931 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
932
933 /**
934  * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
935  *
936  * @param session_limit Session limit
937  * @param vrf_id        VRF id
938  * @return 0 on success, non-zero value otherwise
939  */
940 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
941
942 void expire_per_vrf_sessions (u32 fib_index);
943
944 /**
945  * @brief Match NAT44 static mapping.
946  *
947  * @param key             address and port to match
948  * @param addr            external/local address of the matched mapping
949  * @param port            port of the matched mapping
950  * @param fib_index       fib index of the matched mapping
951  * @param by_external     if 0 match by local address otherwise match by external
952  *                        address
953  * @param is_addr_only    1 if matched mapping is address only
954  * @param twice_nat       matched mapping is twice NAT type
955  * @param lb              1 if matched mapping is load-balanced
956  * @param ext_host_addr   external host address
957  * @param is_identity_nat 1 if indentity mapping
958  * @param out             if !=0 set to pointer of the mapping structure
959  *
960  * @returns 0 if match found otherwise 1.
961  */
962 int snat_static_mapping_match (
963   vlib_main_t *vm, snat_main_t *sm, ip4_address_t match_addr, u16 match_port,
964   u32 match_fib_index, ip_protocol_t match_protocol,
965   ip4_address_t *mapping_addr, u16 *mapping_port, u32 *mapping_fib_index,
966   int by_external, u8 *is_addr_only, twice_nat_type_t *twice_nat,
967   lb_nat_type_t *lb, ip4_address_t *ext_host_addr, u8 *is_identity_nat,
968   snat_static_mapping_t **out);
969
970 /*
971  * Why is this here? Because we don't need to touch this layer to
972  * simply reply to an icmp. We need to change id to a unique
973  * value to NAT an echo request/reply.
974  */
975
976 typedef struct
977 {
978   u16 identifier;
979   u16 sequence;
980 } icmp_echo_header_t;
981
982 typedef struct
983 {
984   u16 src_port, dst_port;
985 } tcp_udp_header_t;
986
987 u32 get_thread_idx_by_port (u16 e_port);
988
989 u8 *format_static_mapping_kvp (u8 *s, va_list *args);
990
991 u8 *format_session_kvp (u8 *s, va_list *args);
992
993 u32 nat_calc_bihash_buckets (u32 n_elts);
994
995 void nat44_addresses_free (snat_address_t **addresses);
996
997 void nat44_ed_sessions_clear ();
998
999 int nat44_ed_set_frame_queue_nelts (u32 frame_queue_nelts);
1000
1001 typedef enum
1002 {
1003   NAT_ED_TRNSL_ERR_SUCCESS = 0,
1004   NAT_ED_TRNSL_ERR_TRANSLATION_FAILED = 1,
1005   NAT_ED_TRNSL_ERR_FLOW_MISMATCH = 2,
1006   NAT_ED_TRNSL_ERR_PACKET_TRUNCATED = 3,
1007   NAT_ED_TRNSL_ERR_INNER_IP_CORRUPT = 4,
1008   NAT_ED_TRNSL_ERR_INVALID_CSUM = 5,
1009 } nat_translation_error_e;
1010
1011 nat_translation_error_e nat_6t_flow_buf_translate_i2o (
1012   vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
1013   nat_6t_flow_t *f, ip_protocol_t proto, int is_output_feature);
1014
1015 nat_translation_error_e nat_6t_flow_buf_translate_o2i (
1016   vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
1017   nat_6t_flow_t *f, ip_protocol_t proto, int is_output_feature);
1018
1019 void nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f);
1020
1021 format_function_t format_nat_ed_translation_error;
1022 format_function_t format_nat_6t_flow;
1023 format_function_t format_ed_session_kvp;
1024
1025 snat_static_mapping_t *nat44_ed_sm_i2o_lookup (snat_main_t *sm,
1026                                                ip4_address_t addr, u16 port,
1027                                                u32 fib_index, u8 proto);
1028
1029 snat_static_mapping_t *nat44_ed_sm_o2i_lookup (snat_main_t *sm,
1030                                                ip4_address_t addr, u16 port,
1031                                                u32 fib_index, u8 proto);
1032
1033 void nat_syslog_nat44_sadd (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
1034                             u16 isport, ip4_address_t *idaddr, u16 idport,
1035                             ip4_address_t *xsaddr, u16 xsport,
1036                             ip4_address_t *xdaddr, u16 xdport, u8 proto,
1037                             u8 is_twicenat);
1038
1039 void nat_syslog_nat44_sdel (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
1040                             u16 isport, ip4_address_t *idaddr, u16 idport,
1041                             ip4_address_t *xsaddr, u16 xsport,
1042                             ip4_address_t *xdaddr, u16 xdport, u8 proto,
1043                             u8 is_twicenat);
1044
1045 #endif /* __included_nat44_ed_h__ */
1046 /*
1047  * fd.io coding-style-patch-verification: ON
1048  *
1049  * Local Variables:
1050  * eval: (c-set-style "gnu")
1051  * End:
1052  */