ee355a4f5fcdef34321479e2fb9b551f121b0ed7
[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   /* values of various timeouts */
316   u32 udp_timeout;
317   u32 tcp_established_timeout;
318   u32 tcp_transitory_timeout;
319   u32 icmp_timeout;
320
321   /* API message ID base */
322   u16 msg_id_base;
323
324   /* convenience */
325   vlib_main_t * vlib_main;
326   vnet_main_t * vnet_main;
327   ip4_main_t * ip4_main;
328   ip_lookup_main_t * ip4_lookup_main;
329   api_main_t * api_main;
330 } snat_main_t;
331
332 extern snat_main_t snat_main;
333 extern vlib_node_registration_t snat_in2out_node;
334 extern vlib_node_registration_t snat_out2in_node;
335 extern vlib_node_registration_t snat_in2out_fast_node;
336 extern vlib_node_registration_t snat_out2in_fast_node;
337 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
338 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
339 extern vlib_node_registration_t snat_det_in2out_node;
340 extern vlib_node_registration_t snat_det_out2in_node;
341
342 void snat_free_outside_address_and_port (snat_main_t * sm, 
343                                          snat_session_key_t * k, 
344                                          u32 address_index);
345
346 int snat_alloc_outside_address_and_port (snat_main_t * sm, 
347                                          u32 fib_index,
348                                          snat_session_key_t * k,
349                                          u32 * address_indexp);
350
351 int snat_static_mapping_match (snat_main_t * sm,
352                                snat_session_key_t match,
353                                snat_session_key_t * mapping,
354                                u8 by_external,
355                                u8 *is_addr_only);
356
357 void snat_add_del_addr_to_fib (ip4_address_t * addr,
358                                u8 p_len,
359                                u32 sw_if_index,
360                                int is_add);
361
362 format_function_t format_snat_user;
363
364 typedef struct {
365   u32 cached_sw_if_index;
366   u32 cached_ip4_address;
367 } snat_runtime_t;
368
369 /** \brief Check if SNAT session is created from static mapping.
370     @param s SNAT session
371     @return 1 if SNAT session is created from static mapping otherwise 0
372 */
373 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
374
375 /* 
376  * Why is this here? Because we don't need to touch this layer to
377  * simply reply to an icmp. We need to change id to a unique
378  * value to NAT an echo request/reply.
379  */
380    
381 typedef struct {
382   u16 identifier;
383   u16 sequence;
384 } icmp_echo_header_t;
385
386 always_inline snat_protocol_t
387 ip_proto_to_snat_proto (u8 ip_proto)
388 {
389   snat_protocol_t snat_proto = ~0;
390
391   snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
392   snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
393   snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
394
395   return snat_proto;
396 }
397
398 always_inline u8
399 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
400 {
401   u8 ip_proto = ~0;
402
403   ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
404   ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
405   ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
406
407   return ip_proto;
408 }
409
410 typedef struct {
411   u16 src_port, dst_port;
412 } tcp_udp_header_t;
413
414 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
415                            u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
416                            snat_session_key_t *p_value,
417                            u8 *p_dont_translate, void *d, void *e);
418 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
419                            u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
420                            snat_session_key_t *p_value,
421                            u8 *p_dont_translate, void *d, void *e);
422 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
423                           u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
424                           snat_session_key_t *p_value,
425                           u8 *p_dont_translate, void *d, void *e);
426 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
427                            u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
428                            snat_session_key_t *p_value,
429                            u8 *p_dont_translate, void *d, void *e);
430 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
431                            u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
432                            snat_session_key_t *p_value,
433                            u8 *p_dont_translate, void *d, void *e);
434 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
435                           u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
436                           snat_session_key_t *p_value,
437                           u8 *p_dont_translate, void *d, void *e);
438
439 static_always_inline u8
440 icmp_is_error_message (icmp46_header_t * icmp)
441 {
442   switch(icmp->type)
443     {
444     case ICMP4_destination_unreachable:
445     case ICMP4_time_exceeded:
446     case ICMP4_parameter_problem:
447     case ICMP4_source_quench:
448     case ICMP4_redirect:
449     case ICMP4_alternate_host_address:
450       return 1;
451     }
452   return 0;
453 }
454
455 static_always_inline u8
456 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0,
457                   u32 ip4_addr)
458 {
459   snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
460   ip4_address_t * first_int_addr;
461
462   if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
463     {
464       first_int_addr =
465         ip4_interface_first_address (sm->ip4_main, sw_if_index0,
466                                      0 /* just want the address */);
467       rt->cached_sw_if_index = sw_if_index0;
468       if (first_int_addr)
469         rt->cached_ip4_address = first_int_addr->as_u32;
470       else
471         rt->cached_ip4_address = 0;
472     }
473
474   if (PREDICT_FALSE(ip4_addr == rt->cached_ip4_address))
475     return 1;
476   else
477     return 0;
478 }
479
480 #endif /* __included_snat_h__ */