NAT44: code cleanup and refactor (VPP-1285)
[vpp.git] / src / plugins / nat / nat.h
1
2 /*
3  * nat.h - NAT plugin definitions
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifndef __included_nat_h__
19 #define __included_nat_h__
20
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/ip/icmp46_packet.h>
25 #include <vnet/api_errno.h>
26 #include <vppinfra/bihash_8_8.h>
27 #include <vppinfra/bihash_16_8.h>
28 #include <vppinfra/dlist.h>
29 #include <vppinfra/error.h>
30 #include <vlibapi/api.h>
31 #include <vlib/log.h>
32
33
34 #define SNAT_UDP_TIMEOUT 300
35 #define SNAT_UDP_TIMEOUT_MIN 120
36 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
37 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
38 #define SNAT_TCP_INCOMING_SYN 6
39 #define SNAT_ICMP_TIMEOUT 60
40
41 #define NAT_FQ_NELTS 64
42
43 #define SNAT_FLAG_HAIRPINNING (1 << 0)
44
45 /* Key */
46 typedef struct {
47   union
48   {
49     struct
50     {
51       ip4_address_t addr;
52       u16 port;
53       u16 protocol:3,
54         fib_index:13;
55     };
56     u64 as_u64;
57   };
58 } snat_session_key_t;
59
60 typedef struct {
61   union
62   {
63     struct
64     {
65       ip4_address_t l_addr;
66       ip4_address_t r_addr;
67       u32 proto:8,
68           fib_index:24;
69       u16 l_port;
70       u16 r_port;
71     };
72     u64 as_u64[2];
73   };
74 } nat_ed_ses_key_t;
75
76 typedef struct {
77   union
78   {
79     struct
80     {
81       ip4_address_t ext_host_addr;
82       u16 ext_host_port;
83       u16 out_port;
84     };
85     u64 as_u64;
86   };
87 } snat_det_out_key_t;
88
89 typedef struct {
90   union
91   {
92     struct
93     {
94       ip4_address_t addr;
95       u32 fib_index;
96     };
97     u64 as_u64;
98   };
99 } snat_user_key_t;
100
101
102 #define foreach_snat_protocol \
103   _(UDP, 0, udp, "udp")       \
104   _(TCP, 1, tcp, "tcp")       \
105   _(ICMP, 2, icmp, "icmp")
106
107 typedef enum {
108 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
109   foreach_snat_protocol
110 #undef _
111 } snat_protocol_t;
112
113
114 #define foreach_snat_session_state          \
115   _(0, UNKNOWN, "unknown")                 \
116   _(1, UDP_ACTIVE, "udp-active")           \
117   _(2, TCP_SYN_SENT, "tcp-syn-sent")       \
118   _(3, TCP_ESTABLISHED, "tcp-established") \
119   _(4, TCP_FIN_WAIT, "tcp-fin-wait")       \
120   _(5, TCP_CLOSE_WAIT, "tcp-close-wait")   \
121   _(6, TCP_CLOSING, "tcp-closing")         \
122   _(7, TCP_LAST_ACK, "tcp-last-ack")       \
123   _(8, TCP_CLOSED, "tcp-closed")           \
124   _(9, ICMP_ACTIVE, "icmp-active")
125
126 typedef enum {
127 #define _(v, N, s) SNAT_SESSION_##N = v,
128   foreach_snat_session_state
129 #undef _
130 } snat_session_state_t;
131
132 #define NAT44_SES_I2O_FIN 1
133 #define NAT44_SES_O2I_FIN 2
134 #define NAT44_SES_I2O_FIN_ACK 4
135 #define NAT44_SES_O2I_FIN_ACK 8
136
137 #define nat44_is_ses_closed(s) s->state == 0xf
138
139 #define SNAT_SESSION_FLAG_STATIC_MAPPING       1
140 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO        2
141 #define SNAT_SESSION_FLAG_LOAD_BALANCING       4
142 #define SNAT_SESSION_FLAG_TWICE_NAT            8
143 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT   16
144 #define SNAT_SESSION_FLAG_FWD_BYPASS           32
145
146 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
147 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
148
149 typedef CLIB_PACKED(struct {
150   snat_session_key_t out2in;    /* 0-15 */
151
152   snat_session_key_t in2out;    /* 16-31 */
153
154   u32 flags;                    /* 32-35 */
155
156   /* per-user translations */
157   u32 per_user_index;           /* 36-39 */
158
159   u32 per_user_list_head_index; /* 40-43 */
160
161   /* Last heard timer */
162   f64 last_heard;               /* 44-51 */
163
164   u64 total_bytes;              /* 52-59 */
165
166   u32 total_pkts;               /* 60-63 */
167
168   /* Outside address */
169   u32 outside_address_index;    /* 64-67 */
170
171   /* External host address and port */
172   ip4_address_t ext_host_addr;  /* 68-71 */
173   u16 ext_host_port;            /* 72-73 */
174
175   /* External hos address and port after translation */
176   ip4_address_t ext_host_nat_addr; /* 74-77 */
177   u16 ext_host_nat_port;           /* 78-79 */
178
179   /* TCP session state */
180   u8 state;
181   u32 i2o_fin_seq;
182   u32 o2i_fin_seq;
183 }) snat_session_t;
184
185
186 typedef struct {
187   ip4_address_t addr;
188   u32 fib_index;
189   u32 sessions_per_user_list_head_index;
190   u32 nsessions;
191   u32 nstaticsessions;
192 } snat_user_t;
193
194 typedef struct {
195   ip4_address_t addr;
196   u32 fib_index;
197 #define _(N, i, n, s) \
198   u16 busy_##n##_ports; \
199   u16 * busy_##n##_ports_per_thread; \
200   uword * busy_##n##_port_bitmap;
201   foreach_snat_protocol
202 #undef _
203 } snat_address_t;
204
205 typedef struct {
206   u16 in_port;
207   snat_det_out_key_t out;
208   u8 state;
209   u32 expire;
210 } snat_det_session_t;
211
212 typedef struct {
213   ip4_address_t in_addr;
214   u8 in_plen;
215   ip4_address_t out_addr;
216   u8 out_plen;
217   u32 sharing_ratio;
218   u16 ports_per_host;
219   u32 ses_num;
220   /* vector of sessions */
221   snat_det_session_t * sessions;
222 } snat_det_map_t;
223
224 typedef struct {
225   ip4_address_t addr;
226   u16 port;
227   u8 probability;
228   u8 prefix;
229 } nat44_lb_addr_port_t;
230
231 typedef enum {
232   TWICE_NAT_DISABLED,
233   TWICE_NAT,
234   TWICE_NAT_SELF,
235 } twice_nat_type_t;
236
237 typedef struct {
238   ip4_address_t local_addr;
239   ip4_address_t external_addr;
240   u16 local_port;
241   u16 external_port;
242   u8 addr_only;
243   twice_nat_type_t twice_nat;
244   u8 out2in_only;
245   u32 vrf_id;
246   u32 fib_index;
247   snat_protocol_t proto;
248   u32 worker_index;
249   u8 *tag;
250   nat44_lb_addr_port_t *locals;
251 } snat_static_mapping_t;
252
253 typedef struct {
254   u32 sw_if_index;
255   u8 flags;
256 } snat_interface_t;
257
258 typedef struct {
259   ip4_address_t l_addr;
260   u16 l_port;
261   u16 e_port;
262   u32 sw_if_index;
263   u32 vrf_id;
264   snat_protocol_t proto;
265   int addr_only;
266   int twice_nat;
267   int is_add;
268   u8 *tag;
269 } snat_static_map_resolve_t;
270
271 typedef struct {
272   /* Main lookup tables */
273   clib_bihash_8_8_t out2in;
274   clib_bihash_8_8_t in2out;
275
276   /* Find-a-user => src address lookup */
277   clib_bihash_8_8_t user_hash;
278
279   /* User pool */
280   snat_user_t * users;
281
282   /* Session pool */
283   snat_session_t * sessions;
284
285   /* Pool of doubly-linked list elements */
286   dlist_elt_t * list_pool;
287
288   u32 snat_thread_index;
289 } snat_main_per_thread_data_t;
290
291 struct snat_main_s;
292
293 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
294                                         vlib_node_runtime_t *node,
295                                         u32 thread_index,
296                                         vlib_buffer_t *b0,
297                                         ip4_header_t *ip0,
298                                         u8 *p_proto,
299                                         snat_session_key_t *p_value,
300                                         u8 *p_dont_translate,
301                                         void *d,
302                                         void *e);
303
304 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
305
306 typedef int nat_alloc_out_addr_and_port_function_t (snat_address_t * addresses,
307                                                     u32 fib_index,
308                                                     u32 thread_index,
309                                                     snat_session_key_t * k,
310                                                     u32 * address_indexp,
311                                                     u16 port_per_thread,
312                                                     u32 snat_thread_index);
313
314 typedef struct snat_main_s {
315   /* Endpoint address dependent sessions lookup tables */
316   clib_bihash_16_8_t out2in_ed;
317   clib_bihash_16_8_t in2out_ed;
318
319   snat_icmp_match_function_t * icmp_match_in2out_cb;
320   snat_icmp_match_function_t * icmp_match_out2in_cb;
321
322   u32 num_workers;
323   u32 first_worker_index;
324   u32 next_worker;
325   u32 * workers;
326   snat_get_worker_function_t * worker_in2out_cb;
327   snat_get_worker_function_t * worker_out2in_cb;
328   u16 port_per_thread;
329   u32 num_snat_thread;
330
331   /* Per thread data */
332   snat_main_per_thread_data_t * per_thread_data;
333
334   /* Find a static mapping by local */
335   clib_bihash_8_8_t static_mapping_by_local;
336
337   /* Find a static mapping by external */
338   clib_bihash_8_8_t static_mapping_by_external;
339
340   /* Static mapping pool */
341   snat_static_mapping_t * static_mappings;
342
343   /* Interface pool */
344   snat_interface_t * interfaces;
345   snat_interface_t * output_feature_interfaces;
346
347   /* Vector of outside addresses */
348   snat_address_t * addresses;
349   nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
350   u8 psid_offset;
351   u8 psid_length;
352   u16 psid;
353
354   /* Vector of twice NAT addresses for extenal hosts */
355   snat_address_t * twice_nat_addresses;
356
357   /* sw_if_indices whose intfc addresses should be auto-added */
358   u32 * auto_add_sw_if_indices;
359   u32 * auto_add_sw_if_indices_twice_nat;
360
361   /* vector of interface address static mappings to resolve. */
362   snat_static_map_resolve_t *to_resolve;
363
364   /* Randomize port allocation order */
365   u32 random_seed;
366
367   /* Worker handoff index */
368   u32 fq_in2out_index;
369   u32 fq_in2out_output_index;
370   u32 fq_out2in_index;
371
372   /* in2out and out2in node index */
373   u32 in2out_node_index;
374   u32 in2out_output_node_index;
375   u32 out2in_node_index;
376   u32 error_node_index;
377
378   /* Deterministic NAT */
379   snat_det_map_t * det_maps;
380
381   /* If forwarding is enabled */
382   u8 forwarding_enabled;
383
384   /* Config parameters */
385   u8 static_mapping_only;
386   u8 static_mapping_connection_tracking;
387   u8 deterministic;
388   u8 out2in_dpo;
389   u32 translation_buckets;
390   u32 translation_memory_size;
391   u32 max_translations;
392   u32 user_buckets;
393   u32 user_memory_size;
394   u32 max_translations_per_user;
395   u32 outside_vrf_id;
396   u32 outside_fib_index;
397   u32 inside_vrf_id;
398   u32 inside_fib_index;
399
400   /* values of various timeouts */
401   u32 udp_timeout;
402   u32 tcp_established_timeout;
403   u32 tcp_transitory_timeout;
404   u32 icmp_timeout;
405
406   /* API message ID base */
407   u16 msg_id_base;
408
409   /* log class */
410   vlib_log_class_t log_class;
411
412   /* convenience */
413   vlib_main_t * vlib_main;
414   vnet_main_t * vnet_main;
415   ip4_main_t * ip4_main;
416   ip_lookup_main_t * ip4_lookup_main;
417   api_main_t * api_main;
418 } snat_main_t;
419
420 extern snat_main_t snat_main;
421 extern vlib_node_registration_t snat_in2out_node;
422 extern vlib_node_registration_t snat_in2out_output_node;
423 extern vlib_node_registration_t snat_out2in_node;
424 extern vlib_node_registration_t snat_in2out_fast_node;
425 extern vlib_node_registration_t snat_out2in_fast_node;
426 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
427 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
428 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
429 extern vlib_node_registration_t snat_det_in2out_node;
430 extern vlib_node_registration_t snat_det_out2in_node;
431 extern vlib_node_registration_t snat_hairpin_dst_node;
432 extern vlib_node_registration_t snat_hairpin_src_node;
433
434 void snat_free_outside_address_and_port (snat_address_t * addresses,
435                                          u32 thread_index,
436                                          snat_session_key_t * k,
437                                          u32 address_index);
438
439 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
440                                          u32 fib_index,
441                                          u32 thread_index,
442                                          snat_session_key_t * k,
443                                          u32 * address_indexp,
444                                          u16 port_per_thread,
445                                          u32 snat_thread_index);
446
447 int snat_static_mapping_match (snat_main_t * sm,
448                                snat_session_key_t match,
449                                snat_session_key_t * mapping,
450                                u8 by_external,
451                                u8 *is_addr_only,
452                                twice_nat_type_t *twice_nat,
453                                u8 *lb);
454
455 void snat_add_del_addr_to_fib (ip4_address_t * addr,
456                                u8 p_len,
457                                u32 sw_if_index,
458                                int is_add);
459
460 format_function_t format_snat_user;
461 format_function_t format_snat_static_mapping;
462 format_function_t format_snat_static_map_to_resolve;
463 format_function_t format_snat_session;
464 format_function_t format_det_map_ses;
465
466 typedef struct {
467   u32 cached_sw_if_index;
468   u32 cached_ip4_address;
469 } snat_runtime_t;
470
471 /** \brief Check if SNAT session is created from static mapping.
472     @param s SNAT session
473     @return 1 if SNAT session is created from static mapping otherwise 0
474 */
475 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
476
477 /** \brief Check if SNAT session for unknown protocol.
478     @param s SNAT session
479     @return 1 if SNAT session for unknown protocol otherwise 0
480 */
481 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
482
483 /** \brief Check if NAT session is twice NAT.
484     @param s NAT session
485     @return 1 if NAT session is twice NAT
486 */
487 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
488
489 /** \brief Check if NAT session is load-balancing.
490     @param s NAT session
491     @return 1 if NAT session is load-balancing
492 */
493 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
494
495 /** \brief Check if NAT session is forwarding bypass.
496     @param s NAT session
497     @return 1 if NAT session is load-balancing
498 */
499 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
500
501 /** \brief Check if NAT session is endpoint dependent.
502     @param s NAT session
503     @return 1 if NAT session is endpoint dependent
504 */
505 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
506
507 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
508 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
509
510 #define nat_log_err(...) \
511   vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
512 #define nat_log_warn(...) \
513   vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
514 #define nat_log_notice(...) \
515   vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
516 #define nat_log_info(...) \
517   vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
518 #define nat_log_debug(...)\
519   vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
520
521 /*
522  * Why is this here? Because we don't need to touch this layer to
523  * simply reply to an icmp. We need to change id to a unique
524  * value to NAT an echo request/reply.
525  */
526
527 typedef struct {
528   u16 identifier;
529   u16 sequence;
530 } icmp_echo_header_t;
531
532 typedef struct {
533   u16 src_port, dst_port;
534 } tcp_udp_header_t;
535
536 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
537                            u32 thread_index, vlib_buffer_t *b0,
538                            ip4_header_t *ip0, u8 *p_proto,
539                            snat_session_key_t *p_value,
540                            u8 *p_dont_translate, void *d, void *e);
541 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
542                            u32 thread_index, vlib_buffer_t *b0,
543                            ip4_header_t *ip0, u8 *p_proto,
544                            snat_session_key_t *p_value,
545                            u8 *p_dont_translate, void *d, void *e);
546 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
547                           u32 thread_index, vlib_buffer_t *b0,
548                           ip4_header_t *ip0, u8 *p_proto,
549                           snat_session_key_t *p_value,
550                           u8 *p_dont_translate, void *d, void *e);
551 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
552                            u32 thread_index, vlib_buffer_t *b0,
553                            ip4_header_t *ip0, u8 *p_proto,
554                            snat_session_key_t *p_value,
555                            u8 *p_dont_translate, void *d, void *e);
556 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
557                            u32 thread_index, vlib_buffer_t *b0,
558                            ip4_header_t *ip0, u8 *p_proto,
559                            snat_session_key_t *p_value,
560                            u8 *p_dont_translate, void *d, void *e);
561 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
562                           u32 thread_index, vlib_buffer_t *b0,
563                           ip4_header_t *ip0, u8 *p_proto,
564                           snat_session_key_t *p_value,
565                           u8 *p_dont_translate, void *d, void *e);
566 void increment_v4_address(ip4_address_t * a);
567 void snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
568                       u8 twice_nat);
569 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
570                      u8 twice_nat);
571 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
572 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
573                             u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
574                             u32 sw_if_index, snat_protocol_t proto, int is_add,
575                             twice_nat_type_t twice_nat, u8 out2in_only,
576                             u8 *tag);
577 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
578 int snat_set_workers (uword * bitmap);
579 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
580 int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside,
581                                           int is_del);
582 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del,
583                                u8 twice_nat);
584 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
585 u8 * format_snat_protocol(u8 * s, va_list * args);
586 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
587                                      snat_protocol_t proto, u32 vrf_id,
588                                      nat44_lb_addr_port_t *locals, u8 is_add,
589                                      twice_nat_type_t twice_nat, u8 out2in_only,
590                                      u8 *tag);
591 int nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
592                        snat_protocol_t proto, u32 vrf_id, int is_in);
593 int nat44_del_ed_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
594                           ip4_address_t *eh_addr, u16 eh_port, u8 proto,
595                           u32 vrf_id, int is_in);
596 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
597                             u32 thread_index);
598 snat_user_t * nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr,
599                                       u32 fib_index, u32 thread_index);
600 snat_session_t * nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u,
601                                                u32 thread_index);
602 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
603                                        u16 psid_length);
604 void nat_set_alloc_addr_and_port_default (void);
605
606 #endif /* __included_snat_h__ */