NAT: Destination NAT44 with load-balancing (VPP-954)
[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
32
33 #define SNAT_UDP_TIMEOUT 300
34 #define SNAT_UDP_TIMEOUT_MIN 120
35 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
36 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
37 #define SNAT_TCP_INCOMING_SYN 6
38 #define SNAT_ICMP_TIMEOUT 60
39
40 #define SNAT_FLAG_HAIRPINNING (1 << 0)
41
42 /* Key */
43 typedef struct {
44   union
45   {
46     struct
47     {
48       ip4_address_t addr;
49       u16 port;
50       u16 protocol:3,
51         fib_index:13;
52     };
53     u64 as_u64;
54   };
55 } snat_session_key_t;
56
57 typedef struct {
58   union
59   {
60     struct
61     {
62       ip4_address_t l_addr;
63       ip4_address_t r_addr;
64       u32 fib_index;
65       u16 l_port;
66       u8 proto;
67       u8 rsvd;
68     };
69     u64 as_u64[2];
70   };
71 } nat_ed_ses_key_t;
72
73 typedef struct {
74   union
75   {
76     struct
77     {
78       ip4_address_t ext_host_addr;
79       u16 ext_host_port;
80       u16 out_port;
81     };
82     u64 as_u64;
83   };
84 } snat_det_out_key_t;
85
86 typedef struct {
87   union
88   {
89     struct
90     {
91       ip4_address_t addr;
92       u32 fib_index;
93     };
94     u64 as_u64;
95   };
96 } snat_user_key_t;
97
98 typedef struct {
99   union
100   {
101     struct
102     {
103       ip4_address_t addr;
104       u16 port;
105       u16 fib_index;
106     };
107     u64 as_u64;
108   };
109 } snat_worker_key_t;
110
111
112 #define foreach_snat_protocol \
113   _(UDP, 0, udp, "udp")       \
114   _(TCP, 1, tcp, "tcp")       \
115   _(ICMP, 2, icmp, "icmp")
116
117 typedef enum {
118 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
119   foreach_snat_protocol
120 #undef _
121 } snat_protocol_t;
122
123
124 #define foreach_snat_session_state          \
125   _(0, UNKNOWN, "unknown")                 \
126   _(1, UDP_ACTIVE, "udp-active")           \
127   _(2, TCP_SYN_SENT, "tcp-syn-sent")       \
128   _(3, TCP_ESTABLISHED, "tcp-established") \
129   _(4, TCP_FIN_WAIT, "tcp-fin-wait")       \
130   _(5, TCP_CLOSE_WAIT, "tcp-close-wait")   \
131   _(6, TCP_LAST_ACK, "tcp-last-ack")       \
132   _(7, ICMP_ACTIVE, "icmp-active")
133
134 typedef enum {
135 #define _(v, N, s) SNAT_SESSION_##N = v,
136   foreach_snat_session_state
137 #undef _
138 } snat_session_state_t;
139
140
141 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
142 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO  2
143 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
144
145 typedef CLIB_PACKED(struct {
146   snat_session_key_t out2in;    /* 0-15 */
147
148   snat_session_key_t in2out;    /* 16-31 */
149
150   u32 flags;                    /* 32-35 */
151
152   /* per-user translations */
153   u32 per_user_index;           /* 36-39 */
154
155   u32 per_user_list_head_index; /* 40-43 */
156
157   /* Last heard timer */
158   f64 last_heard;               /* 44-51 */
159
160   u64 total_bytes;              /* 52-59 */
161
162   u32 total_pkts;               /* 60-63 */
163
164   /* Outside address */
165   u32 outside_address_index;    /* 64-67 */
166
167   /* External host address */
168   ip4_address_t ext_host_addr;  /* 68-71 */
169
170 }) snat_session_t;
171
172
173 typedef struct {
174   ip4_address_t addr;
175   u32 fib_index;
176   u32 sessions_per_user_list_head_index;
177   u32 nsessions;
178   u32 nstaticsessions;
179 } snat_user_t;
180
181 typedef struct {
182   ip4_address_t addr;
183   u32 fib_index;
184 #define _(N, i, n, s) \
185   u32 busy_##n##_ports; \
186   uword * busy_##n##_port_bitmap;
187   foreach_snat_protocol
188 #undef _
189 } snat_address_t;
190
191 typedef struct {
192   u16 in_port;
193   snat_det_out_key_t out;
194   u8 state;
195   u32 expire;
196 } snat_det_session_t;
197
198 typedef struct {
199   ip4_address_t in_addr;
200   u8 in_plen;
201   ip4_address_t out_addr;
202   u8 out_plen;
203   u32 sharing_ratio;
204   u16 ports_per_host;
205   u32 ses_num;
206   /* vector of sessions */
207   snat_det_session_t * sessions;
208 } snat_det_map_t;
209
210 typedef struct {
211   ip4_address_t addr;
212   u16 port;
213   u8 probability;
214   u8 prefix;
215 } nat44_lb_addr_port_t;
216
217 typedef struct {
218   ip4_address_t local_addr;
219   ip4_address_t external_addr;
220   u16 local_port;
221   u16 external_port;
222   u8 addr_only;
223   u32 vrf_id;
224   u32 fib_index;
225   snat_protocol_t proto;
226   nat44_lb_addr_port_t *locals;
227 } snat_static_mapping_t;
228
229 typedef struct {
230   u32 sw_if_index;
231   u8 is_inside;
232 } snat_interface_t;
233
234 typedef struct {
235   ip4_address_t l_addr;
236   u16 l_port;
237   u16 e_port;
238   u32 sw_if_index;
239   u32 vrf_id;
240   snat_protocol_t proto;
241   int addr_only;
242   int is_add;
243 } snat_static_map_resolve_t;
244
245 typedef struct {
246   /* User pool */
247   snat_user_t * users;
248
249   /* Session pool */
250   snat_session_t * sessions;
251
252   /* Pool of doubly-linked list elements */
253   dlist_elt_t * list_pool;
254
255   u32 snat_thread_index;
256 } snat_main_per_thread_data_t;
257
258 struct snat_main_s;
259
260 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
261                                         vlib_node_runtime_t *node,
262                                         u32 thread_index,
263                                         vlib_buffer_t *b0,
264                                         u8 *p_proto,
265                                         snat_session_key_t *p_value,
266                                         u8 *p_dont_translate,
267                                         void *d,
268                                         void *e);
269
270 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
271
272 typedef struct snat_main_s {
273   /* Main lookup tables */
274   clib_bihash_8_8_t out2in;
275   clib_bihash_8_8_t in2out;
276
277   /* Endpoint address dependent sessions lookup tables */
278   clib_bihash_16_8_t out2in_ed;
279   clib_bihash_16_8_t in2out_ed;
280
281   /* Find-a-user => src address lookup */
282   clib_bihash_8_8_t user_hash;
283
284   /* Non-translated packets worker lookup => src address + VRF */
285   clib_bihash_8_8_t worker_by_in;
286
287   /* Translated packets worker lookup => IP address + port number */
288   clib_bihash_8_8_t worker_by_out;
289
290   snat_icmp_match_function_t * icmp_match_in2out_cb;
291   snat_icmp_match_function_t * icmp_match_out2in_cb;
292
293   u32 num_workers;
294   u32 first_worker_index;
295   u32 next_worker;
296   u32 * workers;
297   snat_get_worker_function_t * worker_in2out_cb;
298   snat_get_worker_function_t * worker_out2in_cb;
299   u16 port_per_thread;
300   u32 num_snat_thread;
301
302   /* Per thread data */
303   snat_main_per_thread_data_t * per_thread_data;
304
305   /* Find a static mapping by local */
306   clib_bihash_8_8_t static_mapping_by_local;
307
308   /* Find a static mapping by external */
309   clib_bihash_8_8_t static_mapping_by_external;
310
311   /* Static mapping pool */
312   snat_static_mapping_t * static_mappings;
313
314   /* Interface pool */
315   snat_interface_t * interfaces;
316   snat_interface_t * output_feature_interfaces;
317
318   /* Vector of outside addresses */
319   snat_address_t * addresses;
320
321   /* sw_if_indices whose intfc addresses should be auto-added */
322   u32 * auto_add_sw_if_indices;
323
324   /* vector of interface address static mappings to resolve. */
325   snat_static_map_resolve_t *to_resolve;
326
327   /* Randomize port allocation order */
328   u32 random_seed;
329
330   /* Worker handoff index */
331   u32 fq_in2out_index;
332   u32 fq_in2out_output_index;
333   u32 fq_out2in_index;
334
335   /* in2out and out2in node index */
336   u32 in2out_node_index;
337   u32 in2out_output_node_index;
338   u32 out2in_node_index;
339
340   /* Deterministic NAT */
341   snat_det_map_t * det_maps;
342
343   /* Config parameters */
344   u8 static_mapping_only;
345   u8 static_mapping_connection_tracking;
346   u8 deterministic;
347   u32 translation_buckets;
348   u32 translation_memory_size;
349   u32 user_buckets;
350   u32 user_memory_size;
351   u32 max_translations_per_user;
352   u32 outside_vrf_id;
353   u32 outside_fib_index;
354   u32 inside_vrf_id;
355   u32 inside_fib_index;
356
357   /* tenant VRF aware address pool activation flag */
358   u8 vrf_mode;
359
360   /* values of various timeouts */
361   u32 udp_timeout;
362   u32 tcp_established_timeout;
363   u32 tcp_transitory_timeout;
364   u32 icmp_timeout;
365
366   /* API message ID base */
367   u16 msg_id_base;
368
369   /* convenience */
370   vlib_main_t * vlib_main;
371   vnet_main_t * vnet_main;
372   ip4_main_t * ip4_main;
373   ip_lookup_main_t * ip4_lookup_main;
374   api_main_t * api_main;
375 } snat_main_t;
376
377 extern snat_main_t snat_main;
378 extern vlib_node_registration_t snat_in2out_node;
379 extern vlib_node_registration_t snat_in2out_output_node;
380 extern vlib_node_registration_t snat_out2in_node;
381 extern vlib_node_registration_t snat_in2out_fast_node;
382 extern vlib_node_registration_t snat_out2in_fast_node;
383 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
384 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
385 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
386 extern vlib_node_registration_t snat_det_in2out_node;
387 extern vlib_node_registration_t snat_det_out2in_node;
388 extern vlib_node_registration_t snat_hairpin_dst_node;
389 extern vlib_node_registration_t snat_hairpin_src_node;
390
391 void snat_free_outside_address_and_port (snat_main_t * sm,
392                                          snat_session_key_t * k,
393                                          u32 address_index);
394
395 int snat_alloc_outside_address_and_port (snat_main_t * sm,
396                                          u32 fib_index,
397                                          u32 thread_index,
398                                          snat_session_key_t * k,
399                                          u32 * address_indexp);
400
401 int snat_static_mapping_match (snat_main_t * sm,
402                                snat_session_key_t match,
403                                snat_session_key_t * mapping,
404                                u8 by_external,
405                                u8 *is_addr_only);
406
407 void snat_add_del_addr_to_fib (ip4_address_t * addr,
408                                u8 p_len,
409                                u32 sw_if_index,
410                                int is_add);
411
412 format_function_t format_snat_user;
413
414 typedef struct {
415   u32 cached_sw_if_index;
416   u32 cached_ip4_address;
417 } snat_runtime_t;
418
419 /** \brief Check if SNAT session is created from static mapping.
420     @param s SNAT session
421     @return 1 if SNAT session is created from static mapping otherwise 0
422 */
423 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
424
425 /** \brief Check if SNAT session for unknown protocol.
426     @param s SNAT session
427     @return 1 if SNAT session for unknown protocol otherwise 0
428 */
429 #define snat_is_unk_proto_session(s) s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO
430
431 /*
432  * Why is this here? Because we don't need to touch this layer to
433  * simply reply to an icmp. We need to change id to a unique
434  * value to NAT an echo request/reply.
435  */
436
437 typedef struct {
438   u16 identifier;
439   u16 sequence;
440 } icmp_echo_header_t;
441
442 always_inline u32
443 ip_proto_to_snat_proto (u8 ip_proto)
444 {
445   u32 snat_proto = ~0;
446
447   snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
448   snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
449   snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
450   snat_proto = (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
451
452   return snat_proto;
453 }
454
455 always_inline u8
456 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
457 {
458   u8 ip_proto = ~0;
459
460   ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
461   ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
462   ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
463
464   return ip_proto;
465 }
466
467 typedef struct {
468   u16 src_port, dst_port;
469 } tcp_udp_header_t;
470
471 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
472                            u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
473                            snat_session_key_t *p_value,
474                            u8 *p_dont_translate, void *d, void *e);
475 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
476                            u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
477                            snat_session_key_t *p_value,
478                            u8 *p_dont_translate, void *d, void *e);
479 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
480                           u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
481                           snat_session_key_t *p_value,
482                           u8 *p_dont_translate, void *d, void *e);
483 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
484                            u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
485                            snat_session_key_t *p_value,
486                            u8 *p_dont_translate, void *d, void *e);
487 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
488                            u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
489                            snat_session_key_t *p_value,
490                            u8 *p_dont_translate, void *d, void *e);
491 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
492                           u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
493                           snat_session_key_t *p_value,
494                           u8 *p_dont_translate, void *d, void *e);
495 void increment_v4_address(ip4_address_t * a);
496 void snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id);
497 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm);
498 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
499                             u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
500                             u32 sw_if_index, snat_protocol_t proto, int is_add);
501 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
502 int snat_set_workers (uword * bitmap);
503 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
504 int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside,
505                                           int is_del);
506 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del);
507 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
508 u8 * format_snat_protocol(u8 * s, va_list * args);
509 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
510                                      snat_protocol_t proto, u32 vrf_id,
511                                      nat44_lb_addr_port_t *locals, u8 is_add);
512
513 static_always_inline u8
514 icmp_is_error_message (icmp46_header_t * icmp)
515 {
516   switch(icmp->type)
517     {
518     case ICMP4_destination_unreachable:
519     case ICMP4_time_exceeded:
520     case ICMP4_parameter_problem:
521     case ICMP4_source_quench:
522     case ICMP4_redirect:
523     case ICMP4_alternate_host_address:
524       return 1;
525     }
526   return 0;
527 }
528
529 static_always_inline u8
530 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0,
531                   u32 ip4_addr)
532 {
533   snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
534   ip4_address_t * first_int_addr;
535
536   if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
537     {
538       first_int_addr =
539         ip4_interface_first_address (sm->ip4_main, sw_if_index0,
540                                      0 /* just want the address */);
541       rt->cached_sw_if_index = sw_if_index0;
542       if (first_int_addr)
543         rt->cached_ip4_address = first_int_addr->as_u32;
544       else
545         rt->cached_ip4_address = 0;
546     }
547
548   if (PREDICT_FALSE(ip4_addr == rt->cached_ip4_address))
549     return 1;
550   else
551     return 0;
552 }
553
554 #endif /* __included_nat_h__ */