cnat: Destination based NAT
[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   /* do we know of this ep's vip */
151   cci = cnat_client_add (&vip->ce_ip, flags);
152   cc = cnat_client_get (cci);
153
154   ct = cnat_find_translation (cc->parent_cci, vip->ce_port, proto);
155
156   if (NULL == ct)
157     {
158       pool_get_zero (cnat_translation_pool, ct);
159
160       clib_memcpy (&ct->ct_vip, vip, sizeof (*vip));
161       ct->ct_proto = proto;
162       ct->ct_cci = cci;
163       ct->index = ct - cnat_translation_pool;
164
165       cnat_add_translation_to_db (cci, ct->ct_vip.ce_port, ct->ct_proto,
166                                   ct->index);
167       cnat_client_translation_added (cci);
168
169       vlib_validate_combined_counter (&cnat_translation_counters, ct->index);
170       vlib_zero_combined_counter (&cnat_translation_counters, ct->index);
171     }
172   ct->flags = flags;
173
174   vec_foreach (trk, ct->ct_paths)
175   {
176     cnat_tracker_release (trk);
177   }
178
179   vec_reset_length (ct->ct_paths);
180
181   vec_foreach (path, paths)
182   {
183     vec_add2 (ct->ct_paths, trk, 1);
184
185     cnat_tracker_track (ct->index, path, trk);
186   }
187
188   cnat_translation_stack (ct);
189
190   return (ct->index);
191 }
192
193 void
194 cnat_translation_walk (cnat_translation_walk_cb_t cb, void *ctx)
195 {
196   u32 api;
197
198   /* *INDENT-OFF* */
199   pool_foreach_index(api, cnat_translation_pool,
200   ({
201     if (!cb(api, ctx))
202       break;
203   }));
204   /* *INDENT-ON* */
205 }
206
207 static u8 *
208 format_cnat_ep_trk (u8 * s, va_list * args)
209 {
210   cnat_ep_trk_t *ck = va_arg (*args, cnat_ep_trk_t *);
211   u32 indent = va_arg (*args, u32);
212
213   s = format (s, "%U->%U", format_cnat_endpoint, &ck->ct_ep[VLIB_RX],
214               format_cnat_endpoint, &ck->ct_ep[VLIB_TX]);
215   s = format (s, "\n%Ufib-entry:%d", format_white_space, indent, ck->ct_fei);
216   s = format (s, "\n%U%U",
217               format_white_space, indent, format_dpo_id, &ck->ct_dpo, 6);
218
219   return (s);
220 }
221
222 u8 *
223 format_cnat_translation (u8 * s, va_list * args)
224 {
225   cnat_translation_t *ct = va_arg (*args, cnat_translation_t *);
226   cnat_ep_trk_t *ck;
227
228   s = format (s, "[%d] ", ct->index);
229   s = format (s, "%U %U", format_cnat_endpoint, &ct->ct_vip,
230               format_ip_protocol, ct->ct_proto);
231
232   vec_foreach (ck, ct->ct_paths)
233     s = format (s, "\n%U", format_cnat_ep_trk, ck, 2);
234
235   /* If printing a trace, the LB object might be deleted */
236   if (!pool_is_free_index (load_balance_pool, ct->ct_lb.dpoi_index))
237     {
238       s = format (s, "\n via:");
239       s = format (s, "\n%U%U",
240                   format_white_space, 2, format_dpo_id, &ct->ct_lb, 2);
241     }
242
243   return (s);
244 }
245
246 static clib_error_t *
247 cnat_translation_show (vlib_main_t * vm,
248                        unformat_input_t * input, vlib_cli_command_t * cmd)
249 {
250   index_t cti;
251   cnat_translation_t *ct;
252
253   cti = INDEX_INVALID;
254
255   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
256     {
257       if (unformat (input, "%d", &cti))
258         ;
259       else
260         return (clib_error_return (0, "unknown input '%U'",
261                                    format_unformat_error, input));
262     }
263
264   if (INDEX_INVALID == cti)
265     {
266       /* *INDENT-OFF* */
267       pool_foreach_index(cti, cnat_translation_pool,
268       ({
269         ct = pool_elt_at_index (cnat_translation_pool, cti);
270         vlib_cli_output(vm, "%U", format_cnat_translation, ct);
271       }));
272       /* *INDENT-ON* */
273     }
274   else
275     {
276       vlib_cli_output (vm, "Invalid policy ID:%d", cti);
277     }
278
279   return (NULL);
280 }
281
282 int
283 cnat_translation_purge (void)
284 {
285   /* purge all the translations */
286   index_t tri, *trp, *trs = NULL;
287
288   /* *INDENT-OFF* */
289   pool_foreach_index(tri, cnat_translation_pool,
290   ({
291     vec_add1(trs, tri);
292   }));
293   /* *INDENT-ON* */
294
295   vec_foreach (trp, trs) cnat_translation_delete (*trp);
296
297   ASSERT (0 == pool_elts (cnat_translation_pool));
298
299   vec_free (trs);
300
301   return (0);
302 }
303
304 /* *INDENT-OFF* */
305 VLIB_CLI_COMMAND (cnat_translation_show_cmd_node, static) = {
306   .path = "show cnat translation",
307   .function = cnat_translation_show,
308   .short_help = "show cnat translation <VIP>",
309   .is_mp_safe = 1,
310 };
311 /* *INDENT-ON* */
312
313 static fib_node_t *
314 cnat_translation_get_node (fib_node_index_t index)
315 {
316   cnat_translation_t *ct = cnat_translation_get (index);
317   return (&(ct->ct_node));
318 }
319
320 static cnat_translation_t *
321 cnat_translation_get_from_node (fib_node_t * node)
322 {
323   return ((cnat_translation_t *) (((char *) node) -
324                                   STRUCT_OFFSET_OF (cnat_translation_t,
325                                                     ct_node)));
326 }
327
328 static void
329 cnat_translation_last_lock_gone (fib_node_t * node)
330 {
331  /**/}
332
333 /*
334  * A back walk has reached this ABF policy
335  */
336 static fib_node_back_walk_rc_t
337 cnat_translation_back_walk_notify (fib_node_t * node,
338                                    fib_node_back_walk_ctx_t * ctx)
339 {
340   /*
341    * re-stack the fmask on the n-eos of the via
342    */
343   cnat_translation_t *ct = cnat_translation_get_from_node (node);
344
345   cnat_translation_stack (ct);
346
347   return (FIB_NODE_BACK_WALK_CONTINUE);
348 }
349
350 /*
351  * The translation's graph node virtual function table
352  */
353 static const fib_node_vft_t cnat_translation_vft = {
354   .fnv_get = cnat_translation_get_node,
355   .fnv_last_lock = cnat_translation_last_lock_gone,
356   .fnv_back_walk = cnat_translation_back_walk_notify,
357 };
358
359 static clib_error_t *
360 cnat_translation_cli_add_del (vlib_main_t * vm,
361                               unformat_input_t * input,
362                               vlib_cli_command_t * cmd)
363 {
364   u32 del_index = INDEX_INVALID;
365   ip_protocol_t proto = IP_PROTOCOL_TCP;
366   cnat_endpoint_t vip;
367   u8 flags = CNAT_FLAG_EXCLUSIVE;
368   cnat_endpoint_tuple_t tmp, *paths = NULL, *path;
369
370   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
371     {
372       if (unformat (input, "add"))
373         del_index = INDEX_INVALID;
374       else if (unformat (input, "del %d", &del_index))
375         ;
376       else if (unformat (input, "proto %U", unformat_ip_protocol, &proto))
377         ;
378       else if (unformat (input, "vip %U", unformat_cnat_ep, &vip))
379         flags = CNAT_FLAG_EXCLUSIVE;
380       else if (unformat (input, "real %U", unformat_cnat_ep, &vip))
381         flags = 0;
382       else if (unformat (input, "to %U", unformat_cnat_ep_tuple, &tmp))
383         {
384           pool_get (paths, path);
385           clib_memcpy (path, &tmp, sizeof (cnat_endpoint_tuple_t));
386         }
387       else
388         return (clib_error_return (0, "unknown input '%U'",
389                                    format_unformat_error, input));
390     }
391
392   if (INDEX_INVALID == del_index)
393     cnat_translation_update (&vip, proto, paths, flags);
394   else
395     cnat_translation_delete (del_index);
396
397   pool_free (paths);
398   return (NULL);
399 }
400
401 /* *INDENT-OFF* */
402 VLIB_CLI_COMMAND (cnat_translation_cli_add_del_command, static) =
403 {
404   .path = "cnat translation",
405   .short_help = "cnat translation [add|del] proto [TCP|UDP] [vip|real] [ip] [port] [to [ip] [port]->[ip] [port]]",
406   .function = cnat_translation_cli_add_del,
407 };
408 /* *INDENT-ON* */
409
410 static clib_error_t *
411 cnat_translation_init (vlib_main_t * vm)
412 {
413   cnat_main_t *cm = &cnat_main;
414   cnat_translation_fib_node_type =
415     fib_node_register_new_type (&cnat_translation_vft);
416
417   clib_bihash_init_8_8 (&cnat_translation_db, "CNat translation DB",
418                         cm->translation_hash_buckets,
419                         cm->translation_hash_memory);
420
421   return (NULL);
422 }
423
424 VLIB_INIT_FUNCTION (cnat_translation_init);
425
426 /*
427  * fd.io coding-style-patch-verification: ON
428  *
429  * Local Variables:
430  * eval: (c-set-style "gnu")
431  * End:
432  */