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>
69 } snat_static_mapping_key_t;
73 SNAT_PROTOCOL_UDP = 0,
79 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
81 typedef CLIB_PACKED(struct {
82 snat_session_key_t out2in; /* 0-15 */
84 snat_session_key_t in2out; /* 16-31 */
86 u32 flags; /* 32-35 */
88 /* per-user translations */
89 u32 per_user_index; /* 36-39 */
91 u32 per_user_list_head_index; /* 40-43 */
93 /* Last heard timer */
94 f64 last_heard; /* 44-51 */
96 u64 total_bytes; /* 52-59 */
98 u32 total_pkts; /* 60-63 */
100 /* Outside address */
101 u32 outside_address_index; /* 64-67 */
108 u32 sessions_per_user_list_head_index;
116 uword * busy_port_bitmap;
120 ip4_address_t local_addr;
121 ip4_address_t external_addr;
127 } snat_static_mapping_t;
130 /* Main lookup tables */
131 clib_bihash_8_8_t out2in;
132 clib_bihash_8_8_t in2out;
134 /* Find-a-user => src address lookup */
135 clib_bihash_8_8_t user_hash;
137 /* Find a static mapping by local */
138 clib_bihash_8_8_t static_mapping_by_local;
140 /* Find a static mapping by external */
141 clib_bihash_8_8_t static_mapping_by_external;
147 snat_session_t * sessions;
149 /* Static mapping pool */
150 snat_static_mapping_t * static_mappings;
152 /* Vector of outside addresses */
153 snat_address_t * addresses;
155 /* Pool of doubly-linked list elements */
156 dlist_elt_t * list_pool;
158 /* Randomize port allocation order */
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;
167 /* Config parameters */
168 u8 static_mapping_only;
169 u8 static_mapping_connection_tracking;
170 u32 translation_buckets;
171 u32 translation_memory_size;
173 u32 user_memory_size;
174 u32 max_translations_per_user;
176 u32 outside_fib_index;
178 u32 inside_fib_index;
180 /* API message ID base */
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;
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;
198 void snat_free_outside_address_and_port (snat_main_t * sm,
199 snat_session_key_t * k,
202 int snat_alloc_outside_address_and_port (snat_main_t * sm,
203 snat_session_key_t * k,
204 u32 * address_indexp);
206 int snat_static_mapping_match (snat_main_t * sm,
207 snat_session_key_t match,
208 snat_session_key_t * mapping,
211 format_function_t format_snat_user;
214 u32 cached_sw_if_index;
215 u32 cached_ip4_address;
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
222 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
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.
233 } icmp_echo_header_t;
235 #endif /* __included_snat_h__ */