NAT44: add opaque string tag to static mapping APIs (VPP-1147)
[vpp.git] / src / plugins / nat / nat44_cli.c
1 /*
2  * Copyright (c) 2018 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 NAT44 CLI
18  */
19
20 #include <nat/nat.h>
21 #include <nat/nat_ipfix_logging.h>
22 #include <nat/nat_det.h>
23 #include <vnet/fib/fib_table.h>
24
25 static clib_error_t *
26 set_workers_command_fn (vlib_main_t * vm,
27                         unformat_input_t * input, vlib_cli_command_t * cmd)
28 {
29   unformat_input_t _line_input, *line_input = &_line_input;
30   uword *bitmap = 0;
31   int rv = 0;
32   clib_error_t *error = 0;
33
34   /* Get a line of input. */
35   if (!unformat_user (input, unformat_line_input, line_input))
36     return 0;
37
38   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
39     {
40       if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
41         ;
42       else
43         {
44           error = clib_error_return (0, "unknown input '%U'",
45                                      format_unformat_error, line_input);
46           goto done;
47         }
48     }
49
50   if (bitmap == 0)
51     {
52       error = clib_error_return (0, "List of workers must be specified.");
53       goto done;
54     }
55
56   rv = snat_set_workers (bitmap);
57
58   clib_bitmap_free (bitmap);
59
60   switch (rv)
61     {
62     case VNET_API_ERROR_INVALID_WORKER:
63       error = clib_error_return (0, "Invalid worker(s).");
64       goto done;
65     case VNET_API_ERROR_FEATURE_DISABLED:
66       error = clib_error_return (0,
67                                  "Supported only if 2 or more workes available.");
68       goto done;
69     default:
70       break;
71     }
72
73 done:
74   unformat_free (line_input);
75
76   return error;
77 }
78
79 static clib_error_t *
80 nat_show_workers_commnad_fn (vlib_main_t * vm, unformat_input_t * input,
81                              vlib_cli_command_t * cmd)
82 {
83   snat_main_t *sm = &snat_main;
84   u32 *worker;
85
86   if (sm->num_workers > 1)
87     {
88       vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
89       /* *INDENT-OFF* */
90       vec_foreach (worker, sm->workers)
91         {
92           vlib_worker_thread_t *w =
93             vlib_worker_threads + *worker + sm->first_worker_index;
94           vlib_cli_output (vm, "  %s", w->name);
95         }
96       /* *INDENT-ON* */
97     }
98
99   return 0;
100 }
101
102 static clib_error_t *
103 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
104                                               unformat_input_t * input,
105                                               vlib_cli_command_t * cmd)
106 {
107   unformat_input_t _line_input, *line_input = &_line_input;
108   u32 domain_id = 0;
109   u32 src_port = 0;
110   u8 enable = 1;
111   int rv = 0;
112   clib_error_t *error = 0;
113
114   /* Get a line of input. */
115   if (!unformat_user (input, unformat_line_input, line_input))
116     return 0;
117
118   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
119     {
120       if (unformat (line_input, "domain %d", &domain_id))
121         ;
122       else if (unformat (line_input, "src-port %d", &src_port))
123         ;
124       else if (unformat (line_input, "disable"))
125         enable = 0;
126       else
127         {
128           error = clib_error_return (0, "unknown input '%U'",
129                                      format_unformat_error, line_input);
130           goto done;
131         }
132     }
133
134   rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
135
136   if (rv)
137     {
138       error = clib_error_return (0, "ipfix logging enable failed");
139       goto done;
140     }
141
142 done:
143   unformat_free (line_input);
144
145   return error;
146 }
147
148 static clib_error_t *
149 nat44_set_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
150                                               unformat_input_t * input,
151                                               vlib_cli_command_t * cmd)
152 {
153   unformat_input_t _line_input, *line_input = &_line_input;
154   clib_error_t *error = 0;
155   u32 psid, psid_offset, psid_length;
156
157   /* Get a line of input. */
158   if (!unformat_user (input, unformat_line_input, line_input))
159     return 0;
160
161   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
162     {
163       if (unformat (line_input, "default"))
164         nat_set_alloc_addr_and_port_default ();
165       else
166         if (unformat
167             (line_input, "map-e psid %d psid-offset %d psid-len %d", &psid,
168              &psid_offset, &psid_length))
169         nat_set_alloc_addr_and_port_mape ((u16) psid, (u16) psid_offset,
170                                           (u16) psid_length);
171       else
172         {
173           error = clib_error_return (0, "unknown input '%U'",
174                                      format_unformat_error, line_input);
175           goto done;
176         }
177     }
178
179 done:
180   unformat_free (line_input);
181
182   return error;
183 };
184
185 static clib_error_t *
186 add_address_command_fn (vlib_main_t * vm,
187                         unformat_input_t * input, vlib_cli_command_t * cmd)
188 {
189   unformat_input_t _line_input, *line_input = &_line_input;
190   snat_main_t *sm = &snat_main;
191   ip4_address_t start_addr, end_addr, this_addr;
192   u32 start_host_order, end_host_order;
193   u32 vrf_id = ~0;
194   int i, count;
195   int is_add = 1;
196   int rv = 0;
197   clib_error_t *error = 0;
198   u8 twice_nat = 0;
199
200   /* Get a line of input. */
201   if (!unformat_user (input, unformat_line_input, line_input))
202     return 0;
203
204   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
205     {
206       if (unformat (line_input, "%U - %U",
207                     unformat_ip4_address, &start_addr,
208                     unformat_ip4_address, &end_addr))
209         ;
210       else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
211         ;
212       else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
213         end_addr = start_addr;
214       else if (unformat (line_input, "twice-nat"))
215         twice_nat = 1;
216       else if (unformat (line_input, "del"))
217         is_add = 0;
218       else
219         {
220           error = clib_error_return (0, "unknown input '%U'",
221                                      format_unformat_error, line_input);
222           goto done;
223         }
224     }
225
226   if (sm->static_mapping_only)
227     {
228       error = clib_error_return (0, "static mapping only mode");
229       goto done;
230     }
231
232   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
233   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
234
235   if (end_host_order < start_host_order)
236     {
237       error = clib_error_return (0, "end address less than start address");
238       goto done;
239     }
240
241   count = (end_host_order - start_host_order) + 1;
242
243   if (count > 1024)
244     clib_warning ("%U - %U, %d addresses...",
245                   format_ip4_address, &start_addr,
246                   format_ip4_address, &end_addr, count);
247
248   this_addr = start_addr;
249
250   for (i = 0; i < count; i++)
251     {
252       if (is_add)
253         snat_add_address (sm, &this_addr, vrf_id, twice_nat);
254       else
255         rv = snat_del_address (sm, this_addr, 0, twice_nat);
256
257       switch (rv)
258         {
259         case VNET_API_ERROR_NO_SUCH_ENTRY:
260           error = clib_error_return (0, "S-NAT address not exist.");
261           goto done;
262         case VNET_API_ERROR_UNSPECIFIED:
263           error =
264             clib_error_return (0, "S-NAT address used in static mapping.");
265           goto done;
266         default:
267           break;
268         }
269
270       if (sm->out2in_dpo)
271         nat44_add_del_address_dpo (this_addr, is_add);
272
273       increment_v4_address (&this_addr);
274     }
275
276 done:
277   unformat_free (line_input);
278
279   return error;
280 }
281
282 static clib_error_t *
283 nat44_show_addresses_command_fn (vlib_main_t * vm, unformat_input_t * input,
284                                  vlib_cli_command_t * cmd)
285 {
286   snat_main_t *sm = &snat_main;
287   snat_address_t *ap;
288
289   vlib_cli_output (vm, "NAT44 pool addresses:");
290   /* *INDENT-OFF* */
291   vec_foreach (ap, sm->addresses)
292     {
293       vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
294       if (ap->fib_index != ~0)
295           vlib_cli_output (vm, "  tenant VRF: %u",
296             fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
297       else
298         vlib_cli_output (vm, "  tenant VRF independent");
299     #define _(N, i, n, s) \
300       vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
301       foreach_snat_protocol
302     #undef _
303     }
304   vlib_cli_output (vm, "NAT44 twice-nat pool addresses:");
305   vec_foreach (ap, sm->twice_nat_addresses)
306     {
307       vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
308       if (ap->fib_index != ~0)
309           vlib_cli_output (vm, "  tenant VRF: %u",
310             fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
311       else
312         vlib_cli_output (vm, "  tenant VRF independent");
313     #define _(N, i, n, s) \
314       vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
315       foreach_snat_protocol
316     #undef _
317     }
318   /* *INDENT-ON* */
319   return 0;
320 }
321
322 static clib_error_t *
323 snat_feature_command_fn (vlib_main_t * vm,
324                          unformat_input_t * input, vlib_cli_command_t * cmd)
325 {
326   unformat_input_t _line_input, *line_input = &_line_input;
327   vnet_main_t *vnm = vnet_get_main ();
328   clib_error_t *error = 0;
329   u32 sw_if_index;
330   u32 *inside_sw_if_indices = 0;
331   u32 *outside_sw_if_indices = 0;
332   u8 is_output_feature = 0;
333   int is_del = 0;
334   int i;
335
336   sw_if_index = ~0;
337
338   /* Get a line of input. */
339   if (!unformat_user (input, unformat_line_input, line_input))
340     return 0;
341
342   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
343     {
344       if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
345                     vnm, &sw_if_index))
346         vec_add1 (inside_sw_if_indices, sw_if_index);
347       else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
348                          vnm, &sw_if_index))
349         vec_add1 (outside_sw_if_indices, sw_if_index);
350       else if (unformat (line_input, "output-feature"))
351         is_output_feature = 1;
352       else if (unformat (line_input, "del"))
353         is_del = 1;
354       else
355         {
356           error = clib_error_return (0, "unknown input '%U'",
357                                      format_unformat_error, line_input);
358           goto done;
359         }
360     }
361
362   if (vec_len (inside_sw_if_indices))
363     {
364       for (i = 0; i < vec_len (inside_sw_if_indices); i++)
365         {
366           sw_if_index = inside_sw_if_indices[i];
367           if (is_output_feature)
368             {
369               if (snat_interface_add_del_output_feature
370                   (sw_if_index, 1, is_del))
371                 {
372                   error = clib_error_return (0, "%s %U failed",
373                                              is_del ? "del" : "add",
374                                              format_vnet_sw_interface_name,
375                                              vnm, vnet_get_sw_interface (vnm,
376                                                                          sw_if_index));
377                   goto done;
378                 }
379             }
380           else
381             {
382               if (snat_interface_add_del (sw_if_index, 1, is_del))
383                 {
384                   error = clib_error_return (0, "%s %U failed",
385                                              is_del ? "del" : "add",
386                                              format_vnet_sw_interface_name,
387                                              vnm, vnet_get_sw_interface (vnm,
388                                                                          sw_if_index));
389                   goto done;
390                 }
391             }
392         }
393     }
394
395   if (vec_len (outside_sw_if_indices))
396     {
397       for (i = 0; i < vec_len (outside_sw_if_indices); i++)
398         {
399           sw_if_index = outside_sw_if_indices[i];
400           if (is_output_feature)
401             {
402               if (snat_interface_add_del_output_feature
403                   (sw_if_index, 0, is_del))
404                 {
405                   error = clib_error_return (0, "%s %U failed",
406                                              is_del ? "del" : "add",
407                                              format_vnet_sw_interface_name,
408                                              vnm, vnet_get_sw_interface (vnm,
409                                                                          sw_if_index));
410                   goto done;
411                 }
412             }
413           else
414             {
415               if (snat_interface_add_del (sw_if_index, 0, is_del))
416                 {
417                   error = clib_error_return (0, "%s %U failed",
418                                              is_del ? "del" : "add",
419                                              format_vnet_sw_interface_name,
420                                              vnm, vnet_get_sw_interface (vnm,
421                                                                          sw_if_index));
422                   goto done;
423                 }
424             }
425         }
426     }
427
428 done:
429   unformat_free (line_input);
430   vec_free (inside_sw_if_indices);
431   vec_free (outside_sw_if_indices);
432
433   return error;
434 }
435
436 static clib_error_t *
437 nat44_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
438                                   vlib_cli_command_t * cmd)
439 {
440   snat_main_t *sm = &snat_main;
441   snat_interface_t *i;
442   vnet_main_t *vnm = vnet_get_main ();
443
444   vlib_cli_output (vm, "NAT44 interfaces:");
445   /* *INDENT-OFF* */
446   pool_foreach (i, sm->interfaces,
447   ({
448     vlib_cli_output (vm, " %U %s", format_vnet_sw_interface_name, vnm,
449                      vnet_get_sw_interface (vnm, i->sw_if_index),
450                      (nat_interface_is_inside(i) &&
451                       nat_interface_is_outside(i)) ? "in out" :
452                      (nat_interface_is_inside(i) ? "in" : "out"));
453   }));
454
455   pool_foreach (i, sm->output_feature_interfaces,
456   ({
457     vlib_cli_output (vm, " %U output-feature %s",
458                      format_vnet_sw_interface_name, vnm,
459                      vnet_get_sw_interface (vnm, i->sw_if_index),
460                      (nat_interface_is_inside(i) &&
461                       nat_interface_is_outside(i)) ? "in out" :
462                      (nat_interface_is_inside(i) ? "in" : "out"));
463   }));
464   /* *INDENT-ON* */
465
466   return 0;
467 }
468
469 static clib_error_t *
470 add_static_mapping_command_fn (vlib_main_t * vm,
471                                unformat_input_t * input,
472                                vlib_cli_command_t * cmd)
473 {
474   unformat_input_t _line_input, *line_input = &_line_input;
475   clib_error_t *error = 0;
476   ip4_address_t l_addr, e_addr;
477   u32 l_port = 0, e_port = 0, vrf_id = ~0;
478   int is_add = 1;
479   int addr_only = 1;
480   u32 sw_if_index = ~0;
481   vnet_main_t *vnm = vnet_get_main ();
482   int rv;
483   snat_protocol_t proto = ~0;
484   u8 proto_set = 0;
485   u8 twice_nat = 0;
486   u8 out2in_only = 0;
487
488   /* Get a line of input. */
489   if (!unformat_user (input, unformat_line_input, line_input))
490     return 0;
491
492   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
493     {
494       if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
495                     &l_port))
496         addr_only = 0;
497       else
498         if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
499         ;
500       else if (unformat (line_input, "external %U %u", unformat_ip4_address,
501                          &e_addr, &e_port))
502         addr_only = 0;
503       else if (unformat (line_input, "external %U", unformat_ip4_address,
504                          &e_addr))
505         ;
506       else if (unformat (line_input, "external %U %u",
507                          unformat_vnet_sw_interface, vnm, &sw_if_index,
508                          &e_port))
509         addr_only = 0;
510
511       else if (unformat (line_input, "external %U",
512                          unformat_vnet_sw_interface, vnm, &sw_if_index))
513         ;
514       else if (unformat (line_input, "vrf %u", &vrf_id))
515         ;
516       else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
517         proto_set = 1;
518       else if (unformat (line_input, "twice-nat"))
519         twice_nat = 1;
520       else if (unformat (line_input, "out2in-only"))
521         out2in_only = 1;
522       else if (unformat (line_input, "del"))
523         is_add = 0;
524       else
525         {
526           error = clib_error_return (0, "unknown input: '%U'",
527                                      format_unformat_error, line_input);
528           goto done;
529         }
530     }
531
532   if (twice_nat && addr_only)
533     {
534       error = clib_error_return (0, "twice NAT only for 1:1 NAPT");
535       goto done;
536     }
537
538   if (!addr_only && !proto_set)
539     {
540       error = clib_error_return (0, "missing protocol");
541       goto done;
542     }
543
544   rv = snat_add_static_mapping (l_addr, e_addr, (u16) l_port, (u16) e_port,
545                                 vrf_id, addr_only, sw_if_index, proto, is_add,
546                                 twice_nat, out2in_only, 0);
547
548   switch (rv)
549     {
550     case VNET_API_ERROR_INVALID_VALUE:
551       error = clib_error_return (0, "External port already in use.");
552       goto done;
553     case VNET_API_ERROR_NO_SUCH_ENTRY:
554       if (is_add)
555         error = clib_error_return (0, "External addres must be allocated.");
556       else
557         error = clib_error_return (0, "Mapping not exist.");
558       goto done;
559     case VNET_API_ERROR_NO_SUCH_FIB:
560       error = clib_error_return (0, "No such VRF id.");
561       goto done;
562     case VNET_API_ERROR_VALUE_EXIST:
563       error = clib_error_return (0, "Mapping already exist.");
564       goto done;
565     default:
566       break;
567     }
568
569 done:
570   unformat_free (line_input);
571
572   return error;
573 }
574
575 static clib_error_t *
576 add_identity_mapping_command_fn (vlib_main_t * vm,
577                                  unformat_input_t * input,
578                                  vlib_cli_command_t * cmd)
579 {
580   unformat_input_t _line_input, *line_input = &_line_input;
581   clib_error_t *error = 0;
582   ip4_address_t addr;
583   u32 port = 0, vrf_id = ~0;
584   int is_add = 1;
585   int addr_only = 1;
586   u32 sw_if_index = ~0;
587   vnet_main_t *vnm = vnet_get_main ();
588   int rv;
589   snat_protocol_t proto;
590
591   addr.as_u32 = 0;
592
593   /* Get a line of input. */
594   if (!unformat_user (input, unformat_line_input, line_input))
595     return 0;
596
597   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
598     {
599       if (unformat (line_input, "%U", unformat_ip4_address, &addr))
600         ;
601       else if (unformat (line_input, "external %U",
602                          unformat_vnet_sw_interface, vnm, &sw_if_index))
603         ;
604       else if (unformat (line_input, "vrf %u", &vrf_id))
605         ;
606       else if (unformat (line_input, "%U %u", unformat_snat_protocol, &proto,
607                          &port))
608         addr_only = 0;
609       else if (unformat (line_input, "del"))
610         is_add = 0;
611       else
612         {
613           error = clib_error_return (0, "unknown input: '%U'",
614                                      format_unformat_error, line_input);
615           goto done;
616         }
617     }
618
619   rv = snat_add_static_mapping (addr, addr, (u16) port, (u16) port,
620                                 vrf_id, addr_only, sw_if_index, proto, is_add,
621                                 0, 0, 0);
622
623   switch (rv)
624     {
625     case VNET_API_ERROR_INVALID_VALUE:
626       error = clib_error_return (0, "External port already in use.");
627       goto done;
628     case VNET_API_ERROR_NO_SUCH_ENTRY:
629       if (is_add)
630         error = clib_error_return (0, "External addres must be allocated.");
631       else
632         error = clib_error_return (0, "Mapping not exist.");
633       goto done;
634     case VNET_API_ERROR_NO_SUCH_FIB:
635       error = clib_error_return (0, "No such VRF id.");
636       goto done;
637     case VNET_API_ERROR_VALUE_EXIST:
638       error = clib_error_return (0, "Mapping already exist.");
639       goto done;
640     default:
641       break;
642     }
643
644 done:
645   unformat_free (line_input);
646
647   return error;
648 }
649
650 static clib_error_t *
651 add_lb_static_mapping_command_fn (vlib_main_t * vm,
652                                   unformat_input_t * input,
653                                   vlib_cli_command_t * cmd)
654 {
655   unformat_input_t _line_input, *line_input = &_line_input;
656   clib_error_t *error = 0;
657   ip4_address_t l_addr, e_addr;
658   u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
659   int is_add = 1;
660   int rv;
661   snat_protocol_t proto;
662   u8 proto_set = 0;
663   nat44_lb_addr_port_t *locals = 0, local;
664   u8 twice_nat = 0;
665   u8 out2in_only = 0;
666
667   /* Get a line of input. */
668   if (!unformat_user (input, unformat_line_input, line_input))
669     return 0;
670
671   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
672     {
673       if (unformat (line_input, "local %U:%u probability %u",
674                     unformat_ip4_address, &l_addr, &l_port, &probability))
675         {
676           memset (&local, 0, sizeof (local));
677           local.addr = l_addr;
678           local.port = (u16) l_port;
679           local.probability = (u8) probability;
680           vec_add1 (locals, local);
681         }
682       else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
683                          &e_addr, &e_port))
684         ;
685       else if (unformat (line_input, "vrf %u", &vrf_id))
686         ;
687       else if (unformat (line_input, "protocol %U", unformat_snat_protocol,
688                          &proto))
689         proto_set = 1;
690       else if (unformat (line_input, "twice-nat"))
691         twice_nat = 1;
692       else if (unformat (line_input, "out2in-only"))
693         out2in_only = 1;
694       else if (unformat (line_input, "del"))
695         is_add = 0;
696       else
697         {
698           error = clib_error_return (0, "unknown input: '%U'",
699                                      format_unformat_error, line_input);
700           goto done;
701         }
702     }
703
704   if (vec_len (locals) < 2)
705     {
706       error = clib_error_return (0, "at least two local must be set");
707       goto done;
708     }
709
710   if (!proto_set)
711     {
712       error = clib_error_return (0, "missing protocol");
713       goto done;
714     }
715
716   rv = nat44_add_del_lb_static_mapping (e_addr, (u16) e_port, proto, vrf_id,
717                                         locals, is_add, twice_nat,
718                                         out2in_only, 0);
719
720   switch (rv)
721     {
722     case VNET_API_ERROR_INVALID_VALUE:
723       error = clib_error_return (0, "External port already in use.");
724       goto done;
725     case VNET_API_ERROR_NO_SUCH_ENTRY:
726       if (is_add)
727         error = clib_error_return (0, "External addres must be allocated.");
728       else
729         error = clib_error_return (0, "Mapping not exist.");
730       goto done;
731     case VNET_API_ERROR_VALUE_EXIST:
732       error = clib_error_return (0, "Mapping already exist.");
733       goto done;
734     default:
735       break;
736     }
737
738 done:
739   unformat_free (line_input);
740   vec_free (locals);
741
742   return error;
743 }
744
745 static clib_error_t *
746 nat44_show_static_mappings_command_fn (vlib_main_t * vm,
747                                        unformat_input_t * input,
748                                        vlib_cli_command_t * cmd)
749 {
750   snat_main_t *sm = &snat_main;
751   snat_static_mapping_t *m;
752   snat_static_map_resolve_t *rp;
753
754   vlib_cli_output (vm, "NAT44 static mappings:");
755   /* *INDENT-OFF* */
756   pool_foreach (m, sm->static_mappings,
757   ({
758     vlib_cli_output (vm, " %U", format_snat_static_mapping, m);
759   }));
760   vec_foreach (rp, sm->to_resolve)
761     vlib_cli_output (vm, " %U", format_snat_static_map_to_resolve, rp);
762   /* *INDENT-ON* */
763
764   return 0;
765 }
766
767 static clib_error_t *
768 snat_add_interface_address_command_fn (vlib_main_t * vm,
769                                        unformat_input_t * input,
770                                        vlib_cli_command_t * cmd)
771 {
772   snat_main_t *sm = &snat_main;
773   unformat_input_t _line_input, *line_input = &_line_input;
774   u32 sw_if_index;
775   int rv;
776   int is_del = 0;
777   clib_error_t *error = 0;
778   u8 twice_nat = 0;
779
780   /* Get a line of input. */
781   if (!unformat_user (input, unformat_line_input, line_input))
782     return 0;
783
784   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
785     {
786       if (unformat (line_input, "%U", unformat_vnet_sw_interface,
787                     sm->vnet_main, &sw_if_index))
788         ;
789       else if (unformat (line_input, "twice-nat"))
790         twice_nat = 1;
791       else if (unformat (line_input, "del"))
792         is_del = 1;
793       else
794         {
795           error = clib_error_return (0, "unknown input '%U'",
796                                      format_unformat_error, line_input);
797           goto done;
798         }
799     }
800
801   rv = snat_add_interface_address (sm, sw_if_index, is_del, twice_nat);
802
803   switch (rv)
804     {
805     case 0:
806       break;
807
808     default:
809       error = clib_error_return (0, "snat_add_interface_address returned %d",
810                                  rv);
811       goto done;
812     }
813
814 done:
815   unformat_free (line_input);
816
817   return error;
818 }
819
820 static clib_error_t *
821 nat44_show_interface_address_command_fn (vlib_main_t * vm,
822                                          unformat_input_t * input,
823                                          vlib_cli_command_t * cmd)
824 {
825   snat_main_t *sm = &snat_main;
826   vnet_main_t *vnm = vnet_get_main ();
827   u32 *sw_if_index;
828
829   /* *INDENT-OFF* */
830   vlib_cli_output (vm, "NAT44 pool address interfaces:");
831   vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
832     {
833       vlib_cli_output (vm, " %U", format_vnet_sw_interface_name, vnm,
834                        vnet_get_sw_interface (vnm, *sw_if_index));
835     }
836   vlib_cli_output (vm, "NAT44 twice-nat pool address interfaces:");
837   vec_foreach (sw_if_index, sm->auto_add_sw_if_indices_twice_nat)
838     {
839       vlib_cli_output (vm, " %U", format_vnet_sw_interface_name, vnm,
840                        vnet_get_sw_interface (vnm, *sw_if_index));
841     }
842   /* *INDENT-ON* */
843
844   return 0;
845 }
846
847 static clib_error_t *
848 nat44_show_sessions_command_fn (vlib_main_t * vm, unformat_input_t * input,
849                                 vlib_cli_command_t * cmd)
850 {
851   int verbose = 0;
852   snat_main_t *sm = &snat_main;
853   snat_main_per_thread_data_t *tsm;
854   snat_user_t *u;
855   int i = 0;
856
857   if (unformat (input, "detail"))
858     verbose = 1;
859
860   vlib_cli_output (vm, "NAT44 sessions:");
861
862   /* *INDENT-OFF* */
863   vec_foreach_index (i, sm->per_thread_data)
864     {
865       tsm = vec_elt_at_index (sm->per_thread_data, i);
866
867       pool_foreach (u, tsm->users,
868       ({
869         vlib_cli_output (vm, "  %U", format_snat_user, tsm, u, verbose);
870       }));
871     }
872   /* *INDENT-ON* */
873
874   return 0;
875 }
876
877 static clib_error_t *
878 nat44_del_session_command_fn (vlib_main_t * vm,
879                               unformat_input_t * input,
880                               vlib_cli_command_t * cmd)
881 {
882   snat_main_t *sm = &snat_main;
883   unformat_input_t _line_input, *line_input = &_line_input;
884   int is_in = 0;
885   clib_error_t *error = 0;
886   ip4_address_t addr;
887   u32 port = 0, vrf_id = sm->outside_vrf_id;
888   snat_protocol_t proto;
889   int rv;
890
891   /* Get a line of input. */
892   if (!unformat_user (input, unformat_line_input, line_input))
893     return 0;
894
895   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
896     {
897       if (unformat
898           (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
899            unformat_snat_protocol, &proto))
900         ;
901       else if (unformat (line_input, "in"))
902         {
903           is_in = 1;
904           vrf_id = sm->inside_vrf_id;
905         }
906       else if (unformat (line_input, "vrf %u", &vrf_id))
907         ;
908       else
909         {
910           error = clib_error_return (0, "unknown input '%U'",
911                                      format_unformat_error, line_input);
912           goto done;
913         }
914     }
915
916   rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
917
918   switch (rv)
919     {
920     case 0:
921       break;
922
923     default:
924       error = clib_error_return (0, "nat44_del_session returned %d", rv);
925       goto done;
926     }
927
928 done:
929   unformat_free (line_input);
930
931   return error;
932 }
933
934 static clib_error_t *
935 snat_forwarding_set_command_fn (vlib_main_t * vm,
936                                 unformat_input_t * input,
937                                 vlib_cli_command_t * cmd)
938 {
939   snat_main_t *sm = &snat_main;
940   unformat_input_t _line_input, *line_input = &_line_input;
941   u8 forwarding_enable;
942   u8 forwarding_enable_set = 0;
943   clib_error_t *error = 0;
944
945   /* Get a line of input. */
946   if (!unformat_user (input, unformat_line_input, line_input))
947     return clib_error_return (0, "'enable' or 'disable' expected");
948
949   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
950     {
951       if (!forwarding_enable_set && unformat (line_input, "enable"))
952         {
953           forwarding_enable = 1;
954           forwarding_enable_set = 1;
955         }
956       else if (!forwarding_enable_set && unformat (line_input, "disable"))
957         {
958           forwarding_enable = 0;
959           forwarding_enable_set = 1;
960         }
961       else
962         {
963           error = clib_error_return (0, "unknown input '%U'",
964                                      format_unformat_error, line_input);
965           goto done;
966         }
967     }
968
969   if (!forwarding_enable_set)
970     {
971       error = clib_error_return (0, "'enable' or 'disable' expected");
972       goto done;
973     }
974
975   sm->forwarding_enabled = forwarding_enable;
976
977 done:
978   unformat_free (line_input);
979
980   return error;
981 }
982
983 static clib_error_t *
984 snat_det_map_command_fn (vlib_main_t * vm,
985                          unformat_input_t * input, vlib_cli_command_t * cmd)
986 {
987   snat_main_t *sm = &snat_main;
988   unformat_input_t _line_input, *line_input = &_line_input;
989   ip4_address_t in_addr, out_addr;
990   u32 in_plen, out_plen;
991   int is_add = 1, rv;
992   clib_error_t *error = 0;
993
994   /* Get a line of input. */
995   if (!unformat_user (input, unformat_line_input, line_input))
996     return 0;
997
998   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
999     {
1000       if (unformat
1001           (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
1002         ;
1003       else
1004         if (unformat
1005             (line_input, "out %U/%u", unformat_ip4_address, &out_addr,
1006              &out_plen))
1007         ;
1008       else if (unformat (line_input, "del"))
1009         is_add = 0;
1010       else
1011         {
1012           error = clib_error_return (0, "unknown input '%U'",
1013                                      format_unformat_error, line_input);
1014           goto done;
1015         }
1016     }
1017
1018   rv = snat_det_add_map (sm, &in_addr, (u8) in_plen, &out_addr, (u8) out_plen,
1019                          is_add);
1020
1021   if (rv)
1022     {
1023       error = clib_error_return (0, "snat_det_add_map return %d", rv);
1024       goto done;
1025     }
1026
1027 done:
1028   unformat_free (line_input);
1029
1030   return error;
1031 }
1032
1033 static clib_error_t *
1034 nat44_det_show_mappings_command_fn (vlib_main_t * vm,
1035                                     unformat_input_t * input,
1036                                     vlib_cli_command_t * cmd)
1037 {
1038   snat_main_t *sm = &snat_main;
1039   snat_det_map_t *dm;
1040
1041   vlib_cli_output (vm, "NAT44 deterministic mappings:");
1042   /* *INDENT-OFF* */
1043   pool_foreach (dm, sm->det_maps,
1044   ({
1045     vlib_cli_output (vm, " in %U/%d out %U/%d\n",
1046                      format_ip4_address, &dm->in_addr, dm->in_plen,
1047                      format_ip4_address, &dm->out_addr, dm->out_plen);
1048     vlib_cli_output (vm, "  outside address sharing ratio: %d\n",
1049                      dm->sharing_ratio);
1050     vlib_cli_output (vm, "  number of ports per inside host: %d\n",
1051                      dm->ports_per_host);
1052     vlib_cli_output (vm, "  sessions number: %d\n", dm->ses_num);
1053   }));
1054   /* *INDENT-ON* */
1055
1056   return 0;
1057 }
1058
1059 static clib_error_t *
1060 snat_det_forward_command_fn (vlib_main_t * vm,
1061                              unformat_input_t * input,
1062                              vlib_cli_command_t * cmd)
1063 {
1064   snat_main_t *sm = &snat_main;
1065   unformat_input_t _line_input, *line_input = &_line_input;
1066   ip4_address_t in_addr, out_addr;
1067   u16 lo_port;
1068   snat_det_map_t *dm;
1069   clib_error_t *error = 0;
1070
1071   /* Get a line of input. */
1072   if (!unformat_user (input, unformat_line_input, line_input))
1073     return 0;
1074
1075   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1076     {
1077       if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
1078         ;
1079       else
1080         {
1081           error = clib_error_return (0, "unknown input '%U'",
1082                                      format_unformat_error, line_input);
1083           goto done;
1084         }
1085     }
1086
1087   dm = snat_det_map_by_user (sm, &in_addr);
1088   if (!dm)
1089     vlib_cli_output (vm, "no match");
1090   else
1091     {
1092       snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
1093       vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
1094                        lo_port, lo_port + dm->ports_per_host - 1);
1095     }
1096
1097 done:
1098   unformat_free (line_input);
1099
1100   return error;
1101 }
1102
1103 static clib_error_t *
1104 snat_det_reverse_command_fn (vlib_main_t * vm,
1105                              unformat_input_t * input,
1106                              vlib_cli_command_t * cmd)
1107 {
1108   snat_main_t *sm = &snat_main;
1109   unformat_input_t _line_input, *line_input = &_line_input;
1110   ip4_address_t in_addr, out_addr;
1111   u32 out_port;
1112   snat_det_map_t *dm;
1113   clib_error_t *error = 0;
1114
1115   /* Get a line of input. */
1116   if (!unformat_user (input, unformat_line_input, line_input))
1117     return 0;
1118
1119   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1120     {
1121       if (unformat
1122           (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
1123         ;
1124       else
1125         {
1126           error = clib_error_return (0, "unknown input '%U'",
1127                                      format_unformat_error, line_input);
1128           goto done;
1129         }
1130     }
1131
1132   if (out_port < 1024 || out_port > 65535)
1133     {
1134       error = clib_error_return (0, "wrong port, must be <1024-65535>");
1135       goto done;
1136     }
1137
1138   dm = snat_det_map_by_out (sm, &out_addr);
1139   if (!dm)
1140     vlib_cli_output (vm, "no match");
1141   else
1142     {
1143       snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
1144       vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
1145     }
1146
1147 done:
1148   unformat_free (line_input);
1149
1150   return error;
1151 }
1152
1153 static clib_error_t *
1154 set_timeout_command_fn (vlib_main_t * vm,
1155                         unformat_input_t * input, vlib_cli_command_t * cmd)
1156 {
1157   snat_main_t *sm = &snat_main;
1158   unformat_input_t _line_input, *line_input = &_line_input;
1159   clib_error_t *error = 0;
1160
1161   /* Get a line of input. */
1162   if (!unformat_user (input, unformat_line_input, line_input))
1163     return 0;
1164
1165   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1166     {
1167       if (unformat (line_input, "udp %u", &sm->udp_timeout))
1168         ;
1169       else if (unformat (line_input, "tcp-established %u",
1170                          &sm->tcp_established_timeout))
1171         ;
1172       else if (unformat (line_input, "tcp-transitory %u",
1173                          &sm->tcp_transitory_timeout))
1174         ;
1175       else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
1176         ;
1177       else if (unformat (line_input, "reset"))
1178         {
1179           sm->udp_timeout = SNAT_UDP_TIMEOUT;
1180           sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1181           sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1182           sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1183         }
1184       else
1185         {
1186           error = clib_error_return (0, "unknown input '%U'",
1187                                      format_unformat_error, line_input);
1188           goto done;
1189         }
1190     }
1191
1192 done:
1193   unformat_free (line_input);
1194
1195   return error;
1196 }
1197
1198 static clib_error_t *
1199 nat44_det_show_timeouts_command_fn (vlib_main_t * vm,
1200                                     unformat_input_t * input,
1201                                     vlib_cli_command_t * cmd)
1202 {
1203   snat_main_t *sm = &snat_main;
1204
1205   vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
1206   vlib_cli_output (vm, "tcp-established timeout: %dsec",
1207                    sm->tcp_established_timeout);
1208   vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
1209                    sm->tcp_transitory_timeout);
1210   vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
1211
1212   return 0;
1213 }
1214
1215 static clib_error_t *
1216 nat44_det_show_sessions_command_fn (vlib_main_t * vm,
1217                                     unformat_input_t * input,
1218                                     vlib_cli_command_t * cmd)
1219 {
1220   snat_main_t *sm = &snat_main;
1221   snat_det_map_t *dm;
1222   snat_det_session_t *ses;
1223   int i;
1224
1225   vlib_cli_output (vm, "NAT44 deterministic sessions:");
1226   /* *INDENT-OFF* */
1227   pool_foreach (dm, sm->det_maps,
1228   ({
1229     vec_foreach_index (i, dm->sessions)
1230       {
1231         ses = vec_elt_at_index (dm->sessions, i);
1232         if (ses->in_port)
1233           vlib_cli_output (vm, "  %U", format_det_map_ses, dm, ses, &i);
1234       }
1235   }));
1236   /* *INDENT-ON* */
1237   return 0;
1238 }
1239
1240 static clib_error_t *
1241 snat_det_close_session_out_fn (vlib_main_t * vm,
1242                                unformat_input_t * input,
1243                                vlib_cli_command_t * cmd)
1244 {
1245   snat_main_t *sm = &snat_main;
1246   unformat_input_t _line_input, *line_input = &_line_input;
1247   ip4_address_t out_addr, ext_addr, in_addr;
1248   u32 out_port, ext_port;
1249   snat_det_map_t *dm;
1250   snat_det_session_t *ses;
1251   snat_det_out_key_t key;
1252   clib_error_t *error = 0;
1253
1254   /* Get a line of input. */
1255   if (!unformat_user (input, unformat_line_input, line_input))
1256     return 0;
1257
1258   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1259     {
1260       if (unformat (line_input, "%U:%d %U:%d",
1261                     unformat_ip4_address, &out_addr, &out_port,
1262                     unformat_ip4_address, &ext_addr, &ext_port))
1263         ;
1264       else
1265         {
1266           error = clib_error_return (0, "unknown input '%U'",
1267                                      format_unformat_error, line_input);
1268           goto done;
1269         }
1270     }
1271
1272   unformat_free (line_input);
1273
1274   dm = snat_det_map_by_out (sm, &out_addr);
1275   if (!dm)
1276     vlib_cli_output (vm, "no match");
1277   else
1278     {
1279       snat_det_reverse (dm, &ext_addr, (u16) out_port, &in_addr);
1280       key.ext_host_addr = out_addr;
1281       key.ext_host_port = ntohs ((u16) ext_port);
1282       key.out_port = ntohs ((u16) out_port);
1283       ses = snat_det_get_ses_by_out (dm, &out_addr, key.as_u64);
1284       if (!ses)
1285         vlib_cli_output (vm, "no match");
1286       else
1287         snat_det_ses_close (dm, ses);
1288     }
1289
1290 done:
1291   unformat_free (line_input);
1292
1293   return error;
1294 }
1295
1296 static clib_error_t *
1297 snat_det_close_session_in_fn (vlib_main_t * vm,
1298                               unformat_input_t * input,
1299                               vlib_cli_command_t * cmd)
1300 {
1301   snat_main_t *sm = &snat_main;
1302   unformat_input_t _line_input, *line_input = &_line_input;
1303   ip4_address_t in_addr, ext_addr;
1304   u32 in_port, ext_port;
1305   snat_det_map_t *dm;
1306   snat_det_session_t *ses;
1307   snat_det_out_key_t key;
1308   clib_error_t *error = 0;
1309
1310   /* Get a line of input. */
1311   if (!unformat_user (input, unformat_line_input, line_input))
1312     return 0;
1313
1314   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1315     {
1316       if (unformat (line_input, "%U:%d %U:%d",
1317                     unformat_ip4_address, &in_addr, &in_port,
1318                     unformat_ip4_address, &ext_addr, &ext_port))
1319         ;
1320       else
1321         {
1322           error = clib_error_return (0, "unknown input '%U'",
1323                                      format_unformat_error, line_input);
1324           goto done;
1325         }
1326     }
1327
1328   unformat_free (line_input);
1329
1330   dm = snat_det_map_by_user (sm, &in_addr);
1331   if (!dm)
1332     vlib_cli_output (vm, "no match");
1333   else
1334     {
1335       key.ext_host_addr = ext_addr;
1336       key.ext_host_port = ntohs ((u16) ext_port);
1337       ses =
1338         snat_det_find_ses_by_in (dm, &in_addr, ntohs ((u16) in_port), key);
1339       if (!ses)
1340         vlib_cli_output (vm, "no match");
1341       else
1342         snat_det_ses_close (dm, ses);
1343     }
1344
1345 done:
1346   unformat_free (line_input);
1347
1348   return error;
1349 }
1350 /* *INDENT-OFF* */
1351
1352 /*?
1353  * @cliexpar
1354  * @cliexstart{set snat workers}
1355  * Set NAT workers if 2 or more workers available, use:
1356  *  vpp# set snat workers 0-2,5
1357  * @cliexend
1358 ?*/
1359 VLIB_CLI_COMMAND (set_workers_command, static) = {
1360   .path = "set nat workers",
1361   .function = set_workers_command_fn,
1362   .short_help = "set nat workers <workers-list>",
1363 };
1364
1365 /*?
1366  * @cliexpar
1367  * @cliexstart{show nat workers}
1368  * Show NAT workers.
1369  *  vpp# show nat workers:
1370  *  2 workers
1371  *    vpp_wk_0
1372  *    vpp_wk_1
1373  * @cliexend
1374 ?*/
1375 VLIB_CLI_COMMAND (nat_show_workers_command, static) = {
1376   .path = "show nat workers",
1377   .short_help = "show nat workers",
1378   .function = nat_show_workers_commnad_fn,
1379 };
1380
1381 /*?
1382  * @cliexpar
1383  * @cliexstart{snat ipfix logging}
1384  * To enable NAT IPFIX logging use:
1385  *  vpp# nat ipfix logging
1386  * To set IPFIX exporter use:
1387  *  vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1388  * @cliexend
1389 ?*/
1390 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1391   .path = "nat ipfix logging",
1392   .function = snat_ipfix_logging_enable_disable_command_fn,
1393   .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1394 };
1395
1396 /*?
1397  * @cliexpar
1398  * @cliexstart{nat addr-port-assignment-alg}
1399  * Set address and port assignment algorithm
1400  * For the MAP-E CE limit port choice based on PSID use:
1401  *  vpp# nat addr-port-assignment-alg map-e psid 10 psid-offset 6 psid-len 6
1402  * To set standard (default) address and port assignment algorithm use:
1403  *  vpp# nat addr-port-assignment-alg default
1404  * @cliexend
1405 ?*/
1406 VLIB_CLI_COMMAND (nat44_set_alloc_addr_and_port_alg_command, static) = {
1407     .path = "nat addr-port-assignment-alg",
1408     .short_help = "nat addr-port-assignment-alg <alg-name> [<alg-params>]",
1409     .function = nat44_set_alloc_addr_and_port_alg_command_fn,
1410 };
1411
1412 /*?
1413  * @cliexpar
1414  * @cliexstart{nat44 add address}
1415  * Add/delete NAT44 pool address.
1416  * To add NAT44 pool address use:
1417  *  vpp# nat44 add address 172.16.1.3
1418  *  vpp# nat44 add address 172.16.2.2 - 172.16.2.24
1419  * To add NAT44 pool address for specific tenant (identified by VRF id) use:
1420  *  vpp# nat44 add address 172.16.1.3 tenant-vrf 10
1421  * @cliexend
1422 ?*/
1423 VLIB_CLI_COMMAND (add_address_command, static) = {
1424   .path = "nat44 add address",
1425   .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
1426                 "[tenant-vrf <vrf-id>] [twice-nat] [del]",
1427   .function = add_address_command_fn,
1428 };
1429
1430 /*?
1431  * @cliexpar
1432  * @cliexstart{show nat44 addresses}
1433  * Show NAT44 pool addresses.
1434  * vpp# show nat44 addresses
1435  * NAT44 pool addresses:
1436  * 172.16.2.2
1437  *   tenant VRF independent
1438  *   10 busy udp ports
1439  *   0 busy tcp ports
1440  *   0 busy icmp ports
1441  * 172.16.1.3
1442  *   tenant VRF: 10
1443  *   0 busy udp ports
1444  *   2 busy tcp ports
1445  *   0 busy icmp ports
1446  * NAT44 twice-nat pool addresses:
1447  * 10.20.30.72
1448  *   tenant VRF independent
1449  *   0 busy udp ports
1450  *   0 busy tcp ports
1451  *   0 busy icmp ports
1452  * @cliexend
1453 ?*/
1454 VLIB_CLI_COMMAND (nat44_show_addresses_command, static) = {
1455   .path = "show nat44 addresses",
1456   .short_help = "show nat44 addresses",
1457   .function = nat44_show_addresses_command_fn,
1458 };
1459
1460 /*?
1461  * @cliexpar
1462  * @cliexstart{set interface nat44}
1463  * Enable/disable NAT44 feature on the interface.
1464  * To enable NAT44 feature with local network interface use:
1465  *  vpp# set interface nat44 in GigabitEthernet0/8/0
1466  * To enable NAT44 feature with external network interface use:
1467  *  vpp# set interface nat44 out GigabitEthernet0/a/0
1468  * @cliexend
1469 ?*/
1470 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1471   .path = "set interface nat44",
1472   .function = snat_feature_command_fn,
1473   .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
1474                 "[del]",
1475 };
1476
1477 /*?
1478  * @cliexpar
1479  * @cliexstart{show nat44 interfaces}
1480  * Show interfaces with NAT44 feature.
1481  * vpp# show nat44 interfaces
1482  * NAT44 interfaces:
1483  *  GigabitEthernet0/8/0 in
1484  *  GigabitEthernet0/a/0 out
1485  * @cliexend
1486 ?*/
1487 VLIB_CLI_COMMAND (nat44_show_interfaces_command, static) = {
1488   .path = "show nat44 interfaces",
1489   .short_help = "show nat44 interfaces",
1490   .function = nat44_show_interfaces_command_fn,
1491 };
1492
1493 /*?
1494  * @cliexpar
1495  * @cliexstart{nat44 add static mapping}
1496  * Static mapping allows hosts on the external network to initiate connection
1497  * to to the local network host.
1498  * To create static mapping between local host address 10.0.0.3 port 6303 and
1499  * external address 4.4.4.4 port 3606 for TCP protocol use:
1500  *  vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
1501  * If not runnig "static mapping only" NAT plugin mode use before:
1502  *  vpp# nat44 add address 4.4.4.4
1503  * To create static mapping between local and external address use:
1504  *  vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
1505  * @cliexend
1506 ?*/
1507 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1508   .path = "nat44 add static mapping",
1509   .function = add_static_mapping_command_fn,
1510   .short_help =
1511     "nat44 add static mapping tcp|udp|icmp local <addr> [<port>] "
1512     "external <addr> [<port>] [vrf <table-id>] [twice-nat] [out2in-only] [del]",
1513 };
1514
1515 /*?
1516  * @cliexpar
1517  * @cliexstart{nat44 add identity mapping}
1518  * Identity mapping translate an IP address to itself.
1519  * To create identity mapping for address 10.0.0.3 port 6303 for TCP protocol
1520  * use:
1521  *  vpp# nat44 add identity mapping 10.0.0.3 tcp 6303
1522  * To create identity mapping for address 10.0.0.3 use:
1523  *  vpp# nat44 add identity mapping 10.0.0.3
1524  * To create identity mapping for DHCP addressed interface use:
1525  *  vpp# nat44 add identity mapping GigabitEthernet0/a/0 tcp 3606
1526  * @cliexend
1527 ?*/
1528 VLIB_CLI_COMMAND (add_identity_mapping_command, static) = {
1529   .path = "nat44 add identity mapping",
1530   .function = add_identity_mapping_command_fn,
1531   .short_help = "nat44 add identity mapping <interface>|<ip4-addr> "
1532     "[<protocol> <port>] [vrf <table-id>] [del]",
1533 };
1534
1535 /*?
1536  * @cliexpar
1537  * @cliexstart{nat44 add load-balancing static mapping}
1538  * Service load balancing using NAT44
1539  * To add static mapping with load balancing for service with external IP
1540  * address 1.2.3.4 and TCP port 80 and mapped to 2 local servers
1541  * 10.100.10.10:8080 and 10.100.10.20:8080 with probability 80% resp. 20% use:
1542  *  vpp# nat44 add load-balancing static mapping protocol tcp external 1.2.3.4:80 local 10.100.10.10:8080 probability 80 local 10.100.10.20:8080 probability 20
1543  * @cliexend
1544 ?*/
1545 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
1546   .path = "nat44 add load-balancing static mapping",
1547   .function = add_lb_static_mapping_command_fn,
1548   .short_help =
1549     "nat44 add load-balancing static mapping protocol tcp|udp "
1550     "external <addr>:<port> local <addr>:<port> probability <n> [twice-nat] "
1551     "[vrf <table-id>] [out2in-only] [del]",
1552 };
1553
1554 /*?
1555  * @cliexpar
1556  * @cliexstart{show nat44 static mappings}
1557  * Show NAT44 static mappings.
1558  * vpp# show nat44 static mappings
1559  * NAT44 static mappings:
1560  *  local 10.0.0.3 external 4.4.4.4 vrf 0
1561  *  tcp local 192.168.0.4:6303 external 4.4.4.3:3606 vrf 0
1562  *  tcp vrf 0 external 1.2.3.4:80  out2in-only
1563  *   local 10.100.10.10:8080 probability 80
1564  *   local 10.100.10.20:8080 probability 20
1565  *  tcp local 10.100.3.8:8080 external 169.10.10.1:80 vrf 0 twice-nat
1566  *  tcp local 10.0.0.10:3603 external GigabitEthernet0/a/0:6306 vrf 10
1567  * @cliexend
1568 ?*/
1569 VLIB_CLI_COMMAND (nat44_show_static_mappings_command, static) = {
1570   .path = "show nat44 static mappings",
1571   .short_help = "show nat44 static mappings",
1572   .function = nat44_show_static_mappings_command_fn,
1573 };
1574
1575 /*?
1576  * @cliexpar
1577  * @cliexstart{nat44 add interface address}
1578  * Use NAT44 pool address from specific interfce
1579  * To add NAT44 pool address from specific interface use:
1580  *  vpp# nat44 add interface address GigabitEthernet0/8/0
1581  * @cliexend
1582 ?*/
1583 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
1584     .path = "nat44 add interface address",
1585     .short_help = "nat44 add interface address <interface> [twice-nat] [del]",
1586     .function = snat_add_interface_address_command_fn,
1587 };
1588
1589 /*?
1590  * @cliexpar
1591  * @cliexstart{show nat44 interface address}
1592  * Show NAT44 pool address interfaces
1593  * vpp# show nat44 interface address
1594  * NAT44 pool address interfaces:
1595  *  GigabitEthernet0/a/0
1596  * NAT44 twice-nat pool address interfaces:
1597  *  GigabitEthernet0/8/0
1598  * @cliexend
1599 ?*/
1600 VLIB_CLI_COMMAND (nat44_show_interface_address_command, static) = {
1601   .path = "show nat44 interface address",
1602   .short_help = "show nat44 interface address",
1603   .function = nat44_show_interface_address_command_fn,
1604 };
1605
1606 /*?
1607  * @cliexpar
1608  * @cliexstart{show nat44 sessions}
1609  * Show NAT44 sessions.
1610  * @cliexend
1611 ?*/
1612 VLIB_CLI_COMMAND (nat44_show_sessions_command, static) = {
1613   .path = "show nat44 sessions",
1614   .short_help = "show nat44 sessions [detail]",
1615   .function = nat44_show_sessions_command_fn,
1616 };
1617
1618 /*?
1619  * @cliexpar
1620  * @cliexstart{nat44 del session}
1621  * To administratively delete NAT44 session by inside address and port use:
1622  *  vpp# nat44 del session in 10.0.0.3:6303 tcp
1623  * To administratively delete NAT44 session by outside address and port use:
1624  *  vpp# nat44 del session out 1.0.0.3:6033 udp
1625  * @cliexend
1626 ?*/
1627 VLIB_CLI_COMMAND (nat44_del_session_command, static) = {
1628     .path = "nat44 del session",
1629     .short_help = "nat44 del session in|out <addr>:<port> tcp|udp|icmp [vrf <id>]",
1630     .function = nat44_del_session_command_fn,
1631 };
1632
1633 /*?
1634  * @cliexpar
1635  * @cliexstart{nat44 forwarding}
1636  * Enable or disable forwarding
1637  * Forward packets which don't match existing translation
1638  * or static mapping instead of dropping them.
1639  * To enable forwarding, use:
1640  *  vpp# nat44 forwarding enable
1641  * To disable forwarding, use:
1642  *  vpp# nat44 forwarding disable
1643  * @cliexend
1644 ?*/
1645 VLIB_CLI_COMMAND (snat_forwarding_set_command, static) = {
1646   .path = "nat44 forwarding",
1647   .short_help = "nat44 forwarding enable|disable",
1648   .function = snat_forwarding_set_command_fn,
1649 };
1650
1651 /*?
1652  * @cliexpar
1653  * @cliexstart{nat44 deterministic add}
1654  * Create bijective mapping of inside address to outside address and port range
1655  * pairs, with the purpose of enabling deterministic NAT to reduce logging in
1656  * CGN deployments.
1657  * To create deterministic mapping between inside network 10.0.0.0/18 and
1658  * outside network 1.1.1.0/30 use:
1659  * # vpp# nat44 deterministic add in 10.0.0.0/18 out 1.1.1.0/30
1660  * @cliexend
1661 ?*/
1662 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
1663     .path = "nat44 deterministic add",
1664     .short_help = "nat44 deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
1665     .function = snat_det_map_command_fn,
1666 };
1667
1668 /*?
1669  * @cliexpar
1670  * @cliexpstart{show nat44 deterministic mappings}
1671  * Show NAT44 deterministic mappings
1672  * vpp# show nat44 deterministic mappings
1673  * NAT44 deterministic mappings:
1674  *  in 10.0.0.0/24 out 1.1.1.1/32
1675  *   outside address sharing ratio: 256
1676  *   number of ports per inside host: 252
1677  *   sessions number: 0
1678  * @cliexend
1679 ?*/
1680 VLIB_CLI_COMMAND (nat44_det_show_mappings_command, static) = {
1681     .path = "show nat44 deterministic mappings",
1682     .short_help = "show nat44 deterministic mappings",
1683     .function = nat44_det_show_mappings_command_fn,
1684 };
1685
1686 /*?
1687  * @cliexpar
1688  * @cliexstart{nat44 deterministic forward}
1689  * Return outside address and port range from inside address for deterministic
1690  * NAT.
1691  * To obtain outside address and port of inside host use:
1692  *  vpp# nat44 deterministic forward 10.0.0.2
1693  *  1.1.1.0:<1054-1068>
1694  * @cliexend
1695 ?*/
1696 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
1697     .path = "nat44 deterministic forward",
1698     .short_help = "nat44 deterministic forward <addr>",
1699     .function = snat_det_forward_command_fn,
1700 };
1701
1702 /*?
1703  * @cliexpar
1704  * @cliexstart{nat44 deterministic reverse}
1705  * Return inside address from outside address and port for deterministic NAT.
1706  * To obtain inside host address from outside address and port use:
1707  *  #vpp nat44 deterministic reverse 1.1.1.1:1276
1708  *  10.0.16.16
1709  * @cliexend
1710 ?*/
1711 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
1712     .path = "nat44 deterministic reverse",
1713     .short_help = "nat44 deterministic reverse <addr>:<port>",
1714     .function = snat_det_reverse_command_fn,
1715 };
1716
1717 /*?
1718  * @cliexpar
1719  * @cliexstart{set nat44 deterministic timeout}
1720  * Set values of timeouts for deterministic NAT (in seconds), use:
1721  *  vpp# set nat44 deterministic timeout udp 120 tcp-established 7500
1722  *  tcp-transitory 250 icmp 90
1723  * To reset default values use:
1724  *  vpp# set nat44 deterministic timeout reset
1725  * @cliexend
1726 ?*/
1727 VLIB_CLI_COMMAND (set_timeout_command, static) = {
1728   .path = "set nat44 deterministic timeout",
1729   .function = set_timeout_command_fn,
1730   .short_help =
1731     "set nat44 deterministic timeout [udp <sec> | tcp-established <sec> "
1732     "tcp-transitory <sec> | icmp <sec> | reset]",
1733 };
1734
1735 /*?
1736  * @cliexpar
1737  * @cliexstart{show nat44 deterministic timeouts}
1738  * Show values of timeouts for deterministic NAT.
1739  * vpp# show nat44 deterministic timeouts
1740  * udp timeout: 300sec
1741  * tcp-established timeout: 7440sec
1742  * tcp-transitory timeout: 240sec
1743  * icmp timeout: 60sec
1744  * @cliexend
1745 ?*/
1746 VLIB_CLI_COMMAND (nat44_det_show_timeouts_command, static) = {
1747   .path = "show nat44 deterministic timeouts",
1748   .short_help = "show nat44 deterministic timeouts",
1749   .function = nat44_det_show_timeouts_command_fn,
1750 };
1751
1752 /*?
1753  * @cliexpar
1754  * @cliexstart{show nat44 deterministic sessions}
1755  * Show NAT44 deterministic sessions.
1756  * vpp# show nat44 deterministic sessions
1757  * NAT44 deterministic sessions:
1758  *   in 10.0.0.3:3005 out 1.1.1.2:1146 external host 172.16.1.2:3006 state: udp-active expire: 306
1759  *   in 10.0.0.3:3000 out 1.1.1.2:1141 external host 172.16.1.2:3001 state: udp-active expire: 306
1760  *   in 10.0.0.4:3005 out 1.1.1.2:1177 external host 172.16.1.2:3006 state: udp-active expire: 306
1761  * @cliexend
1762 ?*/
1763 VLIB_CLI_COMMAND (nat44_det_show_sessions_command, static) = {
1764   .path = "show nat44 deterministic sessions",
1765   .short_help = "show nat44 deterministic sessions",
1766   .function = nat44_det_show_sessions_command_fn,
1767 };
1768
1769 /*?
1770  * @cliexpar
1771  * @cliexstart{nat44 deterministic close session out}
1772  * Close session using outside ip address and port
1773  * and external ip address and port, use:
1774  *  vpp# nat44 deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
1775  * @cliexend
1776 ?*/
1777 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
1778   .path = "nat44 deterministic close session out",
1779   .short_help = "nat44 deterministic close session out "
1780                 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
1781   .function = snat_det_close_session_out_fn,
1782 };
1783
1784 /*?
1785  * @cliexpar
1786  * @cliexstart{nat44 deterministic close session in}
1787  * Close session using inside ip address and port
1788  * and external ip address and port, use:
1789  *  vpp# nat44 deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
1790  * @cliexend
1791 ?*/
1792 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
1793   .path = "nat44 deterministic close session in",
1794   .short_help = "nat44 deterministic close session in "
1795                 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
1796   .function = snat_det_close_session_in_fn,
1797 };
1798
1799 /* *INDENT-ON* */
1800
1801 /*
1802  * fd.io coding-style-patch-verification: ON
1803  *
1804  * Local Variables:
1805  * eval: (c-set-style "gnu")
1806  * End:
1807  */