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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <snat/nat64.h>
21 #include <snat/snat.h>
22 #include <vnet/fib/fib_table.h>
25 nat64_add_del_pool_addr_command_fn (vlib_main_t * vm,
26 unformat_input_t * input,
27 vlib_cli_command_t * cmd)
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;
36 clib_error_t *error = 0;
39 return clib_error_return (0,
40 "NAT64 disabled, multi thread not supported");
42 /* Get a line of input. */
43 if (!unformat_user (input, unformat_line_input, line_input))
46 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
48 if (unformat (line_input, "%U - %U",
49 unformat_ip4_address, &start_addr,
50 unformat_ip4_address, &end_addr))
52 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
54 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
55 end_addr = start_addr;
56 else if (unformat (line_input, "del"))
60 error = clib_error_return (0, "unknown input '%U'",
61 format_unformat_error, line_input);
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);
69 if (end_host_order < start_host_order)
71 error = clib_error_return (0, "end address less than start address");
75 count = (end_host_order - start_host_order) + 1;
76 this_addr = start_addr;
78 for (i = 0; i < count; i++)
80 rv = nat64_add_del_pool_addr (&this_addr, vrf_id, is_add);
84 case VNET_API_ERROR_NO_SUCH_ENTRY:
86 clib_error_return (0, "NAT64 pool address %U not exist.",
87 format_ip4_address, &this_addr);
89 case VNET_API_ERROR_VALUE_EXIST:
91 clib_error_return (0, "NAT64 pool address %U exist.",
92 format_ip4_address, &this_addr);
98 increment_v4_address (&this_addr);
102 unformat_free (line_input);
108 nat64_cli_pool_walk (snat_address_t * ap, void *ctx)
110 vlib_main_t *vm = ctx;
112 if (ap->fib_index != ~0)
115 fib = fib_table_get (ap->fib_index, FIB_PROTOCOL_IP6);
118 vlib_cli_output (vm, " %U tenant VRF: %u", format_ip4_address,
119 &ap->addr, fib->ft_table_id);
122 vlib_cli_output (vm, " %U", format_ip4_address, &ap->addr);
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)
132 nat64_main_t *nm = &nat64_main;
135 return clib_error_return (0,
136 "NAT64 disabled, multi thread not supported");
138 vlib_cli_output (vm, "NAT64 pool:");
139 nat64_pool_addr_walk (nat64_cli_pool_walk, vm);
144 static clib_error_t *
145 nat64_interface_feature_command_fn (vlib_main_t * vm,
147 input, vlib_cli_command_t * cmd)
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;
154 u32 *inside_sw_if_indices = 0;
155 u32 *outside_sw_if_indices = 0;
160 return clib_error_return (0,
161 "NAT64 disabled, multi thread not supported");
163 /* Get a line of input. */
164 if (!unformat_user (input, unformat_line_input, line_input))
167 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
169 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
171 vec_add1 (inside_sw_if_indices, sw_if_index);
172 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
174 vec_add1 (outside_sw_if_indices, sw_if_index);
175 else if (unformat (line_input, "del"))
179 error = clib_error_return (0, "unknown input '%U'",
180 format_unformat_error, line_input);
185 if (vec_len (inside_sw_if_indices))
187 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
189 sw_if_index = inside_sw_if_indices[i];
190 rv = nat64_add_del_interface (sw_if_index, 1, is_add);
193 case VNET_API_ERROR_NO_SUCH_ENTRY:
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));
199 case VNET_API_ERROR_VALUE_EXIST:
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));
205 case VNET_API_ERROR_INVALID_VALUE:
206 case VNET_API_ERROR_INVALID_VALUE_2:
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));
220 if (vec_len (outside_sw_if_indices))
222 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
224 sw_if_index = outside_sw_if_indices[i];
225 rv = nat64_add_del_interface (sw_if_index, 0, is_add);
228 case VNET_API_ERROR_NO_SUCH_ENTRY:
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));
234 case VNET_API_ERROR_VALUE_EXIST:
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));
240 case VNET_API_ERROR_INVALID_VALUE:
241 case VNET_API_ERROR_INVALID_VALUE_2:
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));
256 unformat_free (line_input);
257 vec_free (inside_sw_if_indices);
258 vec_free (outside_sw_if_indices);
264 nat64_cli_interface_walk (snat_interface_t * i, void *ctx)
266 vlib_main_t *vm = ctx;
267 vnet_main_t *vnm = vnet_get_main ();
269 vlib_cli_output (vm, " %U %s", format_vnet_sw_interface_name, vnm,
270 vnet_get_sw_interface (vnm, i->sw_if_index),
271 i->is_inside ? "in" : "out");
275 static clib_error_t *
276 nat64_show_interfaces_command_fn (vlib_main_t * vm,
278 input, vlib_cli_command_t * cmd)
280 nat64_main_t *nm = &nat64_main;
283 return clib_error_return (0,
284 "NAT64 disabled, multi thread not supported");
286 vlib_cli_output (vm, "NAT64 interfaces:");
287 nat64_interfaces_walk (nat64_cli_interface_walk, vm);
292 static clib_error_t *
293 nat64_add_del_static_bib_command_fn (vlib_main_t *
296 * input, vlib_cli_command_t * cmd)
298 nat64_main_t *nm = &nat64_main;
299 unformat_input_t _line_input, *line_input = &_line_input;
300 clib_error_t *error = 0;
302 ip6_address_t in_addr;
303 ip4_address_t out_addr;
307 snat_protocol_t proto = 0;
312 return clib_error_return (0,
313 "NAT64 disabled, multi thread not supported");
315 if (!unformat_user (input, unformat_line_input, line_input))
318 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
320 if (unformat (line_input, "%U %u", unformat_ip6_address,
323 else if (unformat (line_input, "%U %u", unformat_ip4_address,
324 &out_addr, &out_port))
326 else if (unformat (line_input, "vrf %u", &vrf_id))
328 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
330 else if (unformat (line_input, "del"))
334 error = clib_error_return (0, "unknown input: '%U'",
335 format_unformat_error, line_input);
342 error = clib_error_return (0, "inside port and address must be set");
348 error = clib_error_return (0, "outside port and address must be set");
352 p = snat_proto_to_ip_proto (proto);
355 nat64_add_del_static_bib_entry (&in_addr, &out_addr, in_port, out_port, p,
360 case VNET_API_ERROR_NO_SUCH_ENTRY:
361 error = clib_error_return (0, "NAT64 BIB entry not exist.");
363 case VNET_API_ERROR_VALUE_EXIST:
364 error = clib_error_return (0, "NAT64 BIB entry exist.");
366 case VNET_API_ERROR_UNSPECIFIED:
367 error = clib_error_return (0, "Crerate NAT64 BIB entry failed.");
369 case VNET_API_ERROR_INVALID_VALUE:
371 clib_error_return (0, "Outside addres %U and port %u already in use.",
372 format_ip4_address, &out_addr, out_port);
379 unformat_free (line_input);
385 nat64_cli_bib_walk (nat64_db_bib_entry_t * bibe, void *ctx)
387 vlib_main_t *vm = ctx;
390 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
394 vlib_cli_output (vm, " %U %u %U %u %U vrf %u %s %u sessions",
395 format_ip6_address, &bibe->in_addr,
396 clib_net_to_host_u16 (bibe->in_port), format_ip4_address,
397 &bibe->out_addr, clib_net_to_host_u16 (bibe->out_port),
398 format_snat_protocol, bibe->proto, fib->ft_table_id,
399 bibe->is_static ? "static" : "dynamic", bibe->ses_num);
403 static clib_error_t *
404 nat64_show_bib_command_fn (vlib_main_t * vm,
405 unformat_input_t * input, vlib_cli_command_t * cmd)
407 nat64_main_t *nm = &nat64_main;
408 unformat_input_t _line_input, *line_input = &_line_input;
409 clib_error_t *error = 0;
410 snat_protocol_t proto = 0;
413 return clib_error_return (0,
414 "NAT64 disabled, multi thread not supported");
416 if (!unformat_user (input, unformat_line_input, line_input))
419 if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
423 error = clib_error_return (0, "unknown input: '%U'",
424 format_unformat_error, line_input);
428 vlib_cli_output (vm, "NAT64 %U BIB:", format_snat_protocol, proto);
429 nat64_db_bib_walk (&nm->db, proto, nat64_cli_bib_walk, vm);
432 unformat_free (line_input);
437 static clib_error_t *
438 nat64_set_timeouts_command_fn (vlib_main_t * vm, unformat_input_t * input,
439 vlib_cli_command_t * cmd)
441 nat64_main_t *nm = &nat64_main;
442 unformat_input_t _line_input, *line_input = &_line_input;
443 clib_error_t *error = 0;
444 u32 timeout, tcp_trans, tcp_est, tcp_incoming_syn;
446 tcp_trans = nat64_get_tcp_trans_timeout ();
447 tcp_est = nat64_get_tcp_est_timeout ();
448 tcp_incoming_syn = nat64_get_tcp_incoming_syn_timeout ();
451 return clib_error_return (0,
452 "NAT64 disabled, multi thread not supported");
454 if (!unformat_user (input, unformat_line_input, line_input))
457 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
459 if (unformat (line_input, "udp %u", &timeout))
461 if (nat64_set_udp_timeout (timeout))
463 error = clib_error_return (0, "Invalid UDP timeout value");
467 else if (unformat (line_input, "icmp %u", &timeout))
469 if (nat64_set_icmp_timeout (timeout))
471 error = clib_error_return (0, "Invalid ICMP timeout value");
475 else if (unformat (line_input, "tcp-trans %u", &tcp_trans))
477 if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
480 clib_error_return (0, "Invalid TCP transitory tiemout value");
484 else if (unformat (line_input, "tcp-est %u", &tcp_est))
486 if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
489 clib_error_return (0,
490 "Invalid TCP established tiemout value");
495 if (unformat (line_input, "tcp-incoming-syn %u", &tcp_incoming_syn))
497 if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
500 clib_error_return (0,
501 "Invalid TCP incoming SYN tiemout value");
505 else if (unformat (line_input, "reset"))
507 nat64_set_udp_timeout (0);
508 nat64_set_icmp_timeout (0);
509 nat64_set_tcp_timeouts (0, 0, 0);
513 error = clib_error_return (0, "unknown input '%U'",
514 format_unformat_error, line_input);
520 unformat_free (line_input);
525 static clib_error_t *
526 nat64_show_timeouts_command_fn (vlib_main_t * vm, unformat_input_t * input,
527 vlib_cli_command_t * cmd)
529 nat64_main_t *nm = &nat64_main;
532 return clib_error_return (0,
533 "NAT64 disabled, multi thread not supported");
535 vlib_cli_output (vm, "NAT64 session timeouts:");
536 vlib_cli_output (vm, " UDP %usec", nat64_get_udp_timeout ());
537 vlib_cli_output (vm, " ICMP %usec", nat64_get_icmp_timeout ());
538 vlib_cli_output (vm, " TCP transitory %usec",
539 nat64_get_tcp_trans_timeout ());
540 vlib_cli_output (vm, " TCP established %usec",
541 nat64_get_tcp_est_timeout ());
542 vlib_cli_output (vm, " TCP incoming SYN %usec",
543 nat64_get_tcp_incoming_syn_timeout ());
549 nat64_cli_st_walk (nat64_db_st_entry_t * ste, void *ctx)
551 vlib_main_t *vm = ctx;
552 nat64_main_t *nm = &nat64_main;
553 nat64_db_bib_entry_t *bibe;
556 bibe = nat64_db_bib_entry_by_index (&nm->db, ste->proto, ste->bibe_index);
560 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
564 u32 vrf_id = fib->ft_table_id;
566 if (ste->proto == SNAT_PROTOCOL_ICMP)
567 vlib_cli_output (vm, " %U %U %u %U %U %u %U vrf %u",
568 format_ip6_address, &bibe->in_addr,
569 format_ip6_address, &ste->in_r_addr,
570 clib_net_to_host_u16 (bibe->in_port),
571 format_ip4_address, &bibe->out_addr,
572 format_ip4_address, &ste->out_r_addr,
573 clib_net_to_host_u16 (bibe->out_port),
574 format_snat_protocol, bibe->proto, vrf_id);
576 vlib_cli_output (vm, " %U %u %U %u %U %u %U %u %U vrf %u",
577 format_ip6_address, &bibe->in_addr,
578 clib_net_to_host_u16 (bibe->in_port),
579 format_ip6_address, &ste->in_r_addr,
580 clib_net_to_host_u16 (ste->r_port),
581 format_ip4_address, &bibe->out_addr,
582 clib_net_to_host_u16 (bibe->out_port),
583 format_ip4_address, &ste->out_r_addr,
584 clib_net_to_host_u16 (ste->r_port),
585 format_snat_protocol, bibe->proto, vrf_id);
589 static clib_error_t *
590 nat64_show_st_command_fn (vlib_main_t * vm,
591 unformat_input_t * input, vlib_cli_command_t * cmd)
593 nat64_main_t *nm = &nat64_main;
594 unformat_input_t _line_input, *line_input = &_line_input;
595 clib_error_t *error = 0;
596 snat_protocol_t proto = 0;
599 return clib_error_return (0,
600 "NAT64 disabled, multi thread not supported");
602 if (!unformat_user (input, unformat_line_input, line_input))
605 if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
609 error = clib_error_return (0, "unknown input: '%U'",
610 format_unformat_error, line_input);
614 vlib_cli_output (vm, "NAT64 %U session table:", format_snat_protocol,
616 nat64_db_st_walk (&nm->db, proto, nat64_cli_st_walk, vm);
619 unformat_free (line_input);
626 VLIB_CLI_COMMAND (nat64_add_pool_address_command, static) = {
627 .path = "nat64 add pool address",
628 .short_help = "nat64 add pool address <ip4-range-start> [- <ip4-range-end>] "
629 "[tenant-vrf <vrf-id>] [del]",
630 .function = nat64_add_del_pool_addr_command_fn,
633 VLIB_CLI_COMMAND (show_nat64_pool_command, static) = {
634 .path = "show nat64 pool",
635 .short_help = "show nat64 pool",
636 .function = nat64_show_pool_command_fn,
639 VLIB_CLI_COMMAND (set_interface_nat64_command, static) = {
640 .path = "set interface nat64",
641 .short_help = "set interface nat64 in|out <intfc> [del]",
642 .function = nat64_interface_feature_command_fn,
645 VLIB_CLI_COMMAND (show_nat64_interfaces_command, static) = {
646 .path = "show nat64 interfaces",
647 .short_help = "show nat64 interfaces",
648 .function = nat64_show_interfaces_command_fn,
651 VLIB_CLI_COMMAND (nat64_add_del_static_bib_command, static) = {
652 .path = "nat64 add static bib",
653 .short_help = "nat64 add static bib <ip6-addr> <port> <ip4-addr> <port> "
654 "tcp|udp|icmp [vfr <table-id>] [del]",
655 .function = nat64_add_del_static_bib_command_fn,
658 VLIB_CLI_COMMAND (show_nat64_bib_command, static) = {
659 .path = "show nat64 bib",
660 .short_help = "show nat64 bib tcp|udp|icmp",
661 .function = nat64_show_bib_command_fn,
664 VLIB_CLI_COMMAND (set_nat64_timeouts_command, static) = {
665 .path = "set nat64 timeouts",
666 .short_help = "set nat64 timeouts udp <sec> icmp <sec> tcp-trans <sec> "
667 "tcp-est <sec> tcp-incoming-syn <sec> | reset",
668 .function = nat64_set_timeouts_command_fn,
671 VLIB_CLI_COMMAND (show_nat64_timeouts_command, static) = {
672 .path = "show nat64 tiemouts",
673 .short_help = "show nat64 tiemouts",
674 .function = nat64_show_timeouts_command_fn,
677 VLIB_CLI_COMMAND (show_nat64_st_command, static) = {
678 .path = "show nat64 session table",
679 .short_help = "show nat64 session table tcp|udp|icmp",
680 .function = nat64_show_st_command_fn,
686 * fd.io coding-style-patch-verification: ON
689 * eval: (c-set-style "gnu")