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:
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 <nat/nat66.h>
22 #include <vnet/fib/fib_table.h>
25 nat66_interface_feature_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 vnet_main_t *vnm = vnet_get_main ();
31 clib_error_t *error = 0;
33 u32 *inside_sw_if_indices = 0;
34 u32 *outside_sw_if_indices = 0;
38 /* Get a line of input. */
39 if (!unformat_user (input, unformat_line_input, line_input))
42 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
44 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
46 vec_add1 (inside_sw_if_indices, sw_if_index);
47 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
49 vec_add1 (outside_sw_if_indices, sw_if_index);
50 else if (unformat (line_input, "del"))
54 error = clib_error_return (0, "unknown input '%U'",
55 format_unformat_error, line_input);
60 if (vec_len (inside_sw_if_indices))
62 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
64 sw_if_index = inside_sw_if_indices[i];
65 rv = nat66_interface_add_del (sw_if_index, 1, is_add);
68 case VNET_API_ERROR_NO_SUCH_ENTRY:
70 clib_error_return (0, "%U NAT66 feature not enabled.",
71 format_vnet_sw_interface_name, vnm,
72 vnet_get_sw_interface (vnm, sw_if_index));
74 case VNET_API_ERROR_VALUE_EXIST:
76 clib_error_return (0, "%U NAT66 feature already enabled.",
77 format_vnet_sw_interface_name, vnm,
78 vnet_get_sw_interface (vnm, sw_if_index));
80 case VNET_API_ERROR_INVALID_VALUE:
81 case VNET_API_ERROR_INVALID_VALUE_2:
84 "%U NAT66 feature enable/disable failed.",
85 format_vnet_sw_interface_name, vnm,
86 vnet_get_sw_interface (vnm, sw_if_index));
95 if (vec_len (outside_sw_if_indices))
97 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
99 sw_if_index = outside_sw_if_indices[i];
100 rv = nat66_interface_add_del (sw_if_index, 0, is_add);
103 case VNET_API_ERROR_NO_SUCH_ENTRY:
105 clib_error_return (0, "%U NAT66 feature not enabled.",
106 format_vnet_sw_interface_name, vnm,
107 vnet_get_sw_interface (vnm, sw_if_index));
109 case VNET_API_ERROR_VALUE_EXIST:
111 clib_error_return (0, "%U NAT66 feature already enabled.",
112 format_vnet_sw_interface_name, vnm,
113 vnet_get_sw_interface (vnm, sw_if_index));
115 case VNET_API_ERROR_INVALID_VALUE:
116 case VNET_API_ERROR_INVALID_VALUE_2:
118 clib_error_return (0,
119 "%U NAT66 feature enable/disable failed.",
120 format_vnet_sw_interface_name, vnm,
121 vnet_get_sw_interface (vnm, sw_if_index));
131 unformat_free (line_input);
132 vec_free (inside_sw_if_indices);
133 vec_free (outside_sw_if_indices);
139 nat66_cli_interface_walk (snat_interface_t * i, void *ctx)
141 vlib_main_t *vm = ctx;
142 vnet_main_t *vnm = vnet_get_main ();
144 vlib_cli_output (vm, " %U %s", format_vnet_sw_interface_name, vnm,
145 vnet_get_sw_interface (vnm, i->sw_if_index),
146 nat_interface_is_inside (i) ? "in" : "out");
150 static clib_error_t *
151 nat66_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
152 vlib_cli_command_t * cmd)
154 vlib_cli_output (vm, "NAT66 interfaces:");
155 nat66_interfaces_walk (nat66_cli_interface_walk, vm);
160 static clib_error_t *
161 nat66_add_del_static_mapping_command_fn (vlib_main_t * vm,
162 unformat_input_t * input,
163 vlib_cli_command_t * cmd)
165 unformat_input_t _line_input, *line_input = &_line_input;
166 clib_error_t *error = 0;
168 ip6_address_t l_addr, e_addr;
172 /* Get a line of input. */
173 if (!unformat_user (input, unformat_line_input, line_input))
176 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
178 if (unformat (line_input, "local %U external %U",
179 unformat_ip6_address, &l_addr,
180 unformat_ip6_address, &e_addr))
182 else if (unformat (line_input, "vrf %u", &vrf_id))
184 else if (unformat (line_input, "del"))
188 error = clib_error_return (0, "unknown input: '%U'",
189 format_unformat_error, line_input);
194 rv = nat66_static_mapping_add_del (&l_addr, &e_addr, vrf_id, is_add);
198 case VNET_API_ERROR_NO_SUCH_ENTRY:
199 error = clib_error_return (0, "NAT66 static mapping entry not exist.");
201 case VNET_API_ERROR_VALUE_EXIST:
202 error = clib_error_return (0, "NAT66 static mapping entry exist.");
209 unformat_free (line_input);
215 nat66_cli_static_mapping_walk (nat66_static_mapping_t * sm, void *ctx)
217 nat66_main_t *nm = &nat66_main;
218 vlib_main_t *vm = ctx;
222 fib = fib_table_get (sm->fib_index, FIB_PROTOCOL_IP6);
226 vlib_get_combined_counter (&nm->session_counters, sm - nm->sm, &vc);
228 vlib_cli_output (vm, " local %U external %U vrf %d",
229 format_ip6_address, &sm->l_addr,
230 format_ip6_address, &sm->e_addr, fib->ft_table_id);
231 vlib_cli_output (vm, " total pkts %lld, total bytes %lld", vc.packets,
237 static clib_error_t *
238 nat66_show_static_mappings_command_fn (vlib_main_t * vm,
239 unformat_input_t * input,
240 vlib_cli_command_t * cmd)
242 vlib_cli_output (vm, "NAT66 static mappings:");
243 nat66_static_mappings_walk (nat66_cli_static_mapping_walk, vm);
250 * @cliexstart{set interface nat66}
251 * Enable/disable NAT66 feature on the interface.
252 * To enable NAT66 feature with local (IPv6) network interface
253 * GigabitEthernet0/8/0 and external (IPv4) network interface
254 * GigabitEthernet0/a/0 use:
255 * vpp# set interface nat66 in GigabitEthernet0/8/0 out GigabitEthernet0/a/0
258 VLIB_CLI_COMMAND (set_interface_nat66_command, static) = {
259 .path = "set interface nat66",
260 .short_help = "set interface nat66 in|out <intfc> [del]",
261 .function = nat66_interface_feature_command_fn,
266 * @cliexstart{show nat66 interfaces}
267 * Show interfaces with NAT66 feature.
268 * To show interfaces with NAT66 feature use:
269 * vpp# show nat66 interfaces
271 * GigabitEthernet0/8/0 in
272 * GigabitEthernet0/a/0 out
275 VLIB_CLI_COMMAND (show_nat66_interfaces_command, static) = {
276 .path = "show nat66 interfaces",
277 .short_help = "show nat66 interfaces",
278 .function = nat66_show_interfaces_command_fn,
283 * @cliexstart{nat66 add static mapping}
284 * Add/delete NAT66 static mapping entry.
285 * To add NAT66 static mapping entry use:
286 * vpp# nat66 add static mapping local fd01:1::4 external 2001:db8:c000:223::
287 * vpp# nat66 add static mapping local fd01:1::2 external 2001:db8:c000:221:: vrf 10
290 VLIB_CLI_COMMAND (show_nat66_add_del_static_mapping_command, static) = {
291 .path = "nat66 add static mapping",
292 .short_help = "nat66 add static mapping local <ip6-addr> external <ip6-addr>"
293 " [vfr <table-id>] [del]",
294 .function = nat66_add_del_static_mapping_command_fn,
299 * @cliexstart{show nat66 static mappings}
300 * Show NAT66 static mappings.
301 * To show NAT66 static mappings use:
302 * vpp# show nat66 static mappings
303 * NAT66 static mappings:
304 * local fd01:1::4 external 2001:db8:c000:223:: vrf 0
305 * local fd01:1::2 external 2001:db8:c000:221:: vrf 10
308 VLIB_CLI_COMMAND (show_nat66_static_mappings_command, static) = {
309 .path = "show nat66 static mappings",
310 .short_help = "show nat66 static mappings",
311 .function = nat66_show_static_mappings_command_fn,
315 * fd.io coding-style-patch-verification: ON
318 * eval: (c-set-style "gnu")