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