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