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