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