2e5d85f071ad285aa089c8be3e8cf431d7c0766c
[vpp.git] / src / plugins / nat / nat44-ei / nat44_ei.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 nat44_ei.h
17  * NAT44 endpoint independent plugin declarations
18  */
19 #ifndef __included_nat44_ei_h__
20 #define __included_nat44_ei_h__
21
22 #include <vlib/log.h>
23 #include <vlibapi/api.h>
24
25 #include <vnet/vnet.h>
26 #include <vnet/ip/ip.h>
27 #include <vnet/ethernet/ethernet.h>
28 #include <vnet/ip/icmp46_packet.h>
29 #include <vnet/api_errno.h>
30 #include <vnet/fib/fib_source.h>
31
32 #include <vppinfra/dlist.h>
33 #include <vppinfra/error.h>
34 #include <vppinfra/bihash_8_8.h>
35 #include <vppinfra/hash.h>
36
37 #include <nat/lib/lib.h>
38 #include <nat/lib/inlines.h>
39
40 /* default number of worker handoff frame queue elements */
41 #define NAT_FQ_NELTS_DEFAULT 64
42
43 /* External address and port allocation modes */
44 #define foreach_nat44_ei_addr_and_port_alloc_alg                              \
45   _ (0, DEFAULT, "default")                                                   \
46   _ (1, MAPE, "map-e")                                                        \
47   _ (2, RANGE, "port-range")
48
49 typedef enum
50 {
51 #define _(v, N, s) NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_##N = v,
52   foreach_nat44_ei_addr_and_port_alloc_alg
53 #undef _
54 } nat44_ei_addr_and_port_alloc_alg_t;
55
56 /* Interface flags */
57 #define NAT44_EI_INTERFACE_FLAG_IS_INSIDE  (1 << 0)
58 #define NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE (1 << 1)
59
60 /* Session flags */
61 #define NAT44_EI_SESSION_FLAG_STATIC_MAPPING (1 << 0)
62 #define NAT44_EI_SESSION_FLAG_UNKNOWN_PROTO  (1 << 1)
63
64 /* Static mapping flags */
65 #define NAT44_EI_STATIC_MAPPING_FLAG_ADDR_ONLY    (1 << 0)
66 #define NAT44_EI_STATIC_MAPPING_FLAG_IDENTITY_NAT (1 << 1)
67
68 typedef struct
69 {
70   ip4_address_t addr;
71   u32 fib_index;
72 #define _(N, i, n, s)                                                         \
73   u32 busy_##n##_ports;                                                       \
74   u32 *busy_##n##_ports_per_thread;                                           \
75   u32 busy_##n##_port_refcounts[0xffff + 1];
76   foreach_nat_protocol
77 #undef _
78 } nat44_ei_address_t;
79
80 clib_error_t *nat44_ei_api_hookup (vlib_main_t *vm);
81
82 /* NAT address and port allocation function */
83 typedef int (nat44_ei_alloc_out_addr_and_port_function_t) (
84   nat44_ei_address_t *addresses, u32 fib_index, u32 thread_index,
85   nat_protocol_t proto, ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
86   u16 port_per_thread, u32 snat_thread_index);
87
88 typedef struct
89 {
90   u16 identifier;
91   u16 sequence;
92 } icmp_echo_header_t;
93
94 typedef struct
95 {
96   u16 src_port, dst_port;
97 } tcp_udp_header_t;
98
99 typedef struct
100 {
101   union
102   {
103     struct
104     {
105       ip4_address_t addr;
106       u32 fib_index;
107     };
108     u64 as_u64;
109   };
110 } nat44_ei_user_key_t;
111
112 typedef struct
113 {
114   /* maximum number of users */
115   u32 users;
116   /* maximum number of sessions */
117   u32 sessions;
118   /* maximum number of ssessions per user */
119   u32 user_sessions;
120
121   /* plugin features */
122   u8 static_mapping_only;
123   u8 connection_tracking;
124   u8 out2in_dpo;
125
126   u32 inside_vrf;
127   u32 outside_vrf;
128
129 } nat44_ei_config_t;
130
131 typedef struct
132 {
133   ip4_address_t l_addr;
134   ip4_address_t pool_addr;
135   u16 l_port;
136   u16 e_port;
137   u32 sw_if_index;
138   u32 vrf_id;
139   u32 flags;
140   nat_protocol_t proto;
141   u8 addr_only;
142   u8 identity_nat;
143   u8 exact;
144   u8 *tag;
145 } nat44_ei_static_map_resolve_t;
146
147 // TODO: cleanup/redo (there is no lb in EI nat)
148 typedef struct
149 {
150   /* backend IP address */
151   ip4_address_t addr;
152   /* backend port number */
153   u16 port;
154   /* probability of the backend to be randomly matched */
155   u8 probability;
156   u8 prefix;
157   /* backend FIB table */
158   u32 vrf_id;
159   u32 fib_index;
160 } nat44_ei_lb_addr_port_t;
161
162 typedef struct
163 {
164   /* preferred pool address */
165   ip4_address_t pool_addr;
166   /* local IP address */
167   ip4_address_t local_addr;
168   /* external IP address */
169   ip4_address_t external_addr;
170   /* local port */
171   u16 local_port;
172   /* external port */
173   u16 external_port;
174   /* local FIB table */
175   u32 vrf_id;
176   u32 fib_index;
177   /* protocol */
178   nat_protocol_t proto;
179   /* worker threads used by backends/local host */
180   u32 *workers;
181   /* opaque string tag */
182   u8 *tag;
183   /* backends for load-balancing mode */
184   nat44_ei_lb_addr_port_t *locals;
185   /* flags */
186   u32 flags;
187 } nat44_ei_static_mapping_t;
188
189 typedef struct
190 {
191   u32 sw_if_index;
192   u8 flags;
193 } nat44_ei_interface_t;
194
195 typedef struct
196 {
197   u32 fib_index;
198   u32 ref_count;
199 } nat44_ei_fib_t;
200
201 typedef struct
202 {
203   u32 fib_index;
204   u32 refcount;
205 } nat44_ei_outside_fib_t;
206
207 typedef CLIB_PACKED (struct {
208   /* Outside network tuple */
209   struct
210   {
211     ip4_address_t addr;
212     u32 fib_index;
213     u16 port;
214   } out2in;
215
216   /* Inside network tuple */
217   struct
218   {
219     ip4_address_t addr;
220     u32 fib_index;
221     u16 port;
222   } in2out;
223
224   nat_protocol_t nat_proto;
225
226   /* Flags */
227   u32 flags;
228
229   /* Per-user translations */
230   u32 per_user_index;
231   u32 per_user_list_head_index;
232
233   /* head of LRU list in which this session is tracked */
234   u32 lru_head_index;
235   /* index in global LRU list */
236   u32 lru_index;
237   f64 last_lru_update;
238
239   /* Last heard timer */
240   f64 last_heard;
241
242   /* Last HA refresh */
243   f64 ha_last_refreshed;
244
245   /* Counters */
246   u64 total_bytes;
247   u32 total_pkts;
248
249   /* External host address and port */
250   ip4_address_t ext_host_addr;
251   u16 ext_host_port;
252
253   /* External host address and port after translation */
254   ip4_address_t ext_host_nat_addr;
255   u16 ext_host_nat_port;
256
257   /* TCP session state */
258   u8 state;
259   u32 i2o_fin_seq;
260   u32 o2i_fin_seq;
261   u64 tcp_closed_timestamp;
262
263   /* user index */
264   u32 user_index;
265 }) nat44_ei_session_t;
266
267 typedef CLIB_PACKED (struct {
268   ip4_address_t addr;
269   u32 fib_index;
270   u32 sessions_per_user_list_head_index;
271   u32 nsessions;
272   u32 nstaticsessions;
273 }) nat44_ei_user_t;
274
275 typedef struct
276 {
277   /* Find-a-user => src address lookup */
278   clib_bihash_8_8_t user_hash;
279
280   /* User pool */
281   nat44_ei_user_t *users;
282
283   /* Session pool */
284   nat44_ei_session_t *sessions;
285
286   /* Pool of doubly-linked list elements */
287   dlist_elt_t *list_pool;
288
289   /* LRU session list - head is stale, tail is fresh */
290   dlist_elt_t *lru_pool;
291   u32 tcp_trans_lru_head_index;
292   u32 tcp_estab_lru_head_index;
293   u32 udp_lru_head_index;
294   u32 icmp_lru_head_index;
295   u32 unk_proto_lru_head_index;
296
297   /* NAT thread index */
298   u32 snat_thread_index;
299
300   /* real thread index */
301   u32 thread_index;
302
303 } nat44_ei_main_per_thread_data_t;
304
305 typedef struct
306 {
307   u32 cached_sw_if_index;
308   uword *cached_presence_by_ip4_address;
309 } nat44_ei_runtime_t;
310
311 typedef struct
312 {
313   u32 thread_index;
314   f64 now;
315 } nat44_ei_is_idle_session_ctx_t;
316
317 typedef struct nat44_ei_main_s
318 {
319   u32 translations;
320   u32 translation_buckets;
321   u32 user_buckets;
322
323   u8 out2in_dpo;
324   u8 forwarding_enabled;
325   u8 static_mapping_only;
326   u8 static_mapping_connection_tracking;
327
328   u16 mss_clamping;
329
330   /* Find a static mapping by local */
331   clib_bihash_8_8_t static_mapping_by_local;
332
333   /* Find a static mapping by external */
334   clib_bihash_8_8_t static_mapping_by_external;
335
336   /* Static mapping pool */
337   nat44_ei_static_mapping_t *static_mappings;
338
339   /* Interface pool */
340   nat44_ei_interface_t *interfaces;
341   nat44_ei_interface_t *output_feature_interfaces;
342
343   /* Is translation memory size calculated or user defined */
344   u8 translation_memory_size_set;
345
346   u32 max_users_per_thread;
347   u32 max_translations_per_thread;
348   u32 max_translations_per_user;
349
350   u32 inside_vrf_id;
351   u32 inside_fib_index;
352
353   u32 outside_vrf_id;
354   u32 outside_fib_index;
355
356   /* Thread settings */
357   u32 num_workers;
358   u32 first_worker_index;
359   u32 *workers;
360   u16 port_per_thread;
361
362   /* Main lookup tables */
363   clib_bihash_8_8_t out2in;
364   clib_bihash_8_8_t in2out;
365
366   /* Per thread data */
367   nat44_ei_main_per_thread_data_t *per_thread_data;
368
369   /* Vector of outside addresses */
370   nat44_ei_address_t *addresses;
371
372   nat44_ei_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
373   /* Address and port allocation type */
374   nat44_ei_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
375   /* Port set parameters (MAP-E) */
376   u8 psid_offset;
377   u8 psid_length;
378   u16 psid;
379   /* Port range parameters */
380   u16 start_port;
381   u16 end_port;
382
383   /* vector of fibs */
384   nat44_ei_fib_t *fibs;
385
386   /* vector of outside fibs */
387   nat44_ei_outside_fib_t *outside_fibs;
388
389   /* sw_if_indices whose intfc addresses should be auto-added */
390   u32 *auto_add_sw_if_indices;
391
392   /* vector of interface address static mappings to resolve. */
393   nat44_ei_static_map_resolve_t *to_resolve;
394
395   u32 in2out_node_index;
396   u32 out2in_node_index;
397   u32 in2out_output_node_index;
398
399   u32 fq_in2out_index;
400   u32 fq_in2out_output_index;
401   u32 fq_out2in_index;
402
403   /* Randomize port allocation order */
404   u32 random_seed;
405
406   nat_timeouts_t timeouts;
407
408   /* counters */
409   vlib_simple_counter_main_t total_users;
410   vlib_simple_counter_main_t total_sessions;
411   vlib_simple_counter_main_t user_limit_reached;
412
413 #define _(x) vlib_simple_counter_main_t x;
414   struct
415   {
416     struct
417     {
418       struct
419       {
420         foreach_nat_counter;
421       } in2out;
422
423       struct
424       {
425         foreach_nat_counter;
426       } out2in;
427
428     } fastpath;
429
430     struct
431     {
432       struct
433       {
434         foreach_nat_counter;
435       } in2out;
436
437       struct
438       {
439         foreach_nat_counter;
440       } out2in;
441     } slowpath;
442
443     vlib_simple_counter_main_t hairpinning;
444   } counters;
445 #undef _
446
447   /* API message ID base */
448   u16 msg_id_base;
449
450   /* log class */
451   vlib_log_class_t log_class;
452   /* logging level */
453   u8 log_level;
454
455   /* convenience */
456   api_main_t *api_main;
457   ip4_main_t *ip4_main;
458   ip_lookup_main_t *ip4_lookup_main;
459
460   fib_source_t fib_src_hi;
461   fib_source_t fib_src_low;
462
463   /* pat (port address translation)
464    * dynamic mapping enabled or conneciton tracking */
465   u8 pat;
466
467   /* number of worker handoff frame queue elements */
468   u32 frame_queue_nelts;
469
470   /* nat44 plugin enabled */
471   u8 enabled;
472
473   nat44_ei_config_t rconfig;
474
475   u32 in2out_hairpinning_finish_ip4_lookup_node_fq_index;
476   u32 in2out_hairpinning_finish_interface_output_node_fq_index;
477   u32 hairpinning_fq_index;
478   u32 hairpin_dst_fq_index;
479
480   vnet_main_t *vnet_main;
481 } nat44_ei_main_t;
482
483 extern nat44_ei_main_t nat44_ei_main;
484
485 int nat44_ei_plugin_enable (nat44_ei_config_t c);
486
487 int nat44_ei_plugin_disable ();
488
489 /**
490  * @brief Delete specific NAT44 EI user and his sessions
491  *
492  * @param addr         IPv4 address
493  * @param fib_index    FIB table index
494  */
495 int nat44_ei_user_del (ip4_address_t *addr, u32 fib_index);
496
497 /**
498  * @brief Delete session for static mapping
499  *
500  * @param addr         IPv4 address
501  * @param fib_index    FIB table index
502  */
503 void nat44_ei_static_mapping_del_sessions (
504   nat44_ei_main_t *nm, nat44_ei_main_per_thread_data_t *tnm,
505   nat44_ei_user_key_t u_key, int addr_only, ip4_address_t e_addr, u16 e_port);
506
507 u32 nat44_ei_get_in2out_worker_index (ip4_header_t *ip0, u32 rx_fib_index0,
508                                       u8 is_output);
509
510 u32 nat44_ei_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip0,
511                                       u32 rx_fib_index0, u8 is_output);
512
513 /**
514  * @brief Set address and port assignment algorithm to default/standard
515  */
516 void nat44_ei_set_alloc_default (void);
517
518 /**
519  * @brief Set address and port assignment algorithm for MAP-E CE
520  *
521  * @param psid        Port Set Identifier value
522  * @param psid_offset number of offset bits
523  * @param psid_length length of PSID
524  */
525 void nat44_ei_set_alloc_mape (u16 psid, u16 psid_offset, u16 psid_length);
526
527 /**
528  * @brief Set address and port assignment algorithm for port range
529  *
530  * @param start_port beginning of the port range
531  * @param end_port   end of the port range
532  */
533 void nat44_ei_set_alloc_range (u16 start_port, u16 end_port);
534
535 /**
536  * @brief Add/delete NAT44-EI static mapping
537  *
538  * @param l_addr       local IPv4 address
539  * @param e_addr       external IPv4 address
540  * @param l_port       local port number
541  * @param e_port       external port number
542  * @param proto        L4 protocol
543  * @param sw_if_index  use interface address as external IPv4 address
544  * @param vrf_id       local VRF ID
545  * @param addr_only    1 = 1:1NAT, 0 = 1:1NAPT
546  * @param identity_nat identity NAT
547  * @param tag opaque   string tag
548  * @param is_add       1 = add, 0 = delete
549  *
550  * @return 0 on success, non-zero value otherwise
551
552  */
553 int nat44_ei_add_del_static_mapping (ip4_address_t l_addr,
554                                      ip4_address_t e_addr, u16 l_port,
555                                      u16 e_port, nat_protocol_t proto,
556                                      u32 sw_if_index, u32 vrf_id, u8 addr_only,
557                                      u8 identity_nat, u8 *tag, u8 is_add);
558
559 /**
560  * @brief Delete NAT44-EI session
561  *
562  * @param addr   IPv4 address
563  * @param port   L4 port number
564  * @param proto  L4 protocol
565  * @param vrf_id VRF ID
566  * @param is_in  1 = inside network address and port pair, 0 = outside
567  *
568  * @return 0 on success, non-zero value otherwise
569  */
570 int nat44_ei_del_session (nat44_ei_main_t *nm, ip4_address_t *addr, u16 port,
571                           nat_protocol_t proto, u32 vrf_id, int is_in);
572
573 /**
574  * @brief Match NAT44-EI static mapping.
575  *
576  * @param key             address and port to match
577  * @param addr            external/local address of the matched mapping
578  * @param port            port of the matched mapping
579  * @param fib_index       fib index of the matched mapping
580  * @param by_external     if 0 match by local address otherwise match by
581  * external address
582  * @param is_addr_only    1 if matched mapping is address only
583  * @param is_identity_nat 1 if indentity mapping
584  *
585  * @returns 0 if match found otherwise 1.
586  */
587 int nat44_ei_static_mapping_match (ip4_address_t match_addr, u16 match_port,
588                                    u32 match_fib_index,
589                                    nat_protocol_t match_protocol,
590                                    ip4_address_t *mapping_addr,
591                                    u16 *mapping_port, u32 *mapping_fib_index,
592                                    u8 by_external, u8 *is_addr_only,
593                                    u8 *is_identity_nat);
594
595 /**
596  * @brief Clear all active NAT44-EI sessions.
597  */
598 void nat44_ei_sessions_clear ();
599
600 nat44_ei_user_t *nat44_ei_user_get_or_create (nat44_ei_main_t *nm,
601                                               ip4_address_t *addr,
602                                               u32 fib_index, u32 thread_index);
603
604 nat44_ei_session_t *nat44_ei_session_alloc_or_recycle (nat44_ei_main_t *nm,
605                                                        nat44_ei_user_t *u,
606                                                        u32 thread_index,
607                                                        f64 now);
608
609 void nat44_ei_free_session_data_v2 (nat44_ei_main_t *nm, nat44_ei_session_t *s,
610                                     u32 thread_index, u8 is_ha);
611
612 void nat44_ei_free_outside_address_and_port (nat44_ei_address_t *addresses,
613                                              u32 thread_index,
614                                              ip4_address_t *addr, u16 port,
615                                              nat_protocol_t protocol);
616
617 int nat44_ei_set_outside_address_and_port (nat44_ei_address_t *addresses,
618                                            u32 thread_index,
619                                            ip4_address_t addr, u16 port,
620                                            nat_protocol_t protocol);
621
622 int nat44_ei_del_address (nat44_ei_main_t *nm, ip4_address_t addr,
623                           u8 delete_sm);
624
625 void nat44_ei_free_session_data (nat44_ei_main_t *nm, nat44_ei_session_t *s,
626                                  u32 thread_index, u8 is_ha);
627
628 int nat44_ei_set_workers (uword *bitmap);
629
630 void nat44_ei_add_del_address_dpo (ip4_address_t addr, u8 is_add);
631
632 int nat44_ei_add_address (nat44_ei_main_t *nm, ip4_address_t *addr,
633                           u32 vrf_id);
634
635 void nat44_ei_delete_session (nat44_ei_main_t *nm, nat44_ei_session_t *ses,
636                               u32 thread_index);
637
638 int nat44_ei_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
639
640 int nat44_ei_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
641                                                int is_del);
642
643 int nat44_ei_add_interface_address (nat44_ei_main_t *nm, u32 sw_if_index,
644                                     int is_del);
645
646 /* Call back functions for clib_bihash_add_or_overwrite_stale */
647 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t *kv, void *arg);
648 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t *kv, void *arg);
649
650 int nat44_ei_hairpinning (vlib_main_t *vm, vlib_node_runtime_t *node,
651                           nat44_ei_main_t *nm, u32 thread_index,
652                           vlib_buffer_t *b0, ip4_header_t *ip0,
653                           udp_header_t *udp0, tcp_header_t *tcp0, u32 proto0,
654                           int do_trace, u32 *required_thread_index);
655
656 void nat44_ei_hairpinning_sm_unknown_proto (nat44_ei_main_t *nm,
657                                             vlib_buffer_t *b,
658                                             ip4_header_t *ip);
659
660 u32 nat44_ei_icmp_hairpinning (nat44_ei_main_t *nm, vlib_buffer_t *b0,
661                                u32 thread_index, ip4_header_t *ip0,
662                                icmp46_header_t *icmp0,
663                                u32 *required_thread_index);
664
665 int nat44_ei_set_frame_queue_nelts (u32 frame_queue_nelts);
666
667 #define nat44_ei_is_session_static(sp)                                        \
668   (sp->flags & NAT44_EI_SESSION_FLAG_STATIC_MAPPING)
669 #define nat44_ei_is_unk_proto_session(sp)                                     \
670   (sp->flags & NAT44_EI_SESSION_FLAG_UNKNOWN_PROTO)
671
672 #define nat44_ei_interface_is_inside(ip)                                      \
673   (ip->flags & NAT44_EI_INTERFACE_FLAG_IS_INSIDE)
674 #define nat44_ei_interface_is_outside(ip)                                     \
675   (ip->flags & NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE)
676
677 #define nat44_ei_is_addr_only_static_mapping(mp)                              \
678   (mp->flags & NAT44_EI_STATIC_MAPPING_FLAG_ADDR_ONLY)
679 #define nat44_ei_is_identity_static_mapping(mp)                               \
680   (mp->flags & NAT44_EI_STATIC_MAPPING_FLAG_IDENTITY_NAT)
681
682 /* logging */
683 #define nat44_ei_log_err(...)                                                 \
684   vlib_log (VLIB_LOG_LEVEL_ERR, nat44_ei_main.log_class, __VA_ARGS__)
685 #define nat44_ei_log_warn(...)                                                \
686   vlib_log (VLIB_LOG_LEVEL_WARNING, nat44_ei_main.log_class, __VA_ARGS__)
687 #define nat44_ei_log_notice(...)                                              \
688   vlib_log (VLIB_LOG_LEVEL_NOTICE, nat44_ei_main.log_class, __VA_ARGS__)
689 #define nat44_ei_log_info(...)                                                \
690   vlib_log (VLIB_LOG_LEVEL_INFO, nat44_ei_main.log_class, __VA_ARGS__)
691 #define nat44_ei_log_debug(...)                                               \
692   vlib_log (VLIB_LOG_LEVEL_DEBUG, nat44_ei_main.log_class, __VA_ARGS__)
693
694 #endif /* __included_nat44_ei_h__ */
695 /*
696  * fd.io coding-style-patch-verification: ON
697  *
698  * Local Variables:
699  * eval: (c-set-style "gnu")
700  * End:
701  */