SNAT: user's dump and session dump of a certain snat user.
[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 #define _(N, i, n, s) \
122   u32 busy_##n##_ports; \
123   uword * busy_##n##_port_bitmap;
124   foreach_snat_protocol
125 #undef _
126 } snat_address_t;
127
128 typedef struct {
129   ip4_address_t local_addr;
130   ip4_address_t external_addr;
131   u16 local_port;
132   u16 external_port;
133   u8 addr_only;
134   u32 vrf_id;
135   u32 fib_index;
136   snat_protocol_t proto;
137 } snat_static_mapping_t;
138
139 typedef struct {
140   u32 sw_if_index;
141   u8 is_inside;
142 } snat_interface_t;
143
144 typedef struct {
145   ip4_address_t l_addr;
146   u16 l_port;
147   u16 e_port;
148   u32 sw_if_index;
149   u32 vrf_id;
150   snat_protocol_t proto;
151   int addr_only;
152   int is_add;
153 } snat_static_map_resolve_t;
154
155 typedef struct {
156   /* User pool */
157   snat_user_t * users;
158
159   /* Session pool */
160   snat_session_t * sessions;
161
162   /* Pool of doubly-linked list elements */
163   dlist_elt_t * list_pool;
164 } snat_main_per_thread_data_t;
165
166 typedef struct {
167   /* Main lookup tables */
168   clib_bihash_8_8_t out2in;
169   clib_bihash_8_8_t in2out;
170
171   /* Find-a-user => src address lookup */
172   clib_bihash_8_8_t user_hash;
173
174   /* Non-translated packets worker lookup => src address + VRF */
175   clib_bihash_8_8_t worker_by_in;
176
177   /* Translated packets worker lookup => IP address + port number */
178   clib_bihash_8_8_t worker_by_out;
179
180   u32 num_workers;
181   u32 first_worker_index;
182   u32 next_worker;
183   u32 * workers;
184
185   /* Per thread data */
186   snat_main_per_thread_data_t * per_thread_data;
187
188   /* Find a static mapping by local */
189   clib_bihash_8_8_t static_mapping_by_local;
190
191   /* Find a static mapping by external */
192   clib_bihash_8_8_t static_mapping_by_external;
193
194   /* Static mapping pool */
195   snat_static_mapping_t * static_mappings;
196
197   /* Interface pool */
198   snat_interface_t * interfaces;
199
200   /* Vector of outside addresses */
201   snat_address_t * addresses;
202
203   /* sw_if_indices whose intfc addresses should be auto-added */
204   u32 * auto_add_sw_if_indices;
205
206   /* vector of interface address static mappings to resolve. */
207   snat_static_map_resolve_t *to_resolve;
208
209   /* Randomize port allocation order */
210   u32 random_seed;
211
212   /* Worker handoff index */
213   u32 fq_in2out_index;
214   u32 fq_out2in_index;
215
216   /* Config parameters */
217   u8 static_mapping_only;
218   u8 static_mapping_connection_tracking;
219   u32 translation_buckets;
220   u32 translation_memory_size;
221   u32 user_buckets;
222   u32 user_memory_size;
223   u32 max_translations_per_user;
224   u32 outside_vrf_id;
225   u32 outside_fib_index;
226   u32 inside_vrf_id;
227   u32 inside_fib_index;
228
229   /* API message ID base */
230   u16 msg_id_base;
231
232   /* convenience */
233   vlib_main_t * vlib_main;
234   vnet_main_t * vnet_main;
235   ip4_main_t * ip4_main;
236   ip_lookup_main_t * ip4_lookup_main;
237   api_main_t * api_main;
238 } snat_main_t;
239
240 extern snat_main_t snat_main;
241 extern vlib_node_registration_t snat_in2out_node;
242 extern vlib_node_registration_t snat_out2in_node;
243 extern vlib_node_registration_t snat_in2out_fast_node;
244 extern vlib_node_registration_t snat_out2in_fast_node;
245 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
246 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
247
248 void snat_free_outside_address_and_port (snat_main_t * sm, 
249                                          snat_session_key_t * k, 
250                                          u32 address_index);
251
252 int snat_alloc_outside_address_and_port (snat_main_t * sm, 
253                                          snat_session_key_t * k,
254                                          u32 * address_indexp);
255
256 int snat_static_mapping_match (snat_main_t * sm,
257                                snat_session_key_t match,
258                                snat_session_key_t * mapping,
259                                u8 by_external);
260
261 format_function_t format_snat_user;
262
263 typedef struct {
264   u32 cached_sw_if_index;
265   u32 cached_ip4_address;
266 } snat_runtime_t;
267
268 /** \brief Check if SNAT session is created from static mapping.
269     @param s SNAT session
270     @return 1 if SNAT session is created from static mapping otherwise 0
271 */
272 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
273
274 /* 
275  * Why is this here? Because we don't need to touch this layer to
276  * simply reply to an icmp. We need to change id to a unique
277  * value to NAT an echo request/reply.
278  */
279    
280 typedef struct {
281   u16 identifier;
282   u16 sequence;
283 } icmp_echo_header_t;
284
285 always_inline snat_protocol_t
286 ip_proto_to_snat_proto (u8 ip_proto)
287 {
288   snat_protocol_t snat_proto = ~0;
289
290   snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
291   snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
292   snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
293
294   return snat_proto;
295 }
296
297 always_inline u8
298 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
299 {
300   u8 ip_proto = ~0;
301
302   ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
303   ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
304   ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
305
306   return ip_proto;
307 }
308
309 #endif /* __included_snat_h__ */