nat: harden ICMP handling
[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   _ (TRNSL_FAILED, "couldn't translate packet")
136
137 typedef enum
138 {
139 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
140   foreach_nat_in2out_ed_error
141 #undef _
142     NAT_IN2OUT_ED_N_ERROR,
143 } nat_in2out_ed_error_t;
144
145 #define foreach_nat_out2in_ed_error                                           \
146   _ (UNSUPPORTED_PROTOCOL, "unsupported protocol")                            \
147   _ (OUT_OF_PORTS, "out of ports")                                            \
148   _ (BAD_ICMP_TYPE, "unsupported ICMP type")                                  \
149   _ (NO_TRANSLATION, "no translation")                                        \
150   _ (MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")                      \
151   _ (NON_SYN, "non-SYN packet try to create session")                         \
152   _ (TCP_CLOSED, "drops due to TCP in transitory timeout")                    \
153   _ (HASH_ADD_FAILED, "hash table add failed")                                \
154   _ (TRNSL_FAILED, "couldn't translate packet")
155
156 typedef enum
157 {
158 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
159   foreach_nat_out2in_ed_error
160 #undef _
161     NAT_OUT2IN_ED_N_ERROR,
162 } nat_out2in_ed_error_t;
163
164
165 /* Endpoint dependent TCP session state */
166 #define NAT44_SES_I2O_FIN 1
167 #define NAT44_SES_O2I_FIN 2
168 #define NAT44_SES_I2O_FIN_ACK 4
169 #define NAT44_SES_O2I_FIN_ACK 8
170 #define NAT44_SES_I2O_SYN 16
171 #define NAT44_SES_O2I_SYN 32
172 #define NAT44_SES_RST     64
173
174 /* Session flags */
175 #define SNAT_SESSION_FLAG_STATIC_MAPPING     (1 << 0)
176 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO      (1 << 1)
177 #define SNAT_SESSION_FLAG_LOAD_BALANCING     (1 << 2)
178 #define SNAT_SESSION_FLAG_TWICE_NAT          (1 << 3)
179 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT (1 << 4)
180 #define SNAT_SESSION_FLAG_FWD_BYPASS         (1 << 5)
181 #define SNAT_SESSION_FLAG_AFFINITY           (1 << 6)
182 #define SNAT_SESSION_FLAG_EXACT_ADDRESS      (1 << 7)
183 #define SNAT_SESSION_FLAG_HAIRPINNING        (1 << 8)
184
185 /* NAT interface flags */
186 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
187 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
188
189 /* Static mapping flags */
190 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY      1
191 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY    2
192 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT   4
193 #define NAT_STATIC_MAPPING_FLAG_LB             8
194 #define NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS  16
195
196 typedef CLIB_PACKED(struct
197 {
198   // number of sessions in this vrf
199   u32 ses_count;
200
201   u32 rx_fib_index;
202   u32 tx_fib_index;
203
204   // is this vrf expired
205   u8 expired;
206 }) per_vrf_sessions_t;
207
208 typedef union
209 {
210   struct
211   {
212     ip4_address_t saddr, daddr;
213     u16 sport; // ICMP id for ICMP case
214     u16 dport;
215     u32 fib_index : 24;
216     u8 proto;
217   };
218   u64 as_u64[2];
219   u64x2u as_u128;
220 } nat_6t_t;
221
222 STATIC_ASSERT_SIZEOF (nat_6t_t, 2 * sizeof (u64));
223
224 typedef struct
225 {
226 #define NAT_FLOW_OP_SADDR_REWRITE   (1 << 1)
227 #define NAT_FLOW_OP_SPORT_REWRITE   (1 << 2)
228 #define NAT_FLOW_OP_DADDR_REWRITE   (1 << 3)
229 #define NAT_FLOW_OP_DPORT_REWRITE   (1 << 4)
230 #define NAT_FLOW_OP_ICMP_ID_REWRITE (1 << 5)
231 #define NAT_FLOW_OP_TXFIB_REWRITE   (1 << 6)
232   int ops;
233   nat_6t_t match;
234   struct
235   {
236     ip4_address_t saddr, daddr;
237     u16 sport;
238     u16 dport;
239     u32 fib_index;
240     u8 proto;
241     u16 icmp_id;
242   } rewrite;
243   uword l3_csum_delta;
244   uword l4_csum_delta;
245 } nat_6t_flow_t;
246
247 void nat44_ed_forwarding_enable_disable (u8 is_enable);
248
249 always_inline void
250 nat_6t_flow_saddr_rewrite_set (nat_6t_flow_t *f, u32 saddr)
251 {
252   f->ops |= NAT_FLOW_OP_SADDR_REWRITE;
253   f->rewrite.saddr.as_u32 = saddr;
254 }
255
256 always_inline void
257 nat_6t_flow_daddr_rewrite_set (nat_6t_flow_t *f, u32 daddr)
258 {
259   f->ops |= NAT_FLOW_OP_DADDR_REWRITE;
260   f->rewrite.daddr.as_u32 = daddr;
261 }
262
263 always_inline void
264 nat_6t_flow_sport_rewrite_set (nat_6t_flow_t *f, u32 sport)
265 {
266   f->ops |= NAT_FLOW_OP_SPORT_REWRITE;
267   f->rewrite.sport = sport;
268 }
269
270 always_inline void
271 nat_6t_flow_dport_rewrite_set (nat_6t_flow_t *f, u32 dport)
272 {
273   f->ops |= NAT_FLOW_OP_DPORT_REWRITE;
274   f->rewrite.dport = dport;
275 }
276
277 always_inline void
278 nat_6t_flow_txfib_rewrite_set (nat_6t_flow_t *f, u32 tx_fib_index)
279 {
280   f->ops |= NAT_FLOW_OP_TXFIB_REWRITE;
281   f->rewrite.fib_index = tx_fib_index;
282 }
283
284 always_inline void
285 nat_6t_flow_icmp_id_rewrite_set (nat_6t_flow_t *f, u16 id)
286 {
287   f->ops |= NAT_FLOW_OP_ICMP_ID_REWRITE;
288   f->rewrite.icmp_id = id;
289 }
290
291 typedef CLIB_PACKED(struct
292 {
293   /* Outside network tuple */
294   struct
295   {
296     ip4_address_t addr;
297     u32 fib_index;
298     u16 port;
299   } out2in;
300
301   /* Inside network tuple */
302   struct
303   {
304     ip4_address_t addr;
305     u32 fib_index;
306     u16 port;
307   } in2out;
308
309   nat_protocol_t nat_proto;
310
311   nat_6t_flow_t i2o;
312   nat_6t_flow_t o2i;
313
314   /* Flags */
315   u32 flags;
316
317   /* head of LRU list in which this session is tracked */
318   u32 lru_head_index;
319   /* index in global LRU list */
320   u32 lru_index;
321   f64 last_lru_update;
322
323   /* Last heard timer */
324   f64 last_heard;
325
326   /* Last HA refresh */
327   f64 ha_last_refreshed;
328
329   /* Counters */
330   u64 total_bytes;
331   u32 total_pkts;
332
333   /* External host address and port */
334   ip4_address_t ext_host_addr;
335   u16 ext_host_port;
336
337   /* External host address and port after translation */
338   ip4_address_t ext_host_nat_addr;
339   u16 ext_host_nat_port;
340
341   /* TCP session state */
342   u8 state;
343   u32 i2o_fin_seq;
344   u32 o2i_fin_seq;
345   u64 tcp_closed_timestamp;
346
347   /* per vrf sessions index */
348   u32 per_vrf_sessions_index;
349
350   u32 thread_index;
351 }) snat_session_t;
352
353 typedef struct
354 {
355   ip4_address_t addr;
356   u32 fib_index;
357 #define _(N, i, n, s) \
358   u32 busy_##n##_ports; \
359   u32 * busy_##n##_ports_per_thread; \
360   u32 busy_##n##_port_refcounts[65535];
361   foreach_nat_protocol
362 #undef _
363 } snat_address_t;
364
365 typedef struct
366 {
367   u32 fib_index;
368   u32 ref_count;
369 } nat_fib_t;
370
371 typedef struct
372 {
373   u32 fib_index;
374   u32 refcount;
375 } nat_outside_fib_t;
376
377 typedef struct
378 {
379   /* backend IP address */
380   ip4_address_t addr;
381   /* backend port number */
382   u16 port;
383   /* probability of the backend to be randomly matched */
384   u8 probability;
385   u8 prefix;
386   /* backend FIB table */
387   u32 vrf_id;
388   u32 fib_index;
389 } nat44_lb_addr_port_t;
390
391 typedef enum
392 {
393   /* twice-nat disabled */
394   TWICE_NAT_DISABLED,
395   /* twice-nat enabled */
396   TWICE_NAT,
397   /* twice-nat only when src IP equals dst IP after translation */
398   TWICE_NAT_SELF,
399 } twice_nat_type_t;
400
401 typedef enum
402 {
403   /* no load-balancing */
404   NO_LB_NAT,
405   /* load-balancing */
406   LB_NAT,
407   /* load-balancing with affinity */
408   AFFINITY_LB_NAT,
409 } lb_nat_type_t;
410
411 typedef struct
412 {
413   /* prefered pool address */
414   ip4_address_t pool_addr;
415   /* local IP address */
416   ip4_address_t local_addr;
417   /* external IP address */
418   ip4_address_t external_addr;
419   /* local port */
420   u16 local_port;
421   /* external port */
422   u16 external_port;
423   /* is twice-nat */
424   twice_nat_type_t twice_nat;
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
544   /* Vector of outside addresses */
545   snat_address_t *addresses;
546   /* Address and port allocation function */
547   nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
548   /* Address and port allocation type */
549   nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
550   /* Port set parameters (MAP-E) */
551   u8 psid_offset;
552   u8 psid_length;
553   u16 psid;
554   /* Port range parameters */
555   u16 start_port;
556   u16 end_port;
557
558   /* vector of fibs */
559   nat_fib_t *fibs;
560
561   /* vector of outside fibs */
562   nat_outside_fib_t *outside_fibs;
563
564   /* Vector of twice NAT addresses for external hosts */
565   snat_address_t *twice_nat_addresses;
566
567   /* sw_if_indices whose intfc addresses should be auto-added */
568   u32 *auto_add_sw_if_indices;
569   u32 *auto_add_sw_if_indices_twice_nat;
570
571   /* vector of interface address static mappings to resolve. */
572   snat_static_map_resolve_t *to_resolve;
573
574   /* Randomize port allocation order */
575   u32 random_seed;
576
577   /* Worker handoff frame-queue index */
578   u32 fq_in2out_index;
579   u32 fq_in2out_output_index;
580   u32 fq_out2in_index;
581
582   u32 out2in_node_index;
583   u32 in2out_node_index;
584   u32 in2out_output_node_index;
585
586   nat44_config_t rconfig;
587   //nat44_config_t cconfig;
588
589   /* If forwarding is enabled */
590   u8 forwarding_enabled;
591
592   /* static mapping config */
593   u8 static_mapping_only;
594   u8 static_mapping_connection_tracking;
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   /* pat - dynamic mapping enabled or conneciton tracking */
668   u8 pat;
669
670   /* number of worker handoff frame queue elements */
671   u32 frame_queue_nelts;
672
673   /* nat44 plugin enabled */
674   u8 enabled;
675
676   vnet_main_t *vnet_main;
677
678 } snat_main_t;
679
680 typedef struct
681 {
682   u32 thread_index;
683   f64 now;
684 } nat44_is_idle_session_ctx_t;
685
686 typedef struct
687 {
688   u32 cached_sw_if_index;
689   uword *cached_presence_by_ip4_address;
690 } snat_runtime_t;
691
692 extern snat_main_t snat_main;
693
694 // nat pre ed next_node feature classification
695 extern vlib_node_registration_t nat_default_node;
696 extern vlib_node_registration_t nat_pre_in2out_node;
697 extern vlib_node_registration_t nat_pre_out2in_node;
698
699 extern vlib_node_registration_t snat_in2out_node;
700 extern vlib_node_registration_t snat_in2out_output_node;
701 extern vlib_node_registration_t snat_out2in_node;
702 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
703 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
704 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
705 extern vlib_node_registration_t nat44_ed_in2out_node;
706 extern vlib_node_registration_t nat44_ed_in2out_output_node;
707 extern vlib_node_registration_t nat44_ed_out2in_node;
708
709 extern fib_source_t nat_fib_src_hi;
710 extern fib_source_t nat_fib_src_low;
711
712 /* format functions */
713 format_function_t format_snat_static_mapping;
714 format_function_t format_snat_static_map_to_resolve;
715 format_function_t format_snat_session;
716 format_function_t format_snat_key;
717 format_function_t format_static_mapping_key;
718 format_function_t format_nat_protocol;
719 format_function_t format_nat_addr_and_port_alloc_alg;
720 /* unformat functions */
721 unformat_function_t unformat_nat_protocol;
722
723 /** \brief Check if SNAT session is created from static mapping.
724     @param s SNAT session
725     @return 1 if SNAT session is created from static mapping otherwise 0
726 */
727 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
728
729 /** \brief Check if SNAT session for unknown protocol.
730     @param s SNAT session
731     @return 1 if SNAT session for unknown protocol otherwise 0
732 */
733 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
734
735 /** \brief Check if NAT session is twice NAT.
736     @param s NAT session
737     @return 1 if NAT session is twice NAT
738 */
739 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
740
741 /** \brief Check if NAT session is load-balancing.
742     @param s NAT session
743     @return 1 if NAT session is load-balancing
744 */
745 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
746
747 /** \brief Check if NAT session is forwarding bypass.
748     @param s NAT session
749     @return 1 if NAT session is load-balancing
750 */
751 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
752
753 /** \brief Check if NAT session is endpoint dependent.
754     @param s NAT session
755     @return 1 if NAT session is endpoint dependent
756 */
757 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
758
759 /** \brief Check if NAT session has affinity record.
760     @param s NAT session
761     @return 1 if NAT session has affinity record
762 */
763 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
764
765 /** \brief Check if exact pool address should be used.
766     @param s SNAT session
767     @return 1 if exact pool address or 0
768 */
769 #define is_exact_address_session(s) (s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS)
770
771 /** \brief Check if NAT interface is inside.
772     @param i NAT interface
773     @return 1 if inside interface
774 */
775 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
776
777 /** \brief Check if NAT interface is outside.
778     @param i NAT interface
779     @return 1 if outside interface
780 */
781 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
782
783 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
784     @param s NAT session
785     @return 1 if session is closed
786 */
787 #define nat44_is_ses_closed(s) s->state == 0xf
788
789 /** \brief Check if NAT static mapping is address only (1:1NAT).
790     @param sm NAT static mapping
791     @return 1 if 1:1NAT, 0 if 1:1NAPT
792 */
793 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
794
795 /** \brief Check if NAT static mapping match only out2in direction.
796     @param sm NAT static mapping
797     @return 1 if rule match only out2in direction
798 */
799 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
800
801 /** \brief Check if NAT static mapping is identity NAT.
802     @param sm NAT static mapping
803     @return 1 if identity NAT
804 */
805 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
806
807 /** \brief Check if NAT static mapping is load-balancing.
808     @param sm NAT static mapping
809     @return 1 if load-balancing
810 */
811 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
812
813 /** \brief Check if exact pool address should be used.
814     @param s SNAT session
815     @return 1 if exact pool address or 0
816 */
817 #define is_exact_address(s) (s->flags & NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS)
818
819 /** \brief Check if client initiating TCP connection (received SYN from client)
820     @param t TCP header
821     @return 1 if client initiating TCP connection
822 */
823 always_inline bool
824 tcp_flags_is_init (u8 f)
825 {
826   return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
827 }
828
829 /* logging */
830 #define nat_log_err(...) \
831   vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
832 #define nat_log_warn(...) \
833   vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
834 #define nat_log_notice(...) \
835   vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
836 #define nat_log_info(...) \
837   vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
838 #define nat_log_debug(...)\
839   vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
840
841 /**
842  * @brief Enable NAT44 plugin
843  *
844  * @param c         nat44_config_t
845  *
846  * @return 0 on success, non-zero value otherwise
847  */
848 int nat44_plugin_enable (nat44_config_t c);
849
850 /**
851  * @brief Disable NAT44 plugin
852  *
853  * @return 0 on success, non-zero value otherwise
854  */
855 int nat44_plugin_disable ();
856
857 /**
858  * @brief Add external address to NAT44 pool
859  *
860  * @param sm        snat global configuration data
861  * @param addr      IPv4 address
862  * @param vrf_id    VRF id of tenant, ~0 means independent of VRF
863  * @param twice_nat 1 if twice NAT address
864  *
865  * @return 0 on success, non-zero value otherwise
866  */
867 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
868                       u8 twice_nat);
869
870 /**
871  * @brief Delete external address from NAT44 pool
872  *
873  * @param sm        snat global configuration data
874  * @param addr      IPv4 address
875  * @param delete_sm 1 if delete static mapping using address
876  * @param twice_nat 1 if twice NAT address
877  *
878  * @return 0 on success, non-zero value otherwise
879  */
880 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
881                       u8 twice_nat);
882
883 /**
884  * @brief Add/delete external address to FIB DPO (out2in DPO mode)
885  *
886  * @param addr   IPv4 address
887  * @param is_add 1 = add, 0 = delete
888  *
889  * @return 0 on success, non-zero value otherwise
890  */
891 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
892
893 /**
894  * @brief Add/delete NAT44 static mapping
895  *
896  * @param l_addr       local IPv4 address
897  * @param e_addr       external IPv4 address
898  * @param l_port       local port number
899  * @param e_port       external port number
900  * @param vrf_id       local VRF ID
901  * @param addr_only    1 = 1:1NAT, 0 = 1:1NAPT
902  * @param sw_if_index  use interface address as external IPv4 address
903  * @param proto        L4 protocol
904  * @param is_add       1 = add, 0 = delete
905  * @param twice_nat    twice-nat mode
906  * @param out2in_only  if 1 rule match only out2in direction
907  * @param tag          opaque string tag
908  * @param identity_nat identity NAT
909  * @param pool_addr    pool IPv4 address
910  * @param exact        1 = exact pool address
911  *
912  * @return 0 on success, non-zero value otherwise
913  */
914 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
915                              u16 l_port, u16 e_port, u32 vrf_id,
916                              int addr_only, u32 sw_if_index,
917                              nat_protocol_t proto, int is_add,
918                              twice_nat_type_t twice_nat, u8 out2in_only,
919                              u8 * tag, u8 identity_nat,
920                              ip4_address_t pool_addr, int exact);
921
922 /**
923  * @brief Add/delete static mapping with load-balancing (multiple backends)
924  *
925  * @param e_addr      external IPv4 address
926  * @param e_port      external port number
927  * @param proto       L4 protocol
928  * @param locals      list of local backends
929  * @param is_add      1 = add, 0 = delete
930  * @param twice_nat   twice-nat mode
931  * @param out2in_only if 1 rule match only out2in direction
932  * @param tag         opaque string tag
933  * @param affinity    0 = disabled, otherwise client IP affinity sticky time
934  *
935  * @return 0 on success, non-zero value otherwise
936  */
937 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
938                                      nat_protocol_t proto,
939                                      nat44_lb_addr_port_t * locals, u8 is_add,
940                                      twice_nat_type_t twice_nat,
941                                      u8 out2in_only, u8 * tag, u32 affinity);
942
943 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
944                                            ip4_address_t l_addr, u16 l_port,
945                                            nat_protocol_t proto, u32 vrf_id,
946                                            u8 probability, u8 is_add);
947
948 clib_error_t *nat44_api_hookup (vlib_main_t * vm);
949
950 /**
951  * @brief Set NAT plugin workers
952  *
953  * @param bitmap NAT workers bitmap
954  *
955  * @return 0 on success, non-zero value otherwise
956  */
957 int snat_set_workers (uword * bitmap);
958
959 int nat44_ed_add_interface (u32 sw_if_index, u8 is_inside);
960 int nat44_ed_del_interface (u32 sw_if_index, u8 is_inside);
961 int nat44_ed_add_output_interface (u32 sw_if_index);
962 int nat44_ed_del_output_interface (u32 sw_if_index);
963
964 /**
965  * @brief Add/delete NAT44 pool address from specific interface
966  *
967  * @param sw_if_index software index of the interface
968  * @param is_del      1 = delete, 0 = add
969  * @param twice_nat   1 = twice NAT address for external hosts
970  *
971  * @return 0 on success, non-zero value otherwise
972  */
973 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
974                                 u8 twice_nat);
975
976 /**
977  * @brief Delete NAT44 endpoint-dependent session
978  *
979  * @param sm     snat global configuration data
980  * @param addr   IPv4 address
981  * @param port   L4 port number
982  * @param proto  L4 protocol
983  * @param vrf_id VRF ID
984  * @param is_in  1 = inside network address and port pair, 0 = outside
985  *
986  * @return 0 on success, non-zero value otherwise
987  */
988 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
989                           ip4_address_t * eh_addr, u16 eh_port, u8 proto,
990                           u32 vrf_id, int is_in);
991
992 /**
993  * @brief Free NAT44 session data (lookup keys, external address port)
994  *
995  * @param sm           snat global configuration data
996  * @param s            NAT session
997  * @param thread_index thread index
998  * @param is_ha        is HA event
999  */
1000 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1001                             u32 thread_index, u8 is_ha);
1002
1003 /**
1004  * @brief Set NAT44 session limit (session limit, vrf id)
1005  *
1006  * @param session_limit Session limit
1007  * @param vrf_id        VRF id
1008  * @return 0 on success, non-zero value otherwise
1009  */
1010 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1011
1012 /**
1013  * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
1014  *
1015  * @param session_limit Session limit
1016  * @param vrf_id        VRF id
1017  * @return 0 on success, non-zero value otherwise
1018  */
1019 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
1020
1021 /**
1022  * @brief Free outside address and port pair
1023  *
1024  * @param addresses    vector of outside addresses
1025  * @param thread_index thread index
1026  * @param key          address, port and protocol
1027  */
1028 void
1029 snat_free_outside_address_and_port (snat_address_t * addresses,
1030                                     u32 thread_index,
1031                                     ip4_address_t * addr,
1032                                     u16 port, nat_protocol_t protocol);
1033
1034 void expire_per_vrf_sessions (u32 fib_index);
1035
1036 /**
1037  * @brief Match NAT44 static mapping.
1038  *
1039  * @param key             address and port to match
1040  * @param addr            external/local address of the matched mapping
1041  * @param port            port of the matched mapping
1042  * @param fib_index       fib index of the matched mapping
1043  * @param by_external     if 0 match by local address otherwise match by external
1044  *                        address
1045  * @param is_addr_only    1 if matched mapping is address only
1046  * @param twice_nat       matched mapping is twice NAT type
1047  * @param lb              1 if matched mapping is load-balanced
1048  * @param ext_host_addr   external host address
1049  * @param is_identity_nat 1 if indentity mapping
1050  * @param out             if !=0 set to pointer of the mapping structure
1051  *
1052  * @returns 0 if match found otherwise 1.
1053  */
1054 int snat_static_mapping_match (
1055   vlib_main_t *vm, snat_main_t *sm, ip4_address_t match_addr, u16 match_port,
1056   u32 match_fib_index, nat_protocol_t match_protocol,
1057   ip4_address_t *mapping_addr, u16 *mapping_port, u32 *mapping_fib_index,
1058   u8 by_external, u8 *is_addr_only, twice_nat_type_t *twice_nat,
1059   lb_nat_type_t *lb, ip4_address_t *ext_host_addr, u8 *is_identity_nat,
1060   snat_static_mapping_t **out);
1061
1062 /**
1063  * @brief Add/del NAT address to FIB.
1064  *
1065  * Add the external NAT address to the FIB as receive entries. This ensures
1066  * that VPP will reply to ARP for this address and we don't need to enable
1067  * proxy ARP on the outside interface.
1068  *
1069  * @param addr        IPv4 address
1070  * @param plen        address prefix length
1071  * @param sw_if_index software index of the outside interface
1072  * @param is_add      0 = delete, 1 = add.
1073  */
1074 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1075                                u8 p_len, u32 sw_if_index, int is_add);
1076
1077 #if 0
1078 void
1079 nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
1080                    ip4_address_t * out_addr, u16 out_port,
1081                    ip4_address_t * eh_addr, u16 eh_port,
1082                    ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
1083                    u32 fib_index, u16 flags, u32 thread_index);
1084
1085 void
1086 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
1087                    ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1088                    u32 fib_index, u32 ti);
1089
1090 void
1091 nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
1092                    ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1093                    u32 fib_index, u32 total_pkts, u64 total_bytes,
1094                    u32 thread_index);
1095 #endif
1096
1097 int nat_set_outside_address_and_port (snat_address_t *addresses,
1098                                       u32 thread_index, ip4_address_t addr,
1099                                       u16 port, nat_protocol_t protocol);
1100
1101 /*
1102  * Why is this here? Because we don't need to touch this layer to
1103  * simply reply to an icmp. We need to change id to a unique
1104  * value to NAT an echo request/reply.
1105  */
1106
1107 typedef struct
1108 {
1109   u16 identifier;
1110   u16 sequence;
1111 } icmp_echo_header_t;
1112
1113 typedef struct
1114 {
1115   u16 src_port, dst_port;
1116 } tcp_udp_header_t;
1117
1118 u32 get_thread_idx_by_port (u16 e_port);
1119
1120 u8 *format_static_mapping_kvp (u8 *s, va_list *args);
1121
1122 u8 *format_session_kvp (u8 *s, va_list *args);
1123
1124 u32 nat_calc_bihash_buckets (u32 n_elts);
1125
1126 void nat44_addresses_free (snat_address_t **addresses);
1127
1128 void nat44_ed_sessions_clear ();
1129
1130 int nat44_ed_set_frame_queue_nelts (u32 frame_queue_nelts);
1131
1132 typedef enum
1133 {
1134   NAT_ED_TRNSL_ERR_SUCCESS = 0,
1135   NAT_ED_TRNSL_ERR_TRANSLATION_FAILED = 1,
1136   NAT_ED_TRNSL_ERR_FLOW_MISMATCH = 2,
1137   NAT_ED_TRNSL_ERR_PACKET_TRUNCATED = 3,
1138   NAT_ED_TRNSL_ERR_INNER_IP_CORRUPT = 4,
1139   NAT_ED_TRNSL_ERR_INVALID_CSUM = 5,
1140 } nat_translation_error_e;
1141
1142 nat_translation_error_e nat_6t_flow_buf_translate_i2o (
1143   vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
1144   nat_6t_flow_t *f, nat_protocol_t proto, int is_output_feature);
1145
1146 nat_translation_error_e nat_6t_flow_buf_translate_o2i (
1147   vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
1148   nat_6t_flow_t *f, nat_protocol_t proto, int is_output_feature);
1149
1150 void nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f);
1151
1152 format_function_t format_nat_ed_translation_error;
1153 format_function_t format_nat_6t_flow;
1154 format_function_t format_ed_session_kvp;
1155
1156 #endif /* __included_nat44_ed_h__ */
1157 /*
1158  * fd.io coding-style-patch-verification: ON
1159  *
1160  * Local Variables:
1161  * eval: (c-set-style "gnu")
1162  * End:
1163  */