VPP-446: 1:1 SNAT Inside overlapping interfaces
[vpp.git] / plugins / snat-plugin / 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   /* Main lookup tables */
131   clib_bihash_8_8_t out2in;
132   clib_bihash_8_8_t in2out;
133
134   /* Find-a-user => src address lookup */
135   clib_bihash_8_8_t user_hash;
136
137   /* Find a static mapping by local */
138   clib_bihash_8_8_t static_mapping_by_local;
139
140   /* Find a static mapping by external */
141   clib_bihash_8_8_t static_mapping_by_external;
142
143   /* User pool */
144   snat_user_t * users;
145
146   /* Session pool */
147   snat_session_t * sessions;
148
149   /* Static mapping pool */
150   snat_static_mapping_t * static_mappings;
151
152   /* Vector of outside addresses */
153   snat_address_t * addresses;
154
155   /* Pool of doubly-linked list elements */
156   dlist_elt_t * list_pool;
157
158   /* Randomize port allocation order */
159   u32 random_seed;
160
161   /* ip4 feature path indices */
162   u32 rx_feature_in2out;
163   u32 rx_feature_out2in;
164   u32 rx_feature_in2out_fast;
165   u32 rx_feature_out2in_fast;
166
167   /* Config parameters */
168   u8 static_mapping_only;
169   u8 static_mapping_connection_tracking;
170   u32 translation_buckets;
171   u32 translation_memory_size;
172   u32 user_buckets;
173   u32 user_memory_size;
174   u32 max_translations_per_user;
175   u32 outside_vrf_id;
176   u32 outside_fib_index;
177   u32 inside_vrf_id;
178   u32 inside_fib_index;
179
180   /* API message ID base */
181   u16 msg_id_base;
182
183   /* convenience */
184   vlib_main_t * vlib_main;
185   vnet_main_t * vnet_main;
186   ip4_main_t * ip4_main;
187   ip_lookup_main_t * ip4_lookup_main;
188   ethernet_main_t * ethernet_main;  
189   api_main_t * api_main;
190 } snat_main_t;
191
192 extern snat_main_t snat_main;
193 extern vlib_node_registration_t snat_in2out_node;
194 extern vlib_node_registration_t snat_out2in_node;
195 extern vlib_node_registration_t snat_in2out_fast_node;
196 extern vlib_node_registration_t snat_out2in_fast_node;
197
198 void snat_free_outside_address_and_port (snat_main_t * sm, 
199                                          snat_session_key_t * k, 
200                                          u32 address_index);
201
202 int snat_alloc_outside_address_and_port (snat_main_t * sm, 
203                                          snat_session_key_t * k,
204                                          u32 * address_indexp);
205
206 int snat_static_mapping_match (snat_main_t * sm,
207                                snat_session_key_t match,
208                                snat_session_key_t * mapping,
209                                u8 by_external);
210
211 format_function_t format_snat_user;
212
213 typedef struct {
214   u32 cached_sw_if_index;
215   u32 cached_ip4_address;
216 } snat_runtime_t;
217
218 /** \brief Check if SNAT session is created from static mapping.
219     @param s SNAT session
220     @return 1 if SNAT session is created from static mapping otherwise 0
221 */
222 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
223
224 /* 
225  * Why is this here? Because we don't need to touch this layer to
226  * simply reply to an icmp. We need to change id to a unique
227  * value to NAT an echo request/reply.
228  */
229    
230 typedef struct {
231   u16 identifier;
232   u16 sequence;
233 } icmp_echo_header_t;
234
235 #endif /* __included_snat_h__ */