Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | * ip/ip4_cli.c: ip4 commands |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vnet/ip/ip.h> |
Klement Sekera | 896c896 | 2019-06-24 11:52:49 +0000 | [diff] [blame] | 41 | #include <vnet/ip/reass/ip4_full_reass.h> |
| 42 | #include <vnet/ip/reass/ip6_full_reass.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 44 | /** |
| 45 | * @file |
| 46 | * @brief Set IP Address. |
| 47 | * |
| 48 | * Configure an IPv4 or IPv6 address for on an interface. |
| 49 | */ |
| 50 | |
| 51 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 52 | int |
| 53 | ip4_address_compare (ip4_address_t * a1, ip4_address_t * a2) |
| 54 | { |
| 55 | return clib_net_to_host_u32 (a1->data_u32) - |
| 56 | clib_net_to_host_u32 (a2->data_u32); |
| 57 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 59 | int |
| 60 | ip6_address_compare (ip6_address_t * a1, ip6_address_t * a2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | { |
| 62 | int i; |
| 63 | for (i = 0; i < ARRAY_LEN (a1->as_u16); i++) |
| 64 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 65 | int cmp = |
| 66 | clib_net_to_host_u16 (a1->as_u16[i]) - |
| 67 | clib_net_to_host_u16 (a2->as_u16[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | if (cmp != 0) |
| 69 | return cmp; |
| 70 | } |
| 71 | return 0; |
| 72 | } |
| 73 | |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 74 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | VLIB_CLI_COMMAND (set_interface_ip_command, static) = { |
| 76 | .path = "set interface ip", |
| 77 | .short_help = "IP4/IP6 commands", |
| 78 | }; |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 79 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 81 | void |
| 82 | ip_del_all_interface_addresses (vlib_main_t * vm, u32 sw_if_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 84 | ip4_main_t *im4 = &ip4_main; |
| 85 | ip4_address_t *ip4_addrs = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | u32 *ip4_masks = 0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 87 | ip6_main_t *im6 = &ip6_main; |
| 88 | ip6_address_t *ip6_addrs = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | u32 *ip6_masks = 0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 90 | ip_interface_address_t *ia; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | int i; |
| 92 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 93 | /* *INDENT-OFF* */ |
| 94 | foreach_ip_interface_address (&im4->lookup_main, ia, sw_if_index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | 0 /* honor unnumbered */, |
| 96 | ({ |
| 97 | ip4_address_t * x = (ip4_address_t *) |
| 98 | ip_interface_address_get_address (&im4->lookup_main, ia); |
| 99 | vec_add1 (ip4_addrs, x[0]); |
| 100 | vec_add1 (ip4_masks, ia->address_length); |
| 101 | })); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 102 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 104 | /* *INDENT-OFF* */ |
| 105 | foreach_ip_interface_address (&im6->lookup_main, ia, sw_if_index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | 0 /* honor unnumbered */, |
| 107 | ({ |
| 108 | ip6_address_t * x = (ip6_address_t *) |
| 109 | ip_interface_address_get_address (&im6->lookup_main, ia); |
| 110 | vec_add1 (ip6_addrs, x[0]); |
| 111 | vec_add1 (ip6_masks, ia->address_length); |
| 112 | })); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 113 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | |
| 115 | for (i = 0; i < vec_len (ip4_addrs); i++) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 116 | ip4_add_del_interface_address (vm, sw_if_index, &ip4_addrs[i], |
| 117 | ip4_masks[i], 1 /* is_del */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | for (i = 0; i < vec_len (ip6_addrs); i++) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 119 | ip6_add_del_interface_address (vm, sw_if_index, &ip6_addrs[i], |
| 120 | ip6_masks[i], 1 /* is_del */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | |
| 122 | vec_free (ip4_addrs); |
| 123 | vec_free (ip4_masks); |
| 124 | vec_free (ip6_addrs); |
| 125 | vec_free (ip6_masks); |
| 126 | } |
| 127 | |
| 128 | static clib_error_t * |
Dave Barach | b8dca74 | 2016-07-01 12:38:11 -0400 | [diff] [blame] | 129 | ip_address_delete_cleanup (vnet_main_t * vnm, u32 hw_if_index, u32 is_create) |
| 130 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 131 | vlib_main_t *vm = vlib_get_main (); |
| 132 | vnet_hw_interface_t *hw; |
Dave Barach | b8dca74 | 2016-07-01 12:38:11 -0400 | [diff] [blame] | 133 | |
| 134 | if (is_create) |
| 135 | return 0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 136 | |
Dave Barach | b8dca74 | 2016-07-01 12:38:11 -0400 | [diff] [blame] | 137 | hw = vnet_get_hw_interface (vnm, hw_if_index); |
| 138 | |
| 139 | ip_del_all_interface_addresses (vm, hw->sw_if_index); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | VNET_HW_INTERFACE_ADD_DEL_FUNCTION (ip_address_delete_cleanup); |
| 144 | |
| 145 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | add_del_ip_address (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 147 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 149 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | ip4_address_t a4; |
| 151 | ip6_address_t a6; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 152 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | u32 sw_if_index, length, is_del; |
| 154 | |
| 155 | sw_if_index = ~0; |
| 156 | is_del = 0; |
| 157 | |
| 158 | if (unformat (input, "del")) |
| 159 | is_del = 1; |
| 160 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 161 | if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | { |
| 163 | error = clib_error_return (0, "unknown interface `%U'", |
| 164 | format_unformat_error, input); |
| 165 | goto done; |
| 166 | } |
| 167 | |
| 168 | if (is_del && unformat (input, "all")) |
| 169 | ip_del_all_interface_addresses (vm, sw_if_index); |
| 170 | else if (unformat (input, "%U/%d", unformat_ip4_address, &a4, &length)) |
| 171 | error = ip4_add_del_interface_address (vm, sw_if_index, &a4, length, |
| 172 | is_del); |
| 173 | else if (unformat (input, "%U/%d", unformat_ip6_address, &a6, &length)) |
| 174 | error = ip6_add_del_interface_address (vm, sw_if_index, &a6, length, |
| 175 | is_del); |
| 176 | else |
| 177 | { |
| 178 | error = clib_error_return (0, "expected IP4/IP6 address/length `%U'", |
| 179 | format_unformat_error, input); |
| 180 | goto done; |
| 181 | } |
| 182 | |
| 183 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 184 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | return error; |
| 186 | } |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 187 | |
Keith Burns (alagalah) | 6ef7bb9 | 2016-09-10 14:55:04 -0700 | [diff] [blame] | 188 | /*? |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 189 | * Add an IP Address to an interface or remove and IP Address from an interface. |
| 190 | * The IP Address can be an IPv4 or an IPv6 address. Interfaces may have multiple |
| 191 | * IPv4 and IPv6 addresses. There is no concept of primary vs. secondary |
| 192 | * interface addresses; they're just addresses. |
| 193 | * |
| 194 | * To display the addresses associated with a given interface, use the command |
| 195 | * '<em>show interface address <interface></em>'. |
| 196 | * |
| 197 | * Note that the debug CLI does not enforce classful mask-width / addressing |
| 198 | * constraints. |
Keith Burns (alagalah) | 6ef7bb9 | 2016-09-10 14:55:04 -0700 | [diff] [blame] | 199 | * |
| 200 | * @cliexpar |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 201 | * @parblock |
| 202 | * An example of how to add an IPv4 address to an interface: |
| 203 | * @cliexcmd{set interface ip address GigabitEthernet2/0/0 172.16.2.12/24} |
| 204 | * |
| 205 | * An example of how to add an IPv6 address to an interface: |
| 206 | * @cliexcmd{set interface ip address GigabitEthernet2/0/0 @::a:1:1:0:7/126} |
| 207 | * |
| 208 | * To delete a specific interface ip address: |
Benoît Ganne | 7bed48c | 2020-10-20 14:36:55 +0200 | [diff] [blame^] | 209 | * @cliexcmd{set interface ip address del GigabitEthernet2/0/0 172.16.2.12/24} |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 210 | * |
| 211 | * To delete all interfaces addresses (IPv4 and IPv6): |
Benoît Ganne | 7bed48c | 2020-10-20 14:36:55 +0200 | [diff] [blame^] | 212 | * @cliexcmd{set interface ip address del GigabitEthernet2/0/0 all} |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 213 | * @endparblock |
Keith Burns (alagalah) | 6ef7bb9 | 2016-09-10 14:55:04 -0700 | [diff] [blame] | 214 | ?*/ |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 215 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | VLIB_CLI_COMMAND (set_interface_ip_address_command, static) = { |
| 217 | .path = "set interface ip address", |
| 218 | .function = add_del_ip_address, |
Matej Klotton | e0cb0cc | 2017-02-03 14:48:18 +0100 | [diff] [blame] | 219 | .short_help = "set interface ip address [del] <interface> <ip-addr>/<mask> | [all]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | }; |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 221 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 222 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 223 | static clib_error_t * |
| 224 | set_reassembly_command_fn (vlib_main_t * vm, |
| 225 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 226 | { |
| 227 | vnet_main_t *vnm = vnet_get_main (); |
| 228 | unformat_input_t _line_input, *line_input = &_line_input; |
| 229 | u32 sw_if_index = ~0; |
| 230 | u8 ip4_on = 0; |
| 231 | u8 ip6_on = 0; |
| 232 | |
| 233 | /* Get a line of input. */ |
| 234 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 235 | { |
| 236 | return NULL; |
| 237 | } |
| 238 | |
Klement Sekera | 2926eca | 2018-04-17 17:48:30 +0200 | [diff] [blame] | 239 | if (!unformat_user |
| 240 | (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 241 | { |
| 242 | return clib_error_return (0, "Invalid interface name"); |
| 243 | } |
| 244 | |
Klement Sekera | 2926eca | 2018-04-17 17:48:30 +0200 | [diff] [blame] | 245 | if (unformat (line_input, "on")) |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 246 | { |
| 247 | ip4_on = 1; |
| 248 | ip6_on = 1; |
| 249 | } |
Klement Sekera | 2926eca | 2018-04-17 17:48:30 +0200 | [diff] [blame] | 250 | else if (unformat (line_input, "off")) |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 251 | { |
| 252 | ip4_on = 0; |
| 253 | ip6_on = 0; |
| 254 | } |
Klement Sekera | 2926eca | 2018-04-17 17:48:30 +0200 | [diff] [blame] | 255 | else if (unformat (line_input, "ip4")) |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 256 | { |
| 257 | ip4_on = 1; |
| 258 | ip6_on = 0; |
| 259 | } |
Klement Sekera | 2926eca | 2018-04-17 17:48:30 +0200 | [diff] [blame] | 260 | else if (unformat (line_input, "ip6")) |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 261 | { |
| 262 | ip4_on = 0; |
| 263 | ip6_on = 1; |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | return clib_error_return (0, "Unknown input `%U'", |
| 268 | format_unformat_error, line_input); |
| 269 | } |
| 270 | |
| 271 | |
Klement Sekera | 896c896 | 2019-06-24 11:52:49 +0000 | [diff] [blame] | 272 | vnet_api_error_t rv4 = ip4_full_reass_enable_disable (sw_if_index, ip4_on); |
| 273 | vnet_api_error_t rv6 = ip6_full_reass_enable_disable (sw_if_index, ip6_on); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 274 | if (rv4 && rv6) |
| 275 | { |
| 276 | return clib_error_return (0, |
Klement Sekera | 896c896 | 2019-06-24 11:52:49 +0000 | [diff] [blame] | 277 | "`ip4_full_reass_enable_disable' API call failed, rv=%d:%U, " |
| 278 | "`ip6_full_reass_enable_disable' API call failed, rv=%d:%U", |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 279 | (int) rv4, format_vnet_api_errno, rv4, |
| 280 | (int) rv6, format_vnet_api_errno, rv6); |
| 281 | } |
| 282 | else if (rv4) |
| 283 | { |
| 284 | return clib_error_return (0, |
Klement Sekera | 896c896 | 2019-06-24 11:52:49 +0000 | [diff] [blame] | 285 | "`ip4_full_reass_enable_disable' API call failed, rv=%d:%U", |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 286 | (int) rv4, format_vnet_api_errno, rv4); |
| 287 | } |
| 288 | else if (rv6) |
| 289 | { |
| 290 | return clib_error_return (0, |
Klement Sekera | 896c896 | 2019-06-24 11:52:49 +0000 | [diff] [blame] | 291 | "`ip6_full_reass_enable_disable' API call failed, rv=%d:%U", |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 292 | (int) rv6, format_vnet_api_errno, rv6); |
| 293 | } |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | /* *INDENT-OFF* */ |
| 298 | VLIB_CLI_COMMAND (set_reassembly_command, static) = { |
| 299 | .path = "set interface reassembly", |
| 300 | .short_help = "set interface reassembly <interface-name> [on|off|ip4|ip6]", |
| 301 | .function = set_reassembly_command_fn, |
| 302 | }; |
| 303 | /* *INDENT-ON* */ |
| 304 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | /* Dummy init function to get us linked in. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 306 | static clib_error_t * |
| 307 | ip4_cli_init (vlib_main_t * vm) |
| 308 | { |
| 309 | return 0; |
| 310 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | |
| 312 | VLIB_INIT_FUNCTION (ip4_cli_init); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 313 | |
| 314 | /* |
| 315 | * fd.io coding-style-patch-verification: ON |
| 316 | * |
| 317 | * Local Variables: |
| 318 | * eval: (c-set-style "gnu") |
| 319 | * End: |
| 320 | */ |