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 /* deterministic session outside key */
90 ip4_address_t ext_host_addr;
100 /* Inside network port */
102 /* Outside network address and port */
103 snat_det_out_key_t out;
108 } snat_det_session_t;
112 /* inside IP address range */
113 ip4_address_t in_addr;
115 /* outside IP address range */
116 ip4_address_t out_addr;
118 /* inside IP addresses / outside IP addresses */
120 /* number of ports available to internal host */
122 /* session counter */
124 /* vector of sessions */
125 snat_det_session_t *sessions;
146 typedef struct det44_main_s
148 det44_config_t config;
150 u32 outside_fib_index;
151 u32 inside_fib_index;
153 /* Vector of outside fibs */
154 det44_fib_t *outside_fibs;
156 fib_source_t fib_src_hi;
157 fib_source_t fib_src_low;
159 u32 out2in_node_index;
160 u32 in2out_node_index;
162 /* Deterministic NAT mappings */
163 snat_det_map_t *det_maps;
165 /* TCP MSS clamping */
168 /* Protocol timeouts */
169 nat_timeouts_t timeouts;
171 /* Expire walk process node index */
172 u32 expire_walk_node_index;
176 /* API message ID base */
180 vlib_log_class_t log_class;
182 det44_interface_t *interfaces;
185 ip4_main_t *ip4_main;
187 vnet_main_t *vnet_main;
191 extern det44_main_t det44_main;
194 #define det44_log_err(...) \
195 vlib_log(VLIB_LOG_LEVEL_ERR, det44_main.log_class, __VA_ARGS__)
196 #define det44_log_warn(...) \
197 vlib_log(VLIB_LOG_LEVEL_WARNING, det44_main.log_class, __VA_ARGS__)
198 #define det44_log_notice(...) \
199 vlib_log(VLIB_LOG_LEVEL_NOTICE, det44_main.log_class, __VA_ARGS__)
200 #define det44_log_info(...) \
201 vlib_log(VLIB_LOG_LEVEL_INFO, det44_main.log_class, __VA_ARGS__)
202 #define det44_log_debug(...)\
203 vlib_log(VLIB_LOG_LEVEL_DEBUG, det44_main.log_class, __VA_ARGS__)
205 /* Deterministic NAT interface flags */
206 #define DET44_INTERFACE_FLAG_IS_INSIDE 1
207 #define DET44_INTERFACE_FLAG_IS_OUTSIDE 2
209 /** \brief Check if Deterministic NAT interface is inside.
210 @param i Deterministic NAT interface
211 @return 1 if inside interface
213 #define det44_interface_is_inside(i) i->flags & DET44_INTERFACE_FLAG_IS_INSIDE
215 /** \brief Check if Deterministic NAT interface is outside.
216 @param i Deterministic NAT interface
217 @return 1 if outside interface
219 #define det44_interface_is_outside(i) i->flags & DET44_INTERFACE_FLAG_IS_OUTSIDE
221 static_always_inline u8
224 det44_main_t *dm = &det44_main;
228 extern vlib_node_registration_t det44_in2out_node;
229 extern vlib_node_registration_t det44_out2in_node;
231 int det44_plugin_enable ();
232 int det44_plugin_disable ();
234 int det44_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
236 int det44_set_timeouts (nat_timeouts_t * timeouts);
237 nat_timeouts_t det44_get_timeouts ();
238 void det44_reset_timeouts ();
240 /* format functions */
241 format_function_t format_det_map_ses;
243 int snat_det_add_map (ip4_address_t * in_addr, u8 in_plen,
244 ip4_address_t * out_addr, u8 out_plen, int is_add);
246 /* icmp session match functions */
247 u32 icmp_match_out2in_det (vlib_node_runtime_t * node,
248 u32 thread_index, vlib_buffer_t * b0,
249 ip4_header_t * ip0, ip4_address_t * addr,
250 u16 * port, u32 * fib_index,
251 nat_protocol_t * proto, void *d, void *e,
252 u8 * dont_translate);
253 u32 icmp_match_in2out_det (vlib_node_runtime_t * node,
254 u32 thread_index, vlib_buffer_t * b0,
255 ip4_header_t * ip0, ip4_address_t * addr,
256 u16 * port, u32 * fib_index,
257 nat_protocol_t * proto, void *d, void *e,
258 u8 * dont_translate);
259 u32 det44_icmp_in2out (vlib_buffer_t * b0, ip4_header_t * ip0,
260 icmp46_header_t * icmp0, u32 sw_if_index0,
261 u32 rx_fib_index0, vlib_node_runtime_t * node,
262 u32 next0, u32 thread_index, void *d, void *e);
263 u32 det44_icmp_out2in (vlib_buffer_t * b0, ip4_header_t * ip0,
264 icmp46_header_t * icmp0, u32 sw_if_index0,
265 u32 rx_fib_index0, vlib_node_runtime_t * node,
266 u32 next0, u32 thread_index, void *d, void *e);
268 static_always_inline int
269 is_addr_in_net (ip4_address_t * addr, ip4_address_t * net, u8 plen)
271 if (net->as_u32 == (addr->as_u32 & ip4_main.fib_masks[plen]))
276 static_always_inline snat_det_map_t *
277 snat_det_map_by_user (ip4_address_t * user_addr)
279 det44_main_t *dm = &det44_main;
282 pool_foreach (mp, dm->det_maps,
284 if (is_addr_in_net(user_addr, &mp->in_addr, mp->in_plen))
291 static_always_inline snat_det_map_t *
292 snat_det_map_by_out (ip4_address_t * out_addr)
294 det44_main_t *dm = &det44_main;
297 pool_foreach (mp, dm->det_maps,
299 if (is_addr_in_net(out_addr, &mp->out_addr, mp->out_plen))
306 static_always_inline void
307 snat_det_forward (snat_det_map_t * dm, ip4_address_t * in_addr,
308 ip4_address_t * out_addr, u16 * lo_port)
310 u32 in_offset, out_offset;
312 in_offset = clib_net_to_host_u32 (in_addr->as_u32) -
313 clib_net_to_host_u32 (dm->in_addr.as_u32);
314 out_offset = in_offset / dm->sharing_ratio;
316 clib_host_to_net_u32 (clib_net_to_host_u32 (dm->out_addr.as_u32) +
318 *lo_port = 1024 + dm->ports_per_host * (in_offset % dm->sharing_ratio);
321 static_always_inline void
322 snat_det_reverse (snat_det_map_t * dm, ip4_address_t * out_addr, u16 out_port,
323 ip4_address_t * in_addr)
325 u32 in_offset1, in_offset2, out_offset;
327 out_offset = clib_net_to_host_u32 (out_addr->as_u32) -
328 clib_net_to_host_u32 (dm->out_addr.as_u32);
329 in_offset1 = out_offset * dm->sharing_ratio;
330 in_offset2 = (out_port - 1024) / dm->ports_per_host;
332 clib_host_to_net_u32 (clib_net_to_host_u32 (dm->in_addr.as_u32) +
333 in_offset1 + in_offset2);
336 static_always_inline u32
337 snat_det_user_ses_offset (ip4_address_t * addr, u8 plen)
339 return (clib_net_to_host_u32 (addr->as_u32) & pow2_mask (32 - plen)) *
343 static_always_inline snat_det_session_t *
344 snat_det_get_ses_by_out (snat_det_map_t * dm, ip4_address_t * in_addr,
350 user_offset = snat_det_user_ses_offset (in_addr, dm->in_plen);
351 for (i = 0; i < DET44_SES_PER_USER; i++)
353 if (dm->sessions[i + user_offset].out.as_u64 == out_key)
354 return &dm->sessions[i + user_offset];
360 static_always_inline snat_det_session_t *
361 snat_det_find_ses_by_in (snat_det_map_t * dm, ip4_address_t * in_addr,
362 u16 in_port, snat_det_out_key_t out_key)
364 snat_det_session_t *ses;
368 user_offset = snat_det_user_ses_offset (in_addr, dm->in_plen);
369 for (i = 0; i < DET44_SES_PER_USER; i++)
371 ses = &dm->sessions[i + user_offset];
372 if (ses->in_port == in_port &&
373 ses->out.ext_host_addr.as_u32 == out_key.ext_host_addr.as_u32 &&
374 ses->out.ext_host_port == out_key.ext_host_port)
375 return &dm->sessions[i + user_offset];
381 static_always_inline snat_det_session_t *
382 snat_det_ses_create (u32 thread_index, snat_det_map_t * dm,
383 ip4_address_t * in_addr, u16 in_port,
384 snat_det_out_key_t * out)
389 user_offset = snat_det_user_ses_offset (in_addr, dm->in_plen);
391 for (i = 0; i < DET44_SES_PER_USER; i++)
393 if (!dm->sessions[i + user_offset].in_port)
395 if (clib_atomic_bool_cmp_and_swap
396 (&dm->sessions[i + user_offset].in_port, 0, in_port))
398 dm->sessions[i + user_offset].out.as_u64 = out->as_u64;
399 dm->sessions[i + user_offset].state = DET44_SESSION_UNKNOWN;
400 dm->sessions[i + user_offset].expire = 0;
401 clib_atomic_add_fetch (&dm->ses_num, 1);
402 return &dm->sessions[i + user_offset];
407 nat_ipfix_logging_max_entries_per_user (thread_index,
413 static_always_inline void
414 snat_det_ses_close (snat_det_map_t * dm, snat_det_session_t * ses)
416 if (clib_atomic_bool_cmp_and_swap (&ses->in_port, ses->in_port, 0))
419 clib_atomic_add_fetch (&dm->ses_num, -1);
423 clib_error_t *det44_api_hookup (vlib_main_t * vm);
425 #endif /* __included_det44_h__ */
428 * fd.io coding-style-patch-verification: ON
431 * eval: (c-set-style "gnu")