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