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 unformat_input_t _line_input, *line_input = &_line_input;
30 ip4_address_t start_addr, end_addr, this_addr;
31 u32 start_host_order, end_host_order;
35 clib_error_t *error = 0;
37 /* Get a line of input. */
38 if (!unformat_user (input, unformat_line_input, line_input))
41 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
43 if (unformat (line_input, "%U - %U",
44 unformat_ip4_address, &start_addr,
45 unformat_ip4_address, &end_addr))
47 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
49 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
50 end_addr = start_addr;
51 else if (unformat (line_input, "del"))
55 error = clib_error_return (0, "unknown input '%U'",
56 format_unformat_error, line_input);
61 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
62 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
64 if (end_host_order < start_host_order)
66 error = clib_error_return (0, "end address less than start address");
70 count = (end_host_order - start_host_order) + 1;
71 this_addr = start_addr;
73 for (i = 0; i < count; i++)
75 rv = nat64_add_del_pool_addr (&this_addr, vrf_id, is_add);
79 case VNET_API_ERROR_NO_SUCH_ENTRY:
81 clib_error_return (0, "NAT64 pool address %U not exist.",
82 format_ip4_address, &this_addr);
84 case VNET_API_ERROR_VALUE_EXIST:
86 clib_error_return (0, "NAT64 pool address %U exist.",
87 format_ip4_address, &this_addr);
93 increment_v4_address (&this_addr);
97 unformat_free (line_input);
103 nat64_cli_pool_walk (snat_address_t * ap, void *ctx)
105 vlib_main_t *vm = ctx;
107 if (ap->fib_index != ~0)
110 fib = fib_table_get (ap->fib_index, FIB_PROTOCOL_IP4);
113 vlib_cli_output (vm, " %U tenant VRF: %u", format_ip4_address,
114 &ap->addr, fib->ft_table_id);
117 vlib_cli_output (vm, " %U", format_ip4_address, &ap->addr);
122 static clib_error_t *
123 nat64_show_pool_command_fn (vlib_main_t * vm,
124 unformat_input_t * input,
125 vlib_cli_command_t * cmd)
127 vlib_cli_output (vm, "NAT64 pool:");
128 nat64_pool_addr_walk (nat64_cli_pool_walk, vm);
133 static clib_error_t *
134 nat64_interface_feature_command_fn (vlib_main_t * vm,
136 input, vlib_cli_command_t * cmd)
138 unformat_input_t _line_input, *line_input = &_line_input;
139 vnet_main_t *vnm = vnet_get_main ();
140 clib_error_t *error = 0;
142 u32 *inside_sw_if_indices = 0;
143 u32 *outside_sw_if_indices = 0;
147 /* Get a line of input. */
148 if (!unformat_user (input, unformat_line_input, line_input))
151 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
153 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
155 vec_add1 (inside_sw_if_indices, sw_if_index);
156 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
158 vec_add1 (outside_sw_if_indices, sw_if_index);
159 else if (unformat (line_input, "del"))
163 error = clib_error_return (0, "unknown input '%U'",
164 format_unformat_error, line_input);
169 if (vec_len (inside_sw_if_indices))
171 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
173 sw_if_index = inside_sw_if_indices[i];
174 rv = nat64_add_del_interface (sw_if_index, 1, is_add);
177 case VNET_API_ERROR_NO_SUCH_ENTRY:
179 clib_error_return (0, "%U NAT64 feature not enabled.",
180 format_vnet_sw_interface_name, vnm,
181 vnet_get_sw_interface (vnm, sw_if_index));
183 case VNET_API_ERROR_VALUE_EXIST:
185 clib_error_return (0, "%U NAT64 feature already enabled.",
186 format_vnet_sw_interface_name, vnm,
187 vnet_get_sw_interface (vnm, sw_if_index));
189 case VNET_API_ERROR_INVALID_VALUE:
190 case VNET_API_ERROR_INVALID_VALUE_2:
192 clib_error_return (0,
193 "%U NAT64 feature enable/disable failed.",
194 format_vnet_sw_interface_name, vnm,
195 vnet_get_sw_interface (vnm, sw_if_index));
204 if (vec_len (outside_sw_if_indices))
206 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
208 sw_if_index = outside_sw_if_indices[i];
209 rv = nat64_add_del_interface (sw_if_index, 0, is_add);
212 case VNET_API_ERROR_NO_SUCH_ENTRY:
214 clib_error_return (0, "%U NAT64 feature not enabled.",
215 format_vnet_sw_interface_name, vnm,
216 vnet_get_sw_interface (vnm, sw_if_index));
218 case VNET_API_ERROR_VALUE_EXIST:
220 clib_error_return (0, "%U NAT64 feature already enabled.",
221 format_vnet_sw_interface_name, vnm,
222 vnet_get_sw_interface (vnm, sw_if_index));
224 case VNET_API_ERROR_INVALID_VALUE:
225 case VNET_API_ERROR_INVALID_VALUE_2:
227 clib_error_return (0,
228 "%U NAT64 feature enable/disable failed.",
229 format_vnet_sw_interface_name, vnm,
230 vnet_get_sw_interface (vnm, sw_if_index));
240 unformat_free (line_input);
241 vec_free (inside_sw_if_indices);
242 vec_free (outside_sw_if_indices);
248 nat64_cli_interface_walk (snat_interface_t * i, void *ctx)
250 vlib_main_t *vm = ctx;
251 vnet_main_t *vnm = vnet_get_main ();
253 vlib_cli_output (vm, " %U %s", format_vnet_sw_interface_name, vnm,
254 vnet_get_sw_interface (vnm, i->sw_if_index),
255 i->is_inside ? "in" : "out");
259 static clib_error_t *
260 nat64_show_interfaces_command_fn (vlib_main_t * vm,
262 input, vlib_cli_command_t * cmd)
264 vlib_cli_output (vm, "NAT64 interfaces:");
265 nat64_interfaces_walk (nat64_cli_interface_walk, vm);
270 static clib_error_t *
271 nat64_add_del_static_bib_command_fn (vlib_main_t *
274 * input, vlib_cli_command_t * cmd)
276 unformat_input_t _line_input, *line_input = &_line_input;
277 clib_error_t *error = 0;
279 ip6_address_t in_addr;
280 ip4_address_t out_addr;
284 snat_protocol_t proto = 0;
288 if (!unformat_user (input, unformat_line_input, line_input))
291 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
293 if (unformat (line_input, "%U %u", unformat_ip6_address,
296 else if (unformat (line_input, "%U %u", unformat_ip4_address,
297 &out_addr, &out_port))
299 else if (unformat (line_input, "vrf %u", &vrf_id))
301 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
303 else if (unformat (line_input, "del"))
307 error = clib_error_return (0, "unknown input: '%U'",
308 format_unformat_error, line_input);
315 error = clib_error_return (0, "inside port and address must be set");
321 error = clib_error_return (0, "outside port and address must be set");
325 p = snat_proto_to_ip_proto (proto);
328 nat64_add_del_static_bib_entry (&in_addr, &out_addr, in_port, out_port, p,
333 case VNET_API_ERROR_NO_SUCH_ENTRY:
334 error = clib_error_return (0, "NAT64 BIB entry not exist.");
336 case VNET_API_ERROR_VALUE_EXIST:
337 error = clib_error_return (0, "NAT64 BIB entry exist.");
339 case VNET_API_ERROR_UNSPECIFIED:
340 error = clib_error_return (0, "Crerate NAT64 BIB entry failed.");
342 case VNET_API_ERROR_INVALID_VALUE:
344 clib_error_return (0, "Outside addres %U and port %u already in use.",
345 format_ip4_address, &out_addr, out_port);
352 unformat_free (line_input);
358 nat64_cli_bib_walk (nat64_db_bib_entry_t * bibe, void *ctx)
360 vlib_main_t *vm = ctx;
363 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
367 vlib_cli_output (vm, " %U %u %U %u %U vrf %u %s %u sessions",
368 format_ip6_address, &bibe->in_addr,
369 clib_net_to_host_u16 (bibe->in_port), format_ip4_address,
370 &bibe->out_addr, clib_net_to_host_u16 (bibe->out_port),
371 format_snat_protocol, bibe->proto, fib->ft_table_id,
372 bibe->is_static ? "static" : "dynamic", bibe->ses_num);
376 static clib_error_t *
377 nat64_show_bib_command_fn (vlib_main_t * vm,
378 unformat_input_t * input, vlib_cli_command_t * cmd)
380 nat64_main_t *nm = &nat64_main;
381 unformat_input_t _line_input, *line_input = &_line_input;
382 clib_error_t *error = 0;
383 snat_protocol_t proto = 0;
385 if (!unformat_user (input, unformat_line_input, line_input))
388 if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
392 error = clib_error_return (0, "unknown input: '%U'",
393 format_unformat_error, line_input);
397 vlib_cli_output (vm, "NAT64 %U BIB:", format_snat_protocol, proto);
398 nat64_db_bib_walk (&nm->db, proto, nat64_cli_bib_walk, vm);
401 unformat_free (line_input);
406 static clib_error_t *
407 nat64_set_timeouts_command_fn (vlib_main_t * vm, unformat_input_t * input,
408 vlib_cli_command_t * cmd)
410 unformat_input_t _line_input, *line_input = &_line_input;
411 clib_error_t *error = 0;
412 u32 timeout, tcp_trans, tcp_est, tcp_incoming_syn;
414 tcp_trans = nat64_get_tcp_trans_timeout ();
415 tcp_est = nat64_get_tcp_est_timeout ();
416 tcp_incoming_syn = nat64_get_tcp_incoming_syn_timeout ();
418 if (!unformat_user (input, unformat_line_input, line_input))
421 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
423 if (unformat (line_input, "udp %u", &timeout))
425 if (nat64_set_udp_timeout (timeout))
427 error = clib_error_return (0, "Invalid UDP timeout value");
431 else if (unformat (line_input, "icmp %u", &timeout))
433 if (nat64_set_icmp_timeout (timeout))
435 error = clib_error_return (0, "Invalid ICMP timeout value");
439 else if (unformat (line_input, "tcp-trans %u", &tcp_trans))
441 if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
444 clib_error_return (0, "Invalid TCP transitory tiemout value");
448 else if (unformat (line_input, "tcp-est %u", &tcp_est))
450 if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
453 clib_error_return (0,
454 "Invalid TCP established tiemout value");
459 if (unformat (line_input, "tcp-incoming-syn %u", &tcp_incoming_syn))
461 if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
464 clib_error_return (0,
465 "Invalid TCP incoming SYN tiemout value");
469 else if (unformat (line_input, "reset"))
471 nat64_set_udp_timeout (0);
472 nat64_set_icmp_timeout (0);
473 nat64_set_tcp_timeouts (0, 0, 0);
477 error = clib_error_return (0, "unknown input '%U'",
478 format_unformat_error, line_input);
484 unformat_free (line_input);
489 static clib_error_t *
490 nat64_show_timeouts_command_fn (vlib_main_t * vm, unformat_input_t * input,
491 vlib_cli_command_t * cmd)
493 vlib_cli_output (vm, "NAT64 session timeouts:");
494 vlib_cli_output (vm, " UDP %usec", nat64_get_udp_timeout ());
495 vlib_cli_output (vm, " ICMP %usec", nat64_get_icmp_timeout ());
496 vlib_cli_output (vm, " TCP transitory %usec",
497 nat64_get_tcp_trans_timeout ());
498 vlib_cli_output (vm, " TCP established %usec",
499 nat64_get_tcp_est_timeout ());
500 vlib_cli_output (vm, " TCP incoming SYN %usec",
501 nat64_get_tcp_incoming_syn_timeout ());
507 nat64_cli_st_walk (nat64_db_st_entry_t * ste, void *ctx)
509 vlib_main_t *vm = ctx;
510 nat64_main_t *nm = &nat64_main;
511 nat64_db_bib_entry_t *bibe;
514 bibe = nat64_db_bib_entry_by_index (&nm->db, ste->proto, ste->bibe_index);
518 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
522 u32 vrf_id = fib->ft_table_id;
524 if (ste->proto == SNAT_PROTOCOL_ICMP)
525 vlib_cli_output (vm, " %U %U %u %U %U %u %U vrf %u",
526 format_ip6_address, &bibe->in_addr,
527 format_ip6_address, &ste->in_r_addr,
528 clib_net_to_host_u16 (bibe->in_port),
529 format_ip4_address, &bibe->out_addr,
530 format_ip4_address, &ste->out_r_addr,
531 clib_net_to_host_u16 (bibe->out_port),
532 format_snat_protocol, bibe->proto, vrf_id);
534 vlib_cli_output (vm, " %U %u %U %u %U %u %U %u %U vrf %u",
535 format_ip6_address, &bibe->in_addr,
536 clib_net_to_host_u16 (bibe->in_port),
537 format_ip6_address, &ste->in_r_addr,
538 clib_net_to_host_u16 (ste->r_port),
539 format_ip4_address, &bibe->out_addr,
540 clib_net_to_host_u16 (bibe->out_port),
541 format_ip4_address, &ste->out_r_addr,
542 clib_net_to_host_u16 (ste->r_port),
543 format_snat_protocol, bibe->proto, vrf_id);
547 static clib_error_t *
548 nat64_show_st_command_fn (vlib_main_t * vm,
549 unformat_input_t * input, vlib_cli_command_t * cmd)
551 nat64_main_t *nm = &nat64_main;
552 unformat_input_t _line_input, *line_input = &_line_input;
553 clib_error_t *error = 0;
554 snat_protocol_t proto = 0;
556 if (!unformat_user (input, unformat_line_input, line_input))
559 if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
563 error = clib_error_return (0, "unknown input: '%U'",
564 format_unformat_error, line_input);
568 vlib_cli_output (vm, "NAT64 %U session table:", format_snat_protocol,
570 nat64_db_st_walk (&nm->db, proto, nat64_cli_st_walk, vm);
573 unformat_free (line_input);
580 VLIB_CLI_COMMAND (nat64_add_pool_address_command, static) = {
581 .path = "nat64 add pool address",
582 .short_help = "nat64 add pool address <ip4-range-start> [- <ip4-range-end>] "
583 "[tenant-vrf <vrf-id>] [del]",
584 .function = nat64_add_del_pool_addr_command_fn,
587 VLIB_CLI_COMMAND (show_nat64_pool_command, static) = {
588 .path = "show nat64 pool",
589 .short_help = "show nat64 pool",
590 .function = nat64_show_pool_command_fn,
593 VLIB_CLI_COMMAND (set_interface_nat64_command, static) = {
594 .path = "set interface nat64",
595 .short_help = "set interface nat64 in|out <intfc> [del]",
596 .function = nat64_interface_feature_command_fn,
599 VLIB_CLI_COMMAND (show_nat64_interfaces_command, static) = {
600 .path = "show nat64 interfaces",
601 .short_help = "show nat64 interfaces",
602 .function = nat64_show_interfaces_command_fn,
605 VLIB_CLI_COMMAND (nat64_add_del_static_bib_command, static) = {
606 .path = "nat64 add static bib",
607 .short_help = "nat64 add static bib <ip6-addr> <port> <ip4-addr> <port> "
608 "tcp|udp|icmp [vfr <table-id>] [del]",
609 .function = nat64_add_del_static_bib_command_fn,
612 VLIB_CLI_COMMAND (show_nat64_bib_command, static) = {
613 .path = "show nat64 bib",
614 .short_help = "show nat64 bib tcp|udp|icmp",
615 .function = nat64_show_bib_command_fn,
618 VLIB_CLI_COMMAND (set_nat64_timeouts_command, static) = {
619 .path = "set nat64 timeouts",
620 .short_help = "set nat64 timeouts udp <sec> icmp <sec> tcp-trans <sec> "
621 "tcp-est <sec> tcp-incoming-syn <sec> | reset",
622 .function = nat64_set_timeouts_command_fn,
625 VLIB_CLI_COMMAND (show_nat64_timeouts_command, static) = {
626 .path = "show nat64 tiemouts",
627 .short_help = "show nat64 tiemouts",
628 .function = nat64_show_timeouts_command_fn,
631 VLIB_CLI_COMMAND (show_nat64_st_command, static) = {
632 .path = "show nat64 session table",
633 .short_help = "show nat64 session table tcp|udp|icmp",
634 .function = nat64_show_st_command_fn,
640 * fd.io coding-style-patch-verification: ON
643 * eval: (c-set-style "gnu")