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