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