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