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