nat: Include platform specific headers on FreeBSD
[vpp.git] / src / plugins / nat / nat64 / nat64_cli.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_table.h>
17 #include <nat/nat64/nat64.h>
18
19 #define NAT64_EXPECTED_ARGUMENT "expected required argument(s)"
20
21 static clib_error_t *
22 nat64_plugin_enable_disable_command_fn (vlib_main_t * vm,
23                                         unformat_input_t * input,
24                                         vlib_cli_command_t * cmd)
25 {
26   unformat_input_t _line_input, *line_input = &_line_input;
27   u8 enable = 0, is_set = 0;
28   clib_error_t *error = 0;
29   nat64_config_t c = { 0 };
30
31   if (!unformat_user (input, unformat_line_input, line_input))
32     return clib_error_return (0, NAT64_EXPECTED_ARGUMENT);
33
34   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
35     {
36       if (!is_set && unformat (line_input, "enable"))
37         {
38           unformat (line_input, "bib-buckets %u", &c.bib_buckets);
39           unformat (line_input, "bib-memory %u", &c.bib_memory_size);
40           unformat (line_input, "st-buckets %u", &c.st_buckets);
41           unformat (line_input, "st-memory %u", &c.st_memory_size);
42           enable = 1;
43         }
44       else if (!is_set && unformat (line_input, "disable"));
45       else
46         {
47           error = clib_error_return (0, "unknown input '%U'",
48                                      format_unformat_error, line_input);
49           goto done;
50         }
51       is_set = 1;
52     }
53
54   if (enable)
55     {
56       if (nat64_plugin_enable (c))
57         error = clib_error_return (0, "plugin enable failed");
58     }
59   else
60     {
61       if (nat64_plugin_disable ())
62         error = clib_error_return (0, "plugin disable failed");
63     }
64 done:
65   unformat_free (line_input);
66   return error;
67 }
68
69 static clib_error_t *
70 nat64_add_del_pool_addr_command_fn (vlib_main_t * vm,
71                                     unformat_input_t * input,
72                                     vlib_cli_command_t * cmd)
73 {
74   unformat_input_t _line_input, *line_input = &_line_input;
75   ip4_address_t start_addr, end_addr, this_addr;
76   u32 start_host_order, end_host_order;
77   int i, count, rv;
78   u32 vrf_id = ~0;
79   u8 is_add = 1;
80   clib_error_t *error = 0;
81
82   /* Get a line of input. */
83   if (!unformat_user (input, unformat_line_input, line_input))
84     return clib_error_return (0, NAT64_EXPECTED_ARGUMENT);
85
86   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
87     {
88       if (unformat (line_input, "%U - %U",
89                     unformat_ip4_address, &start_addr,
90                     unformat_ip4_address, &end_addr))
91         ;
92       else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
93         ;
94       else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
95         end_addr = start_addr;
96       else if (unformat (line_input, "del"))
97         is_add = 0;
98       else
99         {
100           error = clib_error_return (0, "unknown input '%U'",
101                                      format_unformat_error, line_input);
102           goto done;
103         }
104     }
105
106   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
107   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
108
109   if (end_host_order < start_host_order)
110     {
111       error = clib_error_return (0, "end address less than start address");
112       goto done;
113     }
114
115   count = (end_host_order - start_host_order) + 1;
116   this_addr = start_addr;
117
118   for (i = 0; i < count; i++)
119     {
120       rv = nat64_add_del_pool_addr (0, &this_addr, vrf_id, is_add);
121
122       switch (rv)
123         {
124         case VNET_API_ERROR_NO_SUCH_ENTRY:
125           error =
126             clib_error_return (0, "NAT64 pool address %U not exist.",
127                                format_ip4_address, &this_addr);
128           goto done;
129         case VNET_API_ERROR_VALUE_EXIST:
130           error =
131             clib_error_return (0, "NAT64 pool address %U exist.",
132                                format_ip4_address, &this_addr);
133           goto done;
134         default:
135           break;
136
137         }
138       increment_v4_address (&this_addr);
139     }
140
141 done:
142   unformat_free (line_input);
143
144   return error;
145 }
146
147 static int
148 nat64_cli_pool_walk (nat64_address_t * ap, void *ctx)
149 {
150   vlib_main_t *vm = ctx;
151
152   if (ap->fib_index != ~0)
153     {
154       fib_table_t *fib;
155       fib = fib_table_get (ap->fib_index, FIB_PROTOCOL_IP6);
156       if (!fib)
157         return -1;
158       vlib_cli_output (vm, " %U tenant VRF: %u", format_ip4_address,
159                        &ap->addr, fib->ft_table_id);
160     }
161   else
162     vlib_cli_output (vm, " %U", format_ip4_address, &ap->addr);
163
164 #define _(N, i, n, s) \
165   vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
166   foreach_nat_protocol
167 #undef _
168     return 0;
169 }
170
171 static clib_error_t *
172 nat64_show_pool_command_fn (vlib_main_t * vm,
173                             unformat_input_t * input,
174                             vlib_cli_command_t * cmd)
175 {
176   vlib_cli_output (vm, "NAT64 pool:");
177   nat64_pool_addr_walk (nat64_cli_pool_walk, vm);
178
179   return 0;
180 }
181
182 static clib_error_t *
183 nat64_interface_feature_command_fn (vlib_main_t * vm,
184                                     unformat_input_t *
185                                     input, vlib_cli_command_t * cmd)
186 {
187   unformat_input_t _line_input, *line_input = &_line_input;
188   vnet_main_t *vnm = vnet_get_main ();
189   clib_error_t *error = 0;
190   u32 sw_if_index;
191   u32 *inside_sw_if_indices = 0;
192   u32 *outside_sw_if_indices = 0;
193   u8 is_add = 1;
194   int i, rv;
195
196   /* Get a line of input. */
197   if (!unformat_user (input, unformat_line_input, line_input))
198     return clib_error_return (0, NAT64_EXPECTED_ARGUMENT);
199
200   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
201     {
202       if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
203                     vnm, &sw_if_index))
204         vec_add1 (inside_sw_if_indices, sw_if_index);
205       else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
206                          vnm, &sw_if_index))
207         vec_add1 (outside_sw_if_indices, sw_if_index);
208       else if (unformat (line_input, "del"))
209         is_add = 0;
210       else
211         {
212           error = clib_error_return (0, "unknown input '%U'",
213                                      format_unformat_error, line_input);
214           goto done;
215         }
216     }
217
218   if (vec_len (inside_sw_if_indices))
219     {
220       for (i = 0; i < vec_len (inside_sw_if_indices); i++)
221         {
222           sw_if_index = inside_sw_if_indices[i];
223           rv = nat64_interface_add_del (sw_if_index, 1, is_add);
224           switch (rv)
225             {
226             case VNET_API_ERROR_NO_SUCH_ENTRY:
227               error =
228                 clib_error_return (0, "%U NAT64 feature not enabled.",
229                                    format_vnet_sw_if_index_name, vnm,
230                                    sw_if_index);
231               goto done;
232             case VNET_API_ERROR_VALUE_EXIST:
233               error =
234                 clib_error_return (0, "%U NAT64 feature already enabled.",
235                                    format_vnet_sw_if_index_name, vnm,
236                                    vnm, sw_if_index);
237               goto done;
238             case VNET_API_ERROR_INVALID_VALUE:
239             case VNET_API_ERROR_INVALID_VALUE_2:
240               error =
241                 clib_error_return (0,
242                                    "%U NAT64 feature enable/disable failed.",
243                                    format_vnet_sw_if_index_name, vnm,
244                                    sw_if_index);
245               goto done;
246             default:
247               break;
248
249             }
250         }
251     }
252
253   if (vec_len (outside_sw_if_indices))
254     {
255       for (i = 0; i < vec_len (outside_sw_if_indices); i++)
256         {
257           sw_if_index = outside_sw_if_indices[i];
258           rv = nat64_interface_add_del (sw_if_index, 0, is_add);
259           switch (rv)
260             {
261             case VNET_API_ERROR_NO_SUCH_ENTRY:
262               error =
263                 clib_error_return (0, "%U NAT64 feature not enabled.",
264                                    format_vnet_sw_if_index_name, vnm,
265                                    sw_if_index);
266               goto done;
267             case VNET_API_ERROR_VALUE_EXIST:
268               error =
269                 clib_error_return (0, "%U NAT64 feature already enabled.",
270                                    format_vnet_sw_if_index_name, vnm,
271                                    sw_if_index);
272               goto done;
273             case VNET_API_ERROR_INVALID_VALUE:
274             case VNET_API_ERROR_INVALID_VALUE_2:
275               error =
276                 clib_error_return (0,
277                                    "%U NAT64 feature enable/disable failed.",
278                                    format_vnet_sw_if_index_name, vnm,
279                                    sw_if_index);
280               goto done;
281             default:
282               break;
283
284             }
285         }
286     }
287
288 done:
289   unformat_free (line_input);
290   vec_free (inside_sw_if_indices);
291   vec_free (outside_sw_if_indices);
292
293   return error;
294 }
295
296 static int
297 nat64_cli_interface_walk (nat64_interface_t * i, void *ctx)
298 {
299   vlib_main_t *vm = ctx;
300   vnet_main_t *vnm = vnet_get_main ();
301
302   vlib_cli_output (vm, " %U %s", format_vnet_sw_if_index_name, vnm,
303                    i->sw_if_index,
304                    (nat64_interface_is_inside (i)
305                     && nat64_interface_is_outside (i)) ? "in out" :
306                    nat64_interface_is_inside (i) ? "in" : "out");
307   return 0;
308 }
309
310 static clib_error_t *
311 nat64_show_interfaces_command_fn (vlib_main_t * vm,
312                                   unformat_input_t *
313                                   input, vlib_cli_command_t * cmd)
314 {
315   vlib_cli_output (vm, "NAT64 interfaces:");
316   nat64_interfaces_walk (nat64_cli_interface_walk, vm);
317
318   return 0;
319 }
320
321 static clib_error_t *
322 nat64_add_del_static_bib_command_fn (vlib_main_t *
323                                      vm,
324                                      unformat_input_t
325                                      * input, vlib_cli_command_t * cmd)
326 {
327   unformat_input_t _line_input, *line_input = &_line_input;
328   clib_error_t *error = 0;
329   u8 is_add = 1;
330   ip6_address_t in_addr;
331   ip4_address_t out_addr;
332   u32 in_port = 0;
333   u32 out_port = 0;
334   u32 vrf_id = 0, protocol;
335   nat_protocol_t proto = 0;
336   u8 p = 0;
337   int rv;
338
339   if (!unformat_user (input, unformat_line_input, line_input))
340     return clib_error_return (0, NAT64_EXPECTED_ARGUMENT);
341
342   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
343     {
344       if (unformat (line_input, "%U %u", unformat_ip6_address,
345                     &in_addr, &in_port))
346         ;
347       else if (unformat (line_input, "%U %u", unformat_ip4_address,
348                          &out_addr, &out_port))
349         ;
350       else if (unformat (line_input, "vrf %u", &vrf_id))
351         ;
352       else if (unformat (line_input, "%U", unformat_nat_protocol, &proto))
353         ;
354       else
355         if (unformat
356             (line_input, "%U %U %u", unformat_ip6_address, &in_addr,
357              unformat_ip4_address, &out_addr, &protocol))
358         p = (u8) protocol;
359       else if (unformat (line_input, "del"))
360         is_add = 0;
361       else
362         {
363           error = clib_error_return (0, "unknown input: '%U'",
364                                      format_unformat_error, line_input);
365           goto done;
366         }
367     }
368
369   if (!p)
370     {
371       if (!in_port)
372         {
373           error =
374             clib_error_return (0, "inside port and address  must be set");
375           goto done;
376         }
377
378       if (!out_port)
379         {
380           error =
381             clib_error_return (0, "outside port and address  must be set");
382           goto done;
383         }
384
385       p = nat_proto_to_ip_proto (proto);
386     }
387
388   rv =
389     nat64_add_del_static_bib_entry (&in_addr, &out_addr, (u16) in_port,
390                                     (u16) out_port, p, vrf_id, is_add);
391
392   switch (rv)
393     {
394     case VNET_API_ERROR_NO_SUCH_ENTRY:
395       error = clib_error_return (0, "NAT64 BIB entry not exist.");
396       goto done;
397     case VNET_API_ERROR_VALUE_EXIST:
398       error = clib_error_return (0, "NAT64 BIB entry exist.");
399       goto done;
400     case VNET_API_ERROR_UNSPECIFIED:
401       error = clib_error_return (0, "Crerate NAT64 BIB entry failed.");
402       goto done;
403     case VNET_API_ERROR_INVALID_VALUE:
404       error =
405         clib_error_return (0,
406                            "Outside address %U and port %u already in use.",
407                            format_ip4_address, &out_addr, out_port);
408       goto done;
409     case VNET_API_ERROR_INVALID_VALUE_2:
410       error = clib_error_return (0, "Invalid outside port.");
411     default:
412       break;
413     }
414
415 done:
416   unformat_free (line_input);
417
418   return error;
419 }
420
421 static int
422 nat64_cli_bib_walk (nat64_db_bib_entry_t * bibe, void *ctx)
423 {
424   vlib_main_t *vm = ctx;
425   fib_table_t *fib;
426
427   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
428   if (!fib)
429     return -1;
430
431   switch (bibe->proto)
432     {
433     case IP_PROTOCOL_ICMP:
434     case IP_PROTOCOL_TCP:
435     case IP_PROTOCOL_UDP:
436       vlib_cli_output (vm, " %U %u %U %u protocol %U vrf %u %s %u sessions",
437                        format_ip6_address, &bibe->in_addr,
438                        clib_net_to_host_u16 (bibe->in_port),
439                        format_ip4_address, &bibe->out_addr,
440                        clib_net_to_host_u16 (bibe->out_port),
441                        format_nat_protocol,
442                        ip_proto_to_nat_proto (bibe->proto), fib->ft_table_id,
443                        bibe->is_static ? "static" : "dynamic", bibe->ses_num);
444       break;
445     default:
446       vlib_cli_output (vm, " %U %U protocol %u vrf %u %s %u sessions",
447                        format_ip6_address, &bibe->in_addr,
448                        format_ip4_address, &bibe->out_addr,
449                        bibe->proto, fib->ft_table_id,
450                        bibe->is_static ? "static" : "dynamic", bibe->ses_num);
451     }
452   return 0;
453 }
454
455 static clib_error_t *
456 nat64_show_bib_command_fn (vlib_main_t * vm,
457                            unformat_input_t * input, vlib_cli_command_t * cmd)
458 {
459   nat64_main_t *nm = &nat64_main;
460   unformat_input_t _line_input, *line_input = &_line_input;
461   clib_error_t *error = 0;
462   u32 proto = NAT_PROTOCOL_OTHER;
463   u8 p = 255;
464   nat64_db_t *db;
465
466   if (!unformat_user (input, unformat_line_input, line_input))
467     return clib_error_return (0, NAT64_EXPECTED_ARGUMENT);
468
469   if (unformat (line_input, "%U", unformat_nat_protocol, &proto))
470     p = nat_proto_to_ip_proto (proto);
471   else if (unformat (line_input, "unknown"))
472     p = 0;
473   else if (unformat (line_input, "all"))
474     ;
475   else
476     {
477       error = clib_error_return (0, "unknown input: '%U'",
478                                  format_unformat_error, line_input);
479       goto done;
480     }
481
482   if (p == 255)
483     vlib_cli_output (vm, "NAT64 BIB entries:");
484   else
485     vlib_cli_output (vm, "NAT64 %U BIB entries:", format_nat_protocol, proto);
486
487   vec_foreach (db, nm->db)
488     nat64_db_bib_walk (db, p, nat64_cli_bib_walk, vm);
489
490 done:
491   unformat_free (line_input);
492
493   return error;
494 }
495
496 typedef struct nat64_cli_st_walk_ctx_t_
497 {
498   vlib_main_t *vm;
499   nat64_db_t *db;
500 } nat64_cli_st_walk_ctx_t;
501
502 static int
503 nat64_cli_st_walk (nat64_db_st_entry_t * ste, void *arg)
504 {
505   nat64_cli_st_walk_ctx_t *ctx = arg;
506   vlib_main_t *vm = ctx->vm;
507   nat64_db_bib_entry_t *bibe;
508   fib_table_t *fib;
509
510   bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
511   if (!bibe)
512     return -1;
513
514   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
515   if (!fib)
516     return -1;
517
518   u32 vrf_id = fib->ft_table_id;
519
520   if (ste->proto == IP_PROTOCOL_ICMP)
521     vlib_cli_output (vm, " %U %U %u %U %U %u protocol %U vrf %u",
522                      format_ip6_address, &bibe->in_addr,
523                      format_ip6_address, &ste->in_r_addr,
524                      clib_net_to_host_u16 (bibe->in_port),
525                      format_ip4_address, &bibe->out_addr,
526                      format_ip4_address, &ste->out_r_addr,
527                      clib_net_to_host_u16 (bibe->out_port),
528                      format_nat_protocol,
529                      ip_proto_to_nat_proto (bibe->proto), vrf_id);
530   else if (ste->proto == IP_PROTOCOL_TCP || ste->proto == IP_PROTOCOL_UDP)
531     vlib_cli_output (vm, " %U %u %U %u %U %u %U %u protcol %U vrf %u",
532                      format_ip6_address, &bibe->in_addr,
533                      clib_net_to_host_u16 (bibe->in_port),
534                      format_ip6_address, &ste->in_r_addr,
535                      clib_net_to_host_u16 (ste->r_port),
536                      format_ip4_address, &bibe->out_addr,
537                      clib_net_to_host_u16 (bibe->out_port),
538                      format_ip4_address, &ste->out_r_addr,
539                      clib_net_to_host_u16 (ste->r_port),
540                      format_nat_protocol,
541                      ip_proto_to_nat_proto (bibe->proto), vrf_id);
542   else
543     vlib_cli_output (vm, " %U %U %U %U protocol %u vrf %u",
544                      format_ip6_address, &bibe->in_addr,
545                      format_ip6_address, &ste->in_r_addr,
546                      format_ip4_address, &bibe->out_addr,
547                      format_ip4_address, &ste->out_r_addr,
548                      bibe->proto, vrf_id);
549
550   return 0;
551 }
552
553 static clib_error_t *
554 nat64_show_st_command_fn (vlib_main_t * vm,
555                           unformat_input_t * input, vlib_cli_command_t * cmd)
556 {
557   nat64_main_t *nm = &nat64_main;
558   unformat_input_t _line_input, *line_input = &_line_input;
559   clib_error_t *error = 0;
560   u32 proto = NAT_PROTOCOL_OTHER;
561   u8 p = 255;
562   nat64_db_t *db;
563   nat64_cli_st_walk_ctx_t ctx = {
564     .vm = vm,
565   };
566
567   if (!unformat_user (input, unformat_line_input, line_input))
568     return clib_error_return (0, NAT64_EXPECTED_ARGUMENT);
569
570   if (unformat (line_input, "%U", unformat_nat_protocol, &proto))
571     p = nat_proto_to_ip_proto (proto);
572   else if (unformat (line_input, "unknown"))
573     p = 0;
574   else if (unformat (line_input, "all"))
575     ;
576   else
577     {
578       error = clib_error_return (0, "unknown input: '%U'",
579                                  format_unformat_error, line_input);
580       goto done;
581     }
582
583   if (p == 255)
584     vlib_cli_output (vm, "NAT64 sessions:");
585   else
586     vlib_cli_output (vm, "NAT64 %U sessions:", format_nat_protocol, proto);
587   vec_foreach (db, nm->db)
588     {
589       ctx.db = db;
590       nat64_db_st_walk (db, p, nat64_cli_st_walk, &ctx);
591     }
592
593 done:
594   unformat_free (line_input);
595
596   return error;
597 }
598
599 static clib_error_t *
600 nat64_add_del_prefix_command_fn (vlib_main_t * vm, unformat_input_t * input,
601                                  vlib_cli_command_t * cmd)
602 {
603   nat64_main_t *nm = &nat64_main;
604   vnet_main_t *vnm = vnet_get_main ();
605   clib_error_t *error = 0;
606   unformat_input_t _line_input, *line_input = &_line_input;
607   u8 is_add = 1;
608   u32 vrf_id = 0, sw_if_index = ~0;
609   ip6_address_t prefix;
610   u32 plen = 0;
611   int rv;
612
613   if (!unformat_user (input, unformat_line_input, line_input))
614     return clib_error_return (0, NAT64_EXPECTED_ARGUMENT);
615
616   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
617     {
618       if (unformat
619           (line_input, "%U/%u", unformat_ip6_address, &prefix, &plen))
620         ;
621       else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
622         ;
623       else if (unformat (line_input, "del"))
624         is_add = 0;
625       else
626         if (unformat
627             (line_input, "interface %U", unformat_vnet_sw_interface, vnm,
628              &sw_if_index))
629         ;
630       else
631         {
632           error = clib_error_return (0, "unknown input: '%U'",
633                                      format_unformat_error, line_input);
634           goto done;
635         }
636     }
637
638   if (!plen)
639     {
640       error = clib_error_return (0, "NAT64 prefix must be set.");
641       goto done;
642     }
643
644   rv = nat64_add_del_prefix (&prefix, (u8) plen, vrf_id, is_add);
645
646   switch (rv)
647     {
648     case VNET_API_ERROR_NO_SUCH_ENTRY:
649       error = clib_error_return (0, "NAT64 prefix not exist.");
650       goto done;
651     case VNET_API_ERROR_INVALID_VALUE:
652       error = clib_error_return (0, "Invalid prefix length.");
653       goto done;
654     default:
655       break;
656     }
657
658   /*
659    * Add RX interface route, whenNAT isn't running on the real input
660    * interface
661    */
662   if (sw_if_index != ~0)
663     {
664       u32 fib_index;
665       fib_prefix_t fibpfx = {
666         .fp_len = plen,
667         .fp_proto = FIB_PROTOCOL_IP6,
668         .fp_addr = {
669                     .ip6 = prefix}
670       };
671
672       if (is_add)
673         {
674           fib_index =
675             fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
676                                                vrf_id, nm->fib_src_hi);
677           fib_table_entry_update_one_path (fib_index, &fibpfx,
678                                            nm->fib_src_hi,
679                                            FIB_ENTRY_FLAG_NONE,
680                                            DPO_PROTO_IP6, NULL,
681                                            sw_if_index, ~0, 0,
682                                            NULL, FIB_ROUTE_PATH_INTF_RX);
683         }
684       else
685         {
686           fib_index = fib_table_find (FIB_PROTOCOL_IP6, vrf_id);
687           fib_table_entry_path_remove (fib_index, &fibpfx,
688                                        nm->fib_src_hi,
689                                        DPO_PROTO_IP6, NULL,
690                                        sw_if_index, ~0, 1,
691                                        FIB_ROUTE_PATH_INTF_RX);
692           fib_table_unlock (fib_index, FIB_PROTOCOL_IP6, nm->fib_src_hi);
693         }
694     }
695
696 done:
697   unformat_free (line_input);
698
699   return error;
700 }
701
702 static int
703 nat64_cli_prefix_walk (nat64_prefix_t * p, void *ctx)
704 {
705   vlib_main_t *vm = ctx;
706
707   vlib_cli_output (vm, " %U/%u tenant-vrf %u",
708                    format_ip6_address, &p->prefix, p->plen, p->vrf_id);
709
710   return 0;
711 }
712
713 static clib_error_t *
714 nat64_show_prefix_command_fn (vlib_main_t * vm,
715                               unformat_input_t * input,
716                               vlib_cli_command_t * cmd)
717 {
718   vlib_cli_output (vm, "NAT64 prefix:");
719   nat64_prefix_walk (nat64_cli_prefix_walk, vm);
720
721   return 0;
722 }
723
724 static clib_error_t *
725 nat64_add_interface_address_command_fn (vlib_main_t * vm,
726                                         unformat_input_t * input,
727                                         vlib_cli_command_t * cmd)
728 {
729   vnet_main_t *vnm = vnet_get_main ();
730   unformat_input_t _line_input, *line_input = &_line_input;
731   u32 sw_if_index;
732   int rv;
733   int is_add = 1;
734   clib_error_t *error = 0;
735
736   /* Get a line of input. */
737   if (!unformat_user (input, unformat_line_input, line_input))
738     return clib_error_return (0, NAT64_EXPECTED_ARGUMENT);
739
740   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
741     {
742       if (unformat
743           (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index));
744       else if (unformat (line_input, "del"))
745         is_add = 0;
746       else
747         {
748           error = clib_error_return (0, "unknown input '%U'",
749                                      format_unformat_error, line_input);
750           goto done;
751         }
752     }
753
754   rv = nat64_add_interface_address (sw_if_index, is_add);
755
756   switch (rv)
757     {
758     case VNET_API_ERROR_NO_SUCH_ENTRY:
759       error = clib_error_return (0, "entry not exist");
760       break;
761     case VNET_API_ERROR_VALUE_EXIST:
762       error = clib_error_return (0, "entry exist");
763       break;
764     default:
765       break;
766     }
767
768 done:
769   unformat_free (line_input);
770
771   return error;
772 }
773
774 /*?
775  * @cliexpar
776  * @cliexstart{nat64 plugin}
777  * Enable/disable NAT64 plugin.
778  * To enable NAT64 plugin use:
779  *  vpp# nat64 plugin enable
780  * To enable NAT64 plugin and configure buckets/memory:
781  *  vpp# nat64 plugin enable bib-buckets <n> bib-memory <s> \
782  *    st-buckets <n> st-memory <s>
783  * To disable NAT64 plugin:
784  *  vpp# nat64 plugin disable
785  * @cliexend
786 ?*/
787 VLIB_CLI_COMMAND (nat64_plugin_enable_disable_command, static) =
788 {
789   .path = "nat64 plugin",
790   .short_help = "nat64 plugin <enable "
791                 "[bib-buckets <count>] [bib-memory <size>] "
792                 "[st-buckets <count>] [st-memory <size>] | disable>",
793   .function = nat64_plugin_enable_disable_command_fn,
794 };
795
796 /*?
797  * @cliexpar
798  * @cliexstart{nat64 add pool address}
799  * Add/delete NAT64 pool address.
800  * To add single NAT64 pool address use:
801  *  vpp# nat64 add pool address 10.1.1.10
802  * To add NAT64 pool address range use:
803  *  vpp# nat64 add pool address 10.1.1.2 - 10.1.1.5
804  * To add NAT64 pool address for specific tenant use:
805  *  vpp# nat64 add pool address 10.1.1.100 tenant-vrf 100
806  * @cliexend
807 ?*/
808 VLIB_CLI_COMMAND (nat64_add_pool_address_command, static) = {
809   .path = "nat64 add pool address",
810   .short_help = "nat64 add pool address <ip4-range-start> [- <ip4-range-end>] "
811                 "[tenant-vrf <vrf-id>] [del]",
812   .function = nat64_add_del_pool_addr_command_fn,
813 };
814
815 /*?
816  * @cliexpar
817  * @cliexstart{show nat64 pool}
818  * Show NAT64 pool.
819  *  vpp# show nat64 pool
820  *  NAT64 pool:
821  *   10.1.1.3 tenant VRF: 0
822  *   10.1.1.10 tenant VRF: 10
823  * @cliexend
824 ?*/
825 VLIB_CLI_COMMAND (show_nat64_pool_command, static) = {
826   .path = "show nat64 pool",
827   .short_help = "show nat64 pool",
828   .function = nat64_show_pool_command_fn,
829 };
830
831 /*?
832  * @cliexpar
833  * @cliexstart{set interface nat64}
834  * Enable/disable NAT64 feature on the interface.
835  * To enable NAT64 feature with local (IPv6) network interface
836  * GigabitEthernet0/8/0 and external (IPv4) network interface
837  * GigabitEthernet0/a/0 use:
838  *  vpp# set interface nat64 in GigabitEthernet0/8/0 out GigabitEthernet0/a/0
839  * @cliexend
840 ?*/
841 VLIB_CLI_COMMAND (set_interface_nat64_command, static) = {
842   .path = "set interface nat64",
843   .short_help = "set interface nat64 in|out <intfc> [del]",
844   .function = nat64_interface_feature_command_fn,
845 };
846
847 /*?
848  * @cliexpar
849  * @cliexstart{show nat64 interfaces}
850  * Show interfaces with NAT64 feature.
851  * To show interfaces with NAT64 feature use:
852  *  vpp# show nat64 interfaces
853  *  NAT64 interfaces:
854  *   GigabitEthernet0/8/0 in
855  *   GigabitEthernet0/a/0 out
856  * @cliexend
857 ?*/
858 VLIB_CLI_COMMAND (show_nat64_interfaces_command, static) = {
859   .path = "show nat64 interfaces",
860   .short_help = "show nat64 interfaces",
861   .function = nat64_show_interfaces_command_fn,
862 };
863
864 /*?
865  * @cliexpar
866  * @cliexstart{nat64 add static bib}
867  * Add/delete NAT64 static BIB entry.
868  * To create NAT64 satatic BIB entry use:
869  *  vpp# nat64 add static bib 2001:db8:c000:221:: 1234 10.1.1.3 5678 tcp
870  *  vpp# nat64 add static bib 2001:db8:c000:221:: 1234 10.1.1.3 5678 udp vrf 10
871  * @cliexend
872 ?*/
873 VLIB_CLI_COMMAND (nat64_add_del_static_bib_command, static) = {
874   .path = "nat64 add static bib",
875   .short_help = "nat64 add static bib <ip6-addr> <port> <ip4-addr> <port> "
876                 "tcp|udp|icmp [vfr <table-id>] [del]",
877   .function = nat64_add_del_static_bib_command_fn,
878 };
879
880 /*?
881  * @cliexpar
882  * @cliexstart{show nat64 bib}
883  * Show NAT64 BIB entries.
884  * To show NAT64 TCP BIB entries use:
885  *  vpp# show nat64 bib tcp
886  *  NAT64 tcp BIB:
887  *   fd01:1::2 6303 10.0.0.3 62303 tcp vrf 0 dynamic 1 sessions
888  *   2001:db8:c000:221:: 1234 10.1.1.3 5678 tcp vrf 0 static 2 sessions
889  * To show NAT64 UDP BIB entries use:
890  *  vpp# show nat64 bib udp
891  *  NAT64 udp BIB:
892  *   fd01:1::2 6304 10.0.0.3 10546 udp vrf 0 dynamic 10 sessions
893  *   2001:db8:c000:221:: 1234 10.1.1.3 5678 udp vrf 10 static 0 sessions
894  * To show NAT64 ICMP BIB entries use:
895  *  vpp# show nat64 bib icmp
896  *  NAT64 icmp BIB:
897  *   fd01:1::2 6305 10.0.0.3 63209 icmp vrf 10 dynamic 1 sessions
898  * @cliexend
899 ?*/
900 VLIB_CLI_COMMAND (show_nat64_bib_command, static) = {
901   .path = "show nat64 bib",
902   .short_help = "show nat64 bib all|tcp|udp|icmp|unknown",
903   .function = nat64_show_bib_command_fn,
904 };
905
906 /*?
907  * @cliexpar
908  * @cliexstart{show nat64 session table}
909  * Show NAT64 session table.
910  * To show NAT64 TCP session table use:
911  *  vpp# show nat64 session table tcp
912  *  NAT64 tcp session table:
913  *   fd01:1::2 6303 64:ff9b::ac10:202 20 10.0.0.3 62303 172.16.2.2 20 tcp vrf 0
914  *   fd01:3::2 6303 64:ff9b::ac10:202 20 10.0.10.3 21300 172.16.2.2 20 tcp vrf 10
915  * To show NAT64 UDP session table use:
916  * #vpp show nat64 session table udp
917  * NAT64 udp session table:
918  *  fd01:1::2 6304 64:ff9b::ac10:202 20 10.0.0.3 10546 172.16.2.2 20 udp vrf 0
919  *  fd01:3::2 6304 64:ff9b::ac10:202 20 10.0.10.3 58627 172.16.2.2 20 udp vrf 10
920  *  fd01:1::2 1235 64:ff9b::a00:3 4023 10.0.0.3 24488 10.0.0.3 4023 udp vrf 0
921  *  fd01:1::3 23 64:ff9b::a00:3 24488 10.0.0.3 4023 10.0.0.3 24488 udp vrf 0
922  * To show NAT64 ICMP session table use:
923  * #vpp show nat64 session table icmp
924  * NAT64 icmp session table:
925  *  fd01:1::2 64:ff9b::ac10:202 6305 10.0.0.3 172.16.2.2 63209 icmp vrf 0
926  * @cliexend
927 ?*/
928 VLIB_CLI_COMMAND (show_nat64_st_command, static) = {
929   .path = "show nat64 session table",
930   .short_help = "show nat64 session table all|tcp|udp|icmp|unknown",
931   .function = nat64_show_st_command_fn,
932 };
933
934 /*?
935  * @cliexpar
936  * @cliexstart{nat64 add prefix}
937  * Set NAT64 prefix for generating IPv6 representations of IPv4 addresses.
938  * To set NAT64 global prefix use:
939  *  vpp# nat64 add prefix 2001:db8::/32
940  * To set NAT64 prefix for specific tenant use:
941  *  vpp# nat64 add prefix 2001:db8:122:300::/56 tenant-vrf 10
942  * @cliexend
943 ?*/
944 VLIB_CLI_COMMAND (nat64_add_del_prefix_command, static) = {
945   .path = "nat64 add prefix",
946   .short_help = "nat64 add prefix <ip6-prefix>/<plen> [tenant-vrf <vrf-id>] "
947                 "[del] [interface <interface]",
948   .function = nat64_add_del_prefix_command_fn,
949 };
950
951 /*?
952  * @cliexpar
953  * @cliexstart{show nat64 prefix}
954  * Show NAT64 prefix.
955  * To show NAT64 prefix use:
956  *  vpp# show nat64 prefix
957  *  NAT64 prefix:
958  *   2001:db8::/32 tenant-vrf 0
959  *   2001:db8:122:300::/56 tenant-vrf 10
960  * @cliexend
961 ?*/
962 VLIB_CLI_COMMAND (show_nat64_prefix_command, static) = {
963   .path = "show nat64 prefix",
964   .short_help = "show nat64 prefix",
965   .function = nat64_show_prefix_command_fn,
966 };
967
968 /*?
969  * @cliexpar
970  * @cliexstart{nat64 add interface address}
971  * Add/delete NAT64 pool address from specific (DHCP addressed) interface.
972  * To add NAT64 pool address from specific interface use:
973  *  vpp# nat64 add interface address GigabitEthernet0/8/0
974  * @cliexend
975 ?*/
976 VLIB_CLI_COMMAND (nat64_add_interface_address_command, static) = {
977     .path = "nat64 add interface address",
978     .short_help = "nat64 add interface address <interface> [del]",
979     .function = nat64_add_interface_address_command_fn,
980 };
981
982 /*
983  * fd.io coding-style-patch-verification: ON
984  *
985  * Local Variables:
986  * eval: (c-set-style "gnu")
987  * End:
988  */