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