Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * decap.c : IPSec tunnel support |
| 3 | * |
| 4 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vnet/vnet.h> |
| 19 | #include <vnet/api_errno.h> |
| 20 | #include <vnet/ip/ip.h> |
| 21 | #include <vnet/interface.h> |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 22 | #include <vnet/fib/fib.h> |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 23 | #include <vnet/ipip/ipip.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | |
| 25 | #include <vnet/ipsec/ipsec.h> |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 26 | #include <vnet/ipsec/ipsec_tun.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | |
| 28 | static clib_error_t * |
| 29 | set_interface_spd_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 30 | unformat_input_t * input, |
| 31 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 33 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 35 | u32 sw_if_index = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | u32 spd_id; |
| 37 | int is_add = 1; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 38 | clib_error_t *error = NULL; |
Benoît Ganne | 40aa27e | 2020-11-06 14:14:23 +0100 | [diff] [blame] | 39 | int err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 41 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | return 0; |
| 43 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 44 | if (unformat |
| 45 | (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main, |
| 46 | &sw_if_index, &spd_id)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | ; |
| 48 | else if (unformat (line_input, "del")) |
| 49 | is_add = 0; |
| 50 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 51 | { |
| 52 | error = clib_error_return (0, "parse error: '%U'", |
| 53 | format_unformat_error, line_input); |
| 54 | goto done; |
| 55 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
Benoît Ganne | 40aa27e | 2020-11-06 14:14:23 +0100 | [diff] [blame] | 57 | err = ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add); |
| 58 | switch (err) |
| 59 | { |
| 60 | case VNET_API_ERROR_SYSCALL_ERROR_1: |
| 61 | error = clib_error_return (0, "no such spd-id"); |
| 62 | break; |
| 63 | case VNET_API_ERROR_SYSCALL_ERROR_2: |
| 64 | error = clib_error_return (0, "spd already assigned"); |
| 65 | break; |
| 66 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 68 | done: |
| 69 | unformat_free (line_input); |
| 70 | |
| 71 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | VLIB_CLI_COMMAND (set_interface_spd_command, static) = { |
| 75 | .path = "set interface ipsec spd", |
| 76 | .short_help = |
| 77 | "set interface ipsec spd <int> <id>", |
| 78 | .function = set_interface_spd_command_fn, |
| 79 | }; |
| 80 | |
| 81 | static clib_error_t * |
| 82 | ipsec_sa_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 83 | unformat_input_t * input, |
| 84 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 86 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 87 | ipsec_crypto_alg_t crypto_alg; |
| 88 | ipsec_integ_alg_t integ_alg; |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 89 | u32 anti_replay_window_size; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 90 | ipsec_protocol_t proto; |
| 91 | ipsec_sa_flags_t flags; |
| 92 | clib_error_t *error; |
Kingwel Xie | d3d1205 | 2019-03-07 06:34:30 -0500 | [diff] [blame] | 93 | ipsec_key_t ck = { 0 }; |
| 94 | ipsec_key_t ik = { 0 }; |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 95 | u32 id, spi, salt, sai; |
Mohammed Hawari | 048189e | 2021-02-05 19:04:42 +0100 | [diff] [blame] | 96 | int i = 0; |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 97 | u16 udp_src, udp_dst; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 98 | int is_add, rv; |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 99 | u32 m_args = 0; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 100 | tunnel_t tun = {}; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | |
Neale Ranns | 8dc75c0 | 2019-12-10 01:08:19 +0000 | [diff] [blame] | 102 | salt = 0; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 103 | error = NULL; |
| 104 | is_add = 0; |
| 105 | flags = IPSEC_SA_FLAG_NONE; |
| 106 | proto = IPSEC_PROTOCOL_ESP; |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 107 | anti_replay_window_size = 0; |
Neale Ranns | e6be702 | 2019-06-04 15:37:34 +0000 | [diff] [blame] | 108 | integ_alg = IPSEC_INTEG_ALG_NONE; |
| 109 | crypto_alg = IPSEC_CRYPTO_ALG_NONE; |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 110 | udp_src = udp_dst = IPSEC_UDP_PORT_NONE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 112 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 115 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 116 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 117 | if (unformat (line_input, "add %u", &id)) |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 118 | { |
| 119 | is_add = 1; |
| 120 | m_args |= 1 << 0; |
| 121 | } |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 122 | else if (unformat (line_input, "del %u", &id)) |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 123 | { |
| 124 | is_add = 0; |
| 125 | m_args |= 1 << 0; |
| 126 | } |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 127 | else if (unformat (line_input, "spi %u", &spi)) |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 128 | m_args |= 1 << 1; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 129 | else if (unformat (line_input, "salt 0x%x", &salt)) |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 130 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 131 | else if (unformat (line_input, "esp")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 132 | proto = IPSEC_PROTOCOL_ESP; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 133 | else if (unformat (line_input, "ah")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 134 | proto = IPSEC_PROTOCOL_AH; |
| 135 | else if (unformat (line_input, "crypto-key %U", |
| 136 | unformat_ipsec_key, &ck)) |
| 137 | ; |
| 138 | else if (unformat (line_input, "crypto-alg %U", |
| 139 | unformat_ipsec_crypto_alg, &crypto_alg)) |
| 140 | ; |
| 141 | else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik)) |
| 142 | ; |
| 143 | else if (unformat (line_input, "integ-alg %U", |
| 144 | unformat_ipsec_integ_alg, &integ_alg)) |
| 145 | ; |
Damjan Marion | c50bcbd | 2022-05-08 20:48:37 +0200 | [diff] [blame] | 146 | else if (unformat (line_input, "%U", unformat_tunnel, &tun)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 147 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 148 | flags |= IPSEC_SA_FLAG_IS_TUNNEL; |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 149 | if (AF_IP6 == tunnel_get_af (&tun)) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 150 | flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 151 | } |
Mohammed Hawari | 048189e | 2021-02-05 19:04:42 +0100 | [diff] [blame] | 152 | else if (unformat (line_input, "udp-src-port %d", &i)) |
| 153 | udp_src = i; |
| 154 | else if (unformat (line_input, "udp-dst-port %d", &i)) |
| 155 | udp_dst = i; |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 156 | else if (unformat (line_input, "anti-replay-size %d", |
| 157 | &anti_replay_window_size)) |
| 158 | flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY; |
Christian Hopps | 9997538 | 2020-07-17 09:53:18 -0400 | [diff] [blame] | 159 | else if (unformat (line_input, "inbound")) |
| 160 | flags |= IPSEC_SA_FLAG_IS_INBOUND; |
| 161 | else if (unformat (line_input, "use-anti-replay")) |
| 162 | flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY; |
| 163 | else if (unformat (line_input, "use-esn")) |
| 164 | flags |= IPSEC_SA_FLAG_USE_ESN; |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 165 | else if (unformat (line_input, "udp-encap")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 166 | flags |= IPSEC_SA_FLAG_UDP_ENCAP; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 167 | else if (unformat (line_input, "async")) |
| 168 | flags |= IPSEC_SA_FLAG_IS_ASYNC; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 169 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 170 | { |
| 171 | error = clib_error_return (0, "parse error: '%U'", |
| 172 | format_unformat_error, line_input); |
| 173 | goto done; |
| 174 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 175 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 177 | if (!(m_args & 1)) |
| 178 | { |
| 179 | error = clib_error_return (0, "missing id"); |
| 180 | goto done; |
| 181 | } |
| 182 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 183 | if (is_add) |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 184 | { |
| 185 | if (!(m_args & 2)) |
| 186 | { |
| 187 | error = clib_error_return (0, "missing spi"); |
| 188 | goto done; |
| 189 | } |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 190 | rv = |
| 191 | ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, &ck, integ_alg, &ik, |
| 192 | flags, clib_host_to_net_u32 (salt), udp_src, |
| 193 | udp_dst, anti_replay_window_size, &tun, &sai); |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 194 | } |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 195 | else |
Benoît Ganne | bdd8b57 | 2020-07-28 15:56:15 +0200 | [diff] [blame] | 196 | { |
| 197 | rv = ipsec_sa_unlock_id (id); |
| 198 | } |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 199 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 200 | if (rv) |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 201 | error = clib_error_return (0, "failed: %d", rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 203 | done: |
| 204 | unformat_free (line_input); |
| 205 | |
| 206 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = { |
| 210 | .path = "ipsec sa", |
| 211 | .short_help = |
| 212 | "ipsec sa [add|del]", |
| 213 | .function = ipsec_sa_add_del_command_fn, |
| 214 | }; |
| 215 | |
| 216 | static clib_error_t * |
Maxime Peim | 1271e3a | 2023-03-20 14:13:56 +0000 | [diff] [blame] | 217 | ipsec_sa_bind_cli (vlib_main_t *vm, unformat_input_t *input, |
| 218 | vlib_cli_command_t *cmd) |
| 219 | { |
| 220 | unformat_input_t _line_input, *line_input = &_line_input; |
| 221 | u32 id = ~0; |
| 222 | u32 worker = ~0; |
| 223 | bool bind = 1; |
| 224 | int rv; |
| 225 | clib_error_t *error = NULL; |
| 226 | |
| 227 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 228 | return 0; |
| 229 | |
| 230 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 231 | { |
| 232 | if (unformat (line_input, "unbind")) |
| 233 | bind = 0; |
Maxime Peim | f902ba5 | 2023-07-03 09:42:58 +0200 | [diff] [blame] | 234 | else if (id == ~0 && unformat (line_input, "%u", &id)) |
Maxime Peim | 1271e3a | 2023-03-20 14:13:56 +0000 | [diff] [blame] | 235 | ; |
| 236 | else if (unformat (line_input, "%u", &worker)) |
| 237 | ; |
| 238 | else |
| 239 | { |
| 240 | error = clib_error_return (0, "parse error: '%U'", |
| 241 | format_unformat_error, line_input); |
| 242 | goto done; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (id == ~0) |
| 247 | { |
| 248 | error = clib_error_return (0, "please specify SA ID"); |
| 249 | goto done; |
| 250 | } |
| 251 | |
| 252 | if (bind && ~0 == worker) |
| 253 | { |
| 254 | error = clib_error_return (0, "please specify worker to bind to"); |
| 255 | goto done; |
| 256 | } |
| 257 | |
| 258 | rv = ipsec_sa_bind (id, worker, bind); |
| 259 | switch (rv) |
| 260 | { |
| 261 | case VNET_API_ERROR_INVALID_VALUE: |
| 262 | error = clib_error_return (0, "please specify a valid SA ID"); |
| 263 | break; |
| 264 | case VNET_API_ERROR_INVALID_WORKER: |
| 265 | error = clib_error_return (0, "please specify a valid worker index"); |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | done: |
| 270 | unformat_free (line_input); |
| 271 | |
| 272 | return error; |
| 273 | } |
| 274 | |
| 275 | VLIB_CLI_COMMAND (ipsec_sa_bind_cmd, static) = { |
| 276 | .path = "ipsec sa bind", |
| 277 | .short_help = "ipsec sa [unbind] <sa-id> <worker>", |
| 278 | .function = ipsec_sa_bind_cli, |
| 279 | }; |
| 280 | |
| 281 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | ipsec_spd_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 283 | unformat_input_t * input, |
| 284 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 286 | unformat_input_t _line_input, *line_input = &_line_input; |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 287 | u32 spd_id = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | int is_add = ~0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 289 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 290 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 291 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | return 0; |
| 293 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 294 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 295 | { |
| 296 | if (unformat (line_input, "add")) |
| 297 | is_add = 1; |
| 298 | else if (unformat (line_input, "del")) |
| 299 | is_add = 0; |
| 300 | else if (unformat (line_input, "%u", &spd_id)) |
| 301 | ; |
| 302 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 303 | { |
| 304 | error = clib_error_return (0, "parse error: '%U'", |
| 305 | format_unformat_error, line_input); |
| 306 | goto done; |
| 307 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 308 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 309 | |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 310 | if (spd_id == ~0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 311 | { |
| 312 | error = clib_error_return (0, "please specify SPD ID"); |
| 313 | goto done; |
| 314 | } |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 315 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 316 | ipsec_add_del_spd (vm, spd_id, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 318 | done: |
| 319 | unformat_free (line_input); |
| 320 | |
| 321 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = { |
| 325 | .path = "ipsec spd", |
| 326 | .short_help = |
| 327 | "ipsec spd [add|del] <id>", |
| 328 | .function = ipsec_spd_add_del_command_fn, |
| 329 | }; |
| 330 | |
| 331 | |
| 332 | static clib_error_t * |
| 333 | ipsec_policy_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 334 | unformat_input_t * input, |
| 335 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 336 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 337 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | ipsec_policy_t p; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 339 | int rv, is_add = 0; |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 340 | u32 tmp, tmp2, stat_index, local_range_set, remote_range_set; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 341 | clib_error_t *error = NULL; |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 342 | u32 is_outbound; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 343 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 344 | clib_memset (&p, 0, sizeof (p)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 345 | p.lport.stop = p.rport.stop = ~0; |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 346 | remote_range_set = local_range_set = is_outbound = 0; |
Piotr Bronowski | 815c6a4 | 2022-06-09 09:09:28 +0000 | [diff] [blame] | 347 | p.protocol = IPSEC_POLICY_PROTOCOL_ANY; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 348 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 349 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | return 0; |
| 351 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 352 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 353 | { |
| 354 | if (unformat (line_input, "add")) |
| 355 | is_add = 1; |
| 356 | else if (unformat (line_input, "del")) |
| 357 | is_add = 0; |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 358 | else if (unformat (line_input, "ip6")) |
| 359 | p.is_ipv6 = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 360 | else if (unformat (line_input, "spd %u", &p.id)) |
| 361 | ; |
| 362 | else if (unformat (line_input, "inbound")) |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 363 | is_outbound = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 364 | else if (unformat (line_input, "outbound")) |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 365 | is_outbound = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 366 | else if (unformat (line_input, "priority %d", &p.priority)) |
| 367 | ; |
| 368 | else if (unformat (line_input, "protocol %u", &tmp)) |
| 369 | p.protocol = (u8) tmp; |
| 370 | else |
| 371 | if (unformat |
| 372 | (line_input, "action %U", unformat_ipsec_policy_action, |
| 373 | &p.policy)) |
| 374 | { |
| 375 | if (p.policy == IPSEC_POLICY_ACTION_RESOLVE) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 376 | { |
| 377 | error = clib_error_return (0, "unsupported action: 'resolve'"); |
| 378 | goto done; |
| 379 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 380 | } |
| 381 | else if (unformat (line_input, "sa %u", &p.sa_id)) |
| 382 | ; |
| 383 | else if (unformat (line_input, "local-ip-range %U - %U", |
| 384 | unformat_ip4_address, &p.laddr.start.ip4, |
| 385 | unformat_ip4_address, &p.laddr.stop.ip4)) |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 386 | local_range_set = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 387 | else if (unformat (line_input, "remote-ip-range %U - %U", |
| 388 | unformat_ip4_address, &p.raddr.start.ip4, |
| 389 | unformat_ip4_address, &p.raddr.stop.ip4)) |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 390 | remote_range_set = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 391 | else if (unformat (line_input, "local-ip-range %U - %U", |
| 392 | unformat_ip6_address, &p.laddr.start.ip6, |
| 393 | unformat_ip6_address, &p.laddr.stop.ip6)) |
| 394 | { |
| 395 | p.is_ipv6 = 1; |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 396 | local_range_set = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 397 | } |
| 398 | else if (unformat (line_input, "remote-ip-range %U - %U", |
| 399 | unformat_ip6_address, &p.raddr.start.ip6, |
| 400 | unformat_ip6_address, &p.raddr.stop.ip6)) |
| 401 | { |
| 402 | p.is_ipv6 = 1; |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 403 | remote_range_set = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 404 | } |
| 405 | else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2)) |
| 406 | { |
| 407 | p.lport.start = tmp; |
| 408 | p.lport.stop = tmp2; |
| 409 | } |
| 410 | else |
| 411 | if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2)) |
| 412 | { |
| 413 | p.rport.start = tmp; |
| 414 | p.rport.stop = tmp2; |
| 415 | } |
| 416 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 417 | { |
| 418 | error = clib_error_return (0, "parse error: '%U'", |
| 419 | format_unformat_error, line_input); |
| 420 | goto done; |
| 421 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 422 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 423 | |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 424 | if (!remote_range_set) |
| 425 | { |
| 426 | if (p.is_ipv6) |
| 427 | clib_memset (&p.raddr.stop.ip6, 0xff, 16); |
| 428 | else |
| 429 | clib_memset (&p.raddr.stop.ip4, 0xff, 4); |
| 430 | } |
| 431 | if (!local_range_set) |
| 432 | { |
| 433 | if (p.is_ipv6) |
| 434 | clib_memset (&p.laddr.stop.ip6, 0xff, 16); |
| 435 | else |
| 436 | clib_memset (&p.laddr.stop.ip4, 0xff, 4); |
| 437 | } |
| 438 | |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 439 | rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type); |
| 440 | |
| 441 | if (rv) |
| 442 | { |
| 443 | error = clib_error_return (0, "unsupported policy type for:", |
| 444 | " outboud:%s %s action:%U", |
| 445 | (is_outbound ? "yes" : "no"), |
| 446 | (p.is_ipv6 ? "IPv4" : "IPv6"), |
| 447 | format_ipsec_policy_action, p.policy); |
| 448 | goto done; |
| 449 | } |
| 450 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 451 | rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index); |
| 452 | |
| 453 | if (!rv) |
| 454 | vlib_cli_output (vm, "policy-index:%d", stat_index); |
| 455 | else |
| 456 | vlib_cli_output (vm, "error:%d", rv); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 457 | |
| 458 | done: |
| 459 | unformat_free (line_input); |
| 460 | |
| 461 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = { |
| 465 | .path = "ipsec policy", |
| 466 | .short_help = |
| 467 | "ipsec policy [add|del] spd <id> priority <n> ", |
| 468 | .function = ipsec_policy_add_del_command_fn, |
| 469 | }; |
| 470 | |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 471 | static void |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 472 | ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 473 | { |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 474 | u32 sai; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 475 | |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 476 | pool_foreach_index (sai, ipsec_sa_pool) |
| 477 | { |
| 478 | vlib_cli_output (vm, "%U", format_ipsec_sa, sai, |
| 479 | (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF)); |
| 480 | } |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static void |
| 484 | ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im) |
| 485 | { |
| 486 | u32 spdi; |
| 487 | |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 488 | pool_foreach_index (spdi, im->spds) { |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 489 | vlib_cli_output(vm, "%U", format_ipsec_spd, spdi); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 490 | } |
Govindarajan Mohandoss | 6d7dfcb | 2021-03-19 19:20:49 +0000 | [diff] [blame] | 491 | |
Zachary Leaf | 7cd35f5 | 2021-06-25 08:11:15 -0500 | [diff] [blame] | 492 | if (im->output_flow_cache_flag) |
Govindarajan Mohandoss | 6d7dfcb | 2021-03-19 19:20:49 +0000 | [diff] [blame] | 493 | { |
Zachary Leaf | 7cd35f5 | 2021-06-25 08:11:15 -0500 | [diff] [blame] | 494 | vlib_cli_output (vm, "%U", format_ipsec_out_spd_flow_cache); |
| 495 | } |
| 496 | if (im->input_flow_cache_flag) |
| 497 | { |
| 498 | vlib_cli_output (vm, "%U", format_ipsec_in_spd_flow_cache); |
Govindarajan Mohandoss | 6d7dfcb | 2021-03-19 19:20:49 +0000 | [diff] [blame] | 499 | } |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | static void |
| 503 | ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im) |
| 504 | { |
| 505 | u32 spd_id, sw_if_index; |
Neale Ranns | d83c4a8 | 2019-06-14 06:48:27 -0700 | [diff] [blame] | 506 | ipsec_spd_t *spd; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 508 | vlib_cli_output (vm, "SPD Bindings:"); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 509 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 510 | hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({ |
Neale Ranns | d83c4a8 | 2019-06-14 06:48:27 -0700 | [diff] [blame] | 511 | spd = pool_elt_at_index (im->spds, spd_id); |
| 512 | vlib_cli_output (vm, " %d -> %U", spd->id, |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 513 | format_vnet_sw_if_index_name, im->vnet_main, |
| 514 | sw_if_index); |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 515 | })); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 516 | } |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 517 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 518 | static walk_rc_t |
| 519 | ipsec_tun_protect_show_one (index_t itpi, void *ctx) |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 520 | { |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 521 | vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 522 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 523 | return (WALK_CONTINUE); |
| 524 | } |
| 525 | |
| 526 | static void |
| 527 | ipsec_tunnel_show_all (vlib_main_t * vm) |
| 528 | { |
| 529 | ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | static clib_error_t * |
| 533 | show_ipsec_command_fn (vlib_main_t * vm, |
| 534 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 535 | { |
| 536 | ipsec_main_t *im = &ipsec_main; |
| 537 | |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 538 | ipsec_sa_show_all (vm, im, 0); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 539 | ipsec_spd_show_all (vm, im); |
| 540 | ipsec_spd_bindings_show_all (vm, im); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 541 | ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 542 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 543 | vlib_cli_output (vm, "IPSec async mode: %s", |
| 544 | (im->async_mode ? "on" : "off")); |
| 545 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | VLIB_CLI_COMMAND (show_ipsec_command, static) = { |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 550 | .path = "show ipsec all", |
| 551 | .short_help = "show ipsec all", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 552 | .function = show_ipsec_command_fn, |
| 553 | }; |
| 554 | |
| 555 | static clib_error_t * |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 556 | show_ipsec_sa_command_fn (vlib_main_t * vm, |
| 557 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 558 | { |
| 559 | ipsec_main_t *im = &ipsec_main; |
| 560 | u32 sai = ~0; |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 561 | u8 detail = 0; |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 562 | |
| 563 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 564 | { |
| 565 | if (unformat (input, "%u", &sai)) |
| 566 | ; |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 567 | if (unformat (input, "detail")) |
| 568 | detail = 1; |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 569 | else |
| 570 | break; |
| 571 | } |
| 572 | |
| 573 | if (~0 == sai) |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 574 | ipsec_sa_show_all (vm, im, detail); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 575 | else |
Christian E. Hopps | 01d61e7 | 2019-09-27 14:43:22 -0400 | [diff] [blame] | 576 | vlib_cli_output (vm, "%U", format_ipsec_sa, sai, |
| 577 | IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 582 | static clib_error_t * |
| 583 | clear_ipsec_sa_command_fn (vlib_main_t * vm, |
| 584 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 585 | { |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 586 | u32 sai = ~0; |
| 587 | |
| 588 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 589 | { |
| 590 | if (unformat (input, "%u", &sai)) |
| 591 | ; |
| 592 | else |
| 593 | break; |
| 594 | } |
| 595 | |
| 596 | if (~0 == sai) |
| 597 | { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 598 | pool_foreach_index (sai, ipsec_sa_pool) |
| 599 | { |
| 600 | ipsec_sa_clear (sai); |
| 601 | } |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 602 | } |
| 603 | else |
| 604 | { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 605 | if (pool_is_free_index (ipsec_sa_pool, sai)) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 606 | return clib_error_return (0, "unknown SA index: %d", sai); |
| 607 | else |
| 608 | ipsec_sa_clear (sai); |
| 609 | } |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 614 | VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = { |
| 615 | .path = "show ipsec sa", |
| 616 | .short_help = "show ipsec sa [index]", |
| 617 | .function = show_ipsec_sa_command_fn, |
| 618 | }; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 619 | |
| 620 | VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = { |
| 621 | .path = "clear ipsec sa", |
| 622 | .short_help = "clear ipsec sa [index]", |
| 623 | .function = clear_ipsec_sa_command_fn, |
| 624 | }; |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 625 | |
| 626 | static clib_error_t * |
| 627 | show_ipsec_spd_command_fn (vlib_main_t * vm, |
| 628 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 629 | { |
| 630 | ipsec_main_t *im = &ipsec_main; |
| 631 | u8 show_bindings = 0; |
| 632 | u32 spdi = ~0; |
| 633 | |
| 634 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 635 | { |
| 636 | if (unformat (input, "%u", &spdi)) |
| 637 | ; |
| 638 | else if (unformat (input, "bindings")) |
| 639 | show_bindings = 1; |
| 640 | else |
| 641 | break; |
| 642 | } |
| 643 | |
| 644 | if (show_bindings) |
| 645 | ipsec_spd_bindings_show_all (vm, im); |
| 646 | else if (~0 != spdi) |
| 647 | vlib_cli_output (vm, "%U", format_ipsec_spd, spdi); |
| 648 | else |
| 649 | ipsec_spd_show_all (vm, im); |
| 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 654 | VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = { |
| 655 | .path = "show ipsec spd", |
| 656 | .short_help = "show ipsec spd [index]", |
| 657 | .function = show_ipsec_spd_command_fn, |
| 658 | }; |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 659 | |
| 660 | static clib_error_t * |
| 661 | show_ipsec_tunnel_command_fn (vlib_main_t * vm, |
| 662 | unformat_input_t * input, |
| 663 | vlib_cli_command_t * cmd) |
| 664 | { |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 665 | ipsec_tunnel_show_all (vm); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 670 | VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = { |
| 671 | .path = "show ipsec tunnel", |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 672 | .short_help = "show ipsec tunnel", |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 673 | .function = show_ipsec_tunnel_command_fn, |
| 674 | }; |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 675 | |
| 676 | static clib_error_t * |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 677 | ipsec_show_backends_command_fn (vlib_main_t * vm, |
| 678 | unformat_input_t * input, |
| 679 | vlib_cli_command_t * cmd) |
| 680 | { |
| 681 | ipsec_main_t *im = &ipsec_main; |
| 682 | u32 verbose = 0; |
| 683 | |
| 684 | (void) unformat (input, "verbose %u", &verbose); |
| 685 | |
| 686 | vlib_cli_output (vm, "IPsec AH backends available:"); |
| 687 | u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active"); |
| 688 | ipsec_ah_backend_t *ab; |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 689 | pool_foreach (ab, im->ah_backends) { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 690 | s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends, |
| 691 | ab - im->ah_backends == im->ah_current_backend ? "yes" : "no"); |
| 692 | if (verbose) { |
| 693 | vlib_node_t *n; |
| 694 | n = vlib_get_node (vm, ab->ah4_encrypt_node_index); |
| 695 | s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index); |
| 696 | n = vlib_get_node (vm, ab->ah4_decrypt_node_index); |
| 697 | s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index); |
| 698 | n = vlib_get_node (vm, ab->ah6_encrypt_node_index); |
| 699 | s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index); |
| 700 | n = vlib_get_node (vm, ab->ah6_decrypt_node_index); |
| 701 | s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index); |
| 702 | } |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 703 | } |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 704 | vlib_cli_output (vm, "%v", s); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 705 | vec_set_len (s, 0); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 706 | vlib_cli_output (vm, "IPsec ESP backends available:"); |
| 707 | s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active"); |
| 708 | ipsec_esp_backend_t *eb; |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 709 | pool_foreach (eb, im->esp_backends) { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 710 | s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends, |
| 711 | eb - im->esp_backends == im->esp_current_backend ? "yes" |
| 712 | : "no"); |
| 713 | if (verbose) { |
| 714 | vlib_node_t *n; |
| 715 | n = vlib_get_node (vm, eb->esp4_encrypt_node_index); |
| 716 | s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index); |
| 717 | n = vlib_get_node (vm, eb->esp4_decrypt_node_index); |
| 718 | s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index); |
| 719 | n = vlib_get_node (vm, eb->esp6_encrypt_node_index); |
| 720 | s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index); |
| 721 | n = vlib_get_node (vm, eb->esp6_decrypt_node_index); |
| 722 | s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index); |
| 723 | } |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 724 | } |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 725 | vlib_cli_output (vm, "%v", s); |
| 726 | |
| 727 | vec_free (s); |
| 728 | return 0; |
| 729 | } |
| 730 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 731 | VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = { |
| 732 | .path = "show ipsec backends", |
| 733 | .short_help = "show ipsec backends", |
| 734 | .function = ipsec_show_backends_command_fn, |
| 735 | }; |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 736 | |
| 737 | static clib_error_t * |
| 738 | ipsec_select_backend_command_fn (vlib_main_t * vm, |
| 739 | unformat_input_t * input, |
| 740 | vlib_cli_command_t * cmd) |
| 741 | { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 742 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 743 | ipsec_main_t *im = &ipsec_main; |
| 744 | clib_error_t *error; |
| 745 | u32 backend_index; |
| 746 | |
| 747 | error = ipsec_rsc_in_use (im); |
| 748 | |
| 749 | if (error) |
| 750 | return error; |
| 751 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 752 | /* Get a line of input. */ |
| 753 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 754 | return 0; |
| 755 | |
| 756 | if (unformat (line_input, "ah")) |
| 757 | { |
| 758 | if (unformat (line_input, "%u", &backend_index)) |
| 759 | { |
| 760 | if (ipsec_select_ah_backend (im, backend_index) < 0) |
| 761 | { |
| 762 | return clib_error_return (0, "Invalid AH backend index `%u'", |
| 763 | backend_index); |
| 764 | } |
| 765 | } |
| 766 | else |
| 767 | { |
| 768 | return clib_error_return (0, "Invalid backend index `%U'", |
| 769 | format_unformat_error, line_input); |
| 770 | } |
| 771 | } |
| 772 | else if (unformat (line_input, "esp")) |
| 773 | { |
| 774 | if (unformat (line_input, "%u", &backend_index)) |
| 775 | { |
| 776 | if (ipsec_select_esp_backend (im, backend_index) < 0) |
| 777 | { |
| 778 | return clib_error_return (0, "Invalid ESP backend index `%u'", |
| 779 | backend_index); |
| 780 | } |
| 781 | } |
| 782 | else |
| 783 | { |
| 784 | return clib_error_return (0, "Invalid backend index `%U'", |
| 785 | format_unformat_error, line_input); |
| 786 | } |
| 787 | } |
| 788 | else |
| 789 | { |
| 790 | return clib_error_return (0, "Unknown input `%U'", |
| 791 | format_unformat_error, line_input); |
| 792 | } |
| 793 | |
| 794 | return 0; |
| 795 | } |
| 796 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 797 | VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = { |
| 798 | .path = "ipsec select backend", |
| 799 | .short_help = "ipsec select backend <ah|esp> <backend index>", |
| 800 | .function = ipsec_select_backend_command_fn, |
| 801 | }; |
| 802 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 803 | |
| 804 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 805 | clear_ipsec_counters_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 806 | unformat_input_t * input, |
| 807 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 808 | { |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 809 | vlib_clear_combined_counters (&ipsec_spd_policy_counters); |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 810 | vlib_clear_combined_counters (&ipsec_sa_counters); |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 811 | for (int i = 0; i < IPSEC_SA_N_ERRORS; i++) |
| 812 | vlib_clear_simple_counters (&ipsec_sa_err_counters[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 813 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 814 | return (NULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = { |
| 818 | .path = "clear ipsec counters", |
| 819 | .short_help = "clear ipsec counters", |
| 820 | .function = clear_ipsec_counters_command_fn, |
| 821 | }; |
| 822 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 823 | static clib_error_t * |
| 824 | ipsec_tun_protect_cmd (vlib_main_t * vm, |
| 825 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 826 | { |
| 827 | unformat_input_t _line_input, *line_input = &_line_input; |
| 828 | u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 829 | ip_address_t peer = { }; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 830 | vnet_main_t *vnm; |
| 831 | |
| 832 | is_del = 0; |
| 833 | sw_if_index = ~0; |
| 834 | vnm = vnet_get_main (); |
| 835 | |
| 836 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 837 | return 0; |
| 838 | |
| 839 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 840 | { |
| 841 | if (unformat (line_input, "del")) |
| 842 | is_del = 1; |
| 843 | else if (unformat (line_input, "add")) |
| 844 | is_del = 0; |
| 845 | else if (unformat (line_input, "sa-in %d", &sa_in)) |
| 846 | vec_add1 (sa_ins, sa_in); |
| 847 | else if (unformat (line_input, "sa-out %d", &sa_out)) |
| 848 | ; |
| 849 | else if (unformat (line_input, "%U", |
| 850 | unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 851 | ; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 852 | else if (unformat (line_input, "%U", unformat_ip_address, &peer)) |
| 853 | ; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 854 | else |
| 855 | return (clib_error_return (0, "unknown input '%U'", |
| 856 | format_unformat_error, line_input)); |
| 857 | } |
| 858 | |
| 859 | if (!is_del) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 860 | ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins); |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 861 | else |
| 862 | ipsec_tun_protect_del (sw_if_index, &peer); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 863 | |
| 864 | unformat_free (line_input); |
| 865 | return NULL; |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * Protect tunnel with IPSEC |
| 870 | */ |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 871 | VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) = |
| 872 | { |
| 873 | .path = "ipsec tunnel protect", |
| 874 | .function = ipsec_tun_protect_cmd, |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 875 | .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA> [add|del]", |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 876 | // this is not MP safe |
| 877 | }; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 878 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 879 | |
| 880 | static clib_error_t * |
| 881 | ipsec_tun_protect_show (vlib_main_t * vm, |
| 882 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 883 | { |
| 884 | ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm); |
| 885 | |
| 886 | return NULL; |
| 887 | } |
| 888 | |
| 889 | /** |
| 890 | * show IPSEC tunnel protection |
| 891 | */ |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 892 | VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) = |
| 893 | { |
| 894 | .path = "show ipsec protect", |
| 895 | .function = ipsec_tun_protect_show, |
| 896 | .short_help = "show ipsec protect", |
| 897 | }; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 898 | |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 899 | static int |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 900 | ipsec_tun_protect4_hash_show_one (clib_bihash_kv_8_16_t * kv, void *arg) |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 901 | { |
| 902 | ipsec4_tunnel_kv_t *ikv = (ipsec4_tunnel_kv_t *) kv; |
| 903 | vlib_main_t *vm = arg; |
| 904 | |
| 905 | vlib_cli_output (vm, " %U", format_ipsec4_tunnel_kv, ikv); |
| 906 | |
| 907 | return (BIHASH_WALK_CONTINUE); |
| 908 | } |
| 909 | |
| 910 | static int |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 911 | ipsec_tun_protect6_hash_show_one (clib_bihash_kv_24_16_t * kv, void *arg) |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 912 | { |
| 913 | ipsec6_tunnel_kv_t *ikv = (ipsec6_tunnel_kv_t *) kv; |
| 914 | vlib_main_t *vm = arg; |
| 915 | |
| 916 | vlib_cli_output (vm, " %U", format_ipsec6_tunnel_kv, ikv); |
| 917 | |
| 918 | return (BIHASH_WALK_CONTINUE); |
| 919 | } |
| 920 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 921 | static clib_error_t * |
| 922 | ipsec_tun_protect_hash_show (vlib_main_t * vm, |
| 923 | unformat_input_t * input, |
| 924 | vlib_cli_command_t * cmd) |
| 925 | { |
| 926 | ipsec_main_t *im = &ipsec_main; |
| 927 | |
| 928 | { |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 929 | vlib_cli_output (vm, "IPv4:"); |
| 930 | |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 931 | clib_bihash_foreach_key_value_pair_8_16 |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 932 | (&im->tun4_protect_by_key, ipsec_tun_protect4_hash_show_one, vm); |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 933 | |
| 934 | vlib_cli_output (vm, "IPv6:"); |
| 935 | |
Neale Ranns | 302b25a | 2020-10-19 13:23:33 +0000 | [diff] [blame] | 936 | clib_bihash_foreach_key_value_pair_24_16 |
Neale Ranns | 7b4e52f | 2020-05-24 16:17:50 +0000 | [diff] [blame] | 937 | (&im->tun6_protect_by_key, ipsec_tun_protect6_hash_show_one, vm); |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | return NULL; |
| 941 | } |
| 942 | |
| 943 | /** |
| 944 | * show IPSEC tunnel protection hash tables |
| 945 | */ |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 946 | VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) = |
| 947 | { |
| 948 | .path = "show ipsec protect-hash", |
| 949 | .function = ipsec_tun_protect_hash_show, |
| 950 | .short_help = "show ipsec protect-hash", |
| 951 | }; |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 952 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 953 | clib_error_t * |
| 954 | ipsec_cli_init (vlib_main_t * vm) |
| 955 | { |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | VLIB_INIT_FUNCTION (ipsec_cli_init); |
| 960 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 961 | static clib_error_t * |
| 962 | set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 963 | vlib_cli_command_t * cmd) |
| 964 | { |
| 965 | unformat_input_t _line_input, *line_input = &_line_input; |
| 966 | int async_enable = 0; |
| 967 | |
| 968 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 969 | return 0; |
| 970 | |
| 971 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 972 | { |
| 973 | if (unformat (line_input, "on")) |
| 974 | async_enable = 1; |
| 975 | else if (unformat (line_input, "off")) |
| 976 | async_enable = 0; |
| 977 | else |
| 978 | return (clib_error_return (0, "unknown input '%U'", |
| 979 | format_unformat_error, line_input)); |
| 980 | } |
| 981 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 982 | ipsec_set_async_mode (async_enable); |
| 983 | |
| 984 | unformat_free (line_input); |
| 985 | return (NULL); |
| 986 | } |
| 987 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 988 | VLIB_CLI_COMMAND (set_async_mode_command, static) = { |
| 989 | .path = "set ipsec async mode", |
| 990 | .short_help = "set ipsec async mode on|off", |
| 991 | .function = set_async_mode_command_fn, |
| 992 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 993 | |
| 994 | /* |
| 995 | * fd.io coding-style-patch-verification: ON |
| 996 | * |
| 997 | * Local Variables: |
| 998 | * eval: (c-set-style "gnu") |
| 999 | * End: |
| 1000 | */ |