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