Add setting of tenant VRF id for SNAT addresses (VPP-641)
[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 /* Key */
32 typedef struct {
33   union 
34   {
35     struct 
36     {
37       ip4_address_t addr;
38       u16 port;
39       u16 protocol:3,
40         fib_index:13;
41     };
42     u64 as_u64;
43   };
44 } snat_session_key_t;
45
46 typedef struct {
47   union
48   {
49     struct
50     {
51       ip4_address_t addr;
52       u32 fib_index;
53     };
54     u64 as_u64;
55   };
56 } snat_user_key_t;
57
58 typedef struct {
59   union
60   {
61     struct
62     {
63       ip4_address_t addr;
64       u16 port;
65       u16 fib_index;
66     };
67     u64 as_u64;
68   };
69 } snat_worker_key_t;
70
71
72 #define foreach_snat_protocol \
73   _(UDP, 0, udp, "udp")       \
74   _(TCP, 1, tcp, "tcp")       \
75   _(ICMP, 2, icmp, "icmp")
76
77 typedef enum {
78 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
79   foreach_snat_protocol
80 #undef _
81 } snat_protocol_t;
82
83
84 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
85
86 typedef CLIB_PACKED(struct {
87   snat_session_key_t out2in;    /* 0-15 */
88
89   snat_session_key_t in2out;    /* 16-31 */
90
91   u32 flags;                    /* 32-35 */
92
93   /* per-user translations */
94   u32 per_user_index;           /* 36-39 */
95
96   u32 per_user_list_head_index; /* 40-43 */
97
98   /* Last heard timer */
99   f64 last_heard;               /* 44-51 */
100
101   u64 total_bytes;              /* 52-59 */
102   
103   u32 total_pkts;               /* 60-63 */
104
105   /* Outside address */
106   u32 outside_address_index;    /* 64-67 */
107
108 }) snat_session_t;
109
110
111 typedef struct {
112   ip4_address_t addr;
113   u32 fib_index;
114   u32 sessions_per_user_list_head_index;
115   u32 nsessions;
116   u32 nstaticsessions;
117 } snat_user_t;
118
119 typedef struct {
120   ip4_address_t addr;
121   u32 fib_index;
122 #define _(N, i, n, s) \
123   u32 busy_##n##_ports; \
124   uword * busy_##n##_port_bitmap;
125   foreach_snat_protocol
126 #undef _
127 } snat_address_t;
128
129 typedef struct {
130   ip4_address_t local_addr;
131   ip4_address_t external_addr;
132   u16 local_port;
133   u16 external_port;
134   u8 addr_only;
135   u32 vrf_id;
136   u32 fib_index;
137   snat_protocol_t proto;
138 } snat_static_mapping_t;
139
140 typedef struct {
141   u32 sw_if_index;
142   u8 is_inside;
143 } snat_interface_t;
144
145 typedef struct {
146   ip4_address_t l_addr;
147   u16 l_port;
148   u16 e_port;
149   u32 sw_if_index;
150   u32 vrf_id;
151   snat_protocol_t proto;
152   int addr_only;
153   int is_add;
154 } snat_static_map_resolve_t;
155
156 typedef struct {
157   /* User pool */
158   snat_user_t * users;
159
160   /* Session pool */
161   snat_session_t * sessions;
162
163   /* Pool of doubly-linked list elements */
164   dlist_elt_t * list_pool;
165 } snat_main_per_thread_data_t;
166
167 typedef struct {
168   /* Main lookup tables */
169   clib_bihash_8_8_t out2in;
170   clib_bihash_8_8_t in2out;
171
172   /* Find-a-user => src address lookup */
173   clib_bihash_8_8_t user_hash;
174
175   /* Non-translated packets worker lookup => src address + VRF */
176   clib_bihash_8_8_t worker_by_in;
177
178   /* Translated packets worker lookup => IP address + port number */
179   clib_bihash_8_8_t worker_by_out;
180
181   u32 num_workers;
182   u32 first_worker_index;
183   u32 next_worker;
184   u32 * workers;
185
186   /* Per thread data */
187   snat_main_per_thread_data_t * per_thread_data;
188
189   /* Find a static mapping by local */
190   clib_bihash_8_8_t static_mapping_by_local;
191
192   /* Find a static mapping by external */
193   clib_bihash_8_8_t static_mapping_by_external;
194
195   /* Static mapping pool */
196   snat_static_mapping_t * static_mappings;
197
198   /* Interface pool */
199   snat_interface_t * interfaces;
200
201   /* Vector of outside addresses */
202   snat_address_t * addresses;
203
204   /* sw_if_indices whose intfc addresses should be auto-added */
205   u32 * auto_add_sw_if_indices;
206
207   /* vector of interface address static mappings to resolve. */
208   snat_static_map_resolve_t *to_resolve;
209
210   /* Randomize port allocation order */
211   u32 random_seed;
212
213   /* Worker handoff index */
214   u32 fq_in2out_index;
215   u32 fq_out2in_index;
216
217   /* Config parameters */
218   u8 static_mapping_only;
219   u8 static_mapping_connection_tracking;
220   u32 translation_buckets;
221   u32 translation_memory_size;
222   u32 user_buckets;
223   u32 user_memory_size;
224   u32 max_translations_per_user;
225   u32 outside_vrf_id;
226   u32 outside_fib_index;
227   u32 inside_vrf_id;
228   u32 inside_fib_index;
229
230   /* tenant VRF aware address pool activation flag */
231   u8 vrf_mode;
232
233   /* API message ID base */
234   u16 msg_id_base;
235
236   /* convenience */
237   vlib_main_t * vlib_main;
238   vnet_main_t * vnet_main;
239   ip4_main_t * ip4_main;
240   ip_lookup_main_t * ip4_lookup_main;
241   api_main_t * api_main;
242 } snat_main_t;
243
244 extern snat_main_t snat_main;
245 extern vlib_node_registration_t snat_in2out_node;
246 extern vlib_node_registration_t snat_out2in_node;
247 extern vlib_node_registration_t snat_in2out_fast_node;
248 extern vlib_node_registration_t snat_out2in_fast_node;
249 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
250 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
251
252 void snat_free_outside_address_and_port (snat_main_t * sm, 
253                                          snat_session_key_t * k, 
254                                          u32 address_index);
255
256 int snat_alloc_outside_address_and_port (snat_main_t * sm, 
257                                          u32 fib_index,
258                                          snat_session_key_t * k,
259                                          u32 * address_indexp);
260
261 int snat_static_mapping_match (snat_main_t * sm,
262                                snat_session_key_t match,
263                                snat_session_key_t * mapping,
264                                u8 by_external);
265
266 format_function_t format_snat_user;
267
268 typedef struct {
269   u32 cached_sw_if_index;
270   u32 cached_ip4_address;
271 } snat_runtime_t;
272
273 /** \brief Check if SNAT session is created from static mapping.
274     @param s SNAT session
275     @return 1 if SNAT session is created from static mapping otherwise 0
276 */
277 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
278
279 /* 
280  * Why is this here? Because we don't need to touch this layer to
281  * simply reply to an icmp. We need to change id to a unique
282  * value to NAT an echo request/reply.
283  */
284    
285 typedef struct {
286   u16 identifier;
287   u16 sequence;
288 } icmp_echo_header_t;
289
290 always_inline snat_protocol_t
291 ip_proto_to_snat_proto (u8 ip_proto)
292 {
293   snat_protocol_t snat_proto = ~0;
294
295   snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
296   snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
297   snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
298
299   return snat_proto;
300 }
301
302 always_inline u8
303 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
304 {
305   u8 ip_proto = ~0;
306
307   ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
308   ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
309   ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
310
311   return ip_proto;
312 }
313
314 #endif /* __included_snat_h__ */