NAT64: Input feature arc on virtual interface via interface RX DPO.
[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                    (nat_interface_is_inside (i)
272                     && nat_interface_is_outside (i)) ? "in out" :
273                    nat_interface_is_inside (i) ? "in" : "out");
274   return 0;
275 }
276
277 static clib_error_t *
278 nat64_show_interfaces_command_fn (vlib_main_t * vm,
279                                   unformat_input_t *
280                                   input, vlib_cli_command_t * cmd)
281 {
282   nat64_main_t *nm = &nat64_main;
283
284   if (nm->is_disabled)
285     return clib_error_return (0,
286                               "NAT64 disabled, multi thread not supported");
287
288   vlib_cli_output (vm, "NAT64 interfaces:");
289   nat64_interfaces_walk (nat64_cli_interface_walk, vm);
290
291   return 0;
292 }
293
294 static clib_error_t *
295 nat64_add_del_static_bib_command_fn (vlib_main_t *
296                                      vm,
297                                      unformat_input_t
298                                      * input, vlib_cli_command_t * cmd)
299 {
300   nat64_main_t *nm = &nat64_main;
301   unformat_input_t _line_input, *line_input = &_line_input;
302   clib_error_t *error = 0;
303   u8 is_add = 1;
304   ip6_address_t in_addr;
305   ip4_address_t out_addr;
306   u32 in_port = 0;
307   u32 out_port = 0;
308   u32 vrf_id = 0, protocol;
309   snat_protocol_t proto = 0;
310   u8 p = 0;
311   int rv;
312
313   if (nm->is_disabled)
314     return clib_error_return (0,
315                               "NAT64 disabled, multi thread not supported");
316
317   if (!unformat_user (input, unformat_line_input, line_input))
318     return 0;
319
320   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
321     {
322       if (unformat (line_input, "%U %u", unformat_ip6_address,
323                     &in_addr, &in_port))
324         ;
325       else if (unformat (line_input, "%U %u", unformat_ip4_address,
326                          &out_addr, &out_port))
327         ;
328       else if (unformat (line_input, "vrf %u", &vrf_id))
329         ;
330       else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
331         ;
332       else
333         if (unformat
334             (line_input, "%U %U %u", unformat_ip6_address, &in_addr,
335              unformat_ip4_address, &out_addr, &protocol))
336         p = (u8) protocol;
337       else if (unformat (line_input, "del"))
338         is_add = 0;
339       else
340         {
341           error = clib_error_return (0, "unknown input: '%U'",
342                                      format_unformat_error, line_input);
343           goto done;
344         }
345     }
346
347   if (!p)
348     {
349       if (!in_port)
350         {
351           error =
352             clib_error_return (0, "inside port and address  must be set");
353           goto done;
354         }
355
356       if (!out_port)
357         {
358           error =
359             clib_error_return (0, "outside port and address  must be set");
360           goto done;
361         }
362
363       p = snat_proto_to_ip_proto (proto);
364     }
365
366   rv =
367     nat64_add_del_static_bib_entry (&in_addr, &out_addr, (u16) in_port,
368                                     (u16) out_port, p, vrf_id, is_add);
369
370   switch (rv)
371     {
372     case VNET_API_ERROR_NO_SUCH_ENTRY:
373       error = clib_error_return (0, "NAT64 BIB entry not exist.");
374       goto done;
375     case VNET_API_ERROR_VALUE_EXIST:
376       error = clib_error_return (0, "NAT64 BIB entry exist.");
377       goto done;
378     case VNET_API_ERROR_UNSPECIFIED:
379       error = clib_error_return (0, "Crerate NAT64 BIB entry failed.");
380       goto done;
381     case VNET_API_ERROR_INVALID_VALUE:
382       error =
383         clib_error_return (0, "Outside addres %U and port %u already in use.",
384                            format_ip4_address, &out_addr, out_port);
385       goto done;
386     default:
387       break;
388     }
389
390 done:
391   unformat_free (line_input);
392
393   return error;
394 }
395
396 static int
397 nat64_cli_bib_walk (nat64_db_bib_entry_t * bibe, void *ctx)
398 {
399   vlib_main_t *vm = ctx;
400   fib_table_t *fib;
401
402   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
403   if (!fib)
404     return -1;
405
406   switch (bibe->proto)
407     {
408     case IP_PROTOCOL_ICMP:
409     case IP_PROTOCOL_TCP:
410     case IP_PROTOCOL_UDP:
411       vlib_cli_output (vm, " %U %u %U %u protocol %U vrf %u %s %u sessions",
412                        format_ip6_address, &bibe->in_addr,
413                        clib_net_to_host_u16 (bibe->in_port),
414                        format_ip4_address, &bibe->out_addr,
415                        clib_net_to_host_u16 (bibe->out_port),
416                        format_snat_protocol,
417                        ip_proto_to_snat_proto (bibe->proto), fib->ft_table_id,
418                        bibe->is_static ? "static" : "dynamic", bibe->ses_num);
419       break;
420     default:
421       vlib_cli_output (vm, " %U %U protocol %u vrf %u %s %u sessions",
422                        format_ip6_address, &bibe->in_addr,
423                        format_ip4_address, &bibe->out_addr,
424                        bibe->proto, fib->ft_table_id,
425                        bibe->is_static ? "static" : "dynamic", bibe->ses_num);
426     }
427   return 0;
428 }
429
430 static clib_error_t *
431 nat64_show_bib_command_fn (vlib_main_t * vm,
432                            unformat_input_t * input, vlib_cli_command_t * cmd)
433 {
434   nat64_main_t *nm = &nat64_main;
435   unformat_input_t _line_input, *line_input = &_line_input;
436   clib_error_t *error = 0;
437   u32 proto = ~0;
438   u8 p = 255;
439
440   if (nm->is_disabled)
441     return clib_error_return (0,
442                               "NAT64 disabled, multi thread not supported");
443
444   if (!unformat_user (input, unformat_line_input, line_input))
445     return 0;
446
447   if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
448     p = snat_proto_to_ip_proto (proto);
449   else if (unformat (line_input, "unknown"))
450     p = 0;
451   else if (unformat (line_input, "all"))
452     ;
453   else
454     {
455       error = clib_error_return (0, "unknown input: '%U'",
456                                  format_unformat_error, line_input);
457       goto done;
458     }
459
460   if (p == 255)
461     vlib_cli_output (vm, "NAT64 BIB entries:");
462   else
463     vlib_cli_output (vm, "NAT64 %U BIB entries:", format_snat_protocol,
464                      proto);
465   nat64_db_bib_walk (&nm->db, p, nat64_cli_bib_walk, vm);
466
467 done:
468   unformat_free (line_input);
469
470   return error;
471 }
472
473 static clib_error_t *
474 nat64_set_timeouts_command_fn (vlib_main_t * vm, unformat_input_t * input,
475                                vlib_cli_command_t * cmd)
476 {
477   nat64_main_t *nm = &nat64_main;
478   unformat_input_t _line_input, *line_input = &_line_input;
479   clib_error_t *error = 0;
480   u32 timeout, tcp_trans, tcp_est, tcp_incoming_syn;
481
482   tcp_trans = nat64_get_tcp_trans_timeout ();
483   tcp_est = nat64_get_tcp_est_timeout ();
484   tcp_incoming_syn = nat64_get_tcp_incoming_syn_timeout ();
485
486   if (nm->is_disabled)
487     return clib_error_return (0,
488                               "NAT64 disabled, multi thread not supported");
489
490   if (!unformat_user (input, unformat_line_input, line_input))
491     return 0;
492
493   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
494     {
495       if (unformat (line_input, "udp %u", &timeout))
496         {
497           if (nat64_set_udp_timeout (timeout))
498             {
499               error = clib_error_return (0, "Invalid UDP timeout value");
500               goto done;
501             }
502         }
503       else if (unformat (line_input, "icmp %u", &timeout))
504         {
505           if (nat64_set_icmp_timeout (timeout))
506             {
507               error = clib_error_return (0, "Invalid ICMP timeout value");
508               goto done;
509             }
510         }
511       else if (unformat (line_input, "tcp-trans %u", &tcp_trans))
512         {
513           if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
514             {
515               error =
516                 clib_error_return (0,
517                                    "Invalid TCP transitory timeouts value");
518               goto done;
519             }
520         }
521       else if (unformat (line_input, "tcp-est %u", &tcp_est))
522         {
523           if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
524             {
525               error =
526                 clib_error_return (0,
527                                    "Invalid TCP established timeouts value");
528               goto done;
529             }
530         }
531       else
532         if (unformat (line_input, "tcp-incoming-syn %u", &tcp_incoming_syn))
533         {
534           if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
535             {
536               error =
537                 clib_error_return (0,
538                                    "Invalid TCP incoming SYN timeouts value");
539               goto done;
540             }
541         }
542       else if (unformat (line_input, "reset"))
543         {
544           nat64_set_udp_timeout (0);
545           nat64_set_icmp_timeout (0);
546           nat64_set_tcp_timeouts (0, 0, 0);
547         }
548       else
549         {
550           error = clib_error_return (0, "unknown input '%U'",
551                                      format_unformat_error, line_input);
552           goto done;
553         }
554     }
555
556 done:
557   unformat_free (line_input);
558
559   return error;
560 }
561
562 static clib_error_t *
563 nat64_show_timeouts_command_fn (vlib_main_t * vm, unformat_input_t * input,
564                                 vlib_cli_command_t * cmd)
565 {
566   nat64_main_t *nm = &nat64_main;
567
568   if (nm->is_disabled)
569     return clib_error_return (0,
570                               "NAT64 disabled, multi thread not supported");
571
572   vlib_cli_output (vm, "NAT64 session timeouts:");
573   vlib_cli_output (vm, " UDP %usec", nat64_get_udp_timeout ());
574   vlib_cli_output (vm, " ICMP %usec", nat64_get_icmp_timeout ());
575   vlib_cli_output (vm, " TCP transitory %usec",
576                    nat64_get_tcp_trans_timeout ());
577   vlib_cli_output (vm, " TCP established %usec",
578                    nat64_get_tcp_est_timeout ());
579   vlib_cli_output (vm, " TCP incoming SYN %usec",
580                    nat64_get_tcp_incoming_syn_timeout ());
581
582   return 0;
583 }
584
585 static int
586 nat64_cli_st_walk (nat64_db_st_entry_t * ste, void *ctx)
587 {
588   vlib_main_t *vm = ctx;
589   nat64_main_t *nm = &nat64_main;
590   nat64_db_bib_entry_t *bibe;
591   fib_table_t *fib;
592
593   bibe = nat64_db_bib_entry_by_index (&nm->db, ste->proto, ste->bibe_index);
594   if (!bibe)
595     return -1;
596
597   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
598   if (!fib)
599     return -1;
600
601   u32 vrf_id = fib->ft_table_id;
602
603   if (ste->proto == IP_PROTOCOL_ICMP)
604     vlib_cli_output (vm, " %U %U %u %U %U %u protocol %U vrf %u",
605                      format_ip6_address, &bibe->in_addr,
606                      format_ip6_address, &ste->in_r_addr,
607                      clib_net_to_host_u16 (bibe->in_port),
608                      format_ip4_address, &bibe->out_addr,
609                      format_ip4_address, &ste->out_r_addr,
610                      clib_net_to_host_u16 (bibe->out_port),
611                      format_snat_protocol,
612                      ip_proto_to_snat_proto (bibe->proto), vrf_id);
613   else if (ste->proto == IP_PROTOCOL_TCP || ste->proto == IP_PROTOCOL_UDP)
614     vlib_cli_output (vm, " %U %u %U %u %U %u %U %u protcol %U vrf %u",
615                      format_ip6_address, &bibe->in_addr,
616                      clib_net_to_host_u16 (bibe->in_port),
617                      format_ip6_address, &ste->in_r_addr,
618                      clib_net_to_host_u16 (ste->r_port),
619                      format_ip4_address, &bibe->out_addr,
620                      clib_net_to_host_u16 (bibe->out_port),
621                      format_ip4_address, &ste->out_r_addr,
622                      clib_net_to_host_u16 (ste->r_port),
623                      format_snat_protocol,
624                      ip_proto_to_snat_proto (bibe->proto), vrf_id);
625   else
626     vlib_cli_output (vm, " %U %U %U %U protocol %u vrf %u",
627                      format_ip6_address, &bibe->in_addr,
628                      format_ip6_address, &ste->in_r_addr,
629                      format_ip4_address, &bibe->out_addr,
630                      format_ip4_address, &ste->out_r_addr,
631                      bibe->proto, vrf_id);
632
633   return 0;
634 }
635
636 static clib_error_t *
637 nat64_show_st_command_fn (vlib_main_t * vm,
638                           unformat_input_t * input, vlib_cli_command_t * cmd)
639 {
640   nat64_main_t *nm = &nat64_main;
641   unformat_input_t _line_input, *line_input = &_line_input;
642   clib_error_t *error = 0;
643   u32 proto = ~0;
644   u8 p = 255;
645
646   if (nm->is_disabled)
647     return clib_error_return (0,
648                               "NAT64 disabled, multi thread not supported");
649
650   if (!unformat_user (input, unformat_line_input, line_input))
651     return 0;
652
653   if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
654     p = snat_proto_to_ip_proto (proto);
655   else if (unformat (line_input, "unknown"))
656     p = 0;
657   else if (unformat (line_input, "all"))
658     ;
659   else
660     {
661       error = clib_error_return (0, "unknown input: '%U'",
662                                  format_unformat_error, line_input);
663       goto done;
664     }
665
666   if (p == 255)
667     vlib_cli_output (vm, "NAT64 sessions:");
668   else
669     vlib_cli_output (vm, "NAT64 %U sessions:", format_snat_protocol, proto);
670   nat64_db_st_walk (&nm->db, p, nat64_cli_st_walk, vm);
671
672 done:
673   unformat_free (line_input);
674
675   return error;
676 }
677
678 static clib_error_t *
679 nat64_add_del_prefix_command_fn (vlib_main_t * vm, unformat_input_t * input,
680                                  vlib_cli_command_t * cmd)
681 {
682   nat64_main_t *nm = &nat64_main;
683   vnet_main_t *vnm = vnet_get_main ();
684   clib_error_t *error = 0;
685   unformat_input_t _line_input, *line_input = &_line_input;
686   u8 is_add = 1;
687   u32 vrf_id = 0, sw_if_index = ~0;
688   ip6_address_t prefix;
689   u32 plen = 0;
690   int rv;
691
692   if (nm->is_disabled)
693     return clib_error_return (0,
694                               "NAT64 disabled, multi thread not supported");
695
696   if (!unformat_user (input, unformat_line_input, line_input))
697     return 0;
698
699   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
700     {
701       if (unformat
702           (line_input, "%U/%u", unformat_ip6_address, &prefix, &plen))
703         ;
704       else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
705         ;
706       else if (unformat (line_input, "del"))
707         is_add = 0;
708       else
709         if (unformat
710             (line_input, "interface %U", unformat_vnet_sw_interface, vnm,
711              &sw_if_index))
712         ;
713       else
714         {
715           error = clib_error_return (0, "unknown input: '%U'",
716                                      format_unformat_error, line_input);
717           goto done;
718         }
719     }
720
721   if (!plen)
722     {
723       error = clib_error_return (0, "NAT64 prefix must be set.");
724       goto done;
725     }
726
727   rv = nat64_add_del_prefix (&prefix, (u8) plen, vrf_id, is_add);
728
729   switch (rv)
730     {
731     case VNET_API_ERROR_NO_SUCH_ENTRY:
732       error = clib_error_return (0, "NAT64 prefix not exist.");
733       goto done;
734     case VNET_API_ERROR_INVALID_VALUE:
735       error = clib_error_return (0, "Invalid prefix length.");
736       goto done;
737     default:
738       break;
739     }
740
741   /*
742    * Add RX interface route, whenNAT isn't running on the real input
743    * interface
744    */
745   if (sw_if_index != ~0)
746     {
747       u32 fib_index;
748       fib_prefix_t fibpfx = {
749         .fp_len = plen,
750         .fp_proto = FIB_PROTOCOL_IP6,
751         .fp_addr = {.ip6 = prefix}
752       };
753
754       if (is_add)
755         {
756           fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
757                                                          vrf_id,
758                                                          FIB_SOURCE_PLUGIN_HI);
759           fib_table_entry_update_one_path (fib_index, &fibpfx,
760                                            FIB_SOURCE_PLUGIN_HI,
761                                            FIB_ENTRY_FLAG_NONE, DPO_PROTO_IP6,
762                                            NULL, sw_if_index, ~0, 0, NULL,
763                                            FIB_ROUTE_PATH_INTF_RX);
764         }
765       else
766         {
767           fib_index = fib_table_find (FIB_PROTOCOL_IP6, vrf_id);
768           fib_table_entry_path_remove (fib_index, &fibpfx,
769                                        FIB_SOURCE_PLUGIN_HI, DPO_PROTO_IP6,
770                                        NULL, sw_if_index, ~0, 1,
771                                        FIB_ROUTE_PATH_INTF_RX);
772           fib_table_unlock (fib_index, FIB_PROTOCOL_IP6,
773                             FIB_SOURCE_PLUGIN_HI);
774         }
775     }
776
777 done:
778   unformat_free (line_input);
779
780   return error;
781 }
782
783 static int
784 nat64_cli_prefix_walk (nat64_prefix_t * p, void *ctx)
785 {
786   vlib_main_t *vm = ctx;
787
788   vlib_cli_output (vm, " %U/%u tenant-vrf %u",
789                    format_ip6_address, &p->prefix, p->plen, p->vrf_id);
790
791   return 0;
792 }
793
794 static clib_error_t *
795 nat64_show_prefix_command_fn (vlib_main_t * vm, unformat_input_t * input,
796                               vlib_cli_command_t * cmd)
797 {
798   nat64_main_t *nm = &nat64_main;
799
800   if (nm->is_disabled)
801     return clib_error_return (0,
802                               "NAT64 disabled, multi thread not supported");
803
804   vlib_cli_output (vm, "NAT64 prefix:");
805   nat64_prefix_walk (nat64_cli_prefix_walk, vm);
806
807   return 0;
808 }
809
810 /* *INDENT-OFF* */
811
812 /*?
813  * @cliexpar
814  * @cliexstart{nat64 add pool address}
815  * Add/delete NAT64 pool address.
816  * To add single NAT64 pool address use:
817  *  vpp# nat64 add pool address 10.1.1.10
818  * To add NAT64 pool address range use:
819  *  vpp# nat64 add pool address 10.1.1.2 - 10.1.1.5
820  * To add NAT64 pool address for specific tenant use:
821  *  vpp# nat64 add pool address 10.1.1.100 tenant-vrf 100
822  * @cliexend
823 ?*/
824 VLIB_CLI_COMMAND (nat64_add_pool_address_command, static) = {
825   .path = "nat64 add pool address",
826   .short_help = "nat64 add pool address <ip4-range-start> [- <ip4-range-end>] "
827                 "[tenant-vrf <vrf-id>] [del]",
828   .function = nat64_add_del_pool_addr_command_fn,
829 };
830
831 /*?
832  * @cliexpar
833  * @cliexstart{show nat64 pool}
834  * Show NAT64 pool.
835  *  vpp# show nat64 pool
836  *  NAT64 pool:
837  *   10.1.1.3 tenant VRF: 0
838  *   10.1.1.10 tenant VRF: 10
839  * @cliexend
840 ?*/
841 VLIB_CLI_COMMAND (show_nat64_pool_command, static) = {
842   .path = "show nat64 pool",
843   .short_help = "show nat64 pool",
844   .function = nat64_show_pool_command_fn,
845 };
846
847 /*?
848  * @cliexpar
849  * @cliexstart{set interface nat64}
850  * Enable/disable NAT64 feature on the interface.
851  * To enable NAT64 feature with local (IPv6) network interface
852  * GigabitEthernet0/8/0 and external (IPv4) network interface
853  * GigabitEthernet0/a/0 use:
854  *  vpp# set interface nat64 in GigabitEthernet0/8/0 out GigabitEthernet0/a/0
855  * @cliexend
856 ?*/
857 VLIB_CLI_COMMAND (set_interface_nat64_command, static) = {
858   .path = "set interface nat64",
859   .short_help = "set interface nat64 in|out <intfc> [del]",
860   .function = nat64_interface_feature_command_fn,
861 };
862
863 /*?
864  * @cliexpar
865  * @cliexstart{show nat64 interfaces}
866  * Show interfaces with NAT64 feature.
867  * To show interfaces with NAT64 feature use:
868  *  vpp# show nat64 interfaces
869  *  NAT64 interfaces:
870  *   GigabitEthernet0/8/0 in
871  *   GigabitEthernet0/a/0 out
872  * @cliexend
873 ?*/
874 VLIB_CLI_COMMAND (show_nat64_interfaces_command, static) = {
875   .path = "show nat64 interfaces",
876   .short_help = "show nat64 interfaces",
877   .function = nat64_show_interfaces_command_fn,
878 };
879
880 /*?
881  * @cliexpar
882  * @cliexstart{nat64 add static bib}
883  * Add/delete NAT64 static BIB entry.
884  * To create NAT64 satatic BIB entry use:
885  *  vpp# nat64 add static bib 2001:db8:c000:221:: 1234 10.1.1.3 5678 tcp
886  *  vpp# nat64 add static bib 2001:db8:c000:221:: 1234 10.1.1.3 5678 udp vrf 10
887  * @cliexend
888 ?*/
889 VLIB_CLI_COMMAND (nat64_add_del_static_bib_command, static) = {
890   .path = "nat64 add static bib",
891   .short_help = "nat64 add static bib <ip6-addr> <port> <ip4-addr> <port> "
892                 "tcp|udp|icmp [vfr <table-id>] [del]",
893   .function = nat64_add_del_static_bib_command_fn,
894 };
895
896 /*?
897  * @cliexpar
898  * @cliexstart{show nat64 bib}
899  * Show NAT64 BIB entries.
900  * To show NAT64 TCP BIB entries use:
901  *  vpp# show nat64 bib tcp
902  *  NAT64 tcp BIB:
903  *   fd01:1::2 6303 10.0.0.3 62303 tcp vrf 0 dynamic 1 sessions
904  *   2001:db8:c000:221:: 1234 10.1.1.3 5678 tcp vrf 0 static 2 sessions
905  * To show NAT64 UDP BIB entries use:
906  *  vpp# show nat64 bib udp
907  *  NAT64 udp BIB:
908  *   fd01:1::2 6304 10.0.0.3 10546 udp vrf 0 dynamic 10 sessions
909  *   2001:db8:c000:221:: 1234 10.1.1.3 5678 udp vrf 10 static 0 sessions
910  * To show NAT64 ICMP BIB entries use:
911  *  vpp# show nat64 bib icmp
912  *  NAT64 icmp BIB:
913  *   fd01:1::2 6305 10.0.0.3 63209 icmp vrf 10 dynamic 1 sessions
914  * @cliexend
915 ?*/
916 VLIB_CLI_COMMAND (show_nat64_bib_command, static) = {
917   .path = "show nat64 bib",
918   .short_help = "show nat64 bib all|tcp|udp|icmp|unknown",
919   .function = nat64_show_bib_command_fn,
920 };
921
922 /*?
923  * @cliexpar
924  * @cliexstart{set nat64 timeouts}
925  * Set NAT64 session timeouts (in seconds).
926  * To set NAT64 session timeoutes use use:
927  *  vpp# set nat64 timeouts udp 200 icmp 30 tcp-trans 250 tcp-est 7450
928  * To reset NAT64 session timeoutes to default values use:
929  *  vpp# set nat64 timeouts reset
930  * @cliexend
931 ?*/
932 VLIB_CLI_COMMAND (set_nat64_timeouts_command, static) = {
933   .path = "set nat64 timeouts",
934   .short_help = "set nat64 timeouts udp <sec> icmp <sec> tcp-trans <sec> "
935                 "tcp-est <sec> tcp-incoming-syn <sec> | reset",
936   .function = nat64_set_timeouts_command_fn,
937 };
938
939 /*?
940  * @cliexpar
941  * @cliexstart{show nat64 timeoutss}
942  * Show NAT64 session timeouts:
943  *  vpp# show nat64 timeouts
944  *  NAT64 session timeouts:
945  *   UDP 300sec
946  *   ICMP 60sec
947  *   TCP transitory 240sec
948  *   TCP established 7440sec
949  *   TCP incoming SYN 6sec
950  * @cliexend
951 ?*/
952 VLIB_CLI_COMMAND (show_nat64_timeouts_command, static) = {
953   .path = "show nat64 timeouts",
954   .short_help = "show nat64 timeouts",
955   .function = nat64_show_timeouts_command_fn,
956 };
957
958 /*?
959  * @cliexpar
960  * @cliexstart{show nat64 session table}
961  * Show NAT64 session table.
962  * To show NAT64 TCP session table use:
963  *  vpp# show nat64 session table tcp
964  *  NAT64 tcp session table:
965  *   fd01:1::2 6303 64:ff9b::ac10:202 20 10.0.0.3 62303 172.16.2.2 20 tcp vrf 0
966  *   fd01:3::2 6303 64:ff9b::ac10:202 20 10.0.10.3 21300 172.16.2.2 20 tcp vrf 10
967  * To show NAT64 UDP session table use:
968  * #vpp show nat64 session table udp
969  * NAT64 udp session table:
970  *  fd01:1::2 6304 64:ff9b::ac10:202 20 10.0.0.3 10546 172.16.2.2 20 udp vrf 0
971  *  fd01:3::2 6304 64:ff9b::ac10:202 20 10.0.10.3 58627 172.16.2.2 20 udp vrf 10
972  *  fd01:1::2 1235 64:ff9b::a00:3 4023 10.0.0.3 24488 10.0.0.3 4023 udp vrf 0
973  *  fd01:1::3 23 64:ff9b::a00:3 24488 10.0.0.3 4023 10.0.0.3 24488 udp vrf 0
974  * To show NAT64 ICMP session table use:
975  * #vpp show nat64 session table icmp
976  * NAT64 icmp session table:
977  *  fd01:1::2 64:ff9b::ac10:202 6305 10.0.0.3 172.16.2.2 63209 icmp vrf 0
978  * @cliexend
979 ?*/
980 VLIB_CLI_COMMAND (show_nat64_st_command, static) = {
981   .path = "show nat64 session table",
982   .short_help = "show nat64 session table all|tcp|udp|icmp|unknown",
983   .function = nat64_show_st_command_fn,
984 };
985
986 /*?
987  * @cliexpar
988  * @cliexstart{nat64 add prefix}
989  * Set NAT64 prefix for generating IPv6 representations of IPv4 addresses.
990  * To set NAT64 global prefix use:
991  *  vpp# nat64 add prefix 2001:db8::/32
992  * To set NAT64 prefix for specific tenant use:
993  *  vpp# nat64 add prefix 2001:db8:122:300::/56 tenant-vrf 10
994  * @cliexend
995 ?*/
996 VLIB_CLI_COMMAND (nat64_add_del_prefix_command, static) = {
997   .path = "nat64 add prefix",
998   .short_help = "nat64 add prefix <ip6-prefix>/<plen> [tenant-vrf <vrf-id>] "
999                 "[del] [interface <interface]",
1000   .function = nat64_add_del_prefix_command_fn,
1001 };
1002
1003 /*?
1004  * @cliexpar
1005  * @cliexstart{show nat64 prefix}
1006  * Show NAT64 prefix.
1007  * To show NAT64 prefix use:
1008  *  vpp# show nat64 prefix
1009  *  NAT64 prefix:
1010  *   2001:db8::/32 tenant-vrf 0
1011  *   2001:db8:122:300::/56 tenant-vrf 10
1012  * @cliexend
1013 ?*/
1014 VLIB_CLI_COMMAND (show_nat64_prefix_command, static) = {
1015   .path = "show nat64 prefix",
1016   .short_help = "show nat64 prefix",
1017   .function = nat64_show_prefix_command_fn,
1018 };
1019
1020 /* *INDENT-ON* */
1021
1022 /*
1023  * fd.io coding-style-patch-verification: ON
1024  *
1025  * Local Variables:
1026  * eval: (c-set-style "gnu")
1027  * End:
1028  */