nat: ipfix logging separation & refactor
[vpp.git] / src / plugins / nat / det44 / det44.h
1 /*
2  * det44.h - deterministic NAT definitions
3  *
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:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  * @file
20  * @brief Deterministic NAT (CGN) definitions
21  */
22
23 #ifndef __included_det44_h__
24 #define __included_det44_h__
25
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>
35 #include <vlib/log.h>
36 #include <vnet/fib/fib_table.h>
37 #include <vnet/fib/ip4_fib.h>
38 #include <vnet/ip/reass/ip4_sv_reass.h>
39
40 #include <nat/lib/lib.h>
41 #include <nat/lib/inlines.h>
42 #include <nat/lib/ipfix_logging.h>
43
44 /* Session state */
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")
56
57 typedef enum
58 {
59 #define _(v, N, s) DET44_SESSION_##N = v,
60   foreach_det44_session_state
61 #undef _
62 } det44_session_state_t;
63
64 #define DET44_SES_PER_USER 1000
65
66 typedef struct
67 {
68   u16 identifier;
69   u16 sequence;
70 } icmp_echo_header_t;
71
72 typedef struct
73 {
74   u16 src_port, dst_port;
75 } tcp_udp_header_t;
76
77 typedef struct
78 {
79   u32 cached_sw_if_index;
80   u32 cached_ip4_address;
81 } det44_runtime_t;
82
83 typedef struct nat_timeouts_s
84 {
85   u32 udp;
86
87   struct
88   {
89     u32 transitory;
90     u32 established;
91   } tcp;
92
93   u32 icmp;
94
95 } nat_timeouts_t;
96
97 /* deterministic session outside key */
98 typedef struct
99 {
100   union
101   {
102     struct
103     {
104       ip4_address_t ext_host_addr;
105       u16 ext_host_port;
106       u16 out_port;
107     };
108     u64 as_u64;
109   };
110 } snat_det_out_key_t;
111
112 typedef struct
113 {
114   /* Inside network port */
115   u16 in_port;
116   /* Outside network address and port */
117   snat_det_out_key_t out;
118   /* Session state */
119   u8 state;
120   /* Expire timeout */
121   u32 expire;
122 } snat_det_session_t;
123
124 typedef struct
125 {
126   /* inside IP address range */
127   ip4_address_t in_addr;
128   u8 in_plen;
129   /* outside IP address range */
130   ip4_address_t out_addr;
131   u8 out_plen;
132   /* inside IP addresses / outside IP addresses */
133   u32 sharing_ratio;
134   /* number of ports available to internal host */
135   u16 ports_per_host;
136   /* session counter */
137   u32 ses_num;
138   /* vector of sessions */
139   snat_det_session_t *sessions;
140 } snat_det_map_t;
141
142 typedef struct
143 {
144   u32 sw_if_index;
145   u8 flags;
146 } det44_interface_t;
147
148 typedef struct
149 {
150   u32 outside_vrf_id;
151   u32 inside_vrf_id;
152 } det44_config_t;
153
154 typedef struct
155 {
156   u32 fib_index;
157   u32 refcount;
158 } det44_fib_t;
159
160 typedef struct det44_main_s
161 {
162   det44_config_t config;
163
164   u32 outside_fib_index;
165   u32 inside_fib_index;
166
167   /* Vector of outside fibs */
168   det44_fib_t *outside_fibs;
169
170   fib_source_t fib_src_hi;
171   fib_source_t fib_src_low;
172
173   u32 out2in_node_index;
174   u32 in2out_node_index;
175
176   /* Deterministic NAT mappings */
177   snat_det_map_t *det_maps;
178
179   /* TCP MSS clamping */
180   u16 mss_clamping;
181
182   /* Protocol timeouts */
183   nat_timeouts_t timeouts;
184
185   /* Expire walk process node index */
186   u32 expire_walk_node_index;
187
188   u32 enabled;
189
190   /* API message ID base */
191   u16 msg_id_base;
192
193   /* log class */
194   vlib_log_class_t log_class;
195
196   det44_interface_t *interfaces;
197
198   /* convenience */
199   ip4_main_t *ip4_main;
200   /* required */
201   vnet_main_t *vnet_main;
202
203 } det44_main_t;
204
205 extern det44_main_t det44_main;
206
207 /* logging */
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__)
218
219 /* Deterministic NAT interface flags */
220 #define DET44_INTERFACE_FLAG_IS_INSIDE 1
221 #define DET44_INTERFACE_FLAG_IS_OUTSIDE 2
222
223 /** \brief Check if Deterministic NAT interface is inside.
224     @param i Deterministic NAT interface
225     @return 1 if inside interface
226 */
227 #define det44_interface_is_inside(i) i->flags & DET44_INTERFACE_FLAG_IS_INSIDE
228
229 /** \brief Check if Deterministic NAT interface is outside.
230     @param i Deterministic NAT interface
231     @return 1 if outside interface
232 */
233 #define det44_interface_is_outside(i) i->flags & DET44_INTERFACE_FLAG_IS_OUTSIDE
234
235 static_always_inline u8
236 plugin_enabled ()
237 {
238   det44_main_t *dm = &det44_main;
239   return dm->enabled;
240 }
241
242 extern vlib_node_registration_t det44_in2out_node;
243 extern vlib_node_registration_t det44_out2in_node;
244
245 int det44_plugin_enable ();
246 int det44_plugin_disable ();
247
248 int det44_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
249
250 int det44_set_timeouts (nat_timeouts_t * timeouts);
251 nat_timeouts_t det44_get_timeouts ();
252 void det44_reset_timeouts ();
253
254 /* format functions */
255 format_function_t format_det_map_ses;
256
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);
259
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);
281
282 static_always_inline int
283 is_addr_in_net (ip4_address_t * addr, ip4_address_t * net, u8 plen)
284 {
285   if (net->as_u32 == (addr->as_u32 & ip4_main.fib_masks[plen]))
286     return 1;
287   return 0;
288 }
289
290 static_always_inline snat_det_map_t *
291 snat_det_map_by_user (ip4_address_t * user_addr)
292 {
293   det44_main_t *dm = &det44_main;
294   snat_det_map_t *mp;
295   /* *INDENT-OFF* */
296   pool_foreach (mp, dm->det_maps,
297   ({
298     if (is_addr_in_net(user_addr, &mp->in_addr, mp->in_plen))
299       return mp;
300   }));
301   /* *INDENT-ON* */
302   return 0;
303 }
304
305 static_always_inline snat_det_map_t *
306 snat_det_map_by_out (ip4_address_t * out_addr)
307 {
308   det44_main_t *dm = &det44_main;
309   snat_det_map_t *mp;
310   /* *INDENT-OFF* */
311   pool_foreach (mp, dm->det_maps,
312   ({
313     if (is_addr_in_net(out_addr, &mp->out_addr, mp->out_plen))
314       return mp;
315   }));
316   /* *INDENT-ON* */
317   return 0;
318 }
319
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)
323 {
324   u32 in_offset, out_offset;
325
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;
329   out_addr->as_u32 =
330     clib_host_to_net_u32 (clib_net_to_host_u32 (dm->out_addr.as_u32) +
331                           out_offset);
332   *lo_port = 1024 + dm->ports_per_host * (in_offset % dm->sharing_ratio);
333 }
334
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)
338 {
339   u32 in_offset1, in_offset2, out_offset;
340
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;
345   in_addr->as_u32 =
346     clib_host_to_net_u32 (clib_net_to_host_u32 (dm->in_addr.as_u32) +
347                           in_offset1 + in_offset2);
348 }
349
350 static_always_inline u32
351 snat_det_user_ses_offset (ip4_address_t * addr, u8 plen)
352 {
353   return (clib_net_to_host_u32 (addr->as_u32) & pow2_mask (32 - plen)) *
354     DET44_SES_PER_USER;
355 }
356
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,
359                          u64 out_key)
360 {
361   u32 user_offset;
362   u16 i;
363
364   user_offset = snat_det_user_ses_offset (in_addr, dm->in_plen);
365   for (i = 0; i < DET44_SES_PER_USER; i++)
366     {
367       if (dm->sessions[i + user_offset].out.as_u64 == out_key)
368         return &dm->sessions[i + user_offset];
369     }
370
371   return 0;
372 }
373
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)
377 {
378   snat_det_session_t *ses;
379   u32 user_offset;
380   u16 i;
381
382   user_offset = snat_det_user_ses_offset (in_addr, dm->in_plen);
383   for (i = 0; i < DET44_SES_PER_USER; i++)
384     {
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];
390     }
391
392   return 0;
393 }
394
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)
399 {
400   u32 user_offset;
401   u16 i;
402
403   user_offset = snat_det_user_ses_offset (in_addr, dm->in_plen);
404
405   for (i = 0; i < DET44_SES_PER_USER; i++)
406     {
407       if (!dm->sessions[i + user_offset].in_port)
408         {
409           if (clib_atomic_bool_cmp_and_swap
410               (&dm->sessions[i + user_offset].in_port, 0, in_port))
411             {
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];
417             }
418         }
419     }
420
421   nat_ipfix_logging_max_entries_per_user (thread_index,
422                                           DET44_SES_PER_USER,
423                                           in_addr->as_u32);
424   return 0;
425 }
426
427 static_always_inline void
428 snat_det_ses_close (snat_det_map_t * dm, snat_det_session_t * ses)
429 {
430   if (clib_atomic_bool_cmp_and_swap (&ses->in_port, ses->in_port, 0))
431     {
432       ses->out.as_u64 = 0;
433       clib_atomic_add_fetch (&dm->ses_num, -1);
434     }
435 }
436
437 clib_error_t *det44_api_hookup (vlib_main_t * vm);
438
439 #endif /* __included_det44_h__ */
440
441 /*
442  * fd.io coding-style-patch-verification: ON
443  *
444  * Local Variables:
445  * eval: (c-set-style "gnu")
446  * End:
447  */