3 * snat.h - simple nat definitions
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef __included_snat_h__
19 #define __included_snat_h__
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>
72 #define foreach_snat_protocol \
73 _(UDP, 0, udp, "udp") \
74 _(TCP, 1, tcp, "tcp") \
75 _(ICMP, 2, icmp, "icmp")
78 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
84 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
86 typedef CLIB_PACKED(struct {
87 snat_session_key_t out2in; /* 0-15 */
89 snat_session_key_t in2out; /* 16-31 */
91 u32 flags; /* 32-35 */
93 /* per-user translations */
94 u32 per_user_index; /* 36-39 */
96 u32 per_user_list_head_index; /* 40-43 */
98 /* Last heard timer */
99 f64 last_heard; /* 44-51 */
101 u64 total_bytes; /* 52-59 */
103 u32 total_pkts; /* 60-63 */
105 /* Outside address */
106 u32 outside_address_index; /* 64-67 */
113 u32 sessions_per_user_list_head_index;
120 #define _(N, i, n, s) \
121 u32 busy_##n##_ports; \
122 uword * busy_##n##_port_bitmap;
123 foreach_snat_protocol
128 ip4_address_t local_addr;
129 ip4_address_t external_addr;
135 snat_protocol_t proto;
136 } snat_static_mapping_t;
144 ip4_address_t l_addr;
149 snat_protocol_t proto;
152 } snat_static_map_resolve_t;
159 snat_session_t * sessions;
161 /* Pool of doubly-linked list elements */
162 dlist_elt_t * list_pool;
163 } snat_main_per_thread_data_t;
166 /* Main lookup tables */
167 clib_bihash_8_8_t out2in;
168 clib_bihash_8_8_t in2out;
170 /* Find-a-user => src address lookup */
171 clib_bihash_8_8_t user_hash;
173 /* Non-translated packets worker lookup => src address + VRF */
174 clib_bihash_8_8_t worker_by_in;
176 /* Translated packets worker lookup => IP address + port number */
177 clib_bihash_8_8_t worker_by_out;
180 u32 first_worker_index;
184 /* Per thread data */
185 snat_main_per_thread_data_t * per_thread_data;
187 /* Find a static mapping by local */
188 clib_bihash_8_8_t static_mapping_by_local;
190 /* Find a static mapping by external */
191 clib_bihash_8_8_t static_mapping_by_external;
193 /* Static mapping pool */
194 snat_static_mapping_t * static_mappings;
197 snat_interface_t * interfaces;
199 /* Vector of outside addresses */
200 snat_address_t * addresses;
202 /* sw_if_indices whose intfc addresses should be auto-added */
203 u32 * auto_add_sw_if_indices;
205 /* vector of interface address static mappings to resolve. */
206 snat_static_map_resolve_t *to_resolve;
208 /* Randomize port allocation order */
211 /* Worker handoff index */
215 /* Config parameters */
216 u8 static_mapping_only;
217 u8 static_mapping_connection_tracking;
218 u32 translation_buckets;
219 u32 translation_memory_size;
221 u32 user_memory_size;
222 u32 max_translations_per_user;
224 u32 outside_fib_index;
226 u32 inside_fib_index;
228 /* API message ID base */
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;
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;
247 void snat_free_outside_address_and_port (snat_main_t * sm,
248 snat_session_key_t * k,
251 int snat_alloc_outside_address_and_port (snat_main_t * sm,
252 snat_session_key_t * k,
253 u32 * address_indexp);
255 int snat_static_mapping_match (snat_main_t * sm,
256 snat_session_key_t match,
257 snat_session_key_t * mapping,
260 format_function_t format_snat_user;
263 u32 cached_sw_if_index;
264 u32 cached_ip4_address;
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
271 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
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.
282 } icmp_echo_header_t;
284 always_inline snat_protocol_t
285 ip_proto_to_snat_proto (u8 ip_proto)
287 snat_protocol_t snat_proto = ~0;
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;
297 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
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;
308 #endif /* __included_snat_h__ */