cnat: Disable default scanner process
[vpp.git] / src / plugins / cnat / cnat_translation.c
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/fib/fib_source.h>
17 #include <vnet/fib/fib_table.h>
18 #include <vnet/fib/fib_entry_track.h>
19 #include <vnet/dpo/load_balance.h>
20 #include <vnet/dpo/drop_dpo.h>
21
22 #include <cnat/cnat_translation.h>
23 #include <cnat/cnat_session.h>
24 #include <cnat/cnat_client.h>
25
26 cnat_translation_t *cnat_translation_pool;
27 clib_bihash_8_8_t cnat_translation_db;
28
29 static fib_node_type_t cnat_translation_fib_node_type;
30
31 vlib_combined_counter_main_t cnat_translation_counters = {
32   .name = "cnat-translation",
33   .stat_segment_name = "/net/cnat-translation",
34 };
35
36 static void
37 cnat_tracker_release (cnat_ep_trk_t * trk)
38 {
39   fib_entry_untrack (trk->ct_fei, trk->ct_sibling);
40 }
41
42 static void
43 cnat_tracker_track (index_t cti,
44                     const cnat_endpoint_tuple_t * path, cnat_ep_trk_t * trk)
45 {
46   fib_prefix_t pfx;
47
48   ip_address_to_fib_prefix (&path->dst_ep.ce_ip, &pfx);
49
50   clib_memcpy (&trk->ct_ep[VLIB_TX], &path->dst_ep,
51                sizeof (trk->ct_ep[VLIB_TX]));
52   clib_memcpy (&trk->ct_ep[VLIB_RX], &path->src_ep,
53                sizeof (trk->ct_ep[VLIB_RX]));
54
55   trk->ct_fei = fib_entry_track (CNAT_FIB_TABLE,
56                                  &pfx,
57                                  cnat_translation_fib_node_type,
58                                  cti, &trk->ct_sibling);
59
60   fib_entry_contribute_forwarding (trk->ct_fei,
61                                    fib_forw_chain_type_from_fib_proto
62                                    (pfx.fp_proto), &trk->ct_dpo);
63 }
64
65 void
66 cnat_add_translation_to_db (index_t cci, u16 port, ip_protocol_t proto,
67                             index_t cti)
68 {
69   clib_bihash_kv_8_8_t bkey;
70   u64 key;
71
72   key = (proto << 16) | port;
73   key = key << 32 | (u32) cci;
74
75   bkey.key = key;
76   bkey.value = cti;
77
78   clib_bihash_add_del_8_8 (&cnat_translation_db, &bkey, 1);
79 }
80
81 void
82 cnat_remove_translation_from_db (index_t cci, u16 port, ip_protocol_t proto)
83 {
84   clib_bihash_kv_8_8_t bkey;
85   u64 key;
86
87   key = (proto << 16) | port;
88   key = key << 32 | (u32) cci;
89
90   bkey.key = key;
91
92   clib_bihash_add_del_8_8 (&cnat_translation_db, &bkey, 0);
93 }
94
95 static void
96 cnat_translation_stack (cnat_translation_t * ct)
97 {
98   fib_protocol_t fproto;
99   cnat_ep_trk_t *trk;
100   dpo_proto_t dproto;
101   index_t lbi;
102
103   fproto = ip_address_family_to_fib_proto (ct->ct_vip.ce_ip.version);
104   dproto = fib_proto_to_dpo (fproto);
105
106   lbi = load_balance_create (vec_len (ct->ct_paths),
107                              fib_proto_to_dpo (fproto), IP_FLOW_HASH_DEFAULT);
108
109   vec_foreach (trk, ct->ct_paths)
110     load_balance_set_bucket (lbi, trk - ct->ct_paths, &trk->ct_dpo);
111
112   dpo_set (&ct->ct_lb, DPO_LOAD_BALANCE, dproto, lbi);
113   dpo_stack (cnat_client_dpo, dproto, &ct->ct_lb, &ct->ct_lb);
114 }
115
116 int
117 cnat_translation_delete (u32 id)
118 {
119   cnat_translation_t *ct;
120   cnat_ep_trk_t *trk;
121
122   if (pool_is_free_index (cnat_translation_pool, id))
123     return (VNET_API_ERROR_NO_SUCH_ENTRY);
124
125   ct = pool_elt_at_index (cnat_translation_pool, id);
126
127   dpo_reset (&ct->ct_lb);
128
129   vec_foreach (trk, ct->ct_paths) cnat_tracker_release (trk);
130
131   cnat_remove_translation_from_db (ct->ct_cci, ct->ct_vip.ce_port,
132                                    ct->ct_proto);
133   cnat_client_translation_deleted (ct->ct_cci);
134   pool_put (cnat_translation_pool, ct);
135
136   return (0);
137 }
138
139 u32
140 cnat_translation_update (const cnat_endpoint_t * vip,
141                          ip_protocol_t proto,
142                          const cnat_endpoint_tuple_t * paths, u8 flags)
143 {
144   const cnat_endpoint_tuple_t *path;
145   const cnat_client_t *cc;
146   cnat_translation_t *ct;
147   cnat_ep_trk_t *trk;
148   index_t cci;
149
150   cnat_lazy_init ();
151
152   /* do we know of this ep's vip */
153   cci = cnat_client_add (&vip->ce_ip, flags);
154   cc = cnat_client_get (cci);
155
156   ct = cnat_find_translation (cc->parent_cci, vip->ce_port, proto);
157
158   if (NULL == ct)
159     {
160       pool_get_zero (cnat_translation_pool, ct);
161
162       clib_memcpy (&ct->ct_vip, vip, sizeof (*vip));
163       ct->ct_proto = proto;
164       ct->ct_cci = cci;
165       ct->index = ct - cnat_translation_pool;
166
167       cnat_add_translation_to_db (cci, ct->ct_vip.ce_port, ct->ct_proto,
168                                   ct->index);
169       cnat_client_translation_added (cci);
170
171       vlib_validate_combined_counter (&cnat_translation_counters, ct->index);
172       vlib_zero_combined_counter (&cnat_translation_counters, ct->index);
173     }
174   ct->flags = flags;
175
176   vec_foreach (trk, ct->ct_paths)
177   {
178     cnat_tracker_release (trk);
179   }
180
181   vec_reset_length (ct->ct_paths);
182
183   vec_foreach (path, paths)
184   {
185     vec_add2 (ct->ct_paths, trk, 1);
186
187     cnat_tracker_track (ct->index, path, trk);
188   }
189
190   cnat_translation_stack (ct);
191
192   return (ct->index);
193 }
194
195 void
196 cnat_translation_walk (cnat_translation_walk_cb_t cb, void *ctx)
197 {
198   u32 api;
199
200   /* *INDENT-OFF* */
201   pool_foreach_index(api, cnat_translation_pool,
202   ({
203     if (!cb(api, ctx))
204       break;
205   }));
206   /* *INDENT-ON* */
207 }
208
209 static u8 *
210 format_cnat_ep_trk (u8 * s, va_list * args)
211 {
212   cnat_ep_trk_t *ck = va_arg (*args, cnat_ep_trk_t *);
213   u32 indent = va_arg (*args, u32);
214
215   s = format (s, "%U->%U", format_cnat_endpoint, &ck->ct_ep[VLIB_RX],
216               format_cnat_endpoint, &ck->ct_ep[VLIB_TX]);
217   s = format (s, "\n%Ufib-entry:%d", format_white_space, indent, ck->ct_fei);
218   s = format (s, "\n%U%U",
219               format_white_space, indent, format_dpo_id, &ck->ct_dpo, 6);
220
221   return (s);
222 }
223
224 u8 *
225 format_cnat_translation (u8 * s, va_list * args)
226 {
227   cnat_translation_t *ct = va_arg (*args, cnat_translation_t *);
228   cnat_ep_trk_t *ck;
229
230   s = format (s, "[%d] ", ct->index);
231   s = format (s, "%U %U", format_cnat_endpoint, &ct->ct_vip,
232               format_ip_protocol, ct->ct_proto);
233
234   vec_foreach (ck, ct->ct_paths)
235     s = format (s, "\n%U", format_cnat_ep_trk, ck, 2);
236
237   /* If printing a trace, the LB object might be deleted */
238   if (!pool_is_free_index (load_balance_pool, ct->ct_lb.dpoi_index))
239     {
240       s = format (s, "\n via:");
241       s = format (s, "\n%U%U",
242                   format_white_space, 2, format_dpo_id, &ct->ct_lb, 2);
243     }
244
245   return (s);
246 }
247
248 static clib_error_t *
249 cnat_translation_show (vlib_main_t * vm,
250                        unformat_input_t * input, vlib_cli_command_t * cmd)
251 {
252   index_t cti;
253   cnat_translation_t *ct;
254
255   cti = INDEX_INVALID;
256
257   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
258     {
259       if (unformat (input, "%d", &cti))
260         ;
261       else
262         return (clib_error_return (0, "unknown input '%U'",
263                                    format_unformat_error, input));
264     }
265
266   if (INDEX_INVALID == cti)
267     {
268       /* *INDENT-OFF* */
269       pool_foreach_index(cti, cnat_translation_pool,
270       ({
271         ct = pool_elt_at_index (cnat_translation_pool, cti);
272         vlib_cli_output(vm, "%U", format_cnat_translation, ct);
273       }));
274       /* *INDENT-ON* */
275     }
276   else
277     {
278       vlib_cli_output (vm, "Invalid policy ID:%d", cti);
279     }
280
281   return (NULL);
282 }
283
284 int
285 cnat_translation_purge (void)
286 {
287   /* purge all the translations */
288   index_t tri, *trp, *trs = NULL;
289
290   /* *INDENT-OFF* */
291   pool_foreach_index(tri, cnat_translation_pool,
292   ({
293     vec_add1(trs, tri);
294   }));
295   /* *INDENT-ON* */
296
297   vec_foreach (trp, trs) cnat_translation_delete (*trp);
298
299   ASSERT (0 == pool_elts (cnat_translation_pool));
300
301   vec_free (trs);
302
303   return (0);
304 }
305
306 /* *INDENT-OFF* */
307 VLIB_CLI_COMMAND (cnat_translation_show_cmd_node, static) = {
308   .path = "show cnat translation",
309   .function = cnat_translation_show,
310   .short_help = "show cnat translation <VIP>",
311   .is_mp_safe = 1,
312 };
313 /* *INDENT-ON* */
314
315 static fib_node_t *
316 cnat_translation_get_node (fib_node_index_t index)
317 {
318   cnat_translation_t *ct = cnat_translation_get (index);
319   return (&(ct->ct_node));
320 }
321
322 static cnat_translation_t *
323 cnat_translation_get_from_node (fib_node_t * node)
324 {
325   return ((cnat_translation_t *) (((char *) node) -
326                                   STRUCT_OFFSET_OF (cnat_translation_t,
327                                                     ct_node)));
328 }
329
330 static void
331 cnat_translation_last_lock_gone (fib_node_t * node)
332 {
333  /**/}
334
335 /*
336  * A back walk has reached this ABF policy
337  */
338 static fib_node_back_walk_rc_t
339 cnat_translation_back_walk_notify (fib_node_t * node,
340                                    fib_node_back_walk_ctx_t * ctx)
341 {
342   /*
343    * re-stack the fmask on the n-eos of the via
344    */
345   cnat_translation_t *ct = cnat_translation_get_from_node (node);
346
347   cnat_translation_stack (ct);
348
349   return (FIB_NODE_BACK_WALK_CONTINUE);
350 }
351
352 /*
353  * The translation's graph node virtual function table
354  */
355 static const fib_node_vft_t cnat_translation_vft = {
356   .fnv_get = cnat_translation_get_node,
357   .fnv_last_lock = cnat_translation_last_lock_gone,
358   .fnv_back_walk = cnat_translation_back_walk_notify,
359 };
360
361 static clib_error_t *
362 cnat_translation_cli_add_del (vlib_main_t * vm,
363                               unformat_input_t * input,
364                               vlib_cli_command_t * cmd)
365 {
366   u32 del_index = INDEX_INVALID;
367   ip_protocol_t proto = IP_PROTOCOL_TCP;
368   cnat_endpoint_t vip;
369   u8 flags = CNAT_FLAG_EXCLUSIVE;
370   cnat_endpoint_tuple_t tmp, *paths = NULL, *path;
371
372   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
373     {
374       if (unformat (input, "add"))
375         del_index = INDEX_INVALID;
376       else if (unformat (input, "del %d", &del_index))
377         ;
378       else if (unformat (input, "proto %U", unformat_ip_protocol, &proto))
379         ;
380       else if (unformat (input, "vip %U", unformat_cnat_ep, &vip))
381         flags = CNAT_FLAG_EXCLUSIVE;
382       else if (unformat (input, "real %U", unformat_cnat_ep, &vip))
383         flags = 0;
384       else if (unformat (input, "to %U", unformat_cnat_ep_tuple, &tmp))
385         {
386           pool_get (paths, path);
387           clib_memcpy (path, &tmp, sizeof (cnat_endpoint_tuple_t));
388         }
389       else
390         return (clib_error_return (0, "unknown input '%U'",
391                                    format_unformat_error, input));
392     }
393
394   if (INDEX_INVALID == del_index)
395     cnat_translation_update (&vip, proto, paths, flags);
396   else
397     cnat_translation_delete (del_index);
398
399   pool_free (paths);
400   return (NULL);
401 }
402
403 /* *INDENT-OFF* */
404 VLIB_CLI_COMMAND (cnat_translation_cli_add_del_command, static) =
405 {
406   .path = "cnat translation",
407   .short_help = "cnat translation [add|del] proto [TCP|UDP] [vip|real] [ip] [port] [to [ip] [port]->[ip] [port]]",
408   .function = cnat_translation_cli_add_del,
409 };
410 /* *INDENT-ON* */
411
412 static clib_error_t *
413 cnat_translation_init (vlib_main_t * vm)
414 {
415   cnat_main_t *cm = &cnat_main;
416   cnat_translation_fib_node_type =
417     fib_node_register_new_type (&cnat_translation_vft);
418
419   clib_bihash_init_8_8 (&cnat_translation_db, "CNat translation DB",
420                         cm->translation_hash_buckets,
421                         cm->translation_hash_memory);
422
423   return (NULL);
424 }
425
426 VLIB_INIT_FUNCTION (cnat_translation_init);
427
428 /*
429  * fd.io coding-style-patch-verification: ON
430  *
431  * Local Variables:
432  * eval: (c-set-style "gnu")
433  * End:
434  */