2 * det44.h - deterministic NAT definitions
4 * Copyright (c) 2020 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * @brief Deterministic NAT (CGN) definitions
23 #ifndef __included_det44_h__
24 #define __included_det44_h__
26 #include <vnet/vnet.h>
27 #include <vnet/ip/ip.h>
28 #include <vnet/ethernet/ethernet.h>
29 #include <vnet/ip/icmp46_packet.h>
30 #include <vnet/api_errno.h>
31 #include <vnet/fib/fib_source.h>
32 #include <vppinfra/dlist.h>
33 #include <vppinfra/error.h>
34 #include <vlibapi/api.h>
36 #include <vnet/fib/fib_table.h>
37 #include <vnet/fib/ip4_fib.h>
38 #include <vnet/ip/reass/ip4_sv_reass.h>
40 #include <nat/lib/lib.h>
41 #include <nat/lib/inlines.h>
42 #include <nat/lib/ipfix_logging.h>
45 #define foreach_det44_session_state \
46 _(0, UNKNOWN, "unknown") \
47 _(1, UDP_ACTIVE, "udp-active") \
48 _(2, TCP_SYN_SENT, "tcp-syn-sent") \
49 _(3, TCP_ESTABLISHED, "tcp-established") \
50 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
51 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
52 _(6, TCP_CLOSING, "tcp-closing") \
53 _(7, TCP_LAST_ACK, "tcp-last-ack") \
54 _(8, TCP_CLOSED, "tcp-closed") \
55 _(9, ICMP_ACTIVE, "icmp-active")
59 #define _(v, N, s) DET44_SESSION_##N = v,
60 foreach_det44_session_state
62 } det44_session_state_t;
64 #define DET44_SES_PER_USER 1000
74 u16 src_port, dst_port;
79 u32 cached_sw_if_index;
80 u32 cached_ip4_address;
83 typedef struct nat_timeouts_s
97 /* deterministic session outside key */
104 ip4_address_t ext_host_addr;
110 } snat_det_out_key_t;
114 /* Inside network port */
116 /* Outside network address and port */
117 snat_det_out_key_t out;
122 } snat_det_session_t;
126 /* inside IP address range */
127 ip4_address_t in_addr;
129 /* outside IP address range */
130 ip4_address_t out_addr;
132 /* inside IP addresses / outside IP addresses */
134 /* number of ports available to internal host */
136 /* session counter */
138 /* vector of sessions */
139 snat_det_session_t *sessions;
160 typedef struct det44_main_s
162 det44_config_t config;
164 u32 outside_fib_index;
165 u32 inside_fib_index;
167 /* Vector of outside fibs */
168 det44_fib_t *outside_fibs;
170 fib_source_t fib_src_hi;
171 fib_source_t fib_src_low;
173 u32 out2in_node_index;
174 u32 in2out_node_index;
176 /* Deterministic NAT mappings */
177 snat_det_map_t *det_maps;
179 /* TCP MSS clamping */
182 /* Protocol timeouts */
183 nat_timeouts_t timeouts;
185 /* Expire walk process node index */
186 u32 expire_walk_node_index;
190 /* API message ID base */
194 vlib_log_class_t log_class;
196 det44_interface_t *interfaces;
199 ip4_main_t *ip4_main;
201 vnet_main_t *vnet_main;
205 extern det44_main_t det44_main;
208 #define det44_log_err(...) \
209 vlib_log(VLIB_LOG_LEVEL_ERR, det44_main.log_class, __VA_ARGS__)
210 #define det44_log_warn(...) \
211 vlib_log(VLIB_LOG_LEVEL_WARNING, det44_main.log_class, __VA_ARGS__)
212 #define det44_log_notice(...) \
213 vlib_log(VLIB_LOG_LEVEL_NOTICE, det44_main.log_class, __VA_ARGS__)
214 #define det44_log_info(...) \
215 vlib_log(VLIB_LOG_LEVEL_INFO, det44_main.log_class, __VA_ARGS__)
216 #define det44_log_debug(...)\
217 vlib_log(VLIB_LOG_LEVEL_DEBUG, det44_main.log_class, __VA_ARGS__)
219 /* Deterministic NAT interface flags */
220 #define DET44_INTERFACE_FLAG_IS_INSIDE 1
221 #define DET44_INTERFACE_FLAG_IS_OUTSIDE 2
223 /** \brief Check if Deterministic NAT interface is inside.
224 @param i Deterministic NAT interface
225 @return 1 if inside interface
227 #define det44_interface_is_inside(i) i->flags & DET44_INTERFACE_FLAG_IS_INSIDE
229 /** \brief Check if Deterministic NAT interface is outside.
230 @param i Deterministic NAT interface
231 @return 1 if outside interface
233 #define det44_interface_is_outside(i) i->flags & DET44_INTERFACE_FLAG_IS_OUTSIDE
235 static_always_inline u8
238 det44_main_t *dm = &det44_main;
242 extern vlib_node_registration_t det44_in2out_node;
243 extern vlib_node_registration_t det44_out2in_node;
245 int det44_plugin_enable ();
246 int det44_plugin_disable ();
248 int det44_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
250 int det44_set_timeouts (nat_timeouts_t * timeouts);
251 nat_timeouts_t det44_get_timeouts ();
252 void det44_reset_timeouts ();
254 /* format functions */
255 format_function_t format_det_map_ses;
257 int snat_det_add_map (ip4_address_t * in_addr, u8 in_plen,
258 ip4_address_t * out_addr, u8 out_plen, int is_add);
260 /* icmp session match functions */
261 u32 icmp_match_out2in_det (vlib_node_runtime_t * node,
262 u32 thread_index, vlib_buffer_t * b0,
263 ip4_header_t * ip0, ip4_address_t * addr,
264 u16 * port, u32 * fib_index,
265 nat_protocol_t * proto, void *d, void *e,
266 u8 * dont_translate);
267 u32 icmp_match_in2out_det (vlib_node_runtime_t * node,
268 u32 thread_index, vlib_buffer_t * b0,
269 ip4_header_t * ip0, ip4_address_t * addr,
270 u16 * port, u32 * fib_index,
271 nat_protocol_t * proto, void *d, void *e,
272 u8 * dont_translate);
273 u32 det44_icmp_in2out (vlib_buffer_t * b0, ip4_header_t * ip0,
274 icmp46_header_t * icmp0, u32 sw_if_index0,
275 u32 rx_fib_index0, vlib_node_runtime_t * node,
276 u32 next0, u32 thread_index, void *d, void *e);
277 u32 det44_icmp_out2in (vlib_buffer_t * b0, ip4_header_t * ip0,
278 icmp46_header_t * icmp0, u32 sw_if_index0,
279 u32 rx_fib_index0, vlib_node_runtime_t * node,
280 u32 next0, u32 thread_index, void *d, void *e);
282 static_always_inline int
283 is_addr_in_net (ip4_address_t * addr, ip4_address_t * net, u8 plen)
285 if (net->as_u32 == (addr->as_u32 & ip4_main.fib_masks[plen]))
290 static_always_inline snat_det_map_t *
291 snat_det_map_by_user (ip4_address_t * user_addr)
293 det44_main_t *dm = &det44_main;
296 pool_foreach (mp, dm->det_maps,
298 if (is_addr_in_net(user_addr, &mp->in_addr, mp->in_plen))
305 static_always_inline snat_det_map_t *
306 snat_det_map_by_out (ip4_address_t * out_addr)
308 det44_main_t *dm = &det44_main;
311 pool_foreach (mp, dm->det_maps,
313 if (is_addr_in_net(out_addr, &mp->out_addr, mp->out_plen))
320 static_always_inline void
321 snat_det_forward (snat_det_map_t * dm, ip4_address_t * in_addr,
322 ip4_address_t * out_addr, u16 * lo_port)
324 u32 in_offset, out_offset;
326 in_offset = clib_net_to_host_u32 (in_addr->as_u32) -
327 clib_net_to_host_u32 (dm->in_addr.as_u32);
328 out_offset = in_offset / dm->sharing_ratio;
330 clib_host_to_net_u32 (clib_net_to_host_u32 (dm->out_addr.as_u32) +
332 *lo_port = 1024 + dm->ports_per_host * (in_offset % dm->sharing_ratio);
335 static_always_inline void
336 snat_det_reverse (snat_det_map_t * dm, ip4_address_t * out_addr, u16 out_port,
337 ip4_address_t * in_addr)
339 u32 in_offset1, in_offset2, out_offset;
341 out_offset = clib_net_to_host_u32 (out_addr->as_u32) -
342 clib_net_to_host_u32 (dm->out_addr.as_u32);
343 in_offset1 = out_offset * dm->sharing_ratio;
344 in_offset2 = (out_port - 1024) / dm->ports_per_host;
346 clib_host_to_net_u32 (clib_net_to_host_u32 (dm->in_addr.as_u32) +
347 in_offset1 + in_offset2);
350 static_always_inline u32
351 snat_det_user_ses_offset (ip4_address_t * addr, u8 plen)
353 return (clib_net_to_host_u32 (addr->as_u32) & pow2_mask (32 - plen)) *
357 static_always_inline snat_det_session_t *
358 snat_det_get_ses_by_out (snat_det_map_t * dm, ip4_address_t * in_addr,
364 user_offset = snat_det_user_ses_offset (in_addr, dm->in_plen);
365 for (i = 0; i < DET44_SES_PER_USER; i++)
367 if (dm->sessions[i + user_offset].out.as_u64 == out_key)
368 return &dm->sessions[i + user_offset];
374 static_always_inline snat_det_session_t *
375 snat_det_find_ses_by_in (snat_det_map_t * dm, ip4_address_t * in_addr,
376 u16 in_port, snat_det_out_key_t out_key)
378 snat_det_session_t *ses;
382 user_offset = snat_det_user_ses_offset (in_addr, dm->in_plen);
383 for (i = 0; i < DET44_SES_PER_USER; i++)
385 ses = &dm->sessions[i + user_offset];
386 if (ses->in_port == in_port &&
387 ses->out.ext_host_addr.as_u32 == out_key.ext_host_addr.as_u32 &&
388 ses->out.ext_host_port == out_key.ext_host_port)
389 return &dm->sessions[i + user_offset];
395 static_always_inline snat_det_session_t *
396 snat_det_ses_create (u32 thread_index, snat_det_map_t * dm,
397 ip4_address_t * in_addr, u16 in_port,
398 snat_det_out_key_t * out)
403 user_offset = snat_det_user_ses_offset (in_addr, dm->in_plen);
405 for (i = 0; i < DET44_SES_PER_USER; i++)
407 if (!dm->sessions[i + user_offset].in_port)
409 if (clib_atomic_bool_cmp_and_swap
410 (&dm->sessions[i + user_offset].in_port, 0, in_port))
412 dm->sessions[i + user_offset].out.as_u64 = out->as_u64;
413 dm->sessions[i + user_offset].state = DET44_SESSION_UNKNOWN;
414 dm->sessions[i + user_offset].expire = 0;
415 clib_atomic_add_fetch (&dm->ses_num, 1);
416 return &dm->sessions[i + user_offset];
421 nat_ipfix_logging_max_entries_per_user (thread_index,
427 static_always_inline void
428 snat_det_ses_close (snat_det_map_t * dm, snat_det_session_t * ses)
430 if (clib_atomic_bool_cmp_and_swap (&ses->in_port, ses->in_port, 0))
433 clib_atomic_add_fetch (&dm->ses_num, -1);
437 clib_error_t *det44_api_hookup (vlib_main_t * vm);
439 #endif /* __included_det44_h__ */
442 * fd.io coding-style-patch-verification: ON
445 * eval: (c-set-style "gnu")