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