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